@aigne/core 1.15.0 → 1.16.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 +15 -0
- package/README.md +9 -7
- package/README.zh.md +9 -7
- package/lib/cjs/agents/agent.d.ts +129 -6
- package/lib/cjs/agents/agent.js +112 -20
- package/lib/cjs/agents/ai-agent.d.ts +3 -2
- package/lib/cjs/agents/ai-agent.js +10 -6
- package/lib/{esm/models → cjs/agents}/chat-model.d.ts +24 -13
- package/lib/cjs/{models → agents}/chat-model.js +48 -7
- package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
- package/lib/cjs/agents/guide-rail-agent.js +14 -0
- package/lib/cjs/agents/mcp-agent.js +9 -9
- package/lib/cjs/aigne/aigne.d.ts +3 -2
- package/lib/cjs/aigne/aigne.js +2 -2
- package/lib/cjs/aigne/context.d.ts +2 -1
- package/lib/cjs/aigne/context.js +7 -0
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +1 -1
- package/lib/cjs/loader/index.d.ts +18 -11
- package/lib/cjs/loader/index.js +8 -27
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +3 -3
- package/lib/cjs/prompt/template.d.ts +3 -3
- package/lib/cjs/utils/json-schema.js +1 -1
- package/lib/cjs/utils/logger.d.ts +33 -8
- package/lib/cjs/utils/logger.js +63 -5
- package/lib/cjs/utils/model-utils.d.ts +1 -1
- package/lib/cjs/utils/stream-utils.d.ts +3 -1
- package/lib/cjs/utils/stream-utils.js +31 -1
- package/lib/cjs/utils/type-utils.d.ts +5 -0
- package/lib/dts/agents/agent.d.ts +129 -6
- package/lib/dts/agents/ai-agent.d.ts +3 -2
- package/lib/{cjs/models → dts/agents}/chat-model.d.ts +24 -13
- package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
- package/lib/dts/aigne/aigne.d.ts +3 -2
- package/lib/dts/aigne/context.d.ts +2 -1
- package/lib/dts/index.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +1 -1
- package/lib/dts/loader/index.d.ts +18 -11
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +3 -3
- package/lib/dts/prompt/template.d.ts +3 -3
- package/lib/dts/utils/logger.d.ts +33 -8
- package/lib/dts/utils/model-utils.d.ts +1 -1
- package/lib/dts/utils/stream-utils.d.ts +3 -1
- package/lib/dts/utils/type-utils.d.ts +5 -0
- package/lib/esm/agents/agent.d.ts +129 -6
- package/lib/esm/agents/agent.js +112 -20
- package/lib/esm/agents/ai-agent.d.ts +3 -2
- package/lib/esm/agents/ai-agent.js +10 -6
- package/lib/{dts/models → esm/agents}/chat-model.d.ts +24 -13
- package/lib/esm/{models → agents}/chat-model.js +48 -7
- package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
- package/lib/esm/agents/guide-rail-agent.js +11 -0
- package/lib/esm/agents/mcp-agent.js +9 -9
- package/lib/esm/aigne/aigne.d.ts +3 -2
- package/lib/esm/aigne/aigne.js +2 -2
- package/lib/esm/aigne/context.d.ts +2 -1
- package/lib/esm/aigne/context.js +7 -0
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +1 -1
- package/lib/esm/loader/index.d.ts +18 -11
- package/lib/esm/loader/index.js +8 -27
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +3 -3
- package/lib/esm/prompt/template.d.ts +3 -3
- package/lib/esm/utils/json-schema.js +1 -1
- package/lib/esm/utils/logger.d.ts +33 -8
- package/lib/esm/utils/logger.js +61 -4
- package/lib/esm/utils/model-utils.d.ts +1 -1
- package/lib/esm/utils/stream-utils.d.ts +3 -1
- package/lib/esm/utils/stream-utils.js +29 -1
- package/lib/esm/utils/type-utils.d.ts +5 -0
- package/package.json +1 -20
- package/lib/cjs/client/client.d.ts +0 -97
- package/lib/cjs/client/client.js +0 -87
- package/lib/cjs/client/index.d.ts +0 -1
- package/lib/cjs/client/index.js +0 -17
- package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
- package/lib/cjs/models/bedrock-chat-model.js +0 -303
- package/lib/cjs/models/claude-chat-model.d.ts +0 -114
- package/lib/cjs/models/claude-chat-model.js +0 -317
- package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
- package/lib/cjs/models/deepseek-chat-model.js +0 -35
- package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
- package/lib/cjs/models/gemini-chat-model.js +0 -35
- package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
- package/lib/cjs/models/ollama-chat-model.js +0 -34
- package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
- package/lib/cjs/models/open-router-chat-model.js +0 -34
- package/lib/cjs/models/openai-chat-model.d.ts +0 -166
- package/lib/cjs/models/openai-chat-model.js +0 -415
- package/lib/cjs/models/xai-chat-model.d.ts +0 -21
- package/lib/cjs/models/xai-chat-model.js +0 -33
- package/lib/cjs/server/error.d.ts +0 -15
- package/lib/cjs/server/error.js +0 -22
- package/lib/cjs/server/index.d.ts +0 -2
- package/lib/cjs/server/index.js +0 -18
- package/lib/cjs/server/server.d.ts +0 -135
- package/lib/cjs/server/server.js +0 -187
- package/lib/dts/client/client.d.ts +0 -97
- package/lib/dts/client/index.d.ts +0 -1
- package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
- package/lib/dts/models/claude-chat-model.d.ts +0 -114
- package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
- package/lib/dts/models/gemini-chat-model.d.ts +0 -23
- package/lib/dts/models/ollama-chat-model.d.ts +0 -22
- package/lib/dts/models/open-router-chat-model.d.ts +0 -22
- package/lib/dts/models/openai-chat-model.d.ts +0 -166
- package/lib/dts/models/xai-chat-model.d.ts +0 -21
- package/lib/dts/server/error.d.ts +0 -15
- package/lib/dts/server/index.d.ts +0 -2
- package/lib/dts/server/server.d.ts +0 -135
- package/lib/esm/client/client.d.ts +0 -97
- package/lib/esm/client/client.js +0 -83
- package/lib/esm/client/index.d.ts +0 -1
- package/lib/esm/client/index.js +0 -1
- package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
- package/lib/esm/models/bedrock-chat-model.js +0 -298
- package/lib/esm/models/claude-chat-model.d.ts +0 -114
- package/lib/esm/models/claude-chat-model.js +0 -310
- package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
- package/lib/esm/models/deepseek-chat-model.js +0 -31
- package/lib/esm/models/gemini-chat-model.d.ts +0 -23
- package/lib/esm/models/gemini-chat-model.js +0 -31
- package/lib/esm/models/ollama-chat-model.d.ts +0 -22
- package/lib/esm/models/ollama-chat-model.js +0 -30
- package/lib/esm/models/open-router-chat-model.d.ts +0 -22
- package/lib/esm/models/open-router-chat-model.js +0 -30
- package/lib/esm/models/openai-chat-model.d.ts +0 -166
- package/lib/esm/models/openai-chat-model.js +0 -405
- package/lib/esm/models/xai-chat-model.d.ts +0 -21
- package/lib/esm/models/xai-chat-model.js +0 -29
- package/lib/esm/server/error.d.ts +0 -15
- package/lib/esm/server/error.js +0 -18
- package/lib/esm/server/index.d.ts +0 -2
- package/lib/esm/server/index.js +0 -2
- package/lib/esm/server/server.d.ts +0 -135
- package/lib/esm/server/server.js +0 -180
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { DefaultMemory } from "../memory/default-memory.js";
|
|
3
3
|
import { MemoryAgent } from "../memory/memory.js";
|
|
4
|
-
import { ChatModel } from "../models/chat-model.js";
|
|
5
4
|
import { MESSAGE_KEY, PromptBuilder } from "../prompt/prompt-builder.js";
|
|
6
5
|
import { AgentMessageTemplate, ToolMessageTemplate } from "../prompt/template.js";
|
|
7
6
|
import { checkArguments, isEmpty } from "../utils/type-utils.js";
|
|
8
7
|
import { Agent, agentOptionsSchema, } from "./agent.js";
|
|
8
|
+
import { ChatModel, } from "./chat-model.js";
|
|
9
9
|
import { isTransferAgentOutput } from "./types.js";
|
|
10
10
|
/**
|
|
11
11
|
* Tool choice options for AI agents
|
|
@@ -222,9 +222,7 @@ export class AIAgent extends Agent {
|
|
|
222
222
|
if (!tool)
|
|
223
223
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
224
224
|
// NOTE: should pass both arguments (model generated) and input (user provided) to the tool
|
|
225
|
-
const output = await context
|
|
226
|
-
.invoke(tool, { ...input, ...call.function.arguments }, { disableTransfer: true })
|
|
227
|
-
.catch((error) => {
|
|
225
|
+
const output = await this.invokeSkill(tool, { ...input, ...call.function.arguments }, context).catch((error) => {
|
|
228
226
|
if (!this.catchToolsError) {
|
|
229
227
|
return Promise.reject(error);
|
|
230
228
|
}
|
|
@@ -248,7 +246,7 @@ export class AIAgent extends Agent {
|
|
|
248
246
|
}
|
|
249
247
|
}
|
|
250
248
|
const result = {};
|
|
251
|
-
if (
|
|
249
|
+
if (json) {
|
|
252
250
|
Object.assign(result, json);
|
|
253
251
|
}
|
|
254
252
|
else if (text) {
|
|
@@ -260,6 +258,12 @@ export class AIAgent extends Agent {
|
|
|
260
258
|
return;
|
|
261
259
|
}
|
|
262
260
|
}
|
|
261
|
+
async onGuideRailError(error) {
|
|
262
|
+
const outputKey = this.outputKey || MESSAGE_KEY;
|
|
263
|
+
return {
|
|
264
|
+
[outputKey]: error.reason,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
263
267
|
/**
|
|
264
268
|
* Process router mode requests
|
|
265
269
|
*
|
|
@@ -276,7 +280,7 @@ export class AIAgent extends Agent {
|
|
|
276
280
|
const tool = toolsMap.get(call.function.name);
|
|
277
281
|
if (!tool)
|
|
278
282
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
279
|
-
const stream = await context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true });
|
|
283
|
+
const stream = await context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
280
284
|
yield* stream;
|
|
281
285
|
}
|
|
282
286
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Agent, type AgentProcessResult, type Message } from "../agents/agent.js";
|
|
2
1
|
import type { Context } from "../aigne/context.js";
|
|
3
2
|
import type { PromiseOrValue } from "../utils/type-utils.js";
|
|
3
|
+
import { Agent, type AgentProcessResult, type Message } from "./agent.js";
|
|
4
4
|
/**
|
|
5
5
|
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
6
6
|
*
|
|
@@ -10,19 +10,19 @@ import type { PromiseOrValue } from "../utils/type-utils.js";
|
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* Here's how to implement a custom ChatModel:
|
|
13
|
-
* {@includeCode ../../test/
|
|
13
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
16
|
* Here's an example showing streaming response with readable stream:
|
|
17
|
-
* {@includeCode ../../test/
|
|
17
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming}
|
|
18
18
|
*
|
|
19
19
|
* @example
|
|
20
20
|
* Here's an example showing streaming response with async generator:
|
|
21
|
-
* {@includeCode ../../test/
|
|
21
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming-async-generator}
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
24
24
|
* Here's an example with tool calls:
|
|
25
|
-
* {@includeCode ../../test/
|
|
25
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
26
26
|
*/
|
|
27
27
|
export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
|
|
28
28
|
constructor();
|
|
@@ -44,6 +44,17 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
44
44
|
supportsParallelToolCalls: boolean;
|
|
45
45
|
};
|
|
46
46
|
private validateToolNames;
|
|
47
|
+
/**
|
|
48
|
+
* Normalizes tool names to ensure compatibility with language models
|
|
49
|
+
*
|
|
50
|
+
* This method converts tool names to a format that complies with model requirements
|
|
51
|
+
* by replacing hyphens and whitespace characters with underscores. The normalized
|
|
52
|
+
* names are used for tool calls while preserving the original names for reference.
|
|
53
|
+
*
|
|
54
|
+
* @param name - The original tool name to normalize
|
|
55
|
+
* @returns A promise that resolves to the normalized tool name
|
|
56
|
+
*/
|
|
57
|
+
protected normalizeToolName(name: string): Promise<string>;
|
|
47
58
|
/**
|
|
48
59
|
* Performs preprocessing operations before handling input
|
|
49
60
|
*
|
|
@@ -53,7 +64,7 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
53
64
|
* @param context Execution context
|
|
54
65
|
* @throws Error if token usage exceeds maximum limit
|
|
55
66
|
*/
|
|
56
|
-
protected preprocess(input: ChatModelInput, context: Context): void
|
|
67
|
+
protected preprocess(input: ChatModelInput, context: Context): Promise<void>;
|
|
57
68
|
/**
|
|
58
69
|
* Performs postprocessing operations after handling output
|
|
59
70
|
*
|
|
@@ -94,11 +105,11 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
94
105
|
*
|
|
95
106
|
* @example
|
|
96
107
|
* Here's a basic ChatModel input example:
|
|
97
|
-
* {@includeCode ../../test/
|
|
108
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
|
|
98
109
|
*
|
|
99
110
|
* @example
|
|
100
111
|
* Here's an example with tool calling:
|
|
101
|
-
* {@includeCode ../../test/
|
|
112
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
102
113
|
*/
|
|
103
114
|
export interface ChatModelInput extends Message {
|
|
104
115
|
/**
|
|
@@ -213,7 +224,7 @@ export type ChatModelInputResponseFormat = {
|
|
|
213
224
|
*
|
|
214
225
|
* @example
|
|
215
226
|
* Here's an example showing how to use tools:
|
|
216
|
-
* {@includeCode ../../test/
|
|
227
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
217
228
|
*/
|
|
218
229
|
export interface ChatModelInputTool {
|
|
219
230
|
/**
|
|
@@ -249,7 +260,7 @@ export interface ChatModelInputTool {
|
|
|
249
260
|
*
|
|
250
261
|
* @example
|
|
251
262
|
* Here's an example showing how to use tools:
|
|
252
|
-
* {@includeCode ../../test/
|
|
263
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
253
264
|
*/
|
|
254
265
|
export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
255
266
|
type: "function";
|
|
@@ -296,11 +307,11 @@ export interface ChatModelOptions {
|
|
|
296
307
|
*
|
|
297
308
|
* @example
|
|
298
309
|
* Here's a basic output example:
|
|
299
|
-
* {@includeCode ../../test/
|
|
310
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
|
|
300
311
|
*
|
|
301
312
|
* @example
|
|
302
313
|
* Here's an example with tool calls:
|
|
303
|
-
* {@includeCode ../../test/
|
|
314
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
304
315
|
*/
|
|
305
316
|
export interface ChatModelOutput extends Message {
|
|
306
317
|
/**
|
|
@@ -331,7 +342,7 @@ export interface ChatModelOutput extends Message {
|
|
|
331
342
|
*
|
|
332
343
|
* @example
|
|
333
344
|
* Here's an example with tool calls:
|
|
334
|
-
* {@includeCode ../../test/
|
|
345
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
335
346
|
*/
|
|
336
347
|
export interface ChatModelOutputToolCall {
|
|
337
348
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { Agent } from "
|
|
2
|
+
import { Agent } from "./agent.js";
|
|
3
3
|
/**
|
|
4
4
|
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
5
5
|
*
|
|
@@ -9,19 +9,19 @@ import { Agent } from "../agents/agent.js";
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* Here's how to implement a custom ChatModel:
|
|
12
|
-
* {@includeCode ../../test/
|
|
12
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* Here's an example showing streaming response with readable stream:
|
|
16
|
-
* {@includeCode ../../test/
|
|
16
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming}
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* Here's an example showing streaming response with async generator:
|
|
20
|
-
* {@includeCode ../../test/
|
|
20
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming-async-generator}
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* Here's an example with tool calls:
|
|
24
|
-
* {@includeCode ../../test/
|
|
24
|
+
* {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
|
|
25
25
|
*/
|
|
26
26
|
export class ChatModel extends Agent {
|
|
27
27
|
constructor() {
|
|
@@ -56,6 +56,19 @@ export class ChatModel extends Agent {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Normalizes tool names to ensure compatibility with language models
|
|
61
|
+
*
|
|
62
|
+
* This method converts tool names to a format that complies with model requirements
|
|
63
|
+
* by replacing hyphens and whitespace characters with underscores. The normalized
|
|
64
|
+
* names are used for tool calls while preserving the original names for reference.
|
|
65
|
+
*
|
|
66
|
+
* @param name - The original tool name to normalize
|
|
67
|
+
* @returns A promise that resolves to the normalized tool name
|
|
68
|
+
*/
|
|
69
|
+
async normalizeToolName(name) {
|
|
70
|
+
return name.replaceAll(/[-\s]/g, "_");
|
|
71
|
+
}
|
|
59
72
|
/**
|
|
60
73
|
* Performs preprocessing operations before handling input
|
|
61
74
|
*
|
|
@@ -65,14 +78,29 @@ export class ChatModel extends Agent {
|
|
|
65
78
|
* @param context Execution context
|
|
66
79
|
* @throws Error if token usage exceeds maximum limit
|
|
67
80
|
*/
|
|
68
|
-
preprocess(input, context) {
|
|
81
|
+
async preprocess(input, context) {
|
|
69
82
|
super.preprocess(input, context);
|
|
70
83
|
const { limits, usage } = context;
|
|
71
84
|
const usedTokens = usage.outputTokens + usage.inputTokens;
|
|
72
85
|
if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
|
|
73
86
|
throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
|
|
74
87
|
}
|
|
75
|
-
|
|
88
|
+
// Automatically convert tool names to a valid format
|
|
89
|
+
if (input.tools?.length) {
|
|
90
|
+
const toolsMap = {};
|
|
91
|
+
const tools = [];
|
|
92
|
+
for (const originalTool of input.tools) {
|
|
93
|
+
const name = await this.normalizeToolName(originalTool.function.name);
|
|
94
|
+
const tool = {
|
|
95
|
+
...originalTool,
|
|
96
|
+
function: { ...originalTool.function, name },
|
|
97
|
+
};
|
|
98
|
+
tools.push(tool);
|
|
99
|
+
toolsMap[name] = originalTool;
|
|
100
|
+
}
|
|
101
|
+
this.validateToolNames(tools);
|
|
102
|
+
Object.assign(input, { _toolsMap: toolsMap, tools });
|
|
103
|
+
}
|
|
76
104
|
}
|
|
77
105
|
/**
|
|
78
106
|
* Performs postprocessing operations after handling output
|
|
@@ -84,6 +112,19 @@ export class ChatModel extends Agent {
|
|
|
84
112
|
* @param context Execution context
|
|
85
113
|
*/
|
|
86
114
|
postprocess(input, output, context) {
|
|
115
|
+
// Restore original tool names in the output
|
|
116
|
+
if (output.toolCalls?.length) {
|
|
117
|
+
const toolsMap = input._toolsMap;
|
|
118
|
+
if (toolsMap) {
|
|
119
|
+
for (const toolCall of output.toolCalls) {
|
|
120
|
+
const originalTool = toolsMap[toolCall.function.name];
|
|
121
|
+
if (!originalTool) {
|
|
122
|
+
throw new Error(`Tool "${toolCall.function.name}" not found in tools map`);
|
|
123
|
+
}
|
|
124
|
+
toolCall.function.name = originalTool.function.name;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
87
128
|
super.postprocess(input, output, context);
|
|
88
129
|
const { usage } = output;
|
|
89
130
|
if (usage) {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Agent, AgentOptions, Message } from "./agent.js";
|
|
2
|
+
/**
|
|
3
|
+
* Input interface for GuideRail agents
|
|
4
|
+
*
|
|
5
|
+
* GuideRail agents receive both input and expected output, allowing them to
|
|
6
|
+
* validate, transform, or control the flow of messages between other agents.
|
|
7
|
+
*/
|
|
8
|
+
export interface GuideRailAgentInput extends Message {
|
|
9
|
+
/**
|
|
10
|
+
* The input data to be processed
|
|
11
|
+
*
|
|
12
|
+
* This is the original message that would be sent to the target agent
|
|
13
|
+
*/
|
|
14
|
+
input?: unknown;
|
|
15
|
+
/**
|
|
16
|
+
* The expected output data
|
|
17
|
+
*
|
|
18
|
+
* This is what the target agent is expected to produce, allowing
|
|
19
|
+
* the GuideRail agent to validate or transform the data flow
|
|
20
|
+
*/
|
|
21
|
+
output?: unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Output interface for GuideRail agents
|
|
25
|
+
*
|
|
26
|
+
* GuideRail agents can either allow the process to continue or abort it with a reason.
|
|
27
|
+
* This provides a mechanism for enforcing rules, validating data, and controlling
|
|
28
|
+
* the execution flow of the agent system.
|
|
29
|
+
*/
|
|
30
|
+
export interface GuideRailAgentOutput extends Message {
|
|
31
|
+
/**
|
|
32
|
+
* Whether to abort the current process
|
|
33
|
+
*
|
|
34
|
+
* When true, the agent system should stop the current execution flow
|
|
35
|
+
* and prevent further processing based on this input/output pair
|
|
36
|
+
*
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
abort?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Reason for aborting the process
|
|
42
|
+
*
|
|
43
|
+
* When abort is true, this provides a human-readable explanation
|
|
44
|
+
* for why the process was stopped
|
|
45
|
+
*/
|
|
46
|
+
reason?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* GuideRail agent type definition
|
|
50
|
+
*
|
|
51
|
+
* GuideRail agents act as validators, transformers, or controllers for the message
|
|
52
|
+
* flow between agents. They can enforce rules, perform safety checks, ensure data
|
|
53
|
+
* quality, or implement business logic validations.
|
|
54
|
+
*
|
|
55
|
+
* Use GuideRail agents when you need to:
|
|
56
|
+
* - Validate inputs or outputs against specific criteria
|
|
57
|
+
* - Enforce security or safety policies
|
|
58
|
+
* - Implement business rules that control agent interactions
|
|
59
|
+
* - Monitor and audit agent behavior
|
|
60
|
+
*/
|
|
61
|
+
export type GuideRailAgent = Agent<GuideRailAgentInput, GuideRailAgentOutput>;
|
|
62
|
+
export declare const guideRailAgentOptions: AgentOptions<GuideRailAgentInput, GuideRailAgentOutput>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const guideRailAgentOptions = {
|
|
3
|
+
inputSchema: z.object({
|
|
4
|
+
input: z.unknown(),
|
|
5
|
+
output: z.unknown(),
|
|
6
|
+
}),
|
|
7
|
+
outputSchema: z.object({
|
|
8
|
+
abort: z.boolean().optional().describe("Whether to abort the current process"),
|
|
9
|
+
reason: z.string().optional().describe("Reason for aborting the process"),
|
|
10
|
+
}),
|
|
11
|
+
};
|
|
@@ -78,25 +78,25 @@ export class MCPAgent extends Agent {
|
|
|
78
78
|
version: MCP_AGENT_CLIENT_VERSION,
|
|
79
79
|
}, undefined, isSSEServerParameters(options) ? { transportCreator, ...options } : undefined);
|
|
80
80
|
const transport = transportCreator();
|
|
81
|
-
logger.
|
|
81
|
+
logger.debug(`Connecting to MCP server: ${getMCPServerString(options)}`);
|
|
82
82
|
await client.connect(transport);
|
|
83
83
|
const mcpServer = getMCPServerName(client);
|
|
84
84
|
const { tools: isToolsAvailable, prompts: isPromptsAvailable, resources: isResourcesAvailable, } = client.getServerCapabilities() ?? {};
|
|
85
|
-
logger.
|
|
85
|
+
logger.debug(`Listing tools from ${mcpServer}`);
|
|
86
86
|
const skills = isToolsAvailable
|
|
87
87
|
? await client.listTools().then(({ tools }) => {
|
|
88
|
-
logger.
|
|
88
|
+
logger.debug(`Listing tools from ${mcpServer} completed %O`, tools?.map((i) => i.name));
|
|
89
89
|
return tools.map((tool) => toolFromMCPTool(tool, { client }));
|
|
90
90
|
})
|
|
91
91
|
: undefined;
|
|
92
|
-
logger.
|
|
92
|
+
logger.debug(`Listing prompts from ${mcpServer}`);
|
|
93
93
|
const prompts = isPromptsAvailable
|
|
94
94
|
? await client.listPrompts().then(({ prompts }) => {
|
|
95
|
-
logger.
|
|
95
|
+
logger.debug(`Listing prompts from ${mcpServer} completed %O`, prompts?.map((i) => i.name));
|
|
96
96
|
return prompts.map((prompt) => promptFromMCPPrompt(prompt, { client }));
|
|
97
97
|
})
|
|
98
98
|
: undefined;
|
|
99
|
-
logger.
|
|
99
|
+
logger.debug(`Listing resources from ${mcpServer}`);
|
|
100
100
|
// TODO: should conditionally call listResourceTemplates based on the server capabilities
|
|
101
101
|
// but the capability is not correct in the current SDK version
|
|
102
102
|
const resources = isResourcesAvailable
|
|
@@ -105,7 +105,7 @@ export class MCPAgent extends Agent {
|
|
|
105
105
|
client.listResourceTemplates().catch(() => ({ resourceTemplates: [] })),
|
|
106
106
|
]).then(([{ resources }, { resourceTemplates }]) => {
|
|
107
107
|
const result = [...resources, ...resourceTemplates].map((resource) => resourceFromMCPResource(resource, { client }));
|
|
108
|
-
logger.
|
|
108
|
+
logger.debug(`Listing resources from ${mcpServer} completed %O`, result.map((i) => i.name));
|
|
109
109
|
return result;
|
|
110
110
|
})
|
|
111
111
|
: undefined;
|
|
@@ -228,7 +228,7 @@ class ClientWithReconnect extends Client {
|
|
|
228
228
|
}, {
|
|
229
229
|
retries: this.reconnectOptions?.maxReconnects ?? DEFAULT_MAX_RECONNECTS,
|
|
230
230
|
shouldRetry: this.shouldReconnect,
|
|
231
|
-
onFailedAttempt: (error) => logger.
|
|
231
|
+
onFailedAttempt: (error) => logger.error("Reconnect attempt failed: %O", error),
|
|
232
232
|
});
|
|
233
233
|
}
|
|
234
234
|
async request(request, resultSchema, options) {
|
|
@@ -241,7 +241,7 @@ class ClientWithReconnect extends Client {
|
|
|
241
241
|
}
|
|
242
242
|
catch (error) {
|
|
243
243
|
if (this.shouldReconnect(error)) {
|
|
244
|
-
logger.
|
|
244
|
+
logger.error("Error occurred, reconnecting to MCP server: %O", error);
|
|
245
245
|
await this.reconnect();
|
|
246
246
|
return await super.request(request, resultSchema, mergedOptions);
|
|
247
247
|
}
|
package/lib/esm/aigne/aigne.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Agent, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
|
+
import { ChatModel } from "../agents/chat-model.js";
|
|
2
3
|
import type { UserAgent } from "../agents/user-agent.js";
|
|
3
|
-
import {
|
|
4
|
+
import { type LoadOptions } from "../loader/index.js";
|
|
4
5
|
import { AIGNEContext, type InvokeOptions } from "./context.js";
|
|
5
6
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
6
7
|
import type { ContextLimits } from "./usage.js";
|
|
@@ -54,7 +55,7 @@ export declare class AIGNE {
|
|
|
54
55
|
* @param options - Options to override the loaded configuration.
|
|
55
56
|
* @returns A fully initialized AIGNE instance with configured agents and skills.
|
|
56
57
|
*/
|
|
57
|
-
static load(path: string, options
|
|
58
|
+
static load(path: string, options: AIGNEOptions & Pick<LoadOptions, "models">): Promise<AIGNE>;
|
|
58
59
|
/**
|
|
59
60
|
* Creates a new AIGNE instance with the specified options.
|
|
60
61
|
*
|
package/lib/esm/aigne/aigne.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { Agent, } from "../agents/agent.js";
|
|
3
|
+
import { ChatModel } from "../agents/chat-model.js";
|
|
3
4
|
import { load } from "../loader/index.js";
|
|
4
|
-
import { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { checkArguments, createAccessorArray } from "../utils/type-utils.js";
|
|
6
6
|
import { AIGNEContext } from "./context.js";
|
|
7
7
|
import { MessageQueue, } from "./message-queue.js";
|
|
@@ -27,7 +27,7 @@ export class AIGNE {
|
|
|
27
27
|
* @returns A fully initialized AIGNE instance with configured agents and skills.
|
|
28
28
|
*/
|
|
29
29
|
static async load(path, options) {
|
|
30
|
-
const { model, agents, skills, ...aigne } = await load({ path });
|
|
30
|
+
const { model, agents, skills, ...aigne } = await load({ models: options.models, path });
|
|
31
31
|
return new AIGNE({
|
|
32
32
|
...options,
|
|
33
33
|
model: options?.model || model,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
|
+
import type { ChatModel } from "../agents/chat-model.js";
|
|
3
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
4
|
-
import type { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
6
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
7
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
@@ -41,6 +41,7 @@ export type ContextEmitEventMap = {
|
|
|
41
41
|
export interface InvokeOptions extends AgentInvokeOptions {
|
|
42
42
|
returnActiveAgent?: boolean;
|
|
43
43
|
disableTransfer?: boolean;
|
|
44
|
+
sourceAgent?: Agent;
|
|
44
45
|
}
|
|
45
46
|
/**
|
|
46
47
|
* @hidden
|
package/lib/esm/aigne/context.js
CHANGED
|
@@ -167,6 +167,13 @@ class AIGNEContextInternal {
|
|
|
167
167
|
let output;
|
|
168
168
|
for (;;) {
|
|
169
169
|
const result = {};
|
|
170
|
+
if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
|
|
171
|
+
options.sourceAgent.hooks.onHandoff?.({
|
|
172
|
+
source: options.sourceAgent,
|
|
173
|
+
target: activeAgent,
|
|
174
|
+
input,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
170
177
|
const stream = await activeAgent.invoke(input, context, { streaming: true });
|
|
171
178
|
for await (const value of stream) {
|
|
172
179
|
if (value.delta.text) {
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ export * from "./agents/team-agent.js";
|
|
|
5
5
|
export * from "./agents/types.js";
|
|
6
6
|
export * from "./agents/user-agent.js";
|
|
7
7
|
export * from "./aigne/index.js";
|
|
8
|
-
export * from "./
|
|
8
|
+
export * from "./agents/chat-model.js";
|
|
9
9
|
export * from "./prompt/prompt-builder.js";
|
|
10
10
|
export * from "./prompt/template.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -5,6 +5,6 @@ export * from "./agents/team-agent.js";
|
|
|
5
5
|
export * from "./agents/types.js";
|
|
6
6
|
export * from "./agents/user-agent.js";
|
|
7
7
|
export * from "./aigne/index.js";
|
|
8
|
-
export * from "./
|
|
8
|
+
export * from "./agents/chat-model.js";
|
|
9
9
|
export * from "./prompt/prompt-builder.js";
|
|
10
10
|
export * from "./prompt/template.js";
|
|
@@ -14,13 +14,13 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
14
14
|
}, {
|
|
15
15
|
[x: string]: any;
|
|
16
16
|
}> | undefined;
|
|
17
|
-
toolChoice?: AIAgentToolChoice | undefined;
|
|
18
17
|
outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
19
18
|
[x: string]: any;
|
|
20
19
|
}, {
|
|
21
20
|
[x: string]: any;
|
|
22
21
|
}> | undefined;
|
|
23
22
|
outputKey?: string | undefined;
|
|
23
|
+
toolChoice?: AIAgentToolChoice | undefined;
|
|
24
24
|
} | {
|
|
25
25
|
type: "mcp";
|
|
26
26
|
url?: string | undefined;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
import type { Camelize } from "camelize-ts";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
import { type Agent } from "../agents/agent.js";
|
|
3
|
-
import type { ChatModel, ChatModelOptions } from "../
|
|
4
|
+
import type { ChatModel, ChatModelOptions } from "../agents/chat-model.js";
|
|
4
5
|
export interface LoadOptions {
|
|
6
|
+
models: {
|
|
7
|
+
new (parameters: {
|
|
8
|
+
model?: string;
|
|
9
|
+
modelOptions?: ChatModelOptions;
|
|
10
|
+
}): ChatModel;
|
|
11
|
+
}[];
|
|
5
12
|
path: string;
|
|
6
13
|
}
|
|
7
14
|
export declare function load(options: LoadOptions): Promise<{
|
|
@@ -15,12 +22,12 @@ export declare function load(options: LoadOptions): Promise<{
|
|
|
15
22
|
temperature?: number | null | undefined;
|
|
16
23
|
provider?: string | null | undefined;
|
|
17
24
|
top_p?: number | null | undefined;
|
|
18
|
-
|
|
25
|
+
frequency_penalty?: number | null | undefined;
|
|
19
26
|
presence_penalty?: number | null | undefined;
|
|
20
27
|
} | null | undefined;
|
|
21
28
|
}>;
|
|
22
29
|
export declare function loadAgent(path: string): Promise<Agent>;
|
|
23
|
-
export declare function loadModel(model?: z.infer<typeof aigneFileSchema>["chat_model"]
|
|
30
|
+
export declare function loadModel(models: LoadOptions["models"], model?: Camelize<z.infer<typeof aigneFileSchema>["chat_model"]>, modelOptions?: ChatModelOptions): Promise<ChatModel | undefined>;
|
|
24
31
|
declare const aigneFileSchema: z.ZodObject<{
|
|
25
32
|
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
26
33
|
description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
@@ -29,35 +36,35 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
29
36
|
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
30
37
|
temperature: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
31
38
|
top_p: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
32
|
-
|
|
39
|
+
frequency_penalty: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
33
40
|
presence_penalty: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
34
41
|
}, "strip", z.ZodTypeAny, {
|
|
35
42
|
name?: string | null | undefined;
|
|
36
43
|
temperature?: number | null | undefined;
|
|
37
44
|
provider?: string | null | undefined;
|
|
38
45
|
top_p?: number | null | undefined;
|
|
39
|
-
|
|
46
|
+
frequency_penalty?: number | null | undefined;
|
|
40
47
|
presence_penalty?: number | null | undefined;
|
|
41
48
|
}, {
|
|
42
49
|
name?: string | null | undefined;
|
|
43
50
|
temperature?: number | null | undefined;
|
|
44
51
|
provider?: string | null | undefined;
|
|
45
52
|
top_p?: number | null | undefined;
|
|
46
|
-
|
|
53
|
+
frequency_penalty?: number | null | undefined;
|
|
47
54
|
presence_penalty?: number | null | undefined;
|
|
48
55
|
}>]>>>, {
|
|
49
56
|
name?: string | null | undefined;
|
|
50
57
|
temperature?: number | null | undefined;
|
|
51
58
|
provider?: string | null | undefined;
|
|
52
59
|
top_p?: number | null | undefined;
|
|
53
|
-
|
|
60
|
+
frequency_penalty?: number | null | undefined;
|
|
54
61
|
presence_penalty?: number | null | undefined;
|
|
55
62
|
} | null | undefined, string | {
|
|
56
63
|
name?: string | null | undefined;
|
|
57
64
|
temperature?: number | null | undefined;
|
|
58
65
|
provider?: string | null | undefined;
|
|
59
66
|
top_p?: number | null | undefined;
|
|
60
|
-
|
|
67
|
+
frequency_penalty?: number | null | undefined;
|
|
61
68
|
presence_penalty?: number | null | undefined;
|
|
62
69
|
} | null | undefined>;
|
|
63
70
|
agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
@@ -71,7 +78,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
71
78
|
temperature?: number | null | undefined;
|
|
72
79
|
provider?: string | null | undefined;
|
|
73
80
|
top_p?: number | null | undefined;
|
|
74
|
-
|
|
81
|
+
frequency_penalty?: number | null | undefined;
|
|
75
82
|
presence_penalty?: number | null | undefined;
|
|
76
83
|
} | null | undefined;
|
|
77
84
|
agents?: string[] | null | undefined;
|
|
@@ -84,7 +91,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
84
91
|
temperature?: number | null | undefined;
|
|
85
92
|
provider?: string | null | undefined;
|
|
86
93
|
top_p?: number | null | undefined;
|
|
87
|
-
|
|
94
|
+
frequency_penalty?: number | null | undefined;
|
|
88
95
|
presence_penalty?: number | null | undefined;
|
|
89
96
|
} | null | undefined;
|
|
90
97
|
agents?: string[] | null | undefined;
|
|
@@ -98,7 +105,7 @@ export declare function loadAIGNEFile(path: string): Promise<{
|
|
|
98
105
|
temperature?: number | null | undefined;
|
|
99
106
|
provider?: string | null | undefined;
|
|
100
107
|
top_p?: number | null | undefined;
|
|
101
|
-
|
|
108
|
+
frequency_penalty?: number | null | undefined;
|
|
102
109
|
presence_penalty?: number | null | undefined;
|
|
103
110
|
} | null | undefined;
|
|
104
111
|
agents?: string[] | null | undefined;
|