Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/plugin/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function tool<Args extends z.ZodRawShape>(input: {
args: Args
execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext): Promise<ToolResult>
}) {
const invalid = Object.entries(input.args).find(
(entry) => typeof entry[1] !== "object" || entry[1] === null || !("_zod" in entry[1]),
)
if (invalid) throw new TypeError(`Invalid tool argument "${invalid[0]}": args must contain Zod schemas`)
return input
}
tool.schema = z
Expand Down
27 changes: 27 additions & 0 deletions packages/plugin/test/tool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, test } from "bun:test"
import { tool } from "../src/tool"

describe("tool", () => {
test("rejects plain-object argument definitions with a clear error", () => {
expect(() =>
tool({
description: "invalid tool",
args: {
// @ts-expect-error Verify the runtime diagnostic for JavaScript callers.
foo: "string",
},
execute: async () => "ok",
}),
).toThrow('Invalid tool argument "foo": args must contain Zod schemas')
})

test("accepts Zod argument definitions", () => {
expect(
tool({
description: "valid tool",
args: { foo: tool.schema.string() },
execute: async ({ foo }) => foo,
}),
).toBeDefined()
})
})
Loading