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
1 change: 1 addition & 0 deletions src/runWithTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const runWithTools = async (
!validateArgsWithZod(
args,
selectedTool.parameters.properties as any,
(selectedTool.parameters.required as string[]) ?? [],
)
) {
Logger.error(
Expand Down
18 changes: 15 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function fetchSpec(
}
}

function convertToZodSchema(schema: any): ZodTypeAny {
function convertToZodSchema(schema: any, required: string[] = []): ZodTypeAny {
switch (schema.type) {
case "string":
return z.string();
Expand All @@ -60,7 +60,14 @@ function convertToZodSchema(schema: any): ZodTypeAny {
case "object":
const properties: Record<string, ZodTypeAny> = {};
for (const key in schema.properties) {
properties[key] = convertToZodSchema(schema.properties[key]);
let property = convertToZodSchema(
schema.properties[key],
schema.properties[key].required ?? [],
);
if (!required.includes(key)) {
property = property.optional();
}
properties[key] = property;
}
return z.object(properties);
default:
Expand All @@ -71,10 +78,15 @@ function convertToZodSchema(schema: any): ZodTypeAny {
export function validateArgsWithZod(
args: any,
properties: Record<string, JSONSchema7>,
required: string[] = [],
): boolean {
const zodSchema: Record<string, ZodTypeAny> = {};
for (const key in properties) {
zodSchema[key] = convertToZodSchema(properties[key]);
let property = convertToZodSchema(properties[key]);
if (!required.includes(key)) {
property = property.optional();
}
zodSchema[key] = property;
}

const schema = z.object(zodSchema);
Expand Down