@aigne/core 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/lib/cjs/agents/agent.d.ts +5 -2
- package/lib/cjs/agents/agent.js +42 -24
- package/lib/cjs/agents/ai-agent.d.ts +8 -8
- package/lib/cjs/agents/ai-agent.js +5 -2
- package/lib/cjs/agents/mcp-agent.d.ts +11 -0
- package/lib/cjs/agents/mcp-agent.js +37 -23
- package/lib/cjs/agents/user-agent.d.ts +9 -9
- package/lib/cjs/agents/user-agent.js +26 -16
- package/lib/cjs/execution-engine/context.d.ts +84 -46
- package/lib/cjs/execution-engine/context.js +136 -98
- package/lib/cjs/execution-engine/execution-engine.d.ts +16 -47
- package/lib/cjs/execution-engine/execution-engine.js +13 -40
- package/lib/cjs/execution-engine/message-queue.d.ts +3 -3
- package/lib/cjs/execution-engine/message-queue.js +32 -2
- package/lib/cjs/execution-engine/usage.d.ts +11 -0
- package/lib/cjs/execution-engine/usage.js +10 -0
- package/lib/cjs/loader/index.js +1 -1
- package/lib/cjs/models/chat-model.d.ts +3 -2
- package/lib/cjs/models/chat-model.js +6 -5
- package/lib/cjs/models/claude-chat-model.js +10 -7
- package/lib/cjs/models/openai-chat-model.js +5 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -1
- package/lib/cjs/utils/json-schema.js +2 -2
- package/lib/cjs/utils/logger.d.ts +3 -15
- package/lib/cjs/utils/logger.js +3 -77
- package/lib/cjs/utils/mcp-utils.js +1 -5
- package/lib/cjs/utils/model-utils.js +2 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/typed-event-emtter.d.ts +10 -0
- package/lib/cjs/utils/typed-event-emtter.js +2 -0
- package/lib/dts/agents/agent.d.ts +5 -2
- package/lib/dts/agents/ai-agent.d.ts +8 -8
- package/lib/dts/agents/mcp-agent.d.ts +11 -0
- package/lib/dts/agents/user-agent.d.ts +9 -9
- package/lib/dts/execution-engine/context.d.ts +84 -46
- package/lib/dts/execution-engine/execution-engine.d.ts +16 -47
- package/lib/dts/execution-engine/message-queue.d.ts +3 -3
- package/lib/dts/execution-engine/usage.d.ts +11 -0
- package/lib/dts/models/chat-model.d.ts +3 -2
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/utils/logger.d.ts +3 -15
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/dts/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/agents/agent.d.ts +5 -2
- package/lib/esm/agents/agent.js +42 -24
- package/lib/esm/agents/ai-agent.d.ts +8 -8
- package/lib/esm/agents/ai-agent.js +5 -2
- package/lib/esm/agents/mcp-agent.d.ts +11 -0
- package/lib/esm/agents/mcp-agent.js +38 -24
- package/lib/esm/agents/user-agent.d.ts +9 -9
- package/lib/esm/agents/user-agent.js +26 -16
- package/lib/esm/execution-engine/context.d.ts +84 -46
- package/lib/esm/execution-engine/context.js +135 -98
- package/lib/esm/execution-engine/execution-engine.d.ts +16 -47
- package/lib/esm/execution-engine/execution-engine.js +14 -38
- package/lib/esm/execution-engine/message-queue.d.ts +3 -3
- package/lib/esm/execution-engine/message-queue.js +33 -3
- package/lib/esm/execution-engine/usage.d.ts +11 -0
- package/lib/esm/execution-engine/usage.js +7 -0
- package/lib/esm/loader/index.js +2 -2
- package/lib/esm/models/chat-model.d.ts +3 -2
- package/lib/esm/models/chat-model.js +6 -5
- package/lib/esm/models/claude-chat-model.js +10 -7
- package/lib/esm/models/openai-chat-model.js +5 -2
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -1
- package/lib/esm/utils/json-schema.js +2 -2
- package/lib/esm/utils/logger.d.ts +3 -15
- package/lib/esm/utils/logger.js +3 -77
- package/lib/esm/utils/mcp-utils.js +1 -5
- package/lib/esm/utils/model-utils.js +2 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/utils/typed-event-emtter.js +1 -0
- package/package.json +7 -8
- package/lib/cjs/utils/run-chat-loop.d.ts +0 -11
- package/lib/cjs/utils/run-chat-loop.js +0 -82
- package/lib/dts/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.js +0 -76
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
-
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
2
|
+
import { SSEClientTransport, } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
3
3
|
import { StdioClientTransport, getDefaultEnvironment, } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
4
|
import { UriTemplate } from "@modelcontextprotocol/sdk/shared/uriTemplate.js";
|
|
5
5
|
import pRetry from "p-retry";
|
|
@@ -11,7 +11,6 @@ import { Agent } from "./agent.js";
|
|
|
11
11
|
const MCP_AGENT_CLIENT_NAME = "MCPAgent";
|
|
12
12
|
const MCP_AGENT_CLIENT_VERSION = "0.0.1";
|
|
13
13
|
const DEFAULT_MAX_RECONNECTS = 10;
|
|
14
|
-
const debug = logger.base.extend("mcp");
|
|
15
14
|
function isSSEServerParameters(options) {
|
|
16
15
|
return "url" in options && typeof options.url === "string";
|
|
17
16
|
}
|
|
@@ -31,7 +30,7 @@ export class MCPAgent extends Agent {
|
|
|
31
30
|
static from(options) {
|
|
32
31
|
checkArguments("MCPAgent.from", mcpAgentOptionsSchema, options);
|
|
33
32
|
if (isSSEServerParameters(options)) {
|
|
34
|
-
const transport = () => new SSEClientTransport(new URL(options.url));
|
|
33
|
+
const transport = () => new SSEClientTransport(new URL(options.url), options.opts);
|
|
35
34
|
return MCPAgent.fromTransport(transport, options);
|
|
36
35
|
}
|
|
37
36
|
if (isStdioServerParameters(options)) {
|
|
@@ -53,29 +52,36 @@ export class MCPAgent extends Agent {
|
|
|
53
52
|
version: MCP_AGENT_CLIENT_VERSION,
|
|
54
53
|
}, undefined, isSSEServerParameters(options) ? { transportCreator, ...options } : undefined);
|
|
55
54
|
const transport = transportCreator();
|
|
56
|
-
|
|
55
|
+
logger.mcp(`Connecting to MCP server: ${getMCPServerString(options)}`);
|
|
56
|
+
await client.connect(transport);
|
|
57
57
|
const mcpServer = getMCPServerName(client);
|
|
58
58
|
const { tools: isToolsAvailable, prompts: isPromptsAvailable, resources: isResourcesAvailable, } = client.getServerCapabilities() ?? {};
|
|
59
|
+
logger.mcp(`Listing tools from ${mcpServer}`);
|
|
59
60
|
const tools = isToolsAvailable
|
|
60
|
-
? await
|
|
61
|
-
.
|
|
62
|
-
|
|
61
|
+
? await client.listTools().then(({ tools }) => {
|
|
62
|
+
logger.mcp(`Listing tools from ${mcpServer} completed %O`, tools?.map((i) => i.name));
|
|
63
|
+
return tools.map((tool) => toolFromMCPTool(tool, { client }));
|
|
64
|
+
})
|
|
63
65
|
: undefined;
|
|
66
|
+
logger.mcp(`Listing prompts from ${mcpServer}`);
|
|
64
67
|
const prompts = isPromptsAvailable
|
|
65
|
-
? await
|
|
66
|
-
.
|
|
67
|
-
|
|
68
|
+
? await client.listPrompts().then(({ prompts }) => {
|
|
69
|
+
logger.mcp(`Listing prompts from ${mcpServer} completed %O`, prompts?.map((i) => i.name));
|
|
70
|
+
return prompts.map((prompt) => promptFromMCPPrompt(prompt, { client }));
|
|
71
|
+
})
|
|
68
72
|
: undefined;
|
|
73
|
+
logger.mcp(`Listing resources from ${mcpServer}`);
|
|
74
|
+
// TODO: should conditionally call listResourceTemplates based on the server capabilities
|
|
75
|
+
// but the capability is not correct in the current SDK version
|
|
69
76
|
const resources = isResourcesAvailable
|
|
70
|
-
? await
|
|
71
|
-
.spinner(
|
|
72
|
-
// TODO: should conditionally call listResourceTemplates based on the server capabilities
|
|
73
|
-
// but the capability is not correct in the current SDK version
|
|
74
|
-
Promise.all([
|
|
77
|
+
? await Promise.all([
|
|
75
78
|
client.listResources().catch(() => ({ resources: [] })),
|
|
76
79
|
client.listResourceTemplates().catch(() => ({ resourceTemplates: [] })),
|
|
77
|
-
])
|
|
78
|
-
|
|
80
|
+
]).then(([{ resources }, { resourceTemplates }]) => {
|
|
81
|
+
const result = [...resources, ...resourceTemplates].map((resource) => resourceFromMCPResource(resource, { client }));
|
|
82
|
+
logger.mcp(`Listing resources from ${mcpServer} completed %O`, result.map((i) => i.name));
|
|
83
|
+
return result;
|
|
84
|
+
})
|
|
79
85
|
: undefined;
|
|
80
86
|
return new MCPAgent({
|
|
81
87
|
name: client.getServerVersion()?.name,
|
|
@@ -131,18 +137,22 @@ class ClientWithReconnect extends Client {
|
|
|
131
137
|
}, {
|
|
132
138
|
retries: this.reconnectOptions?.maxReconnects ?? DEFAULT_MAX_RECONNECTS,
|
|
133
139
|
shouldRetry: this.shouldReconnect,
|
|
134
|
-
onFailedAttempt: (error) =>
|
|
140
|
+
onFailedAttempt: (error) => logger.mcp("Reconnect attempt failed: %O", error),
|
|
135
141
|
});
|
|
136
142
|
}
|
|
137
143
|
async request(request, resultSchema, options) {
|
|
144
|
+
const mergedOptions = {
|
|
145
|
+
...(options ?? {}),
|
|
146
|
+
timeout: options?.timeout ?? this.reconnectOptions?.timeout ?? 10000,
|
|
147
|
+
};
|
|
138
148
|
try {
|
|
139
|
-
return await super.request(request, resultSchema,
|
|
149
|
+
return await super.request(request, resultSchema, mergedOptions);
|
|
140
150
|
}
|
|
141
151
|
catch (error) {
|
|
142
152
|
if (this.shouldReconnect(error)) {
|
|
143
|
-
|
|
153
|
+
logger.mcp("Error occurred, reconnecting to MCP server: %O", error);
|
|
144
154
|
await this.reconnect();
|
|
145
|
-
return await super.request(request, resultSchema,
|
|
155
|
+
return await super.request(request, resultSchema, mergedOptions);
|
|
146
156
|
}
|
|
147
157
|
throw error;
|
|
148
158
|
}
|
|
@@ -160,13 +170,13 @@ export class MCPBase extends Agent {
|
|
|
160
170
|
}
|
|
161
171
|
export class MCPTool extends MCPBase {
|
|
162
172
|
async process(input) {
|
|
163
|
-
const result = await
|
|
173
|
+
const result = await this.client.callTool({ name: this.name, arguments: input });
|
|
164
174
|
return result;
|
|
165
175
|
}
|
|
166
176
|
}
|
|
167
177
|
export class MCPPrompt extends MCPBase {
|
|
168
178
|
async process(input) {
|
|
169
|
-
const result = await
|
|
179
|
+
const result = await this.client.getPrompt({ name: this.name, arguments: input });
|
|
170
180
|
return result;
|
|
171
181
|
}
|
|
172
182
|
}
|
|
@@ -178,7 +188,7 @@ export class MCPResource extends MCPBase {
|
|
|
178
188
|
uri;
|
|
179
189
|
async process(input) {
|
|
180
190
|
const uri = new UriTemplate(this.uri).expand(input);
|
|
181
|
-
const result = await
|
|
191
|
+
const result = await this.client.readResource({ uri });
|
|
182
192
|
return result;
|
|
183
193
|
}
|
|
184
194
|
}
|
|
@@ -197,6 +207,10 @@ const mcpAgentOptionsSchema = z.union([
|
|
|
197
207
|
}),
|
|
198
208
|
z.object({
|
|
199
209
|
url: z.string(),
|
|
210
|
+
opts: z.object({}).optional(),
|
|
211
|
+
timeout: z.number().optional(),
|
|
212
|
+
maxReconnects: z.number().optional(),
|
|
213
|
+
shouldReconnect: z.function().args(z.instanceof(Error)).returns(z.boolean()).optional(),
|
|
200
214
|
}),
|
|
201
215
|
z.object({
|
|
202
216
|
command: z.string(),
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
-
import type
|
|
3
|
-
import type { MessagePayload
|
|
2
|
+
import { type Context, type Runnable } from "../execution-engine/context.js";
|
|
3
|
+
import type { MessagePayload } from "../execution-engine/message-queue.js";
|
|
4
4
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
5
|
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
6
6
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
7
|
context: Context;
|
|
8
8
|
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
9
|
+
activeAgent?: Runnable;
|
|
9
10
|
}
|
|
10
11
|
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
11
12
|
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
12
13
|
constructor(options: UserAgentOptions<I, O>);
|
|
13
|
-
|
|
14
|
-
private get ctx();
|
|
14
|
+
context: Context;
|
|
15
15
|
private _process?;
|
|
16
|
+
private activeAgent?;
|
|
16
17
|
call(input: string | I, context?: Context): Promise<O>;
|
|
17
18
|
process(input: I, context: Context): Promise<O>;
|
|
18
|
-
publish
|
|
19
|
-
subscribe
|
|
20
|
-
|
|
21
|
-
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
22
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
19
|
+
publish: Context["publish"];
|
|
20
|
+
subscribe: Context["subscribe"];
|
|
21
|
+
unsubscribe: Context["unsubscribe"];
|
|
23
22
|
get stream(): ReadableStream<MessagePayload & {
|
|
24
23
|
topic: string;
|
|
25
24
|
}>;
|
|
25
|
+
protected checkUsageAgentCalls(_context: Context): void;
|
|
26
26
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
+
import { createPublishMessage } from "../execution-engine/context.js";
|
|
2
3
|
import { orArrayToArray } from "../utils/type-utils.js";
|
|
3
4
|
import { Agent } from "./agent.js";
|
|
4
5
|
export class UserAgent extends Agent {
|
|
@@ -6,40 +7,46 @@ export class UserAgent extends Agent {
|
|
|
6
7
|
return new UserAgent(options);
|
|
7
8
|
}
|
|
8
9
|
constructor(options) {
|
|
9
|
-
super({ ...options,
|
|
10
|
+
super({ ...options, disableEvents: true });
|
|
10
11
|
this._process = options.process;
|
|
11
12
|
this.context = options.context;
|
|
13
|
+
this.activeAgent = options.activeAgent;
|
|
12
14
|
}
|
|
13
15
|
context;
|
|
14
|
-
get ctx() {
|
|
15
|
-
if (!this.context)
|
|
16
|
-
throw new Error("UserAgent must have a context");
|
|
17
|
-
return this.context;
|
|
18
|
-
}
|
|
19
16
|
_process;
|
|
17
|
+
activeAgent;
|
|
20
18
|
call(input, context) {
|
|
19
|
+
if (!context)
|
|
20
|
+
this.context = this.context.newContext({ reset: true });
|
|
21
21
|
return super.call(input, context ?? this.context);
|
|
22
22
|
}
|
|
23
23
|
async process(input, context) {
|
|
24
24
|
if (this._process) {
|
|
25
25
|
return this._process(input, context);
|
|
26
26
|
}
|
|
27
|
+
if (this.activeAgent) {
|
|
28
|
+
const [output, agent] = await context.call(this.activeAgent, input, {
|
|
29
|
+
returnActiveAgent: true,
|
|
30
|
+
});
|
|
31
|
+
this.activeAgent = agent;
|
|
32
|
+
return output;
|
|
33
|
+
}
|
|
27
34
|
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
28
35
|
if (publicTopic?.length) {
|
|
29
|
-
context.publish(publicTopic, input, this);
|
|
36
|
+
context.publish(publicTopic, createPublishMessage(input, this));
|
|
30
37
|
return {};
|
|
31
38
|
}
|
|
32
39
|
throw new Error("UserAgent must have a process function or a publishTopic");
|
|
33
40
|
}
|
|
34
|
-
publish(
|
|
35
|
-
return this.
|
|
36
|
-
}
|
|
37
|
-
subscribe(
|
|
38
|
-
return this.
|
|
39
|
-
}
|
|
40
|
-
unsubscribe(
|
|
41
|
-
this.
|
|
42
|
-
}
|
|
41
|
+
publish = ((...args) => {
|
|
42
|
+
return this.context.publish(...args);
|
|
43
|
+
});
|
|
44
|
+
subscribe = ((...args) => {
|
|
45
|
+
return this.context.subscribe(...args);
|
|
46
|
+
});
|
|
47
|
+
unsubscribe = ((...args) => {
|
|
48
|
+
this.context.unsubscribe(...args);
|
|
49
|
+
});
|
|
43
50
|
get stream() {
|
|
44
51
|
let subscriptions = [];
|
|
45
52
|
return new ReadableStream({
|
|
@@ -56,4 +63,7 @@ export class UserAgent extends Agent {
|
|
|
56
63
|
},
|
|
57
64
|
});
|
|
58
65
|
}
|
|
66
|
+
checkUsageAgentCalls(_context) {
|
|
67
|
+
// ignore calls usage check for UserAgent
|
|
68
|
+
}
|
|
59
69
|
}
|
|
@@ -2,19 +2,36 @@ import EventEmitter from "node:events";
|
|
|
2
2
|
import { Agent, type FunctionAgentFn, type Message } from "../agents/agent.js";
|
|
3
3
|
import { UserAgent } from "../agents/user-agent.js";
|
|
4
4
|
import type { ChatModel } from "../models/chat-model.js";
|
|
5
|
+
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
|
+
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
5
7
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
8
|
+
import { type ContextLimits, type ContextUsage } from "./usage.js";
|
|
6
9
|
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
7
|
-
export interface
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
export interface AgentEvent {
|
|
11
|
+
parentContextId?: string;
|
|
12
|
+
contextId: string;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
agent: Agent;
|
|
11
15
|
}
|
|
12
|
-
export interface
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
export interface ContextEventMap {
|
|
17
|
+
agentStarted: [AgentEvent & {
|
|
18
|
+
input: Message;
|
|
19
|
+
}];
|
|
20
|
+
agentSucceed: [AgentEvent & {
|
|
21
|
+
output: Message;
|
|
22
|
+
}];
|
|
23
|
+
agentFailed: [AgentEvent & {
|
|
24
|
+
error: Error;
|
|
25
|
+
}];
|
|
16
26
|
}
|
|
17
|
-
export
|
|
27
|
+
export type ContextEmitEventMap = {
|
|
28
|
+
[K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
|
|
29
|
+
};
|
|
30
|
+
export interface CallOptions {
|
|
31
|
+
returnActiveAgent?: boolean;
|
|
32
|
+
disableTransfer?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
18
35
|
model?: ChatModel;
|
|
19
36
|
tools?: Agent[];
|
|
20
37
|
usage: ContextUsage;
|
|
@@ -26,13 +43,6 @@ export interface Context extends EventEmitter {
|
|
|
26
43
|
* @returns User agent
|
|
27
44
|
*/
|
|
28
45
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
29
|
-
/**
|
|
30
|
-
* Call an agent with a message
|
|
31
|
-
* @param agent Agent to call
|
|
32
|
-
* @param message Message to pass to the agent
|
|
33
|
-
* @returns the output of the agent
|
|
34
|
-
*/
|
|
35
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
36
46
|
/**
|
|
37
47
|
* Call an agent with a message and return the output and the active agent
|
|
38
48
|
* @param agent Agent to call
|
|
@@ -40,54 +50,82 @@ export interface Context extends EventEmitter {
|
|
|
40
50
|
* @param options.returnActiveAgent return the active agent
|
|
41
51
|
* @returns the output of the agent and the final active agent
|
|
42
52
|
*/
|
|
43
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
53
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
|
|
44
54
|
returnActiveAgent: true;
|
|
45
55
|
}): Promise<[O, Runnable]>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Call an agent with a message
|
|
58
|
+
* @param agent Agent to call
|
|
59
|
+
* @param message Message to pass to the agent
|
|
60
|
+
* @returns the output of the agent
|
|
61
|
+
*/
|
|
62
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions): Promise<O>;
|
|
63
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
49
64
|
/**
|
|
50
65
|
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
51
66
|
* @param topic topic name, or an array of topic names
|
|
52
|
-
* @param
|
|
53
|
-
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
67
|
+
* @param payload message to publish
|
|
54
68
|
*/
|
|
55
|
-
publish(topic: string | string[],
|
|
69
|
+
publish(topic: string | string[], payload: Omit<MessagePayload, "context">): void;
|
|
56
70
|
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
57
71
|
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
58
72
|
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
73
|
+
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
59
74
|
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
75
|
+
/**
|
|
76
|
+
* Create a child context with the same configuration as the parent context.
|
|
77
|
+
* If `reset` is true, the child context will have a new state (such as: usage).
|
|
78
|
+
*
|
|
79
|
+
* @param options
|
|
80
|
+
* @param options.reset create a new context with initial state (such as: usage)
|
|
81
|
+
* @returns new context
|
|
82
|
+
*/
|
|
83
|
+
newContext(options?: {
|
|
84
|
+
reset?: boolean;
|
|
85
|
+
}): Context;
|
|
86
|
+
}
|
|
87
|
+
export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
|
|
88
|
+
export declare class ExecutionContext implements Context {
|
|
89
|
+
constructor(parent?: ConstructorParameters<typeof ExecutionContextInternal>[0]);
|
|
90
|
+
parentId?: string;
|
|
91
|
+
id: string;
|
|
92
|
+
readonly internal: ExecutionContextInternal;
|
|
93
|
+
get model(): ChatModel | undefined;
|
|
94
|
+
get tools(): Agent<Message, Message>[] | undefined;
|
|
95
|
+
get limits(): ContextLimits | undefined;
|
|
96
|
+
get status(): "normal" | "timeout";
|
|
97
|
+
get usage(): ContextUsage;
|
|
98
|
+
newContext({ reset }?: {
|
|
99
|
+
reset?: boolean;
|
|
100
|
+
}): ExecutionContext;
|
|
101
|
+
call: Context["call"];
|
|
102
|
+
publish: Context["publish"];
|
|
103
|
+
subscribe: Context["subscribe"];
|
|
104
|
+
unsubscribe: Context["unsubscribe"];
|
|
105
|
+
emit<K extends keyof ContextEmitEventMap>(eventName: K, ...args: Args<K, ContextEmitEventMap>): boolean;
|
|
106
|
+
on<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
107
|
+
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
108
|
+
off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
60
109
|
}
|
|
61
|
-
|
|
62
|
-
private readonly
|
|
63
|
-
constructor(
|
|
64
|
-
model?: ChatModel;
|
|
65
|
-
tools?: Agent[];
|
|
66
|
-
limits?: ContextLimits;
|
|
110
|
+
declare class ExecutionContextInternal {
|
|
111
|
+
private readonly parent?;
|
|
112
|
+
constructor(parent?: (Pick<Context, "model" | "tools" | "limits"> & {
|
|
67
113
|
messageQueue?: MessageQueue;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
114
|
+
}) | undefined);
|
|
115
|
+
readonly messageQueue: MessageQueue;
|
|
116
|
+
readonly events: EventEmitter<ContextEventMap>;
|
|
71
117
|
get model(): ChatModel | undefined;
|
|
72
118
|
get tools(): Agent<Message, Message>[] | undefined;
|
|
119
|
+
get limits(): ContextLimits | undefined;
|
|
73
120
|
usage: ContextUsage;
|
|
74
|
-
limits?: ContextLimits | undefined;
|
|
75
121
|
private abortController;
|
|
76
122
|
private timer?;
|
|
77
123
|
private initTimeout;
|
|
78
124
|
get status(): "normal" | "timeout";
|
|
79
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}): Promise<[O, Runnable]>;
|
|
84
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
|
|
85
|
-
returnActiveAgent?: boolean;
|
|
86
|
-
}): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
87
|
-
publish(topic: string | string[], message: Message | string, from?: Agent): void;
|
|
88
|
-
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
89
|
-
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
90
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
91
|
-
emit(eventName: string | symbol, ...args: unknown[]): boolean;
|
|
125
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions): Promise<{
|
|
126
|
+
agent: Runnable;
|
|
127
|
+
output: O;
|
|
128
|
+
}>;
|
|
92
129
|
private callAgent;
|
|
93
130
|
}
|
|
131
|
+
export {};
|