Fix 法规对话

This commit is contained in:
wangwei
2026-05-21 23:20:39 +08:00
parent 1b640f0084
commit bf6d47e1fd
11 changed files with 553 additions and 428 deletions
+32 -7
View File
@@ -38,7 +38,14 @@ function parseSSEChunk(raw: string, onMessage: (data: SSEMessage) => void) {
const joined = dataLines.join('\n');
if (!joined) continue;
if (eventName === 'sources') {
// /agent/chat/stream uses named events (sources, content, done, error, session, status)
// /rag/chat wraps everything in event:message with type in JSON body — handle both
if (eventName === 'session') {
try {
const payload = JSON.parse(joined) as Record<string, unknown>;
onMessage({ type: 'session', session_id: String(payload.session_id ?? '') });
} catch { /* ignore */ }
} else if (eventName === 'sources') {
try {
const docs = JSON.parse(joined) as Array<Record<string, unknown>>;
onMessage({
@@ -53,17 +60,26 @@ function parseSSEChunk(raw: string, onMessage: (data: SSEMessage) => void) {
download_url: doc.doc_id ? `${AGENT_API_BASE}/documents/download/${String(doc.doc_id)}` : undefined,
})),
});
} catch {
// Ignore malformed source payloads.
}
} catch { /* ignore */ }
} else if (eventName === 'content') {
onMessage({ type: 'chunk', text: joined });
} else if (eventName === 'done') {
onMessage({ type: 'done', text: joined });
try {
const payload = JSON.parse(joined) as Record<string, unknown>;
onMessage({ type: 'done', session_id: payload.session_id ? String(payload.session_id) : undefined });
} catch {
onMessage({ type: 'done' });
}
} else if (eventName === 'error') {
onMessage({ type: 'error', text: joined });
} else if (eventName === 'status') {
onMessage({ type: 'status', text: joined });
} else if (eventName === 'message') {
// /rag/chat format: event:message + JSON body with type field
try {
const payload = JSON.parse(joined) as SSEMessage;
onMessage(payload);
} catch { /* ignore */ }
}
}
}
@@ -74,7 +90,9 @@ export async function ragChat(
onMessage: (data: SSEMessage) => void,
onError?: (error: Error) => void,
onComplete?: () => void,
filters?: string
filters?: string,
sessionId?: string,
signal?: AbortSignal,
): Promise<void> {
try {
const response = await fetch(`${AGENT_API_BASE}/agent/chat/stream`, {
@@ -83,7 +101,13 @@ export async function ragChat(
'Content-Type': 'application/json',
Accept: 'text/event-stream',
},
body: JSON.stringify({ query, top_k: topK, ...(filters ? { filters } : {}) }),
body: JSON.stringify({
query,
top_k: topK,
...(filters ? { filters } : {}),
...(sessionId ? { session_id: sessionId } : {}),
}),
signal,
});
if (!response.ok || !response.body) {
@@ -112,6 +136,7 @@ export async function ragChat(
onComplete();
}
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') return;
if (onError) {
onError(error instanceof Error ? error : new Error(String(error)));
}