@aigne/core 1.57.4 → 1.58.0

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.
@@ -3,8 +3,11 @@ import { stringify } from "yaml";
3
3
  import { ZodObject } from "zod";
4
4
  import { zodToJsonSchema } from "zod-to-json-schema";
5
5
  import { Agent } from "../agents/agent.js";
6
+ import { DEFAULT_FILE_OUTPUT_KEY, DEFAULT_OUTPUT_KEY } from "../agents/ai-agent.js";
7
+ import { fileUnionContentsSchema, } from "../agents/chat-model.js";
8
+ import { optionalize } from "../loader/schema.js";
6
9
  import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
7
- import { isNil, isRecord, unique } from "../utils/type-utils.js";
10
+ import { checkArguments, flat, isRecord, unique } from "../utils/type-utils.js";
8
11
  import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
9
12
  import { STRUCTURED_STREAM_INSTRUCTIONS } from "./prompts/structured-stream-instructions.js";
10
13
  import { AgentMessageTemplate, ChatMessagesTemplate, PromptTemplate, SystemMessageTemplate, UserMessageTemplate, } from "./template.js";
@@ -34,11 +37,11 @@ export class PromptBuilder {
34
37
  content = resource.text;
35
38
  }
36
39
  else if (typeof resource.blob === "string") {
37
- content = [{ type: "image_url", url: resource.blob }];
40
+ content = [{ type: "url", url: resource.blob }];
38
41
  }
39
42
  }
40
43
  else if (i.content.type === "image") {
41
- content = [{ type: "image_url", url: i.content.data }];
44
+ content = [{ type: "url", url: i.content.data }];
42
45
  }
43
46
  if (!content)
44
47
  throw new Error(`Unsupported content type ${i.content.type}`);
@@ -62,6 +65,7 @@ export class PromptBuilder {
62
65
  responseFormat: options.agent?.structuredStreamMode
63
66
  ? undefined
64
67
  : this.buildResponseFormat(options),
68
+ fileOutputType: options.agent?.fileOutputType,
65
69
  ...this.buildTools(options),
66
70
  };
67
71
  }
@@ -80,6 +84,10 @@ export class PromptBuilder {
80
84
  const messages = (await (typeof this.instructions === "string"
81
85
  ? ChatMessagesTemplate.from([SystemMessageTemplate.from(this.instructions)])
82
86
  : this.instructions)?.format(options.input, { workingDir: this.workingDir })) ?? [];
87
+ const fileInputKey = options.agent?.fileInputKey;
88
+ const files = flat(fileInputKey
89
+ ? checkArguments("Check input files", optionalize(fileUnionContentsSchema), input?.[fileInputKey])
90
+ : null);
83
91
  const memories = [];
84
92
  if (options.agent && options.context) {
85
93
  memories.push(...(await options.agent.retrieveMemories({ search: message }, { context: options.context })));
@@ -101,26 +109,68 @@ export class PromptBuilder {
101
109
  },
102
110
  })));
103
111
  }
104
- if (message) {
105
- messages.push({
106
- role: "user",
107
- content: message,
108
- });
112
+ if (message || files.length) {
113
+ const content = [];
114
+ if (message)
115
+ content.push({ type: "text", text: message });
116
+ if (files.length)
117
+ content.push(...files);
118
+ messages.push({ role: "user", content });
109
119
  }
110
120
  return messages;
111
121
  }
112
122
  async convertMemoriesToMessages(memories, options) {
113
123
  const messages = [];
114
124
  const other = [];
125
+ const inputKey = options.agent?.inputKey;
126
+ const fileInputKey = options.agent?.fileInputKey;
127
+ const outputKey = options.agent?.outputKey || DEFAULT_OUTPUT_KEY;
128
+ const fileOutputKey = options.agent?.fileOutputKey || DEFAULT_FILE_OUTPUT_KEY;
115
129
  const stringOrStringify = (value) => typeof value === "string" ? value : stringify(value);
116
- for (const { content } of memories) {
117
- if (isRecord(content) && "input" in content && "output" in content) {
118
- if (!isNil(content.input) && content.input !== "") {
119
- messages.push({ role: "user", content: stringOrStringify(content.input) });
130
+ const convertMemoryToMessage = async (content) => {
131
+ const { input, output } = content;
132
+ if (!input || !output)
133
+ return [];
134
+ const result = [];
135
+ const userMessageContent = [];
136
+ if (typeof input === "object") {
137
+ const inputMessage = inputKey ? Reflect.get(input, inputKey) : undefined;
138
+ const inputFiles = fileInputKey ? Reflect.get(input, fileInputKey) : undefined;
139
+ if (inputMessage) {
140
+ userMessageContent.push({ type: "text", text: stringOrStringify(inputMessage) });
120
141
  }
121
- if (!isNil(content.output) && content.output !== "") {
122
- messages.push({ role: "agent", content: stringOrStringify(content.output) });
142
+ if (inputFiles) {
143
+ userMessageContent.push(...flat(checkArguments("Check memory input files", optionalize(fileUnionContentsSchema), inputFiles)));
144
+ }
145
+ }
146
+ if (!userMessageContent.length) {
147
+ userMessageContent.push({ type: "text", text: stringOrStringify(input) });
148
+ }
149
+ result.push({ role: "user", content: userMessageContent });
150
+ const agentMessageContent = [];
151
+ if (typeof output === "object") {
152
+ const outputMessage = Reflect.get(output, outputKey);
153
+ const outputFiles = Reflect.get(output, fileOutputKey);
154
+ if (outputMessage) {
155
+ agentMessageContent.push({ type: "text", text: stringOrStringify(outputMessage) });
123
156
  }
157
+ if (outputFiles) {
158
+ agentMessageContent.push(...flat(checkArguments("Check memory output files", optionalize(fileUnionContentsSchema), outputFiles)));
159
+ }
160
+ }
161
+ if (!agentMessageContent.length) {
162
+ agentMessageContent.push({ type: "text", text: stringOrStringify(output) });
163
+ }
164
+ result.push({ role: "agent", content: agentMessageContent });
165
+ return result;
166
+ };
167
+ for (const { content } of memories) {
168
+ if (isRecord(content) &&
169
+ "input" in content &&
170
+ content.input &&
171
+ "output" in content &&
172
+ content.output) {
173
+ messages.push(...(await convertMemoryToMessage(content)));
124
174
  }
125
175
  else {
126
176
  other.push(content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.57.4",
3
+ "version": "1.58.0",
4
4
  "description": "The functional core of agentic AI",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -82,6 +82,7 @@
82
82
  "is-network-error": "^1.1.0",
83
83
  "jaison": "^2.0.2",
84
84
  "jsonata": "^2.0.6",
85
+ "mime": "^4.0.7",
85
86
  "nunjucks": "^3.2.4",
86
87
  "p-retry": "^6.2.1",
87
88
  "raw-body": "^3.0.0",
@@ -91,7 +92,7 @@
91
92
  "yaml": "^2.8.0",
92
93
  "zod": "^3.25.67",
93
94
  "zod-to-json-schema": "^3.24.6",
94
- "@aigne/observability-api": "^0.10.1",
95
+ "@aigne/observability-api": "^0.10.2",
95
96
  "@aigne/platform-helpers": "^0.6.2"
96
97
  },
97
98
  "devDependencies": {