MCPcopy Create free account
hub / github.com/TanStack/ai / onChunk

Function onChunk

packages/ai-event-client/src/devtools-middleware.ts:332–456  ·  view source on GitHub ↗
(ctx, rawChunk)

Source from the content-addressed store, hash-verified

330 },
331
332 onChunk(ctx, rawChunk) {
333 if (!isKnownChunk(rawChunk)) return
334 const chunk = rawChunk
335 const base = buildEventContext(ctx)
336
337 switch (chunk.type) {
338 case 'TEXT_MESSAGE_CONTENT': {
339 if (chunk.content) {
340 localAccumulatedContent = chunk.content
341 } else {
342 localAccumulatedContent += chunk.delta
343 }
344 safeEmit('text:chunk:content', {
345 ...base,
346 messageId: localMessageId || undefined,
347 content: localAccumulatedContent,
348 delta: chunk.delta,
349 timestamp: Date.now(),
350 })
351 break
352 }
353 case 'TOOL_CALL_START': {
354 const toolIndex = chunk.index ?? 0
355 const toolName = chunk.toolCallName
356 activeToolCalls.set(chunk.toolCallId, {
357 toolName,
358 index: toolIndex,
359 })
360 safeEmit('text:chunk:tool-call', {
361 ...base,
362 messageId: localMessageId || undefined,
363 toolCallId: chunk.toolCallId,
364 toolName,
365 index: toolIndex,
366 arguments: '',
367 timestamp: Date.now(),
368 })
369 break
370 }
371 case 'TOOL_CALL_ARGS': {
372 const active = activeToolCalls.get(chunk.toolCallId)
373 safeEmit('text:chunk:tool-call', {
374 ...base,
375 messageId: localMessageId || undefined,
376 toolCallId: chunk.toolCallId,
377 toolName: active?.toolName ?? '',
378 index: active?.index ?? 0,
379 arguments: chunk.delta,
380 timestamp: Date.now(),
381 })
382 break
383 }
384 case 'TOOL_CALL_END': {
385 activeToolCalls.delete(chunk.toolCallId)
386 safeEmit('text:chunk:tool-result', {
387 ...base,
388 messageId: localMessageId || undefined,
389 toolCallId: chunk.toolCallId,

Callers 1

[Symbol.asyncIterator]Function · 0.50

Calls 7

isKnownChunkFunction · 0.85
buildEventContextFunction · 0.85
safeEmitFunction · 0.85
nowMethod · 0.80
setMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected