@aigne/core 1.58.1 → 1.58.3

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,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.58.3](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.58.2...core-v1.58.3) (2025-09-08)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * handle absolute paths in agent YAML prompt URLs ([#466](https://github.com/AIGNE-io/aigne-framework/issues/466)) ([a07a088](https://github.com/AIGNE-io/aigne-framework/commit/a07a0880728f65fc831578763b62ce5144d1aed8))
9
+ * support optional field sturectured output for gemini ([#468](https://github.com/AIGNE-io/aigne-framework/issues/468)) ([70c6279](https://github.com/AIGNE-io/aigne-framework/commit/70c62795039a2862e3333f26707329489bf938de))
10
+
11
+ ## [1.58.2](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.58.1...core-v1.58.2) (2025-09-05)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * **model:** transform local file to base64 before request llm ([#462](https://github.com/AIGNE-io/aigne-framework/issues/462)) ([58ef5d7](https://github.com/AIGNE-io/aigne-framework/commit/58ef5d77046c49f3c4eed15b7f0cc283cbbcd74a))
17
+
3
18
  ## [1.58.1](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.58.0...core-v1.58.1) (2025-09-05)
4
19
 
5
20
 
@@ -1,6 +1,6 @@
1
1
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import type * as prompts from "@inquirer/prompts";
3
- import { ZodObject, type ZodType } from "zod";
3
+ import { type ZodObject, type ZodType } from "zod";
4
4
  import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
5
5
  import type { MessagePayload } from "../aigne/message-queue.js";
6
6
  import type { ContextUsage } from "../aigne/usage.js";
@@ -51,6 +51,7 @@ const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
51
51
  const nunjucks_1 = __importDefault(require("nunjucks"));
52
52
  const zod_1 = require("zod");
53
53
  const agent_utils_js_1 = require("../utils/agent-utils.js");
54
+ const json_schema_js_1 = require("../utils/json-schema.js");
54
55
  const logger_js_1 = require("../utils/logger.js");
55
56
  const stream_utils_js_1 = require("../utils/stream-utils.js");
56
57
  const type_utils_js_1 = require("../utils/type-utils.js");
@@ -748,7 +749,7 @@ async function agentProcessResultToObject(response) {
748
749
  : response;
749
750
  }
750
751
  function checkAgentInputOutputSchema(schema) {
751
- if (!(schema instanceof zod_1.ZodObject) && typeof schema !== "function") {
752
+ if (typeof schema !== "function" && !(0, json_schema_js_1.isZodSchema)(schema)) {
752
753
  throw new Error(`schema must be a zod object or function return a zod object, got: ${typeof schema}`);
753
754
  }
754
755
  }
@@ -224,11 +224,11 @@ export declare const fileContentBaseSchema: z.ZodObject<{
224
224
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
225
225
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
226
226
  }, "strip", z.ZodTypeAny, {
227
- filename?: string | undefined;
228
227
  mimeType?: string | undefined;
229
- }, {
230
228
  filename?: string | undefined;
229
+ }, {
231
230
  mimeType?: string | undefined;
231
+ filename?: string | undefined;
232
232
  }>;
233
233
  /**
234
234
  * Image URL content type
@@ -248,13 +248,13 @@ export declare const urlContentSchema: z.ZodObject<{
248
248
  }, "strip", z.ZodTypeAny, {
249
249
  type: "url";
250
250
  url: string;
251
- filename?: string | undefined;
252
251
  mimeType?: string | undefined;
252
+ filename?: string | undefined;
253
253
  }, {
254
254
  type: "url";
255
255
  url: string;
256
- filename?: string | undefined;
257
256
  mimeType?: string | undefined;
257
+ filename?: string | undefined;
258
258
  }>;
259
259
  export interface FileContent extends FileContentBase {
260
260
  type: "file";
@@ -269,13 +269,13 @@ export declare const fileContentSchema: z.ZodObject<{
269
269
  }, "strip", z.ZodTypeAny, {
270
270
  type: "file";
271
271
  data: string;
272
- filename?: string | undefined;
273
272
  mimeType?: string | undefined;
273
+ filename?: string | undefined;
274
274
  }, {
275
275
  type: "file";
276
276
  data: string;
277
- filename?: string | undefined;
278
277
  mimeType?: string | undefined;
278
+ filename?: string | undefined;
279
279
  }>;
280
280
  export interface LocalContent extends FileContentBase {
281
281
  type: "local";
@@ -290,13 +290,13 @@ export declare const localContentSchema: z.ZodObject<{
290
290
  }, "strip", z.ZodTypeAny, {
291
291
  path: string;
292
292
  type: "local";
293
- filename?: string | undefined;
294
293
  mimeType?: string | undefined;
294
+ filename?: string | undefined;
295
295
  }, {
296
296
  path: string;
297
297
  type: "local";
298
- filename?: string | undefined;
299
298
  mimeType?: string | undefined;
299
+ filename?: string | undefined;
300
300
  }>;
301
301
  export type FileUnionContent = LocalContent | UrlContent | FileContent;
302
302
  export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -308,13 +308,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
308
308
  }, "strip", z.ZodTypeAny, {
309
309
  path: string;
310
310
  type: "local";
311
- filename?: string | undefined;
312
311
  mimeType?: string | undefined;
312
+ filename?: string | undefined;
313
313
  }, {
314
314
  path: string;
315
315
  type: "local";
316
- filename?: string | undefined;
317
316
  mimeType?: string | undefined;
317
+ filename?: string | undefined;
318
318
  }>, z.ZodObject<{
319
319
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
320
320
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -324,13 +324,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
324
324
  }, "strip", z.ZodTypeAny, {
325
325
  type: "url";
326
326
  url: string;
327
- filename?: string | undefined;
328
327
  mimeType?: string | undefined;
328
+ filename?: string | undefined;
329
329
  }, {
330
330
  type: "url";
331
331
  url: string;
332
- filename?: string | undefined;
333
332
  mimeType?: string | undefined;
333
+ filename?: string | undefined;
334
334
  }>, z.ZodObject<{
335
335
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
336
336
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -340,13 +340,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
340
340
  }, "strip", z.ZodTypeAny, {
341
341
  type: "file";
342
342
  data: string;
343
- filename?: string | undefined;
344
343
  mimeType?: string | undefined;
344
+ filename?: string | undefined;
345
345
  }, {
346
346
  type: "file";
347
347
  data: string;
348
- filename?: string | undefined;
349
348
  mimeType?: string | undefined;
349
+ filename?: string | undefined;
350
350
  }>]>;
351
351
  export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
352
352
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -357,13 +357,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
357
357
  }, "strip", z.ZodTypeAny, {
358
358
  path: string;
359
359
  type: "local";
360
- filename?: string | undefined;
361
360
  mimeType?: string | undefined;
361
+ filename?: string | undefined;
362
362
  }, {
363
363
  path: string;
364
364
  type: "local";
365
- filename?: string | undefined;
366
365
  mimeType?: string | undefined;
366
+ filename?: string | undefined;
367
367
  }>, z.ZodObject<{
368
368
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
369
369
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -373,13 +373,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
373
373
  }, "strip", z.ZodTypeAny, {
374
374
  type: "url";
375
375
  url: string;
376
- filename?: string | undefined;
377
376
  mimeType?: string | undefined;
377
+ filename?: string | undefined;
378
378
  }, {
379
379
  type: "url";
380
380
  url: string;
381
- filename?: string | undefined;
382
381
  mimeType?: string | undefined;
382
+ filename?: string | undefined;
383
383
  }>, z.ZodObject<{
384
384
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
385
385
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -389,13 +389,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
389
389
  }, "strip", z.ZodTypeAny, {
390
390
  type: "file";
391
391
  data: string;
392
- filename?: string | undefined;
393
392
  mimeType?: string | undefined;
393
+ filename?: string | undefined;
394
394
  }, {
395
395
  type: "file";
396
396
  data: string;
397
- filename?: string | undefined;
398
397
  mimeType?: string | undefined;
398
+ filename?: string | undefined;
399
399
  }>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
400
400
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
401
401
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -405,13 +405,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
405
405
  }, "strip", z.ZodTypeAny, {
406
406
  path: string;
407
407
  type: "local";
408
- filename?: string | undefined;
409
408
  mimeType?: string | undefined;
409
+ filename?: string | undefined;
410
410
  }, {
411
411
  path: string;
412
412
  type: "local";
413
- filename?: string | undefined;
414
413
  mimeType?: string | undefined;
414
+ filename?: string | undefined;
415
415
  }>, z.ZodObject<{
416
416
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
417
417
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -421,13 +421,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
421
421
  }, "strip", z.ZodTypeAny, {
422
422
  type: "url";
423
423
  url: string;
424
- filename?: string | undefined;
425
424
  mimeType?: string | undefined;
425
+ filename?: string | undefined;
426
426
  }, {
427
427
  type: "url";
428
428
  url: string;
429
- filename?: string | undefined;
430
429
  mimeType?: string | undefined;
430
+ filename?: string | undefined;
431
431
  }>, z.ZodObject<{
432
432
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
433
433
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -437,13 +437,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
437
437
  }, "strip", z.ZodTypeAny, {
438
438
  type: "file";
439
439
  data: string;
440
- filename?: string | undefined;
441
440
  mimeType?: string | undefined;
441
+ filename?: string | undefined;
442
442
  }, {
443
443
  type: "file";
444
444
  data: string;
445
- filename?: string | undefined;
446
445
  mimeType?: string | undefined;
446
+ filename?: string | undefined;
447
447
  }>]>, "many">]>;
448
448
  export type UnionContent = TextContent | FileUnionContent;
449
449
  export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -464,13 +464,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
464
464
  }, "strip", z.ZodTypeAny, {
465
465
  path: string;
466
466
  type: "local";
467
- filename?: string | undefined;
468
467
  mimeType?: string | undefined;
468
+ filename?: string | undefined;
469
469
  }, {
470
470
  path: string;
471
471
  type: "local";
472
- filename?: string | undefined;
473
472
  mimeType?: string | undefined;
473
+ filename?: string | undefined;
474
474
  }>, z.ZodObject<{
475
475
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
476
476
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -480,13 +480,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
480
480
  }, "strip", z.ZodTypeAny, {
481
481
  type: "url";
482
482
  url: string;
483
- filename?: string | undefined;
484
483
  mimeType?: string | undefined;
484
+ filename?: string | undefined;
485
485
  }, {
486
486
  type: "url";
487
487
  url: string;
488
- filename?: string | undefined;
489
488
  mimeType?: string | undefined;
489
+ filename?: string | undefined;
490
490
  }>, z.ZodObject<{
491
491
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
492
492
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -496,13 +496,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
496
496
  }, "strip", z.ZodTypeAny, {
497
497
  type: "file";
498
498
  data: string;
499
- filename?: string | undefined;
500
499
  mimeType?: string | undefined;
500
+ filename?: string | undefined;
501
501
  }, {
502
502
  type: "file";
503
503
  data: string;
504
- filename?: string | undefined;
505
504
  mimeType?: string | undefined;
505
+ filename?: string | undefined;
506
506
  }>]>;
507
507
  /**
508
508
  * Model response format settings
@@ -173,6 +173,25 @@ class ChatModel extends agent_js_1.Agent {
173
173
  enumerable: false,
174
174
  });
175
175
  }
176
+ input.messages = await Promise.all(input.messages.map(async (message) => {
177
+ if (!Array.isArray(message.content))
178
+ return message;
179
+ return {
180
+ ...message,
181
+ content: await Promise.all(message.content.map(async (item) => {
182
+ if (item.type === "local") {
183
+ return {
184
+ ...item,
185
+ type: "file",
186
+ data: await index_js_1.nodejs.fs.readFile(item.path, "base64"),
187
+ path: undefined,
188
+ mimeType: item.mimeType || ChatModel.getMimeType(item.filename || item.path),
189
+ };
190
+ }
191
+ return item;
192
+ })),
193
+ };
194
+ }));
176
195
  }
177
196
  /**
178
197
  * Performs postprocessing operations after handling output
@@ -197,14 +216,6 @@ class ChatModel extends agent_js_1.Agent {
197
216
  }
198
217
  }
199
218
  }
200
- if (input.responseFormat?.type === "json_schema" &&
201
- // NOTE: Should not validate if there are tool calls
202
- !output.toolCalls?.length) {
203
- const ajv = new ajv_1.Ajv();
204
- if (!ajv.validate(input.responseFormat.jsonSchema.schema, output.json)) {
205
- throw new StructuredOutputError(`Output JSON does not conform to the provided JSON schema: ${ajv.errorsText()}`);
206
- }
207
- }
208
219
  super.postprocess(input, output, options);
209
220
  const { usage } = output;
210
221
  if (usage) {
@@ -222,6 +233,16 @@ class ChatModel extends agent_js_1.Agent {
222
233
  files: await Promise.all(files.map((file) => this.transformFileOutput(input, file, options))),
223
234
  };
224
235
  }
236
+ // Remove fields with `null` value for validation
237
+ output = (0, type_utils_js_1.omitByDeep)(output, (value) => (0, type_utils_js_1.isNil)(value));
238
+ if (input.responseFormat?.type === "json_schema" &&
239
+ // NOTE: Should not validate if there are tool calls
240
+ !output.toolCalls?.length) {
241
+ const ajv = new ajv_1.Ajv();
242
+ if (!ajv.validate(input.responseFormat.jsonSchema.schema, output.json)) {
243
+ throw new StructuredOutputError(`Output JSON does not conform to the provided JSON schema: ${ajv.errorsText()}`);
244
+ }
245
+ }
225
246
  return super.processAgentOutput(input, output, options);
226
247
  }
227
248
  async transformFileOutput(input, data, options) {
@@ -59,7 +59,9 @@ async function parseAgentFile(path, data) {
59
59
  ])
60
60
  .transform((v) => typeof v === "string"
61
61
  ? { content: v, path }
62
- : Promise.resolve(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), v.url)).then((path) => index_js_1.nodejs.fs.readFile(path, "utf8").then((content) => ({ content, path }))));
62
+ : Promise.resolve(index_js_1.nodejs.path.isAbsolute(v.url)
63
+ ? v.url
64
+ : index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), v.url)).then((path) => index_js_1.nodejs.fs.readFile(path, "utf8").then((content) => ({ content, path }))));
63
65
  return (0, schema_js_1.camelizeSchema)(zod_1.z.discriminatedUnion("type", [
64
66
  zod_1.z
65
67
  .object({
@@ -68,6 +70,7 @@ async function parseAgentFile(path, data) {
68
70
  inputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
69
71
  outputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
70
72
  toolChoice: (0, schema_js_1.optionalize)(zod_1.z.nativeEnum(ai_agent_js_1.AIAgentToolChoice)),
73
+ structuredStreamMode: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
71
74
  })
72
75
  .extend(baseAgentSchema.shape),
73
76
  zod_1.z
@@ -10,3 +10,4 @@ export declare function parseJSON(json: string): any;
10
10
  * @returns The union array with at least 1 item (but the type is at least 2 items for z.union)
11
11
  */
12
12
  export declare function ensureZodUnionArray<T extends z.ZodType>(union: T[]): [T, T, ...T[]];
13
+ export declare function isZodSchema(schema: ZodType): schema is ZodType;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.outputSchemaToResponseFormatSchema = outputSchemaToResponseFormatSchema;
4
4
  exports.parseJSON = parseJSON;
5
5
  exports.ensureZodUnionArray = ensureZodUnionArray;
6
+ exports.isZodSchema = isZodSchema;
6
7
  const zod_to_json_schema_1 = require("zod-to-json-schema");
7
8
  const logger_js_1 = require("./logger.js");
8
9
  function outputSchemaToResponseFormatSchema(agentOutput) {
@@ -45,3 +46,8 @@ function ensureZodUnionArray(union) {
45
46
  }
46
47
  return union;
47
48
  }
49
+ function isZodSchema(schema) {
50
+ if (!schema || typeof schema !== "object")
51
+ return false;
52
+ return typeof schema.parse === "function" && typeof schema.safeParse === "function";
53
+ }
@@ -19,6 +19,7 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, ...key
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
21
  export declare function omitBy<T extends object, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
+ export declare function omitByDeep(obj: any, predicate: (value: any, key: any) => boolean): any;
22
23
  export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
24
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
25
  [key: string]: T;
@@ -12,6 +12,7 @@ exports.pick = pick;
12
12
  exports.omit = omit;
13
13
  exports.omitDeep = omitDeep;
14
14
  exports.omitBy = omitBy;
15
+ exports.omitByDeep = omitByDeep;
15
16
  exports.flat = flat;
16
17
  exports.createAccessorArray = createAccessorArray;
17
18
  exports.checkArguments = checkArguments;
@@ -102,6 +103,24 @@ function omitBy(obj, predicate) {
102
103
  return !predicate(value, k);
103
104
  }));
104
105
  }
106
+ function omitByDeep(obj, predicate) {
107
+ if (obj === null || obj === undefined)
108
+ return obj;
109
+ if (Array.isArray(obj)) {
110
+ return obj.map((item) => omitByDeep(item, predicate));
111
+ }
112
+ if (typeof obj === "object") {
113
+ const result = {};
114
+ for (const [key, value] of Object.entries(obj)) {
115
+ const newValue = omitByDeep(value, predicate);
116
+ if (!predicate(newValue, key)) {
117
+ result[key] = newValue;
118
+ }
119
+ }
120
+ return result;
121
+ }
122
+ return obj;
123
+ }
105
124
  function flat(...value) {
106
125
  return value.flat().filter(isNonNullable);
107
126
  }
@@ -1,6 +1,6 @@
1
1
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import type * as prompts from "@inquirer/prompts";
3
- import { ZodObject, type ZodType } from "zod";
3
+ import { type ZodObject, type ZodType } from "zod";
4
4
  import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
5
5
  import type { MessagePayload } from "../aigne/message-queue.js";
6
6
  import type { ContextUsage } from "../aigne/usage.js";
@@ -224,11 +224,11 @@ export declare const fileContentBaseSchema: z.ZodObject<{
224
224
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
225
225
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
226
226
  }, "strip", z.ZodTypeAny, {
227
- filename?: string | undefined;
228
227
  mimeType?: string | undefined;
229
- }, {
230
228
  filename?: string | undefined;
229
+ }, {
231
230
  mimeType?: string | undefined;
231
+ filename?: string | undefined;
232
232
  }>;
233
233
  /**
234
234
  * Image URL content type
@@ -248,13 +248,13 @@ export declare const urlContentSchema: z.ZodObject<{
248
248
  }, "strip", z.ZodTypeAny, {
249
249
  type: "url";
250
250
  url: string;
251
- filename?: string | undefined;
252
251
  mimeType?: string | undefined;
252
+ filename?: string | undefined;
253
253
  }, {
254
254
  type: "url";
255
255
  url: string;
256
- filename?: string | undefined;
257
256
  mimeType?: string | undefined;
257
+ filename?: string | undefined;
258
258
  }>;
259
259
  export interface FileContent extends FileContentBase {
260
260
  type: "file";
@@ -269,13 +269,13 @@ export declare const fileContentSchema: z.ZodObject<{
269
269
  }, "strip", z.ZodTypeAny, {
270
270
  type: "file";
271
271
  data: string;
272
- filename?: string | undefined;
273
272
  mimeType?: string | undefined;
273
+ filename?: string | undefined;
274
274
  }, {
275
275
  type: "file";
276
276
  data: string;
277
- filename?: string | undefined;
278
277
  mimeType?: string | undefined;
278
+ filename?: string | undefined;
279
279
  }>;
280
280
  export interface LocalContent extends FileContentBase {
281
281
  type: "local";
@@ -290,13 +290,13 @@ export declare const localContentSchema: z.ZodObject<{
290
290
  }, "strip", z.ZodTypeAny, {
291
291
  path: string;
292
292
  type: "local";
293
- filename?: string | undefined;
294
293
  mimeType?: string | undefined;
294
+ filename?: string | undefined;
295
295
  }, {
296
296
  path: string;
297
297
  type: "local";
298
- filename?: string | undefined;
299
298
  mimeType?: string | undefined;
299
+ filename?: string | undefined;
300
300
  }>;
301
301
  export type FileUnionContent = LocalContent | UrlContent | FileContent;
302
302
  export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -308,13 +308,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
308
308
  }, "strip", z.ZodTypeAny, {
309
309
  path: string;
310
310
  type: "local";
311
- filename?: string | undefined;
312
311
  mimeType?: string | undefined;
312
+ filename?: string | undefined;
313
313
  }, {
314
314
  path: string;
315
315
  type: "local";
316
- filename?: string | undefined;
317
316
  mimeType?: string | undefined;
317
+ filename?: string | undefined;
318
318
  }>, z.ZodObject<{
319
319
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
320
320
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -324,13 +324,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
324
324
  }, "strip", z.ZodTypeAny, {
325
325
  type: "url";
326
326
  url: string;
327
- filename?: string | undefined;
328
327
  mimeType?: string | undefined;
328
+ filename?: string | undefined;
329
329
  }, {
330
330
  type: "url";
331
331
  url: string;
332
- filename?: string | undefined;
333
332
  mimeType?: string | undefined;
333
+ filename?: string | undefined;
334
334
  }>, z.ZodObject<{
335
335
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
336
336
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -340,13 +340,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
340
340
  }, "strip", z.ZodTypeAny, {
341
341
  type: "file";
342
342
  data: string;
343
- filename?: string | undefined;
344
343
  mimeType?: string | undefined;
344
+ filename?: string | undefined;
345
345
  }, {
346
346
  type: "file";
347
347
  data: string;
348
- filename?: string | undefined;
349
348
  mimeType?: string | undefined;
349
+ filename?: string | undefined;
350
350
  }>]>;
351
351
  export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
352
352
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -357,13 +357,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
357
357
  }, "strip", z.ZodTypeAny, {
358
358
  path: string;
359
359
  type: "local";
360
- filename?: string | undefined;
361
360
  mimeType?: string | undefined;
361
+ filename?: string | undefined;
362
362
  }, {
363
363
  path: string;
364
364
  type: "local";
365
- filename?: string | undefined;
366
365
  mimeType?: string | undefined;
366
+ filename?: string | undefined;
367
367
  }>, z.ZodObject<{
368
368
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
369
369
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -373,13 +373,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
373
373
  }, "strip", z.ZodTypeAny, {
374
374
  type: "url";
375
375
  url: string;
376
- filename?: string | undefined;
377
376
  mimeType?: string | undefined;
377
+ filename?: string | undefined;
378
378
  }, {
379
379
  type: "url";
380
380
  url: string;
381
- filename?: string | undefined;
382
381
  mimeType?: string | undefined;
382
+ filename?: string | undefined;
383
383
  }>, z.ZodObject<{
384
384
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
385
385
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -389,13 +389,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
389
389
  }, "strip", z.ZodTypeAny, {
390
390
  type: "file";
391
391
  data: string;
392
- filename?: string | undefined;
393
392
  mimeType?: string | undefined;
393
+ filename?: string | undefined;
394
394
  }, {
395
395
  type: "file";
396
396
  data: string;
397
- filename?: string | undefined;
398
397
  mimeType?: string | undefined;
398
+ filename?: string | undefined;
399
399
  }>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
400
400
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
401
401
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -405,13 +405,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
405
405
  }, "strip", z.ZodTypeAny, {
406
406
  path: string;
407
407
  type: "local";
408
- filename?: string | undefined;
409
408
  mimeType?: string | undefined;
409
+ filename?: string | undefined;
410
410
  }, {
411
411
  path: string;
412
412
  type: "local";
413
- filename?: string | undefined;
414
413
  mimeType?: string | undefined;
414
+ filename?: string | undefined;
415
415
  }>, z.ZodObject<{
416
416
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
417
417
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -421,13 +421,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
421
421
  }, "strip", z.ZodTypeAny, {
422
422
  type: "url";
423
423
  url: string;
424
- filename?: string | undefined;
425
424
  mimeType?: string | undefined;
425
+ filename?: string | undefined;
426
426
  }, {
427
427
  type: "url";
428
428
  url: string;
429
- filename?: string | undefined;
430
429
  mimeType?: string | undefined;
430
+ filename?: string | undefined;
431
431
  }>, z.ZodObject<{
432
432
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
433
433
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -437,13 +437,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
437
437
  }, "strip", z.ZodTypeAny, {
438
438
  type: "file";
439
439
  data: string;
440
- filename?: string | undefined;
441
440
  mimeType?: string | undefined;
441
+ filename?: string | undefined;
442
442
  }, {
443
443
  type: "file";
444
444
  data: string;
445
- filename?: string | undefined;
446
445
  mimeType?: string | undefined;
446
+ filename?: string | undefined;
447
447
  }>]>, "many">]>;
448
448
  export type UnionContent = TextContent | FileUnionContent;
449
449
  export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -464,13 +464,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
464
464
  }, "strip", z.ZodTypeAny, {
465
465
  path: string;
466
466
  type: "local";
467
- filename?: string | undefined;
468
467
  mimeType?: string | undefined;
468
+ filename?: string | undefined;
469
469
  }, {
470
470
  path: string;
471
471
  type: "local";
472
- filename?: string | undefined;
473
472
  mimeType?: string | undefined;
473
+ filename?: string | undefined;
474
474
  }>, z.ZodObject<{
475
475
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
476
476
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -480,13 +480,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
480
480
  }, "strip", z.ZodTypeAny, {
481
481
  type: "url";
482
482
  url: string;
483
- filename?: string | undefined;
484
483
  mimeType?: string | undefined;
484
+ filename?: string | undefined;
485
485
  }, {
486
486
  type: "url";
487
487
  url: string;
488
- filename?: string | undefined;
489
488
  mimeType?: string | undefined;
489
+ filename?: string | undefined;
490
490
  }>, z.ZodObject<{
491
491
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
492
492
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -496,13 +496,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
496
496
  }, "strip", z.ZodTypeAny, {
497
497
  type: "file";
498
498
  data: string;
499
- filename?: string | undefined;
500
499
  mimeType?: string | undefined;
500
+ filename?: string | undefined;
501
501
  }, {
502
502
  type: "file";
503
503
  data: string;
504
- filename?: string | undefined;
505
504
  mimeType?: string | undefined;
505
+ filename?: string | undefined;
506
506
  }>]>;
507
507
  /**
508
508
  * Model response format settings
@@ -10,3 +10,4 @@ export declare function parseJSON(json: string): any;
10
10
  * @returns The union array with at least 1 item (but the type is at least 2 items for z.union)
11
11
  */
12
12
  export declare function ensureZodUnionArray<T extends z.ZodType>(union: T[]): [T, T, ...T[]];
13
+ export declare function isZodSchema(schema: ZodType): schema is ZodType;
@@ -19,6 +19,7 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, ...key
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
21
  export declare function omitBy<T extends object, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
+ export declare function omitByDeep(obj: any, predicate: (value: any, key: any) => boolean): any;
22
23
  export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
24
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
25
  [key: string]: T;
@@ -1,6 +1,6 @@
1
1
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import type * as prompts from "@inquirer/prompts";
3
- import { ZodObject, type ZodType } from "zod";
3
+ import { type ZodObject, type ZodType } from "zod";
4
4
  import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
5
5
  import type { MessagePayload } from "../aigne/message-queue.js";
6
6
  import type { ContextUsage } from "../aigne/usage.js";
@@ -1,8 +1,9 @@
1
1
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import equal from "fast-deep-equal";
3
3
  import nunjucks from "nunjucks";
4
- import { ZodObject, z } from "zod";
4
+ import { z } from "zod";
5
5
  import { sortHooks } from "../utils/agent-utils.js";
6
+ import { isZodSchema } from "../utils/json-schema.js";
6
7
  import { logger } from "../utils/logger.js";
7
8
  import { agentResponseStreamToObject, asyncGeneratorToReadableStream, isAsyncGenerator, mergeAgentResponseChunk, objectToAgentResponseStream, onAgentResponseStreamEnd, } from "../utils/stream-utils.js";
8
9
  import { checkArguments, createAccessorArray, flat, isEmpty, isNil, isNonNullable, isRecord, } from "../utils/type-utils.js";
@@ -699,7 +700,7 @@ export async function agentProcessResultToObject(response) {
699
700
  : response;
700
701
  }
701
702
  function checkAgentInputOutputSchema(schema) {
702
- if (!(schema instanceof ZodObject) && typeof schema !== "function") {
703
+ if (typeof schema !== "function" && !isZodSchema(schema)) {
703
704
  throw new Error(`schema must be a zod object or function return a zod object, got: ${typeof schema}`);
704
705
  }
705
706
  }
@@ -224,11 +224,11 @@ export declare const fileContentBaseSchema: z.ZodObject<{
224
224
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
225
225
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
226
226
  }, "strip", z.ZodTypeAny, {
227
- filename?: string | undefined;
228
227
  mimeType?: string | undefined;
229
- }, {
230
228
  filename?: string | undefined;
229
+ }, {
231
230
  mimeType?: string | undefined;
231
+ filename?: string | undefined;
232
232
  }>;
233
233
  /**
234
234
  * Image URL content type
@@ -248,13 +248,13 @@ export declare const urlContentSchema: z.ZodObject<{
248
248
  }, "strip", z.ZodTypeAny, {
249
249
  type: "url";
250
250
  url: string;
251
- filename?: string | undefined;
252
251
  mimeType?: string | undefined;
252
+ filename?: string | undefined;
253
253
  }, {
254
254
  type: "url";
255
255
  url: string;
256
- filename?: string | undefined;
257
256
  mimeType?: string | undefined;
257
+ filename?: string | undefined;
258
258
  }>;
259
259
  export interface FileContent extends FileContentBase {
260
260
  type: "file";
@@ -269,13 +269,13 @@ export declare const fileContentSchema: z.ZodObject<{
269
269
  }, "strip", z.ZodTypeAny, {
270
270
  type: "file";
271
271
  data: string;
272
- filename?: string | undefined;
273
272
  mimeType?: string | undefined;
273
+ filename?: string | undefined;
274
274
  }, {
275
275
  type: "file";
276
276
  data: string;
277
- filename?: string | undefined;
278
277
  mimeType?: string | undefined;
278
+ filename?: string | undefined;
279
279
  }>;
280
280
  export interface LocalContent extends FileContentBase {
281
281
  type: "local";
@@ -290,13 +290,13 @@ export declare const localContentSchema: z.ZodObject<{
290
290
  }, "strip", z.ZodTypeAny, {
291
291
  path: string;
292
292
  type: "local";
293
- filename?: string | undefined;
294
293
  mimeType?: string | undefined;
294
+ filename?: string | undefined;
295
295
  }, {
296
296
  path: string;
297
297
  type: "local";
298
- filename?: string | undefined;
299
298
  mimeType?: string | undefined;
299
+ filename?: string | undefined;
300
300
  }>;
301
301
  export type FileUnionContent = LocalContent | UrlContent | FileContent;
302
302
  export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -308,13 +308,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
308
308
  }, "strip", z.ZodTypeAny, {
309
309
  path: string;
310
310
  type: "local";
311
- filename?: string | undefined;
312
311
  mimeType?: string | undefined;
312
+ filename?: string | undefined;
313
313
  }, {
314
314
  path: string;
315
315
  type: "local";
316
- filename?: string | undefined;
317
316
  mimeType?: string | undefined;
317
+ filename?: string | undefined;
318
318
  }>, z.ZodObject<{
319
319
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
320
320
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -324,13 +324,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
324
324
  }, "strip", z.ZodTypeAny, {
325
325
  type: "url";
326
326
  url: string;
327
- filename?: string | undefined;
328
327
  mimeType?: string | undefined;
328
+ filename?: string | undefined;
329
329
  }, {
330
330
  type: "url";
331
331
  url: string;
332
- filename?: string | undefined;
333
332
  mimeType?: string | undefined;
333
+ filename?: string | undefined;
334
334
  }>, z.ZodObject<{
335
335
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
336
336
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -340,13 +340,13 @@ export declare const fileUnionContentSchema: z.ZodDiscriminatedUnion<"type", [z.
340
340
  }, "strip", z.ZodTypeAny, {
341
341
  type: "file";
342
342
  data: string;
343
- filename?: string | undefined;
344
343
  mimeType?: string | undefined;
344
+ filename?: string | undefined;
345
345
  }, {
346
346
  type: "file";
347
347
  data: string;
348
- filename?: string | undefined;
349
348
  mimeType?: string | undefined;
349
+ filename?: string | undefined;
350
350
  }>]>;
351
351
  export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
352
352
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -357,13 +357,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
357
357
  }, "strip", z.ZodTypeAny, {
358
358
  path: string;
359
359
  type: "local";
360
- filename?: string | undefined;
361
360
  mimeType?: string | undefined;
361
+ filename?: string | undefined;
362
362
  }, {
363
363
  path: string;
364
364
  type: "local";
365
- filename?: string | undefined;
366
365
  mimeType?: string | undefined;
366
+ filename?: string | undefined;
367
367
  }>, z.ZodObject<{
368
368
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
369
369
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -373,13 +373,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
373
373
  }, "strip", z.ZodTypeAny, {
374
374
  type: "url";
375
375
  url: string;
376
- filename?: string | undefined;
377
376
  mimeType?: string | undefined;
377
+ filename?: string | undefined;
378
378
  }, {
379
379
  type: "url";
380
380
  url: string;
381
- filename?: string | undefined;
382
381
  mimeType?: string | undefined;
382
+ filename?: string | undefined;
383
383
  }>, z.ZodObject<{
384
384
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
385
385
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -389,13 +389,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
389
389
  }, "strip", z.ZodTypeAny, {
390
390
  type: "file";
391
391
  data: string;
392
- filename?: string | undefined;
393
392
  mimeType?: string | undefined;
393
+ filename?: string | undefined;
394
394
  }, {
395
395
  type: "file";
396
396
  data: string;
397
- filename?: string | undefined;
398
397
  mimeType?: string | undefined;
398
+ filename?: string | undefined;
399
399
  }>]>, z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
400
400
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
401
401
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -405,13 +405,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
405
405
  }, "strip", z.ZodTypeAny, {
406
406
  path: string;
407
407
  type: "local";
408
- filename?: string | undefined;
409
408
  mimeType?: string | undefined;
409
+ filename?: string | undefined;
410
410
  }, {
411
411
  path: string;
412
412
  type: "local";
413
- filename?: string | undefined;
414
413
  mimeType?: string | undefined;
414
+ filename?: string | undefined;
415
415
  }>, z.ZodObject<{
416
416
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
417
417
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -421,13 +421,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
421
421
  }, "strip", z.ZodTypeAny, {
422
422
  type: "url";
423
423
  url: string;
424
- filename?: string | undefined;
425
424
  mimeType?: string | undefined;
425
+ filename?: string | undefined;
426
426
  }, {
427
427
  type: "url";
428
428
  url: string;
429
- filename?: string | undefined;
430
429
  mimeType?: string | undefined;
430
+ filename?: string | undefined;
431
431
  }>, z.ZodObject<{
432
432
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
433
433
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -437,13 +437,13 @@ export declare const fileUnionContentsSchema: z.ZodUnion<[z.ZodDiscriminatedUnio
437
437
  }, "strip", z.ZodTypeAny, {
438
438
  type: "file";
439
439
  data: string;
440
- filename?: string | undefined;
441
440
  mimeType?: string | undefined;
441
+ filename?: string | undefined;
442
442
  }, {
443
443
  type: "file";
444
444
  data: string;
445
- filename?: string | undefined;
446
445
  mimeType?: string | undefined;
446
+ filename?: string | undefined;
447
447
  }>]>, "many">]>;
448
448
  export type UnionContent = TextContent | FileUnionContent;
449
449
  export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
@@ -464,13 +464,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
464
464
  }, "strip", z.ZodTypeAny, {
465
465
  path: string;
466
466
  type: "local";
467
- filename?: string | undefined;
468
467
  mimeType?: string | undefined;
468
+ filename?: string | undefined;
469
469
  }, {
470
470
  path: string;
471
471
  type: "local";
472
- filename?: string | undefined;
473
472
  mimeType?: string | undefined;
473
+ filename?: string | undefined;
474
474
  }>, z.ZodObject<{
475
475
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
476
476
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -480,13 +480,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
480
480
  }, "strip", z.ZodTypeAny, {
481
481
  type: "url";
482
482
  url: string;
483
- filename?: string | undefined;
484
483
  mimeType?: string | undefined;
484
+ filename?: string | undefined;
485
485
  }, {
486
486
  type: "url";
487
487
  url: string;
488
- filename?: string | undefined;
489
488
  mimeType?: string | undefined;
489
+ filename?: string | undefined;
490
490
  }>, z.ZodObject<{
491
491
  filename: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
492
492
  mimeType: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -496,13 +496,13 @@ export declare const unionContentSchema: z.ZodDiscriminatedUnion<"type", [z.ZodO
496
496
  }, "strip", z.ZodTypeAny, {
497
497
  type: "file";
498
498
  data: string;
499
- filename?: string | undefined;
500
499
  mimeType?: string | undefined;
500
+ filename?: string | undefined;
501
501
  }, {
502
502
  type: "file";
503
503
  data: string;
504
- filename?: string | undefined;
505
504
  mimeType?: string | undefined;
505
+ filename?: string | undefined;
506
506
  }>]>;
507
507
  /**
508
508
  * Model response format settings
@@ -4,7 +4,7 @@ import mime from "mime";
4
4
  import { v7 } from "uuid";
5
5
  import { z } from "zod";
6
6
  import { optionalize } from "../loader/schema.js";
7
- import { checkArguments, pick } from "../utils/type-utils.js";
7
+ import { checkArguments, isNil, omitByDeep, pick, } from "../utils/type-utils.js";
8
8
  import { Agent, agentOptionsSchema, } from "./agent.js";
9
9
  const CHAT_MODEL_DEFAULT_RETRY_OPTIONS = {
10
10
  retries: 3,
@@ -133,6 +133,25 @@ export class ChatModel extends Agent {
133
133
  enumerable: false,
134
134
  });
135
135
  }
136
+ input.messages = await Promise.all(input.messages.map(async (message) => {
137
+ if (!Array.isArray(message.content))
138
+ return message;
139
+ return {
140
+ ...message,
141
+ content: await Promise.all(message.content.map(async (item) => {
142
+ if (item.type === "local") {
143
+ return {
144
+ ...item,
145
+ type: "file",
146
+ data: await nodejs.fs.readFile(item.path, "base64"),
147
+ path: undefined,
148
+ mimeType: item.mimeType || ChatModel.getMimeType(item.filename || item.path),
149
+ };
150
+ }
151
+ return item;
152
+ })),
153
+ };
154
+ }));
136
155
  }
137
156
  /**
138
157
  * Performs postprocessing operations after handling output
@@ -157,14 +176,6 @@ export class ChatModel extends Agent {
157
176
  }
158
177
  }
159
178
  }
160
- if (input.responseFormat?.type === "json_schema" &&
161
- // NOTE: Should not validate if there are tool calls
162
- !output.toolCalls?.length) {
163
- const ajv = new Ajv();
164
- if (!ajv.validate(input.responseFormat.jsonSchema.schema, output.json)) {
165
- throw new StructuredOutputError(`Output JSON does not conform to the provided JSON schema: ${ajv.errorsText()}`);
166
- }
167
- }
168
179
  super.postprocess(input, output, options);
169
180
  const { usage } = output;
170
181
  if (usage) {
@@ -182,6 +193,16 @@ export class ChatModel extends Agent {
182
193
  files: await Promise.all(files.map((file) => this.transformFileOutput(input, file, options))),
183
194
  };
184
195
  }
196
+ // Remove fields with `null` value for validation
197
+ output = omitByDeep(output, (value) => isNil(value));
198
+ if (input.responseFormat?.type === "json_schema" &&
199
+ // NOTE: Should not validate if there are tool calls
200
+ !output.toolCalls?.length) {
201
+ const ajv = new Ajv();
202
+ if (!ajv.validate(input.responseFormat.jsonSchema.schema, output.json)) {
203
+ throw new StructuredOutputError(`Output JSON does not conform to the provided JSON schema: ${ajv.errorsText()}`);
204
+ }
205
+ }
185
206
  return super.processAgentOutput(input, output, options);
186
207
  }
187
208
  async transformFileOutput(input, data, options) {
@@ -55,7 +55,9 @@ export async function parseAgentFile(path, data) {
55
55
  ])
56
56
  .transform((v) => typeof v === "string"
57
57
  ? { content: v, path }
58
- : Promise.resolve(nodejs.path.join(nodejs.path.dirname(path), v.url)).then((path) => nodejs.fs.readFile(path, "utf8").then((content) => ({ content, path }))));
58
+ : Promise.resolve(nodejs.path.isAbsolute(v.url)
59
+ ? v.url
60
+ : nodejs.path.join(nodejs.path.dirname(path), v.url)).then((path) => nodejs.fs.readFile(path, "utf8").then((content) => ({ content, path }))));
59
61
  return camelizeSchema(z.discriminatedUnion("type", [
60
62
  z
61
63
  .object({
@@ -64,6 +66,7 @@ export async function parseAgentFile(path, data) {
64
66
  inputKey: optionalize(z.string()),
65
67
  outputKey: optionalize(z.string()),
66
68
  toolChoice: optionalize(z.nativeEnum(AIAgentToolChoice)),
69
+ structuredStreamMode: optionalize(z.boolean()),
67
70
  })
68
71
  .extend(baseAgentSchema.shape),
69
72
  z
@@ -10,3 +10,4 @@ export declare function parseJSON(json: string): any;
10
10
  * @returns The union array with at least 1 item (but the type is at least 2 items for z.union)
11
11
  */
12
12
  export declare function ensureZodUnionArray<T extends z.ZodType>(union: T[]): [T, T, ...T[]];
13
+ export declare function isZodSchema(schema: ZodType): schema is ZodType;
@@ -40,3 +40,8 @@ export function ensureZodUnionArray(union) {
40
40
  }
41
41
  return union;
42
42
  }
43
+ export function isZodSchema(schema) {
44
+ if (!schema || typeof schema !== "object")
45
+ return false;
46
+ return typeof schema.parse === "function" && typeof schema.safeParse === "function";
47
+ }
@@ -19,6 +19,7 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, ...key
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
21
  export declare function omitBy<T extends object, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
+ export declare function omitByDeep(obj: any, predicate: (value: any, key: any) => boolean): any;
22
23
  export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
24
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
25
  [key: string]: T;
@@ -84,6 +84,24 @@ export function omitBy(obj, predicate) {
84
84
  return !predicate(value, k);
85
85
  }));
