From 72dc484f80016f5c4af318560f010c7aefd7fea9 Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Fri, 29 May 2026 15:48:20 +0800 Subject: [PATCH] fix(chat): create session and send model for stream --- src/api.ts | 14 +++++--------- src/components/ChatPreview.tsx | 13 ++++++++++++- src/pages/ChatPage.tsx | 31 +++++++++++++++++++++++-------- src/styles.css | 9 +++++++++ 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/api.ts b/src/api.ts index 99a635e..68fc734 100644 --- a/src/api.ts +++ b/src/api.ts @@ -167,16 +167,14 @@ export const ChatAPI = { agentId: string, content: string, sessionId?: string, - overrides?: ModelOverrides, - attachmentsText?: string, + model?: string, imageUrls?: string[] ) => api .post<{ user: ChatMessage; assistant: ChatMessage }>(`/chat/${agentId}/messages`, { content, sessionId, - overrides, - attachmentsText, + model, imageUrls }) .then((r) => r.data), @@ -560,8 +558,7 @@ export async function streamChat( handlers: StreamEvents, signal?: AbortSignal, sessionId?: string, - overrides?: ModelOverrides, - attachmentsText?: string, + model?: string, imageUrls?: string[] ) { const resp = await fetch(`https://api.hoyidata.com/aura/v1/chat/${agentId}/messages/stream`, { @@ -570,9 +567,8 @@ export async function streamChat( body: JSON.stringify({ content, sessionId, - overrides, - attachmentsText, - imageUrls + model, + imageUrls: imageUrls ?? [] }), signal, credentials: 'include' diff --git a/src/components/ChatPreview.tsx b/src/components/ChatPreview.tsx index 8b4b89f..0dc0424 100644 --- a/src/components/ChatPreview.tsx +++ b/src/components/ChatPreview.tsx @@ -4,6 +4,7 @@ import ReactMarkdown from 'react-markdown'; import { Agent, ChatMessage, + SessionAPI, streamChat, RetrievedSnippet, ToolCallTrace @@ -26,6 +27,7 @@ export default function ChatPreview({ agent, agentId }: Props) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [sending, setSending] = useState(false); + const [sessionId, setSessionId] = useState(''); const [streaming, setStreaming] = useState({ active: false, text: '', @@ -66,6 +68,13 @@ export default function ChatPreview({ agent, agentId }: Props) { abortRef.current = ctrl; try { + let sid = sessionId; + if (!sid) { + const created = await SessionAPI.create(agentId); + sid = created.id; + setSessionId(sid); + } + const model = String(agent?.model || '').split(',')[0]?.trim() || undefined; await streamChat( agentId, text, @@ -104,7 +113,9 @@ export default function ChatPreview({ agent, agentId }: Props) { setStreaming({ active: false, text: '', retrieved: [], toolCalls: [] }); } }, - ctrl.signal + ctrl.signal, + sid, + model ); } catch (e: any) { if (e?.name !== 'AbortError') { diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx index 6db8a78..f844347 100644 --- a/src/pages/ChatPage.tsx +++ b/src/pages/ChatPage.tsx @@ -67,6 +67,7 @@ export default function ChatPage() { }); const bodyRef = useRef(null); const abortRef = useRef(null); + const creatingSessionRef = useRef(false); // URL 参数 ?session=xxx&msg=yyy useEffect(() => { @@ -139,6 +140,7 @@ export default function ChatPage() { setAgent(null); setMessages([]); setOverrides({}); + setSessionId(null); return () => abortRef.current?.abort(); } loadAgent(); @@ -147,6 +149,17 @@ export default function ChatPage() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [id]); + useEffect(() => { + if (!id || sessionId || creatingSessionRef.current) return; + creatingSessionRef.current = true; + SessionAPI.create(id) + .then((s) => setSessionId(s.id)) + .catch(() => msg.error('创建会话失败')) + .finally(() => { + creatingSessionRef.current = false; + }); + }, [id, msg, sessionId]); + useEffect(() => { if (!id) return; loadMessages(); @@ -180,12 +193,14 @@ export default function ChatPage() { const ctrl = new AbortController(); abortRef.current = ctrl; + const model = overrides.model || parseAgentModels(agent?.model)[0] || ''; const attText = buildAttachmentsText(); + const content = attText ? `${text}\n\n${attText}` : text; try { await streamChat( id, - text, + content, { onMeta: (m) => setStreaming((s) => ({ ...s, retrieved: m.retrieved || [] })), onDelta: (chunk) => @@ -238,9 +253,8 @@ export default function ChatPage() { }, ctrl.signal, sessionId, - overrides, - attText || undefined, - imageUrls.length > 0 ? imageUrls : undefined + model, + imageUrls ); } catch (e: any) { if (e?.name !== 'AbortError') { @@ -262,14 +276,15 @@ export default function ChatPage() { setMessages((m) => [...m, tempUser]); scrollBottom(); const attText = buildAttachmentsText(); + const content = attText ? `${text}\n\n${attText}` : text; + const model = overrides.model || parseAgentModels(agent?.model)[0] || ''; try { const res = await ChatAPI.send( id, - text, + content, sessionId, - overrides, - attText || undefined, - imageUrls.length > 0 ? imageUrls : undefined + model, + imageUrls ); setMessages((m) => [...m.filter((x) => x.id !== tempUser.id), res.user, res.assistant]); setSessionRefresh((t) => t + 1); diff --git a/src/styles.css b/src/styles.css index f753134..abcd3ca 100644 --- a/src/styles.css +++ b/src/styles.css @@ -776,6 +776,15 @@ body { color: var(--color-text) !important; } +.chat-input-textarea:hover, +.chat-input-textarea:focus, +.chat-input-textarea:focus-visible, +.chat-input-textarea:active { + border: none !important; + box-shadow: none !important; + outline: none !important; +} + .monica-editor-column { height: 100%; overflow-y: auto;