UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

110 lines 7.52 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerReportComposerGenerateCommand = registerReportComposerGenerateCommand; const chalk_1 = __importDefault(require("chalk")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const ora_1 = __importDefault(require("ora")); const anthropic_1 = require("../../agents/reportGenerator/anthropic"); const openai_1 = require("../../agents/reportGenerator/openai"); // ─── Command ────────────────────────────────────────────────────────────────── function registerReportComposerGenerateCommand(generateCommand) { generateCommand .command('report-composer') .description('Generate a report composer report.yaml configuration from an instructions file using Claude AI') .requiredOption('-f, --file <path>', 'Path to the instructions Markdown file') .requiredOption('-o, --output <dir>', 'Output directory for the generated report.yaml') .option('--anthropicApiKey <key>', 'Anthropic API key (or set ANTHROPIC_API_KEY env var)') .option('--openAiApiKey <key>', 'OpenAI API key (or set OPENAI_API_KEY env var)') .option('--model <model>', 'Model ID (defaults to claude-sonnet-4-6 for Anthropic, gpt-4o for OpenAI)') .option('--maxIterations <n>', 'Maximum agent iterations', '5') .action((options) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; // ── Resolve API keys ──────────────────────────────────────────────────── const anthropicKey = options.anthropicApiKey || process.env.ANTHROPIC_API_KEY; const openAiKey = options.openAiApiKey || process.env.OPENAI_API_KEY; if (!anthropicKey && !openAiKey) { console.error(chalk_1.default.red('Error: An API key is required. Provide --anthropicApiKey / ANTHROPIC_API_KEY or --openAiApiKey / OPENAI_API_KEY.')); process.exit(1); } // Anthropic takes precedence when both are available const useAnthropic = !!anthropicKey; const provider = useAnthropic ? 'Claude (Anthropic)' : 'GPT (OpenAI)'; const defaultModel = useAnthropic ? 'claude-sonnet-4-6' : 'gpt-4o'; const model = (_a = options.model) !== null && _a !== void 0 ? _a : defaultModel; // ── Read instructions file ────────────────────────────────────────────── const instructionsPath = path_1.default.resolve(options.file); if (!fs_1.default.existsSync(instructionsPath)) { console.error(chalk_1.default.red(`Error: Instructions file not found: ${instructionsPath}`)); process.exit(1); } const instructions = fs_1.default.readFileSync(instructionsPath, 'utf8'); if (!instructions.trim()) { console.error(chalk_1.default.red('Error: Instructions file is empty.')); process.exit(1); } // ── Ensure output directory exists ───────────────────────────────────── const outputDir = path_1.default.resolve(options.output); fs_1.default.mkdirSync(outputDir, { recursive: true }); const reportOut = path_1.default.join(outputDir, 'report.yaml'); // ── Run agent ────────────────────────────────────────────────────────── console.log(chalk_1.default.bold('\nNestbox — Report Composer Generator')); console.log(chalk_1.default.dim(`Instructions: ${instructionsPath}`)); console.log(chalk_1.default.dim(`Output: ${outputDir}`)); console.log(chalk_1.default.dim(`Provider: ${provider}`)); console.log(chalk_1.default.dim(`Model: ${model}`)); console.log(); const spinner = (0, ora_1.default)('Initialising agent...').start(); try { const agentOptions = { instructions, model, maxIterations: parseInt((_b = options.maxIterations) !== null && _b !== void 0 ? _b : '5', 10), onProgress: (msg) => { spinner.text = msg; }, }; const result = useAnthropic ? yield (0, anthropic_1.runReportComposerAgent)(Object.assign(Object.assign({}, agentOptions), { anthropicApiKey: anthropicKey })) : yield (0, openai_1.runReportComposerAgentWithOpenAI)(Object.assign(Object.assign({}, agentOptions), { openAiApiKey: openAiKey })); spinner.stop(); // ── Write output file ──────────────────────────────────────────────── const reportWritten = result.reportYaml.trim().length > 0; if (reportWritten) fs_1.default.writeFileSync(reportOut, result.reportYaml, 'utf8'); // ── Summary ────────────────────────────────────────────────────────── console.log(chalk_1.default.bold('Results')); if (reportWritten) { const status = result.reportValid ? chalk_1.default.green('✓ valid') : chalk_1.default.yellow('⚠ invalid'); console.log(` report.yaml ${status}${reportOut}`); } else { console.log(` report.yaml ${chalk_1.default.red('✗ not generated')}`); } console.log(chalk_1.default.dim(`\n Completed in ${result.iterations} iteration(s).`)); if (!reportWritten || !result.reportValid) { console.log(chalk_1.default.yellow('\nWarning: report.yaml was not generated or has validation issues.')); process.exit(1); } console.log(chalk_1.default.green('\nDone.')); } catch (err) { spinner.stop(); console.error(chalk_1.default.red(`\nError: ${(_c = err === null || err === void 0 ? void 0 : err.message) !== null && _c !== void 0 ? _c : err}`)); process.exit(1); } })); } //# sourceMappingURL=reportComposer.js.map