World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Send each chat question to the model once instead of twice (#929)
Maya Kid76f1d894b995
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rsindex d126a22c..e0fdd3b9 100644--- a/crates/utopia-server/src/api/chat.rs+++ b/crates/utopia-server/src/api/chat.rs@@ -608,6 +608,14 @@ pub async fn chat(MAX_HISTORY as i64,).await?;+ // 这一问已经落库,所以它在历史里;runner 又把它当 prompt 发一次。两份都发,+ // 每个请求里就有两条一样的 user,已认下的实体那条 system 也夹到了两份中间+ // (#548 起如此)。按落库 id 剔除,不按文本:同一会话里并发的另一问可能排在+ // 它后面。收尾那一步用同一个位置把当前问题从背景里剔出去+ let current = history+ .turn_ids+ .iter()+ .position(|id| *id == user_message_id);let workspace_id = kb.workspace_id;// 数据描述(探索从 schema 写的)与约定(人写的)跟着进 system prompt。// **每次都在,不靠检索碰运气**:约定写成一页文档只靠检索也到过 14/18,@@ -721,9 +729,16 @@ pub async fn chat(.add_hook(policy).tool_server_handle(tool_server).build();+ let prior: Vec<(String, String)> = history+ .turns+ .iter()+ .enumerate()+ .filter(|(i, _)| Some(*i) != current)+ .map(|(_, turn)| turn.clone())+ .collect();let mut runner = rig_agent.runner(Message::user(query.clone()))- .history(agent::history_messages(&history.turns, &history.last_tool_exchange));+ .history(agent::history_messages(&prior, &history.last_tool_exchange));// 贴在历史之后、当前问题之前——位置就是服从性,跟抽取里 known_block// 紧挨正文是同一条理由(角色与位置由 `rig_model::wire` 定)if let Some(block) = agent::known_entities_block(&history.entities, KNOWN_ENTITY_LIMIT) {@@ -890,7 +905,6 @@ pub async fn chat(let sink = shared.sink.lock().await;(sink.sources.clone(), sink.resolved.clone())};- let current = history.turn_ids.iter().position(|id| *id == user_message_id);let input = finalization::AnswerContext {question: &query, history: &history.turns, current,prior_exchange: &history.last_tool_exchange,diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rsindex 927ddc67..0104e36a 100644--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs@@ -364,6 +364,8 @@ async fn a_reply_that_stays_empty_is_an_error_after_one_retry() -> anyhow::Resul#[path = "chat_fallback_tests.rs"]mod fallback_tests;+#[path = "chat_history_tests.rs"]+mod history_tests;#[path = "chat_persistence_tests.rs"]mod persistence_tests;#[path = "chat_registry_tests.rs"]diff --git a/crates/utopia-server/src/api/chat_history_tests.rs b/crates/utopia-server/src/api/chat_history_tests.rsnew file mode 100644index 00000000..1ae3147c--- /dev/null+++ b/crates/utopia-server/src/api/chat_history_tests.rs@@ -0,0 +1,150 @@+//! What the model is sent for one turn: the stored conversation, then the question once.+//!+//! The question is stored before the history is read, so the history already holds it, and+//! the runner sends the question again as its prompt. Each request the scripted endpoint+//! receives is checked for how often each question appears and what sits right before it.+use super::*;++/// How many user messages in one request say exactly `text`+fn user_turns(request: &serde_json::Value, text: &str) -> usize {+ request["messages"]+ .as_array()+ .into_iter()+ .flatten()+ .filter(|m| m["role"] == "user" && m["content"] == text)+ .count()+}++fn last_user(request: &serde_json::Value) -> String {+ request["messages"]+ .as_array()+ .into_iter()+ .flatten()+ .rev()+ .find(|m| m["role"] == "user")+ .and_then(|m| m["content"].as_str())+ .unwrap_or_default()+ .to_string()+}++/// Ask in an existing conversation and collect the whole SSE body+async fn ask_in(f: &Fx, conversation_id: Uuid, message: &str) -> anyhow::Result<String> {+ let sse = chat(+ State(f.state.clone()),+ AuthUser(f.user.clone()),+ Path(f.kb),+ Json(ChatReq {+ conversation_id: Some(conversation_id),+ message: message.into(),+ }),+ )+ .await+ .map_err(|_| anyhow::anyhow!("chat handler refused the request"))?;+ let body = axum::body::to_bytes(sse.into_response().into_body(), 4 * 1024 * 1024).await?;+ Ok(String::from_utf8_lossy(&body).into_owned())+}++#[tokio::test]+async fn the_question_reaches_the_model_once() -> anyhow::Result<()> {+ let gate = Reply::Tool("no_evidence_needed", r#"{"reason":"greeting"}"#);+ let Some(f) = fixture(Scripted::new(vec![+ gate,+ Reply::Text("First answer."),+ gate,+ Reply::Text("Second answer."),+ ]))+ .await?+ else {+ return Ok(());+ };+ let sse = f.ask("first question").await?;+ assert!(sse.contains("event: done"), "{sse}");+ let id: Uuid = sqlx::query_scalar("SELECT id FROM conversations WHERE kb_id = $1")+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ let sse = ask_in(&f, id, "second question").await?;+ assert!(sse.contains("event: done"), "{sse}");++ let requests = f.requests();+ assert_eq!(requests.len(), 4, "a tool turn and an answer per question");+ for (i, request) in requests.iter().enumerate() {+ let current = if i < 2 {+ "first question"+ } else {+ "second question"+ };+ assert_eq!(+ user_turns(request, current),+ 1,+ "request {i} must carry its question once:\n{request}"+ );+ assert_eq!(last_user(request), current, "request {i}:\n{request}");+ // The earlier question is replayed from the history, and only from there+ assert_eq!(+ user_turns(request, "first question"),+ 1,+ "request {i}:\n{request}"+ );+ }+ f.cleanup().await+}++#[tokio::test]+async fn earlier_entities_sit_right_before_the_question() -> anyhow::Result<()> {+ let Some(f) = fixture(Scripted::new(vec![+ Reply::Tool("no_evidence_needed", r#"{"reason":"follow-up"}"#),+ Reply::Text("Answered."),+ ]))+ .await?+ else {+ return Ok(());+ };+ // An earlier turn resolved an entity; the next turn sends its id as a system message+ let id = utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "Acme").await?;+ utopia_store::conversations::append_message(+ &f.pool,+ id,+ "user",+ "Who founded Acme?",+ &utopia_store::conversations::TurnRecord::empty(),+ )+ .await?;+ utopia_store::conversations::append_message(+ &f.pool,+ id,+ "assistant",+ "Acme was founded by Ada.",+ &utopia_store::conversations::TurnRecord {+ steps: json!([]),+ sources: json!([]),+ resolved: json!([{ "id": Uuid::now_v7(), "name": "Acme", "type": "Organization" }]),+ tool_exchange: json!([]),+ },+ )+ .await?;+ let sse = ask_in(&f, id, "When was it founded?").await?;+ assert!(sse.contains("event: done"), "{sse}");++ let requests = f.requests();+ let first = &requests[0];+ let messages = first["messages"].as_array().expect("messages");+ let block = messages+ .iter()+ .position(|m| {+ m["role"] == "system"+ && m["content"]+ .as_str()+ .is_some_and(|c| c.starts_with("Entities already identified"))+ })+ .expect("the entities resolved earlier are sent");+ assert_eq!(messages[block + 1]["role"], "user", "{first}");+ assert_eq!(+ messages[block + 1]["content"],+ "When was it founded?",+ "{first}"+ );+ assert_eq!(user_turns(first, "When was it founded?"), 1, "{first}");+ assert_eq!(user_turns(first, "Who founded Acme?"), 1, "{first}");+ f.cleanup().await+}