Open History to see the last commit
1import { MessageId, ProviderDriverKind, ThreadId } from "@t3tools/contracts";
2import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3import * as DateTime from "effect/DateTime";
4import { assert, describe, expect, it, vi } from "vite-plus/test";
5import { makeAssistantStreamingFilter, splitBufferedAssistantText } from "./assistantStreaming.ts";
6
7describe("V2 assistant streaming", () => {
8 it("keeps code fences intact", () => {
9 expect(splitBufferedAssistantText("Intro\n\n```ts\nx()\n\n")).toEqual({
10 ready: "Intro\n\n",
11 rest: "```ts\nx()\n\n",
12 });
13 expect(splitBufferedAssistantText("```ts\nx()\n```\nrest")).toEqual({
14 ready: "```ts\nx()\n```\n",
15 rest: "rest",
16 });
17 expect(splitBufferedAssistantText("```a``` is inline.\n\n```\ncode\n\nmore\n")).toEqual({
18 ready: "```a``` is inline.\n\n",
19 rest: "```\ncode\n\nmore\n",
20 });
21 expect(splitBufferedAssistantText("Intro\n\n- ```ts\n x()\n\n y()")).toEqual({
22 ready: "Intro\n\n",
23 rest: "- ```ts\n x()\n\n y()",
24 });
25 expect(splitBufferedAssistantText("Intro\n\n- ```ts\n x()\n\n ```\nrest")).toEqual({
26 ready: "Intro\n\n- ```ts\n x()\n\n ```\n",
27 rest: "rest",
28 });
29 expect(splitBufferedAssistantText("Intro\n\n1. ~~~ts\n x()\n\n y()")).toEqual({
30 ready: "Intro\n\n",
31 rest: "1. ~~~ts\n x()\n\n y()",
32 });
33 expect(splitBufferedAssistantText("Intro\n\n-\t```ts\n x()\n\n\t```\nrest")).toEqual({
34 ready: "Intro\n\n-\t```ts\n x()\n\n\t```\n",
35 rest: "rest",
36 });
37 expect(splitBufferedAssistantText("- ```\n x\n```\n\nbody\n```\nafter")).toEqual({
38 ready: "- ```\n x\n```\n\nbody\n```\n",
39 rest: "after",
40 });
41 expect(splitBufferedAssistantText("Intro\n\n - ```\n output\n\nAfter\n\n")).toEqual({
42 ready: "Intro\n\n - ```\n output\n\nAfter\n\n",
43 rest: "",
44 });
45 });
46});
47
48it("holds a streamed section heading until its content has a boundary", () => {
49 expect(splitBufferedAssistantText("Intro\n\n## Results\n\n")).toEqual({
50 ready: "Intro\n\n",
51 rest: "## Results\n\n",
52 });
53 expect(splitBufferedAssistantText("**Results**\n\nBody\n\nNext")).toEqual({
54 ready: "**Results**\n\nBody\n\n",
55 rest: "Next",
56 });
57});
58
59type Step =
60 // A snapshot at `at` ms, and the text it delivers at once (null when held back).
61 | { readonly at: number; readonly text: string; readonly shows: string | null }
62 | { readonly at: number; readonly final: string }
63 | { readonly id: string; readonly at: number; readonly text: string; readonly shows: string }
64 // Held snapshots due by `idle` ms, delivered whole.
65 | { readonly idle: number; readonly shows: ReadonlyArray<string> }
66 | { readonly dueAt: number | null };
67
68const createdAt = DateTime.makeUnsafe(0);
69const snapshot = (id: string, text: string, streaming: boolean) =>
70 ({
71 type: "message.updated",
72 driver: ProviderDriverKind.make("codex"),
73 message: {
74 createdBy: "agent",
75 creationSource: "provider",
76 id: MessageId.make(id),
77 threadId: ThreadId.make("thread"),
78 runId: null,
79 nodeId: null,
80 role: "assistant",
81 text,
82 attachments: [],
83 streaming,
84 createdAt,
85 updatedAt: createdAt,
86 },
87 }) satisfies ProviderAdapter.ProviderAdapterV2Event;
88const shown = (event: ProviderAdapter.ProviderAdapterV2Event | null) =>
89 event?.type === "message.updated" ? event.message.text : null;
90
91it.each<{ readonly name: string; readonly mode?: "turn"; readonly steps: ReadonlyArray<Step> }>([
92 {
93 name: "an unfinished code fence",
94 steps: [
95 { at: 0, text: "A\n\n```ts\nx()", shows: "A\n\n" },
96 { dueAt: 800 },
97 { idle: 799, shows: [] },
98 { idle: 800, shows: ["A\n\n```ts\nx()"] },
99 { dueAt: null },
100 { at: 1_300, text: "A\n\n```ts\nx()\n```\n\nB", shows: "A\n\n```ts\nx()\n```\n\n" },
101 ],
102 },
103 {
104 name: "a list's partial last item",
105 steps: [
106 { at: 0, text: "Steps:\n\n- one\n- tw", shows: "Steps:\n\n- one\n" },
107 { idle: 800, shows: ["Steps:\n\n- one\n- tw"] },
108 { at: 1_300, text: "Steps:\n\n- one\n- two\n- th", shows: "Steps:\n\n- one\n- two\n" },
109 ],
110 },
111 {
112 name: "an incomplete table row",
113 steps: [
114 { at: 0, text: "Intro.\n\n| a | b |\n|---|---|\n| 1 |", shows: "Intro.\n\n" },
115 { idle: 800, shows: ["Intro.\n\n| a | b |\n|---|---|\n| 1 |"] },
116 {
117 at: 1_300,
118 text: "Intro.\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\nAfter",
119 shows: "Intro.\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\n",
120 },
121 ],
122 },
123 {
124 name: "more of the same paragraph after an idle delivery",
125 steps: [
126 { at: 0, text: "A\n\nB", shows: "A\n\n" },
127 { idle: 800, shows: ["A\n\nB"] },
128 { at: 900, text: "A\n\nB more", shows: null },
129 { idle: 1_699, shows: [] },
130 { idle: 1_700, shows: ["A\n\nB more"] },
131 { idle: 5_000, shows: [] },
132 // Its only boundary is behind what the idle delivery showed.
133 { at: 5_100, text: "A\n\nB more and", shows: null },
134 { at: 5_200, text: "A\n\nB more and.\n\nC", shows: "A\n\nB more and.\n\n" },
135 { at: 5_300, final: "A\n\nB more and.\n\nC." },
136 { idle: 10_000, shows: [] },
137 ],
138 },
139 {
140 name: "completion at the idle deadline",
141 steps: [
142 { at: 0, text: "A\n\nB", shows: "A\n\n" },
143 { at: 800, final: "A\n\nB." },
144 { idle: 800, shows: [] },
145 ],
146 },
147 {
148 name: "completion right after an idle delivery at the same instant",
149 steps: [
150 { at: 0, text: "A\n\nB", shows: "A\n\n" },
151 { idle: 800, shows: ["A\n\nB"] },
152 { at: 800, final: "A\n\nB." },
153 { idle: 5_000, shows: [] },
154 ],
155 },
156 {
157 name: "several fast paragraphs inside the throttle",
158 steps: [
159 { at: 0, text: "A\n\nB", shows: "A\n\n" },
160 { at: 100, text: "A\n\nB\n\nC", shows: null },
161 { at: 200, text: "A\n\nB\n\nC\n\nD", shows: null },
162 { at: 300, text: "A\n\nB\n\nC\n\nD\n\nE", shows: null },
163 { idle: 1_099, shows: [] },
164 { idle: 1_100, shows: ["A\n\nB\n\nC\n\nD\n\nE"] },
165 ],
166 },
167 {
168 name: "two messages due at different times",
169 steps: [
170 { id: "a", at: 0, text: "A\n\nx", shows: "A\n\n" },
171 { id: "b", at: 300, text: "B\n\ny", shows: "B\n\n" },
172 { dueAt: 800 },
173 { idle: 800, shows: ["A\n\nx"] },
174 { dueAt: 1_100 },
175 { idle: 1_100, shows: ["B\n\ny"] },
176 ],
177 },
178 {
179 name: "empty text",
180 steps: [{ at: 0, text: "", shows: null }, { dueAt: null }],
181 },
182 {
183 name: "whitespace-only text",
184 steps: [
185 { at: 0, text: " \n", shows: null },
186 { idle: 800, shows: [" \n"] },
187 { idle: 5_000, shows: [] },
188 ],
189 },
190 {
191 // Rewrites replace delivered text: shorter at once, same length after the throttle.
192 name: "provider rewrites and truncation",
193 steps: [
194 { at: 0, text: "Original long paragraph.\n\n", shows: "Original long paragraph.\n\n" },
195 { at: 1_000, text: "New.\n\n", shows: "New.\n\n" },
196 { at: 1_100, text: "Old.\n\n", shows: null },
197 { idle: 1_900, shows: ["Old.\n\n"] },
198 { at: 2_500, text: "Old", shows: null },
199 { idle: 3_300, shows: ["Old"] },
200 { at: 4_000, text: "", shows: null },
201 { idle: 4_800, shows: [""] },
202 ],
203 },
204 {
205 name: "a rewrite that drops an open code fence",
206 steps: [
207 { at: 0, text: "Intro\n\n```ts\nx()\n", shows: "Intro\n\n" },
208 { at: 400, text: "Retry\n\n- a\n- b", shows: "Retry\n\n- a\n" },
209 ],
210 },
211 {
212 name: "paragraphs separated by carriage-return line endings",
213 steps: [
214 { at: 0, text: "Intro\r\rBody", shows: "Intro\r\r" },
215 { dueAt: 800 },
216 { idle: 800, shows: ["Intro\r\rBody"] },
217 { at: 1_200, text: "Intro\r\rBody\r\rNext", shows: "Intro\r\rBody\r\r" },
218 ],
219 },
220 {
221 name: "CRLF split across snapshots",
222 steps: [
223 { at: 0, text: "Intro\r", shows: null },
224 { at: 400, text: "Intro\r\nBody", shows: null },
225 { at: 800, text: "Intro\r\nBody\r\n\r", shows: "Intro\r\nBody\r\n\r" },
226 { at: 1_200, text: "Intro\r\nBody\r\n\r\nNext", shows: "Intro\r\nBody\r\n\r\n" },
227 { idle: 2_000, shows: ["Intro\r\nBody\r\n\r\nNext"] },
228 ],
229 },
230 {
231 name: "turn mode",
232 mode: "turn",
233 steps: [{ at: 0, text: "A\n\nB", shows: null }, { dueAt: null }],
234 },
235])("paragraph streaming holds and idle-delivers $name", ({ mode, steps }) => {
236 const filter = makeAssistantStreamingFilter(mode ?? "paragraph", 800);
237 for (const step of steps) {
238 if ("dueAt" in step) assert.equal(filter.nextIdleFlushAt(), step.dueAt);
239 else if ("idle" in step)
240 assert.deepEqual(filter.takeIdleFlushes(step.idle).map(shown), [...step.shows]);
241 else if ("final" in step)
242 assert.equal(shown(filter.filter(snapshot("m", step.final, false), step.at)), step.final);
243 else
244 assert.equal(
245 shown(filter.filter(snapshot("id" in step ? step.id : "m", step.text, true), step.at)),
246 step.shows,
247 );
248 }
249});
250
251it("scans each streamed line once while a long code fence is still open", () => {
252 const filter = makeAssistantStreamingFilter("paragraph", 800);
253 const lineCount = 400;
254 let text = "Intro\n\n```ts\n";
255 const indexOf = vi.spyOn(String.prototype, "indexOf");
256 try {
257 for (let line = 0; line < lineCount; line++) {
258 text += `const value${line} = ${line};\n`;
259 filter.filter(snapshot("m", text, true), line * 400);
260 }
261 const newlineSearches = indexOf.mock.calls.filter(([search]) => search === "\n").length;
262 expect(newlineSearches).toBeLessThan(lineCount * 4);
263 } finally {
264 indexOf.mockRestore();
265 }
266 text += "```\n";
267 assert.equal(shown(filter.filter(snapshot("m", text, true), lineCount * 400)), text);
268});