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

Function handleResponse

web/src/llm-api/siliconflow.ts:418–498  ·  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

416}
417
418async function handleResponse({
419 userId,
420 stripeCustomerId,
421 agentId,
422 clientId,
423 clientRequestId,
424 costMode,
425 startTime,
426 request,
427 originalModel,
428 data,
429 state,
430 logger,
431 insertMessage,
432}: {
433 userId: string
434 stripeCustomerId?: string | null
435 agentId: string
436 clientId: string | null
437 clientRequestId: string | null
438 costMode: string | undefined
439 startTime: Date
440 request: unknown
441 originalModel: string
442 data: Record<string, unknown>
443 state: StreamState
444 logger: Logger
445 insertMessage: InsertMessageBigqueryFn
446}): Promise<{ state: StreamState; billedCredits?: number }> {
447 state = handleStreamChunk({ data, state, startTime, logger, userId, agentId, model: originalModel })
448
449 // Some providers send cumulative usage on EVERY chunk (not just the final one),
450 // so we must only bill once on the final chunk to avoid charging N times.
451 if ('error' in data || !data.usage || state.billedAlready || !isFinalChunk(data)) {
452 // Strip usage from non-final chunks and duplicate final chunks
453 // so the SDK doesn't see multiple usage objects
454 if (data.usage && (!isFinalChunk(data) || state.billedAlready)) {
455 delete data.usage
456 }
457 return { state }
458 }
459
460 const usageData = extractUsageAndCost(data.usage as Record<string, unknown>)
461 const messageId = typeof data.id === 'string' ? data.id : 'unknown'
462
463 state.billedAlready = true
464
465 insertMessageToBigQuery({
466 messageId,
467 userId,
468 startTime,
469 request,
470 reasoningText: state.reasoningText,
471 responseText: state.responseText,
472 usageData,
473 logger,
474 insertMessageBigquery: insertMessage,
475 }).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