|
|
@ -288,6 +288,7 @@ import '@tdesign-vue-next/chat/es/style/index.css' |
|
|
import { MessagePlugin } from 'tdesign-vue-next' |
|
|
import { MessagePlugin } from 'tdesign-vue-next' |
|
|
import { readSSEStream } from '@/utils/sse' |
|
|
import { readSSEStream } from '@/utils/sse' |
|
|
import { renderMarkdown } from '@/utils/markdown' |
|
|
import { renderMarkdown } from '@/utils/markdown' |
|
|
|
|
|
import { toast } from '@/utils/toast' |
|
|
import { toChatData, type ChatMessage } from '@/utils/chatAdapter' |
|
|
import { toChatData, type ChatMessage } from '@/utils/chatAdapter' |
|
|
|
|
|
|
|
|
// ==================== 常量 ==================== |
|
|
// ==================== 常量 ==================== |
|
|
@ -673,17 +674,21 @@ async function sendDemo(val?: string): Promise<void> { |
|
|
demoMessages.value.push({ id: genId(), role: 'user', content: text, streaming: false, time: fmtTime() }) |
|
|
demoMessages.value.push({ id: genId(), role: 'user', content: text, streaming: false, time: fmtTime() }) |
|
|
const assistantMsg: ChatMessage = { id: genId(), role: 'assistant', content: '', streaming: true, time: fmtTime(), sources: [] } |
|
|
const assistantMsg: ChatMessage = { id: genId(), role: 'assistant', content: '', streaming: true, time: fmtTime(), sources: [] } |
|
|
demoMessages.value.push(assistantMsg) |
|
|
demoMessages.value.push(assistantMsg) |
|
|
await scrollDemoBottom() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cid = demoChatId.value |
|
|
|
|
|
const requestMode = demoMode.value |
|
|
const opts = { enableRag: demoRag.value, rewriteStrategy: demoRag.value ? demoStrategy.value : undefined } |
|
|
const opts = { enableRag: demoRag.value, rewriteStrategy: demoRag.value ? demoStrategy.value : undefined } |
|
|
const url = buildDemoUrl(demoMode.value === 'sync' ? 'chat' : 'chat/stream', text, demoChatId.value, opts) |
|
|
|
|
|
|
|
|
// 提前构建 URL,固定本轮角色、账号、域名和检索配置。 |
|
|
|
|
|
const url = buildDemoUrl(requestMode === 'sync' ? 'chat' : 'chat/stream', text, cid, opts) |
|
|
|
|
|
const sourcesUrl = opts.enableRag ? buildDemoUrl('chat/sources', text, cid, opts) : '' |
|
|
const headers = sdkAuthHeaders() |
|
|
const headers = sdkAuthHeaders() |
|
|
|
|
|
await scrollDemoBottom() |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
if (abortController) abortController.abort() |
|
|
if (abortController) abortController.abort() |
|
|
abortController = new AbortController() |
|
|
abortController = new AbortController() |
|
|
|
|
|
|
|
|
if (demoMode.value === 'sync') { |
|
|
|
|
|
|
|
|
if (requestMode === 'sync') { |
|
|
const res = await fetch(url, { headers, signal: abortController.signal }) |
|
|
const res = await fetch(url, { headers, signal: abortController.signal }) |
|
|
if (!res.ok) throw new Error('HTTP ' + res.status) |
|
|
if (!res.ok) throw new Error('HTTP ' + res.status) |
|
|
assistantMsg.content = await res.text() |
|
|
assistantMsg.content = await res.text() |
|
|
@ -701,15 +706,19 @@ async function sendDemo(val?: string): Promise<void> { |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// RAG 模式下拉取引用来源 |
|
|
|
|
|
if (demoRag.value) { |
|
|
|
|
|
try { |
|
|
|
|
|
const sres = await fetch(buildDemoUrl('chat/sources', text, demoChatId.value, opts), { headers }) |
|
|
|
|
|
|
|
|
// 引用来源后台补齐,正文完成后立即结束发送状态。 |
|
|
|
|
|
if (opts.enableRag) { |
|
|
|
|
|
void fetch(sourcesUrl, { headers }).then(async sres => { |
|
|
const json = await sres.json() |
|
|
const json = await sres.json() |
|
|
if (json && json.success) assistantMsg.sources = json.data || [] |
|
|
|
|
|
} catch { |
|
|
|
|
|
/* 来源获取失败不影响主回答 */ |
|
|
|
|
|
|
|
|
if (!sres.ok || !json?.success) { |
|
|
|
|
|
throw new Error(json?.message || (sres.ok ? '引用来源获取失败' : 'HTTP ' + sres.status)) |
|
|
} |
|
|
} |
|
|
|
|
|
if (demoChatId.value !== cid || !demoMessages.value.some(msg => msg.id === assistantMsg.id)) return |
|
|
|
|
|
assistantMsg.sources = json.data || [] |
|
|
|
|
|
demoMessages.value = [...demoMessages.value] |
|
|
|
|
|
}).catch((e: any) => { |
|
|
|
|
|
toast('引用来源获取失败: ' + (e.message || e), 'warning') |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
} catch (e: any) { |
|
|
} catch (e: any) { |
|
|
if (e.name === 'AbortError') { |
|
|
if (e.name === 'AbortError') { |
|
|
|