eachSseData (providerAnthropicProtocol.cjs)

Open-ClaudeCode · ai

Parses SSE stream data from multiple LLM providers (Anthropic, OpenAI), extracting and accumulating text deltas from streaming responses.

function eachSseData(raw, visitor) {
  for (const frame of String(raw || '').split(/\n\n+/)) {
    const dataLines = frame.split(/\n/).filter(line => line.startsWith('data:'))
    for (const dataLine of dataLines) {
      const data = dataLine.slice(5).trim()
      if (!data || data === '[DONE]') continue
      try {
        visitor(JSON.parse(data))
      } catch {
        // Ignore malformed SSE frames from upstream providers.
      }
    }
  }
}

function anthropicDeltaText(delta = {}) {
  if (delta.type === 'text_delta') return delta.text || ''
  return delta.text || ''
}

function openAiDeltaText(parsed = {}) {
  const choice = parsed.choices?.[0]
  const delta = choice?.delta || {}
  return delta.content || choice?.message?.content || ''
}

function collectProviderStreamText(raw) {
 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key