@aigne/core 1.57.5 → 1.58.1

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 CHANGED
@@ -1,5 +1,31 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.58.1](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.58.0...core-v1.58.1) (2025-09-05)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * should not return local path from aigne hub service ([#460](https://github.com/AIGNE-io/aigne-framework/issues/460)) ([c959717](https://github.com/AIGNE-io/aigne-framework/commit/c95971774f7e84dbeb3313f60b3e6464e2bb22e4))
9
+
10
+ ## [1.58.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.57.5...core-v1.58.0) (2025-09-05)
11
+
12
+
13
+ ### Features
14
+
15
+ * add modalities support for chat model ([#454](https://github.com/AIGNE-io/aigne-framework/issues/454)) ([70d1bf6](https://github.com/AIGNE-io/aigne-framework/commit/70d1bf631f4e711235d89c6df8ee210a19179b30))
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * add disabled observability env ([#453](https://github.com/AIGNE-io/aigne-framework/issues/453)) ([3e01107](https://github.com/AIGNE-io/aigne-framework/commit/3e01107deb07d3e4eb6fbe49a7b39919fa412df1))
21
+
22
+
23
+ ### Dependencies
24
+
25
+ * The following workspace dependencies were updated
26
+ * dependencies
27
+ * @aigne/observability-api bumped to 0.10.2
28
+
3
29
  ## [1.57.5](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.57.4...core-v1.57.5) (2025-09-01)
4
30
 
5
31
 
@@ -432,7 +432,7 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
432
432
  * @param options Invocation options
433
433
  * @returns Final processed output
434
434
  */
435
- private processAgentOutput;
435
+ protected processAgentOutput(input: I, output: Exclude<AgentResponse<O>, AgentResponseStream<O>>, options: AgentInvokeOptions): Promise<O>;
436
436
  /**
437
437
  * Process errors that occur during agent execution
438
438
  *
@@ -1,9 +1,10 @@
1
1
  import { type ZodObject, type ZodType, z } from "zod";
2
2
  import { PromptBuilder } from "../prompt/prompt-builder.js";
3
3
  import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
4
- import type { ChatModel, ChatModelInput } from "./chat-model.js";
4
+ import type { ChatModel, ChatModelInput, FileOutputType } from "./chat-model.js";
5
5
  import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
6
6
  export declare const DEFAULT_OUTPUT_KEY = "message";
7
+ export declare const DEFAULT_FILE_OUTPUT_KEY = "files";
7
8
  /**
8
9
  * Configuration options for an AI Agent
9
10
  *
@@ -31,12 +32,15 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
31
32
  * Pick a message from input to use as the user's message
32
33
  */
33
34
  inputKey?: string;
35
+ fileInputKey?: string;
34
36
  /**
35
37
  * Custom key to use for text output in the response
36
38
  *
37
39
  * Defaults to `message` if not specified
38
40
  */
39
41
  outputKey?: string;
42
+ fileOutputKey?: string;
43
+ fileOutputType?: FileOutputType;
40
44
  /**
41
45
  * Controls how the agent uses tools during execution
42
46
  *
@@ -217,6 +221,7 @@ export declare class AIAgent<I extends Message = any, O extends Message = any> e
217
221
  * Pick a message from input to use as the user's message
218
222
  */
219
223
  inputKey?: string;
224
+ fileInputKey?: string;
220
225
  /**
221
226
  * Custom key to use for text output in the response
222
227
  *
@@ -225,6 +230,8 @@ export declare class AIAgent<I extends Message = any, O extends Message = any> e
225
230
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
226
231
  */
227
232
  outputKey: string;
233
+ fileOutputKey: string;
234
+ fileOutputType?: FileOutputType;
228
235
  /**
229
236
  * Controls how the agent uses tools during execution
230
237
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = exports.AIAgentToolChoice = exports.DEFAULT_OUTPUT_KEY = void 0;
3
+ exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = exports.AIAgentToolChoice = exports.DEFAULT_FILE_OUTPUT_KEY = exports.DEFAULT_OUTPUT_KEY = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
6
6
  const structured_stream_instructions_js_1 = require("../prompt/prompts/structured-stream-instructions.js");
@@ -10,6 +10,7 @@ const type_utils_js_1 = require("../utils/type-utils.js");
10
10
  const agent_js_1 = require("./agent.js");
11
11
  const types_js_1 = require("./types.js");
12
12
  exports.DEFAULT_OUTPUT_KEY = "message";
13
+ exports.DEFAULT_FILE_OUTPUT_KEY = "files";
13
14
  /**
14
15
  * Tool choice options for AI agents
15
16
  *
@@ -111,7 +112,10 @@ class AIAgent extends agent_js_1.Agent {
111
112
  ? prompt_builder_js_1.PromptBuilder.from(options.instructions)
112
113
  : (options.instructions ?? new prompt_builder_js_1.PromptBuilder());
113
114
  this.inputKey = options.inputKey;
115
+ this.fileInputKey = options.fileInputKey;
114
116
  this.outputKey = options.outputKey || exports.DEFAULT_OUTPUT_KEY;
117
+ this.fileOutputKey = options.fileOutputKey || exports.DEFAULT_FILE_OUTPUT_KEY;
118
+ this.fileOutputType = options.fileOutputType;
115
119
  this.toolChoice = options.toolChoice;
116
120
  this.memoryAgentsAsTools = options.memoryAgentsAsTools;
117
121
  this.memoryPromptTemplate = options.memoryPromptTemplate;
@@ -151,6 +155,7 @@ class AIAgent extends agent_js_1.Agent {
151
155
  * Pick a message from input to use as the user's message
152
156
  */
153
157
  inputKey;
158
+ fileInputKey;
154
159
  /**
155
160
  * Custom key to use for text output in the response
156
161
  *
@@ -159,6 +164,8 @@ class AIAgent extends agent_js_1.Agent {
159
164
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
160
165
  */
161
166
  outputKey;
167
+ fileOutputKey;
168
+ fileOutputType;
162
169
  /**
163
170
  * Controls how the agent uses tools during execution
164
171
  *
@@ -269,7 +276,7 @@ class AIAgent extends agent_js_1.Agent {
269
276
  }
270
277
  }
271
278
  }
272
- const { toolCalls, json, text } = modelOutput;
279
+ const { toolCalls, json, text, files } = modelOutput;
273
280
  if (toolCalls?.length) {
274
281
  const executedToolCalls = [];
275
282
  // Execute tools
@@ -308,6 +315,9 @@ class AIAgent extends agent_js_1.Agent {
308
315
  if (text) {
309
316
  Object.assign(result, { [outputKey]: text });
310
317
  }
318
+ if (files) {
319
+ Object.assign(result, { [this.fileOutputKey]: files });
320
+ }
311
321
  if (!(0, type_utils_js_1.isEmpty)(result)) {
312
322
  yield { delta: { json: result } };
313
323
  }
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { type PromiseOrValue } from "../utils/type-utils.js";
3
- import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
3
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type AgentResponse, type AgentResponseStream, type Message } from "./agent.js";
4
4
  export declare class StructuredOutputError extends Error {
5
5
  }
6
6
  /**
@@ -104,6 +104,11 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
104
104
  * @returns A promise or direct value containing the model's response
105
105
  */
106
106
  abstract process(input: ChatModelInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
107
+ protected processAgentOutput(input: ChatModelInput, output: Exclude<AgentResponse<ChatModelOutput>, AgentResponseStream<ChatModelOutput>>, options: AgentInvokeOptions): Promise<ChatModelOutput>;
108
+ transformFileOutput(input: ChatModelInput, data: FileUnionContent, options: AgentInvokeOptions): Promise<FileUnionContent>;
109
+ static getFileExtension(type: string): string | undefined;
110
+ static getMimeType(filename: string): string | undefined;
111
+ protected downloadFile(url: string): Promise<Response>;
107
112
  }
108
113
  /**
109
114
  * Input message format for ChatModel
@@ -128,6 +133,7 @@ export interface ChatModelInput extends Message {
128
133
  * Specifies the expected response format
129
134
  */
130
135
  responseFormat?: ChatModelInputResponseFormat;
136
+ fileOutputType?: FileOutputType;
131
137
  /**
132
138
  * List of tools available for the model to use
133
139
  */
@@ -190,7 +196,7 @@ export interface ChatModelInputMessage {
190
196
  *
191
197
  * Can be a simple string, or a mixed array of text and image content
192
198
  */
193
- export type ChatModelInputMessageContent = string | (TextContent | ImageUrlContent)[];
199
+ export type ChatModelInputMessageContent = string | UnionContent[];
194
200
  /**
195
201
  * Text content type
196
202
  *
@@ -200,15 +206,304 @@ export type TextContent = {
200
206
  type: "text";
201
207
  text: string;
202
208
  };
209
+ export declare const textContentSchema: z.ZodObject<{
210
+ type: z.ZodLiteral<"text">;
211
+ text: z.ZodString;
212
+ }, "strip", z.ZodTypeAny, {
213
+ type: "text";
214
+ text: string;
215
+ }, {
216
+ type: "text";
217
+ text: string;
218
+ }>;
219
+ export interface FileContentBase {
220
+ filename?: string;
221
+ mimeType?: string;
222
+ }
223
+ export declare const fileContentBaseSchema: z.ZodObject<{
224
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
225
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
226
+ }, "strip", z.ZodTypeAny, {
227
+ filename?: string | undefined;
228
+ mimeType?: string | undefined;
229
+ }, {
230
+ filename?: string | undefined;
231
+ mimeType?: string | undefined;
232
+ }>;
203
233
  /**
204
234
  * Image URL content type
205
235
  *
206
236
  * Used for image parts of message content, referencing images via URL
207
237
  */
208
- export type ImageUrlContent = {
209
- type: "image_url";
238
+ export interface UrlContent extends FileContentBase {
239
+ type: "url";
210
240
  url: string;
211
- };
241
+ }
242
+ export declare const urlContentSchema: z.ZodObject<{
243
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
244
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
245
+ } & {
246
+ type: z.ZodLiteral<"url">;
247
+ url: z.ZodString;
248
+ }, "strip", z.ZodTypeAny, {
249
+ type: "url";
250
+ url: string;
251
+ filename?: string | undefined;
252
+ mimeType?: string | undefined;
253
+ }, {
254
+ type: "url";
255
+ url: string;
256
+ filename?: string | undefined;
257
+ mimeType?: string | undefined;
258
+ }>;
259
+ export interface FileContent extends FileContentBase {
260
+ type: "file";
261
+ data: string;
262
+ }
263
+ export declare const fileContentSchema: z.ZodObject<{
264
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
265
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
266
+ } & {
267
+ type: z.ZodLiteral<"file">;
268
+ data: z.ZodString;
269
+ }, "strip", z.ZodTypeAny, {
270
+ type: "file";
271
+ data: string;
272
+ filename?: string | undefined;
273
+ mimeType?: string | undefined;
274
+ }, {
275
+ type: "file";
276
+ data: string;
277
+ filename?: string | undefined;
278
+ mimeType?: string | undefined;
279
+ }>;
280
+ export interface LocalContent extends FileContentBase {
281
+ type: "local";
282
+ path: string;
283
+ }
284
+ export declare const localContentSchema: z.ZodObject<{
285
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
286
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
287
+ } & {
288
+ type: z.ZodLiteral<"local">;
289
+ path: z.ZodString;
290
+ }, "strip", z.ZodTypeAny, {
291
+ path: string;
292
+ type: "local";
293
+ filename?: string | undefined;
294
+ mimeType?: string | undefined;
295
+ }, {
296
+ path: string;
297
+ type: "local";
298
+ filename?: string | undefined;
299
+ mimeType?: string | undefined;
300
+ }>;
301
+ export type FileUnionContent = LocalContent | UrlContent | FileContent;
302
+ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
303
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
304
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
305
+ } & {
306
+ type: z.ZodLiteral<"local">;
307
+ path: z.ZodString;
308
+ }, "strip", z.ZodTypeAny, {
309
+ path: string;
310
+ type: "local";
311
+ filename?: string | undefined;
312
+ mimeType?: string | undefined;
313
+ }, {
314
+ path: string;
315
+ type: "local";
316
+ filename?: string | undefined;
317
+ mimeType?: string | undefined;
318
+ }>, z.ZodObject<{
319
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
320
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
321
+ } & {
322
+ type: z.ZodLiteral<"url">;
323
+ url: z.ZodString;
324
+ }, "strip", z.ZodTypeAny, {
325
+ type: "url";
326
+ url: string;
327
+ filename?: string | undefined;
328
+ mimeType?: string | undefined;
329
+ }, {
330
+ type: "url";
331
+ url: string;
332
+ filename?: string | undefined;
333
+ mimeType?: string | undefined;
334
+ }>, z.ZodObject<{
335
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
336
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
337
+ } & {
338
+ type: z.ZodLiteral<"file">;
339
+ data: z.ZodString;
340
+ }, "strip", z.ZodTypeAny, {
341
+ type: "file";
342
+ data: string;
343
+ filename?: string | undefined;
344
+ mimeType?: string | undefined;
345
+ }, {
346
+ type: "file";
347
+ data: string;
348
+ filename?: string | undefined;
349
+ mimeType?: string | undefined;
350
+ }>]>;
351
+ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
352
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
353
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
354
+ } & {
355
+ type: z.ZodLiteral<"local">;
356
+ path: z.ZodString;
357
+ }, "strip", z.ZodTypeAny, {
358
+ path: string;
359
+ type: "local";
360
+ filename?: string | undefined;
361
+ mimeType?: string | undefined;
362
+ }, {
363
+ path: string;
364
+ type: "local";
365
+ filename?: string | undefined;
366
+ mimeType?: string | undefined;
367
+ }>, z.ZodObject<{
368
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
369
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
370
+ } & {
371
+ type: z.ZodLiteral<"url">;
372
+ url: z.ZodString;
373
+ }, "strip", z.ZodTypeAny, {
374
+ type: "url";
375
+ url: string;
376
+ filename?: string | undefined;
377
+ mimeType?: string | undefined;
378
+ }, {
379
+ type: "url";
380
+ url: string;
381
+ filename?: string | undefined;
382
+ mimeType?: string | undefined;
383
+ }>, z.ZodObject<{
384
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
385
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
386
+ } & {
387
+ type: z.ZodLiteral<"file">;
388
+ data: z.ZodString;
389
+ }, "strip", z.ZodTypeAny, {
390
+ type: "file";
391
+ data: string;
392
+ filename?: string | undefined;
393
+ mimeType?: string | undefined;
394
+ }, {
395
+ type: "file";
396
+ data: string;
397
+ filename?: string | undefined;
398
+ mimeType?: string | undefined;
399
+ }>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
400
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
401
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
402
+ } & {
403
+ type: z.ZodLiteral<"local">;
404
+ path: z.ZodString;
405
+ }, "strip", z.ZodTypeAny, {
406
+ path: string;
407
+ type: "local";
408
+ filename?: string | undefined;
409
+ mimeType?: string | undefined;
410
+ }, {
411
+ path: string;
412
+ type: "local";
413
+ filename?: string | undefined;
414
+ mimeType?: string | undefined;
415
+ }>, z.ZodObject<{
416
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
417
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
418
+ } & {
419
+ type: z.ZodLiteral<"url">;
420
+ url: z.ZodString;
421
+ }, "strip", z.ZodTypeAny, {
422
+ type: "url";
423
+ url: string;
424
+ filename?: string | undefined;
425
+ mimeType?: string | undefined;
426
+ }, {
427
+ type: "url";
428
+ url: string;
429
+ filename?: string | undefined;
430
+ mimeType?: string | undefined;
431
+ }>, z.ZodObject<{
432
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
433
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
434
+ } & {
435
+ type: z.ZodLiteral<"file">;
436
+ data: z.ZodString;
437
+ }, "strip", z.ZodTypeAny, {
438
+ type: "file";
439
+ data: string;
440
+ filename?: string | undefined;
441
+ mimeType?: string | undefined;
442
+ }, {
443
+ type: "file";
444
+ data: string;
445
+ filename?: string | undefined;
446
+ mimeType?: string | undefined;
447
+ }>]>, "many">]>;
448
+ export type UnionContent = TextContent | FileUnionContent;
449
+ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
450
+ type: z.ZodLiteral<"text">;
451
+ text: z.ZodString;
452
+ }, "strip", z.ZodTypeAny, {
453
+ type: "text";
454
+ text: string;
455
+ }, {
456
+ type: "text";
457
+ text: string;
458
+ }>, z.ZodObject<{
459
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
460
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
461
+ } & {
462
+ type: z.ZodLiteral<"local">;
463
+ path: z.ZodString;
464
+ }, "strip", z.ZodTypeAny, {
465
+ path: string;
466
+ type: "local";
467
+ filename?: string | undefined;
468
+ mimeType?: string | undefined;
469
+ }, {
470
+ path: string;
471
+ type: "local";
472
+ filename?: string | undefined;
473
+ mimeType?: string | undefined;
474
+ }>, z.ZodObject<{
475
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
476
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
477
+ } & {
478
+ type: z.ZodLiteral<"url">;
479
+ url: z.ZodString;
480
+ }, "strip", z.ZodTypeAny, {
481
+ type: "url";
482
+ url: string;
483
+ filename?: string | undefined;
484
+ mimeType?: string | undefined;
485
+ }, {
486
+ type: "url";
487
+ url: string;
488
+ filename?: string | undefined;
489
+ mimeType?: string | undefined;
490
+ }>, z.ZodObject<{
491
+ filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
492
+ mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
493
+ } & {
494
+ type: z.ZodLiteral<"file">;
495
+ data: z.ZodString;
496
+ }, "strip", z.ZodTypeAny, {
497
+ type: "file";
498
+ data: string;
499
+ filename?: string | undefined;
500
+ mimeType?: string | undefined;
501
+ }, {
502
+ type: "file";
503
+ data: string;
504
+ filename?: string | undefined;
505
+ mimeType?: string | undefined;
506
+ }>]>;
212
507
  /**
213
508
  * Model response format settings
214
509
  *
@@ -277,6 +572,7 @@ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
277
572
  description?: string;
278
573
  };
279
574
  };
575
+ export type Modality = "text" | "image" | "audio";
280
576
  /**
281
577
  * Model-specific configuration options
282
578
  *
@@ -307,6 +603,7 @@ export interface ChatModelOptions {
307
603
  * Whether to allow parallel tool calls
308
604
  */
