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

chore: type #8

Merged
merged 1 commit into from
Oct 21, 2024
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
32 changes: 17 additions & 15 deletions src/tools/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import type { IContext } from "../context/context.ts";
type Parameters = Tool["function"]["parameters"];

// Utility type to map the "type" string to actual TypeScript types
type MapToTypes<T extends string> = T extends "string" ? string
: T extends "number" ? number
: T extends "boolean" ? boolean
type MapToTypes<T extends string> = T extends "string"
? string
: T extends "number"
? number
: T extends "boolean"
? boolean
: unknown;

// Extract required and optional parameters
Expand All @@ -18,17 +21,15 @@ type ExtractParameters<P extends Tool["function"]["parameters"]> = {
};

type RequiredParams<P extends Parameters> = {
[
K in keyof ExtractParameters<P> as K extends P["required"][number] ? K
: never
]: ExtractParameters<P>[K];
[K in keyof ExtractParameters<P> as K extends P["required"][number]
? K
: never]: ExtractParameters<P>[K];
};

type OptionalParams<P extends Parameters> = {
[
K in keyof ExtractParameters<P> as K extends P["required"][number] ? never
: K
]?: ExtractParameters<P>[K];
[K in keyof ExtractParameters<P> as K extends P["required"][number]
? never
: K]?: ExtractParameters<P>[K];
};

export type ITool<P extends Parameters = Parameters> = FunctionToolType & {
Expand All @@ -37,15 +38,16 @@ export type ITool<P extends Parameters = Parameters> = FunctionToolType & {

// TODO map paramaters type to call args, it doesn't seem to be able to inferred with typescript without being explicit on the generic param
export abstract class FunctionTool<P extends Parameters = Parameters>
implements ITool<P> {
implements ITool<P>
{
name = this.constructor.name;

abstract parameters: P;
abstract description: string;
abstract call(
args: unknown, /*RequiredParams<P> & OptionalParams<P>*/
ctx: IContext,
): Promise<string>;
args: unknown /*RequiredParams<P> & OptionalParams<P>*/,
ctx: IContext
): Promise<string | null>;

toTool(): Tool {
return {
Expand Down