@aigne/core 1.72.0-beta.2 → 1.72.0-beta.23
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 +261 -0
- package/lib/cjs/agents/agent.d.ts +42 -11
- package/lib/cjs/agents/agent.js +34 -8
- package/lib/cjs/agents/ai-agent.d.ts +63 -4
- package/lib/cjs/agents/ai-agent.js +154 -20
- package/lib/cjs/agents/chat-model.d.ts +157 -0
- package/lib/cjs/agents/chat-model.js +71 -6
- package/lib/cjs/agents/image-agent.d.ts +17 -1
- package/lib/cjs/agents/image-agent.js +16 -0
- package/lib/cjs/agents/image-model.d.ts +12 -2
- package/lib/cjs/agents/image-model.js +1 -1
- package/lib/cjs/agents/mcp-agent.d.ts +17 -0
- package/lib/cjs/agents/mcp-agent.js +18 -0
- package/lib/cjs/agents/model.d.ts +3 -3
- package/lib/cjs/agents/model.js +2 -2
- package/lib/cjs/agents/team-agent.d.ts +55 -0
- package/lib/cjs/agents/team-agent.js +31 -0
- package/lib/cjs/agents/transform-agent.d.ts +12 -0
- package/lib/cjs/agents/transform-agent.js +13 -0
- package/lib/cjs/agents/video-model.d.ts +10 -0
- package/lib/cjs/agents/video-model.js +1 -1
- package/lib/cjs/aigne/context.js +1 -3
- package/lib/cjs/aigne/usage.d.ts +4 -0
- package/lib/cjs/aigne/usage.js +6 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/loader/agent-yaml.d.ts +5 -63
- package/lib/cjs/loader/agent-yaml.js +4 -129
- package/lib/cjs/loader/agents.d.ts +4 -0
- package/lib/cjs/loader/agents.js +17 -0
- package/lib/cjs/loader/index.d.ts +16 -12
- package/lib/cjs/loader/index.js +20 -81
- package/lib/cjs/loader/schema.d.ts +21 -6
- package/lib/cjs/loader/schema.js +60 -1
- package/lib/cjs/memory/recorder.d.ts +4 -4
- package/lib/cjs/memory/retriever.d.ts +4 -4
- package/lib/cjs/prompt/agent-session.d.ts +163 -0
- package/lib/cjs/prompt/agent-session.js +1008 -0
- package/lib/cjs/prompt/compact/compactor.d.ts +7 -0
- package/lib/cjs/prompt/compact/compactor.js +52 -0
- package/lib/cjs/prompt/compact/session-memory-extractor.d.ts +7 -0
- package/lib/cjs/prompt/compact/session-memory-extractor.js +143 -0
- package/lib/cjs/prompt/compact/types.d.ts +336 -0
- package/lib/cjs/prompt/compact/types.js +53 -0
- package/lib/cjs/prompt/compact/user-memory-extractor.d.ts +7 -0
- package/lib/cjs/prompt/compact/user-memory-extractor.js +124 -0
- package/lib/cjs/prompt/context/afs/history.d.ts +5 -1
- package/lib/cjs/prompt/context/afs/history.js +3 -2
- package/lib/cjs/prompt/context/afs/index.js +8 -1
- package/lib/cjs/prompt/prompt-builder.d.ts +11 -9
- package/lib/cjs/prompt/prompt-builder.js +79 -120
- package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.d.ts +19 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.js +69 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.d.ts +12 -0
- package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.js +50 -0
- package/lib/cjs/prompt/skills/afs/delete.js +15 -3
- package/lib/cjs/prompt/skills/afs/edit.d.ts +6 -9
- package/lib/cjs/prompt/skills/afs/edit.js +85 -59
- package/lib/cjs/prompt/skills/afs/exec.js +17 -6
- package/lib/cjs/prompt/skills/afs/index.js +4 -1
- package/lib/cjs/prompt/skills/afs/list.d.ts +2 -0
- package/lib/cjs/prompt/skills/afs/list.js +35 -11
- package/lib/cjs/prompt/skills/afs/read.d.ts +9 -3
- package/lib/cjs/prompt/skills/afs/read.js +67 -15
- package/lib/cjs/prompt/skills/afs/rename.js +18 -4
- package/lib/cjs/prompt/skills/afs/search.js +21 -5
- package/lib/cjs/prompt/skills/afs/write.js +20 -6
- package/lib/cjs/prompt/template.d.ts +84 -9
- package/lib/cjs/prompt/template.js +46 -17
- package/lib/cjs/utils/mcp-utils.js +1 -1
- package/lib/cjs/utils/token-estimator.js +1 -1
- package/lib/dts/agents/agent.d.ts +42 -11
- package/lib/dts/agents/ai-agent.d.ts +63 -4
- package/lib/dts/agents/chat-model.d.ts +157 -0
- package/lib/dts/agents/image-agent.d.ts +17 -1
- package/lib/dts/agents/image-model.d.ts +12 -2
- package/lib/dts/agents/mcp-agent.d.ts +17 -0
- package/lib/dts/agents/model.d.ts +3 -3
- package/lib/dts/agents/team-agent.d.ts +55 -0
- package/lib/dts/agents/transform-agent.d.ts +12 -0
- package/lib/dts/agents/video-model.d.ts +10 -0
- package/lib/dts/aigne/context.d.ts +2 -2
- package/lib/dts/aigne/usage.d.ts +4 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/loader/agent-yaml.d.ts +5 -63
- package/lib/dts/loader/agents.d.ts +4 -0
- package/lib/dts/loader/index.d.ts +16 -12
- package/lib/dts/loader/schema.d.ts +21 -6
- package/lib/dts/memory/recorder.d.ts +4 -4
- package/lib/dts/memory/retriever.d.ts +4 -4
- package/lib/dts/prompt/agent-session.d.ts +163 -0
- package/lib/dts/prompt/compact/compactor.d.ts +7 -0
- package/lib/dts/prompt/compact/session-memory-extractor.d.ts +7 -0
- package/lib/dts/prompt/compact/types.d.ts +336 -0
- package/lib/dts/prompt/compact/user-memory-extractor.d.ts +7 -0
- package/lib/dts/prompt/context/afs/history.d.ts +5 -1
- package/lib/dts/prompt/prompt-builder.d.ts +11 -9
- package/lib/dts/prompt/skills/afs/agent-skill/agent-skill.d.ts +19 -0
- package/lib/dts/prompt/skills/afs/agent-skill/skill-loader.d.ts +12 -0
- package/lib/dts/prompt/skills/afs/edit.d.ts +6 -9
- package/lib/dts/prompt/skills/afs/list.d.ts +2 -0
- package/lib/dts/prompt/skills/afs/read.d.ts +9 -3
- package/lib/dts/prompt/template.d.ts +84 -9
- package/lib/esm/agents/agent.d.ts +42 -11
- package/lib/esm/agents/agent.js +34 -8
- package/lib/esm/agents/ai-agent.d.ts +63 -4
- package/lib/esm/agents/ai-agent.js +154 -20
- package/lib/esm/agents/chat-model.d.ts +157 -0
- package/lib/esm/agents/chat-model.js +70 -5
- package/lib/esm/agents/image-agent.d.ts +17 -1
- package/lib/esm/agents/image-agent.js +16 -0
- package/lib/esm/agents/image-model.d.ts +12 -2
- package/lib/esm/agents/image-model.js +1 -1
- package/lib/esm/agents/mcp-agent.d.ts +17 -0
- package/lib/esm/agents/mcp-agent.js +18 -0
- package/lib/esm/agents/model.d.ts +3 -3
- package/lib/esm/agents/model.js +2 -2
- package/lib/esm/agents/team-agent.d.ts +55 -0
- package/lib/esm/agents/team-agent.js +31 -0
- package/lib/esm/agents/transform-agent.d.ts +12 -0
- package/lib/esm/agents/transform-agent.js +13 -0
- package/lib/esm/agents/video-model.d.ts +10 -0
- package/lib/esm/agents/video-model.js +1 -1
- package/lib/esm/aigne/context.d.ts +2 -2
- package/lib/esm/aigne/context.js +2 -4
- package/lib/esm/aigne/usage.d.ts +4 -0
- package/lib/esm/aigne/usage.js +6 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/loader/agent-yaml.d.ts +5 -63
- package/lib/esm/loader/agent-yaml.js +4 -128
- package/lib/esm/loader/agents.d.ts +4 -0
- package/lib/esm/loader/agents.js +14 -0
- package/lib/esm/loader/index.d.ts +16 -12
- package/lib/esm/loader/index.js +21 -81
- package/lib/esm/loader/schema.d.ts +21 -6
- package/lib/esm/loader/schema.js +57 -0
- package/lib/esm/memory/recorder.d.ts +4 -4
- package/lib/esm/memory/retriever.d.ts +4 -4
- package/lib/esm/prompt/agent-session.d.ts +163 -0
- package/lib/esm/prompt/agent-session.js +968 -0
- package/lib/esm/prompt/compact/compactor.d.ts +7 -0
- package/lib/esm/prompt/compact/compactor.js +48 -0
- package/lib/esm/prompt/compact/session-memory-extractor.d.ts +7 -0
- package/lib/esm/prompt/compact/session-memory-extractor.js +139 -0
- package/lib/esm/prompt/compact/types.d.ts +336 -0
- package/lib/esm/prompt/compact/types.js +50 -0
- package/lib/esm/prompt/compact/user-memory-extractor.d.ts +7 -0
- package/lib/esm/prompt/compact/user-memory-extractor.js +120 -0
- package/lib/esm/prompt/context/afs/history.d.ts +5 -1
- package/lib/esm/prompt/context/afs/history.js +3 -2
- package/lib/esm/prompt/context/afs/index.js +8 -1
- package/lib/esm/prompt/prompt-builder.d.ts +11 -9
- package/lib/esm/prompt/prompt-builder.js +80 -121
- package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.d.ts +19 -0
- package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.js +65 -0
- package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.d.ts +12 -0
- package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.js +43 -0
- package/lib/esm/prompt/skills/afs/delete.js +15 -3
- package/lib/esm/prompt/skills/afs/edit.d.ts +6 -9
- package/lib/esm/prompt/skills/afs/edit.js +85 -59
- package/lib/esm/prompt/skills/afs/exec.js +17 -6
- package/lib/esm/prompt/skills/afs/index.js +4 -1
- package/lib/esm/prompt/skills/afs/list.d.ts +2 -0
- package/lib/esm/prompt/skills/afs/list.js +35 -11
- package/lib/esm/prompt/skills/afs/read.d.ts +9 -3
- package/lib/esm/prompt/skills/afs/read.js +67 -15
- package/lib/esm/prompt/skills/afs/rename.js +18 -4
- package/lib/esm/prompt/skills/afs/search.js +21 -5
- package/lib/esm/prompt/skills/afs/write.js +20 -6
- package/lib/esm/prompt/template.d.ts +84 -9
- package/lib/esm/prompt/template.js +46 -17
- package/lib/esm/utils/mcp-utils.js +1 -1
- package/lib/esm/utils/token-estimator.js +1 -1
- package/package.json +7 -6
|
@@ -35,10 +35,13 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = exports.AIAgentToolChoice = exports.DEFAULT_OUTPUT_FILE_KEY = exports.DEFAULT_OUTPUT_KEY = void 0;
|
|
37
37
|
const zod_1 = require("zod");
|
|
38
|
+
const agent_yaml_js_1 = require("../loader/agent-yaml.js");
|
|
39
|
+
const schema_js_1 = require("../loader/schema.js");
|
|
38
40
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
39
41
|
const structured_stream_instructions_js_1 = require("../prompt/prompts/structured-stream-instructions.js");
|
|
40
|
-
const
|
|
42
|
+
const agent_skill_js_1 = require("../prompt/skills/afs/agent-skill/agent-skill.js");
|
|
41
43
|
const fastq = __importStar(require("../utils/queue.js"));
|
|
44
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
42
45
|
const structured_stream_extractor_js_1 = require("../utils/structured-stream-extractor.js");
|
|
43
46
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
44
47
|
const agent_js_1 = require("./agent.js");
|
|
@@ -120,6 +123,91 @@ exports.aiAgentOptionsSchema = agent_js_1.agentOptionsSchema.extend({
|
|
|
120
123
|
*/
|
|
121
124
|
class AIAgent extends agent_js_1.Agent {
|
|
122
125
|
tag = "AIAgent";
|
|
126
|
+
static schema({ filepath }) {
|
|
127
|
+
const instructionsSchema = (0, schema_js_1.getInstructionsSchema)({ filepath });
|
|
128
|
+
const nestAgentSchema = (0, agent_yaml_js_1.getNestAgentSchema)({ filepath });
|
|
129
|
+
const schema = (0, schema_js_1.camelizeSchema)(zod_1.z.object({
|
|
130
|
+
instructions: (0, schema_js_1.optionalize)(instructionsSchema),
|
|
131
|
+
inputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
|
|
132
|
+
outputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
|
|
133
|
+
inputFileKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
|
|
134
|
+
outputFileKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
|
|
135
|
+
toolChoice: (0, schema_js_1.optionalize)(zod_1.z.nativeEnum(AIAgentToolChoice)),
|
|
136
|
+
toolCallsConcurrency: (0, schema_js_1.optionalize)(zod_1.z.number().int().min(0)),
|
|
137
|
+
keepTextInToolUses: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
138
|
+
catchToolsError: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
139
|
+
structuredStreamMode: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
140
|
+
session: (0, schema_js_1.optionalize)((0, schema_js_1.camelizeSchema)(zod_1.z.object({
|
|
141
|
+
mode: (0, schema_js_1.optionalize)(zod_1.z.enum(["auto", "disabled"])),
|
|
142
|
+
sessionMemory: (0, schema_js_1.optionalize)((0, schema_js_1.camelizeSchema)(zod_1.z.object({
|
|
143
|
+
mode: (0, schema_js_1.optionalize)(zod_1.z.enum(["auto", "disabled"])),
|
|
144
|
+
memoryRatio: (0, schema_js_1.optionalize)(zod_1.z.number().min(0).max(1)),
|
|
145
|
+
queryLimit: (0, schema_js_1.optionalize)(zod_1.z.number().int().min(0)),
|
|
146
|
+
async: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
147
|
+
extractor: (0, schema_js_1.optionalize)(nestAgentSchema),
|
|
148
|
+
}))),
|
|
149
|
+
userMemory: (0, schema_js_1.optionalize)((0, schema_js_1.camelizeSchema)(zod_1.z.object({
|
|
150
|
+
mode: (0, schema_js_1.optionalize)(zod_1.z.enum(["auto", "disabled"])),
|
|
151
|
+
memoryRatio: (0, schema_js_1.optionalize)(zod_1.z.number().min(0).max(1)),
|
|
152
|
+
queryLimit: (0, schema_js_1.optionalize)(zod_1.z.number().int().min(0)),
|
|
153
|
+
async: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
154
|
+
extractor: (0, schema_js_1.optionalize)(nestAgentSchema),
|
|
155
|
+
}))),
|
|
156
|
+
compact: (0, schema_js_1.optionalize)((0, schema_js_1.camelizeSchema)(zod_1.z.object({
|
|
157
|
+
mode: (0, schema_js_1.optionalize)(zod_1.z.enum(["auto", "disabled"])),
|
|
158
|
+
maxTokens: zod_1.z.number().int().min(0).optional(),
|
|
159
|
+
keepRecentRatio: (0, schema_js_1.optionalize)(zod_1.z.number().min(0).max(1)),
|
|
160
|
+
async: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
161
|
+
compactor: (0, schema_js_1.optionalize)(nestAgentSchema),
|
|
162
|
+
}))),
|
|
163
|
+
}))),
|
|
164
|
+
}));
|
|
165
|
+
return schema;
|
|
166
|
+
}
|
|
167
|
+
static async load(options) {
|
|
168
|
+
const schema = AIAgent.schema(options);
|
|
169
|
+
const valid = await schema.parseAsync(options.parsed);
|
|
170
|
+
// Load nested agents from session config if present
|
|
171
|
+
const sessionCompactor = valid.session?.compact?.compactor
|
|
172
|
+
? await options.options?.loadNestAgent(options.filepath, valid.session.compact.compactor, options.options)
|
|
173
|
+
: undefined;
|
|
174
|
+
const sessionMemoryExtractor = valid.session?.sessionMemory?.extractor
|
|
175
|
+
? await options.options?.loadNestAgent(options.filepath, valid.session.sessionMemory.extractor, options.options)
|
|
176
|
+
: undefined;
|
|
177
|
+
const userMemoryExtractor = valid.session?.userMemory?.extractor
|
|
178
|
+
? await options.options?.loadNestAgent(options.filepath, valid.session.userMemory.extractor, options.options)
|
|
179
|
+
: undefined;
|
|
180
|
+
// Build session configuration with loaded agents
|
|
181
|
+
const sessionConfig = valid.session
|
|
182
|
+
? {
|
|
183
|
+
...valid.session,
|
|
184
|
+
compact: valid.session.compact
|
|
185
|
+
? {
|
|
186
|
+
...valid.session.compact,
|
|
187
|
+
compactor: sessionCompactor,
|
|
188
|
+
}
|
|
189
|
+
: undefined,
|
|
190
|
+
sessionMemory: valid.session.sessionMemory
|
|
191
|
+
? {
|
|
192
|
+
...valid.session.sessionMemory,
|
|
193
|
+
extractor: sessionMemoryExtractor,
|
|
194
|
+
}
|
|
195
|
+
: undefined,
|
|
196
|
+
userMemory: valid.session.userMemory
|
|
197
|
+
? {
|
|
198
|
+
...valid.session.userMemory,
|
|
199
|
+
extractor: userMemoryExtractor,
|
|
200
|
+
}
|
|
201
|
+
: undefined,
|
|
202
|
+
}
|
|
203
|
+
: undefined;
|
|
204
|
+
return new AIAgent({
|
|
205
|
+
...options.parsed,
|
|
206
|
+
...valid,
|
|
207
|
+
instructions: valid.instructions && (0, schema_js_1.instructionsToPromptBuilder)(valid.instructions),
|
|
208
|
+
session: sessionConfig,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
123
211
|
/**
|
|
124
212
|
* Create an AIAgent with the specified options
|
|
125
213
|
*
|
|
@@ -147,8 +235,6 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
147
235
|
typeof options.instructions === "string"
|
|
148
236
|
? prompt_builder_js_1.PromptBuilder.from(options.instructions)
|
|
149
237
|
: (options.instructions ?? new prompt_builder_js_1.PromptBuilder());
|
|
150
|
-
this.autoReorderSystemMessages = options.autoReorderSystemMessages ?? true;
|
|
151
|
-
this.autoMergeSystemMessages = options.autoMergeSystemMessages ?? true;
|
|
152
238
|
this.inputKey = options.inputKey;
|
|
153
239
|
this.inputFileKey = options.inputFileKey;
|
|
154
240
|
this.outputKey = options.outputKey || exports.DEFAULT_OUTPUT_KEY;
|
|
@@ -160,6 +246,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
160
246
|
this.memoryAgentsAsTools = options.memoryAgentsAsTools;
|
|
161
247
|
this.memoryPromptTemplate = options.memoryPromptTemplate;
|
|
162
248
|
this.useMemoriesFromContext = options.useMemoriesFromContext;
|
|
249
|
+
this.session = options.session;
|
|
163
250
|
if (typeof options.catchToolsError === "boolean")
|
|
164
251
|
this.catchToolsError = options.catchToolsError;
|
|
165
252
|
this.structuredStreamMode = options.structuredStreamMode;
|
|
@@ -185,8 +272,6 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
185
272
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
186
273
|
*/
|
|
187
274
|
instructions;
|
|
188
|
-
autoReorderSystemMessages;
|
|
189
|
-
autoMergeSystemMessages;
|
|
190
275
|
/**
|
|
191
276
|
* Pick a message from input to use as the user's message
|
|
192
277
|
*/
|
|
@@ -277,6 +362,10 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
277
362
|
* which outputs structured data in YAML format within <metadata> tags.
|
|
278
363
|
*/
|
|
279
364
|
customStructuredStreamInstructions;
|
|
365
|
+
/**
|
|
366
|
+
* Agent session configuration
|
|
367
|
+
*/
|
|
368
|
+
session;
|
|
280
369
|
get inputSchema() {
|
|
281
370
|
let schema = super.inputSchema;
|
|
282
371
|
if (this.inputKey) {
|
|
@@ -303,7 +392,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
303
392
|
const model = this.model || options.model || options.context.model;
|
|
304
393
|
if (!model)
|
|
305
394
|
throw new Error("model is required to run AIAgent");
|
|
306
|
-
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
395
|
+
const { toolAgents, session, userMessage, ...modelInput } = await this.instructions.build({
|
|
307
396
|
...options,
|
|
308
397
|
agent: this,
|
|
309
398
|
input,
|
|
@@ -312,25 +401,35 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
312
401
|
modelInput.modelOptions = await model.getModelOptions(input, options);
|
|
313
402
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
314
403
|
if (this.toolChoice === "router") {
|
|
315
|
-
return yield* this._processRouter(input, model, modelInput, options, toolsMap);
|
|
404
|
+
return yield* this._processRouter(input, model, { messages: [...(await session.getMessages()), userMessage], ...modelInput }, options, toolsMap);
|
|
405
|
+
}
|
|
406
|
+
const inputMessage = this.inputKey ? input[this.inputKey] : undefined;
|
|
407
|
+
if (inputMessage) {
|
|
408
|
+
yield {
|
|
409
|
+
progress: {
|
|
410
|
+
event: "message",
|
|
411
|
+
message: { role: "user", content: [{ type: "text", text: inputMessage }] },
|
|
412
|
+
},
|
|
413
|
+
};
|
|
316
414
|
}
|
|
317
|
-
|
|
415
|
+
await session.startMessage(input, userMessage, options);
|
|
416
|
+
// const toolCallMessages: ChatModelInputMessage[] = [];
|
|
318
417
|
const outputKey = this.outputKey;
|
|
319
418
|
for (;;) {
|
|
320
419
|
const modelOutput = {};
|
|
321
|
-
let stream = await this.invokeChildAgent(model, {
|
|
420
|
+
let stream = await this.invokeChildAgent(model, { messages: await session.getMessages(), ...modelInput }, { ...options, streaming: true });
|
|
322
421
|
if (this.structuredStreamMode) {
|
|
323
422
|
const { metadataStart, metadataEnd, parse } = this.customStructuredStreamInstructions || structured_stream_instructions_js_1.STRUCTURED_STREAM_INSTRUCTIONS;
|
|
324
423
|
stream = stream.pipeThrough(new structured_stream_extractor_js_1.ExtractMetadataTransform({ start: metadataStart, end: metadataEnd, parse }));
|
|
325
424
|
}
|
|
326
425
|
let isTextIgnored = false;
|
|
327
426
|
for await (const value of stream) {
|
|
427
|
+
(0, stream_utils_js_1.mergeAgentResponseChunk)(modelOutput, value);
|
|
328
428
|
if ((0, agent_js_1.isAgentResponseDelta)(value)) {
|
|
329
429
|
if (!isTextIgnored && value.delta.text?.text) {
|
|
330
430
|
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
331
431
|
}
|
|
332
432
|
if (value.delta.json) {
|
|
333
|
-
Object.assign(modelOutput, value.delta.json);
|
|
334
433
|
if (this.structuredStreamMode) {
|
|
335
434
|
yield { delta: { json: value.delta.json.json } };
|
|
336
435
|
if (!isTextIgnored && modelOutput.json && this.ignoreTextOfStructuredStreamMode) {
|
|
@@ -340,14 +439,39 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
340
439
|
}
|
|
341
440
|
}
|
|
342
441
|
}
|
|
343
|
-
const { toolCalls, json, text, files } = modelOutput;
|
|
344
|
-
if (
|
|
442
|
+
const { toolCalls, json, text, thoughts, files } = modelOutput;
|
|
443
|
+
if (text || thoughts) {
|
|
444
|
+
const content = [];
|
|
445
|
+
if (thoughts) {
|
|
446
|
+
content.push({ type: "text", text: thoughts, isThinking: true });
|
|
447
|
+
}
|
|
448
|
+
if (text) {
|
|
449
|
+
content.push({ type: "text", text });
|
|
450
|
+
}
|
|
451
|
+
if (content.length) {
|
|
452
|
+
const message = { role: "agent", content };
|
|
453
|
+
yield { progress: { event: "message", message } };
|
|
454
|
+
await session.appendCurrentMessages(message, options);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const toolCallsWithTools = toolCalls?.map((call) => {
|
|
458
|
+
const tool = toolsMap.get(call.function.name);
|
|
459
|
+
if (!tool)
|
|
460
|
+
throw new Error(`Tool not found: ${call.function.name}`);
|
|
461
|
+
return {
|
|
462
|
+
...call,
|
|
463
|
+
tool,
|
|
464
|
+
};
|
|
465
|
+
});
|
|
466
|
+
if (toolCallsWithTools?.length) {
|
|
345
467
|
if (this.keepTextInToolUses !== true) {
|
|
346
468
|
yield { delta: { json: { [outputKey]: "" } } };
|
|
347
469
|
}
|
|
348
470
|
else {
|
|
349
|
-
yield { delta: { text: { [outputKey]: "\n" } } };
|
|
471
|
+
yield { delta: { text: { [outputKey]: "\n\n" } } };
|
|
350
472
|
}
|
|
473
|
+
const toolCallMessage = { role: "agent", toolCalls };
|
|
474
|
+
yield { progress: { event: "message", message: toolCallMessage } };
|
|
351
475
|
const executedToolCalls = [];
|
|
352
476
|
let error;
|
|
353
477
|
const queue = fastq.promise(async ({ tool, call }) => {
|
|
@@ -364,7 +488,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
364
488
|
},
|
|
365
489
|
};
|
|
366
490
|
});
|
|
367
|
-
executedToolCalls.push({ call, output });
|
|
491
|
+
executedToolCalls.push({ call, tool, output });
|
|
368
492
|
}
|
|
369
493
|
catch (e) {
|
|
370
494
|
error = e;
|
|
@@ -372,21 +496,30 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
372
496
|
}
|
|
373
497
|
}, this.toolCallsConcurrency || 1);
|
|
374
498
|
// Execute tools
|
|
375
|
-
for (const call of
|
|
376
|
-
|
|
377
|
-
if (!tool)
|
|
378
|
-
throw new Error(`Tool not found: ${call.function.name}`);
|
|
379
|
-
queue.push({ tool, call });
|
|
499
|
+
for (const call of toolCallsWithTools) {
|
|
500
|
+
queue.push({ tool: call.tool, call });
|
|
380
501
|
}
|
|
381
502
|
await queue.drained();
|
|
382
503
|
if (error)
|
|
383
504
|
throw error;
|
|
505
|
+
const toolResultMessages = [];
|
|
384
506
|
// Continue LLM function calling loop if any tools were executed
|
|
385
507
|
if (executedToolCalls.length) {
|
|
508
|
+
for (const { call, tool, output } of executedToolCalls) {
|
|
509
|
+
const isAgentSkill = !output.isError && tool instanceof agent_skill_js_1.AgentSkill ? true : undefined;
|
|
510
|
+
const text = await tool.formatOutput(output);
|
|
511
|
+
const message = {
|
|
512
|
+
role: "tool",
|
|
513
|
+
toolCallId: call.id,
|
|
514
|
+
content: [{ type: "text", text, isAgentSkill }],
|
|
515
|
+
};
|
|
516
|
+
yield { progress: { event: "message", message: message } };
|
|
517
|
+
toolResultMessages.push(message);
|
|
518
|
+
}
|
|
386
519
|
const transferOutput = executedToolCalls.find((i) => (0, types_js_1.isTransferAgentOutput)(i.output))?.output;
|
|
387
520
|
if (transferOutput)
|
|
388
521
|
return transferOutput;
|
|
389
|
-
|
|
522
|
+
await session.appendCurrentMessages([toolCallMessage, ...toolResultMessages], options);
|
|
390
523
|
continue;
|
|
391
524
|
}
|
|
392
525
|
}
|
|
@@ -403,6 +536,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
403
536
|
if (!(0, type_utils_js_1.isEmpty)(result)) {
|
|
404
537
|
yield { delta: { json: result } };
|
|
405
538
|
}
|
|
539
|
+
await session.endMessage(result, undefined, options);
|
|
406
540
|
return;
|
|
407
541
|
}
|
|
408
542
|
}
|
|
@@ -57,7 +57,9 @@ export declare abstract class ChatModel extends Model<ChatModelInput, ChatModelO
|
|
|
57
57
|
getModelCapabilities(): {
|
|
58
58
|
supportsParallelToolCalls: boolean;
|
|
59
59
|
};
|
|
60
|
+
getModelOptions(input: Message, options: AgentInvokeOptions): Promise<ChatModelInputOptions>;
|
|
60
61
|
private validateToolNames;
|
|
62
|
+
countTokens(input: ChatModelInput): Promise<number>;
|
|
61
63
|
/**
|
|
62
64
|
* Normalizes tool names to ensure compatibility with language models
|
|
63
65
|
*
|
|
@@ -203,6 +205,13 @@ export interface ChatModelInputMessage {
|
|
|
203
205
|
* Name of the message sender (for multi-agent scenarios)
|
|
204
206
|
*/
|
|
205
207
|
name?: string;
|
|
208
|
+
/**
|
|
209
|
+
* Cache control marker for the entire message (only supported by Claude)
|
|
210
|
+
*
|
|
211
|
+
* This is syntactic sugar that applies cacheControl to the last content block
|
|
212
|
+
* of the message. See {@link CacheControl} for details.
|
|
213
|
+
*/
|
|
214
|
+
cacheControl?: CacheControl;
|
|
206
215
|
}
|
|
207
216
|
/**
|
|
208
217
|
* Type of input message content
|
|
@@ -218,27 +227,66 @@ export type ChatModelInputMessageContent = string | UnionContent[];
|
|
|
218
227
|
export type TextContent = {
|
|
219
228
|
type: "text";
|
|
220
229
|
text: string;
|
|
230
|
+
isThinking?: boolean;
|
|
231
|
+
isAgentSkill?: boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Cache control marker (only supported by Claude)
|
|
234
|
+
*
|
|
235
|
+
* When set, this content block will be marked as a cache breakpoint.
|
|
236
|
+
* See {@link CacheControl} for details.
|
|
237
|
+
*/
|
|
238
|
+
cacheControl?: CacheControl;
|
|
221
239
|
};
|
|
222
240
|
export declare const textContentSchema: z.ZodObject<{
|
|
223
241
|
type: z.ZodLiteral<"text">;
|
|
224
242
|
text: z.ZodString;
|
|
243
|
+
cacheControl: ZodType<{
|
|
244
|
+
type: "ephemeral";
|
|
245
|
+
ttl?: "5m" | "1h" | undefined;
|
|
246
|
+
} | undefined, z.ZodTypeDef, {
|
|
247
|
+
type: "ephemeral";
|
|
248
|
+
ttl?: "5m" | "1h" | undefined;
|
|
249
|
+
} | undefined>;
|
|
225
250
|
}, "strip", z.ZodTypeAny, {
|
|
226
251
|
type: "text";
|
|
227
252
|
text: string;
|
|
253
|
+
cacheControl?: {
|
|
254
|
+
type: "ephemeral";
|
|
255
|
+
ttl?: "5m" | "1h" | undefined;
|
|
256
|
+
} | undefined;
|
|
228
257
|
}, {
|
|
229
258
|
type: "text";
|
|
230
259
|
text: string;
|
|
260
|
+
cacheControl?: {
|
|
261
|
+
type: "ephemeral";
|
|
262
|
+
ttl?: "5m" | "1h" | undefined;
|
|
263
|
+
} | undefined;
|
|
231
264
|
}>;
|
|
232
265
|
export type UnionContent = TextContent | FileUnionContent;
|
|
233
266
|
export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
234
267
|
type: z.ZodLiteral<"text">;
|
|
235
268
|
text: z.ZodString;
|
|
269
|
+
cacheControl: ZodType<{
|
|
270
|
+
type: "ephemeral";
|
|
271
|
+
ttl?: "5m" | "1h" | undefined;
|
|
272
|
+
} | undefined, z.ZodTypeDef, {
|
|
273
|
+
type: "ephemeral";
|
|
274
|
+
ttl?: "5m" | "1h" | undefined;
|
|
275
|
+
} | undefined>;
|
|
236
276
|
}, "strip", z.ZodTypeAny, {
|
|
237
277
|
type: "text";
|
|
238
278
|
text: string;
|
|
279
|
+
cacheControl?: {
|
|
280
|
+
type: "ephemeral";
|
|
281
|
+
ttl?: "5m" | "1h" | undefined;
|
|
282
|
+
} | undefined;
|
|
239
283
|
}, {
|
|
240
284
|
type: "text";
|
|
241
285
|
text: string;
|
|
286
|
+
cacheControl?: {
|
|
287
|
+
type: "ephemeral";
|
|
288
|
+
ttl?: "5m" | "1h" | undefined;
|
|
289
|
+
} | undefined;
|
|
242
290
|
}>, z.ZodObject<{
|
|
243
291
|
filename: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
244
292
|
mimeType: ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
|
|
@@ -340,6 +388,14 @@ export interface ChatModelInputTool {
|
|
|
340
388
|
* For example, Gemini's thought_signature
|
|
341
389
|
*/
|
|
342
390
|
metadata?: Record<string, any>;
|
|
391
|
+
/**
|
|
392
|
+
* Cache control marker (only supported by Claude)
|
|
393
|
+
*
|
|
394
|
+
* When set, this tool definition will be marked as a cache breakpoint.
|
|
395
|
+
* Typically applied to the last tool in the tools array.
|
|
396
|
+
* See {@link CacheControl} for details.
|
|
397
|
+
*/
|
|
398
|
+
cacheControl?: CacheControl;
|
|
343
399
|
}
|
|
344
400
|
/**
|
|
345
401
|
* Tool selection strategy
|
|
@@ -362,6 +418,82 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
|
|
|
362
418
|
};
|
|
363
419
|
};
|
|
364
420
|
export type Modality = "text" | "image" | "audio";
|
|
421
|
+
/**
|
|
422
|
+
* Cache control marker for prompt caching
|
|
423
|
+
*
|
|
424
|
+
* Used to mark content blocks, messages, or tools for caching.
|
|
425
|
+
* Currently only supported by Anthropic (Claude) models.
|
|
426
|
+
*/
|
|
427
|
+
export interface CacheControl {
|
|
428
|
+
/**
|
|
429
|
+
* Cache type (currently only "ephemeral" is supported)
|
|
430
|
+
*/
|
|
431
|
+
type: "ephemeral";
|
|
432
|
+
/**
|
|
433
|
+
* Cache TTL (Time To Live)
|
|
434
|
+
* - "5m": 5 minutes (default)
|
|
435
|
+
* - "1h": 1 hour
|
|
436
|
+
*/
|
|
437
|
+
ttl?: "5m" | "1h";
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Cache configuration options
|
|
441
|
+
*
|
|
442
|
+
* Controls how prompt caching is used for supported providers.
|
|
443
|
+
* Prompt caching can significantly reduce costs and latency by reusing
|
|
444
|
+
* previously processed prompts (system messages, tool definitions, etc.).
|
|
445
|
+
*/
|
|
446
|
+
export interface CacheConfig {
|
|
447
|
+
/**
|
|
448
|
+
* Whether to enable prompt caching
|
|
449
|
+
*
|
|
450
|
+
* - OpenAI: Ignored (always enabled automatically)
|
|
451
|
+
* - Gemini: Controls explicit caching
|
|
452
|
+
* - Claude: Controls whether to add cache_control markers
|
|
453
|
+
*
|
|
454
|
+
* @default true
|
|
455
|
+
*/
|
|
456
|
+
enabled?: boolean;
|
|
457
|
+
/**
|
|
458
|
+
* Cache TTL (Time To Live)
|
|
459
|
+
*
|
|
460
|
+
* - OpenAI: Ignored (automatic)
|
|
461
|
+
* - Gemini: Supports custom seconds
|
|
462
|
+
* - Claude: Only supports "5m" or "1h"
|
|
463
|
+
*
|
|
464
|
+
* @default "5m"
|
|
465
|
+
*/
|
|
466
|
+
ttl?: "5m" | "1h" | number;
|
|
467
|
+
/**
|
|
468
|
+
* Caching strategy
|
|
469
|
+
*
|
|
470
|
+
* - "auto": Automatically add cache breakpoints at optimal locations
|
|
471
|
+
* - "manual": Require explicit cacheControl markers on messages/tools
|
|
472
|
+
*
|
|
473
|
+
* @default "auto"
|
|
474
|
+
*/
|
|
475
|
+
strategy?: "auto" | "manual";
|
|
476
|
+
/**
|
|
477
|
+
* Auto cache breakpoint locations (only effective when strategy="auto")
|
|
478
|
+
*
|
|
479
|
+
* @default { tools: true, system: true, lastMessage: false }
|
|
480
|
+
*/
|
|
481
|
+
autoBreakpoints?: {
|
|
482
|
+
/** Cache tool definitions */
|
|
483
|
+
tools?: boolean;
|
|
484
|
+
/** Cache system messages */
|
|
485
|
+
system?: boolean;
|
|
486
|
+
/** Cache last message in conversation history */
|
|
487
|
+
lastMessage?: boolean;
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Default cache configuration
|
|
492
|
+
*
|
|
493
|
+
* Enables automatic caching for system messages and tool definitions,
|
|
494
|
+
* which typically provides the best cost/performance tradeoff.
|
|
495
|
+
*/
|
|
496
|
+
export declare const DEFAULT_CACHE_CONFIG: CacheConfig;
|
|
365
497
|
/**
|
|
366
498
|
* Model-specific configuration options
|
|
367
499
|
*
|
|
@@ -395,6 +527,15 @@ export interface ChatModelInputOptions extends Record<string, unknown> {
|
|
|
395
527
|
modalities?: Modality[];
|
|
396
528
|
preferInputFileType?: "file" | "url";
|
|
397
529
|
reasoningEffort?: number | "minimal" | "low" | "medium" | "high";
|
|
530
|
+
/**
|
|
531
|
+
* Cache configuration for prompt caching
|
|
532
|
+
*
|
|
533
|
+
* Enables caching of system messages, tool definitions, and conversation history
|
|
534
|
+
* to reduce costs and latency. See {@link CacheConfig} for details.
|
|
535
|
+
*
|
|
536
|
+
* @default DEFAULT_CACHE_CONFIG (enabled with auto strategy)
|
|
537
|
+
*/
|
|
538
|
+
cacheConfig?: CacheConfig;
|
|
398
539
|
}
|
|
399
540
|
export type ChatModelInputOptionsWithGetter = GetterSchema<ChatModelInputOptions>;
|
|
400
541
|
/**
|
|
@@ -492,6 +633,16 @@ export interface ChatModelOutputUsage {
|
|
|
492
633
|
* AIGNE Hub credit usage
|
|
493
634
|
*/
|
|
494
635
|
aigneHubCredits?: number;
|
|
636
|
+
/**
|
|
637
|
+
* Number of tokens written to cache (first time caching)
|
|
638
|
+
* Only applicable for providers that support explicit cache creation (e.g., Anthropic)
|
|
639
|
+
*/
|
|
640
|
+
cacheCreationInputTokens?: number;
|
|
641
|
+
/**
|
|
642
|
+
* Number of tokens read from cache (cache hit)
|
|
643
|
+
* Supported by OpenAI, Anthropic, and Gemini
|
|
644
|
+
*/
|
|
645
|
+
cacheReadInputTokens?: number;
|
|
495
646
|
/**
|
|
496
647
|
* Credit prefix
|
|
497
648
|
*/
|
|
@@ -501,15 +652,21 @@ export declare const chatModelOutputUsageSchema: z.ZodObject<{
|
|
|
501
652
|
inputTokens: z.ZodNumber;
|
|
502
653
|
outputTokens: z.ZodNumber;
|
|
503
654
|
aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
|
|
655
|
+
cacheCreationInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
|
|
656
|
+
cacheReadInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
|
|
504
657
|
creditPrefix: ZodType<"$" | "€" | "¥" | undefined, z.ZodTypeDef, "$" | "€" | "¥" | undefined>;
|
|
505
658
|
}, "strip", z.ZodTypeAny, {
|
|
506
659
|
inputTokens: number;
|
|
507
660
|
outputTokens: number;
|
|
508
661
|
aigneHubCredits?: number | undefined;
|
|
662
|
+
cacheCreationInputTokens?: number | undefined;
|
|
663
|
+
cacheReadInputTokens?: number | undefined;
|
|
509
664
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
510
665
|
}, {
|
|
511
666
|
inputTokens: number;
|
|
512
667
|
outputTokens: number;
|
|
513
668
|
aigneHubCredits?: number | undefined;
|
|
669
|
+
cacheCreationInputTokens?: number | undefined;
|
|
670
|
+
cacheReadInputTokens?: number | undefined;
|
|
514
671
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
515
672
|
}>;
|