@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
|
@@ -20,7 +20,14 @@ function createAFSContext(agent, context) {
|
|
|
20
20
|
get histories() {
|
|
21
21
|
if (!agent)
|
|
22
22
|
return Promise.resolve([]);
|
|
23
|
-
return (0, history_js_1.getHistories)(
|
|
23
|
+
return (0, history_js_1.getHistories)({
|
|
24
|
+
agent,
|
|
25
|
+
filter: {
|
|
26
|
+
agentId: agent.name,
|
|
27
|
+
userId: context?.userContext.userId,
|
|
28
|
+
sessionId: context?.userContext.sessionId,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
24
31
|
},
|
|
25
32
|
get skills() {
|
|
26
33
|
const afs = agent?.afs;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import type { GetPromptResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
-
import { Agent, type
|
|
2
|
+
import { Agent, type Message } from "../agents/agent.js";
|
|
3
3
|
import { type AIAgent } from "../agents/ai-agent.js";
|
|
4
|
-
import type { ChatModel, ChatModelInput } from "../agents/chat-model.js";
|
|
4
|
+
import type { ChatModel, ChatModelInput, ChatModelInputMessage } from "../agents/chat-model.js";
|
|
5
5
|
import { type FileUnionContent } from "../agents/model.js";
|
|
6
|
+
import type { Context } from "../aigne/context.js";
|
|
7
|
+
import { AgentSession } from "./agent-session.js";
|
|
6
8
|
import { ChatMessagesTemplate } from "./template.js";
|
|
7
9
|
export interface PromptBuilderOptions {
|
|
8
10
|
instructions?: string | ChatMessagesTemplate;
|
|
9
11
|
workingDir?: string;
|
|
10
12
|
}
|
|
11
|
-
export interface PromptBuildOptions
|
|
13
|
+
export interface PromptBuildOptions {
|
|
14
|
+
context?: Context;
|
|
12
15
|
agent?: AIAgent;
|
|
13
16
|
input?: Message;
|
|
14
17
|
model?: ChatModel;
|
|
@@ -26,7 +29,9 @@ export declare class PromptBuilder {
|
|
|
26
29
|
instructions?: string | ChatMessagesTemplate;
|
|
27
30
|
workingDir?: string;
|
|
28
31
|
copy(): PromptBuilder;
|
|
29
|
-
build(options: PromptBuildOptions): Promise<ChatModelInput & {
|
|
32
|
+
build(options: PromptBuildOptions): Promise<Omit<ChatModelInput, "messages"> & {
|
|
33
|
+
session: AgentSession;
|
|
34
|
+
userMessage: ChatModelInputMessage;
|
|
30
35
|
toolAgents?: Agent[];
|
|
31
36
|
}>;
|
|
32
37
|
buildPrompt(options: Pick<PromptBuildOptions, "input" | "context"> & {
|
|
@@ -37,11 +42,8 @@ export declare class PromptBuilder {
|
|
|
37
42
|
}>;
|
|
38
43
|
private getTemplateVariables;
|
|
39
44
|
private buildMessages;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
content: unknown;
|
|
43
|
-
}[]>;
|
|
44
|
-
private refineMessages;
|
|
45
|
+
private mergeMessages;
|
|
46
|
+
protected deprecatedMemories(message: string | undefined, options: PromptBuildOptions): Promise<ChatModelInputMessage[]>;
|
|
45
47
|
private convertMemoriesToMessages;
|
|
46
48
|
private buildResponseFormat;
|
|
47
49
|
private buildTools;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PromptBuilder = void 0;
|
|
4
|
-
const afs_history_1 = require("@aigne/afs-history");
|
|
5
4
|
const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
|
|
5
|
+
const uuid_1 = require("@aigne/uuid");
|
|
6
6
|
const yaml_1 = require("yaml");
|
|
7
7
|
const zod_1 = require("zod");
|
|
8
8
|
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
@@ -12,8 +12,8 @@ const model_js_1 = require("../agents/model.js");
|
|
|
12
12
|
const schema_js_1 = require("../loader/schema.js");
|
|
13
13
|
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
14
14
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
15
|
+
const agent_session_js_1 = require("./agent-session.js");
|
|
15
16
|
const index_js_2 = require("./context/index.js");
|
|
16
|
-
const afs_builtin_prompt_js_1 = require("./prompts/afs-builtin-prompt.js");
|
|
17
17
|
const memory_message_template_js_1 = require("./prompts/memory-message-template.js");
|
|
18
18
|
const structured_stream_instructions_js_1 = require("./prompts/structured-stream-instructions.js");
|
|
19
19
|
const index_js_3 = require("./skills/afs/index.js");
|
|
@@ -73,13 +73,28 @@ class PromptBuilder {
|
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
75
|
async build(options) {
|
|
76
|
+
let { userId, sessionId } = options.context?.userContext || {};
|
|
77
|
+
const agentId = options.agent?.name;
|
|
78
|
+
const afs = options.agent?.afs;
|
|
79
|
+
sessionId ||= (0, uuid_1.v7)();
|
|
80
|
+
const session = new agent_session_js_1.AgentSession({
|
|
81
|
+
agentId,
|
|
82
|
+
userId,
|
|
83
|
+
sessionId,
|
|
84
|
+
afs,
|
|
85
|
+
compact: options.agent?.compact,
|
|
86
|
+
});
|
|
87
|
+
const { systemMessage, userMessage } = await this.buildMessages(options);
|
|
88
|
+
if (systemMessage)
|
|
89
|
+
await session.setSystemMessages(systemMessage);
|
|
76
90
|
return {
|
|
77
|
-
|
|
91
|
+
userMessage,
|
|
78
92
|
responseFormat: options.agent?.structuredStreamMode
|
|
79
93
|
? undefined
|
|
80
94
|
: this.buildResponseFormat(options),
|
|
81
95
|
outputFileType: options.agent?.outputFileType,
|
|
82
96
|
...(await this.buildTools(options)),
|
|
97
|
+
session,
|
|
83
98
|
};
|
|
84
99
|
}
|
|
85
100
|
async buildPrompt(options) {
|
|
@@ -102,84 +117,41 @@ class PromptBuilder {
|
|
|
102
117
|
const { input } = options;
|
|
103
118
|
const inputKey = options.agent?.inputKey;
|
|
104
119
|
const message = inputKey && typeof input?.[inputKey] === "string" ? input[inputKey] : undefined;
|
|
105
|
-
const
|
|
120
|
+
const template = typeof this.instructions === "string"
|
|
106
121
|
? template_js_1.ChatMessagesTemplate.from([template_js_1.SystemMessageTemplate.from(this.instructions)])
|
|
107
|
-
: this.instructions
|
|
122
|
+
: this.instructions;
|
|
123
|
+
const [systemMessages, userMessages] = (0, type_utils_js_1.partition)((await template?.format(this.getTemplateVariables(options), {
|
|
124
|
+
workingDir: this.workingDir,
|
|
125
|
+
})) ?? [], (i) => i.role === "system");
|
|
108
126
|
const inputFileKey = options.agent?.inputFileKey;
|
|
109
127
|
const files = (0, type_utils_js_1.flat)(inputFileKey
|
|
110
128
|
? (0, type_utils_js_1.checkArguments)("Check input files", (0, schema_js_1.optionalize)(model_js_1.fileUnionContentsSchema), input?.[inputFileKey])
|
|
111
129
|
: null);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (options.agent?.useMemoriesFromContext && options.context?.memories?.length) {
|
|
117
|
-
memories.push(...options.context.memories);
|
|
118
|
-
}
|
|
119
|
-
const afs = options.agent?.afs;
|
|
120
|
-
if (afs && options.agent?.historyConfig?.disabled !== true) {
|
|
121
|
-
const historyModule = (await afs.listModules()).find((m) => m.module instanceof afs_history_1.AFSHistory);
|
|
122
|
-
if (historyModule) {
|
|
123
|
-
const history = await afs.list(historyModule.path, {
|
|
124
|
-
limit: options.agent?.maxRetrieveMemoryCount || 10,
|
|
125
|
-
orderBy: [["createdAt", "desc"]],
|
|
126
|
-
});
|
|
127
|
-
memories.push(...history.data
|
|
128
|
-
.reverse()
|
|
129
|
-
.filter((i) => (0, type_utils_js_1.isNonNullable)(i.content)));
|
|
130
|
-
if (message) {
|
|
131
|
-
const result = (await afs.search("/", message)).data;
|
|
132
|
-
const ms = result
|
|
133
|
-
.map((entry) => {
|
|
134
|
-
if (entry.metadata?.execute)
|
|
135
|
-
return null;
|
|
136
|
-
const content = entry.content || entry.summary;
|
|
137
|
-
if (!content)
|
|
138
|
-
return null;
|
|
139
|
-
return {
|
|
140
|
-
content,
|
|
141
|
-
description: entry.description,
|
|
142
|
-
};
|
|
143
|
-
})
|
|
144
|
-
.filter(type_utils_js_1.isNonNullable);
|
|
145
|
-
memories.push(...ms);
|
|
146
|
-
const executable = result.filter((i) => !!i.metadata?.execute);
|
|
147
|
-
if (executable.length) {
|
|
148
|
-
messages.push({
|
|
149
|
-
role: "system",
|
|
150
|
-
content: await template_js_1.PromptTemplate.from(afs_builtin_prompt_js_1.AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE).format({
|
|
151
|
-
tools: executable.map((entry) => ({
|
|
152
|
-
path: entry.path,
|
|
153
|
-
name: entry.metadata.execute.name,
|
|
154
|
-
description: entry.metadata.execute.description,
|
|
155
|
-
inputSchema: entry.metadata.execute.inputSchema,
|
|
156
|
-
outputSchema: entry.metadata.execute.outputSchema,
|
|
157
|
-
})),
|
|
158
|
-
}),
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
130
|
+
if (options.agent?.memories.length || options.context?.memories.length) {
|
|
131
|
+
const deprecatedMemories = await this.deprecatedMemories(message, options);
|
|
132
|
+
if (deprecatedMemories.length)
|
|
133
|
+
systemMessages.push(...deprecatedMemories);
|
|
163
134
|
}
|
|
164
|
-
if (memories.length)
|
|
165
|
-
messages.push(...(await this.convertMemoriesToMessages(memories, options)));
|
|
166
135
|
// if the agent is using structured stream mode, add the instructions
|
|
167
136
|
const { structuredStreamMode, outputSchema } = options.agent || {};
|
|
168
137
|
if (structuredStreamMode && outputSchema) {
|
|
169
138
|
const instructions = options.agent?.customStructuredStreamInstructions?.instructions ||
|
|
170
139
|
PromptBuilder.from(structured_stream_instructions_js_1.STRUCTURED_STREAM_INSTRUCTIONS.instructions);
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
140
|
+
systemMessages.push({
|
|
141
|
+
role: "system",
|
|
142
|
+
content: (await instructions.buildPrompt({
|
|
143
|
+
input: {
|
|
144
|
+
...input,
|
|
145
|
+
outputJsonSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(outputSchema),
|
|
146
|
+
},
|
|
147
|
+
})).prompt,
|
|
148
|
+
});
|
|
177
149
|
}
|
|
178
150
|
if (message || files.length) {
|
|
179
151
|
const content = [];
|
|
180
152
|
if (message &&
|
|
181
|
-
// avoid duplicate user messages: developer may have already included the message in the
|
|
182
|
-
!
|
|
153
|
+
// avoid duplicate user messages: developer may have already included the message in the messages
|
|
154
|
+
!userMessages.some((i) => i.role === "user" &&
|
|
183
155
|
(typeof i.content === "string"
|
|
184
156
|
? i.content.includes(message)
|
|
185
157
|
: i.content?.some((c) => c.type === "text" && c.text.includes(message))))) {
|
|
@@ -188,63 +160,51 @@ class PromptBuilder {
|
|
|
188
160
|
if (files.length)
|
|
189
161
|
content.push(...files);
|
|
190
162
|
if (content.length) {
|
|
191
|
-
|
|
163
|
+
userMessages.push({ role: "user", content });
|
|
192
164
|
}
|
|
193
165
|
}
|
|
194
|
-
|
|
195
|
-
|
|
166
|
+
let systemMessage = this.mergeMessages(systemMessages, "system");
|
|
167
|
+
if (!systemMessage.content?.length)
|
|
168
|
+
systemMessage = undefined;
|
|
169
|
+
let userMessage = this.mergeMessages(userMessages, "user");
|
|
170
|
+
if (!userMessage.content?.length) {
|
|
171
|
+
userMessage = { role: "user", content: systemMessage?.content };
|
|
172
|
+
systemMessage = undefined;
|
|
173
|
+
}
|
|
174
|
+
if (!userMessage.content?.length)
|
|
175
|
+
throw new Error("User message cannot be empty.");
|
|
176
|
+
return {
|
|
177
|
+
systemMessage,
|
|
178
|
+
userMessage,
|
|
179
|
+
};
|
|
196
180
|
}
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
.map((i) => {
|
|
212
|
-
if (!i.content)
|
|
213
|
-
return;
|
|
214
|
-
const { input, output } = i.content;
|
|
215
|
-
if (!input || !output)
|
|
216
|
-
return;
|
|
217
|
-
return [
|
|
218
|
-
{ role: "user", content: input },
|
|
219
|
-
{ role: "agent", content: output },
|
|
220
|
-
];
|
|
221
|
-
})
|
|
222
|
-
.filter(type_utils_js_1.isNonNullable)
|
|
223
|
-
.flat();
|
|
181
|
+
mergeMessages(messages, role) {
|
|
182
|
+
const content = [];
|
|
183
|
+
for (const message of messages) {
|
|
184
|
+
if (typeof message.content === "string") {
|
|
185
|
+
content.push({ type: "text", text: message.content });
|
|
186
|
+
}
|
|
187
|
+
else if (Array.isArray(message.content)) {
|
|
188
|
+
content.push(...message.content);
|
|
189
|
+
}
|
|
190
|
+
else if (message.content) {
|
|
191
|
+
throw new Error(`Unsupported message content type: ${typeof message.content}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return { role, content };
|
|
224
195
|
}
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (!autoMergeSystemMessages) {
|
|
231
|
-
return systemMessages.concat(otherMessages);
|
|
196
|
+
async deprecatedMemories(message, options) {
|
|
197
|
+
const messages = [];
|
|
198
|
+
const memories = [];
|
|
199
|
+
if (options.agent && options.context) {
|
|
200
|
+
memories.push(...(await options.agent.retrieveMemories({ search: message }, { context: options.context })));
|
|
232
201
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
result.push({
|
|
236
|
-
role: "system",
|
|
237
|
-
content: systemMessages
|
|
238
|
-
.map((i) => typeof i.content === "string"
|
|
239
|
-
? i.content
|
|
240
|
-
: i.content
|
|
241
|
-
?.map((c) => (c.type === "text" ? c.text : null))
|
|
242
|
-
.filter(type_utils_js_1.isNonNullable)
|
|
243
|
-
.join("\n"))
|
|
244
|
-
.join("\n"),
|
|
245
|
-
});
|
|
202
|
+
if (options.agent?.useMemoriesFromContext && options.context?.memories?.length) {
|
|
203
|
+
memories.push(...options.context.memories);
|
|
246
204
|
}
|
|
247
|
-
|
|
205
|
+
if (memories.length)
|
|
206
|
+
messages.push(...(await this.convertMemoriesToMessages(memories, options)));
|
|
207
|
+
return messages;
|
|
248
208
|
}
|
|
249
209
|
async convertMemoriesToMessages(memories, options) {
|
|
250
210
|
const messages = [];
|
|
@@ -42,7 +42,7 @@ export declare class AgentMessageTemplate extends ChatMessageTemplate {
|
|
|
42
42
|
static from(template?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[], name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): AgentMessageTemplate;
|
|
43
43
|
constructor(content?: ChatModelInputMessage["content"], toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
|
|
44
44
|
format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
|
|
45
|
-
role: "
|
|
45
|
+
role: "agent" | "user" | "system" | "tool";
|
|
46
46
|
name: string | undefined;
|
|
47
47
|
content: ChatModelInputMessageContent | undefined;
|
|
48
48
|
toolCalls: ChatModelOutputToolCall[] | undefined;
|
|
@@ -54,7 +54,7 @@ export declare class ToolMessageTemplate extends ChatMessageTemplate {
|
|
|
54
54
|
static from(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]): ToolMessageTemplate;
|
|
55
55
|
constructor(content: object | string, toolCallId: string, name?: string, options?: FormatOptions, cacheControl?: ChatModelInputMessage["cacheControl"]);
|
|
56
56
|
format(_variables?: Record<string, unknown>, _options?: FormatOptions): Promise<{
|
|
57
|
-
role: "
|
|
57
|
+
role: "agent" | "user" | "system" | "tool";
|
|
58
58
|
name: string | undefined;
|
|
59
59
|
content: ChatModelInputMessageContent | undefined;
|
|
60
60
|
toolCallId: string;
|
|
@@ -83,21 +83,21 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
83
83
|
ttl?: "5m" | "1h" | undefined;
|
|
84
84
|
}>>;
|
|
85
85
|
}, "strip", z.ZodTypeAny, {
|
|
86
|
-
content: string;
|
|
87
86
|
role: "system";
|
|
87
|
+
content: string;
|
|
88
|
+
name?: string | undefined;
|
|
88
89
|
cacheControl?: {
|
|
89
90
|
type: "ephemeral";
|
|
90
91
|
ttl?: "5m" | "1h" | undefined;
|
|
91
92
|
} | undefined;
|
|
92
|
-
name?: string | undefined;
|
|
93
93
|
}, {
|
|
94
|
-
content: string;
|
|
95
94
|
role: "system";
|
|
95
|
+
content: string;
|
|
96
|
+
name?: string | undefined;
|
|
96
97
|
cacheControl?: {
|
|
97
98
|
type: "ephemeral";
|
|
98
99
|
ttl?: "5m" | "1h" | undefined;
|
|
99
100
|
} | undefined;
|
|
100
|
-
name?: string | undefined;
|
|
101
101
|
}>, z.ZodObject<{
|
|
102
102
|
role: z.ZodLiteral<"user">;
|
|
103
103
|
content: z.ZodString;
|
|
@@ -113,21 +113,21 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
113
113
|
ttl?: "5m" | "1h" | undefined;
|
|
114
114
|
}>>;
|
|
115
115
|
}, "strip", z.ZodTypeAny, {
|
|
116
|
-
content: string;
|
|
117
116
|
role: "user";
|
|
117
|
+
content: string;
|
|
118
|
+
name?: string | undefined;
|
|
118
119
|
cacheControl?: {
|
|
119
120
|
type: "ephemeral";
|
|
120
121
|
ttl?: "5m" | "1h" | undefined;
|
|
121
122
|
} | undefined;
|
|
122
|
-
name?: string | undefined;
|
|
123
123
|
}, {
|
|
124
|
-
content: string;
|
|
125
124
|
role: "user";
|
|
125
|
+
content: string;
|
|
126
|
+
name?: string | undefined;
|
|
126
127
|
cacheControl?: {
|
|
127
128
|
type: "ephemeral";
|
|
128
129
|
ttl?: "5m" | "1h" | undefined;
|
|
129
130
|
} | undefined;
|
|
130
|
-
name?: string | undefined;
|
|
131
131
|
}>, z.ZodObject<{
|
|
132
132
|
role: z.ZodLiteral<"agent">;
|
|
133
133
|
content: z.ZodOptional<z.ZodString>;
|
|
@@ -172,6 +172,7 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
172
172
|
}>>;
|
|
173
173
|
}, "strip", z.ZodTypeAny, {
|
|
174
174
|
role: "agent";
|
|
175
|
+
name?: string | undefined;
|
|
175
176
|
content?: string | undefined;
|
|
176
177
|
cacheControl?: {
|
|
177
178
|
type: "ephemeral";
|
|
@@ -185,9 +186,9 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
185
186
|
type: "function";
|
|
186
187
|
id: string;
|
|
187
188
|
}[] | undefined;
|
|
188
|
-
name?: string | undefined;
|
|
189
189
|
}, {
|
|
190
190
|
role: "agent";
|
|
191
|
+
name?: string | undefined;
|
|
191
192
|
content?: string | undefined;
|
|
192
193
|
cacheControl?: {
|
|
193
194
|
type: "ephemeral";
|
|
@@ -201,7 +202,6 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
201
202
|
type: "function";
|
|
202
203
|
id: string;
|
|
203
204
|
}[] | undefined;
|
|
204
|
-
name?: string | undefined;
|
|
205
205
|
}>, z.ZodObject<{
|
|
206
206
|
role: z.ZodLiteral<"tool">;
|
|
207
207
|
content: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodUnknown>]>, string, string | Record<string, unknown>>;
|
|
@@ -218,23 +218,23 @@ declare const chatMessageSchema: z.ZodUnion<[z.ZodObject<{
|
|
|
218
218
|
ttl?: "5m" | "1h" | undefined;
|
|
219
219
|
}>>;
|
|
220
220
|
}, "strip", z.ZodTypeAny, {
|
|
221
|
-
content: string;
|
|
222
221
|
role: "tool";
|
|
222
|
+
content: string;
|
|
223
223
|
toolCallId: string;
|
|
224
|
+
name?: string | undefined;
|
|
224
225
|
cacheControl?: {
|
|
225
226
|
type: "ephemeral";
|
|
226
227
|
ttl?: "5m" | "1h" | undefined;
|
|
227
228
|
} | undefined;
|
|
228
|
-
name?: string | undefined;
|
|
229
229
|
}, {
|
|
230
|
-
content: string | Record<string, unknown>;
|
|
231
230
|
role: "tool";
|
|
231
|
+
content: string | Record<string, unknown>;
|
|
232
232
|
toolCallId: string;
|
|
233
|
+
name?: string | undefined;
|
|
233
234
|
cacheControl?: {
|
|
234
235
|
type: "ephemeral";
|
|
235
236
|
ttl?: "5m" | "1h" | undefined;
|
|
236
237
|
} | undefined;
|
|
237
|
-
name?: string | undefined;
|
|
238
238
|
}>]>;
|
|
239
239
|
export declare function safeParseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;
|
|
240
240
|
export declare function parseChatMessages(messages: (z.infer<typeof chatMessageSchema> & {
|
|
@@ -9,7 +9,7 @@ import type { Memory, MemoryAgent } from "../memory/memory.js";
|
|
|
9
9
|
import type { MemoryRecorderInput } from "../memory/recorder.js";
|
|
10
10
|
import type { MemoryRetrieverInput } from "../memory/retriever.js";
|
|
11
11
|
import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
|
|
12
|
-
import type { ChatModel } from "./chat-model.js";
|
|
12
|
+
import type { ChatModel, ChatModelInputMessage } from "./chat-model.js";
|
|
13
13
|
import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
14
14
|
import type { ImageModel } from "./image-model.js";
|
|
15
15
|
import { type GetterSchema, type TransferAgentOutput } from "./types.js";
|
|
@@ -118,7 +118,6 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
118
118
|
* agentSucceed, or agentFailed
|
|
119
119
|
*/
|
|
120
120
|
disableEvents?: boolean;
|
|
121
|
-
historyConfig?: Agent["historyConfig"];
|
|
122
121
|
/**
|
|
123
122
|
* One or more memory agents this agent can use
|
|
124
123
|
*/
|
|
@@ -211,15 +210,15 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
211
210
|
parsed: object;
|
|
212
211
|
}): Promise<Agent<I, O>>;
|
|
213
212
|
constructor(options?: AgentOptions<I, O>);
|
|
213
|
+
afs?: AFS;
|
|
214
|
+
tag?: string;
|
|
214
215
|
/**
|
|
215
216
|
* List of memories this agent can use
|
|
216
217
|
*
|
|
217
218
|
* @deprecated use afs instead
|
|
218
219
|
*/
|
|
219
220
|
readonly memories: MemoryAgent[];
|
|
220
|
-
afs?: AFS;
|
|
221
221
|
asyncMemoryRecord?: boolean;
|
|
222
|
-
tag?: string;
|
|
223
222
|
/**
|
|
224
223
|
* Maximum number of memory items to retrieve
|
|
225
224
|
*/
|
|
@@ -349,11 +348,6 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
349
348
|
* agentSucceed, or agentFailed
|
|
350
349
|
*/
|
|
351
350
|
private disableEvents?;
|
|
352
|
-
historyConfig?: {
|
|
353
|
-
disabled?: boolean;
|
|
354
|
-
maxTokens?: number;
|
|
355
|
-
maxItems?: number;
|
|
356
|
-
};
|
|
357
351
|
private subscriptions;
|
|
358
352
|
/**
|
|
359
353
|
* Attach agent to context:
|
|
@@ -455,7 +449,9 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
455
449
|
* @param options Invocation options
|
|
456
450
|
* @returns Final processed output
|
|
457
451
|
*/
|
|
458
|
-
protected processAgentOutput(input: I, output: Exclude<AgentResponse<O>, AgentResponseStream<O>>, options: AgentInvokeOptions
|
|
452
|
+
protected processAgentOutput(input: I, output: Exclude<AgentResponse<O>, AgentResponseStream<O>>, { messages, ...options }: AgentInvokeOptions & {
|
|
453
|
+
messages?: ChatModelInputMessage[];
|
|
454
|
+
}): Promise<O>;
|
|
459
455
|
/**
|
|
460
456
|
* Process errors that occur during agent execution
|
|
461
457
|
*
|
|
@@ -785,29 +781,29 @@ export interface AgentResponseProgress {
|
|
|
785
781
|
error: Error;
|
|
786
782
|
} | {
|
|
787
783
|
event: "message";
|
|
788
|
-
|
|
789
|
-
message: ({
|
|
790
|
-
type: "text";
|
|
791
|
-
content: string;
|
|
792
|
-
} | {
|
|
793
|
-
type: "thinking";
|
|
794
|
-
thoughts: string;
|
|
795
|
-
} | {
|
|
796
|
-
type: "tool_use";
|
|
797
|
-
toolUseId: string;
|
|
798
|
-
name: string;
|
|
799
|
-
input: unknown;
|
|
800
|
-
} | {
|
|
801
|
-
type: "tool_result";
|
|
802
|
-
toolUseId: string;
|
|
803
|
-
content: unknown;
|
|
804
|
-
})[];
|
|
784
|
+
message: ChatModelInputMessage;
|
|
805
785
|
}) & Omit<AgentEvent, "agent"> & {
|
|
806
786
|
agent: {
|
|
807
787
|
name: string;
|
|
808
788
|
};
|
|
809
789
|
};
|
|
810
790
|
}
|
|
791
|
+
export type AgentResponseProgressMessageItem = {
|
|
792
|
+
type: "text";
|
|
793
|
+
content: string;
|
|
794
|
+
} | {
|
|
795
|
+
type: "thinking";
|
|
796
|
+
thoughts: string;
|
|
797
|
+
} | {
|
|
798
|
+
type: "tool_use";
|
|
799
|
+
toolUseId: string;
|
|
800
|
+
name: string;
|
|
801
|
+
input: unknown;
|
|
802
|
+
} | {
|
|
803
|
+
type: "tool_result";
|
|
804
|
+
toolUseId: string;
|
|
805
|
+
content: unknown;
|
|
806
|
+
};
|
|
811
807
|
export declare function isAgentResponseProgress<T>(chunk: AgentResponseChunk<T>): chunk is AgentResponseProgress;
|
|
812
808
|
/**
|
|
813
809
|
* Creates a text delta for streaming responses
|
|
@@ -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
|