fix: add SSE parsing function to handle server-sent events and message processing

This commit is contained in:
Mabe
2026-06-21 17:33:06 +02:00
parent d866664064
commit cbe64ba8ad
+26
View File
@@ -172,6 +172,32 @@ async function fetchOIDCConfig(): Promise<OIDCConfig> {
return response.json()
}
function parseSSE(chunk: string, onMessage: (message: string) => void) {
const events = chunk.split(/\n\n+/)
for (const event of events) {
if (!event.trim()) continue
const lines = event.split(/\n/)
let data = ''
for (const line of lines) {
const [field, ...rest] = line.split(':')
if (field === 'data') {
data += rest.join(':').trim()
}
}
if (data && data !== '[DONE]') {
try {
const parsed = JSON.parse(data)
const text = parsed?.choices?.[0]?.delta?.content
if (typeof text === 'string') {
onMessage(text)
}
} catch {
onMessage(data)
}
}
}
}
async function handleRedirectCallback(): Promise<string | null> {
const params = new URLSearchParams(window.location.search)
const code = params.get('code')