fix(chat): create session and send model for stream

main
sp mac bookpro 2605 2026-05-29 15:48:20 +08:00
parent bd8a494cb2
commit 72dc484f80
4 changed files with 49 additions and 18 deletions

View File

@ -167,16 +167,14 @@ export const ChatAPI = {
agentId: string, agentId: string,
content: string, content: string,
sessionId?: string, sessionId?: string,
overrides?: ModelOverrides, model?: string,
attachmentsText?: string,
imageUrls?: string[] imageUrls?: string[]
) => ) =>
api api
.post<{ user: ChatMessage; assistant: ChatMessage }>(`/chat/${agentId}/messages`, { .post<{ user: ChatMessage; assistant: ChatMessage }>(`/chat/${agentId}/messages`, {
content, content,
sessionId, sessionId,
overrides, model,
attachmentsText,
imageUrls imageUrls
}) })
.then((r) => r.data), .then((r) => r.data),
@ -560,8 +558,7 @@ export async function streamChat(
handlers: StreamEvents, handlers: StreamEvents,
signal?: AbortSignal, signal?: AbortSignal,
sessionId?: string, sessionId?: string,
overrides?: ModelOverrides, model?: string,
attachmentsText?: string,
imageUrls?: string[] imageUrls?: string[]
) { ) {
const resp = await fetch(`https://api.hoyidata.com/aura/v1/chat/${agentId}/messages/stream`, { 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({ body: JSON.stringify({
content, content,
sessionId, sessionId,
overrides, model,
attachmentsText, imageUrls: imageUrls ?? []
imageUrls
}), }),
signal, signal,
credentials: 'include' credentials: 'include'

View File

@ -4,6 +4,7 @@ import ReactMarkdown from 'react-markdown';
import { import {
Agent, Agent,
ChatMessage, ChatMessage,
SessionAPI,
streamChat, streamChat,
RetrievedSnippet, RetrievedSnippet,
ToolCallTrace ToolCallTrace
@ -26,6 +27,7 @@ export default function ChatPreview({ agent, agentId }: Props) {
const [messages, setMessages] = useState<ChatMessage[]>([]); const [messages, setMessages] = useState<ChatMessage[]>([]);
const [input, setInput] = useState(''); const [input, setInput] = useState('');
const [sending, setSending] = useState(false); const [sending, setSending] = useState(false);
const [sessionId, setSessionId] = useState<string>('');
const [streaming, setStreaming] = useState<StreamingState>({ const [streaming, setStreaming] = useState<StreamingState>({
active: false, active: false,
text: '', text: '',
@ -66,6 +68,13 @@ export default function ChatPreview({ agent, agentId }: Props) {
abortRef.current = ctrl; abortRef.current = ctrl;
try { 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( await streamChat(
agentId, agentId,
text, text,
@ -104,7 +113,9 @@ export default function ChatPreview({ agent, agentId }: Props) {
setStreaming({ active: false, text: '', retrieved: [], toolCalls: [] }); setStreaming({ active: false, text: '', retrieved: [], toolCalls: [] });
} }
}, },
ctrl.signal ctrl.signal,
sid,
model
); );
} catch (e: any) { } catch (e: any) {
if (e?.name !== 'AbortError') { if (e?.name !== 'AbortError') {

View File

@ -67,6 +67,7 @@ export default function ChatPage() {
}); });
const bodyRef = useRef<HTMLDivElement>(null); const bodyRef = useRef<HTMLDivElement>(null);
const abortRef = useRef<AbortController | null>(null); const abortRef = useRef<AbortController | null>(null);
const creatingSessionRef = useRef(false);
// URL 参数 ?session=xxx&msg=yyy // URL 参数 ?session=xxx&msg=yyy
useEffect(() => { useEffect(() => {
@ -139,6 +140,7 @@ export default function ChatPage() {
setAgent(null); setAgent(null);
setMessages([]); setMessages([]);
setOverrides({}); setOverrides({});
setSessionId(null);
return () => abortRef.current?.abort(); return () => abortRef.current?.abort();
} }
loadAgent(); loadAgent();
@ -147,6 +149,17 @@ export default function ChatPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [id]); }, [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(() => { useEffect(() => {
if (!id) return; if (!id) return;
loadMessages(); loadMessages();
@ -180,12 +193,14 @@ export default function ChatPage() {
const ctrl = new AbortController(); const ctrl = new AbortController();
abortRef.current = ctrl; abortRef.current = ctrl;
const model = overrides.model || parseAgentModels(agent?.model)[0] || '';
const attText = buildAttachmentsText(); const attText = buildAttachmentsText();
const content = attText ? `${text}\n\n${attText}` : text;
try { try {
await streamChat( await streamChat(
id, id,
text, content,
{ {
onMeta: (m) => setStreaming((s) => ({ ...s, retrieved: m.retrieved || [] })), onMeta: (m) => setStreaming((s) => ({ ...s, retrieved: m.retrieved || [] })),
onDelta: (chunk) => onDelta: (chunk) =>
@ -238,9 +253,8 @@ export default function ChatPage() {
}, },
ctrl.signal, ctrl.signal,
sessionId, sessionId,
overrides, model,
attText || undefined, imageUrls
imageUrls.length > 0 ? imageUrls : undefined
); );
} catch (e: any) { } catch (e: any) {
if (e?.name !== 'AbortError') { if (e?.name !== 'AbortError') {
@ -262,14 +276,15 @@ export default function ChatPage() {
setMessages((m) => [...m, tempUser]); setMessages((m) => [...m, tempUser]);
scrollBottom(); scrollBottom();
const attText = buildAttachmentsText(); const attText = buildAttachmentsText();
const content = attText ? `${text}\n\n${attText}` : text;
const model = overrides.model || parseAgentModels(agent?.model)[0] || '';
try { try {
const res = await ChatAPI.send( const res = await ChatAPI.send(
id, id,
text, content,
sessionId, sessionId,
overrides, model,
attText || undefined, imageUrls
imageUrls.length > 0 ? imageUrls : undefined
); );
setMessages((m) => [...m.filter((x) => x.id !== tempUser.id), res.user, res.assistant]); setMessages((m) => [...m.filter((x) => x.id !== tempUser.id), res.user, res.assistant]);
setSessionRefresh((t) => t + 1); setSessionRefresh((t) => t + 1);

View File

@ -776,6 +776,15 @@ body {
color: var(--color-text) !important; 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 { .monica-editor-column {
height: 100%; height: 100%;
overflow-y: auto; overflow-y: auto;