@vibe-kit/grok-cli
Version:
An open-source AI agent that brings the power of Grok directly into your terminal.
113 lines • 4.04 kB
JavaScript
;
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 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;
// Session flags for different operation types
this.sessionFlags = {
fileOperations: false,
bashCommands: false,
allOperations: false,
};
}
async requestConfirmation(options, operationType = "file") {
// Check session flags
if (this.sessionFlags.allOperations ||
(operationType === "file" && this.sessionFlags.fileOperations) ||
(operationType === "bash" && this.sessionFlags.bashCommands)) {
return { confirmed: true };
}
// 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 };
}
setSessionFlag(flagType, value) {
this.sessionFlags[flagType] = value;
}
}
exports.ConfirmationService = ConfirmationService;
//# sourceMappingURL=confirmation-service.js.map