(
config: {
name: string;
description: string;
schema: z.ZodTypeAny;
},
handler: (
// biome-ignore lint/suspicious/noExplicitAny: deliberate, see comment above
input: any,
ctx: ChatAgentContext,
runCtx: ToolRunContext,
) => Promise<unknown>,
)
| 37 | * decide whether to retry with narrower params or move on. |
| 38 | */ |
| 39 | export function chatTool( |
| 40 | config: { |
| 41 | name: string; |
| 42 | description: string; |
| 43 | schema: z.ZodTypeAny; |
| 44 | }, |
| 45 | handler: ( |
| 46 | // biome-ignore lint/suspicious/noExplicitAny: deliberate, see comment above |
| 47 | input: any, |
| 48 | ctx: ChatAgentContext, |
| 49 | runCtx: ToolRunContext, |
| 50 | ) => Promise<unknown>, |
| 51 | ): AgentToolDefinition { |
| 52 | // biome-ignore lint/suspicious/noExplicitAny: see block comment above |
| 53 | const contract: any = defineTool({ |
| 54 | name: config.name, |
| 55 | description: config.description, |
| 56 | schema: config.schema, |
| 57 | }); |
| 58 | return contract.server( |
| 59 | async (input: unknown, runCtx: ToolRunContext) => { |
| 60 | const work = handler( |
| 61 | input, |
| 62 | runCtx.context as ChatAgentContext, |
| 63 | runCtx, |
| 64 | ); |
| 65 | let timer: NodeJS.Timeout | undefined; |
| 66 | const timeout = new Promise<never>((_, reject) => { |
| 67 | timer = setTimeout(() => { |
| 68 | reject( |
| 69 | new Error( |
| 70 | `Tool "${config.name}" timed out after ${TOOL_TIMEOUT_MS / 1000}s`, |
| 71 | ), |
| 72 | ); |
| 73 | }, TOOL_TIMEOUT_MS); |
| 74 | }); |
| 75 | try { |
| 76 | return await Promise.race([work, timeout]); |
| 77 | } finally { |
| 78 | if (timer) clearTimeout(timer); |
| 79 | } |
| 80 | }, |
| 81 | ) as AgentToolDefinition; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Cap an array result to `max` items and append a truncation marker so the |
no test coverage detected