|
| 1 | +import { publisher } from '../../constants' |
| 2 | + |
| 3 | +import type { AgentStepContext, ToolCall } from '../../types/agent-definition' |
| 4 | +import type { SecretAgentDefinition } from '../../types/secret-agent-definition' |
| 5 | + |
| 6 | +/** |
| 7 | + * Creates a multi-prompt code reviewer agent that spawns one code-reviewer per prompt. |
| 8 | + * Each prompt specifies a slightly different review focus or perspective. |
| 9 | + * Combines all review outputs into a single comprehensive review. |
| 10 | + */ |
| 11 | +export function createCodeReviewerMultiPrompt(): Omit< |
| 12 | + SecretAgentDefinition, |
| 13 | + 'id' |
| 14 | +> { |
| 15 | + return { |
| 16 | + publisher, |
| 17 | + model: 'anthropic/claude-opus-4.5', |
| 18 | + displayName: 'Multi-Prompt Code Reviewer', |
| 19 | + spawnerPrompt: |
| 20 | + 'Reviews code by spawning multiple code-reviewer agents with different focus prompts, then combines all review outputs into a comprehensive review. Make sure to read relevant files before spawning this agent. Pass an input array of short prompts specifying several different review focuses or perspectives.', |
| 21 | + |
| 22 | + includeMessageHistory: true, |
| 23 | + inheritParentSystemPrompt: true, |
| 24 | + |
| 25 | + toolNames: ['spawn_agents', 'set_output'], |
| 26 | + spawnableAgents: ['code-reviewer'], |
| 27 | + |
| 28 | + inputSchema: { |
| 29 | + params: { |
| 30 | + type: 'object', |
| 31 | + properties: { |
| 32 | + prompts: { |
| 33 | + type: 'array', |
| 34 | + items: { type: 'string' }, |
| 35 | + description: |
| 36 | + 'Array of 3-5 short prompts, each specifying a different review focus or perspective. Example: ["api design", "frontend changes", "correctness and edge cases", "code style and readability", "performance implications", "security concerns"]', |
| 37 | + }, |
| 38 | + }, |
| 39 | + required: ['prompts'], |
| 40 | + }, |
| 41 | + }, |
| 42 | + outputMode: 'structured_output', |
| 43 | + |
| 44 | + handleSteps: handleStepsMultiPrompt, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function* handleStepsMultiPrompt({ |
| 49 | + params, |
| 50 | +}: AgentStepContext): ReturnType< |
| 51 | + NonNullable<SecretAgentDefinition['handleSteps']> |
| 52 | +> { |
| 53 | + const prompts = (params?.prompts as string[] | undefined) ?? [] |
| 54 | + |
| 55 | + if (prompts.length === 0) { |
| 56 | + yield { |
| 57 | + toolName: 'set_output', |
| 58 | + input: { |
| 59 | + error: |
| 60 | + 'No prompts provided. Please pass an array of review focus prompts.', |
| 61 | + }, |
| 62 | + } satisfies ToolCall<'set_output'> |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Spawn one code-reviewer per prompt |
| 67 | + const reviewerAgents: { agent_type: string; prompt: string }[] = prompts.map( |
| 68 | + (prompt) => ({ |
| 69 | + agent_type: 'code-reviewer', |
| 70 | + prompt: `Review focus: ${prompt}`, |
| 71 | + }), |
| 72 | + ) |
| 73 | + |
| 74 | + // Spawn all reviewer agents |
| 75 | + const { toolResult: reviewerResults } = yield { |
| 76 | + toolName: 'spawn_agents', |
| 77 | + input: { |
| 78 | + agents: reviewerAgents, |
| 79 | + }, |
| 80 | + includeToolCall: false, |
| 81 | + } satisfies ToolCall<'spawn_agents'> |
| 82 | + |
| 83 | + // Extract spawn results - each is last_message output (string content) |
| 84 | + const spawnedReviews = extractSpawnResults<string>(reviewerResults) |
| 85 | + |
| 86 | + // Combine all reviews with their focus areas |
| 87 | + const combinedReviews = spawnedReviews |
| 88 | + .map((review, index) => { |
| 89 | + const focus = prompts[index] ?? 'unknown' |
| 90 | + if (!review || (typeof review === 'object' && 'errorMessage' in review)) { |
| 91 | + return `## Review Focus: ${focus}\n\nError: ${(review as { errorMessage?: string })?.errorMessage ?? 'Unknown error'}` |
| 92 | + } |
| 93 | + return `## Review Focus: ${focus}\n\n${review}` |
| 94 | + }) |
| 95 | + .join('\n\n---\n\n') |
| 96 | + |
| 97 | + // Set output with the combined reviews |
| 98 | + yield { |
| 99 | + toolName: 'set_output', |
| 100 | + input: { |
| 101 | + reviews: spawnedReviews, |
| 102 | + combinedReview: combinedReviews, |
| 103 | + promptCount: prompts.length, |
| 104 | + }, |
| 105 | + includeToolCall: false, |
| 106 | + } satisfies ToolCall<'set_output'> |
| 107 | + |
| 108 | + /** |
| 109 | + * Extracts the array of subagent results from spawn_agents tool output. |
| 110 | + * For code-reviewer agents with outputMode: 'last_message', the value is the message content. |
| 111 | + */ |
| 112 | + function extractSpawnResults<T>( |
| 113 | + results: { type: string; value?: unknown }[] | undefined, |
| 114 | + ): (T | { errorMessage: string })[] { |
| 115 | + if (!results || results.length === 0) return [] |
| 116 | + |
| 117 | + const jsonResult = results.find((r) => r.type === 'json') |
| 118 | + if (!jsonResult?.value) return [] |
| 119 | + |
| 120 | + const spawnedResults = Array.isArray(jsonResult.value) |
| 121 | + ? jsonResult.value |
| 122 | + : [jsonResult.value] |
| 123 | + |
| 124 | + const extracted: (T | { errorMessage: string })[] = [] |
| 125 | + for (const result of spawnedResults) { |
| 126 | + const innerValue = result?.value |
| 127 | + if ( |
| 128 | + innerValue && |
| 129 | + typeof innerValue === 'object' && |
| 130 | + 'value' in innerValue |
| 131 | + ) { |
| 132 | + extracted.push(innerValue.value as T) |
| 133 | + } else if ( |
| 134 | + innerValue && |
| 135 | + typeof innerValue === 'object' && |
| 136 | + 'errorMessage' in innerValue |
| 137 | + ) { |
| 138 | + extracted.push({ errorMessage: String(innerValue.errorMessage) }) |
| 139 | + } else if (innerValue != null) { |
| 140 | + extracted.push(innerValue as T) |
| 141 | + } |
| 142 | + } |
| 143 | + return extracted |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +const definition = { |
| 148 | + ...createCodeReviewerMultiPrompt(), |
| 149 | + id: 'code-reviewer-multi-prompt', |
| 150 | +} |
| 151 | +export default definition |
0 commit comments