@aigne/core 1.11.0 → 1.13.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 +28 -0
- package/README.md +11 -10
- package/README.zh.md +7 -6
- package/lib/cjs/agents/agent.d.ts +20 -20
- package/lib/cjs/agents/agent.js +25 -26
- package/lib/cjs/agents/ai-agent.d.ts +17 -18
- package/lib/cjs/agents/ai-agent.js +6 -6
- package/lib/cjs/agents/mcp-agent.d.ts +10 -4
- package/lib/cjs/agents/mcp-agent.js +12 -6
- package/lib/cjs/agents/memory.d.ts +1 -1
- package/lib/cjs/agents/team-agent.d.ts +28 -0
- package/lib/cjs/agents/team-agent.js +93 -0
- package/lib/cjs/agents/user-agent.d.ts +9 -10
- package/lib/cjs/agents/user-agent.js +10 -13
- package/lib/{esm/execution-engine/execution-engine.d.ts → cjs/aigne/aigne.d.ts} +9 -12
- package/lib/cjs/{execution-engine/execution-engine.js → aigne/aigne.js} +19 -19
- package/lib/cjs/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/cjs/{execution-engine → aigne}/context.js +30 -40
- package/lib/cjs/aigne/index.d.ts +4 -0
- package/lib/cjs/{execution-engine → aigne}/index.js +2 -2
- package/lib/cjs/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/cjs/client/client.d.ts +19 -0
- package/lib/cjs/client/client.js +49 -0
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/loader/agent-yaml.d.ts +3 -3
- package/lib/cjs/loader/agent-yaml.js +10 -3
- package/lib/cjs/loader/index.d.ts +9 -9
- package/lib/cjs/loader/index.js +8 -6
- package/lib/cjs/models/bedrock-chat-model.d.ts +70 -0
- package/lib/cjs/models/bedrock-chat-model.js +273 -0
- package/lib/cjs/models/chat-model.d.ts +2 -1
- package/lib/cjs/models/chat-model.js +8 -0
- package/lib/cjs/models/claude-chat-model.d.ts +3 -3
- package/lib/cjs/models/gemini-chat-model.d.ts +1 -0
- package/lib/cjs/models/gemini-chat-model.js +1 -0
- package/lib/cjs/models/openai-chat-model.d.ts +6 -3
- package/lib/cjs/models/openai-chat-model.js +100 -100
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -3
- package/lib/cjs/server/error.d.ts +4 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/server.d.ts +54 -0
- package/lib/cjs/server/server.js +130 -0
- package/lib/cjs/utils/event-stream.d.ts +11 -0
- package/lib/cjs/utils/event-stream.js +91 -0
- package/lib/cjs/utils/mcp-utils.js +4 -1
- package/lib/cjs/utils/prompts.js +1 -1
- package/lib/cjs/utils/stream-utils.d.ts +7 -2
- package/lib/cjs/utils/stream-utils.js +43 -34
- package/lib/cjs/utils/type-utils.d.ts +5 -2
- package/lib/cjs/utils/type-utils.js +22 -2
- package/lib/dts/agents/agent.d.ts +20 -20
- package/lib/dts/agents/ai-agent.d.ts +17 -18
- package/lib/dts/agents/mcp-agent.d.ts +10 -4
- package/lib/dts/agents/memory.d.ts +1 -1
- package/lib/dts/agents/team-agent.d.ts +28 -0
- package/lib/dts/agents/user-agent.d.ts +9 -10
- package/lib/dts/{execution-engine/execution-engine.d.ts → aigne/aigne.d.ts} +9 -12
- package/lib/dts/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/dts/aigne/index.d.ts +4 -0
- package/lib/dts/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/dts/client/client.d.ts +19 -0
- package/lib/dts/index.d.ts +2 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -3
- package/lib/dts/loader/index.d.ts +9 -9
- package/lib/dts/models/bedrock-chat-model.d.ts +70 -0
- package/lib/dts/models/chat-model.d.ts +2 -1
- package/lib/dts/models/claude-chat-model.d.ts +3 -3
- package/lib/dts/models/gemini-chat-model.d.ts +1 -0
- package/lib/dts/models/openai-chat-model.d.ts +6 -3
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/server/error.d.ts +4 -0
- package/lib/dts/server/server.d.ts +54 -0
- package/lib/dts/utils/event-stream.d.ts +11 -0
- package/lib/dts/utils/stream-utils.d.ts +7 -2
- package/lib/dts/utils/type-utils.d.ts +5 -2
- package/lib/esm/agents/agent.d.ts +20 -20
- package/lib/esm/agents/agent.js +25 -26
- package/lib/esm/agents/ai-agent.d.ts +17 -18
- package/lib/esm/agents/ai-agent.js +6 -6
- package/lib/esm/agents/mcp-agent.d.ts +10 -4
- package/lib/esm/agents/mcp-agent.js +12 -6
- package/lib/esm/agents/memory.d.ts +1 -1
- package/lib/esm/agents/team-agent.d.ts +28 -0
- package/lib/esm/agents/team-agent.js +89 -0
- package/lib/esm/agents/user-agent.d.ts +9 -10
- package/lib/esm/agents/user-agent.js +11 -14
- package/lib/{cjs/execution-engine/execution-engine.d.ts → esm/aigne/aigne.d.ts} +9 -12
- package/lib/esm/{execution-engine/execution-engine.js → aigne/aigne.js} +18 -18
- package/lib/esm/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/esm/{execution-engine → aigne}/context.js +28 -38
- package/lib/esm/aigne/index.d.ts +4 -0
- package/lib/esm/aigne/index.js +4 -0
- package/lib/esm/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/esm/client/client.d.ts +19 -0
- package/lib/esm/client/client.js +45 -0
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/loader/agent-yaml.d.ts +3 -3
- package/lib/esm/loader/agent-yaml.js +10 -3
- package/lib/esm/loader/index.d.ts +9 -9
- package/lib/esm/loader/index.js +8 -6
- package/lib/esm/models/bedrock-chat-model.d.ts +70 -0
- package/lib/esm/models/bedrock-chat-model.js +268 -0
- package/lib/esm/models/chat-model.d.ts +2 -1
- package/lib/esm/models/chat-model.js +8 -0
- package/lib/esm/models/claude-chat-model.d.ts +3 -3
- package/lib/esm/models/gemini-chat-model.d.ts +1 -0
- package/lib/esm/models/gemini-chat-model.js +1 -0
- package/lib/esm/models/openai-chat-model.d.ts +6 -3
- package/lib/esm/models/openai-chat-model.js +100 -100
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +4 -4
- package/lib/esm/server/error.d.ts +4 -0
- package/lib/esm/server/error.js +7 -0
- package/lib/esm/server/server.d.ts +54 -0
- package/lib/esm/server/server.js +123 -0
- package/lib/esm/utils/event-stream.d.ts +11 -0
- package/lib/esm/utils/event-stream.js +85 -0
- package/lib/esm/utils/mcp-utils.js +4 -1
- package/lib/esm/utils/prompts.js +1 -1
- package/lib/esm/utils/stream-utils.d.ts +7 -2
- package/lib/esm/utils/stream-utils.js +42 -33
- package/lib/esm/utils/type-utils.d.ts +5 -2
- package/lib/esm/utils/type-utils.js +20 -2
- package/package.json +20 -4
- package/lib/cjs/execution-engine/index.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.js +0 -34
- package/lib/dts/execution-engine/index.d.ts +0 -4
- package/lib/dts/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/index.d.ts +0 -4
- package/lib/esm/execution-engine/index.js +0 -4
- package/lib/esm/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/utils.js +0 -30
- /package/lib/cjs/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/cjs/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/cjs/{execution-engine → aigne}/usage.js +0 -0
- /package/lib/dts/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/esm/{execution-engine → aigne}/usage.js +0 -0
|
@@ -16,5 +16,6 @@ class GeminiChatModel extends openai_chat_model_js_1.OpenAIChatModel {
|
|
|
16
16
|
supportsEndWithSystemMessage = false;
|
|
17
17
|
supportsToolsUseWithJsonSchema = false;
|
|
18
18
|
supportsParallelToolCalls = false;
|
|
19
|
+
supportsToolStreaming = false;
|
|
19
20
|
}
|
|
20
21
|
exports.GeminiChatModel = GeminiChatModel;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import type {
|
|
5
|
-
import type { Context } from "../
|
|
4
|
+
import type { AgentInvokeOptions, AgentResponse } from "../agents/agent.js";
|
|
5
|
+
import type { Context } from "../aigne/context.js";
|
|
6
6
|
import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
|
|
7
7
|
export interface OpenAIChatModelCapabilities {
|
|
8
8
|
supportsNativeStructuredOutputs: boolean;
|
|
@@ -10,6 +10,7 @@ export interface OpenAIChatModelCapabilities {
|
|
|
10
10
|
supportsToolsUseWithJsonSchema: boolean;
|
|
11
11
|
supportsParallelToolCalls: boolean;
|
|
12
12
|
supportsToolsEmptyParameters: boolean;
|
|
13
|
+
supportsToolStreaming: boolean;
|
|
13
14
|
supportsTemperature: boolean;
|
|
14
15
|
}
|
|
15
16
|
export interface OpenAIChatModelOptions {
|
|
@@ -80,14 +81,16 @@ export declare class OpenAIChatModel extends ChatModel {
|
|
|
80
81
|
protected supportsToolsUseWithJsonSchema: boolean;
|
|
81
82
|
protected supportsParallelToolCalls: boolean;
|
|
82
83
|
protected supportsToolsEmptyParameters: boolean;
|
|
84
|
+
protected supportsToolStreaming: boolean;
|
|
83
85
|
protected supportsTemperature: boolean;
|
|
84
86
|
get client(): OpenAI;
|
|
85
87
|
get modelOptions(): ChatModelOptions | undefined;
|
|
86
|
-
process(input: ChatModelInput, _context: Context, options?:
|
|
88
|
+
process(input: ChatModelInput, _context: Context, options?: AgentInvokeOptions): Promise<AgentResponse<ChatModelOutput>>;
|
|
87
89
|
private getParallelToolCalls;
|
|
88
90
|
private getRunMessages;
|
|
89
91
|
private getRunResponseFormat;
|
|
90
92
|
private requestStructuredOutput;
|
|
93
|
+
private extractResultFromStream;
|
|
91
94
|
}
|
|
92
95
|
export declare const ROLE_MAP: {
|
|
93
96
|
[key in Role]: ChatCompletionMessageParam["role"];
|
|
@@ -54,6 +54,7 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
54
54
|
supportsToolsUseWithJsonSchema = true;
|
|
55
55
|
supportsParallelToolCalls = true;
|
|
56
56
|
supportsToolsEmptyParameters = true;
|
|
57
|
+
supportsToolStreaming = true;
|
|
57
58
|
supportsTemperature = true;
|
|
58
59
|
get client() {
|
|
59
60
|
const apiKey = this.options?.apiKey || process.env[this.apiKeyEnvName] || this.apiKeyDefault;
|
|
@@ -95,9 +96,9 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
95
96
|
response_format: responseFormat,
|
|
96
97
|
});
|
|
97
98
|
if (options?.streaming && input.responseFormat?.type !== "json_schema") {
|
|
98
|
-
return await extractResultFromStream(stream, false, true);
|
|
99
|
+
return await this.extractResultFromStream(stream, false, true);
|
|
99
100
|
}
|
|
100
|
-
const result = await extractResultFromStream(stream, jsonMode);
|
|
101
|
+
const result = await this.extractResultFromStream(stream, jsonMode);
|
|
101
102
|
if (!this.supportsToolsUseWithJsonSchema &&
|
|
102
103
|
!result.toolCalls?.length &&
|
|
103
104
|
input.responseFormat?.type === "json_schema" &&
|
|
@@ -163,7 +164,103 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
163
164
|
...body,
|
|
164
165
|
response_format: resolvedResponseFormat,
|
|
165
166
|
});
|
|
166
|
-
return extractResultFromStream(res, jsonMode);
|
|
167
|
+
return this.extractResultFromStream(res, jsonMode);
|
|
168
|
+
}
|
|
169
|
+
async extractResultFromStream(stream, jsonMode, streaming) {
|
|
170
|
+
const result = new ReadableStream({
|
|
171
|
+
start: async (controller) => {
|
|
172
|
+
try {
|
|
173
|
+
let text = "";
|
|
174
|
+
let refusal = "";
|
|
175
|
+
const toolCalls = [];
|
|
176
|
+
let model;
|
|
177
|
+
for await (const chunk of stream) {
|
|
178
|
+
const choice = chunk.choices?.[0];
|
|
179
|
+
if (!model) {
|
|
180
|
+
model = chunk.model;
|
|
181
|
+
controller.enqueue({
|
|
182
|
+
delta: {
|
|
183
|
+
json: {
|
|
184
|
+
model,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (choice?.delta.tool_calls?.length) {
|
|
190
|
+
for (const call of choice.delta.tool_calls) {
|
|
191
|
+
if (this.supportsToolStreaming && call.index !== undefined) {
|
|
192
|
+
handleToolCallDelta(toolCalls, call);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
handleCompleteToolCall(toolCalls, call);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (choice?.delta.content) {
|
|
200
|
+
text += choice.delta.content;
|
|
201
|
+
if (!jsonMode) {
|
|
202
|
+
controller.enqueue({
|
|
203
|
+
delta: {
|
|
204
|
+
text: {
|
|
205
|
+
text: choice.delta.content,
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (choice?.delta.refusal) {
|
|
212
|
+
refusal += choice.delta.refusal;
|
|
213
|
+
if (!jsonMode) {
|
|
214
|
+
controller.enqueue({
|
|
215
|
+
delta: {
|
|
216
|
+
text: { text: choice.delta.refusal },
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (chunk.usage) {
|
|
222
|
+
controller.enqueue({
|
|
223
|
+
delta: {
|
|
224
|
+
json: {
|
|
225
|
+
usage: {
|
|
226
|
+
inputTokens: chunk.usage.prompt_tokens,
|
|
227
|
+
outputTokens: chunk.usage.completion_tokens,
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
text = text || refusal;
|
|
235
|
+
if (jsonMode && text) {
|
|
236
|
+
controller.enqueue({
|
|
237
|
+
delta: {
|
|
238
|
+
json: {
|
|
239
|
+
json: (0, json_schema_js_1.parseJSON)(text),
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (toolCalls.length) {
|
|
245
|
+
controller.enqueue({
|
|
246
|
+
delta: {
|
|
247
|
+
json: {
|
|
248
|
+
toolCalls: toolCalls.map(({ args, ...c }) => ({
|
|
249
|
+
...c,
|
|
250
|
+
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
251
|
+
})),
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
controller.close();
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
controller.error(error);
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
167
264
|
}
|
|
168
265
|
}
|
|
169
266
|
exports.OpenAIChatModel = OpenAIChatModel;
|
|
@@ -245,103 +342,6 @@ function jsonSchemaToOpenAIJsonSchema(schema) {
|
|
|
245
342
|
}
|
|
246
343
|
return schema;
|
|
247
344
|
}
|
|
248
|
-
async function extractResultFromStream(stream, jsonMode, streaming) {
|
|
249
|
-
const result = new ReadableStream({
|
|
250
|
-
async start(controller) {
|
|
251
|
-
try {
|
|
252
|
-
let text = "";
|
|
253
|
-
let refusal = "";
|
|
254
|
-
const toolCalls = [];
|
|
255
|
-
let model;
|
|
256
|
-
for await (const chunk of stream) {
|
|
257
|
-
const choice = chunk.choices?.[0];
|
|
258
|
-
if (!model) {
|
|
259
|
-
model = chunk.model;
|
|
260
|
-
controller.enqueue({
|
|
261
|
-
delta: {
|
|
262
|
-
json: {
|
|
263
|
-
model,
|
|
264
|
-
},
|
|
265
|
-
},
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
if (choice?.delta.tool_calls?.length) {
|
|
269
|
-
for (const call of choice.delta.tool_calls) {
|
|
270
|
-
// Gemini not support tool call delta
|
|
271
|
-
if (call.index !== undefined) {
|
|
272
|
-
handleToolCallDelta(toolCalls, call);
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
handleCompleteToolCall(toolCalls, call);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
if (choice?.delta.content) {
|
|
280
|
-
text += choice.delta.content;
|
|
281
|
-
if (!jsonMode) {
|
|
282
|
-
controller.enqueue({
|
|
283
|
-
delta: {
|
|
284
|
-
text: {
|
|
285
|
-
text: choice.delta.content,
|
|
286
|
-
},
|
|
287
|
-
},
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
if (choice?.delta.refusal) {
|
|
292
|
-
refusal += choice.delta.refusal;
|
|
293
|
-
if (!jsonMode) {
|
|
294
|
-
controller.enqueue({
|
|
295
|
-
delta: {
|
|
296
|
-
text: { text: choice.delta.refusal },
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
if (chunk.usage) {
|
|
302
|
-
controller.enqueue({
|
|
303
|
-
delta: {
|
|
304
|
-
json: {
|
|
305
|
-
usage: {
|
|
306
|
-
inputTokens: chunk.usage.prompt_tokens,
|
|
307
|
-
outputTokens: chunk.usage.completion_tokens,
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
text = text || refusal;
|
|
315
|
-
if (jsonMode && text) {
|
|
316
|
-
controller.enqueue({
|
|
317
|
-
delta: {
|
|
318
|
-
json: {
|
|
319
|
-
json: (0, json_schema_js_1.parseJSON)(text),
|
|
320
|
-
},
|
|
321
|
-
},
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
if (toolCalls.length) {
|
|
325
|
-
controller.enqueue({
|
|
326
|
-
delta: {
|
|
327
|
-
json: {
|
|
328
|
-
toolCalls: toolCalls.map(({ args, ...c }) => ({
|
|
329
|
-
...c,
|
|
330
|
-
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
331
|
-
})),
|
|
332
|
-
},
|
|
333
|
-
},
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
controller.close();
|
|
337
|
-
}
|
|
338
|
-
catch (error) {
|
|
339
|
-
controller.error(error);
|
|
340
|
-
}
|
|
341
|
-
},
|
|
342
|
-
});
|
|
343
|
-
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
344
|
-
}
|
|
345
345
|
function handleToolCallDelta(toolCalls, call) {
|
|
346
346
|
toolCalls[call.index] ??= {
|
|
347
347
|
id: call.id || (0, nanoid_1.nanoid)(),
|
|
@@ -2,7 +2,7 @@ import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
|
2
2
|
import { Agent, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { AIAgent } from "../agents/ai-agent.js";
|
|
4
4
|
import type { AgentMemory } from "../agents/memory.js";
|
|
5
|
-
import type { Context } from "../
|
|
5
|
+
import type { Context } from "../aigne/context.js";
|
|
6
6
|
import type { ChatModel, ChatModelInput } from "../models/chat-model.js";
|
|
7
7
|
import { ChatMessagesTemplate } from "./template.js";
|
|
8
8
|
export declare const MESSAGE_KEY = "$message";
|
|
@@ -117,10 +117,10 @@ class PromptBuilder {
|
|
|
117
117
|
: undefined;
|
|
118
118
|
}
|
|
119
119
|
buildTools(options) {
|
|
120
|
-
const toolAgents = (options.context?.
|
|
121
|
-
.concat(options.agent?.
|
|
120
|
+
const toolAgents = (0, type_utils_js_1.unique)((options.context?.skills ?? [])
|
|
121
|
+
.concat(options.agent?.skills ?? [])
|
|
122
122
|
// TODO: support nested tools?
|
|
123
|
-
.flatMap((i) => (i.
|
|
123
|
+
.flatMap((i) => (i.isInvokable ? i.skills.concat(i) : i.skills)), (i) => i.name);
|
|
124
124
|
const tools = toolAgents.map((i) => ({
|
|
125
125
|
type: "function",
|
|
126
126
|
function: {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerError = void 0;
|
|
4
|
+
class ServerError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
constructor(status, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.status = status;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ServerError = ServerError;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { AIGNE } from "../aigne/aigne.js";
|
|
4
|
+
export declare const invokePayloadSchema: z.ZodObject<{
|
|
5
|
+
agent: z.ZodString;
|
|
6
|
+
input: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
7
|
+
options: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
8
|
+
streaming: z.ZodOptional<z.ZodNullable<z.ZodBoolean>>;
|
|
9
|
+
}, "strip", z.ZodTypeAny, {
|
|
10
|
+
streaming?: boolean | null | undefined;
|
|
11
|
+
}, {
|
|
12
|
+
streaming?: boolean | null | undefined;
|
|
13
|
+
}>>>;
|
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
|
15
|
+
agent: string;
|
|
16
|
+
input: Record<string, unknown>;
|
|
17
|
+
options?: {
|
|
18
|
+
streaming?: boolean | null | undefined;
|
|
19
|
+
} | null | undefined;
|
|
20
|
+
}, {
|
|
21
|
+
agent: string;
|
|
22
|
+
input: Record<string, unknown>;
|
|
23
|
+
options?: {
|
|
24
|
+
streaming?: boolean | null | undefined;
|
|
25
|
+
} | null | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
export interface AIGNEServerOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Maximum body size for the request.
|
|
30
|
+
* Only used when the request is a Node.js IncomingMessage and no `body` property is present.
|
|
31
|
+
* @default "4mb"
|
|
32
|
+
*/
|
|
33
|
+
maximumBodySize?: string;
|
|
34
|
+
}
|
|
35
|
+
export declare class AIGNEServer {
|
|
36
|
+
engine: AIGNE;
|
|
37
|
+
options?: AIGNEServerOptions | undefined;
|
|
38
|
+
constructor(engine: AIGNE, options?: AIGNEServerOptions | undefined);
|
|
39
|
+
/**
|
|
40
|
+
* Invoke the agent with the given input.
|
|
41
|
+
* @param request - The request object, which can be a parsed JSON object, a Fetch API Request object, or a Node.js IncomingMessage object.
|
|
42
|
+
* @returns The web standard response, you can return it directly in supported frameworks like hono.
|
|
43
|
+
*/
|
|
44
|
+
invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
|
|
45
|
+
/**
|
|
46
|
+
* Invoke the agent with the given input, and write the SSE response to the Node.js ServerResponse.
|
|
47
|
+
* @param request - The request object, which can be a parsed JSON object, a Fetch API Request object, or a Node.js IncomingMessage object.
|
|
48
|
+
* @param response - The Node.js ServerResponse object to write the SSE response to.
|
|
49
|
+
*/
|
|
50
|
+
invoke(request: Record<string, unknown> | Request | IncomingMessage, response: ServerResponse): Promise<void>;
|
|
51
|
+
_invoke(request: Record<string, unknown> | Request | IncomingMessage): Promise<Response>;
|
|
52
|
+
_prepareInput(request: Record<string, unknown> | Request | IncomingMessage): Promise<Record<string, unknown>>;
|
|
53
|
+
_writeResponse(response: Response, res: ServerResponse): Promise<void>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AIGNEServer = exports.invokePayloadSchema = void 0;
|
|
7
|
+
const node_http_1 = require("node:http");
|
|
8
|
+
const content_type_1 = __importDefault(require("content-type"));
|
|
9
|
+
const raw_body_1 = __importDefault(require("raw-body"));
|
|
10
|
+
const zod_1 = require("zod");
|
|
11
|
+
const event_stream_js_1 = require("../utils/event-stream.js");
|
|
12
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
13
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
14
|
+
const error_js_1 = require("./error.js");
|
|
15
|
+
const DEFAULT_MAXIMUM_BODY_SIZE = "4mb";
|
|
16
|
+
exports.invokePayloadSchema = zod_1.z.object({
|
|
17
|
+
agent: zod_1.z.string(),
|
|
18
|
+
input: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
|
|
19
|
+
options: zod_1.z
|
|
20
|
+
.object({
|
|
21
|
+
streaming: zod_1.z.boolean().nullish(),
|
|
22
|
+
})
|
|
23
|
+
.nullish(),
|
|
24
|
+
});
|
|
25
|
+
class AIGNEServer {
|
|
26
|
+
engine;
|
|
27
|
+
options;
|
|
28
|
+
constructor(engine, options) {
|
|
29
|
+
this.engine = engine;
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
async invoke(request, response) {
|
|
33
|
+
const result = await this._invoke(request);
|
|
34
|
+
if (response instanceof node_http_1.ServerResponse) {
|
|
35
|
+
await this._writeResponse(result, response);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
async _invoke(request) {
|
|
41
|
+
const { engine } = this;
|
|
42
|
+
try {
|
|
43
|
+
const payload = await this._prepareInput(request);
|
|
44
|
+
const { agent: agentName, input, options, } = (0, type_utils_js_1.tryOrThrow)(() => (0, type_utils_js_1.checkArguments)(`Invoke agent ${payload.agent}`, exports.invokePayloadSchema, payload), (error) => new error_js_1.ServerError(400, error.message));
|
|
45
|
+
const agent = engine.agents[agentName];
|
|
46
|
+
if (!agent)
|
|
47
|
+
throw new error_js_1.ServerError(404, `Agent ${agentName} not found`);
|
|
48
|
+
if (!options?.streaming) {
|
|
49
|
+
const result = await engine.invoke(agent, input);
|
|
50
|
+
return new Response(JSON.stringify(result), {
|
|
51
|
+
headers: { "Content-Type": "application/json" },
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const stream = await engine.invoke(agent, input, { streaming: true });
|
|
55
|
+
return new Response(new event_stream_js_1.AgentResponseStreamSSE(stream), {
|
|
56
|
+
headers: {
|
|
57
|
+
"Content-Type": "text/event-stream",
|
|
58
|
+
"Cache-Control": "no-cache",
|
|
59
|
+
"X-Accel-Buffering": "no",
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return new Response(JSON.stringify({ error: { message: error.message } }), {
|
|
65
|
+
status: error instanceof error_js_1.ServerError ? error.status : 500,
|
|
66
|
+
headers: { "Content-Type": "application/json" },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async _prepareInput(request) {
|
|
71
|
+
const contentTypeError = new error_js_1.ServerError(415, "Unsupported Media Type: Content-Type must be application/json");
|
|
72
|
+
if (request instanceof node_http_1.IncomingMessage) {
|
|
73
|
+
// Support for express with json() middleware
|
|
74
|
+
if ("body" in request && typeof request.body === "object") {
|
|
75
|
+
if (!(0, type_utils_js_1.isRecord)(request.body))
|
|
76
|
+
throw contentTypeError;
|
|
77
|
+
return request.body;
|
|
78
|
+
}
|
|
79
|
+
// Support vanilla nodejs http server
|
|
80
|
+
const maximumBodySize = this.options?.maximumBodySize || DEFAULT_MAXIMUM_BODY_SIZE;
|
|
81
|
+
const ct = request.headers["content-type"];
|
|
82
|
+
if (!ct || !ct.includes("application/json"))
|
|
83
|
+
throw contentTypeError;
|
|
84
|
+
const parsedCt = content_type_1.default.parse(ct);
|
|
85
|
+
const raw = await (0, raw_body_1.default)(request, {
|
|
86
|
+
limit: maximumBodySize,
|
|
87
|
+
encoding: parsedCt.parameters.charset ?? "utf-8",
|
|
88
|
+
});
|
|
89
|
+
return (0, type_utils_js_1.tryOrThrow)(() => JSON.parse(raw.toString()), (error) => new error_js_1.ServerError(400, `Parse request body to json error: ${error.message}`));
|
|
90
|
+
}
|
|
91
|
+
if (request instanceof Request) {
|
|
92
|
+
if (!request.headers.get("content-type")?.includes("application/json")) {
|
|
93
|
+
throw contentTypeError;
|
|
94
|
+
}
|
|
95
|
+
return await request.json();
|
|
96
|
+
}
|
|
97
|
+
if (!(0, type_utils_js_1.isRecord)(request))
|
|
98
|
+
throw contentTypeError;
|
|
99
|
+
return request;
|
|
100
|
+
}
|
|
101
|
+
async _writeResponse(response, res) {
|
|
102
|
+
try {
|
|
103
|
+
res.writeHead(response.status, response.headers.toJSON());
|
|
104
|
+
res.flushHeaders();
|
|
105
|
+
if (!response.body)
|
|
106
|
+
throw new Error("Response body is empty");
|
|
107
|
+
for await (const chunk of (0, stream_utils_js_1.readableStreamToAsyncIterator)(response.body)) {
|
|
108
|
+
res.write(chunk);
|
|
109
|
+
// Support for express with compression middleware
|
|
110
|
+
if ("flush" in res && typeof res.flush === "function") {
|
|
111
|
+
res.flush();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (!res.headersSent) {
|
|
117
|
+
res.writeHead(error instanceof error_js_1.ServerError ? error.status : 500, {
|
|
118
|
+
"Content-Type": "application/json",
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (res.writable) {
|
|
122
|
+
res.write(JSON.stringify({ error: { message: error.message } }));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
res.end();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.AIGNEServer = AIGNEServer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AgentResponseChunk, AgentResponseStream, Message } from "../agents/agent.js";
|
|
2
|
+
export declare class EventStreamParser<T> extends TransformStream<string, T | Error> {
|
|
3
|
+
constructor();
|
|
4
|
+
}
|
|
5
|
+
export declare class AgentResponseStreamParser<O extends Message> extends TransformStream<AgentResponseChunk<O> | Error, AgentResponseChunk<O>> {
|
|
6
|
+
private json;
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
export declare class AgentResponseStreamSSE<O extends Message> extends ReadableStream<string> {
|
|
10
|
+
constructor(stream: AgentResponseStream<O>);
|
|
11
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentResponseStreamSSE = exports.AgentResponseStreamParser = exports.EventStreamParser = void 0;
|
|
4
|
+
const eventsource_parser_1 = require("eventsource-parser");
|
|
5
|
+
const immer_1 = require("immer");
|
|
6
|
+
const type_utils_js_1 = require("./type-utils.js");
|
|
7
|
+
class EventStreamParser extends TransformStream {
|
|
8
|
+
constructor() {
|
|
9
|
+
let parser;
|
|
10
|
+
super({
|
|
11
|
+
start(controller) {
|
|
12
|
+
parser = (0, eventsource_parser_1.createParser)({
|
|
13
|
+
onEvent: (event) => {
|
|
14
|
+
const json = (0, type_utils_js_1.tryOrThrow)(() => JSON.parse(event.data), (e) => {
|
|
15
|
+
controller.enqueue(new Error(`Parse response chunk json error: ${e.message} ${event.data}`));
|
|
16
|
+
});
|
|
17
|
+
if (json) {
|
|
18
|
+
if (event.event === "error") {
|
|
19
|
+
controller.enqueue(new Error(json.message));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
controller.enqueue(json);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
transform(chunk) {
|
|
29
|
+
parser?.feed(chunk);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.EventStreamParser = EventStreamParser;
|
|
35
|
+
class AgentResponseStreamParser extends TransformStream {
|
|
36
|
+
json = {};
|
|
37
|
+
constructor() {
|
|
38
|
+
super({
|
|
39
|
+
transform: (chunk, controller) => {
|
|
40
|
+
if (chunk instanceof Error) {
|
|
41
|
+
controller.error(chunk);
|
|
42
|
+
controller.terminate();
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.json = (0, immer_1.produce)(this.json, (draft) => {
|
|
46
|
+
if (chunk.delta.json)
|
|
47
|
+
Object.assign(draft, chunk.delta.json);
|
|
48
|
+
if (chunk.delta.text) {
|
|
49
|
+
for (const [key, text] of Object.entries(chunk.delta.text)) {
|
|
50
|
+
const original = draft[key];
|
|
51
|
+
const t = (original || "") + (text || "");
|
|
52
|
+
if (t)
|
|
53
|
+
Object.assign(draft, { [key]: t });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
controller.enqueue({
|
|
58
|
+
...chunk,
|
|
59
|
+
delta: {
|
|
60
|
+
...chunk.delta,
|
|
61
|
+
json: this.json,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.AgentResponseStreamParser = AgentResponseStreamParser;
|
|
69
|
+
class AgentResponseStreamSSE extends ReadableStream {
|
|
70
|
+
constructor(stream) {
|
|
71
|
+
let reader;
|
|
72
|
+
super({
|
|
73
|
+
async pull(controller) {
|
|
74
|
+
reader ??= stream.getReader();
|
|
75
|
+
try {
|
|
76
|
+
const { value, done } = await reader.read();
|
|
77
|
+
if (done) {
|
|
78
|
+
controller.close();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
controller.enqueue(`data: ${JSON.stringify(value)}\n\n`);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
controller.enqueue(`event: error\ndata: ${JSON.stringify({ message: error.message })}\n\n`);
|
|
85
|
+
controller.close();
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.AgentResponseStreamSSE = AgentResponseStreamSSE;
|
|
@@ -8,12 +8,15 @@ const uriTemplate_js_1 = require("@modelcontextprotocol/sdk/shared/uriTemplate.j
|
|
|
8
8
|
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
9
9
|
const zod_1 = require("zod");
|
|
10
10
|
const mcp_agent_js_1 = require("../agents/mcp-agent.js");
|
|
11
|
+
const type_utils_js_1 = require("./type-utils.js");
|
|
11
12
|
function toolFromMCPTool(tool, options) {
|
|
12
13
|
return new mcp_agent_js_1.MCPTool({
|
|
13
14
|
...options,
|
|
14
15
|
name: tool.name,
|
|
15
16
|
description: tool.description,
|
|
16
|
-
inputSchema: (0,
|
|
17
|
+
inputSchema: (0, type_utils_js_1.isEmpty)(tool.inputSchema.properties)
|
|
18
|
+
? zod_1.z.object({})
|
|
19
|
+
: (0, json_schema_to_zod_1.jsonSchemaToZod)(tool.inputSchema),
|
|
17
20
|
outputSchema: types_js_1.CallToolResultSchema,
|
|
18
21
|
});
|
|
19
22
|
}
|
package/lib/cjs/utils/prompts.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getJsonOutputPrompt = getJsonOutputPrompt;
|
|
4
4
|
function getJsonOutputPrompt(schema) {
|
|
5
|
-
let prompt = "
|
|
5
|
+
let prompt = "Output must be a JSON object containing the following fields only.";
|
|
6
6
|
if (typeof schema === "string") {
|
|
7
7
|
prompt += `\n<json_fields>\n${schema}\n</json_fields>`;
|
|
8
8
|
}
|
|
@@ -11,7 +11,12 @@ export declare function onAgentResponseStreamEnd<T extends Message>(stream: Agen
|
|
|
11
11
|
}): ReadableStream<any>;
|
|
12
12
|
export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
|
|
13
13
|
export declare function arrayToAgentProcessAsyncGenerator<T extends Message>(chunks: (AgentResponseChunk<T> | Error)[], result?: Partial<T>): AgentProcessAsyncGenerator<T>;
|
|
14
|
-
export declare function
|
|
15
|
-
export declare function readableStreamToArray<T>(stream: ReadableStream<T
|
|
14
|
+
export declare function arrayToReadableStream<T>(chunks: (T | Error)[]): ReadableStream<T>;
|
|
15
|
+
export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options: {
|
|
16
|
+
catchError: true;
|
|
17
|
+
}): Promise<(T | Error)[]>;
|
|
18
|
+
export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
|
|
19
|
+
catchError?: false;
|
|
20
|
+
}): Promise<T[]>;
|
|
16
21
|
export declare function readableStreamToAsyncIterator<T>(stream: ReadableStream<T>): AsyncIterable<T>;
|
|
17
22
|
export declare function stringToAgentResponseStream(str: string, key?: "text" | typeof MESSAGE_KEY | string): AgentResponseStream<Message>;
|