|
| 1 | +import type { |
| 2 | + AgentDefinition, |
| 3 | + AgentStepContext, |
| 4 | +} from '../types/agent-definition' |
| 5 | + |
| 6 | +const definition: AgentDefinition = { |
| 7 | + id: 'codebase-analyzer', |
| 8 | + displayName: 'CodeBase Analyzer', |
| 9 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 10 | + |
| 11 | + spawnerPrompt: |
| 12 | + 'Analyzes codebase implementation details. Call the codebase-analyzer agent when you need to find detailed information about specific components. As always, the more detailed your request prompt, the better! :)', |
| 13 | + |
| 14 | + inputSchema: { |
| 15 | + prompt: { |
| 16 | + type: 'string', |
| 17 | + description: |
| 18 | + 'What specific component, feature, or implementation details you need analyzed. Be as specific as possible about what you want to understand.', |
| 19 | + }, |
| 20 | + }, |
| 21 | + |
| 22 | + outputMode: 'structured_output', |
| 23 | + includeMessageHistory: false, |
| 24 | + |
| 25 | + outputSchema: { |
| 26 | + type: 'object', |
| 27 | + properties: { |
| 28 | + title: { |
| 29 | + type: 'string', |
| 30 | + description: 'Title in format "Analysis: [Feature/Component Name]"', |
| 31 | + }, |
| 32 | + overview: { |
| 33 | + type: 'string', |
| 34 | + description: '2-3 sentence summary of how it works', |
| 35 | + }, |
| 36 | + entryPoints: { |
| 37 | + type: 'array', |
| 38 | + description: 'Entry points into the component', |
| 39 | + items: { |
| 40 | + type: 'object', |
| 41 | + properties: { |
| 42 | + location: { |
| 43 | + type: 'string', |
| 44 | + description: |
| 45 | + 'File path with line number, e.g. "api/routes.js:45"', |
| 46 | + }, |
| 47 | + description: { |
| 48 | + type: 'string', |
| 49 | + description: 'What this entry point does', |
| 50 | + }, |
| 51 | + }, |
| 52 | + required: ['location', 'description'], |
| 53 | + }, |
| 54 | + }, |
| 55 | + coreImplementation: { |
| 56 | + type: 'array', |
| 57 | + description: 'Detailed breakdown of core implementation steps', |
| 58 | + items: { |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + stepName: { |
| 62 | + type: 'string', |
| 63 | + description: 'Name of the implementation step', |
| 64 | + }, |
| 65 | + location: { |
| 66 | + type: 'string', |
| 67 | + description: |
| 68 | + 'File path with line numbers, e.g. "handlers/webhook.js:15-32"', |
| 69 | + }, |
| 70 | + details: { |
| 71 | + type: 'array', |
| 72 | + description: 'Detailed explanation points', |
| 73 | + items: { type: 'string' }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + required: ['stepName', 'location', 'details'], |
| 77 | + }, |
| 78 | + }, |
| 79 | + dataFlow: { |
| 80 | + type: 'array', |
| 81 | + description: 'Step-by-step data flow through the system', |
| 82 | + items: { |
| 83 | + type: 'object', |
| 84 | + properties: { |
| 85 | + step: { type: 'number', description: 'Step number in the flow' }, |
| 86 | + description: { |
| 87 | + type: 'string', |
| 88 | + description: 'What happens at this step', |
| 89 | + }, |
| 90 | + location: { |
| 91 | + type: 'string', |
| 92 | + description: 'File path with line number', |
| 93 | + }, |
| 94 | + }, |
| 95 | + required: ['step', 'description', 'location'], |
| 96 | + }, |
| 97 | + }, |
| 98 | + keyPatterns: { |
| 99 | + type: 'array', |
| 100 | + description: 'Key architectural patterns identified', |
| 101 | + items: { |
| 102 | + type: 'object', |
| 103 | + properties: { |
| 104 | + patternName: { type: 'string', description: 'Name of the pattern' }, |
| 105 | + description: { |
| 106 | + type: 'string', |
| 107 | + description: 'How the pattern is implemented', |
| 108 | + }, |
| 109 | + location: { |
| 110 | + type: 'string', |
| 111 | + description: 'Where this pattern is found', |
| 112 | + }, |
| 113 | + }, |
| 114 | + required: ['patternName', 'description'], |
| 115 | + }, |
| 116 | + }, |
| 117 | + configuration: { |
| 118 | + type: 'array', |
| 119 | + description: 'Configuration settings and their locations', |
| 120 | + items: { |
| 121 | + type: 'object', |
| 122 | + properties: { |
| 123 | + setting: { type: 'string', description: 'What is configured' }, |
| 124 | + location: { |
| 125 | + type: 'string', |
| 126 | + description: 'File path with line number', |
| 127 | + }, |
| 128 | + description: { |
| 129 | + type: 'string', |
| 130 | + description: 'What this configuration controls', |
| 131 | + }, |
| 132 | + }, |
| 133 | + required: ['setting', 'location', 'description'], |
| 134 | + }, |
| 135 | + }, |
| 136 | + errorHandling: { |
| 137 | + type: 'array', |
| 138 | + description: 'Error handling mechanisms', |
| 139 | + items: { |
| 140 | + type: 'object', |
| 141 | + properties: { |
| 142 | + errorType: { |
| 143 | + type: 'string', |
| 144 | + description: 'Type of error or scenario', |
| 145 | + }, |
| 146 | + location: { |
| 147 | + type: 'string', |
| 148 | + description: 'File path with line number', |
| 149 | + }, |
| 150 | + mechanism: { |
| 151 | + type: 'string', |
| 152 | + description: 'How the error is handled', |
| 153 | + }, |
| 154 | + }, |
| 155 | + required: ['errorType', 'location', 'mechanism'], |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + required: ['title', 'overview'], |
| 160 | + }, |
| 161 | + |
| 162 | + toolNames: [ |
| 163 | + 'read_files', |
| 164 | + 'code_search', |
| 165 | + 'find_files', |
| 166 | + 'add_message', |
| 167 | + 'end_turn', |
| 168 | + 'set_output', |
| 169 | + ], |
| 170 | + spawnableAgents: [], |
| 171 | + |
| 172 | + systemPrompt: `# Persona: CodeBase Analyzer |
| 173 | +
|
| 174 | +You are a specialist at understanding HOW code works. Your job is to analyze implementation details, trace data flow, and explain technical workings with precise file:line references. |
| 175 | +
|
| 176 | +## Core Responsibilities |
| 177 | +
|
| 178 | +1. **Analyze Implementation Details** |
| 179 | + - Read specific files to understand logic |
| 180 | + - Identify key functions and their purposes |
| 181 | + - Trace method calls and data transformations |
| 182 | + - Note important algorithms or patterns |
| 183 | +
|
| 184 | +2. **Trace Data Flow** |
| 185 | + - Follow data from entry to exit points |
| 186 | + - Map transformations and validations |
| 187 | + - Identify state changes and side effects |
| 188 | + - Document API contracts between components |
| 189 | +
|
| 190 | +3. **Identify Architectural Patterns** |
| 191 | + - Recognize design patterns in use |
| 192 | + - Note architectural decisions |
| 193 | + - Identify conventions and best practices |
| 194 | + - Find integration points between systems |
| 195 | +
|
| 196 | +## Analysis Strategy |
| 197 | +
|
| 198 | +### Step 1: Read Entry Points |
| 199 | +- Start with main files mentioned in the request |
| 200 | +- Look for exports, public methods, or route handlers |
| 201 | +- Identify the "surface area" of the component |
| 202 | +
|
| 203 | +### Step 2: Follow the Code Path |
| 204 | +- Trace function calls step by step |
| 205 | +- Read each file involved in the flow |
| 206 | +- Note where data is transformed |
| 207 | +- Identify external dependencies |
| 208 | +- Take time to deeply understand how all these pieces connect and interact |
| 209 | +
|
| 210 | +### Step 3: Understand Key Logic |
| 211 | +- Focus on business logic, not boilerplate |
| 212 | +- Identify validation, transformation, error handling |
| 213 | +- Note any complex algorithms or calculations |
| 214 | +- Look for configuration or feature flags |
| 215 | +
|
| 216 | +## Important Guidelines |
| 217 | +
|
| 218 | +- **Always include file:line references** for claims |
| 219 | +- **Read files thoroughly** before making statements |
| 220 | +- **Trace actual code paths** don't assume |
| 221 | +- **Focus on "how"** not "what" or "why" |
| 222 | +- **Be precise** about function names and variables |
| 223 | +- **Note exact transformations** with before/after |
| 224 | +
|
| 225 | +## What NOT to Do |
| 226 | +
|
| 227 | +- Don't guess about implementation |
| 228 | +- Don't skip error handling or edge cases |
| 229 | +- Don't ignore configuration or dependencies |
| 230 | +- Don't make architectural recommendations |
| 231 | +- Don't analyze code quality or suggest improvements |
| 232 | +
|
| 233 | +Remember: You're explaining HOW the code currently works, with surgical precision and exact references. Help users understand the implementation as it exists today.`, |
| 234 | + |
| 235 | + instructionsPrompt: `Analyze the requested component or feature in detail. Follow this structure: |
| 236 | +
|
| 237 | +## Analysis: [Feature/Component Name] |
| 238 | +
|
| 239 | +### Overview |
| 240 | +[2-3 sentence summary of how it works] |
| 241 | +
|
| 242 | +### Entry Points |
| 243 | +- \`file.js:45\` - Function or endpoint description |
| 244 | +- \`handler.js:12\` - Key method description |
| 245 | +
|
| 246 | +### Core Implementation |
| 247 | +
|
| 248 | +#### 1. [Step Name] (\`file.js:15-32\`) |
| 249 | +- Detailed explanation with exact line references |
| 250 | +- What happens at each step |
| 251 | +- Any validation or error handling |
| 252 | +
|
| 253 | +#### 2. [Next Step] (\`service.js:8-45\`) |
| 254 | +- Continue tracing the flow |
| 255 | +- Note data transformations |
| 256 | +- Identify side effects |
| 257 | +
|
| 258 | +### Data Flow |
| 259 | +1. Entry at \`file.js:45\` |
| 260 | +2. Processing at \`handler.js:12\` |
| 261 | +3. Storage at \`store.js:55\` |
| 262 | +
|
| 263 | +### Key Patterns |
| 264 | +- **Pattern Name**: Description with file references |
| 265 | +- **Architecture**: How components interact |
| 266 | +
|
| 267 | +### Configuration |
| 268 | +- Settings locations with file:line references |
| 269 | +- Feature flags and their effects |
| 270 | +
|
| 271 | +### Error Handling |
| 272 | +- How errors are caught and handled |
| 273 | +- Retry logic and fallbacks |
| 274 | +
|
| 275 | +Use the read_files, code_search, and find_files tools to gather information, then provide a comprehensive analysis with exact file:line references.`, |
| 276 | + |
| 277 | + stepPrompt: `Focus on understanding HOW the code works. Read files, trace execution paths, and provide precise implementation details with exact file:line references.`, |
| 278 | + |
| 279 | + handleSteps: function* ({ |
| 280 | + agentState: initialAgentState, |
| 281 | + prompt, |
| 282 | + }: AgentStepContext) { |
| 283 | + let agentState = initialAgentState |
| 284 | + const stepLimit = 15 |
| 285 | + let stepCount = 0 |
| 286 | + |
| 287 | + while (true) { |
| 288 | + stepCount++ |
| 289 | + |
| 290 | + const stepResult = yield 'STEP' |
| 291 | + agentState = stepResult.agentState |
| 292 | + |
| 293 | + if (stepResult.stepsComplete) { |
| 294 | + break |
| 295 | + } |
| 296 | + |
| 297 | + if (stepCount === stepLimit - 1) { |
| 298 | + yield { |
| 299 | + toolName: 'add_message', |
| 300 | + input: { |
| 301 | + role: 'user', |
| 302 | + content: |
| 303 | + 'Please finish your analysis now using the exact format specified in your instructions. Make sure to include all required sections: Overview, Entry Points, Core Implementation, Data Flow, Key Patterns, Configuration, and Error Handling with precise file:line references.', |
| 304 | + }, |
| 305 | + includeToolCall: false, |
| 306 | + } |
| 307 | + |
| 308 | + const finalStepResult = yield 'STEP' |
| 309 | + agentState = finalStepResult.agentState |
| 310 | + break |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + // Final enforcement message if analysis doesn't follow format |
| 315 | + const lastMessage = |
| 316 | + agentState.messageHistory[agentState.messageHistory.length - 1] |
| 317 | + if (lastMessage?.role === 'assistant' && lastMessage.content) { |
| 318 | + const content = |
| 319 | + typeof lastMessage.content === 'string' ? lastMessage.content : '' |
| 320 | + if ( |
| 321 | + !content.includes('## Analysis:') || |
| 322 | + !content.includes('### Overview') || |
| 323 | + !content.includes('### Entry Points') |
| 324 | + ) { |
| 325 | + yield { |
| 326 | + toolName: 'add_message', |
| 327 | + input: { |
| 328 | + role: 'user', |
| 329 | + content: |
| 330 | + 'Your analysis must follow the exact format:\n\n## Analysis: [Feature/Component Name]\n\n### Overview\n[2-3 sentence summary]\n\n### Entry Points\n- `file.js:45` - Function description\n\n### Core Implementation\n\n#### 1. [Step Name] (`file.js:15-32`)\n- Detailed explanation\n\n### Data Flow\n1. Entry at `file.js:45`\n\n### Key Patterns\n- **Pattern Name**: Description\n\n### Configuration\n- Settings locations\n\n### Error Handling\n- How errors are handled\n\nPlease reformat your response to match this structure exactly.', |
| 331 | + }, |
| 332 | + includeToolCall: false, |
| 333 | + } |
| 334 | + |
| 335 | + yield 'STEP' |
| 336 | + } |
| 337 | + } |
| 338 | + }, |
| 339 | +} |
| 340 | + |
| 341 | +export default definition |
0 commit comments