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,
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'

View File

@ -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<ChatMessage[]>([]);
const [input, setInput] = useState('');
const [sending, setSending] = useState(false);
const [sessionId, setSessionId] = useState<string>('');
const [streaming, setStreaming] = useState<StreamingState>({
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') {

View File

@ -67,6 +67,7 @@ export default function ChatPage() {
});
const bodyRef = useRef<HTMLDivElement>(null);
const abortRef = useRef<AbortController | null>(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);

View File

@ -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;