import type { AIGNEObserver } from "@aigne/observability-api"; import type { Span } from "@opentelemetry/api"; import { Emitter } from "strict-event-emitter"; import { type Agent, type AgentHooks, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js"; import type { ChatModel } from "../agents/chat-model.js"; import type { ImageModel } from "../agents/image-model.js"; import { UserAgent } from "../agents/user-agent.js"; import type { Memory } from "../memory/memory.js"; import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js"; import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emitter.js"; import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js"; import type { AIGNEMetadata } from "./type.js"; import { type ContextLimits, type ContextUsage } from "./usage.js"; /** * @hidden */ export interface AgentEvent { parentContextId?: string; contextId: string; timestamp: number; agent: Agent; } /** * @hidden */ export interface ContextEventMap { agentStarted: [AgentEvent & { input: Message; taskTitle?: string; }]; agentSucceed: [AgentEvent & { output: Message; }]; agentFailed: [AgentEvent & { error: Error; }]; } /** * @hidden */ export type ContextEmitEventMap = { [K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement; }; /** * @hidden */ export interface InvokeOptions extends Partial, "context">> { returnActiveAgent?: boolean; returnProgressChunks?: boolean; returnMetadata?: boolean; disableTransfer?: boolean; sourceAgent?: Agent; /** * Whether to create a new context for this invocation. * If false, the invocation will use the current context. * * @default true */ newContext?: boolean; userContext?: U; memories?: Pick[]; } /** * @hidden */ export interface UserContext extends Record { userId?: string; sessionId?: string; } /** * @hidden */ export interface Context extends TypedEventEmitter { id: string; parentId?: string; rootId: string; model?: ChatModel; imageModel?: ImageModel; skills?: Agent[]; agents: Agent[]; observer?: AIGNEObserver; metadata?: AIGNEMetadata; span?: Span; usage: ContextUsage; limits?: ContextLimits; status?: "normal" | "timeout"; userContext: U; hooks?: AgentHooks[]; memories: Pick[]; /** * Create a user agent to consistently invoke an agent * @param agent Agent to invoke * @returns User agent */ invoke(agent: Agent): UserAgent; /** * Invoke an agent with a message and return the output and the active agent * @param agent Agent to invoke * @param message Message to pass to the agent * @param options.returnActiveAgent return the active agent * @param options.streaming return a stream of the output * @returns the output of the agent and the final active agent */ invoke(agent: Agent, message: I & Message, options: InvokeOptions & { returnActiveAgent: true; streaming?: false; }): Promise<[O, Agent]>; invoke(agent: Agent, message: I & Message, options: InvokeOptions & { returnActiveAgent: true; streaming: true; }): Promise<[AgentResponseStream, Promise]>; /** * Invoke an agent with a message * @param agent Agent to invoke * @param message Message to pass to the agent * @returns the output of the agent */ invoke(agent: Agent, message: I & Message, options?: InvokeOptions & { streaming?: false; }): Promise; invoke(agent: Agent, message: I & Message, options: InvokeOptions & { streaming: true; }): Promise>; invoke(agent: Agent, message?: I & Message, options?: InvokeOptions): UserAgent | Promise | [AgentResponse, Agent]>; /** * Publish a message to a topic, the aigne will invoke the listeners of the topic * @param topic topic name, or an array of topic names * @param payload message to publish */ publish(topic: string | string[], payload: Omit | Message, options?: InvokeOptions): void; subscribe(topic: string | string[], listener?: undefined): Promise; subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe; subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise; subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise; unsubscribe(topic: string | string[], listener: MessageQueueListener): void; /** * Create a child context with the same configuration as the parent context. * If `reset` is true, the child context will have a new state (such as: usage). * * @param options * @param options.reset create a new context with initial state (such as: usage) * @returns new context */ newContext(options?: { reset?: boolean; }): Context; } /** * @hidden */ export declare class AIGNEContext implements Context { constructor(parent?: ConstructorParameters[0], { reset }?: { reset?: boolean; }); id: string; parentId?: string; rootId: string; span?: Span; readonly internal: AIGNEContextShared; get messageQueue(): MessageQueue; get model(): ChatModel | undefined; get imageModel(): ImageModel | undefined; get skills(): Agent[] | undefined; get agents(): Agent[]; get observer(): AIGNEObserver | undefined; get limits(): ContextLimits | undefined; get status(): "normal" | "timeout"; get usage(): ContextUsage; get userContext(): Context["userContext"]; set userContext(userContext: Context["userContext"]); get memories(): Context["memories"]; set memories(memories: Context["memories"]); get hooks(): AgentHooks[]; set hooks(hooks: AgentHooks[]); newContext({ reset }?: { reset?: boolean; }): AIGNEContext; invoke: Context["invoke"]; private onInvocationResult; private processOptions; publish: Context["publish"]; subscribe: Context["subscribe"]; unsubscribe: Context["unsubscribe"]; emit(eventName: K, ...args: Args): boolean; private trace; on(eventName: K, listener: Listener): this; once(eventName: K, listener: Listener): this; off(eventName: K, listener: Listener): this; } declare class AIGNEContextShared { private readonly parent?; constructor(parent?: (Pick & { messageQueue?: MessageQueue; events?: Emitter; contextIds?: Set; }) | undefined); readonly messageQueue: MessageQueue; readonly events: Emitter; get model(): ChatModel | undefined; get imageModel(): ImageModel | undefined; get skills(): Agent[] | undefined; get agents(): Agent[]; get observer(): AIGNEObserver | undefined; get metadata(): AIGNEMetadata | undefined; get limits(): ContextLimits | undefined; usage: ContextUsage; contextIds: Set; userContext: Context["userContext"]; memories: Context["memories"]; hooks: AgentHooks[]; private abortController; private timer?; private initTimeout; get status(): "normal" | "timeout"; invoke(agent: Agent, input: I & Message, context: Context, options?: InvokeOptions): AgentProcessAsyncGenerator; private invokeAgent; } export {};