Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zod schema issue with tool #248

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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
27 changes: 22 additions & 5 deletions packages/lms-client/src/llm/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,41 @@ export const toolSchema = z.discriminatedUnion("type", [functionToolSchema]);
*
* @public
*/
export function tool<const TParameters extends Record<string, ZodSchema>>({
export function tool<const TParameters extends Record<string, { parse(input: any): any }>>({
name,
description,
parameters,
implementation,
}: {
name: string;
description: string;
/**
* The parameters of the function. Must be an with values being zod schemas.
*
* IMPORTANT
*
* The type here only requires an object with a `parse` function. This is not enough! We need an
* actual zod schema because we will need to extract the JSON schema from it.
*
* The reason we only have a `parse` function here (as oppose to actually requiring ZodType is due
* to this zod bug causing TypeScript breakage, when multiple versions of zod exist.
*
* - https://github.com/colinhacks/zod/issues/577
* - https://github.com/colinhacks/zod/issues/2697
* - https://github.com/colinhacks/zod/issues/3435
*/
parameters: TParameters;
implementation: (params: { [K in keyof TParameters]: z.infer<TParameters[K]> }) =>
| any
| Promise<any>;
implementation: (params: {
[K in keyof TParameters]: TParameters[K] extends { parse: (input: any) => infer RReturnType }
? RReturnType
: never;
}) => any | Promise<any>;
}): Tool {
return {
name,
description,
type: "function",
parametersSchema: z.object(parameters),
parametersSchema: z.object(parameters as any),
implementation: implementation as any, // Erase types
};
}
Expand Down