UNPKG

giga-code

Version:

A personal AI CLI assistant powered by Grok for local development.

143 lines 5.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfirmationService = void 0; const child_process_1 = require("child_process"); const util_1 = require("util"); const events_1 = require("events"); const mode_manager_1 = require("./mode-manager"); const types_1 = require("../types"); const execAsync = (0, util_1.promisify)(child_process_1.exec); class ConfirmationService extends events_1.EventEmitter { static getInstance() { if (!ConfirmationService.instance) { ConfirmationService.instance = new ConfirmationService(); } return ConfirmationService.instance; } constructor() { super(); this.skipConfirmationThisSession = false; this.pendingConfirmation = null; this.resolveConfirmation = null; this.headlessMode = false; // Session flags for different operation types this.sessionFlags = { fileOperations: false, bashCommands: false, allOperations: false }; } async requestConfirmation(options, operationType = 'file') { // In headless mode, auto-approve all operations if (this.headlessMode) { return { confirmed: true }; } // In GIGA mode, default to no confirmations (full power mode) const currentMode = mode_manager_1.modeManager.getCurrentMode(); if (currentMode === types_1.AgentMode.GIGA) { // GIGA mode = full power, no permission requests by default return { confirmed: true }; } // In CHILL mode, always ask for confirmation unless user has set session flags if (currentMode === types_1.AgentMode.CHILL) { // Check session flags first if (this.sessionFlags.allOperations || (operationType === 'file' && this.sessionFlags.fileOperations) || (operationType === 'bash' && this.sessionFlags.bashCommands)) { return { confirmed: true }; } // In CHILL mode, we need to ask for confirmation } // In PLAN mode, generally approve basic operations but still respect session flags if (currentMode === types_1.AgentMode.PLAN) { if (this.sessionFlags.allOperations || (operationType === 'file' && this.sessionFlags.fileOperations) || (operationType === 'bash' && this.sessionFlags.bashCommands)) { return { confirmed: true }; } // For PLAN mode, we might want to be more permissive for read operations // but still ask for write operations - the individual tools can decide } // If VS Code should be opened, try to open it if (options.showVSCodeOpen) { try { await this.openInVSCode(options.filename); } catch (error) { // If VS Code opening fails, continue without it options.showVSCodeOpen = false; } } // Create a promise that will be resolved by the UI component this.pendingConfirmation = new Promise((resolve) => { this.resolveConfirmation = resolve; }); // Emit custom event that the UI can listen to (using setImmediate to ensure the UI updates) setImmediate(() => { this.emit('confirmation-requested', options); }); const result = await this.pendingConfirmation; if (result.dontAskAgain) { // Set the appropriate session flag based on operation type if (operationType === 'file') { this.sessionFlags.fileOperations = true; } else if (operationType === 'bash') { this.sessionFlags.bashCommands = true; } // Could also set allOperations for global skip } return result; } confirmOperation(confirmed, dontAskAgain) { if (this.resolveConfirmation) { this.resolveConfirmation({ confirmed, dontAskAgain }); this.resolveConfirmation = null; this.pendingConfirmation = null; } } rejectOperation(feedback) { if (this.resolveConfirmation) { this.resolveConfirmation({ confirmed: false, feedback }); this.resolveConfirmation = null; this.pendingConfirmation = null; } } async openInVSCode(filename) { // Try different VS Code commands const commands = ['code', 'code-insiders', 'codium']; for (const cmd of commands) { try { await execAsync(`which ${cmd}`); await execAsync(`${cmd} "${filename}"`); return; } catch (error) { // Continue to next command continue; } } throw new Error('VS Code not found'); } isPending() { return this.pendingConfirmation !== null; } resetSession() { this.sessionFlags = { fileOperations: false, bashCommands: false, allOperations: false }; } getSessionFlags() { return { ...this.sessionFlags }; } setHeadlessMode(enabled) { this.headlessMode = enabled; } isHeadlessMode() { return this.headlessMode; } } exports.ConfirmationService = ConfirmationService; //# sourceMappingURL=confirmation-service.js.map