From dfab0f6d43ca3dbc52224d7896c45f36f808c36b Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Wed, 3 Jun 2026 01:23:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=B9=B6?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0model=5Fid=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 流式请求失败时不再隐藏用户消息,而是展示错误回复 - 在 ChatMessage.meta 类型中增加 error 字段用于标记错误消息 - 在 streamChat 函数中增加 model_id 参数 - 在 ModelOverrides 类型中增加 model_id 字段 --- src/api.ts | 62 ++++++++++++++++++++++-------------------- src/pages/ChatPage.tsx | 22 +++++++++++++-- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/api.ts b/src/api.ts index 951dc93..3b2fd57 100644 --- a/src/api.ts +++ b/src/api.ts @@ -91,19 +91,20 @@ export interface ToolCallTrace { result: any; } -export interface ChatMessage { - id: string; - role: 'user' | 'assistant'; - content: string; +export interface ChatMessage { + id: string; + role: 'user' | 'assistant'; + content: string; reasoning?: string | null; - parentId?: string | null; - createdAt: number; - meta?: { - retrieved?: RetrievedSnippet[]; - toolCalls?: ToolCallTrace[]; - aborted?: boolean; + parentId?: string | null; + createdAt: number; + meta?: { + retrieved?: RetrievedSnippet[]; + toolCalls?: ToolCallTrace[]; + aborted?: boolean; reasoning?: string; - } | null; + error?: string; + } | null; } export interface BranchInfo { @@ -700,33 +701,36 @@ export interface StreamEvents { export interface ModelOverrides { model?: string; + model_id?: string; temperature?: number; topP?: number; maxTokens?: number; } -export async function streamChat( - agentId: string, - content: string, - handlers: StreamEvents, - signal?: AbortSignal, - sessionId?: string, +export async function streamChat( + agentId: string, + content: string, + handlers: StreamEvents, + signal?: AbortSignal, + sessionId?: string, model?: string, - imageUrls?: string[] -) { - const resp = await fetch(`https://api.hoyidata.com/aura/v1/chat/${agentId}/messages/stream`, { - method: 'POST', + modelId?: string, + imageUrls?: string[] +) { + const resp = await fetch(`https://api.hoyidata.com/aura/v1/chat/${agentId}/messages/stream`, { + method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'text/event-stream' }, - body: JSON.stringify({ - content, - sessionId, + body: JSON.stringify({ + content, + sessionId, model, + model_id: modelId, imageUrls: imageUrls ?? [] - }), - signal, - credentials: 'include' - }); - return await consumeSSE(resp, handlers, signal); + }), + signal, + credentials: 'include' + }); + return await consumeSSE(resp, handlers, signal); } /** 重新生成(开新分支);行为同 streamChat */ diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx index ec052e8..9f13c1e 100644 --- a/src/pages/ChatPage.tsx +++ b/src/pages/ChatPage.tsx @@ -258,6 +258,7 @@ export default function ChatPage() { abortRef.current = ctrl; const model = overrides.model || parseAgentModels(agent?.model)[0] || ''; + const modelId = overrides.model_id || ''; const attText = buildAttachmentsText(); const content = attText ? `${text}\n\n${attText}` : text; @@ -363,7 +364,15 @@ export default function ChatPage() { }, onError: (errMsg) => { msg.error('流式失败:' + errMsg); - setMessages((m) => (m || []).filter((x) => x.id !== tempUser.id)); + // 创建错误消息作为助手回复,保留用户消息 + const errorMessage: ChatMessage = { + id: 'error-' + Date.now(), + role: 'assistant', + content: `❌ 请求失败:${errMsg}`, + createdAt: Date.now(), + meta: { error: errMsg } + }; + setMessages((m) => [...(m || []), errorMessage]); setStreaming({ active: false, reasoningText: '', @@ -378,13 +387,22 @@ export default function ChatPage() { ctrl.signal, sessionId || undefined, model, + modelId, imageUrls ); } catch (e: any) { if (e?.name !== 'AbortError') { msg.error('请求失败:' + (e?.message ?? e)); + // 创建错误消息作为助手回复,保留用户消息 + const errorMessage: ChatMessage = { + id: 'error-' + Date.now(), + role: 'assistant', + content: `❌ 请求失败:${e?.message ?? String(e)}`, + createdAt: Date.now(), + meta: { error: e?.message ?? String(e) } + }; + setMessages((m) => [...(m || []), errorMessage]); } - setMessages((m) => (m || []).filter((x) => x.id !== tempUser.id)); setStreaming({ active: false, reasoningText: '',