(controller)
| 220 | signal?.addEventListener('abort', abortHandler, { once: true }) |
| 221 | }, |
| 222 | async pull(controller) { |
| 223 | if (streamClosed) return |
| 224 | try { |
| 225 | await ensureStarted() |
| 226 | while (!signal?.aborted) { |
| 227 | const raw = await binding.wsV2Next?.(sessionId) |
| 228 | if (!raw) { |
| 229 | throw new Error('WS v2 stream closed before completion') |
| 230 | } |
| 231 | const message = JSON.parse(raw) as Record<string, unknown> |
| 232 | if (message.id === requestId) { |
| 233 | if ('error' in message) { |
| 234 | throw new Error(`WS v2 request rejected: ${JSON.stringify(message.error)}`) |
| 235 | } |
| 236 | continue |
| 237 | } |
| 238 | if (!isRequestNotification(message, requestId)) { |
| 239 | continue |
| 240 | } |
| 241 | const method = message.method |
| 242 | const params = (message.params ?? {}) as { data?: unknown; error?: unknown } |
| 243 | if (method === 'chat.completions.delta') { |
| 244 | if (typeof params.data === 'string') { |
| 245 | controller.enqueue(encodeSseFrame(params.data)) |
| 246 | return |
| 247 | } |
| 248 | continue |
| 249 | } |
| 250 | if (method === 'chat.completions.completed') { |
| 251 | controller.enqueue(encodeSseFrame('[DONE]')) |
| 252 | safeCloseController(controller) |
| 253 | await closeAfterTerminalFrame() |
| 254 | return |
| 255 | } |
| 256 | if (method === 'chat.completions.error') { |
| 257 | throw new Error(`WS v2 stream error: ${JSON.stringify(params.error)}`) |
| 258 | } |
| 259 | } |
| 260 | safeCloseController(controller) |
| 261 | await closeAfterTerminalFrame() |
| 262 | } catch (error) { |
| 263 | streamClosed = true |
| 264 | removeAbortHandler() |
| 265 | await close() |
| 266 | controller.error(error) |
| 267 | } |
| 268 | }, |
| 269 | async cancel() { |
| 270 | streamClosed = true |
| 271 | removeAbortHandler() |
nothing calls this directly
no test coverage detected