|
| 1 | +import type { SecretAgentDefinition } from '../../types/secret-agent-definition' |
| 2 | +import { publisher } from '../../constants' |
| 3 | + |
| 4 | +const definition: SecretAgentDefinition = { |
| 5 | + id: 'base2-best-of-n-orchestrator', |
| 6 | + publisher, |
| 7 | + model: 'anthropic/claude-sonnet-4.5', |
| 8 | + displayName: 'Best-of-N Implementation Orchestrator', |
| 9 | + spawnerPrompt: |
| 10 | + 'Orchestrates multiple implementor agents to generate implementation proposals and selects the best one', |
| 11 | + |
| 12 | + includeMessageHistory: true, |
| 13 | + inheritParentSystemPrompt: true, |
| 14 | + |
| 15 | + toolNames: ['spawn_agents', 'set_output'], |
| 16 | + spawnableAgents: [ |
| 17 | + 'base2-implementor', |
| 18 | + 'base2-selector', |
| 19 | + 'base2-best-of-n-editor', |
| 20 | + ], |
| 21 | + |
| 22 | + inputSchema: {}, |
| 23 | + outputMode: 'structured_output', |
| 24 | + |
| 25 | + handleSteps: function* ({ logger }) { |
| 26 | + // Spawn 5 implementor agents in parallel |
| 27 | + const { toolResult: implementorsResult } = yield { |
| 28 | + toolName: 'spawn_agents', |
| 29 | + input: { |
| 30 | + agents: [ |
| 31 | + { agent_type: 'base2-implementor' }, |
| 32 | + { agent_type: 'base2-implementor' }, |
| 33 | + { agent_type: 'base2-implementor' }, |
| 34 | + { agent_type: 'base2-implementor' }, |
| 35 | + { agent_type: 'base2-implementor' }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + // Extract all the plans from the structured outputs |
| 41 | + const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 42 | + // Parse implementations from tool results |
| 43 | + const implementations = (implementorsResult ?? []) |
| 44 | + .filter((result) => result.type === 'json') |
| 45 | + .map( |
| 46 | + (result) => |
| 47 | + (result as any).value as { agentType: string; value: string }[], |
| 48 | + ) |
| 49 | + .flatMap((results) => |
| 50 | + results.map((result, index) => ({ |
| 51 | + id: letters[index], |
| 52 | + content: JSON.stringify((result.value as any).value), |
| 53 | + })), |
| 54 | + ) |
| 55 | + |
| 56 | + // Spawn selector with implementations as params |
| 57 | + const { toolResult: selectorResult } = yield { |
| 58 | + toolName: 'spawn_agents', |
| 59 | + input: { |
| 60 | + agents: [ |
| 61 | + { |
| 62 | + agent_type: 'base2-selector', |
| 63 | + params: { implementations }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // Extract chosen implementation from selector output |
| 70 | + const selectorOutput = |
| 71 | + (selectorResult ?? []) |
| 72 | + .filter((result) => result.type === 'json') |
| 73 | + .map( |
| 74 | + (result) => |
| 75 | + result.value as { |
| 76 | + value: { value: { implementationId: string; reasoning: string } } |
| 77 | + }[], |
| 78 | + )[0][0] || {} |
| 79 | + |
| 80 | + const chosenImplementationId = selectorOutput.value.value.implementationId |
| 81 | + const chosenImplementation = implementations.find( |
| 82 | + (implementation) => implementation.id === chosenImplementationId, |
| 83 | + ) |
| 84 | + if (!chosenImplementation) { |
| 85 | + yield { |
| 86 | + toolName: 'set_output', |
| 87 | + input: { error: 'Failed to choose an implementation.' }, |
| 88 | + } |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Spawn editor to apply the chosen implementation |
| 93 | + yield { |
| 94 | + toolName: 'spawn_agents', |
| 95 | + input: { |
| 96 | + agents: [ |
| 97 | + { |
| 98 | + agent_type: 'base2-best-of-n-editor', |
| 99 | + prompt: chosenImplementation.content, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + // Set output with the chosen implementation and reasoning |
| 106 | + yield { |
| 107 | + toolName: 'set_output', |
| 108 | + input: { |
| 109 | + implementation: chosenImplementation.content, |
| 110 | + }, |
| 111 | + } |
| 112 | + }, |
| 113 | +} |
| 114 | + |
| 115 | +export default definition |
0 commit comments