@@ -100,6 +100,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
100100 }
101101 }
102102
103+ // -- LLM batch (multiple messages drained from updateChan) ---------------
104+ case llmBatchMsg:
105+ return m.handleLLMBatch(msg)
106+
103107 // -- LLM messages --------------------------------------------------------
104108 case llmUserMsg:
105109 now := time.Now()
@@ -120,6 +124,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
120124 if !m.streaming {
121125 m.streaming = true
122126 m.streamEntryStart = len(m.convEntries)
127+ m.streamWrapStart = len(m.wrappedConvLines())
123128 m.streamingReasoning = ""
124129 m.streamingContent = ""
125130 }
@@ -134,6 +139,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
134139 if !m.streaming {
135140 m.streaming = true
136141 m.streamEntryStart = len(m.convEntries)
142+ m.streamWrapStart = len(m.wrappedConvLines())
137143 m.streamingReasoning = ""
138144 m.streamingContent = ""
139145 }
@@ -277,6 +283,159 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
277283 return m, tea.Batch(cmds...)
278284 }
279285
286+// handleLLMBatch processes a batch of messages drained from updateChan.
287+// Delta messages are accumulated first, with a single rebuildStreamEntries
288+// at the end, avoiding per-token re-wraps.
289+func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
290+ needRebuild := false
291+ var finalCmd tea.Cmd
292+
293+ for _, raw := range batch {
294+ switch msg := raw.(type) {
295+ case llmReasoningDeltaMsg:
296+ if !m.streaming {
297+ m.streaming = true
298+ m.streamEntryStart = len(m.convEntries)
299+ m.streamWrapStart = len(m.wrappedConvLines())
300+ m.streamingReasoning = ""
301+ m.streamingContent = ""
302+ }
303+ m.streamingReasoning += msg.content
304+ needRebuild = true
305+
306+ case llmContentDeltaMsg:
307+ if !m.streaming {
308+ m.streaming = true
309+ m.streamEntryStart = len(m.convEntries)
310+ m.streamWrapStart = len(m.wrappedConvLines())
311+ m.streamingReasoning = ""
312+ m.streamingContent = ""
313+ }
314+ m.streamingContent += msg.content
315+ needRebuild = true
316+
317+ case llmHistoryMsg:
318+ m.history = append(m.history, msg.msg)
319+
320+ case llmAssistantMsg:
321+ // Flush any pending streaming rebuild before finalizing.
322+ if needRebuild {
323+ m.rebuildStreamEntries()
324+ needRebuild = false
325+ }
326+ m.applyAssistantMsg(msg)
327+
328+ case llmToolResultMsg:
329+ if needRebuild {
330+ m.rebuildStreamEntries()
331+ needRebuild = false
332+ }
333+ m.applyToolResultMsg(msg)
334+
335+ case llmErrorMsg:
336+ if needRebuild {
337+ m.rebuildStreamEntries()
338+ }
339+ m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
340+ return m, nil
341+
342+ case llmDoneMsg:
343+ if needRebuild {
344+ m.rebuildStreamEntries()
345+ }
346+ m.appendText("")
347+ sep := m.makeSeparator(msg.duration.Round(time.Second).String(), msg.timestamp)
348+ m.appendText(sep)
349+ return m, nil
350+ }
351+ }
352+
353+ // Apply one rebuild for all accumulated deltas.
354+ if needRebuild {
355+ m.rebuildStreamEntries()
356+ }
357+
358+ // Keep scroll pinned to bottom during streaming.
359+ if m.scrollOffset == 0 {
360+ m.scrollOffset = 0
361+ }
362+
363+ // More messages may follow — keep listening.
364+ finalCmd = m.waitForLLMUpdate()
365+ return m, finalCmd
366+}
367+
368+// applyAssistantMsg finalizes streaming state and appends the assistant's
369+// response entries. Extracted so handleLLMBatch can reuse the logic.
370+func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
371+ if m.streaming {
372+ m.streaming = false
373+ if m.streamEntryStart >= 0 && m.streamEntryStart <= len(m.convEntries) {
374+ m.convEntries = m.convEntries[:m.streamEntryStart]
375+ }
376+ m.streamEntryStart = -1
377+ m.streamingReasoning = ""
378+ m.streamingContent = ""
379+ m.convLines = nil
380+ }
381+ if msg.reasoning != "" {
382+ wasBottom := m.appendText(styledLines(msg.reasoning, m.styles.Muted)...)
383+ m.appendText("")
384+ if wasBottom {
385+ m.scrollOffset = 0
386+ }
387+ }
388+ if msg.content != "" {
389+ wasBottom := m.appendText(styledLines(msg.content, m.styles.Text)...)
390+ m.appendText("")
391+ if wasBottom {
392+ m.scrollOffset = 0
393+ }
394+ }
395+ for _, tc := range msg.toolCalls {
396+ entry := m.styles.ToolArrow.Render("→") + " " + m.styles.ToolCall.Render(tc.Name+"(...)")
397+ wasBottom := m.appendText(entry)
398+ if wasBottom {
399+ m.scrollOffset = 0
400+ }
401+ }
402+}
403+
404+// applyToolResultMsg appends tool result display entries.
405+func (m *Model) applyToolResultMsg(msg llmToolResultMsg) {
406+ var filePath string
407+ if sm := toolResultFileRe.FindStringSubmatch(msg.content); sm != nil {
408+ filePath = sm[1]
409+ }
410+
411+ lines := strings.Split(msg.content, "\n")
412+ preview := lines
413+ truncated := false
414+ if len(lines) > maxPreviewLines {
415+ preview = lines[:maxPreviewLines]
416+ truncated = true
417+ }
418+
419+ arrow := m.styles.ToolArrow.Render("←") + " "
420+ var wasBottom bool
421+ for i, line := range preview {
422+ display := m.styles.Dim.Render(line)
423+ if i == 0 {
424+ display = arrow + display
425+ wasBottom = m.appendConv(convEntry{display: display, kind: entryToolResult, filePath: filePath, full: msg.content})
426+ } else {
427+ m.appendConv(convEntry{display: display, kind: entryToolResult, filePath: filePath, full: msg.content})
428+ }
429+ }
430+ if truncated {
431+ hint := fmt.Sprintf(" ... %d more lines (click to view)", len(lines)-maxPreviewLines)
432+ m.appendConv(convEntry{display: m.styles.Muted.Render(hint), kind: entryToolResult, filePath: filePath, full: msg.content})
433+ }
434+ if wasBottom {
435+ m.scrollOffset = 0
436+ }
437+}
438+
280439 // updateComponentSizes pushes layout dimensions to sub-models.
281440 func (m *Model) updateComponentSizes() {
282441 m.editor.SetWidth(m.layout.editor.Dx())