fix: add SSE parsing function to handle server-sent events and message processing
This commit is contained in:
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user