From a443ab28993c561d9cdf3e7b6a088c427887acb9 Mon Sep 17 00:00:00 2001 From: akdenizemirhan Date: Wed, 25 Mar 2026 23:33:37 +0300 Subject: [PATCH] fix: preserve assistant responses in chat history Streaming text from Claude's responses was not being saved to the session history. When the popover closed and reopened, only user messages appeared - assistant responses were lost. The root cause: Claude CLI returns streaming text via "assistant" type messages, but the "result" type message has an empty "result" field. The history was only populated from the "result" field, so responses were never saved. Fix: accumulate streaming text during the assistant response and save it to history when the turn completes. Co-Authored-By: Claude Opus 4.6 (1M context) --- LilAgents/ClaudeSession.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/LilAgents/ClaudeSession.swift b/LilAgents/ClaudeSession.swift index dc641d2..fc292d2 100644 --- a/LilAgents/ClaudeSession.swift +++ b/LilAgents/ClaudeSession.swift @@ -8,6 +8,7 @@ class ClaudeSession { private var lineBuffer = "" private(set) var isRunning = false private(set) var isBusy = false // true between send() and result + private var currentStreamingResponse = "" private static var claudePath: String? private static var shellEnvironment: [String: String]? @@ -262,6 +263,7 @@ class ClaudeSession { for block in content { let blockType = block["type"] as? String ?? "" if blockType == "text", let text = block["text"] as? String { + currentStreamingResponse += text onText?(text) } else if blockType == "tool_use" { let toolName = block["name"] as? String ?? "Tool" @@ -304,9 +306,13 @@ class ClaudeSession { case "result": isBusy = false - if let result = json["result"] as? String, !result.isEmpty { + let responseText = currentStreamingResponse.trimmingCharacters(in: .whitespacesAndNewlines) + if !responseText.isEmpty { + history.append(Message(role: .assistant, text: responseText)) + } else if let result = json["result"] as? String, !result.isEmpty { history.append(Message(role: .assistant, text: result)) } + currentStreamingResponse = "" onTurnComplete?() default: