@@ -125,8 +125,8 @@ func (m Model) Init() tea.Cmd {
125125 }
126126
127127 // wrapText wraps text to the conversation pane width, applying style after wrapping.
128-// Returns lines that fit within the pane width.
129-func (m *Model) wrapText(text string, style lipgloss.Style) []string {
128+// Returns lines that fit within the pane width and the updated model with cache.
129+func (m Model) wrapText(text string, style lipgloss.Style) ([]string, Model) {
130130 if m.rightPaneWidth <= 0 {
131131 // Fallback if width not set yet
132132 lines := strings.Split(text, "\n")
@@ -134,7 +134,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
134134 for i, line := range lines {
135135 result[i] = style.Render(line)
136136 }
137- return result
137+ return result, m
138138 }
139139
140140 // Create cache key from text and width
@@ -142,7 +142,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
142142
143143 // Check cache
144144 if cached, ok := m.wrapCache[cacheKey]; ok {
145- return cached
145+ return cached, m
146146 }
147147
148148 // Word wrap first, then apply style to each line
@@ -162,7 +162,7 @@ func (m *Model) wrapText(text string, style lipgloss.Style) []string {
162162 }
163163 m.wrapCache[cacheKey] = result
164164
165- return result
165+ return result, m
166166 }
167167
168168 // llmUserMsg adds user message to conversation (ELM Msg)
@@ -205,6 +205,20 @@ type UpdateToolsMsg struct {
205205 Tools []mcp.Tool
206206 }
207207
208+// clipboardCopyMsg is sent to copy text to clipboard (ELM Msg)
209+type clipboardCopyMsg struct {
210+ text string
211+}
212+
213+// copyToClipboardCmd copies text to clipboard asynchronously (ELM Cmd)
214+func copyToClipboardCmd(text string) tea.Cmd {
215+ return func() tea.Msg {
216+ // Fire and forget - clipboard operations are best effort
217+ _ = clipboard.WriteAll(text)
218+ return nil
219+ }
220+}
221+
208222 // sendToLLM sends user message to LLM (ELM Cmd)
209223 func (m Model) sendToLLM(userInput string) tea.Cmd {
210224 return func() tea.Msg {
@@ -222,30 +236,42 @@ func (m Model) waitForLLMUpdate() tea.Cmd {
222236
223237 // processLLM runs LLM turn in background (ELM Cmd)
224238 func (m Model) processLLM() tea.Cmd {
239+ // Copy values to avoid capturing model state
240+ prov := m.provider
241+ proxy := m.mcpProxy
242+ // Copy tools slice
243+ tools := make([]mcp.Tool, len(m.mcpTools))
244+ copy(tools, m.mcpTools)
245+ // Copy history slice
246+ history := make([]provider.Message, len(m.history))
247+ copy(history, m.history)
248+ updateChan := m.updateChan
249+ ctx := m.ctx
250+
225251 return func() tea.Msg {
226252 go func() {
227253 startTime := time.Now()
228254
229255 opts := llm.ProcessTurnOptions{
230- Provider: m.provider,
231- Proxy: m.mcpProxy,
232- Tools: m.mcpTools,
233- History: m.history,
256+ Provider: prov,
257+ Proxy: proxy,
258+ Tools: tools,
259+ History: history,
234260 MaxToolRounds: 20,
235261 OnMessage: func(msg provider.Message) {
236262 // Send message to update history
237- m.updateChan <- llmHistoryMsg{msg: msg}
263+ updateChan <- llmHistoryMsg{msg: msg}
238264
239265 if msg.Role == "assistant" {
240266 // Send complete assistant message as single block
241- m.updateChan <- llmAssistantMsg{
267+ updateChan <- llmAssistantMsg{
242268 reasoning: msg.Reasoning,
243269 content: msg.Content,
244270 toolCalls: msg.ToolCalls,
245271 }
246272 } else if msg.Role == "tool" {
247273 // Send full tool result (wrapping will be handled by wrapText)
248- m.updateChan <- llmToolResultMsg{
274+ updateChan <- llmToolResultMsg{
249275 toolCallID: msg.ToolCallID,
250276 content: msg.Content,
251277 }
@@ -253,14 +279,14 @@ func (m Model) processLLM() tea.Cmd {
253279 },
254280 }
255281
256- err := llm.ProcessTurn(m.ctx, opts)
282+ err := llm.ProcessTurn(ctx, opts)
257283 if err != nil {
258- m.updateChan <- llmErrorMsg{err: err}
284+ updateChan <- llmErrorMsg{err: err}
259285 return
260286 }
261287
262288 // Send done message
263- m.updateChan <- llmDoneMsg{
289+ updateChan <- llmDoneMsg{
264290 duration: time.Since(startTime),
265291 timestamp: startTime.Format("15:04"),
266292 }
@@ -387,8 +413,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
387413 }
388414 }
389415
390- // Copy to clipboard (best effort, ignore errors)
391- _ = clipboard.WriteAll(selectedText.String())
416+ // Copy to clipboard via command
417+ cmds = append(cmds, copyToClipboardCmd(selectedText.String()))
392418 }
393419 // Clear selection
394420 m.conversationSelectStart = 0
@@ -450,7 +476,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
450476 })
451477 // Format and display user message first
452478 grayStyle := lipgloss.NewStyle().Foreground(lipgloss.NoColor{})
453- wrappedLines := m.wrapText(msg.content, grayStyle)
479+ wrappedLines, m := m.wrapText(msg.content, grayStyle)
454480 m.conversation = append(m.conversation, wrappedLines...)
455481 m.conversation = append(m.conversation, "")
456482 // Add timestamp separator after message
@@ -482,21 +508,24 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
482508
483509 // Display reasoning if present
484510 if msg.reasoning != "" {
485- wrappedLines := m.wrapText(msg.reasoning, grayStyle)
511+ var wrappedLines []string
512+ wrappedLines, m = m.wrapText(msg.reasoning, grayStyle)
486513 m.conversation = append(m.conversation, wrappedLines...)
487514 m.conversation = append(m.conversation, "")
488515 }
489516
490517 // Display content
491518 if msg.content != "" {
492- wrappedLines := m.wrapText(msg.content, plainStyle)
519+ var wrappedLines []string
520+ wrappedLines, m = m.wrapText(msg.content, plainStyle)
493521 m.conversation = append(m.conversation, wrappedLines...)
494522 m.conversation = append(m.conversation, "")
495523 }
496524
497525 // Display tool calls
498526 for _, tc := range msg.toolCalls {
499- wrappedLines := m.wrapText("→ "+tc.Name+"(...)", plainStyle)
527+ var wrappedLines []string
528+ wrappedLines, m = m.wrapText("→ "+tc.Name+"(...)", plainStyle)
500529 m.conversation = append(m.conversation, wrappedLines...)
501530 }
502531
@@ -504,7 +533,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
504533
505534 case llmToolResultMsg:
506535 plainStyle := lipgloss.NewStyle()
507- wrappedLines := m.wrapText("← "+msg.content, plainStyle)
536+ wrappedLines, m := m.wrapText("← "+msg.content, plainStyle)
508537 m.conversation = append(m.conversation, wrappedLines...)
509538 return m, m.waitForLLMUpdate() // Continue listening