xonecas.io
62159
Sy

symb / files

symb / internal / tui / update_llm.go

14.2 KB
1package tui
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"sort"
8	"strconv"
9	"strings"
10	"time"
11
12	tea "charm.land/bubbletea/v2"
13	"github.com/xonecas/symb/internal/hashline"
14	"github.com/xonecas/symb/internal/provider"
15	"github.com/xonecas/symb/internal/store"
16)
17
18func (m Model) handleLLMUser(msg llmUserMsg) (tea.Model, tea.Cmd) {
19	updated, cmd := m.handleUserMsg(msg)
20	return updated, cmd
21}
22
23// handleUserMsg records a user message in the conversation display.
24func (m *Model) handleUserMsg(msg llmUserMsg) (Model, tea.Cmd) {
25	now := time.Now()
26	llmMsg := provider.Message{Role: "user", Content: msg.content, CreatedAt: now}
27	storeMsg := provider.Message{Role: "user", Content: msg.display, CreatedAt: now}
28
29	convIdx := len(m.convEntries)
30
31	if m.provider != nil {
32		m.turnPending = true
33	}
34
35	m.turnBoundaries = append(m.turnBoundaries, turnBoundary{
36		convIdx:      convIdx,
37		dbMsgID:      0,
38		inputTokens:  m.totalInputTokens,
39		outputTokens: m.totalOutputTokens,
40	})
41
42	m.appendText("")
43	m.appendText(highlightMarkdown(msg.display, m.styles.Text)...)
44	wasBottom := m.appendText("")
45	m.turnInputTokens = 0
46	m.turnOutputTokens = 0
47	m.turnContextTokens = 0
48	if wasBottom {
49		m.scrollOffset = 0
50	}
51	cmd := m.saveUserMessageCmd(llmMsg, storeMsg, convIdx)
52	return *m, cmd
53}
54
55func (m *Model) saveUserMessageCmd(llmMsg, storeMsg provider.Message, convIdx int) tea.Cmd {
56	store := m.store
57	sessionID := m.sessionID
58	if store == nil {
59		return func() tea.Msg {
60			return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: llmMsg}
61		}
62	}
63	return func() tea.Msg {
64		id, err := store.SaveMessageSync(sessionID, messageToStore(storeMsg))
65		if err != nil {
66			return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: llmMsg, err: err}
67		}
68		return userMsgSavedMsg{convIdx: convIdx, dbMsgID: id, userMsg: llmMsg}
69	}
70}
71
72func (m Model) handleUserMsgSaved(msg userMsgSavedMsg) (tea.Model, tea.Cmd) {
73	m.turnPending = false
74	for i := range m.turnBoundaries {
75		if m.turnBoundaries[i].convIdx == msg.convIdx {
76			m.turnBoundaries[i].dbMsgID = msg.dbMsgID
77			break
78		}
79	}
80	if m.deltaTracker != nil && msg.dbMsgID > 0 {
81		m.deltaTracker.BeginTurn(msg.dbMsgID)
82	}
83	if m.provider == nil {
84		return m, nil
85	}
86	m.llmInFlight = true
87	m.turnCtx, m.turnCancel = context.WithCancel(context.Background())
88	// Always supply the current user message via extra so the LLM receives the
89	// expanded form (@ mentions replaced with file content). When the store is
90	// present the display form was saved to DB; we need to exclude it from the
91	// loaded history to avoid sending a duplicate.
92	extra := []provider.Message{msg.userMsg}
93	return m, tea.Batch(m.processLLMWithExtra(extra), m.waitForLLMUpdate())
94}
95
96// handleLLMBatch processes a batch of messages drained from updateChan.
97// Streaming deltas just mark dirty — the next frame tick does the rebuild.
98// Finalization messages (assistant, tool result) flush immediately since
99// they clear streaming state.
100func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
101	var history []provider.Message
102	for _, raw := range batch {
103		if msg, ok := raw.(llmHistoryMsg); ok {
104			history = append(history, msg.msg)
105		}
106	}
107	saveCmd := m.saveMessagesCmd(history)
108	if !m.llmInFlight {
109		return m.drainCancelled(batch, saveCmd)
110	}
111	for _, raw := range batch {
112		switch msg := raw.(type) {
113		case llmReasoningDeltaMsg:
114			m.ensureStreaming()
115			m.streamingReasoning += msg.content
116			m.streamDirty = true
117
118		case llmContentDeltaMsg:
119			m.ensureStreaming()
120			m.streamingContent += msg.content
121			m.streamDirty = true
122
123		case llmHistoryMsg:
124			// Saved in a single transaction above.
125
126		case llmAssistantMsg:
127			m.applyAssistantMsg(msg)
128
129		case llmToolResultMsg:
130			m.applyToolResultMsg(msg)
131
132		case llmErrorMsg:
133			m.finishTurn()
134			m.lastNetError = msg.err.Error()
135			m.clearStreaming()
136			m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
137			return m, saveCmd
138
139		case llmUsageMsg:
140			m.turnInputTokens += msg.inputTokens
141			m.turnOutputTokens += msg.outputTokens
142			m.totalInputTokens += msg.inputTokens
143			m.totalOutputTokens += msg.outputTokens
144
145		case llmDoneMsg:
146			m.finishTurn()
147			m.lastNetError = ""
148			m.demoteOldUndo()
149			m.appendText("")
150			m.turnContextTokens = msg.contextTokens
151			sep := makeSeparator(m.styles, msg.duration.Round(time.Second).String(), msg.timestamp,
152				msg.inputTokens, msg.outputTokens, m.totalInputTokens+m.totalOutputTokens, m.turnContextTokens)
153			m.appendConv(m.makeUndoEntry(sep)...)
154			m.trimOldTurns()
155			return m, saveCmd
156		}
157	}
158
159	// More messages may follow — keep listening.
160	return m, tea.Batch(saveCmd, m.waitForLLMUpdate())
161}
162
163// drainCancelled handles a batch after the turn was cancelled.
164// It persists history messages and patches invalid API state on termination.
165func (m Model) drainCancelled(batch llmBatchMsg, saveCmd tea.Cmd) (tea.Model, tea.Cmd) {
166	for _, raw := range batch {
167		switch raw.(type) {
168		case llmHistoryMsg:
169			// Saved in a single transaction above.
170		case llmDoneMsg, llmErrorMsg:
171			m.turnCancel = nil
172			m.turnCtx = nil
173			return m, tea.Batch(saveCmd, m.patchInterruptedHistoryCmd())
174		}
175	}
176	return m, tea.Batch(saveCmd, m.waitForLLMUpdate())
177}
178
179// finishTurn clears in-flight state and cancels the turn context.
180func (m *Model) finishTurn() {
181	m.llmInFlight = false
182	if m.turnCancel != nil {
183		m.turnCancel()
184		m.turnCancel = nil
185	}
186	m.turnCtx = nil
187}
188
189// patchInterruptedHistory appends a synthetic assistant message if the
190// interrupted turn left history in an invalid API state (trailing tool_use
191// without result, or trailing tool result with no assistant follow-up).
192func (m *Model) patchInterruptedHistoryCmd() tea.Cmd {
193	if m.store == nil {
194		return nil
195	}
196	cache := m.store
197	sessionID := m.sessionID
198	storeQueue := m.storeQueue
199	return func() tea.Msg {
200		last, err := cache.LoadLastMessage(sessionID)
201		if err != nil || last == nil {
202			return nil
203		}
204		hasToolCalls := len(last.ToolCalls) > 0 && string(last.ToolCalls) != "[]"
205		needsPatch := last.Role != roleAssistant || hasToolCalls
206		if needsPatch {
207			interruptMsg := provider.Message{
208				Role:      roleAssistant,
209				Content:   "The user interrupted me.",
210				CreatedAt: time.Now(),
211			}
212			stored := []store.SessionMessage{messageToStore(interruptMsg)}
213			if enqueueStoreBatch(storeQueue, storeBatch{sessionID: sessionID, msgs: stored}) {
214				return nil
215			}
216			if err := cache.SaveMessages(sessionID, stored); err != nil {
217				return nil
218			}
219		}
220		return nil
221	}
222}
223
224// brailleFrames is the spinner animation sequence.
225var brailleFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
226
227// tickSpinner advances the braille spinner frame based on elapsed time.
228// Slow (~500ms/frame) when idle, fast (~100ms/frame) during LLM turns.
229func (m *Model) tickSpinner(now time.Time) {
230	interval := 500 * time.Millisecond
231	if m.llmInFlight {
232		interval = 100 * time.Millisecond
233	}
234	if now.Sub(m.spinFrameAt) >= interval {
235		m.spinFrame = (m.spinFrame + 1) % len(brailleFrames)
236		m.spinFrameAt = now
237	}
238}
239
240// tickStreaming rebuilds streaming entries when new content has arrived.
241func (m *Model) tickStreaming() {
242	if m.streamDirty {
243		m.rebuildStreamEntries()
244		m.streamDirty = false
245	}
246}
247
248// ensureStreaming initialises stream state on the first delta of a turn.
249func (m *Model) ensureStreaming() {
250	if m.streaming {
251		return
252	}
253	m.streaming = true
254	m.streamEntryStart = len(m.convEntries)
255	m.streamingReasoning = ""
256	m.streamingContent = ""
257}
258
259// applyAssistantMsg finalizes streaming state and appends the assistant's
260// response entries. Extracted so handleLLMBatch can reuse the logic.
261func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
262	m.clearStreaming()
263	if msg.reasoning != "" {
264		wasBottom := m.appendText(styledLines(msg.reasoning, m.styles.Muted)...)
265		m.appendText("")
266		if wasBottom {
267			m.scrollOffset = 0
268		}
269	}
270	if msg.content != "" {
271		wasBottom := m.appendText(highlightMarkdown(msg.content, m.styles.Text)...)
272		m.appendText("")
273		if wasBottom {
274			m.scrollOffset = 0
275		}
276	}
277	for _, tc := range msg.toolCalls {
278		if m.pendingToolCalls == nil {
279			m.pendingToolCalls = make(map[string]provider.ToolCall)
280		}
281		m.pendingToolCalls[tc.ID] = tc
282		display := m.styles.ToolArrow.Render("→") + m.styles.BgFill.Render("  ") + m.styles.ToolCall.Render(formatToolCall(tc))
283		wasBottom := m.appendConv(convEntry{display: display, kind: entryToolCall})
284		if wasBottom {
285			m.scrollOffset = 0
286		}
287	}
288}
289
290// cancelTurn gracefully interrupts the in-flight LLM turn.
291// History, turn boundaries, file changes, and deltas are preserved
292// so the user can undo via the message footer if desired.
293// turnCancel is kept non-nil until the drain loop finishes (see
294// handleLLMBatch) so the enter guard blocks new submissions.
295func (m *Model) cancelTurn() {
296	m.llmInFlight = false
297	m.clearStreaming()
298	m.appendText("", m.styles.Dim.Render("(interrupted)"), "")
299	m.scrollOffset = 0
300}
301
302func (m *Model) cancelTurnCmd() tea.Cmd {
303	if m.turnCancel == nil {
304		return nil
305	}
306	cancel := m.turnCancel
307	return func() tea.Msg {
308		cancel()
309		return nil
310	}
311}
312
313// clearStreaming resets active streaming state.
314func (m *Model) clearStreaming() {
315	if !m.streaming {
316		return
317	}
318	m.streaming = false
319	if m.streamEntryStart >= 0 && m.streamEntryStart <= len(m.convEntries) {
320		m.convEntries = m.convEntries[:m.streamEntryStart]
321	}
322	m.streamEntryStart = -1
323	m.streamingReasoning = ""
324	m.streamingContent = ""
325}
326
327// applyToolResultMsg appends tool result display entries.
328// It also clears any active streaming state so the next applyAssistantMsg doesn't truncate the tool result entries.
329func (m *Model) applyToolResultMsg(msg llmToolResultMsg) {
330	m.clearStreaming()
331
332	var filePath string
333	if sm := toolResultFileRe.FindStringSubmatch(msg.content); sm != nil {
334		filePath = sm[1]
335	}
336
337	// Resolve the tool name from the pending call.
338	var toolName string
339	if tc, ok := m.pendingToolCalls[msg.toolCallID]; ok {
340		toolName = tc.Name
341	}
342
343	// Extract target line for cursor positioning.
344	// Read results: from "(lines N-M)". Edit results: from the tool call arguments.
345	var startLine int
346	if sm := toolResultLineRe.FindStringSubmatch(msg.content); sm != nil {
347		startLine, _ = strconv.Atoi(sm[1])
348	} else if tc, ok := m.pendingToolCalls[msg.toolCallID]; ok && tc.Name == "Edit" {
349		startLine = toolCallEditLine(tc.Arguments)
350	}
351	if startLine == 0 && filePath != "" {
352		startLine = toolResultHashlineStart(msg.content, filePath)
353	}
354
355	body, diagLines := extractDiagLines(msg.content)
356	if idx := strings.Index(body, "\n"); idx >= 0 {
357		body = body[:idx]
358	}
359
360	// Build display: "← summary  [view]"
361	arrow := m.styles.ToolArrow.Render("←") + m.styles.BgFill.Render("  ")
362	summary := arrow + m.styleToolResultLine(body)
363	viewBtn := m.styles.BgFill.Render("  ") + m.styles.Clickable.Render("view")
364	display := summary + viewBtn
365
366	entry := convEntry{
367		display:  display,
368		kind:     entryToolResult,
369		filePath: filePath,
370		full:     msg.content,
371		line:     startLine,
372		toolName: toolName,
373	}
374	wasBottom := m.appendConv(entry)
375	for _, dl := range diagLines {
376		m.appendConv(convEntry{display: m.styleToolResultLine(dl), kind: entryToolDiag, full: msg.content})
377	}
378	if wasBottom {
379		m.scrollOffset = 0
380	}
381}
382
383// extractDiagLines splits diagnostic lines from tool result content.
384// Returns the body without the diagnostics block and the ERROR/WARNING lines.
385func extractDiagLines(content string) (body string, diags []string) {
386	idx := strings.Index(content, "\nLSP diagnostics:")
387	if idx < 0 {
388		return content, nil
389	}
390	for _, dl := range strings.Split(content[idx+1:], "\n") {
391		if strings.HasPrefix(dl, "ERROR ") || strings.HasPrefix(dl, "WARNING ") {
392			diags = append(diags, dl)
393		}
394	}
395	return content[:idx], diags
396}
397
398// styleToolResultLine applies semantic styling to a tool result line.
399// Diagnostic lines (ERROR/WARNING) get colored; everything else is dim.
400func (m *Model) styleToolResultLine(line string) string {
401	switch {
402	case strings.HasPrefix(line, "ERROR "):
403		return m.styles.Error.Render(line)
404	case strings.HasPrefix(line, "WARNING "):
405		return m.styles.Warning.Render(line)
406	default:
407		return m.styles.Dim.Render(line)
408	}
409}
410
411// toolCallEditLine extracts the target line from an Edit tool call's arguments.
412// Returns the start line of the edit operation (replace/insert/delete), or 0.
413func toolCallEditLine(args json.RawMessage) int {
414	var parsed struct {
415		Replace *struct {
416			Start struct {
417				Line int `json:"line"`
418			} `json:"start"`
419		} `json:"replace"`
420		Insert *struct {
421			After struct {
422				Line int `json:"line"`
423			} `json:"after"`
424		} `json:"insert"`
425		Delete *struct {
426			Start struct {
427				Line int `json:"line"`
428			} `json:"start"`
429		} `json:"delete"`
430	}
431	if json.Unmarshal(args, &parsed) != nil {
432		return 0
433	}
434	switch {
435	case parsed.Replace != nil:
436		return parsed.Replace.Start.Line
437	case parsed.Insert != nil:
438		return parsed.Insert.After.Line
439	case parsed.Delete != nil:
440		return parsed.Delete.Start.Line
441	}
442	return 0
443}
444
445// toolResultHashlineStart scans a tool result body for the first hashline and
446// returns its line number if it matches the provided file path.
447func toolResultHashlineStart(content, filePath string) int {
448	prefix := "Read " + filePath + " "
449	if !strings.HasPrefix(content, prefix) {
450		prefix = "Edited " + filePath + " "
451		if !strings.HasPrefix(content, prefix) {
452			prefix = "Created " + filePath + " "
453			if !strings.HasPrefix(content, prefix) {
454				return 0
455			}
456		}
457	}
458	lines := strings.Split(content, "\n")
459	for _, line := range lines {
460		if line == "" {
461			continue
462		}
463		idx := strings.IndexByte(line, '|')
464		if idx <= 0 {
465			continue
466		}
467		anchor := line[:idx]
468		parsed, err := hashline.ParseAnchor(anchor)
469		if err != nil {
470			continue
471		}
472		return parsed.Num
473	}
474	return 0
475}
476
477// formatToolCall renders a tool call as Name(key="val", key2="val2").
478// Long values are truncated. Falls back to Name(...) on parse errors.
479func formatToolCall(tc provider.ToolCall) string {
480	const maxVal = 40
481	var args map[string]json.RawMessage
482	if err := json.Unmarshal(tc.Arguments, &args); err != nil || len(args) == 0 {
483		return tc.Name + "(...)"
484	}
485	keys := make([]string, 0, len(args))
486	for k := range args {
487		keys = append(keys, k)
488	}
489	sort.Strings(keys)
490	parts := make([]string, 0, len(keys))
491	for _, k := range keys {
492		var v interface{}
493		if err := json.Unmarshal(args[k], &v); err != nil {
494			continue
495		}
496		s := fmt.Sprintf("%v", v)
497		if len(s) > maxVal {
498			s = s[:maxVal] + "…"
499		}
500		parts = append(parts, k+"="+s)
501	}
502	return tc.Name + "(" + strings.Join(parts, ", ") + ")"
503}