|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import { codebuffConfigFile } from '@codebuff/common/json-config/constants' |
| 5 | +import { green, red, yellow, cyan, gray } from 'picocolors' |
| 6 | + |
| 7 | +import { getProjectRoot } from '../project-files' |
| 8 | +import { loadRawCodebuffConfig } from '../json-config/parser' |
| 9 | + |
| 10 | +/** |
| 11 | + * Add one or more agent IDs to the addedSpawnableAgents field in codebuff.json |
| 12 | + * @param agentIds Array of agent IDs to add |
| 13 | + */ |
| 14 | +export async function handleSaveAgent(agentIds: string[]): Promise<void> { |
| 15 | + if (!agentIds || agentIds.length === 0) { |
| 16 | + console.log( |
| 17 | + red( |
| 18 | + 'Agent ID is required. Usage: codebuff save-agent <agent-id> [agent-id2] ...', |
| 19 | + ), |
| 20 | + ) |
| 21 | + console.log( |
| 22 | + gray( |
| 23 | + 'Example: codebuff save-agent codebuff/file-picker@1.0.0 codebuff/reviewer@1.0.0', |
| 24 | + ), |
| 25 | + ) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + const projectRoot = getProjectRoot() |
| 30 | + const configPath = path.join(projectRoot, codebuffConfigFile) |
| 31 | + |
| 32 | + try { |
| 33 | + // Use the safe configuration loader that handles JSONC |
| 34 | + const config = loadRawCodebuffConfig() |
| 35 | + |
| 36 | + // Initialize addedSpawnableAgents if it doesn't exist |
| 37 | + if (!config.addedSpawnableAgents) { |
| 38 | + config.addedSpawnableAgents = [] |
| 39 | + } |
| 40 | + |
| 41 | + // Track which agents were added vs already existed |
| 42 | + const newAgents: string[] = [] |
| 43 | + const existingAgents: string[] = [] |
| 44 | + |
| 45 | + for (const agentId of agentIds) { |
| 46 | + if (config.addedSpawnableAgents.includes(agentId)) { |
| 47 | + existingAgents.push(agentId) |
| 48 | + } else { |
| 49 | + config.addedSpawnableAgents.push(agentId) |
| 50 | + newAgents.push(agentId) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Write the updated configuration back to file |
| 55 | + const configJson = JSON.stringify(config, null, 2) |
| 56 | + fs.writeFileSync(configPath, configJson + '\n') |
| 57 | + |
| 58 | + // Provide feedback to the user |
| 59 | + if (newAgents.length > 0) { |
| 60 | + console.log( |
| 61 | + green( |
| 62 | + `✅ Successfully added ${newAgents.length} agent(s) to codebuff.json:`, |
| 63 | + ), |
| 64 | + ) |
| 65 | + newAgents.forEach((agentId) => { |
| 66 | + console.log(cyan(` + ${agentId}`)) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + if (existingAgents.length > 0) { |
| 71 | + console.log( |
| 72 | + yellow( |
| 73 | + `⚠️ ${existingAgents.length} agent(s) were already in the list:`, |
| 74 | + ), |
| 75 | + ) |
| 76 | + existingAgents.forEach((agentId) => { |
| 77 | + console.log(gray(` • ${agentId}`)) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + if (newAgents.length > 0) { |
| 82 | + console.log() |
| 83 | + console.log( |
| 84 | + gray('These agents are now available for spawning by the base agent.'), |
| 85 | + ) |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + console.log( |
| 89 | + red( |
| 90 | + `Error updating codebuff.json: ${error instanceof Error ? error.message : String(error)}`, |
| 91 | + ), |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
0 commit comments