World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Remember the entity a chat turn read, not every candidate of an ambiguous name (#933)
Maya Kidfa4c4a1ffd43
diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rsindex 0104e36a..31af51ca 100644--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs@@ -370,6 +370,8 @@ mod history_tests;mod persistence_tests;#[path = "chat_registry_tests.rs"]mod registry_tests;+#[path = "chat_resolved_tests.rs"]+mod resolved_tests;#[path = "chat_sources_tests.rs"]mod sources_tests;diff --git a/crates/utopia-server/src/api/chat_resolved_tests.rs b/crates/utopia-server/src/api/chat_resolved_tests.rsnew file mode 100644index 00000000..9ff1d320--- /dev/null+++ b/crates/utopia-server/src/api/chat_resolved_tests.rs@@ -0,0 +1,145 @@+//! What a turn records as "already identified". The next turn is told to use those ids+//! directly and not to look the names up again, so the record must hold the entities the+//! turn read, or the one a search clearly pointed at, and never the unchosen candidates of+//! an ambiguous name.+use super::*;++/// A tool call's arguments, built at run time: `Reply::Tool` takes static text+fn args(value: serde_json::Value) -> &'static str {+ Box::leak(value.to_string().into_boxed_str())+}++async fn entity(f: &Fx, id: Uuid, ty: Uuid, name: &str) -> anyhow::Result<()> {+ sqlx::query("INSERT INTO entities(id,kb_id,type_id,canonical_name) VALUES ($1,$2,$3,$4)")+ .bind(id)+ .bind(f.kb)+ .bind(ty)+ .bind(name)+ .execute(&f.pool)+ .await?;+ Ok(())+}++async fn entity_type(f: &Fx, key: &str, label: &str) -> anyhow::Result<Uuid> {+ let id = Uuid::now_v7();+ sqlx::query("INSERT INTO entity_types(id,kb_id,key,label) VALUES ($1,$2,$3,$4)")+ .bind(id)+ .bind(f.kb)+ .bind(key)+ .bind(label)+ .execute(&f.pool)+ .await?;+ Ok(id)+}++/// The ids each stored answer recorded, oldest answer first+async fn recorded(f: &Fx) -> anyhow::Result<Vec<Vec<String>>> {+ let rows: Vec<serde_json::Value> = sqlx::query_scalar(+ "SELECT m.resolved FROM conversation_messages m+ JOIN conversations c ON c.id = m.conversation_id+ WHERE c.kb_id = $1 AND m.role = 'assistant'+ ORDER BY m.created_at",+ )+ .bind(f.kb)+ .fetch_all(&f.pool)+ .await?;+ Ok(rows+ .iter()+ .map(|r| {+ r.as_array()+ .into_iter()+ .flatten()+ .filter_map(|e| e["id"].as_str().map(str::to_string))+ .collect()+ })+ .collect())+}++#[tokio::test]+async fn a_turn_remembers_the_entity_it_read_not_every_candidate() -> anyhow::Result<()> {+ let (first, second) = (Uuid::now_v7(), Uuid::now_v7());+ let Some(f) = fixture(Scripted::new(vec![+ Reply::Tool("find_entities", r#"{"name":"Zhang Wei"}"#),+ Reply::Tool("entity_facts", args(json!({ "entity_id": second }))),+ Reply::Text("The Zhang Wei who joined in 2021."),+ Reply::Tool("no_evidence_needed", r#"{"reason":"follow-up"}"#),+ Reply::Text("Follow-up."),+ ]))+ .await?+ else {+ return Ok(());+ };+ let person = entity_type(&f, "person", "Person").await?;+ entity(&f, first, person, "Zhang Wei").await?;+ entity(&f, second, person, "Zhang Wei").await?;++ let sse = f.ask("Which Zhang Wei joined in 2021?").await?;+ assert!(sse.contains("event: done"), "{sse}");+ assert_eq!(+ recorded(&f).await?,+ vec![vec![second.to_string()]],+ "the search's candidates are not identified; the one read by id is"+ );++ let id: Uuid = sqlx::query_scalar("SELECT id FROM conversations WHERE kb_id = $1")+ .bind(f.kb)+ .fetch_one(&f.pool)+ .await?;+ let follow = chat(+ State(f.state.clone()),+ AuthUser(f.user.clone()),+ Path(f.kb),+ Json(ChatReq {+ conversation_id: Some(id),+ message: "When did he join?".into(),+ }),+ )+ .await+ .map_err(|_| anyhow::anyhow!("chat handler refused the follow-up"))?;+ let _ = axum::body::to_bytes(follow.into_response().into_body(), 4 * 1024 * 1024).await?;+ let requests = f.requests();+ let told = requests[3]["messages"]+ .as_array()+ .expect("messages")+ .iter()+ .filter_map(|m| m["content"].as_str())+ .find(|c| c.starts_with("Entities already identified"))+ .expect("the next turn is told which entity was identified")+ .to_string();+ assert!(told.contains(&second.to_string()), "{told}");+ assert!(+ !told.contains(&first.to_string()),+ "the unchosen namesake must not be handed to the next turn: {told}"+ );+ f.cleanup().await+}++#[tokio::test]+async fn a_search_remembers_only_a_clear_best_match() -> anyhow::Result<()> {+ let Some(f) = fixture(Scripted::new(vec![+ Reply::Tool("find_entities", r#"{"name":"Acme"}"#),+ Reply::Text("Acme is an organization."),+ Reply::Tool("find_entities", r#"{"name":"Zhang Wei"}"#),+ Reply::Text("Which Zhang Wei do you mean?"),+ ]))+ .await?+ else {+ return Ok(());+ };+ let org = entity_type(&f, "organization", "Organization").await?;+ let person = entity_type(&f, "person", "Person").await?;+ let acme = Uuid::now_v7();+ entity(&f, acme, org, "Acme").await?;+ entity(&f, Uuid::now_v7(), org, "Acme Labs").await?;+ entity(&f, Uuid::now_v7(), person, "Zhang Wei").await?;+ entity(&f, Uuid::now_v7(), person, "Zhang Wei").await?;++ // The exact name is the best match the search reports, so it is the one identified+ let sse = f.ask("What is Acme?").await?;+ assert!(sse.contains("event: done"), "{sse}");+ // Two namesakes and no choice made: nothing is identified yet+ let sse = f.ask("Tell me about Zhang Wei.").await?;+ assert!(sse.contains("event: done"), "{sse}");+ assert_eq!(recorded(&f).await?, vec![vec![acme.to_string()], vec![]]);+ f.cleanup().await+}diff --git a/crates/utopia-server/src/api/tools_graph.rs b/crates/utopia-server/src/api/tools_graph.rsindex 483b8ccd..13f82e7c 100644--- a/crates/utopia-server/src/api/tools_graph.rs+++ b/crates/utopia-server/src/api/tools_graph.rs@@ -80,9 +80,15 @@ fn node_line(n: &GraphNode) -> String {)}+/// 记下这一轮认下的实体。下一轮会被告知「直接用这些 id,别再按名字查」,所以这里+/// 只能是真的读过、或者搜索明确指向的那个——同一轮先按名字认、再按 id 读,只记一次fn remember(sink: &mut ToolSink, n: &GraphNode) {+ let id = n.id.to_string();+ if sink.resolved.iter().any(|e| e["id"] == id.as_str()) {+ return;+ }sink.resolved.push(json!({- "id": n.id.to_string(), "name": n.name, "type": n.type_label+ "id": id, "name": n.name, "type": n.type_label}));}@@ -209,9 +215,10 @@ pub async fn find_entities(ctx: &ToolCtx<'_>, sink: &mut ToolSink, args: &Value)}};let (ranked, by_question) = rank_by_question(ctx, rank(hits, &name), &name).await;+ let clear = by_question || dominant(&ranked, &name);let text = if ranked.is_empty() {"No matching entities.".to_string()- } else if by_question || dominant(&ranked, &name) {+ } else if clear {let mut lines = vec![format!("Best match: {}", node_line(&ranked[0]))];if ranked.len() > 1 {lines.push("Other matches:".to_string());@@ -225,8 +232,12 @@ pub async fn find_entities(ctx: &ToolCtx<'_>, sink: &mut ToolSink, args: &Value)lines.extend(ranked.iter().map(node_line));lines.join("\n")};- for n in &ranked {- remember(sink, n);+ // 同名歧义时候选只是搜索结果,不是认下的实体。从前这里全记,下一轮就收到一串+ // 同名的 id 和一句「直接用这些」,没选的那个还常常排在前面。选中的那个在它被+ // 读的时候记(entity_facts / neighbors / timeline);上一轮的候选另有工具往返+ // 的回放可查+ if let Some(best) = ranked.first().filter(|_| clear) {+ remember(sink, best);}ToolResult::new(text,@@ -605,6 +616,9 @@ pub async fn entity_facts(ctx: &ToolCtx<'_>, sink: &mut ToolSink, args: &Value).error();}};+ // 按 id 读的也是认下:模型常常先搜到一串同名的,再按 id 读其中一个,读的这个+ // 才是这一轮说的那个(按名字认下的 `resolve` 已记过,这里不会重复)+ remember(sink, &node);// 规则的结论也是这个实体的一部分(0021)。**不给的话模型会拿那些读数自己再判// 一遍**——而阈值写在规则里,它看不见,于是两处判断迟早不一致let derived = match@@ -800,6 +814,7 @@ pub async fn neighbors(ctx: &ToolCtx<'_>, sink: &mut ToolSink, args: &Value) ->.error();}};+ remember(sink, &node);// 邻居是对端**实体**;属性值不算邻居,entity_facts 里有let (matched, aligned) = filtered(ctx, &facts, &filter).await;let linked: Vec<&EntityFact> = matched@@ -926,6 +941,7 @@ pub async fn timeline(ctx: &ToolCtx<'_>, sink: &mut ToolSink, args: &Value) -> T.error();}};+ remember(sink, &node);// 只要**说出了世界时间**的事实。没日期的那些起点是摄取时刻,排进时间线只会// 把一篇文章的日期当成事件的日期let (matched, aligned) = filtered(ctx, &facts, &filter).await;