xonecas.io
62159
Sy

symb / files

symb / internal / tui / messages.go

12.0 KB
1package tui
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"os"
8	"os/exec"
9	"strings"
10	"time"
11
12	tea "charm.land/bubbletea/v2"
13	"github.com/rs/zerolog/log"
14	"github.com/xonecas/symb/internal/delta"
15	"github.com/xonecas/symb/internal/llm"
16	"github.com/xonecas/symb/internal/mcp"
17	"github.com/xonecas/symb/internal/provider"
18	"github.com/xonecas/symb/internal/store"
19)
20
21// ---------------------------------------------------------------------------
22// ELM messages
23// ---------------------------------------------------------------------------
24
25type llmUserMsg struct {
26	display string // raw text shown in conversation (with @tokens)
27	content string // expanded text sent to LLM (@ tokens replaced with file content)
28}
29
30type llmAssistantMsg struct {
31	reasoning string
32	content   string
33	toolCalls []provider.ToolCall
34}
35
36type llmToolResultMsg struct {
37	toolCallID string
38	content    string
39}
40
41type llmDoneMsg struct {
42	duration      time.Duration
43	timestamp     string
44	inputTokens   int
45	outputTokens  int
46	contextTokens int
47}
48
49type llmUsageMsg struct {
50	inputTokens  int
51	outputTokens int
52}
53
54type storeBatch struct {
55	sessionID string
56	msgs      []store.SessionMessage
57}
58
59type llmHistoryMsg struct{ msg provider.Message }
60type llmErrorMsg struct{ err error }
61
62type modelsFetchedMsg struct {
63	models []provider.TaggedModel
64	err    error
65}
66
67type modelSwitchedMsg struct {
68	modelName    string
69	providerName string
70	prov         provider.Provider
71	err          error
72}
73
74type userMsgSavedMsg struct {
75	convIdx int
76	dbMsgID int64
77	userMsg provider.Message
78	err     error
79}
80
81type undoResultMsg struct{ err error }
82
83// Streaming delta messages
84type llmContentDeltaMsg struct{ content string }
85type llmReasoningDeltaMsg struct{ content string }
86
87// tickMsg drives the 60fps frame loop (~16ms). Rendering work (highlight,
88// wrap) is deferred to this tick so streaming deltas don't cause per-batch
89// rebuilds.
90type tickMsg time.Time
91
92// undoMsg is sent when the user clicks the undo control.
93type undoMsg struct{}
94
95// openToolViewMsg is sent when the user clicks the [view] button on a tool result.
96type openToolViewMsg struct {
97	title   string
98	content string
99}
100
101// llmBatchMsg carries multiple messages drained from updateChan in one go.
102type llmBatchMsg []tea.Msg
103
104// UpdateToolsMsg is exported so main.go can send it via program.Send.
105type UpdateToolsMsg struct{ Tools []mcp.Tool }
106
107// LSPDiagnosticsMsg carries diagnostic line severities from the LSP manager to the TUI.
108type LSPDiagnosticsMsg struct {
109	FilePath string      // absolute path of the file
110	Lines    map[int]int // bufRow (0-indexed) -> max severity (1=error, 2=warning)
111}
112
113// gitBranchMsg carries the current git branch and dirty status.
114type gitBranchMsg struct {
115	branch   string
116	dirty    bool
117	added    int
118	modified int
119	removed  int
120}
121
122// ---------------------------------------------------------------------------
123// ELM commands
124// ---------------------------------------------------------------------------
125
126// frameTick returns a command that fires a tickMsg after ~16ms (~60fps).
127func frameTick() tea.Cmd {
128	return tea.Tick(16*time.Millisecond, func(t time.Time) tea.Msg {
129		return tickMsg(t)
130	})
131}
132
133func gitStatusCounts() (added int, modified int, removed int) {
134	out, err := exec.Command("git", "status", "--porcelain").Output()
135	if err != nil {
136		return 0, 0, 0
137	}
138	trimmed := strings.TrimSpace(string(out))
139	if trimmed == "" {
140		return 0, 0, 0
141	}
142	for _, line := range strings.Split(trimmed, "\n") {
143		if line == "" {
144			continue
145		}
146		if strings.HasPrefix(line, "??") {
147			added++
148			continue
149		}
150		if len(line) < 2 {
151			continue
152		}
153		x := line[0]
154		y := line[1]
155		if x == 'A' || y == 'A' {
156			added++
157		}
158		if x == 'M' || y == 'M' || x == 'R' || y == 'R' || x == 'C' || y == 'C' {
159			modified++
160		}
161		if x == 'D' || y == 'D' {
162			removed++
163		}
164	}
165	return added, modified, removed
166}
167
168// gitBranchCmd runs git to detect the current branch and dirty status.
169func gitBranchCmd() tea.Cmd {
170	return func() tea.Msg {
171		branch := ""
172		if out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output(); err == nil {
173			branch = strings.TrimSpace(string(out))
174		}
175		dirty := false
176		if err := exec.Command("git", "diff", "--quiet", "HEAD").Run(); err != nil {
177			dirty = true // exit code 1 = dirty
178		}
179		added, modified, removed := gitStatusCounts()
180		return gitBranchMsg{branch: branch, dirty: dirty, added: added, modified: modified, removed: removed}
181	}
182}
183
184// gitBranchTick schedules a git branch re-poll after a 5-second delay.
185func gitBranchTick() tea.Cmd {
186	return tea.Tick(5*time.Second, func(time.Time) tea.Msg {
187		// Run git commands inline after the delay to avoid an extra message type.
188		branch := ""
189		if out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output(); err == nil {
190			branch = strings.TrimSpace(string(out))
191		}
192		dirty := false
193		if err := exec.Command("git", "diff", "--quiet", "HEAD").Run(); err != nil {
194			dirty = true
195		}
196		added, modified, removed := gitStatusCounts()
197		return gitBranchMsg{branch: branch, dirty: dirty, added: added, modified: modified, removed: removed}
198	})
199}
200
201func (m Model) sendToLLM(display, content string) tea.Cmd {
202	return func() tea.Msg { return llmUserMsg{display: display, content: content} }
203}
204
205func (m Model) waitForLLMUpdate() tea.Cmd {
206	ch := m.updateChan
207	return func() tea.Msg {
208		// Block until at least one message arrives.
209		first := <-ch
210		batch := llmBatchMsg{first}
211		// Drain all pending messages without blocking.
212		for {
213			select {
214			case msg := <-ch:
215				batch = append(batch, msg)
216			default:
217				return batch
218			}
219		}
220	}
221}
222
223func (m Model) processLLMWithExtra(extra []provider.Message) tea.Cmd {
224	deps := m.llmTurnDeps()
225	return func() tea.Msg {
226		go m.runLLMTurn(deps, extra)
227		return nil
228	}
229}
230
231type llmTurnDeps struct {
232	provider  provider.Provider
233	proxy     *mcp.Proxy
234	tools     []mcp.Tool
235	store     *store.Cache
236	sessionID string
237	ch        chan tea.Msg
238	ctx       context.Context
239	dt        *delta.Tracker
240	pad       llm.ScratchpadReader
241	systemMsg *provider.Message
242}
243
244type usageTracker struct {
245	turnIn    int
246	turnOut   int
247	contextIn int
248}
249
250func (m Model) llmTurnDeps() llmTurnDeps {
251	tools := make([]mcp.Tool, len(m.mcpTools))
252	copy(tools, m.mcpTools)
253	return llmTurnDeps{
254		provider:  m.provider,
255		proxy:     m.mcpProxy,
256		tools:     tools,
257		store:     m.store,
258		sessionID: m.sessionID,
259		ch:        m.updateChan,
260		ctx:       m.turnCtx,
261		dt:        m.deltaTracker,
262		pad:       m.scratchpad,
263		systemMsg: m.initialSystemMsg,
264	}
265}
266
267func (m Model) runLLMTurn(deps llmTurnDeps, extra []provider.Message) {
268	history, err := loadHistory(deps.store, deps.sessionID)
269	if err != nil {
270		deps.ch <- llmErrorMsg{err: fmt.Errorf("load history: %w", err)}
271		return
272	}
273	history = ensureSystemMessage(history, deps.systemMsg)
274	if len(extra) > 0 {
275		// extra carries the current user message in its expanded form. The DB
276		// already saved the display form as the last entry, so trim it to avoid
277		// sending a duplicate to the LLM.
278		if n := len(history); n > 0 && history[n-1].Role == "user" {
279			history = history[:n-1]
280		}
281		history = append(history, extra...)
282	}
283
284	preSnap, snapRoot := snapshotBeforeTurn(deps.dt)
285
286	start := time.Now()
287	usage := &usageTracker{}
288	err = llm.ProcessTurn(deps.ctx, llm.ProcessTurnOptions{
289		Provider:   deps.provider,
290		Proxy:      deps.proxy,
291		Tools:      deps.tools,
292		History:    history,
293		Scratchpad: deps.pad,
294		OnDelta: func(evt provider.StreamEvent) {
295			dispatchStreamEvent(deps.ch, evt)
296		},
297		OnUsage: usage.onUsage(deps.ch),
298		OnMessage: func(msg provider.Message) {
299			dispatchHistoryMessage(deps.ch, msg)
300		},
301	})
302
303	recordTurnDeltas(deps.dt, snapRoot, preSnap, err)
304	if err != nil {
305		deps.ch <- llmErrorMsg{err: err}
306		return
307	}
308	deps.ch <- llmDoneMsg{
309		duration:      time.Since(start),
310		timestamp:     start.Format("15:04"),
311		inputTokens:   usage.turnIn,
312		outputTokens:  usage.turnOut,
313		contextTokens: usage.contextIn,
314	}
315}
316
317func loadHistory(db *store.Cache, sessionID string) ([]provider.Message, error) {
318	if db == nil {
319		return nil, nil
320	}
321	stored, err := db.LoadMessages(sessionID)
322	if err != nil {
323		return nil, err
324	}
325	return store.ToProviderMessages(stored), nil
326}
327
328func ensureSystemMessage(history []provider.Message, systemMsg *provider.Message) []provider.Message {
329	if systemMsg == nil {
330		return history
331	}
332	for _, msg := range history {
333		if msg.Role == "system" {
334			return history
335		}
336	}
337	return append([]provider.Message{*systemMsg}, history...)
338}
339
340func snapshotBeforeTurn(dt *delta.Tracker) (map[string]delta.FileSnapshot, string) {
341	if dt == nil || dt.TurnID() == 0 {
342		return nil, ""
343	}
344	cwd, err := os.Getwd()
345	if err != nil {
346		return nil, ""
347	}
348	return delta.SnapshotDir(cwd), cwd
349}
350
351func recordTurnDeltas(dt *delta.Tracker, snapRoot string, preSnap map[string]delta.FileSnapshot, err error) {
352	if preSnap == nil || err != nil {
353		return
354	}
355	postSnap := delta.SnapshotDir(snapRoot)
356	delta.RecordDeltas(dt, snapRoot, preSnap, postSnap)
357}
358
359func dispatchStreamEvent(ch chan tea.Msg, evt provider.StreamEvent) {
360	switch evt.Type {
361	case provider.EventContentDelta:
362		ch <- llmContentDeltaMsg{content: evt.Content}
363	case provider.EventReasoningDelta:
364		ch <- llmReasoningDeltaMsg{content: evt.Content}
365	}
366}
367
368func dispatchHistoryMessage(ch chan tea.Msg, msg provider.Message) {
369	ch <- llmHistoryMsg{msg: msg}
370	switch msg.Role {
371	case roleAssistant:
372		ch <- llmAssistantMsg{
373			reasoning: msg.Reasoning,
374			content:   msg.Content,
375			toolCalls: msg.ToolCalls,
376		}
377	case "tool":
378		ch <- llmToolResultMsg{
379			toolCallID: msg.ToolCallID,
380			content:    msg.Content,
381		}
382	}
383}
384
385func (u *usageTracker) onUsage(ch chan tea.Msg) func(int, int) {
386	return func(inputTokens, outputTokens int) {
387		u.turnIn += inputTokens
388		u.turnOut += outputTokens
389		u.contextIn = inputTokens
390		ch <- llmUsageMsg{inputTokens: inputTokens, outputTokens: outputTokens}
391	}
392}
393
394// messageToStore converts a provider.Message to a store.SessionMessage.
395func messageToStore(msg provider.Message) store.SessionMessage {
396	var tc json.RawMessage
397	if len(msg.ToolCalls) > 0 {
398		encoded, err := json.Marshal(msg.ToolCalls)
399		if err != nil {
400			log.Warn().Err(err).Msg("failed to marshal tool calls")
401		} else {
402			tc = encoded
403		}
404	}
405	return store.SessionMessage{
406		Role:         msg.Role,
407		Content:      msg.Content,
408		Reasoning:    msg.Reasoning,
409		ToolCalls:    tc,
410		ToolCallID:   msg.ToolCallID,
411		CreatedAt:    msg.CreatedAt,
412		InputTokens:  msg.InputTokens,
413		OutputTokens: msg.OutputTokens,
414	}
415}
416
417func (m *Model) saveMessages(msgs []provider.Message) {
418	if m.store == nil || len(msgs) == 0 {
419		return
420	}
421	stored := make([]store.SessionMessage, 0, len(msgs))
422	for _, msg := range msgs {
423		stored = append(stored, messageToStore(msg))
424	}
425	if enqueueStoreBatch(m.storeQueue, storeBatch{sessionID: m.sessionID, msgs: stored}) {
426		return
427	}
428	if err := m.store.SaveMessages(m.sessionID, stored); err != nil {
429		log.Warn().Err(err).Msg("failed to save message batch")
430	}
431}
432
433func (m Model) saveMessagesCmd(msgs []provider.Message) tea.Cmd {
434	if m.store == nil || len(msgs) == 0 {
435		return nil
436	}
437	return func() tea.Msg {
438		m.saveMessages(msgs)
439		return nil
440	}
441}
442
443func enqueueStoreBatch(queue chan storeBatch, batch storeBatch) (ok bool) {
444	if queue == nil {
445		return false
446	}
447	defer func() {
448		if recover() != nil {
449			ok = false
450		}
451	}()
452	select {
453	case queue <- batch:
454		return true
455	default:
456		log.Warn().Msg("store queue full; dropping message batch")
457		return false
458	}
459}
460
461func startStoreWorker(db *store.Cache, queue <-chan storeBatch) <-chan struct{} {
462	done := make(chan struct{})
463	go func() {
464		defer close(done)
465		for batch := range queue {
466			for attempt := 0; attempt <= store.SQLiteBusyMaxRetries; attempt++ {
467				err := db.SaveMessages(batch.sessionID, batch.msgs)
468				if err == nil {
469					break
470				}
471				if !store.IsSQLiteBusy(err) || attempt == store.SQLiteBusyMaxRetries {
472					log.Warn().Err(err).Msg("failed to save message batch")
473					break
474				}
475				log.Warn().Err(err).Msg("retrying message save after busy")
476				backoff := time.Duration((attempt+1)*store.SQLiteBusyBackoffStepMs) * time.Millisecond
477				if backoff > store.SQLiteBusyMaxBackoff {
478					backoff = store.SQLiteBusyMaxBackoff
479				}
480				time.Sleep(backoff)
481			}
482		}
483	}()
484	return done
485}