6119
My
mysis / files
mysis / internal / llm / loop.go
7.5 KB
1package llm 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "strings" 8 "time" 9 10 "github.com/rs/zerolog/log" 11 "github.com/xonecas/mysis/internal/mcp" 12 "github.com/xonecas/mysis/internal/provider" 13 "github.com/xonecas/mysis/internal/store" 14 "github.com/xonecas/mysis/internal/styles" 15) 16 17// MessageCallback is called when a message should be added to history and saved. 18type MessageCallback func(msg provider.Message) 19 20// ToolCallCallback is called when tool calls are about to be executed. 21type ToolCallCallback func() 22 23// ProcessTurnOptions holds configuration for processing a turn. 24type ProcessTurnOptions struct { 25 Provider provider.Provider 26 Proxy *mcp.Proxy 27 Tools []mcp.Tool 28 History []provider.Message 29 OnMessage MessageCallback 30 OnToolCall ToolCallCallback // Optional: called before executing tool calls 31 MaxToolRounds int 32 HistoryKeepLast int 33 SuppressOutput bool // If true, suppress fmt.Println output (for TUI mode) 34} 35 36// ProcessTurn handles one conversation turn, which may involve tool calls. 37// It returns an error if the LLM call fails or max rounds are exceeded. 38func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error { 39 if opts.MaxToolRounds == 0 { 40 opts.MaxToolRounds = 20 41 } 42 if opts.HistoryKeepLast == 0 { 43 opts.HistoryKeepLast = 10 44 } 45 46 for round := 0; round < opts.MaxToolRounds; round++ { 47 // Compress history before sending to LLM 48 // Keep last N turns full, compress older state queries 49 compressedHistory := store.CompressHistory(opts.History, opts.HistoryKeepLast) 50 51 // Log compression stats 52 if len(compressedHistory) < len(opts.History) { 53 originalTokens := store.EstimateTokenCount(opts.History) 54 compressedTokens := store.EstimateTokenCount(compressedHistory) 55 log.Debug(). 56 Int("original_msgs", len(opts.History)). 57 Int("compressed_msgs", len(compressedHistory)). 58 Int("original_tokens", originalTokens). 59 Int("compressed_tokens", compressedTokens). 60 Int("saved_tokens", originalTokens-compressedTokens). 61 Msg("History compressed") 62 } 63 64 // Convert MCP tools to provider format 65 providerTools := make([]provider.Tool, len(opts.Tools)) 66 for i, t := range opts.Tools { 67 providerTools[i] = provider.Tool{ 68 Name: t.Name, 69 Description: t.Description, 70 Parameters: t.InputSchema, 71 } 72 } 73 74 // Call LLM with compressed history 75 resp, err := opts.Provider.ChatWithTools(ctx, compressedHistory, providerTools) 76 if err != nil { 77 return fmt.Errorf("LLM call failed: %w", err) 78 } 79 80 // Display reasoning if present (CLI mode only) 81 if resp.Reasoning != "" && !opts.SuppressOutput { 82 displayReasoning(resp.Reasoning) 83 } 84 85 // If no tool calls, display text response and we're done 86 if len(resp.ToolCalls) == 0 { 87 if resp.Content != "" && !opts.SuppressOutput { 88 fmt.Println(resp.Content) 89 } 90 91 // Add assistant response to history 92 assistantMsg := provider.Message{ 93 Role: "assistant", 94 Content: resp.Content, 95 Reasoning: resp.Reasoning, 96 CreatedAt: time.Now(), 97 } 98 opts.OnMessage(assistantMsg) 99 opts.History = append(opts.History, assistantMsg) 100 101 return nil 102 } 103 104 // Tool calls present - add assistant message with tool calls to history 105 assistantMsg := provider.Message{ 106 Role: "assistant", 107 Content: resp.Content, 108 Reasoning: resp.Reasoning, 109 ToolCalls: resp.ToolCalls, 110 CreatedAt: time.Now(), 111 } 112 opts.OnMessage(assistantMsg) 113 opts.History = append(opts.History, assistantMsg) 114 115 // Notify about tool calls if callback provided 116 if opts.OnToolCall != nil { 117 opts.OnToolCall() 118 } 119 120 // Execute each tool call and update history 121 toolResults := executeToolCalls(ctx, opts.Proxy, resp.ToolCalls, opts.OnMessage, opts.SuppressOutput) 122 opts.History = append(opts.History, toolResults...) 123 124 // Continue loop to let LLM process tool results 125 } 126 127 return fmt.Errorf("too many tool call rounds (limit: %d)", opts.MaxToolRounds) 128} 129 130// displayReasoning shows the LLM's reasoning in a compact format. 131func displayReasoning(reasoning string) { 132 // Trim excessive whitespace and collapse multiple spaces/newlines 133 reasoning = strings.TrimSpace(reasoning) 134 reasoning = strings.Join(strings.Fields(reasoning), " ") 135 136 // Truncate if too long (from the end per design spec) 137 if len(reasoning) > 200 { 138 reasoning = "..." + reasoning[len(reasoning)-197:] 139 } 140 141 fmt.Println(styles.Muted.Render("∴ " + reasoning)) 142} 143 144// executeToolCalls executes a list of tool calls and adds results to history. 145// Returns the list of tool result messages that were added. 146func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provider.ToolCall, onMessage MessageCallback, suppressOutput bool) []provider.Message { 147 toolResults := make([]provider.Message, 0, len(toolCalls)) 148 149 for _, toolCall := range toolCalls { 150 if !suppressOutput { 151 fmt.Print(styles.Secondary.Render(fmt.Sprintf("⚙ %s", toolCall.Name))) 152 } 153 154 // Show arguments (truncated if long) 155 displayToolArguments(toolCall.Arguments, suppressOutput) 156 157 // Execute tool via MCP proxy 158 result, err := proxy.CallTool(ctx, toolCall.Name, toolCall.Arguments) 159 160 if err != nil { 161 if !suppressOutput { 162 fmt.Println(styles.Error.Render(" ✗")) 163 fmt.Println(styles.Error.Render(" Error: " + err.Error())) 164 } 165 166 // Add error result to history 167 toolMsg := provider.Message{ 168 Role: "tool", 169 Content: fmt.Sprintf("Error: %v", err), 170 ToolCallID: toolCall.ID, 171 CreatedAt: time.Now(), 172 } 173 onMessage(toolMsg) 174 toolResults = append(toolResults, toolMsg) 175 continue 176 } 177 178 // Check if result is an error 179 if result.IsError { 180 if !suppressOutput { 181 fmt.Println(styles.Error.Render(" ✗")) 182 } 183 errText := extractTextFromContent(result.Content) 184 if errText != "" && !suppressOutput { 185 fmt.Println(styles.Error.Render(" " + errText)) 186 } 187 188 // Add error result to history 189 toolMsg := provider.Message{ 190 Role: "tool", 191 Content: errText, 192 ToolCallID: toolCall.ID, 193 CreatedAt: time.Now(), 194 } 195 onMessage(toolMsg) 196 toolResults = append(toolResults, toolMsg) 197 continue 198 } 199 200 // Success 201 if !suppressOutput { 202 fmt.Println(styles.Success.Render(" ✓")) 203 } 204 205 // Extract and display result 206 resultText := extractTextFromContent(result.Content) 207 displayToolResult(resultText, suppressOutput) 208 209 // Add tool result to history 210 toolMsg := provider.Message{ 211 Role: "tool", 212 Content: resultText, 213 ToolCallID: toolCall.ID, 214 CreatedAt: time.Now(), 215 } 216 onMessage(toolMsg) 217 toolResults = append(toolResults, toolMsg) 218 } 219 220 return toolResults 221} 222 223// displayToolArguments shows tool arguments in a truncated format. 224func displayToolArguments(arguments json.RawMessage, suppressOutput bool) { 225 if suppressOutput { 226 return 227 } 228 229 var args map[string]interface{} 230 if err := json.Unmarshal(arguments, &args); err == nil { 231 argsStr, _ := json.Marshal(args) 232 if len(argsStr) > 60 { 233 argsStr = argsStr[:57] 234 argsStr = append(argsStr, '.', '.', '.') 235 } 236 fmt.Print(styles.Muted.Render(string(argsStr))) 237 } 238} 239 240// displayToolResult shows tool result in a truncated format. 241func displayToolResult(resultText string, suppressOutput bool) { 242 if suppressOutput { 243 return 244 } 245 246 if len(resultText) > 100 { 247 preview := resultText[:97] + "..." 248 fmt.Println(styles.Muted.Render(" " + preview)) 249 } else if resultText != "" { 250 fmt.Println(styles.Muted.Render(" " + resultText)) 251 } 252} 253 254// extractTextFromContent extracts text from MCP content blocks. 255func extractTextFromContent(content []mcp.ContentBlock) string { 256 var text string 257 for _, block := range content { 258 if block.Type == "text" { 259 text += block.Text 260 } 261 } 262 return text 263}