MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / handleResponse

Function handleResponse

web/src/llm-api/canopywave.ts:453–533  ·  view source on GitHub ↗
({
  userId,
  stripeCustomerId,
  agentId,
  clientId,
  clientRequestId,
  costMode,
  startTime,
  request,
  originalModel,
  data,
  state,
  logger,
  insertMessage,
}: {
  userId: string
  stripeCustomerId?: string | null
  agentId: string
  clientId: string | null
  clientRequestId: string | null
  costMode: string | undefined
  startTime: Date
  request: unknown
  originalModel: string
  data: Record<string, unknown>
  state: StreamState
  logger: Logger
  insertMessage: InsertMessageBigqueryFn
})

Source from the content-addressed store, hash-verified

451}
452
453async function handleResponse({
454 userId,
455 stripeCustomerId,
456 agentId,
457 clientId,
458 clientRequestId,
459 costMode,
460 startTime,
461 request,
462 originalModel,
463 data,
464 state,
465 logger,
466 insertMessage,
467}: {
468 userId: string
469 stripeCustomerId?: string | null
470 agentId: string
471 clientId: string | null
472 clientRequestId: string | null
473 costMode: string | undefined
474 startTime: Date
475 request: unknown
476 originalModel: string
477 data: Record<string, unknown>
478 state: StreamState
479 logger: Logger
480 insertMessage: InsertMessageBigqueryFn
481}): Promise<{ state: StreamState; billedCredits?: number }> {
482 state = handleStreamChunk({ data, state, startTime, logger, userId, agentId, model: originalModel })
483
484 // Some providers send cumulative usage on EVERY chunk (not just the final one),
485 // so we must only bill once on the final chunk to avoid charging N times.
486 if ('error' in data || !data.usage || state.billedAlready || !isFinalChunk(data)) {
487 // Strip usage from non-final chunks and duplicate final chunks
488 // so the SDK doesn't see multiple usage objects
489 if (data.usage && (!isFinalChunk(data) || state.billedAlready)) {
490 delete data.usage
491 }
492 return { state }
493 }
494
495 const usageData = extractUsageAndCost(data.usage as Record<string, unknown>, originalModel)
496 const messageId = typeof data.id === 'string' ? data.id : 'unknown'
497
498 state.billedAlready = true
499
500 insertMessageToBigQuery({
501 messageId,
502 userId,
503 startTime,
504 request,
505 reasoningText: state.reasoningText,
506 responseText: state.responseText,
507 usageData,
508 logger,
509 insertMessageBigquery: insertMessage,
510 }).catch((error) => {

Callers 1

handleLineFunction · 0.70

Calls 5

insertMessageToBigQueryFunction · 0.90
consumeCreditsForMessageFunction · 0.90
handleStreamChunkFunction · 0.70
isFinalChunkFunction · 0.70
extractUsageAndCostFunction · 0.70

Tested by

no test coverage detected