|
| 1 | +import { AgentDefinition, StepText } from 'types/agent-definition' |
| 2 | +import { publisher } from '../constants' |
| 3 | + |
| 4 | +export const createCodeEditor = (options: { |
| 5 | + model: 'gpt-5' | 'opus' |
| 6 | +}): Omit<AgentDefinition, 'id'> => { |
| 7 | + const { model } = options |
| 8 | + return { |
| 9 | + publisher, |
| 10 | + model: |
| 11 | + options.model === 'gpt-5' |
| 12 | + ? 'openai/gpt-5.1' |
| 13 | + : 'anthropic/claude-opus-4.5', |
| 14 | + displayName: 'Code Editor', |
| 15 | + spawnerPrompt: |
| 16 | + 'Expert code reviewer that reviews recent code changes and makes improvements.', |
| 17 | + outputMode: 'structured_output', |
| 18 | + toolNames: ['write_file', 'str_replace', 'set_output'], |
| 19 | + |
| 20 | + includeMessageHistory: true, |
| 21 | + inheritParentSystemPrompt: true, |
| 22 | + |
| 23 | + instructionsPrompt: `You are an expert code reviewer with deep understanding of software engineering principles. You were spawned to review recent code changes and make improvements. Do not spawn a reviewer agent, you are the reviewer agent and have already been spawned. |
| 24 | + |
| 25 | +Analyze the recent code changes and make improvements. However, try to only make changes that you are confident are fully correct and the user would want. It's ok to not make any changes. |
| 26 | +
|
| 27 | +Important: You can not make any other tool calls besides editing files. You cannot read more files, write todos, spawn agents, or set output. set_output in particular should not be used. Do not call any of these tools! |
| 28 | +
|
| 29 | +Write out what changes you would make using the tool call format below. Use this exact format for each file change: |
| 30 | +
|
| 31 | +<codebuff_tool_call> |
| 32 | +{ |
| 33 | + "cb_tool_name": "str_replace", |
| 34 | + "path": "path/to/file", |
| 35 | + "replacements": [ |
| 36 | + { |
| 37 | + "old": "exact old code", |
| 38 | + "new": "exact new code" |
| 39 | + }, |
| 40 | + { |
| 41 | + "old": "exact old code 2", |
| 42 | + "new": "exact new code 2" |
| 43 | + }, |
| 44 | + ] |
| 45 | +} |
| 46 | +</codebuff_tool_call> |
| 47 | +
|
| 48 | +OR for new files or major rewrites: |
| 49 | +
|
| 50 | +<codebuff_tool_call> |
| 51 | +{ |
| 52 | + "cb_tool_name": "write_file", |
| 53 | + "path": "path/to/file", |
| 54 | + "instructions": "What the change does", |
| 55 | + "content": "Complete file content or edit snippet" |
| 56 | +} |
| 57 | +</codebuff_tool_call> |
| 58 | +
|
| 59 | +${ |
| 60 | + model === 'gpt-5' |
| 61 | + ? '' |
| 62 | + : `Before you start writing your implementation, you should use <think> tags to think about the best way to implement the changes. |
| 63 | +
|
| 64 | +You can also use <think> tags interspersed between tool calls to think about the best way to implement the changes. |
| 65 | +
|
| 66 | +<example> |
| 67 | +
|
| 68 | +<think> |
| 69 | +[ Long think about the best way to implement the changes ] |
| 70 | +</think> |
| 71 | +
|
| 72 | +<codebuff_tool_call> |
| 73 | +[ First tool call to implement the feature ] |
| 74 | +</codebuff_tool_call> |
| 75 | +
|
| 76 | +<codebuff_tool_call> |
| 77 | +[ Second tool call to implement the feature ] |
| 78 | +</codebuff_tool_call> |
| 79 | +
|
| 80 | +<think> |
| 81 | +[ Thoughts about a tricky part of the implementation ] |
| 82 | +</think> |
| 83 | +
|
| 84 | +<codebuff_tool_call> |
| 85 | +[ Third tool call to implement the feature ] |
| 86 | +</codebuff_tool_call> |
| 87 | +
|
| 88 | +</example>` |
| 89 | +} |
| 90 | +
|
| 91 | +### Simplify the code. |
| 92 | +
|
| 93 | +See if there's a simpler design that is more maintainable and easier to understand. |
| 94 | +
|
| 95 | +See if you can remove any of the following: |
| 96 | + - fallback code that is not really needed anymore |
| 97 | + - any unnecessary type casts |
| 98 | + - any dead code |
| 99 | + - any added try/catch blocks -- these clutter the code and are often unnecessary. |
| 100 | + - any optional arguments -- these make the code more complex and harder to understand. |
| 101 | + - any unused imports |
| 102 | +
|
| 103 | +### Improve the code |
| 104 | +- Instead of creating new functions, reuse existing functions if possible. |
| 105 | +- New components usually should be added to a new file, not added to an existing file. |
| 106 | +- Utilities that could be reused should be moved to a shared utilities file. |
| 107 | +
|
| 108 | +Write out your edits now.`, |
| 109 | + |
| 110 | + handleSteps: function* ({ agentState: initialAgentState, logger }) { |
| 111 | + const initialMessageHistoryLength = |
| 112 | + initialAgentState.messageHistory.length |
| 113 | + const { agentState } = yield 'STEP' |
| 114 | + const { messageHistory } = agentState |
| 115 | + |
| 116 | + const newMessages = messageHistory.slice(initialMessageHistoryLength) |
| 117 | + |
| 118 | + yield { |
| 119 | + toolName: 'set_output', |
| 120 | + input: { |
| 121 | + output: { |
| 122 | + messages: newMessages, |
| 123 | + }, |
| 124 | + }, |
| 125 | + includeToolCall: false, |
| 126 | + } |
| 127 | + }, |
| 128 | + } satisfies Omit<AgentDefinition, 'id'> |
| 129 | +} |
| 130 | + |
| 131 | +const definition = { |
| 132 | + ...createCodeEditor({ model: 'opus' }), |
| 133 | + id: 'reviewer-editor', |
| 134 | +} |
| 135 | +export default definition |
0 commit comments