@robota-sdk/agent-tools 3.0.0-beta.65 → 3.0.0-beta.67
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/dist/node/index.js
CHANGED
|
@@ -348,6 +348,77 @@ var ToolRegistry = class {
|
|
|
348
348
|
}
|
|
349
349
|
};
|
|
350
350
|
//#endregion
|
|
351
|
+
//#region src/implementations/function-tool/parameter-validator.ts
|
|
352
|
+
/**
|
|
353
|
+
* Validate individual parameter type against its schema.
|
|
354
|
+
* Returns an error string if invalid, undefined if valid.
|
|
355
|
+
*/
|
|
356
|
+
function validateParameterType(key, value, schema) {
|
|
357
|
+
switch (schema["type"]) {
|
|
358
|
+
case "string":
|
|
359
|
+
if (typeof value !== "string") return `Parameter "${key}" must be a string, got ${typeof value}`;
|
|
360
|
+
break;
|
|
361
|
+
case "number":
|
|
362
|
+
if (typeof value !== "number" || isNaN(value)) return `Parameter "${key}" must be a number, got ${typeof value}`;
|
|
363
|
+
break;
|
|
364
|
+
case "boolean":
|
|
365
|
+
if (typeof value !== "boolean") return `Parameter "${key}" must be a boolean, got ${typeof value}`;
|
|
366
|
+
break;
|
|
367
|
+
case "array":
|
|
368
|
+
if (!Array.isArray(value)) return `Parameter "${key}" must be an array, got ${typeof value}`;
|
|
369
|
+
if (schema.items) for (let i = 0; i < value.length; i++) {
|
|
370
|
+
const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);
|
|
371
|
+
if (itemError) return itemError;
|
|
372
|
+
}
|
|
373
|
+
break;
|
|
374
|
+
case "object":
|
|
375
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return `Parameter "${key}" must be an object, got ${typeof value}`;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
379
|
+
const enumValues = schema.enum;
|
|
380
|
+
let isValidEnum = false;
|
|
381
|
+
for (const enumValue of enumValues) if (value === enumValue) {
|
|
382
|
+
isValidEnum = true;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (!isValidEnum) return `Parameter "${key}" must be one of: ${enumValues.join(", ")}, got ${value}`;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Collect all validation errors for the given parameters against a schema.
|
|
390
|
+
*/
|
|
391
|
+
function getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties) {
|
|
392
|
+
const errors = [];
|
|
393
|
+
for (const field of schemaRequired) if (!(field in parameters)) errors.push(`Missing required parameter: ${field}`);
|
|
394
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
395
|
+
const paramSchema = schemaProperties[key];
|
|
396
|
+
if (!paramSchema) {
|
|
397
|
+
if (additionalProperties === true) continue;
|
|
398
|
+
if (additionalProperties && typeof additionalProperties === "object") {
|
|
399
|
+
const additionalTypeError = validateParameterType(key, value, additionalProperties);
|
|
400
|
+
if (additionalTypeError) errors.push(additionalTypeError);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
errors.push(`Unknown parameter: ${key}`);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
const typeError = validateParameterType(key, value, paramSchema);
|
|
407
|
+
if (typeError) errors.push(typeError);
|
|
408
|
+
}
|
|
409
|
+
return errors;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Validate parameters and return a structured result.
|
|
413
|
+
*/
|
|
414
|
+
function validateToolParameters(parameters, schemaRequired, schemaProperties, additionalProperties) {
|
|
415
|
+
const errors = getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties);
|
|
416
|
+
return {
|
|
417
|
+
isValid: errors.length === 0,
|
|
418
|
+
errors
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
351
422
|
//#region src/implementations/function-tool/schema-converter.ts
|
|
352
423
|
/**
|
|
353
424
|
* Convert Zod schema to JSON Schema format with safe undefined handling
|
|
@@ -453,77 +524,6 @@ function isRequiredField(typeObj) {
|
|
|
453
524
|
return typeDef.typeName !== "ZodOptional" && typeDef.typeName !== "ZodNullable" && typeDef.typeName !== "ZodDefault";
|
|
454
525
|
}
|
|
455
526
|
//#endregion
|
|
456
|
-
//#region src/implementations/function-tool/parameter-validator.ts
|
|
457
|
-
/**
|
|
458
|
-
* Validate individual parameter type against its schema.
|
|
459
|
-
* Returns an error string if invalid, undefined if valid.
|
|
460
|
-
*/
|
|
461
|
-
function validateParameterType(key, value, schema) {
|
|
462
|
-
switch (schema["type"]) {
|
|
463
|
-
case "string":
|
|
464
|
-
if (typeof value !== "string") return `Parameter "${key}" must be a string, got ${typeof value}`;
|
|
465
|
-
break;
|
|
466
|
-
case "number":
|
|
467
|
-
if (typeof value !== "number" || isNaN(value)) return `Parameter "${key}" must be a number, got ${typeof value}`;
|
|
468
|
-
break;
|
|
469
|
-
case "boolean":
|
|
470
|
-
if (typeof value !== "boolean") return `Parameter "${key}" must be a boolean, got ${typeof value}`;
|
|
471
|
-
break;
|
|
472
|
-
case "array":
|
|
473
|
-
if (!Array.isArray(value)) return `Parameter "${key}" must be an array, got ${typeof value}`;
|
|
474
|
-
if (schema.items) for (let i = 0; i < value.length; i++) {
|
|
475
|
-
const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);
|
|
476
|
-
if (itemError) return itemError;
|
|
477
|
-
}
|
|
478
|
-
break;
|
|
479
|
-
case "object":
|
|
480
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) return `Parameter "${key}" must be an object, got ${typeof value}`;
|
|
481
|
-
break;
|
|
482
|
-
}
|
|
483
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
484
|
-
const enumValues = schema.enum;
|
|
485
|
-
let isValidEnum = false;
|
|
486
|
-
for (const enumValue of enumValues) if (value === enumValue) {
|
|
487
|
-
isValidEnum = true;
|
|
488
|
-
break;
|
|
489
|
-
}
|
|
490
|
-
if (!isValidEnum) return `Parameter "${key}" must be one of: ${enumValues.join(", ")}, got ${value}`;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Collect all validation errors for the given parameters against a schema.
|
|
495
|
-
*/
|
|
496
|
-
function getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties) {
|
|
497
|
-
const errors = [];
|
|
498
|
-
for (const field of schemaRequired) if (!(field in parameters)) errors.push(`Missing required parameter: ${field}`);
|
|
499
|
-
for (const [key, value] of Object.entries(parameters)) {
|
|
500
|
-
const paramSchema = schemaProperties[key];
|
|
501
|
-
if (!paramSchema) {
|
|
502
|
-
if (additionalProperties === true) continue;
|
|
503
|
-
if (additionalProperties && typeof additionalProperties === "object") {
|
|
504
|
-
const additionalTypeError = validateParameterType(key, value, additionalProperties);
|
|
505
|
-
if (additionalTypeError) errors.push(additionalTypeError);
|
|
506
|
-
continue;
|
|
507
|
-
}
|
|
508
|
-
errors.push(`Unknown parameter: ${key}`);
|
|
509
|
-
continue;
|
|
510
|
-
}
|
|
511
|
-
const typeError = validateParameterType(key, value, paramSchema);
|
|
512
|
-
if (typeError) errors.push(typeError);
|
|
513
|
-
}
|
|
514
|
-
return errors;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Validate parameters and return a structured result.
|
|
518
|
-
*/
|
|
519
|
-
function validateToolParameters(parameters, schemaRequired, schemaProperties, additionalProperties) {
|
|
520
|
-
const errors = getValidationErrors(parameters, schemaRequired, schemaProperties, additionalProperties);
|
|
521
|
-
return {
|
|
522
|
-
isValid: errors.length === 0,
|
|
523
|
-
errors
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
//#endregion
|
|
527
527
|
//#region src/implementations/function-tool.ts
|
|
528
528
|
/**
|
|
529
529
|
* Function tool implementation
|