UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

120 lines 8.38 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.registerDocProcGenerateCommand = registerDocProcGenerateCommand; 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/docProc/anthropic"); const openai_1 = require("../../agents/docProc/openai"); // ─── Command ────────────────────────────────────────────────────────────────── function registerDocProcGenerateCommand(generateCommand) { generateCommand .command('doc-proc') .description('Generate a document pipeline config.yaml and eval.yaml 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 files') .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', '4') .option('--maxTokens <n>', 'Maximum tokens per model response (defaults to 8096 for Anthropic, 16384 for OpenAI)') .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 configOut = path_1.default.join(outputDir, 'config.yaml'); const evalOut = path_1.default.join(outputDir, 'eval.yaml'); // ── Run agent ────────────────────────────────────────────────────────── console.log(chalk_1.default.bold('\nNestbox — Document Pipeline 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 maxTokens = options.maxTokens ? parseInt(options.maxTokens, 10) : undefined; const agentOptions = Object.assign(Object.assign({ instructions, model, maxIterations: parseInt((_b = options.maxIterations) !== null && _b !== void 0 ? _b : '4', 10) }, (maxTokens != null && { maxTokens })), { onProgress: (msg) => { spinner.text = msg; } }); const result = useAnthropic ? yield (0, anthropic_1.runDocProcAgent)(Object.assign(Object.assign({}, agentOptions), { anthropicApiKey: anthropicKey })) : yield (0, openai_1.runDocProcAgentWithOpenAI)(Object.assign(Object.assign({}, agentOptions), { openAiApiKey: openAiKey })); spinner.stop(); // ── Write output files ──────────────────────────────────────────────── const configWritten = result.configYaml.trim().length > 0; const evalWritten = result.evalYaml.trim().length > 0; if (configWritten) fs_1.default.writeFileSync(configOut, result.configYaml, 'utf8'); if (evalWritten) fs_1.default.writeFileSync(evalOut, result.evalYaml, 'utf8'); // ── Summary ────────────────────────────────────────────────────────── console.log(chalk_1.default.bold('Results')); if (configWritten) { const status = result.configValid ? chalk_1.default.green('✓ valid') : chalk_1.default.yellow('⚠ invalid'); console.log(` config.yaml ${status}${configOut}`); } else { console.log(` config.yaml ${chalk_1.default.red('✗ not generated')}`); } if (evalWritten) { const status = result.evalValid ? chalk_1.default.green('✓ valid') : chalk_1.default.yellow('⚠ invalid'); console.log(` eval.yaml ${status}${evalOut}`); } else { console.log(` eval.yaml ${chalk_1.default.red('✗ not generated')}`); } console.log(chalk_1.default.dim(`\n Completed in ${result.iterations} iteration(s).`)); const allDone = configWritten && evalWritten && result.configValid && result.evalValid; if (!allDone) { console.log(chalk_1.default.yellow('\nWarning: one or more files were not generated or have 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=docProc.js.map