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