diff --git a/packages/ai/src/tool.ts b/packages/ai/src/tool.ts index ef272521fae..fc2e9f5ff3f 100644 --- a/packages/ai/src/tool.ts +++ b/packages/ai/src/tool.ts @@ -279,16 +279,19 @@ const standardJsonSchema = (schema: StandardSchemaV1): JsonSchema.JsonSchema => return body } +// A throwing or rejecting `validate` is an input error the model can see, not a defect. const validateStandard = (schema: StandardSchemaV1, value: unknown) => - Effect.suspend(() => { - const result = schema["~standard"].validate(value) - return result instanceof Promise ? Effect.promise(() => result) : Effect.succeed(result) - }).pipe( + Effect.try({ try: () => schema["~standard"].validate(value), catch: toError }).pipe( + Effect.flatMap((result) => + result instanceof Promise ? Effect.tryPromise({ try: () => result, catch: toError }) : Effect.succeed(result), + ), Effect.flatMap((result) => result.issues === undefined ? Effect.succeed(result.value) : Effect.fail(new Error(formatIssues(result.issues))), ), ) +const toError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause))) + const formatIssues = (issues: ReadonlyArray) => issues .map((issue) => { diff --git a/packages/ai/test/standard-schema.test.ts b/packages/ai/test/standard-schema.test.ts index 45498b37d03..bb8d17f93c6 100644 --- a/packages/ai/test/standard-schema.test.ts +++ b/packages/ai/test/standard-schema.test.ts @@ -67,6 +67,36 @@ describe("Standard Schema tool input", () => { }) }) + test("returns a throwing or rejecting validate as a tool error", async () => { + const schema = (validate: () => never) => ({ + "~standard": { + version: 1 as const, + vendor: "test", + validate, + jsonSchema: { input: () => ({ type: "object" }), output: () => ({ type: "object" }) }, + }, + }) + const results = await Promise.all( + [ + schema(() => { + throw new Error("sync boom") + }), + schema(() => Promise.reject(new Error("async boom")) as never), + ].map((input) => + Effect.runPromise( + ToolRuntime.dispatch( + [tool({ name: "boom", description: "Boom.", input, execute: () => Effect.succeed("ok") })], + call("boom", {}), + ), + ), + ), + ) + expect(results.map((result) => result.result)).toEqual([ + { type: "error", value: "Invalid tool input: sync boom" }, + { type: "error", value: "Invalid tool input: async boom" }, + ]) + }) + test("fails clearly when the schema has no JSON Schema export", () => { expect(() => tool({ name: "weather", description: "Get the weather.", input: v.object({ city: v.string() }) }),