forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-message-content.test.ts
More file actions
199 lines (185 loc) · 5.65 KB
/
Copy pathchat-message-content.test.ts
File metadata and controls
199 lines (185 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { describe, expect, it } from "vitest";
import {
extractAssistantTextForPhase,
extractAssistantVisibleText,
extractFirstTextBlock,
resolveAssistantMessagePhase,
} from "./chat-message-content.js";
describe("shared/chat-message-content", () => {
it("extracts the first text block from array content", () => {
expect(
extractFirstTextBlock({
content: [{ text: "hello" }, { text: "world" }],
}),
).toBe("hello");
});
it("returns plain string content", () => {
expect(
extractFirstTextBlock({
content: "hello from string content",
}),
).toBe("hello from string content");
});
it("preserves empty-string text in the first block", () => {
expect(
extractFirstTextBlock({
content: [{ text: "" }, { text: "later" }],
}),
).toBe("");
});
it("only considers the first content block even if later blocks have text", () => {
expect(
extractFirstTextBlock({
content: [null, { text: "later" }],
}),
).toBeUndefined();
expect(
extractFirstTextBlock({
content: [{ type: "image" }, { text: "later" }],
}),
).toBeUndefined();
});
it("returns undefined for missing, empty, or non-text content", () => {
expect(extractFirstTextBlock(null)).toBeUndefined();
expect(extractFirstTextBlock({ content: [] })).toBeUndefined();
expect(extractFirstTextBlock({ content: [{ type: "image" }] })).toBeUndefined();
expect(extractFirstTextBlock({ content: ["hello"] })).toBeUndefined();
expect(extractFirstTextBlock({ content: [{ text: 1 }, { text: "later" }] })).toBeUndefined();
});
});
describe("extractAssistantVisibleText", () => {
it("preserves boundary spacing when joining adjacent final_answer text blocks", () => {
expect(
extractAssistantTextForPhase(
{
role: "assistant",
content: [
{
type: "text",
text: "Hi ",
textSignature: JSON.stringify({ v: 1, id: "msg_final_1", phase: "final_answer" }),
},
{
type: "text",
text: "there",
textSignature: JSON.stringify({ v: 1, id: "msg_final_2", phase: "final_answer" }),
},
],
},
{ phase: "final_answer", joinWith: "" },
),
).toBe("Hi there");
});
it("prefers final_answer text over commentary text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [
{
type: "text",
text: "thinking like caveman",
textSignature: JSON.stringify({ v: 1, id: "msg_commentary", phase: "commentary" }),
},
{
type: "text",
text: "Actual final answer",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBe("Actual final answer");
});
it("does not fall back to commentary-only text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [
{
type: "text",
text: "thinking like caveman",
textSignature: JSON.stringify({ v: 1, id: "msg_commentary", phase: "commentary" }),
},
],
}),
).toBeUndefined();
});
it("does not fall back to unphased legacy text when final_answer is empty", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [
{ type: "text", text: "Legacy answer" },
{
type: "text",
text: " ",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBeUndefined();
});
it("falls back to unphased legacy text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [{ type: "text", text: "Legacy answer" }],
}),
).toBe("Legacy answer");
});
it("does not mix unphased legacy text into final_answer output", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
phase: "final_answer",
content: [
{ type: "text", text: "Legacy answer" },
{
type: "text",
text: "Actual final answer",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBe("Actual final answer");
});
});
describe("resolveAssistantMessagePhase", () => {
it("prefers the top-level assistant phase when present", () => {
expect(resolveAssistantMessagePhase({ role: "assistant", phase: "commentary" })).toBe(
"commentary",
);
});
it("resolves a single explicit phase from textSignature metadata", () => {
expect(
resolveAssistantMessagePhase({
role: "assistant",
content: [
{
type: "text",
text: "Actual final answer",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBe("final_answer");
});
it("returns undefined when text blocks contain mixed explicit phases", () => {
expect(
resolveAssistantMessagePhase({
role: "assistant",
content: [
{
type: "text",
text: "Working...",
textSignature: JSON.stringify({ v: 1, id: "msg_commentary", phase: "commentary" }),
},
{
type: "text",
text: "Done.",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBeUndefined();
});
});