fix persistent terminal id?

This commit is contained in:
Andrew Pareles 2025-04-23 19:32:37 -07:00
parent 8f09a94c78
commit 1a45baa23e
3 changed files with 26 additions and 27 deletions

View file

@ -92,7 +92,7 @@ const validateRecursiveParamStr = (paramsUnknown: unknown) => {
}
const validateProposedTerminalId = (terminalIdUnknown: unknown) => {
if (!terminalIdUnknown) return '1'
if (!terminalIdUnknown) throw new Error(`A value for terminalID must be specified, but the value was "${terminalIdUnknown}"`)
const terminalId = terminalIdUnknown + ''
return terminalId
}
@ -251,19 +251,19 @@ export class ToolsService implements IToolsService {
// ---
run_command: (params: RawToolParamsObj) => {
const { command: commandUnknown, terminal_id: terminalIdUnknown } = params;
const { command: commandUnknown, persistent_terminal_id: terminalIdUnknown } = params;
const command = validateStr('command', commandUnknown);
const proposedTerminalId = terminalIdUnknown ? validateProposedTerminalId(terminalIdUnknown) : null;
return { command, bgTerminalId: proposedTerminalId };
const persistentTerminalId = terminalIdUnknown ? validateProposedTerminalId(terminalIdUnknown) : null;
return { command, persistentTerminalId };
},
open_persistent_terminal: (_params: RawToolParamsObj) => {
// No parameters needed; will open a new background terminal
return {};
},
kill_persistent_terminal: (params: RawToolParamsObj) => {
const { terminal_id: terminalIdUnknown } = params;
const terminalId = validateProposedTerminalId(terminalIdUnknown);
return { terminalId };
const { persistent_terminal_id: terminalIdUnknown } = params;
const persistentTerminalId = validateProposedTerminalId(terminalIdUnknown);
return { persistentTerminalId };
},
}
@ -414,21 +414,20 @@ export class ToolsService implements IToolsService {
return { result: lintErrorsPromise, interruptTool }
},
// ---
run_command: async ({ command, bgTerminalId }) => {
const { terminalId, resPromise } = await this.terminalToolService.runCommand(command, bgTerminalId)
run_command: async ({ command, persistentTerminalId }) => {
const { terminalId, resPromise } = await this.terminalToolService.runCommand(command, persistentTerminalId)
const interruptTool = () => {
this.terminalToolService.killTerminal(terminalId)
}
return { result: resPromise, interruptTool }
},
open_persistent_terminal: async () => {
// Open a new background terminal without waiting for completion
const terminalId = await this.terminalToolService.createTerminal()
return { result: { terminalId } }
const persistentTerminalId = await this.terminalToolService.createTerminal()
return { result: { persistentTerminalId } }
},
kill_persistent_terminal: async ({ terminalId }) => {
kill_persistent_terminal: async ({ persistentTerminalId }) => {
// Close the background terminal by sending exit
await this.terminalToolService.killTerminal(terminalId)
await this.terminalToolService.killTerminal(persistentTerminalId)
return { result: {} }
},
@ -497,18 +496,18 @@ export class ToolsService implements IToolsService {
resolveReason,
result: result_,
} = result
const { bgTerminalId } = params
const { persistentTerminalId } = params
// success
if (resolveReason.type === 'done') {
const desc = bgTerminalId ? ` in terminal ${bgTerminalId}` : ''
const desc = persistentTerminalId ? ` in terminal ${persistentTerminalId}` : ''
return `Terminal command executed and finished${desc}. Result (exit code ${resolveReason.exitCode}):\n${result_}`
}
// bg command
if (bgTerminalId !== null) {
if (persistentTerminalId !== null) {
if (resolveReason.type === 'timeout') {
return `Terminal command is running in the background in terminal ${bgTerminalId}. Here were the outputs after ${MAX_TERMINAL_INACTIVE_TIME} seconds:\n${result_}`
return `Terminal command is running in the background in terminal ${persistentTerminalId}. Here were the outputs after ${MAX_TERMINAL_INACTIVE_TIME} seconds:\n${result_}`
}
}
// normal command
@ -521,11 +520,11 @@ export class ToolsService implements IToolsService {
throw new Error(`Unexpected internal error: Terminal command did not resolve with a valid reason.`)
},
open_persistent_terminal: (_params, result) => {
const { terminalId } = result;
return `Successfully created background terminal with ID ${terminalId}`;
const { persistentTerminalId } = result;
return `Successfully created background terminal with ID ${persistentTerminalId}`;
},
kill_persistent_terminal: (params, _result) => {
return `Successfully closed terminal ${params.terminalId}.`;
return `Successfully closed terminal ${params.persistentTerminalId}.`;
},
}

View file

@ -221,7 +221,7 @@ Here's an example of a good output:\n${editToolDescriptionExample}`
description: `Runs a terminal command and waits for the result (times out after ${MAX_TERMINAL_INACTIVE_TIME}s of inactivity). You can use this tool to run any command: sed, grep, etc. Do not edit any files with this tool; use edit_file instead. When working with git and other tools that open an editor (e.g. git diff), you should pipe to cat to get all results and not get stuck in vim.`,
params: {
command: { description: 'The terminal command to run.' },
bg_terminal_id: { description: 'Optional. This only applies to terminals that have been opened with open_persistent_terminal. Runs the command in the terminal with the specified ID.' },
persistent_terminal_id: { description: 'Optional. Runs the command in the persistent terminal that you created with open_persistent_terminal.' },
},
},
@ -232,8 +232,8 @@ Here's an example of a good output:\n${editToolDescriptionExample}`
},
kill_persistent_terminal: {
name: 'kill_persistent_terminal',
description: `Closes a BG terminal with the given ID.`,
params: { terminal_id: { description: `The terminal ID to interrupt and close.` } }
description: `Interrupts and closes a persistent terminal that you opened with open_persistent_terminal.`,
params: { persistent_terminal_id: { description: `The ID of the persistent terminal.` } }
}

View file

@ -46,9 +46,9 @@ export type ToolCallParams = {
'create_file_or_folder': { uri: URI, isFolder: boolean },
'delete_file_or_folder': { uri: URI, isRecursive: boolean, isFolder: boolean },
// ---
'run_command': { command: string; bgTerminalId: string | null },
'run_command': { command: string; persistentTerminalId: string | null },
'open_persistent_terminal': {},
'kill_persistent_terminal': { terminalId: string },
'kill_persistent_terminal': { persistentTerminalId: string },
}
// RESULT OF TOOL CALL
@ -66,7 +66,7 @@ export type ToolResultType = {
'delete_file_or_folder': {},
// ---
'run_command': { result: string; resolveReason: TerminalResolveReason; },
'open_persistent_terminal': { terminalId: string },
'open_persistent_terminal': { persistentTerminalId: string },
'kill_persistent_terminal': {},
}