diff --git a/apps/desktop/src/commands.rs b/apps/desktop/src/commands.rs index 2dfa5ee..2c184dd 100644 --- a/apps/desktop/src/commands.rs +++ b/apps/desktop/src/commands.rs @@ -458,12 +458,12 @@ pub async fn chat_send( app: AppHandle, state: State<'_, App>, payload: ChatSendPayload, -) -> Result<(), String> { +) -> Result { let core = state.core.clone(); let engine = state.engine.clone(); let base = state.engine_base.clone(); - let (history, model, engine_bin) = { + let (history, model, engine_bin, user_message_id) = { let db = core.db.lock().unwrap(); if sessions_db::get_conversation(&db, &payload.conversation_id) .map_err(|e| e.to_string())? @@ -479,7 +479,7 @@ pub async fn chat_send( ) .map_err(|e| e.to_string())?; } - sessions_db::add_message( + let user_message_id = sessions_db::add_message( &db, &payload.conversation_id, "user", @@ -508,7 +508,7 @@ pub async fn chat_send( let bin = settings_db::get(&db, "engine_bin") .map_err(|e| e.to_string())? .unwrap_or_default(); - (history, model, bin) + (history, model, bin, user_message_id) }; let conversation_id = payload.conversation_id.clone(); @@ -523,13 +523,16 @@ pub async fn chat_send( core, engine, base, - conversation_id, + conversation_id.clone(), history, model, engine_bin, payload.params, )); - Ok(()) + Ok(serde_json::json!({ + "conversation_id": conversation_id, + "message_id": user_message_id, + })) } /// 统一的生成与流式转发:支持本地引擎 / 远程 API,统计首字延迟、token 数与耗时。 diff --git a/ui/src/api.ts b/ui/src/api.ts index bf44df6..a1712b6 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -188,7 +188,8 @@ export const api = { invoke("deploy_model", { modelId, params }), engineStop: () => invoke("engine_stop"), engineStatus: () => invoke("engine_status"), - chatSend: (payload: ChatSendPayload) => invoke("chat_send", { payload }), + chatSend: (payload: ChatSendPayload) => + invoke<{ conversation_id: string; message_id: string }>("chat_send", { payload }), regenerateMessage: ( conversationId: string, messageId: string, diff --git a/ui/src/pages/ChatPage.tsx b/ui/src/pages/ChatPage.tsx index 7798f06..a6c39d3 100644 --- a/ui/src/pages/ChatPage.tsx +++ b/ui/src/pages/ChatPage.tsx @@ -219,21 +219,27 @@ export default function ChatPage() { await refreshConversations(); } + const tempUserId = `user-${Date.now()}`; setMessages((prev) => [ ...prev, - { id: `user-${Date.now()}`, role: "user", content, images }, + { id: tempUserId, role: "user", content, images }, { id: `assistant-${Date.now()}`, role: "assistant", content: "", streaming: true }, ]); setStreaming(true); try { - await api.chatSend({ + const result = await api.chatSend({ conversation_id: convId, model_id: selectedModelId, content, params, images, }); + if (result?.message_id) { + setMessages((prev) => + prev.map((m) => (m.id === tempUserId ? { ...m, id: result.message_id } : m)), + ); + } } catch (e) { setStreaming(false); setError(String(e));