@aigne/core 1.66.0 → 1.67.0-beta

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,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.67.0-beta](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.66.0...core-v1.67.0-beta) (2025-11-11)
4
+
5
+
6
+ ### Features
7
+
8
+ * **core:** add tson.stringify filter for prompt template ([#725](https://github.com/AIGNE-io/aigne-framework/issues/725)) ([c88e71c](https://github.com/AIGNE-io/aigne-framework/commit/c88e71c245a3eaa8a2d60f25c75368bced35fde2))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **core:** improve checkArguments error messages and refactor optional schemas ([#726](https://github.com/AIGNE-io/aigne-framework/issues/726)) ([8680f43](https://github.com/AIGNE-io/aigne-framework/commit/8680f43e6ad224eb84948b056c51e80ef8c47e06))
14
+
15
+
16
+ ### Dependencies
17
+
18
+ * The following workspace dependencies were updated
19
+ * dependencies
20
+ * @aigne/observability-api bumped to 0.11.6-beta
21
+
3
22
  ## [1.66.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.66.0-beta.5...core-v1.66.0) (2025-11-07)
4
23
 
5
24
 
@@ -486,7 +486,7 @@ export interface ChatModelOutputUsage {
486
486
  export declare const chatModelOutputUsageSchema: z.ZodObject<{
487
487
  inputTokens: z.ZodNumber;
488
488
  outputTokens: z.ZodNumber;
489
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
489
+ aigneHubCredits: z.ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
490
490
  }, "strip", z.ZodTypeAny, {
491
491
  inputTokens: number;
492
492
  outputTokens: number;
@@ -37,6 +37,7 @@ exports.chatModelOutputUsageSchema = exports.unionContentSchema = exports.textCo
37
37
  const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
38
38
  const zod_1 = require("zod");
39
39
  const zod_from_json_schema_1 = require("zod-from-json-schema");
40
+ const schema_js_1 = require("../loader/schema.js");
40
41
  const json_schema_js_1 = require("../utils/json-schema.js");
41
42
  const logger_js_1 = require("../utils/logger.js");
42
43
  const type_utils_js_1 = require("../utils/type-utils.js");
@@ -310,19 +311,17 @@ exports.unionContentSchema = zod_1.z.discriminatedUnion("type", [
310
311
  ]);
311
312
  const chatModelInputMessageSchema = zod_1.z.object({
312
313
  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")]),
313
- content: zod_1.z.union([zod_1.z.string(), zod_1.z.array(exports.unionContentSchema)]).optional(),
314
- toolCalls: zod_1.z
315
- .array(zod_1.z.object({
314
+ content: (0, schema_js_1.optionalize)(zod_1.z.union([zod_1.z.string(), zod_1.z.array(exports.unionContentSchema)])),
315
+ toolCalls: (0, schema_js_1.optionalize)(zod_1.z.array(zod_1.z.object({
316
316
  id: zod_1.z.string(),
317
317
  type: zod_1.z.literal("function"),
318
318
  function: zod_1.z.object({
319
319
  name: zod_1.z.string(),
320
320
  arguments: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
321
321
  }),
322
- }))
323
- .optional(),
324
- toolCallId: zod_1.z.string().optional(),
325
- name: zod_1.z.string().optional(),
322
+ }))),
323
+ toolCallId: (0, schema_js_1.optionalize)(zod_1.z.string()),
324
+ name: (0, schema_js_1.optionalize)(zod_1.z.string()),
326
325
  });
327
326
  const chatModelInputResponseFormatSchema = zod_1.z.discriminatedUnion("type", [
328
327
  zod_1.z.object({ type: zod_1.z.literal("text") }),
@@ -330,9 +329,9 @@ const chatModelInputResponseFormatSchema = zod_1.z.discriminatedUnion("type", [
330
329
  type: zod_1.z.literal("json_schema"),
331
330
  jsonSchema: zod_1.z.object({
332
331
  name: zod_1.z.string(),
333
- description: zod_1.z.string().optional(),
332
+ description: (0, schema_js_1.optionalize)(zod_1.z.string()),
334
333
  schema: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
335
- strict: zod_1.z.boolean().optional(),
334
+ strict: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
336
335
  }),
337
336
  }),
338
337
  ]);
@@ -340,7 +339,7 @@ const chatModelInputToolSchema = zod_1.z.object({
340
339
  type: zod_1.z.literal("function"),
341
340
  function: zod_1.z.object({
342
341
  name: zod_1.z.string(),
343
- description: zod_1.z.string().optional(),
342
+ description: (0, schema_js_1.optionalize)(zod_1.z.string()),
344
343
  parameters: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
345
344
  }),
346
345
  });
@@ -351,31 +350,31 @@ const chatModelInputToolChoiceSchema = zod_1.z.union([
351
350
  chatModelInputToolSchema,
352
351
  ]);
353
352
  const modelOptionsSchema = zod_1.z.object({
354
- model: (0, agent_js_1.getterSchema)(zod_1.z.string()).optional(),
355
- temperature: (0, agent_js_1.getterSchema)(zod_1.z.number()).optional(),
356
- topP: (0, agent_js_1.getterSchema)(zod_1.z.number()).optional(),
357
- frequencyPenalty: (0, agent_js_1.getterSchema)(zod_1.z.number()).optional(),
358
- presencePenalty: (0, agent_js_1.getterSchema)(zod_1.z.number()).optional(),
359
- parallelToolCalls: (0, agent_js_1.getterSchema)(zod_1.z.boolean()).optional().default(true),
360
- modalities: (0, agent_js_1.getterSchema)(zod_1.z.array(zod_1.z.enum(["text", "image", "audio"]))).optional(),
361
- reasoningEffort: (0, agent_js_1.getterSchema)(zod_1.z.union([
353
+ model: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.string())),
354
+ temperature: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.number())),
355
+ topP: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.number())),
356
+ frequencyPenalty: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.number())),
357
+ presencePenalty: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.number())),
358
+ parallelToolCalls: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.boolean())).default(true),
359
+ modalities: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.array(zod_1.z.enum(["text", "image", "audio"])))),
360
+ reasoningEffort: (0, schema_js_1.optionalize)((0, agent_js_1.getterSchema)(zod_1.z.union([
362
361
  zod_1.z.number(),
363
362
  zod_1.z.literal("minimal"),
364
363
  zod_1.z.literal("low"),
365
364
  zod_1.z.literal("medium"),
366
365
  zod_1.z.literal("high"),
367
- ])).optional(),
366
+ ]))),
368
367
  });
