| 43 | } |
| 44 | |
| 45 | class ShellToolInvocation extends BaseToolInvocation< |
| 46 | ShellToolParams, |
| 47 | ToolResult |
| 48 | > { |
| 49 | constructor( |
| 50 | private readonly config: Config, |
| 51 | params: ShellToolParams, |
| 52 | private readonly allowlist: Set<string>, |
| 53 | ) { |
| 54 | super(params); |
| 55 | } |
| 56 | |
| 57 | getDescription(): string { |
| 58 | let description = `${this.params.command}`; |
| 59 | // append optional [in directory] |
| 60 | // note description is needed even if validation fails due to absolute path |
| 61 | if (this.params.directory) { |
| 62 | description += ` [in ${this.params.directory}]`; |
| 63 | } |
| 64 | // append optional (description), replacing any line breaks with spaces |
| 65 | if (this.params.description) { |
| 66 | description += ` (${this.params.description.replace(/\n/g, ' ')})`; |
| 67 | } |
| 68 | return description; |
| 69 | } |
| 70 | |
| 71 | override async shouldConfirmExecute( |
| 72 | _abortSignal: AbortSignal, |
| 73 | ): Promise<ToolCallConfirmationDetails | false> { |
| 74 | const command = stripShellWrapper(this.params.command); |
| 75 | const rootCommands = [...new Set(getCommandRoots(command))]; |
| 76 | const commandsToConfirm = rootCommands.filter( |
| 77 | (command) => !this.allowlist.has(command), |
| 78 | ); |
| 79 | |
| 80 | if (commandsToConfirm.length === 0) { |
| 81 | return false; // already approved and whitelisted |
| 82 | } |
| 83 | |
| 84 | const confirmationDetails: ToolExecuteConfirmationDetails = { |
| 85 | type: 'exec', |
| 86 | title: 'Confirm Shell Command', |
| 87 | command: this.params.command, |
| 88 | rootCommand: commandsToConfirm.join(', '), |
| 89 | onConfirm: async (outcome: ToolConfirmationOutcome) => { |
| 90 | if (outcome === ToolConfirmationOutcome.ProceedAlways) { |
| 91 | commandsToConfirm.forEach((command) => this.allowlist.add(command)); |
| 92 | } |
| 93 | }, |
| 94 | }; |
| 95 | return confirmationDetails; |
| 96 | } |
| 97 | |
| 98 | async execute( |
| 99 | signal: AbortSignal, |
| 100 | updateOutput?: (output: string) => void, |
| 101 | ): Promise<ToolResult> { |
| 102 | const strippedCommand = stripShellWrapper(this.params.command); |
nothing calls this directly
no outgoing calls
no test coverage detected