|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// ────────────────────────────────────────────────────────────── |
| 9 | +// Subagent — read-only research agent in isolated context |
| 10 | +// |
| 11 | +// Modeled after the web app's dispatch_agent tool. Spawns a |
| 12 | +// separate agent loop with its own conversation history and |
| 13 | +// read-only tools. Returns only the final text summary. |
| 14 | +// Max depth 1 (subagents cannot spawn sub-subagents). |
| 15 | +// ────────────────────────────────────────────────────────────── |
| 16 | + |
| 17 | +const subagentMaxTurns = 15 |
| 18 | + |
| 19 | +const subagentSystemPrompt = `You are a focused research assistant. You help gather information by reading files, searching code, and listing directories. |
| 20 | +
|
| 21 | +You have read-only access to the project. You CANNOT modify any files. |
| 22 | +
|
| 23 | +Available tools: read_file, list_files, search_files, shell |
| 24 | +
|
| 25 | +Guidelines: |
| 26 | +- Be thorough but efficient — gather what's needed, then summarize |
| 27 | +- Use search_files to find relevant code quickly |
| 28 | +- Use list_files to explore project structure |
| 29 | +- Use shell for read-only commands only (ls, cat, grep, git log, etc.) |
| 30 | +- When done, provide a clear, concise summary of your findings` |
| 31 | + |
| 32 | +// subagentToolDefs contains only read-only tools. |
| 33 | +var subagentToolDefs []ToolDef |
| 34 | + |
| 35 | +func init() { |
| 36 | + for _, td := range toolDefs { |
| 37 | + switch td.Function.Name { |
| 38 | + case "read_file", "list_files", "search_files", "shell": |
| 39 | + subagentToolDefs = append(subagentToolDefs, td) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// RunSubagent executes a read-only research subagent and returns its final text. |
| 45 | +func RunSubagent(client *LLMClient, workDir, task string) (string, error) { |
| 46 | + sysContent := subagentSystemPrompt + fmt.Sprintf("\n\nWorking directory: %s", workDir) |
| 47 | + |
| 48 | + history := []ChatMessage{ |
| 49 | + {Role: "system", Content: strPtr(sysContent)}, |
| 50 | + {Role: "user", Content: strPtr(task)}, |
| 51 | + } |
| 52 | + |
| 53 | + var finalText strings.Builder |
| 54 | + |
| 55 | + for turn := 1; turn <= subagentMaxTurns; turn++ { |
| 56 | + // Check compaction |
| 57 | + if needsCompaction(history, client.Model) { |
| 58 | + compacted, ok := compactHistory(client, history) |
| 59 | + if ok { |
| 60 | + history = compacted |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Stream LLM call |
| 65 | + streamCh := make(chan StreamEvent, 64) |
| 66 | + go client.StreamChat(history, subagentToolDefs, streamCh) |
| 67 | + |
| 68 | + var textContent strings.Builder |
| 69 | + var toolCalls []ToolCall |
| 70 | + |
| 71 | + for evt := range streamCh { |
| 72 | + switch evt.Type { |
| 73 | + case StreamText: |
| 74 | + textContent.WriteString(evt.Text) |
| 75 | + case StreamToolCalls: |
| 76 | + toolCalls = evt.ToolCalls |
| 77 | + case StreamError: |
| 78 | + return "", fmt.Errorf("subagent LLM error: %v", evt.Error) |
| 79 | + case StreamDone: |
| 80 | + // handled below |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Build assistant message |
| 85 | + assistantMsg := ChatMessage{Role: "assistant"} |
| 86 | + txt := textContent.String() |
| 87 | + if txt != "" { |
| 88 | + assistantMsg.Content = strPtr(txt) |
| 89 | + } |
| 90 | + if len(toolCalls) > 0 { |
| 91 | + assistantMsg.ToolCalls = toolCalls |
| 92 | + } |
| 93 | + history = append(history, assistantMsg) |
| 94 | + |
| 95 | + // If no tool calls, we're done |
| 96 | + if len(toolCalls) == 0 { |
| 97 | + finalText.WriteString(txt) |
| 98 | + break |
| 99 | + } |
| 100 | + |
| 101 | + // Execute read-only tools (all parallel-safe) |
| 102 | + for _, tc := range toolCalls { |
| 103 | + // Safety: only allow read-only tools |
| 104 | + switch tc.Function.Name { |
| 105 | + case "read_file", "list_files", "search_files", "shell": |
| 106 | + // shell is allowed but subagent should only use read-only commands |
| 107 | + default: |
| 108 | + history = append(history, ChatMessage{ |
| 109 | + Role: "tool", |
| 110 | + ToolCallID: tc.ID, |
| 111 | + Name: tc.Function.Name, |
| 112 | + Content: strPtr(fmt.Sprintf("Error: tool %q is not available in read-only mode", tc.Function.Name)), |
| 113 | + }) |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + output, _ := ExecuteTool(tc.Function.Name, tc.Function.Arguments, workDir) |
| 118 | + history = append(history, ChatMessage{ |
| 119 | + Role: "tool", |
| 120 | + ToolCallID: tc.ID, |
| 121 | + Name: tc.Function.Name, |
| 122 | + Content: strPtr(output), |
| 123 | + }) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + result := finalText.String() |
| 128 | + if result == "" { |
| 129 | + result = "Subagent completed without producing a summary." |
| 130 | + } |
| 131 | + |
| 132 | + return result, nil |
| 133 | +} |
0 commit comments