feat: 同步知识库与工作台相关改动

This commit is contained in:
Jackzhou
2026-08-24 10:40:15 +08:00
parent 8d0588827d
commit 4f33b036d1
50 changed files with 8034 additions and 273 deletions
+51 -3
View File
@@ -38,8 +38,32 @@ export function getSecretsStatus() {
}
// SSE 流式对话:后端 text/event-stream,每行 "data: {json}\n\n"
export async function streamChat(message, context, history, onChunk) {
export async function streamChat(message, context, history, handlers) {
const token = localStorage.getItem('token')
const onChunk = typeof handlers === 'function' ? handlers : handlers?.onChunk
const onMeta = typeof handlers === 'function' ? null : handlers?.onMeta
const onDone = typeof handlers === 'function' ? null : handlers?.onDone
const traceId = `chat-${Date.now()}`
// #region debug-point A:frontend-request
fetch('http://127.0.0.1:7777/event', {
method: 'POST',
body: JSON.stringify({
sessionId: 'knowledge-chat-401',
runId: 'pre-fix',
hypothesisId: 'A',
location: 'frontend/src/api/ai.js:streamChat:request',
traceId,
msg: '[DEBUG] streamChat request',
data: {
hasToken: Boolean(token),
url: '/api/ai-chat/message',
messagePreview: String(message || '').slice(0, 80),
spaceKey: context?.knowledge_space_key || '',
},
ts: Date.now(),
}),
}).catch(() => {})
// #endregion
const resp = await fetch('/api/ai-chat/message', {
method: 'POST',
headers: {
@@ -51,12 +75,33 @@ export async function streamChat(message, context, history, onChunk) {
if (!resp.ok) {
let msg = 'LLM 请求失败'
let raw = ''
try {
const j = await resp.json()
raw = await resp.text()
const j = JSON.parse(raw)
msg = j.message || j.error || msg
} catch {
/* ignore */
}
// #region debug-point A:frontend-non200
fetch('http://127.0.0.1:7777/event', {
method: 'POST',
body: JSON.stringify({
sessionId: 'knowledge-chat-401',
runId: 'pre-fix',
hypothesisId: 'A',
location: 'frontend/src/api/ai.js:streamChat:non200',
traceId,
msg: '[DEBUG] streamChat non-200 response',
data: {
status: resp.status,
statusText: resp.statusText,
bodyPreview: String(raw || '').slice(0, 240),
},
ts: Date.now(),
}),
}).catch(() => {})
// #endregion
throw new Error(msg)
}
@@ -84,10 +129,13 @@ export async function streamChat(message, context, history, onChunk) {
continue
}
if (obj.type === 'text') {
onChunk(obj.content)
onChunk?.(obj.content)
} else if (obj.type === 'meta') {
onMeta?.(obj)
} else if (obj.type === 'error') {
throw new Error(obj.message || 'LLM 请求失败')
} else if (obj.type === 'done') {
onDone?.()
return
}
}