|
3 | 3 | StreamingResponseFormatProcessor, |
4 | 4 | streamingResponseFormatProcessor, |
5 | 5 | } from '@/executor/utils' |
| 6 | +import type { AgentStreamEvent, AgentStreamSink } from '@/providers/stream-events' |
6 | 7 |
|
7 | 8 | describe('StreamingResponseFormatProcessor', () => { |
8 | 9 | let processor: StreamingResponseFormatProcessor |
@@ -170,6 +171,91 @@ describe('StreamingResponseFormatProcessor', () => { |
170 | 171 | expect(result).toBe('charlie') |
171 | 172 | }) |
172 | 173 |
|
| 174 | + it('projects a selected string field from live event deltas', async () => { |
| 175 | + let sourceSink: AgentStreamSink | undefined |
| 176 | + const projectedEvents: AgentStreamEvent[] = [] |
| 177 | + const subscribe = processor.processEventSubscription( |
| 178 | + (sink) => { |
| 179 | + sourceSink = sink |
| 180 | + return () => {} |
| 181 | + }, |
| 182 | + 'block-1', |
| 183 | + ['block-1_answer'], |
| 184 | + JSON.stringify({ |
| 185 | + type: 'object', |
| 186 | + properties: { |
| 187 | + meta: { type: 'object' }, |
| 188 | + answer: { type: 'string' }, |
| 189 | + score: { type: 'number' }, |
| 190 | + }, |
| 191 | + }) |
| 192 | + ) |
| 193 | + |
| 194 | + expect(subscribe).toBeDefined() |
| 195 | + subscribe?.({ |
| 196 | + onEvent: async (event) => { |
| 197 | + projectedEvents.push(event) |
| 198 | + }, |
| 199 | + }) |
| 200 | + |
| 201 | + await sourceSink?.onEvent({ |
| 202 | + type: 'text_delta', |
| 203 | + text: '{"meta":{"source":"test"},"answer":"Hello ', |
| 204 | + turn: 'pending', |
| 205 | + }) |
| 206 | + expect(projectedEvents).toEqual([{ type: 'text_delta', text: 'Hello ', turn: 'pending' }]) |
| 207 | + |
| 208 | + await sourceSink?.onEvent({ |
| 209 | + type: 'text_delta', |
| 210 | + text: 'world","score":1}', |
| 211 | + turn: 'pending', |
| 212 | + }) |
| 213 | + await sourceSink?.onEvent({ type: 'thinking_delta', text: 'done' }) |
| 214 | + await sourceSink?.onEvent({ type: 'turn_end', turn: 'final' }) |
| 215 | + |
| 216 | + expect(projectedEvents).toEqual([ |
| 217 | + { type: 'text_delta', text: 'Hello ', turn: 'pending' }, |
| 218 | + { type: 'text_delta', text: 'world', turn: 'pending' }, |
| 219 | + { type: 'thinking_delta', text: 'done' }, |
| 220 | + { type: 'turn_end', turn: 'final' }, |
| 221 | + ]) |
| 222 | + }) |
| 223 | + |
| 224 | + it('holds incomplete JSON escapes until they can be decoded', async () => { |
| 225 | + let sourceSink: AgentStreamSink | undefined |
| 226 | + const projectedText: string[] = [] |
| 227 | + const subscribe = processor.processEventSubscription( |
| 228 | + (sink) => { |
| 229 | + sourceSink = sink |
| 230 | + return () => {} |
| 231 | + }, |
| 232 | + 'block-1', |
| 233 | + ['block-1_answer'], |
| 234 | + { schema: { properties: { answer: { type: 'string' } } } } |
| 235 | + ) |
| 236 | + |
| 237 | + subscribe?.({ |
| 238 | + onEvent: (event) => { |
| 239 | + if (event.type === 'text_delta') projectedText.push(event.text) |
| 240 | + }, |
| 241 | + }) |
| 242 | + await sourceSink?.onEvent({ type: 'text_delta', text: '{"answer":"line\\', turn: 'final' }) |
| 243 | + await sourceSink?.onEvent({ type: 'text_delta', text: 'nnext"}', turn: 'final' }) |
| 244 | + |
| 245 | + expect(projectedText).toEqual(['line', '\nnext']) |
| 246 | + }) |
| 247 | + |
| 248 | + it('does not claim live sink projection for non-string fields', () => { |
| 249 | + const subscribe = processor.processEventSubscription( |
| 250 | + () => () => {}, |
| 251 | + 'block-1', |
| 252 | + ['block-1_score'], |
| 253 | + { schema: { properties: { score: { type: 'number' } } } } |
| 254 | + ) |
| 255 | + |
| 256 | + expect(subscribe).toBeUndefined() |
| 257 | + }) |
| 258 | + |
173 | 259 | it.concurrent('should handle missing fields gracefully', async () => { |
174 | 260 | const mockStream = new ReadableStream({ |
175 | 261 | start(controller) { |
|
0 commit comments