From cfc697a96d2e716a75e1c3b7f0f34fce81abaf1e Mon Sep 17 00:00:00 2001 From: Taylor Mullen Date: Thu, 17 Apr 2025 18:06:21 -0400 Subject: [PATCH] Run `npm run format` - Also updated README.md accordingly. Part of https://b.corp.google.com/issues/411384603 --- .prettierrc.json | 10 +- README.md | 10 + eslint.config.js | 12 +- packages/cli/package.json | 70 +- packages/cli/src/config/args.ts | 4 +- packages/cli/src/config/env.ts | 64 +- packages/cli/src/core/gemini-client.ts | 714 +++--- packages/cli/src/core/gemini-stream.ts | 276 +-- packages/cli/src/core/history-updater.ts | 347 +-- packages/cli/src/core/prompts.ts | 6 +- packages/cli/src/gemini.ts | 115 +- packages/cli/src/tools/edit.tool.ts | 648 +++--- packages/cli/src/tools/glob.tool.ts | 91 +- packages/cli/src/tools/grep.tool.ts | 233 +- packages/cli/src/tools/ls.tool.ts | 60 +- packages/cli/src/tools/read-file.tool.ts | 63 +- packages/cli/src/tools/terminal.tool.ts | 1956 ++++++++++------- packages/cli/src/tools/tool-registry.ts | 88 +- packages/cli/src/tools/tools.ts | 31 +- packages/cli/src/tools/write-file.tool.ts | 52 +- packages/cli/src/ui/App.tsx | 153 +- packages/cli/src/ui/components/Footer.tsx | 22 +- packages/cli/src/ui/components/Header.tsx | 44 +- .../cli/src/ui/components/HistoryDisplay.tsx | 45 +- .../cli/src/ui/components/InputPrompt.tsx | 51 +- .../src/ui/components/LoadingIndicator.tsx | 42 +- packages/cli/src/ui/components/Tips.tsx | 25 +- .../ui/components/messages/DiffRenderer.tsx | 69 +- .../ui/components/messages/ErrorMessage.tsx | 30 +- .../ui/components/messages/GeminiMessage.tsx | 56 +- .../ui/components/messages/InfoMessage.tsx | 30 +- .../messages/ToolConfirmationMessage.tsx | 61 +- .../components/messages/ToolGroupMessage.tsx | 65 +- .../ui/components/messages/ToolMessage.tsx | 95 +- .../ui/components/messages/UserMessage.tsx | 28 +- packages/cli/src/ui/constants.ts | 35 +- packages/cli/src/ui/hooks/useGeminiStream.ts | 259 ++- .../cli/src/ui/hooks/useLoadingIndicator.ts | 100 +- packages/cli/src/ui/types.ts | 75 +- .../cli/src/ui/utils/MarkdownRenderer.tsx | 579 +++-- .../src/utils/BackgroundTerminalAnalyzer.ts | 585 ++--- packages/cli/src/utils/getFolderStructure.ts | 233 +- packages/cli/src/utils/paths.ts | 132 +- packages/cli/src/utils/schemaValidator.ts | 22 +- packages/cli/tsconfig.json | 19 +- 45 files changed, 4373 insertions(+), 3332 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index 694a6ff7..fa9699b8 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,7 +1,7 @@ { - "semi": true, - "trailingComma": "all", - "singleQuote": true, - "printWidth": 80, - "tabWidth": 2 + "semi": true, + "trailingComma": "all", + "singleQuote": true, + "printWidth": 80, + "tabWidth": 2 } diff --git a/README.md b/README.md index 9644e8cb..b84e1ded 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,13 @@ To debug the CLI application using VS Code: 2. In VS Code, use the "Attach" launch configuration (found in `.vscode/launch.json`). This configuration is set up to attach to the Node.js process listening on port 9229, which is the default port used by `--inspect-brk`. Alternatively, you can use the "Launch Program" configuration in VS Code if you prefer to launch the currently open file directly, but the "Attach" method is generally recommended for debugging the main CLI entry point. + +## Formatting + +To format the code in this project, run the following command from the root directory: + +```bash +npm run format +``` + +This command uses Prettier to format the code according to the project's style guidelines. diff --git a/eslint.config.js b/eslint.config.js index 32f4c364..917ce7e6 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -95,7 +95,11 @@ export default tseslint.config( '@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }], '@typescript-eslint/no-unused-vars': [ 'warn', - { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_' }, + { + argsIgnorePattern: '^_', + varsIgnorePattern: '^_', + caughtErrorsIgnorePattern: '^_', + }, ], 'no-cond-assign': 'error', 'no-debugger': 'error', @@ -108,12 +112,14 @@ export default tseslint.config( }, { selector: 'ThrowStatement > Literal:not([value=/^\\w+Error:/])', - message: 'Do not throw string literals or non-Error objects. Throw new Error("...") instead.', + message: + 'Do not throw string literals or non-Error objects. Throw new Error("...") instead.', }, ], 'no-unsafe-finally': 'error', 'no-unused-expressions': 'off', // Disable base rule - '@typescript-eslint/no-unused-expressions': [ // Enable TS version + '@typescript-eslint/no-unused-expressions': [ + // Enable TS version 'error', { allowShortCircuit: true, allowTernary: true }, ], diff --git a/packages/cli/package.json b/packages/cli/package.json index 7bce64e7..b5dd0c23 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,40 +1,40 @@ { - "name": "gemini-code-cli", - "version": "1.0.0", - "description": "Gemini Code CLI", - "type": "module", - "main": "dist/gemini.js", - "scripts": { - "build": "tsc", - "start": "node dist/gemini.js", + "name": "gemini-code-cli", + "version": "1.0.0", + "description": "Gemini Code CLI", + "type": "module", + "main": "dist/gemini.js", + "scripts": { + "build": "tsc", + "start": "node dist/gemini.js", "debug": "node --inspect-brk dist/gemini.js", "lint": "eslint . --ext .ts,.tsx", "format": "prettier --write ." - }, - "files": [ - "dist" - ], - "dependencies": { - "@google/genai": "^0.8.0", - "diff": "^7.0.0", - "dotenv": "^16.4.7", - "fast-glob": "^3.3.3", - "ink": "^5.2.0", - "ink-select-input": "^6.0.0", - "ink-spinner": "^5.0.0", - "ink-text-input": "^6.0.0", - "react": "^18.3.1", - "yargs": "^17.7.2" - }, - "devDependencies": { - "@types/diff": "^7.0.2", - "@types/dotenv": "^6.1.1", - "@types/node": "^20.11.24", - "@types/react": "^19.1.0", - "@types/yargs": "^17.0.32", - "typescript": "^5.3.3" - }, - "engines": { - "node": ">=18" - } + }, + "files": [ + "dist" + ], + "dependencies": { + "@google/genai": "^0.8.0", + "diff": "^7.0.0", + "dotenv": "^16.4.7", + "fast-glob": "^3.3.3", + "ink": "^5.2.0", + "ink-select-input": "^6.0.0", + "ink-spinner": "^5.0.0", + "ink-text-input": "^6.0.0", + "react": "^18.3.1", + "yargs": "^17.7.2" + }, + "devDependencies": { + "@types/diff": "^7.0.2", + "@types/dotenv": "^6.1.1", + "@types/node": "^20.11.24", + "@types/react": "^19.1.0", + "@types/yargs": "^17.0.32", + "typescript": "^5.3.3" + }, + "engines": { + "node": ">=18" + } } diff --git a/packages/cli/src/config/args.ts b/packages/cli/src/config/args.ts index 45f654db..7c4ebbc0 100644 --- a/packages/cli/src/config/args.ts +++ b/packages/cli/src/config/args.ts @@ -24,11 +24,11 @@ export async function parseArguments(): Promise { // Handle warnings for extra arguments here if (argv._ && argv._.length > 0) { console.warn( - `Warning: Additional arguments provided (${argv._.join(', ')}), but will be ignored.` + `Warning: Additional arguments provided (${argv._.join(', ')}), but will be ignored.`, ); } // Cast to the interface to ensure the structure aligns with expectations // Use `unknown` first for safer casting if types might not perfectly match return argv as unknown as CliArgs; -} \ No newline at end of file +} diff --git a/packages/cli/src/config/env.ts b/packages/cli/src/config/env.ts index 8a15fd6e..ad535b80 100644 --- a/packages/cli/src/config/env.ts +++ b/packages/cli/src/config/env.ts @@ -4,43 +4,47 @@ import * as path from 'node:path'; import process from 'node:process'; function findEnvFile(startDir: string): string | null { - // Start search from the provided directory (e.g., current working directory) - let currentDir = path.resolve(startDir); // Ensure absolute path - while (true) { - const envPath = path.join(currentDir, '.env'); - if (fs.existsSync(envPath)) { - return envPath; - } - - const parentDir = path.dirname(currentDir); - if (parentDir === currentDir || !parentDir) { - return null; - } - currentDir = parentDir; + // Start search from the provided directory (e.g., current working directory) + let currentDir = path.resolve(startDir); // Ensure absolute path + while (true) { + const envPath = path.join(currentDir, '.env'); + if (fs.existsSync(envPath)) { + return envPath; } + + const parentDir = path.dirname(currentDir); + if (parentDir === currentDir || !parentDir) { + return null; + } + currentDir = parentDir; + } } export function loadEnvironment(): void { - // Start searching from the current working directory by default - const envFilePath = findEnvFile(process.cwd()); + // Start searching from the current working directory by default + const envFilePath = findEnvFile(process.cwd()); - if (!envFilePath) { - return; - } + if (!envFilePath) { + return; + } - dotenv.config({ path: envFilePath }); + dotenv.config({ path: envFilePath }); - if (!process.env.GEMINI_API_KEY) { - console.error('Error: GEMINI_API_KEY environment variable is not set in the loaded .env file.'); - process.exit(1); - } + if (!process.env.GEMINI_API_KEY) { + console.error( + 'Error: GEMINI_API_KEY environment variable is not set in the loaded .env file.', + ); + process.exit(1); + } } export function getApiKey(): string { - loadEnvironment(); - const apiKey = process.env.GEMINI_API_KEY; - if (!apiKey) { - throw new Error('GEMINI_API_KEY is missing. Ensure loadEnvironment() was called successfully.'); - } - return apiKey; -} \ No newline at end of file + loadEnvironment(); + const apiKey = process.env.GEMINI_API_KEY; + if (!apiKey) { + throw new Error( + 'GEMINI_API_KEY is missing. Ensure loadEnvironment() was called successfully.', + ); + } + return apiKey; +} diff --git a/packages/cli/src/core/gemini-client.ts b/packages/cli/src/core/gemini-client.ts index 67812f8e..41cabdb7 100644 --- a/packages/cli/src/core/gemini-client.ts +++ b/packages/cli/src/core/gemini-client.ts @@ -1,13 +1,20 @@ import { - GenerateContentConfig, GoogleGenAI, Part, Chat, - Type, - SchemaUnion, - PartListUnion, - Content + GenerateContentConfig, + GoogleGenAI, + Part, + Chat, + Type, + SchemaUnion, + PartListUnion, + Content, } from '@google/genai'; import { getApiKey } from '../config/env.js'; import { CoreSystemPrompt } from './prompts.js'; -import { type ToolCallEvent, type ToolCallConfirmationDetails, ToolCallStatus } from '../ui/types.js'; +import { + type ToolCallEvent, + type ToolCallConfirmationDetails, + ToolCallStatus, +} from '../ui/types.js'; import process from 'node:process'; import { toolRegistry } from '../tools/tool-registry.js'; import { ToolResult } from '../tools/tools.js'; @@ -15,41 +22,45 @@ import { getFolderStructure } from '../utils/getFolderStructure.js'; import { GeminiEventType, GeminiStream } from './gemini-stream.js'; type ToolExecutionOutcome = { - callId: string; - name: string; - args: Record; - result?: ToolResult; - error?: any; - confirmationDetails?: ToolCallConfirmationDetails; + callId: string; + name: string; + args: Record; + result?: ToolResult; + error?: any; + confirmationDetails?: ToolCallConfirmationDetails; }; export class GeminiClient { - private ai: GoogleGenAI; - private defaultHyperParameters: GenerateContentConfig = { - temperature: 0, - topP: 1, - }; - private readonly MAX_TURNS = 100; + private ai: GoogleGenAI; + private defaultHyperParameters: GenerateContentConfig = { + temperature: 0, + topP: 1, + }; + private readonly MAX_TURNS = 100; - constructor() { - const apiKey = getApiKey(); - this.ai = new GoogleGenAI({ apiKey }); - } + constructor() { + const apiKey = getApiKey(); + this.ai = new GoogleGenAI({ apiKey }); + } - public async startChat(): Promise { - const tools = toolRegistry.getToolSchemas(); + public async startChat(): Promise { + const tools = toolRegistry.getToolSchemas(); - // --- Get environmental information --- - const cwd = process.cwd(); - const today = new Date().toLocaleDateString(undefined, { // Use locale-aware date formatting - weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' - }); - const platform = process.platform; + // --- Get environmental information --- + const cwd = process.cwd(); + const today = new Date().toLocaleDateString(undefined, { + // Use locale-aware date formatting + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', + }); + const platform = process.platform; - // --- Format information into a conversational multi-line string --- - const folderStructure = await getFolderStructure(cwd); - // --- End folder structure formatting ---) - const initialContextText = ` + // --- Format information into a conversational multi-line string --- + const folderStructure = await getFolderStructure(cwd); + // --- End folder structure formatting ---) + const initialContextText = ` Okay, just setting up the context for our chat. Today is ${today}. My operating system is: ${platform} @@ -57,194 +68,258 @@ I'm currently working in the directory: ${cwd} ${folderStructure} `.trim(); - const initialContextPart: Part = { text: initialContextText }; - // --- End environmental information formatting --- + const initialContextPart: Part = { text: initialContextText }; + // --- End environmental information formatting --- - try { - const chat = this.ai.chats.create({ - model: 'gemini-2.0-flash',//'gemini-2.0-flash', - config: { - systemInstruction: CoreSystemPrompt, - ...this.defaultHyperParameters, - tools, - }, - history: [ - // --- Add the context as a single part in the initial user message --- - { - role: "user", - parts: [initialContextPart] // Pass the single Part object in an array - }, - // --- Add an empty model response to balance the history --- - { - role: "model", - parts: [{ text: "Got it. Thanks for the context!" }] // A slightly more conversational model response - } - // --- End history modification --- - ], - }); - return chat; - } catch (error) { - console.error("Error initializing Gemini chat session:", error); - const message = error instanceof Error ? error.message : "Unknown error."; - throw new Error(`Failed to initialize chat: ${message}`); + try { + const chat = this.ai.chats.create({ + model: 'gemini-2.0-flash', //'gemini-2.0-flash', + config: { + systemInstruction: CoreSystemPrompt, + ...this.defaultHyperParameters, + tools, + }, + history: [ + // --- Add the context as a single part in the initial user message --- + { + role: 'user', + parts: [initialContextPart], // Pass the single Part object in an array + }, + // --- Add an empty model response to balance the history --- + { + role: 'model', + parts: [{ text: 'Got it. Thanks for the context!' }], // A slightly more conversational model response + }, + // --- End history modification --- + ], + }); + return chat; + } catch (error) { + console.error('Error initializing Gemini chat session:', error); + const message = error instanceof Error ? error.message : 'Unknown error.'; + throw new Error(`Failed to initialize chat: ${message}`); + } + } + + public addMessageToHistory(chat: Chat, message: Content): void { + const history = chat.getHistory(); + history.push(message); + this.ai.chats; + chat; + } + + public async *sendMessageStream( + chat: Chat, + request: PartListUnion, + signal?: AbortSignal, + ): GeminiStream { + let currentMessageToSend: PartListUnion = request; + let turns = 0; + + try { + while (turns < this.MAX_TURNS) { + turns++; + const resultStream = await chat.sendMessageStream({ + message: currentMessageToSend, + }); + let functionResponseParts: Part[] = []; + let pendingToolCalls: Array<{ + callId: string; + name: string; + args: Record; + }> = []; + let yieldedTextInTurn = false; + const chunksForDebug = []; + + for await (const chunk of resultStream) { + chunksForDebug.push(chunk); + if (signal?.aborted) { + const abortError = new Error( + 'Request cancelled by user during stream.', + ); + abortError.name = 'AbortError'; + throw abortError; + } + + const functionCalls = chunk.functionCalls; + if (functionCalls && functionCalls.length > 0) { + for (const call of functionCalls) { + const callId = + call.id ?? + `${call.name}-${Date.now()}-${Math.random().toString(16).slice(2)}`; + const name = call.name || 'undefined_tool_name'; + const args = (call.args || {}) as Record; + + pendingToolCalls.push({ callId, name, args }); + const evtValue: ToolCallEvent = { + type: 'tool_call', + status: ToolCallStatus.Pending, + callId, + name, + args, + resultDisplay: undefined, + confirmationDetails: undefined, + }; + yield { + type: GeminiEventType.ToolCallInfo, + value: evtValue, + }; + } + } else { + const text = chunk.text; + if (text) { + yieldedTextInTurn = true; + yield { + type: GeminiEventType.Content, + value: text, + }; + } + } } - } - public addMessageToHistory(chat: Chat, message: Content): void { - const history = chat.getHistory(); - history.push(message); - this.ai.chats - chat - } + if (pendingToolCalls.length > 0) { + const toolPromises: Promise[] = + pendingToolCalls.map(async (pendingToolCall) => { + const tool = toolRegistry.getTool(pendingToolCall.name); - public async* sendMessageStream( - chat: Chat, - request: PartListUnion, - signal?: AbortSignal - ): GeminiStream { - let currentMessageToSend: PartListUnion = request; - let turns = 0; + if (!tool) { + // Directly return error outcome if tool not found + return { + ...pendingToolCall, + error: new Error( + `Tool "${pendingToolCall.name}" not found or is not registered.`, + ), + }; + } - try { - while (turns < this.MAX_TURNS) { - turns++; - const resultStream = await chat.sendMessageStream({ message: currentMessageToSend }); - let functionResponseParts: Part[] = []; - let pendingToolCalls: Array<{ callId: string; name: string; args: Record }> = []; - let yieldedTextInTurn = false; - const chunksForDebug = []; - - for await (const chunk of resultStream) { - chunksForDebug.push(chunk); - if (signal?.aborted) { - const abortError = new Error("Request cancelled by user during stream."); - abortError.name = 'AbortError'; - throw abortError; - } - - const functionCalls = chunk.functionCalls; - if (functionCalls && functionCalls.length > 0) { - for (const call of functionCalls) { - const callId = call.id ?? `${call.name}-${Date.now()}-${Math.random().toString(16).slice(2)}`; - const name = call.name || 'undefined_tool_name'; - const args = (call.args || {}) as Record; - - pendingToolCalls.push({ callId, name, args }); - const evtValue: ToolCallEvent = { - type: 'tool_call', - status: ToolCallStatus.Pending, - callId, - name, - args, - resultDisplay: undefined, - confirmationDetails: undefined, - } - yield { - type: GeminiEventType.ToolCallInfo, - value: evtValue, - }; - } - } else { - const text = chunk.text; - if (text) { - yieldedTextInTurn = true; - yield { - type: GeminiEventType.Content, - value: text, - }; - } - } + try { + const confirmation = await tool.shouldConfirmExecute( + pendingToolCall.args, + ); + if (confirmation) { + return { + ...pendingToolCall, + confirmationDetails: confirmation, + }; } + } catch (error) { + return { + ...pendingToolCall, + error: new Error( + `Tool failed to check tool confirmation: ${error}`, + ), + }; + } - if (pendingToolCalls.length > 0) { - const toolPromises: Promise[] = pendingToolCalls.map(async pendingToolCall => { - const tool = toolRegistry.getTool(pendingToolCall.name); + try { + const result = await tool.execute(pendingToolCall.args); + return { ...pendingToolCall, result }; + } catch (error) { + return { + ...pendingToolCall, + error: new Error(`Tool failed to execute: ${error}`), + }; + } + }); + const toolExecutionOutcomes: ToolExecutionOutcome[] = + await Promise.all(toolPromises); - if (!tool) { - // Directly return error outcome if tool not found - return { ...pendingToolCall, error: new Error(`Tool "${pendingToolCall.name}" not found or is not registered.`) }; - } + for (const executedTool of toolExecutionOutcomes) { + const { callId, name, args, result, error, confirmationDetails } = + executedTool; - try { - const confirmation = await tool.shouldConfirmExecute(pendingToolCall.args); - if (confirmation) { - return { ...pendingToolCall, confirmationDetails: confirmation }; - } - } catch (error) { - return { ...pendingToolCall, error: new Error(`Tool failed to check tool confirmation: ${error}`) }; - } + if (error) { + const errorMessage = error?.message || String(error); + yield { + type: GeminiEventType.Content, + value: `[Error invoking tool ${name}: ${errorMessage}]`, + }; + } else if ( + result && + typeof result === 'object' && + result !== null && + 'error' in result + ) { + const errorMessage = String(result.error); + yield { + type: GeminiEventType.Content, + value: `[Error executing tool ${name}: ${errorMessage}]`, + }; + } else { + const status = confirmationDetails + ? ToolCallStatus.Confirming + : ToolCallStatus.Invoked; + const evtValue: ToolCallEvent = { + type: 'tool_call', + status, + callId, + name, + args, + resultDisplay: result?.returnDisplay, + confirmationDetails, + }; + yield { + type: GeminiEventType.ToolCallInfo, + value: evtValue, + }; + } + } - try { - const result = await tool.execute(pendingToolCall.args); - return { ...pendingToolCall, result }; - } catch (error) { - return { ...pendingToolCall, error: new Error(`Tool failed to execute: ${error}`) }; - } - }); - const toolExecutionOutcomes: ToolExecutionOutcome[] = await Promise.all(toolPromises); + pendingToolCalls = []; - for (const executedTool of toolExecutionOutcomes) { - const { callId, name, args, result, error, confirmationDetails } = executedTool; + const waitingOnConfirmations = + toolExecutionOutcomes.filter( + (outcome) => outcome.confirmationDetails, + ).length > 0; + if (waitingOnConfirmations) { + // Stop processing content, wait for user. + // TODO: Kill token processing once API supports signals. + break; + } - if (error) { - const errorMessage = error?.message || String(error); - yield { - type: GeminiEventType.Content, - value: `[Error invoking tool ${name}: ${errorMessage}]`, - }; - } else if (result && typeof result === 'object' && result !== null && 'error' in result) { - const errorMessage = String(result.error); - yield { - type: GeminiEventType.Content, - value: `[Error executing tool ${name}: ${errorMessage}]`, - }; - } else { - const status = confirmationDetails ? ToolCallStatus.Confirming : ToolCallStatus.Invoked; - const evtValue: ToolCallEvent = { type: 'tool_call', status, callId, name, args, resultDisplay: result?.returnDisplay, confirmationDetails } - yield { - type: GeminiEventType.ToolCallInfo, - value: evtValue, - }; - } - } + functionResponseParts = toolExecutionOutcomes.map( + (executedTool: ToolExecutionOutcome): Part => { + const { name, result, error } = executedTool; + const output = { output: result?.llmContent }; + let toolOutcomePayload: any; - pendingToolCalls = []; + if (error) { + const errorMessage = error?.message || String(error); + toolOutcomePayload = { + error: `Invocation failed: ${errorMessage}`, + }; + console.error( + `[Turn ${turns}] Critical error invoking tool ${name}:`, + error, + ); + } else if ( + result && + typeof result === 'object' && + result !== null && + 'error' in result + ) { + toolOutcomePayload = output; + console.warn( + `[Turn ${turns}] Tool ${name} returned an error structure:`, + result.error, + ); + } else { + toolOutcomePayload = output; + } - const waitingOnConfirmations = toolExecutionOutcomes.filter(outcome => outcome.confirmationDetails).length > 0; - if (waitingOnConfirmations) { - // Stop processing content, wait for user. - // TODO: Kill token processing once API supports signals. - break; - } - - functionResponseParts = toolExecutionOutcomes.map((executedTool: ToolExecutionOutcome): Part => { - const { name, result, error } = executedTool; - const output = { "output": result?.llmContent }; - let toolOutcomePayload: any; - - if (error) { - const errorMessage = error?.message || String(error); - toolOutcomePayload = { error: `Invocation failed: ${errorMessage}` }; - console.error(`[Turn ${turns}] Critical error invoking tool ${name}:`, error); - } else if (result && typeof result === 'object' && result !== null && 'error' in result) { - toolOutcomePayload = output; - console.warn(`[Turn ${turns}] Tool ${name} returned an error structure:`, result.error); - } else { - toolOutcomePayload = output; - } - - return { - functionResponse: { - name: name, - id: executedTool.callId, - response: toolOutcomePayload, - }, - }; - }); - currentMessageToSend = functionResponseParts; - } else if (yieldedTextInTurn) { - const history = chat.getHistory(); - const checkPrompt = `Analyze *only* the content and structure of your immediately preceding response (your last turn in the conversation history). Based *strictly* on that response, determine who should logically speak next: the 'user' or the 'model' (you). + return { + functionResponse: { + name: name, + id: executedTool.callId, + response: toolOutcomePayload, + }, + }; + }, + ); + currentMessageToSend = functionResponseParts; + } else if (yieldedTextInTurn) { + const history = chat.getHistory(); + const checkPrompt = `Analyze *only* the content and structure of your immediately preceding response (your last turn in the conversation history). Based *strictly* on that response, determine who should logically speak next: the 'user' or the 'model' (you). **Decision Rules (apply in order):** @@ -274,110 +349,135 @@ Respond *only* in JSON format according to the following schema. Do not include \`\`\` }`; - // Schema Idea - const responseSchema: SchemaUnion = { - type: Type.OBJECT, - properties: { - reasoning: { - type: Type.STRING, - description: "Brief explanation justifying the 'next_speaker' choice based *strictly* on the applicable rule and the content/structure of the preceding turn." - }, - next_speaker: { - type: Type.STRING, - enum: ['user', 'model'], // Enforce the choices - description: "Who should speak next based *only* on the preceding turn and the decision rules", - }, - }, - required: ['reasoning', 'next_speaker'] - }; + // Schema Idea + const responseSchema: SchemaUnion = { + type: Type.OBJECT, + properties: { + reasoning: { + type: Type.STRING, + description: + "Brief explanation justifying the 'next_speaker' choice based *strictly* on the applicable rule and the content/structure of the preceding turn.", + }, + next_speaker: { + type: Type.STRING, + enum: ['user', 'model'], // Enforce the choices + description: + 'Who should speak next based *only* on the preceding turn and the decision rules', + }, + }, + required: ['reasoning', 'next_speaker'], + }; - try { - // Use the new generateJson method, passing the history and the check prompt - const parsedResponse = await this.generateJson([...history, { role: "user", parts: [{ text: checkPrompt }] }], responseSchema); - - // Safely extract the next speaker value - const nextSpeaker: string | undefined = typeof parsedResponse?.next_speaker === 'string' ? parsedResponse.next_speaker : undefined; - - if (nextSpeaker === 'model') { - currentMessageToSend = { text: 'alright' }; // Or potentially a more meaningful continuation prompt - } else { - // 'user' should speak next, or value is missing/invalid. End the turn. - break; - } - - } catch (error) { - console.error(`[Turn ${turns}] Failed to get or parse next speaker check:`, error); - // If the check fails, assume user should speak next to avoid infinite loops - break; - } - } else { - console.warn(`[Turn ${turns}] No text or function calls received from Gemini. Ending interaction.`); - break; - } - - } - - if (turns >= this.MAX_TURNS) { - console.warn("sendMessageStream: Reached maximum tool call turns limit."); - yield { - type: GeminiEventType.Content, - value: "\n\n[System Notice: Maximum interaction turns reached. The conversation may be incomplete.]", - }; - } - - } catch (error: unknown) { - if (error instanceof Error && error.name === 'AbortError') { - console.log("Gemini stream request aborted by user."); - throw error; - } else { - console.error(`Error during Gemini stream or tool interaction:`, error); - const message = error instanceof Error ? error.message : String(error); - yield { - type: GeminiEventType.Content, - value: `\n\n[Error: An unexpected error occurred during the chat: ${message}]`, - }; - throw error; - } - } - } - - /** - * Generates structured JSON content based on conversational history and a schema. - * @param contents The conversational history (Content array) to provide context. - * @param schema The SchemaUnion defining the desired JSON structure. - * @returns A promise that resolves to the parsed JSON object matching the schema. - * @throws Throws an error if the API call fails or the response is not valid JSON. - */ - public async generateJson(contents: Content[], schema: SchemaUnion): Promise { - try { - const result = await this.ai.models.generateContent({ - model: 'gemini-2.0-flash', // Using flash for potentially faster structured output - config: { - ...this.defaultHyperParameters, - systemInstruction: CoreSystemPrompt, - responseSchema: schema, - responseMimeType: 'application/json', + try { + // Use the new generateJson method, passing the history and the check prompt + const parsedResponse = await this.generateJson( + [ + ...history, + { + role: 'user', + parts: [{ text: checkPrompt }], }, - contents: contents, // Pass the full Content array - }); + ], + responseSchema, + ); - const responseText = result.text; - if (!responseText) { - throw new Error("API returned an empty response."); - } + // Safely extract the next speaker value + const nextSpeaker: string | undefined = + typeof parsedResponse?.next_speaker === 'string' + ? parsedResponse.next_speaker + : undefined; - try { - const parsedJson = JSON.parse(responseText); - // TODO: Add schema validation if needed - return parsedJson; - } catch (parseError) { - console.error("Failed to parse JSON response:", responseText); - throw new Error(`Failed to parse API response as JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`); + if (nextSpeaker === 'model') { + currentMessageToSend = { text: 'alright' }; // Or potentially a more meaningful continuation prompt + } else { + // 'user' should speak next, or value is missing/invalid. End the turn. + break; } - } catch (error) { - console.error("Error generating JSON content:", error); - const message = error instanceof Error ? error.message : "Unknown API error."; - throw new Error(`Failed to generate JSON content: ${message}`); + } catch (error) { + console.error( + `[Turn ${turns}] Failed to get or parse next speaker check:`, + error, + ); + // If the check fails, assume user should speak next to avoid infinite loops + break; + } + } else { + console.warn( + `[Turn ${turns}] No text or function calls received from Gemini. Ending interaction.`, + ); + break; } + } + + if (turns >= this.MAX_TURNS) { + console.warn( + 'sendMessageStream: Reached maximum tool call turns limit.', + ); + yield { + type: GeminiEventType.Content, + value: + '\n\n[System Notice: Maximum interaction turns reached. The conversation may be incomplete.]', + }; + } + } catch (error: unknown) { + if (error instanceof Error && error.name === 'AbortError') { + console.log('Gemini stream request aborted by user.'); + throw error; + } else { + console.error(`Error during Gemini stream or tool interaction:`, error); + const message = error instanceof Error ? error.message : String(error); + yield { + type: GeminiEventType.Content, + value: `\n\n[Error: An unexpected error occurred during the chat: ${message}]`, + }; + throw error; + } } + } + + /** + * Generates structured JSON content based on conversational history and a schema. + * @param contents The conversational history (Content array) to provide context. + * @param schema The SchemaUnion defining the desired JSON structure. + * @returns A promise that resolves to the parsed JSON object matching the schema. + * @throws Throws an error if the API call fails or the response is not valid JSON. + */ + public async generateJson( + contents: Content[], + schema: SchemaUnion, + ): Promise { + try { + const result = await this.ai.models.generateContent({ + model: 'gemini-2.0-flash', // Using flash for potentially faster structured output + config: { + ...this.defaultHyperParameters, + systemInstruction: CoreSystemPrompt, + responseSchema: schema, + responseMimeType: 'application/json', + }, + contents: contents, // Pass the full Content array + }); + + const responseText = result.text; + if (!responseText) { + throw new Error('API returned an empty response.'); + } + + try { + const parsedJson = JSON.parse(responseText); + // TODO: Add schema validation if needed + return parsedJson; + } catch (parseError) { + console.error('Failed to parse JSON response:', responseText); + throw new Error( + `Failed to parse API response as JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`, + ); + } + } catch (error) { + console.error('Error generating JSON content:', error); + const message = + error instanceof Error ? error.message : 'Unknown API error.'; + throw new Error(`Failed to generate JSON content: ${message}`); + } + } } diff --git a/packages/cli/src/core/gemini-stream.ts b/packages/cli/src/core/gemini-stream.ts index b47eb1c6..065d261a 100644 --- a/packages/cli/src/core/gemini-stream.ts +++ b/packages/cli/src/core/gemini-stream.ts @@ -1,167 +1,175 @@ -import { ToolCallEvent } from "../ui/types.js"; +import { ToolCallEvent } from '../ui/types.js'; import { Part } from '@google/genai'; import { HistoryItem } from '../ui/types.js'; -import { handleToolCallChunk, addErrorMessageToHistory } from './history-updater.js'; +import { + handleToolCallChunk, + addErrorMessageToHistory, +} from './history-updater.js'; export enum GeminiEventType { - Content, - ToolCallInfo, + Content, + ToolCallInfo, } export interface GeminiContentEvent { - type: GeminiEventType.Content; - value: string; + type: GeminiEventType.Content; + value: string; } export interface GeminiToolCallInfoEvent { - type: GeminiEventType.ToolCallInfo; - value: ToolCallEvent; + type: GeminiEventType.ToolCallInfo; + value: ToolCallEvent; } -export type GeminiEvent = - | GeminiContentEvent - | GeminiToolCallInfoEvent; +export type GeminiEvent = GeminiContentEvent | GeminiToolCallInfoEvent; export type GeminiStream = AsyncIterable; export enum StreamingState { - Idle, - Responding, + Idle, + Responding, } interface StreamProcessorParams { - stream: GeminiStream; - signal: AbortSignal; - setHistory: React.Dispatch>; - submitQuery: (query: Part) => Promise, - getNextMessageId: () => number; - addHistoryItem: (itemData: Omit, id: number) => void; - currentToolGroupIdRef: React.MutableRefObject; + stream: GeminiStream; + signal: AbortSignal; + setHistory: React.Dispatch>; + submitQuery: (query: Part) => Promise; + getNextMessageId: () => number; + addHistoryItem: (itemData: Omit, id: number) => void; + currentToolGroupIdRef: React.MutableRefObject; } /** * Processes the Gemini stream, managing text buffering, adaptive rendering, * and delegating history updates for tool calls and errors. */ -export const processGeminiStream = async ({ // Renamed function for clarity - stream, - signal, - setHistory, - submitQuery, - getNextMessageId, - addHistoryItem, - currentToolGroupIdRef, +export const processGeminiStream = async ({ + // Renamed function for clarity + stream, + signal, + setHistory, + submitQuery, + getNextMessageId, + addHistoryItem, + currentToolGroupIdRef, }: StreamProcessorParams): Promise => { - // --- State specific to this stream processing invocation --- - let textBuffer = ''; - let renderTimeoutId: NodeJS.Timeout | null = null; - let isStreamComplete = false; - let currentGeminiMessageId: number | null = null; + // --- State specific to this stream processing invocation --- + let textBuffer = ''; + let renderTimeoutId: NodeJS.Timeout | null = null; + let isStreamComplete = false; + let currentGeminiMessageId: number | null = null; + + const render = (content: string) => { + if (currentGeminiMessageId === null) { + return; + } + setHistory((prev) => + prev.map((item) => + item.id === currentGeminiMessageId && item.type === 'gemini' + ? { ...item, text: (item.text ?? '') + content } + : item, + ), + ); + }; + // --- Adaptive Rendering Logic (nested) --- + const renderBufferedText = () => { + if (signal.aborted) { + if (renderTimeoutId) clearTimeout(renderTimeoutId); + renderTimeoutId = null; + return; + } + + const bufferLength = textBuffer.length; + let chunkSize = 0; + let delay = 50; + + if (bufferLength > 150) { + chunkSize = Math.min(bufferLength, 30); + delay = 5; + } else if (bufferLength > 30) { + chunkSize = Math.min(bufferLength, 10); + delay = 10; + } else if (bufferLength > 0) { + chunkSize = 2; + delay = 20; + } + + if (chunkSize > 0) { + const chunkToRender = textBuffer.substring(0, chunkSize); + textBuffer = textBuffer.substring(chunkSize); + render(chunkToRender); + + renderTimeoutId = setTimeout(renderBufferedText, delay); + } else { + renderTimeoutId = null; // Clear timeout ID if nothing to render + if (!isStreamComplete) { + // Buffer empty, but stream might still send data, check again later + renderTimeoutId = setTimeout(renderBufferedText, 50); + } + } + }; + + const scheduleRender = () => { + if (renderTimeoutId === null) { + renderTimeoutId = setTimeout(renderBufferedText, 0); + } + }; + + // --- Stream Processing Loop --- + try { + for await (const chunk of stream) { + if (signal.aborted) break; + + if (chunk.type === GeminiEventType.Content) { + currentToolGroupIdRef.current = null; // Reset tool group on text - const render = (content: string) => { if (currentGeminiMessageId === null) { - return; + currentGeminiMessageId = getNextMessageId(); + addHistoryItem({ type: 'gemini', text: '' }, currentGeminiMessageId); + textBuffer = ''; } - setHistory(prev => prev.map(item => - item.id === currentGeminiMessageId && item.type === 'gemini' - ? { ...item, text: (item.text ?? '') + content } - : item - )); - } - // --- Adaptive Rendering Logic (nested) --- - const renderBufferedText = () => { - if (signal.aborted) { - if (renderTimeoutId) clearTimeout(renderTimeoutId); - renderTimeoutId = null; - return; - } - - const bufferLength = textBuffer.length; - let chunkSize = 0; - let delay = 50; - - if (bufferLength > 150) { - chunkSize = Math.min(bufferLength, 30); delay = 5; - } else if (bufferLength > 30) { - chunkSize = Math.min(bufferLength, 10); delay = 10; - } else if (bufferLength > 0) { - chunkSize = 2; delay = 20; - } - - if (chunkSize > 0) { - const chunkToRender = textBuffer.substring(0, chunkSize); - textBuffer = textBuffer.substring(chunkSize); - render(chunkToRender); - - renderTimeoutId = setTimeout(renderBufferedText, delay); - } else { - renderTimeoutId = null; // Clear timeout ID if nothing to render - if (!isStreamComplete) { - // Buffer empty, but stream might still send data, check again later - renderTimeoutId = setTimeout(renderBufferedText, 50); - } - } - }; - - const scheduleRender = () => { - if (renderTimeoutId === null) { - renderTimeoutId = setTimeout(renderBufferedText, 0); - } - }; - - // --- Stream Processing Loop --- - try { - for await (const chunk of stream) { - if (signal.aborted) break; - - if (chunk.type === GeminiEventType.Content) { - currentToolGroupIdRef.current = null; // Reset tool group on text - - if (currentGeminiMessageId === null) { - currentGeminiMessageId = getNextMessageId(); - addHistoryItem({ type: 'gemini', text: '' }, currentGeminiMessageId); - textBuffer = ''; - } - textBuffer += chunk.value; - scheduleRender(); - - } else if (chunk.type === GeminiEventType.ToolCallInfo) { - if (renderTimeoutId) { // Stop rendering loop - clearTimeout(renderTimeoutId); - renderTimeoutId = null; - } - // Flush any text buffer content. - render(textBuffer); - currentGeminiMessageId = null; // End text message context - textBuffer = ''; // Clear buffer - - // Delegate history update for tool call - handleToolCallChunk( - chunk.value, - setHistory, - submitQuery, - getNextMessageId, - currentToolGroupIdRef - ); - } - } - if (signal.aborted) { - throw new Error("Request cancelled by user"); - } - } catch (error: any) { - if (renderTimeoutId) { // Ensure render loop stops on error - clearTimeout(renderTimeoutId); - renderTimeoutId = null; - } - // Delegate history update for error message - addErrorMessageToHistory(error, setHistory, getNextMessageId); - } finally { - isStreamComplete = true; // Signal stream end for render loop completion + textBuffer += chunk.value; + scheduleRender(); + } else if (chunk.type === GeminiEventType.ToolCallInfo) { if (renderTimeoutId) { - clearTimeout(renderTimeoutId); - renderTimeoutId = null; + // Stop rendering loop + clearTimeout(renderTimeoutId); + renderTimeoutId = null; } + // Flush any text buffer content. + render(textBuffer); + currentGeminiMessageId = null; // End text message context + textBuffer = ''; // Clear buffer - renderBufferedText(); // Force final render + // Delegate history update for tool call + handleToolCallChunk( + chunk.value, + setHistory, + submitQuery, + getNextMessageId, + currentToolGroupIdRef, + ); + } } -}; \ No newline at end of file + if (signal.aborted) { + throw new Error('Request cancelled by user'); + } + } catch (error: any) { + if (renderTimeoutId) { + // Ensure render loop stops on error + clearTimeout(renderTimeoutId); + renderTimeoutId = null; + } + // Delegate history update for error message + addErrorMessageToHistory(error, setHistory, getNextMessageId); + } finally { + isStreamComplete = true; // Signal stream end for render loop completion + if (renderTimeoutId) { + clearTimeout(renderTimeoutId); + renderTimeoutId = null; + } + + renderBufferedText(); // Force final render + } +}; diff --git a/packages/cli/src/core/history-updater.ts b/packages/cli/src/core/history-updater.ts index 369454ff..12dd30c0 100644 --- a/packages/cli/src/core/history-updater.ts +++ b/packages/cli/src/core/history-updater.ts @@ -1,7 +1,15 @@ -import { Part } from "@google/genai"; -import { toolRegistry } from "../tools/tool-registry.js"; -import { HistoryItem, IndividualToolCallDisplay, ToolCallEvent, ToolCallStatus, ToolConfirmationOutcome, ToolEditConfirmationDetails, ToolExecuteConfirmationDetails } from "../ui/types.js"; -import { ToolResultDisplay } from "../tools/tools.js"; +import { Part } from '@google/genai'; +import { toolRegistry } from '../tools/tool-registry.js'; +import { + HistoryItem, + IndividualToolCallDisplay, + ToolCallEvent, + ToolCallStatus, + ToolConfirmationOutcome, + ToolEditConfirmationDetails, + ToolExecuteConfirmationDetails, +} from '../ui/types.js'; +import { ToolResultDisplay } from '../tools/tools.js'; /** * Processes a tool call chunk and updates the history state accordingly. @@ -9,114 +17,160 @@ import { ToolResultDisplay } from "../tools/tools.js"; * Resides here as its primary effect is updating history based on tool events. */ export const handleToolCallChunk = ( - chunk: ToolCallEvent, - setHistory: React.Dispatch>, - submitQuery: (query: Part) => Promise, - getNextMessageId: () => number, - currentToolGroupIdRef: React.MutableRefObject + chunk: ToolCallEvent, + setHistory: React.Dispatch>, + submitQuery: (query: Part) => Promise, + getNextMessageId: () => number, + currentToolGroupIdRef: React.MutableRefObject, ): void => { - const toolDefinition = toolRegistry.getTool(chunk.name); - const description = toolDefinition?.getDescription - ? toolDefinition.getDescription(chunk.args) - : ''; - const toolDisplayName = toolDefinition?.displayName ?? chunk.name; - let confirmationDetails = chunk.confirmationDetails; - if (confirmationDetails) { - const originalConfirmationDetails = confirmationDetails; - const historyUpdatingConfirm = async (outcome: ToolConfirmationOutcome) => { - originalConfirmationDetails.onConfirm(outcome); + const toolDefinition = toolRegistry.getTool(chunk.name); + const description = toolDefinition?.getDescription + ? toolDefinition.getDescription(chunk.args) + : ''; + const toolDisplayName = toolDefinition?.displayName ?? chunk.name; + let confirmationDetails = chunk.confirmationDetails; + if (confirmationDetails) { + const originalConfirmationDetails = confirmationDetails; + const historyUpdatingConfirm = async (outcome: ToolConfirmationOutcome) => { + originalConfirmationDetails.onConfirm(outcome); - if (outcome === ToolConfirmationOutcome.Cancel) { - let resultDisplay: ToolResultDisplay | undefined; - if ('fileDiff' in originalConfirmationDetails) { - resultDisplay = { fileDiff: (originalConfirmationDetails as ToolEditConfirmationDetails).fileDiff }; - } else { - resultDisplay = `~~${(originalConfirmationDetails as ToolExecuteConfirmationDetails).command}~~`; - } - handleToolCallChunk({ ...chunk, status: ToolCallStatus.Canceled, confirmationDetails: undefined, resultDisplay, }, setHistory, submitQuery, getNextMessageId, currentToolGroupIdRef); - const functionResponse: Part = { - functionResponse: { - name: chunk.name, - response: { "error": "User rejected function call." }, - }, - } - await submitQuery(functionResponse); - } else { - const tool = toolRegistry.getTool(chunk.name) - if (!tool) { - throw new Error(`Tool "${chunk.name}" not found or is not registered.`); - } - handleToolCallChunk({ ...chunk, status: ToolCallStatus.Invoked, resultDisplay: "Executing...", confirmationDetails: undefined }, setHistory, submitQuery, getNextMessageId, currentToolGroupIdRef); - const result = await tool.execute(chunk.args); - handleToolCallChunk({ ...chunk, status: ToolCallStatus.Invoked, resultDisplay: result.returnDisplay, confirmationDetails: undefined }, setHistory, submitQuery, getNextMessageId, currentToolGroupIdRef); - const functionResponse: Part = { - functionResponse: { - name: chunk.name, - id: chunk.callId, - response: { "output": result.llmContent }, - }, - } - await submitQuery(functionResponse); - } + if (outcome === ToolConfirmationOutcome.Cancel) { + let resultDisplay: ToolResultDisplay | undefined; + if ('fileDiff' in originalConfirmationDetails) { + resultDisplay = { + fileDiff: ( + originalConfirmationDetails as ToolEditConfirmationDetails + ).fileDiff, + }; + } else { + resultDisplay = `~~${(originalConfirmationDetails as ToolExecuteConfirmationDetails).command}~~`; } - - confirmationDetails = { - ...originalConfirmationDetails, - onConfirm: historyUpdatingConfirm, + handleToolCallChunk( + { + ...chunk, + status: ToolCallStatus.Canceled, + confirmationDetails: undefined, + resultDisplay, + }, + setHistory, + submitQuery, + getNextMessageId, + currentToolGroupIdRef, + ); + const functionResponse: Part = { + functionResponse: { + name: chunk.name, + response: { error: 'User rejected function call.' }, + }, }; - } - const toolDetail: IndividualToolCallDisplay = { - callId: chunk.callId, - name: toolDisplayName, - description, - resultDisplay: chunk.resultDisplay, - status: chunk.status, - confirmationDetails: confirmationDetails, + await submitQuery(functionResponse); + } else { + const tool = toolRegistry.getTool(chunk.name); + if (!tool) { + throw new Error( + `Tool "${chunk.name}" not found or is not registered.`, + ); + } + handleToolCallChunk( + { + ...chunk, + status: ToolCallStatus.Invoked, + resultDisplay: 'Executing...', + confirmationDetails: undefined, + }, + setHistory, + submitQuery, + getNextMessageId, + currentToolGroupIdRef, + ); + const result = await tool.execute(chunk.args); + handleToolCallChunk( + { + ...chunk, + status: ToolCallStatus.Invoked, + resultDisplay: result.returnDisplay, + confirmationDetails: undefined, + }, + setHistory, + submitQuery, + getNextMessageId, + currentToolGroupIdRef, + ); + const functionResponse: Part = { + functionResponse: { + name: chunk.name, + id: chunk.callId, + response: { output: result.llmContent }, + }, + }; + await submitQuery(functionResponse); + } }; - const activeGroupId = currentToolGroupIdRef.current; - setHistory(prev => { - if (chunk.status === ToolCallStatus.Pending) { - if (activeGroupId === null) { - // Start a new tool group - const newGroupId = getNextMessageId(); - currentToolGroupIdRef.current = newGroupId; - return [ - ...prev, - { id: newGroupId, type: 'tool_group', tools: [toolDetail] } as HistoryItem - ]; - } + confirmationDetails = { + ...originalConfirmationDetails, + onConfirm: historyUpdatingConfirm, + }; + } + const toolDetail: IndividualToolCallDisplay = { + callId: chunk.callId, + name: toolDisplayName, + description, + resultDisplay: chunk.resultDisplay, + status: chunk.status, + confirmationDetails: confirmationDetails, + }; - // Add to existing tool group - return prev.map(item => - item.id === activeGroupId && item.type === 'tool_group' - ? item.tools.some(t => t.callId === toolDetail.callId) - ? item // Tool already listed as pending - : { ...item, tools: [...item.tools, toolDetail] } - : item - ); - } + const activeGroupId = currentToolGroupIdRef.current; + setHistory((prev) => { + if (chunk.status === ToolCallStatus.Pending) { + if (activeGroupId === null) { + // Start a new tool group + const newGroupId = getNextMessageId(); + currentToolGroupIdRef.current = newGroupId; + return [ + ...prev, + { + id: newGroupId, + type: 'tool_group', + tools: [toolDetail], + } as HistoryItem, + ]; + } - // Update the status of a pending tool within the active group - if (activeGroupId === null) { - // Log if an invoked tool arrives without an active group context - console.warn("Received invoked tool status without an active tool group ID:", chunk); - return prev; - } + // Add to existing tool group + return prev.map((item) => + item.id === activeGroupId && item.type === 'tool_group' + ? item.tools.some((t) => t.callId === toolDetail.callId) + ? item // Tool already listed as pending + : { ...item, tools: [...item.tools, toolDetail] } + : item, + ); + } - return prev.map(item => - item.id === activeGroupId && item.type === 'tool_group' - ? { - ...item, - tools: item.tools.map(t => - t.callId === toolDetail.callId - ? { ...t, ...toolDetail, status: chunk.status } // Update details & status - : t - ) - } - : item - ); - }); + // Update the status of a pending tool within the active group + if (activeGroupId === null) { + // Log if an invoked tool arrives without an active group context + console.warn( + 'Received invoked tool status without an active tool group ID:', + chunk, + ); + return prev; + } + + return prev.map((item) => + item.id === activeGroupId && item.type === 'tool_group' + ? { + ...item, + tools: item.tools.map((t) => + t.callId === toolDetail.callId + ? { ...t, ...toolDetail, status: chunk.status } // Update details & status + : t, + ), + } + : item, + ); + }); }; /** @@ -124,45 +178,58 @@ export const handleToolCallChunk = ( * it to the last non-user message or creating a new entry. */ export const addErrorMessageToHistory = ( - error: any, - setHistory: React.Dispatch>, - getNextMessageId: () => number + error: any, + setHistory: React.Dispatch>, + getNextMessageId: () => number, ): void => { - const isAbort = error.name === 'AbortError'; - const errorType = isAbort ? 'info' : 'error'; - const errorText = isAbort - ? '[Request cancelled by user]' - : `[Error: ${error.message || 'Unknown error'}]`; + const isAbort = error.name === 'AbortError'; + const errorType = isAbort ? 'info' : 'error'; + const errorText = isAbort + ? '[Request cancelled by user]' + : `[Error: ${error.message || 'Unknown error'}]`; - setHistory(prev => { - const reversedHistory = [...prev].reverse(); - // Find the last message that isn't from the user to append the error/info to - const lastBotMessageIndex = reversedHistory.findIndex(item => item.type !== 'user'); - const originalIndex = lastBotMessageIndex !== -1 ? prev.length - 1 - lastBotMessageIndex : -1; + setHistory((prev) => { + const reversedHistory = [...prev].reverse(); + // Find the last message that isn't from the user to append the error/info to + const lastBotMessageIndex = reversedHistory.findIndex( + (item) => item.type !== 'user', + ); + const originalIndex = + lastBotMessageIndex !== -1 ? prev.length - 1 - lastBotMessageIndex : -1; - if (originalIndex !== -1) { - // Append error to the last relevant message - return prev.map((item, index) => { - if (index === originalIndex) { - let baseText = ''; - // Determine base text based on item type - if (item.type === 'gemini') baseText = item.text ?? ''; - else if (item.type === 'tool_group') baseText = `Tool execution (${item.tools.length} calls)`; - else if (item.type === 'error' || item.type === 'info') baseText = item.text ?? ''; - // Safely handle potential undefined text + if (originalIndex !== -1) { + // Append error to the last relevant message + return prev.map((item, index) => { + if (index === originalIndex) { + let baseText = ''; + // Determine base text based on item type + if (item.type === 'gemini') baseText = item.text ?? ''; + else if (item.type === 'tool_group') + baseText = `Tool execution (${item.tools.length} calls)`; + else if (item.type === 'error' || item.type === 'info') + baseText = item.text ?? ''; + // Safely handle potential undefined text - const updatedText = (baseText + (baseText && !baseText.endsWith('\n') ? '\n' : '') + errorText).trim(); - // Reuse existing ID, update type and text - return { ...item, type: errorType, text: updatedText }; - } - return item; - }); - } else { - // No previous message to append to, add a new error item - return [ - ...prev, - { id: getNextMessageId(), type: errorType, text: errorText } as HistoryItem - ]; + const updatedText = ( + baseText + + (baseText && !baseText.endsWith('\n') ? '\n' : '') + + errorText + ).trim(); + // Reuse existing ID, update type and text + return { ...item, type: errorType, text: updatedText }; } - }); -}; \ No newline at end of file + return item; + }); + } else { + // No previous message to append to, add a new error item + return [ + ...prev, + { + id: getNextMessageId(), + type: errorType, + text: errorText, + } as HistoryItem, + ]; + } + }); +}; diff --git a/packages/cli/src/core/prompts.ts b/packages/cli/src/core/prompts.ts index 92a6708d..8fc623ad 100644 --- a/packages/cli/src/core/prompts.ts +++ b/packages/cli/src/core/prompts.ts @@ -1,5 +1,5 @@ -import { ReadFileTool } from "../tools/read-file.tool.js"; -import { TerminalTool } from "../tools/terminal.tool.js"; +import { ReadFileTool } from '../tools/read-file.tool.js'; +import { TerminalTool } from '../tools/terminal.tool.js'; const MEMORY_FILE_NAME = 'GEMINI.md'; @@ -91,4 +91,4 @@ assistant: I can run \`rm -rf ./temp\`. This will permanently delete the directo # Final Reminder Your core function is efficient and safe assistance. Balance extreme conciseness with the crucial need for clarity, especially regarding safety and potential system modifications. Always prioritize user control and project conventions. Never make assumptions on the contents of files; instead use the ${ReadFileTool.Name} to ensure you aren't making too broad of assumptions. -`; \ No newline at end of file +`; diff --git a/packages/cli/src/gemini.ts b/packages/cli/src/gemini.ts index 96d70f01..175dd9c6 100644 --- a/packages/cli/src/gemini.ts +++ b/packages/cli/src/gemini.ts @@ -14,77 +14,78 @@ import { TerminalTool } from './tools/terminal.tool.js'; import { WriteFileTool } from './tools/write-file.tool.js'; async function main() { - // 1. Configuration - loadEnvironment(); - const argv = await parseArguments(); // Ensure args.ts imports printWarning from ui/display - const targetDir = getTargetDirectory(argv.target_dir); + // 1. Configuration + loadEnvironment(); + const argv = await parseArguments(); // Ensure args.ts imports printWarning from ui/display + const targetDir = getTargetDirectory(argv.target_dir); - // 2. Configure tools - registerTools(targetDir); + // 2. Configure tools + registerTools(targetDir); - // 3. Render UI - render(React.createElement(App, { directory: targetDir })); + // 3. Render UI + render(React.createElement(App, { directory: targetDir })); } // --- Global Unhandled Rejection Handler --- process.on('unhandledRejection', (reason, promise) => { - // Check if this is the known 429 ClientError that sometimes escapes - // this is a workaround for a specific issue with the way we are calling gemini - // where a 429 error is thrown but not caught, causing an unhandled rejection - // TODO(adh): Remove this when the race condition is fixed - const isKnownEscaped429 = - reason instanceof Error && - reason.name === 'ClientError' && - reason.message.includes('got status: 429'); + // Check if this is the known 429 ClientError that sometimes escapes + // this is a workaround for a specific issue with the way we are calling gemini + // where a 429 error is thrown but not caught, causing an unhandled rejection + // TODO(adh): Remove this when the race condition is fixed + const isKnownEscaped429 = + reason instanceof Error && + reason.name === 'ClientError' && + reason.message.includes('got status: 429'); - if (isKnownEscaped429) { - // Log it differently and DON'T exit, as it's likely already handled visually - console.warn('-----------------------------------------'); - console.warn('WORKAROUND: Suppressed known escaped 429 Unhandled Rejection.'); - console.warn('-----------------------------------------'); - console.warn('Reason:', reason); - // No process.exit(1); - } else { - // Log other unexpected unhandled rejections as critical errors - console.error('========================================='); - console.error('CRITICAL: Unhandled Promise Rejection!'); - console.error('========================================='); - console.error('Reason:', reason); - console.error('Stack trace may follow:'); - if (!(reason instanceof Error)) { - console.error(reason); - } - // Exit for genuinely unhandled errors - process.exit(1); + if (isKnownEscaped429) { + // Log it differently and DON'T exit, as it's likely already handled visually + console.warn('-----------------------------------------'); + console.warn( + 'WORKAROUND: Suppressed known escaped 429 Unhandled Rejection.', + ); + console.warn('-----------------------------------------'); + console.warn('Reason:', reason); + // No process.exit(1); + } else { + // Log other unexpected unhandled rejections as critical errors + console.error('========================================='); + console.error('CRITICAL: Unhandled Promise Rejection!'); + console.error('========================================='); + console.error('Reason:', reason); + console.error('Stack trace may follow:'); + if (!(reason instanceof Error)) { + console.error(reason); } + // Exit for genuinely unhandled errors + process.exit(1); + } }); // --- Global Entry Point --- main().catch((error) => { - console.error('An unexpected critical error occurred:'); - if (error instanceof Error) { - console.error(error.message); - } else { - console.error(String(error)); - } - process.exit(1); + console.error('An unexpected critical error occurred:'); + if (error instanceof Error) { + console.error(error.message); + } else { + console.error(String(error)); + } + process.exit(1); }); function registerTools(targetDir: string) { - const lsTool = new LSTool(targetDir); - const readFileTool = new ReadFileTool(targetDir); - const grepTool = new GrepTool(targetDir); - const globTool = new GlobTool(targetDir); - const editTool = new EditTool(targetDir); - const terminalTool = new TerminalTool(targetDir); - const writeFileTool = new WriteFileTool(targetDir); + const lsTool = new LSTool(targetDir); + const readFileTool = new ReadFileTool(targetDir); + const grepTool = new GrepTool(targetDir); + const globTool = new GlobTool(targetDir); + const editTool = new EditTool(targetDir); + const terminalTool = new TerminalTool(targetDir); + const writeFileTool = new WriteFileTool(targetDir); - toolRegistry.registerTool(lsTool); - toolRegistry.registerTool(readFileTool); - toolRegistry.registerTool(grepTool); - toolRegistry.registerTool(globTool); - toolRegistry.registerTool(editTool); - toolRegistry.registerTool(terminalTool); - toolRegistry.registerTool(writeFileTool); + toolRegistry.registerTool(lsTool); + toolRegistry.registerTool(readFileTool); + toolRegistry.registerTool(grepTool); + toolRegistry.registerTool(globTool); + toolRegistry.registerTool(editTool); + toolRegistry.registerTool(terminalTool); + toolRegistry.registerTool(writeFileTool); } - diff --git a/packages/cli/src/tools/edit.tool.ts b/packages/cli/src/tools/edit.tool.ts index 52ef4fe8..a98b9861 100644 --- a/packages/cli/src/tools/edit.tool.ts +++ b/packages/cli/src/tools/edit.tool.ts @@ -3,7 +3,11 @@ import path from 'path'; import * as Diff from 'diff'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { BaseTool, ToolResult } from './tools.js'; -import { ToolCallConfirmationDetails, ToolConfirmationOutcome, ToolEditConfirmationDetails } from '../ui/types.js'; +import { + ToolCallConfirmationDetails, + ToolConfirmationOutcome, + ToolEditConfirmationDetails, +} from '../ui/types.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; import { ReadFileTool } from './read-file.tool.js'; import { WriteFileTool } from './write-file.tool.js'; @@ -12,39 +16,38 @@ import { WriteFileTool } from './write-file.tool.js'; * Parameters for the Edit tool */ export interface EditToolParams { - /** - * The absolute path to the file to modify - */ - file_path: string; + /** + * The absolute path to the file to modify + */ + file_path: string; - /** - * The text to replace - */ - old_string: string; + /** + * The text to replace + */ + old_string: string; - /** - * The text to replace it with - */ - new_string: string; + /** + * The text to replace it with + */ + new_string: string; - /** - * The expected number of replacements to perform (optional, defaults to 1) - */ - expected_replacements?: number; + /** + * The expected number of replacements to perform (optional, defaults to 1) + */ + expected_replacements?: number; } /** * Result from the Edit tool */ -export interface EditToolResult extends ToolResult { -} +export interface EditToolResult extends ToolResult {} interface CalculatedEdit { - currentContent: string | null; - newContent: string; - occurrences: number; - error?: { display: string, raw: string }; - isNewFile: boolean; + currentContent: string | null; + newContent: string; + occurrences: number; + error?: { display: string; raw: string }; + isNewFile: boolean; } /** @@ -52,317 +55,350 @@ interface CalculatedEdit { * This tool maintains state for the "Always Edit" confirmation preference. */ export class EditTool extends BaseTool { - private shouldAlwaysEdit = false; - private readonly rootDirectory: string; + private shouldAlwaysEdit = false; + private readonly rootDirectory: string; - /** - * Creates a new instance of the EditTool - * @param rootDirectory Root directory to ground this tool in. - */ - constructor(rootDirectory: string) { - super( - 'replace', - 'Edit', - `Replaces a SINGLE, UNIQUE occurrence of text within a file. Requires providing significant context around the change to ensure uniqueness. For moving/renaming files, use the Bash tool with \`mv\`. For replacing entire file contents or creating new files use the ${WriteFileTool.Name} tool. Always use the ${ReadFileTool.Name} tool to examine the file before using this tool.`, - { - properties: { - file_path: { - description: 'The absolute path to the file to modify. Must start with /. When creating a new file, ensure the parent directory exists (use the `LS` tool to verify).', - type: 'string' - }, - old_string: { - description: 'The exact text to replace. CRITICAL: Must uniquely identify the single instance to change. Include at least 3-5 lines of context BEFORE and AFTER the target text, matching whitespace and indentation precisely. If this string matches multiple locations or does not match exactly, the tool will fail. Use an empty string ("") when creating a new file.', - type: 'string' - }, - new_string: { - description: 'The text to replace the `old_string` with. When creating a new file (using an empty `old_string`), this should contain the full desired content of the new file. Ensure the resulting code is correct and idiomatic.', - type: 'string' - } - }, - required: ['file_path', 'old_string', 'new_string'], - type: 'object' - } - ); - this.rootDirectory = path.resolve(rootDirectory); + /** + * Creates a new instance of the EditTool + * @param rootDirectory Root directory to ground this tool in. + */ + constructor(rootDirectory: string) { + super( + 'replace', + 'Edit', + `Replaces a SINGLE, UNIQUE occurrence of text within a file. Requires providing significant context around the change to ensure uniqueness. For moving/renaming files, use the Bash tool with \`mv\`. For replacing entire file contents or creating new files use the ${WriteFileTool.Name} tool. Always use the ${ReadFileTool.Name} tool to examine the file before using this tool.`, + { + properties: { + file_path: { + description: + 'The absolute path to the file to modify. Must start with /. When creating a new file, ensure the parent directory exists (use the `LS` tool to verify).', + type: 'string', + }, + old_string: { + description: + 'The exact text to replace. CRITICAL: Must uniquely identify the single instance to change. Include at least 3-5 lines of context BEFORE and AFTER the target text, matching whitespace and indentation precisely. If this string matches multiple locations or does not match exactly, the tool will fail. Use an empty string ("") when creating a new file.', + type: 'string', + }, + new_string: { + description: + 'The text to replace the `old_string` with. When creating a new file (using an empty `old_string`), this should contain the full desired content of the new file. Ensure the resulting code is correct and idiomatic.', + type: 'string', + }, + }, + required: ['file_path', 'old_string', 'new_string'], + type: 'object', + }, + ); + this.rootDirectory = path.resolve(rootDirectory); + } + + /** + * Checks if a path is within the root directory. + * @param pathToCheck The absolute path to check. + * @returns True if the path is within the root directory, false otherwise. + */ + private isWithinRoot(pathToCheck: string): boolean { + const normalizedPath = path.normalize(pathToCheck); + const normalizedRoot = this.rootDirectory; + + const rootWithSep = normalizedRoot.endsWith(path.sep) + ? normalizedRoot + : normalizedRoot + path.sep; + + return ( + normalizedPath === normalizedRoot || + normalizedPath.startsWith(rootWithSep) + ); + } + + /** + * Validates the parameters for the Edit tool + * @param params Parameters to validate + * @returns True if parameters are valid, false otherwise + */ + validateParams(params: EditToolParams): boolean { + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { + return false; } - /** - * Checks if a path is within the root directory. - * @param pathToCheck The absolute path to check. - * @returns True if the path is within the root directory, false otherwise. - */ - private isWithinRoot(pathToCheck: string): boolean { - const normalizedPath = path.normalize(pathToCheck); - const normalizedRoot = this.rootDirectory; - - const rootWithSep = normalizedRoot.endsWith(path.sep) - ? normalizedRoot - : normalizedRoot + path.sep; - - return normalizedPath === normalizedRoot || normalizedPath.startsWith(rootWithSep); + // Ensure path is absolute + if (!path.isAbsolute(params.file_path)) { + console.error(`File path must be absolute: ${params.file_path}`); + return false; } - /** - * Validates the parameters for the Edit tool - * @param params Parameters to validate - * @returns True if parameters are valid, false otherwise - */ - validateParams(params: EditToolParams): boolean { - if (this.schema.parameters && !SchemaValidator.validate(this.schema.parameters as Record, params)) { - return false; - } - - // Ensure path is absolute - if (!path.isAbsolute(params.file_path)) { - console.error(`File path must be absolute: ${params.file_path}`); - return false; - } - - // Ensure path is within the root directory - if (!this.isWithinRoot(params.file_path)) { - console.error(`File path must be within the root directory (${this.rootDirectory}): ${params.file_path}`); - return false; - } - - - // Validate expected_replacements if provided - if (params.expected_replacements !== undefined && params.expected_replacements < 0) { - console.error('Expected replacements must be a non-negative number'); - return false; - } - - return true; + // Ensure path is within the root directory + if (!this.isWithinRoot(params.file_path)) { + console.error( + `File path must be within the root directory (${this.rootDirectory}): ${params.file_path}`, + ); + return false; } - /** - * Calculates the potential outcome of an edit operation. - * @param params Parameters for the edit operation - * @returns An object describing the potential edit outcome - * @throws File system errors if reading the file fails unexpectedly (e.g., permissions) - */ - private calculateEdit(params: EditToolParams): CalculatedEdit { - const expectedReplacements = params.expected_replacements === undefined ? 1 : params.expected_replacements; - let currentContent: string | null = null; - let fileExists = false; - let isNewFile = false; - let newContent = ''; - let occurrences = 0; - let error: { display: string, raw: string } | undefined = undefined; + // Validate expected_replacements if provided + if ( + params.expected_replacements !== undefined && + params.expected_replacements < 0 + ) { + console.error('Expected replacements must be a non-negative number'); + return false; + } - try { - currentContent = fs.readFileSync(params.file_path, 'utf8'); - fileExists = true; - } catch (err: any) { - if (err.code !== 'ENOENT') { - throw err; - } - fileExists = false; - } + return true; + } - if (params.old_string === '' && !fileExists) { - isNewFile = true; - newContent = params.new_string; - occurrences = 0; - } else if (!fileExists) { - error = { - display: `File not found.`, - raw: `File not found: ${params.file_path}` - }; - } else if (currentContent !== null) { - occurrences = this.countOccurrences(currentContent, params.old_string); + /** + * Calculates the potential outcome of an edit operation. + * @param params Parameters for the edit operation + * @returns An object describing the potential edit outcome + * @throws File system errors if reading the file fails unexpectedly (e.g., permissions) + */ + private calculateEdit(params: EditToolParams): CalculatedEdit { + const expectedReplacements = + params.expected_replacements === undefined + ? 1 + : params.expected_replacements; + let currentContent: string | null = null; + let fileExists = false; + let isNewFile = false; + let newContent = ''; + let occurrences = 0; + let error: { display: string; raw: string } | undefined = undefined; - if (occurrences === 0) { - error = { - display: `No edits made`, - raw: `Failed to edit, 0 occurrences found` - } - } else if (occurrences !== expectedReplacements) { - error = { - display: `Failed to edit, expected ${expectedReplacements} occurrences but found ${occurrences}`, - raw: `Failed to edit, Expected ${expectedReplacements} occurrences but found ${occurrences} in file: ${params.file_path}` - } - } else { - newContent = this.replaceAll(currentContent, params.old_string, params.new_string); - } - } else { - error = { - display: `Failed to read content`, - raw: `Failed to read content of existing file: ${params.file_path}` - } - } + try { + currentContent = fs.readFileSync(params.file_path, 'utf8'); + fileExists = true; + } catch (err: any) { + if (err.code !== 'ENOENT') { + throw err; + } + fileExists = false; + } - return { - currentContent, - newContent, - occurrences, - error, - isNewFile + if (params.old_string === '' && !fileExists) { + isNewFile = true; + newContent = params.new_string; + occurrences = 0; + } else if (!fileExists) { + error = { + display: `File not found.`, + raw: `File not found: ${params.file_path}`, + }; + } else if (currentContent !== null) { + occurrences = this.countOccurrences(currentContent, params.old_string); + + if (occurrences === 0) { + error = { + display: `No edits made`, + raw: `Failed to edit, 0 occurrences found`, }; + } else if (occurrences !== expectedReplacements) { + error = { + display: `Failed to edit, expected ${expectedReplacements} occurrences but found ${occurrences}`, + raw: `Failed to edit, Expected ${expectedReplacements} occurrences but found ${occurrences} in file: ${params.file_path}`, + }; + } else { + newContent = this.replaceAll( + currentContent, + params.old_string, + params.new_string, + ); + } + } else { + error = { + display: `Failed to read content`, + raw: `Failed to read content of existing file: ${params.file_path}`, + }; } - /** - * Determines if confirmation is needed and prepares the confirmation details. - * This method performs the calculation needed to generate the diff and respects the `shouldAlwaysEdit` state. - * @param params Parameters for the potential edit operation - * @returns Confirmation details object or false if no confirmation is needed/possible. - */ - async shouldConfirmExecute(params: EditToolParams): Promise { - if (this.shouldAlwaysEdit) { - return false; - } + return { + currentContent, + newContent, + occurrences, + error, + isNewFile, + }; + } - if (!this.validateParams(params)) { - console.error("[EditTool] Attempted confirmation with invalid parameters."); - return false; - } + /** + * Determines if confirmation is needed and prepares the confirmation details. + * This method performs the calculation needed to generate the diff and respects the `shouldAlwaysEdit` state. + * @param params Parameters for the potential edit operation + * @returns Confirmation details object or false if no confirmation is needed/possible. + */ + async shouldConfirmExecute( + params: EditToolParams, + ): Promise { + if (this.shouldAlwaysEdit) { + return false; + } - let calculatedEdit: CalculatedEdit; - try { - calculatedEdit = this.calculateEdit(params); - } catch (error) { - console.error(`Error calculating edit for confirmation: ${error instanceof Error ? error.message : String(error)}`); - return false; - } + if (!this.validateParams(params)) { + console.error( + '[EditTool] Attempted confirmation with invalid parameters.', + ); + return false; + } - if (calculatedEdit.error) { - return false; - } + let calculatedEdit: CalculatedEdit; + try { + calculatedEdit = this.calculateEdit(params); + } catch (error) { + console.error( + `Error calculating edit for confirmation: ${error instanceof Error ? error.message : String(error)}`, + ); + return false; + } + if (calculatedEdit.error) { + return false; + } + + const fileName = path.basename(params.file_path); + const fileDiff = Diff.createPatch( + fileName, + calculatedEdit.currentContent ?? '', + calculatedEdit.newContent, + 'Current', + 'Proposed', + { context: 3, ignoreWhitespace: true }, + ); + + const confirmationDetails: ToolEditConfirmationDetails = { + title: `Confirm Edit: ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`, + fileName, + fileDiff, + onConfirm: async (outcome: ToolConfirmationOutcome) => { + if (outcome === ToolConfirmationOutcome.ProceedAlways) { + this.shouldAlwaysEdit = true; + } + }, + }; + return confirmationDetails; + } + + getDescription(params: EditToolParams): string { + const relativePath = makeRelative(params.file_path, this.rootDirectory); + const oldStringSnippet = + params.old_string.split('\n')[0].substring(0, 30) + + (params.old_string.length > 30 ? '...' : ''); + const newStringSnippet = + params.new_string.split('\n')[0].substring(0, 30) + + (params.new_string.length > 30 ? '...' : ''); + return `${shortenPath(relativePath)}: ${oldStringSnippet} => ${newStringSnippet}`; + } + + /** + * Executes the edit operation with the given parameters. + * This method recalculates the edit operation before execution. + * @param params Parameters for the edit operation + * @returns Result of the edit operation + */ + async execute(params: EditToolParams): Promise { + if (!this.validateParams(params)) { + return { + llmContent: 'Invalid parameters for file edit operation', + returnDisplay: '**Error:** Invalid parameters for file edit operation', + }; + } + + let editData: CalculatedEdit; + try { + editData = this.calculateEdit(params); + } catch (error) { + return { + llmContent: `Error preparing edit: ${error instanceof Error ? error.message : String(error)}`, + returnDisplay: 'Failed to prepare edit', + }; + } + + if (editData.error) { + return { + llmContent: editData.error.raw, + returnDisplay: editData.error.display, + }; + } + + try { + this.ensureParentDirectoriesExist(params.file_path); + fs.writeFileSync(params.file_path, editData.newContent, 'utf8'); + + if (editData.isNewFile) { + return { + llmContent: `Created new file: ${params.file_path} with provided content.`, + returnDisplay: `Created ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`, + }; + } else { const fileName = path.basename(params.file_path); const fileDiff = Diff.createPatch( - fileName, - calculatedEdit.currentContent ?? '', - calculatedEdit.newContent, - 'Current', - 'Proposed', - { context: 3, ignoreWhitespace: true, } + fileName, + editData.currentContent ?? '', + editData.newContent, + 'Current', + 'Proposed', + { context: 3, ignoreWhitespace: true }, ); - const confirmationDetails: ToolEditConfirmationDetails = { - title: `Confirm Edit: ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`, - fileName, - fileDiff, - onConfirm: async (outcome: ToolConfirmationOutcome) => { - if (outcome === ToolConfirmationOutcome.ProceedAlways) { - this.shouldAlwaysEdit = true; - } - }, + return { + llmContent: `Successfully modified file: ${params.file_path} (${editData.occurrences} replacements).`, + returnDisplay: { fileDiff }, }; - return confirmationDetails; + } + } catch (error) { + return { + llmContent: `Error executing edit: ${error instanceof Error ? error.message : String(error)}`, + returnDisplay: `Failed to edit file`, + }; } + } - getDescription(params: EditToolParams): string { - const relativePath = makeRelative(params.file_path, this.rootDirectory); - const oldStringSnippet = params.old_string.split('\n')[0].substring(0, 30) + (params.old_string.length > 30 ? '...' : ''); - const newStringSnippet = params.new_string.split('\n')[0].substring(0, 30) + (params.new_string.length > 30 ? '...' : ''); - return `${shortenPath(relativePath)}: ${oldStringSnippet} => ${newStringSnippet}`; + /** + * Counts occurrences of a substring in a string + * @param str String to search in + * @param substr Substring to count + * @returns Number of occurrences + */ + private countOccurrences(str: string, substr: string): number { + if (substr === '') { + return 0; } - - /** - * Executes the edit operation with the given parameters. - * This method recalculates the edit operation before execution. - * @param params Parameters for the edit operation - * @returns Result of the edit operation - */ - async execute(params: EditToolParams): Promise { - if (!this.validateParams(params)) { - return { - llmContent: 'Invalid parameters for file edit operation', - returnDisplay: '**Error:** Invalid parameters for file edit operation' - }; - } - - let editData: CalculatedEdit; - try { - editData = this.calculateEdit(params); - } catch (error) { - return { - llmContent: `Error preparing edit: ${error instanceof Error ? error.message : String(error)}`, - returnDisplay: 'Failed to prepare edit' - }; - } - - if (editData.error) { - return { - llmContent: editData.error.raw, - returnDisplay: editData.error.display - }; - } - - try { - this.ensureParentDirectoriesExist(params.file_path); - fs.writeFileSync(params.file_path, editData.newContent, 'utf8'); - - if (editData.isNewFile) { - return { - llmContent: `Created new file: ${params.file_path} with provided content.`, - returnDisplay: `Created ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}` - }; - } else { - const fileName = path.basename(params.file_path); - const fileDiff = Diff.createPatch( - fileName, - editData.currentContent ?? '', - editData.newContent, - 'Current', - 'Proposed', - { context: 3, ignoreWhitespace: true } - ); - - return { - llmContent: `Successfully modified file: ${params.file_path} (${editData.occurrences} replacements).`, - returnDisplay: { fileDiff } - }; - } - } catch (error) { - return { - llmContent: `Error executing edit: ${error instanceof Error ? error.message : String(error)}`, - returnDisplay: `Failed to edit file` - }; - } + let count = 0; + let pos = str.indexOf(substr); + while (pos !== -1) { + count++; + pos = str.indexOf(substr, pos + substr.length); } + return count; + } - /** - * Counts occurrences of a substring in a string - * @param str String to search in - * @param substr Substring to count - * @returns Number of occurrences - */ - private countOccurrences(str: string, substr: string): number { - if (substr === '') { - return 0; - } - let count = 0; - let pos = str.indexOf(substr); - while (pos !== -1) { - count++; - pos = str.indexOf(substr, pos + substr.length); - } - return count; + /** + * Replaces all occurrences of a substring in a string + * @param str String to modify + * @param find Substring to find + * @param replace Replacement string + * @returns Modified string + */ + private replaceAll(str: string, find: string, replace: string): string { + if (find === '') { + return str; } + return str.split(find).join(replace); + } - /** - * Replaces all occurrences of a substring in a string - * @param str String to modify - * @param find Substring to find - * @param replace Replacement string - * @returns Modified string - */ - private replaceAll(str: string, find: string, replace: string): string { - if (find === '') { - return str; - } - return str.split(find).join(replace); - } - - /** - * Creates parent directories if they don't exist - * @param filePath Path to ensure parent directories exist - */ - private ensureParentDirectoriesExist(filePath: string): void { - const dirName = path.dirname(filePath); - if (!fs.existsSync(dirName)) { - fs.mkdirSync(dirName, { recursive: true }); - } + /** + * Creates parent directories if they don't exist + * @param filePath Path to ensure parent directories exist + */ + private ensureParentDirectoriesExist(filePath: string): void { + const dirName = path.dirname(filePath); + if (!fs.existsSync(dirName)) { + fs.mkdirSync(dirName, { recursive: true }); } + } } diff --git a/packages/cli/src/tools/glob.tool.ts b/packages/cli/src/tools/glob.tool.ts index b63aa1cc..6c14b7d1 100644 --- a/packages/cli/src/tools/glob.tool.ts +++ b/packages/cli/src/tools/glob.tool.ts @@ -23,8 +23,7 @@ export interface GlobToolParams { /** * Result from the GlobTool */ -export interface GlobToolResult extends ToolResult { -} +export interface GlobToolResult extends ToolResult {} /** * Implementation of the GlobTool that finds files matching patterns, @@ -49,17 +48,19 @@ export class GlobTool extends BaseTool { { properties: { pattern: { - description: 'The glob pattern to match against (e.g., \'*.py\', \'src/**/*.js\', \'docs/*.md\').', - type: 'string' + description: + "The glob pattern to match against (e.g., '*.py', 'src/**/*.js', 'docs/*.md').", + type: 'string', }, path: { - description: 'Optional: The absolute path to the directory to search within. If omitted, searches the root directory.', - type: 'string' - } + description: + 'Optional: The absolute path to the directory to search within. If omitted, searches the root directory.', + type: 'string', + }, }, required: ['pattern'], - type: 'object' - } + type: 'object', + }, ); // Set the root directory @@ -84,7 +85,10 @@ export class GlobTool extends BaseTool { // Check if it's the root itself or starts with the root path followed by a separator. // This ensures that we don't accidentally allow access to parent directories. - return normalizedPath === normalizedRoot || normalizedPath.startsWith(rootWithSep); + return ( + normalizedPath === normalizedRoot || + normalizedPath.startsWith(rootWithSep) + ); } /** @@ -94,7 +98,13 @@ export class GlobTool extends BaseTool { * @returns An error message string if invalid, null otherwise */ invalidParams(params: GlobToolParams): string | null { - if (this.schema.parameters && !SchemaValidator.validate(this.schema.parameters as Record, params)) { + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { return "Parameters failed schema validation. Ensure 'pattern' is a string and 'path' (if provided) is a string."; } @@ -121,8 +131,12 @@ export class GlobTool extends BaseTool { } // Validate glob pattern (basic non-empty check) - if (!params.pattern || typeof params.pattern !== 'string' || params.pattern.trim() === '') { - return "The 'pattern' parameter cannot be empty."; + if ( + !params.pattern || + typeof params.pattern !== 'string' || + params.pattern.trim() === '' + ) { + return "The 'pattern' parameter cannot be empty."; } // Could add more sophisticated glob pattern validation if needed @@ -156,7 +170,7 @@ export class GlobTool extends BaseTool { if (validationError) { return { llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`, - returnDisplay: `**Error:** Failed to execute tool.` + returnDisplay: `**Error:** Failed to execute tool.`, }; } @@ -168,10 +182,10 @@ export class GlobTool extends BaseTool { // We use fast-glob because it's performant and supports glob patterns. const entries = await fg(params.pattern, { cwd: searchDirAbsolute, // Search within this absolute directory - absolute: true, // Return absolute paths - onlyFiles: true, // Match only files - stats: true, // Include file stats object for sorting - dot: true, // Include files starting with a dot + absolute: true, // Return absolute paths + onlyFiles: true, // Match only files + stats: true, // Include file stats object for sorting + dot: true, // Include files starting with a dot ignore: ['**/node_modules/**', '**/.git/**'], // Common sensible default, adjust as needed followSymbolicLinks: false, // Avoid potential issues with symlinks unless specifically needed suppressErrors: true, // Suppress EACCES errors for individual files (we handle dir access in validation) @@ -181,7 +195,7 @@ export class GlobTool extends BaseTool { if (!entries || entries.length === 0) { return { llmContent: `No files found matching pattern "${params.pattern}" within ${searchDirAbsolute}.`, - returnDisplay: `No files found` + returnDisplay: `No files found`, }; } @@ -197,30 +211,39 @@ export class GlobTool extends BaseTool { }); // 5. Format Output - const sortedAbsolutePaths = entries.map(entry => entry.path); + const sortedAbsolutePaths = entries.map((entry) => entry.path); // Convert absolute paths to relative paths (to rootDir) for clearer display - const sortedRelativePaths = sortedAbsolutePaths.map(absPath => makeRelative(absPath, this.rootDirectory)); + const sortedRelativePaths = sortedAbsolutePaths.map((absPath) => + makeRelative(absPath, this.rootDirectory), + ); // Construct the result message - const fileListDescription = sortedRelativePaths.map(p => ` - ${shortenPath(p)}`).join('\n'); + const fileListDescription = sortedRelativePaths + .map((p) => ` - ${shortenPath(p)}`) + .join('\n'); const fileCount = sortedRelativePaths.length; - const relativeSearchDir = makeRelative(searchDirAbsolute, this.rootDirectory); - const displayPath = shortenPath(relativeSearchDir === '.' ? 'root directory' : relativeSearchDir); + const relativeSearchDir = makeRelative( + searchDirAbsolute, + this.rootDirectory, + ); + const displayPath = shortenPath( + relativeSearchDir === '.' ? 'root directory' : relativeSearchDir, + ); return { llmContent: `Found ${fileCount} file(s) matching "${params.pattern}" within ${displayPath}, sorted by modification time (newest first):\n${fileListDescription}`, - returnDisplay: `Found ${fileCount} matching file(s)` + returnDisplay: `Found ${fileCount} matching file(s)`, }; - } catch (error) { - // Catch unexpected errors during glob execution (less likely with suppressErrors=true, but possible) - const errorMessage = error instanceof Error ? error.message : String(error); - console.error(`GlobTool execute Error: ${errorMessage}`, error); - return { - llmContent: `Error during glob search operation: ${errorMessage}`, - returnDisplay: `**Error:** An unexpected error occurred.` - }; + // Catch unexpected errors during glob execution (less likely with suppressErrors=true, but possible) + const errorMessage = + error instanceof Error ? error.message : String(error); + console.error(`GlobTool execute Error: ${errorMessage}`, error); + return { + llmContent: `Error during glob search operation: ${errorMessage}`, + returnDisplay: `**Error:** An unexpected error occurred.`, + }; } } -} \ No newline at end of file +} diff --git a/packages/cli/src/tools/grep.tool.ts b/packages/cli/src/tools/grep.tool.ts index ed75890b..72e28d01 100644 --- a/packages/cli/src/tools/grep.tool.ts +++ b/packages/cli/src/tools/grep.tool.ts @@ -42,8 +42,7 @@ interface GrepMatch { /** * Result from the GrepTool */ -export interface GrepToolResult extends ToolResult { -} +export interface GrepToolResult extends ToolResult {} // --- GrepTool Class --- @@ -65,21 +64,24 @@ export class GrepTool extends BaseTool { { properties: { pattern: { - description: 'The regular expression (regex) pattern to search for within file contents (e.g., \'function\\s+myFunction\', \'import\\s+\\{.*\\}\\s+from\\s+.*\').', - type: 'string' + description: + "The regular expression (regex) pattern to search for within file contents (e.g., 'function\\s+myFunction', 'import\\s+\\{.*\\}\\s+from\\s+.*').", + type: 'string', }, path: { - description: 'Optional: The absolute path to the directory to search within. If omitted, searches the current working directory.', - type: 'string' + description: + 'Optional: The absolute path to the directory to search within. If omitted, searches the current working directory.', + type: 'string', }, include: { - description: 'Optional: A glob pattern to filter which files are searched (e.g., \'*.js\', \'*.{ts,tsx}\', \'src/**\'). If omitted, searches all files (respecting potential global ignores).', - type: 'string' - } + description: + "Optional: A glob pattern to filter which files are searched (e.g., '*.js', '*.{ts,tsx}', 'src/**'). If omitted, searches all files (respecting potential global ignores).", + type: 'string', + }, }, required: ['pattern'], - type: 'object' - } + type: 'object', + }, ); // Ensure rootDirectory is absolute and normalized this.rootDirectory = path.resolve(rootDirectory); @@ -97,8 +99,13 @@ export class GrepTool extends BaseTool { const targetPath = path.resolve(this.rootDirectory, relativePath || '.'); // Security Check: Ensure the resolved path is still within the root directory. - if (!targetPath.startsWith(this.rootDirectory) && targetPath !== this.rootDirectory) { - throw new Error(`Path validation failed: Attempted path "${relativePath || '.'}" resolves outside the allowed root directory "${this.rootDirectory}".`); + if ( + !targetPath.startsWith(this.rootDirectory) && + targetPath !== this.rootDirectory + ) { + throw new Error( + `Path validation failed: Attempted path "${relativePath || '.'}" resolves outside the allowed root directory "${this.rootDirectory}".`, + ); } // Check existence and type after resolving @@ -111,7 +118,9 @@ export class GrepTool extends BaseTool { if (err.code === 'ENOENT') { throw new Error(`Path does not exist: ${targetPath}`); } - throw new Error(`Failed to access path stats for ${targetPath}: ${err.message}`); + throw new Error( + `Failed to access path stats for ${targetPath}: ${err.message}`, + ); } return targetPath; @@ -123,8 +132,14 @@ export class GrepTool extends BaseTool { * @returns An error message string if invalid, null otherwise */ invalidParams(params: GrepToolParams): string | null { - if (this.schema.parameters && !SchemaValidator.validate(this.schema.parameters as Record, params)) { - return "Parameters failed schema validation."; + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { + return 'Parameters failed schema validation.'; } try { @@ -142,7 +157,6 @@ export class GrepTool extends BaseTool { return null; // Parameters are valid } - // --- Core Execution --- /** @@ -156,7 +170,7 @@ export class GrepTool extends BaseTool { console.error(`GrepTool Parameter Validation Failed: ${validationError}`); return { llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`, - returnDisplay: `**Error:** Failed to execute tool.` + returnDisplay: `**Error:** Failed to execute tool.`, }; } @@ -177,40 +191,49 @@ export class GrepTool extends BaseTool { return { llmContent: noMatchMsg, returnDisplay: noMatchUser }; } - const matchesByFile = matches.reduce((acc, match) => { - const relativeFilePath = path.relative(searchDirAbs, path.resolve(searchDirAbs, match.filePath)) || path.basename(match.filePath); - if (!acc[relativeFilePath]) { - acc[relativeFilePath] = []; - } - acc[relativeFilePath].push(match); - acc[relativeFilePath].sort((a, b) => a.lineNumber - b.lineNumber); - return acc; - }, {} as Record); + const matchesByFile = matches.reduce( + (acc, match) => { + const relativeFilePath = + path.relative( + searchDirAbs, + path.resolve(searchDirAbs, match.filePath), + ) || path.basename(match.filePath); + if (!acc[relativeFilePath]) { + acc[relativeFilePath] = []; + } + acc[relativeFilePath].push(match); + acc[relativeFilePath].sort((a, b) => a.lineNumber - b.lineNumber); + return acc; + }, + {} as Record, + ); let llmContent = `Found ${matches.length} match(es) for pattern "${params.pattern}" in path "${searchDirDisplay}"${params.include ? ` (filter: "${params.include}")` : ''}:\n---\n`; for (const filePath in matchesByFile) { llmContent += `File: ${filePath}\n`; - matchesByFile[filePath].forEach(match => { + matchesByFile[filePath].forEach((match) => { const trimmedLine = match.line.trim(); llmContent += `L${match.lineNumber}: ${trimmedLine}\n`; }); llmContent += '---\n'; } - return { llmContent: llmContent.trim(), returnDisplay: `Found ${matches.length} matche(s)` }; - + return { + llmContent: llmContent.trim(), + returnDisplay: `Found ${matches.length} matche(s)`, + }; } catch (error) { console.error(`Error during GrepTool execution: ${error}`); - const errorMessage = error instanceof Error ? error.message : String(error); + const errorMessage = + error instanceof Error ? error.message : String(error); return { llmContent: `Error during grep search operation: ${errorMessage}`, - returnDisplay: errorMessage + returnDisplay: errorMessage, }; } } - // --- Inlined Grep Logic and Helpers --- /** @@ -221,9 +244,13 @@ export class GrepTool extends BaseTool { private isCommandAvailable(command: string): Promise { return new Promise((resolve) => { const checkCommand = process.platform === 'win32' ? 'where' : 'command'; - const checkArgs = process.platform === 'win32' ? [command] : ['-v', command]; + const checkArgs = + process.platform === 'win32' ? [command] : ['-v', command]; try { - const child = spawn(checkCommand, checkArgs, { stdio: 'ignore', shell: process.platform === 'win32' }); + const child = spawn(checkCommand, checkArgs, { + stdio: 'ignore', + shell: process.platform === 'win32', + }); child.on('close', (code) => resolve(code === 0)); child.on('error', () => resolve(false)); } catch (e) { @@ -252,7 +279,9 @@ export class GrepTool extends BaseTool { return false; } catch (err: any) { if (err.code !== 'ENOENT') { - console.error(`Error checking for .git in ${currentPath}: ${err.message}`); + console.error( + `Error checking for .git in ${currentPath}: ${err.message}`, + ); return false; } } @@ -263,19 +292,21 @@ export class GrepTool extends BaseTool { currentPath = path.dirname(currentPath); } } catch (err: any) { - console.error(`Error traversing directory structure upwards from ${dirPath}: ${err instanceof Error ? err.message : String(err)}`); + console.error( + `Error traversing directory structure upwards from ${dirPath}: ${err instanceof Error ? err.message : String(err)}`, + ); } return false; } /** - * Parses the standard output of grep-like commands (git grep, system grep). - * Expects format: filePath:lineNumber:lineContent - * Handles colons within file paths and line content correctly. - * @param {string} output The raw stdout string. - * @param {string} basePath The absolute directory the search was run from, for relative paths. - * @returns {GrepMatch[]} Array of match objects. - */ + * Parses the standard output of grep-like commands (git grep, system grep). + * Expects format: filePath:lineNumber:lineContent + * Handles colons within file paths and line content correctly. + * @param {string} output The raw stdout string. + * @param {string} basePath The absolute directory the search was run from, for relative paths. + * @returns {GrepMatch[]} Array of match objects. + */ private parseGrepOutput(output: string, basePath: string): GrepMatch[] { const results: GrepMatch[] = []; if (!output) return results; @@ -302,7 +333,10 @@ export class GrepTool extends BaseTool { // Extract parts based on the found colon indices const filePathRaw = line.substring(0, firstColonIndex); - const lineNumberStr = line.substring(firstColonIndex + 1, secondColonIndex); + const lineNumberStr = line.substring( + firstColonIndex + 1, + secondColonIndex, + ); // The rest of the line, starting after the second colon, is the content. const lineContent = line.substring(secondColonIndex + 1); @@ -327,10 +361,10 @@ export class GrepTool extends BaseTool { } /** - * Gets a description of the grep operation - * @param params Parameters for the grep operation - * @returns A string describing the grep - */ + * Gets a description of the grep operation + * @param params Parameters for the grep operation + * @returns A string describing the grep + */ getDescription(params: GrepToolParams): string { let description = `'${params.pattern}'`; @@ -363,37 +397,59 @@ export class GrepTool extends BaseTool { try { // --- Strategy 1: git grep --- const isGit = await this.isGitRepository(absolutePath); - const gitAvailable = isGit && await this.isCommandAvailable('git'); + const gitAvailable = isGit && (await this.isCommandAvailable('git')); if (gitAvailable) { strategyUsed = 'git grep'; - const gitArgs = ['grep', '--untracked', '-n', '-E', '--ignore-case', pattern]; + const gitArgs = [ + 'grep', + '--untracked', + '-n', + '-E', + '--ignore-case', + pattern, + ]; if (include) { gitArgs.push('--', include); } try { const output = await new Promise((resolve, reject) => { - const child = spawn('git', gitArgs, { cwd: absolutePath, windowsHide: true }); + const child = spawn('git', gitArgs, { + cwd: absolutePath, + windowsHide: true, + }); const stdoutChunks: Buffer[] = []; const stderrChunks: Buffer[] = []; - child.stdout.on('data', (chunk) => { stdoutChunks.push(chunk); }); - child.stderr.on('data', (chunk) => { stderrChunks.push(chunk); }); + child.stdout.on('data', (chunk) => { + stdoutChunks.push(chunk); + }); + child.stderr.on('data', (chunk) => { + stderrChunks.push(chunk); + }); - child.on('error', (err) => reject(new Error(`Failed to start git grep: ${err.message}`))); + child.on('error', (err) => + reject(new Error(`Failed to start git grep: ${err.message}`)), + ); child.on('close', (code) => { const stdoutData = Buffer.concat(stdoutChunks).toString('utf8'); const stderrData = Buffer.concat(stderrChunks).toString('utf8'); if (code === 0) resolve(stdoutData); - else if (code === 1) resolve(''); // No matches is not an error - else reject(new Error(`git grep exited with code ${code}: ${stderrData}`)); + else if (code === 1) + resolve(''); // No matches is not an error + else + reject( + new Error(`git grep exited with code ${code}: ${stderrData}`), + ); }); }); return this.parseGrepOutput(output, absolutePath); } catch (gitError: any) { - console.error(`GrepTool: git grep strategy failed: ${gitError.message}. Falling back...`); + console.error( + `GrepTool: git grep strategy failed: ${gitError.message}. Falling back...`, + ); } } @@ -403,7 +459,7 @@ export class GrepTool extends BaseTool { strategyUsed = 'system grep'; const grepArgs = ['-r', '-n', '-H', '-E']; const commonExcludes = ['.git', 'node_modules', 'bower_components']; - commonExcludes.forEach(dir => grepArgs.push(`--exclude-dir=${dir}`)); + commonExcludes.forEach((dir) => grepArgs.push(`--exclude-dir=${dir}`)); if (include) { grepArgs.push(`--include=${include}`); } @@ -412,41 +468,67 @@ export class GrepTool extends BaseTool { try { const output = await new Promise((resolve, reject) => { - const child = spawn('grep', grepArgs, { cwd: absolutePath, windowsHide: true }); + const child = spawn('grep', grepArgs, { + cwd: absolutePath, + windowsHide: true, + }); const stdoutChunks: Buffer[] = []; const stderrChunks: Buffer[] = []; - child.stdout.on('data', (chunk) => { stdoutChunks.push(chunk); }); + child.stdout.on('data', (chunk) => { + stdoutChunks.push(chunk); + }); child.stderr.on('data', (chunk) => { const stderrStr = chunk.toString(); - if (!stderrStr.includes('Permission denied') && !/grep:.*: Is a directory/i.test(stderrStr)) { + if ( + !stderrStr.includes('Permission denied') && + !/grep:.*: Is a directory/i.test(stderrStr) + ) { stderrChunks.push(chunk); } }); - child.on('error', (err) => reject(new Error(`Failed to start system grep: ${err.message}`))); + child.on('error', (err) => + reject(new Error(`Failed to start system grep: ${err.message}`)), + ); child.on('close', (code) => { const stdoutData = Buffer.concat(stdoutChunks).toString('utf8'); - const stderrData = Buffer.concat(stderrChunks).toString('utf8').trim(); + const stderrData = Buffer.concat(stderrChunks) + .toString('utf8') + .trim(); if (code === 0) resolve(stdoutData); - else if (code === 1) resolve(''); // No matches + else if (code === 1) + resolve(''); // No matches else { - if (stderrData) reject(new Error(`System grep exited with code ${code}: ${stderrData}`)); + if (stderrData) + reject( + new Error( + `System grep exited with code ${code}: ${stderrData}`, + ), + ); else resolve(''); } }); }); return this.parseGrepOutput(output, absolutePath); } catch (grepError: any) { - console.error(`GrepTool: System grep strategy failed: ${grepError.message}. Falling back...`); + console.error( + `GrepTool: System grep strategy failed: ${grepError.message}. Falling back...`, + ); } } // --- Strategy 3: Pure JavaScript Fallback --- strategyUsed = 'javascript fallback'; const globPattern = include ? include : '**/*'; - const ignorePatterns = ['.git', 'node_modules', 'bower_components', '.svn', '.hg']; + const ignorePatterns = [ + '.git', + 'node_modules', + 'bower_components', + '.svn', + '.hg', + ]; const filesStream = fastGlob.stream(globPattern, { cwd: absolutePath, @@ -469,7 +551,9 @@ export class GrepTool extends BaseTool { lines.forEach((line, index) => { if (regex.test(line)) { allMatches.push({ - filePath: path.relative(absolutePath, fileAbsolutePath) || path.basename(fileAbsolutePath), + filePath: + path.relative(absolutePath, fileAbsolutePath) || + path.basename(fileAbsolutePath), lineNumber: index + 1, line: line, }); @@ -477,16 +561,19 @@ export class GrepTool extends BaseTool { }); } catch (readError: any) { if (readError.code !== 'ENOENT') { - console.error(`GrepTool: Could not read or process file ${fileAbsolutePath}: ${readError.message}`); + console.error( + `GrepTool: Could not read or process file ${fileAbsolutePath}: ${readError.message}`, + ); } } } return allMatches; - } catch (error: any) { - console.error(`GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${error.message}`); + console.error( + `GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${error.message}`, + ); throw error; // Re-throw to be caught by the execute method's handler } } -} \ No newline at end of file +} diff --git a/packages/cli/src/tools/ls.tool.ts b/packages/cli/src/tools/ls.tool.ts index 6c4d5848..0a7ad2fa 100644 --- a/packages/cli/src/tools/ls.tool.ts +++ b/packages/cli/src/tools/ls.tool.ts @@ -91,20 +91,21 @@ export class LSTool extends BaseTool { { properties: { path: { - description: 'The absolute path to the directory to list (must be absolute, not relative)', - type: 'string' + description: + 'The absolute path to the directory to list (must be absolute, not relative)', + type: 'string', }, ignore: { description: 'List of glob patterns to ignore', items: { - type: 'string' + type: 'string', }, - type: 'array' - } + type: 'array', + }, }, required: ['path'], - type: 'object' - } + type: 'object', + }, ); // Set the root directory @@ -123,7 +124,10 @@ export class LSTool extends BaseTool { const rootWithSep = normalizedRoot.endsWith(path.sep) ? normalizedRoot : normalizedRoot + path.sep; - return normalizedPath === normalizedRoot || normalizedPath.startsWith(rootWithSep); + return ( + normalizedPath === normalizedRoot || + normalizedPath.startsWith(rootWithSep) + ); } /** @@ -132,8 +136,14 @@ export class LSTool extends BaseTool { * @returns An error message string if invalid, null otherwise */ invalidParams(params: LSToolParams): string | null { - if (this.schema.parameters && !SchemaValidator.validate(this.schema.parameters as Record, params)) { - return "Parameters failed schema validation."; + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { + return 'Parameters failed schema validation.'; } // Ensure path is absolute if (!path.isAbsolute(params.path)) { @@ -194,7 +204,7 @@ export class LSTool extends BaseTool { listedPath: params.path, totalEntries: 0, llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`, - returnDisplay: "**Error:** Failed to execute tool." + returnDisplay: '**Error:** Failed to execute tool.', }; } @@ -206,7 +216,7 @@ export class LSTool extends BaseTool { listedPath: params.path, totalEntries: 0, llmContent: `Directory does not exist: ${params.path}`, - returnDisplay: `Directory does not exist` + returnDisplay: `Directory does not exist`, }; } // Check if path is a directory @@ -217,7 +227,7 @@ export class LSTool extends BaseTool { listedPath: params.path, totalEntries: 0, llmContent: `Path is not a directory: ${params.path}`, - returnDisplay: `Path is not a directory` + returnDisplay: `Path is not a directory`, }; } @@ -230,7 +240,7 @@ export class LSTool extends BaseTool { listedPath: params.path, totalEntries: 0, llmContent: `Directory is empty: ${params.path}`, - returnDisplay: `Directory is empty.` + returnDisplay: `Directory is empty.`, }; } @@ -248,7 +258,7 @@ export class LSTool extends BaseTool { path: fullPath, isDirectory: isDir, size: isDir ? 0 : stats.size, - modifiedTime: stats.mtime + modifiedTime: stats.mtime, }); } catch (error) { // Skip entries that can't be accessed @@ -264,18 +274,20 @@ export class LSTool extends BaseTool { }); // Create formatted content for display - const directoryContent = entries.map(entry => { - const typeIndicator = entry.isDirectory ? 'd' : '-'; - const sizeInfo = entry.isDirectory ? '' : ` (${entry.size} bytes)`; - return `${typeIndicator} ${entry.name}${sizeInfo}`; - }).join('\n'); - + const directoryContent = entries + .map((entry) => { + const typeIndicator = entry.isDirectory ? 'd' : '-'; + const sizeInfo = entry.isDirectory ? '' : ` (${entry.size} bytes)`; + return `${typeIndicator} ${entry.name}${sizeInfo}`; + }) + .join('\n'); + return { entries, listedPath: params.path, totalEntries: entries.length, llmContent: `Directory listing for ${params.path}:\n${directoryContent}`, - returnDisplay: `Found ${entries.length} item(s).` + returnDisplay: `Found ${entries.length} item(s).`, }; } catch (error) { const errorMessage = `Error listing directory: ${error instanceof Error ? error.message : String(error)}`; @@ -284,8 +296,8 @@ export class LSTool extends BaseTool { listedPath: params.path, totalEntries: 0, llmContent: errorMessage, - returnDisplay: `**Error:** ${errorMessage}` + returnDisplay: `**Error:** ${errorMessage}`, }; } } -} \ No newline at end of file +} diff --git a/packages/cli/src/tools/read-file.tool.ts b/packages/cli/src/tools/read-file.tool.ts index 7cca3391..fc4dc977 100644 --- a/packages/cli/src/tools/read-file.tool.ts +++ b/packages/cli/src/tools/read-file.tool.ts @@ -27,13 +27,15 @@ export interface ReadFileToolParams { /** * Standardized result from the ReadFile tool */ -export interface ReadFileToolResult extends ToolResult { -} +export interface ReadFileToolResult extends ToolResult {} /** * Implementation of the ReadFile tool that reads files from the filesystem */ -export class ReadFileTool extends BaseTool { +export class ReadFileTool extends BaseTool< + ReadFileToolParams, + ReadFileToolResult +> { public static readonly Name: string = 'read_file'; // Maximum number of lines to read by default @@ -60,21 +62,24 @@ export class ReadFileTool extends BaseTool, params)) { - return "Parameters failed schema validation."; + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { + return 'Parameters failed schema validation.'; } const filePath = params.file_path; if (!path.isAbsolute(filePath)) { @@ -151,7 +165,7 @@ export class ReadFileTool extends BaseTool 0.3; + return nonPrintableCount / bytesRead > 0.3; } catch (error) { return false; } @@ -166,7 +180,9 @@ export class ReadFileTool extends BaseTool { let processedLine = line; if (line.length > ReadFileTool.MAX_LINE_LENGTH) { - processedLine = line.substring(0, ReadFileTool.MAX_LINE_LENGTH) + '... [truncated]'; + processedLine = + line.substring(0, ReadFileTool.MAX_LINE_LENGTH) + '... [truncated]'; truncated = true; } return processedLine; }); - const contentTruncated = (endLine < lines.length) || truncated; + const contentTruncated = endLine < lines.length || truncated; let llmContent = ''; if (contentTruncated) { @@ -273,4 +290,4 @@ export class ReadFileTool extends BaseTool void; - reject: (error: Error) => void; - confirmationDetails: ToolExecuteConfirmationDetails | false; // Kept for potential future use + params: TerminalToolParams; + resolve: (result: TerminalToolResult) => void; + reject: (error: Error) => void; + confirmationDetails: ToolExecuteConfirmationDetails | false; // Kept for potential future use } /** * Implementation of the terminal tool that executes shell commands within a persistent session. */ -export class TerminalTool extends BaseTool { - public static Name: string = 'execute_bash_command'; +export class TerminalTool extends BaseTool< + TerminalToolParams, + TerminalToolResult +> { + public static Name: string = 'execute_bash_command'; - private readonly rootDirectory: string; - private readonly outputLimit: number; - private bashProcess: ChildProcessWithoutNullStreams | null = null; - private currentCwd: string; - private isExecuting: boolean = false; - private commandQueue: QueuedCommand[] = []; - private currentCommandCleanup: (() => void) | null = null; - private shouldAlwaysExecuteCommands: Map = new Map(); // Track confirmation per root command - private shellReady: Promise; - private resolveShellReady: (() => void) | undefined; // Definite assignment assertion - private rejectShellReady: ((reason?: any) => void) | undefined; // Definite assignment assertion - private readonly backgroundTerminalAnalyzer: BackgroundTerminalAnalyzer; + private readonly rootDirectory: string; + private readonly outputLimit: number; + private bashProcess: ChildProcessWithoutNullStreams | null = null; + private currentCwd: string; + private isExecuting: boolean = false; + private commandQueue: QueuedCommand[] = []; + private currentCommandCleanup: (() => void) | null = null; + private shouldAlwaysExecuteCommands: Map = new Map(); // Track confirmation per root command + private shellReady: Promise; + private resolveShellReady: (() => void) | undefined; // Definite assignment assertion + private rejectShellReady: ((reason?: any) => void) | undefined; // Definite assignment assertion + private readonly backgroundTerminalAnalyzer: BackgroundTerminalAnalyzer; - - constructor(rootDirectory: string, outputLimit: number = MAX_OUTPUT_LENGTH) { - const toolDisplayName = 'Terminal'; - // --- LLM-Facing Description --- - // Updated description for background tasks to mention polling and LLM analysis - const toolDescription = `Executes one or more bash commands sequentially in a secure and persistent interactive shell session. Can run commands in the foreground (waiting for completion) or background (returning after launch, with subsequent status polling). + constructor(rootDirectory: string, outputLimit: number = MAX_OUTPUT_LENGTH) { + const toolDisplayName = 'Terminal'; + // --- LLM-Facing Description --- + // Updated description for background tasks to mention polling and LLM analysis + const toolDescription = `Executes one or more bash commands sequentially in a secure and persistent interactive shell session. Can run commands in the foreground (waiting for completion) or background (returning after launch, with subsequent status polling). Core Functionality: * Starts in project root: '${path.basename(rootDirectory)}'. Current Directory starts as: ${rootDirectory} (will update based on 'cd' commands). @@ -101,7 +168,7 @@ Usage Guidance & Restrictions: * Do NOT use this tool for listing files (\`ls\`). Use the dedicated File System tool ('list_directory') instead. Relying on this tool's output for directory structure is unreliable due to potential truncation and lack of structured data. 3. **Security & Banned Commands:** - * Certain commands are banned for security (e.g., network: ${BANNED_COMMAND_ROOTS.filter(c => ['curl', 'wget', 'ssh'].includes(c)).join(', ')}; session: ${BANNED_COMMAND_ROOTS.filter(c => ['exit', 'export', 'kill'].includes(c)).join(', ')}; etc.). The full list is extensive. + * Certain commands are banned for security (e.g., network: ${BANNED_COMMAND_ROOTS.filter((c) => ['curl', 'wget', 'ssh'].includes(c)).join(', ')}; session: ${BANNED_COMMAND_ROOTS.filter((c) => ['exit', 'export', 'kill'].includes(c)).join(', ')}; etc.). The full list is extensive. * If you attempt a banned command, this tool will return an error explaining the restriction. You MUST relay this error clearly to the user. 4. **Command Execution Notes:** @@ -120,838 +187,1023 @@ Usage Guidance & Restrictions: * The initial exit code (usually 0) signifies successful *launching*; the final status indicates completion or timeout after polling. Use this tool for running build steps (\`npm install\`, \`make\`), linters (\`eslint .\`), test runners (\`pytest\`, \`jest\`), code formatters (\`prettier --write .\`), package managers (\`pip install\`), version control operations (\`git status\`, \`git diff\`), starting background servers/services (\`node server.js --runInBackground true\`), or other safe, standard command-line operations within the project workspace.`; - // --- Parameter Schema --- - const toolParameterSchema = { - type: 'object', - properties: { - command: { - description: `The exact bash command or sequence of commands (using ';' or '&&') to execute. Must adhere to usage guidelines. Example: 'npm install && npm run build'`, - type: 'string' - }, - description: { - description: `Optional: A brief, user-centric explanation of what the command does and why it's being run. Used for logging and confirmation prompts. Example: 'Install project dependencies'`, - type: 'string' - }, - timeout: { - description: `Optional execution time limit in milliseconds for FOREGROUND commands. Max ${MAX_TIMEOUT_OVERRIDE_MS}ms (${MAX_TIMEOUT_OVERRIDE_MS / 60000} min). Defaults to ${DEFAULT_TIMEOUT_MS}ms (${DEFAULT_TIMEOUT_MS / 60000} min) if not specified or invalid. Ignored if 'runInBackground' is true.`, - type: 'number' - }, - runInBackground: { - description: `If true, execute the command in the background using '&'. Defaults to false. Use for servers or long tasks.`, - type: 'boolean', - } - }, - required: ['command'] - }; + // --- Parameter Schema --- + const toolParameterSchema = { + type: 'object', + properties: { + command: { + description: `The exact bash command or sequence of commands (using ';' or '&&') to execute. Must adhere to usage guidelines. Example: 'npm install && npm run build'`, + type: 'string', + }, + description: { + description: `Optional: A brief, user-centric explanation of what the command does and why it's being run. Used for logging and confirmation prompts. Example: 'Install project dependencies'`, + type: 'string', + }, + timeout: { + description: `Optional execution time limit in milliseconds for FOREGROUND commands. Max ${MAX_TIMEOUT_OVERRIDE_MS}ms (${MAX_TIMEOUT_OVERRIDE_MS / 60000} min). Defaults to ${DEFAULT_TIMEOUT_MS}ms (${DEFAULT_TIMEOUT_MS / 60000} min) if not specified or invalid. Ignored if 'runInBackground' is true.`, + type: 'number', + }, + runInBackground: { + description: `If true, execute the command in the background using '&'. Defaults to false. Use for servers or long tasks.`, + type: 'boolean', + }, + }, + required: ['command'], + }; + super( + TerminalTool.Name, + toolDisplayName, + toolDescription, + toolParameterSchema, + ); - super( - TerminalTool.Name, - toolDisplayName, - toolDescription, - toolParameterSchema + this.rootDirectory = path.resolve(rootDirectory); + this.currentCwd = this.rootDirectory; + this.outputLimit = outputLimit; + this.shellReady = new Promise((resolve, reject) => { + this.resolveShellReady = resolve; + this.rejectShellReady = reject; + }); + this.backgroundTerminalAnalyzer = new BackgroundTerminalAnalyzer(); + + this.initializeShell(); + } + + // --- Shell Initialization and Management (largely unchanged) --- + private initializeShell() { + if (this.bashProcess) { + try { + this.bashProcess.kill(); + } catch (e) { + /* Ignore */ + } + } + + const spawnOptions: SpawnOptions = { + cwd: this.rootDirectory, + shell: true, + env: { ...process.env }, + stdio: ['pipe', 'pipe', 'pipe'], + }; + + try { + const bashPath = os.platform() === 'win32' ? 'bash.exe' : 'bash'; + this.bashProcess = spawn( + bashPath, + ['-s'], + spawnOptions, + ) as ChildProcessWithoutNullStreams; + this.currentCwd = this.rootDirectory; // Reset CWD on restart + + this.bashProcess.on('error', (err) => { + console.error('Persistent Bash Error:', err); + this.rejectShellReady?.(err); // Use optional chaining as reject might be cleared + this.bashProcess = null; + this.isExecuting = false; + this.clearQueue( + new Error(`Persistent bash process failed to start: ${err.message}`), ); + }); - this.rootDirectory = path.resolve(rootDirectory); - this.currentCwd = this.rootDirectory; - this.outputLimit = outputLimit; + this.bashProcess.on('close', (code, signal) => { + this.bashProcess = null; + this.isExecuting = false; + // Only reject if it hasn't been resolved/rejected already + this.rejectShellReady?.( + new Error( + `Persistent bash process exited (code: ${code}, signal: ${signal})`, + ), + ); + // Reset shell readiness promise for reinitialization attempts this.shellReady = new Promise((resolve, reject) => { - this.resolveShellReady = resolve; - this.rejectShellReady = reject; + this.resolveShellReady = resolve; + this.rejectShellReady = reject; }); - this.backgroundTerminalAnalyzer = new BackgroundTerminalAnalyzer(); + this.clearQueue( + new Error( + `Persistent bash process exited unexpectedly (code: ${code}, signal: ${signal}). State is lost. Queued commands cancelled.`, + ), + ); + // Attempt to reinitialize after a short delay + setTimeout(() => this.initializeShell(), 1000); + }); - this.initializeShell(); + // Readiness check - ensure shell is responsive + // Slightly longer timeout to allow shell init + setTimeout(() => { + if (this.bashProcess && !this.bashProcess.killed) { + this.resolveShellReady?.(); // Use optional chaining + } else if (!this.bashProcess) { + // Error likely already handled by 'error' or 'close' event + } else { + // Process was killed during init? + this.rejectShellReady?.( + new Error('Shell killed during initialization'), + ); + } + }, 1000); // Increase readiness check timeout slightly + } catch (error: any) { + console.error('Failed to spawn persistent bash:', error); + this.rejectShellReady?.(error); // Use optional chaining + this.bashProcess = null; + this.clearQueue( + new Error(`Failed to spawn persistent bash: ${error.message}`), + ); + } + } + + // --- Parameter Validation (unchanged) --- + invalidParams(params: TerminalToolParams): string | null { + if ( + !SchemaValidator.validate( + this.parameterSchema as Record, + params, + ) + ) { + return `Parameters failed schema validation.`; } - // --- Shell Initialization and Management (largely unchanged) --- - private initializeShell() { - if (this.bashProcess) { - try { - this.bashProcess.kill(); - } catch (e) { /* Ignore */ } + const commandOriginal = params.command.trim(); + if (!commandOriginal) { + return 'Command cannot be empty.'; + } + const commandLower = commandOriginal.toLowerCase(); + const commandParts = commandOriginal.split(/[\s;&&|]+/); + + for (const part of commandParts) { + if (!part) continue; + // Improved check: strip leading special chars before checking basename + const cleanPart = + part + .replace(/^[^a-zA-Z0-9]+/, '') + .split(/[\/\\]/) + .pop() || part.replace(/^[^a-zA-Z0-9]+/, ''); + if (cleanPart && BANNED_COMMAND_ROOTS.includes(cleanPart.toLowerCase())) { + return `Command contains a banned keyword: '${cleanPart}'. Banned list includes network tools, session control, etc.`; + } + } + + if ( + params.timeout !== undefined && + (typeof params.timeout !== 'number' || params.timeout <= 0) + ) { + return 'Timeout must be a positive number of milliseconds.'; + } + + // Relax the absolute path restriction slightly if needed, but generally good practice + // const firstCommandPart = commandParts[0]; + // if (firstCommandPart && (firstCommandPart.startsWith('/') || firstCommandPart.startsWith('\\'))) { + // return 'Executing commands via absolute paths (starting with \'/\' or \'\\\') is restricted. Use commands available in PATH or relative paths.'; + // } + + return null; // Parameters are valid + } + + // --- Description and Confirmation (unchanged) --- + getDescription(params: TerminalToolParams): string { + return params.description || params.command; + } + + async shouldConfirmExecute( + params: TerminalToolParams, + ): Promise { + const rootCommand = + params.command + .trim() + .split(/[\s;&&|]+/)[0] + ?.split(/[\/\\]/) + .pop() || 'unknown'; + + if (this.shouldAlwaysExecuteCommands.get(rootCommand)) { + return false; + } + + const description = this.getDescription(params); + + const confirmationDetails: ToolExecuteConfirmationDetails = { + title: 'Confirm Shell Command', + command: params.command, + rootCommand: rootCommand, + description: `Execute in '${this.currentCwd}':\n${description}`, + onConfirm: async (outcome: ToolConfirmationOutcome) => { + if (outcome === ToolConfirmationOutcome.ProceedAlways) { + this.shouldAlwaysExecuteCommands.set(rootCommand, true); } + }, + }; + return confirmationDetails; + } - const spawnOptions: SpawnOptions = { - cwd: this.rootDirectory, - shell: true, - env: { ...process.env }, - stdio: ['pipe', 'pipe', 'pipe'] - }; + // --- Command Execution and Queueing (unchanged structure) --- + async execute(params: TerminalToolParams): Promise { + const validationError = this.invalidParams(params); + if (validationError) { + return { + llmContent: `Command rejected: ${params.command}\nReason: ${validationError}`, + returnDisplay: `Error: ${validationError}`, + }; + } + // Assume confirmation is handled before calling execute + + return new Promise((resolve) => { + const queuedItem: QueuedCommand = { + params, + resolve, // Resolve outer promise + reject: (error) => + resolve({ + // Handle internal errors by resolving outer promise + llmContent: `Internal tool error for command: ${params.command}\nError: ${error.message}`, + returnDisplay: `Internal Tool Error: ${error.message}`, + }), + confirmationDetails: false, // Placeholder + }; + this.commandQueue.push(queuedItem); + // Ensure queue processing is triggered *after* adding the item + setImmediate(() => this.triggerQueueProcessing()); + }); + } + + private async triggerQueueProcessing(): Promise { + if (this.isExecuting || this.commandQueue.length === 0) { + return; + } + + this.isExecuting = true; + const { params, resolve, reject } = this.commandQueue.shift()!; + + try { + await this.shellReady; // Wait for the shell to be ready (or reinitialized) + if (!this.bashProcess || this.bashProcess.killed) { + // Check if killed + throw new Error( + 'Persistent bash process is not available or was killed.', + ); + } + // **** Core execution logic call **** + const result = await this.executeCommandInShell(params); + resolve(result); // Resolve the specific command's promise + } catch (error: any) { + console.error(`Error executing command "${params.command}":`, error); + reject(error); // Use the specific command's reject handler + } finally { + this.isExecuting = false; + // Use setImmediate to avoid potential deep recursion + setImmediate(() => this.triggerQueueProcessing()); + } + } + + // --- **** MODIFIED: Core Command Execution Logic **** --- + private executeCommandInShell( + params: TerminalToolParams, + ): Promise { + // Define temp file paths here to be accessible throughout + let tempStdoutPath: string | null = null; + let tempStderrPath: string | null = null; + let originalResolve: ( + value: TerminalToolResult | PromiseLike, + ) => void; // To pass to polling + let originalReject: (reason?: any) => void; + + const promise = new Promise((resolve, reject) => { + originalResolve = resolve; // Assign outer scope resolve + originalReject = reject; // Assign outer scope reject + + if (!this.bashProcess) { + return reject( + new Error('Bash process is not running. Cannot execute command.'), + ); + } + + const isBackgroundTask = params.runInBackground ?? false; + const commandUUID = crypto.randomUUID(); + const startDelimiter = `::START_CMD_${commandUUID}::`; + const endDelimiter = `::END_CMD_${commandUUID}::`; + const exitCodeDelimiter = `::EXIT_CODE_${commandUUID}::`; + const pidDelimiter = `::PID_${commandUUID}::`; // For background PID + + // --- Initialize Temp Files for Background Task --- + if (isBackgroundTask) { try { - const bashPath = os.platform() === 'win32' ? 'bash.exe' : 'bash'; - this.bashProcess = spawn(bashPath, ['-s'], spawnOptions) as ChildProcessWithoutNullStreams; - this.currentCwd = this.rootDirectory; // Reset CWD on restart - - this.bashProcess.on('error', (err) => { - console.error('Persistent Bash Error:', err); - this.rejectShellReady?.(err); // Use optional chaining as reject might be cleared - this.bashProcess = null; - this.isExecuting = false; - this.clearQueue(new Error(`Persistent bash process failed to start: ${err.message}`)); - }); - - this.bashProcess.on('close', (code, signal) => { - this.bashProcess = null; - this.isExecuting = false; - // Only reject if it hasn't been resolved/rejected already - this.rejectShellReady?.(new Error(`Persistent bash process exited (code: ${code}, signal: ${signal})`)); - // Reset shell readiness promise for reinitialization attempts - this.shellReady = new Promise((resolve, reject) => { - this.resolveShellReady = resolve; - this.rejectShellReady = reject; - }); - this.clearQueue(new Error(`Persistent bash process exited unexpectedly (code: ${code}, signal: ${signal}). State is lost. Queued commands cancelled.`)); - // Attempt to reinitialize after a short delay - setTimeout(() => this.initializeShell(), 1000); - }); - - // Readiness check - ensure shell is responsive - // Slightly longer timeout to allow shell init - setTimeout(() => { - if (this.bashProcess && !this.bashProcess.killed) { - this.resolveShellReady?.(); // Use optional chaining - } else if (!this.bashProcess) { - // Error likely already handled by 'error' or 'close' event - } else { - // Process was killed during init? - this.rejectShellReady?.(new Error("Shell killed during initialization")); - } - }, 1000); // Increase readiness check timeout slightly - - } catch (error: any) { - console.error("Failed to spawn persistent bash:", error); - this.rejectShellReady?.(error); // Use optional chaining - this.bashProcess = null; - this.clearQueue(new Error(`Failed to spawn persistent bash: ${error.message}`)); - } - } - - // --- Parameter Validation (unchanged) --- - invalidParams(params: TerminalToolParams): string | null { - if (!SchemaValidator.validate(this.parameterSchema as Record, params)) { - return `Parameters failed schema validation.`; - } - - const commandOriginal = params.command.trim(); - if (!commandOriginal) { - return "Command cannot be empty."; - } - const commandLower = commandOriginal.toLowerCase(); - const commandParts = commandOriginal.split(/[\s;&&|]+/); - - for (const part of commandParts) { - if (!part) continue; - // Improved check: strip leading special chars before checking basename - const cleanPart = part.replace(/^[^a-zA-Z0-9]+/, '').split(/[\/\\]/).pop() || part.replace(/^[^a-zA-Z0-9]+/, ''); - if (cleanPart && BANNED_COMMAND_ROOTS.includes(cleanPart.toLowerCase())) { - return `Command contains a banned keyword: '${cleanPart}'. Banned list includes network tools, session control, etc.`; - } - } - - if (params.timeout !== undefined && (typeof params.timeout !== 'number' || params.timeout <= 0)) { - return 'Timeout must be a positive number of milliseconds.'; - } - - // Relax the absolute path restriction slightly if needed, but generally good practice - // const firstCommandPart = commandParts[0]; - // if (firstCommandPart && (firstCommandPart.startsWith('/') || firstCommandPart.startsWith('\\'))) { - // return 'Executing commands via absolute paths (starting with \'/\' or \'\\\') is restricted. Use commands available in PATH or relative paths.'; - // } - - return null; // Parameters are valid - } - - // --- Description and Confirmation (unchanged) --- - getDescription(params: TerminalToolParams): string { - return params.description || params.command; - } - - async shouldConfirmExecute(params: TerminalToolParams): Promise { - const rootCommand = params.command.trim().split(/[\s;&&|]+/)[0]?.split(/[\/\\]/).pop() || 'unknown'; - - if (this.shouldAlwaysExecuteCommands.get(rootCommand)) { - return false; - } - - const description = this.getDescription(params); - - const confirmationDetails: ToolExecuteConfirmationDetails = { - title: 'Confirm Shell Command', - command: params.command, - rootCommand: rootCommand, - description: `Execute in '${this.currentCwd}':\n${description}`, - onConfirm: async (outcome: ToolConfirmationOutcome) => { - if (outcome === ToolConfirmationOutcome.ProceedAlways) { - this.shouldAlwaysExecuteCommands.set(rootCommand, true); - } - }, - }; - return confirmationDetails; - } - - // --- Command Execution and Queueing (unchanged structure) --- - async execute(params: TerminalToolParams): Promise { - const validationError = this.invalidParams(params); - if (validationError) { - return { - llmContent: `Command rejected: ${params.command}\nReason: ${validationError}`, - returnDisplay: `Error: ${validationError}`, - }; - } - - // Assume confirmation is handled before calling execute - - return new Promise((resolve) => { - const queuedItem: QueuedCommand = { - params, - resolve, // Resolve outer promise - reject: (error) => resolve({ // Handle internal errors by resolving outer promise - llmContent: `Internal tool error for command: ${params.command}\nError: ${error.message}`, - returnDisplay: `Internal Tool Error: ${error.message}` - }), - confirmationDetails: false // Placeholder - }; - this.commandQueue.push(queuedItem); - // Ensure queue processing is triggered *after* adding the item - setImmediate(() => this.triggerQueueProcessing()); - }); - } - - private async triggerQueueProcessing(): Promise { - if (this.isExecuting || this.commandQueue.length === 0) { - return; - } - - this.isExecuting = true; - const { params, resolve, reject } = this.commandQueue.shift()!; - - try { - await this.shellReady; // Wait for the shell to be ready (or reinitialized) - if (!this.bashProcess || this.bashProcess.killed) { // Check if killed - throw new Error("Persistent bash process is not available or was killed."); - } - // **** Core execution logic call **** - const result = await this.executeCommandInShell(params); - resolve(result); // Resolve the specific command's promise - } catch (error: any) { - console.error(`Error executing command "${params.command}":`, error); - reject(error); // Use the specific command's reject handler - } finally { - this.isExecuting = false; - // Use setImmediate to avoid potential deep recursion - setImmediate(() => this.triggerQueueProcessing()); - } - } - - - // --- **** MODIFIED: Core Command Execution Logic **** --- - private executeCommandInShell(params: TerminalToolParams): Promise { - // Define temp file paths here to be accessible throughout - let tempStdoutPath: string | null = null; - let tempStderrPath: string | null = null; - let originalResolve: (value: TerminalToolResult | PromiseLike) => void; // To pass to polling - let originalReject: (reason?: any) => void; - - const promise = new Promise((resolve, reject) => { - originalResolve = resolve; // Assign outer scope resolve - originalReject = reject; // Assign outer scope reject - - if (!this.bashProcess) { - return reject(new Error("Bash process is not running. Cannot execute command.")); - } - - const isBackgroundTask = params.runInBackground ?? false; - const commandUUID = crypto.randomUUID(); - const startDelimiter = `::START_CMD_${commandUUID}::`; - const endDelimiter = `::END_CMD_${commandUUID}::`; - const exitCodeDelimiter = `::EXIT_CODE_${commandUUID}::`; - const pidDelimiter = `::PID_${commandUUID}::`; // For background PID - - // --- Initialize Temp Files for Background Task --- - if (isBackgroundTask) { - try { - const tempDir = os.tmpdir(); - tempStdoutPath = path.join(tempDir, `term_out_${commandUUID}.log`); - tempStderrPath = path.join(tempDir, `term_err_${commandUUID}.log`); - } catch (err: any) { - // If temp dir setup fails, reject immediately - return reject(new Error(`Failed to determine temporary directory: ${err.message}`)); - } - } - // --- End Temp File Init --- - - let stdoutBuffer = ''; // For launch output - let stderrBuffer = ''; // For launch output - let commandOutputStarted = false; - let exitCode: number | null = null; - let backgroundPid: number | null = null; // Store PID - let receivedEndDelimiter = false; - - // Timeout only applies to foreground execution or background *launch* phase - const effectiveTimeout = isBackgroundTask - ? BACKGROUND_LAUNCH_TIMEOUT_MS - : Math.min( - params.timeout ?? DEFAULT_TIMEOUT_MS, // Use default timeout if not provided - MAX_TIMEOUT_OVERRIDE_MS - ); - - let onStdoutData: ((data: Buffer) => void) | null = null; - let onStderrData: ((data: Buffer) => void) | null = null; - let launchTimeoutId: NodeJS.Timeout | null = null; // Renamed for clarity - - launchTimeoutId = setTimeout(() => { - const timeoutMessage = isBackgroundTask - ? `Background command launch timed out after ${effectiveTimeout}ms.` - : `Command timed out after ${effectiveTimeout}ms.`; - - if (!isBackgroundTask && this.bashProcess && !this.bashProcess.killed) { - try { - this.bashProcess.stdin.write('\x03'); // Ctrl+C for foreground timeout - } catch (e: any) { console.error("Error writing SIGINT on timeout:", e); } - } - // Store listeners before calling cleanup, as cleanup nullifies them - const listenersToClean = { onStdoutData, onStderrData }; - cleanupListeners(listenersToClean); // Clean up listeners for this command - - // Clean up temp files if background launch timed out - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch(err => { - console.warn(`Error cleaning up temp files on timeout: ${err.message}`); - }); - } - - // Resolve the main promise with timeout info - originalResolve({ - llmContent: `Command execution failed: ${timeoutMessage}\nCommand: ${params.command}\nExecuted in: ${this.currentCwd}\n${isBackgroundTask ? 'Mode: Background Launch' : `Mode: Foreground\nTimeout Limit: ${effectiveTimeout}ms`}\nPartial Stdout (Launch):\n${this.truncateOutput(stdoutBuffer)}\nPartial Stderr (Launch):\n${this.truncateOutput(stderrBuffer)}\nNote: ${isBackgroundTask ? 'Launch failed or took too long.' : 'Attempted interrupt (SIGINT). Shell state might be unpredictable if command ignored interrupt.'}`, - returnDisplay: `Timeout: ${timeoutMessage}` - }); - }, effectiveTimeout); - - // --- Data processing logic (refined slightly) --- - const processDataChunk = (chunk: string, isStderr: boolean): boolean => { - let dataToProcess = chunk; - - if (!commandOutputStarted) { - const startIndex = dataToProcess.indexOf(startDelimiter); - if (startIndex !== -1) { - commandOutputStarted = true; - dataToProcess = dataToProcess.substring(startIndex + startDelimiter.length); - } else { - return false; // Still waiting for start delimiter - } - } - - // Process PID delimiter (mostly expected on stderr for background) - const pidIndex = dataToProcess.indexOf(pidDelimiter); - if (pidIndex !== -1) { - // Extract PID value strictly between delimiter and newline/end - const pidMatch = dataToProcess.substring(pidIndex + pidDelimiter.length).match(/^(\d+)/); - if (pidMatch?.[1]) { - backgroundPid = parseInt(pidMatch[1], 10); - const pidEndIndex = pidIndex + pidDelimiter.length + pidMatch[1].length; - const beforePid = dataToProcess.substring(0, pidIndex); - if (isStderr) stderrBuffer += beforePid; else stdoutBuffer += beforePid; - dataToProcess = dataToProcess.substring(pidEndIndex); - } else { - // Consume delimiter even if no number followed - const beforePid = dataToProcess.substring(0, pidIndex); - if (isStderr) stderrBuffer += beforePid; else stdoutBuffer += beforePid; - dataToProcess = dataToProcess.substring(pidIndex + pidDelimiter.length); - } - } - - - // Process Exit Code delimiter - const exitCodeIndex = dataToProcess.indexOf(exitCodeDelimiter); - if (exitCodeIndex !== -1) { - const exitCodeMatch = dataToProcess.substring(exitCodeIndex + exitCodeDelimiter.length).match(/^(\d+)/); - if (exitCodeMatch?.[1]) { - exitCode = parseInt(exitCodeMatch[1], 10); - const beforeExitCode = dataToProcess.substring(0, exitCodeIndex); - if (isStderr) stderrBuffer += beforeExitCode; else stdoutBuffer += beforeExitCode; - dataToProcess = dataToProcess.substring(exitCodeIndex + exitCodeDelimiter.length + exitCodeMatch[1].length); - } else { - const beforeExitCode = dataToProcess.substring(0, exitCodeIndex); - if (isStderr) stderrBuffer += beforeExitCode; else stdoutBuffer += beforeExitCode; - dataToProcess = dataToProcess.substring(exitCodeIndex + exitCodeDelimiter.length); - } - } - - // Process End delimiter - const endDelimiterIndex = dataToProcess.indexOf(endDelimiter); - if (endDelimiterIndex !== -1) { - receivedEndDelimiter = true; - const beforeEndDelimiter = dataToProcess.substring(0, endDelimiterIndex); - if (isStderr) stderrBuffer += beforeEndDelimiter; else stdoutBuffer += beforeEndDelimiter; - // Consume delimiter and potentially the exit code echoed after it - const afterEndDelimiter = dataToProcess.substring(endDelimiterIndex + endDelimiter.length); - const exitCodeEchoMatch = afterEndDelimiter.match(/^(\d+)/); - dataToProcess = exitCodeEchoMatch ? afterEndDelimiter.substring(exitCodeEchoMatch[1].length) : afterEndDelimiter; - } - - // Append remaining data - if (dataToProcess.length > 0) { - if (isStderr) stderrBuffer += dataToProcess; else stdoutBuffer += dataToProcess; - } - - // Check completion criteria - if (receivedEndDelimiter && exitCode !== null) { - setImmediate(cleanupAndResolve); // Use setImmediate - return true; // Signal completion of this command's stream processing - } - - return false; // More data or delimiters expected - }; - - // Assign listeners - onStdoutData = (data: Buffer) => processDataChunk(data.toString(), false); - onStderrData = (data: Buffer) => processDataChunk(data.toString(), true); - - // --- Cleanup Logic --- - // Pass listeners to allow cleanup even if they are nullified later - const cleanupListeners = (listeners?: { onStdoutData: any, onStderrData: any }) => { - if (launchTimeoutId) clearTimeout(launchTimeoutId); - launchTimeoutId = null; - - // Use passed-in listeners if available, otherwise use current scope's - const stdoutListener = listeners?.onStdoutData ?? onStdoutData; - const stderrListener = listeners?.onStderrData ?? onStderrData; - - if (this.bashProcess && !this.bashProcess.killed) { - if (stdoutListener) this.bashProcess.stdout.removeListener('data', stdoutListener); - if (stderrListener) this.bashProcess.stderr.removeListener('data', stderrListener); - } - // Only nullify the *current command's* cleanup reference if it matches - if (this.currentCommandCleanup === cleanupListeners) { - this.currentCommandCleanup = null; - } - // Nullify the listener references in the outer scope regardless - onStdoutData = null; - onStderrData = null; - }; - // Store *this specific* cleanup function instance for the current command - this.currentCommandCleanup = cleanupListeners; - - // --- Final Resolution / Polling Logic --- - const cleanupAndResolve = async () => { - // Prevent double execution if cleanup was already called (e.g., by timeout) - if (!this.currentCommandCleanup || this.currentCommandCleanup !== cleanupListeners) { - // Ensure temp files are cleaned if this command was superseded but might have created them - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch(err => { - console.warn(`Error cleaning up temp files for superseded command: ${err.message}`); - }); - } - return; - } - - // Capture initial output *before* cleanup nullifies buffers indirectly - const launchStdout = this.truncateOutput(stdoutBuffer); - const launchStderr = this.truncateOutput(stderrBuffer); - - // Store listeners before calling cleanup - const listenersToClean = { onStdoutData, onStderrData }; - cleanupListeners(listenersToClean); // Remove listeners and clear launch timeout NOW - - // --- Error check for missing exit code --- - if (exitCode === null) { - console.error(`CRITICAL: Command "${params.command}" (background: ${isBackgroundTask}) finished delimiter processing but exitCode is null.`); - const errorMode = isBackgroundTask ? 'Background Launch' : 'Foreground'; - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); - } - originalResolve({ // Use originalResolve as this is a failure *before* polling starts - llmContent: `Command: ${params.command}\nExecuted in: ${this.currentCwd}\nMode: ${errorMode}\nExit Code: -2 (Internal Error: Exit code not captured)\nStdout (during setup):\n${launchStdout}\nStderr (during setup):\n${launchStderr}`, - returnDisplay: `Internal Error: Failed to capture command exit code.\n${launchStdout}\nStderr: ${launchStderr}`.trim() - }); - return; - } - - // --- CWD Update Logic (Only for Foreground Success or 'cd') --- - let cwdUpdateError = ''; - if (!isBackgroundTask) { // Only run for foreground - const mightChangeCwd = params.command.trim().startsWith('cd '); - if (exitCode === 0 || mightChangeCwd) { - try { - const latestCwd = await this.getCurrentShellCwd(); - if (this.currentCwd !== latestCwd) { - this.currentCwd = latestCwd; - } - } catch (e: any) { - if (exitCode === 0) { // Only warn if the command itself succeeded - cwdUpdateError = `\nWarning: Failed to verify/update current working directory after command: ${e.message}`; - console.error("Failed to update CWD after successful command:", e); - } - } - } - } - // --- End CWD Update --- - - // --- Result Formatting & Polling Decision --- - if (isBackgroundTask) { - const launchSuccess = exitCode === 0; - const pidString = backgroundPid !== null ? backgroundPid.toString() : 'Not Captured'; - - // Check if polling should start - if (launchSuccess && backgroundPid !== null && tempStdoutPath && tempStderrPath) { - // --- START POLLING --- - // Don't await this, let it run in the background and resolve the original promise later - this.inspectBackgroundProcess( - backgroundPid, - params.command, - this.currentCwd, // CWD at time of launch - launchStdout, // Initial output captured during launch - launchStderr, // Initial output captured during launch - tempStdoutPath, // Path for final stdout - tempStderrPath, // Path for final stderr - originalResolve // The resolve function of the main promise - ); - // IMPORTANT: Do NOT resolve the promise here. pollBackgroundProcess will do it. - // --- END POLLING --- - } else { - // Background launch failed OR PID was not captured OR temp files missing - const reason = backgroundPid === null ? "PID not captured" : `Launch failed (Exit Code: ${exitCode})`; - const displayMessage = `Failed to launch process in background (${reason})`; - console.error(`Background launch failed for command: ${params.command}. Reason: ${reason}`); // ERROR LOG - // Ensure cleanup of temp files if launch failed - if (tempStdoutPath && tempStderrPath) { - await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); - } - originalResolve({ // Use originalResolve as polling won't start - llmContent: `Background Command Launch Failed: ${params.command}\nExecuted in: ${this.currentCwd}\nReason: ${reason}\nPID: ${pidString}\nExit Code (Launch): ${exitCode}\nStdout (During Launch):\n${launchStdout}\nStderr (During Launch):\n${launchStderr}`, - returnDisplay: displayMessage - }); - } - - } else { - // --- Foreground task result (resolve immediately) --- - let displayOutput = ''; - const stdoutTrimmed = launchStdout.trim(); - const stderrTrimmed = launchStderr.trim(); - - if (stderrTrimmed) { - displayOutput = stderrTrimmed; - } else if (stdoutTrimmed) { - displayOutput = stdoutTrimmed; - } - - if (exitCode !== 0 && !displayOutput) { - displayOutput = `Failed with exit code: ${exitCode}`; - } else if (exitCode === 0 && !displayOutput) { - displayOutput = `Success (no output)`; - } - - originalResolve({ // Use originalResolve for foreground result - llmContent: `Command: ${params.command}\nExecuted in: ${this.currentCwd}\nExit Code: ${exitCode}\nStdout:\n${launchStdout}\nStderr:\n${launchStderr}${cwdUpdateError}`, - returnDisplay: displayOutput.trim() || `Exit Code: ${exitCode}` // Ensure some display - }); - // --- End Foreground Result --- - } - }; // End of cleanupAndResolve - - - // --- Attach listeners --- - if (!this.bashProcess || this.bashProcess.killed) { - console.error("Bash process lost or killed before listeners could be attached."); - // Ensure temp files are cleaned up if they exist - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch(err => { - console.warn(`Error cleaning up temp files on attach failure: ${err.message}`); - }); - } - return originalReject(new Error("Bash process lost or killed before listeners could be attached.")); - } - // Defensive remove shouldn't be strictly necessary with current cleanup logic, but harmless - // if (onStdoutData) this.bashProcess.stdout.removeListener('data', onStdoutData); - // if (onStderrData) this.bashProcess.stderr.removeListener('data', onStderrData); - - // Attach the fresh listeners - if (onStdoutData) this.bashProcess.stdout.on('data', onStdoutData); - if (onStderrData) this.bashProcess.stderr.on('data', onStderrData); - - // --- Construct and Write Command --- - let commandToWrite: string; - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - // Background: Redirect command's stdout/stderr to temp files. - // Use subshell { ... } > file 2> file to redirect the command inside. - // Capture PID of the subshell. Capture exit code of the subshell launch. - // Ensure the subshell itself doesn't interfere with delimiter capture on stderr. - commandToWrite = `echo "${startDelimiter}"; { { ${params.command} > "${tempStdoutPath}" 2> "${tempStderrPath}"; } & } 2>/dev/null; __LAST_PID=$!; echo "${pidDelimiter}$__LAST_PID" >&2; echo "${exitCodeDelimiter}$?" >&2; echo "${endDelimiter}$?" >&1\n`; - } else if (!isBackgroundTask) { - // Foreground: Original structure. Capture command exit code. - commandToWrite = `echo "${startDelimiter}"; ${params.command}; __EXIT_CODE=$?; echo "${exitCodeDelimiter}$__EXIT_CODE" >&2; echo "${endDelimiter}$__EXIT_CODE" >&1\n`; - } else { - // Should not happen if background task setup failed, but handle defensively - return originalReject(new Error("Internal setup error: Missing temporary file paths for background execution.")); - } - - try { - if (this.bashProcess?.stdin?.writable) { - this.bashProcess.stdin.write(commandToWrite, (err) => { - if (err) { - console.error(`Error writing command "${params.command}" to bash stdin (callback):`, err); - // Store listeners before calling cleanup - const listenersToClean = { onStdoutData, onStderrData }; - cleanupListeners(listenersToClean); // Attempt cleanup - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch(e => console.warn(`Cleanup failed: ${e.message}`)); - } - originalReject(new Error(`Shell stdin write error: ${err.message}. Command likely did not execute.`)); - } - }); - } else { - throw new Error("Shell stdin is not writable or process closed when attempting to write command."); - } - } catch (e: any) { - console.error(`Error writing command "${params.command}" to bash stdin (sync):`, e); - // Store listeners before calling cleanup - const listenersToClean = { onStdoutData, onStderrData }; - cleanupListeners(listenersToClean); // Attempt cleanup - if (isBackgroundTask && tempStdoutPath && tempStderrPath) { - this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch(err => console.warn(`Cleanup failed: ${err.message}`)); - } - originalReject(new Error(`Shell stdin write exception: ${e.message}. Command likely did not execute.`)); - } - }); // End of main promise constructor - - return promise; // Return the promise created at the top - } // End of executeCommandInShell - - - // --- **** NEW: Background Process Polling **** --- - private async inspectBackgroundProcess( - pid: number, - command: string, - cwd: string, - initialStdout: string, // Stdout during launch phase - initialStderr: string, // Stderr during launch phase - tempStdoutPath: string, // Path to redirected stdout - tempStderrPath: string, // Path to redirected stderr - resolve: (value: TerminalToolResult | PromiseLike) => void // The original promise's resolve - ): Promise { // This function manages its own lifecycle but resolves the outer promise - let finalStdout = ''; - let finalStderr = ''; - let llmAnalysis = ''; - let fileReadError = ''; - - // --- Call LLM Analysis --- - try { - const { status, summary } = await this.backgroundTerminalAnalyzer.analyze(pid, tempStdoutPath, tempStderrPath, command); - if (status === 'Unknown') - llmAnalysis = `LLM analysis failed: ${summary}`; - else - llmAnalysis = summary; - - } catch (llmError: any) { - console.error(`LLM analysis failed for PID ${pid} command "${command}":`, llmError); - llmAnalysis = `LLM analysis failed: ${llmError.message}`; // Include error in analysis placeholder - } - // --- End LLM Call --- - - try { - finalStdout = await fs.readFile(tempStdoutPath, 'utf-8'); - finalStderr = await fs.readFile(tempStderrPath, 'utf-8'); + const tempDir = os.tmpdir(); + tempStdoutPath = path.join(tempDir, `term_out_${commandUUID}.log`); + tempStderrPath = path.join(tempDir, `term_err_${commandUUID}.log`); } catch (err: any) { - console.error(`Error reading temp output files for PID ${pid}:`, err); - fileReadError = `\nWarning: Failed to read temporary output files (${err.message}). Final output may be incomplete.`; + // If temp dir setup fails, reject immediately + return reject( + new Error( + `Failed to determine temporary directory: ${err.message}`, + ), + ); + } + } + // --- End Temp File Init --- + + let stdoutBuffer = ''; // For launch output + let stderrBuffer = ''; // For launch output + let commandOutputStarted = false; + let exitCode: number | null = null; + let backgroundPid: number | null = null; // Store PID + let receivedEndDelimiter = false; + + // Timeout only applies to foreground execution or background *launch* phase + const effectiveTimeout = isBackgroundTask + ? BACKGROUND_LAUNCH_TIMEOUT_MS + : Math.min( + params.timeout ?? DEFAULT_TIMEOUT_MS, // Use default timeout if not provided + MAX_TIMEOUT_OVERRIDE_MS, + ); + + let onStdoutData: ((data: Buffer) => void) | null = null; + let onStderrData: ((data: Buffer) => void) | null = null; + let launchTimeoutId: NodeJS.Timeout | null = null; // Renamed for clarity + + launchTimeoutId = setTimeout(() => { + const timeoutMessage = isBackgroundTask + ? `Background command launch timed out after ${effectiveTimeout}ms.` + : `Command timed out after ${effectiveTimeout}ms.`; + + if (!isBackgroundTask && this.bashProcess && !this.bashProcess.killed) { + try { + this.bashProcess.stdin.write('\x03'); // Ctrl+C for foreground timeout + } catch (e: any) { + console.error('Error writing SIGINT on timeout:', e); + } + } + // Store listeners before calling cleanup, as cleanup nullifies them + const listenersToClean = { onStdoutData, onStderrData }; + cleanupListeners(listenersToClean); // Clean up listeners for this command + + // Clean up temp files if background launch timed out + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch((err) => { + console.warn( + `Error cleaning up temp files on timeout: ${err.message}`, + ); + }); } - // --- Clean up temp files --- - await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); - // --- End Cleanup --- - - const truncatedFinalStdout = this.truncateOutput(finalStdout); - const truncatedFinalStderr = this.truncateOutput(finalStderr); - - // Resolve the original promise passed into pollBackgroundProcess - resolve({ - llmContent: `Background Command: ${command}\nLaunched in: ${cwd}\nPID: ${pid}\n--- LLM Analysis ---\n${llmAnalysis}\n--- Final Stdout (from ${path.basename(tempStdoutPath)}) ---\n${truncatedFinalStdout}\n--- Final Stderr (from ${path.basename(tempStderrPath)}) ---\n${truncatedFinalStderr}\n--- Launch Stdout ---\n${initialStdout}\n--- Launch Stderr ---\n${initialStderr}${fileReadError}`, - returnDisplay: `(PID: ${pid}): ${this.truncateOutput(llmAnalysis, 200)}` + // Resolve the main promise with timeout info + originalResolve({ + llmContent: `Command execution failed: ${timeoutMessage}\nCommand: ${params.command}\nExecuted in: ${this.currentCwd}\n${isBackgroundTask ? 'Mode: Background Launch' : `Mode: Foreground\nTimeout Limit: ${effectiveTimeout}ms`}\nPartial Stdout (Launch):\n${this.truncateOutput(stdoutBuffer)}\nPartial Stderr (Launch):\n${this.truncateOutput(stderrBuffer)}\nNote: ${isBackgroundTask ? 'Launch failed or took too long.' : 'Attempted interrupt (SIGINT). Shell state might be unpredictable if command ignored interrupt.'}`, + returnDisplay: `Timeout: ${timeoutMessage}`, }); - } // End of pollBackgroundProcess + }, effectiveTimeout); - // --- **** NEW: Helper to cleanup temp files **** --- - private async cleanupTempFiles(stdoutPath: string | null, stderrPath: string | null): Promise { - const unlinkQuietly = async (filePath: string | null) => { - if (!filePath) return; - try { - await fs.unlink(filePath); - } catch (err: any) { - // Ignore errors like file not found (it might have been deleted already or failed to create) - if (err.code !== 'ENOENT') { - console.warn(`Failed to delete temporary file '${filePath}': ${err.message}`); - } else { - } - } - }; - // Run deletions concurrently and wait for both - await Promise.all([ - unlinkQuietly(stdoutPath), - unlinkQuietly(stderrPath) - ]); - } + // --- Data processing logic (refined slightly) --- + const processDataChunk = (chunk: string, isStderr: boolean): boolean => { + let dataToProcess = chunk; - - // --- Get CWD (mostly unchanged, added robustness) --- - private getCurrentShellCwd(): Promise { - return new Promise((resolve, reject) => { - if (!this.bashProcess || !this.bashProcess.stdin?.writable || this.bashProcess.killed) { - return reject(new Error("Shell not running, stdin not writable, or killed for PWD check")); - } - - const pwdUuid = crypto.randomUUID(); - const pwdDelimiter = `::PWD_${pwdUuid}::`; - let pwdOutput = ''; - let onPwdData: ((data: Buffer) => void) | null = null; - let onPwdError: ((data: Buffer) => void) | null = null; // To catch errors during pwd - let pwdTimeoutId: NodeJS.Timeout | null = null; - let finished = false; // Prevent double resolution/rejection - - const cleanupPwdListeners = (err?: Error) => { - if (finished) return; // Already handled - finished = true; - if (pwdTimeoutId) clearTimeout(pwdTimeoutId); - pwdTimeoutId = null; - - const stdoutListener = onPwdData; // Capture current reference - const stderrListener = onPwdError; // Capture current reference - onPwdData = null; // Nullify before removing - onPwdError = null; - - if (this.bashProcess && !this.bashProcess.killed) { - if (stdoutListener) this.bashProcess.stdout.removeListener('data', stdoutListener); - if (stderrListener) this.bashProcess.stderr.removeListener('data', stderrListener); - } - - if (err) { - reject(err); - } else { - // Trim whitespace and trailing newlines robustly - resolve(pwdOutput.trim()); - } - } - - onPwdData = (data: Buffer) => { - if (!onPwdData) return; // Listener removed - const dataStr = data.toString(); - const delimiterIndex = dataStr.indexOf(pwdDelimiter); - if (delimiterIndex !== -1) { - pwdOutput += dataStr.substring(0, delimiterIndex); - cleanupPwdListeners(); // Resolve successfully - } else { - pwdOutput += dataStr; - } - }; - - onPwdError = (data: Buffer) => { - if (!onPwdError) return; // Listener removed - const dataStr = data.toString(); - // If delimiter appears on stderr, or any stderr occurs, treat as error - console.error(`Error during PWD check: ${dataStr}`); - cleanupPwdListeners(new Error(`Stderr received during pwd check: ${this.truncateOutput(dataStr, 100)}`)); - }; - - // Attach listeners - this.bashProcess.stdout.on('data', onPwdData); - this.bashProcess.stderr.on('data', onPwdError); - - // Set timeout - pwdTimeoutId = setTimeout(() => { - cleanupPwdListeners(new Error("Timeout waiting for pwd response")); - }, 5000); // 5 second timeout for pwd - - // Write command - try { - // Use printf for robustness against special characters in PWD and ensure newline - const pwdCommand = `printf "%s" "$PWD"; printf "${pwdDelimiter}";\n`; - if (this.bashProcess?.stdin?.writable) { - this.bashProcess.stdin.write(pwdCommand, (err) => { - if (err) { - // Error during write callback, likely means shell is unresponsive - console.error("Error writing pwd command (callback):", err); - cleanupPwdListeners(new Error(`Failed to write pwd command: ${err.message}`)); - } - }); - } else { - throw new Error("Shell stdin not writable for pwd command."); - } - } catch (e: any) { - console.error("Exception writing pwd command:", e); - cleanupPwdListeners(new Error(`Exception writing pwd command: ${e.message}`)); - } - }); - } - - // --- Truncate Output (unchanged) --- - private truncateOutput(output: string, limit?: number): string { - const effectiveLimit = limit ?? this.outputLimit; - if (output.length > effectiveLimit) { - return output.substring(0, effectiveLimit) + `\n... [Output truncated at ${effectiveLimit} characters]`; - } - return output; - } - - // --- Clear Queue (unchanged) --- - private clearQueue(error: Error) { - const queuedCount = this.commandQueue.length; - const queue = this.commandQueue; - this.commandQueue = []; - queue.forEach(({ resolve, params }) => resolve({ - llmContent: `Command cancelled: ${params.command}\nReason: ${error.message}`, - returnDisplay: `Command Cancelled: ${error.message}` - })); - } - - // --- Destroy (Added cleanup for pending background tasks if possible) --- - destroy() { - // Reject any pending shell readiness promise - this.rejectShellReady?.(new Error("BashTool destroyed during initialization or operation.")); - this.rejectShellReady = undefined; // Prevent further calls - this.resolveShellReady = undefined; - - this.clearQueue(new Error("BashTool is being destroyed.")); - - // Attempt to cleanup listeners for the *currently executing* command, if any - try { - this.currentCommandCleanup?.(); - } catch (e) { - console.warn("Error during current command cleanup:", e) + if (!commandOutputStarted) { + const startIndex = dataToProcess.indexOf(startDelimiter); + if (startIndex !== -1) { + commandOutputStarted = true; + dataToProcess = dataToProcess.substring( + startIndex + startDelimiter.length, + ); + } else { + return false; // Still waiting for start delimiter + } } - // Handle the bash process itself - if (this.bashProcess) { - const proc = this.bashProcess; // Reference before nullifying - const pid = proc.pid; - this.bashProcess = null; // Nullify reference immediately + // Process PID delimiter (mostly expected on stderr for background) + const pidIndex = dataToProcess.indexOf(pidDelimiter); + if (pidIndex !== -1) { + // Extract PID value strictly between delimiter and newline/end + const pidMatch = dataToProcess + .substring(pidIndex + pidDelimiter.length) + .match(/^(\d+)/); + if (pidMatch?.[1]) { + backgroundPid = parseInt(pidMatch[1], 10); + const pidEndIndex = + pidIndex + pidDelimiter.length + pidMatch[1].length; + const beforePid = dataToProcess.substring(0, pidIndex); + if (isStderr) stderrBuffer += beforePid; + else stdoutBuffer += beforePid; + dataToProcess = dataToProcess.substring(pidEndIndex); + } else { + // Consume delimiter even if no number followed + const beforePid = dataToProcess.substring(0, pidIndex); + if (isStderr) stderrBuffer += beforePid; + else stdoutBuffer += beforePid; + dataToProcess = dataToProcess.substring( + pidIndex + pidDelimiter.length, + ); + } + } - proc.stdout?.removeAllListeners(); - proc.stderr?.removeAllListeners(); - proc.removeAllListeners('error'); - proc.removeAllListeners('close'); + // Process Exit Code delimiter + const exitCodeIndex = dataToProcess.indexOf(exitCodeDelimiter); + if (exitCodeIndex !== -1) { + const exitCodeMatch = dataToProcess + .substring(exitCodeIndex + exitCodeDelimiter.length) + .match(/^(\d+)/); + if (exitCodeMatch?.[1]) { + exitCode = parseInt(exitCodeMatch[1], 10); + const beforeExitCode = dataToProcess.substring(0, exitCodeIndex); + if (isStderr) stderrBuffer += beforeExitCode; + else stdoutBuffer += beforeExitCode; + dataToProcess = dataToProcess.substring( + exitCodeIndex + + exitCodeDelimiter.length + + exitCodeMatch[1].length, + ); + } else { + const beforeExitCode = dataToProcess.substring(0, exitCodeIndex); + if (isStderr) stderrBuffer += beforeExitCode; + else stdoutBuffer += beforeExitCode; + dataToProcess = dataToProcess.substring( + exitCodeIndex + exitCodeDelimiter.length, + ); + } + } - // Ensure stdin is closed - proc.stdin?.end(); + // Process End delimiter + const endDelimiterIndex = dataToProcess.indexOf(endDelimiter); + if (endDelimiterIndex !== -1) { + receivedEndDelimiter = true; + const beforeEndDelimiter = dataToProcess.substring( + 0, + endDelimiterIndex, + ); + if (isStderr) stderrBuffer += beforeEndDelimiter; + else stdoutBuffer += beforeEndDelimiter; + // Consume delimiter and potentially the exit code echoed after it + const afterEndDelimiter = dataToProcess.substring( + endDelimiterIndex + endDelimiter.length, + ); + const exitCodeEchoMatch = afterEndDelimiter.match(/^(\d+)/); + dataToProcess = exitCodeEchoMatch + ? afterEndDelimiter.substring(exitCodeEchoMatch[1].length) + : afterEndDelimiter; + } + // Append remaining data + if (dataToProcess.length > 0) { + if (isStderr) stderrBuffer += dataToProcess; + else stdoutBuffer += dataToProcess; + } + + // Check completion criteria + if (receivedEndDelimiter && exitCode !== null) { + setImmediate(cleanupAndResolve); // Use setImmediate + return true; // Signal completion of this command's stream processing + } + + return false; // More data or delimiters expected + }; + + // Assign listeners + onStdoutData = (data: Buffer) => processDataChunk(data.toString(), false); + onStderrData = (data: Buffer) => processDataChunk(data.toString(), true); + + // --- Cleanup Logic --- + // Pass listeners to allow cleanup even if they are nullified later + const cleanupListeners = (listeners?: { + onStdoutData: any; + onStderrData: any; + }) => { + if (launchTimeoutId) clearTimeout(launchTimeoutId); + launchTimeoutId = null; + + // Use passed-in listeners if available, otherwise use current scope's + const stdoutListener = listeners?.onStdoutData ?? onStdoutData; + const stderrListener = listeners?.onStderrData ?? onStderrData; + + if (this.bashProcess && !this.bashProcess.killed) { + if (stdoutListener) + this.bashProcess.stdout.removeListener('data', stdoutListener); + if (stderrListener) + this.bashProcess.stderr.removeListener('data', stderrListener); + } + // Only nullify the *current command's* cleanup reference if it matches + if (this.currentCommandCleanup === cleanupListeners) { + this.currentCommandCleanup = null; + } + // Nullify the listener references in the outer scope regardless + onStdoutData = null; + onStderrData = null; + }; + // Store *this specific* cleanup function instance for the current command + this.currentCommandCleanup = cleanupListeners; + + // --- Final Resolution / Polling Logic --- + const cleanupAndResolve = async () => { + // Prevent double execution if cleanup was already called (e.g., by timeout) + if ( + !this.currentCommandCleanup || + this.currentCommandCleanup !== cleanupListeners + ) { + // Ensure temp files are cleaned if this command was superseded but might have created them + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch( + (err) => { + console.warn( + `Error cleaning up temp files for superseded command: ${err.message}`, + ); + }, + ); + } + return; + } + + // Capture initial output *before* cleanup nullifies buffers indirectly + const launchStdout = this.truncateOutput(stdoutBuffer); + const launchStderr = this.truncateOutput(stderrBuffer); + + // Store listeners before calling cleanup + const listenersToClean = { onStdoutData, onStderrData }; + cleanupListeners(listenersToClean); // Remove listeners and clear launch timeout NOW + + // --- Error check for missing exit code --- + if (exitCode === null) { + console.error( + `CRITICAL: Command "${params.command}" (background: ${isBackgroundTask}) finished delimiter processing but exitCode is null.`, + ); + const errorMode = isBackgroundTask + ? 'Background Launch' + : 'Foreground'; + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); + } + originalResolve({ + // Use originalResolve as this is a failure *before* polling starts + llmContent: `Command: ${params.command}\nExecuted in: ${this.currentCwd}\nMode: ${errorMode}\nExit Code: -2 (Internal Error: Exit code not captured)\nStdout (during setup):\n${launchStdout}\nStderr (during setup):\n${launchStderr}`, + returnDisplay: + `Internal Error: Failed to capture command exit code.\n${launchStdout}\nStderr: ${launchStderr}`.trim(), + }); + return; + } + + // --- CWD Update Logic (Only for Foreground Success or 'cd') --- + let cwdUpdateError = ''; + if (!isBackgroundTask) { + // Only run for foreground + const mightChangeCwd = params.command.trim().startsWith('cd '); + if (exitCode === 0 || mightChangeCwd) { try { - // Don't wait for these, just attempt - proc.kill('SIGTERM'); // Attempt graceful first - setTimeout(() => { - if (!proc.killed) { - proc.kill('SIGKILL'); // Force kill if needed - } - }, 500); // 500ms grace period - + const latestCwd = await this.getCurrentShellCwd(); + if (this.currentCwd !== latestCwd) { + this.currentCwd = latestCwd; + } } catch (e: any) { - // Catch errors if process already exited etc. - console.warn(`Error trying to kill bash process PID: ${pid}: ${e.message}`); + if (exitCode === 0) { + // Only warn if the command itself succeeded + cwdUpdateError = `\nWarning: Failed to verify/update current working directory after command: ${e.message}`; + console.error( + 'Failed to update CWD after successful command:', + e, + ); + } } + } + } + // --- End CWD Update --- + + // --- Result Formatting & Polling Decision --- + if (isBackgroundTask) { + const launchSuccess = exitCode === 0; + const pidString = + backgroundPid !== null ? backgroundPid.toString() : 'Not Captured'; + + // Check if polling should start + if ( + launchSuccess && + backgroundPid !== null && + tempStdoutPath && + tempStderrPath + ) { + // --- START POLLING --- + // Don't await this, let it run in the background and resolve the original promise later + this.inspectBackgroundProcess( + backgroundPid, + params.command, + this.currentCwd, // CWD at time of launch + launchStdout, // Initial output captured during launch + launchStderr, // Initial output captured during launch + tempStdoutPath, // Path for final stdout + tempStderrPath, // Path for final stderr + originalResolve, // The resolve function of the main promise + ); + // IMPORTANT: Do NOT resolve the promise here. pollBackgroundProcess will do it. + // --- END POLLING --- + } else { + // Background launch failed OR PID was not captured OR temp files missing + const reason = + backgroundPid === null + ? 'PID not captured' + : `Launch failed (Exit Code: ${exitCode})`; + const displayMessage = `Failed to launch process in background (${reason})`; + console.error( + `Background launch failed for command: ${params.command}. Reason: ${reason}`, + ); // ERROR LOG + // Ensure cleanup of temp files if launch failed + if (tempStdoutPath && tempStderrPath) { + await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); + } + originalResolve({ + // Use originalResolve as polling won't start + llmContent: `Background Command Launch Failed: ${params.command}\nExecuted in: ${this.currentCwd}\nReason: ${reason}\nPID: ${pidString}\nExit Code (Launch): ${exitCode}\nStdout (During Launch):\n${launchStdout}\nStderr (During Launch):\n${launchStderr}`, + returnDisplay: displayMessage, + }); + } + } else { + // --- Foreground task result (resolve immediately) --- + let displayOutput = ''; + const stdoutTrimmed = launchStdout.trim(); + const stderrTrimmed = launchStderr.trim(); + + if (stderrTrimmed) { + displayOutput = stderrTrimmed; + } else if (stdoutTrimmed) { + displayOutput = stdoutTrimmed; + } + + if (exitCode !== 0 && !displayOutput) { + displayOutput = `Failed with exit code: ${exitCode}`; + } else if (exitCode === 0 && !displayOutput) { + displayOutput = `Success (no output)`; + } + + originalResolve({ + // Use originalResolve for foreground result + llmContent: `Command: ${params.command}\nExecuted in: ${this.currentCwd}\nExit Code: ${exitCode}\nStdout:\n${launchStdout}\nStderr:\n${launchStderr}${cwdUpdateError}`, + returnDisplay: displayOutput.trim() || `Exit Code: ${exitCode}`, // Ensure some display + }); + // --- End Foreground Result --- + } + }; // End of cleanupAndResolve + + // --- Attach listeners --- + if (!this.bashProcess || this.bashProcess.killed) { + console.error( + 'Bash process lost or killed before listeners could be attached.', + ); + // Ensure temp files are cleaned up if they exist + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch((err) => { + console.warn( + `Error cleaning up temp files on attach failure: ${err.message}`, + ); + }); + } + return originalReject( + new Error( + 'Bash process lost or killed before listeners could be attached.', + ), + ); + } + // Defensive remove shouldn't be strictly necessary with current cleanup logic, but harmless + // if (onStdoutData) this.bashProcess.stdout.removeListener('data', onStdoutData); + // if (onStderrData) this.bashProcess.stderr.removeListener('data', onStderrData); + + // Attach the fresh listeners + if (onStdoutData) this.bashProcess.stdout.on('data', onStdoutData); + if (onStderrData) this.bashProcess.stderr.on('data', onStderrData); + + // --- Construct and Write Command --- + let commandToWrite: string; + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + // Background: Redirect command's stdout/stderr to temp files. + // Use subshell { ... } > file 2> file to redirect the command inside. + // Capture PID of the subshell. Capture exit code of the subshell launch. + // Ensure the subshell itself doesn't interfere with delimiter capture on stderr. + commandToWrite = `echo "${startDelimiter}"; { { ${params.command} > "${tempStdoutPath}" 2> "${tempStderrPath}"; } & } 2>/dev/null; __LAST_PID=$!; echo "${pidDelimiter}$__LAST_PID" >&2; echo "${exitCodeDelimiter}$?" >&2; echo "${endDelimiter}$?" >&1\n`; + } else if (!isBackgroundTask) { + // Foreground: Original structure. Capture command exit code. + commandToWrite = `echo "${startDelimiter}"; ${params.command}; __EXIT_CODE=$?; echo "${exitCodeDelimiter}$__EXIT_CODE" >&2; echo "${endDelimiter}$__EXIT_CODE" >&1\n`; + } else { + // Should not happen if background task setup failed, but handle defensively + return originalReject( + new Error( + 'Internal setup error: Missing temporary file paths for background execution.', + ), + ); + } + + try { + if (this.bashProcess?.stdin?.writable) { + this.bashProcess.stdin.write(commandToWrite, (err) => { + if (err) { + console.error( + `Error writing command "${params.command}" to bash stdin (callback):`, + err, + ); + // Store listeners before calling cleanup + const listenersToClean = { + onStdoutData, + onStderrData, + }; + cleanupListeners(listenersToClean); // Attempt cleanup + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch( + (e) => console.warn(`Cleanup failed: ${e.message}`), + ); + } + originalReject( + new Error( + `Shell stdin write error: ${err.message}. Command likely did not execute.`, + ), + ); + } + }); + } else { + throw new Error( + 'Shell stdin is not writable or process closed when attempting to write command.', + ); + } + } catch (e: any) { + console.error( + `Error writing command "${params.command}" to bash stdin (sync):`, + e, + ); + // Store listeners before calling cleanup + const listenersToClean = { onStdoutData, onStderrData }; + cleanupListeners(listenersToClean); // Attempt cleanup + if (isBackgroundTask && tempStdoutPath && tempStderrPath) { + this.cleanupTempFiles(tempStdoutPath, tempStderrPath).catch((err) => + console.warn(`Cleanup failed: ${err.message}`), + ); + } + originalReject( + new Error( + `Shell stdin write exception: ${e.message}. Command likely did not execute.`, + ), + ); + } + }); // End of main promise constructor + + return promise; // Return the promise created at the top + } // End of executeCommandInShell + + // --- **** NEW: Background Process Polling **** --- + private async inspectBackgroundProcess( + pid: number, + command: string, + cwd: string, + initialStdout: string, // Stdout during launch phase + initialStderr: string, // Stderr during launch phase + tempStdoutPath: string, // Path to redirected stdout + tempStderrPath: string, // Path to redirected stderr + resolve: ( + value: TerminalToolResult | PromiseLike, + ) => void, // The original promise's resolve + ): Promise { + // This function manages its own lifecycle but resolves the outer promise + let finalStdout = ''; + let finalStderr = ''; + let llmAnalysis = ''; + let fileReadError = ''; + + // --- Call LLM Analysis --- + try { + const { status, summary } = await this.backgroundTerminalAnalyzer.analyze( + pid, + tempStdoutPath, + tempStderrPath, + command, + ); + if (status === 'Unknown') llmAnalysis = `LLM analysis failed: ${summary}`; + else llmAnalysis = summary; + } catch (llmError: any) { + console.error( + `LLM analysis failed for PID ${pid} command "${command}":`, + llmError, + ); + llmAnalysis = `LLM analysis failed: ${llmError.message}`; // Include error in analysis placeholder + } + // --- End LLM Call --- + + try { + finalStdout = await fs.readFile(tempStdoutPath, 'utf-8'); + finalStderr = await fs.readFile(tempStderrPath, 'utf-8'); + } catch (err: any) { + console.error(`Error reading temp output files for PID ${pid}:`, err); + fileReadError = `\nWarning: Failed to read temporary output files (${err.message}). Final output may be incomplete.`; + } + + // --- Clean up temp files --- + await this.cleanupTempFiles(tempStdoutPath, tempStderrPath); + // --- End Cleanup --- + + const truncatedFinalStdout = this.truncateOutput(finalStdout); + const truncatedFinalStderr = this.truncateOutput(finalStderr); + + // Resolve the original promise passed into pollBackgroundProcess + resolve({ + llmContent: `Background Command: ${command}\nLaunched in: ${cwd}\nPID: ${pid}\n--- LLM Analysis ---\n${llmAnalysis}\n--- Final Stdout (from ${path.basename(tempStdoutPath)}) ---\n${truncatedFinalStdout}\n--- Final Stderr (from ${path.basename(tempStderrPath)}) ---\n${truncatedFinalStderr}\n--- Launch Stdout ---\n${initialStdout}\n--- Launch Stderr ---\n${initialStderr}${fileReadError}`, + returnDisplay: `(PID: ${pid}): ${this.truncateOutput(llmAnalysis, 200)}`, + }); + } // End of pollBackgroundProcess + + // --- **** NEW: Helper to cleanup temp files **** --- + private async cleanupTempFiles( + stdoutPath: string | null, + stderrPath: string | null, + ): Promise { + const unlinkQuietly = async (filePath: string | null) => { + if (!filePath) return; + try { + await fs.unlink(filePath); + } catch (err: any) { + // Ignore errors like file not found (it might have been deleted already or failed to create) + if (err.code !== 'ENOENT') { + console.warn( + `Failed to delete temporary file '${filePath}': ${err.message}`, + ); } else { } + } + }; + // Run deletions concurrently and wait for both + await Promise.all([unlinkQuietly(stdoutPath), unlinkQuietly(stderrPath)]); + } - // Note: We cannot reliably clean up temp files for background tasks - // that were polling when destroy() was called without more complex state tracking. - // OS should eventually clean /tmp, or implement a startup cleanup routine if needed. + // --- Get CWD (mostly unchanged, added robustness) --- + private getCurrentShellCwd(): Promise { + return new Promise((resolve, reject) => { + if ( + !this.bashProcess || + !this.bashProcess.stdin?.writable || + this.bashProcess.killed + ) { + return reject( + new Error( + 'Shell not running, stdin not writable, or killed for PWD check', + ), + ); + } + + const pwdUuid = crypto.randomUUID(); + const pwdDelimiter = `::PWD_${pwdUuid}::`; + let pwdOutput = ''; + let onPwdData: ((data: Buffer) => void) | null = null; + let onPwdError: ((data: Buffer) => void) | null = null; // To catch errors during pwd + let pwdTimeoutId: NodeJS.Timeout | null = null; + let finished = false; // Prevent double resolution/rejection + + const cleanupPwdListeners = (err?: Error) => { + if (finished) return; // Already handled + finished = true; + if (pwdTimeoutId) clearTimeout(pwdTimeoutId); + pwdTimeoutId = null; + + const stdoutListener = onPwdData; // Capture current reference + const stderrListener = onPwdError; // Capture current reference + onPwdData = null; // Nullify before removing + onPwdError = null; + + if (this.bashProcess && !this.bashProcess.killed) { + if (stdoutListener) + this.bashProcess.stdout.removeListener('data', stdoutListener); + if (stderrListener) + this.bashProcess.stderr.removeListener('data', stderrListener); + } + + if (err) { + reject(err); + } else { + // Trim whitespace and trailing newlines robustly + resolve(pwdOutput.trim()); + } + }; + + onPwdData = (data: Buffer) => { + if (!onPwdData) return; // Listener removed + const dataStr = data.toString(); + const delimiterIndex = dataStr.indexOf(pwdDelimiter); + if (delimiterIndex !== -1) { + pwdOutput += dataStr.substring(0, delimiterIndex); + cleanupPwdListeners(); // Resolve successfully + } else { + pwdOutput += dataStr; + } + }; + + onPwdError = (data: Buffer) => { + if (!onPwdError) return; // Listener removed + const dataStr = data.toString(); + // If delimiter appears on stderr, or any stderr occurs, treat as error + console.error(`Error during PWD check: ${dataStr}`); + cleanupPwdListeners( + new Error( + `Stderr received during pwd check: ${this.truncateOutput(dataStr, 100)}`, + ), + ); + }; + + // Attach listeners + this.bashProcess.stdout.on('data', onPwdData); + this.bashProcess.stderr.on('data', onPwdError); + + // Set timeout + pwdTimeoutId = setTimeout(() => { + cleanupPwdListeners(new Error('Timeout waiting for pwd response')); + }, 5000); // 5 second timeout for pwd + + // Write command + try { + // Use printf for robustness against special characters in PWD and ensure newline + const pwdCommand = `printf "%s" "$PWD"; printf "${pwdDelimiter}";\n`; + if (this.bashProcess?.stdin?.writable) { + this.bashProcess.stdin.write(pwdCommand, (err) => { + if (err) { + // Error during write callback, likely means shell is unresponsive + console.error('Error writing pwd command (callback):', err); + cleanupPwdListeners( + new Error(`Failed to write pwd command: ${err.message}`), + ); + } + }); + } else { + throw new Error('Shell stdin not writable for pwd command.'); + } + } catch (e: any) { + console.error('Exception writing pwd command:', e); + cleanupPwdListeners( + new Error(`Exception writing pwd command: ${e.message}`), + ); + } + }); + } + + // --- Truncate Output (unchanged) --- + private truncateOutput(output: string, limit?: number): string { + const effectiveLimit = limit ?? this.outputLimit; + if (output.length > effectiveLimit) { + return ( + output.substring(0, effectiveLimit) + + `\n... [Output truncated at ${effectiveLimit} characters]` + ); } -} // End of TerminalTool class \ No newline at end of file + return output; + } + + // --- Clear Queue (unchanged) --- + private clearQueue(error: Error) { + const queuedCount = this.commandQueue.length; + const queue = this.commandQueue; + this.commandQueue = []; + queue.forEach(({ resolve, params }) => + resolve({ + llmContent: `Command cancelled: ${params.command}\nReason: ${error.message}`, + returnDisplay: `Command Cancelled: ${error.message}`, + }), + ); + } + + // --- Destroy (Added cleanup for pending background tasks if possible) --- + destroy() { + // Reject any pending shell readiness promise + this.rejectShellReady?.( + new Error('BashTool destroyed during initialization or operation.'), + ); + this.rejectShellReady = undefined; // Prevent further calls + this.resolveShellReady = undefined; + + this.clearQueue(new Error('BashTool is being destroyed.')); + + // Attempt to cleanup listeners for the *currently executing* command, if any + try { + this.currentCommandCleanup?.(); + } catch (e) { + console.warn('Error during current command cleanup:', e); + } + + // Handle the bash process itself + if (this.bashProcess) { + const proc = this.bashProcess; // Reference before nullifying + const pid = proc.pid; + this.bashProcess = null; // Nullify reference immediately + + proc.stdout?.removeAllListeners(); + proc.stderr?.removeAllListeners(); + proc.removeAllListeners('error'); + proc.removeAllListeners('close'); + + // Ensure stdin is closed + proc.stdin?.end(); + + try { + // Don't wait for these, just attempt + proc.kill('SIGTERM'); // Attempt graceful first + setTimeout(() => { + if (!proc.killed) { + proc.kill('SIGKILL'); // Force kill if needed + } + }, 500); // 500ms grace period + } catch (e: any) { + // Catch errors if process already exited etc. + console.warn( + `Error trying to kill bash process PID: ${pid}: ${e.message}`, + ); + } + } else { + } + + // Note: We cannot reliably clean up temp files for background tasks + // that were polling when destroy() was called without more complex state tracking. + // OS should eventually clean /tmp, or implement a startup cleanup routine if needed. + } +} // End of TerminalTool class diff --git a/packages/cli/src/tools/tool-registry.ts b/packages/cli/src/tools/tool-registry.ts index 48030397..a27d09b9 100644 --- a/packages/cli/src/tools/tool-registry.ts +++ b/packages/cli/src/tools/tool-registry.ts @@ -2,56 +2,58 @@ import { ToolListUnion, FunctionDeclaration } from '@google/genai'; import { Tool } from './tools.js'; class ToolRegistry { - private tools: Map = new Map(); + private tools: Map = new Map(); - /** - * Registers a tool definition. - * @param tool - The tool object containing schema and execution logic. - */ - registerTool(tool: Tool): void { - if (this.tools.has(tool.name)) { - // Decide on behavior: throw error, log warning, or allow overwrite - console.warn(`Tool with name "${tool.name}" is already registered. Overwriting.`); - } - this.tools.set(tool.name, tool); + /** + * Registers a tool definition. + * @param tool - The tool object containing schema and execution logic. + */ + registerTool(tool: Tool): void { + if (this.tools.has(tool.name)) { + // Decide on behavior: throw error, log warning, or allow overwrite + console.warn( + `Tool with name "${tool.name}" is already registered. Overwriting.`, + ); } + this.tools.set(tool.name, tool); + } - /** - * Retrieves the list of tool schemas in the format required by Gemini. - * @returns A ToolListUnion containing the function declarations. - */ - getToolSchemas(): ToolListUnion { - const declarations: FunctionDeclaration[] = []; - this.tools.forEach(tool => { - declarations.push(tool.schema); - }); + /** + * Retrieves the list of tool schemas in the format required by Gemini. + * @returns A ToolListUnion containing the function declarations. + */ + getToolSchemas(): ToolListUnion { + const declarations: FunctionDeclaration[] = []; + this.tools.forEach((tool) => { + declarations.push(tool.schema); + }); - // Return Gemini's expected format. Handle the case of no tools. - if (declarations.length === 0) { - // Depending on the SDK version, you might need `undefined`, `[]`, or `[{ functionDeclarations: [] }]` - // Check the documentation for your @google/genai version. - // Let's assume an empty array works or signifies no tools. - return []; - // Or if it requires the structure: - // return [{ functionDeclarations: [] }]; - } - return [{ functionDeclarations: declarations }]; + // Return Gemini's expected format. Handle the case of no tools. + if (declarations.length === 0) { + // Depending on the SDK version, you might need `undefined`, `[]`, or `[{ functionDeclarations: [] }]` + // Check the documentation for your @google/genai version. + // Let's assume an empty array works or signifies no tools. + return []; + // Or if it requires the structure: + // return [{ functionDeclarations: [] }]; } + return [{ functionDeclarations: declarations }]; + } - /** - * Optional: Get a list of registered tool names. - */ - listAvailableTools(): string[] { - return Array.from(this.tools.keys()); - } + /** + * Optional: Get a list of registered tool names. + */ + listAvailableTools(): string[] { + return Array.from(this.tools.keys()); + } - /** - * Get the definition of a specific tool. - */ - getTool(name: string): Tool | undefined { - return this.tools.get(name); - } + /** + * Get the definition of a specific tool. + */ + getTool(name: string): Tool | undefined { + return this.tools.get(name); + } } // Export a singleton instance of the registry -export const toolRegistry = new ToolRegistry(); \ No newline at end of file +export const toolRegistry = new ToolRegistry(); diff --git a/packages/cli/src/tools/tools.ts b/packages/cli/src/tools/tools.ts index 21dd88d5..74acb919 100644 --- a/packages/cli/src/tools/tools.ts +++ b/packages/cli/src/tools/tools.ts @@ -1,10 +1,13 @@ -import { FunctionDeclaration, Schema } from "@google/genai"; -import { ToolCallConfirmationDetails } from "../ui/types.js"; +import { FunctionDeclaration, Schema } from '@google/genai'; +import { ToolCallConfirmationDetails } from '../ui/types.js'; /** * Interface representing the base Tool functionality */ -export interface Tool { +export interface Tool< + TParams = unknown, + TResult extends ToolResult = ToolResult, +> { /** * The internal name of the tool (used for API calls) */ @@ -45,7 +48,9 @@ export interface Tool; + shouldConfirmExecute( + params: TParams, + ): Promise; /** * Executes the tool with the given parameters @@ -55,11 +60,14 @@ export interface Tool; } - /** * Base implementation for tools with common functionality */ -export abstract class BaseTool implements Tool { +export abstract class BaseTool< + TParams = unknown, + TResult extends ToolResult = ToolResult, +> implements Tool +{ /** * Creates a new instance of BaseTool * @param name Internal name of the tool (used for API calls) @@ -71,7 +79,7 @@ export abstract class BaseTool + public readonly parameterSchema: Record, ) {} /** @@ -81,7 +89,7 @@ export abstract class BaseTool { + shouldConfirmExecute( + params: TParams, + ): Promise { return Promise.resolve(false); } @@ -125,7 +135,6 @@ export abstract class BaseTool; } - export interface ToolResult { /** * Content meant to be included in LLM history. @@ -143,5 +152,5 @@ export interface ToolResult { export type ToolResultDisplay = string | FileDiff; export interface FileDiff { - fileDiff: string + fileDiff: string; } diff --git a/packages/cli/src/tools/write-file.tool.ts b/packages/cli/src/tools/write-file.tool.ts index 78f14440..8cf0a422 100644 --- a/packages/cli/src/tools/write-file.tool.ts +++ b/packages/cli/src/tools/write-file.tool.ts @@ -3,7 +3,11 @@ import path from 'path'; import { BaseTool, ToolResult } from './tools.js'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; -import { ToolCallConfirmationDetails, ToolConfirmationOutcome, ToolEditConfirmationDetails } from '../ui/types.js'; +import { + ToolCallConfirmationDetails, + ToolConfirmationOutcome, + ToolEditConfirmationDetails, +} from '../ui/types.js'; import * as Diff from 'diff'; /** @@ -24,13 +28,15 @@ export interface WriteFileToolParams { /** * Standardized result from the WriteFile tool */ -export interface WriteFileToolResult extends ToolResult { -} +export interface WriteFileToolResult extends ToolResult {} /** * Implementation of the WriteFile tool that writes files to the filesystem */ -export class WriteFileTool extends BaseTool { +export class WriteFileTool extends BaseTool< + WriteFileToolParams, + WriteFileToolResult +> { public static readonly Name: string = 'write_file'; private shouldAlwaysWrite = false; @@ -52,17 +58,18 @@ export class WriteFileTool extends BaseTool, params)) { + if ( + this.schema.parameters && + !SchemaValidator.validate( + this.schema.parameters as Record, + params, + ) + ) { return 'Parameters failed schema validation.'; } @@ -114,7 +130,9 @@ export class WriteFileTool extends BaseTool { + async shouldConfirmExecute( + params: WriteFileToolParams, + ): Promise { if (this.shouldAlwaysWrite) { return false; } @@ -135,7 +153,7 @@ export class WriteFileTool extends BaseTool { - const [query, setQuery] = useState(''); - const [history, setHistory] = useState([]); - const { streamingState, submitQuery, initError } = useGeminiStream(setHistory); - const { elapsedTime, currentLoadingPhrase } = useLoadingIndicator(streamingState); + const [query, setQuery] = useState(''); + const [history, setHistory] = useState([]); + const { streamingState, submitQuery, initError } = + useGeminiStream(setHistory); + const { elapsedTime, currentLoadingPhrase } = + useLoadingIndicator(streamingState); - const handleInputSubmit = (value: PartListUnion) => { - submitQuery(value).then(() => { - setQuery(''); - }).catch(() => { - setQuery(''); - }); - }; + const handleInputSubmit = (value: PartListUnion) => { + submitQuery(value) + .then(() => { + setQuery(''); + }) + .catch(() => { + setQuery(''); + }); + }; - useEffect(() => { - if (initError && !history.some(item => item.type === 'error' && item.text?.includes(initError))) { - setHistory(prev => [ - ...prev, - { id: Date.now(), type: 'error', text: `Initialization Error: ${initError}. Please check API key and configuration.` } as HistoryItem - ]); - } - }, [initError, history]); + useEffect(() => { + if ( + initError && + !history.some( + (item) => item.type === 'error' && item.text?.includes(initError), + ) + ) { + setHistory((prev) => [ + ...prev, + { + id: Date.now(), + type: 'error', + text: `Initialization Error: ${initError}. Please check API key and configuration.`, + } as HistoryItem, + ]); + } + }, [initError, history]); - const isWaitingForToolConfirmation = history.some(item => - item.type === 'tool_group' && item.tools.some(tool => tool.confirmationDetails !== undefined) - ); - const isInputActive = streamingState === StreamingState.Idle && !initError; + const isWaitingForToolConfirmation = history.some( + (item) => + item.type === 'tool_group' && + item.tools.some((tool) => tool.confirmationDetails !== undefined), + ); + const isInputActive = streamingState === StreamingState.Idle && !initError; + return ( + +
- return ( - -
+ - - - {initError && streamingState !== StreamingState.Responding && !isWaitingForToolConfirmation && ( - - {history.find(item => item.type === 'error' && item.text?.includes(initError))?.text ? ( - {history.find(item => item.type === 'error' && item.text?.includes(initError))?.text} - ) : ( - <> - Initialization Error: {initError} - Please check API key and configuration. - - )} - + {initError && + streamingState !== StreamingState.Responding && + !isWaitingForToolConfirmation && ( + + {history.find( + (item) => item.type === 'error' && item.text?.includes(initError), + )?.text ? ( + + { + history.find( + (item) => + item.type === 'error' && item.text?.includes(initError), + )?.text + } + + ) : ( + <> + Initialization Error: {initError} + + {' '} + Please check API key and configuration. + + )} + + )} - - - - + + + + - {!isWaitingForToolConfirmation && isInputActive && ( - - )} + {!isWaitingForToolConfirmation && isInputActive && ( + + )} -