|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import { AGENT_TEMPLATES_DIR } from '@codebuff/common/constants' |
| 5 | +import { green, gray } from 'picocolors' |
| 6 | + |
| 7 | +// Import files to replicate in the user's .agents directory. Bun bundler requires relative paths. |
| 8 | + |
| 9 | +import basicDiffReviewer from '../../../common/src/templates/initial-agents-dir/examples/01-basic-diff-reviewer' with { type: 'text' } |
| 10 | +import intermediateGitCommitter from '../../../common/src/templates/initial-agents-dir/examples/02-intermediate-git-committer' with { type: 'text' } |
| 11 | +import advancedFileExplorer from '../../../common/src/templates/initial-agents-dir/examples/03-advanced-file-explorer' with { type: 'text' } |
| 12 | +import myCustomAgent from '../../../common/src/templates/initial-agents-dir/my-custom-agent' with { type: 'text' } |
| 13 | + |
| 14 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 15 | +// @ts-ignore - It complains about the .md file, but it works. |
| 16 | +import readmeContent from '../../../common/src/templates/initial-agents-dir/README.md' with { type: 'text' } |
| 17 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 18 | +import agentDefinitionTypes from '../../../common/src/templates/initial-agents-dir/types/agent-definition' with { type: 'text' } |
| 19 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 20 | +import messageTypes from '../../../common/src/templates/initial-agents-dir/types/codebuff-message' with { type: 'text' } |
| 21 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 22 | +import contentPartTypes from '../../../common/src/templates/initial-agents-dir/types/content-part' with { type: 'text' } |
| 23 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 24 | +import dataContentTypes from '../../../common/src/templates/initial-agents-dir/types/data-content' with { type: 'text' } |
| 25 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 26 | +import jsonTypes from '../../../common/src/templates/initial-agents-dir/types/json' with { type: 'text' } |
| 27 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 28 | +import providerMetadataTypes from '../../../common/src/templates/initial-agents-dir/types/provider-metadata' with { type: 'text' } |
| 29 | +// @ts-ignore - No default import, but we are importing as text so it's fine |
| 30 | +import toolsTypes from '../../../common/src/templates/initial-agents-dir/types/tools' with { type: 'text' } |
| 31 | + |
| 32 | +import { getProjectRoot } from '../project-files' |
| 33 | + |
| 34 | +/** |
| 35 | + * Create example agent files in the .agents directory |
| 36 | + * This function is shared between the /agents command and the codebuff init-agents command |
| 37 | + */ |
| 38 | +export async function createExampleAgentFiles() { |
| 39 | + const agentsDir = path.join(getProjectRoot(), AGENT_TEMPLATES_DIR) |
| 40 | + const typesDir = path.join(agentsDir, 'types') |
| 41 | + const examplesDir = path.join(agentsDir, 'examples') |
| 42 | + |
| 43 | + // Create directories |
| 44 | + if (!fs.existsSync(agentsDir)) { |
| 45 | + fs.mkdirSync(agentsDir, { recursive: true }) |
| 46 | + } |
| 47 | + if (!fs.existsSync(typesDir)) { |
| 48 | + fs.mkdirSync(typesDir, { recursive: true }) |
| 49 | + } |
| 50 | + if (!fs.existsSync(examplesDir)) { |
| 51 | + fs.mkdirSync(examplesDir, { recursive: true }) |
| 52 | + } |
| 53 | + |
| 54 | + const filesToCreate = [ |
| 55 | + { |
| 56 | + path: path.join(agentsDir, 'README.md'), |
| 57 | + content: readmeContent, |
| 58 | + description: 'Documentation for your agents', |
| 59 | + }, |
| 60 | + { |
| 61 | + path: path.join(typesDir, 'agent-definition.ts'), |
| 62 | + content: agentDefinitionTypes, |
| 63 | + description: 'TypeScript type definitions for agents', |
| 64 | + }, |
| 65 | + { |
| 66 | + path: path.join(typesDir, 'tools.ts'), |
| 67 | + content: toolsTypes, |
| 68 | + description: 'TypeScript type definitions for tools', |
| 69 | + }, |
| 70 | + { |
| 71 | + path: path.join(typesDir, 'codebuff-message.ts'), |
| 72 | + content: messageTypes, |
| 73 | + description: 'TypeScript type definitions for messages', |
| 74 | + }, |
| 75 | + { |
| 76 | + path: path.join(typesDir, 'content-part.ts'), |
| 77 | + content: contentPartTypes, |
| 78 | + description: 'TypeScript type definitions for content parts', |
| 79 | + }, |
| 80 | + { |
| 81 | + path: path.join(typesDir, 'data-content.ts'), |
| 82 | + content: dataContentTypes, |
| 83 | + description: 'TypeScript type definitions for data content', |
| 84 | + }, |
| 85 | + { |
| 86 | + path: path.join(typesDir, 'provider-metadata.ts'), |
| 87 | + content: providerMetadataTypes, |
| 88 | + description: 'TypeScript type definitions for provider metadata', |
| 89 | + }, |
| 90 | + { |
| 91 | + path: path.join(typesDir, 'json.ts'), |
| 92 | + content: jsonTypes, |
| 93 | + description: 'TypeScript type definitions for JSON', |
| 94 | + }, |
| 95 | + { |
| 96 | + path: path.join(agentsDir, 'my-custom-agent.ts'), |
| 97 | + content: myCustomAgent, |
| 98 | + description: 'Your first custom agent example', |
| 99 | + }, |
| 100 | + { |
| 101 | + path: path.join(examplesDir, '01-basic-diff-reviewer.ts'), |
| 102 | + content: basicDiffReviewer, |
| 103 | + description: 'Basic diff reviewer agent example', |
| 104 | + }, |
| 105 | + { |
| 106 | + path: path.join(examplesDir, '02-intermediate-git-committer.ts'), |
| 107 | + content: intermediateGitCommitter, |
| 108 | + description: 'Intermediate git commiter agent example', |
| 109 | + }, |
| 110 | + { |
| 111 | + path: path.join(examplesDir, '03-advanced-file-explorer.ts'), |
| 112 | + content: advancedFileExplorer, |
| 113 | + description: 'Advanced file explorer agent example', |
| 114 | + }, |
| 115 | + ] |
| 116 | + |
| 117 | + console.log(green('\n📁 Creating agent files:')) |
| 118 | + |
| 119 | + for (const file of filesToCreate) { |
| 120 | + fs.writeFileSync(file.path, file.content) |
| 121 | + const relativePath = path.relative(getProjectRoot(), file.path) |
| 122 | + console.log(gray(` ✓ ${relativePath} - ${file.description}`)) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Handle the codebuff init-agents command |
| 128 | + */ |
| 129 | +export async function handleInitAgents() { |
| 130 | + try { |
| 131 | + await createExampleAgentFiles() |
| 132 | + console.log(green('\n✅ Created example agent files in .agents directory!')) |
| 133 | + console.log( |
| 134 | + gray('Check out the files and edit them to create your custom agents.'), |
| 135 | + ) |
| 136 | + console.log( |
| 137 | + gray('Run "codebuff --agent your-agent-id" to test your agents.'), |
| 138 | + ) |
| 139 | + } catch (error) { |
| 140 | + console.error('Error creating example files:', error) |
| 141 | + process.exit(1) |
| 142 | + } |
| 143 | +} |
0 commit comments