@aigne/core 1.72.0-beta.6 → 1.72.0-beta.8
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 +35 -0
- package/lib/cjs/agents/agent.d.ts +23 -27
- package/lib/cjs/agents/agent.js +16 -8
- package/lib/cjs/agents/ai-agent.d.ts +20 -4
- package/lib/cjs/agents/ai-agent.js +47 -55
- package/lib/cjs/agents/chat-model.d.ts +1 -0
- package/lib/cjs/agents/image-model.d.ts +4 -4
- package/lib/cjs/agents/mcp-agent.d.ts +2 -2
- package/lib/cjs/agents/video-model.d.ts +4 -4
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/loader/index.d.ts +2 -2
- 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 +53 -0
- package/lib/cjs/prompt/agent-session.js +341 -0
- package/lib/cjs/prompt/compact/compactor.d.ts +7 -0
- package/lib/cjs/prompt/compact/compactor.js +48 -0
- package/lib/cjs/prompt/compact/types.d.ts +79 -0
- package/lib/cjs/prompt/compact/types.js +19 -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 +77 -117
- package/lib/cjs/prompt/template.d.ts +16 -16
- package/lib/dts/agents/agent.d.ts +23 -27
- package/lib/dts/agents/ai-agent.d.ts +20 -4
- package/lib/dts/agents/chat-model.d.ts +1 -0
- package/lib/dts/agents/image-model.d.ts +4 -4
- package/lib/dts/agents/mcp-agent.d.ts +2 -2
- package/lib/dts/agents/video-model.d.ts +4 -4
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/loader/index.d.ts +2 -2
- 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 +53 -0
- package/lib/dts/prompt/compact/compactor.d.ts +7 -0
- package/lib/dts/prompt/compact/types.d.ts +79 -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/template.d.ts +16 -16
- package/lib/esm/agents/agent.d.ts +23 -27
- package/lib/esm/agents/agent.js +16 -8
- package/lib/esm/agents/ai-agent.d.ts +20 -4
- package/lib/esm/agents/ai-agent.js +47 -55
- package/lib/esm/agents/chat-model.d.ts +1 -0
- package/lib/esm/agents/image-model.d.ts +4 -4
- package/lib/esm/agents/mcp-agent.d.ts +2 -2
- package/lib/esm/agents/video-model.d.ts +4 -4
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/loader/index.d.ts +2 -2
- 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 +53 -0
- package/lib/esm/prompt/agent-session.js +304 -0
- package/lib/esm/prompt/compact/compactor.d.ts +7 -0
- package/lib/esm/prompt/compact/compactor.js +44 -0
- package/lib/esm/prompt/compact/types.d.ts +79 -0
- package/lib/esm/prompt/compact/types.js +16 -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 +78 -118
- package/lib/esm/prompt/template.d.ts +16 -16
- package/package.json +4 -4
package/lib/esm/agents/agent.js
CHANGED
|
@@ -104,7 +104,6 @@ export class Agent {
|
|
|
104
104
|
if (options.skills?.length)
|
|
105
105
|
this.skills.push(...options.skills.map(functionToAgent));
|
|
106
106
|
this.disableEvents = options.disableEvents;
|
|
107
|
-
this.historyConfig = options.historyConfig;
|
|
108
107
|
if (Array.isArray(options.memory)) {
|
|
109
108
|
this.memories.push(...options.memory);
|
|
110
109
|
}
|
|
@@ -131,15 +130,15 @@ export class Agent {
|
|
|
131
130
|
: options.retryOnError;
|
|
132
131
|
this.guideRails = options.guideRails;
|
|
133
132
|
}
|
|
133
|
+
afs;
|
|
134
|
+
tag;
|
|
134
135
|
/**
|
|
135
136
|
* List of memories this agent can use
|
|
136
137
|
*
|
|
137
138
|
* @deprecated use afs instead
|
|
138
139
|
*/
|
|
139
140
|
memories = [];
|
|
140
|
-
afs;
|
|
141
141
|
asyncMemoryRecord;
|
|
142
|
-
tag;
|
|
143
142
|
/**
|
|
144
143
|
* Maximum number of memory items to retrieve
|
|
145
144
|
*/
|
|
@@ -284,7 +283,6 @@ export class Agent {
|
|
|
284
283
|
* agentSucceed, or agentFailed
|
|
285
284
|
*/
|
|
286
285
|
disableEvents;
|
|
287
|
-
historyConfig;
|
|
288
286
|
subscriptions = [];
|
|
289
287
|
/**
|
|
290
288
|
* Attach agent to context:
|
|
@@ -415,11 +413,15 @@ export class Agent {
|
|
|
415
413
|
: isAsyncGenerator(response)
|
|
416
414
|
? asyncGeneratorToReadableStream(response)
|
|
417
415
|
: objectToAgentResponseStream(response);
|
|
416
|
+
const messages = [];
|
|
418
417
|
for await (const chunk of stream) {
|
|
419
418
|
mergeAgentResponseChunk(output, chunk);
|
|
420
419
|
yield chunk;
|
|
420
|
+
if (isAgentResponseProgress(chunk) && chunk.progress.event === "message") {
|
|
421
|
+
messages.push(chunk.progress.message);
|
|
422
|
+
}
|
|
421
423
|
}
|
|
422
|
-
let result = await this.processAgentOutput(input, output, options);
|
|
424
|
+
let result = await this.processAgentOutput(input, output, { ...options, messages });
|
|
423
425
|
if (attempt > 0) {
|
|
424
426
|
result = { ...result, $meta: { ...result.$meta, retries: attempt } };
|
|
425
427
|
}
|
|
@@ -516,7 +518,7 @@ export class Agent {
|
|
|
516
518
|
* @param options Invocation options
|
|
517
519
|
* @returns Final processed output
|
|
518
520
|
*/
|
|
519
|
-
async processAgentOutput(input, output, options) {
|
|
521
|
+
async processAgentOutput(input, output, { messages, ...options }) {
|
|
520
522
|
const { context } = options;
|
|
521
523
|
if (!isRecord(output)) {
|
|
522
524
|
throw new Error(`expect to return a record type such as {result: ...}, but got (${typeof output}): ${output}`);
|
|
@@ -528,8 +530,14 @@ export class Agent {
|
|
|
528
530
|
const o = await this.callHooks(["onSuccess", "onEnd"], { input, output: finalOutput }, options);
|
|
529
531
|
if (o?.output)
|
|
530
532
|
finalOutput = o.output;
|
|
531
|
-
|
|
532
|
-
this.
|
|
533
|
+
this.afs?.emit("agentSucceed", {
|
|
534
|
+
agentId: this.name,
|
|
535
|
+
userId: context.userContext.userId,
|
|
536
|
+
sessionId: context.userContext.sessionId,
|
|
537
|
+
input,
|
|
538
|
+
output: finalOutput,
|
|
539
|
+
messages,
|
|
540
|
+
});
|
|
533
541
|
if (!this.disableEvents)
|
|
534
542
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
535
543
|
return finalOutput;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
|
+
import { type NestAgentSchema } from "../loader/agent-yaml.js";
|
|
3
|
+
import type { AgentLoadOptions } from "../loader/index.js";
|
|
4
|
+
import { type Instructions } from "../loader/schema.js";
|
|
5
|
+
import type { CompactConfig } from "../prompt/agent-session.js";
|
|
2
6
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
3
7
|
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type AgentProcessResult, type Message } from "./agent.js";
|
|
4
8
|
import type { ChatModel, ChatModelInput } from "./chat-model.js";
|
|
@@ -23,8 +27,6 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
|
|
|
23
27
|
* more complex prompt templates
|
|
24
28
|
*/
|
|
25
29
|
instructions?: string | PromptBuilder;
|
|
26
|
-
autoReorderSystemMessages?: boolean;
|
|
27
|
-
autoMergeSystemMessages?: boolean;
|
|
28
30
|
/**
|
|
29
31
|
* Pick a message from input to use as the user's message
|
|
30
32
|
*/
|
|
@@ -124,6 +126,20 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
|
|
|
124
126
|
*/
|
|
125
127
|
memoryPromptTemplate?: string;
|
|
126
128
|
useMemoriesFromContext?: boolean;
|
|
129
|
+
compact?: CompactConfig;
|
|
130
|
+
}
|
|
131
|
+
export interface AIAgentLoadSchema {
|
|
132
|
+
instructions?: Instructions;
|
|
133
|
+
inputKey?: string;
|
|
134
|
+
inputFileKey?: string;
|
|
135
|
+
outputKey?: string;
|
|
136
|
+
outputFileKey?: string;
|
|
137
|
+
toolChoice?: AIAgentToolChoice;
|
|
138
|
+
toolCallsConcurrency?: number;
|
|
139
|
+
keepTextInToolUses?: boolean;
|
|
140
|
+
compact?: Omit<CompactConfig, "compactor"> & {
|
|
141
|
+
compactor?: NestAgentSchema;
|
|
142
|
+
};
|
|
127
143
|
}
|
|
128
144
|
/**
|
|
129
145
|
* Tool choice options for AI agents
|
|
@@ -194,6 +210,7 @@ export declare class AIAgent<I extends Message = any, O extends Message = any> e
|
|
|
194
210
|
static load<I extends Message = any, O extends Message = any>(options: {
|
|
195
211
|
filepath: string;
|
|
196
212
|
parsed: object;
|
|
213
|
+
options?: AgentLoadOptions;
|
|
197
214
|
}): Promise<Agent<I, O>>;
|
|
198
215
|
/**
|
|
199
216
|
* Create an AIAgent with the specified options
|
|
@@ -225,8 +242,6 @@ export declare class AIAgent<I extends Message = any, O extends Message = any> e
|
|
|
225
242
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
226
243
|
*/
|
|
227
244
|
instructions: PromptBuilder;
|
|
228
|
-
autoReorderSystemMessages?: boolean;
|
|
229
|
-
autoMergeSystemMessages?: boolean;
|
|
230
245
|
/**
|
|
231
246
|
* Pick a message from input to use as the user's message
|
|
232
247
|
*/
|
|
@@ -322,6 +337,7 @@ export declare class AIAgent<I extends Message = any, O extends Message = any> e
|
|
|
322
337
|
metadataEnd: string;
|
|
323
338
|
parse: (raw: string) => object;
|
|
324
339
|
};
|
|
340
|
+
compact?: CompactConfig;
|
|
325
341
|
get inputSchema(): ZodType<I>;
|
|
326
342
|
/**
|
|
327
343
|
* Process an input message and generate a response
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { getNestAgentSchema } from "../loader/agent-yaml.js";
|
|
2
3
|
import { camelizeSchema, getInstructionsSchema, instructionsToPromptBuilder, optionalize, } from "../loader/schema.js";
|
|
3
4
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
4
5
|
import { STRUCTURED_STREAM_INSTRUCTIONS } from "../prompt/prompts/structured-stream-instructions.js";
|
|
5
|
-
import { AgentMessageTemplate, ToolMessageTemplate } from "../prompt/template.js";
|
|
6
6
|
import * as fastq from "../utils/queue.js";
|
|
7
7
|
import { mergeAgentResponseChunk } from "../utils/stream-utils.js";
|
|
8
8
|
import { ExtractMetadataTransform } from "../utils/structured-stream-extractor.js";
|
|
@@ -88,10 +88,9 @@ export class AIAgent extends Agent {
|
|
|
88
88
|
tag = "AIAgent";
|
|
89
89
|
static schema({ filepath }) {
|
|
90
90
|
const instructionsSchema = getInstructionsSchema({ filepath });
|
|
91
|
+
const nestAgentSchema = getNestAgentSchema({ filepath });
|
|
91
92
|
return camelizeSchema(z.object({
|
|
92
93
|
instructions: optionalize(instructionsSchema),
|
|
93
|
-
autoReorderSystemMessages: optionalize(z.boolean()),
|
|
94
|
-
autoMergeSystemMessages: optionalize(z.boolean()),
|
|
95
94
|
inputKey: optionalize(z.string()),
|
|
96
95
|
outputKey: optionalize(z.string()),
|
|
97
96
|
inputFileKey: optionalize(z.string()),
|
|
@@ -101,6 +100,13 @@ export class AIAgent extends Agent {
|
|
|
101
100
|
keepTextInToolUses: optionalize(z.boolean()),
|
|
102
101
|
catchToolsError: optionalize(z.boolean()),
|
|
103
102
|
structuredStreamMode: optionalize(z.boolean()),
|
|
103
|
+
compact: camelizeSchema(optionalize(z.object({
|
|
104
|
+
mode: optionalize(z.enum(["auto", "disabled"])),
|
|
105
|
+
maxTokens: z.number().int().min(0).optional(),
|
|
106
|
+
keepRecentRatio: optionalize(z.number().min(0).max(1)),
|
|
107
|
+
async: optionalize(z.boolean()),
|
|
108
|
+
compactor: optionalize(nestAgentSchema),
|
|
109
|
+
}))),
|
|
104
110
|
}));
|
|
105
111
|
}
|
|
106
112
|
static async load(options) {
|
|
@@ -110,6 +116,12 @@ export class AIAgent extends Agent {
|
|
|
110
116
|
...options.parsed,
|
|
111
117
|
...valid,
|
|
112
118
|
instructions: valid.instructions && instructionsToPromptBuilder(valid.instructions),
|
|
119
|
+
compact: {
|
|
120
|
+
...valid.compact,
|
|
121
|
+
compactor: valid.compact?.compactor
|
|
122
|
+
? await options.options?.loadNestAgent(options.filepath, valid.compact.compactor, options.options)
|
|
123
|
+
: undefined,
|
|
124
|
+
},
|
|
113
125
|
});
|
|
114
126
|
}
|
|
115
127
|
/**
|
|
@@ -139,8 +151,6 @@ export class AIAgent extends Agent {
|
|
|
139
151
|
typeof options.instructions === "string"
|
|
140
152
|
? PromptBuilder.from(options.instructions)
|
|
141
153
|
: (options.instructions ?? new PromptBuilder());
|
|
142
|
-
this.autoReorderSystemMessages = options.autoReorderSystemMessages ?? true;
|
|
143
|
-
this.autoMergeSystemMessages = options.autoMergeSystemMessages ?? true;
|
|
144
154
|
this.inputKey = options.inputKey;
|
|
145
155
|
this.inputFileKey = options.inputFileKey;
|
|
146
156
|
this.outputKey = options.outputKey || DEFAULT_OUTPUT_KEY;
|
|
@@ -152,6 +162,7 @@ export class AIAgent extends Agent {
|
|
|
152
162
|
this.memoryAgentsAsTools = options.memoryAgentsAsTools;
|
|
153
163
|
this.memoryPromptTemplate = options.memoryPromptTemplate;
|
|
154
164
|
this.useMemoriesFromContext = options.useMemoriesFromContext;
|
|
165
|
+
this.compact = options.compact;
|
|
155
166
|
if (typeof options.catchToolsError === "boolean")
|
|
156
167
|
this.catchToolsError = options.catchToolsError;
|
|
157
168
|
this.structuredStreamMode = options.structuredStreamMode;
|
|
@@ -177,8 +188,6 @@ export class AIAgent extends Agent {
|
|
|
177
188
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
178
189
|
*/
|
|
179
190
|
instructions;
|
|
180
|
-
autoReorderSystemMessages;
|
|
181
|
-
autoMergeSystemMessages;
|
|
182
191
|
/**
|
|
183
192
|
* Pick a message from input to use as the user's message
|
|
184
193
|
*/
|
|
@@ -269,6 +278,7 @@ export class AIAgent extends Agent {
|
|
|
269
278
|
* which outputs structured data in YAML format within <metadata> tags.
|
|
270
279
|
*/
|
|
271
280
|
customStructuredStreamInstructions;
|
|
281
|
+
compact;
|
|
272
282
|
get inputSchema() {
|
|
273
283
|
let schema = super.inputSchema;
|
|
274
284
|
if (this.inputKey) {
|
|
@@ -295,7 +305,7 @@ export class AIAgent extends Agent {
|
|
|
295
305
|
const model = this.model || options.model || options.context.model;
|
|
296
306
|
if (!model)
|
|
297
307
|
throw new Error("model is required to run AIAgent");
|
|
298
|
-
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
308
|
+
const { toolAgents, session, userMessage, ...modelInput } = await this.instructions.build({
|
|
299
309
|
...options,
|
|
300
310
|
agent: this,
|
|
301
311
|
input,
|
|
@@ -304,23 +314,23 @@ export class AIAgent extends Agent {
|
|
|
304
314
|
modelInput.modelOptions = await model.getModelOptions(input, options);
|
|
305
315
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
306
316
|
if (this.toolChoice === "router") {
|
|
307
|
-
return yield* this._processRouter(input, model, modelInput, options, toolsMap);
|
|
317
|
+
return yield* this._processRouter(input, model, { messages: [...(await session.getMessages()), userMessage], ...modelInput }, options, toolsMap);
|
|
308
318
|
}
|
|
309
|
-
const toolCallMessages = [];
|
|
310
|
-
const outputKey = this.outputKey;
|
|
311
319
|
const inputMessage = this.inputKey ? input[this.inputKey] : undefined;
|
|
312
320
|
if (inputMessage) {
|
|
313
321
|
yield {
|
|
314
322
|
progress: {
|
|
315
323
|
event: "message",
|
|
316
|
-
role: "user",
|
|
317
|
-
message: [{ type: "text", content: inputMessage }],
|
|
324
|
+
message: { role: "user", content: [{ type: "text", text: inputMessage }] },
|
|
318
325
|
},
|
|
319
326
|
};
|
|
320
327
|
}
|
|
328
|
+
await session.startMessage(input, userMessage, options);
|
|
329
|
+
// const toolCallMessages: ChatModelInputMessage[] = [];
|
|
330
|
+
const outputKey = this.outputKey;
|
|
321
331
|
for (;;) {
|
|
322
332
|
const modelOutput = {};
|
|
323
|
-
let stream = await this.invokeChildAgent(model, {
|
|
333
|
+
let stream = await this.invokeChildAgent(model, { messages: await session.getMessages(), ...modelInput }, { ...options, streaming: true });
|
|
324
334
|
if (this.structuredStreamMode) {
|
|
325
335
|
const { metadataStart, metadataEnd, parse } = this.customStructuredStreamInstructions || STRUCTURED_STREAM_INSTRUCTIONS;
|
|
326
336
|
stream = stream.pipeThrough(new ExtractMetadataTransform({ start: metadataStart, end: metadataEnd, parse }));
|
|
@@ -343,15 +353,19 @@ export class AIAgent extends Agent {
|
|
|
343
353
|
}
|
|
344
354
|
}
|
|
345
355
|
const { toolCalls, json, text, thoughts, files } = modelOutput;
|
|
346
|
-
if (text) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
356
|
+
if (text || thoughts) {
|
|
357
|
+
const content = [];
|
|
358
|
+
if (thoughts) {
|
|
359
|
+
content.push({ type: "text", text: thoughts, isThinking: true });
|
|
360
|
+
}
|
|
361
|
+
if (text) {
|
|
362
|
+
content.push({ type: "text", text });
|
|
363
|
+
}
|
|
364
|
+
if (content.length) {
|
|
365
|
+
const message = { role: "agent", content };
|
|
366
|
+
yield { progress: { event: "message", message } };
|
|
367
|
+
await session.appendCurrentMessages(message);
|
|
368
|
+
}
|
|
355
369
|
}
|
|
356
370
|
if (toolCalls?.length) {
|
|
357
371
|
if (this.keepTextInToolUses !== true) {
|
|
@@ -383,25 +397,14 @@ export class AIAgent extends Agent {
|
|
|
383
397
|
queue.killAndDrain();
|
|
384
398
|
}
|
|
385
399
|
}, this.toolCallsConcurrency || 1);
|
|
400
|
+
const message = { role: "agent", toolCalls };
|
|
401
|
+
yield { progress: { event: "message", message } };
|
|
402
|
+
await session.appendCurrentMessages(message);
|
|
386
403
|
// Execute tools
|
|
387
404
|
for (const call of toolCalls) {
|
|
388
405
|
const tool = toolsMap.get(call.function.name);
|
|
389
406
|
if (!tool)
|
|
390
407
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
391
|
-
yield {
|
|
392
|
-
progress: {
|
|
393
|
-
event: "message",
|
|
394
|
-
role: "agent",
|
|
395
|
-
message: [
|
|
396
|
-
{
|
|
397
|
-
type: "tool_use",
|
|
398
|
-
name: call.function.name,
|
|
399
|
-
input: call.function.arguments,
|
|
400
|
-
toolUseId: call.id,
|
|
401
|
-
},
|
|
402
|
-
],
|
|
403
|
-
},
|
|
404
|
-
};
|
|
405
408
|
queue.push({ tool, call });
|
|
406
409
|
}
|
|
407
410
|
await queue.drained();
|
|
@@ -410,24 +413,17 @@ export class AIAgent extends Agent {
|
|
|
410
413
|
// Continue LLM function calling loop if any tools were executed
|
|
411
414
|
if (executedToolCalls.length) {
|
|
412
415
|
for (const { call, output } of executedToolCalls) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
message: [
|
|
418
|
-
{
|
|
419
|
-
type: "tool_result",
|
|
420
|
-
toolUseId: call.id,
|
|
421
|
-
content: output,
|
|
422
|
-
},
|
|
423
|
-
],
|
|
424
|
-
},
|
|
416
|
+
const message = {
|
|
417
|
+
role: "tool",
|
|
418
|
+
toolCallId: call.id,
|
|
419
|
+
content: JSON.stringify(output),
|
|
425
420
|
};
|
|
421
|
+
yield { progress: { event: "message", message: message } };
|
|
422
|
+
await session.appendCurrentMessages(message);
|
|
426
423
|
}
|
|
427
424
|
const transferOutput = executedToolCalls.find((i) => isTransferAgentOutput(i.output))?.output;
|
|
428
425
|
if (transferOutput)
|
|
429
426
|
return transferOutput;
|
|
430
|
-
toolCallMessages.push(await AgentMessageTemplate.from(undefined, executedToolCalls.map(({ call }) => call)).format(), ...(await Promise.all(executedToolCalls.map(({ call, output }) => ToolMessageTemplate.from(output, call.id).format()))));
|
|
431
427
|
continue;
|
|
432
428
|
}
|
|
433
429
|
}
|
|
@@ -444,11 +440,7 @@ export class AIAgent extends Agent {
|
|
|
444
440
|
if (!isEmpty(result)) {
|
|
445
441
|
yield { delta: { json: result } };
|
|
446
442
|
}
|
|
447
|
-
|
|
448
|
-
yield {
|
|
449
|
-
progress: { event: "message", role: "agent", message: [{ type: "text", content: text }] },
|
|
450
|
-
};
|
|
451
|
-
}
|
|
443
|
+
await session.endMessage(result, options);
|
|
452
444
|
return;
|
|
453
445
|
}
|
|
454
446
|
}
|
|
@@ -91,7 +91,6 @@ export declare const imageModelInputSchema: z.ZodObject<{
|
|
|
91
91
|
}, "strip", z.ZodTypeAny, {
|
|
92
92
|
prompt: string;
|
|
93
93
|
modelOptions?: Record<string, unknown> | undefined;
|
|
94
|
-
outputFileType?: "local" | "url" | "file" | undefined;
|
|
95
94
|
image?: ({
|
|
96
95
|
type: "url";
|
|
97
96
|
url: string;
|
|
@@ -109,10 +108,10 @@ export declare const imageModelInputSchema: z.ZodObject<{
|
|
|
109
108
|
mimeType?: string | undefined;
|
|
110
109
|
})[] | undefined;
|
|
111
110
|
n?: number | undefined;
|
|
111
|
+
outputFileType?: "local" | "file" | "url" | undefined;
|
|
112
112
|
}, {
|
|
113
113
|
prompt: string;
|
|
114
114
|
modelOptions?: Record<string, unknown> | undefined;
|
|
115
|
-
outputFileType?: "local" | "url" | "file" | undefined;
|
|
116
115
|
image?: ({
|
|
117
116
|
type: "url";
|
|
118
117
|
url: string;
|
|
@@ -130,6 +129,7 @@ export declare const imageModelInputSchema: z.ZodObject<{
|
|
|
130
129
|
mimeType?: string | undefined;
|
|
131
130
|
})[] | undefined;
|
|
132
131
|
n?: number | undefined;
|
|
132
|
+
outputFileType?: "local" | "file" | "url" | undefined;
|
|
133
133
|
}>;
|
|
134
134
|
export interface ImageModelOutput extends Message {
|
|
135
135
|
images: FileUnionContent[];
|
|
@@ -232,6 +232,7 @@ export declare const imageModelOutputSchema: z.ZodObject<{
|
|
|
232
232
|
filename?: string | undefined;
|
|
233
233
|
mimeType?: string | undefined;
|
|
234
234
|
})[];
|
|
235
|
+
model?: string | undefined;
|
|
235
236
|
usage?: {
|
|
236
237
|
inputTokens: number;
|
|
237
238
|
outputTokens: number;
|
|
@@ -240,7 +241,6 @@ export declare const imageModelOutputSchema: z.ZodObject<{
|
|
|
240
241
|
cacheReadInputTokens?: number | undefined;
|
|
241
242
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
242
243
|
} | undefined;
|
|
243
|
-
model?: string | undefined;
|
|
244
244
|
}, {
|
|
245
245
|
images: ({
|
|
246
246
|
type: "url";
|
|
@@ -258,6 +258,7 @@ export declare const imageModelOutputSchema: z.ZodObject<{
|
|
|
258
258
|
filename?: string | undefined;
|
|
259
259
|
mimeType?: string | undefined;
|
|
260
260
|
})[];
|
|
261
|
+
model?: string | undefined;
|
|
261
262
|
usage?: {
|
|
262
263
|
inputTokens: number;
|
|
263
264
|
outputTokens: number;
|
|
@@ -266,5 +267,4 @@ export declare const imageModelOutputSchema: z.ZodObject<{
|
|
|
266
267
|
cacheReadInputTokens?: number | undefined;
|
|
267
268
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
268
269
|
} | undefined;
|
|
269
|
-
model?: string | undefined;
|
|
270
270
|
}>;
|
|
@@ -64,12 +64,12 @@ export declare class MCPAgent extends Agent {
|
|
|
64
64
|
args: ZodType<string[] | undefined, z.ZodTypeDef, string[] | undefined>;
|
|
65
65
|
}, "strip", z.ZodTypeAny, {
|
|
66
66
|
url?: string | undefined;
|
|
67
|
-
args?: string[] | undefined;
|
|
68
67
|
command?: string | undefined;
|
|
68
|
+
args?: string[] | undefined;
|
|
69
69
|
}, {
|
|
70
70
|
url?: string | undefined;
|
|
71
|
-
args?: string[] | undefined;
|
|
72
71
|
command?: string | undefined;
|
|
72
|
+
args?: string[] | undefined;
|
|
73
73
|
}>;
|
|
74
74
|
static load<I extends Message = any, O extends Message = any>(options: {
|
|
75
75
|
filepath: string;
|
|
@@ -94,7 +94,6 @@ export declare const videoModelInputSchema: z.ZodObject<{
|
|
|
94
94
|
prompt: string;
|
|
95
95
|
model?: string | undefined;
|
|
96
96
|
modelOptions?: Record<string, unknown> | undefined;
|
|
97
|
-
outputFileType?: "local" | "url" | "file" | undefined;
|
|
98
97
|
image?: {
|
|
99
98
|
type: "url";
|
|
100
99
|
url: string;
|
|
@@ -111,13 +110,13 @@ export declare const videoModelInputSchema: z.ZodObject<{
|
|
|
111
110
|
filename?: string | undefined;
|
|
112
111
|
mimeType?: string | undefined;
|
|
113
112
|
} | undefined;
|
|
113
|
+
outputFileType?: "local" | "file" | "url" | undefined;
|
|
114
114
|
size?: string | undefined;
|
|
115
115
|
seconds?: string | undefined;
|
|
116
116
|
}, {
|
|
117
117
|
prompt: string;
|
|
118
118
|
model?: string | undefined;
|
|
119
119
|
modelOptions?: Record<string, unknown> | undefined;
|
|
120
|
-
outputFileType?: "local" | "url" | "file" | undefined;
|
|
121
120
|
image?: {
|
|
122
121
|
type: "url";
|
|
123
122
|
url: string;
|
|
@@ -134,6 +133,7 @@ export declare const videoModelInputSchema: z.ZodObject<{
|
|
|
134
133
|
filename?: string | undefined;
|
|
135
134
|
mimeType?: string | undefined;
|
|
136
135
|
} | undefined;
|
|
136
|
+
outputFileType?: "local" | "file" | "url" | undefined;
|
|
137
137
|
size?: string | undefined;
|
|
138
138
|
seconds?: string | undefined;
|
|
139
139
|
}>;
|
|
@@ -240,6 +240,7 @@ export declare const videoModelOutputSchema: z.ZodObject<{
|
|
|
240
240
|
filename?: string | undefined;
|
|
241
241
|
mimeType?: string | undefined;
|
|
242
242
|
})[];
|
|
243
|
+
model?: string | undefined;
|
|
243
244
|
usage?: {
|
|
244
245
|
inputTokens: number;
|
|
245
246
|
outputTokens: number;
|
|
@@ -248,7 +249,6 @@ export declare const videoModelOutputSchema: z.ZodObject<{
|
|
|
248
249
|
cacheReadInputTokens?: number | undefined;
|
|
249
250
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
250
251
|
} | undefined;
|
|
251
|
-
model?: string | undefined;
|
|
252
252
|
seconds?: number | undefined;
|
|
253
253
|
}, {
|
|
254
254
|
videos: ({
|
|
@@ -267,6 +267,7 @@ export declare const videoModelOutputSchema: z.ZodObject<{
|
|
|
267
267
|
filename?: string | undefined;
|
|
268
268
|
mimeType?: string | undefined;
|
|
269
269
|
})[];
|
|
270
|
+
model?: string | undefined;
|
|
270
271
|
usage?: {
|
|
271
272
|
inputTokens: number;
|
|
272
273
|
outputTokens: number;
|
|
@@ -275,6 +276,5 @@ export declare const videoModelOutputSchema: z.ZodObject<{
|
|
|
275
276
|
cacheReadInputTokens?: number | undefined;
|
|
276
277
|
creditPrefix?: "$" | "€" | "¥" | undefined;
|
|
277
278
|
} | undefined;
|
|
278
|
-
model?: string | undefined;
|
|
279
279
|
seconds?: number | undefined;
|
|
280
280
|
}>;
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./agents/user-agent.js";
|
|
|
13
13
|
export * from "./agents/video-model.js";
|
|
14
14
|
export * from "./aigne/index.js";
|
|
15
15
|
export * from "./memory/index.js";
|
|
16
|
+
export * from "./prompt/agent-session.js";
|
|
16
17
|
export * from "./prompt/prompt-builder.js";
|
|
17
18
|
export * from "./prompt/template.js";
|
|
18
19
|
export * from "./utils/json-utils.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./agents/user-agent.js";
|
|
|
13
13
|
export * from "./agents/video-model.js";
|
|
14
14
|
export * from "./aigne/index.js";
|
|
15
15
|
export * from "./memory/index.js";
|
|
16
|
+
export * from "./prompt/agent-session.js";
|
|
16
17
|
export * from "./prompt/prompt-builder.js";
|
|
17
18
|
export * from "./prompt/template.js";
|
|
18
19
|
export * from "./utils/json-utils.js";
|
|
@@ -137,7 +137,6 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
137
137
|
chat?: string | undefined;
|
|
138
138
|
} | undefined>;
|
|
139
139
|
}, "strip", z.ZodTypeAny, {
|
|
140
|
-
name?: string | undefined;
|
|
141
140
|
model?: z.objectInputType<{
|
|
142
141
|
model: ZodType<string | {
|
|
143
142
|
$get: string;
|
|
@@ -170,6 +169,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
170
169
|
$get: string;
|
|
171
170
|
} | undefined>;
|
|
172
171
|
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
172
|
+
name?: string | undefined;
|
|
173
173
|
description?: string | undefined;
|
|
174
174
|
imageModel?: z.objectInputType<{
|
|
175
175
|
model: ZodType<string | {
|
|
@@ -188,7 +188,6 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
188
188
|
chat?: string | undefined;
|
|
189
189
|
} | undefined;
|
|
190
190
|
}, {
|
|
191
|
-
name?: string | undefined;
|
|
192
191
|
model?: z.objectInputType<{
|
|
193
192
|
model: ZodType<string | {
|
|
194
193
|
$get: string;
|
|
@@ -221,6 +220,7 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
221
220
|
$get: string;
|
|
222
221
|
} | undefined>;
|
|
223
222
|
}, z.ZodTypeAny, "passthrough"> | undefined;
|
|
223
|
+
name?: string | undefined;
|
|
224
224
|
description?: string | undefined;
|
|
225
225
|
imageModel?: z.objectInputType<{
|
|
226
226
|
model: ZodType<string | {
|
|
@@ -42,24 +42,24 @@ export declare const memoryRecorderOutputSchema: z.ZodObject<{
|
|
|
42
42
|
content: ZodType<{}, z.ZodTypeDef, {}>;
|
|
43
43
|
createdAt: z.ZodString;
|
|
44
44
|
}, "strip", z.ZodTypeAny, {
|
|
45
|
-
content: {};
|
|
46
45
|
id: string;
|
|
46
|
+
content: {};
|
|
47
47
|
createdAt: string;
|
|
48
48
|
}, {
|
|
49
|
-
content: {};
|
|
50
49
|
id: string;
|
|
50
|
+
content: {};
|
|
51
51
|
createdAt: string;
|
|
52
52
|
}>, "many">;
|
|
53
53
|
}, "strip", z.ZodTypeAny, {
|
|
54
54
|
memories: {
|
|
55
|
-
content: {};
|
|
56
55
|
id: string;
|
|
56
|
+
content: {};
|
|
57
57
|
createdAt: string;
|
|
58
58
|
}[];
|
|
59
59
|
}, {
|
|
60
60
|
memories: {
|
|
61
|
-
content: {};
|
|
62
61
|
id: string;
|
|
62
|
+
content: {};
|
|
63
63
|
createdAt: string;
|
|
64
64
|
}[];
|
|
65
65
|
}>;
|
|
@@ -55,24 +55,24 @@ export declare const memoryRetrieverOutputSchema: z.ZodObject<{
|
|
|
55
55
|
content: z.ZodType<{}, z.ZodTypeDef, {}>;
|
|
56
56
|
createdAt: z.ZodString;
|
|
57
57
|
}, "strip", z.ZodTypeAny, {
|
|
58
|
-
content: {};
|
|
59
58
|
id: string;
|
|
59
|
+
content: {};
|
|
60
60
|
createdAt: string;
|
|
61
61
|
}, {
|
|
62
|
-
content: {};
|
|
63
62
|
id: string;
|
|
63
|
+
content: {};
|
|
64
64
|
createdAt: string;
|
|
65
65
|
}>, "many">;
|
|
66
66
|
}, "strip", z.ZodTypeAny, {
|
|
67
67
|
memories: {
|
|
68
|
-
content: {};
|
|
69
68
|
id: string;
|
|
69
|
+
content: {};
|
|
70
70
|
createdAt: string;
|
|
71
71
|
}[];
|
|
72
72
|
}, {
|
|
73
73
|
memories: {
|
|
74
|
-
content: {};
|
|
75
74
|
id: string;
|
|
75
|
+
content: {};
|
|
76
76
|
createdAt: string;
|
|
77
77
|
}[];
|
|
78
78
|
}>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { AFS } from "@aigne/afs";
|
|
2
|
+
import type { AgentInvokeOptions } from "../agents/agent.js";
|
|
3
|
+
import type { ChatModelInputMessage } from "../agents/chat-model.js";
|
|
4
|
+
import { type CompactConfig, type CompactContent, type EntryContent } from "./compact/types.js";
|
|
5
|
+
export type { CompactConfig, CompactContent, EntryContent };
|
|
6
|
+
export interface AgentSessionOptions {
|
|
7
|
+
sessionId: string;
|
|
8
|
+
userId?: string;
|
|
9
|
+
agentId?: string;
|
|
10
|
+
afs?: AFS;
|
|
11
|
+
/**
|
|
12
|
+
* Compaction configuration
|
|
13
|
+
*/
|
|
14
|
+
compact?: CompactConfig;
|
|
15
|
+
}
|
|
16
|
+
export declare class AgentSession {
|
|
17
|
+
readonly sessionId: string;
|
|
18
|
+
readonly userId?: string;
|
|
19
|
+
readonly agentId?: string;
|
|
20
|
+
private afs?;
|
|
21
|
+
private historyModulePath?;
|
|
22
|
+
private compactConfig;
|
|
23
|
+
private runtimeState;
|
|
24
|
+
private initialized?;
|
|
25
|
+
private compactionPromise?;
|
|
26
|
+
constructor(options: AgentSessionOptions);
|
|
27
|
+
setSystemMessages(...messages: ChatModelInputMessage[]): Promise<void>;
|
|
28
|
+
getMessages(): Promise<ChatModelInputMessage[]>;
|
|
29
|
+
startMessage(input: unknown, message: ChatModelInputMessage, options: AgentInvokeOptions): Promise<void>;
|
|
30
|
+
endMessage(output: unknown, options: AgentInvokeOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Manually trigger compaction
|
|
33
|
+
*/
|
|
34
|
+
compact(options: AgentInvokeOptions): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Internal method that performs the actual compaction
|
|
37
|
+
*/
|
|
38
|
+
private doCompact;
|
|
39
|
+
private maybeAutoCompact;
|
|
40
|
+
/**
|
|
41
|
+
* Estimate token count for an array of messages
|
|
42
|
+
*/
|
|
43
|
+
private estimateMessagesTokens;
|
|
44
|
+
/**
|
|
45
|
+
* Split entries into batches based on token limit
|
|
46
|
+
* Each batch will not exceed the specified maxTokens
|
|
47
|
+
*/
|
|
48
|
+
private splitIntoBatches;
|
|
49
|
+
appendCurrentMessages(...messages: ChatModelInputMessage[]): Promise<void>;
|
|
50
|
+
private ensureInitialized;
|
|
51
|
+
private initialize;
|
|
52
|
+
private initializeDefaultCompactor;
|
|
53
|
+
}
|