Repository navigation
Expand file tree
/
Copy pathtest-get-claude-user-message.ts
More file actions
341 lines (318 loc) · 9.64 KB
/
Copy pathtest-get-claude-user-message.ts
File metadata and controls
341 lines (318 loc) · 9.64 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Unit tests for getClaudeUserMessage in src/message-builder.ts.
*
* Covers the v0.4.8 fix: tool-role messages (AI SDK V3 shape) must produce
* tool_result content blocks instead of falling through to the "(empty)"
* sentinel — otherwise opencode's outer agent loop hangs after every proxy
* tool call, forcing the user to press "continue".
*/
import { test } from "node:test"
import assert from "node:assert/strict"
import { getClaudeUserMessage } from "./src/message-builder.js"
const p = (msgs: any[]) => msgs as any
function parsed(prompt: any) {
return JSON.parse(getClaudeUserMessage(prompt))
}
test("tool-role tool-result produces tool_result block, not sentinel", () => {
const out = parsed(
p([
{ role: "user", content: "run bash" },
{ role: "assistant", content: [{ type: "text", text: "ok" }] },
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_1",
output: { type: "text", value: "hello from bash" },
},
],
},
]),
)
const blocks = out.message.content
assert.equal(Array.isArray(blocks), true)
assert.equal(blocks.length, 1)
assert.equal(blocks[0].type, "tool_result")
assert.equal(blocks[0].tool_use_id, "call_1")
// Must NOT be the "(empty)" sentinel.
assert.notEqual(blocks[0].type, "text")
})
test("multiple tool-results in single tool-role message all flow through", () => {
const out = parsed(
p([
{ role: "user", content: "do both" },
{ role: "assistant", content: [{ type: "text", text: "running" }] },
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_a",
output: { type: "text", value: "a result" },
},
{
type: "tool-result",
toolCallId: "call_b",
output: { type: "text", value: "b result" },
},
],
},
]),
)
const blocks = out.message.content
assert.equal(blocks.length, 2)
assert.deepEqual(
blocks.map((b: any) => [b.type, b.tool_use_id]),
[
["tool_result", "call_a"],
["tool_result", "call_b"],
],
)
})
test("tool-role without tool-result parts still falls through to sentinel", () => {
const out = parsed(
p([
{ role: "user", content: "x" },
{ role: "assistant", content: [{ type: "text", text: "ok" }] },
{
role: "tool",
content: [{ type: "something-else" }],
},
]),
)
// No tool-result extracted → falls through to "(empty)" sentinel path
// (correct behavior, matches hasNewUserContent's symmetry).
const blocks = out.message.content
assert.equal(blocks.length, 1)
assert.equal(blocks[0].type, "text")
assert.equal(blocks[0].text, "(empty)")
})
test("mixed user-text + tool-role both flow into the same content array", () => {
const out = parsed(
p([
{ role: "user", content: "first turn" },
{ role: "assistant", content: [{ type: "text", text: "running tool" }] },
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_1",
output: { type: "text", value: "tool output" },
},
],
},
{
role: "user",
content: [{ type: "text", text: "follow-up question" }],
},
]),
)
const blocks = out.message.content
// Should have both the tool_result and the follow-up text, no sentinel.
const types = blocks.map((b: any) => b.type)
assert.ok(types.includes("tool_result"), `expected tool_result in ${types}`)
assert.ok(types.includes("text"), `expected text in ${types}`)
// No "(empty)" sentinel injected.
const textBlock = blocks.find((b: any) => b.type === "text")
assert.notEqual(textBlock.text, "(empty)")
})
// ---------------------------------------------------------------------------
// Compaction mode tests
// ---------------------------------------------------------------------------
function parsedCompaction(prompt: any) {
return JSON.parse(
getClaudeUserMessage(prompt as any, false, undefined, {
compactionMode: true,
}),
)
}
test("compaction wraps transcript in <conversation_transcript> tag", () => {
const out = parsedCompaction(
p([
{ role: "user", content: "what's 2+2?" },
{ role: "assistant", content: [{ type: "text", text: "4" }] },
{
role: "user",
content: [{ type: "text", text: "summarize this conversation" }],
},
]),
)
const blocks = out.message.content
const textBlock = blocks.find((b: any) => b.type === "text")
assert.ok(textBlock, "expected a text block")
assert.ok(
textBlock.text.includes("<conversation_transcript>"),
"expected transcript wrapper",
)
assert.ok(
textBlock.text.includes("</conversation_transcript>"),
"expected closing transcript tag",
)
assert.ok(
!textBlock.text.includes("from a previous session that couldn't be resumed"),
"should not use the fresh-session wrapper text",
)
})
test("compaction transcript includes tool_use input, not just count", () => {
const out = parsedCompaction(
p([
{ role: "user", content: "list files" },
{
role: "assistant",
content: [
{ type: "text", text: "running ls" },
{
type: "tool-call",
toolCallId: "call_1",
toolName: "Bash",
input: { command: "ls -la /tmp/specific-path" },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_1",
toolName: "Bash",
output: {
type: "text",
value: "file1.txt\nfile2.txt\nspecific-content-here",
},
},
],
},
{ role: "user", content: "summarize" },
]),
)
const transcript = out.message.content.find((b: any) => b.type === "text").text
assert.ok(
transcript.includes("tool_use:Bash"),
"expected rendered tool_use with name",
)
assert.ok(
transcript.includes("ls -la /tmp/specific-path"),
"expected tool input rendered, not placeholder",
)
assert.ok(
transcript.includes("specific-content-here"),
"expected tool_result content rendered, not placeholder",
)
// Legacy placeholder text must NOT appear in compaction mode.
assert.ok(
!transcript.includes("[Called 1 tool(s)"),
"should not use legacy placeholder",
)
assert.ok(
!transcript.includes("[Received 1 tool result(s)]"),
"should not use legacy placeholder",
)
})
test("compaction clips long tool_result with truncation marker", () => {
const longOutput = "x".repeat(15_000)
const out = parsedCompaction(
p([
{ role: "user", content: "do thing" },
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call_1",
toolName: "Read",
input: { file: "big.txt" },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call_1",
toolName: "Read",
output: { type: "text", value: longOutput },
},
],
},
{ role: "user", content: "summarize" },
]),
)
const transcript = out.message.content.find((b: any) => b.type === "text").text
assert.ok(
transcript.includes("[truncated"),
"expected truncation marker for over-cap tool_result",
)
// Bounded: must not contain the full 15k blob.
assert.ok(
transcript.length < 14_000,
`transcript should be capped near 10k chars per tool_result, got ${transcript.length}`,
)
})
test("compaction final user instruction follows the transcript", () => {
const out = parsedCompaction(
p([
{ role: "user", content: "what's up" },
{ role: "assistant", content: [{ type: "text", text: "hi" }] },
{
role: "user",
content: [
{
type: "text",
text: "Your task is to summarize the conversation.",
},
],
},
]),
)
const blocks = out.message.content
// Expect: [transcript-text-block, instruction-text-block]
const texts = blocks.filter((b: any) => b.type === "text").map((b: any) => b.text)
assert.equal(texts.length, 2, `expected 2 text blocks, got ${texts.length}`)
assert.ok(texts[0].includes("<conversation_transcript>"))
assert.ok(texts[1].includes("Your task is to summarize"))
// Synthesis instruction must NOT be embedded inside the transcript block.
assert.ok(!texts[0].includes("Your task is to summarize"))
})
test("compaction suppresses reasoning keyword injection", () => {
const out = JSON.parse(
getClaudeUserMessage(
p([
{ role: "user", content: "anything" },
{ role: "assistant", content: [{ type: "text", text: "ok" }] },
{ role: "user", content: [{ type: "text", text: "summarize" }] },
]) as any,
false,
"max",
{ compactionMode: true },
),
)
const texts = out.message.content
.filter((b: any) => b.type === "text")
.map((b: any) => b.text)
.join("\n")
assert.ok(
!texts.includes("(ultrathink)"),
"reasoning keyword should be suppressed in compaction mode",
)
})
test("non-compaction call still injects reasoning keyword", () => {
const out = JSON.parse(
getClaudeUserMessage(
p([{ role: "user", content: "hello" }]) as any,
false,
"max",
),
)
const texts = out.message.content
.filter((b: any) => b.type === "text")
.map((b: any) => b.text)
.join("\n")
assert.ok(
texts.includes("(ultrathink)"),
"reasoning keyword should still be injected for normal turns",
)
})