The Open Framework for Building Agents
fix(eve): build tools with Zod 3 input schemas (#1888)
vercel-gh-bot-3[bot]52000dd7fbad
diff --git a/.changeset/support-zod-three-tool-schemas.md b/.changeset/support-zod-three-tool-schemas.mdnew file mode 100644index 000000000..85426f199--- /dev/null+++ b/.changeset/support-zod-three-tool-schemas.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Build tool input schemas authored with Zod 3 instead of crashing during JSON Schema conversion.diff --git a/packages/eve/extension-contracts/compatibility/dynamicTool/v10.ts b/packages/eve/extension-contracts/compatibility/dynamicTool/v10.tsnew file mode 100644index 000000000..81c198d1a--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/dynamicTool/v10.ts@@ -0,0 +1,12 @@+import { defineDynamic, defineTool } from "#public/tools/index.js";++export default defineDynamic({+ events: {+ "session.started": (_event, ctx) =>+ defineTool({+ description: "Return the active session identifier.",+ inputSchema: { type: "object", properties: {} },+ execute: () => ({ sessionId: ctx.session.id }),+ }),+ },+});diff --git a/packages/eve/extension-contracts/compatibility/tool/v9.ts b/packages/eve/extension-contracts/compatibility/tool/v9.tsnew file mode 100644index 000000000..35c4c7be1--- /dev/null+++ b/packages/eve/extension-contracts/compatibility/tool/v9.ts@@ -0,0 +1,13 @@+import { defineTool } from "#public/tools/index.js";++export default defineTool({+ description: "Look up a report.",+ inputSchema: {+ type: "object",+ properties: { reportId: { type: "string" } },+ required: ["reportId"],+ },+ execute(input) {+ return { reportId: input.reportId };+ },+});diff --git a/packages/eve/extension-contracts/reports/dynamicTool/v11.json b/packages/eve/extension-contracts/reports/dynamicTool/v11.jsonnew file mode 100644index 000000000..405bbe3ce--- /dev/null+++ b/packages/eve/extension-contracts/reports/dynamicTool/v11.json@@ -0,0 +1,13 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "dynamicTool",+ "epoch": 11,+ "sha256": "4a17ba3bcd88e6983494763d82c094956b95fe6014babbf37c2ba5c8222d8e19",+ "exports": [+ "DynamicToolEntry",+ "DynamicToolEvents",+ "DynamicToolResult",+ "DynamicToolSet",+ "defineDynamic"+ ]+}diff --git a/packages/eve/extension-contracts/reports/tool/v10.json b/packages/eve/extension-contracts/reports/tool/v10.jsonnew file mode 100644index 000000000..ab718cf27--- /dev/null+++ b/packages/eve/extension-contracts/reports/tool/v10.json@@ -0,0 +1,22 @@+{+ "kind": "eve-extension-capability-contract",+ "capability": "tool",+ "epoch": 10,+ "sha256": "7dc0670665724e00b382728efe582bb0440ed08d96ef80fea64a9b154fcd7877",+ "exports": [+ "defineBashTool",+ "defineGlobTool",+ "defineGrepTool",+ "defineReadFileTool",+ "defineTool",+ "defineWriteFileTool",+ "disableTool",+ "experimental_workflow",+ "isDisabledToolSentinel",+ "isExperimentalWorkflowToolDefinition",+ "toolOutput",+ "toolOutputPart",+ "toolResultFrom",+ "webSearch"+ ]+}diff --git a/packages/eve/src/compiler/extension-compatibility.ts b/packages/eve/src/compiler/extension-compatibility.tsindex 771caa464..910f46948 100644--- a/packages/eve/src/compiler/extension-compatibility.ts+++ b/packages/eve/src/compiler/extension-compatibility.ts@@ -21,8 +21,8 @@ interface ExtensionCapabilityContract {const EXTENSION_CAPABILITY_CONTRACTS = {extension: { current: 1, supported: [1], dropped: {} },- tool: { current: 9, supported: [1, 2, 3, 4, 5, 6, 7, 8, 9], dropped: {} },- dynamicTool: { current: 10, supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dropped: {} },+ tool: { current: 10, supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dropped: {} },+ dynamicTool: { current: 11, supported: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dropped: {} },connection: { current: 3, supported: [1, 2, 3], dropped: {} },hook: { current: 8, supported: [1, 2, 3, 4, 5, 6, 7, 8], dropped: {} },skill: { current: 1, supported: [1], dropped: {} },diff --git a/packages/eve/src/internal/authored-definition/schema-backed.test.ts b/packages/eve/src/internal/authored-definition/schema-backed.test.tsindex 406b900ec..7cfc68903 100644--- a/packages/eve/src/internal/authored-definition/schema-backed.test.ts+++ b/packages/eve/src/internal/authored-definition/schema-backed.test.ts@@ -1,4 +1,5 @@import { describe, expect, it } from "vitest";+import { z as z3 } from "zod/v3";import { z } from "#compiled/zod/index.js";import {@@ -33,6 +34,27 @@ describe("normalizeToolDefinition", () => {expect(typeof entry.definition.execute).toBe("function");});+ it("normalizes a tool with a Zod 3 input schema", () => {+ const tool = defineTool({+ description: "Gets weather for a city.",+ inputSchema: z3.object({ city: z3.string() }),+ execute(input) {+ return input.city;+ },+ });++ const entry = normalizeToolDefinition(tool, FAILURE_MESSAGE);++ expect(entry.kind).toBe("tool");+ if (entry.kind !== "tool") throw new Error("expected tool kind");+ expect(entry.definition.inputSchema).toEqual({+ additionalProperties: false,+ properties: { city: { type: "string" } },+ required: ["city"],+ type: "object",+ });+ });+it("returns a disabled entry for a disableTool sentinel", () => {const sentinel = disableTool();diff --git a/packages/eve/src/public/definitions/exact.test.ts b/packages/eve/src/public/definitions/exact.test.tsindex dce266d47..066ae8ff2 100644--- a/packages/eve/src/public/definitions/exact.test.ts+++ b/packages/eve/src/public/definitions/exact.test.ts@@ -1,4 +1,5 @@import { describe, expect, expectTypeOf, it } from "vitest";+import { z as z3 } from "zod/v3";import { z } from "#compiled/zod/index.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";@@ -81,6 +82,19 @@ describe("definition helper exact inputs", () => {>();});+ it("infers tool input from Zod 3 schemas", () => {+ const tool = defineTool({+ description: "Fetch current weather for a city.",+ inputSchema: z3.object({ city: z3.string() }),+ execute(input) {+ expectTypeOf(input.city).toEqualTypeOf<string>();+ return input.city;+ },+ });++ expectTypeOf<ReturnType<typeof tool.execute>>().toEqualTypeOf<string>();+ });+it("keeps the public hook event map aligned with runtime stream events", () => {expectTypeOf<keyof HookEventMap>().toEqualTypeOf<UnstampedMessageStreamEvent["type"]>();});diff --git a/packages/eve/src/public/definitions/tool.ts b/packages/eve/src/public/definitions/tool.tsindex 449b67641..8a7fca566 100644--- a/packages/eve/src/public/definitions/tool.ts+++ b/packages/eve/src/public/definitions/tool.ts@@ -1,4 +1,7 @@-import type { StandardJSONSchemaV1 } from "#compiled/@standard-schema/spec/index.js";+import type {+ StandardJSONSchemaV1,+ StandardSchemaV1,+} from "#compiled/@standard-schema/spec/index.js";import { stampDefinitionKey } from "#public/tool-result-narrowing.js";import type { PublicToolDefinition, ToolModelOutput } from "#shared/tool-definition.js";@@ -161,7 +164,7 @@ type ToolDefinitionWithExecuteReturn<TInput, TOutput, TReturn> = ToolDefinition<* stamps a brand that lifecycle code validates; it rejects raw object literals.*/export function defineTool<- TInputSchema extends StandardJSONSchemaV1<unknown, unknown>,+ TInputSchema extends StandardSchemaV1<unknown, unknown> | StandardJSONSchemaV1<unknown, unknown>,TOutputSchema extends StandardJSONSchemaV1<unknown, unknown>,TReturn extends| Promise<StandardJSONSchemaV1.InferOutput<TOutputSchema>>@@ -171,29 +174,29 @@ export function defineTool<description: ToolDefinition<unknown, unknown>["description"];inputSchema: TInputSchema;outputSchema: TOutputSchema;- execute(input: StandardJSONSchemaV1.InferOutput<TInputSchema>, ctx: ToolContext): TReturn;- approval?: ToolDefinition<StandardJSONSchemaV1.InferOutput<TInputSchema>, unknown>["approval"];+ execute(input: StandardSchemaV1.InferOutput<TInputSchema>, ctx: ToolContext): TReturn;+ approval?: ToolDefinition<StandardSchemaV1.InferOutput<TInputSchema>, unknown>["approval"];toModelOutput?: ToolDefinition<unknown,StandardJSONSchemaV1.InferOutput<TOutputSchema>>["toModelOutput"];}): ToolDefinitionWithExecuteReturn<- StandardJSONSchemaV1.InferOutput<TInputSchema>,+ StandardSchemaV1.InferOutput<TInputSchema>,StandardJSONSchemaV1.InferOutput<TOutputSchema>,TReturn>;export function defineTool<- TSchema extends StandardJSONSchemaV1<unknown, unknown>,+ TSchema extends StandardSchemaV1<unknown, unknown> | StandardJSONSchemaV1<unknown, unknown>,TReturn,>(definition: {description: ToolDefinition<unknown, unknown>["description"];inputSchema: TSchema;outputSchema?: JsonObject;- execute(input: StandardJSONSchemaV1.InferOutput<TSchema>, ctx: ToolContext): TReturn;- approval?: ToolDefinition<StandardJSONSchemaV1.InferOutput<TSchema>, unknown>["approval"];+ execute(input: StandardSchemaV1.InferOutput<TSchema>, ctx: ToolContext): TReturn;+ approval?: ToolDefinition<StandardSchemaV1.InferOutput<TSchema>, unknown>["approval"];toModelOutput?: ToolDefinition<unknown, ToolOutputFromExecuteReturn<TReturn>>["toModelOutput"];}): ToolDefinitionWithExecuteReturn<- StandardJSONSchemaV1.InferOutput<TSchema>,+ StandardSchemaV1.InferOutput<TSchema>,ToolOutputFromExecuteReturn<TReturn>,TReturn>;diff --git a/packages/eve/src/shared/tool-definition.ts b/packages/eve/src/shared/tool-definition.tsindex 6b65755b0..53d171c3a 100644--- a/packages/eve/src/shared/tool-definition.ts+++ b/packages/eve/src/shared/tool-definition.ts@@ -1,5 +1,8 @@import type { ToolExecutionOptions } from "ai";-import type { StandardJSONSchemaV1 } from "#compiled/@standard-schema/spec/index.js";+import type {+ StandardJSONSchemaV1,+ StandardSchemaV1,+} from "#compiled/@standard-schema/spec/index.js";import type { JsonObject } from "#shared/json.js";/**@@ -31,6 +34,7 @@ export interface InternalToolDefinition extends ToolDefinitionBase {}export type PublicToolInputSchema<TInput = unknown> =+ | StandardSchemaV1<unknown, TInput>| StandardJSONSchemaV1<unknown, TInput>| JsonObject;diff --git a/packages/eve/src/shared/tool-schema.test.ts b/packages/eve/src/shared/tool-schema.test.tsindex 048e8136e..3bee57a27 100644--- a/packages/eve/src/shared/tool-schema.test.ts+++ b/packages/eve/src/shared/tool-schema.test.ts@@ -1,6 +1,7 @@import { asSchema } from "ai";import { describe, expect, it } from "vitest";import { z } from "zod";+import { z as z3 } from "zod/v3";import {UNSPECIFIED_INPUT_SCHEMA,@@ -142,6 +143,41 @@ describe("ToolSchema", () => {});});+ it("serializes and rehydrates a Zod 3 input schema", async () => {+ const source = z3.object({ city: z3.string() });++ expect(serializeInputSchema(source)).toEqual({+ additionalProperties: false,+ properties: { city: { type: "string" } },+ required: ["city"],+ type: "object",+ });++ const validate = asSchema(toInputSchema(source)).validate;+ await expect(validate?.({ city: "San Francisco" })).resolves.toMatchObject({ success: true });+ await expect(validate?.({ city: 42 })).resolves.toMatchObject({ success: false });+ });++ it("rejects a Zod 3 output schema with an actionable error", () => {+ expect(() => serializeOutputSchema(z3.object({ city: z3.string() }))).toThrow(+ "Zod 3 cannot emit an output JSON Schema. Upgrade to Zod 4 or provide a plain JSON Schema object.",+ );+ });++ it("explains when a Standard Schema cannot emit JSON Schema", () => {+ const schema = {+ "~standard": {+ validate: (value: unknown) => ({ value }),+ vendor: "example",+ version: 1 as const,+ },+ };++ expect(() => serializeInputSchema(schema)).toThrow(+ 'Standard Schema vendor "example" does not support JSON Schema conversion. Provide a Standard Schema implementation with JSON Schema conversion or a plain JSON Schema object.',+ );+ });+it("strips the $schema version key from serialized data", () => {expect(serializeInputSchema({diff --git a/packages/eve/src/shared/tool-schema.ts b/packages/eve/src/shared/tool-schema.tsindex 540b7ea9c..8dc3f9a15 100644--- a/packages/eve/src/shared/tool-schema.ts+++ b/packages/eve/src/shared/tool-schema.ts@@ -1,3 +1,5 @@+import { asSchema } from "ai";+import type {StandardJSONSchemaV1,StandardSchemaV1,@@ -16,11 +18,12 @@ export type ToolSchema<Input = unknown, Output = Input> = StandardSchemaV1<InputStandardJSONSchemaV1<Input, Output>;/**- * Any value accepted at a schema boundary: a live {@link ToolSchema}, a- * JSON-Schema-capable Standard Schema, or plain JSON Schema data. Plain data- * is runtime-validated by {@link parseJsonObject} during conversion.+ * Any value accepted at a schema boundary: a live {@link ToolSchema}, a Zod 3+ * Standard Schema, a JSON-Schema-capable Standard Schema, or plain JSON Schema+ * data. Plain data is runtime-validated by {@link parseJsonObject} during+ * conversion.*/-export type ToolSchemaSource = StandardJSONSchemaV1 | Record<string, unknown>;+export type ToolSchemaSource = StandardJSONSchemaV1 | StandardSchemaV1 | Record<string, unknown>;type SchemaDirection = "input" | "output";@@ -88,14 +91,9 @@ export function serializeOutputSchema<T extends ToolSchemaSource | null | undefi* Standard Schema validation plus JSON Schema emission.*/export function isToolSchema(value: unknown): value is ToolSchema {- if (typeof value !== "object" || value === null || !("~standard" in value)) {- return false;- }-- const standard = (value as Record<string, unknown>)["~standard"];- if (typeof standard !== "object" || standard === null) return false;+ const properties = getStandardSchemaProperties(value);+ if (properties === undefined) return false;- const properties = standard as Record<string, unknown>;const jsonSchema = properties.jsonSchema;return (typeof properties.validate === "function" &&@@ -195,13 +193,48 @@ function serializeSchema(* wire form.*/function toJsonObject(source: ToolSchemaSource, direction: SchemaDirection): JsonObject {- const raw = isStandardJsonSchema(source)- ? parseJsonObject(source["~standard"].jsonSchema[direction]({ target: JSON_SCHEMA_TARGET }))- : parseJsonObject(source);+ const standard = getStandardSchemaProperties(source);+ const jsonSchema = standard?.jsonSchema;+ const emit =+ typeof jsonSchema === "object" && jsonSchema !== null+ ? (jsonSchema as Record<string, unknown>)[direction]+ : undefined;+ const vendor = typeof standard?.vendor === "string" ? standard.vendor : "unknown";+ if (standard !== undefined && typeof emit !== "function" && vendor === "zod") {+ if (direction === "input") {+ const schema = asSchema(source as Parameters<typeof asSchema>[0]);+ const { $schema: _schemaVersion, ...canonical } = parseJsonObject(schema.jsonSchema);+ return canonical;+ }++ throw new Error(+ "Zod 3 cannot emit an output JSON Schema. Upgrade to Zod 4 or provide a plain JSON Schema object.",+ );+ }++ if (standard !== undefined && typeof emit !== "function") {+ throw new Error(+ `Standard Schema vendor "${vendor}" does not support JSON Schema conversion. Provide a Standard Schema implementation with JSON Schema conversion or a plain JSON Schema object.`,+ );+ }++ const raw =+ standard === undefined+ ? parseJsonObject(source)+ : parseJsonObject(+ (emit as StandardJSONSchemaV1.Converter[SchemaDirection])({+ target: JSON_SCHEMA_TARGET,+ }),+ );const { $schema: _schemaVersion, ...canonical } = raw;return canonical;}-function isStandardJsonSchema(value: unknown): value is StandardJSONSchemaV1 {- return value !== null && typeof value === "object" && "~standard" in value;+function getStandardSchemaProperties(value: unknown): Record<string, unknown> | undefined {+ if (typeof value !== "object" || value === null || !("~standard" in value)) return undefined;++ const standard = (value as Record<string, unknown>)["~standard"];+ return typeof standard === "object" && standard !== null+ ? (standard as Record<string, unknown>)+ : undefined;}