|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import type { TextStreamPart, ToolSet } from '@objectstack/spec/contracts'; |
| 5 | +import { encodeStreamPart, encodeVercelDataStream } from '../stream/vercel-stream-encoder.js'; |
| 6 | + |
| 7 | +// ───────────────────────────────────────────────────────────────── |
| 8 | +// encodeStreamPart — individual frame encoding |
| 9 | +// ───────────────────────────────────────────────────────────────── |
| 10 | + |
| 11 | +describe('encodeStreamPart', () => { |
| 12 | + it('should encode text-delta as "0:" frame', () => { |
| 13 | + const part = { type: 'text-delta', text: 'Hello world' } as TextStreamPart<ToolSet>; |
| 14 | + expect(encodeStreamPart(part)).toBe('0:"Hello world"\n'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should JSON-escape text-delta content', () => { |
| 18 | + const part = { type: 'text-delta', text: 'say "hi"\nnewline' } as TextStreamPart<ToolSet>; |
| 19 | + const frame = encodeStreamPart(part); |
| 20 | + expect(frame).toBe(`0:${JSON.stringify('say "hi"\nnewline')}\n`); |
| 21 | + expect(frame.startsWith('0:')).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should encode tool-call as "9:" frame', () => { |
| 25 | + const part = { |
| 26 | + type: 'tool-call', |
| 27 | + toolCallId: 'call_1', |
| 28 | + toolName: 'get_weather', |
| 29 | + input: { location: 'San Francisco' }, |
| 30 | + } as TextStreamPart<ToolSet>; |
| 31 | + |
| 32 | + const frame = encodeStreamPart(part); |
| 33 | + expect(frame.startsWith('9:')).toBe(true); |
| 34 | + |
| 35 | + const payload = JSON.parse(frame.slice(2)); |
| 36 | + expect(payload).toEqual({ |
| 37 | + toolCallId: 'call_1', |
| 38 | + toolName: 'get_weather', |
| 39 | + args: { location: 'San Francisco' }, |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should encode tool-call-streaming-start as "b:" frame', () => { |
| 44 | + const part = { |
| 45 | + type: 'tool-call-streaming-start', |
| 46 | + toolCallId: 'call_2', |
| 47 | + toolName: 'search', |
| 48 | + } as TextStreamPart<ToolSet>; |
| 49 | + |
| 50 | + const frame = encodeStreamPart(part); |
| 51 | + expect(frame.startsWith('b:')).toBe(true); |
| 52 | + |
| 53 | + const payload = JSON.parse(frame.slice(2)); |
| 54 | + expect(payload).toEqual({ |
| 55 | + toolCallId: 'call_2', |
| 56 | + toolName: 'search', |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should encode tool-call-delta as "c:" frame', () => { |
| 61 | + const part = { |
| 62 | + type: 'tool-call-delta', |
| 63 | + toolCallId: 'call_2', |
| 64 | + argsTextDelta: '{"query":', |
| 65 | + } as TextStreamPart<ToolSet>; |
| 66 | + |
| 67 | + const frame = encodeStreamPart(part); |
| 68 | + expect(frame.startsWith('c:')).toBe(true); |
| 69 | + |
| 70 | + const payload = JSON.parse(frame.slice(2)); |
| 71 | + expect(payload).toEqual({ |
| 72 | + toolCallId: 'call_2', |
| 73 | + argsTextDelta: '{"query":', |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should encode tool-result as "a:" frame', () => { |
| 78 | + const part = { |
| 79 | + type: 'tool-result', |
| 80 | + toolCallId: 'call_1', |
| 81 | + toolName: 'get_weather', |
| 82 | + result: { temperature: 72 }, |
| 83 | + } as TextStreamPart<ToolSet>; |
| 84 | + |
| 85 | + const frame = encodeStreamPart(part); |
| 86 | + expect(frame.startsWith('a:')).toBe(true); |
| 87 | + |
| 88 | + const payload = JSON.parse(frame.slice(2)); |
| 89 | + expect(payload).toEqual({ |
| 90 | + toolCallId: 'call_1', |
| 91 | + result: { temperature: 72 }, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should encode finish as "d:" frame', () => { |
| 96 | + const part = { |
| 97 | + type: 'finish', |
| 98 | + finishReason: 'stop', |
| 99 | + totalUsage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 }, |
| 100 | + rawFinishReason: 'stop', |
| 101 | + } as unknown as TextStreamPart<ToolSet>; |
| 102 | + |
| 103 | + const frame = encodeStreamPart(part); |
| 104 | + expect(frame.startsWith('d:')).toBe(true); |
| 105 | + |
| 106 | + const payload = JSON.parse(frame.slice(2)); |
| 107 | + expect(payload.finishReason).toBe('stop'); |
| 108 | + expect(payload.usage).toEqual({ promptTokens: 10, completionTokens: 20, totalTokens: 30 }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should encode step-finish as "e:" frame', () => { |
| 112 | + const part = { |
| 113 | + type: 'step-finish', |
| 114 | + finishReason: 'tool-calls', |
| 115 | + totalUsage: { promptTokens: 5, completionTokens: 10, totalTokens: 15 }, |
| 116 | + isContinued: true, |
| 117 | + } as unknown as TextStreamPart<ToolSet>; |
| 118 | + |
| 119 | + const frame = encodeStreamPart(part); |
| 120 | + expect(frame.startsWith('e:')).toBe(true); |
| 121 | + |
| 122 | + const payload = JSON.parse(frame.slice(2)); |
| 123 | + expect(payload.finishReason).toBe('tool-calls'); |
| 124 | + expect(payload.isContinued).toBe(true); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return empty string for unknown event types', () => { |
| 128 | + const part = { type: 'unknown-internal' } as unknown as TextStreamPart<ToolSet>; |
| 129 | + expect(encodeStreamPart(part)).toBe(''); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +// ───────────────────────────────────────────────────────────────── |
| 134 | +// encodeVercelDataStream — async iterable transformation |
| 135 | +// ───────────────────────────────────────────────────────────────── |
| 136 | + |
| 137 | +describe('encodeVercelDataStream', () => { |
| 138 | + it('should transform stream events into Vercel Data Stream frames', async () => { |
| 139 | + async function* source(): AsyncIterable<TextStreamPart<ToolSet>> { |
| 140 | + yield { type: 'text-delta', text: 'Hello' } as TextStreamPart<ToolSet>; |
| 141 | + yield { type: 'text-delta', text: ' world' } as TextStreamPart<ToolSet>; |
| 142 | + yield { |
| 143 | + type: 'finish', |
| 144 | + finishReason: 'stop', |
| 145 | + totalUsage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 }, |
| 146 | + rawFinishReason: 'stop', |
| 147 | + } as unknown as TextStreamPart<ToolSet>; |
| 148 | + } |
| 149 | + |
| 150 | + const frames: string[] = []; |
| 151 | + for await (const frame of encodeVercelDataStream(source())) { |
| 152 | + frames.push(frame); |
| 153 | + } |
| 154 | + |
| 155 | + expect(frames).toHaveLength(3); |
| 156 | + expect(frames[0]).toBe('0:"Hello"\n'); |
| 157 | + expect(frames[1]).toBe('0:" world"\n'); |
| 158 | + expect(frames[2]).toMatch(/^d:/); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should skip events with no wire format mapping', async () => { |
| 162 | + async function* source(): AsyncIterable<TextStreamPart<ToolSet>> { |
| 163 | + yield { type: 'text-delta', text: 'Hi' } as TextStreamPart<ToolSet>; |
| 164 | + yield { type: 'unknown-internal' } as unknown as TextStreamPart<ToolSet>; |
| 165 | + yield { |
| 166 | + type: 'finish', |
| 167 | + finishReason: 'stop', |
| 168 | + totalUsage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 }, |
| 169 | + rawFinishReason: 'stop', |
| 170 | + } as unknown as TextStreamPart<ToolSet>; |
| 171 | + } |
| 172 | + |
| 173 | + const frames: string[] = []; |
| 174 | + for await (const frame of encodeVercelDataStream(source())) { |
| 175 | + frames.push(frame); |
| 176 | + } |
| 177 | + |
| 178 | + // 'unknown-internal' is silently dropped |
| 179 | + expect(frames).toHaveLength(2); |
| 180 | + expect(frames[0]).toBe('0:"Hi"\n'); |
| 181 | + expect(frames[1]).toMatch(/^d:/); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should handle empty stream', async () => { |
| 185 | + async function* source(): AsyncIterable<TextStreamPart<ToolSet>> { |
| 186 | + // empty |
| 187 | + } |
| 188 | + |
| 189 | + const frames: string[] = []; |
| 190 | + for await (const frame of encodeVercelDataStream(source())) { |
| 191 | + frames.push(frame); |
| 192 | + } |
| 193 | + |
| 194 | + expect(frames).toHaveLength(0); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should handle tool-call events in stream', async () => { |
| 198 | + async function* source(): AsyncIterable<TextStreamPart<ToolSet>> { |
| 199 | + yield { |
| 200 | + type: 'tool-call', |
| 201 | + toolCallId: 'call_1', |
| 202 | + toolName: 'search', |
| 203 | + input: { query: 'test' }, |
| 204 | + } as TextStreamPart<ToolSet>; |
| 205 | + yield { |
| 206 | + type: 'tool-result', |
| 207 | + toolCallId: 'call_1', |
| 208 | + toolName: 'search', |
| 209 | + result: { hits: 42 }, |
| 210 | + } as TextStreamPart<ToolSet>; |
| 211 | + yield { |
| 212 | + type: 'finish', |
| 213 | + finishReason: 'tool-calls', |
| 214 | + totalUsage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 }, |
| 215 | + rawFinishReason: 'tool_calls', |
| 216 | + } as unknown as TextStreamPart<ToolSet>; |
| 217 | + } |
| 218 | + |
| 219 | + const frames: string[] = []; |
| 220 | + for await (const frame of encodeVercelDataStream(source())) { |
| 221 | + frames.push(frame); |
| 222 | + } |
| 223 | + |
| 224 | + expect(frames).toHaveLength(3); |
| 225 | + expect(frames[0]).toMatch(/^9:/); |
| 226 | + expect(frames[1]).toMatch(/^a:/); |
| 227 | + expect(frames[2]).toMatch(/^d:/); |
| 228 | + |
| 229 | + // Verify tool-call frame content |
| 230 | + const toolCallPayload = JSON.parse(frames[0].slice(2)); |
| 231 | + expect(toolCallPayload.toolCallId).toBe('call_1'); |
| 232 | + expect(toolCallPayload.args).toEqual({ query: 'test' }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments