@every-env/cli
Version:
Multi-agent orchestrator for AI-powered development workflows
91 lines • 3.9 kB
JavaScript
import { BaseAgent } from './base-agent.js';
import { logger } from '../utils/logger.js';
import { existsSync, writeFileSync, mkdirSync } from 'fs';
import { dirname } from 'path';
import { validatePath } from '../utils/security.js';
export class GenericAgent extends BaseAgent {
async run(prompt, outputPath) {
this.startTime = Date.now();
try {
// Build command arguments
const args = this.buildArgs(prompt);
const command = this.config.command || 'node';
logger.debug(`Running generic agent ${this.config.id}`, {
command: command,
args: args.slice(0, 3), // Log first few args
});
// Spawn the process
const exitCode = await this.spawnProcess(command, args, prompt);
// Handle output
if (exitCode === 0 && this.stdout.length > 0) {
try {
// Validate output path is within working directory
const baseDir = this.options.workingDir || this.config.workingDir || process.cwd();
const safeOutputPath = validatePath(outputPath, baseDir);
// Ensure output directory exists
const outputDir = dirname(safeOutputPath);
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
// Write stdout to file
const content = this.stdout.join('');
writeFileSync(safeOutputPath, content);
logger.debug(`[${this.config.id}] Wrote stdout to file:`, {
outputPath: safeOutputPath,
contentLength: content.length
});
return this.buildResult('success', safeOutputPath);
}
catch (writeError) {
logger.error(`[${this.config.id}] Failed to write output file:`, writeError);
return this.buildResult('failed', undefined, `Failed to write output: ${writeError instanceof Error ? writeError.message : 'Unknown error'}`);
}
}
else if (exitCode === 0) {
// Success but no output
return this.buildResult('success', outputPath);
}
else {
return this.buildResult('failed', undefined, `Exit code: ${exitCode}, stderr: ${this.stderr.join('')}`);
}
}
catch (error) {
logger.error(`Agent ${this.config.id} failed:`, error);
if (error instanceof Error) {
return this.buildResult('failed', undefined, error.message);
}
return this.buildResult('failed', undefined, 'Unknown error');
}
}
buildArgs(prompt) {
const args = [];
// Add custom flags
if (this.config.flags) {
args.push(...this.config.flags);
}
// Add prompt based on mode
switch (this.config.promptMode) {
case 'flag':
case 'arg':
args.push(prompt);
break;
case 'stdin':
// Prompt will be sent via stdin
break;
case 'env':
// Prompt is in environment variable
break;
}
// Add allowedTools flags (for Claude-compatible tools)
if (this.config.allowedTools && this.config.allowedTools.length > 0) {
logger.debug(`[${this.config.id}] Adding allowedTools:`, {
allowedTools: this.config.allowedTools
});
this.config.allowedTools.forEach(tool => {
args.push('--allowedTools', tool);
});
}
return args;
}
}
//# sourceMappingURL=generic-agent.js.map