summaryrefslogtreecommitdiffhomepage
path: root/packages/openai-stream/src/parse-sse.test.ts
blob: 191083302e3a0be3a416afa275af86afad01efe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import type { ProviderEvent } from "@dispatch/kernel";
import { describe, expect, it } from "vitest";
import { parseSSELines } from "./parse-sse.js";

describe("parseSSELines", () => {
	it("parses text delta events", () => {
		const lines = [
			'data: {"id":"chatcmpl-1","choices":[{"delta":{"content":"Hello"},"index":0}]}',
			'data: {"id":"chatcmpl-1","choices":[{"delta":{"content":" world"},"index":0}]}',
			'data: {"id":"chatcmpl-1","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "text-delta", delta: "Hello" },
			{ type: "text-delta", delta: " world" },
			{ type: "finish", reason: "stop" },
		]);
	});

	it("parses a fragmented tool_call across chunks", () => {
		const lines = [
			'data: {"id":"chatcmpl-2","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_abc","function":{"name":"read_file","arguments":""}}]},"index":0}]}',
			'data: {"id":"chatcmpl-2","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"path\\""}}]},"index":0}]}',
			'data: {"id":"chatcmpl-2","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":":\\"main.ts\\"}"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-2","choices":[{"delta":{},"finish_reason":"tool_calls","index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{
				type: "tool-call",
				toolCallId: "call_abc",
				toolName: "read_file",
				input: { path: "main.ts" },
			},
			{ type: "finish", reason: "tool_calls" },
		]);
	});

	it("parses multiple tool_calls in one response", () => {
		const lines = [
			'data: {"id":"chatcmpl-3","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"read_file","arguments":""}}]},"index":0}]}',
			'data: {"id":"chatcmpl-3","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"path\\":\\"a.ts\\"}"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-3","choices":[{"delta":{"tool_calls":[{"index":1,"id":"call_2","function":{"name":"read_file","arguments":""}}]},"index":0}]}',
			'data: {"id":"chatcmpl-3","choices":[{"delta":{"tool_calls":[{"index":1,"function":{"arguments":"{\\"path\\":\\"b.ts\\"}"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-3","choices":[{"delta":{},"finish_reason":"tool_calls","index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "tool-call", toolCallId: "call_1", toolName: "read_file", input: { path: "a.ts" } },
			{ type: "tool-call", toolCallId: "call_2", toolName: "read_file", input: { path: "b.ts" } },
			{ type: "finish", reason: "tool_calls" },
		]);
	});

	it("parses usage from the final chunk", () => {
		const lines = [
			'data: {"id":"chatcmpl-4","choices":[{"delta":{"content":"Hi"},"index":0}]}',
			'data: {"id":"chatcmpl-4","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-4","usage":{"prompt_tokens":10,"completion_tokens":5}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "text-delta", delta: "Hi" },
			{ type: "finish", reason: "stop" },
			{
				type: "usage",
				usage: {
					inputTokens: 10,
					outputTokens: 5,
					cacheReadTokens: undefined,
					cacheWriteTokens: undefined,
				},
			},
		]);
	});

	it("parses reasoning_content deltas", () => {
		const lines = [
			'data: {"id":"chatcmpl-5","choices":[{"delta":{"reasoning_content":"Let me think..."},"index":0}]}',
			'data: {"id":"chatcmpl-5","choices":[{"delta":{"content":"Here is my answer."},"index":0}]}',
			'data: {"id":"chatcmpl-5","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "reasoning-delta", delta: "Let me think..." },
			{ type: "text-delta", delta: "Here is my answer." },
			{ type: "finish", reason: "stop" },
		]);
	});

	it("handles invalid JSON gracefully", () => {
		const lines = [
			"data: {invalid json}",
			'data: {"id":"chatcmpl-6","choices":[{"delta":{"content":"ok"},"index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toHaveLength(2);
		expect(events[0]?.type).toBe("error");
		expect(events[1]).toEqual({ type: "text-delta", delta: "ok" });
	});

	it("ignores non-data lines", () => {
		const lines = [
			"event: message",
			": comment line",
			'data: {"id":"chatcmpl-7","choices":[{"delta":{"content":"hi"},"index":0}]}',
			"",
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([{ type: "text-delta", delta: "hi" }]);
	});

	it("stops at [DONE] sentinel", () => {
		const lines = [
			'data: {"id":"chatcmpl-8","choices":[{"delta":{"content":"before"},"index":0}]}',
			"data: [DONE]",
			'data: {"id":"chatcmpl-8","choices":[{"delta":{"content":"after"},"index":0}]}',
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([{ type: "text-delta", delta: "before" }]);
	});

	it("parses nested prompt_tokens_details.cached_tokens → cacheReadTokens", () => {
		const lines = [
			'data: {"id":"chatcmpl-nested","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-nested","usage":{"prompt_tokens":665,"completion_tokens":90,"prompt_tokens_details":{"cached_tokens":384},"completion_tokens_details":{"reasoning_tokens":86}}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		const usageEvent = events.find((e) => e.type === "usage") as Extract<
			ProviderEvent,
			{ type: "usage" }
		>;
		expect(usageEvent.usage.inputTokens).toBe(665);
		expect(usageEvent.usage.outputTokens).toBe(90);
		expect(usageEvent.usage.cacheReadTokens).toBe(384);
		expect(usageEvent.usage.cacheWriteTokens).toBeUndefined();
	});

	it("flat cache_read_tokens takes precedence over nested cached_tokens", () => {
		const lines = [
			'data: {"id":"chatcmpl-both","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-both","usage":{"prompt_tokens":100,"completion_tokens":20,"cache_read_tokens":50,"prompt_tokens_details":{"cached_tokens":99}}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		const usageEvent = events.find((e) => e.type === "usage") as Extract<
			ProviderEvent,
			{ type: "usage" }
		>;
		expect(usageEvent.usage.cacheReadTokens).toBe(50);
	});

	it("returns undefined for cacheReadTokens when neither flat nor nested present", () => {
		const lines = [
			'data: {"id":"chatcmpl-none","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-none","usage":{"prompt_tokens":10,"completion_tokens":5}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		const usageEvent = events.find((e) => e.type === "usage") as Extract<
			ProviderEvent,
			{ type: "usage" }
		>;
		expect(usageEvent.usage.cacheReadTokens).toBeUndefined();
		expect(usageEvent.usage.cacheWriteTokens).toBeUndefined();
	});

	it("handles missing/partial prompt_tokens_details safely", () => {
		const lines = [
			'data: {"id":"chatcmpl-partial","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-partial","usage":{"prompt_tokens":50,"completion_tokens":10,"prompt_tokens_details":{}}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		const usageEvent = events.find((e) => e.type === "usage") as Extract<
			ProviderEvent,
			{ type: "usage" }
		>;
		expect(usageEvent.usage.cacheReadTokens).toBeUndefined();
	});

	it("handles empty prompt_tokens_details object safely", () => {
		const lines = [
			'data: {"id":"chatcmpl-empty","choices":[{"delta":{},"finish_reason":"stop","index":0}]}',
			'data: {"id":"chatcmpl-empty","usage":{"prompt_tokens":30,"completion_tokens":8,"prompt_tokens_details":null}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		const usageEvent = events.find((e) => e.type === "usage") as Extract<
			ProviderEvent,
			{ type: "usage" }
		>;
		expect(usageEvent.usage.cacheReadTokens).toBeUndefined();
	});

	it("handles a complete turn with text, tool call, usage, and finish", () => {
		const lines = [
			'data: {"id":"chatcmpl-9","choices":[{"delta":{"content":"Let me check."},"index":0}]}',
			'data: {"id":"chatcmpl-9","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_xyz","function":{"name":"search","arguments":""}}]},"index":0}]}',
			'data: {"id":"chatcmpl-9","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"query\\":"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-9","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\\"dispatch\\"}"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-9","choices":[{"delta":{},"finish_reason":"tool_calls","index":0}]}',
			'data: {"id":"chatcmpl-9","usage":{"prompt_tokens":50,"completion_tokens":20}}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "text-delta", delta: "Let me check." },
			{
				type: "tool-call",
				toolCallId: "call_xyz",
				toolName: "search",
				input: { query: "dispatch" },
			},
			{ type: "finish", reason: "tool_calls" },
			{
				type: "usage",
				usage: {
					inputTokens: 50,
					outputTokens: 20,
					cacheReadTokens: undefined,
					cacheWriteTokens: undefined,
				},
			},
		]);
	});

	it("handles tool_call with unparseable arguments as raw string", () => {
		const lines = [
			'data: {"id":"chatcmpl-10","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_bad","function":{"name":"foo","arguments":"not-json"}}]},"index":0}]}',
			'data: {"id":"chatcmpl-10","choices":[{"delta":{},"finish_reason":"tool_calls","index":0}]}',
			"data: [DONE]",
		];

		const events = parseSSELines(lines);
		expect(events).toEqual([
			{ type: "tool-call", toolCallId: "call_bad", toolName: "foo", input: "not-json" },
			{ type: "finish", reason: "tool_calls" },
		]);
	});
});