Feat: Append web citations in Claude streaming

Co-authored-by: gemini-3.7-flash <noreply@google.com>
This commit is contained in:
2026-08-20 10:31:58 -07:00
parent 32f8c661e4
commit ce3b3290ea

View File

@@ -258,6 +258,7 @@ export async function streamClaude({ apiKey, body, signal, onDelta, isRunning })
const includeThoughts = body.reasoning?.exclude !== true const includeThoughts = body.reasoning?.exclude !== true
let hasThinking = false, hasContent = false let hasThinking = false, hasContent = false
const sources = new Set()
const stream = client.messages.stream(payload) const stream = client.messages.stream(payload)
try { try {
@@ -272,11 +273,19 @@ export async function streamClaude({ apiKey, body, signal, onDelta, isRunning })
if (hasThinking && !hasContent) onDelta('\n') if (hasThinking && !hasContent) onDelta('\n')
onDelta(delta.text) onDelta(delta.text)
hasContent = true hasContent = true
} else if (delta.type === 'citations_delta' && delta.citation) {
const uri = delta.citation.url || delta.citation.source
if (uri) sources.add(uri)
} }
} }
} finally { } finally {
try { stream.controller?.abort() } catch {} try { stream.controller?.abort() } catch {}
} }
if (sources.size && isRunning()) {
const list = [...sources].map((uri, i) => `${i + 1}. [${uri}](${uri})`).join('\n')
onDelta(`\n\n---\n\n**Sources**\n\n${list}\n`)
}
} }
export async function streamGoogle({ apiKey, body, signal, onDelta, isRunning }) { export async function streamGoogle({ apiKey, body, signal, onDelta, isRunning }) {