Open History to see the last commit
1import {
2 NodeId,
3 ProviderDriverKind,
4 RunId,
5 ThreadId,
6 TurnItemId,
7 type OrchestrationV2TurnItem,
8} from "@t3tools/contracts";
9import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
10import * as DateTime from "effect/DateTime";
11import { describe, expect, it } from "vite-plus/test";
12import { makeAssistantStreamingFilter, splitBufferedAssistantText } from "./assistantStreaming.ts";
13
14describe("V2 assistant streaming", () => {
15 it("keeps code fences intact", () => {
16 expect(splitBufferedAssistantText("Intro\n\n```ts\nx()\n\n")).toEqual({
17 ready: "Intro\n\n",
18 rest: "```ts\nx()\n\n",
19 });
20 expect(splitBufferedAssistantText("```ts\nx()\n```\nrest")).toEqual({
21 ready: "```ts\nx()\n```\n",
22 rest: "rest",
23 });
24 });
25});
26
27it("holds a streamed section heading until its content has a boundary", () => {
28 expect(splitBufferedAssistantText("Intro\n\n## Results\n\n")).toEqual({
29 ready: "Intro\n\n",
30 rest: "## Results\n\n",
31 });
32 expect(splitBufferedAssistantText("**Results**\n\nBody\n\nNext")).toEqual({
33 ready: "**Results**\n\nBody\n\n",
34 rest: "Next",
35 });
36});
37
38function reasoningUpdate(text: string) {
39 const now = DateTime.makeUnsafe("2026-10-24T12:00:00.000Z");
40 const item = {
41 id: TurnItemId.make("reasoning"),
42 threadId: ThreadId.make("thread"),
43 runId: RunId.make("run"),
44 nodeId: NodeId.make("node"),
45 providerThreadId: null,
46 providerTurnId: null,
47 nativeItemRef: null,
48 parentItemId: null,
49 ordinal: 0,
50 status: "running",
51 title: null,
52 startedAt: now,
53 completedAt: null,
54 updatedAt: now,
55 type: "reasoning",
56 text,
57 streaming: true,
58 } satisfies OrchestrationV2TurnItem;
59 return {
60 type: "turn_item.updated",
61 driver: ProviderDriverKind.make("opencode"),
62 turnItem: item,
63 } satisfies ProviderAdapter.ProviderAdapterV2Event;
64}
65
66function passedText(passed: ProviderAdapter.ProviderAdapterV2Event | null) {
67 return passed?.type === "turn_item.updated" && passed.turnItem.type === "reasoning"
68 ? passed.turnItem.text
69 : null;
70}
71
72// Streams one 500-character paragraph per event, each past the 400 ms gate,
73// and returns the snapshot lengths the filter passes on to be stored.
74function streamParagraphs(paragraphs: number): { stored: number[]; finalLength: number } {
75 const filter = makeAssistantStreamingFilter("paragraph");
76 const stored: number[] = [];
77 let text = "";
78 for (let index = 0; index < paragraphs; index++) {
79 text += `${"x".repeat(498)}\n\n`;
80 const passed = passedText(filter(reasoningUpdate(text), index * 400));
81 if (passed !== null) stored.push(passed.length);
82 }
83 return { stored, finalLength: text.length };
84}
85
86it("keeps stored snapshots of a long streamed item linear in its length", () => {
87 const { stored, finalLength } = streamParagraphs(200);
88 const storedBytes = stored.reduce((total, length) => total + length, 0);
89 expect(storedBytes).toBeLessThan(finalLength * 8);
90});
91
92it("still passes every paragraph of a short streamed item", () => {
93 expect(streamParagraphs(8).stored).toEqual([500, 1000, 1500, 2000, 2500, 3000, 3500, 4000]);
94});
95
96it("passes a long replacement that does not extend the delivered text", () => {
97 const filter = makeAssistantStreamingFilter("paragraph");
98 filter(reasoningUpdate(`${"x".repeat(9_998)}\n\n`), 0);
99 expect(passedText(filter(reasoningUpdate(`${"y".repeat(8_998)}\n\n`), 400))?.length).toBe(9_000);
100});