Codebuff

editor-best-of-n

v0.0.4
Published Nov 19, 2025

Usage Statistics
v0.0.4

Definition

const agentDefinition = {
id: "editor-best-of-n",
displayName: "Best-of-N Sonnet Editor",
publisher: "codebuff",
version: "0.0.4",
model: "anthropic/claude-sonnet-4.5",
toolNames: [
"spawn_agents",
"str_replace",
"write_file",
"set_messages",
"set_output"
],
spawnableAgents: [
"codebuff/best-of-n-selector@0.0.8"
],
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.
You can also use <think> tags interspersed between tool calls to think about the best way to implement the changes. Keep these thoughts very brief. You may not need to use think tags at all.
<example>
<think>
[ Thoughts about the best way to implement the feature ]
</think>
<codebuff_tool_call>
[ First tool call to implement the feature ]
</codebuff_tool_call>
<codebuff_tool_call>
[ Second tool call to implement the feature ]
</codebuff_tool_call>
<think>
[ Thoughts about a tricky part of the implementation ]
</think>
<codebuff_tool_call>
[ Third tool call to implement the feature ]
</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
}