|
| 1 | +import { |
| 2 | + convertToModelMessages, |
| 3 | + readUIMessageStream, |
| 4 | + type ModelMessage, |
| 5 | + type UIMessage, |
| 6 | + type UIMessageChunk, |
| 7 | +} from "ai"; |
| 8 | +import { describe, expect, it } from "vitest"; |
| 9 | +import { z } from "zod"; |
| 10 | +import { responseAfterCompaction } from "./compactionResponse.js"; |
| 11 | +import { restoreModelLane } from "./transcriptStorage.js"; |
| 12 | + |
| 13 | +const summary: ModelMessage = { role: "assistant", content: "SUMMARY" }; |
| 14 | +const oldUser: UIMessage = { id: "u1", role: "user", parts: [{ type: "text", text: "OLD_USER" }] }; |
| 15 | +const nextUser: UIMessage = { |
| 16 | + id: "u2", |
| 17 | + role: "user", |
| 18 | + parts: [{ type: "text", text: "NEXT_QUESTION" }], |
| 19 | +}; |
| 20 | +const convert = (messages: UIMessage[]) => |
| 21 | + convertToModelMessages(messages, { ignoreIncompleteToolCalls: true }); |
| 22 | +const text = (messages: unknown) => JSON.stringify(messages); |
| 23 | +const step = (...parts: UIMessage["parts"]): UIMessage["parts"] => [ |
| 24 | + { type: "step-start" }, |
| 25 | + ...parts, |
| 26 | +]; |
| 27 | +const toolResult = ( |
| 28 | + id: string, |
| 29 | + output: string, |
| 30 | + providerExecuted = false |
| 31 | +): UIMessage["parts"][number] => ({ |
| 32 | + type: "dynamic-tool", |
| 33 | + toolName: "lookup", |
| 34 | + toolCallId: id, |
| 35 | + state: "output-available", |
| 36 | + input: { id }, |
| 37 | + output, |
| 38 | + providerExecuted, |
| 39 | +}); |
| 40 | +function response(compactedSteps: number): UIMessage { |
| 41 | + return { |
| 42 | + id: "a1", |
| 43 | + role: "assistant", |
| 44 | + parts: [ |
| 45 | + ...Array.from({ length: compactedSteps }, (_, i) => |
| 46 | + step({ type: "text", text: `OLD_ASSISTANT_${i}` }, toolResult(`old-${i}`, `OLD_TOOL_${i}`)) |
| 47 | + ).flat(), |
| 48 | + ...step(toolResult("new", "NEW_TOOL")), |
| 49 | + ...step({ type: "text", text: "NEW_ANSWER" }), |
| 50 | + ], |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +describe("response model history after inner compaction", () => { |
| 55 | + it.each([1, 2, 3])( |
| 56 | + "keeps only the response after %i compacted steps, including on restoration", |
| 57 | + async (count) => { |
| 58 | + const ui = response(count); |
| 59 | + const before = structuredClone(ui); |
| 60 | + const model = [summary, ...(await convert([responseAfterCompaction(ui, count)]))]; |
| 61 | + expect(text(model)).toContain("SUMMARY"); |
| 62 | + expect(text(model)).toContain("NEW_TOOL"); |
| 63 | + expect(text(model)).toContain("NEW_ANSWER"); |
| 64 | + expect.soft(text(model)).not.toContain("OLD_ASSISTANT"); |
| 65 | + expect.soft(text(model)).not.toContain("OLD_TOOL"); |
| 66 | + expect(ui).toEqual(before); |
| 67 | + expect(text(ui)).toContain("OLD_TOOL"); |
| 68 | + const state = JSON.parse( |
| 69 | + JSON.stringify({ v: 1, compaction: { modelMessages: model, throughId: ui.id } }) |
| 70 | + ); |
| 71 | + const restored = await restoreModelLane([oldUser, ui, nextUser], state, convert); |
| 72 | + expect.soft(text(restored.messages)).not.toContain("OLD_"); |
| 73 | + expect(restored.messages).toEqual([...model, ...(await convert([nextUser]))]); |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + it("keeps the complete response when this turn did not compact", () => { |
| 78 | + const ui = response(1); |
| 79 | + expect(responseAfterCompaction(ui)).toBe(ui); |
| 80 | + }); |
| 81 | + |
| 82 | + it("does not confuse an empty or hidden-reasoning step with a model message", async () => { |
| 83 | + const ui: UIMessage = { |
| 84 | + id: "a1", |
| 85 | + role: "assistant", |
| 86 | + parts: [ |
| 87 | + ...step(), // A provider step whose reasoning was omitted from the UI stream. |
| 88 | + ...step(toolResult("old", "OLD_TOOL", true)), |
| 89 | + ...step({ type: "text", text: "NEW_ANSWER" }), |
| 90 | + ], |
| 91 | + }; |
| 92 | + const model = await convert([responseAfterCompaction(ui, 2)]); |
| 93 | + expect(model).toEqual([{ role: "assistant", content: [{ type: "text", text: "NEW_ANSWER" }] }]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("retains a partially streamed post-compaction answer", async () => { |
| 97 | + const ui: UIMessage = { |
| 98 | + id: "a1", |
| 99 | + role: "assistant", |
| 100 | + parts: [ |
| 101 | + ...step(toolResult("old", "OLD_TOOL")), |
| 102 | + ...step({ type: "text", text: "PARTIAL", state: "streaming" }), |
| 103 | + ], |
| 104 | + }; |
| 105 | + const model = await convert([responseAfterCompaction(ui, 1)]); |
| 106 | + expect(text(model)).toContain("PARTIAL"); |
| 107 | + expect(text(model)).not.toContain("OLD_TOOL"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("appends nothing if stopped before the first post-compaction step", async () => { |
| 111 | + const ui: UIMessage = { |
| 112 | + id: "a1", |
| 113 | + role: "assistant", |
| 114 | + parts: step(toolResult("old", "OLD_TOOL")), |
| 115 | + }; |
| 116 | + expect(await convert([responseAfterCompaction(ui, 1)])).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it.each([true, false])( |
| 120 | + "handles a same-ID continuation whose original has step markers: %s", |
| 121 | + async (hasMarkers) => { |
| 122 | + const original: UIMessage = { |
| 123 | + id: "a1", |
| 124 | + role: "assistant", |
| 125 | + parts: [ |
| 126 | + ...(hasMarkers ? [{ type: "step-start" as const }] : []), |
| 127 | + toolResult("prior", "PRIOR_TURN_TOOL"), |
| 128 | + ], |
| 129 | + }; |
| 130 | + const ui = response(1); |
| 131 | + ui.parts.unshift(...original.parts); |
| 132 | + const originalBefore = structuredClone(original); |
| 133 | + const model = await convert([responseAfterCompaction(ui, 1, original)]); |
| 134 | + expect(text(model)).toContain("NEW_ANSWER"); |
| 135 | + expect(text(model)).toContain("NEW_TOOL"); |
| 136 | + expect(text(model)).not.toContain("OLD_"); |
| 137 | + expect(text(model)).not.toContain("PRIOR_TURN_TOOL"); |
| 138 | + expect(original).toEqual(originalBefore); |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + it("ignores an original response with a different ID", async () => { |
| 143 | + const original = { ...response(3), id: "other" }; |
| 144 | + const model = await convert([responseAfterCompaction(response(1), 1, original)]); |
| 145 | + expect(text(model)).toContain("NEW_ANSWER"); |
| 146 | + expect(text(model)).not.toContain("OLD_"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("keeps post-compaction provider tools and custom tool output conversion", async () => { |
| 150 | + const ui: UIMessage = { |
| 151 | + id: "a1", |
| 152 | + role: "assistant", |
| 153 | + parts: [...step(toolResult("old", "OLD_TOOL")), ...step(toolResult("new", "NEW_TOOL", true))], |
| 154 | + }; |
| 155 | + const model = await convertToModelMessages([responseAfterCompaction(ui, 1)], { |
| 156 | + tools: { |
| 157 | + lookup: { |
| 158 | + inputSchema: z.object({ id: z.string() }), |
| 159 | + toModelOutput: ({ output }: { output: unknown }) => ({ |
| 160 | + type: "text" as const, |
| 161 | + value: `CONVERTED:${output}`, |
| 162 | + }), |
| 163 | + }, |
| 164 | + }, |
| 165 | + }); |
| 166 | + expect(model).toEqual([ |
| 167 | + { |
| 168 | + role: "assistant", |
| 169 | + content: [ |
| 170 | + { |
| 171 | + type: "tool-call", |
| 172 | + toolCallId: "new", |
| 173 | + toolName: "lookup", |
| 174 | + input: { id: "new" }, |
| 175 | + providerExecuted: true, |
| 176 | + }, |
| 177 | + { |
| 178 | + type: "tool-result", |
| 179 | + toolCallId: "new", |
| 180 | + toolName: "lookup", |
| 181 | + output: { type: "text", value: "CONVERTED:NEW_TOOL" }, |
| 182 | + }, |
| 183 | + ], |
| 184 | + }, |
| 185 | + ]); |
| 186 | + }); |
| 187 | + |
| 188 | + it("uses the step boundaries produced by the real UI stream reader", async () => { |
| 189 | + const chunks: UIMessageChunk[] = [ |
| 190 | + { type: "start", messageId: "a1" }, |
| 191 | + { type: "start-step" }, |
| 192 | + { type: "tool-input-available", toolCallId: "old", toolName: "lookup", input: {} }, |
| 193 | + { type: "tool-output-available", toolCallId: "old", output: "OLD_TOOL" }, |
| 194 | + { type: "finish-step" }, |
| 195 | + { type: "start-step" }, |
| 196 | + { type: "text-start", id: "t" }, |
| 197 | + { type: "text-delta", id: "t", delta: "NEW_ANSWER" }, |
| 198 | + { type: "text-end", id: "t" }, |
| 199 | + { type: "finish-step" }, |
| 200 | + { type: "finish" }, |
| 201 | + ]; |
| 202 | + let ui: UIMessage | undefined; |
| 203 | + for await (const message of readUIMessageStream({ |
| 204 | + stream: new ReadableStream({ |
| 205 | + start(controller) { |
| 206 | + for (const chunk of chunks) controller.enqueue(chunk); |
| 207 | + controller.close(); |
| 208 | + }, |
| 209 | + }), |
| 210 | + })) |
| 211 | + ui = message; |
| 212 | + expect(ui).toBeDefined(); |
| 213 | + expect(text(await convert([responseAfterCompaction(ui!, 1)]))).not.toContain("OLD_TOOL"); |
| 214 | + expect(text(ui)).toContain("OLD_TOOL"); |
| 215 | + }); |
| 216 | +}); |
0 commit comments