From d36f63a26200a8df351d32609c880fac81296a98 Mon Sep 17 00:00:00 2001 From: wei-py Date: Mon, 14 Sep 2026 14:13:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(frontend):=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=AB=AF=E5=AF=B9=E8=AF=9D=E5=A4=8D=E7=94=A8=E5=90=8C=E6=AC=A1?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/chat.ts | 28 +-- frontend/src/components/MessageSources.vue | 15 +- frontend/src/sdk-test/SdkTestPanel.vue | 147 ++++++----- frontend/src/types/sse.ts | 18 ++ frontend/src/utils/chatAdapter.ts | 3 +- frontend/src/utils/sse.ts | 275 ++++++++------------- frontend/src/views/ChatPanel.vue | 131 +++++----- frontend/src/views/PipelineFlow.vue | 34 +-- frontend/tests/chat-protocol.test.mjs | 164 ++++++++++++ 9 files changed, 460 insertions(+), 355 deletions(-) create mode 100644 frontend/tests/chat-protocol.test.mjs diff --git a/frontend/src/api/chat.ts b/frontend/src/api/chat.ts index 4331a79..592c908 100644 --- a/frontend/src/api/chat.ts +++ b/frontend/src/api/chat.ts @@ -1,6 +1,5 @@ -import request from './request' import { getToken } from '@/utils/token' -import type { ApiResponse } from '@/types/api' +import type { ChatResult } from '@/types/sse' const API_BASE = '' @@ -28,13 +27,11 @@ export interface ChatOptions { * @param message 用户消息 * @param chatId 会话 ID * @param options 可选参数 - * @param includeEnableRag 是否写入 enableRag 参数(sources 接口无意义,需排除) */ function buildChatQueryParams( message: string, chatId: string, options?: ChatOptions, - includeEnableRag = true, ): URLSearchParams { const params = new URLSearchParams() params.set('message', message) @@ -42,7 +39,7 @@ function buildChatQueryParams( if (options) { if (options.roleId) params.set('roleId', options.roleId) if (options.accountId) params.set('accountId', options.accountId) - if (includeEnableRag && options.enableRag === true) params.set('enableRag', 'true') + if (options.enableRag === true) params.set('enableRag', 'true') if (options.rewriteStrategy) params.set('rewriteStrategy', options.rewriteStrategy) if (options.categoryId !== undefined && options.categoryId !== '') params.set('categoryId', String(options.categoryId)) if (options.categoryIds && options.categoryIds.length > 0) params.set('categoryIds', options.categoryIds.join(',')) @@ -51,10 +48,17 @@ function buildChatQueryParams( return params } -/** 同步对话:GET /ai/chat */ -export function chatSync(message: string, chatId: string, options?: ChatOptions): Promise { - const url = `${API_BASE}/ai/chat?${buildChatQueryParams(message, chatId, options).toString()}` - return fetch(url, { headers: authHeaders() }).then(res => res.text()) +/** 同次回答和引用来源:GET /ai/chat/result(直接 JSON,无 data 包裹)。 */ +export function chatSync(message: string, chatId: string, options?: ChatOptions, signal?: AbortSignal): Promise { + const url = `${API_BASE}/ai/chat/result?${buildChatQueryParams(message, chatId, options).toString()}` + return fetchChatResult(url, authHeaders(), signal) +} + +/** SDK 测试页也复用此响应处理,保留其独立域名与 SDK Token。 */ +export async function fetchChatResult(url: string, headers: Record, signal?: AbortSignal): Promise { + const res = await fetch(url, { headers, signal }) + if (!res.ok) throw new Error((await res.text()) || 'HTTP ' + res.status) + return res.json() } /** 获取 SSE 流式对话 URL:GET /ai/chat/stream */ @@ -62,12 +66,6 @@ export function chatSSEUrl(message: string, chatId: string, options?: ChatOption return `${API_BASE}/ai/chat/stream?${buildChatQueryParams(message, chatId, options).toString()}` } -/** 获取 RAG 引用来源:GET /ai/chat/sources(不传 enableRag,其余参数照传) */ -export function ragSources(message: string, chatId: string, options?: ChatOptions): Promise { - const path = `/ai/chat/sources?${buildChatQueryParams(message, chatId, options, false).toString()}` - return request.get(path).then(r => r.data) -} - /** * 获取 AI 推荐问题列表(管理后台 ChatPanel 用)。 * 调用 /conversation/{id}/suggestions,由管理后台 JwtAuthFilter 守卫。 diff --git a/frontend/src/components/MessageSources.vue b/frontend/src/components/MessageSources.vue index d76c7aa..92b1ab8 100644 --- a/frontend/src/components/MessageSources.vue +++ b/frontend/src/components/MessageSources.vue @@ -5,9 +5,9 @@
📄 {{ group.title || '文档 ' + key }}
-
- #{{ chunk.chunkIndex }} - +
+ #{{ chunk.chunkIndex }} +
@@ -19,17 +19,18 @@