MCPcopy Index your code
hub / github.com/TanStack/ai / processStream

Method processStream

packages/ai-client/src/generation-client.ts:236–292  ·  view source on GitHub ↗

* Process a stream of AG-UI events from the streaming connection adapter.

(
    source: AsyncIterable<StreamChunk>,
    fallbackRunId: string,
  )

Source from the content-addressed store, hash-verified

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

Callers 1

generateMethod · 0.95

Calls 4

setResultMethod · 0.95
setProgressMethod · 0.95
setStatusMethod · 0.95
ensureRunStartedMethod · 0.45

Tested by

no test coverage detected