Skip to content

Commit f258ef6

Browse files
fix(chat): preserve trace across history reloads
1 parent 18c9f27 commit f258ef6

9 files changed

Lines changed: 247 additions & 51 deletions

File tree

‎apps/.i18n/native-source.json‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40707,55 +40707,55 @@
4070740707
},
4070840708
{
4070940709
"kind": "conditional-branch",
40710-
"line": 708,
40710+
"line": 701,
4071140711
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4071240712
"source": "Voice note",
4071340713
"surface": "apple",
4071440714
"id": "native.apple.3c8c3679fd99c074"
4071540715
},
4071640716
{
4071740717
"kind": "ui-localized-call",
40718-
"line": 749,
40718+
"line": 742,
4071940719
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4072040720
"source": "Attachment",
4072140721
"surface": "apple",
4072240722
"id": "native.apple.eca473edeb132a44"
4072340723
},
4072440724
{
4072540725
"kind": "ui-call",
40726-
"line": 789,
40726+
"line": 782,
4072740727
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4072840728
"source": "Writing",
4072940729
"surface": "apple",
4073040730
"id": "native.apple.e9914cfc93a514cb"
4073140731
},
4073240732
{
4073340733
"kind": "ui-call",
40734-
"line": 828,
40734+
"line": 821,
4073540735
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4073640736
"source": "Preparing audio…",
4073740737
"surface": "apple",
4073840738
"id": "native.apple.cbc285107b965641"
4073940739
},
4074040740
{
4074140741
"kind": "ui-call",
40742-
"line": 831,
40742+
"line": 824,
4074340743
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4074440744
"source": "Speaking…",
4074540745
"surface": "apple",
4074640746
"id": "native.apple.d781ade49132fec6"
4074740747
},
4074840748
{
4074940749
"kind": "conditional-branch",
40750-
"line": 839,
40750+
"line": 832,
4075140751
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4075240752
"source": "Preparing audio, tap to cancel",
4075340753
"surface": "apple",
4075440754
"id": "native.apple.9e4b306460f4a32d"
4075540755
},
4075640756
{
4075740757
"kind": "conditional-branch",
40758-
"line": 840,
40758+
"line": 833,
4075940759
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift",
4076040760
"source": "Speaking, tap to stop",
4076140761
"surface": "apple",
@@ -41987,71 +41987,71 @@
4198741987
},
4198841988
{
4198941989
"kind": "ui-call",
41990-
"line": 1172,
41990+
"line": 1165,
4199141991
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4199241992
"source": "Copy Message",
4199341993
"surface": "apple",
4199441994
"id": "native.apple.fe9f30f12f1dcfaf"
4199541995
},
4199641996
{
4199741997
"kind": "ui-call",
41998-
"line": 1195,
41998+
"line": 1188,
4199941999
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4200042000
"source": "Open Full Message",
4200142001
"surface": "apple",
4200242002
"id": "native.apple.90bc8a8045e7017e"
4200342003
},
4200442004
{
4200542005
"kind": "ui-call",
42006-
"line": 1214,
42006+
"line": 1207,
4200742007
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4200842008
"source": "Rewind to Here",
4200942009
"surface": "apple",
4201042010
"id": "native.apple.002bfcede229ad4b"
4201142011
},
4201242012
{
4201342013
"kind": "ui-call",
42014-
"line": 1234,
42014+
"line": 1227,
4201542015
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4201642016
"source": "Fork from Here",
4201742017
"surface": "apple",
4201842018
"id": "native.apple.14df2e4997689fb3"
4201942019
},
4202042020
{
4202142021
"kind": "ui-localized-call",
42022-
"line": 1260,
42022+
"line": 1253,
4202342023
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4202442024
"source": "Reply",
4202542025
"surface": "apple",
4202642026
"id": "native.apple.b37ec7bd7dc1579a"
4202742027
},
4202842028
{
4202942029
"kind": "ui-localized-call",
42030-
"line": 1270,
42030+
"line": 1263,
4203142031
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4203242032
"source": "You",
4203342033
"surface": "apple",
4203442034
"id": "native.apple.8d80a6bb3d6e99b0"
4203542035
},
4203642036
{
4203742037
"kind": "ui-localized-call",
42038-
"line": 1272,
42038+
"line": 1265,
4203942039
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4204042040
"source": "Assistant",
4204142041
"surface": "apple",
4204242042
"id": "native.apple.3b44a67aa284886f"
4204342043
},
4204442044
{
4204542045
"kind": "ui-call",
42046-
"line": 1338,
42046+
"line": 1331,
4204742047
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4204842048
"source": "Loading chat",
4204942049
"surface": "apple",
4205042050
"id": "native.apple.28c2691c03030434"
4205142051
},
4205242052
{
4205342053
"kind": "ui-modifier",
42054-
"line": 1418,
42054+
"line": 1411,
4205542055
"path": "apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift",
4205642056
"source": "Dismiss",
4205742057
"surface": "apple",

‎apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageViews.swift‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,9 @@ private struct ChatMessageBody: View {
515515
}
516516

517517
private var primaryText: String {
518-
let parts = self.message.content.compactMap { content -> String? in
519-
let kind = (content.type ?? "text").lowercased()
520-
guard kind == "text" || kind.isEmpty else { return nil }
521-
return content.text
522-
}
523-
return OpenClawChatMessage.displayText(
524-
contentText: parts.joined(separator: "\n"),
525-
role: self.message.role,
526-
stopReason: self.message.stopReason,
527-
errorMessage: self.message.errorMessage)
518+
ChatMessageVisibleText.displayText(
519+
in: self.message,
520+
includeThinking: self.displayOptions.contains(.reasoning))
528521
}
529522

530523
private var inlineAttachments: [OpenClawChatMessageContent] {

‎apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatMessageVisibleText.swift‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,33 @@ public enum ChatMessageVisibleText {
2525
.isEmpty
2626
}
2727

28-
private static func primaryText(in message: OpenClawChatMessage) -> String {
28+
static func displayText(in message: OpenClawChatMessage, includeThinking: Bool) -> String {
29+
let isAssistant = message.role.trimmingCharacters(in: .whitespacesAndNewlines)
30+
.lowercased() == "assistant"
2931
let parts = message.content.compactMap { content -> String? in
3032
let kind = (content.type ?? "text").lowercased()
31-
guard kind == "text" || kind.isEmpty else { return nil }
32-
return content.text
33+
if kind == "text" || kind.isEmpty {
34+
return content.text
35+
}
36+
guard
37+
includeThinking,
38+
isAssistant,
39+
kind == "thinking",
40+
let thinking = content.thinking,
41+
!thinking.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
42+
else {
43+
return nil
44+
}
45+
return "<think>\n\(thinking)\n</think>"
3346
}
3447
return OpenClawChatMessage.displayText(
3548
contentText: parts.joined(separator: "\n"),
3649
role: message.role,
3750
stopReason: message.stopReason,
3851
errorMessage: message.errorMessage)
3952
}
53+
54+
private static func primaryText(in message: OpenClawChatMessage) -> String {
55+
self.displayText(in: message, includeThinking: false)
56+
}
4057
}

‎apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatView.swift‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,16 +1111,9 @@ extension OpenClawChatView {
11111111
}
11121112

11131113
private func primaryText(in message: OpenClawChatMessage) -> String {
1114-
let parts = message.content.compactMap { content -> String? in
1115-
let kind = (content.type ?? "text").lowercased()
1116-
guard kind == "text" || kind.isEmpty else { return nil }
1117-
return content.text
1118-
}
1119-
return OpenClawChatMessage.displayText(
1120-
contentText: parts.joined(separator: "\n"),
1121-
role: message.role,
1122-
stopReason: message.stopReason,
1123-
errorMessage: message.errorMessage)
1114+
ChatMessageVisibleText.displayText(
1115+
in: message,
1116+
includeThinking: self.displayOptions.contains(.reasoning))
11241117
}
11251118

11261119
private func hasInlineAttachments(in message: OpenClawChatMessage) -> Bool {

‎apps/shared/OpenClawKit/Tests/OpenClawKitTests/ChatMessageVisibleTextTests.swift‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ private func toolCallContent(name: String) -> OpenClawChatMessageContent {
1717
name: name)
1818
}
1919

20+
private func thinkingContent(_ thinking: String) -> OpenClawChatMessageContent {
21+
OpenClawChatMessageContent(
22+
type: "thinking",
23+
text: nil,
24+
thinking: thinking,
25+
mimeType: nil,
26+
fileName: nil,
27+
content: nil)
28+
}
29+
2030
@Suite("ChatMessageVisibleText")
2131
struct ChatMessageVisibleTextTests {
2232
@Test func `assistant visible text skips non text blocks`() {
@@ -56,6 +66,23 @@ struct ChatMessageVisibleTextTests {
5666
#expect(ChatMessageVisibleText.copyText(in: user) == "Keep <think>this literal tag</think>")
5767
}
5868

69+
@Test func `assistant display includes structured thinking only when enabled`() {
70+
let message = OpenClawChatMessage(
71+
role: "assistant",
72+
content: [
73+
thinkingContent("Check the persisted state."),
74+
textContent("Here is the answer."),
75+
toolCallContent(name: "read"),
76+
],
77+
timestamp: 1)
78+
79+
#expect(ChatMessageVisibleText.displayText(in: message, includeThinking: false)
80+
== "Here is the answer.")
81+
#expect(ChatMessageVisibleText.displayText(in: message, includeThinking: true)
82+
== "<think>\nCheck the persisted state.\n</think>\nHere is the answer.")
83+
#expect(ChatMessageVisibleText.copyText(in: message) == "Here is the answer.")
84+
}
85+
5986
@Test func `history decode retains transcript identity and truncation signals`() throws {
6087
let metadata = try JSONDecoder().decode(
6188
OpenClawChatMessage.self,

‎src/gateway/chat-display-projection.sanitize.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function sanitizeAssistantPhasedContentBlocks(content: unknown[]): {
157157
};
158158
}
159159

160-
function projectAssistantTextFromMixedToolContent(
160+
function projectAssistantMixedToolContent(
161161
content: unknown[],
162162
maxChars: number,
163163
): { content: unknown[]; changed: boolean } | null {
@@ -171,23 +171,31 @@ function projectAssistantTextFromMixedToolContent(
171171
return null;
172172
}
173173

174-
const textBlocks: unknown[] = [];
174+
let hasVisibleText = false;
175+
const projectedContent: unknown[] = [];
175176
for (const block of content) {
176177
if (!block || typeof block !== "object") {
177178
continue;
178179
}
179180
const entry = block as { type?: unknown; text?: unknown };
180-
if (entry.type !== "text" || typeof entry.text !== "string" || !entry.text.trim()) {
181+
if (!isAssistantTextContentType(entry.type)) {
182+
projectedContent.push(block);
183+
continue;
184+
}
185+
if (typeof entry.text !== "string" || !entry.text.trim()) {
181186
continue;
182187
}
183188
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
184189
const truncated = truncateChatHistoryText(stripped.text, maxChars);
185190
if (truncated.text.trim()) {
186-
textBlocks.push({ type: "text", text: truncated.text });
191+
projectedContent.push({ type: "text", text: truncated.text });
192+
hasVisibleText = true;
187193
}
188194
}
189195

190-
return textBlocks.length > 0 ? { content: textBlocks, changed: true } : null;
196+
// Mixed messages supply both the visible bubble and its reasoning/tool trace.
197+
// Keep structured siblings or a history reload loses activity shown while live.
198+
return hasVisibleText ? { content: projectedContent, changed: true } : null;
191199
}
192200

193201
function toFiniteNumber(x: unknown): number | undefined {
@@ -394,9 +402,9 @@ export function sanitizeChatHistoryMessage(
394402
changed = true;
395403
}
396404
if (entry.role === "assistant" && Array.isArray(entry.content)) {
397-
const mixedToolText = projectAssistantTextFromMixedToolContent(entry.content, maxChars);
398-
if (mixedToolText) {
399-
entry.content = mixedToolText.content;
405+
const mixedToolContent = projectAssistantMixedToolContent(entry.content, maxChars);
406+
if (mixedToolContent) {
407+
entry.content = mixedToolContent.content;
400408
if (entry.phase === "commentary") {
401409
delete entry.phase;
402410
}

‎src/gateway/server-methods/server-methods.test.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,15 @@ describe("projectRecentChatDisplayMessages", () => {
11231123
errorMessage: privateError,
11241124
errorBody: "private response body",
11251125
},
1126-
content: [{ type: "text", text: "I read the requested file before the run failed." }],
1126+
content: [
1127+
{ type: "text", text: "I read the requested file before the run failed." },
1128+
{
1129+
type: "toolCall",
1130+
id: "call-1",
1131+
name: "read",
1132+
arguments: { path: "README.md" },
1133+
},
1134+
],
11271135
},
11281136
];
11291137

@@ -1927,7 +1935,7 @@ describe("projectRecentChatDisplayMessages", () => {
19271935
]);
19281936
});
19291937

1930-
it("keeps visible assistant progress text from mixed tool-use messages", () => {
1938+
it("preserves structured trace alongside visible assistant progress text", () => {
19311939
const result = projectRecentChatDisplayMessages([
19321940
{
19331941
role: "user",
@@ -1968,7 +1976,16 @@ describe("projectRecentChatDisplayMessages", () => {
19681976

19691977
expect(result[1]).toEqual({
19701978
role: "assistant",
1971-
content: [{ type: "text", text: "I will clean that up now." }],
1979+
content: [
1980+
{ type: "thinking", thinking: "private reasoning" },
1981+
{ type: "text", text: "I will clean that up now." },
1982+
{
1983+
type: "toolCall",
1984+
id: "call-read",
1985+
name: "read",
1986+
arguments: { path: "AGENTS.md" },
1987+
},
1988+
],
19721989
timestamp: 2,
19731990
__openclaw: { seq: 2 },
19741991
});

‎src/gateway/server.chat.gateway-server-chat-b.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5192,7 +5192,7 @@ describe("gateway server chat", () => {
51925192
});
51935193
});
51945194

5195-
test("chat.history keeps visible assistant progress text from mixed tool-use transcript messages", async () => {
5195+
test("chat.history preserves assistant trace from mixed tool-use transcript messages", async () => {
51965196
await withGatewayChatHarness(async ({ ws, createSessionDir }) => {
51975197
await prepareMainHistoryHarness({ ws, createSessionDir });
51985198
await writeMainSessionTranscript([
@@ -5240,7 +5240,14 @@ describe("gateway server chat", () => {
52405240
};
52415241
expect(assistantMessage.role).toBe("assistant");
52425242
expect(assistantMessage.content).toEqual([
5243+
{ type: "thinking", thinking: "private reasoning" },
52435244
{ type: "text", text: "I will clean that up now." },
5245+
{
5246+
type: "toolCall",
5247+
id: "call-read",
5248+
name: "read",
5249+
arguments: { path: "AGENTS.md" },
5250+
},
52445251
]);
52455252
expect(assistantMessage.timestamp).toBe(2);
52465253
});

0 commit comments

Comments
 (0)