@@ -30,6 +30,8 @@ type Model struct {
3030 history []provider.Message
3131 conversation []string // Formatted conversation log for display
3232 updateChan chan tea.Msg // Channel for streaming LLM updates
33+ ctx context.Context
34+ cancel context.CancelFunc
3335 }
3436
3537 // New creates a new TUI model
@@ -59,7 +61,8 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool) Model {
5961 agentInput.Cursor.Style = cursorStyle
6062 agentInput.Focus()
6163
62- updateChan := make(chan tea.Msg, 100)
64+ updateChan := make(chan tea.Msg, 500)
65+ ctx, cancel := context.WithCancel(context.Background())
6366
6467 return Model{
6568 spinner: s,
@@ -71,6 +74,8 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool) Model {
7174 history: []provider.Message{},
7275 conversation: []string{},
7376 updateChan: updateChan,
77+ ctx: ctx,
78+ cancel: cancel,
7479 }
7580 }
7681
@@ -105,6 +110,16 @@ type llmDoneMsg struct {
105110 timestamp string
106111 }
107112
113+// llmHistoryMsg updates history with new message (ELM Msg)
114+type llmHistoryMsg struct {
115+ msg provider.Message
116+}
117+
118+// llmErrorMsg handles LLM errors (ELM Msg)
119+type llmErrorMsg struct {
120+ err error
121+}
122+
108123 // sendToLLM sends user message to LLM (ELM Cmd)
109124 func (m Model) sendToLLM(userInput string) tea.Cmd {
110125 return func() tea.Msg {
@@ -123,9 +138,9 @@ func (m Model) waitForLLMUpdate() tea.Cmd {
123138 // processLLM runs LLM turn in background (ELM Cmd)
124139 func (m Model) processLLM() tea.Cmd {
125140 return func() tea.Msg {
126- startTime := time.Now()
127-
128141 go func() {
142+ startTime := time.Now()
143+
129144 opts := llm.ProcessTurnOptions{
130145 Provider: m.provider,
131146 Proxy: m.mcpProxy,
@@ -133,6 +148,9 @@ func (m Model) processLLM() tea.Cmd {
133148 History: m.history,
134149 MaxToolRounds: 20,
135150 OnMessage: func(msg provider.Message) {
151+ // Send message to update history
152+ m.updateChan <- llmHistoryMsg{msg: msg}
153+
136154 if msg.Role == "assistant" {
137155 // Send tool calls
138156 for _, tc := range msg.ToolCalls {
@@ -150,7 +168,11 @@ func (m Model) processLLM() tea.Cmd {
150168 },
151169 }
152170
153- _ = llm.ProcessTurn(context.Background(), opts)
171+ err := llm.ProcessTurn(m.ctx, opts)
172+ if err != nil {
173+ m.updateChan <- llmErrorMsg{err: err}
174+ return
175+ }
154176
155177 // Send done message
156178 m.updateChan <- llmDoneMsg{
@@ -159,8 +181,7 @@ func (m Model) processLLM() tea.Cmd {
159181 }
160182 }()
161183
162- // Start waiting for updates
163- return m.waitForLLMUpdate()
184+ return nil
164185 }
165186 }
166187
@@ -172,6 +193,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
172193 case tea.KeyMsg:
173194 switch msg.String() {
174195 case "ctrl+c":
196+ m.cancel() // Cancel any running goroutines
175197 return m, tea.Quit
176198 case "esc":
177199 if m.agentInput.Focused() {
@@ -213,8 +235,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
213235 m.conversation = append(m.conversation, grayStyle.Render(line))
214236 }
215237 m.conversation = append(m.conversation, "")
216- // Start LLM processing
217- return m, m.processLLM()
238+ // Start LLM processing and wait for updates
239+ return m, tea.Batch(m.processLLM(), m.waitForLLMUpdate())
240+
241+ case llmHistoryMsg:
242+ // Update history with message from LLM
243+ m.history = append(m.history, msg.msg)
244+ return m, m.waitForLLMUpdate() // Continue listening
218245
219246 case llmAssistantMsg:
220247 for _, line := range strings.Split(msg.content, "\n") {
@@ -231,6 +258,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
231258 m.conversation = append(m.conversation, "← "+msg.content)
232259 return m, m.waitForLLMUpdate() // Continue listening
233260
261+ case llmErrorMsg:
262+ // Display error message
263+ m.conversation = append(m.conversation, "")
264+ errLine := lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")).Render("Error: " + msg.err.Error())
265+ m.conversation = append(m.conversation, errLine)
266+ m.conversation = append(m.conversation, "")
267+ return m, nil // Done listening
268+
234269 case llmDoneMsg:
235270 // Add timestamp separator (blank line first, then separator)
236271 m.conversation = append(m.conversation, "")