Skip to content

World's first open-source enterprise world model.

The model sees an attribute's value, not a question mark (#349)

WaylandYang791b3de459c6
diff --git a/crates/utopia-server/src/api/tools.rs b/crates/utopia-server/src/api/tools.rs
index 6da762d7..45c46755 100644
--- a/crates/utopia-server/src/api/tools.rs
+++ b/crates/utopia-server/src/api/tools.rs
@@ -539,7 +539,19 @@ async fn run_query(state: &AppState, ds_id: Uuid, sql: &str) -> anyhow::Result<S
/// 事实行:"works at → 星云科技 (2023-08 → now) [90%]",in 方向用 ←。
fn fact_line(f: &EntityFact) -> String {
- let other = f.other_name.as_deref().unwrap_or("?");
+ // 属性事实没有对端实体,值在 `object_value` 里(0004)。从前这里只看 `other_name`,
+ // 于是薪资、职位到了模型眼前是 `salary → ?`——区间和置信度都在,唯独值没到,
+ // 模型只能说"没有薪资信息"(#348)。渲染规则与客户端 `fmtObjectValue` 一致
+ let literal = f
+ .object_value
+ .as_ref()
+ .and_then(literal_text)
+ .filter(|_| f.other_name.is_none());
+ let other = f
+ .other_name
+ .as_deref()
+ .or(literal.as_deref())
+ .unwrap_or("?");
// 本体没认下、原文说法也没留下时用 "?"——与 other 同一个约定。
// 不编一个"相关"出来:那正是删掉 related_to 要消灭的东西
let pred = f.predicate_label.as_deref().unwrap_or("?");
@@ -559,6 +571,42 @@ fn fact_line(f: &EntityFact) -> String {
format!("{core}{range} [{}%]", (f.confidence * 100.0).round() as i32)
}
+/// 字面值宾语给模型看的样子:`{value, unit}` → "28000 CNY",布尔 → ✓/✗,
+/// 映射那类 `{summary}` → 摘要本身。与 `web/src/pages/Graph.tsx::fmtObjectValue` 同一条规则,
+/// 两边分叉的话,人看到的和模型看到的就不是同一个值
+fn literal_text(v: &serde_json::Value) -> Option<String> {
+ // 裸标量(`changes` 那头的旧数据长这样):字符串读成它自己,不带引号
+ match v {
+ serde_json::Value::Null => return None,
+ serde_json::Value::String(s) => return Some(s.clone()),
+ serde_json::Value::Bool(_) | serde_json::Value::Number(_) => return Some(v.to_string()),
+ _ => {}
+ }
+ if let Some(val) = v.get("value") {
+ let text = match val {
+ serde_json::Value::Bool(true) => "✓".to_string(),
+ serde_json::Value::Bool(false) => "✗".to_string(),
+ serde_json::Value::String(s) => s.clone(),
+ serde_json::Value::Null => return None,
+ other => other.to_string(),
+ };
+ return Some(
+ match v
+ .get("unit")
+ .and_then(|u| u.as_str())
+ .filter(|u| !u.is_empty())
+ {
+ Some(unit) => format!("{text} {unit}"),
+ None => text,
+ },
+ );
+ }
+ if let Some(summary) = v.get("summary").and_then(|s| s.as_str()) {
+ return Some(summary.to_string());
+ }
+ Some(v.to_string())
+}
+
/// changes 的时间窗:把两个可选日期变成 (SQL 用的半开区间, 展示用的窗口串)。
///
/// **抽成纯函数是因为这里出过一次错。** `until` 进 SQL 前要加一天(说"到 3 月 31 日
@@ -600,12 +648,11 @@ fn changes_window(
/// 若把它们排成一串日期,模型会把"2026 年记下的"读成"2026 年发生的"——那正是
/// 这个工具要防的误读。
fn change_line(c: &GraphChange) -> String {
+ // 字面值宾语与 `fact_line` 同一条规则(`literal_text`):两个工具看到的
+ // 不能是两种写法,否则 `{value, unit}` 在这里会印成一段 JSON
let object = match (&c.object_name, &c.object_value) {
(Some(name), _) => name.clone(),
- (None, Some(v)) => match v {
- serde_json::Value::String(s) => s.clone(),
- other => other.to_string(),
- },
+ (None, Some(v)) => literal_text(v).unwrap_or_else(|| "?".to_string()),
(None, None) => "?".to_string(),
};
let range = match (&c.valid_from, &c.valid_to) {
@@ -719,6 +766,57 @@ mod tests {
}
}
+ fn attribute_fact(value: serde_json::Value) -> EntityFact {
+ EntityFact {
+ id: Uuid::nil(),
+ direction: "out".into(),
+ predicate_key: Some("salary".into()),
+ predicate_label: Some("salary".into()),
+ inferred: false,
+ temporal: Some("state".into()),
+ other_id: None,
+ other_name: None,
+ object_value: Some(value),
+ valid_from: Some(t("2023-06-01T00:00:00Z")),
+ valid_to: Some(t("2024-02-20T00:00:00Z")),
+ valid_from_precision: Some("day".into()),
+ valid_to_precision: Some("day".into()),
+ confidence: 0.9,
+ evidence_count: 1,
+ stale: false,
+ corrected: false,
+ last_evidence_time: None,
+ contested: None,
+ }
+ }
+
+ /// 属性事实的值要到模型眼前(#348)。从前这里是 `salary → ? (2023-06-01 → 2024-02-20)`:
+ /// 区间和置信度都在,唯独值没到,模型只能说"没有薪资信息"——而账本里明明有
+ #[test]
+ fn an_attribute_fact_shows_the_model_its_value() {
+ let line = fact_line(&attribute_fact(
+ serde_json::json!({ "value": 28000, "unit": "CNY" }),
+ ));
+ assert!(line.starts_with("salary → 28000 CNY"), "{line}");
+ assert!(line.contains("(2023-06-01 → 2024-02-20)"), "{line}");
+
+ // 与客户端 fmtObjectValue 同一条规则:布尔画成 ✓/✗,映射摘要读摘要本身
+ let flag = fact_line(&attribute_fact(serde_json::json!({ "value": true })));
+ assert!(flag.starts_with("salary → ✓"), "{flag}");
+ let mapped = fact_line(&attribute_fact(
+ serde_json::json!({ "summary": "orders.total" }),
+ ));
+ assert!(mapped.starts_with("salary → orders.total"), "{mapped}");
+ }
+
+ /// 本体没接住的关系仍然是 "?":那个问号是给「没有谓词」留的,不是给「有值」用的
+ #[test]
+ fn a_missing_object_still_reads_as_a_question_mark() {
+ let mut f = attribute_fact(serde_json::json!(null));
+ f.object_value = None;
+ assert!(fact_line(&f).starts_with("salary → ?"), "{}", fact_line(&f));
+ }
+
/// 字面值要读成它自己。走 `Value::to_string()` 会把字符串连引号一起印出来,
/// 模型于是把 `"1993"` 当成答案的一部分
#[test]