Elffuss BraidHub

kikocis/utopia public

a5cb6ee00a00 · 41.2 KiBRawPermalink
1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
6pub struct Organization {
7 pub id: Uuid,
8 pub name: String,
9 pub created_at: DateTime<Utc>,
10}
11
12#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
13pub struct User {
14 pub id: Uuid,
15 pub org_id: Uuid,
16 pub email: String,
17 #[serde(skip_serializing)]
18 pub password_hash: String,
19 pub display_name: String,
20 /// 系统管理员(部署的首个注册用户)
21 pub is_admin: bool,
22 pub created_at: DateTime<Utc>,
23}
24
25/// 工作区成员视图(成员管理页用)。
26#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
27pub struct MemberView {
28 pub user_id: Uuid,
29 pub email: String,
30 pub display_name: String,
31 pub role: String,
32 pub is_admin: bool,
33}
34
35/// 部署内用户列表(添加成员的选人器用)。
36#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
37pub struct OrgUser {
38 pub id: Uuid,
39 pub email: String,
40 pub display_name: String,
41 pub is_admin: bool,
42}
43
44#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
45pub struct Workspace {
46 pub id: Uuid,
47 pub org_id: Uuid,
48 pub name: String,
49 pub created_at: DateTime<Utc>,
50}
51
52/// 成员角色,按权限从高到低排序。数据库中存小写文本。
53#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
54#[serde(rename_all = "lowercase")]
55pub enum Role {
56 Viewer,
57 Editor,
58 Admin,
59 Owner,
60}
61
62impl Role {
63 pub fn as_str(&self) -> &'static str {
64 match self {
65 Role::Owner => "owner",
66 Role::Admin => "admin",
67 Role::Editor => "editor",
68 Role::Viewer => "viewer",
69 }
70 }
71
72 pub fn parse(s: &str) -> Option<Role> {
73 match s {
74 "owner" => Some(Role::Owner),
75 "admin" => Some(Role::Admin),
76 "editor" => Some(Role::Editor),
77 "viewer" => Some(Role::Viewer),
78 _ => None,
79 }
80 }
81}
82
83#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
84pub struct Document {
85 pub id: Uuid,
86 pub kb_id: Uuid,
87 pub source_id: Option<Uuid>,
88 pub filename: String,
89 pub mime: String,
90 pub size_bytes: i64,
91 pub sha256: String,
92 /// pending → parsing → indexing → embedding → ready | failed
93 pub status: String,
94 pub error: Option<String>,
95 pub doc_time: Option<DateTime<Utc>>,
96 pub doc_time_source: String,
97 /// 图谱抽取状态:none → queued → extracting → done | failed
98 pub graph_status: String,
99 /// 抽取失败原因(失败时才有)。与 error 分列——那列归解析管道,
100 /// set_status 会清空它,两者共用一列会互相抹掉。
101 pub graph_error: Option<String>,
102 pub text_len: i32,
103 pub chunk_count: i32,
104 pub tags: Vec<String>,
105 /// 来源内的逻辑身份(相对路径 / url / rss guid / api external_id);上传为 NULL
106 pub external_key: Option<String>,
107 /// watch_folder 同步时发现源文件已消失(默认保留文档,仅标记)
108 pub missing_since: Option<DateTime<Utc>>,
109 pub created_at: DateTime<Utc>,
110 pub updated_at: DateTime<Utc>,
111}
112
113/// 摄入来源("来源即文件夹":容器 + 定时同步)。
114#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
115pub struct Source {
116 pub id: Uuid,
117 pub kb_id: Uuid,
118 /// upload | watch_folder | url | rss | api
119 pub kind: String,
120 pub name: String,
121 /// kind 专属配置:watch_folder {path} / url {urls:[..]} / rss {feed_url}
122 pub config: serde_json::Value,
123 /// lucide 图标名(NULL 时前端按 kind 取默认)
124 pub icon: Option<String>,
125 /// NULL = 仅手动同步(与 sync_cron 互斥)
126 pub sync_interval_minutes: Option<i32>,
127 /// 标准 5 段 cron(服务器本地时区;与 sync_interval_minutes 互斥)
128 pub sync_cron: Option<String>,
129 pub last_sync_at: Option<DateTime<Utc>>,
130 /// never | queued | running | ok | failed
131 pub last_sync_status: String,
132 pub last_sync_error: Option<String>,
133 pub last_sync_added: i32,
134 /// api 来源的推送密钥(明文;查看走 Editor 权限的专用端点,列表响应不带)
135 #[serde(skip_serializing)]
136 pub ingest_token: Option<String>,
137 pub created_at: DateTime<Utc>,
138}
139
140/// 来源同步运行记录(渠道审计历史)。
141#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
142pub struct SyncRun {
143 pub id: Uuid,
144 pub started_at: DateTime<Utc>,
145 pub finished_at: Option<DateTime<Utc>>,
146 /// running | ok | failed
147 pub status: String,
148 pub created_docs: i32,
149 pub updated_docs: i32,
150 pub error: Option<String>,
151}
152
153/// 分块的抽取产物视图(文档查看器右栏:这个 chunk 抽出了什么)。
154#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
155pub struct ChunkFactView {
156 pub chunk_id: Uuid,
157 pub fact_id: Uuid,
158 pub subject_id: Uuid,
159 pub subject: String,
160 /// 本体没认下这条关系时回落到原文说法;两者都拿不出时为 None(更早的历史数据长这样)
161 pub predicate: Option<String>,
162 pub inferred: bool,
163 pub object_id: Option<Uuid>,
164 pub object: Option<String>,
165 pub valid_from: Option<DateTime<Utc>>,
166 pub valid_to: Option<DateTime<Utc>>,
167 pub confidence: f32,
168}
169
170/// 来源列表视图(带文档数)。
171#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
172pub struct SourceView {
173 pub id: Uuid,
174 pub kind: String,
175 pub name: String,
176 pub config: serde_json::Value,
177 pub icon: Option<String>,
178 pub sync_interval_minutes: Option<i32>,
179 pub sync_cron: Option<String>,
180 pub last_sync_at: Option<DateTime<Utc>>,
181 pub last_sync_status: String,
182 pub last_sync_error: Option<String>,
183 pub last_sync_added: i32,
184 pub doc_count: i64,
185 /// 已标记"不在来源中"的文档数(url 全集对账 / custom 墓碑产生)
186 pub missing_count: i64,
187}
188
189/// 审计事件视图(带操作人显示名;删号后为 NULL)。纯审计展示用。
190#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
191pub struct AuditEventView {
192 pub id: Uuid,
193 pub action: String,
194 pub target_kind: String,
195 pub target_id: Option<Uuid>,
196 pub detail: serde_json::Value,
197 /// NULL = 引擎自动(裁决器合并、一致性检查、推理物化……)。界面靠它把
198 /// 「没有人」和「人已被移除」分开:后者 actor_id 还在,只是查不到显示名
199 pub actor_id: Option<Uuid>,
200 pub actor_name: Option<String>,
201 pub created_at: DateTime<Utc>,
202}
203
204/// 账户层"我的知识库"行信息(成员行可空:open 库凭部署身份进入,无矩阵记录)。
205#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
206pub struct MyKbInfo {
207 pub kb_id: Uuid,
208 pub member_role: Option<String>,
209 pub joined_at: Option<DateTime<Utc>>,
210 pub added_by_name: Option<String>,
211 pub doc_count: i64,
212 pub member_count: i64,
213}
214
215/// Chat 会话行(左栏列表)。
216#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
217pub struct ConversationView {
218 pub id: Uuid,
219 pub title: String,
220 pub created_at: DateTime<Utc>,
221 pub updated_at: DateTime<Utc>,
222 pub message_count: i64,
223}
224
225/// Chat 消息(含落库的行动轨迹与引用,历史回放用)。
226#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
227pub struct ConversationMessage {
228 pub id: Uuid,
229 pub role: String,
230 pub content: String,
231 pub steps: serde_json::Value,
232 pub sources: serde_json::Value,
233 pub created_at: DateTime<Utc>,
234}
235
236/// 检索结果用的分块视图(带文档信息)。
237#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
238pub struct ChunkView {
239 pub id: Uuid,
240 pub document_id: Uuid,
241 pub seq: i32,
242 pub text: String,
243 pub filename: String,
244}
245
246#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
247pub struct LlmSettings {
248 pub workspace_id: Uuid,
249 pub chat_base_url: Option<String>,
250 #[serde(skip_serializing)]
251 pub chat_api_key: Option<String>,
252 pub chat_model: Option<String>,
253 pub embed_base_url: Option<String>,
254 #[serde(skip_serializing)]
255 pub embed_api_key: Option<String>,
256 pub embed_model: Option<String>,
257 pub embed_dim: Option<i32>,
258 pub updated_at: DateTime<Utc>,
259}
260
261impl LlmSettings {
262 pub fn chat_ready(&self) -> bool {
263 self.chat_base_url.is_some() && self.chat_model.is_some()
264 }
265 pub fn embed_ready(&self) -> bool {
266 self.embed_base_url.is_some() && self.embed_model.is_some()
267 }
268}
269
270#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
271pub struct EntityType {
272 pub id: Uuid,
273 pub kb_id: Uuid,
274 pub key: String,
275 pub label: String,
276 pub color: String,
277 /// 图谱节点形状:circle | square
278 pub shape: String,
279 pub builtin: bool,
280 /// subClassOf 层级(公理推理 P4 点亮,编辑器先维护数据)
281 /// 全部父类(subClassOf 可以有多个:FOAF 的 Person 同时是 Agent 与 SpatialThing)
282 pub parents: Vec<Uuid>,
283 /// 左栏画树时挂在哪一支下。不参与语义,只管展示
284 pub primary_parent: Option<Uuid>,
285 /// OWL 导入的全局身份。手工建的类为 NULL;重导入按它匹配,不按 key——
286 /// 上游改一次 rdfs:label 派生的 key 就变了,按 key 匹配会把同一个类当新类建
287 pub iri: Option<String>,
288 /// 语义指引:注入抽取 prompt(什么算这个类,举例)
289 pub description: String,
290}
291
292/// 本体编辑器:某个类下的实体实例行(详情区实例列表用)。
293#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
294pub struct EntityInstance {
295 pub id: Uuid,
296 pub name: String,
297 pub fact_count: i64,
298}
299
300/// 本体编辑器视图:类型 + 使用量(删除保护与 UX 提示用)。
301#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
302pub struct EntityTypeView {
303 pub id: Uuid,
304 pub key: String,
305 pub label: String,
306 pub color: String,
307 pub shape: String,
308 pub builtin: bool,
309 /// 全部父类(subClassOf 可以有多个:FOAF 的 Person 同时是 Agent 与 SpatialThing)
310 pub parents: Vec<Uuid>,
311 /// 左栏画树时挂在哪一支下。不参与语义,只管展示
312 pub primary_parent: Option<Uuid>,
313 /// 与这个类互斥的类:**声明「不可能同时是」**。一致性检查据此报出
314 /// 不可满足的类——一个类继承了两个互斥的祖先,就永远不可能有实例(0002)
315 pub disjoint: Vec<Uuid>,
316 pub description: String,
317 pub usage: i64,
318}
319
320#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
321pub struct RelationTypeView {
322 pub id: Uuid,
323 pub key: String,
324 pub label: String,
325 pub temporal: String,
326 pub functional: bool,
327 pub inverse_functional: bool,
328 /// 其余四条 OWL 公理。**推理机的判据全在这里**(0002)——它们从前只能
329 /// 靠导入 OWL 带进来,在界面上建本体的人永远开不了那台机器
330 pub is_transitive: bool,
331 pub is_symmetric: bool,
332 pub is_asymmetric: bool,
333 pub is_irreflexive: bool,
334 /// 指向另一个关系的两条。**必须回给界面**——下拉框要显示当前选的是谁,
335 /// 否则每次打开表单都是空的,编辑一次就把已声明的抹掉了
336 pub inverse_of: Option<Uuid>,
337 pub sub_property_of: Option<Uuid>,
338 pub builtin: bool,
339 pub description: String,
340 /// relation(宾语是实体)| attribute(宾语是字面值)
341 pub kind: String,
342 /// 可以当主语的类。attribute 至少一个;relation 可空(未声明 = 不限)
343 pub domains: Vec<Uuid>,
344 /// 可以当宾语的类。**只对 relation 有意义**——attribute 的值域是字面量类型,
345 /// 落在 datatype 上
346 pub ranges: Vec<Uuid>,
347 /// attribute 专用:text | number | date | bool
348 pub datatype: Option<String>,
349 pub unit: Option<String>,
350 pub usage: i64,
351}
352
353/// 抽取未匹配统计(本体扩展建议的信号源)。
354#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
355pub struct OntologyMiss {
356 pub kind: String,
357 pub key: String,
358 pub example: Option<String>,
359 pub count: i32,
360}
361
362/// 一个待认领的表层谓词:原文这么说过,但本体里没有对应关系,事实降级成了
363/// related_to。与 `OntologyMiss` 的纯计数不同,它连着具体事实——所以采纳时
364/// 能说清"将重新归类 57 条",并真的去改。
365#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
366pub struct ProposedPredicate {
367 pub form: String,
368 /// 有多少条 live 的 related_to 事实由这个说法而来
369 pub fact_count: i64,
370 /// 出现在多少篇文档里。只在一篇里出现过的是那篇文档的用词,不是这个
371 /// 组织的词汇——自动扩展据此设门槛,人工提案只作参考不拦
372 pub doc_count: i64,
373 /// 一条样例("Dino Crisis (Steam) → GeForce NOW"),让人一眼判断这是什么关系
374 pub example: Option<String>,
375}
376
377/// 一次 OWL 导入的记录。原文按内容寻址存在 blob 里,这行只是账。
378/// `summary` 记下那次投影做了什么,包括**暂未投影**的公理——将来补上消费者
379/// 时据此知道哪些导入值得重跑。
380#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
381pub struct OntologyImportView {
382 pub id: Uuid,
383 pub filename: String,
384 pub format: String,
385 pub byte_size: i64,
386 pub summary: serde_json::Value,
387 pub imported_at: DateTime<Utc>,
388 pub imported_by_name: Option<String>,
389}
390
391/// 一个模型的并发上限。约束来自供应商的速率限制,那是按 (base_url, model) 算的——
392/// 本地 Ollama 与托管 API 用同一个数字本来就不对。
393#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
394pub struct ModelLimit {
395 pub base_url: String,
396 pub model: String,
397 pub max_concurrent: i32,
398}
399
400/// 一个待认领的实体类型:模型提议过、本体没有、实体因此降级成了 concept。
401/// 与 `ProposedPredicate` 对称——它连着具体实体,所以采纳时能说清"将重新归类
402/// 43 个"并真的去改,而不是只建一个空类。
403#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
404pub struct ProposedType {
405 pub form: String,
406 pub entity_count: i64,
407 /// 一个样例名字,让人一眼判断这是什么类
408 pub example: Option<String>,
409}
410
411/// 抽取丢弃信号:事实抽出来了却没能落地,以及为什么。
412/// 与 `OntologyMiss` 分开——那个说"你的本体缺这些"(读者是本体维护者),
413/// 这个说"这些事实没落地"(读者是上传文档的人)。
414#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
415pub struct ExtractionDrop {
416 pub document_id: Uuid,
417 pub reason: String,
418 pub detail: String,
419 pub count: i32,
420 pub example: Option<String>,
421}
422
423#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
424pub struct RelationType {
425 pub id: Uuid,
426 pub kb_id: Uuid,
427 pub key: String,
428 pub label: String,
429 /// state | event | eternal
430 pub temporal: String,
431 /// 主语侧唯一:同一时刻一个主语至多一个宾语
432 pub functional: bool,
433 /// 宾语侧唯一:同一时刻一个宾语至多一个主语(如一个项目只有一个 leads 它的人)
434 pub inverse_functional: bool,
435 pub builtin: bool,
436 /// 语义指引:注入抽取 prompt
437 pub description: String,
438 /// OWL 导入的全局身份;手工建的为 NULL。重导入按它匹配,不按 key——
439 /// 上游改一次 rdfs:label 派生的 key 就变了,按 key 匹配会把同一个当成新的
440 pub iri: Option<String>,
441 /// relation(宾语是实体)| attribute(宾语是字面值,走 facts.object_value)
442 pub kind: String,
443 /// 可以当主语的类(多值:OWL 里一个属性有多个 rdfs:domain 是常态)
444 pub domains: Vec<Uuid>,
445 /// 可以当宾语的类。只对 relation 有意义
446 pub ranges: Vec<Uuid>,
447 /// attribute 专用:text | number | date | bool
448 pub datatype: Option<String>,
449 pub unit: Option<String>,
450}
451
452#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
453pub struct Entity {
454 pub id: Uuid,
455 pub kb_id: Uuid,
456 /// `None` = 还没判出来。见 `docs/decisions/0009`——它不是一个类,
457 /// 是「抽取器抽到了东西,但本体里没有对应的类」这个状态
458 pub type_id: Option<Uuid>,
459 pub canonical_name: String,
460 pub aliases: Vec<String>,
461 pub merged_into: Option<Uuid>,
462 pub created_at: DateTime<Utc>,
463 pub updated_at: DateTime<Utc>,
464}
465
466/// 图渲染节点。
467#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
468pub struct GraphNode {
469 pub id: Uuid,
470 pub name: String,
471 /// 类型 key。**可能没有**(0009:没判出来就是 NULL),
472 /// 前端据此显示"未分类"而不是编一个名字
473 pub type_key: Option<String>,
474 pub type_label: Option<String>,
475 pub color: String,
476 /// 类型形状:circle | square
477 pub shape: String,
478 pub degree: i64,
479 /// 同名并存时的展示消歧后缀(如所属组织名)
480 pub disambiguator: Option<String>,
481}
482
483/// 图渲染边(= 一条 live 事实)。
484#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
485pub struct GraphEdge {
486 pub id: Uuid,
487 pub source: Uuid,
488 pub target: Uuid,
489 /// 本体里没有对应关系时回落到原文说法(见 `facts.predicate_id`)。
490 /// 两个来源都拿不出时为 None——那是 add_evidence 记录原文说法之前的老数据
491 pub predicate: Option<String>,
492 pub label: Option<String>,
493 /// true = 这条边的名字来自原文,不是本体认下的关系。界面要显示得看得出区别
494 pub inferred: bool,
495 /// true = 这条边是**推出来的**,不是任何人断言的(R1,住在 `derived_facts`)。
496 ///
497 /// **与 `inferred` 不是一回事**,尽管两个词很近:那一位说的是「名字来自原文
498 /// 而不是本体」,这一位说的是「这条边根本不是谁说的,是引擎推的」
499 pub derived: bool,
500 /// 推它出来的那条规则(`transitive` / `symmetric` / `inverse` / `sub_property`);
501 /// 断言的边为 None。
502 ///
503 /// **界面需要分辨 `inverse`**:`A works_at B` 与它推出的 `B employs A` 是
504 /// 同一件事的两种说法,画成两条边只是把冗余画了两遍;而 `sub_property`
505 /// 推出的是另一条粒度不同的事实,该各画各的
506 pub rule: Option<String>,
507 /// 推它出来用到的前提事实(按证明顺序)。断言的边为空。
508 ///
509 /// **界面并边要靠它认准来源。** 只按「同一对节点」找,会把 `contains`
510 /// 挂到恰好也连着那两点的 `allied_with` 上——那条说法属于 `part_of`,
511 /// 挂错的结果看着完全正常,正是最难发现的那种
512 pub premises: Vec<Uuid>,
513 pub valid_from: Option<DateTime<Utc>>,
514 pub valid_to: Option<DateTime<Utc>>,
515 pub confidence: f32,
516}
517
518/// 实体详情页的事实行(时间线)。
519#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
520pub struct EntityFact {
521 pub id: Uuid,
522 /// out = 该实体为主语;in = 为宾语
523 pub direction: String,
524 /// 本体没认下这条关系时回落到原文说法;两者都拿不出时为 None(更早的历史数据长这样)
525 pub predicate_key: Option<String>,
526 pub predicate_label: Option<String>,
527 /// true = 这条事实的名字来自原文,不是本体认下的关系。界面要显示得看得出区别
528 pub inferred: bool,
529 /// 关系的时态类别(point/state/eternal)。没有谓词就无从谈起,为 None
530 pub temporal: Option<String>,
531 pub other_id: Option<Uuid>,
532 pub other_name: Option<String>,
533 /// 字面值宾语(属性事实/问数映射):{"value":…,"unit":…} 或 {"summary":…}
534 pub object_value: Option<serde_json::Value>,
535 pub valid_from: Option<DateTime<Utc>>,
536 pub valid_to: Option<DateTime<Utc>>,
537 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
538 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(见 `facts.valid_from_precision`)
539 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
540 pub valid_from_precision: Option<String>,
541 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
542 /// `valid_to` 与它都为 None 才是「仍在持续」(见 `facts.valid_to_precision`)
543 pub valid_to_precision: Option<String>,
544 pub confidence: f32,
545 pub evidence_count: i64,
546 /// 证据全部停留在来源文档的旧版(未被现行内容确认;不代表事实失效)
547 pub stale: bool,
548 /// 修正行(supersedes 链上):区间闭合来自引擎对账/人工裁决而非抽取原文
549 pub corrected: bool,
550 /// 证据集合里最新的文档时间——开放事实的"最后确认时间"(时效性透明化)
551 pub last_evidence_time: Option<DateTime<Utc>>,
552}
553
554/// 实体的一次认知变更(记录时间轴上的事件,与 EntityFact 的有效时间轴正交)。
555///
556/// 账本 append-only,所以"我们曾经怎么认为"全部留存:一条事实行最多产出两个
557/// 事件——写入(asserted / corrected)与作废(rejected,仅当没有后继修正行时;
558/// 有后继的话这次死亡已由那条 corrected 解释,不重复记)。
559///
560/// **不是每个事件都来自一条事实。** 改类(`retyped` / `retype_reverted`)来自
561/// `entity_retypes`:它没有谓词、没有对方、没有方向,那几个字段因此可空。
562/// 从前这里只有事实事件,于是「改了类」在实体历史里完全不显形——0001 P3a 记着
563/// 「可撤销不等于会被撤销」,错了不会自己冒出来。
564#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
565pub struct EntityHistoryEvent {
566 /// 事实事件才有。改类事件为 None
567 pub fact_id: Option<Uuid>,
568 /// 事件发生的记录时刻(写入 = recorded_at,作废 = invalidated_at,
569 /// 改类 = entity_retypes.created_at / reverted_at)
570 pub at: DateTime<Utc>,
571 /// asserted(首次断言)| corrected(区间被修正)| rejected(认知被推翻)
572 /// | merged(并进了另一条断言)| retyped(改了类)| retype_reverted(改类被撤销)
573 pub kind: String,
574 pub direction: Option<String>,
575 pub predicate_label: Option<String>,
576 pub other_name: Option<String>,
577 pub object_value: Option<serde_json::Value>,
578 pub valid_from: Option<DateTime<Utc>>,
579 pub valid_to: Option<DateTime<Utc>>,
580 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
581 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(见 `facts.valid_from_precision`)
582 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
583 pub valid_from_precision: Option<String>,
584 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
585 /// `valid_to` 与它都为 None 才是「仍在持续」(见 `facts.valid_to_precision`)
586 pub valid_to_precision: Option<String>,
587 pub confidence: Option<f32>,
588 /// 人工操作者;NULL = 引擎自动(抽取写入 / 时态对账闭合 / 高置信改类)
589 pub actor_name: Option<String>,
590 /// 触发本次变更的审计动作(fact.close / conflict.close_old / fact.reject …)
591 pub action: Option<String>,
592 pub document_id: Option<Uuid>,
593 pub filename: Option<String>,
594 pub quote: Option<String>,
595 /// 改类事件的两端。起点可空——0009 之后「从没有类到有类」是最常见的一次改类
596 pub from_type_label: Option<String>,
597 pub to_type_label: Option<String>,
598}
599
600/// 一段**记录时间**窗口里,整个库上发生的认知变更。
601///
602/// 跟 `EntityHistoryEvent` 是同一批事件,两处不同:
603/// 1. 开窗在**认知轴**上(recorded_at / invalidated_at),不锁定单个实体——
604/// "上季度有什么变了"这种问题没有一个先验的实体可问;
605/// 2. 主宾都写全(`direction` 是"以某实体为中心"才有的概念,这里没有中心)。
606#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
607pub struct GraphChange {
608 pub fact_id: Uuid,
609 /// 事件落在认知轴上的时刻(写入 = recorded_at,作废 = invalidated_at)
610 pub at: DateTime<Utc>,
611 /// asserted(新断言)| corrected(订正了前一条)| rejected(被推翻)| merged(并入他条)
612 pub kind: String,
613 pub subject_id: Uuid,
614 pub subject_name: String,
615 pub predicate_label: Option<String>,
616 pub object_name: Option<String>,
617 pub object_value: Option<serde_json::Value>,
618 /// 这条断言说的是**世界轴**上的哪一段——与 `at` 正交,别读混
619 pub valid_from: Option<DateTime<Utc>>,
620 pub valid_to: Option<DateTime<Utc>>,
621 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
622 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(见 `facts.valid_from_precision`)
623 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
624 pub valid_from_precision: Option<String>,
625 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
626 /// `valid_to` 与它都为 None 才是「仍在持续」(见 `facts.valid_to_precision`)
627 pub valid_to_precision: Option<String>,
628 pub confidence: f32,
629 pub document_id: Option<Uuid>,
630 pub filename: Option<String>,
631 pub quote: Option<String>,
632}
633
634/// 消解审核项的一侧实体摘要。
635#[derive(Debug, Clone, Serialize)]
636pub struct ReviewSide {
637 pub id: Uuid,
638 pub name: String,
639 /// 没判出类型时为 None(0009)。颜色另有缺省值——它是画布必须拿到的
640 pub type_label: Option<String>,
641 pub color: String,
642 pub disambiguator: Option<String>,
643 pub degree: i64,
644 pub top_facts: Vec<String>,
645}
646
647/// 消解审核项:疑似同一实体的灰区对。
648#[derive(Debug, Clone, Serialize)]
649pub struct ReviewItem {
650 pub id: Uuid,
651 pub score: f32,
652 pub reason: Option<String>,
653 /// adjudicating = 等 LLM 裁决;human = 等人工终审
654 pub stage: String,
655 pub created_at: DateTime<Utc>,
656 pub left: ReviewSide,
657 pub right: ReviewSide,
658}
659
660/// 合并日志行(审核页历史区)。
661#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
662pub struct MergeLogView {
663 pub id: Uuid,
664 pub source_name: String,
665 pub target_name: String,
666 /// NULL = LLM 自动合并
667 pub merged_by_name: Option<String>,
668 pub reason: Option<String>,
669 pub created_at: DateTime<Utc>,
670 pub reverted_at: Option<DateTime<Utc>>,
671}
672
673/// 低置信事实审核行。
674#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
675pub struct FactReviewItem {
676 pub id: Uuid,
677 pub subject_name: String,
678 pub predicate_label: Option<String>,
679 pub object_name: Option<String>,
680 pub valid_from: Option<DateTime<Utc>>,
681 pub valid_to: Option<DateTime<Utc>>,
682 pub confidence: f32,
683 pub evidence_count: i64,
684 pub quote: Option<String>,
685}
686
687/// 事实的证据(引句 + 原文定位)。
688#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
689pub struct EvidenceView {
690 /// 模型在这一块里实际用的谓词说法。词表外谓词被降级成 related_to 后,
691 /// 事实行上只剩"有关联"——原意只在这里
692 pub proposed_predicate: Option<String>,
693 pub quote: Option<String>,
694 pub chunk_id: Uuid,
695 pub document_id: Uuid,
696 pub filename: String,
697 pub seq: i32,
698 /// 证据出自文档的第几版
699 pub doc_version: i32,
700 /// 文档已有更新的版本(证据停留在旧版;不代表事实失效)
701 pub stale: bool,
702}
703
704/// 时态冲突(S3 自动闭合拿不准的那些):旧事实 vs 新事实,Review 页人裁。
705#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
706pub struct ConflictView {
707 pub id: Uuid,
708 /// no_time | simultaneous | low_confidence
709 pub reason: String,
710 pub created_at: DateTime<Utc>,
711 pub predicate_label: String,
712 /// 双方完整三元组:主语侧冲突变的是宾语,宾语侧冲突变的是主语
713 pub old_fact_id: Uuid,
714 pub old_subject: String,
715 pub old_object: Option<String>,
716 pub old_valid_from: Option<DateTime<Utc>>,
717 pub new_fact_id: Uuid,
718 pub new_subject: String,
719 pub new_object: Option<String>,
720 pub new_valid_from: Option<DateTime<Utc>>,
721 pub new_confidence: f32,
722}
723
724/// 文档查看器用的分块视图。
725#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
726pub struct ChunkFull {
727 pub id: Uuid,
728 pub seq: i32,
729 pub text: String,
730}
731
732#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
733pub struct KnowledgeBase {
734 pub id: Uuid,
735 pub workspace_id: Uuid,
736 pub name: String,
737 /// `knowledge` | `memory`(Agent 记忆空间)
738 pub kind: String,
739 pub description: Option<String>,
740 /// open = 全员按部署角色;restricted = 仅 kb_members 名单可见
741 pub visibility: String,
742 /// 部署的公共默认空间(第一个建的库):永远 open、不可删除
743 pub is_default: bool,
744 /// 抽取遇到本体外的说法时,是否允许系统自动把它补进本体并改写等它的事实。
745 /// 缺省开——新库的十个默认关系不是任何人选的,等人手工补齐之前图基本没法用。
746 /// 关掉不影响"留意":未匹配统计照常累积、照常可见,只是变成你点一下的提案。
747 pub auto_extend_ontology: bool,
748 /// 内置本体按哪种语言播种,以及新的类/关系描述写成哪种语言(`en` | `zh`)。
749 /// **跟语料走,不跟界面走**——description 的读者是正在读这些文档的模型。
750 /// 见 docs/decisions/0004。
751 /// 是否把推出来的事实写进账本(R1)。**缺省关**——这一步往图里加东西,
752 /// 而 0001 判据 2 说「本体是引导不是执法」:声明可能是错的,不该在用户
753 /// 没表态时就按它改图
754 pub materialize_inferences: bool,
755 /// 多久重推一次(分钟)。见 `knowledge_bases.inference_interval_minutes`
756 pub inference_interval_minutes: i32,
757 /// 上次推完的时间。**答的是「上次看过没有」,不是「上次改过没有」**
758 pub last_inference_at: Option<DateTime<Utc>>,
759 pub ontology_lang: String,
760 pub created_at: DateTime<Utc>,
761 pub updated_at: DateTime<Utc>,
762}
763
764/// KB 成员矩阵行(库 Settings 的 Members 区)。
765#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
766pub struct KbMemberView {
767 pub user_id: Uuid,
768 pub email: String,
769 pub display_name: String,
770 /// viewer | editor | admin
771 pub role: String,
772}
773
774/// 问数数据源列表视图:连接串不下发(凭据只进不出),只露 host:port/db 摘要。
775#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
776pub struct DataSourceView {
777 pub id: Uuid,
778 pub name: String,
779 pub engine: String,
780 /// 连接摘要(host:port/db,无凭据)
781 pub summary: String,
782 pub created_at: DateTime<Utc>,
783 pub last_test_at: Option<DateTime<Utc>>,
784 pub last_test_ok: Option<bool>,
785}
786
787/// 向量检索出来的一个候选本体行(类 / 关系 / 属性)。
788///
789/// `distance` 是余弦距离,越小越近。原样带给调用方而不是先折成"相似度":
790/// 阈值该定在哪由消费者按自己的数据定,这里不替它归一化。
791#[derive(Debug, Clone, Serialize)]
792pub struct TypeCandidate {
793 pub id: Uuid,
794 pub key: String,
795 pub label: String,
796 pub description: String,
797 /// 关系行才有:`relation` 或 `attribute`
798 pub kind: Option<String>,
799 pub distance: f32,
800}
801
802/// 一个被记下来、但本体里没有对应属性的**字面值**说法。
803///
804/// 跟 [`ProposedPredicate`] 是一对:那个是宾语指向实体的("收购"),
805/// 这个是宾语是字面值的("成立日期 = 2015")。两者不能混——提案要产出的东西
806/// 不一样(关系 vs 属性),而混起来的后果具体:一条 `founding_date` 会变成
807/// 一条指向「2015」这个假实体的边。
808#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
809pub struct ProposedAttribute {
810 pub form: String,
811 pub fact_count: i64,
812 pub doc_count: i64,
813 /// 一条样例值(`"2015"`、`1200`),让人一眼看出这是什么类型的数
814 pub example: Option<String>,
815 /// 这个说法**实际挂在哪些类上**(主语的类型)。
816 ///
817 /// 属性必须声明 domain,而 domain 猜错的代价是硬的:主语类型对不上
818 /// 就整条丢弃(`attr_domain_mismatch`)。所以不问模型,直接从数据里取——
819 /// 事实已经在那儿了,它们的主语是什么类是事实,不是判断
820 pub domain_keys: Vec<String>,
821}
822
823/// 一条口径改动之前的样子。
824///
825/// **存整版快照而不是差异**(0006):读的时候要回答的是「当时是什么」,
826/// 而差异得从头重放才答得出来。`before` 是改动前那一行的 `to_jsonb`,
827/// 去掉了 id 与 kb_id。
828#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
829pub struct MappingRevision {
830 pub id: Uuid,
831 pub before: serde_json::Value,
832 /// 改的人。**裸外键 + 用户软删除**,所以归因不会因为人离职而丢;
833 /// 真被硬删过才会是 NULL
834 pub changed_by_name: Option<String>,
835 pub changed_at: DateTime<Utc>,
836}
837
838/// 语义层的一条映射:业务概念 → 数据资产定义(见 `docs/decisions/0011`)。
839///
840/// **字段是列,不是 JSON 里的键。** 从前它是一条 `mapped_to` 事实,
841/// 这几样全塞在 `object_value` 里——于是「哪些概念映射到了 orders 这张表」
842/// 要扒 JSON,而「同一个概念同一个源只该有一条」这条约束数据库管不到。
843#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
844pub struct ConceptMapping {
845 pub id: Uuid,
846 pub concept_id: Uuid,
847 /// 概念的名字。读的一侧总要它(问数 prompt、Review 列表),
848 /// 每次再查一遍实体表是白跑
849 pub concept_name: String,
850 /// 挂载的数据源。同一个概念在不同源上可以有不同定义,这是有意支持的
851 pub source: String,
852 pub table_name: Option<String>,
853 pub expr: Option<String>,
854 pub sql: Option<String>,
855 pub unit: Option<String>,
856 pub summary: Option<String>,
857 /// 派生指标(「转化率 = 成交数 / 访问数」):算出来的,不是表里的列
858 pub derived: bool,
859 /// proposed | confirmed | rejected
860 ///
861 /// **状态而不是置信度。** 从前借事实的 confidence 表达「提议 0.6 / 确认 1.0」,
862 /// 那是把二值状态编码成浮点数,还顺带让它落进「低置信事实」那一档
863 pub status: String,
864}
865
866/// 一处公理违规,配好展示所需的三元组文本(见 `axiom_violations`)。
867///
868/// **两条事实都展开成 主-谓-宾 文本**:Review 页要让人一眼看出矛盾在哪,
869/// 而两个 UUID 看不出任何东西。自反那一类两条相同——它就是一条事实。
870#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
871pub struct AxiomViolation {
872 pub id: Uuid,
873 /// self_loop | asymmetry | cycle | functional
874 pub kind: String,
875 /// 判据来自哪条关系。人若判「公理写错了」,从这里进本体去改
876 pub predicate: Option<String>,
877 pub left_fact: Uuid,
878 pub left_text: String,
879 pub right_fact: Uuid,
880 pub right_text: String,
881 /// 环的长度(含首尾)。其余三类为 0——前端据此决定要不要显示「查看路径」
882 pub path_len: i32,
883 pub detected_at: chrono::DateTime<chrono::Utc>,
884}
885
886/// 本体自己的一处自相矛盾(见 `ontology_defects`)。
887///
888/// **与 [`AxiomViolation`] 不是一回事**:那个说「事实与定义抵触」,这个说
889/// 「定义自己站不住」。后者更根本——一个自相矛盾的本体会让前者的结论全部可疑。
890#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
891pub struct OntologyDefect {
892 pub id: Uuid,
893 /// symmetric_and_asymmetric | transitive_and_functional | subclass_cycle
894 /// | disjoint_with_ancestor | inherits_disjoint
895 pub kind: String,
896 /// 出问题那个对象的标签(类或谓词)。查不到就是它已经被删了
897 pub subject_label: Option<String>,
898 /// 另一方:互斥的那个类
899 pub other_label: Option<String>,
900 /// 环上类的标签,按顺序
901 pub path_labels: Vec<String>,
902 pub detected_at: chrono::DateTime<chrono::Utc>,
903}
904
905/// 一条推出来的事实,连同它的证明(实体面板的「推出来的」那一档)。
906///
907/// **`premises` 是这一档存在的理由**:不给出前提的话,一条派生边跟一条普通的边
908/// 在界面上看不出区别,而那正是「推理污染知识」的样子。
909#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
910pub struct DerivedFactView {
911 pub id: Uuid,
912 pub subject_id: Uuid,
913 pub subject: String,
914 pub object_id: Uuid,
915 pub object: String,
916 pub predicate: String,
917 /// transitive | symmetric——靠哪条规则推的
918 pub rule: String,
919 pub valid_from: Option<DateTime<Utc>>,
920 pub valid_to: Option<DateTime<Utc>>,
921 pub confidence: f32,
922 pub derived_at: DateTime<Utc>,
923 /// 直接前提,按推导顺序展开成三元组文本
924 pub premises: Vec<String>,
925}
926
927/// 审核队列各档的**真实条数**。
928///
929/// 与列表分开取是有意的:列表有上限(一页十条),数数没有。从前左栏读的是
930/// 数组长度,而接口固定只回 100 条——一个有 164 条待办的库,界面写着 100,
931/// 清完还会再冒出来。
932/// 等人点头的一条事实(0015)。`quote` 是那句记忆的全文——确认界面要把原句和
933/// 三元组并排显示,只列三元组等于要人凭空判断它对不对。
934#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
935pub struct PendingFactView {
936 pub id: Uuid,
937 pub subject_id: Uuid,
938 pub subject_name: String,
939 pub predicate_id: Option<Uuid>,
940 /// 本体里的关系名;为空时前端显示 `proposed_predicate`(斜体,标明是原话)
941 pub predicate_label: Option<String>,
942 pub proposed_predicate: Option<String>,
943 pub object_id: Option<Uuid>,
944 pub object_name: Option<String>,
945 pub object_value: Option<serde_json::Value>,
946 pub valid_from: Option<DateTime<Utc>>,
947 pub valid_from_precision: Option<String>,
948 pub valid_to: Option<DateTime<Utc>>,
949 pub valid_to_precision: Option<String>,
950 pub confidence: f32,
951 pub chunk_id: Uuid,
952 pub quote: String,
953 pub proposed_by: Option<Uuid>,
954 pub proposed_by_name: Option<String>,
955 pub created_at: DateTime<Utc>,
956}
957
958#[derive(Debug, Clone, Copy, Default, Serialize, sqlx::FromRow)]
959pub struct ReviewCounts {
960 /// 记忆抽出、等人点头的事实(0015)。排第一:它是人自己说的话
961 pub pending: i64,
962 pub duplicates: i64,
963 pub conflicts: i64,
964 pub unconfirmed: i64,
965 pub lowconf: i64,
966 pub mappings: i64,
967 pub violations: i64,
968 pub defects: i64,
969 pub merges: i64,
970}
971
972/// 一个关系声明了哪些 OWL 公理。
973///
974/// **打包成一个东西传,不是一串参数。** 它们本来就是同一族——推理机
975/// (0002)拿它们当判据,界面上也该并排出现;散成参数表里的六个 bool,
976/// 调用点迟早传错顺序,而 `bool` 之间编译器帮不上忙。
977///
978/// 后两位不是 bool:`inverseOf` 与 `subPropertyOf` 指向**另一个关系**,
979/// 界面上是下拉框而不是复选框。形状不同不改变它们属于这一族——推理机
980/// 的四种规则源正是这六位里的两条加上这两条(0002)。
981#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
982pub struct RelationAxioms {
983 /// 主语侧唯一(一个人一个出生地)
984 pub functional: bool,
985 /// 宾语侧唯一(一个项目一个 leader)
986 pub inverse_functional: bool,
987 /// A→B ∧ B→C ⟹ A→C
988 pub transitive: bool,
989 /// A→B ⟹ B→A
990 pub symmetric: bool,
991 /// A→B ⟹ 不存在 B→A
992 pub asymmetric: bool,
993 /// 不存在 A→A
994 pub irreflexive: bool,
995 /// `p⁻¹ = q`:`A p B ⟹ B q A`。**单向存,双向用**——载入公理时归一化
996 /// (`reasoning::axioms`),所以只需在一侧声明,反向那条自动成立
997 pub inverse_of: Option<Uuid>,
998 /// `p ⊑ q`:`A p B ⟹ A q B`。断言了具体的,通用的也成立
999 pub sub_property_of: Option<Uuid>,
1000}
1001
1002/// 文库的一页,连同这一页之外的统计。
1003///
1004/// **统计不受名字/状态筛选影响**:`ready` / `extracting` / `failed` 说的是这个
1005/// 来源里有多少,那是批量按钮的作用范围,跟你此刻在搜什么无关。
1006#[derive(Debug, Clone, Serialize)]
1007pub struct DocumentPage {
1008 pub docs: Vec<Document>,
1009 /// 命中筛选的总数(分页器用它)
1010 pub total: i64,
1011 pub ready: i64,
1012 pub extracting: i64,
1013 pub failed: i64,
1014}
1015
1016/// 一枚个人访问令牌的元信息(0014)。**永远不含明文**——
1017/// 明文只在 `tokens::issue` 返回的那一次存在,库里只有哈希。
1018#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
1019pub struct TokenView {
1020 pub id: Uuid,
1021 pub name: String,
1022 /// 给人认的那一小截(`utp_pat_ab12`)。够对上配置文件里那一串,
1023 /// 又不足以复原
1024 pub token_prefix: String,
1025 /// read | write。**上限不是授权**:有效权限 = 这个人的角色 ∩ 这个 scope
1026 pub scope: String,
1027 /// None = 这个人能进的全部库
1028 pub kb_ids: Option<Vec<Uuid>>,
1029 pub expires_at: Option<DateTime<Utc>>,
1030 /// 「这把还在用吗」。撤之前要答得出,否则没人敢撤
1031 pub last_used_at: Option<DateTime<Utc>>,
1032 /// **撤销打戳不删行**:撤过这件事本身要留痕
1033 pub revoked_at: Option<DateTime<Utc>>,
1034 pub created_at: DateTime<Utc>,
1035}