* Process a stream of AG-UI events from the streaming connection adapter.
(
source: AsyncIterable<StreamChunk>,
fallbackRunId: string,
)
| 234 | * Process a stream of AG-UI events from the streaming connection adapter. |
| 235 | */ |
| 236 | private async processStream( |
| 237 | source: AsyncIterable<StreamChunk>, |
| 238 | fallbackRunId: string, |
| 239 | ): Promise<void> { |
| 240 | let streamRunId: string | undefined |
| 241 | |
| 242 | for await (const chunk of source) { |
| 243 | if (this.abortController?.signal.aborted) break |
| 244 | |
| 245 | this.callbacksRef.onChunk?.(chunk) |
| 246 | const chunkRunId = |
| 247 | 'runId' in chunk && typeof chunk.runId === 'string' |
| 248 | ? chunk.runId |
| 249 | : undefined |
| 250 | |
| 251 | // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- AG-UI EventType has ~22 variants; this consumer only handles the subset relevant to generation lifecycle. |
| 252 | switch (chunk.type) { |
| 253 | case 'RUN_STARTED': { |
| 254 | streamRunId = chunk.runId |
| 255 | this.devtoolsBridge.ensureRunStarted(chunk.runId) |
| 256 | break |
| 257 | } |
| 258 | case 'CUSTOM': { |
| 259 | this.devtoolsBridge.ensureRunStarted(streamRunId ?? fallbackRunId) |
| 260 | if (chunk.name === GENERATION_EVENTS.RESULT) { |
| 261 | this.setResult(chunk.value as TResult) |
| 262 | } else if (chunk.name === GENERATION_EVENTS.PROGRESS) { |
| 263 | const { progress, message } = chunk.value as { |
| 264 | progress: number |
| 265 | message?: string |
| 266 | } |
| 267 | this.setProgress(progress, message) |
| 268 | } |
| 269 | break |
| 270 | } |
| 271 | case 'RUN_FINISHED': { |
| 272 | streamRunId = chunk.runId |
| 273 | this.devtoolsBridge.ensureRunStarted(chunk.runId) |
| 274 | this.setStatus('success') |
| 275 | break |
| 276 | } |
| 277 | case 'RUN_ERROR': { |
| 278 | this.devtoolsBridge.ensureRunStarted( |
| 279 | chunkRunId ?? streamRunId ?? fallbackRunId, |
| 280 | ) |
| 281 | // Prefer spec `message`; fall back to deprecated `error.message` |
| 282 | const msg = |
| 283 | (chunk.message as string | undefined) || |
| 284 | chunk.error?.message || |
| 285 | 'An error occurred' |
| 286 | throw new Error(msg) |
| 287 | } |
| 288 | default: |
| 289 | break |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 |
no test coverage detected