Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderRuntimeIngestion.activity.test.ts

144 lines4.9 KB

1import {
2 EventId,
3 ProviderDriverKind,
4 RuntimeTaskId,
5 ThreadId,
6 type ProviderRuntimeEvent,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9​
10import { runtimeEventToActivities } from "./ProviderRuntimeIngestion.ts";
11​
12const base = {
13 provider: ProviderDriverKind.make("codex"),
14 createdAt: "2026-08-06T00:00:00.000Z",
15 threadId: ThreadId.make("thread-1"),
16};
17​
18describe("runtimeEventToActivities task progress", () => {
19 it("persists usage independently from replaceable activity", () => {
20 const taskId = RuntimeTaskId.make("agent-1");
21 const usageOnly = {
22 ...base,
23 type: "task.progress",
24 eventId: EventId.make("evt-usage"),
25 payload: {
26 taskId,
27 description: "Agent one",
28 typedUsage: { totalTokens: 73_700_000 },
29 },
30 } satisfies ProviderRuntimeEvent;
31 const command = {
32 ...base,
33 type: "task.progress",
34 eventId: EventId.make("evt-command"),
35 payload: {
36 taskId,
37 description: "Agent one",
38 summary: "Running tests",
39 lastToolName: "exec_command",
40 },
41 } satisfies ProviderRuntimeEvent;
42​
43 const usageActivities = runtimeEventToActivities(usageOnly);
44 const commandActivities = runtimeEventToActivities(command);
45​
46 expect(usageActivities.map((activity) => activity.id)).toEqual(["task-usage:thread-1:agent-1"]);
47 expect(commandActivities.map((activity) => activity.id)).toEqual([
48 "task-progress:thread-1:agent-1",
49 ]);
50 const usagePayload = usageActivities[0]?.payload as Record<string, unknown> | undefined;
51 expect(usagePayload?.typedUsage).toEqual({ totalTokens: 73_700_000 });
52 expect(usagePayload?.usageSnapshot).toBe(true);
53 });
54​
55 it("splits combined progress and usage into their independent snapshots", () => {
56 const event = {
57 ...base,
58 type: "task.progress",
59 eventId: EventId.make("evt-combined"),
60 payload: {
61 taskId: RuntimeTaskId.make("agent-2"),
62 description: "Agent two",
63 summary: "Inspecting the panel",
64 typedUsage: { totalTokens: 4_200, toolUses: 7 },
65 status: "running",
66 },
67 } satisfies ProviderRuntimeEvent;
68​
69 const activities = runtimeEventToActivities(event);
70 const progressPayload = activities[0]?.payload as Record<string, unknown>;
71 const usagePayload = activities[1]?.payload as Record<string, unknown>;
72​
73 expect(activities.map((activity) => activity.id)).toEqual([
74 "task-progress:thread-1:agent-2",
75 "task-usage:thread-1:agent-2",
76 ]);
77 expect(progressPayload.summary).toBe("Inspecting the panel");
78 expect(progressPayload.status).toBe("running");
79 expect(progressPayload).not.toHaveProperty("typedUsage");
80 expect(usagePayload.typedUsage).toEqual({ totalTokens: 4_200, toolUses: 7 });
81 expect(usagePayload.usageSnapshot).toBe(true);
82 expect(usagePayload).not.toHaveProperty("status");
83 });
84});
85describe("runtimeEventToActivities tool streaming persistence", () => {
86 const accumulatedStdout = [
87 "first line of output",
88 ...Array.from({ length: 500 }, (_, index) => `Capturing frame ${index}/9028`),
89 ].join("\n");
90 const streamingData = {
91 toolCallId: "tool-call-1",
92 kind: "execute",
93 command: "blender --render",
94 rawOutput: { stdout: accumulatedStdout },
95 content: [{ type: "content", content: { type: "text", text: accumulatedStdout } }],
96 };
97​
98 it("persists tool.updated with the wire projection of data, not the accumulated stream", () => {
99 const event = {
100 ...base,
101 type: "item.updated",
102 eventId: EventId.make("evt-tool-streaming-updated"),
103 payload: {
104 itemType: "command_execution",
105 status: "inProgress",
106 title: "Render",
107 detail: accumulatedStdout,
108 data: streamingData,
109 },
110 } satisfies ProviderRuntimeEvent;
111​
112 const activities = runtimeEventToActivities(event);
113​
114 expect(activities).toHaveLength(1);
115 const payload = activities[0]?.payload as Record<string, unknown>;
116 const data = payload.data as Record<string, unknown>;
117 expect(payload.status).toBe("inProgress");
118 expect(data.toolCallId).toBe("tool-call-1");
119 expect(data.command).toBe("blender --render");
120 expect(data.rawOutput).toEqual({ content: "first line of output" });
121 expect(data.content).toBeUndefined();
122 expect(JSON.stringify(data).length).toBeLessThan(1_000);
123 });
124​
125 it("persists the full terminal payload on tool.completed", () => {
126 const event = {
127 ...base,
128 type: "item.completed",
129 eventId: EventId.make("evt-tool-streaming-completed"),
130 payload: {
131 itemType: "command_execution",
132 status: "completed",
133 title: "Render",
134 data: streamingData,
135 },
136 } satisfies ProviderRuntimeEvent;
137​
138 const activities = runtimeEventToActivities(event);
139​
140 expect(activities).toHaveLength(1);
141 const payload = activities[0]?.payload as Record<string, unknown>;
142 expect(payload.data).toEqual(streamingData);
143 });
144});