86
86
  }
87
+ export function omitByDeep(obj, predicate) {
88
+ if (obj === null || obj === undefined)
89
+ return obj;
90
+ if (Array.isArray(obj)) {
91
+ return obj.map((item) => omitByDeep(item, predicate));
92
+ }
93
+ if (typeof obj === "object") {
94
+ const result = {};
95
+ for (const [key, value] of Object.entries(obj)) {
96
+ const newValue = omitByDeep(value, predicate);
97
+ if (!predicate(newValue, key)) {
98
+ result[key] = newValue;
99
+ }
100
+ }
101
+ return result;
102
+ }
103
+ return obj;
104
+ }
87
105
  export function flat(...value) {
88
106
  return value.flat().filter(isNonNullable);
89
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.58.1",
3
+ "version": "1.58.3",
4
4
  "description": "The functional core of agentic AI",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -92,8 +92,8 @@
92
92
  "yaml": "^2.8.0",
93
93
  "zod": "^3.25.67",
94
94
  "zod-to-json-schema": "^3.24.6",
95
- "@aigne/observability-api": "^0.10.2",
96
- "@aigne/platform-helpers": "^0.6.2"
95
+ "@aigne/platform-helpers": "^0.6.2",
96
+ "@aigne/observability-api": "^0.10.2"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@types/bun": "^1.2.18",