@aigne/core 1.72.0-beta.7 → 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 +20 -0
- package/lib/cjs/agents/agent.d.ts +2 -21
- package/lib/cjs/agents/agent.js +10 -15
- package/lib/cjs/agents/ai-agent.d.ts +20 -4
- package/lib/cjs/agents/ai-agent.js +37 -35
- 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/agent-yaml.d.ts +1 -2
- package/lib/cjs/loader/agent-yaml.js +0 -8
- 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.js +1 -1
- package/lib/cjs/prompt/prompt-builder.d.ts +6 -8
- package/lib/cjs/prompt/prompt-builder.js +67 -123
- package/lib/cjs/prompt/template.d.ts +16 -16
- package/lib/dts/agents/agent.d.ts +2 -21
- package/lib/dts/agents/ai-agent.d.ts +20 -4
- 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/agent-yaml.d.ts +1 -2
- 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/prompt-builder.d.ts +6 -8
- package/lib/dts/prompt/template.d.ts +16 -16
- package/lib/esm/agents/agent.d.ts +2 -21
- package/lib/esm/agents/agent.js +10 -15
- package/lib/esm/agents/ai-agent.d.ts +20 -4
- package/lib/esm/agents/ai-agent.js +37 -35
- 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/agent-yaml.d.ts +1 -2
- package/lib/esm/loader/agent-yaml.js +0 -8
- 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.js +1 -1
- package/lib/esm/prompt/prompt-builder.d.ts +6 -8
- package/lib/esm/prompt/prompt-builder.js +68 -124
- package/lib/esm/prompt/template.d.ts +16 -16
- package/package.json +4 -4
|
@@ -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,25 +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
|
-
message: {
|
|
317
|
-
role: "user",
|
|
318
|
-
content: [{ type: "text", text: inputMessage }],
|
|
319
|
-
},
|
|
324
|
+
message: { role: "user", content: [{ type: "text", text: inputMessage }] },
|
|
320
325
|
},
|
|
321
326
|
};
|
|
322
327
|
}
|
|
328
|
+
await session.startMessage(input, userMessage, options);
|
|
329
|
+
// const toolCallMessages: ChatModelInputMessage[] = [];
|
|
330
|
+
const outputKey = this.outputKey;
|
|
323
331
|
for (;;) {
|
|
324
332
|
const modelOutput = {};
|
|
325
|
-
let stream = await this.invokeChildAgent(model, {
|
|
333
|
+
let stream = await this.invokeChildAgent(model, { messages: await session.getMessages(), ...modelInput }, { ...options, streaming: true });
|
|
326
334
|
if (this.structuredStreamMode) {
|
|
327
335
|
const { metadataStart, metadataEnd, parse } = this.customStructuredStreamInstructions || STRUCTURED_STREAM_INSTRUCTIONS;
|
|
328
336
|
stream = stream.pipeThrough(new ExtractMetadataTransform({ start: metadataStart, end: metadataEnd, parse }));
|
|
@@ -354,12 +362,9 @@ export class AIAgent extends Agent {
|
|
|
354
362
|
content.push({ type: "text", text });
|
|
355
363
|
}
|
|
356
364
|
if (content.length) {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
message: { role: "agent", content },
|
|
361
|
-
},
|
|
362
|
-
};
|
|
365
|
+
const message = { role: "agent", content };
|
|
366
|
+
yield { progress: { event: "message", message } };
|
|
367
|
+
await session.appendCurrentMessages(message);
|
|
363
368
|
}
|
|
364
369
|
}
|
|
365
370
|
if (toolCalls?.length) {
|
|
@@ -392,9 +397,9 @@ export class AIAgent extends Agent {
|
|
|
392
397
|
queue.killAndDrain();
|
|
393
398
|
}
|
|
394
399
|
}, this.toolCallsConcurrency || 1);
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
400
|
+
const message = { role: "agent", toolCalls };
|
|
401
|
+
yield { progress: { event: "message", message } };
|
|
402
|
+
await session.appendCurrentMessages(message);
|
|
398
403
|
// Execute tools
|
|
399
404
|
for (const call of toolCalls) {
|
|
400
405
|
const tool = toolsMap.get(call.function.name);
|
|
@@ -408,21 +413,17 @@ export class AIAgent extends Agent {
|
|
|
408
413
|
// Continue LLM function calling loop if any tools were executed
|
|
409
414
|
if (executedToolCalls.length) {
|
|
410
415
|
for (const { call, output } of executedToolCalls) {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
role: "tool",
|
|
416
|
-
toolCallId: call.id,
|
|
417
|
-
content: JSON.stringify(output),
|
|
418
|
-
},
|
|
419
|
-
},
|
|
416
|
+
const message = {
|
|
417
|
+
role: "tool",
|
|
418
|
+
toolCallId: call.id,
|
|
419
|
+
content: JSON.stringify(output),
|
|
420
420
|
};
|
|
421
|
+
yield { progress: { event: "message", message: message } };
|
|
422
|
+
await session.appendCurrentMessages(message);
|
|
421
423
|
}
|
|
422
424
|
const transferOutput = executedToolCalls.find((i) => isTransferAgentOutput(i.output))?.output;
|
|
423
425
|
if (transferOutput)
|
|
424
426
|
return transferOutput;
|
|
425
|
-
toolCallMessages.push(await AgentMessageTemplate.from(undefined, executedToolCalls.map(({ call }) => call)).format(), ...(await Promise.all(executedToolCalls.map(({ call, output }) => ToolMessageTemplate.from(output, call.id).format()))));
|
|
426
427
|
continue;
|
|
427
428
|
}
|
|
428
429
|
}
|
|
@@ -439,6 +440,7 @@ export class AIAgent extends Agent {
|
|
|
439
440
|
if (!isEmpty(result)) {
|
|
440
441
|
yield { delta: { json: result } };
|
|
441
442
|
}
|
|
443
|
+
await session.endMessage(result, options);
|
|
442
444
|
return;
|
|
443
445
|
}
|
|
444
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";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AFSOptions } from "@aigne/afs";
|
|
2
2
|
import { type ZodType, z } from "zod";
|
|
3
|
-
import type {
|
|
3
|
+
import type { AgentHooks, TaskRenderMode } from "../agents/agent.js";
|
|
4
4
|
import type { LoadOptions } from "./index.js";
|
|
5
5
|
import { chatModelSchema, imageModelSchema } from "./schema.js";
|
|
6
6
|
export interface HooksSchema {
|
|
@@ -65,7 +65,6 @@ export interface AgentSchema {
|
|
|
65
65
|
context?: AFSContextSchema;
|
|
66
66
|
});
|
|
67
67
|
shareAFS?: boolean;
|
|
68
|
-
historyConfig?: Agent["historyConfig"];
|
|
69
68
|
[key: string]: unknown;
|
|
70
69
|
}
|
|
71
70
|
export declare function parseAgentFile(path: string, data: any, options: LoadOptions): Promise<AgentSchema>;
|
|
@@ -95,14 +95,6 @@ export const getAgentSchema = ({ filepath }) => {
|
|
|
95
95
|
})),
|
|
96
96
|
])),
|
|
97
97
|
shareAFS: optionalize(z.boolean()),
|
|
98
|
-
historyConfig: camelizeSchema(optionalize(z.object({
|
|
99
|
-
enabled: optionalize(z.boolean()),
|
|
100
|
-
record: optionalize(z.boolean()),
|
|
101
|
-
inject: optionalize(z.boolean()),
|
|
102
|
-
use_old_memory: optionalize(z.boolean()),
|
|
103
|
-
maxTokens: optionalize(z.number().int().positive()),
|
|
104
|
-
maxItems: optionalize(z.number().int().positive()),
|
|
105
|
-
}))),
|
|
106
98
|
});
|
|
107
99
|
return camelizeSchema(baseAgentSchema.passthrough());
|
|
108
100
|
});
|
|
@@ -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
|
+
}
|