diff --git a/src/internals/helpers/object.ts b/src/internals/helpers/object.ts index a389158b..834ec8eb 100644 --- a/src/internals/helpers/object.ts +++ b/src/internals/helpers/object.ts @@ -65,7 +65,7 @@ export function getPropStrict(target: NonNullable, path: string): any { export function getProp( target: unknown, - paths: readonly (string | symbol)[], + paths: readonly (keyof any)[], defaultValue: any = undefined, ) { let value: any = target; diff --git a/src/tools/base.ts b/src/tools/base.ts index d8b4c984..61bde9c0 100644 --- a/src/tools/base.ts +++ b/src/tools/base.ts @@ -203,6 +203,8 @@ export abstract class Tool< } run(input: ToolInputRaw, options?: TRunOptions): Promise { + input = shallowCopy(input); + return RunContext.enter( this, { signal: options?.signal, params: [input, options] as const }, @@ -211,6 +213,7 @@ export abstract class Tool< let errorPropagated = false; try { + this.preprocessInput(input); await this.assertInput(input); const output = await new Retryable({ @@ -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, diff --git a/src/tools/weather/openMeteo.ts b/src/tools/weather/openMeteo.ts index c8b9d0d7..54ff0d5f 100644 --- a/src/tools/weather/openMeteo.ts +++ b/src/tools/weather/openMeteo.ts @@ -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; @@ -102,6 +103,20 @@ export class OpenMeteoTool extends Tool< this.register(); } + protected preprocessInput(rawInput: unknown) { + super.preprocessInput(rawInput); + + const fixDate = (key: keyof ToolInput) => { + 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, _options: BaseToolRunOptions | undefined,