369
368
  const chatModelOptionsSchema = agent_js_1.agentOptionsSchema.extend({
370
- model: zod_1.z.string().optional(),
371
- modelOptions: modelOptionsSchema.optional(),
369
+ model: (0, schema_js_1.optionalize)(zod_1.z.string()),
370
+ modelOptions: (0, schema_js_1.optionalize)(modelOptionsSchema),
372
371
  });
373
372
  const chatModelInputSchema = zod_1.z.object({
374
373
  messages: zod_1.z.array(chatModelInputMessageSchema),
375
- responseFormat: chatModelInputResponseFormatSchema.optional(),
376
- tools: zod_1.z.array(chatModelInputToolSchema).optional(),
377
- toolChoice: chatModelInputToolChoiceSchema.optional(),
378
- modelOptions: modelOptionsSchema.optional(),
374
+ responseFormat: (0, schema_js_1.optionalize)(chatModelInputResponseFormatSchema),
375
+ tools: (0, schema_js_1.optionalize)(zod_1.z.array(chatModelInputToolSchema)),
376
+ toolChoice: (0, schema_js_1.optionalize)(chatModelInputToolChoiceSchema),
377
+ modelOptions: (0, schema_js_1.optionalize)(modelOptionsSchema),
379
378
  });
380
379
  const chatModelOutputToolCallSchema = zod_1.z.object({
381
380
  id: zod_1.z.string(),
@@ -388,14 +387,14 @@ const chatModelOutputToolCallSchema = zod_1.z.object({
388
387
  exports.chatModelOutputUsageSchema = zod_1.z.object({
389
388
  inputTokens: zod_1.z.number(),
390
389
  outputTokens: zod_1.z.number(),
391
- aigneHubCredits: zod_1.z.number().optional(),
390
+ aigneHubCredits: (0, schema_js_1.optionalize)(zod_1.z.number()),
392
391
  });
393
392
  const chatModelOutputSchema = zod_1.z.object({
394
- text: zod_1.z.string().optional(),
395
- thoughts: zod_1.z.string().optional(),
396
- json: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
397
- toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
398
- usage: exports.chatModelOutputUsageSchema.optional(),
399
- model: zod_1.z.string().optional(),
400
- files: zod_1.z.array(model_js_1.fileUnionContentSchema).optional(),
393
+ text: (0, schema_js_1.optionalize)(zod_1.z.string()),
394
+ thoughts: (0, schema_js_1.optionalize)(zod_1.z.string()),
395
+ json: (0, schema_js_1.optionalize)(zod_1.z.record(zod_1.z.string(), zod_1.z.unknown())),
396
+ toolCalls: (0, schema_js_1.optionalize)(zod_1.z.array(chatModelOutputToolCallSchema)),
397
+ usage: (0, schema_js_1.optionalize)(exports.chatModelOutputUsageSchema),
398
+ model: (0, schema_js_1.optionalize)(zod_1.z.string()),
399
+ files: (0, schema_js_1.optionalize)(zod_1.z.array(model_js_1.fileUnionContentSchema)),
401
400
  });
@@ -195,7 +195,7 @@ export declare const imageModelOutputSchema: z.ZodObject<{
195
195
  usage: z.ZodOptional<z.ZodObject<{
196
196
  inputTokens: z.ZodNumber;
197
197
  outputTokens: z.ZodNumber;
198
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
198
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
199
199
  }, "strip", z.ZodTypeAny, {
200
200
  inputTokens: number;
201
201
  outputTokens: number;
@@ -202,7 +202,7 @@ export declare const videoModelOutputSchema: z.ZodObject<{
202
202
  usage: z.ZodOptional<z.ZodObject<{
203
203
  inputTokens: z.ZodNumber;
204
204
  outputTokens: z.ZodNumber;
205
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
205
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
206
  }, "strip", z.ZodTypeAny, {
207
207
  inputTokens: number;
208
208
  outputTokens: number;
@@ -1,6 +1,40 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.setupFilters = setupFilters;
37
+ const tson = __importStar(require("@zenoaihq/tson"));
4
38
  const yaml_1 = require("yaml");
5
39
  function setupFilters(env) {
6
40
  env.addFilter("yaml.stringify", (obj) => {
@@ -9,4 +43,7 @@ function setupFilters(env) {
9
43
  env.addFilter("json.stringify", (obj, ...args) => {
10
44
  return JSON.stringify(obj, ...args);
11
45
  });
46
+ env.addFilter("tson.stringify", (obj) => {
47
+ return tson.dumps(obj);
48
+ });
12
49
  }
@@ -15,6 +15,7 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
15
15
  export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
16
16
  export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
17
17
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
+ export declare function get(obj: any, path: (string | number | symbol)[]): any;
18
19
  export declare function pick<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Pick<T, K>;
19
20
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
21
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
@@ -8,6 +8,7 @@ exports.isNotEmpty = isNotEmpty;
8
8
  exports.duplicates = duplicates;
9
9
  exports.remove = remove;
10
10
  exports.unique = unique;
11
+ exports.get = get;
11
12
  exports.pick = pick;
12
13
  exports.omit = omit;
13
14
  exports.omitDeep = omitDeep;
@@ -78,6 +79,9 @@ function unique(arr, key = (item) => item) {
78
79
  return true;
79
80
  });
80
81
  }
82
+ function get(obj, path) {
83
+ return path.reduce((acc, key) => (isRecord(acc) ? Reflect.get(acc, key) : undefined), obj);
84
+ }
81
85
  function pick(obj, ...keys) {
82
86
  const flattenedKeys = new Set(keys.flat());
83
87
  return Object.fromEntries(Object.entries(obj).filter(([key]) => flattenedKeys.has(key)));
@@ -132,46 +136,33 @@ function createAccessorArray(array, accessor) {
132
136
  }
133
137
  function checkArguments(prefix, schema, args) {
134
138
  try {
135
- return schema.parse(args, {
136
- errorMap: (issue, ctx) => {
137
- if (issue.code === "invalid_union") {
138
- // handle all issues that are not invalid_type
139
- const otherQuestions = issue.unionErrors
140
- .map(({ issues: [issue] }) => {
141
- if (issue && issue.code !== "invalid_type") {
142
- return issue.message || zod_1.z.defaultErrorMap(issue, ctx).message;
143
- }
144
- })
145
- .filter(isNonNullable);
146
- if (otherQuestions.length) {
147
- return { message: otherQuestions.join(", ") };
148
- }
149
- // handle invalid_type issues
150
- const expected = issue.unionErrors
151
- .map(({ issues: [issue] }) => {
152
- if (issue?.code === "invalid_type") {
153
- return issue;
154
- }
155
- })
156
- .filter(isNonNullable);
157
- if (expected.length) {
158
- return {
159
- message: `Expected ${expected.map((i) => i.expected).join(" or ")}, received ${expected[0]?.received}`,
160
- };
161
- }
162
- }
163
- return { message: ctx.defaultError };
164
- },
165
- });
139
+ return schema.parse(args);
166
140
  }
167
141
  catch (error) {
168
142
  if (error instanceof zod_1.z.ZodError) {
169
- const message = error.issues.map((i) => `${i.path}: ${i.message}`).join(", ");
143
+ const message = error.issues
144
+ .map((i) => {
145
+ const msg = i.code === "invalid_union"
146
+ ? `${i.message} expect ${getExpectedTypes(i)} got ${get(args, i.path)}`
147
+ : i.message;
148
+ return `${i.path}: ${msg}`;
149
+ })
150
+ .join(", ");
170
151
  throw new Error(`${prefix} check arguments error: ${message}`);
171
152
  }
172
153
  throw error;
173
154
  }
174
155
  }
156
+ function getExpectedTypes(issue) {
157
+ if (Array.isArray(issue))
158
+ return issue.flatMap((issue) => getExpectedTypes(issue));
159
+ if ("expected" in issue)
160
+ return [String(issue.expected)];
161
+ if (issue.code === "invalid_union") {
162
+ return issue.unionErrors.flatMap((e) => getExpectedTypes(e.issues));
163
+ }
164
+ return [];
165
+ }
175
166
  function tryOrThrow(fn, error) {
176
167
  const createError = (e) => {
177
168
  return typeof error === "function"
@@ -486,7 +486,7 @@ export interface ChatModelOutputUsage {
486
486
  export declare const chatModelOutputUsageSchema: z.ZodObject<{
487
487
  inputTokens: z.ZodNumber;
488
488
  outputTokens: z.ZodNumber;
489
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
489
+ aigneHubCredits: z.ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
490
490
  }, "strip", z.ZodTypeAny, {
491
491
  inputTokens: number;
492
492
  outputTokens: number;
@@ -195,7 +195,7 @@ export declare const imageModelOutputSchema: z.ZodObject<{
195
195
  usage: z.ZodOptional<z.ZodObject<{
196
196
  inputTokens: z.ZodNumber;
197
197
  outputTokens: z.ZodNumber;
198
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
198
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
199
199
  }, "strip", z.ZodTypeAny, {
200
200
  inputTokens: number;
201
201
  outputTokens: number;
@@ -202,7 +202,7 @@ export declare const videoModelOutputSchema: z.ZodObject<{
202
202
  usage: z.ZodOptional<z.ZodObject<{
203
203
  inputTokens: z.ZodNumber;
204
204
  outputTokens: z.ZodNumber;
205
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
205
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
206
  }, "strip", z.ZodTypeAny, {
207
207
  inputTokens: number;
208
208
  outputTokens: number;
@@ -15,6 +15,7 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
15
15
  export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
16
16
  export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
17
17
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
+ export declare function get(obj: any, path: (string | number | symbol)[]): any;
18
19
  export declare function pick<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Pick<T, K>;
19
20
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
21
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
@@ -486,7 +486,7 @@ export interface ChatModelOutputUsage {
486
486
  export declare const chatModelOutputUsageSchema: z.ZodObject<{
487
487
  inputTokens: z.ZodNumber;
488
488
  outputTokens: z.ZodNumber;
489
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
489
+ aigneHubCredits: z.ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
490
490
  }, "strip", z.ZodTypeAny, {
491
491
  inputTokens: number;
492
492
  outputTokens: number;
@@ -1,6 +1,7 @@
1
1
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
2
2
  import { z } from "zod";
3
3
  import { convertJsonSchemaToZod } from "zod-from-json-schema";
4
+ import { optionalize } from "../loader/schema.js";
4
5
  import { wrapAutoParseJsonSchema } from "../utils/json-schema.js";
5
6
  import { logger } from "../utils/logger.js";
6
7
  import { checkArguments, isNil, omitByDeep } from "../utils/type-utils.js";
@@ -272,19 +273,17 @@ export const unionContentSchema = z.discriminatedUnion("type", [
272
273
  ]);
273
274
  const chatModelInputMessageSchema = z.object({
274
275
  role: z.union([z.literal("system"), z.literal("user"), z.literal("agent"), z.literal("tool")]),
275
- content: z.union([z.string(), z.array(unionContentSchema)]).optional(),
276
- toolCalls: z
277
- .array(z.object({
276
+ content: optionalize(z.union([z.string(), z.array(unionContentSchema)])),
277
+ toolCalls: optionalize(z.array(z.object({
278
278
  id: z.string(),
279
279
  type: z.literal("function"),
280
280
  function: z.object({
281
281
  name: z.string(),
282
282
  arguments: z.record(z.string(), z.unknown()),
283
283
  }),
284
- }))
285
- .optional(),
286
- toolCallId: z.string().optional(),
287
- name: z.string().optional(),
284
+ }))),
285
+ toolCallId: optionalize(z.string()),
286
+ name: optionalize(z.string()),
288
287
  });
289
288
  const chatModelInputResponseFormatSchema = z.discriminatedUnion("type", [
290
289
  z.object({ type: z.literal("text") }),
@@ -292,9 +291,9 @@ const chatModelInputResponseFormatSchema = z.discriminatedUnion("type", [
292
291
  type: z.literal("json_schema"),
293
292
  jsonSchema: z.object({
294
293
  name: z.string(),
295
- description: z.string().optional(),
294
+ description: optionalize(z.string()),
296
295
  schema: z.record(z.string(), z.unknown()),
297
- strict: z.boolean().optional(),
296
+ strict: optionalize(z.boolean()),
298
297
  }),
299
298
  }),
300
299
  ]);
@@ -302,7 +301,7 @@ const chatModelInputToolSchema = z.object({
302
301
  type: z.literal("function"),
303
302
  function: z.object({
304
303
  name: z.string(),
305
- description: z.string().optional(),
304
+ description: optionalize(z.string()),
306
305
  parameters: z.record(z.string(), z.unknown()),
307
306
  }),
308
307
  });
@@ -313,31 +312,31 @@ const chatModelInputToolChoiceSchema = z.union([
313
312
  chatModelInputToolSchema,
314
313
  ]);
315
314
  const modelOptionsSchema = z.object({
316
- model: getterSchema(z.string()).optional(),
317
- temperature: getterSchema(z.number()).optional(),
318
- topP: getterSchema(z.number()).optional(),
319
- frequencyPenalty: getterSchema(z.number()).optional(),
320
- presencePenalty: getterSchema(z.number()).optional(),
321
- parallelToolCalls: getterSchema(z.boolean()).optional().default(true),
322
- modalities: getterSchema(z.array(z.enum(["text", "image", "audio"]))).optional(),
323
- reasoningEffort: getterSchema(z.union([
315
+ model: optionalize(getterSchema(z.string())),
316
+ temperature: optionalize(getterSchema(z.number())),
317
+ topP: optionalize(getterSchema(z.number())),
318
+ frequencyPenalty: optionalize(getterSchema(z.number())),
319
+ presencePenalty: optionalize(getterSchema(z.number())),
320
+ parallelToolCalls: optionalize(getterSchema(z.boolean())).default(true),
321
+ modalities: optionalize(getterSchema(z.array(z.enum(["text", "image", "audio"])))),
322
+ reasoningEffort: optionalize(getterSchema(z.union([
324
323
  z.number(),
325
324
  z.literal("minimal"),
326
325
  z.literal("low"),
327
326
  z.literal("medium"),
328
327
  z.literal("high"),
329
- ])).optional(),
328
+ ]))),
330
329
  });
331
330
  const chatModelOptionsSchema = agentOptionsSchema.extend({
332
- model: z.string().optional(),
333
- modelOptions: modelOptionsSchema.optional(),
331
+ model: optionalize(z.string()),
332
+ modelOptions: optionalize(modelOptionsSchema),
334
333
  });
335
334
  const chatModelInputSchema = z.object({
336
335
  messages: z.array(chatModelInputMessageSchema),
337
- responseFormat: chatModelInputResponseFormatSchema.optional(),
338
- tools: z.array(chatModelInputToolSchema).optional(),
339
- toolChoice: chatModelInputToolChoiceSchema.optional(),
340
- modelOptions: modelOptionsSchema.optional(),
336
+ responseFormat: optionalize(chatModelInputResponseFormatSchema),
337
+ tools: optionalize(z.array(chatModelInputToolSchema)),
338
+ toolChoice: optionalize(chatModelInputToolChoiceSchema),
339
+ modelOptions: optionalize(modelOptionsSchema),
341
340
  });
342
341
  const chatModelOutputToolCallSchema = z.object({
343
342
  id: z.string(),
@@ -350,14 +349,14 @@ const chatModelOutputToolCallSchema = z.object({
350
349
  export const chatModelOutputUsageSchema = z.object({
351
350
  inputTokens: z.number(),
352
351
  outputTokens: z.number(),
353
- aigneHubCredits: z.number().optional(),
352
+ aigneHubCredits: optionalize(z.number()),
354
353
  });
355
354
  const chatModelOutputSchema = z.object({
356
- text: z.string().optional(),
357
- thoughts: z.string().optional(),
358
- json: z.record(z.string(), z.unknown()).optional(),
359
- toolCalls: z.array(chatModelOutputToolCallSchema).optional(),
360
- usage: chatModelOutputUsageSchema.optional(),
361
- model: z.string().optional(),
362
- files: z.array(fileUnionContentSchema).optional(),
355
+ text: optionalize(z.string()),
356
+ thoughts: optionalize(z.string()),
357
+ json: optionalize(z.record(z.string(), z.unknown())),
358
+ toolCalls: optionalize(z.array(chatModelOutputToolCallSchema)),
359
+ usage: optionalize(chatModelOutputUsageSchema),
360
+ model: optionalize(z.string()),
361
+ files: optionalize(z.array(fileUnionContentSchema)),
363
362
  });
@@ -195,7 +195,7 @@ export declare const imageModelOutputSchema: z.ZodObject<{
195
195
  usage: z.ZodOptional<z.ZodObject<{
196
196
  inputTokens: z.ZodNumber;
197
197
  outputTokens: z.ZodNumber;
198
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
198
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
199
199
  }, "strip", z.ZodTypeAny, {
200
200
  inputTokens: number;
201
201
  outputTokens: number;
@@ -202,7 +202,7 @@ export declare const videoModelOutputSchema: z.ZodObject<{
202
202
  usage: z.ZodOptional<z.ZodObject<{
203
203
  inputTokens: z.ZodNumber;
204
204
  outputTokens: z.ZodNumber;
205
- aigneHubCredits: z.ZodOptional<z.ZodNumber>;
205
+ aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
206
  }, "strip", z.ZodTypeAny, {
207
207
  inputTokens: number;
208
208
  outputTokens: number;
@@ -1,3 +1,4 @@
1
+ import * as tson from "@zenoaihq/tson";
1
2
  import { stringify } from "yaml";
2
3
  export function setupFilters(env) {
3
4
  env.addFilter("yaml.stringify", (obj) => {
@@ -6,4 +7,7 @@ export function setupFilters(env) {
6
7
  env.addFilter("json.stringify", (obj, ...args) => {
7
8
  return JSON.stringify(obj, ...args);
8
9
  });
10
+ env.addFilter("tson.stringify", (obj) => {
11
+ return tson.dumps(obj);
12
+ });
9
13
  }
@@ -15,6 +15,7 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
15
15
  export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
16
16
  export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
17
17
  export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
18
+ export declare function get(obj: any, path: (string | number | symbol)[]): any;
18
19
  export declare function pick<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Pick<T, K>;
19
20
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
21
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
@@ -58,6 +58,9 @@ export function unique(arr, key = (item) => item) {
58
58
  return true;
59
59
  });
60
60
  }
61
+ export function get(obj, path) {
62
+ return path.reduce((acc, key) => (isRecord(acc) ? Reflect.get(acc, key) : undefined), obj);
63
+ }
61
64
  export function pick(obj, ...keys) {
62
65
  const flattenedKeys = new Set(keys.flat());
63
66
  return Object.fromEntries(Object.entries(obj).filter(([key]) => flattenedKeys.has(key)));
@@ -112,46 +115,33 @@ export function createAccessorArray(array, accessor) {
112
115
  }
113
116
  export function checkArguments(prefix, schema, args) {
114
117
  try {
115
- return schema.parse(args, {
116
- errorMap: (issue, ctx) => {
117
- if (issue.code === "invalid_union") {
118
- // handle all issues that are not invalid_type
119
- const otherQuestions = issue.unionErrors
120
- .map(({ issues: [issue] }) => {
121
- if (issue && issue.code !== "invalid_type") {
122
- return issue.message || z.defaultErrorMap(issue, ctx).message;
123
- }
124
- })
125
- .filter(isNonNullable);
126
- if (otherQuestions.length) {
127
- return { message: otherQuestions.join(", ") };
128
- }
129
- // handle invalid_type issues
130
- const expected = issue.unionErrors
131
- .map(({ issues: [issue] }) => {
132
- if (issue?.code === "invalid_type") {
133
- return issue;
134
- }
135
- })
136
- .filter(isNonNullable);
137
- if (expected.length) {
138
- return {
139
- message: `Expected ${expected.map((i) => i.expected).join(" or ")}, received ${expected[0]?.received}`,
140
- };
141
- }
142
- }
143
- return { message: ctx.defaultError };
144
- },
145
- });
118
+ return schema.parse(args);
146
119
  }
147
120
  catch (error) {
148
121
  if (error instanceof z.ZodError) {
149
- const message = error.issues.map((i) => `${i.path}: ${i.message}`).join(", ");
122
+ const message = error.issues
123
+ .map((i) => {
124
+ const msg = i.code === "invalid_union"
125
+ ? `${i.message} expect ${getExpectedTypes(i)} got ${get(args, i.path)}`
126
+ : i.message;
127
+ return `${i.path}: ${msg}`;
128
+ })
129
+ .join(", ");
150
130
  throw new Error(`${prefix} check arguments error: ${message}`);
151
131
  }
152
132
  throw error;
153
133
  }
154
134
  }
135
+ function getExpectedTypes(issue) {
136
+ if (Array.isArray(issue))
137
+ return issue.flatMap((issue) => getExpectedTypes(issue));
138
+ if ("expected" in issue)
139
+ return [String(issue.expected)];
140
+ if (issue.code === "invalid_union") {
141
+ return issue.unionErrors.flatMap((e) => getExpectedTypes(e.issues));
142
+ }
143
+ return [];
144
+ }
155
145
  export function tryOrThrow(fn, error) {
156
146
  const createError = (e) => {
157
147
  return typeof error === "function"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.66.0",
3
+ "version": "1.67.0-beta",
4
4
  "description": "The functional core of agentic AI",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -72,6 +72,7 @@
72
72
  "@opentelemetry/api": "^1.9.0",
73
73
  "@opentelemetry/sdk-trace-base": "^2.1.0",
74
74
  "@types/debug": "^4.1.12",
75
+ "@zenoaihq/tson": "^1.0.0",
75
76
  "camelize-ts": "^3.0.0",
76
77
  "content-type": "^1.0.5",
77
78
  "debug": "^4.4.3",
@@ -91,7 +92,7 @@
91
92
  "zod": "^3.25.67",
92
93
  "zod-from-json-schema": "^0.0.5",
93
94
  "zod-to-json-schema": "^3.24.6",
94
- "@aigne/observability-api": "^0.11.5",
95
+ "@aigne/observability-api": "^0.11.6-beta",
95
96
  "@aigne/platform-helpers": "^0.6.3",
96
97
  "@aigne/afs": "^1.1.1"
97
98
  },