62159
Sy
symb / files
symb / internal / subagent / subagent.go
4.5 KB
1package subagent 2 3import ( 4 "context" 5 "fmt" 6 "strings" 7 "time" 8 9 "github.com/xonecas/symb/internal/llm" 10 "github.com/xonecas/symb/internal/mcp" 11 "github.com/xonecas/symb/internal/provider" 12) 13 14const ( 15 // MaxSubAgentDepth is the maximum recursion depth for sub-agents. 16 // Depth 0 = root agent, depth 1 = sub-agent spawned by root. 17 MaxSubAgentDepth = 1 18 19 // MaxSubAgentIterations is the default max tool rounds for sub-agents. 20 MaxSubAgentIterations = 5 21 22 // MaxAllowedIterations is the upper bound for user-specified max_iterations. 23 MaxAllowedIterations = 20 24) 25 26// Options configures a sub-agent run. 27type Options struct { 28 Provider provider.Provider 29 Proxy *mcp.Proxy 30 Tools []mcp.Tool 31 Prompt string 32 Type string 33 MaxIterations int 34} 35 36// Result reports a sub-agent run outcome. 37type Result struct { 38 Content string 39 InputTokens int 40 OutputTokens int 41} 42 43// Run executes a sub-agent turn and returns the final assistant content. 44func Run(ctx context.Context, opts Options) (Result, error) { 45 if err := ctx.Err(); err != nil { 46 return Result{}, fmt.Errorf("sub-agent cancelled: %v", err) 47 } 48 if opts.Provider == nil { 49 return Result{}, fmt.Errorf("provider is required") 50 } 51 if opts.Proxy == nil { 52 return Result{}, fmt.Errorf("proxy is required") 53 } 54 if opts.Prompt == "" { 55 return Result{}, fmt.Errorf("prompt is required") 56 } 57 58 maxIter := DefaultIterations(opts.Type) 59 if opts.MaxIterations > 0 { 60 if opts.MaxIterations > MaxAllowedIterations { 61 return Result{}, fmt.Errorf("max_iterations too large (max: %d)", MaxAllowedIterations) 62 } 63 maxIter = opts.MaxIterations 64 } 65 66 subHistory := []provider.Message{ 67 { 68 Role: "system", 69 Content: SystemPrompt(opts.Type), 70 CreatedAt: time.Now(), 71 }, 72 { 73 Role: "user", 74 Content: opts.Prompt, 75 CreatedAt: time.Now(), 76 }, 77 } 78 79 var totalIn, totalOut int 80 var subMessages []provider.Message 81 82 err := llm.ProcessTurn(ctx, llm.ProcessTurnOptions{ 83 Provider: opts.Provider, 84 Proxy: opts.Proxy, 85 Tools: opts.Tools, 86 History: subHistory, 87 OnMessage: func(msg provider.Message) { 88 subMessages = append(subMessages, msg) 89 }, 90 OnUsage: func(in, out int) { 91 totalIn += in 92 totalOut += out 93 }, 94 MaxToolRounds: maxIter, 95 Depth: MaxSubAgentDepth, 96 }) 97 if err != nil { 98 return Result{}, fmt.Errorf("sub-agent failed: %v", err) 99 } 100 101 if len(subMessages) == 0 { 102 return Result{}, fmt.Errorf("sub-agent produced no output") 103 } 104 105 var finalContent string 106 for i := len(subMessages) - 1; i >= 0; i-- { 107 if subMessages[i].Role == "assistant" && subMessages[i].Content != "" { 108 finalContent = subMessages[i].Content 109 break 110 } 111 } 112 113 if finalContent == "" { 114 return Result{}, fmt.Errorf("sub-agent produced no final response") 115 } 116 117 return Result{Content: finalContent, InputTokens: totalIn, OutputTokens: totalOut}, nil 118} 119 120// FilterTools removes the SubAgent tool from a tool list. 121func FilterTools(tools []mcp.Tool) []mcp.Tool { 122 filtered := make([]mcp.Tool, 0, len(tools)) 123 for _, t := range tools { 124 if t.Name != "SubAgent" { 125 filtered = append(filtered, t) 126 } 127 } 128 return filtered 129} 130 131// FilterToolsForType removes SubAgent and filters by sub-agent type. 132func FilterToolsForType(tools []mcp.Tool, agentType string) []mcp.Tool { 133 base := FilterTools(tools) 134 switch agentType { 135 case "explore": 136 return filterByName(base, "Read", "Grep", "Shell") 137 case "editor": 138 return filterByName(base, "Read", "Edit", "Grep", "Shell") 139 case "reviewer": 140 return filterByName(base, "Read", "Grep", "Shell") 141 case "web": 142 return filterByName(base, "web_search_exa", "get_code_context_exa") 143 default: 144 return base 145 } 146} 147 148func filterByName(tools []mcp.Tool, names ...string) []mcp.Tool { 149 allowed := make(map[string]bool, len(names)) 150 for _, n := range names { 151 allowed[n] = true 152 } 153 filtered := make([]mcp.Tool, 0, len(names)) 154 for _, t := range tools { 155 if allowed[t.Name] { 156 filtered = append(filtered, t) 157 } 158 } 159 return filtered 160} 161 162// DefaultIterations returns the default max tool rounds for a sub-agent type. 163func DefaultIterations(agentType string) int { 164 switch agentType { 165 case "explore": 166 return 10 167 case "editor": 168 return 8 169 case "reviewer": 170 return 10 171 case "web": 172 return 5 173 default: 174 return MaxSubAgentIterations 175 } 176} 177 178// SystemPrompt returns the system prompt for sub-agents. 179func SystemPrompt(agentType string) string { 180 parts := []string{ 181 llm.SubAgentBasePrompt(), 182 llm.SubAgentTypePrompt(agentType), 183 } 184 if instructions := llm.LoadAgentInstructions(); instructions != "" { 185 parts = append(parts, instructions) 186 } 187 return strings.TrimSpace(strings.Join(parts, "\n\n---\n\n")) 188}