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

feat(tool): add optional input preprocessor #128

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/internals/helpers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function getPropStrict(target: NonNullable<unknown>, path: string): any {

export function getProp(
target: unknown,
paths: readonly (string | symbol)[],
paths: readonly (keyof any)[],
defaultValue: any = undefined,
) {
let value: any = target;
Expand Down
6 changes: 6 additions & 0 deletions src/tools/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export abstract class Tool<
}

run(input: ToolInputRaw<this>, options?: TRunOptions): Promise<TOutput> {
input = shallowCopy(input);

return RunContext.enter(
this,
{ signal: options?.signal, params: [input, options] as const },
Expand All @@ -211,6 +213,7 @@ export abstract class Tool<
let errorPropagated = false;

try {
this.preprocessInput(input);
await this.assertInput(input);

const output = await new Retryable({
Expand Down Expand Up @@ -340,6 +343,9 @@ export abstract class Tool<
this.validateInput(schema, input);
}

// eslint-disable-next-line unused-imports/no-unused-vars
protected preprocessInput(rawInput: unknown): void {}

protected validateInput(
schema: AnyToolSchemaLike,
rawInput: unknown,
Expand Down
15 changes: 15 additions & 0 deletions src/tools/weather/openMeteo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createURLParams } from "@/internals/fetcher.js";
import { isNullish, pick, pickBy } from "remeda";
import { Cache } from "@/cache/decoratorCache.js";
import { RunContext } from "@/context.js";
import { getProp, setProp } from "@/internals/helpers/object.js";

type ToolOptions = { apiKey?: string } & BaseToolOptions;
type ToolRunOptions = BaseToolRunOptions;
Expand Down Expand Up @@ -102,6 +103,20 @@ export class OpenMeteoTool extends Tool<
this.register();
}

protected preprocessInput(rawInput: unknown) {
super.preprocessInput(rawInput);

const fixDate = (key: keyof ToolInput<this>) => {
const value = getProp(rawInput, [key]);
if (value) {
setProp(rawInput, [key], value.substring(0, 10));
}
};

fixDate("start_date");
fixDate("end_date");
}

protected async _run(
{ location, start_date: startDate, end_date: endDate, ...input }: ToolInput<this>,
_options: BaseToolRunOptions | undefined,
Expand Down