Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { MessageId, ProviderDriverKind, ThreadId } from "@t3tools/contracts";
3import * as DateTime from "effect/DateTime";
4import { makeAssistantStreamingFilter, splitBufferedAssistantText } from "./assistantStreaming.ts";
5import type { ProviderAdapterV2Event } from "./ProviderAdapter.ts";
6
7const message = (text: string, streaming = true): ProviderAdapterV2Event => ({
8 type: "message.updated",
9 driver: ProviderDriverKind.make("codex"),
10 message: {
11 id: MessageId.make("message"),
12 threadId: ThreadId.make("thread"),
13 runId: null,
14 nodeId: null,
15 createdBy: "agent",
16 creationSource: "provider",
17 updatedAt: DateTime.makeUnsafe("2026-09-14T00:00:00Z"),
18 role: "assistant",
19 text,
20 attachments: [],
21 createdAt: DateTime.makeUnsafe("2026-09-14T00:00:00Z"),
22 streaming,
23 },
24});
25
26describe("V2 assistant streaming", () => {
27 it("delivers completed paragraphs, coalesces rapid updates, and flushes final text", () => {
28 const filter = makeAssistantStreamingFilter("paragraph");
29 expect(filter(message("First"), 0)).toBeNull();
30 expect(filter(message("First\n\nSec"), 10)).toMatchObject({ message: { text: "First\n\n" } });
31 expect(filter(message("First\n\nSecond\n\nThi"), 100)).toBeNull();
32 expect(filter(message("First\n\nSecond\n\nThird"), 410)).toMatchObject({
33 message: { text: "First\n\nSecond\n\n" },
34 });
35 const final = message("First\n\nSecond\n\nThird", false);
36 expect(filter(final, 420)).toBe(final);
37 });
38 it("keeps code fences intact", () => {
39 expect(splitBufferedAssistantText("Intro\n\n```ts\nx()\n\n")).toEqual({
40 ready: "Intro\n\n",
41 rest: "```ts\nx()\n\n",
42 });
43 expect(splitBufferedAssistantText("```ts\nx()\n```\nrest")).toEqual({
44 ready: "```ts\nx()\n```\n",
45 rest: "rest",
46 });
47 });
48 it("retains token and whole-turn delivery modes", () => {
49 const running = message("partial");
50 const final = message("complete", false);
51 expect(makeAssistantStreamingFilter("token")(running, 0)).toBe(running);
52 const buffered = makeAssistantStreamingFilter("turn");
53 expect(buffered(running, 0)).toBeNull();
54 expect(buffered(final, 1)).toBe(final);
55 });
56});