forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprebuild-agents.ts
More file actions
203 lines (172 loc) · 5.4 KB
/
Copy pathprebuild-agents.ts
File metadata and controls
203 lines (172 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env bun
/**
* Prebuild script that scans the agents/ directory and generates a TypeScript
* module with all agent definitions embedded as static data.
*
* This allows agent definitions to be bundled into the CLI binary without
* requiring runtime filesystem access to the agents/ directory.
*
* Note: The agents/ directory (without dot) contains official bundled agents.
* The .agents/ directory is for user/project-specific agents loaded at runtime.
*
* Run this before building the binary:
* bun run scripts/prebuild-agents.ts
*/
import * as fs from 'fs'
import * as path from 'path'
const AGENTS_DIR = path.join(import.meta.dir, '../../agents')
const OUTPUT_FILE = path.join(import.meta.dir, '../src/agents/bundled-agents.generated.ts')
interface AgentDefinition {
id: string
displayName?: string
[key: string]: any
}
/**
* Recursively get all TypeScript files from a directory
*/
function getAllTsFiles(dir: string): string[] {
const files: string[] = []
try {
const entries = fs.readdirSync(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
// Skip __tests__ and node_modules directories
if (entry.name === '__tests__' || entry.name === 'node_modules' || entry.name === 'types') {
continue
}
files.push(...getAllTsFiles(fullPath))
} else if (
entry.isFile() &&
entry.name.endsWith('.ts') &&
!entry.name.endsWith('.d.ts') &&
!entry.name.endsWith('.test.ts')
) {
files.push(fullPath)
}
}
} catch (error) {
console.error(`Error reading directory ${dir}:`, error)
}
return files
}
/**
* Load and process an agent definition from a TypeScript file
*/
async function loadAgentDefinition(filePath: string): Promise<AgentDefinition | null> {
try {
// Use dynamic import to load the module
const module = await import(filePath)
const definition = module.default
if (!definition || !definition.id || !definition.model) {
return null
}
// Process the definition - convert handleSteps function to string
const processed: AgentDefinition = { ...definition }
if (typeof processed.handleSteps === 'function') {
processed.handleSteps = processed.handleSteps.toString()
}
return processed
} catch (error) {
console.error(`Error loading agent from ${filePath}:`, error)
return null
}
}
/**
* Generate the bundled agents TypeScript file
*/
function generateBundledAgentsFile(agents: Record<string, AgentDefinition>): string {
const agentCount = Object.keys(agents).length
return `/**
* AUTO-GENERATED FILE - DO NOT EDIT MANUALLY
*
* This file is generated by scripts/prebuild-agents.ts
* It contains all bundled agent definitions from the agents/ directory.
*
* Generated at: ${new Date().toISOString()}
* Agent count: ${agentCount}
*/
import type { LocalAgentInfo } from '../utils/local-agent-registry'
/**
* All bundled agent definitions keyed by their ID.
* These are the default Codebuff agents that ship with the CLI binary.
*/
export const bundledAgents: Record<string, any> = ${JSON.stringify(agents, null, 2)};
/**
* Get bundled agents as LocalAgentInfo format for the CLI
*/
export function getBundledAgentsAsLocalInfo(): LocalAgentInfo[] {
return Object.values(bundledAgents).map((agent) => ({
id: agent.id,
displayName: agent.displayName || agent.id,
filePath: '[bundled]',
isBundled: true,
}));
}
/**
* Get all bundled agent IDs
*/
export function getBundledAgentIds(): string[] {
return Object.keys(bundledAgents);
}
/**
* Check if an agent ID is a bundled agent
*/
export function isBundledAgent(agentId: string): boolean {
return agentId in bundledAgents;
}
`
}
async function main() {
const DEBUG = false
if (DEBUG) {
console.log('🔍 DEBUG: Scanning agents/ directory...')
}
if (!fs.existsSync(AGENTS_DIR)) {
console.error(`Error: agents/ directory not found at ${AGENTS_DIR}`)
// process.exit(1)
return
}
const tsFiles = getAllTsFiles(AGENTS_DIR)
if (DEBUG) {
console.log(`📁 DEBUG: Found ${tsFiles.length} TypeScript files`)
}
const agents: Record<string, AgentDefinition> = {}
let loadedCount = 0
let skippedCount = 0
for (const filePath of tsFiles) {
const relativePath = path.relative(AGENTS_DIR, filePath)
const definition = await loadAgentDefinition(filePath)
if (definition) {
agents[definition.id] = definition
loadedCount++
if (DEBUG) {
console.log(` ✅ DEBUG: ${definition.id} (${relativePath})`)
}
} else {
skippedCount++
if (DEBUG) {
console.log(` ⏭️ DEBUG: Skipped: ${relativePath} (no valid default export)`)
}
}
}
if (DEBUG) {
console.log(`\n📦 DEBUG: Loaded ${loadedCount} agents, skipped ${skippedCount} files`)
}
// Generate the output file
const output = generateBundledAgentsFile(agents)
// Ensure output directory exists
const outputDir = path.dirname(OUTPUT_FILE)
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
fs.writeFileSync(OUTPUT_FILE, output, 'utf-8')
if (DEBUG) {
console.log(`\n✨ DEBUG: Generated ${OUTPUT_FILE}`)
console.log(` DEBUG: ${Object.keys(agents).length} agents bundled`)
}
}
main().catch((error) => {
console.error('Fatal error:', error)
process.exit(1)
})