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

Function handleStreamChunk

web/src/llm-api/moonshot.ts:682–767  ·  view source on GitHub ↗
({
  data,
  state,
  startTime,
  logger,
  userId,
  agentId,
  model,
}: {
  data: Record<string, unknown>
  state: StreamState
  startTime: Date
  logger: Logger
  userId: string
  agentId: string
  model: string
})

Source from the content-addressed store, hash-verified

680}
681
682function handleStreamChunk({
683 data,
684 state,
685 startTime,
686 logger,
687 userId,
688 agentId,
689 model,
690}: {
691 data: Record<string, unknown>
692 state: StreamState
693 startTime: Date
694 logger: Logger
695 userId: string
696 agentId: string
697 model: string
698}): StreamState {
699 const MAX_BUFFER_SIZE = 1 * 1024 * 1024
700
701 if ('error' in data) {
702 const errorData = data.error as Record<string, unknown>
703 logger.error(
704 {
705 userId,
706 agentId,
707 model,
708 errorCode: errorData?.code,
709 errorType: errorData?.type,
710 errorMessage: errorData?.message,
711 },
712 'Received error chunk in Moonshot stream',
713 )
714 return state
715 }
716
717 const choices = data.choices as Array<Record<string, unknown>> | undefined
718 if (!choices?.length) {
719 return state
720 }
721
722 const choice = choices[0]
723 const delta = choice.delta as Record<string, unknown> | undefined
724 const contentDelta = typeof delta?.content === 'string' ? delta.content : ''
725
726 if (state.responseText.length < MAX_BUFFER_SIZE) {
727 state.responseText += contentDelta
728 if (state.responseText.length >= MAX_BUFFER_SIZE) {
729 state.responseText =
730 state.responseText.slice(0, MAX_BUFFER_SIZE) + '\n---[TRUNCATED]---'
731 logger.warn(
732 { userId, agentId, model },
733 'Response text buffer truncated at 1MB',
734 )
735 }
736 }
737
738 const reasoningDelta =
739 typeof delta?.reasoning_content === 'string'

Callers 3

handleResponseFunction · 0.70
onResponseChunkFunction · 0.50
onSubagentResponseChunkFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected