@drax/ai-back 3.60.0 → 3.62.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.
@@ -231,31 +231,38 @@ class BuilderTool {
231
231
  getTypeName(field) {
232
232
  return field?.constructor?.name;
233
233
  }
234
+ getDefType(field) {
235
+ return field?.def?.type;
236
+ }
234
237
  fieldAdapter(field) {
235
238
  const f = field;
236
239
  const typeName = this.getTypeName(f);
237
- if (typeof f?.unwrap === "function" && typeName === "ZodOptional") {
240
+ const defType = this.getDefType(f);
241
+ if (typeof f?.unwrap === "function" && (typeName === "ZodOptional" || defType === "optional")) {
238
242
  return this.fieldAdapter(f.unwrap()).optional();
239
243
  }
240
- if (typeof f?.unwrap === "function" && typeName === "ZodNullable") {
244
+ if (typeof f?.unwrap === "function" && (typeName === "ZodNullable" || defType === "nullable")) {
241
245
  return this.fieldAdapter(f.unwrap()).nullable();
242
246
  }
243
- if (typeof f?.unwrap === "function" && typeName === "ZodDefault") {
247
+ if (typeof f?.unwrap === "function" && (typeName === "ZodDefault" || defType === "default")) {
244
248
  return this.fieldAdapter(f.unwrap()).default(f.def.defaultValue);
245
249
  }
246
- if (typeof f?.unwrap === "function" && typeName === "ZodCatch") {
250
+ if (typeof f?.unwrap === "function" && (typeName === "ZodCatch" || defType === "catch")) {
247
251
  return this.fieldAdapter(f.unwrap()).catch(f.def.catchValue);
248
252
  }
249
- if (typeof f?.unwrap === "function" && typeName === "ZodReadonly") {
253
+ if (typeof f?.unwrap === "function" && (typeName === "ZodReadonly" || defType === "readonly")) {
250
254
  return this.fieldAdapter(f.unwrap()).readonly();
251
255
  }
252
- if (typeName === "ZodArray" && f?.element) {
256
+ if ((typeName === "ZodPipe" || defType === "pipe") && f?.def?.out) {
257
+ return this.fieldAdapter(f.def.out);
258
+ }
259
+ if ((typeName === "ZodArray" || defType === "array") && f?.element) {
253
260
  return z.array(this.fieldAdapter(f.element));
254
261
  }
255
- if (typeName === "ZodObject" && f?.shape) {
262
+ if ((typeName === "ZodObject" || defType === "object") && f?.shape) {
256
263
  return this.schemaAdapter(f);
257
264
  }
258
- if (typeName === "ZodDate") {
265
+ if (typeName === "ZodDate" || defType === "date") {
259
266
  return z.iso.datetime();
260
267
  }
261
268
  return f;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "3.60.0",
6
+ "version": "3.62.1",
7
7
  "description": "Ai utils",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "license": "ISC",
20
20
  "dependencies": {
21
21
  "@drax/ai-share": "^3.52.0",
22
- "@drax/crud-back": "^3.60.0",
22
+ "@drax/crud-back": "^3.62.0",
23
23
  "mongoose": "^8.23.0",
24
24
  "mongoose-paginate-v2": "^1.8.3"
25
25
  },
@@ -46,5 +46,5 @@
46
46
  "typescript": "^5.9.3",
47
47
  "vitest": "^3.0.8"
48
48
  },
49
- "gitHead": "4a552213a8d7fc1b4156c9aa06d68c820b1a05a6"
49
+ "gitHead": "52472c2b45c1472ceb647b37a372ac4782eb11ad"
50
50
  }
@@ -262,39 +262,48 @@ class BuilderTool<T = any, C = any, U = any> {
262
262
  return field?.constructor?.name;
263
263
  }
264
264
 
265
+ protected getDefType(field: any): string | undefined {
266
+ return field?.def?.type;
267
+ }
268
+
265
269
  protected fieldAdapter(field: unknown): ZodTypeAny {
266
270
  const f: any = field;
267
271
  const typeName = this.getTypeName(f);
272
+ const defType = this.getDefType(f);
268
273
 
269
- if (typeof f?.unwrap === "function" && typeName === "ZodOptional") {
274
+ if (typeof f?.unwrap === "function" && (typeName === "ZodOptional" || defType === "optional")) {
270
275
  return this.fieldAdapter(f.unwrap()).optional();
271
276
  }
272
277
 
273
- if (typeof f?.unwrap === "function" && typeName === "ZodNullable") {
278
+ if (typeof f?.unwrap === "function" && (typeName === "ZodNullable" || defType === "nullable")) {
274
279
  return this.fieldAdapter(f.unwrap()).nullable();
275
280
  }
276
281
 
277
- if (typeof f?.unwrap === "function" && typeName === "ZodDefault") {
282
+ if (typeof f?.unwrap === "function" && (typeName === "ZodDefault" || defType === "default")) {
278
283
  return this.fieldAdapter(f.unwrap()).default(f.def.defaultValue);
279
284
  }
280
285
 
281
- if (typeof f?.unwrap === "function" && typeName === "ZodCatch") {
286
+ if (typeof f?.unwrap === "function" && (typeName === "ZodCatch" || defType === "catch")) {
282
287
  return this.fieldAdapter(f.unwrap()).catch(f.def.catchValue);
283
288
  }
284
289
 
285
- if (typeof f?.unwrap === "function" && typeName === "ZodReadonly") {
290
+ if (typeof f?.unwrap === "function" && (typeName === "ZodReadonly" || defType === "readonly")) {
286
291
  return this.fieldAdapter(f.unwrap()).readonly();
287
292
  }
288
293
 
289
- if (typeName === "ZodArray" && f?.element) {
294
+ if ((typeName === "ZodPipe" || defType === "pipe") && f?.def?.out) {
295
+ return this.fieldAdapter(f.def.out);
296
+ }
297
+
298
+ if ((typeName === "ZodArray" || defType === "array") && f?.element) {
290
299
  return z.array(this.fieldAdapter(f.element));
291
300
  }
292
301
 
293
- if (typeName === "ZodObject" && f?.shape) {
302
+ if ((typeName === "ZodObject" || defType === "object") && f?.shape) {
294
303
  return this.schemaAdapter(f);
295
304
  }
296
305
 
297
- if (typeName === "ZodDate") {
306
+ if (typeName === "ZodDate" || defType === "date") {
298
307
  return z.iso.datetime();
299
308
  }
300
309
 
@@ -125,6 +125,51 @@ describe("ToolBuilder", () => {
125
125
  expect(() => builder.getSystemPromptSection()).not.toThrow();
126
126
  });
127
127
 
128
+ test("adapts preprocessed date schemas to OpenAI-compatible JSON schema", () => {
129
+ const service: any = {
130
+ async create(data: any) {
131
+ return data;
132
+ },
133
+ };
134
+
135
+ const optionalNullableDateSchema = z.preprocess(value => {
136
+ if (value === "" || value === 0 || value === "0") {
137
+ return null;
138
+ }
139
+
140
+ return value;
141
+ }, z.coerce.date().nullable().optional());
142
+
143
+ const schema = z.object({
144
+ name: z.string().min(1),
145
+ timezone: z.string()
146
+ .default("America/Argentina/Buenos_Aires")
147
+ .refine(value => value.length > 0, "validation.timezone.invalid"),
148
+ runAt: optionalNullableDateSchema,
149
+ });
150
+
151
+ const builder = new BuilderTool({
152
+ entityName: "taskSchedule",
153
+ schema,
154
+ service,
155
+ methods: ["create"],
156
+ });
157
+
158
+ expect(() => builder.getTools()).not.toThrow();
159
+ expect(() => builder.getSystemPromptSection()).not.toThrow();
160
+
161
+ const tools = builder.getTools();
162
+ const createParameters: any = tools[0].parameters;
163
+ const dataSchema = createParameters.properties.data;
164
+
165
+ expect(dataSchema.properties.runAt).toMatchObject({
166
+ anyOf: [
167
+ {type: "string", format: "date-time"},
168
+ {type: "null"},
169
+ ],
170
+ });
171
+ });
172
+
128
173
  test("fails when a requested service method is not available", () => {
129
174
  const builder = new BuilderTool({
130
175
  entityName: "person",