UNPKG

autoagent-cli

Version:

Run autonomous AI agents using Claude or Gemini for task execution

212 lines (211 loc) 8.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Provider = void 0; const child_process_1 = require("child_process"); const stream_formatter_1 = require("../utils/stream-formatter"); class Provider { getName() { return this.name; } spawnProcess(command, args, signal) { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, args); let stdout = ''; let stderr = ''; if (signal) { signal.addEventListener('abort', () => { child.kill('SIGTERM'); reject(new Error('Process aborted')); }); } child.stdout?.on('data', (data) => { stdout += data.toString(); }); child.stderr?.on('data', (data) => { stderr += data.toString(); }); child.on('error', (error) => { reject(error); }); child.on('close', (code) => { resolve({ stdout, stderr, code }); }); }); } spawnProcessWithStreaming(command, args, signal) { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, args); let stdout = ''; let stderr = ''; if (command === 'gemini') { stream_formatter_1.StreamFormatter.showHeader('gemini'); } else if (command === 'claude') { stream_formatter_1.StreamFormatter.showHeader('claude'); } child.on('error', (error) => { reject(error); }); if (signal) { signal.addEventListener('abort', () => { child.kill('SIGTERM'); reject(new Error('Process aborted')); }); } child.stdout?.on('data', (data) => { const chunk = data.toString(); stdout += chunk; if (process.env.DEBUG === 'true') { console.error('[DEBUG] Raw chunk:', chunk); } if (command === 'gemini') { const formatted = stream_formatter_1.StreamFormatter.formatGeminiOutput(chunk); if (formatted) { stream_formatter_1.StreamFormatter.displayGeminiText(formatted); } } else { const lines = chunk.split('\n').filter(line => line.trim().length > 0); for (const line of lines) { try { const message = JSON.parse(line); if (command === 'claude') { stream_formatter_1.StreamFormatter.formatClaudeMessage(message); } } catch (e) { if (process.env.DEBUG === 'true') { console.error('[DEBUG] Parse error:', e, 'Line:', line); } } } } }); child.stderr?.on('data', (data) => { stderr += data.toString(); }); child.on('error', (error) => { reject(error); }); child.on('close', (code) => { if (command === 'gemini') { const remaining = stream_formatter_1.StreamFormatter.flushGeminiBuffer(); if (remaining) { stream_formatter_1.StreamFormatter.displayGeminiText(remaining); } stream_formatter_1.StreamFormatter.showFooter(); } else if (command === 'claude') { stream_formatter_1.StreamFormatter.showFooter(); } resolve({ stdout, stderr, code }); }); }); } spawnProcessWithStreamingAndStdin(command, args, stdinContent, signal) { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, args, { stdio: ['pipe', 'pipe', 'pipe'] }); let stdout = ''; let stderr = ''; if (command === 'gemini') { stream_formatter_1.StreamFormatter.showHeader('gemini'); } else if (command === 'claude') { stream_formatter_1.StreamFormatter.showHeader('claude'); } if (signal) { signal.addEventListener('abort', () => { child.kill('SIGTERM'); reject(new Error('Process aborted')); }); } if (child.stdin) { child.stdin.write(stdinContent); child.stdin.end(); } child.stdout?.on('data', (data) => { const chunk = data.toString(); stdout += chunk; if (process.env.DEBUG === 'true') { console.error('[DEBUG] Raw chunk:', chunk); } if (command === 'gemini') { const formatted = stream_formatter_1.StreamFormatter.formatGeminiOutput(chunk); if (formatted) { stream_formatter_1.StreamFormatter.displayGeminiText(formatted); } } else { const lines = chunk.split('\n').filter(line => line.trim().length > 0); for (const line of lines) { try { const message = JSON.parse(line); if (command === 'claude') { stream_formatter_1.StreamFormatter.formatClaudeMessage(message); } } catch (e) { if (process.env.DEBUG === 'true') { console.error('[DEBUG] Parse error:', e, 'Line:', line); } } } } }); child.stderr?.on('data', (data) => { stderr += data.toString(); }); child.on('error', (error) => { reject(error); }); child.on('close', (code) => { if (command === 'gemini') { const remaining = stream_formatter_1.StreamFormatter.flushGeminiBuffer(); if (remaining) { stream_formatter_1.StreamFormatter.displayGeminiText(remaining); } stream_formatter_1.StreamFormatter.showFooter(); } else if (command === 'claude') { stream_formatter_1.StreamFormatter.showFooter(); } resolve({ stdout, stderr, code }); }); }); } formatError(error) { if (error instanceof Error) { return error.message; } if (typeof error === 'string') { return error; } return 'Unknown error occurred'; } extractFilesChanged(output) { const files = []; const patterns = [ /Modified:\s+(.+)/gi, /Created:\s+(.+)/gi, /Updated:\s+(.+)/gi, /Changed:\s+(.+)/gi, /File changed:\s+(.+)/gi, /Wrote to:\s+(.+)/gi, /Writing to:\s+(.+)/gi, /Saved:\s+(.+)/gi ]; for (const pattern of patterns) { let match; while ((match = pattern.exec(output)) !== null) { const file = match[1]?.trim(); if (file !== undefined && file !== '' && !files.includes(file)) { files.push(file); } } } return files; } } exports.Provider = Provider;