World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
An empty ontology says what the documents still need, and no longer claims to cover them (#1083)
WaylandYang51d22df7a794
diff --git a/crates/utopia-server/src/api/question_routes.rs b/crates/utopia-server/src/api/question_routes.rsindex ca4fcc9c..2502e724 100644--- a/crates/utopia-server/src/api/question_routes.rs+++ b/crates/utopia-server/src/api/question_routes.rs@@ -188,7 +188,8 @@ pub async fn record_result(Ok(Json(json!({ "ok": true })))}-/// 两个数(决定 5):问题答对了几条;代理的提案人改过或拒掉的占几成+/// 两个数(决定 5):问题答对了几条;代理的提案人改过或拒掉的占几成。另带文档说了、+/// 本体还放不下的类别词与短语形状各有多少,工作台靠它说本体还缺什么pub async fn report(State(state): State<AppState>,AuthUser(user): AuthUser,@@ -197,10 +198,12 @@ pub async fn report(utopia_store::access::require_kb(&state.pool, &user, kb_id, Role::Viewer).await?;let q = questions::report(&state.pool, kb_id).await?;let p = utopia_store::ontology::agent_proposal_report(&state.pool, kb_id).await?;+ let uncovered = utopia_store::ontology::uncovered(&state.pool, kb_id).await?;let decided = p.adopted + p.rejected;let changed = p.adopted_edited + p.rejected;Ok(Json(json!({"questions": q,+ "uncovered": uncovered,"proposals": {"open": p.open,"adopted": p.adopted,diff --git a/crates/utopia-store/src/ontology.rs b/crates/utopia-store/src/ontology.rsindex 07d9e105..c54d49fd 100644--- a/crates/utopia-store/src/ontology.rs+++ b/crates/utopia-store/src/ontology.rs@@ -2497,6 +2497,27 @@ pub struct AgentProposalReport {pub rejected: i64,}+/// 文档说了、本体还放不下的:没绑到类的类别词,和没绑到属性的短语形状(判成 none 的+/// 与两票不一致的)。本体代理读的就是这两样(0061 决定 2);工作台拿这两个数说「还缺什么」,+/// 不再拿 0003 的 `ontology_misses`——开放图谱的抽取不往那张表里写+#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::FromRow, serde::Serialize)]+pub struct Uncovered {+ pub kind_words: i64,+ pub phrases: i64,+}++pub async fn uncovered(pool: &PgPool, kb_id: Uuid) -> AppResult<Uncovered> {+ Ok(sqlx::query_as(+ "SELECT (SELECT count(*) FROM type_bindings+ WHERE kb_id = $1 AND status <> 'bound') AS kind_words,+ (SELECT count(*) FROM phrase_bindings+ WHERE kb_id = $1 AND status <> 'bound') AS phrases",+ )+ .bind(kb_id)+ .fetch_one(pool)+ .await?)+}+pub async fn agent_proposal_report(pool: &PgPool, kb_id: Uuid) -> AppResult<AgentProposalReport> {Ok(sqlx::query_as("SELECT count(*) FILTER (WHERE status = 'open') AS open,diff --git a/crates/utopia-store/tests/store/an_empty_ontology_does_not_cover_the_corpus.rs b/crates/utopia-store/tests/store/an_empty_ontology_does_not_cover_the_corpus.rsnew file mode 100644index 00000000..b8787100--- /dev/null+++ b/crates/utopia-store/tests/store/an_empty_ontology_does_not_cover_the_corpus.rs@@ -0,0 +1,98 @@+//! 文档说了、本体还放不下的有多少:没绑到类的类别词、没绑到属性的短语形状。+//!+//! 工作台从前拿 0003 的 `ontology_misses` 说「本体覆盖了语料」,而开放图谱的抽取不往那张表+//! 里写:一个一个类都没有的库,二十二个类别词没着落,页面上写的是「覆盖了」。这两个数是+//! 本体代理读的那两样(0061 决定 2),别的库的不算。+//!+//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。++use sqlx::PgPool;+use utopia_store::ontology::{uncovered, Uncovered};+use uuid::Uuid;++const ORG: &str = "empty-ontology-does-not-cover-test";++#[tokio::test]+async fn kind_words_and_phrases_with_no_place_are_counted_per_base() -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ sqlx::query("DELETE FROM organizations WHERE name = $1")+ .bind(ORG)+ .execute(&pool)+ .await?;+ let (org, ws, kb, other, person, leads) = (+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ );+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations(id,name) VALUES ('{org}','{ORG}');+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','{ORG}');+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','a'), ('{other}','{ws}','b');"+ ))+ .execute(&pool)+ .await?;++ let run = async {+ // 什么都还没读过:没有要等的+ let none = Uncovered {+ kind_words: 0,+ phrases: 0,+ };+ assert_eq!(uncovered(&pool, kb).await?, none);++ // 读过文档、一个类都没有:每个类别词都没着落+ for word in ["person", "project", "city"] {+ sqlx::query(+ "INSERT INTO type_bindings (id, kb_id, kind_word, status) VALUES ($1, $2, $3, 'none')",+ )+ .bind(Uuid::now_v7())+ .bind(kb)+ .bind(word)+ .execute(&pool)+ .await?;+ }+ assert_eq!(uncovered(&pool, kb).await?.kind_words, 3);++ // 加了一个类、一条属性:绑上的不算,判成 none 的和两票不一致的都算+ sqlx::raw_sql(&format!(+ "INSERT INTO entity_types(id,kb_id,key,label) VALUES ('{person}','{kb}','person','person');+ INSERT INTO relation_types(id,kb_id,key,label,kind,temporal,description)+ VALUES ('{leads}','{kb}','leads','leads','relation','state','');+ UPDATE type_bindings SET status='bound', type_id='{person}'+ WHERE kb_id='{kb}' AND kind_word='person';+ UPDATE type_bindings SET status='undecided' WHERE kb_id='{kb}' AND kind_word='city';+ INSERT INTO phrase_bindings (id, kb_id, phrase, subject_type_id, status, relation_type_id, direction)+ VALUES ('{}','{kb}','负责','{person}','bound','{leads}','forward'),+ ('{}','{kb}','汇报给','{person}','none',NULL,NULL),+ ('{}','{kb}','调任','{person}','undecided',NULL,NULL);+ INSERT INTO type_bindings (id, kb_id, kind_word, status) VALUES ('{}','{other}','supplier','none');",+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ Uuid::now_v7(),+ ))+ .execute(&pool)+ .await?;+ assert_eq!(+ uncovered(&pool, kb).await?,+ Uncovered {+ kind_words: 2,+ phrases: 2,+ }+ );+ assert_eq!(uncovered(&pool, other).await?.kind_words, 1, "each base counts its own");+ anyhow::Ok(())+ }+ .await;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(org)+ .execute(&pool)+ .await?;+ run+}diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rsindex ddff6a60..3f386de1 100644--- a/crates/utopia-store/tests/store/main.rs+++ b/crates/utopia-store/tests/store/main.rs@@ -105,6 +105,7 @@ mod an_amount_outlives_adoption;mod an_automatic_merge_is_gated_by_what_it_can_undo;mod an_axiom_that_moved_closes_its_findings;mod an_earlier_mention_keeps_the_stated_end;+mod an_empty_ontology_does_not_cover_the_corpus;mod an_end_date_closes_the_open_span;mod an_event_holds_at_the_moment_it_names;mod an_exploration_says_what_it_covered;diff --git a/web/src/api.ts b/web/src/api.tsindex eaf8c9a1..39413a71 100644--- a/web/src/api.ts+++ b/web/src/api.ts@@ -1289,6 +1289,8 @@ export interface QuestionReport {changed: number;changed_share: number | null;};+ /** 文档说了、本体还放不下的:没有类的类别词、没绑到属性的短语形状(代理读的就是这两样) */+ uncovered: { kind_words: number; phrases: number };}export interface OntologyMiss {diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.tsindex 20394dad..5fe3fabc 100644--- a/web/src/i18n/en.ts+++ b/web/src/i18n/en.ts@@ -1692,8 +1692,8 @@ export const en = {importBy: (who: string, when: string) => `${who} · ${when}`,importSize: (bytes: number) =>bytes < 1024 ? `${bytes} B` : `${(bytes / 1024).toFixed(0)} KB`,- misses: "Unmatched from extraction",- missesHint: "Types and predicates the extractor produced outside the ontology, to judge what the ontology still lacks.",+ misses: "Not in the ontology yet",+ missesHint: "What the documents say that the ontology has no place for, and what the agent proposes for it.",/* ---- 一端挂着两个以上开放值的谓词(#341) ----文案克制:状态一句话,后果一句话,动作在按钮上。这一档的读者要判断的是「这条关系一次只能有一个值吗」,不是读一篇关于双时态的说明 */@@ -1729,7 +1729,16 @@ export const en = {restore: "Restore",suggest: "Suggest with AI",suggesting: "Analyzing…",- noMisses: "No unmatched types — the ontology covers your corpus.",+ noMisses: "Nothing the documents say is waiting for a place in the ontology.",+ uncovered: (kindWords: number, phrases: number) => {+ const what = [+ kindWords > 0 ? `${kindWords} ${kindWords === 1 ? "kind of thing" : "kinds of thing"}` : "",+ phrases > 0 ? `${phrases} relation ${phrases === 1 ? "phrase" : "phrases"}` : "",+ ]+ .filter(Boolean)+ .join(" and ");+ return `The documents use ${what} the ontology has no place for. Ask the agent to propose classes and properties for them; nothing is added until you accept it.`;+ },approve: "Add",/* 映射那一档的按钮。刻意不叫 Add——它不加东西,本体里已经有了。两个按钮都写 Add 的话,"已经有了"这件事在界面上就消失了 */@@ -1828,7 +1837,9 @@ export const en = {/* 从前这句把「先加个类或导入 OWL 文件」说成了开始的前提,而本体本来就从语料里长(0003,默认开)——那句话正是 #313 说的劝退点。现在只说状态,动作留给左栏本来就有的 New class 与 Import */- schemaEmpty: "No classes yet. Extraction adds them as documents arrive.",+ schemaEmpty:+ "No classes yet. Documents are read without them; classes and properties are what turn what was read into typed facts. Have the agent propose them from your documents, import an ontology, or add a class yourself.",+ schemaEmptyAction: "See what the documents need",schemaFitView: "Fit view",schemaZoomIn: "Zoom in",schemaZoomOut: "Zoom out",diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.tsindex c530be00..8dd843e8 100644--- a/web/src/i18n/zh.ts+++ b/web/src/i18n/zh.ts@@ -1489,8 +1489,8 @@ export const zh: Strings = {importBy: (who: string, when: string) => `${who} · ${when}`,importSize: (bytes: number) =>bytes < 1024 ? `${bytes} B` : `${(bytes / 1024).toFixed(0)} KB`,- misses: "抽取中未匹配的",- missesHint: "抽取时落在本体之外的类型和谓词,用来判断本体还缺什么。",+ misses: "本体里还没有的",+ missesHint: "文档说到了、本体还放不下的东西,以及代理为它们提的建议。",/* 一端挂着两个以上开放值的谓词(#341):状态一句话,后果一句话,动作在按钮上 */uniqueness: "并存的取值",uniquenessHint: "标明哪些关系同时只能有一个值,之后新值会自动结束旧值。",@@ -1513,7 +1513,16 @@ export const zh: Strings = {restore: "撤回忽略",suggest: "用 AI 建议",suggesting: "分析中…",- noMisses: "没有未匹配的类型——本体覆盖了你的语料。",+ noMisses: "文档说到的东西里,没有在等本体给位置的。",+ uncovered: (kindWords: number, phrases: number) => {+ const what = [+ kindWords > 0 ? `${kindWords} 种东西` : "",+ phrases > 0 ? `${phrases} 种关系说法` : "",+ ]+ .filter(Boolean)+ .join("、");+ return `文档里有 ${what},本体里还没有位置。可以让代理为它们提出类和属性;你采纳之前什么都不会加进本体。`;+ },approve: "加入",mapOver: "用已有的",willRemap: (n: number) => `将重新归类 ${n} 条事实`,@@ -1585,7 +1594,9 @@ export const zh: Strings = {proposalsChangedHint: "代理的提案里人表过态的,有几条被拒或改过再采纳。",/* ---- 模式图 ---- */schemaDiagram: "模式图",- schemaEmpty: "还没有类。文档进来时会自动补上。",+ schemaEmpty:+ "还没有类。没有类文档也照常读;类和属性是把读到的内容变成类型化事实的那一步。可以让代理按你的文档提出来、导入一份本体,或者自己加一个类。",+ schemaEmptyAction: "看看文档需要什么",schemaFitView: "归位",schemaZoomIn: "放大",schemaZoomOut: "缩小",diff --git a/web/src/pages/Ontology.tsx b/web/src/pages/Ontology.tsxindex 38cb3a7f..d5fa0900 100644--- a/web/src/pages/Ontology.tsx+++ b/web/src/pages/Ontology.tsx@@ -225,8 +225,11 @@ export function Ontology() {enabled: !!kb,});- const refresh = () =>+ const refresh = () => {queryClient.invalidateQueries({ queryKey: ["ontology", kb?.id] });+ // 采纳、拒绝之后「本体还缺什么」的那两个数也变了+ queryClient.invalidateQueries({ queryKey: ["questionReport", kb?.id] });+ };// 错误统一走全局 toast,不再用页面内嵌错误行const onError = (e: unknown) => toast.error((e as Error).message);@@ -576,6 +579,7 @@ export function Ontology() {) : (<OntologySchemaGraphloading={loading}+ onEmptyAction={() => setSel({ kind: "misses" })}entityTypes={entity_types}relationTypes={relation_types}rules={rules.data?.rules ?? []}@@ -2126,6 +2130,11 @@ function MissesPanel({onChanged: () => void;onError: (e: unknown) => void;}) {+ // 文档说了、本体还放不下的有多少(代理读的那两样):空着的本体不等于「覆盖了语料」+ const uncovered = useQuery({+ queryKey: ["questionReport", kbId],+ queryFn: () => api.questionReport(kbId),+ }).data?.uncovered;// 默认收起:已忽略的是**背景信息**,不该跟待处理的挤在一起抢注意力const [showDismissed, setShowDismissed] = useState(false);const [proposals, setProposals] = useState<OntologyProposals | null>(null);@@ -2574,7 +2583,14 @@ function MissesPanel({/>{misses.length === 0 ? (- <p className="text-body text-ink-2">{S.ontology.noMisses}</p>+ // 两个数还没取回来时不说话:先说一句「没有在等的」再改口,比空着更糟+ uncovered && (+ <p className="text-body text-ink-2">+ {uncovered.kind_words + uncovered.phrases > 0+ ? S.ontology.uncovered(uncovered.kind_words, uncovered.phrases)+ : S.ontology.noMisses}+ </p>+ )) : (<div className="flex flex-wrap gap-2">{misses.map((m) => (diff --git a/web/src/pages/OntologySchemaGraph.tsx b/web/src/pages/OntologySchemaGraph.tsxindex dcbc2e18..d2e69827 100644--- a/web/src/pages/OntologySchemaGraph.tsx+++ b/web/src/pages/OntologySchemaGraph.tsx@@ -78,6 +78,7 @@ import { Maximize2, ZoomIn, ZoomOut } from "lucide-react";import type { BusinessRule, EntityTypeView, RelationTypeView } from "../api";import { S } from "../i18n";import {+ Button,CanvasLoading,cn,Pill,@@ -582,7 +583,10 @@ export function OntologySchemaGraph({selected,onSelect,loading = false,+ onEmptyAction,}: {+ /** 一个类都没有时那句话下面的按钮:带人去看文档说了什么、本体还缺什么 */+ onEmptyAction?: () => void;entityTypes: EntityTypeView[];/** 业务规则:画成主类 → 结论类的一条紫弧,点它打开规则那一页 */rules?: BusinessRule[];@@ -1138,8 +1142,15 @@ export function OntologySchemaGraph({<CanvasLoading />) : empty ? (<div className="absolute inset-0 grid place-items-center pointer-events-none">- <div className="text-center text-body text-ink-2 max-w-xs">+ <div className="text-center text-body text-ink-2 max-w-sm">{S.ontology.schemaEmpty}+ {onEmptyAction && (+ <div className="mt-3 pointer-events-auto">+ <Button size="sm" variant="secondary" onClick={onEmptyAction}>+ {S.ontology.schemaEmptyAction}+ </Button>+ </div>+ )}</div></div>) : null}