Elffuss BraidHub

kikocis/utopia public

834f8753ffd7 · 28.5 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 pub predicate: String,
161 pub object_id: Option<Uuid>,
162 pub object: Option<String>,
163 pub valid_from: Option<DateTime<Utc>>,
164 pub valid_to: Option<DateTime<Utc>>,
165 pub confidence: f32,
166}
167
168/// 来源列表视图(带文档数)。
169#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
170pub struct SourceView {
171 pub id: Uuid,
172 pub kind: String,
173 pub name: String,
174 pub config: serde_json::Value,
175 pub icon: Option<String>,
176 pub sync_interval_minutes: Option<i32>,
177 pub sync_cron: Option<String>,
178 pub last_sync_at: Option<DateTime<Utc>>,
179 pub last_sync_status: String,
180 pub last_sync_error: Option<String>,
181 pub last_sync_added: i32,
182 pub doc_count: i64,
183 /// 已标记"不在来源中"的文档数(url 全集对账 / custom 墓碑产生)
184 pub missing_count: i64,
185}
186
187/// 审计事件视图(带操作人显示名;删号后为 NULL)。纯审计展示用。
188#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
189pub struct AuditEventView {
190 pub id: Uuid,
191 pub action: String,
192 pub target_kind: String,
193 pub target_id: Option<Uuid>,
194 pub detail: serde_json::Value,
195 pub actor_name: Option<String>,
196 pub created_at: DateTime<Utc>,
197}
198
199/// 账户层"我的知识库"行信息(成员行可空:open 库凭部署身份进入,无矩阵记录)。
200#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
201pub struct MyKbInfo {
202 pub kb_id: Uuid,
203 pub member_role: Option<String>,
204 pub joined_at: Option<DateTime<Utc>>,
205 pub added_by_name: Option<String>,
206 pub doc_count: i64,
207 pub member_count: i64,
208}
209
210/// Chat 会话行(左栏列表)。
211#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
212pub struct ConversationView {
213 pub id: Uuid,
214 pub title: String,
215 pub created_at: DateTime<Utc>,
216 pub updated_at: DateTime<Utc>,
217 pub message_count: i64,
218}
219
220/// Chat 消息(含落库的行动轨迹与引用,历史回放用)。
221#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
222pub struct ConversationMessage {
223 pub id: Uuid,
224 pub role: String,
225 pub content: String,
226 pub steps: serde_json::Value,
227 pub sources: serde_json::Value,
228 pub created_at: DateTime<Utc>,
229}
230
231/// 检索结果用的分块视图(带文档信息)。
232#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
233pub struct ChunkView {
234 pub id: Uuid,
235 pub document_id: Uuid,
236 pub seq: i32,
237 pub text: String,
238 pub filename: String,
239}
240
241#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
242pub struct LlmSettings {
243 pub workspace_id: Uuid,
244 pub chat_base_url: Option<String>,
245 #[serde(skip_serializing)]
246 pub chat_api_key: Option<String>,
247 pub chat_model: Option<String>,
248 pub embed_base_url: Option<String>,
249 #[serde(skip_serializing)]
250 pub embed_api_key: Option<String>,
251 pub embed_model: Option<String>,
252 pub embed_dim: Option<i32>,
253 pub updated_at: DateTime<Utc>,
254}
255
256impl LlmSettings {
257 pub fn chat_ready(&self) -> bool {
258 self.chat_base_url.is_some() && self.chat_model.is_some()
259 }
260 pub fn embed_ready(&self) -> bool {
261 self.embed_base_url.is_some() && self.embed_model.is_some()
262 }
263}
264
265#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
266pub struct EntityType {
267 pub id: Uuid,
268 pub kb_id: Uuid,
269 pub key: String,
270 pub label: String,
271 pub color: String,
272 /// 图谱节点形状:circle | square
273 pub shape: String,
274 pub builtin: bool,
275 /// subClassOf 层级(公理推理 P4 点亮,编辑器先维护数据)
276 /// 全部父类(subClassOf 可以有多个:FOAF 的 Person 同时是 Agent 与 SpatialThing)
277 pub parents: Vec<Uuid>,
278 /// 左栏画树时挂在哪一支下。不参与语义,只管展示
279 pub primary_parent: Option<Uuid>,
280 /// OWL 导入的全局身份。手工建的类为 NULL;重导入按它匹配,不按 key——
281 /// 上游改一次 rdfs:label 派生的 key 就变了,按 key 匹配会把同一个类当新类建
282 pub iri: Option<String>,
283 /// 语义指引:注入抽取 prompt(什么算这个类,举例)
284 pub description: String,
285}
286
287/// 本体编辑器:某个类下的实体实例行(详情区实例列表用)。
288#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
289pub struct EntityInstance {
290 pub id: Uuid,
291 pub name: String,
292 pub fact_count: i64,
293}
294
295/// 本体编辑器视图:类型 + 使用量(删除保护与 UX 提示用)。
296#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
297pub struct EntityTypeView {
298 pub id: Uuid,
299 pub key: String,
300 pub label: String,
301 pub color: String,
302 pub shape: String,
303 pub builtin: bool,
304 /// 全部父类(subClassOf 可以有多个:FOAF 的 Person 同时是 Agent 与 SpatialThing)
305 pub parents: Vec<Uuid>,
306 /// 左栏画树时挂在哪一支下。不参与语义,只管展示
307 pub primary_parent: Option<Uuid>,
308 pub description: String,
309 pub usage: i64,
310}
311
312#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
313pub struct RelationTypeView {
314 pub id: Uuid,
315 pub key: String,
316 pub label: String,
317 pub temporal: String,
318 pub functional: bool,
319 pub inverse_functional: bool,
320 pub builtin: bool,
321 pub description: String,
322 /// relation(宾语是实体)| attribute(宾语是字面值)
323 pub kind: String,
324 /// 可以当主语的类。attribute 至少一个;relation 可空(未声明 = 不限)
325 pub domains: Vec<Uuid>,
326 /// 可以当宾语的类。**只对 relation 有意义**——attribute 的值域是字面量类型,
327 /// 落在 datatype 上
328 pub ranges: Vec<Uuid>,
329 /// attribute 专用:text | number | date | bool
330 pub datatype: Option<String>,
331 pub unit: Option<String>,
332 pub usage: i64,
333}
334
335/// 抽取未匹配统计(本体扩展建议的信号源)。
336#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
337pub struct OntologyMiss {
338 pub kind: String,
339 pub key: String,
340 pub example: Option<String>,
341 pub count: i32,
342}
343
344/// 一个待认领的表层谓词:原文这么说过,但本体里没有对应关系,事实降级成了
345/// related_to。与 `OntologyMiss` 的纯计数不同,它连着具体事实——所以采纳时
346/// 能说清"将重新归类 57 条",并真的去改。
347#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
348pub struct ProposedPredicate {
349 pub form: String,
350 /// 有多少条 live 的 related_to 事实由这个说法而来
351 pub fact_count: i64,
352 /// 出现在多少篇文档里。只在一篇里出现过的是那篇文档的用词,不是这个
353 /// 组织的词汇——自动扩展据此设门槛,人工提案只作参考不拦
354 pub doc_count: i64,
355 /// 一条样例("Dino Crisis (Steam) → GeForce NOW"),让人一眼判断这是什么关系
356 pub example: Option<String>,
357}
358
359/// 一次 OWL 导入的记录。原文按内容寻址存在 blob 里,这行只是账。
360/// `summary` 记下那次投影做了什么,包括**暂未投影**的公理——将来补上消费者
361/// 时据此知道哪些导入值得重跑。
362#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
363pub struct OntologyImportView {
364 pub id: Uuid,
365 pub filename: String,
366 pub format: String,
367 pub byte_size: i64,
368 pub summary: serde_json::Value,
369 pub imported_at: DateTime<Utc>,
370 pub imported_by_name: Option<String>,
371}
372
373/// 一个模型的并发上限。约束来自供应商的速率限制,那是按 (base_url, model) 算的——
374/// 本地 Ollama 与托管 API 用同一个数字本来就不对。
375#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
376pub struct ModelLimit {
377 pub base_url: String,
378 pub model: String,
379 pub max_concurrent: i32,
380}
381
382/// 一个待认领的实体类型:模型提议过、本体没有、实体因此降级成了 concept。
383/// 与 `ProposedPredicate` 对称——它连着具体实体,所以采纳时能说清"将重新归类
384/// 43 个"并真的去改,而不是只建一个空类。
385#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
386pub struct ProposedType {
387 pub form: String,
388 pub entity_count: i64,
389 /// 一个样例名字,让人一眼判断这是什么类
390 pub example: Option<String>,
391}
392
393/// 抽取丢弃信号:事实抽出来了却没能落地,以及为什么。
394/// 与 `OntologyMiss` 分开——那个说"你的本体缺这些"(读者是本体维护者),
395/// 这个说"这些事实没落地"(读者是上传文档的人)。
396#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
397pub struct ExtractionDrop {
398 pub document_id: Uuid,
399 pub reason: String,
400 pub detail: String,
401 pub count: i32,
402 pub example: Option<String>,
403}
404
405#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
406pub struct RelationType {
407 pub id: Uuid,
408 pub kb_id: Uuid,
409 pub key: String,
410 pub label: String,
411 /// state | event | eternal
412 pub temporal: String,
413 /// 主语侧唯一:同一时刻一个主语至多一个宾语
414 pub functional: bool,
415 /// 宾语侧唯一:同一时刻一个宾语至多一个主语(如一个项目只有一个 leads 它的人)
416 pub inverse_functional: bool,
417 pub builtin: bool,
418 /// 语义指引:注入抽取 prompt
419 pub description: String,
420 /// OWL 导入的全局身份;手工建的为 NULL。重导入按它匹配,不按 key——
421 /// 上游改一次 rdfs:label 派生的 key 就变了,按 key 匹配会把同一个当成新的
422 pub iri: Option<String>,
423 /// relation(宾语是实体)| attribute(宾语是字面值,走 facts.object_value)
424 pub kind: String,
425 /// 可以当主语的类(多值:OWL 里一个属性有多个 rdfs:domain 是常态)
426 pub domains: Vec<Uuid>,
427 /// 可以当宾语的类。只对 relation 有意义
428 pub ranges: Vec<Uuid>,
429 /// attribute 专用:text | number | date | bool
430 pub datatype: Option<String>,
431 pub unit: Option<String>,
432}
433
434#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
435pub struct Entity {
436 pub id: Uuid,
437 pub kb_id: Uuid,
438 /// `None` = 还没判出来。见 `docs/decisions/0009`——它不是一个类,
439 /// 是「抽取器抽到了东西,但本体里没有对应的类」这个状态
440 pub type_id: Option<Uuid>,
441 pub canonical_name: String,
442 pub aliases: Vec<String>,
443 pub merged_into: Option<Uuid>,
444 pub created_at: DateTime<Utc>,
445 pub updated_at: DateTime<Utc>,
446}
447
448/// 图渲染节点。
449#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
450pub struct GraphNode {
451 pub id: Uuid,
452 pub name: String,
453 /// 类型 key。**可能没有**(0009:没判出来就是 NULL),
454 /// 前端据此显示"未分类"而不是编一个名字
455 pub type_key: Option<String>,
456 pub type_label: Option<String>,
457 pub color: String,
458 /// 类型形状:circle | square
459 pub shape: String,
460 pub degree: i64,
461 /// 同名并存时的展示消歧后缀(如所属组织名)
462 pub disambiguator: Option<String>,
463}
464
465/// 图渲染边(= 一条 live 事实)。
466#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
467pub struct GraphEdge {
468 pub id: Uuid,
469 pub source: Uuid,
470 pub target: Uuid,
471 pub predicate: String,
472 pub label: String,
473 pub valid_from: Option<DateTime<Utc>>,
474 pub valid_to: Option<DateTime<Utc>>,
475 pub confidence: f32,
476}
477
478/// 实体详情页的事实行(时间线)。
479#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
480pub struct EntityFact {
481 pub id: Uuid,
482 /// out = 该实体为主语;in = 为宾语
483 pub direction: String,
484 pub predicate_key: String,
485 pub predicate_label: String,
486 pub temporal: String,
487 pub other_id: Option<Uuid>,
488 pub other_name: Option<String>,
489 /// 字面值宾语(属性事实/问数映射):{"value":…,"unit":…} 或 {"summary":…}
490 pub object_value: Option<serde_json::Value>,
491 pub valid_from: Option<DateTime<Utc>>,
492 pub valid_to: Option<DateTime<Utc>>,
493 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
494 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(迁移 0045)
495 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
496 pub valid_from_precision: Option<String>,
497 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
498 /// `valid_to` 与它都为 None 才是「仍在持续」(迁移 0046)
499 pub valid_to_precision: Option<String>,
500 pub confidence: f32,
501 pub evidence_count: i64,
502 /// 证据全部停留在来源文档的旧版(未被现行内容确认;不代表事实失效)
503 pub stale: bool,
504 /// 修正行(supersedes 链上):区间闭合来自引擎对账/人工裁决而非抽取原文
505 pub corrected: bool,
506 /// 证据集合里最新的文档时间——开放事实的"最后确认时间"(时效性透明化)
507 pub last_evidence_time: Option<DateTime<Utc>>,
508}
509
510/// 实体的一次认知变更(记录时间轴上的事件,与 EntityFact 的有效时间轴正交)。
511///
512/// 账本 append-only,所以"我们曾经怎么认为"全部留存:一条事实行最多产出两个
513/// 事件——写入(asserted / corrected)与作废(rejected,仅当没有后继修正行时;
514/// 有后继的话这次死亡已由那条 corrected 解释,不重复记)。
515///
516/// **不是每个事件都来自一条事实。** 改类(`retyped` / `retype_reverted`)来自
517/// `entity_retypes`:它没有谓词、没有对方、没有方向,那几个字段因此可空。
518/// 从前这里只有事实事件,于是「改了类」在实体历史里完全不显形——0001 P3a 记着
519/// 「可撤销不等于会被撤销」,错了不会自己冒出来。
520#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
521pub struct EntityHistoryEvent {
522 /// 事实事件才有。改类事件为 None
523 pub fact_id: Option<Uuid>,
524 /// 事件发生的记录时刻(写入 = recorded_at,作废 = invalidated_at,
525 /// 改类 = entity_retypes.created_at / reverted_at)
526 pub at: DateTime<Utc>,
527 /// asserted(首次断言)| corrected(区间被修正)| rejected(认知被推翻)
528 /// | merged(并进了另一条断言)| retyped(改了类)| retype_reverted(改类被撤销)
529 pub kind: String,
530 pub direction: Option<String>,
531 pub predicate_label: Option<String>,
532 pub other_name: Option<String>,
533 pub object_value: Option<serde_json::Value>,
534 pub valid_from: Option<DateTime<Utc>>,
535 pub valid_to: Option<DateTime<Utc>>,
536 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
537 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(迁移 0045)
538 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
539 pub valid_from_precision: Option<String>,
540 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
541 /// `valid_to` 与它都为 None 才是「仍在持续」(迁移 0046)
542 pub valid_to_precision: Option<String>,
543 pub confidence: Option<f32>,
544 /// 人工操作者;NULL = 引擎自动(抽取写入 / 时态对账闭合 / 高置信改类)
545 pub actor_name: Option<String>,
546 /// 触发本次变更的审计动作(fact.close / conflict.close_old / fact.reject …)
547 pub action: Option<String>,
548 pub document_id: Option<Uuid>,
549 pub filename: Option<String>,
550 pub quote: Option<String>,
551 /// 改类事件的两端。起点可空——0009 之后「从没有类到有类」是最常见的一次改类
552 pub from_type_label: Option<String>,
553 pub to_type_label: Option<String>,
554}
555
556/// 一段**记录时间**窗口里,整个库上发生的认知变更。
557///
558/// 跟 `EntityHistoryEvent` 是同一批事件,两处不同:
559/// 1. 开窗在**认知轴**上(recorded_at / invalidated_at),不锁定单个实体——
560/// "上季度有什么变了"这种问题没有一个先验的实体可问;
561/// 2. 主宾都写全(`direction` 是"以某实体为中心"才有的概念,这里没有中心)。
562#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
563pub struct GraphChange {
564 pub fact_id: Uuid,
565 /// 事件落在认知轴上的时刻(写入 = recorded_at,作废 = invalidated_at)
566 pub at: DateTime<Utc>,
567 /// asserted(新断言)| corrected(订正了前一条)| rejected(被推翻)| merged(并入他条)
568 pub kind: String,
569 pub subject_id: Uuid,
570 pub subject_name: String,
571 pub predicate_label: String,
572 pub object_name: Option<String>,
573 pub object_value: Option<serde_json::Value>,
574 /// 这条断言说的是**世界轴**上的哪一段——与 `at` 正交,别读混
575 pub valid_from: Option<DateTime<Utc>>,
576 pub valid_to: Option<DateTime<Utc>>,
577 /// 精度描述的是这条事实**有的那些日期**的粒度。两端都没有日期时为 None——
578 /// 从前这里是 NOT NULL DEFAULT day,于是没日期的事实也自称精确到日(迁移 0045)
579 /// 起始端的粒度:year | month | day。没有 valid_from 时为 None
580 pub valid_from_precision: Option<String>,
581 /// 结束端的粒度,外加一个 `unknown`——**原文说它结束了,但没说哪天**。
582 /// `valid_to` 与它都为 None 才是「仍在持续」(迁移 0046)
583 pub valid_to_precision: Option<String>,
584 pub confidence: f32,
585 pub document_id: Option<Uuid>,
586 pub filename: Option<String>,
587 pub quote: Option<String>,
588}
589
590/// 消解审核项的一侧实体摘要。
591#[derive(Debug, Clone, Serialize)]
592pub struct ReviewSide {
593 pub id: Uuid,
594 pub name: String,
595 /// 没判出类型时为 None(0009)。颜色另有缺省值——它是画布必须拿到的
596 pub type_label: Option<String>,
597 pub color: String,
598 pub disambiguator: Option<String>,
599 pub degree: i64,
600 pub top_facts: Vec<String>,
601}
602
603/// 消解审核项:疑似同一实体的灰区对。
604#[derive(Debug, Clone, Serialize)]
605pub struct ReviewItem {
606 pub id: Uuid,
607 pub score: f32,
608 pub reason: Option<String>,
609 /// adjudicating = 等 LLM 裁决;human = 等人工终审
610 pub stage: String,
611 pub created_at: DateTime<Utc>,
612 pub left: ReviewSide,
613 pub right: ReviewSide,
614}
615
616/// 合并日志行(审核页历史区)。
617#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
618pub struct MergeLogView {
619 pub id: Uuid,
620 pub source_name: String,
621 pub target_name: String,
622 /// NULL = LLM 自动合并
623 pub merged_by_name: Option<String>,
624 pub reason: Option<String>,
625 pub created_at: DateTime<Utc>,
626 pub reverted_at: Option<DateTime<Utc>>,
627}
628
629/// 低置信事实审核行。
630#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
631pub struct FactReviewItem {
632 pub id: Uuid,
633 pub subject_name: String,
634 pub predicate_label: String,
635 pub object_name: Option<String>,
636 pub valid_from: Option<DateTime<Utc>>,
637 pub valid_to: Option<DateTime<Utc>>,
638 pub confidence: f32,
639 pub evidence_count: i64,
640 pub quote: Option<String>,
641}
642
643/// 事实的证据(引句 + 原文定位)。
644#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
645pub struct EvidenceView {
646 /// 模型在这一块里实际用的谓词说法。词表外谓词被降级成 related_to 后,
647 /// 事实行上只剩"有关联"——原意只在这里
648 pub proposed_predicate: Option<String>,
649 pub quote: Option<String>,
650 pub chunk_id: Uuid,
651 pub document_id: Uuid,
652 pub filename: String,
653 pub seq: i32,
654 /// 证据出自文档的第几版
655 pub doc_version: i32,
656 /// 文档已有更新的版本(证据停留在旧版;不代表事实失效)
657 pub stale: bool,
658}
659
660/// 时态冲突(S3 自动闭合拿不准的那些):旧事实 vs 新事实,Review 页人裁。
661#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
662pub struct ConflictView {
663 pub id: Uuid,
664 /// no_time | simultaneous | low_confidence
665 pub reason: String,
666 pub created_at: DateTime<Utc>,
667 pub predicate_label: String,
668 /// 双方完整三元组:主语侧冲突变的是宾语,宾语侧冲突变的是主语
669 pub old_fact_id: Uuid,
670 pub old_subject: String,
671 pub old_object: Option<String>,
672 pub old_valid_from: Option<DateTime<Utc>>,
673 pub new_fact_id: Uuid,
674 pub new_subject: String,
675 pub new_object: Option<String>,
676 pub new_valid_from: Option<DateTime<Utc>>,
677 pub new_confidence: f32,
678}
679
680/// 文档查看器用的分块视图。
681#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
682pub struct ChunkFull {
683 pub id: Uuid,
684 pub seq: i32,
685 pub text: String,
686}
687
688#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
689pub struct KnowledgeBase {
690 pub id: Uuid,
691 pub workspace_id: Uuid,
692 pub name: String,
693 /// `knowledge` | `memory`(Agent 记忆空间)
694 pub kind: String,
695 pub description: Option<String>,
696 /// open = 全员按部署角色;restricted = 仅 kb_members 名单可见
697 pub visibility: String,
698 /// 部署的公共默认空间(第一个建的库):永远 open、不可删除
699 pub is_default: bool,
700 /// 抽取遇到本体外的说法时,是否允许系统自动把它补进本体并改写等它的事实。
701 /// 缺省开——新库的十个默认关系不是任何人选的,等人手工补齐之前图基本没法用。
702 /// 关掉不影响"留意":未匹配统计照常累积、照常可见,只是变成你点一下的提案。
703 pub auto_extend_ontology: bool,
704 /// 内置本体按哪种语言播种,以及新的类/关系描述写成哪种语言(`en` | `zh`)。
705 /// **跟语料走,不跟界面走**——description 的读者是正在读这些文档的模型。
706 /// 见 docs/decisions/0004。
707 pub ontology_lang: String,
708 pub created_at: DateTime<Utc>,
709 pub updated_at: DateTime<Utc>,
710}
711
712/// KB 成员矩阵行(库 Settings 的 Members 区)。
713#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
714pub struct KbMemberView {
715 pub user_id: Uuid,
716 pub email: String,
717 pub display_name: String,
718 /// viewer | editor | admin
719 pub role: String,
720}
721
722/// 问数数据源列表视图:连接串不下发(凭据只进不出),只露 host:port/db 摘要。
723#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
724pub struct DataSourceView {
725 pub id: Uuid,
726 pub name: String,
727 pub engine: String,
728 /// 连接摘要(host:port/db,无凭据)
729 pub summary: String,
730 pub created_at: DateTime<Utc>,
731 pub last_test_at: Option<DateTime<Utc>>,
732 pub last_test_ok: Option<bool>,
733}
734
735/// 向量检索出来的一个候选本体行(类 / 关系 / 属性)。
736///
737/// `distance` 是余弦距离,越小越近。原样带给调用方而不是先折成"相似度":
738/// 阈值该定在哪由消费者按自己的数据定,这里不替它归一化。
739#[derive(Debug, Clone, Serialize)]
740pub struct TypeCandidate {
741 pub id: Uuid,
742 pub key: String,
743 pub label: String,
744 pub description: String,
745 /// 关系行才有:`relation` 或 `attribute`
746 pub kind: Option<String>,
747 pub distance: f32,
748}
749
750/// 一个被记下来、但本体里没有对应属性的**字面值**说法。
751///
752/// 跟 [`ProposedPredicate`] 是一对:那个是宾语指向实体的("收购"),
753/// 这个是宾语是字面值的("成立日期 = 2015")。两者不能混——提案要产出的东西
754/// 不一样(关系 vs 属性),而混起来的后果具体:一条 `founding_date` 会变成
755/// 一条指向「2015」这个假实体的边。
756#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
757pub struct ProposedAttribute {
758 pub form: String,
759 pub fact_count: i64,
760 pub doc_count: i64,
761 /// 一条样例值(`"2015"`、`1200`),让人一眼看出这是什么类型的数
762 pub example: Option<String>,
763 /// 这个说法**实际挂在哪些类上**(主语的类型)。
764 ///
765 /// 属性必须声明 domain,而 domain 猜错的代价是硬的:主语类型对不上
766 /// 就整条丢弃(`attr_domain_mismatch`)。所以不问模型,直接从数据里取——
767 /// 事实已经在那儿了,它们的主语是什么类是事实,不是判断
768 pub domain_keys: Vec<String>,
769}