Changesets · Branches · Tags · Compare
| Changeset | 8e2e285aa6e87620570ec8feb4976a70049854167cec5509a6b97a736a2f2228 |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-03 01:21 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-09-03 01:21 +08:00 |
| Parents | 752b35df5321 |
24 files changed
modified crates/utopia-core/src/models.rs +28 −0
| @@ -926,8 +926,36 @@ | ||
| 926 | 926 | /// 与列表分开取是有意的:列表有上限(一页十条),数数没有。从前左栏读的是 |
| 927 | 927 | /// 数组长度,而接口固定只回 100 条——一个有 164 条待办的库,界面写着 100, |
| 928 | 928 | /// 清完还会再冒出来。 |
| 929 | +/// 等人点头的一条事实(0015)。`quote` 是那句记忆的全文——确认界面要把原句和 | |
| 930 | +/// 三元组并排显示,只列三元组等于要人凭空判断它对不对。 | |
| 931 | +#[derive(Debug, Clone, Serialize, sqlx::FromRow)] | |
| 932 | +pub struct PendingFactView { | |
| 933 | + pub id: Uuid, | |
| 934 | + pub subject_id: Uuid, | |
| 935 | + pub subject_name: String, | |
| 936 | + pub predicate_id: Option<Uuid>, | |
| 937 | + /// 本体里的关系名;为空时前端显示 `proposed_predicate`(斜体,标明是原话) | |
| 938 | + pub predicate_label: Option<String>, | |
| 939 | + pub proposed_predicate: Option<String>, | |
| 940 | + pub object_id: Option<Uuid>, | |
| 941 | + pub object_name: Option<String>, | |
| 942 | + pub object_value: Option<serde_json::Value>, | |
| 943 | + pub valid_from: Option<DateTime<Utc>>, | |
| 944 | + pub valid_from_precision: Option<String>, | |
| 945 | + pub valid_to: Option<DateTime<Utc>>, | |
| 946 | + pub valid_to_precision: Option<String>, | |
| 947 | + pub confidence: f32, | |
| 948 | + pub chunk_id: Uuid, | |
| 949 | + pub quote: String, | |
| 950 | + pub proposed_by: Option<Uuid>, | |
| 951 | + pub proposed_by_name: Option<String>, | |
| 952 | + pub created_at: DateTime<Utc>, | |
| 953 | +} | |
| 954 | + | |
| 929 | 955 | #[derive(Debug, Clone, Copy, Default, Serialize, sqlx::FromRow)] |
| 930 | 956 | pub struct ReviewCounts { |
| 957 | + /// 记忆抽出、等人点头的事实(0015)。排第一:它是人自己说的话 | |
| 958 | + pub pending: i64, | |
| 931 | 959 | pub duplicates: i64, |
| 932 | 960 | pub conflicts: i64, |
| 933 | 961 | pub unconfirmed: i64, |
modified crates/utopia-server/src/api/chat.rs +17 −16
| @@ -29,17 +29,14 @@ | ||
| 29 | 29 | const MAX_HISTORY: usize = 20; |
| 30 | 30 | const MAX_ROUNDS: usize = 6; |
| 31 | 31 | |
| 32 | -/// **`remember` 暂时停用**(见 `docs/decisions/0015`)。 | |
| 32 | +/// `remember` 曾整个停用过一段(见 `docs/decisions/0015`):它那时会把一句话直接 | |
| 33 | +/// 变成图上一条活边,实测里「记住 Acme 把总部搬到了深圳」落成的是一条**空谓词、 | |
| 34 | +/// 0.9 置信**的边,而助手宣称的和图里得到的不是一回事。 | |
| 33 | 35 | /// |
| 34 | -/// 它今天会把一句话直接变成图上一条活边,而中间那一步没人看得见:实测里 | |
| 35 | -/// 「记住 Acme 把总部搬到了深圳」落成的是一条 **空谓词、0.9 置信** 的边—— | |
| 36 | -/// 本体里没有「搬迁到」这类关系,抽取落不上就留空(按 0010 这是对的), | |
| 37 | -/// 于是助手宣称的和图里得到的不是一回事。 | |
| 38 | -/// | |
| 39 | -/// 0018 建好了 `pending_facts`:抽出来的先等人点头。**抽取侧接上那张表 | |
| 40 | -/// 之后,把这里改回 true。** 在那之前宁可没有这个工具,也不要一个会 | |
| 41 | -/// 悄悄改图的工具。 | |
| 42 | -pub(super) const REMEMBER_ENABLED: bool = false; | |
| 36 | +/// 现在抽取侧接上了 `pending_facts`:记忆抽出的事实先等人点头,再进账本。 | |
| 37 | +/// 这个开关留着,是为了下次再发现「工具会悄悄改图」时有地方立刻拉闸—— | |
| 38 | +/// 宁可没有这个工具,也不要一个会悄悄改图的工具。 | |
| 39 | +pub(super) const REMEMBER_ENABLED: bool = true; | |
| 43 | 40 | |
| 44 | 41 | #[derive(Deserialize)] |
| 45 | 42 | pub struct ChatReq { |
| @@ -95,10 +92,11 @@ | ||
| 95 | 92 | "name": "remember", |
| 96 | 93 | "description": "Record one memory episode into the knowledge base's temporal \ |
| 97 | 94 | memory. Use ONLY when the user explicitly asks to remember/record \ |
| 98 | - something, or clearly states a decision or fact to keep. The episode is \ | |
| 99 | - extracted into the knowledge graph; if it contradicts an existing \ | |
| 100 | - single-valued fact, the old fact's validity is closed automatically \ | |
| 101 | - (never deleted). Do not use for casual conversation.", | |
| 95 | + something, or clearly states a decision or fact to keep. The sentence \ | |
| 96 | + is stored immediately; facts extracted from it are PROPOSED and shown \ | |
| 97 | + to the user for confirmation before they enter the knowledge graph. \ | |
| 98 | + Never claim a fact was added to the graph. Do not use for casual \ | |
| 99 | + conversation.", | |
| 102 | 100 | "parameters": { |
| 103 | 101 | "type": "object", |
| 104 | 102 | "properties": { |
| @@ -188,8 +186,10 @@ | ||
| 188 | 186 | |
| 189 | 187 | const MEMORY_PROMPT: &str = "\ |
| 190 | 188 | Memory: you can persist knowledge with the remember tool. Use it when the user says \ |
| 191 | - \"remember/record this\" or states a decision meant to last. Confirm in your reply what \ | |
| 192 | - was recorded. Never invent memories, and never call it for small talk."; | |
| 189 | + \"remember/record this\" or states a decision meant to last. In your reply, say that the \ | |
| 190 | + sentence was recorded and that the facts extracted from it will be shown for the user's \ | |
| 191 | + confirmation before entering the graph; never say a fact is already in the graph. \ | |
| 192 | + Never invent memories, and never call it for small talk."; | |
| 193 | 193 | |
| 194 | 194 | /// 工具的 JSON schema——**给模型看的那一份**。 |
| 195 | 195 | /// |
| @@ -687,6 +687,7 @@ | ||
| 687 | 687 | workspace_id, |
| 688 | 688 | mounted_sources: &mounted_sources, |
| 689 | 689 | can_write, |
| 690 | + actor: Some(user.id), | |
| 690 | 691 | }; |
| 691 | 692 | let (result, step) = tools::dispatch(&ctx, &mut sink, &call.name, &args).await; |
| 692 | 693 | // **这一步发生在正文的哪个位置。** |
modified crates/utopia-server/src/api/mcp.rs +1 −0
| @@ -176,6 +176,7 @@ | ||
| 176 | 176 | workspace_id: kb.workspace_id, |
| 177 | 177 | mounted_sources: &[], |
| 178 | 178 | can_write: false, |
| 179 | + actor: Some(user.id), | |
| 179 | 180 | }; |
| 180 | 181 | let mut sink = ToolSink::default(); |
| 181 | 182 | let (text, _step) = tools::dispatch(&ctx, &mut sink, name, &args).await; |
modified crates/utopia-server/src/api/mod.rs +9 −0
| @@ -333,6 +333,15 @@ | ||
| 333 | 333 | ) |
| 334 | 334 | .route("/kbs/{id}/review", get(review_routes::list)) |
| 335 | 335 | .route("/kbs/{id}/review/history", get(review_routes::history)) |
| 336 | + // 记忆抽出、等人点头的事实(0015):按句取、逐条裁 | |
| 337 | + .route( | |
| 338 | + "/kbs/{id}/review/pending", | |
| 339 | + get(review_routes::pending_for_chunk), | |
| 340 | + ) | |
| 341 | + .route( | |
| 342 | + "/kbs/{id}/review/pending/{pending_id}", | |
| 343 | + post(review_routes::decide_pending), | |
| 344 | + ) | |
| 336 | 345 | .route("/kbs/{id}/review/{review_id}", post(review_routes::decide)) |
| 337 | 346 | // 语义层映射的表态(0011)。跟消解审核并排——都是「引擎提议、人裁决」 |
| 338 | 347 | .route( |
modified crates/utopia-server/src/api/review_routes.rs +86 −0
| @@ -75,6 +75,8 @@ | ||
| 75 | 75 | let offset = q.offset.unwrap_or(0).max(0); |
| 76 | 76 | |
| 77 | 77 | let items = match queue { |
| 78 | + // 记忆抽出、等人点头的事实(0015)。排第一:它是人自己说的话 | |
| 79 | + "pending" => json!(utopia_store::pending::list(&state.pool, kb_id, limit, offset).await?), | |
| 78 | 80 | "duplicates" => { |
| 79 | 81 | json!(utopia_store::resolution::list_reviews(&state.pool, kb_id, limit, offset).await?) |
| 80 | 82 | } |
| @@ -636,6 +638,90 @@ | ||
| 636 | 638 | /// 就按它改图。开关关着时不静默跳过:回一个明确的错,界面才说得出为什么没动。 |
| 637 | 639 | /// |
| 638 | 640 | /// 同步跑,与一致性检查同一个理由:纯计算,没有模型调用也没有网络。 |
| 641 | +#[derive(Deserialize)] | |
| 642 | +pub struct PendingQuery { | |
| 643 | + pub chunk_id: Uuid, | |
| 644 | +} | |
| 645 | + | |
| 646 | +/// 一句记忆抽出的全部待确认项——对话里那张确认卡按这个取(0015)。 | |
| 647 | +/// Viewer 也能看:看得见提议、看不见按钮,与 Review 页同一口径 | |
| 648 | +pub async fn pending_for_chunk( | |
| 649 | + State(state): State<AppState>, | |
| 650 | + AuthUser(user): AuthUser, | |
| 651 | + Path(kb_id): Path<Uuid>, | |
| 652 | + Query(q): Query<PendingQuery>, | |
| 653 | +) -> ApiResult<Json<serde_json::Value>> { | |
| 654 | + require_kb(&state, &user, kb_id, Role::Viewer).await?; | |
| 655 | + let items = utopia_store::pending::for_chunk(&state.pool, kb_id, q.chunk_id).await?; | |
| 656 | + Ok(Json(json!({ "items": items }))) | |
| 657 | +} | |
| 658 | + | |
| 659 | +#[derive(Deserialize)] | |
| 660 | +pub struct DecidePendingBody { | |
| 661 | + /// `confirm` 进账本;`reject` 记进 `rejected_facts`,下一轮重抽不再提 | |
| 662 | + pub action: String, | |
| 663 | +} | |
| 664 | + | |
| 665 | +/// 人对一条待确认事实点头或摇头。 | |
| 666 | +/// | |
| 667 | +/// 两个动作都进决策台账,快照自包含——行删了之后台账上还读得出当时确认的是什么。 | |
| 668 | +/// 确认走与抽取相同的那条路(事实 + 证据 + 时态对账),所以一句「Mira 交给 Devin 了」 | |
| 669 | +/// 点头之后,Mira 那条会像从文档里抽出来时一样被闭合 | |
| 670 | +pub async fn decide_pending( | |
| 671 | + State(state): State<AppState>, | |
| 672 | + AuthUser(user): AuthUser, | |
| 673 | + Path((kb_id, pending_id)): Path<(Uuid, Uuid)>, | |
| 674 | + Json(body): Json<DecidePendingBody>, | |
| 675 | +) -> ApiResult<Json<serde_json::Value>> { | |
| 676 | + require_kb(&state, &user, kb_id, Role::Editor).await?; | |
| 677 | + match body.action.as_str() { | |
| 678 | + "confirm" => { | |
| 679 | + let done = utopia_store::pending::confirm(&state.pool, kb_id, pending_id).await?; | |
| 680 | + let _ = utopia_store::audit::record( | |
| 681 | + &state.pool, | |
| 682 | + Some(kb_id), | |
| 683 | + user.id, | |
| 684 | + "fact.nod_confirmed", | |
| 685 | + "fact", | |
| 686 | + Some(done.fact_id), | |
| 687 | + done.snapshot, | |
| 688 | + ) | |
| 689 | + .await; | |
| 690 | + state.emit_pending(kb_id); | |
| 691 | + state.emit_review(kb_id); | |
| 692 | + state.emit_graph(kb_id); | |
| 693 | + Ok(Json(json!({ | |
| 694 | + "ok": true, | |
| 695 | + "fact_id": done.fact_id, | |
| 696 | + "created": done.created, | |
| 697 | + "conflicts": done.conflicts, | |
| 698 | + }))) | |
| 699 | + } | |
| 700 | + "reject" => { | |
| 701 | + let snap = utopia_store::pending::reject(&state.pool, kb_id, pending_id, Some(user.id)) | |
| 702 | + .await?; | |
| 703 | + let _ = utopia_store::audit::record( | |
| 704 | + &state.pool, | |
| 705 | + Some(kb_id), | |
| 706 | + user.id, | |
| 707 | + "fact.nod_rejected", | |
| 708 | + "pending_fact", | |
| 709 | + Some(pending_id), | |
| 710 | + snap, | |
| 711 | + ) | |
| 712 | + .await; | |
| 713 | + state.emit_pending(kb_id); | |
| 714 | + state.emit_review(kb_id); | |
| 715 | + Ok(Json(json!({ "ok": true }))) | |
| 716 | + } | |
| 717 | + other => Err(utopia_core::AppError::invalid( | |
| 718 | + "unknown_action", | |
| 719 | + format!("action must be confirm or reject, got {other}"), | |
| 720 | + ) | |
| 721 | + .into()), | |
| 722 | + } | |
| 723 | +} | |
| 724 | + | |
| 639 | 725 | pub async fn run_inference( |
| 640 | 726 | State(state): State<AppState>, |
| 641 | 727 | AuthUser(user): AuthUser, |
modified crates/utopia-server/src/api/tools.rs +16 −5
| @@ -32,6 +32,9 @@ | ||
| 32 | 32 | pub mounted_sources: &'a [DataSourceView], |
| 33 | 33 | /// editor 及以上才有 `remember` |
| 34 | 34 | pub can_write: bool, |
| 35 | + /// 在说话的人。`remember` 把它记成「谁说的」,一路跟到待确认队列(0015)。 | |
| 36 | + /// 对话里恒有值;MCP 也有——令牌以人的身份行事(0014) | |
| 37 | + pub actor: Option<Uuid>, | |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /// 工具执行过程中往外攒的东西。 |
| @@ -343,23 +346,31 @@ | ||
| 343 | 346 | } |
| 344 | 347 | match utopia_store::memory::append_episode(&ctx.state.pool, ctx.kb_id, text, occurred_at).await |
| 345 | 348 | { |
| 346 | - Ok((doc_id, _chunk)) => { | |
| 347 | - // 摄入(embedding/索引/增量抽取)异步走队列,不阻塞对话 | |
| 349 | + Ok((doc_id, chunk_id)) => { | |
| 350 | + // 摄入(embedding/索引/增量抽取)异步走队列,不阻塞对话。 | |
| 351 | + // 抽出来的事实**先等人点头**(0015)——所以这里只能如实说「记下了这句话」, | |
| 352 | + // 说不出抽出了几条:抽取还没跑。卡片在任务完成时长到对话里 | |
| 348 | 353 | let _ = utopia_store::jobs::enqueue( |
| 349 | 354 | &ctx.state.pool, |
| 350 | 355 | "memory_ingest", |
| 351 | - json!({ "document_id": doc_id }), | |
| 356 | + json!({ "document_id": doc_id, "proposed_by": ctx.actor }), | |
| 352 | 357 | ) |
| 353 | 358 | .await; |
| 354 | 359 | ctx.state.emit_document(ctx.kb_id, doc_id); |
| 355 | 360 | ( |
| 356 | 361 | format!( |
| 357 | - "Recorded (effective {}): {text}", | |
| 362 | + "Recorded the sentence (effective {}): {text}\n\ | |
| 363 | + Facts extracted from it will be shown to the user for confirmation \ | |
| 364 | + before entering the graph. Tell the user exactly that: the sentence is \ | |
| 365 | + recorded, and the extracted facts await their confirmation. Do not claim \ | |
| 366 | + any fact has been added to the knowledge graph.", | |
| 358 | 367 | occurred_at.format("%Y-%m-%d") |
| 359 | 368 | ), |
| 360 | 369 | json!({ |
| 361 | 370 | "kind": "tool", "label": "remember", |
| 362 | - "detail": text.chars().take(60).collect::<String>() | |
| 371 | + "detail": text.chars().take(60).collect::<String>(), | |
| 372 | + // 对话里那张确认卡按它取待确认项;回放时也据此重画 | |
| 373 | + "chunk_id": chunk_id, | |
| 363 | 374 | }), |
| 364 | 375 | ) |
| 365 | 376 | } |
modified crates/utopia-server/src/extraction.rs +92 −4
| @@ -212,8 +212,14 @@ | ||
| 212 | 212 | Ok(()) |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | -pub async fn extract_document(state: &AppState, document_id: Uuid) -> anyhow::Result<()> { | |
| 216 | - match run(state, document_id).await { | |
| 215 | +/// `proposed_by`:这篇文档若是记忆日志,抽出的事实等人点头,这一列记「谁说的」。 | |
| 216 | +/// 批量摄入的文档传 None——那条路不经待确认队列,这个值用不上 | |
| 217 | +pub async fn extract_document( | |
| 218 | + state: &AppState, | |
| 219 | + document_id: Uuid, | |
| 220 | + proposed_by: Option<Uuid>, | |
| 221 | +) -> anyhow::Result<()> { | |
| 222 | + match run(state, document_id, proposed_by).await { | |
| 217 | 223 | Ok(()) => Ok(()), |
| 218 | 224 | Err(e) => { |
| 219 | 225 | // 原因随状态落库:只进日志的错误等于没有错误 |
| @@ -280,7 +286,7 @@ | ||
| 280 | 286 | Ok(r.entity_id) |
| 281 | 287 | } |
| 282 | 288 | |
| 283 | -async fn run(state: &AppState, document_id: Uuid) -> anyhow::Result<()> { | |
| 289 | +async fn run(state: &AppState, document_id: Uuid, proposed_by: Option<Uuid>) -> anyhow::Result<()> { | |
| 284 | 290 | let doc = utopia_store::documents::get(&state.pool, document_id).await?; |
| 285 | 291 | let kb = utopia_store::kbs::get(&state.pool, doc.kb_id).await?; |
| 286 | 292 | let settings = utopia_store::settings::get(&state.pool, kb.workspace_id) |
| @@ -389,6 +395,13 @@ | ||
| 389 | 395 | // 本轮要从头讲一遍这篇文档的故事,旧信号先清掉(重抽自动作数) |
| 390 | 396 | let _ = utopia_store::extraction_drops::clear_for_document(&state.pool, document_id).await; |
| 391 | 397 | |
| 398 | + // **记忆抽出的事实先等人点头**(0015)。一句 remember 一次一句、人就在对话里, | |
| 399 | + // 确认成本最低的时刻就是说完那句话的时候;而批量摄入一次上万条,逐条确认 | |
| 400 | + // 不可能,那条路仍旧乐观写入 + 事后审阅。判据只有一个:这篇是不是记忆日志。 | |
| 401 | + // 实体照常消解并创建——`pending_facts.subject_id` 是外键,这是 0018 定下的取舍 | |
| 402 | + let await_nod = utopia_store::memory::is_memory_document(&state.pool, document_id).await?; | |
| 403 | + let mut pending_count = 0usize; | |
| 404 | + | |
| 392 | 405 | let doc_time = doc.doc_time.map(|t| t.format("%Y-%m-%d").to_string()); |
| 393 | 406 | let chunks = utopia_store::documents::chunks_for_extraction(&state.pool, document_id).await?; |
| 394 | 407 | |
| @@ -716,6 +729,29 @@ | ||
| 716 | 729 | // 单位随事实落笔:类型上的单位以后改了,旧值仍按记录时的单位读 |
| 717 | 730 | object_value["unit"] = serde_json::json!(u); |
| 718 | 731 | } |
| 732 | + if await_nod { | |
| 733 | + if let utopia_store::pending::Outcome::Proposed(_) = | |
| 734 | + utopia_store::pending::propose( | |
| 735 | + &state.pool, | |
| 736 | + utopia_store::pending::Proposal { | |
| 737 | + kb_id: doc.kb_id, | |
| 738 | + subject_id, | |
| 739 | + predicate_id: Some(attr.id), | |
| 740 | + object_id: None, | |
| 741 | + object_value: Some(&object_value), | |
| 742 | + proposed_predicate: Some(f.predicate.as_str()), | |
| 743 | + validity, | |
| 744 | + confidence, | |
| 745 | + chunk_id: chunk.id, | |
| 746 | + proposed_by, | |
| 747 | + }, | |
| 748 | + ) | |
| 749 | + .await? | |
| 750 | + { | |
| 751 | + pending_count += 1; | |
| 752 | + } | |
| 753 | + continue; | |
| 754 | + } | |
| 719 | 755 | let (fact_id, created) = utopia_store::graph::insert_value_fact( |
| 720 | 756 | &state.pool, |
| 721 | 757 | doc.kb_id, |
| @@ -812,12 +848,36 @@ | ||
| 812 | 848 | Some(&format!("{subject_name} → {value}")), |
| 813 | 849 | ) |
| 814 | 850 | .await; |
| 851 | + let literal = serde_json::json!({ "value": value }); | |
| 852 | + if await_nod { | |
| 853 | + if let utopia_store::pending::Outcome::Proposed(_) = | |
| 854 | + utopia_store::pending::propose( | |
| 855 | + &state.pool, | |
| 856 | + utopia_store::pending::Proposal { | |
| 857 | + kb_id: doc.kb_id, | |
| 858 | + subject_id, | |
| 859 | + predicate_id: None, | |
| 860 | + object_id: None, | |
| 861 | + object_value: Some(&literal), | |
| 862 | + proposed_predicate: Some(f.predicate.as_str()), | |
| 863 | + validity, | |
| 864 | + confidence, | |
| 865 | + chunk_id: chunk.id, | |
| 866 | + proposed_by, | |
| 867 | + }, | |
| 868 | + ) | |
| 869 | + .await? | |
| 870 | + { | |
| 871 | + pending_count += 1; | |
| 872 | + } | |
| 873 | + continue; | |
| 874 | + } | |
| 815 | 875 | let (fact_id, created) = utopia_store::graph::insert_value_fact( |
| 816 | 876 | &state.pool, |
| 817 | 877 | doc.kb_id, |
| 818 | 878 | subject_id, |
| 819 | 879 | None, |
| 820 | - &serde_json::json!({ "value": value }), | |
| 880 | + &literal, | |
| 821 | 881 | validity, |
| 822 | 882 | confidence, |
| 823 | 883 | ) |
| @@ -1024,6 +1084,28 @@ | ||
| 1024 | 1084 | (predicate_id, subject_id, object_id) |
| 1025 | 1085 | }; |
| 1026 | 1086 | |
| 1087 | + if await_nod { | |
| 1088 | + if let utopia_store::pending::Outcome::Proposed(_) = utopia_store::pending::propose( | |
| 1089 | + &state.pool, | |
| 1090 | + utopia_store::pending::Proposal { | |
| 1091 | + kb_id: doc.kb_id, | |
| 1092 | + subject_id, | |
| 1093 | + predicate_id, | |
| 1094 | + object_id: Some(object_id), | |
| 1095 | + object_value: None, | |
| 1096 | + proposed_predicate: Some(f.predicate.as_str()), | |
| 1097 | + validity, | |
| 1098 | + confidence, | |
| 1099 | + chunk_id: chunk.id, | |
| 1100 | + proposed_by, | |
| 1101 | + }, | |
| 1102 | + ) | |
| 1103 | + .await? | |
| 1104 | + { | |
| 1105 | + pending_count += 1; | |
| 1106 | + } | |
| 1107 | + continue; | |
| 1108 | + } | |
| 1027 | 1109 | { |
| 1028 | 1110 | let (fact_id, created) = utopia_store::graph::insert_fact( |
| 1029 | 1111 | &state.pool, |
| @@ -1092,6 +1174,12 @@ | ||
| 1092 | 1174 | // (LLM 调用/解析失败的块在上方 continue 掉,不打标,下次重试) |
| 1093 | 1175 | utopia_store::documents::mark_chunk_extracted(&state.pool, chunk.id).await?; |
| 1094 | 1176 | } |
| 1177 | + // 队列里多了东西才叫醒人:Review 的计数与对话里那张确认卡都靠这一声 | |
| 1178 | + if pending_count > 0 { | |
| 1179 | + tracing::info!(%document_id, pending_count, "记忆抽出的事实进了待确认队列"); | |
| 1180 | + state.emit_pending(doc.kb_id); | |
| 1181 | + state.emit_review(doc.kb_id); | |
| 1182 | + } | |
| 1095 | 1183 | |
| 1096 | 1184 | // 消歧后缀在实体创建时算会早于其事实写入——收尾时对本文档涉及的名字统一刷新 |
| 1097 | 1185 | let names: std::collections::HashSet<&str> = |
modified crates/utopia-server/src/main.rs +10 −2
| @@ -33,6 +33,14 @@ | ||
| 33 | 33 | use utopia_search::SearchIndex; |
| 34 | 34 | use uuid::Uuid; |
| 35 | 35 | |
| 36 | +/// 记忆那条路带着「谁说的」(0015);别的任务没有这个字段,读到 None 本就该如此 | |
| 37 | +fn payload_proposed_by(payload: &serde_json::Value) -> Option<Uuid> { | |
| 38 | + payload | |
| 39 | + .get("proposed_by") | |
| 40 | + .and_then(|v| v.as_str()) | |
| 41 | + .and_then(|s| s.parse().ok()) | |
| 42 | +} | |
| 43 | + | |
| 36 | 44 | fn payload_document_id(payload: &serde_json::Value) -> anyhow::Result<Uuid> { |
| 37 | 45 | payload |
| 38 | 46 | .get("document_id") |
| @@ -295,7 +303,7 @@ | ||
| 295 | 303 | } |
| 296 | 304 | "memory_ingest" => { |
| 297 | 305 | let id = payload_document_id(&job.payload)?; |
| 298 | - pipeline::memory_ingest(st, id).await | |
| 306 | + pipeline::memory_ingest(st, id, payload_proposed_by(&job.payload)).await | |
| 299 | 307 | } |
| 300 | 308 | "explore_mappings" => { |
| 301 | 309 | let kb_id: Uuid = job |
| @@ -342,7 +350,7 @@ | ||
| 342 | 350 | } |
| 343 | 351 | "extract_document" => { |
| 344 | 352 | let id = payload_document_id(&job.payload)?; |
| 345 | - extraction::extract_document(st, id).await | |
| 353 | + extraction::extract_document(st, id, payload_proposed_by(&job.payload)).await | |
| 346 | 354 | } |
| 347 | 355 | "bootstrap_ontology" => { |
| 348 | 356 | let kb_id: Uuid = job |
modified crates/utopia-server/src/pipeline.rs +8 −2
| @@ -93,7 +93,13 @@ | ||
| 93 | 93 | /// 记忆摄入(episodes 快速路径的后半程):新 episode chunk 补 embedding、 |
| 94 | 94 | /// 重建全文索引、触发增量抽取(extracted_at 为空的新 chunk 才会被抽)。 |
| 95 | 95 | /// 免解析免分块——episode 落库时已是 chunk。 |
| 96 | -pub async fn memory_ingest(state: &AppState, document_id: Uuid) -> anyhow::Result<()> { | |
| 96 | +/// | |
| 97 | +/// `proposed_by`:说这句话的人。一路传到抽取,落在 `pending_facts.proposed_by`(0015) | |
| 98 | +pub async fn memory_ingest( | |
| 99 | + state: &AppState, | |
| 100 | + document_id: Uuid, | |
| 101 | + proposed_by: Option<Uuid>, | |
| 102 | +) -> anyhow::Result<()> { | |
| 97 | 103 | let doc = utopia_store::documents::get(&state.pool, document_id).await?; |
| 98 | 104 | let kb_row = utopia_store::kbs::get(&state.pool, doc.kb_id).await?; |
| 99 | 105 | let settings = utopia_store::settings::get(&state.pool, kb_row.workspace_id).await?; |
| @@ -132,7 +138,7 @@ | ||
| 132 | 138 | utopia_store::jobs::enqueue( |
| 133 | 139 | &state.pool, |
| 134 | 140 | "extract_document", |
| 135 | - serde_json::json!({ "document_id": document_id }), | |
| 141 | + serde_json::json!({ "document_id": document_id, "proposed_by": proposed_by }), | |
| 136 | 142 | ) |
| 137 | 143 | .await?; |
| 138 | 144 | } |
modified crates/utopia-server/src/state.rs +10 −0
| @@ -82,6 +82,16 @@ | ||
| 82 | 82 | }); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | + /// 一句记忆抽出了等人点头的事实(0015)。对话里那张确认卡按这个刷新—— | |
| 86 | + /// 抽取是异步的,卡片只能在任务完成时长出来,而不是在助手回话的那一刻 | |
| 87 | + pub fn emit_pending(&self, kb_id: Uuid) { | |
| 88 | + let _ = self.events.send(AppEvent { | |
| 89 | + kb_id: Some(kb_id), | |
| 90 | + kind: "pending", | |
| 91 | + document_id: None, | |
| 92 | + }); | |
| 93 | + } | |
| 94 | + | |
| 85 | 95 | /// 图变了。推理往图里加过边之后要发一次——它不经过文档管道, |
| 86 | 96 | /// 而 `document` 那条事件是文档管道专用的 |
| 87 | 97 | pub fn emit_graph(&self, kb_id: Uuid) { |
modified crates/utopia-store/src/lib.rs +1 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | pub mod model_limits; |
| 20 | 20 | pub mod ontology; |
| 21 | 21 | pub mod palette; |
| 22 | +pub mod pending; | |
| 22 | 23 | pub mod reasoning; |
| 23 | 24 | pub mod resolution; |
| 24 | 25 | pub mod review; |
added crates/utopia-store/src/pending.rs +350 −0
| @@ -0,0 +1,350 @@ | ||
| 1 | +//! 等人点头的事实(见 `docs/decisions/0015`,表在 `migrations/0018`)。 | |
| 2 | +//! | |
| 3 | +//! 一句记忆抽出来的三元组先落这里,**不进 `facts`**:不上图、不参与检索、 | |
| 4 | +//! 不进推理。人在对话里或 Review 页看见「原句在上、三元组在下」之后点头, | |
| 5 | +//! 它才经与抽取相同的那条路(`insert_fact` + 证据 + 时态对账)进账本。 | |
| 6 | +//! | |
| 7 | +//! **为什么另一张表而不是 `facts` 加一列**:五十多处查询按 `invalidated_at IS NULL` | |
| 8 | +//! 捞活事实,逐个补过滤漏一处就有一条没人点头的事实混进图里——而这张表存在的 | |
| 9 | +//! 全部理由就是防这件事。分开之后忘了读它的后果是「看不见待确认队列」, | |
| 10 | +//! 方向对了。0013 的 `derived_facts` 是同一个判断。 | |
| 11 | +//! | |
| 12 | +//! **只拦交互式的单条写入。** 批量摄入仍旧乐观写入 + 事后审阅——那条路不经这里。 | |
| 13 | + | |
| 14 | +use sqlx::PgPool; | |
| 15 | +use utopia_core::models::PendingFactView; | |
| 16 | +use utopia_core::{AppError, AppResult}; | |
| 17 | +use uuid::Uuid; | |
| 18 | + | |
| 19 | +use crate::graph::Validity; | |
| 20 | + | |
| 21 | +/// 抽取器交过来的一条提议。字段与 `insert_fact` / `insert_value_fact` 对齐, | |
| 22 | +/// 多出 `proposed_predicate`(模型原话)、`chunk_id`(那句记忆)、`proposed_by`(谁说的)。 | |
| 23 | +pub struct Proposal<'a> { | |
| 24 | + pub kb_id: Uuid, | |
| 25 | + pub subject_id: Uuid, | |
| 26 | + /// None = 本体里没有对应的关系(0010)。**人要看见的正是这个空** | |
| 27 | + pub predicate_id: Option<Uuid>, | |
| 28 | + pub object_id: Option<Uuid>, | |
| 29 | + pub object_value: Option<&'a serde_json::Value>, | |
| 30 | + pub proposed_predicate: Option<&'a str>, | |
| 31 | + pub validity: Validity<'a>, | |
| 32 | + pub confidence: f32, | |
| 33 | + pub chunk_id: Uuid, | |
| 34 | + pub proposed_by: Option<Uuid>, | |
| 35 | +} | |
| 36 | + | |
| 37 | +/// 提议的去向。三种「没提」都不是错误——重抽一句记忆会再次算出同样的三元组, | |
| 38 | +/// 不挡住就等于每抽一次都把人的决定抹掉一次。 | |
| 39 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 40 | +pub enum Outcome { | |
| 41 | + Proposed(Uuid), | |
| 42 | + /// 图上已经有一条一样的活事实——不必再问 | |
| 43 | + AlreadyAsserted, | |
| 44 | + /// 已经在队列里等着 | |
| 45 | + AlreadyPending, | |
| 46 | + /// 人拒绝过这个三元组(`rejected_facts`) | |
| 47 | + Rejected, | |
| 48 | +} | |
| 49 | + | |
| 50 | +pub async fn propose(pool: &PgPool, p: Proposal<'_>) -> AppResult<Outcome> { | |
| 51 | + let asserted: Option<(i32,)> = sqlx::query_as( | |
| 52 | + "SELECT 1 FROM facts | |
| 53 | + WHERE kb_id = $1 AND subject_id = $2 | |
| 54 | + AND predicate_id IS NOT DISTINCT FROM $3 | |
| 55 | + AND object_id IS NOT DISTINCT FROM $4 | |
| 56 | + AND object_value IS NOT DISTINCT FROM $5 | |
| 57 | + AND invalidated_at IS NULL | |
| 58 | + LIMIT 1", | |
| 59 | + ) | |
| 60 | + .bind(p.kb_id) | |
| 61 | + .bind(p.subject_id) | |
| 62 | + .bind(p.predicate_id) | |
| 63 | + .bind(p.object_id) | |
| 64 | + .bind(p.object_value) | |
| 65 | + .fetch_optional(pool) | |
| 66 | + .await?; | |
| 67 | + if asserted.is_some() { | |
| 68 | + return Ok(Outcome::AlreadyAsserted); | |
| 69 | + } | |
| 70 | + let pending: Option<(i32,)> = sqlx::query_as( | |
| 71 | + "SELECT 1 FROM pending_facts | |
| 72 | + WHERE kb_id = $1 AND subject_id = $2 | |
| 73 | + AND predicate_id IS NOT DISTINCT FROM $3 | |
| 74 | + AND object_id IS NOT DISTINCT FROM $4 | |
| 75 | + AND object_value IS NOT DISTINCT FROM $5 | |
| 76 | + LIMIT 1", | |
| 77 | + ) | |
| 78 | + .bind(p.kb_id) | |
| 79 | + .bind(p.subject_id) | |
| 80 | + .bind(p.predicate_id) | |
| 81 | + .bind(p.object_id) | |
| 82 | + .bind(p.object_value) | |
| 83 | + .fetch_optional(pool) | |
| 84 | + .await?; | |
| 85 | + if pending.is_some() { | |
| 86 | + return Ok(Outcome::AlreadyPending); | |
| 87 | + } | |
| 88 | + // 拒绝记录只按 (主语, 谓词, 宾语实体) 查。**字面值事实不查**:`rejected_facts` | |
| 89 | + // 没有 object_value 列,按 (主语, 谓词) 挡会把「薪水 28000 被拒」扩大成 | |
| 90 | + // 「薪水这个属性永远不再提」。宁可多问一次,不替人拒掉一个新值 | |
| 91 | + if let Some(object_id) = p.object_id { | |
| 92 | + let rejected: Option<(i32,)> = sqlx::query_as( | |
| 93 | + "SELECT 1 FROM rejected_facts | |
| 94 | + WHERE kb_id = $1 AND subject_id = $2 | |
| 95 | + AND predicate_id IS NOT DISTINCT FROM $3 | |
| 96 | + AND object_id = $4 | |
| 97 | + LIMIT 1", | |
| 98 | + ) | |
| 99 | + .bind(p.kb_id) | |
| 100 | + .bind(p.subject_id) | |
| 101 | + .bind(p.predicate_id) | |
| 102 | + .bind(object_id) | |
| 103 | + .fetch_optional(pool) | |
| 104 | + .await?; | |
| 105 | + if rejected.is_some() { | |
| 106 | + return Ok(Outcome::Rejected); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + let id = Uuid::now_v7(); | |
| 110 | + sqlx::query( | |
| 111 | + "INSERT INTO pending_facts | |
| 112 | + (id, kb_id, subject_id, predicate_id, object_id, object_value, proposed_predicate, | |
| 113 | + valid_from, valid_from_precision, valid_to, valid_to_precision, | |
| 114 | + confidence, chunk_id, proposed_by) | |
| 115 | + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", | |
| 116 | + ) | |
| 117 | + .bind(id) | |
| 118 | + .bind(p.kb_id) | |
| 119 | + .bind(p.subject_id) | |
| 120 | + .bind(p.predicate_id) | |
| 121 | + .bind(p.object_id) | |
| 122 | + .bind(p.object_value) | |
| 123 | + .bind(p.proposed_predicate) | |
| 124 | + .bind(p.validity.from) | |
| 125 | + .bind(p.validity.from_precision) | |
| 126 | + .bind(p.validity.to) | |
| 127 | + .bind(p.validity.to_precision) | |
| 128 | + .bind(p.confidence) | |
| 129 | + .bind(p.chunk_id) | |
| 130 | + .bind(p.proposed_by) | |
| 131 | + .execute(pool) | |
| 132 | + .await?; | |
| 133 | + Ok(Outcome::Proposed(id)) | |
| 134 | +} | |
| 135 | + | |
| 136 | +/// 给人看的一条待确认项:`utopia_core::models::PendingFactView`。 | |
| 137 | +const VIEW_SELECT: &str = "\ | |
| 138 | + SELECT p.id, p.subject_id, s.canonical_name AS subject_name, | |
| 139 | + p.predicate_id, r.label AS predicate_label, p.proposed_predicate, | |
| 140 | + p.object_id, o.canonical_name AS object_name, p.object_value, | |
| 141 | + p.valid_from, p.valid_from_precision, p.valid_to, p.valid_to_precision, | |
| 142 | + p.confidence, p.chunk_id, c.text AS quote, | |
| 143 | + p.proposed_by, u.display_name AS proposed_by_name, p.created_at | |
| 144 | + FROM pending_facts p | |
| 145 | + JOIN entities s ON s.id = p.subject_id | |
| 146 | + LEFT JOIN relation_types r ON r.id = p.predicate_id | |
| 147 | + LEFT JOIN entities o ON o.id = p.object_id | |
| 148 | + JOIN chunks c ON c.id = p.chunk_id | |
| 149 | + LEFT JOIN users u ON u.id = p.proposed_by"; | |
| 150 | + | |
| 151 | +pub async fn list( | |
| 152 | + pool: &PgPool, | |
| 153 | + kb_id: Uuid, | |
| 154 | + limit: i64, | |
| 155 | + offset: i64, | |
| 156 | +) -> AppResult<Vec<PendingFactView>> { | |
| 157 | + Ok(sqlx::query_as(&format!( | |
| 158 | + "{VIEW_SELECT} WHERE p.kb_id = $1 ORDER BY p.created_at DESC LIMIT $2 OFFSET $3" | |
| 159 | + )) | |
| 160 | + .bind(kb_id) | |
| 161 | + .bind(limit) | |
| 162 | + .bind(offset) | |
| 163 | + .fetch_all(pool) | |
| 164 | + .await?) | |
| 165 | +} | |
| 166 | + | |
| 167 | +/// 一句记忆抽出的全部待确认项——对话里那张卡片按这个取。 | |
| 168 | +pub async fn for_chunk( | |
| 169 | + pool: &PgPool, | |
| 170 | + kb_id: Uuid, | |
| 171 | + chunk_id: Uuid, | |
| 172 | +) -> AppResult<Vec<PendingFactView>> { | |
| 173 | + Ok(sqlx::query_as(&format!( | |
| 174 | + "{VIEW_SELECT} WHERE p.kb_id = $1 AND p.chunk_id = $2 ORDER BY p.created_at" | |
| 175 | + )) | |
| 176 | + .bind(kb_id) | |
| 177 | + .bind(chunk_id) | |
| 178 | + .fetch_all(pool) | |
| 179 | + .await?) | |
| 180 | +} | |
| 181 | + | |
| 182 | +async fn get(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppResult<PendingFactView> { | |
| 183 | + sqlx::query_as(&format!("{VIEW_SELECT} WHERE p.kb_id = $1 AND p.id = $2")) | |
| 184 | + .bind(kb_id) | |
| 185 | + .bind(id) | |
| 186 | + .fetch_optional(pool) | |
| 187 | + .await? | |
| 188 | + .ok_or(AppError::NotFound) | |
| 189 | +} | |
| 190 | + | |
| 191 | +/// 决策台账要的自包含快照:行删了之后台账上还读得出当时确认或拒绝的是什么。 | |
| 192 | +fn snapshot(v: &PendingFactView) -> serde_json::Value { | |
| 193 | + serde_json::json!({ | |
| 194 | + "subject": v.subject_name, | |
| 195 | + "predicate": v.predicate_label, | |
| 196 | + "proposed_predicate": v.proposed_predicate, | |
| 197 | + "object": v.object_name, | |
| 198 | + "object_value": v.object_value, | |
| 199 | + "valid_from": v.valid_from, | |
| 200 | + "valid_to": v.valid_to, | |
| 201 | + "confidence": v.confidence, | |
| 202 | + "quote": v.quote, | |
| 203 | + "proposed_by": v.proposed_by_name, | |
| 204 | + }) | |
| 205 | +} | |
| 206 | + | |
| 207 | +pub struct Confirmed { | |
| 208 | + pub fact_id: Uuid, | |
| 209 | + /// false = 图上已有同一条活事实,这次只补了证据 | |
| 210 | + pub created: bool, | |
| 211 | + /// 时态对账拿不准、进了 `fact_conflicts` 的条数 | |
| 212 | + pub conflicts: u32, | |
| 213 | + pub snapshot: serde_json::Value, | |
| 214 | +} | |
| 215 | + | |
| 216 | +/// 人点头:按抽取那条路进账本——落事实、挂证据、时态对账——然后从队列里拿掉。 | |
| 217 | +/// | |
| 218 | +/// **置信度不动。** 人的态度不用浮点数表达(0011 的教训),它在审计台账里。 | |
| 219 | +/// | |
| 220 | +/// 不包事务:`insert_fact` 按 (主语, 谓词, 宾语) 对活事实去重,中途断掉再点一次 | |
| 221 | +/// 只会补证据、不会落第二条,删行在最后——幂等靡有余悸。 | |
| 222 | +pub async fn confirm(pool: &PgPool, kb_id: Uuid, id: Uuid) -> AppResult<Confirmed> { | |
| 223 | + let v = get(pool, kb_id, id).await?; | |
| 224 | + let validity = Validity { | |
| 225 | + from: v.valid_from, | |
| 226 | + from_precision: v.valid_from_precision.as_deref(), | |
| 227 | + to: v.valid_to, | |
| 228 | + to_precision: v.valid_to_precision.as_deref(), | |
| 229 | + }; | |
| 230 | + let (fact_id, created) = match (v.object_id, v.object_value.as_ref()) { | |
| 231 | + (Some(object_id), _) => { | |
| 232 | + crate::graph::insert_fact( | |
| 233 | + pool, | |
| 234 | + kb_id, | |
| 235 | + v.subject_id, | |
| 236 | + v.predicate_id, | |
| 237 | + object_id, | |
| 238 | + validity, | |
| 239 | + v.confidence, | |
| 240 | + ) | |
| 241 | + .await? | |
| 242 | + } | |
| 243 | + (None, Some(value)) => { | |
| 244 | + crate::graph::insert_value_fact( | |
| 245 | + pool, | |
| 246 | + kb_id, | |
| 247 | + v.subject_id, | |
| 248 | + v.predicate_id, | |
| 249 | + value, | |
| 250 | + validity, | |
| 251 | + v.confidence, | |
| 252 | + ) | |
| 253 | + .await? | |
| 254 | + } | |
| 255 | + (None, None) => { | |
| 256 | + return Err(AppError::invalid( | |
| 257 | + "pending_fact_has_no_object", | |
| 258 | + "a pending fact must have an object entity or a literal value", | |
| 259 | + )) | |
| 260 | + } | |
| 261 | + }; | |
| 262 | + // 证据指回那句记忆。引句就是整句——一条 episode 本来就只有一句 | |
| 263 | + crate::graph::add_evidence( | |
| 264 | + pool, | |
| 265 | + fact_id, | |
| 266 | + v.chunk_id, | |
| 267 | + Some(&v.quote), | |
| 268 | + v.proposed_predicate.as_deref(), | |
| 269 | + ) | |
| 270 | + .await?; | |
| 271 | + | |
| 272 | + // 与抽取相同的时态对账:带唯一性约束的状态关系,新事实闭合旧事实。 | |
| 273 | + // 这正是记忆该有的行为——「Mira 交给 Devin 了」说完,Mira 那条就该闭合 | |
| 274 | + let mut conflicts = 0u32; | |
| 275 | + if created { | |
| 276 | + if let Some(pid) = v.predicate_id { | |
| 277 | + let meta: Option<(bool, bool, String)> = sqlx::query_as( | |
| 278 | + "SELECT functional, inverse_functional, temporal FROM relation_types WHERE id = $1", | |
| 279 | + ) | |
| 280 | + .bind(pid) | |
| 281 | + .fetch_optional(pool) | |
| 282 | + .await?; | |
| 283 | + if let Some((functional, inverse_functional, temporal)) = meta { | |
| 284 | + if temporal == "state" { | |
| 285 | + let mut directions = Vec::new(); | |
| 286 | + if functional { | |
| 287 | + directions.push(crate::temporal::Uniqueness::SubjectSide); | |
| 288 | + } | |
| 289 | + // 宾语侧唯一只对实体宾语有意义;字面值没有「谁被指着」 | |
| 290 | + if inverse_functional && v.object_id.is_some() { | |
| 291 | + directions.push(crate::temporal::Uniqueness::ObjectSide); | |
| 292 | + } | |
| 293 | + for dir in directions { | |
| 294 | + let report = crate::temporal::reconcile_new_fact( | |
| 295 | + pool, | |
| 296 | + kb_id, | |
| 297 | + fact_id, | |
| 298 | + v.subject_id, | |
| 299 | + pid, | |
| 300 | + v.object_id, | |
| 301 | + v.object_value.as_ref(), | |
| 302 | + dir, | |
| 303 | + validity, | |
| 304 | + v.confidence, | |
| 305 | + ) | |
| 306 | + .await?; | |
| 307 | + conflicts += report.conflicts; | |
| 308 | + } | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + } | |
| 313 | + sqlx::query("DELETE FROM pending_facts WHERE id = $1") | |
| 314 | + .bind(id) | |
| 315 | + .execute(pool) | |
| 316 | + .await?; | |
| 317 | + Ok(Confirmed { | |
| 318 | + fact_id, | |
| 319 | + created, | |
| 320 | + conflicts, | |
| 321 | + snapshot: snapshot(&v), | |
| 322 | + }) | |
| 323 | +} | |
| 324 | + | |
| 325 | +/// 人拒绝:记进 `rejected_facts`(下一轮重抽先查它),从队列里拿掉。 | |
| 326 | +/// 那句记忆本身留着——人确实说过那句话,只是没抽出可用的事实。 | |
| 327 | +pub async fn reject( | |
| 328 | + pool: &PgPool, | |
| 329 | + kb_id: Uuid, | |
| 330 | + id: Uuid, | |
| 331 | + rejected_by: Option<Uuid>, | |
| 332 | +) -> AppResult<serde_json::Value> { | |
| 333 | + let v = get(pool, kb_id, id).await?; | |
| 334 | + sqlx::query( | |
| 335 | + "INSERT INTO rejected_facts (kb_id, subject_id, predicate_id, object_id, rejected_by) | |
| 336 | + VALUES ($1, $2, $3, $4, $5)", | |
| 337 | + ) | |
| 338 | + .bind(kb_id) | |
| 339 | + .bind(v.subject_id) | |
| 340 | + .bind(v.predicate_id) | |
| 341 | + .bind(v.object_id) | |
| 342 | + .bind(rejected_by) | |
| 343 | + .execute(pool) | |
| 344 | + .await?; | |
| 345 | + sqlx::query("DELETE FROM pending_facts WHERE id = $1") | |
| 346 | + .bind(id) | |
| 347 | + .execute(pool) | |
| 348 | + .await?; | |
| 349 | + Ok(snapshot(&v)) | |
| 350 | +} |
modified crates/utopia-store/src/review.rs +1 −0
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | pub async fn counts(pool: &PgPool, kb_id: Uuid) -> AppResult<ReviewCounts> { |
| 23 | 23 | Ok(sqlx::query_as( |
| 24 | 24 | "SELECT |
| 25 | + (SELECT count(*) FROM pending_facts WHERE kb_id = $1) AS pending, | |
| 25 | 26 | (SELECT count(*) FROM resolution_reviews |
| 26 | 27 | WHERE kb_id = $1 AND status = 'pending') AS duplicates, |
| 27 | 28 | (SELECT count(*) FROM fact_conflicts |
added crates/utopia-store/tests/a_fact_awaits_a_nod.rs +204 −0
| @@ -0,0 +1,204 @@ | ||
| 1 | +//! 记忆抽出的事实先等人点头(`docs/decisions/0015`,表在迁移 0018)——打在真库上。 | |
| 2 | +//! | |
| 3 | +//! 守的是那次实测的反面:对话里说「Acme 把总部搬到了深圳」,图上**不能**立刻多出 | |
| 4 | +//! 一条活边。提议只进 `pending_facts`;人点头之后它才按抽取那条路进账本(事实 + | |
| 5 | +//! 证据指回那句话 + 时态对账);摇头之后同一个三元组不再被提。 | |
| 6 | + | |
| 7 | +use sqlx::PgPool; | |
| 8 | +use uuid::Uuid; | |
| 9 | + | |
| 10 | +struct Fixture { | |
| 11 | + /// 夹具建的 org,收尾时从它删起(级联) | |
| 12 | + org: Uuid, | |
| 13 | + kb: Uuid, | |
| 14 | + acme: Uuid, | |
| 15 | + shenzhen: Uuid, | |
| 16 | + shanghai: Uuid, | |
| 17 | + hq: Uuid, | |
| 18 | + chunk: Uuid, | |
| 19 | +} | |
| 20 | + | |
| 21 | +async fn fixture(pool: &PgPool) -> anyhow::Result<Fixture> { | |
| 22 | + let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); | |
| 23 | + sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'nod-test')") | |
| 24 | + .bind(org) | |
| 25 | + .execute(pool) | |
| 26 | + .await?; | |
| 27 | + sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'nod-test')") | |
| 28 | + .bind(ws) | |
| 29 | + .bind(org) | |
| 30 | + .execute(pool) | |
| 31 | + .await?; | |
| 32 | + sqlx::query("INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'nod-test')") | |
| 33 | + .bind(kb) | |
| 34 | + .bind(ws) | |
| 35 | + .execute(pool) | |
| 36 | + .await?; | |
| 37 | + // 总部只有一个:functional 让时态对账有事可做 | |
| 38 | + let hq = Uuid::now_v7(); | |
| 39 | + sqlx::query( | |
| 40 | + "INSERT INTO relation_types (id, kb_id, key, label, functional) | |
| 41 | + VALUES ($1, $2, 'headquartered_in', 'headquartered in', TRUE)", | |
| 42 | + ) | |
| 43 | + .bind(hq) | |
| 44 | + .bind(kb) | |
| 45 | + .execute(pool) | |
| 46 | + .await?; | |
| 47 | + let (acme, shenzhen, shanghai) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); | |
| 48 | + for (id, name) in [ | |
| 49 | + (acme, "Acme"), | |
| 50 | + (shenzhen, "Shenzhen"), | |
| 51 | + (shanghai, "Shanghai"), | |
| 52 | + ] { | |
| 53 | + sqlx::query("INSERT INTO entities (id, kb_id, canonical_name) VALUES ($1, $2, $3)") | |
| 54 | + .bind(id) | |
| 55 | + .bind(kb) | |
| 56 | + .bind(name) | |
| 57 | + .execute(pool) | |
| 58 | + .await?; | |
| 59 | + } | |
| 60 | + // 那句记忆,走产品自己的路落成 chunk | |
| 61 | + let (_doc, chunk) = utopia_store::memory::append_episode( | |
| 62 | + pool, | |
| 63 | + kb, | |
| 64 | + "Acme moved its headquarters to Shenzhen on 2026-03-15.", | |
| 65 | + chrono::Utc::now(), | |
| 66 | + ) | |
| 67 | + .await?; | |
| 68 | + Ok(Fixture { | |
| 69 | + org, | |
| 70 | + kb, | |
| 71 | + acme, | |
| 72 | + shenzhen, | |
| 73 | + shanghai, | |
| 74 | + hq, | |
| 75 | + chunk, | |
| 76 | + }) | |
| 77 | +} | |
| 78 | + | |
| 79 | +fn day(s: &str) -> chrono::DateTime<chrono::Utc> { | |
| 80 | + chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") | |
| 81 | + .unwrap() | |
| 82 | + .and_hms_opt(0, 0, 0) | |
| 83 | + .unwrap() | |
| 84 | + .and_utc() | |
| 85 | +} | |
| 86 | + | |
| 87 | +async fn live_facts(pool: &PgPool, kb: Uuid) -> anyhow::Result<i64> { | |
| 88 | + let (n,): (i64,) = | |
| 89 | + sqlx::query_as("SELECT count(*) FROM facts WHERE kb_id = $1 AND invalidated_at IS NULL") | |
| 90 | + .bind(kb) | |
| 91 | + .fetch_one(pool) | |
| 92 | + .await?; | |
| 93 | + Ok(n) | |
| 94 | +} | |
| 95 | + | |
| 96 | +#[tokio::test] | |
| 97 | +async fn a_remembered_fact_waits_for_a_nod() -> anyhow::Result<()> { | |
| 98 | + let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { | |
| 99 | + eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); | |
| 100 | + return Ok(()); | |
| 101 | + }; | |
| 102 | + let pool = PgPool::connect(&url).await?; | |
| 103 | + let f = fixture(&pool).await?; | |
| 104 | + | |
| 105 | + let run = async { | |
| 106 | + use utopia_store::pending::{self, Outcome, Proposal}; | |
| 107 | + let propose = |object: Uuid, from: &'static str| { | |
| 108 | + pending::propose( | |
| 109 | + &pool, | |
| 110 | + Proposal { | |
| 111 | + kb_id: f.kb, | |
| 112 | + subject_id: f.acme, | |
| 113 | + predicate_id: Some(f.hq), | |
| 114 | + object_id: Some(object), | |
| 115 | + object_value: None, | |
| 116 | + proposed_predicate: Some("moved headquarters to"), | |
| 117 | + validity: utopia_store::graph::Validity { | |
| 118 | + from: Some(day(from)), | |
| 119 | + from_precision: Some("day"), | |
| 120 | + to: None, | |
| 121 | + to_precision: None, | |
| 122 | + }, | |
| 123 | + confidence: 0.9, | |
| 124 | + chunk_id: f.chunk, | |
| 125 | + proposed_by: None, | |
| 126 | + }, | |
| 127 | + ) | |
| 128 | + }; | |
| 129 | + | |
| 130 | + // 1. 提议不上图 | |
| 131 | + let first = propose(f.shenzhen, "2026-03-15").await?; | |
| 132 | + assert!(matches!(first, Outcome::Proposed(_)), "第一次该进队列"); | |
| 133 | + assert_eq!(live_facts(&pool, f.kb).await?, 0, "提议阶段图上不能有活边"); | |
| 134 | + let queued = pending::for_chunk(&pool, f.kb, f.chunk).await?; | |
| 135 | + assert_eq!(queued.len(), 1); | |
| 136 | + assert_eq!(queued[0].subject_name, "Acme"); | |
| 137 | + assert!(queued[0].quote.contains("Acme moved its headquarters"), "原句要跟着提议一起给人看"); | |
| 138 | + | |
| 139 | + // 2. 同一句重抽不重复提 | |
| 140 | + assert_eq!(propose(f.shenzhen, "2026-03-15").await?, Outcome::AlreadyPending); | |
| 141 | + | |
| 142 | + // 3. 点头:进账本,证据指回那句话,队列清空 | |
| 143 | + let Outcome::Proposed(id) = first else { unreachable!() }; | |
| 144 | + let done = pending::confirm(&pool, f.kb, id).await?; | |
| 145 | + assert!(done.created, "确认该落一条新事实"); | |
| 146 | + assert_eq!(live_facts(&pool, f.kb).await?, 1); | |
| 147 | + let (ev_chunk, ev_proposed): (Uuid, Option<String>) = sqlx::query_as( | |
| 148 | + "SELECT chunk_id, proposed_predicate FROM fact_evidence WHERE fact_id = $1", | |
| 149 | + ) | |
| 150 | + .bind(done.fact_id) | |
| 151 | + .fetch_one(&pool) | |
| 152 | + .await?; | |
| 153 | + assert_eq!(ev_chunk, f.chunk, "证据要指回那句记忆"); | |
| 154 | + assert_eq!(ev_proposed.as_deref(), Some("moved headquarters to")); | |
| 155 | + assert!(pending::for_chunk(&pool, f.kb, f.chunk).await?.is_empty()); | |
| 156 | + | |
| 157 | + // 4. 图上已有的不再问 | |
| 158 | + assert_eq!(propose(f.shenzhen, "2026-03-15").await?, Outcome::AlreadyAsserted); | |
| 159 | + | |
| 160 | + // 5. 摇头有记忆:拒过的三元组下一轮不再被提 | |
| 161 | + let Outcome::Proposed(bad) = propose(f.shanghai, "2020-01-01").await? else { | |
| 162 | + panic!("换一个宾语该是新提议"); | |
| 163 | + }; | |
| 164 | + pending::reject(&pool, f.kb, bad, None).await?; | |
| 165 | + assert_eq!(propose(f.shanghai, "2020-01-01").await?, Outcome::Rejected); | |
| 166 | + assert_eq!(live_facts(&pool, f.kb).await?, 1, "拒绝不碰图"); | |
| 167 | + | |
| 168 | + // 6. 点头走的是抽取那条路:functional 关系的新值闭合旧值。 | |
| 169 | + // 拒绝记录按 (主语, 谓词, 宾语) 查,同一个宾语换日期照样被挡—— | |
| 170 | + // 所以换一个宾语实体来演接任 | |
| 171 | + let beijing = Uuid::now_v7(); | |
| 172 | + sqlx::query("INSERT INTO entities (id, kb_id, canonical_name) VALUES ($1, $2, 'Beijing')") | |
| 173 | + .bind(beijing) | |
| 174 | + .bind(f.kb) | |
| 175 | + .execute(&pool) | |
| 176 | + .await?; | |
| 177 | + let Outcome::Proposed(next) = propose(beijing, "2027-01-01").await? else { | |
| 178 | + panic!("接任该是新提议"); | |
| 179 | + }; | |
| 180 | + let moved = pending::confirm(&pool, f.kb, next).await?; | |
| 181 | + assert!(moved.created); | |
| 182 | + assert_eq!(moved.conflicts, 0, "先后清楚、置信够高,该自动闭合而不是进冲突"); | |
| 183 | + let (closed,): (i64,) = sqlx::query_as( | |
| 184 | + "SELECT count(*) FROM facts | |
| 185 | + WHERE kb_id = $1 AND object_id = $2 AND invalidated_at IS NULL AND valid_to IS NOT NULL", | |
| 186 | + ) | |
| 187 | + .bind(f.kb) | |
| 188 | + .bind(f.shenzhen) | |
| 189 | + .fetch_one(&pool) | |
| 190 | + .await?; | |
| 191 | + assert_eq!(closed, 1, "深圳那条该被闭合成有终点的区间"); | |
| 192 | + Ok::<_, anyhow::Error>(()) | |
| 193 | + } | |
| 194 | + .await; | |
| 195 | + | |
| 196 | + // 删掉夹具自己建的 org,级联带走工作区、知识库与其余一切。 | |
| 197 | + // 从前只删知识库,每跑一次就在库里多留一对空的 org / workspace—— | |
| 198 | + // 「生产代码里没有删 org 这个动作」是对的,但夹具不是生产代码,自己建的自己收 | |
| 199 | + sqlx::query("DELETE FROM organizations WHERE id = $1") | |
| 200 | + .bind(f.org) | |
| 201 | + .execute(&pool) | |
| 202 | + .await?; | |
| 203 | + run | |
| 204 | +} |
modified docs/decisions/0015-recording-a-sentence-is-not-asserting-a-fact.md +23 −2
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # 0015 · 记下一句话,不等于断言一个事实 |
| 2 | 2 | |
| 3 | -- **状态**:进行中 · schema 已落(迁移 `0018_a_fact_awaiting_a_nod`,与本文同一 PR #180)· **抽取侧尚未接线,三条决定一条都没落地;`remember` 已被整体停用作为临时闸**(2026-09-02 核,修订见文末) | |
| 3 | +- **状态**:已实施 · schema 在迁移 `0018`(#180),运行时随 [0016](0016-close-the-open-seams-before-cutting-new-ones.md) A1 接上:抽取分流进 `pending_facts`、Review 新一档、对话里跟在 remember 后面的确认卡、`remember` 重新打开。**决定 3 的措辞与本文写的不同**,见文末最后一条修订 | |
| 4 | 4 | - **成文**:2026-09-01(约定见 [README](README.md)) |
| 5 | 5 | - **相关**:[0010](0010-no-relation-is-no-relation.md) 删掉兜底关系(本篇那条空谓词正是它的 |
| 6 | 6 | 正确行为)、[0011](0011-a-mapping-is-not-a-fact.md) 批过「拿浮点数编码二值状态」—— |
| @@ -136,12 +136,33 @@ | ||
| 136 | 136 | |
| 137 | 137 | **同一类问题的另一面**(#173,迁移 `0015_what_it_did_not_just_what_it_said`):跨轮回放助手上一轮的工具调用与结果,模型才知道自己做过什么,不会把上一轮的检索重跑一遍落到另一批同名实体上。本文讲的是「助手说的」与「图里得到的」之间的落差,那一条讲的是「助手说的」与「助手做的」之间的落差。 |
| 138 | 138 | |
| 139 | +## 修订记录:接上运行时之后,三处与本文不同 | |
| 140 | + | |
| 141 | +**一、助手说不出「抽出 N 条」。** 决定 3 写的是「已记录,抽出 N 条待你确认」。抽取是异步走队列的, | |
| 142 | +`remember` 回话的那一刻抽取还没跑,N 不存在。两条路:把记忆抽取改成同步(一句话十来秒,卡住整个对话循环), | |
| 143 | +或者如实说「这句话记下了,从中抽出的事实会先给你确认,确认前不进图」,等抽取完成时再把确认卡**长到对话里**。 | |
| 144 | +选了后者。卡片靠 SSE 的 `pending` 事件触发,按那句记忆的 chunk 取待确认项;回放旧会话时同样按 chunk 取, | |
| 145 | +还有没点头的就照样显示,都处理完了就不占地方。 | |
| 146 | + | |
| 147 | +**二、确认界面在两处,同一个组件。** Review 页新一档「等你点头」排在最前——它是人自己说的话,而且这一档里的 | |
| 148 | +东西还没进图,别处每一档审的都是已经在图上的。对话里那张卡是同一行组件。载荷是声明式的(chunk + 三元组列表), | |
| 149 | +将来若要让别人的客户端渲染它(MCP 那一侧),换的是渲染器不是服务端。 | |
| 150 | + | |
| 151 | +**三、确认走的正是抽取那条路。** 事实 + 证据(指回那句记忆,引句就是整句)+ 时态对账。所以「Mira 交给 | |
| 152 | +Devin 了」点头之后,Mira 那条会像从文档里抽出来时一样被闭合。置信度不动,人的态度在台账里 | |
| 153 | +(`fact.nod_confirmed` / `fact.nod_rejected`,快照自包含)。 | |
| 154 | + | |
| 155 | +**顺带定下的**:拒绝记录按(主语, 谓词, 宾语实体)查,**字面值事实不查**——`rejected_facts` 没有 `object_value` | |
| 156 | +列,按(主语, 谓词)挡会把「薪水 28000 被拒」扩大成「薪水这个属性永远不再提」,宁可多问一次。 | |
| 157 | +实体照常消解并创建,`pending_facts.subject_id` 是外键,这是 0018 定下的取舍;代价是几个暂时孤立的节点。 | |
| 158 | +「谁说的」从 `remember` 经 `memory_ingest` 与 `extract_document` 的任务载荷一路传到 `proposed_by`。 | |
| 159 | + | |
| 139 | 160 | ## 未决 |
| 140 | 161 | |
| 141 | 162 | - **闸只拦记忆,还是拦所有「单条、交互式」的写入?** 今天只有 `remember` 这一条路, |
| 142 | 163 | 但将来若有「在图上手工加一条边」的界面,它该走哪边? |
| 143 | 164 | - **确认之后置信度取什么。** 人点了头还留 0.9,还是升到 1.0?0011 的教训说别拿 |
| 144 | - confidence 表达人的态度,那大概是「不动它」——但要想清楚。 | |
| 165 | + confidence 表达人的态度,那大概是「不动它」——但要想清楚。〔**已定:不动**。人的态度在台账里。〕 | |
| 145 | 166 | - **拒绝之后那条记忆怎么办。** 文档还在(人确实说过那句话),只是没抽出可用的事实。 |
| 146 | 167 | 下一轮重抽会不会又提议一遍?`concept_mappings` 那边靠 `status='rejected'` 挡住了 |
| 147 | 168 | 重复提议,这里需要对应的东西。〔答了一半:`rejected_facts` 表与查重索引建好了,语义就是「这个三元组在这个库里被拒过」,但没有任何代码写它或查它。〕 |
modified docs/decisions/0016-close-the-open-seams-before-cutting-new-ones.md +5 −1
| @@ -50,7 +50,7 @@ | ||
| 50 | 50 | |
| 51 | 51 | ### A · 收口——两到三周,不加新概念 |
| 52 | 52 | |
| 53 | -**A1 · 把 0015 接上。** 抽取遇到记忆文档(`memory::is_memory_document`,已写好零调用)时写 `pending_facts` 而不是 `facts`; | |
| 53 | +**A1 · 把 0015 接上。**〔已做,形态与下文有两处不同,见 0015 末尾的修订:助手说不出 N,确认卡在对话里随抽取完成长出来。〕 抽取遇到记忆文档(`memory::is_memory_document`,已写好零调用)时写 `pending_facts` 而不是 `facts`; | |
| 54 | 54 | Review 加 `pending` 一档,界面**原句在上、三元组在下**;确认 → `insert_fact`,**置信度不动**(0011 的教训:别拿浮点数表达人的态度,人的态度在审计台账里); |
| 55 | 55 | 拒绝 → 写 `rejected_facts`,下一轮重抽先查它;`remember` 的回话改成「已记录,抽出 N 条待你确认」;然后把 `REMEMBER_ENABLED` 改回 true。 |
| 56 | 56 | **验收**:那条实测(「Acme 把总部搬到深圳」)跑一遍,图上不出现空谓词活边,Review 里出现一条带原句的待确认项。 |
| @@ -114,5 +114,9 @@ | ||
| 114 | 114 | - **A1 的闸只拦记忆,还是拦所有单条交互式写入?** 0015 未决。今天只有 `remember` 一条路,先拦它;将来「在图上手工加一条边」的界面出现时,按同一张表走。 |
| 115 | 115 | - **C3 生成的中文描述算不算「原文保真」的例外?** 它不是导入的原文,是我们生成的投影。倾向记成投影(可重跑、可丢弃),原文 IRI 与英文描述保留在包里。 |
| 116 | 116 | - **B4 的阈值**:10 秒是拍的。 |
| 117 | +- **对话内 agent 生成的界面走哪个协议。** A1 的确认卡载荷是声明式的(chunk + 三元组列表),渲染器是我们的 React。 | |
| 118 | + 讨论过 A2UI(2026-09-02):它解决的是「你不控制的客户端渲染你 agent 的 UI」,而 Chat 页两头都是我们的; | |
| 119 | + 卖点成立的地方在 MCP 那一侧,那里对应的是 MCP 自己的 UI 扩展,不是 A2UI。**判据是谁的客户端在看**, | |
| 120 | + 等 MCP 放开写工具、有外部客户端要渲染这张卡时再定。 | |
| 117 | 121 | - **要不要把抽取层拆出 `utopia-server`。** 今天约 6,500 行领域代码(extraction、type_resolution、bootstrap_ontology、predicate_match、adjudication、owl_import、连接器)住在 HTTP crate 里,依赖方向仍是干净的一条线。讨论过(2026-09-02)决定**不拆**:唯一实在的收益是脱离 server 测纯函数,而它们的单测今天已经不起 server;搬动 1,500 行的 `extraction.rs` 会让紧接着的 A1 diff 和 blame 变难看。判据留下:**等它造成实际损失再拆,损失的定义是「有东西没法脱离 server 测试」**。连接器那一块的边界等第三个连接器(飞书)来了再定。 |
| 118 | 122 | - **十万文档基准跑出来之后**,单进程 worker 与内嵌 Tantivy 的天花板在哪,决定了要不要在 v0.2 谈横向扩展。现在不谈。 |
modified docs/decisions/README.md +1 −1
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | | 0012 | [本体是一份契约,不只是一份建议](0012-the-ontology-is-a-contract-not-a-suggestion.md) | 已实施 · 违反率 57%→4%、反向 39→0 · 三条待做全未做 | |
| 41 | 41 | | 0013 | [一个来源该交出它的历史,不是它的现状](0013-a-source-should-hand-over-its-history.md) | 已实施两个(GitHub / Jira)· 文档协作类未开工 | |
| 42 | 42 | | 0014 | [身份跟着人,范围跟着令牌](0014-identity-from-the-person-scope-from-the-token.md) | 已实施(#180)· MCP 只读五工具 · **没有令牌界面** · 误导性的占位 crate 已删 | |
| 43 | -| 0015 | [记下一句话,不等于断言一个事实](0015-recording-a-sentence-is-not-asserting-a-fact.md) | 进行中 · schema 已落、**运行时零接线** · `remember` 整体停用作临时闸 | | |
| 43 | +| 0015 | [记下一句话,不等于断言一个事实](0015-recording-a-sentence-is-not-asserting-a-fact.md) | 已实施 · 记忆抽出的事实进 `pending_facts`,Review 新档 + 跟在 remember 步骤后的确认卡 · `remember` 重新打开 · MCP 放开写是下一刀 | | |
| 44 | 44 | | 0016 | [先把开着的口子收上,再开新的](0016-close-the-open-seams-before-cutting-new-ones.md) | 规划中 · v0.1.0 之后的排期:A 收口 → B 推理机 ∥ C 尺子与本体 → D 语义层 → E 企业交付;模拟引擎后置 | |
| 45 | 45 | |
| 46 | 46 | ## 不是决策记录的那些 |
modified web/src/api.ts +39 −0
| @@ -307,6 +307,7 @@ | ||
| 307 | 307 | /** 审核页的分档。**与服务端的 queue 参数是同一组字面量**——拼错会拿到 |
| 308 | 308 | * 一个明确的 unknown_queue 错误,而不是悄悄的空列表。 */ |
| 309 | 309 | export type ReviewQueue = |
| 310 | + | "pending" | |
| 310 | 311 | | "duplicates" |
| 311 | 312 | | "conflicts" |
| 312 | 313 | | "unconfirmed" |
| @@ -318,6 +319,8 @@ | ||
| 318 | 319 | |
| 319 | 320 | /** 各档的真实条数。左栏的徽标读它,不读列表长度 */ |
| 320 | 321 | export interface ReviewCounts { |
| 322 | + /** 记忆抽出、等人点头的事实(0015) */ | |
| 323 | + pending: number; | |
| 321 | 324 | duplicates: number; |
| 322 | 325 | conflicts: number; |
| 323 | 326 | unconfirmed: number; |
| @@ -472,6 +475,29 @@ | ||
| 472 | 475 | quote: string | null; |
| 473 | 476 | } |
| 474 | 477 | |
| 478 | +/** 一句记忆抽出、等人点头的事实(0015)。`quote` 是那句记忆的全文—— | |
| 479 | + * 确认界面把原句放在三元组上面,人对着原句判断,而不是凭空判断三元组。 */ | |
| 480 | +export interface PendingFactItem { | |
| 481 | + id: string; | |
| 482 | + subject_id: string; | |
| 483 | + subject_name: string; | |
| 484 | + predicate_id: string | null; | |
| 485 | + /** 本体里的关系名;为空时显示 `proposed_predicate`(斜体,标明是原话) */ | |
| 486 | + predicate_label: string | null; | |
| 487 | + proposed_predicate: string | null; | |
| 488 | + object_id: string | null; | |
| 489 | + object_name: string | null; | |
| 490 | + object_value: { value?: unknown; unit?: string; summary?: string } | null; | |
| 491 | + valid_from: string | null; | |
| 492 | + valid_to: string | null; | |
| 493 | + confidence: number; | |
| 494 | + chunk_id: string; | |
| 495 | + quote: string; | |
| 496 | + proposed_by: string | null; | |
| 497 | + proposed_by_name: string | null; | |
| 498 | + created_at: string; | |
| 499 | +} | |
| 500 | + | |
| 475 | 501 | /** 时态冲突(自动闭合拿不准的那些):旧事实 vs 新事实。 */ |
| 476 | 502 | export interface ConflictItem { |
| 477 | 503 | id: string; |
| @@ -785,6 +811,9 @@ | ||
| 785 | 811 | kind: "search" | "docs" | "entity" | "facts" | "changes" | "query" | "tool"; |
| 786 | 812 | label: string; |
| 787 | 813 | detail: string; |
| 814 | + /** `remember` 那一步带着它:那句记忆落成的 chunk。对话里的确认卡按它取 | |
| 815 | + * 待确认项(0015);回放时也据此重画 */ | |
| 816 | + chunk_id?: string; | |
| 788 | 817 | /** 这一步发生时正文已经有多长(UTF-16 码元,与 `string.length` 同一单位)。 |
| 789 | 818 | * 据此把轨迹穿回正文里,而不是全堆在最前面。 |
| 790 | 819 | * **这条迁移之前的消息没有它**——缺省时整段轨迹回到顶部,即旧的样子 */ |
| @@ -1521,6 +1550,16 @@ | ||
| 1521 | 1550 | method: "POST", |
| 1522 | 1551 | body: JSON.stringify(body), |
| 1523 | 1552 | }), |
| 1553 | + /** 一句记忆抽出的全部待确认项(0015)。空数组 = 这句话没有等着人的东西了 */ | |
| 1554 | + pendingForChunk: (kbId: string, chunkId: string) => | |
| 1555 | + request<{ items: PendingFactItem[] }>( | |
| 1556 | + `/api/v1/kbs/${kbId}/review/pending?chunk_id=${chunkId}`, | |
| 1557 | + ), | |
| 1558 | + decidePending: (kbId: string, pendingId: string, action: "confirm" | "reject") => | |
| 1559 | + request<{ ok: boolean; fact_id?: string; created?: boolean; conflicts?: number }>( | |
| 1560 | + `/api/v1/kbs/${kbId}/review/pending/${pendingId}`, | |
| 1561 | + { method: "POST", body: JSON.stringify({ action }) }, | |
| 1562 | + ), | |
| 1524 | 1563 | decideReview: (kbId: string, reviewId: string, action: "merge" | "keep") => |
| 1525 | 1564 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/review/${reviewId}`, { |
| 1526 | 1565 | method: "POST", |
modified web/src/i18n/en.ts +16 −0
| @@ -1171,6 +1171,7 @@ | ||
| 1171 | 1171 | empty: "Nothing to review — the graph is clean.", |
| 1172 | 1172 | historyEmpty: "No merges yet.", |
| 1173 | 1173 | // 左栏分类导航 |
| 1174 | + railPending: "Awaiting your nod", | |
| 1174 | 1175 | railDuplicates: "Duplicates", |
| 1175 | 1176 | railConflicts: "Conflicts", |
| 1176 | 1177 | railUnconfirmed: "Unconfirmed", |
| @@ -1192,6 +1193,8 @@ | ||
| 1192 | 1193 | "review.keep": "Kept apart", |
| 1193 | 1194 | "fact.confirm": "Confirmed", |
| 1194 | 1195 | "fact.reject": "Rejected", |
| 1196 | + "fact.nod_confirmed": "Confirmed a remembered fact", | |
| 1197 | + "fact.nod_rejected": "Rejected a remembered fact", | |
| 1195 | 1198 | "fact.close": "Closed", |
| 1196 | 1199 | "conflict.close_old": "Closed old", |
| 1197 | 1200 | "conflict.keep_both": "Kept both", |
| @@ -1273,6 +1276,19 @@ | ||
| 1273 | 1276 | mappingsHint: |
| 1274 | 1277 | "Proposed mappings from a business concept to how it is computed. Confirm one and Ask uses it instead of guessing from the schema.", |
| 1275 | 1278 | mappingDerived: "derived", |
| 1279 | + // 记忆抽出、等人点头的事实(0015) | |
| 1280 | + pending: "Awaiting your confirmation", | |
| 1281 | + pendingHint: | |
| 1282 | + "Facts extracted from what you asked the assistant to remember. Nothing here is in the graph yet: " + | |
| 1283 | + "the sentence is kept, the facts wait for your nod. Confirm to add with the sentence as evidence; " + | |
| 1284 | + "reject and it will not be proposed again.", | |
| 1285 | + pendingNoPredicate: "The ontology has no relation for this; the word is the model's own.", | |
| 1286 | + pendingNoPredicateChip: "no relation in ontology", | |
| 1287 | + pendingSaidBy: (name: string) => `said by ${name}`, | |
| 1288 | + nodCardTitle: (n: number) => | |
| 1289 | + n === 1 | |
| 1290 | + ? "One fact extracted from this. Confirm to add it to the graph, or reject." | |
| 1291 | + : `${n} facts extracted from this. Confirm to add them to the graph, or reject.`, | |
| 1276 | 1292 | lowConfidenceHint: |
| 1277 | 1293 | "Extracted with confidence below 75%. Confirm to trust, reject to remove from the graph (the ledger keeps the record).", |
| 1278 | 1294 | confirm: "Confirm", |
modified web/src/i18n/zh.ts +13 −0
| @@ -1054,6 +1054,7 @@ | ||
| 1054 | 1054 | tabHistory: "历史", |
| 1055 | 1055 | empty: "没有待审阅的——图谱是干净的。", |
| 1056 | 1056 | historyEmpty: "还没有合并记录。", |
| 1057 | + railPending: "等你点头", | |
| 1057 | 1058 | railDuplicates: "重复项", |
| 1058 | 1059 | railConflicts: "冲突", |
| 1059 | 1060 | railUnconfirmed: "未确认", |
| @@ -1074,6 +1075,8 @@ | ||
| 1074 | 1075 | "review.keep": "保持分开", |
| 1075 | 1076 | "fact.confirm": "已确认", |
| 1076 | 1077 | "fact.reject": "已驳回", |
| 1078 | + "fact.nod_confirmed": "已确认一条记忆事实", | |
| 1079 | + "fact.nod_rejected": "已驳回一条记忆事实", | |
| 1077 | 1080 | "fact.close": "已闭合", |
| 1078 | 1081 | "conflict.close_old": "闭合旧的", |
| 1079 | 1082 | "conflict.keep_both": "两条都留", |
| @@ -1150,6 +1153,16 @@ | ||
| 1150 | 1153 | mappingsHint: |
| 1151 | 1154 | "从业务概念到「这个数怎么算」的提议。确认之后问数直接用这个口径,而不是每次从 schema 猜。", |
| 1152 | 1155 | mappingDerived: "派生", |
| 1156 | + // 记忆抽出、等人点头的事实(0015) | |
| 1157 | + pending: "等你确认", | |
| 1158 | + pendingHint: | |
| 1159 | + "从你让助手记住的话里抽出的事实。这里的东西都还没进图:那句话已经存下,事实等你点头。" + | |
| 1160 | + "确认后以那句话为证据进账本;驳回后不会再被提议。", | |
| 1161 | + pendingNoPredicate: "本体里没有这个关系,这个词是模型自己的说法。", | |
| 1162 | + pendingNoPredicateChip: "本体里没有这个关系", | |
| 1163 | + pendingSaidBy: (name: string) => `${name} 说的`, | |
| 1164 | + nodCardTitle: (n: number) => | |
| 1165 | + n === 1 ? "从这句话里抽出 1 条事实。确认进图,或驳回。" : `从这句话里抽出 ${n} 条事实。确认进图,或驳回。`, | |
| 1153 | 1166 | lowConfidenceHint: |
| 1154 | 1167 | "抽取时置信度低于 75%。确认表示采信,驳回表示从图谱移除(台账仍留有记录)。", |
| 1155 | 1168 | confirm: "确认", |
modified web/src/pages/Chat.tsx +10 −4
| @@ -36,6 +36,7 @@ | ||
| 36 | 36 | import { useKb, useKbId } from "../kb"; |
| 37 | 37 | import { DangerConfirm, RAIL_CLS } from "../ui"; |
| 38 | 38 | import { liveAnswer, type Turn } from "../liveAnswer"; |
| 39 | +import { NodCard } from "./PendingFacts"; | |
| 39 | 40 | |
| 40 | 41 | /* `Turn` 定义在 liveAnswer 里:进行中的那一次也是一串 Turn, |
| 41 | 42 | 而它必须活得比这个组件长(见那个文件顶上的说明) */ |
| @@ -705,10 +706,15 @@ | ||
| 705 | 706 | className="my-2.5 space-y-1 border-l border-white/15 pl-2.5" |
| 706 | 707 | > |
| 707 | 708 | {seg.steps.map((s, j) => ( |
| 708 | - <div key={j} className="flex items-center gap-1.5 text-xs"> | |
| 709 | - <span className="text-neutral-600">{stepIcon(s.kind)}</span> | |
| 710 | - <span className="text-neutral-400 truncate">{s.label}</span> | |
| 711 | - <span className="text-neutral-600 shrink-0">· {s.detail}</span> | |
| 709 | + <div key={j}> | |
| 710 | + <div className="flex items-center gap-1.5 text-xs"> | |
| 711 | + <span className="text-neutral-600">{stepIcon(s.kind)}</span> | |
| 712 | + <span className="text-neutral-400 truncate">{s.label}</span> | |
| 713 | + <span className="text-neutral-600 shrink-0">· {s.detail}</span> | |
| 714 | + </div> | |
| 715 | + {/* remember 那一步后面跟着确认卡(0015):这句话抽出的事实先等人点头。 | |
| 716 | + 抽取是异步的,卡片在任务完成时才长出来;回放时按同一个 chunk 重画 */} | |
| 717 | + {s.chunk_id && <NodCard kbId={kbId} chunkId={s.chunk_id} />} | |
| 712 | 718 | </div> |
| 713 | 719 | ))} |
| 714 | 720 | </div> |
added web/src/pages/PendingFacts.tsx +150 −0
| @@ -0,0 +1,150 @@ | ||
| 1 | +/* 等人点头的事实(docs/decisions/0015)。 | |
| 2 | + 一句 remember 抽出的三元组先进待确认队列,不上图;人在这里点头它才进账本。 | |
| 3 | + **原句在上,三元组在下**:只列三元组等于要人凭空判断它对不对—— | |
| 4 | + 实测里 `Acme --?--> 深圳` 那条,人一看原句就知道该拒。 | |
| 5 | + 两处共用同一行组件:Review 页的「待确认」一档,与 Chat 里跟在 remember 步骤后面的那张卡。 */ | |
| 6 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | |
| 7 | +import { api, type PendingFactItem } from "../api"; | |
| 8 | +import { S } from "../i18n"; | |
| 9 | +import { toast } from "../toast"; | |
| 10 | + | |
| 11 | +function ym(iso: string | null): string | null { | |
| 12 | + return iso ? iso.slice(0, 7) : null; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/** 记忆落库时正文前面带着 `[YYYY-MM-DD HH:MM] `(`memory::append_episode` 加的时间戳)。 | |
| 16 | + * 卡片上要的是那句话本身,时间戳是索引用的,剥掉 */ | |
| 17 | +function sentence(quote: string): string { | |
| 18 | + return quote.replace(/^\[\d{4}-\d{2}-\d{2}(?: \d{2}:\d{2})?\]\s*/, ""); | |
| 19 | +} | |
| 20 | + | |
| 21 | +function objectText(f: PendingFactItem): string { | |
| 22 | + if (f.object_name) return f.object_name; | |
| 23 | + const v = f.object_value; | |
| 24 | + if (!v) return "?"; | |
| 25 | + if (v.summary) return v.summary; | |
| 26 | + const val = v.value === undefined || v.value === null ? "?" : String(v.value); | |
| 27 | + return v.unit ? `${val} ${v.unit}` : val; | |
| 28 | +} | |
| 29 | + | |
| 30 | +/** 点头是写图的动作,Editor 起步——与服务端 `require_kb(Role::Editor)` 同一口径。 | |
| 31 | + * Viewer 看得见提议、看不见按钮:按钮亮着却点不动,等于让人猜自己有没有权限。 | |
| 32 | + * 查询键与 Library 页共用(`kbOne`),不多打一次接口 */ | |
| 33 | +export function useCanDecide(kbId: string | undefined): boolean { | |
| 34 | + const kbDetail = useQuery({ | |
| 35 | + queryKey: ["kbOne", kbId], | |
| 36 | + queryFn: () => api.kbDetail(kbId!), | |
| 37 | + enabled: !!kbId, | |
| 38 | + }); | |
| 39 | + return ["editor", "admin", "owner"].includes(kbDetail.data?.my_role ?? ""); | |
| 40 | +} | |
| 41 | + | |
| 42 | +export function PendingFactRow({ | |
| 43 | + fact, | |
| 44 | + busy, | |
| 45 | + canDecide, | |
| 46 | + onConfirm, | |
| 47 | + onReject, | |
| 48 | +}: { | |
| 49 | + fact: PendingFactItem; | |
| 50 | + busy: boolean; | |
| 51 | + canDecide: boolean; | |
| 52 | + onConfirm: () => void; | |
| 53 | + onReject: () => void; | |
| 54 | +}) { | |
| 55 | + const from = ym(fact.valid_from); | |
| 56 | + const to = ym(fact.valid_to); | |
| 57 | + const range = from || to ? `${from ?? "…"} → ${to ?? S.review.ongoing}` : null; | |
| 58 | + return ( | |
| 59 | + <div className="glass rounded-xl p-4"> | |
| 60 | + {/* 原句先出。它是人自己说的,判断的依据就是它 */} | |
| 61 | + <p className="text-xs text-neutral-400 italic">“{sentence(fact.quote)}”</p> | |
| 62 | + <div className="mt-2.5 flex items-center gap-2 flex-wrap"> | |
| 63 | + <span className="text-sm font-medium text-white">{fact.subject_name}</span> | |
| 64 | + <span className="text-xs text-neutral-500"> | |
| 65 | + —{" "} | |
| 66 | + {fact.predicate_label ? ( | |
| 67 | + <span>{fact.predicate_label}</span> | |
| 68 | + ) : ( | |
| 69 | + /* 本体里没有这个关系:显示原话,斜体标明它不是词表里的词(0010) */ | |
| 70 | + <span | |
| 71 | + className="italic text-neutral-600" | |
| 72 | + title={S.review.pendingNoPredicate} | |
| 73 | + > | |
| 74 | + {fact.proposed_predicate ?? S.graph.unknownPredicate} | |
| 75 | + </span> | |
| 76 | + )}{" "} | |
| 77 | + → | |
| 78 | + </span> | |
| 79 | + <span className="text-sm font-medium text-white">{objectText(fact)}</span> | |
| 80 | + {range && <span className="text-xs text-neutral-500">({range})</span>} | |
| 81 | + {!fact.predicate_label && ( | |
| 82 | + <span className="u-chip u-chip-warn ml-auto">{S.review.pendingNoPredicateChip}</span> | |
| 83 | + )} | |
| 84 | + </div> | |
| 85 | + <div className="mt-3 flex items-center gap-2"> | |
| 86 | + {fact.proposed_by_name && ( | |
| 87 | + <span className="text-[11px] text-neutral-600"> | |
| 88 | + {S.review.pendingSaidBy(fact.proposed_by_name)} | |
| 89 | + </span> | |
| 90 | + )} | |
| 91 | + {canDecide && ( | |
| 92 | + <div className="ml-auto flex gap-2"> | |
| 93 | + <button | |
| 94 | + className="u-btn u-btn-ghost px-3 py-1.5 text-xs text-[var(--u-danger)]" | |
| 95 | + disabled={busy} | |
| 96 | + onClick={onReject} | |
| 97 | + > | |
| 98 | + {S.review.reject} | |
| 99 | + </button> | |
| 100 | + <button | |
| 101 | + className="u-btn u-btn-ghost px-3 py-1.5 text-xs" | |
| 102 | + disabled={busy} | |
| 103 | + onClick={onConfirm} | |
| 104 | + > | |
| 105 | + {S.review.confirm} | |
| 106 | + </button> | |
| 107 | + </div> | |
| 108 | + )} | |
| 109 | + </div> | |
| 110 | + </div> | |
| 111 | + ); | |
| 112 | +} | |
| 113 | + | |
| 114 | +/** 跟在 remember 步骤后面的确认卡。 | |
| 115 | + * 抽取是异步的,卡片在任务完成时才长出来(SSE `pending` 事件让查询失效重取); | |
| 116 | + * 回放旧会话时同样按 chunk 取——还有没点头的就照样显示,都处理完了就不占地方。 */ | |
| 117 | +export function NodCard({ kbId, chunkId }: { kbId: string; chunkId: string }) { | |
| 118 | + const queryClient = useQueryClient(); | |
| 119 | + const canDecide = useCanDecide(kbId); | |
| 120 | + const q = useQuery({ | |
| 121 | + queryKey: ["pending", kbId, chunkId], | |
| 122 | + queryFn: () => api.pendingForChunk(kbId, chunkId), | |
| 123 | + }); | |
| 124 | + const decide = useMutation({ | |
| 125 | + mutationFn: ({ id, action }: { id: string; action: "confirm" | "reject" }) => | |
| 126 | + api.decidePending(kbId, id, action), | |
| 127 | + onError: (e: Error) => toast.error(e.message), | |
| 128 | + onSettled: () => { | |
| 129 | + queryClient.invalidateQueries({ queryKey: ["pending", kbId] }); | |
| 130 | + queryClient.invalidateQueries({ queryKey: ["review", kbId] }); | |
| 131 | + }, | |
| 132 | + }); | |
| 133 | + const items = q.data?.items ?? []; | |
| 134 | + if (items.length === 0) return null; | |
| 135 | + return ( | |
| 136 | + <div className="my-2 space-y-2"> | |
| 137 | + <div className="text-xs text-neutral-500">{S.review.nodCardTitle(items.length)}</div> | |
| 138 | + {items.map((f) => ( | |
| 139 | + <PendingFactRow | |
| 140 | + key={f.id} | |
| 141 | + fact={f} | |
| 142 | + busy={decide.isPending && decide.variables?.id === f.id} | |
| 143 | + canDecide={canDecide} | |
| 144 | + onConfirm={() => decide.mutate({ id: f.id, action: "confirm" })} | |
| 145 | + onReject={() => decide.mutate({ id: f.id, action: "reject" })} | |
| 146 | + /> | |
| 147 | + ))} | |
| 148 | + </div> | |
| 149 | + ); | |
| 150 | +} |
modified web/src/pages/Review.tsx +55 −0
| @@ -10,10 +10,12 @@ | ||
| 10 | 10 | type ConflictItem, |
| 11 | 11 | type FactReviewItem, |
| 12 | 12 | type MergeLog, |
| 13 | + type PendingFactItem, | |
| 13 | 14 | type ReviewHistoryEvent, |
| 14 | 15 | type ReviewItem, |
| 15 | 16 | type ReviewSide, |
| 16 | 17 | } from "../api"; |
| 18 | +import { PendingFactRow, useCanDecide } from "./PendingFacts"; | |
| 17 | 19 | import { S } from "../i18n"; |
| 18 | 20 | import { useKb, useKbId } from "../kb"; |
| 19 | 21 | import { Chip, type ChipTone, Pager, RAIL_CLS, cn } from "../ui"; |
| @@ -597,6 +599,9 @@ | ||
| 597 | 599 | /* ---------- 页面:左栏分类 + 单类内容区 ---------- */ |
| 598 | 600 | |
| 599 | 601 | type Sel = |
| 602 | + // 记忆抽出、等人点头的事实(0015)。排第一:它是人自己说的话, | |
| 603 | + // 而且在这一档里的东西**还没进图**——别处每一档审的都是已经在图上的 | |
| 604 | + | "pending" | |
| 600 | 605 | | "duplicates" |
| 601 | 606 | | "conflicts" |
| 602 | 607 | | "unconfirmed" |
| @@ -611,6 +616,7 @@ | ||
| 611 | 616 | |
| 612 | 617 | /** 走服务端分页的那几档(决策台账另有自己的接口) */ |
| 613 | 618 | const QUEUE_FETCHED: ReviewQueue[] = [ |
| 619 | + "pending", | |
| 614 | 620 | "duplicates", |
| 615 | 621 | "conflicts", |
| 616 | 622 | "unconfirmed", |
| @@ -621,6 +627,7 @@ | ||
| 621 | 627 | ]; |
| 622 | 628 | |
| 623 | 629 | const QUEUE_ORDER: Sel[] = [ |
| 630 | + "pending", | |
| 624 | 631 | "duplicates", |
| 625 | 632 | "conflicts", |
| 626 | 633 | "unconfirmed", |
| @@ -629,6 +636,7 @@ | ||
| 629 | 636 | "defects", |
| 630 | 637 | ]; |
| 631 | 638 | const PAGE_SIZE: Record<Sel, number> = { |
| 639 | + pending: FACT_PAGE, | |
| 632 | 640 | duplicates: DUP_PAGE, |
| 633 | 641 | conflicts: CONFLICT_PAGE, |
| 634 | 642 | unconfirmed: FACT_PAGE, |
| @@ -749,6 +757,22 @@ | ||
| 749 | 757 | : api.rejectFact(kb!.id, id), |
| 750 | 758 | onSettled: invalidate, |
| 751 | 759 | }); |
| 760 | + // 等人点头的事实(0015):确认进账本,驳回记进 rejected_facts | |
| 761 | + // 点头是写图的动作:Viewer 看得见提议、看不见按钮(与服务端 Editor 门槛同口径) | |
| 762 | + const canDecidePending = useCanDecide(kb?.id); | |
| 763 | + const pendingAction = useMutation({ | |
| 764 | + mutationFn: ({ | |
| 765 | + id, | |
| 766 | + action, | |
| 767 | + }: { | |
| 768 | + id: string; | |
| 769 | + action: "confirm" | "reject"; | |
| 770 | + }) => api.decidePending(kb!.id, id, action), | |
| 771 | + onSettled: () => { | |
| 772 | + invalidate(); | |
| 773 | + queryClient.invalidateQueries({ queryKey: ["pending", kb?.id] }); | |
| 774 | + }, | |
| 775 | + }); | |
| 752 | 776 | const defectAction = useMutation({ |
| 753 | 777 | mutationFn: ({ |
| 754 | 778 | id, |
| @@ -804,6 +828,7 @@ | ||
| 804 | 828 | // mappings 不是本页的一档(审批在「数据映射」页),但计数照收: |
| 805 | 829 | // 收件箱该说「有几条等你」 |
| 806 | 830 | const counts: Record<Sel | "mappings", number> = { |
| 831 | + pending: c?.pending ?? 0, | |
| 807 | 832 | duplicates: c?.duplicates ?? 0, |
| 808 | 833 | conflicts: c?.conflicts ?? 0, |
| 809 | 834 | unconfirmed: c?.unconfirmed ?? 0, |
| @@ -817,6 +842,7 @@ | ||
| 817 | 842 | // 当前这一档的一页。**服务端已经切好了**,这里只按档收窄类型—— |
| 818 | 843 | // 收窄错了会在渲染时露馅,而不是悄悄显示空列表 |
| 819 | 844 | const rows = review.data?.queue === queueSel ? (review.data.items ?? []) : []; |
| 845 | + const asPending = () => rows as PendingFactItem[]; | |
| 820 | 846 | const asDuplicates = () => rows as ReviewItem[]; |
| 821 | 847 | const asFacts = () => rows as FactReviewItem[]; |
| 822 | 848 | const asConflicts = () => rows as ConflictItem[]; |
| @@ -841,6 +867,7 @@ | ||
| 841 | 867 | const isQueueSel = QUEUE_ORDER.includes(active); |
| 842 | 868 | |
| 843 | 869 | const SECTION: Record<Sel, { title: string; hint: string | null }> = { |
| 870 | + pending: { title: S.review.pending, hint: S.review.pendingHint }, | |
| 844 | 871 | duplicates: { title: S.review.duplicates, hint: S.review.duplicatesHint }, |
| 845 | 872 | conflicts: { title: S.review.conflicts, hint: S.review.conflictsHint }, |
| 846 | 873 | unconfirmed: { |
| @@ -869,6 +896,12 @@ | ||
| 869 | 896 | <aside className={`${RAIL_CLS} flex flex-col overflow-y-auto u-scroll`}> |
| 870 | 897 | <RailHeader label={S.review.tabQueue} /> |
| 871 | 898 | <div className="px-2 space-y-0.5"> |
| 899 | + <RailItem | |
| 900 | + active={active === "pending"} | |
| 901 | + label={S.review.railPending} | |
| 902 | + count={counts.pending} | |
| 903 | + onClick={() => select("pending")} | |
| 904 | + /> | |
| 872 | 905 | <RailItem |
| 873 | 906 | active={active === "duplicates"} |
| 874 | 907 | label={S.review.railDuplicates} |
| @@ -1032,6 +1065,28 @@ | ||
| 1032 | 1065 | </div> |
| 1033 | 1066 | )} |
| 1034 | 1067 | |
| 1068 | + {active === "pending" && counts.pending > 0 && ( | |
| 1069 | + <div className="space-y-3"> | |
| 1070 | + {asPending().map((fact) => ( | |
| 1071 | + <PendingFactRow | |
| 1072 | + key={fact.id} | |
| 1073 | + fact={fact} | |
| 1074 | + canDecide={canDecidePending} | |
| 1075 | + busy={ | |
| 1076 | + pendingAction.isPending && | |
| 1077 | + pendingAction.variables?.id === fact.id | |
| 1078 | + } | |
| 1079 | + onConfirm={() => | |
| 1080 | + pendingAction.mutate({ id: fact.id, action: "confirm" }) | |
| 1081 | + } | |
| 1082 | + onReject={() => | |
| 1083 | + pendingAction.mutate({ id: fact.id, action: "reject" }) | |
| 1084 | + } | |
| 1085 | + /> | |
| 1086 | + ))} | |
| 1087 | + </div> | |
| 1088 | + )} | |
| 1089 | + | |
| 1035 | 1090 | {active === "lowconf" && counts.lowconf > 0 && ( |
| 1036 | 1091 | <div className="space-y-3"> |
| 1037 | 1092 | {asFacts().map((fact) => ( |
modified web/src/useKbEvents.ts +5 −0
| @@ -19,6 +19,11 @@ | ||
| 19 | 19 | es.addEventListener("review", () => { |
| 20 | 20 | queryClient.invalidateQueries({ queryKey: ["review", kbId] }); |
| 21 | 21 | }); |
| 22 | + // 一句记忆抽出了等人点头的事实(0015):对话里那张确认卡跟着长出来 | |
| 23 | + es.addEventListener("pending", () => { | |
| 24 | + queryClient.invalidateQueries({ queryKey: ["pending", kbId] }); | |
| 25 | + queryClient.invalidateQueries({ queryKey: ["review", kbId] }); | |
| 26 | + }); | |
| 22 | 27 | es.addEventListener("source", () => { |
| 23 | 28 | queryClient.invalidateQueries({ queryKey: ["sources", kbId] }); |
| 24 | 29 | queryClient.invalidateQueries({ queryKey: ["documents", kbId] }); |