Skip to content

Commit 92dad7a

Browse files
committed
init-agents command
1 parent abe7d7c commit 92dad7a

File tree

3 files changed

+151
-110
lines changed

3 files changed

+151
-110
lines changed

npm-app/src/cli-handlers/agents.ts

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,13 @@ import {
88
} from '@codebuff/common/util/agent-file-utils'
99
import { green, yellow, cyan, magenta, bold, gray, red } from 'picocolors'
1010

11-
// Import files to replicate in the user's .agents directory. Bun bundler requires relative paths.
12-
13-
import basicDiffReviewer from '../../../common/src/templates/initial-agents-dir/examples/01-basic-diff-reviewer' with { type: 'text' }
14-
import intermediateGitCommitter from '../../../common/src/templates/initial-agents-dir/examples/02-intermediate-git-committer' with { type: 'text' }
15-
import advancedFileExplorer from '../../../common/src/templates/initial-agents-dir/examples/03-advanced-file-explorer' with { type: 'text' }
16-
import myCustomAgent from '../../../common/src/templates/initial-agents-dir/my-custom-agent' with { type: 'text' }
17-
18-
// @ts-ignore - No default import, but we are importing as text so it's fine
19-
// @ts-ignore - It complains about the .md file, but it works.
20-
import readmeContent from '../../../common/src/templates/initial-agents-dir/README.md' with { type: 'text' }
21-
// @ts-ignore - No default import, but we are importing as text so it's fine
22-
import agentDefinitionTypes from '../../../common/src/templates/initial-agents-dir/types/agent-definition' with { type: 'text' }
23-
// @ts-ignore - No default import, but we are importing as text so it's fine
24-
import messageTypes from '../../../common/src/templates/initial-agents-dir/types/codebuff-message' with { type: 'text' }
25-
// @ts-ignore - No default import, but we are importing as text so it's fine
26-
import contentPartTypes from '../../../common/src/templates/initial-agents-dir/types/content-part' with { type: 'text' }
27-
// @ts-ignore - No default import, but we are importing as text so it's fine
28-
import dataContentTypes from '../../../common/src/templates/initial-agents-dir/types/data-content' with { type: 'text' }
29-
// @ts-ignore - No default import, but we are importing as text so it's fine
30-
import jsonTypes from '../../../common/src/templates/initial-agents-dir/types/json' with { type: 'text' }
31-
// @ts-ignore - No default import, but we are importing as text so it's fine
32-
import providerMetadataTypes from '../../../common/src/templates/initial-agents-dir/types/provider-metadata' with { type: 'text' }
33-
// @ts-ignore - No default import, but we are importing as text so it's fine
34-
import toolsTypes from '../../../common/src/templates/initial-agents-dir/types/tools' with { type: 'text' }
3511
import {
3612
loadLocalAgents,
3713
getLoadedAgentNames,
3814
loadedAgents,
3915
} from '../agents/load-agents'
4016
import { CLI } from '../cli'
17+
import { createExampleAgentFiles } from './init-agents'
4118
import { getProjectRoot } from '../project-files'
4219
import { Spinner } from '../utils/spinner'
4320
import {
@@ -638,93 +615,7 @@ async function startDirectAgentCreation(onExit: () => void) {
638615
onExit()
639616
}
640617

641-
async function createExampleAgentFiles() {
642-
const agentsDir = path.join(getProjectRoot(), AGENT_TEMPLATES_DIR)
643-
const typesDir = path.join(agentsDir, 'types')
644-
const examplesDir = path.join(agentsDir, 'examples')
645-
646-
// Create directories
647-
if (!fs.existsSync(agentsDir)) {
648-
fs.mkdirSync(agentsDir, { recursive: true })
649-
}
650-
if (!fs.existsSync(typesDir)) {
651-
fs.mkdirSync(typesDir, { recursive: true })
652-
}
653-
if (!fs.existsSync(examplesDir)) {
654-
fs.mkdirSync(examplesDir, { recursive: true })
655-
}
656-
657-
const filesToCreate = [
658-
{
659-
path: path.join(agentsDir, 'README.md'),
660-
content: readmeContent,
661-
description: 'Documentation for your agents',
662-
},
663-
{
664-
path: path.join(typesDir, 'agent-definition.ts'),
665-
content: agentDefinitionTypes,
666-
description: 'TypeScript type definitions for agents',
667-
},
668-
{
669-
path: path.join(typesDir, 'tools.ts'),
670-
content: toolsTypes,
671-
description: 'TypeScript type definitions for tools',
672-
},
673-
{
674-
path: path.join(typesDir, 'codebuff-message.ts'),
675-
content: messageTypes,
676-
description: 'TypeScript type definitions for messages',
677-
},
678-
{
679-
path: path.join(typesDir, 'content-part.ts'),
680-
content: contentPartTypes,
681-
description: 'TypeScript type definitions for content parts',
682-
},
683-
{
684-
path: path.join(typesDir, 'data-content.ts'),
685-
content: dataContentTypes,
686-
description: 'TypeScript type definitions for data content',
687-
},
688-
{
689-
path: path.join(typesDir, 'provider-metadata.ts'),
690-
content: providerMetadataTypes,
691-
description: 'TypeScript type definitions for provider metadata',
692-
},
693-
{
694-
path: path.join(typesDir, 'json.ts'),
695-
content: jsonTypes,
696-
description: 'TypeScript type definitions for JSON',
697-
},
698-
{
699-
path: path.join(agentsDir, 'my-custom-agent.ts'),
700-
content: myCustomAgent,
701-
description: 'Your first custom agent example',
702-
},
703-
{
704-
path: path.join(examplesDir, '01-basic-diff-reviewer.ts'),
705-
content: basicDiffReviewer,
706-
description: 'Basic diff reviewer agent example',
707-
},
708-
{
709-
path: path.join(examplesDir, '02-intermediate-git-committer.ts'),
710-
content: intermediateGitCommitter,
711-
description: 'Intermediate git commiter agent example',
712-
},
713-
{
714-
path: path.join(examplesDir, '03-advanced-file-explorer.ts'),
715-
content: advancedFileExplorer,
716-
description: 'Advanced file explorer agent example',
717-
},
718-
]
719-
720-
console.log(green('\n📁 Creating agent files:'))
721618

722-
for (const file of filesToCreate) {
723-
fs.writeFileSync(file.path, file.content)
724-
const relativePath = path.relative(getProjectRoot(), file.path)
725-
console.log(gray(` ✓ ${relativePath} - ${file.description}`))
726-
}
727-
}
728619

729620
// Cleanup function
730621
export function cleanupAgentsBuffer() {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

npm-app/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { displayLoadedAgents, loadLocalAgents } from './agents/load-agents'
99
import { CLI } from './cli'
1010
import { cliArguments, cliOptions } from './cli-definitions'
1111
import { handlePublish } from './cli-handlers/publish'
12+
import { handleInitAgents } from './cli-handlers/init-agents'
1213
import { npmAppVersion, backendUrl } from './config'
1314
import { createTemplateProject } from './create-template-project'
1415
import { printModeLog, setPrintMode } from './display/print-mode'
@@ -153,6 +154,12 @@ For all commands and options, run 'codebuff' and then type 'help'.
153154
process.exit(0)
154155
}
155156

157+
// Handle init-agents command
158+
if (args[0] === 'init-agents') {
159+
await handleInitAgents()
160+
process.exit(0)
161+
}
162+
156163
// Handle deprecated --pro flag
157164
if (options.pro) {
158165
console.error(

0 commit comments

Comments
 (0)