309
605
  parallelToolCalls?: boolean;
606
+ modalities?: Modality[];
310
607
  }
311
608
  /**
312
609
  * Output message format for ChatModel
@@ -342,6 +639,11 @@ export interface ChatModelOutput extends Message {
342
639
  * Model name or version used
343
640
  */
344
641
  model?: string;
642
+ files?: FileUnionContent[];
643
+ }
644
+ export declare enum FileOutputType {
645
+ local = "local",
646
+ file = "file"
345
647
  }
346
648
  /**
347
649
  * Tool call information in model output
@@ -32,10 +32,17 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.chatModelOutputUsageSchema = exports.ChatModel = exports.StructuredOutputError = void 0;
39
+ exports.chatModelOutputUsageSchema = exports.FileOutputType = exports.unionContentSchema = exports.fileUnionContentsSchema = exports.fileUnionContentSchema = exports.localContentSchema = exports.fileContentSchema = exports.urlContentSchema = exports.fileContentBaseSchema = exports.textContentSchema = exports.ChatModel = exports.StructuredOutputError = void 0;
40
+ const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
37
41
  const ajv_1 = require("ajv");
42
+ const mime_1 = __importDefault(require("mime"));
43
+ const uuid_1 = require("uuid");
38
44
  const zod_1 = require("zod");
45
+ const schema_js_1 = require("../loader/schema.js");
39
46
  const type_utils_js_1 = require("../utils/type-utils.js");
40
47
  const agent_js_1 = require("./agent.js");
41
48
  const CHAT_MODEL_DEFAULT_RETRY_OPTIONS = {
@@ -207,19 +214,111 @@ class ChatModel extends agent_js_1.Agent {
207
214
  options.context.usage.aigneHubCredits += usage.aigneHubCredits;
208
215
  }
209
216
  }
217
+ async processAgentOutput(input, output, options) {
218
+ if (output.files) {
219
+ const files = zod_1.z.array(exports.fileUnionContentSchema).parse(output.files);
220
+ output = {
221
+ ...output,
222
+ files: await Promise.all(files.map((file) => this.transformFileOutput(input, file, options))),
223
+ };
224
+ }
225
+ return super.processAgentOutput(input, output, options);
226
+ }
227
+ async transformFileOutput(input, data, options) {
228
+ const fileOutputType = input.fileOutputType || FileOutputType.local;
229
+ if (fileOutputType === data.type)
230
+ return data;
231
+ const common = (0, type_utils_js_1.pick)(data, "filename", "mimeType");
232
+ switch (fileOutputType) {
233
+ case FileOutputType.local: {
234
+ const dir = index_js_1.nodejs.path.join(index_js_1.nodejs.os.tmpdir(), options.context.id);
235
+ await index_js_1.nodejs.fs.mkdir(dir, { recursive: true });
236
+ const ext = ChatModel.getFileExtension(data.mimeType || data.filename || "");
237
+ const id = (0, uuid_1.v7)();
238
+ const filename = ext ? `${id}.${ext}` : id;
239
+ const path = index_js_1.nodejs.path.join(dir, filename);
240
+ if (data.type === "file") {
241
+ await index_js_1.nodejs.fs.writeFile(path, data.data, "base64");
242
+ }
243
+ else if (data.type === "url") {
244
+ await this.downloadFile(data.url)
245
+ .then((res) => res.body)
246
+ .then((body) => body && index_js_1.nodejs.fs.writeFile(path, body));
247
+ }
248
+ else {
249
+ throw new Error(`Unexpected file type: ${data.type}`);
250
+ }
251
+ return { ...common, type: "local", path };
252
+ }
253
+ case FileOutputType.file: {
254
+ let base64;
255
+ if (data.type === "local") {
256
+ base64 = await index_js_1.nodejs.fs.readFile(data.path, "base64");
257
+ }
258
+ else if (data.type === "url") {
259
+ base64 = Buffer.from(await (await this.downloadFile(data.url)).arrayBuffer()).toString("base64");
260
+ }
261
+ else {
262
+ throw new Error(`Unexpected file type: ${data.type}`);
263
+ }
264
+ return { ...common, type: "file", data: base64 };
265
+ }
266
+ }
267
+ }
268
+ static getFileExtension(type) {
269
+ return mime_1.default.getExtension(type) || undefined;
270
+ }
271
+ static getMimeType(filename) {
272
+ return mime_1.default.getType(filename) || undefined;
273
+ }
274
+ async downloadFile(url) {
275
+ const response = await fetch(url);
276
+ if (!response.ok) {
277
+ const text = await response.text().catch(() => null);
278
+ throw new Error(`Failed to download content from ${url}, ${response.status} ${response.statusText} ${text}`);
279
+ }
280
+ return response;
281
+ }
210
282
  }
211
283
  exports.ChatModel = ChatModel;
284
+ exports.textContentSchema = zod_1.z.object({
285
+ type: zod_1.z.literal("text"),
286
+ text: zod_1.z.string(),
287
+ });
288
+ exports.fileContentBaseSchema = zod_1.z.object({
289
+ filename: (0, schema_js_1.optionalize)(zod_1.z.string()),
290
+ mimeType: (0, schema_js_1.optionalize)(zod_1.z.string()),
291
+ });
292
+ exports.urlContentSchema = exports.fileContentBaseSchema.extend({
293
+ type: zod_1.z.literal("url"),
294
+ url: zod_1.z.string(),
295
+ });
296
+ exports.fileContentSchema = exports.fileContentBaseSchema.extend({
297
+ type: zod_1.z.literal("file"),
298
+ data: zod_1.z.string(),
299
+ });
300
+ exports.localContentSchema = exports.fileContentBaseSchema.extend({
301
+ type: zod_1.z.literal("local"),
302
+ path: zod_1.z.string(),
303
+ });
304
+ exports.fileUnionContentSchema = zod_1.z.discriminatedUnion("type", [
305
+ exports.localContentSchema,
306
+ exports.urlContentSchema,
307
+ exports.fileContentSchema,
308
+ ]);
309
+ exports.fileUnionContentsSchema = zod_1.z.union([
310
+ exports.fileUnionContentSchema,
311
+ zod_1.z.array(exports.fileUnionContentSchema),
312
+ ]);
313
+ exports.unionContentSchema = zod_1.z.discriminatedUnion("type", [
314
+ exports.textContentSchema,
315
+ exports.localContentSchema,
316
+ exports.urlContentSchema,
317
+ exports.fileContentSchema,
318
+ ]);
212
319
  const chatModelInputMessageSchema = zod_1.z.object({
213
320
  role: zod_1.z.union([zod_1.z.literal("system"), zod_1.z.literal("user"), zod_1.z.literal("agent"), zod_1.z.literal("tool")]),
214
- content: zod_1.z
215
- .union([
216
- zod_1.z.string(),
217
- zod_1.z.array(zod_1.z.union([
218
- zod_1.z.object({ type: zod_1.z.literal("text"), text: zod_1.z.string() }),
219
- zod_1.z.object({ type: zod_1.z.literal("image_url"), url: zod_1.z.string() }),
220
- ])),
221
- ])
222
- .optional(),
321
+ content: zod_1.z.union([zod_1.z.string(), zod_1.z.array(exports.unionContentSchema)]).optional(),
223
322
  toolCalls: zod_1.z
224
323
  .array(zod_1.z.object({
225
324
  id: zod_1.z.string(),
@@ -266,6 +365,7 @@ const chatModelOptionsSchema = zod_1.z.object({
266
365
  frequencyPenalty: zod_1.z.number().optional(),
267
366
  presencePenalty: zod_1.z.number().optional(),
268
367
  parallelToolCalls: zod_1.z.boolean().optional().default(true),
368
+ modalities: zod_1.z.array(zod_1.z.enum(["text", "image", "audio"])).optional(),
269
369
  });
270
370
  const chatModelInputSchema = zod_1.z.object({
271
371
  messages: zod_1.z.array(chatModelInputMessageSchema),
@@ -274,6 +374,11 @@ const chatModelInputSchema = zod_1.z.object({
274
374
  toolChoice: chatModelInputToolChoiceSchema.optional(),
275
375
  modelOptions: chatModelOptionsSchema.optional(),
276
376
  });
377
+ var FileOutputType;
378
+ (function (FileOutputType) {
379
+ FileOutputType["local"] = "local";
380
+ FileOutputType["file"] = "file";
381
+ })(FileOutputType || (exports.FileOutputType = FileOutputType = {}));
277
382
  const chatModelOutputToolCallSchema = zod_1.z.object({
278
383
  id: zod_1.z.string(),
279
384
  type: zod_1.z.literal("function"),
@@ -293,4 +398,5 @@ const chatModelOutputSchema = zod_1.z.object({
293
398
  toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
294
399
  usage: exports.chatModelOutputUsageSchema.optional(),
295
400
  model: zod_1.z.string().optional(),
401
+ files: zod_1.z.array(exports.fileUnionContentSchema).optional(),
296
402
  });