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

Function handleStreamChunk

web/src/llm-api/openrouter.ts:708–809  ·  view source on GitHub ↗
({
  data,
  state,
  startTime,
  logger,
  userId,
  agentId,
  model,
}: {
  data: OpenRouterStreamChatCompletionChunk
  state: StreamState
  startTime: Date
  logger: Logger
  userId: string
  agentId: string
  model: string | undefined
})

Source from the content-addressed store, hash-verified

706}
707
708async function handleStreamChunk({
709 data,
710 state,
711 startTime,
712 logger,
713 userId,
714 agentId,
715 model,
716}: {
717 data: OpenRouterStreamChatCompletionChunk
718 state: StreamState
719 startTime: Date
720 logger: Logger
721 userId: string
722 agentId: string
723 model: string | undefined
724}): Promise<StreamState> {
725 // Define a safe buffer limit to prevent OOM errors on the server while
726 // still storing enough data for logging and billing. 1MB is a generous limit.
727 const MAX_BUFFER_SIZE = 1 * 1024 * 1024 // 1MB
728
729 // Capture generation id and model from any regular chunk so we can still
730 // bill via the generation-lookup fallback if the stream never emits usage.
731 if (!('error' in data)) {
732 if (data.id && !state.generationId) {
733 state.generationId = data.id
734 }
735 if (data.model && !state.model) {
736 state.model = data.model
737 }
738 }
739
740 if ('error' in data) {
741 // Log detailed error information for stream errors (e.g., Forbidden from Anthropic)
742 const errorData = data.error as {
743 code?: string | number | null
744 type?: string | null
745 message: string
746 param?: unknown
747 metadata?: { raw?: string; provider_name?: string }
748 }
749 logger.error(
750 {
751 userId,
752 agentId,
753 model,
754 errorCode: errorData.code,
755 errorType: errorData.type,
756 errorMessage: errorData.message,
757 errorParam: errorData.param,
758 // Provider-specific error details (e.g., from Anthropic via OpenRouter)
759 providerName: errorData.metadata?.provider_name,
760 providerRawError: errorData.metadata?.raw,
761 },
762 'Received error chunk in OpenRouter stream',
763 )
764 return state
765 }

Callers 1

handleResponseFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected