@aigne/core 1.13.0 → 1.14.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 +13 -26
- package/README.zh.md +24 -37
- package/lib/cjs/agents/agent.d.ts +522 -15
- package/lib/cjs/agents/agent.js +357 -36
- package/lib/cjs/agents/ai-agent.d.ts +210 -52
- package/lib/cjs/agents/ai-agent.js +182 -24
- package/lib/cjs/agents/mcp-agent.d.ts +112 -0
- package/lib/cjs/agents/mcp-agent.js +79 -1
- package/lib/cjs/agents/team-agent.d.ts +99 -0
- package/lib/cjs/agents/team-agent.js +94 -0
- package/lib/cjs/agents/user-agent.d.ts +6 -4
- package/lib/cjs/agents/user-agent.js +16 -5
- package/lib/cjs/aigne/aigne.d.ts +263 -16
- package/lib/cjs/aigne/aigne.js +130 -20
- package/lib/cjs/aigne/context.d.ts +24 -8
- package/lib/cjs/aigne/context.js +8 -22
- package/lib/cjs/aigne/message-queue.d.ts +26 -4
- package/lib/cjs/aigne/message-queue.js +42 -7
- package/lib/cjs/aigne/usage.d.ts +9 -0
- package/lib/cjs/aigne/usage.js +3 -0
- package/lib/cjs/client/client.d.ts +81 -3
- package/lib/cjs/client/client.js +38 -0
- package/lib/cjs/client/index.d.ts +1 -0
- package/lib/cjs/client/index.js +17 -0
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +2 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/memory/default-memory.d.ts +16 -0
- package/lib/cjs/memory/default-memory.js +70 -0
- package/lib/cjs/memory/index.d.ts +3 -0
- package/lib/cjs/memory/index.js +19 -0
- package/lib/cjs/memory/memory.d.ts +89 -0
- package/lib/cjs/memory/memory.js +132 -0
- package/lib/cjs/memory/recorder.d.ts +86 -0
- package/lib/cjs/memory/recorder.js +50 -0
- package/lib/cjs/memory/retriever.d.ts +99 -0
- package/lib/cjs/memory/retriever.js +51 -0
- package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
- package/lib/cjs/models/bedrock-chat-model.js +54 -24
- package/lib/cjs/models/chat-model.d.ts +278 -1
- package/lib/cjs/models/chat-model.js +54 -0
- package/lib/cjs/models/claude-chat-model.d.ts +49 -3
- package/lib/cjs/models/claude-chat-model.js +34 -2
- package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
- package/lib/cjs/models/deepseek-chat-model.js +16 -0
- package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
- package/lib/cjs/models/gemini-chat-model.js +14 -0
- package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
- package/lib/cjs/models/ollama-chat-model.js +16 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
- package/lib/cjs/models/open-router-chat-model.js +16 -0
- package/lib/cjs/models/openai-chat-model.d.ts +67 -3
- package/lib/cjs/models/openai-chat-model.js +47 -2
- package/lib/cjs/models/xai-chat-model.d.ts +16 -0
- package/lib/cjs/models/xai-chat-model.js +16 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
- package/lib/cjs/prompt/prompt-builder.js +19 -18
- package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
- package/lib/cjs/prompt/template.js +5 -1
- package/lib/cjs/server/error.d.ts +11 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +18 -0
- package/lib/cjs/server/server.d.ts +89 -8
- package/lib/cjs/server/server.js +58 -0
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/cjs/utils/prompts.d.ts +1 -0
- package/lib/cjs/utils/prompts.js +11 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +14 -0
- package/lib/dts/agents/agent.d.ts +522 -15
- package/lib/dts/agents/ai-agent.d.ts +210 -52
- package/lib/dts/agents/mcp-agent.d.ts +112 -0
- package/lib/dts/agents/team-agent.d.ts +99 -0
- package/lib/dts/agents/user-agent.d.ts +6 -4
- package/lib/dts/aigne/aigne.d.ts +263 -16
- package/lib/dts/aigne/context.d.ts +24 -8
- package/lib/dts/aigne/message-queue.d.ts +26 -4
- package/lib/dts/aigne/usage.d.ts +9 -0
- package/lib/dts/client/client.d.ts +81 -3
- package/lib/dts/client/index.d.ts +1 -0
- package/lib/dts/index.d.ts +0 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +4 -4
- package/lib/dts/memory/default-memory.d.ts +16 -0
- package/lib/dts/memory/index.d.ts +3 -0
- package/lib/dts/memory/memory.d.ts +89 -0
- package/lib/dts/memory/recorder.d.ts +86 -0
- package/lib/dts/memory/retriever.d.ts +99 -0
- package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
- package/lib/dts/models/chat-model.d.ts +278 -1
- package/lib/dts/models/claude-chat-model.d.ts +49 -3
- package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
- package/lib/dts/models/gemini-chat-model.d.ts +14 -0
- package/lib/dts/models/ollama-chat-model.d.ts +16 -0
- package/lib/dts/models/open-router-chat-model.d.ts +16 -0
- package/lib/dts/models/openai-chat-model.d.ts +67 -3
- package/lib/dts/models/xai-chat-model.d.ts +16 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -4
- package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/dts/server/error.d.ts +11 -0
- package/lib/dts/server/index.d.ts +2 -0
- package/lib/dts/server/server.d.ts +89 -8
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/dts/utils/prompts.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +522 -15
- package/lib/esm/agents/agent.js +351 -35
- package/lib/esm/agents/ai-agent.d.ts +210 -52
- package/lib/esm/agents/ai-agent.js +183 -25
- package/lib/esm/agents/mcp-agent.d.ts +112 -0
- package/lib/esm/agents/mcp-agent.js +79 -1
- package/lib/esm/agents/team-agent.d.ts +99 -0
- package/lib/esm/agents/team-agent.js +94 -0
- package/lib/esm/agents/user-agent.d.ts +6 -4
- package/lib/esm/agents/user-agent.js +17 -6
- package/lib/esm/aigne/aigne.d.ts +263 -16
- package/lib/esm/aigne/aigne.js +132 -22
- package/lib/esm/aigne/context.d.ts +24 -8
- package/lib/esm/aigne/context.js +9 -22
- package/lib/esm/aigne/message-queue.d.ts +26 -4
- package/lib/esm/aigne/message-queue.js +42 -8
- package/lib/esm/aigne/usage.d.ts +9 -0
- package/lib/esm/aigne/usage.js +3 -0
- package/lib/esm/client/client.d.ts +81 -3
- package/lib/esm/client/client.js +38 -0
- package/lib/esm/client/index.d.ts +1 -0
- package/lib/esm/client/index.js +1 -0
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +2 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/memory/default-memory.d.ts +16 -0
- package/lib/esm/memory/default-memory.js +63 -0
- package/lib/esm/memory/index.d.ts +3 -0
- package/lib/esm/memory/index.js +3 -0
- package/lib/esm/memory/memory.d.ts +89 -0
- package/lib/esm/memory/memory.js +127 -0
- package/lib/esm/memory/recorder.d.ts +86 -0
- package/lib/esm/memory/recorder.js +46 -0
- package/lib/esm/memory/retriever.d.ts +99 -0
- package/lib/esm/memory/retriever.js +47 -0
- package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
- package/lib/esm/models/bedrock-chat-model.js +56 -26
- package/lib/esm/models/chat-model.d.ts +278 -1
- package/lib/esm/models/chat-model.js +54 -0
- package/lib/esm/models/claude-chat-model.d.ts +49 -3
- package/lib/esm/models/claude-chat-model.js +35 -3
- package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
- package/lib/esm/models/deepseek-chat-model.js +16 -0
- package/lib/esm/models/gemini-chat-model.d.ts +14 -0
- package/lib/esm/models/gemini-chat-model.js +14 -0
- package/lib/esm/models/ollama-chat-model.d.ts +16 -0
- package/lib/esm/models/ollama-chat-model.js +16 -0
- package/lib/esm/models/open-router-chat-model.d.ts +16 -0
- package/lib/esm/models/open-router-chat-model.js +16 -0
- package/lib/esm/models/openai-chat-model.d.ts +67 -3
- package/lib/esm/models/openai-chat-model.js +47 -2
- package/lib/esm/models/xai-chat-model.d.ts +16 -0
- package/lib/esm/models/xai-chat-model.js +16 -0
- package/lib/esm/prompt/prompt-builder.d.ts +4 -4
- package/lib/esm/prompt/prompt-builder.js +20 -19
- package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
- package/lib/esm/prompt/template.js +5 -1
- package/lib/esm/server/error.d.ts +11 -0
- package/lib/esm/server/error.js +11 -0
- package/lib/esm/server/index.d.ts +2 -0
- package/lib/esm/server/index.js +2 -0
- package/lib/esm/server/server.d.ts +89 -8
- package/lib/esm/server/server.js +58 -0
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/lib/esm/utils/prompts.d.ts +1 -0
- package/lib/esm/utils/prompts.js +10 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +13 -0
- package/package.json +14 -11
- package/lib/cjs/agents/memory.d.ts +0 -26
- package/lib/cjs/agents/memory.js +0 -45
- package/lib/dts/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.js +0 -41
- /package/{LICENSE → LICENSE.md} +0 -0
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -15,6 +15,9 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
18
21
|
var __importStar = (this && this.__importStar) || (function () {
|
|
19
22
|
var ownKeys = function(o) {
|
|
20
23
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
@@ -33,18 +36,57 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
36
|
};
|
|
34
37
|
})();
|
|
35
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.FunctionAgent = exports.Agent = void 0;
|
|
39
|
+
exports.FunctionAgent = exports.Agent = exports.agentOptionsSchema = void 0;
|
|
37
40
|
exports.isEmptyChunk = isEmptyChunk;
|
|
41
|
+
exports.textDelta = textDelta;
|
|
42
|
+
exports.jsonDelta = jsonDelta;
|
|
38
43
|
const node_util_1 = require("node:util");
|
|
39
44
|
const zod_1 = require("zod");
|
|
40
45
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
41
46
|
const logger_js_1 = require("../utils/logger.js");
|
|
42
47
|
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
43
48
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
44
|
-
const memory_js_1 = require("./memory.js");
|
|
45
49
|
const types_js_1 = require("./types.js");
|
|
50
|
+
__exportStar(require("./types.js"), exports);
|
|
51
|
+
exports.agentOptionsSchema = zod_1.z.object({
|
|
52
|
+
subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
|
|
53
|
+
publishTopic: zod_1.z
|
|
54
|
+
.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string()), zod_1.z.custom()])
|
|
55
|
+
.optional(),
|
|
56
|
+
name: zod_1.z.string().optional(),
|
|
57
|
+
description: zod_1.z.string().optional(),
|
|
58
|
+
inputSchema: zod_1.z.custom().optional(),
|
|
59
|
+
outputSchema: zod_1.z.custom().optional(),
|
|
60
|
+
includeInputInOutput: zod_1.z.boolean().optional(),
|
|
61
|
+
skills: zod_1.z.array(zod_1.z.union([zod_1.z.custom(), zod_1.z.custom()])).optional(),
|
|
62
|
+
disableEvents: zod_1.z.boolean().optional(),
|
|
63
|
+
memory: zod_1.z.union([zod_1.z.custom(), zod_1.z.array(zod_1.z.custom())]).optional(),
|
|
64
|
+
});
|
|
65
|
+
/**
|
|
66
|
+
* Agent is the base class for all agents.
|
|
67
|
+
* It provides a mechanism for defining input/output schemas and implementing processing logic,
|
|
68
|
+
* serving as the foundation of the entire agent system.
|
|
69
|
+
*
|
|
70
|
+
* By extending the Agent class and implementing the process method, you can create custom agents
|
|
71
|
+
* with various capabilities:
|
|
72
|
+
* - Process structured input and output data
|
|
73
|
+
* - Validate data formats using schemas
|
|
74
|
+
* - Communicate between agents through contexts
|
|
75
|
+
* - Support streaming or non-streaming responses
|
|
76
|
+
* - Maintain memory of past interactions
|
|
77
|
+
* - Output in multiple formats (JSON/text)
|
|
78
|
+
* - Forward tasks to other agents
|
|
79
|
+
*
|
|
80
|
+
* @template I The input message type the agent accepts
|
|
81
|
+
* @template O The output message type the agent returns
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* Here's an example of how to create a custom agent:
|
|
85
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
|
|
86
|
+
*/
|
|
46
87
|
class Agent {
|
|
47
|
-
constructor(
|
|
88
|
+
constructor(options = {}) {
|
|
89
|
+
const { inputSchema, outputSchema } = options;
|
|
48
90
|
this.name = options.name || this.constructor.name;
|
|
49
91
|
this.description = options.description;
|
|
50
92
|
if (inputSchema)
|
|
@@ -59,68 +101,159 @@ class Agent {
|
|
|
59
101
|
if (options.skills?.length)
|
|
60
102
|
this.skills.push(...options.skills.map(functionToAgent));
|
|
61
103
|
this.disableEvents = options.disableEvents;
|
|
62
|
-
if (options.memory) {
|
|
63
|
-
this.memory
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
? new memory_js_1.AgentMemory({ enabled: options.memory })
|
|
68
|
-
: new memory_js_1.AgentMemory(options.memory);
|
|
104
|
+
if (Array.isArray(options.memory)) {
|
|
105
|
+
this.memories.push(...options.memory);
|
|
106
|
+
}
|
|
107
|
+
else if (options.memory) {
|
|
108
|
+
this.memories.push(options.memory);
|
|
69
109
|
}
|
|
70
110
|
}
|
|
71
|
-
|
|
111
|
+
/**
|
|
112
|
+
* List of memories this agent can use
|
|
113
|
+
*/
|
|
114
|
+
memories = [];
|
|
115
|
+
/**
|
|
116
|
+
* Name of the agent, used for identification and logging
|
|
117
|
+
*
|
|
118
|
+
* Defaults to the class constructor name if not specified in options
|
|
119
|
+
*/
|
|
72
120
|
name;
|
|
73
121
|
/**
|
|
74
|
-
* Default topic this agent
|
|
122
|
+
* Default topic this agent subscribes to
|
|
123
|
+
*
|
|
124
|
+
* Each agent has a default topic in the format "$agent_[agent name]"
|
|
125
|
+
* The agent automatically subscribes to this topic to receive messages
|
|
126
|
+
*
|
|
127
|
+
* @returns The default topic string
|
|
75
128
|
*/
|
|
76
129
|
get topic() {
|
|
77
130
|
return `$agent_${this.name}`;
|
|
78
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Description of the agent's purpose and capabilities
|
|
134
|
+
*
|
|
135
|
+
* Useful for documentation and when agents need to understand
|
|
136
|
+
* each other's roles in a multi-agent system
|
|
137
|
+
*/
|
|
79
138
|
description;
|
|
80
139
|
_inputSchema;
|
|
81
140
|
_outputSchema;
|
|
141
|
+
/**
|
|
142
|
+
* Get the input data schema for this agent
|
|
143
|
+
*
|
|
144
|
+
* Used to validate that input messages conform to required format
|
|
145
|
+
* If no input schema is set, returns an empty object schema by default
|
|
146
|
+
*
|
|
147
|
+
* @returns The Zod type definition for input data
|
|
148
|
+
*/
|
|
82
149
|
get inputSchema() {
|
|
83
150
|
const s = this._inputSchema;
|
|
84
151
|
const schema = typeof s === "function" ? s(this) : s || zod_1.z.object({});
|
|
85
152
|
checkAgentInputOutputSchema(schema);
|
|
86
153
|
return schema.passthrough();
|
|
87
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Get the output data schema for this agent
|
|
157
|
+
*
|
|
158
|
+
* Used to validate that output messages conform to required format
|
|
159
|
+
* If no output schema is set, returns an empty object schema by default
|
|
160
|
+
*
|
|
161
|
+
* @returns The Zod type definition for output data
|
|
162
|
+
*/
|
|
88
163
|
get outputSchema() {
|
|
89
164
|
const s = this._outputSchema;
|
|
90
165
|
const schema = typeof s === "function" ? s(this) : s || zod_1.z.object({});
|
|
91
166
|
checkAgentInputOutputSchema(schema);
|
|
92
167
|
return schema.passthrough();
|
|
93
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Whether to include the original input in the output
|
|
171
|
+
*
|
|
172
|
+
* When true, the agent will merge input fields into the output object
|
|
173
|
+
*/
|
|
94
174
|
includeInputInOutput;
|
|
175
|
+
/**
|
|
176
|
+
* Topics the agent subscribes to for receiving messages
|
|
177
|
+
*
|
|
178
|
+
* Can be a single topic string or an array of topic strings
|
|
179
|
+
*/
|
|
95
180
|
subscribeTopic;
|
|
181
|
+
/**
|
|
182
|
+
* Topics the agent publishes to for sending messages
|
|
183
|
+
*
|
|
184
|
+
* Can be a string, array of strings, or a function that determines
|
|
185
|
+
* topics based on the output
|
|
186
|
+
*/
|
|
96
187
|
publishTopic;
|
|
188
|
+
/**
|
|
189
|
+
* Collection of skills (other agents) this agent can use
|
|
190
|
+
*
|
|
191
|
+
* Skills can be accessed by name or by array index, allowing
|
|
192
|
+
* the agent to delegate tasks to specialized sub-agents
|
|
193
|
+
*/
|
|
97
194
|
skills = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((t) => t.name === name));
|
|
195
|
+
/**
|
|
196
|
+
* Whether to disable emitting events for agent actions
|
|
197
|
+
*
|
|
198
|
+
* When true, the agent won't emit events like agentStarted,
|
|
199
|
+
* agentSucceed, or agentFailed
|
|
200
|
+
*/
|
|
98
201
|
disableEvents;
|
|
202
|
+
subscriptions = [];
|
|
99
203
|
/**
|
|
100
204
|
* Attach agent to context:
|
|
101
|
-
* -
|
|
102
|
-
* -
|
|
103
|
-
*
|
|
205
|
+
* - Subscribe to topics and invoke process method when messages are received
|
|
206
|
+
* - Subscribe to memory topics if memory is enabled
|
|
207
|
+
*
|
|
208
|
+
* Agents can receive messages and respond through the topic subscription system,
|
|
209
|
+
* enabling inter-agent communication.
|
|
210
|
+
*
|
|
211
|
+
* @param context Context to attach to
|
|
104
212
|
*/
|
|
105
213
|
attach(context) {
|
|
106
|
-
this.
|
|
214
|
+
for (const memory of this.memories) {
|
|
215
|
+
memory.attach(context);
|
|
216
|
+
}
|
|
217
|
+
this.subscribeToTopics(context);
|
|
218
|
+
}
|
|
219
|
+
subscribeToTopics(context) {
|
|
107
220
|
for (const topic of (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic).concat(this.topic)) {
|
|
108
|
-
context.subscribe(topic,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
221
|
+
this.subscriptions.push(context.subscribe(topic, (payload) => this.onMessage(payload)));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async onMessage({ message, context }) {
|
|
225
|
+
try {
|
|
226
|
+
await context.invoke(this, message);
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
context.emit("agentFailed", { agent: this, error });
|
|
116
230
|
}
|
|
117
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Add skills (other agents or functions) to this agent
|
|
234
|
+
*
|
|
235
|
+
* Skills allow agents to reuse functionality from other agents,
|
|
236
|
+
* building more complex behaviors.
|
|
237
|
+
*
|
|
238
|
+
* @param skills List of skills to add, can be Agent instances or functions
|
|
239
|
+
*/
|
|
118
240
|
addSkill(...skills) {
|
|
119
241
|
this.skills.push(...skills.map((skill) => (typeof skill === "function" ? functionToAgent(skill) : skill)));
|
|
120
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Check if the agent is invokable
|
|
245
|
+
*
|
|
246
|
+
* An agent is invokable if it has implemented the process method
|
|
247
|
+
*/
|
|
121
248
|
get isInvokable() {
|
|
122
249
|
return !!this.process;
|
|
123
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Check context status to ensure it hasn't timed out
|
|
253
|
+
*
|
|
254
|
+
* @param context The context to check
|
|
255
|
+
* @throws Error if the context has timed out
|
|
256
|
+
*/
|
|
124
257
|
checkContextStatus(context) {
|
|
125
258
|
if (context) {
|
|
126
259
|
const { status } = context;
|
|
@@ -142,7 +275,7 @@ class Agent {
|
|
|
142
275
|
const parsedInput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} input`, this.inputSchema, message);
|
|
143
276
|
this.preprocess(parsedInput, ctx);
|
|
144
277
|
this.checkContextStatus(ctx);
|
|
145
|
-
let response = await this.process(parsedInput, ctx
|
|
278
|
+
let response = await this.process(parsedInput, ctx);
|
|
146
279
|
if (response instanceof Agent) {
|
|
147
280
|
response = (0, types_js_1.transferToAgentOutput)(response);
|
|
148
281
|
}
|
|
@@ -175,6 +308,16 @@ class Agent {
|
|
|
175
308
|
this.processAgentError(error, ctx);
|
|
176
309
|
}
|
|
177
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Process agent output
|
|
313
|
+
*
|
|
314
|
+
* Validates output format, applies post-processing operations, and triggers success events
|
|
315
|
+
*
|
|
316
|
+
* @param input Original input message
|
|
317
|
+
* @param output Raw output produced by the agent
|
|
318
|
+
* @param context Execution context
|
|
319
|
+
* @returns Final processed output
|
|
320
|
+
*/
|
|
178
321
|
async processAgentOutput(input, output, context) {
|
|
179
322
|
const parsedOutput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} output`, this.outputSchema, output);
|
|
180
323
|
const finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
@@ -184,12 +327,30 @@ class Agent {
|
|
|
184
327
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
185
328
|
return finalOutput;
|
|
186
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Process errors that occur during agent execution
|
|
332
|
+
*
|
|
333
|
+
* Logs error information, triggers failure events, and re-throws the error
|
|
334
|
+
*
|
|
335
|
+
* @param error Caught error
|
|
336
|
+
* @param context Execution context
|
|
337
|
+
* @throws Always throws the received error
|
|
338
|
+
*/
|
|
187
339
|
processAgentError(error, context) {
|
|
188
340
|
logger_js_1.logger.core("Invoke agent %s failed with error: %O", this.name, error);
|
|
189
341
|
if (!this.disableEvents)
|
|
190
342
|
context.emit("agentFailed", { agent: this, error });
|
|
191
343
|
throw error;
|
|
192
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* Check agent invocation usage to prevent exceeding limits
|
|
347
|
+
*
|
|
348
|
+
* If the context has a maximum invocation limit set, checks if the limit
|
|
349
|
+
* has been exceeded and increments the invocation counter
|
|
350
|
+
*
|
|
351
|
+
* @param context Execution context
|
|
352
|
+
* @throws Error if maximum invocation limit is exceeded
|
|
353
|
+
*/
|
|
193
354
|
checkAgentInvokesUsage(context) {
|
|
194
355
|
const { limits, usage } = context;
|
|
195
356
|
if (limits?.maxAgentInvokes && usage.agentCalls >= limits.maxAgentInvokes) {
|
|
@@ -197,52 +358,212 @@ class Agent {
|
|
|
197
358
|
}
|
|
198
359
|
usage.agentCalls++;
|
|
199
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Pre-processing operations before handling input
|
|
363
|
+
*
|
|
364
|
+
* Preparatory work done before executing the agent's main processing logic, including:
|
|
365
|
+
* - Checking context status
|
|
366
|
+
* - Verifying invocation limits
|
|
367
|
+
*
|
|
368
|
+
* @param _ Input message (unused)
|
|
369
|
+
* @param context Execution context
|
|
370
|
+
*/
|
|
200
371
|
preprocess(_, context) {
|
|
201
372
|
this.checkContextStatus(context);
|
|
202
373
|
this.checkAgentInvokesUsage(context);
|
|
203
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Post-processing operations after handling output
|
|
377
|
+
*
|
|
378
|
+
* Operations performed after the agent produces output, including:
|
|
379
|
+
* - Checking context status
|
|
380
|
+
* - Adding interaction records to memory
|
|
381
|
+
*
|
|
382
|
+
* @param input Input message
|
|
383
|
+
* @param output Output message
|
|
384
|
+
* @param context Execution context
|
|
385
|
+
*/
|
|
204
386
|
postprocess(input, output, context) {
|
|
205
387
|
this.checkContextStatus(context);
|
|
206
|
-
this.
|
|
207
|
-
this.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
388
|
+
this.publishToTopics(output, context);
|
|
389
|
+
for (const memory of this.memories) {
|
|
390
|
+
if (memory.autoUpdate) {
|
|
391
|
+
memory.record({
|
|
392
|
+
content: [
|
|
393
|
+
{ role: "user", content: input },
|
|
394
|
+
{ role: "agent", content: (0, types_js_1.replaceTransferAgentToName)(output), source: this.name },
|
|
395
|
+
],
|
|
396
|
+
}, context);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
212
399
|
}
|
|
400
|
+
async publishToTopics(output, context) {
|
|
401
|
+
const publishTopics = typeof this.publishTopic === "function" ? await this.publishTopic(output) : this.publishTopic;
|
|
402
|
+
if (publishTopics?.length) {
|
|
403
|
+
context.publish(publishTopics, {
|
|
404
|
+
role: this.constructor.name === "UserAgent" ? "user" : "agent",
|
|
405
|
+
source: this.name,
|
|
406
|
+
message: output,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Shut down the agent and clean up resources
|
|
412
|
+
*
|
|
413
|
+
* Primarily used to clean up memory and other resources to prevent memory leaks
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* Here's an example of shutting down an agent:
|
|
417
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown}
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* Here's an example of shutting down an agent by using statement:
|
|
421
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
|
|
422
|
+
*/
|
|
213
423
|
async shutdown() {
|
|
214
|
-
this.
|
|
424
|
+
for (const sub of this.subscriptions) {
|
|
425
|
+
sub();
|
|
426
|
+
}
|
|
427
|
+
this.subscriptions = [];
|
|
428
|
+
for (const m of this.memories) {
|
|
429
|
+
m.shutdown();
|
|
430
|
+
}
|
|
215
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Custom object inspection behavior
|
|
434
|
+
*
|
|
435
|
+
* When using Node.js's util.inspect function to inspect an agent,
|
|
436
|
+
* only the agent's name will be shown, making output more concise
|
|
437
|
+
*
|
|
438
|
+
* @returns Agent name
|
|
439
|
+
*/
|
|
216
440
|
[node_util_1.inspect.custom]() {
|
|
217
441
|
return this.name;
|
|
218
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Async dispose method for shutdown the agent
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* Here's an example of shutting down an agent by using statement:
|
|
448
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
|
|
449
|
+
*/
|
|
450
|
+
async [Symbol.asyncDispose]() {
|
|
451
|
+
await this.shutdown();
|
|
452
|
+
}
|
|
219
453
|
}
|
|
220
454
|
exports.Agent = Agent;
|
|
455
|
+
/**
|
|
456
|
+
* Check if a response chunk is empty
|
|
457
|
+
*
|
|
458
|
+
* @template T Response data type
|
|
459
|
+
* @param chunk The response chunk to check
|
|
460
|
+
* @returns True if the chunk is empty
|
|
461
|
+
*/
|
|
221
462
|
function isEmptyChunk(chunk) {
|
|
222
463
|
return (0, type_utils_js_1.isEmpty)(chunk.delta.json) && (0, type_utils_js_1.isEmpty)(chunk.delta.text);
|
|
223
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Creates a text delta for streaming responses
|
|
467
|
+
*
|
|
468
|
+
* This utility function creates an AgentResponseDelta object with only the text part,
|
|
469
|
+
* useful for incrementally building streaming text responses in agents.
|
|
470
|
+
*
|
|
471
|
+
* @template T Agent message type extending Message
|
|
472
|
+
* @param textDelta The text content to include in the delta update
|
|
473
|
+
* @returns An AgentResponseDelta with the text delta wrapped in the expected structure
|
|
474
|
+
*/
|
|
475
|
+
function textDelta(textDelta) {
|
|
476
|
+
return { delta: { text: textDelta } };
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Creates a JSON delta for streaming responses
|
|
480
|
+
*
|
|
481
|
+
* This utility function creates an AgentResponseDelta object with only the JSON part,
|
|
482
|
+
* useful for incrementally building structured data responses in streaming mode.
|
|
483
|
+
*
|
|
484
|
+
* @template T Agent message type extending Message
|
|
485
|
+
* @param jsonDelta The JSON data to include in the delta update
|
|
486
|
+
* @returns An AgentResponseDelta with the JSON delta wrapped in the expected structure
|
|
487
|
+
*/
|
|
488
|
+
function jsonDelta(jsonDelta) {
|
|
489
|
+
return { delta: { json: jsonDelta } };
|
|
490
|
+
}
|
|
224
491
|
function checkAgentInputOutputSchema(schema) {
|
|
225
492
|
if (!(schema instanceof zod_1.ZodObject) && typeof schema !== "function") {
|
|
226
493
|
throw new Error(`schema must be a zod object or function return a zod object, got: ${typeof schema}`);
|
|
227
494
|
}
|
|
228
495
|
}
|
|
496
|
+
/**
|
|
497
|
+
* Function agent class, implements agent logic through a function
|
|
498
|
+
*
|
|
499
|
+
* Provides a convenient way to create agents using functions without
|
|
500
|
+
* needing to extend the Agent class
|
|
501
|
+
*
|
|
502
|
+
* @template I Agent input message type
|
|
503
|
+
* @template O Agent output message type
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* Here's an example of creating a function agent:
|
|
507
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
|
|
508
|
+
*/
|
|
229
509
|
class FunctionAgent extends Agent {
|
|
510
|
+
/**
|
|
511
|
+
* Create a function agent from a function or options
|
|
512
|
+
*
|
|
513
|
+
* Provides a convenient factory method to create an agent directly from a function
|
|
514
|
+
*
|
|
515
|
+
* @param options Function agent options or function
|
|
516
|
+
* @returns New function agent instance
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* Here's an example of creating a function agent from a function:
|
|
520
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-function-agent-from-function}
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* Here's an example of creating a function agent without basic agent options:
|
|
524
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* Here's an example of creating a function agent from a function returning a stream:
|
|
528
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-function-agent-stream}
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* Here's an example of creating a function agent from a function returning an async generator:
|
|
532
|
+
* {@includeCode ../../test/agents/agent.test.ts#example-function-agent-async-generator}
|
|
533
|
+
*/
|
|
230
534
|
static from(options) {
|
|
231
535
|
return typeof options === "function" ? functionToAgent(options) : new FunctionAgent(options);
|
|
232
536
|
}
|
|
537
|
+
/**
|
|
538
|
+
* Create a function agent instance
|
|
539
|
+
*
|
|
540
|
+
* @param options Function agent configuration options
|
|
541
|
+
*/
|
|
233
542
|
constructor(options) {
|
|
234
543
|
super(options);
|
|
235
|
-
this.
|
|
544
|
+
this._process = options.process;
|
|
236
545
|
}
|
|
237
|
-
|
|
546
|
+
/**
|
|
547
|
+
* Stores the function used to process agent input and generate output
|
|
548
|
+
*
|
|
549
|
+
* @private
|
|
550
|
+
*/
|
|
551
|
+
_process;
|
|
552
|
+
/**
|
|
553
|
+
* Process input implementation, calls the configured processing function
|
|
554
|
+
*
|
|
555
|
+
* @param input Input message
|
|
556
|
+
* @param context Execution context
|
|
557
|
+
* @returns Processing result
|
|
558
|
+
*/
|
|
238
559
|
process(input, context) {
|
|
239
|
-
return this.
|
|
560
|
+
return this._process(input, context);
|
|
240
561
|
}
|
|
241
562
|
}
|
|
242
563
|
exports.FunctionAgent = FunctionAgent;
|
|
243
564
|
function functionToAgent(agent) {
|
|
244
565
|
if (typeof agent === "function") {
|
|
245
|
-
return FunctionAgent.from({ name: agent.name,
|
|
566
|
+
return FunctionAgent.from({ name: agent.name, process: agent });
|
|
246
567
|
}
|
|
247
568
|
return agent;
|
|
248
569
|
}
|