const agentDefinition = {
id: "editor-best-of-n-gpt-5",
displayName: "Best-of-N GPT-5 Editor",
publisher: "codebuff",
version: "0.0.1",
model: "openai/gpt-5",
toolNames: [
"spawn_agents",
"str_replace",
"write_file",
"set_messages",
"set_output"
],
spawnableAgents: [
"codebuff/best-of-n-implementor-gpt-5@0.0.4",
"codebuff/best-of-n-selector-gpt-5@0.0.4"
],
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: ``,
stepPrompt: ``,
handleSteps: function* handleStepsGpt5({
agentState,
params
}) {
const n = Math.min(10, Math.max(1, params?.n ?? 5)), messages = agentState.messageHistory.concat();
messages.pop();
yield {
toolName: "set_messages",
input: {
messages
},
includeToolCall: !1
};
const { toolResult: implementorsResult1 } = yield {
toolName: "spawn_agents",
input: {
agents: Array.from({ length: n }, () => ({
agent_type: "best-of-n-implementor-gpt-5"
}))
},
includeToolCall: !1
}, implementorsResult = extractSpawnResults(implementorsResult1), letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", implementations = implementorsResult.map((content, index) => ({
id: letters[index],
content
})), { toolResult: selectorResult } = yield {
toolName: "spawn_agents",
input: {
agents: [
{
agent_type: "best-of-n-selector-gpt-5",
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
}
};
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
}