diff options
| author | joshualitt <[email protected]> | 2025-08-14 13:28:33 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-14 20:28:33 +0000 |
| commit | 48af0456c1883834a83ae74281f0c871129779d8 (patch) | |
| tree | 3a6c5ef4830c638cd9a5049f8f6214e6d565fff5 /packages/core/src/tools/tool-registry.ts | |
| parent | 5c5fc89eb16afb65a5bbcb30e3bc576ed55e66b8 (diff) | |
feat(core): Migrate web-search, write-file, and discovered-tool. (#6188)
Co-authored-by: Jacob Richman <[email protected]>
Diffstat (limited to 'packages/core/src/tools/tool-registry.ts')
| -rw-r--r-- | packages/core/src/tools/tool-registry.ts | 104 |
1 files changed, 70 insertions, 34 deletions
diff --git a/packages/core/src/tools/tool-registry.ts b/packages/core/src/tools/tool-registry.ts index 416ee99e..ff155679 100644 --- a/packages/core/src/tools/tool-registry.ts +++ b/packages/core/src/tools/tool-registry.ts @@ -5,7 +5,14 @@ */ import { FunctionDeclaration } from '@google/genai'; -import { AnyDeclarativeTool, Kind, ToolResult, BaseTool } from './tools.js'; +import { + AnyDeclarativeTool, + Kind, + ToolResult, + BaseDeclarativeTool, + BaseToolInvocation, + ToolInvocation, +} from './tools.js'; import { Config } from '../config/config.js'; import { spawn } from 'node:child_process'; import { StringDecoder } from 'node:string_decoder'; @@ -15,46 +22,29 @@ import { parse } from 'shell-quote'; type ToolParams = Record<string, unknown>; -export class DiscoveredTool extends BaseTool<ToolParams, ToolResult> { +class DiscoveredToolInvocation extends BaseToolInvocation< + ToolParams, + ToolResult +> { constructor( private readonly config: Config, - name: string, - override readonly description: string, - override readonly parameterSchema: Record<string, unknown>, + private readonly toolName: string, + params: ToolParams, ) { - const discoveryCmd = config.getToolDiscoveryCommand()!; - const callCommand = config.getToolCallCommand()!; - description += ` - -This tool was discovered from the project by executing the command \`${discoveryCmd}\` on project root. -When called, this tool will execute the command \`${callCommand} ${name}\` on project root. -Tool discovery and call commands can be configured in project or user settings. - -When called, the tool call command is executed as a subprocess. -On success, tool output is returned as a json string. -Otherwise, the following information is returned: + super(params); + } -Stdout: Output on stdout stream. Can be \`(empty)\` or partial. -Stderr: Output on stderr stream. Can be \`(empty)\` or partial. -Error: Error or \`(none)\` if no error was reported for the subprocess. -Exit Code: Exit code or \`(none)\` if terminated by signal. -Signal: Signal number or \`(none)\` if no signal was received. -`; - super( - name, - name, - description, - Kind.Other, - parameterSchema, - false, // isOutputMarkdown - false, // canUpdateOutput - ); + getDescription(): string { + return `Calling discovered tool: ${this.toolName}`; } - async execute(params: ToolParams): Promise<ToolResult> { + async execute( + _signal: AbortSignal, + _updateOutput?: (output: string) => void, + ): Promise<ToolResult> { const callCommand = this.config.getToolCallCommand()!; - const child = spawn(callCommand, [this.name]); - child.stdin.write(JSON.stringify(params)); + const child = spawn(callCommand, [this.toolName]); + child.stdin.write(JSON.stringify(this.params)); child.stdin.end(); let stdout = ''; @@ -124,6 +114,52 @@ Signal: Signal number or \`(none)\` if no signal was received. } } +export class DiscoveredTool extends BaseDeclarativeTool< + ToolParams, + ToolResult +> { + constructor( + private readonly config: Config, + name: string, + override readonly description: string, + override readonly parameterSchema: Record<string, unknown>, + ) { + const discoveryCmd = config.getToolDiscoveryCommand()!; + const callCommand = config.getToolCallCommand()!; + description += ` + +This tool was discovered from the project by executing the command \`${discoveryCmd}\` on project root. +When called, this tool will execute the command \`${callCommand} ${name}\` on project root. +Tool discovery and call commands can be configured in project or user settings. + +When called, the tool call command is executed as a subprocess. +On success, tool output is returned as a json string. +Otherwise, the following information is returned: + +Stdout: Output on stdout stream. Can be \`(empty)\` or partial. +Stderr: Output on stderr stream. Can be \`(empty)\` or partial. +Error: Error or \`(none)\` if no error was reported for the subprocess. +Exit Code: Exit code or \`(none)\` if terminated by signal. +Signal: Signal number or \`(none)\` if no signal was received. +`; + super( + name, + name, + description, + Kind.Other, + parameterSchema, + false, // isOutputMarkdown + false, // canUpdateOutput + ); + } + + protected createInvocation( + params: ToolParams, + ): ToolInvocation<ToolParams, ToolResult> { + return new DiscoveredToolInvocation(this.config, this.name, params); + } +} + export class ToolRegistry { private tools: Map<string, AnyDeclarativeTool> = new Map(); private config: Config; |
