const agentDefinition = {
id: "editor-best-of-n-gpt-5",
displayName: "Best-of-N GPT-5 Editor",
publisher: "codebuff",
version: "0.0.3",
model: "openai/gpt-5.1",
toolNames: [
"spawn_agents",
"str_replace",
"write_file",
"set_messages",
"set_output"
],
spawnableAgents: [
"codebuff/best-of-n-selector@0.0.7"
],
inputSchema: {
params: {
type: "object",
properties: {
n: {
type: "number",
description: "Number of parallel implementor agents to spawn. Defaults to 5. Use fewer for simple tasks and max of 10 for complex tasks."
}
}
}
},
includeMessageHistory: true,
outputMode: "structured_output",
spawnerPrompt: `Edits code by orchestrating multiple implementor agents to generate implementation proposals, selects the best one, and applies the changes. Do not specify an input prompt for this agent; it inherits the context of the entire conversation with the user. Make sure to read any files intended to be edited before spawning this agent as it cannot read files on its own.`,
systemPrompt: ``,
instructionsPrompt: `You are one agent within the editor-best-of-n. You were spawned to generate an implementation for the user's request.
Your task is to write out ALL the code changes needed to complete the user's request in a single comprehensive response.
Important: You can not make any other tool calls besides editing files. You cannot read more files, write todos, or spawn agents.
Write out what changes you would make using str_replace and/or write_file tool calls.
<example>
<codebuff_tool_call>
{
"cb_tool_name": "str_replace",
"path": "path/to/file",
"replacements": [
{
"old": "exact old code",
"new": "exact new code"
},
{
"old": "exact old code 2",
"new": "exact new code 2"
},
]
}
</codebuff_tool_call>
<codebuff_tool_call>
{
"cb_tool_name": "write_file",
"path": "path/to/file",
"instructions": "What the change does",
"content": "Complete file content or edit snippet"
}
</codebuff_tool_call>
</example>
Your implementation should:
- Be complete and comprehensive
- Include all necessary changes to fulfill the user's request
- Follow the project's conventions and patterns
- Be as simple and maintainable as possible
- Reuse existing code wherever possible
- Be well-structured and organized
More style notes:
- Try/catch blocks clutter the code -- use them sparingly.
- Optional arguments are code smell and worse than required arguments.
- New components often should be added to a new file, not added to an existing file.
Write out your complete implementation now as a series of file editing tool calls.`,
stepPrompt: ``,
handleSteps: function* handleStepsSonnet({
params
}) {
const n = Math.min(10, Math.max(1, params?.n ?? 5)), { nResponses = [] } = yield {
type: "GENERATE_N",
n
}, implementations = nResponses.map((content, index) => ({
id: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index],
content
})), { toolResult: selectorResult } = yield {
toolName: "spawn_agents",
input: {
agents: [
{
agent_type: "best-of-n-selector",
params: { implementations }
}
]
},
includeToolCall: !1
}, selectorOutput = extractSpawnResults(selectorResult)[0];
if ("errorMessage" in selectorOutput) {
yield {
toolName: "set_output",
input: { error: selectorOutput.errorMessage }
};
return;
}
const { implementationId } = selectorOutput, chosenImplementation = implementations.find((implementation) => implementation.id === implementationId);
if (!chosenImplementation) {
yield {
toolName: "set_output",
input: { error: "Failed to find chosen implementation." }
};
return;
}
const toolCallsOnly = extractToolCallsOnly(typeof chosenImplementation.content === "string" ? chosenImplementation.content : ""), { agentState: postEditsAgentState } = yield {
type: "STEP_TEXT",
text: toolCallsOnly
}, { messageHistory } = postEditsAgentState, lastAssistantMessageIndex = messageHistory.findLastIndex((message) => message.role === "assistant"), editToolResults = messageHistory.slice(lastAssistantMessageIndex).filter((message) => message.role === "tool").flatMap((message) => message.content.output).filter((output) => output.type === "json").map((output) => output.value);
yield {
toolName: "set_output",
input: {
response: chosenImplementation.content,
toolResults: editToolResults
},
includeToolCall: !1
};
function extractSpawnResults(results) {
if (!results)
return [];
return results.filter((result) => result.type === "json").map((result) => result.value).flat().map((result) => result.value.value ?? {
errorMessage: result.value.errorMessage ?? "Error extracting spawn results"
});
}
function extractToolCallsOnly(text) {
const toolExtractionPattern = /<codebuff_tool_call>\n(.*?)\n<\/codebuff_tool_call>/gs, matches = [];
for (const match of text.matchAll(toolExtractionPattern))
matches.push(match[0]);
return matches.join(`
`);
}
},
mcpServers: {},
inheritParentSystemPrompt: true
}