Skip to content

Commit 7be8d10

Browse files
committed
add v4 schema support for zodFunction
1 parent 8f56474 commit 7be8d10

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/helpers/zod.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,32 @@ export function zodFunction<Parameters extends ZodType>(options: {
142142
arguments: Parameters;
143143
name: string;
144144
function: (args: zodInfer<Parameters>) => unknown;
145-
}> {
146-
// @ts-expect-error TODO
145+
}>;
146+
export function zodFunction<Parameters extends ZodTypeV4>(options: {
147+
name: string;
148+
parameters: Parameters;
149+
function?: ((args: zodInferV4<Parameters>) => unknown | Promise<unknown>) | undefined;
150+
description?: string | undefined;
151+
}): AutoParseableTool<{
152+
arguments: Parameters;
153+
name: string;
154+
function: (args: zodInferV4<Parameters>) => unknown;
155+
}>;
156+
export function zodFunction<Parameters extends ZodType | ZodTypeV4>(options: {
157+
name: string;
158+
parameters: Parameters;
159+
function?: ((args: any) => unknown | Promise<unknown>) | undefined;
160+
description?: string | undefined;
161+
}): unknown {
147162
return makeParseableTool<any>(
148163
{
149164
type: 'function',
150165
function: {
151166
name: options.name,
152-
parameters: zodToJsonSchema(options.parameters, { name: options.name }),
167+
parameters:
168+
isZodV4(options.parameters) ?
169+
toJSONSchema(options.parameters)
170+
: zodToJsonSchema(options.parameters, { name: options.name }),
153171
strict: true,
154172
...(options.description ? { description: options.description } : undefined),
155173
},

src/lib/transform.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ function ensureStrictJsonSchema(
9898

9999
const resolved = resolveRef(root, ref);
100100
if (typeof resolved === 'boolean') {
101-
throw new ValueError(`Expected \`$ref: ${ref}\` to resolve to an object schema but got boolean`);
101+
throw new Error(`Expected \`$ref: ${ref}\` to resolve to an object schema but got boolean`);
102102
}
103103
if (!isDict(resolved)) {
104-
throw new ValueError(
104+
throw new Error(
105105
`Expected \`$ref: ${ref}\` to resolve to a dictionary but got ${JSON.stringify(resolved)}`,
106106
);
107107
}
@@ -120,7 +120,7 @@ function ensureStrictJsonSchema(
120120

121121
function resolveRef(root: JSONSchema, ref: string): JSONSchemaDefinition {
122122
if (!ref.startsWith('#/')) {
123-
throw new ValueError(`Unexpected $ref format ${JSON.stringify(ref)}; Does not start with #/`);
123+
throw new Error(`Unexpected $ref format ${JSON.stringify(ref)}; Does not start with #/`);
124124
}
125125

126126
const pathParts = ref.slice(2).split('/');
@@ -156,10 +156,3 @@ function hasMoreThanNKeys(obj: Record<string, any>, n: number): boolean {
156156
}
157157
return false;
158158
}
159-
160-
class ValueError extends Error {
161-
constructor(message: string) {
162-
super(message);
163-
this.name = 'ValueError';
164-
}
165-
}

0 commit comments

Comments
 (0)