()
| 210 | // ============================================================================ |
| 211 | |
| 212 | function createSseTransformStream(): TransformStream<Uint8Array, Uint8Array> { |
| 213 | const encoder = new TextEncoder() |
| 214 | const decoder = new TextDecoder() |
| 215 | |
| 216 | let buffer = '' |
| 217 | let responseId: string | null = null |
| 218 | let responseModel: string | null = null |
| 219 | let nextToolCallIndex = 0 |
| 220 | const outputIndexToToolIndex = new Map<number, number>() |
| 221 | let emittedRole = false |
| 222 | |
| 223 | function emit( |
| 224 | controller: TransformStreamDefaultController<Uint8Array>, |
| 225 | chunk: Record<string, unknown>, |
| 226 | ) { |
| 227 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`)) |
| 228 | } |
| 229 | |
| 230 | function processEvent( |
| 231 | controller: TransformStreamDefaultController<Uint8Array>, |
| 232 | data: Record<string, unknown>, |
| 233 | ) { |
| 234 | const type = data.type as string | undefined |
| 235 | if (!type) return |
| 236 | |
| 237 | switch (type) { |
| 238 | case 'response.created': { |
| 239 | const resp = data.response as Record<string, unknown> | undefined |
| 240 | responseId = (resp?.id as string) ?? null |
| 241 | responseModel = (resp?.model as string) ?? null |
| 242 | if (!emittedRole) { |
| 243 | emit(controller, { |
| 244 | id: responseId, |
| 245 | model: responseModel, |
| 246 | choices: [ |
| 247 | { index: 0, delta: { role: 'assistant' }, finish_reason: null }, |
| 248 | ], |
| 249 | }) |
| 250 | emittedRole = true |
| 251 | } |
| 252 | break |
| 253 | } |
| 254 | |
| 255 | case 'response.output_text.delta': { |
| 256 | emit(controller, { |
| 257 | id: responseId, |
| 258 | choices: [ |
| 259 | { |
| 260 | index: 0, |
| 261 | delta: { content: data.delta as string }, |
| 262 | finish_reason: null, |
| 263 | }, |
| 264 | ], |
| 265 | }) |
| 266 | break |
| 267 | } |
| 268 | |
| 269 | case 'response.reasoning_summary_text.delta': { |
no outgoing calls
no test coverage detected