@aigne/core 1.72.0-beta.5 → 1.72.0-beta.7

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.
@@ -5,6 +5,7 @@ import { ZodObject } from "zod";
5
5
  import { zodToJsonSchema } from "zod-to-json-schema";
6
6
  import { Agent } from "../agents/agent.js";
7
7
  import { DEFAULT_OUTPUT_FILE_KEY, DEFAULT_OUTPUT_KEY } from "../agents/ai-agent.js";
8
+ import { roleSchema, } from "../agents/chat-model.js";
8
9
  import { fileUnionContentsSchema } from "../agents/model.js";
9
10
  import { optionalize } from "../loader/schema.js";
10
11
  import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
@@ -97,6 +98,9 @@ export class PromptBuilder {
97
98
  }
98
99
  async buildMessages(options) {
99
100
  const { input } = options;
101
+ const agentId = options.agent?.name;
102
+ const userId = options.context?.userContext.userId;
103
+ const sessionId = options.context?.userContext.sessionId;
100
104
  const inputKey = options.agent?.inputKey;
101
105
  const message = inputKey && typeof input?.[inputKey] === "string" ? input[inputKey] : undefined;
102
106
  const [messages, otherCustomMessages] = partition((await (typeof this.instructions === "string"
@@ -106,6 +110,50 @@ export class PromptBuilder {
106
110
  const files = flat(inputFileKey
107
111
  ? checkArguments("Check input files", optionalize(fileUnionContentsSchema), input?.[inputFileKey])
108
112
  : null);
113
+ const historyConfig = options.agent?.historyConfig;
114
+ const injectHistory = historyConfig?.inject === true || (historyConfig?.inject !== false && historyConfig?.enabled);
115
+ if (injectHistory) {
116
+ if (historyConfig.useOldMemory) {
117
+ messages.push(...(await this.deprecatedMemories(message, options)));
118
+ }
119
+ else {
120
+ const history = await this.getHistories({ ...options, agentId, userId, sessionId });
121
+ messages.push(...history);
122
+ }
123
+ }
124
+ // if the agent is using structured stream mode, add the instructions
125
+ const { structuredStreamMode, outputSchema } = options.agent || {};
126
+ if (structuredStreamMode && outputSchema) {
127
+ const instructions = options.agent?.customStructuredStreamInstructions?.instructions ||
128
+ PromptBuilder.from(STRUCTURED_STREAM_INSTRUCTIONS.instructions);
129
+ messages.push(...(await instructions.buildMessages({
130
+ input: {
131
+ ...input,
132
+ outputJsonSchema: zodToJsonSchema(outputSchema),
133
+ },
134
+ })));
135
+ }
136
+ messages.push(...otherCustomMessages);
137
+ if (message || files.length) {
138
+ const content = [];
139
+ if (message &&
140
+ // avoid duplicate user messages: developer may have already included the message in the messages
141
+ !otherCustomMessages.some((i) => i.role === "user" &&
142
+ (typeof i.content === "string"
143
+ ? i.content.includes(message)
144
+ : i.content?.some((c) => c.type === "text" && c.text.includes(message))))) {
145
+ content.push({ type: "text", text: message });
146
+ }
147
+ if (files.length)
148
+ content.push(...files);
149
+ if (content.length) {
150
+ messages.push({ role: "user", content });
151
+ }
152
+ }
153
+ return this.refineMessages(options, messages);
154
+ }
155
+ async deprecatedMemories(message, options) {
156
+ const messages = [];
109
157
  const memories = [];
110
158
  if (options.agent && options.context) {
111
159
  memories.push(...(await options.agent.retrieveMemories({ search: message }, { context: options.context })));
@@ -114,7 +162,7 @@ export class PromptBuilder {
114
162
  memories.push(...options.context.memories);
115
163
  }
116
164
  const afs = options.agent?.afs;
117
- if (afs && options.agent?.historyConfig?.disabled !== true) {
165
+ if (afs && options.agent?.historyConfig?.enabled) {
118
166
  const historyModule = (await afs.listModules()).find((m) => m.module instanceof AFSHistory);
119
167
  if (historyModule) {
120
168
  const history = await afs.list(historyModule.path, {
@@ -160,38 +208,9 @@ export class PromptBuilder {
160
208
  }
161
209
  if (memories.length)
162
210
  messages.push(...(await this.convertMemoriesToMessages(memories, options)));
163
- // if the agent is using structured stream mode, add the instructions
164
- const { structuredStreamMode, outputSchema } = options.agent || {};
165
- if (structuredStreamMode && outputSchema) {
166
- const instructions = options.agent?.customStructuredStreamInstructions?.instructions ||
167
- PromptBuilder.from(STRUCTURED_STREAM_INSTRUCTIONS.instructions);
168
- messages.push(...(await instructions.buildMessages({
169
- input: {
170
- ...input,
171
- outputJsonSchema: zodToJsonSchema(outputSchema),
172
- },
173
- })));
174
- }
175
- if (message || files.length) {
176
- const content = [];
177
- if (message &&
178
- // avoid duplicate user messages: developer may have already included the message in the custom user messages
179
- !otherCustomMessages.some((i) => i.role === "user" &&
180
- (typeof i.content === "string"
181
- ? i.content.includes(message)
182
- : i.content?.some((c) => c.type === "text" && c.text.includes(message))))) {
183
- content.push({ type: "text", text: message });
184
- }
185
- if (files.length)
186
- content.push(...files);
187
- if (content.length) {
188
- messages.push({ role: "user", content });
189
- }
190
- }
191
- messages.push(...otherCustomMessages);
192
- return this.refineMessages(options, messages);
211
+ return messages;
193
212
  }
194
- async getHistories(options) {
213
+ async getHistories({ agentId, userId, sessionId, ...options }) {
195
214
  const { agent } = options;
196
215
  const afs = agent?.afs;
197
216
  if (!afs)
@@ -200,24 +219,21 @@ export class PromptBuilder {
200
219
  if (!historyModule)
201
220
  return [];
202
221
  const history = (await afs.list(historyModule.path, {
222
+ filter: { agentId, userId, sessionId },
203
223
  limit: agent.historyConfig?.maxItems || 10,
204
224
  orderBy: [["createdAt", "desc"]],
205
- })).data;
206
- return history
207
- .reverse()
208
- .map((i) => {
209
- if (!i.content)
210
- return;
211
- const { input, output } = i.content;
212
- if (!input || !output)
213
- return;
214
- return [
215
- { role: "user", content: input },
216
- { role: "agent", content: output },
217
- ];
218
- })
219
- .filter(isNonNullable)
220
- .flat();
225
+ })).data.reverse();
226
+ return (await Promise.all(history.map(async (i) => {
227
+ if (Array.isArray(i.content?.messages) &&
228
+ i.content.messages.every((m) => roleSchema.parse(m?.role))) {
229
+ return i.content.messages;
230
+ }
231
+ const { input, output } = i.content || {};
232
+ if (input && output) {
233
+ return await this.convertMemoriesToMessages([{ content: { input, output } }], options);
234
+ }
235
+ return [];
236
+ }))).flat();
221
237
  }
222
238
  refineMessages(options, messages) {
223
239
  const { autoReorderSystemMessages, autoMergeSystemMessages } = options.agent ?? {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.72.0-beta.5",
3
+ "version": "1.72.0-beta.7",
4
4
  "description": "The functional core of agentic AI",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -93,9 +93,9 @@
93
93
  "zod": "^3.25.67",
94
94
  "zod-from-json-schema": "^0.0.5",
95
95
  "zod-to-json-schema": "^3.24.6",
96
- "@aigne/afs": "^1.4.0-beta.3",
96
+ "@aigne/afs": "^1.4.0-beta.4",
97
+ "@aigne/afs-history": "^1.2.0-beta.4",
97
98
  "@aigne/observability-api": "^0.11.14-beta.1",
98
- "@aigne/afs-history": "^1.2.0-beta.3",
99
99
  "@aigne/platform-helpers": "^0.6.7-beta"
100
100
  },
101
101
  "devDependencies": {