| 1 | import type { SourceKind } from "./sourceKinds"; |
| 2 | import { S, lang } from "./i18n"; |
| 3 | |
| 4 | export class ApiError extends Error { |
| 5 | status: number; |
| 6 | /** 服务端给的稳定错误码(没有则是尚未转换的契约守卫,message 已是英文原句) */ |
| 7 | code?: string; |
| 8 | constructor(status: number, message: string, code?: string) { |
| 9 | super(message); |
| 10 | this.status = status; |
| 11 | this.code = code; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | async function request<T>(path: string, init?: RequestInit): Promise<T> { |
| 16 | const res = await fetch(path, { |
| 17 | credentials: "include", |
| 18 | headers: |
| 19 | init?.body instanceof FormData |
| 20 | ? {} |
| 21 | : { "Content-Type": "application/json" }, |
| 22 | ...init, |
| 23 | }); |
| 24 | if (!res.ok) { |
| 25 | // 措辞在这一个收口点定:22 个文件里的 toast.error(e.message) 一处都不用改。 |
| 26 | // 有 code 就查 i18n,没有(或这一条还没进表)就退回服务端的英文原句—— |
| 27 | // 服务端永远说英文,因为界面语言在客户端(docs/decisions/0004) |
| 28 | let message = res.statusText; |
| 29 | let code: string | undefined; |
| 30 | try { |
| 31 | const body = (await res.json()) as { |
| 32 | error?: string; |
| 33 | code?: string; |
| 34 | detail?: string; |
| 35 | }; |
| 36 | if (body.error) message = body.error; |
| 37 | code = body.code; |
| 38 | // code 来自网络,不是字面量——这一处 cast 换来 err 表本身的全量类型检查 |
| 39 | const worded = code |
| 40 | ? (S.err as Record<string, string | undefined>)[code] |
| 41 | : undefined; |
| 42 | if (worded) message = worded; |
| 43 | if (body.detail) message = S.errDetail(message, body.detail); |
| 44 | } catch { |
| 45 | // 非 JSON 响应体,保留 statusText |
| 46 | } |
| 47 | throw new ApiError(res.status, message, code); |
| 48 | } |
| 49 | return res.json() as Promise<T>; |
| 50 | } |
| 51 | |
| 52 | export interface User { |
| 53 | id: string; |
| 54 | org_id: string; |
| 55 | email: string; |
| 56 | display_name: string; |
| 57 | is_admin: boolean; |
| 58 | created_at: string; |
| 59 | } |
| 60 | |
| 61 | /** 个人访问令牌(0014):给 MCP 客户端的长命钥匙,以发它的人的身份行事。 |
| 62 | * 有效权限 = 这个人的角色 ∩ scope;`kb_ids` 为空 = 这个人能进的全部库。 */ |
| 63 | export interface TokenView { |
| 64 | id: string; |
| 65 | name: string; |
| 66 | /** 给人认的那一小截(`utp_pat_ab12`),够对上配置文件里那一串,又不足以复原 */ |
| 67 | token_prefix: string; |
| 68 | scope: "read" | "write"; |
| 69 | kb_ids: string[] | null; |
| 70 | expires_at: string | null; |
| 71 | last_used_at: string | null; |
| 72 | /** 撤销打戳不删行 */ |
| 73 | revoked_at: string | null; |
| 74 | created_at: string; |
| 75 | } |
| 76 | |
| 77 | export interface Workspace { |
| 78 | id: string; |
| 79 | org_id: string; |
| 80 | name: string; |
| 81 | created_at: string; |
| 82 | } |
| 83 | |
| 84 | /** 建库时可选的预置本体包(`GET /ontology-packs`)。 */ |
| 85 | export type OntologyPack = { |
| 86 | id: string; |
| 87 | name: string; |
| 88 | summary: string; |
| 89 | classes: number; |
| 90 | properties: number; |
| 91 | }; |
| 92 | |
| 93 | export interface Kb { |
| 94 | id: string; |
| 95 | workspace_id: string; |
| 96 | name: string; |
| 97 | kind: string; |
| 98 | description: string | null; |
| 99 | visibility: "open" | "restricted"; |
| 100 | /** 部署的公共默认空间(第一个建的库):永远 open、不可删除 */ |
| 101 | is_default: boolean; |
| 102 | /** 抽取遇到本体外的说法时,是否允许系统自动补进本体并改写等它的事实。 |
| 103 | 关掉不影响"留意":未匹配统计照常累积可见,只是变成你点一下的提案 */ |
| 104 | auto_extend_ontology: boolean; |
| 105 | /** 把推出来的事实写进账本(R1)。**缺省关**——推理往图里加东西, |
| 106 | * 而声明可能是错的,不该在用户没表态时就按它改图 */ |
| 107 | materialize_inferences: boolean; |
| 108 | /** 多久重推一次(分钟)。事实持续在变,只靠手点会让派生一直是缺的 */ |
| 109 | inference_interval_minutes: number; |
| 110 | /** 上次推完的时间 */ |
| 111 | last_inference_at: string | null; |
| 112 | /** 内置本体按哪种语言播种、新描述写哪种语言。**跟语料走,不跟界面走** |
| 113 | (界面语言在客户端,见 docs/decisions/0004) */ |
| 114 | ontology_lang: "en" | "zh"; |
| 115 | /** 调用者在本库的角色(仅详情接口返回):前端据此门控破坏性入口 */ |
| 116 | my_role?: "viewer" | "editor" | "admin" | "owner" | null; |
| 117 | } |
| 118 | |
| 119 | /** 账户层"我的知识库"行:库 + 我的角色 + 加入信息 + 概览统计。 */ |
| 120 | export interface MyKb { |
| 121 | kb: Kb; |
| 122 | my_role: "viewer" | "editor" | "admin" | "owner" | null; |
| 123 | joined_at: string | null; |
| 124 | added_by_name: string | null; |
| 125 | doc_count: number; |
| 126 | member_count: number; |
| 127 | } |
| 128 | |
| 129 | /** 审计事件(纯审计展示)。 */ |
| 130 | export interface AuditEvent { |
| 131 | id: string; |
| 132 | action: string; |
| 133 | target_kind: string; |
| 134 | target_id: string | null; |
| 135 | detail: Record<string, unknown>; |
| 136 | /** null = 引擎自动(裁决器、一致性检查、推理);有 id 没名字 = 账号已移除 */ |
| 137 | actor_id: string | null; |
| 138 | actor_name: string | null; |
| 139 | created_at: string; |
| 140 | } |
| 141 | |
| 142 | export interface KbMember { |
| 143 | user_id: string; |
| 144 | email: string; |
| 145 | display_name: string; |
| 146 | role: "viewer" | "editor" | "admin"; |
| 147 | } |
| 148 | |
| 149 | export interface Doc { |
| 150 | id: string; |
| 151 | kb_id: string; |
| 152 | source_id: string | null; |
| 153 | filename: string; |
| 154 | mime: string; |
| 155 | size_bytes: number; |
| 156 | status: string; |
| 157 | graph_status: string; |
| 158 | /** 摄入管道的失败原因 */ |
| 159 | error: string | null; |
| 160 | /** 图谱抽取管道的失败原因(与 error 分列:两条管道各存各的) */ |
| 161 | graph_error: string | null; |
| 162 | chunk_count: number; |
| 163 | /** 文档标签。**今天没有任何界面用它**——故意留着,理由写在 |
| 164 | * `migrations/0002_ingest.sql` 的 `tags` 列上 */ |
| 165 | tags: string[]; |
| 166 | missing_since: string | null; |
| 167 | created_at: string; |
| 168 | } |
| 169 | |
| 170 | /** 一类抽取丢弃在一篇文档里的聚合:事实抽出来了,却没能落地。 */ |
| 171 | export interface ExtractionDrop { |
| 172 | document_id: string; |
| 173 | /** 稳定的原因码,前端按它查文案(attr_domain_mismatch / low_confidence / ...) */ |
| 174 | reason: string; |
| 175 | /** 该原因下的具体对象(属性 key、谓词名、"salary@organization") */ |
| 176 | detail: string; |
| 177 | count: number; |
| 178 | example: string | null; |
| 179 | } |
| 180 | |
| 181 | export interface SourceView { |
| 182 | id: string; |
| 183 | kind: SourceKind; |
| 184 | name: string; |
| 185 | config: { |
| 186 | urls?: string[]; |
| 187 | feed_url?: string; |
| 188 | endpoint?: string; |
| 189 | /** github_issues:owner/name */ |
| 190 | repo?: string; |
| 191 | /** github_issues:PR 在 GitHub 模型里也是工单,默认不收 */ |
| 192 | include_pull_requests?: boolean; |
| 193 | /** jira_issues:站点地址,如 https://issues.apache.org/jira */ |
| 194 | base_url?: string; |
| 195 | /** jira_issues:项目 key,如 KAFKA */ |
| 196 | project?: string; |
| 197 | } | null; |
| 198 | icon: string | null; |
| 199 | sync_interval_minutes: number | null; |
| 200 | sync_cron: string | null; |
| 201 | last_sync_at: string | null; |
| 202 | last_sync_status: "never" | "queued" | "running" | "ok" | "failed"; |
| 203 | last_sync_error: string | null; |
| 204 | last_sync_added: number; |
| 205 | doc_count: number; |
| 206 | missing_count: number; |
| 207 | } |
| 208 | |
| 209 | export interface SearchResult { |
| 210 | id: string; |
| 211 | document_id: string; |
| 212 | seq: number; |
| 213 | text: string; |
| 214 | filename: string; |
| 215 | } |
| 216 | |
| 217 | export interface LlmSettingsView { |
| 218 | chat_base_url?: string | null; |
| 219 | chat_model?: string | null; |
| 220 | has_chat_key?: boolean; |
| 221 | embed_base_url?: string | null; |
| 222 | embed_model?: string | null; |
| 223 | embed_dim?: number | null; |
| 224 | has_embed_key?: boolean; |
| 225 | } |
| 226 | |
| 227 | export interface Member { |
| 228 | user_id: string; |
| 229 | email: string; |
| 230 | display_name: string; |
| 231 | role: string; |
| 232 | is_admin: boolean; |
| 233 | } |
| 234 | |
| 235 | export interface OrgUser { |
| 236 | id: string; |
| 237 | email: string; |
| 238 | display_name: string; |
| 239 | is_admin: boolean; |
| 240 | } |
| 241 | |
| 242 | /** 问数数据源(凭据不下发,只有 host:port/db 摘要)。 */ |
| 243 | export interface DataSourceView { |
| 244 | id: string; |
| 245 | name: string; |
| 246 | engine: string; |
| 247 | summary: string; |
| 248 | created_at: string; |
| 249 | last_test_at: string | null; |
| 250 | last_test_ok: boolean | null; |
| 251 | } |
| 252 | |
| 253 | export interface GraphNode { |
| 254 | id: string; |
| 255 | name: string; |
| 256 | // 没判出类型时为 null(0009) |
| 257 | type_key: string | null; |
| 258 | type_label: string | null; |
| 259 | color: string; |
| 260 | shape: "circle" | "square"; |
| 261 | degree: number; |
| 262 | disambiguator: string | null; |
| 263 | } |
| 264 | |
| 265 | export interface SyncRun { |
| 266 | id: string; |
| 267 | started_at: string; |
| 268 | finished_at: string | null; |
| 269 | status: "running" | "ok" | "failed"; |
| 270 | created_docs: number; |
| 271 | updated_docs: number; |
| 272 | error: string | null; |
| 273 | } |
| 274 | |
| 275 | /** 分块的抽取产物(文档查看器右栏)。 */ |
| 276 | export interface ChunkFact { |
| 277 | chunk_id: string; |
| 278 | fact_id: string; |
| 279 | subject_id: string; |
| 280 | subject: string; |
| 281 | /** 本体没认下这条关系时是原文说法;两者都拿不出时为 null */ |
| 282 | predicate: string | null; |
| 283 | /** true = 名字来自原文,不是本体认下的关系 */ |
| 284 | inferred: boolean; |
| 285 | object_id: string | null; |
| 286 | object: string | null; |
| 287 | valid_from: string | null; |
| 288 | valid_to: string | null; |
| 289 | confidence: number; |
| 290 | } |
| 291 | |
| 292 | /** 一条推出来的事实,连同它的证明(实体面板的「推出来的」那一档)。 |
| 293 | * |
| 294 | * `premises` 是这一档存在的理由:不给出前提的话,一条派生边跟一条普通的边 |
| 295 | * 在界面上看不出区别,而那正是「推理污染知识」的样子。 */ |
| 296 | export interface DerivedFact { |
| 297 | id: string; |
| 298 | subject_id: string; |
| 299 | subject: string; |
| 300 | object_id: string; |
| 301 | object: string; |
| 302 | predicate: string; |
| 303 | /** 靠哪条规则推的 */ |
| 304 | /** 靠哪条规则推的。后两种是 0017 补上的跨谓词规则 */ |
| 305 | rule: "transitive" | "symmetric" | "inverse" | "sub_property"; |
| 306 | valid_from: string | null; |
| 307 | valid_to: string | null; |
| 308 | confidence: number; |
| 309 | derived_at: string; |
| 310 | /** 直接前提,按推导顺序 */ |
| 311 | premises: string[]; |
| 312 | } |
| 313 | |
| 314 | /** 一条**没有落地**的派生(0017 §3):推出来了,撞上一条断言,拦在图外。 |
| 315 | * 没有自己的 id,用那条违规的 id 指它——面板、幽灵边、Review 卡片三处靠它对上 */ |
| 316 | export interface BlockedDerivation { |
| 317 | violation_id: string; |
| 318 | subject_id: string; |
| 319 | subject: string; |
| 320 | object_id: string; |
| 321 | object: string; |
| 322 | predicate: string; |
| 323 | rule: string; |
| 324 | via_label: string; |
| 325 | valid_from: string | null; |
| 326 | valid_to: string | null; |
| 327 | against_fact: string; |
| 328 | against_text: string; |
| 329 | premises: string[]; |
| 330 | } |
| 331 | |
| 332 | /** 证明的一步:一条断言前提,带它的证据。前提一律是断言,所以证明是链不是树 */ |
| 333 | export interface ProofStep { |
| 334 | seq: number; |
| 335 | fact_id: string; |
| 336 | subject_id: string; |
| 337 | subject: string; |
| 338 | predicate_id: string | null; |
| 339 | predicate: string | null; |
| 340 | object_id: string | null; |
| 341 | object: string | null; |
| 342 | valid_from: string | null; |
| 343 | valid_to: string | null; |
| 344 | confidence: number; |
| 345 | /** 这条前提后来被撤了;派生随之失效,证明仍要读得出当时靠的是什么 */ |
| 346 | retracted: boolean; |
| 347 | evidence: Evidence[]; |
| 348 | } |
| 349 | |
| 350 | export interface Proof { |
| 351 | derived: DerivedFact; |
| 352 | steps: ProofStep[]; |
| 353 | } |
| 354 | |
| 355 | /** 审核页的分档。**与服务端的 queue 参数是同一组字面量**——拼错会拿到 |
| 356 | * 一个明确的 unknown_queue 错误,而不是悄悄的空列表。 */ |
| 357 | export type ReviewQueue = |
| 358 | | "pending" |
| 359 | | "duplicates" |
| 360 | | "conflicts" |
| 361 | | "unconfirmed" |
| 362 | | "lowconf" |
| 363 | | "mappings" |
| 364 | | "violations" |
| 365 | | "defects" |
| 366 | | "merges"; |
| 367 | |
| 368 | /** 各档的真实条数。左栏的徽标读它,不读列表长度 */ |
| 369 | export interface ReviewCounts { |
| 370 | /** 记忆抽出、等人点头的事实(0015) */ |
| 371 | pending: number; |
| 372 | duplicates: number; |
| 373 | conflicts: number; |
| 374 | unconfirmed: number; |
| 375 | lowconf: number; |
| 376 | mappings: number; |
| 377 | violations: number; |
| 378 | defects: number; |
| 379 | merges: number; |
| 380 | } |
| 381 | |
| 382 | /** 类型消解的一条建议:一个待精化的实体、送去检索的画像、以及候选类。 |
| 383 | * |
| 384 | * `profile` 回给调用方是有意的——检索找不着的时候,第一个要看的就是 |
| 385 | * 「我们拿什么去找的」,而不是猜是画像不对还是类的描述不对。 */ |
| 386 | export interface TypeSuggestion { |
| 387 | entity_id: string; |
| 388 | name: string; |
| 389 | /** 现在挂着的类,可能没有(0009) */ |
| 390 | coarse: string | null; |
| 391 | coarse_description: string | null; |
| 392 | proposed_type: string | null; |
| 393 | specific_type: string | null; |
| 394 | fact_count: number; |
| 395 | profile: string; |
| 396 | candidates: { |
| 397 | id: string; |
| 398 | key: string; |
| 399 | label: string; |
| 400 | description: string; |
| 401 | distance: number; |
| 402 | }[]; |
| 403 | } |
| 404 | |
| 405 | /** 跑完一轮的结果。**三档分开报**:自动改了的、留给人的、裁决说「都不是」的。 |
| 406 | * 最后那一档带着理由——这一步押在「选择都不是是个体面答案」上, |
| 407 | * 不记理由,最大的那一档就是不透明的。 */ |
| 408 | export interface ResolutionOutcome { |
| 409 | batch: string | null; |
| 410 | retyped: number; |
| 411 | for_review: { |
| 412 | entity_id: string; |
| 413 | name: string; |
| 414 | coarse: string | null; |
| 415 | from_type_id: string | null; |
| 416 | to_type_id: string; |
| 417 | choice: string; |
| 418 | confidence: number; |
| 419 | reason: string | null; |
| 420 | /** 选中的类不在粗类的子树里——换的是分类轴,不是往下走一格 */ |
| 421 | crosses_axis: boolean; |
| 422 | }[]; |
| 423 | left_alone: { |
| 424 | name: string; |
| 425 | coarse: string | null; |
| 426 | specific_type: string | null; |
| 427 | reason: string | null; |
| 428 | top_candidate: string | null; |
| 429 | }[]; |
| 430 | } |
| 431 | export interface ReviewSide { |
| 432 | id: string; |
| 433 | name: string; |
| 434 | // 没判出类型时为 null(0009) |
| 435 | type_label: string | null; |
| 436 | color: string; |
| 437 | disambiguator: string | null; |
| 438 | degree: number; |
| 439 | top_facts: string[]; |
| 440 | } |
| 441 | |
| 442 | export interface ReviewItem { |
| 443 | id: string; |
| 444 | score: number; |
| 445 | reason: string | null; |
| 446 | stage: "adjudicating" | "human"; |
| 447 | created_at: string; |
| 448 | left: ReviewSide; |
| 449 | right: ReviewSide; |
| 450 | } |
| 451 | |
| 452 | /** 数据映射的一条口径:业务概念 → 数据资产定义(见 docs/decisions/0011)。 |
| 453 | * |
| 454 | * 字段是列而不是 JSON 里的键——从前它是一条 `mapped_to` 事实, |
| 455 | * 这几样全塞在 `object_value` 里。 */ |
| 456 | export interface ConceptMapping { |
| 457 | id: string; |
| 458 | concept_id: string; |
| 459 | concept_name: string; |
| 460 | /** 挂载的数据源。同一概念在不同源上可以有不同定义 */ |
| 461 | source: string; |
| 462 | table_name: string | null; |
| 463 | expr: string | null; |
| 464 | sql: string | null; |
| 465 | unit: string | null; |
| 466 | summary: string | null; |
| 467 | /** 派生指标:算出来的,不是表里的列 */ |
| 468 | derived: boolean; |
| 469 | status: "proposed" | "confirmed" | "rejected"; |
| 470 | } |
| 471 | /** 一条口径改动之前的样子。**整版快照而不是差异**(0006): |
| 472 | * 读的时候要回答的是「当时是什么」,差异得从头重放才答得出来 */ |
| 473 | export interface MappingRevision { |
| 474 | id: string; |
| 475 | before: Record<string, unknown>; |
| 476 | /** 改的人;用户是软删除的,所以归因不会因为人离职而丢 */ |
| 477 | changed_by_name: string | null; |
| 478 | changed_at: string; |
| 479 | } |
| 480 | /** 一处公理违规(0002 R0)。判据来自本体自己声明的公理,没声明就不报 */ |
| 481 | /** derived_contradiction 独有(0017):推出来的那条三元组——它没有落库, |
| 482 | * 只能在这里写出来。其它种类是 `{}` */ |
| 483 | export interface ViolationDetail { |
| 484 | axiom?: "functional" | "asymmetry" | "self_loop"; |
| 485 | rule?: "transitive" | "symmetric" | "inverse" | "sub_property"; |
| 486 | via_label?: string; |
| 487 | subject?: string; |
| 488 | predicate?: string; |
| 489 | object?: string; |
| 490 | valid_from?: string | null; |
| 491 | valid_to?: string | null; |
| 492 | premises?: string[]; |
| 493 | } |
| 494 | export type ViolationResolution = |
| 495 | | "fact_retracted" |
| 496 | | "fact_closed" |
| 497 | | "axiom_relaxed" |
| 498 | | "accepted"; |
| 499 | export interface AxiomViolation { |
| 500 | id: string; |
| 501 | kind: |
| 502 | | "self_loop" |
| 503 | | "asymmetry" |
| 504 | | "cycle" |
| 505 | | "functional" |
| 506 | | "signature" |
| 507 | | "derived_contradiction"; |
| 508 | /** 判据来自哪条关系。判「公理写错了」时从这里进本体去改 */ |
| 509 | predicate: string | null; |
| 510 | left_fact: string; |
| 511 | left_text: string; |
| 512 | /** 自反那一类与 left 相同——一条事实跟自己矛盾 */ |
| 513 | right_fact: string; |
| 514 | right_text: string; |
| 515 | /** 环的长度;其余三类为 0 */ |
| 516 | path_len: number; |
| 517 | detected_at: string; |
| 518 | detail: ViolationDetail; |
| 519 | /** 审核线索(0017 §2),一次只给一条:旧断言没写结束日期、有同名实体、 |
| 520 | * 抽取置信度低。没有就空 */ |
| 521 | hint: "stale" | "duplicate" | "unsure" | null; |
| 522 | } |
| 523 | /** 本体自己的一处自相矛盾。**与 AxiomViolation 不是一回事**:那个说 |
| 524 | * 「事实与定义抵触」,这个说「定义自己站不住」,后者更根本 */ |
| 525 | export interface OntologyDefect { |
| 526 | id: string; |
| 527 | kind: |
| 528 | | "symmetric_and_asymmetric" |
| 529 | | "transitive_and_functional" |
| 530 | | "subclass_cycle" |
| 531 | | "disjoint_with_ancestor" |
| 532 | | "inherits_disjoint" |
| 533 | // 0017 加的三类:都在谓词上,前两类关于逆,第三类是子属性成环 |
| 534 | | "inverse_of_itself" |
| 535 | | "inverse_not_mutual" |
| 536 | | "sub_property_cycle" |
| 537 | // 0017:两条规则加在一起产出互斥的派生,按规则对聚合报一次 |
| 538 | | "rules_disagree"; |
| 539 | subject_label: string | null; |
| 540 | other_label: string | null; |
| 541 | path_labels: string[]; |
| 542 | detected_at: string; |
| 543 | detail: DefectDetail; |
| 544 | } |
| 545 | /** rules_disagree 独有:哪两条规则、撞在哪条公理上、几对、几个例子 */ |
| 546 | export interface DefectDetail { |
| 547 | count?: number; |
| 548 | rules?: { |
| 549 | rule_a: string; |
| 550 | via_a: string; |
| 551 | rule_b: string; |
| 552 | via_b: string; |
| 553 | axiom: string; |
| 554 | count: number; |
| 555 | examples: [string, string][]; |
| 556 | }[]; |
| 557 | } |
| 558 | export interface FactReviewItem { |
| 559 | id: string; |
| 560 | subject_name: string; |
| 561 | predicate_label: string | null; |
| 562 | object_name: string | null; |
| 563 | valid_from: string | null; |
| 564 | valid_to: string | null; |
| 565 | confidence: number; |
| 566 | evidence_count: number; |
| 567 | quote: string | null; |
| 568 | } |
| 569 | |
| 570 | /** 一句记忆抽出、等人点头的事实(0015)。`quote` 是那句记忆的全文—— |
| 571 | * 确认界面把原句放在三元组上面,人对着原句判断,而不是凭空判断三元组。 */ |
| 572 | export interface PendingFactItem { |
| 573 | id: string; |
| 574 | subject_id: string; |
| 575 | subject_name: string; |
| 576 | predicate_id: string | null; |
| 577 | /** 本体里的关系名;为空时显示 `proposed_predicate`(斜体,标明是原话) */ |
| 578 | predicate_label: string | null; |
| 579 | proposed_predicate: string | null; |
| 580 | object_id: string | null; |
| 581 | object_name: string | null; |
| 582 | object_value: { value?: unknown; unit?: string; summary?: string } | null; |
| 583 | valid_from: string | null; |
| 584 | valid_to: string | null; |
| 585 | confidence: number; |
| 586 | chunk_id: string; |
| 587 | quote: string; |
| 588 | proposed_by: string | null; |
| 589 | proposed_by_name: string | null; |
| 590 | created_at: string; |
| 591 | } |
| 592 | |
| 593 | /** 时态冲突(自动闭合拿不准的那些):旧事实 vs 新事实。 */ |
| 594 | export interface ConflictItem { |
| 595 | id: string; |
| 596 | reason: "no_time" | "simultaneous" | "low_confidence"; |
| 597 | created_at: string; |
| 598 | predicate_label: string; |
| 599 | old_fact_id: string; |
| 600 | old_subject: string; |
| 601 | old_object: string | null; |
| 602 | old_valid_from: string | null; |
| 603 | new_fact_id: string; |
| 604 | new_subject: string; |
| 605 | new_object: string | null; |
| 606 | new_valid_from: string | null; |
| 607 | new_confidence: number; |
| 608 | } |
| 609 | |
| 610 | /** 决策台账事件(audit_events 的 review 域切片):detail 是决策时的自包含快照。 */ |
| 611 | export interface ReviewHistoryEvent { |
| 612 | id: string; |
| 613 | action: string; |
| 614 | target_kind: string; |
| 615 | target_id: string | null; |
| 616 | detail: Record<string, unknown>; |
| 617 | /** null = 系统(AI 裁决器) */ |
| 618 | actor_name: string | null; |
| 619 | created_at: string; |
| 620 | } |
| 621 | |
| 622 | export interface MergeLog { |
| 623 | id: string; |
| 624 | source_name: string; |
| 625 | target_name: string; |
| 626 | merged_by_name: string | null; |
| 627 | reason: string | null; |
| 628 | created_at: string; |
| 629 | reverted_at: string | null; |
| 630 | } |
| 631 | |
| 632 | export interface GraphEdge { |
| 633 | id: string; |
| 634 | source: string; |
| 635 | target: string; |
| 636 | /** 本体没认下这条关系时是原文说法;两者都拿不出时为 null(0052 之前的老数据) */ |
| 637 | predicate: string | null; |
| 638 | label: string | null; |
| 639 | /** true = 这条边的名字来自原文,不是本体认下的关系 */ |
| 640 | inferred: boolean; |
| 641 | /** true = 这条边是**推出来的**,不是任何人断言的(R1)。 |
| 642 | * 与 `inferred` 不是一回事:那个说「名字来自原文」,这个说「不是谁说的」 */ |
| 643 | derived: boolean; |
| 644 | /** 推它出来的那条规则;断言的边为 null。 |
| 645 | * 界面靠它认出 `inverse`——那种边与来源边是同一件事的两种说法, |
| 646 | * 画两条只是把冗余画了两遍(见 Graph 的 `layOutParallelEdges`) */ |
| 647 | rule: string | null; |
| 648 | /** 推它出来用到的前提事实 id(按证明顺序);断言的边为空。 |
| 649 | * 并边要靠它认准来源——只按节点对匹配会把说法挂到错的边上 */ |
| 650 | premises: string[]; |
| 651 | valid_from: string | null; |
| 652 | valid_to: string | null; |
| 653 | confidence: number; |
| 654 | /** 有争议(0017 §3):有一条 open 的公理违规或时态冲突指着它。整条边画成警戒色 */ |
| 655 | contested: boolean; |
| 656 | /** 幽灵边(0017 §3):没落地的派生。`id` 是那条 `derived_contradiction` 违规的 id; |
| 657 | * `derived` 同时为 true,跟着派生开关走。点它打开主语的面板 */ |
| 658 | blocked: boolean; |
| 659 | } |
| 660 | |
| 661 | export interface EntityFact { |
| 662 | id: string; |
| 663 | direction: "out" | "in"; |
| 664 | /** 同 GraphEdge:本体外的关系回落到原文说法,两者都没有时为 null */ |
| 665 | predicate_key: string | null; |
| 666 | predicate_label: string | null; |
| 667 | /** true = 名字来自原文,不是本体认下的关系 */ |
| 668 | inferred: boolean; |
| 669 | /** 关系的时态类别。没有谓词就无从谈起,为 null */ |
| 670 | temporal: string | null; |
| 671 | other_id: string | null; |
| 672 | other_name: string | null; |
| 673 | /** 字面值宾语(属性事实/问数映射):{"value":…} 或 {"summary":…} */ |
| 674 | object_value: Record<string, unknown> | null; |
| 675 | valid_from: string | null; |
| 676 | valid_to: string | null; |
| 677 | valid_from_precision: string | null; |
| 678 | /** year | month | day,外加 unknown = 原文说它结束了但没说哪天 */ |
| 679 | valid_to_precision: string | null; |
| 680 | confidence: number; |
| 681 | evidence_count: number; |
| 682 | /** 证据全部停留在来源文档的旧版(未被现行内容确认;不代表事实失效) */ |
| 683 | stale: boolean; |
| 684 | /** 修正行:区间闭合来自引擎对账/人工裁决而非抽取原文 */ |
| 685 | corrected: boolean; |
| 686 | /** 有争议(0017 §3):哪一种、Review 里那一项的 id、派生撞断言时推出来的那句话。 |
| 687 | * 行不压暗——断言仍然活着 */ |
| 688 | contested: { |
| 689 | kind: string; |
| 690 | ref_id: string; |
| 691 | derived?: string | null; |
| 692 | } | null; |
| 693 | /** 证据集合里最新的文档时间(开放事实的"最后确认时间") */ |
| 694 | last_evidence_time: string | null; |
| 695 | } |
| 696 | |
| 697 | /** 实体的一次认知变更(记录时间轴上的事件,与 EntityFact 的有效时间轴正交)。 |
| 698 | * |
| 699 | * 不是每个事件都来自一条事实:retyped / retype_reverted 来自改类账本, |
| 700 | * 没有谓词、没有对方、没有方向。 */ |
| 701 | export interface EntityHistoryEvent { |
| 702 | /** 事实事件才有;改类事件为 null */ |
| 703 | fact_id: string | null; |
| 704 | /** 记录时刻:写入 = recorded_at,作废 = invalidated_at,改类 = 改类那一刻 */ |
| 705 | at: string; |
| 706 | kind: |
| 707 | | "asserted" |
| 708 | | "corrected" |
| 709 | | "rejected" |
| 710 | | "merged" |
| 711 | | "retyped" |
| 712 | | "retype_reverted"; |
| 713 | direction: "out" | "in" | null; |
| 714 | predicate_label: string | null; |
| 715 | other_name: string | null; |
| 716 | object_value: Record<string, unknown> | null; |
| 717 | valid_from: string | null; |
| 718 | valid_to: string | null; |
| 719 | valid_from_precision: string | null; |
| 720 | /** year | month | day,外加 unknown = 原文说它结束了但没说哪天 */ |
| 721 | valid_to_precision: string | null; |
| 722 | confidence: number | null; |
| 723 | /** null = 引擎自动(抽取写入 / 时态对账闭合 / 高置信自动改类) */ |
| 724 | actor_name: string | null; |
| 725 | action: string | null; |
| 726 | document_id: string | null; |
| 727 | filename: string | null; |
| 728 | quote: string | null; |
| 729 | /** 改类事件的两端。起点为 null = 从「未分类」改过来(0009 之后最常见的一种) */ |
| 730 | from_type_label: string | null; |
| 731 | to_type_label: string | null; |
| 732 | } |
| 733 | |
| 734 | export interface Evidence { |
| 735 | /** 模型在这一块里实际用的谓词说法。本体外的谓词不落到关系上,界面显示的就是这里 */ |
| 736 | proposed_predicate: string | null; |
| 737 | quote: string | null; |
| 738 | chunk_id: string; |
| 739 | document_id: string; |
| 740 | filename: string; |
| 741 | seq: number; |
| 742 | /** 证据出自文档的第几版 */ |
| 743 | doc_version: number; |
| 744 | /** 文档已有更新版本(证据停留在旧版;不代表事实失效) */ |
| 745 | stale: boolean; |
| 746 | } |
| 747 | |
| 748 | export interface ChunkFull { |
| 749 | id: string; |
| 750 | seq: number; |
| 751 | text: string; |
| 752 | } |
| 753 | |
| 754 | export interface EntityTypeView { |
| 755 | id: string; |
| 756 | key: string; |
| 757 | label: string; |
| 758 | color: string; |
| 759 | shape: "circle" | "square"; |
| 760 | builtin: boolean; |
| 761 | /** 全部父类(subClassOf 可以有多个) */ |
| 762 | parents: string[]; |
| 763 | /** 与它互斥的类:**声明「不可能同时是」** */ |
| 764 | disjoint: string[]; |
| 765 | /** 左栏画树时挂在哪一支下。不参与语义,只管展示 */ |
| 766 | primary_parent: string | null; |
| 767 | description: string; |
| 768 | usage: number; |
| 769 | } |
| 770 | |
| 771 | export interface RelationTypeView { |
| 772 | id: string; |
| 773 | key: string; |
| 774 | label: string; |
| 775 | temporal: string; |
| 776 | functional: boolean; |
| 777 | inverse_functional: boolean; |
| 778 | /** 其余四条 OWL 公理。**推理机的判据全在这里** */ |
| 779 | is_transitive: boolean; |
| 780 | is_symmetric: boolean; |
| 781 | is_asymmetric: boolean; |
| 782 | is_irreflexive: boolean; |
| 783 | /** 指向另一个关系的两条:`p⁻¹ = q` 与 `p ⊑ q`。是 id 不是布尔, |
| 784 | * 所以界面上是下拉框 */ |
| 785 | inverse_of: string | null; |
| 786 | sub_property_of: string | null; |
| 787 | builtin: boolean; |
| 788 | description: string; |
| 789 | /** relation(宾语是实体)| attribute(宾语是字面值) */ |
| 790 | kind: "relation" | "attribute"; |
| 791 | /** 可以当主语的类。attribute 至少一个;relation 留空 = 不限 */ |
| 792 | domains: string[]; |
| 793 | /** 可以当宾语的类。只对 relation 有意义——attribute 的值域是 datatype */ |
| 794 | ranges: string[]; |
| 795 | datatype: "text" | "number" | "date" | "bool" | null; |
| 796 | unit: string | null; |
| 797 | usage: number; |
| 798 | } |
| 799 | |
| 800 | export interface OntologyMiss { |
| 801 | kind: "entity_type" | "relation_type"; |
| 802 | key: string; |
| 803 | example: string | null; |
| 804 | count: number; |
| 805 | } |
| 806 | |
| 807 | /** `description` 与 `reason` 不是一回事:description 逐字进抽取提示词,是模型判断 |
| 808 | "什么算这个类"的唯一依据;reason 只是给人看的"为什么该加"。喂错了这个类会成为 |
| 809 | 下一个倾倒场——实测 technology 就是这么来的。 */ |
| 810 | export interface OntologyProposals { |
| 811 | entity_types: { |
| 812 | key: string; |
| 813 | label: string; |
| 814 | description?: string; |
| 815 | reason?: string; |
| 816 | }[]; |
| 817 | relation_types: { |
| 818 | key: string; |
| 819 | label: string; |
| 820 | temporal?: string; |
| 821 | functional?: boolean; |
| 822 | description?: string; |
| 823 | reason?: string; |
| 824 | /** 这条关系归并了哪些表层说法。有它才谈得上把等待的事实改写过去 */ |
| 825 | forms?: string[]; |
| 826 | }[]; |
| 827 | /** |
| 828 | * 宾语是字面值的说法("成立日期 = 2015")。 |
| 829 | * |
| 830 | * 跟 relation_types 分开是因为它们要的东西不一样:属性有 datatype, |
| 831 | * 而把它当关系建出来,那个值就会变成一个假实体。domain 不在这里—— |
| 832 | * 服务端从事实的主语类型里取,猜错会让整条被丢弃 |
| 833 | */ |
| 834 | attribute_types?: { |
| 835 | key: string; |
| 836 | label: string; |
| 837 | datatype?: string; |
| 838 | unit?: string; |
| 839 | description?: string; |
| 840 | reason?: string; |
| 841 | forms?: string[]; |
| 842 | }[]; |
| 843 | /** |
| 844 | * 本体里**已经有**这个意思,只需把说法挂过去。 |
| 845 | * |
| 846 | * 跟 relation_types 的区别是不建东西:同一个意思长出第二个 key, |
| 847 | * 这批事实就永久分在两处,谁也认不出它们本是一回事。 |
| 848 | */ |
| 849 | map_to?: { |
| 850 | key: string; |
| 851 | /** 服务端标的:目标落在关系还是属性上。两条改写路径不一样, |
| 852 | 而模型只答得出一个 key,看不出它在哪一档 */ |
| 853 | kind?: string; |
| 854 | forms?: string[]; |
| 855 | reason?: string; |
| 856 | }[]; |
| 857 | } |
| 858 | |
| 859 | /** 原文说过、本体里没有、因而事实没有谓词的说法。 */ |
| 860 | export interface ProposedPredicate { |
| 861 | form: string; |
| 862 | fact_count: number; |
| 863 | example: string | null; |
| 864 | } |
| 865 | |
| 866 | /** 一个类/属性在这次导入里的去向。key_taken = key 被另一个 IRI 占着,报告但不动 */ |
| 867 | export interface PlannedItem { |
| 868 | iri: string; |
| 869 | key: string; |
| 870 | label: string; |
| 871 | has_description: boolean; |
| 872 | disposition: "create" | "update" | "key_taken"; |
| 873 | functional?: boolean; |
| 874 | conflict_with?: string | null; |
| 875 | } |
| 876 | |
| 877 | /** 预览与落库返回同一个计划:点确认之后发生的事就是刚看过的事 */ |
| 878 | export interface ImportPlan { |
| 879 | format: string; |
| 880 | triples: number; |
| 881 | classes: PlannedItem[]; |
| 882 | relations: PlannedItem[]; |
| 883 | attributes: PlannedItem[]; |
| 884 | /** 出现过但今天不消费的公理 → 次数。不是已跳过,是暂未投影 */ |
| 885 | unprojected: [string, number][]; |
| 886 | classes_without_description: number; |
| 887 | functional_relations: number; |
| 888 | } |
| 889 | |
| 890 | export interface OntologyImportView { |
| 891 | id: string; |
| 892 | filename: string; |
| 893 | format: string; |
| 894 | byte_size: number; |
| 895 | summary: Record<string, unknown>; |
| 896 | imported_by_name: string | null; |
| 897 | imported_at: string; |
| 898 | } |
| 899 | |
| 900 | export interface Source { |
| 901 | n: number; |
| 902 | /** 缺省 = 文档 chunk 引用;charter = 内置手册(跳 /docs/{slug}#{anchor}) */ |
| 903 | kind?: "charter"; |
| 904 | chunk_id?: string; |
| 905 | document_id?: string; |
| 906 | slug?: string; |
| 907 | anchor?: string; |
| 908 | heading?: string; |
| 909 | filename: string; |
| 910 | excerpt: string; |
| 911 | } |
| 912 | |
| 913 | /** Agentic 对话的行动轨迹(工具调用一步一条)。 */ |
| 914 | export interface ChatStep { |
| 915 | kind: "search" | "docs" | "entity" | "facts" | "changes" | "query" | "tool"; |
| 916 | label: string; |
| 917 | detail: string; |
| 918 | /** `remember` 那一步带着它:那句记忆落成的 chunk。对话里的确认卡按它取 |
| 919 | * 待确认项(0015);回放时也据此重画 */ |
| 920 | chunk_id?: string; |
| 921 | /** 这一步发生时正文已经有多长(UTF-16 码元,与 `string.length` 同一单位)。 |
| 922 | * 据此把轨迹穿回正文里,而不是全堆在最前面。 |
| 923 | * **这条迁移之前的消息没有它**——缺省时整段轨迹回到顶部,即旧的样子 */ |
| 924 | at?: number; |
| 925 | } |
| 926 | |
| 927 | /** 会话行(Chat 左栏列表)。 */ |
| 928 | export interface ConversationRow { |
| 929 | id: string; |
| 930 | title: string; |
| 931 | created_at: string; |
| 932 | updated_at: string; |
| 933 | message_count: number; |
| 934 | } |
| 935 | |
| 936 | /** 会话消息(含落库的行动轨迹与引用,历史回放用)。 */ |
| 937 | export interface ConversationMessage { |
| 938 | id: string; |
| 939 | role: "user" | "assistant"; |
| 940 | content: string; |
| 941 | steps: ChatStep[]; |
| 942 | sources: Source[]; |
| 943 | created_at: string; |
| 944 | } |
| 945 | |
| 946 | /** 告警中心的一组(0005):**连着的、同类的几次故障**。 |
| 947 | * |
| 948 | * 存储那边仍是一次故障一行,折叠在服务端读的时候做——这样翻页数的是组, |
| 949 | * 一段连续故障不会被页边界切断。 */ |
| 950 | export type AlertGroup = { |
| 951 | kb_id: string | null; |
| 952 | /** 系统级告警没有库名 */ |
| 953 | kb_name: string | null; |
| 954 | /** `source.sync_failed` / `llm.unreachable` —— 措辞在 i18n 里按这个查 */ |
| 955 | kind: string; |
| 956 | severity: "info" | "warning" | "error"; |
| 957 | /** 这一组几次 */ |
| 958 | count: number; |
| 959 | /** 其中我没读过的几次 */ |
| 960 | unread: number; |
| 961 | latest_at: string; |
| 962 | /** 跟 latest_at 一起圈出这一组,标已读时原样发回去 */ |
| 963 | earliest_at: string; |
| 964 | /** 明细,最多几条,新的在前 */ |
| 965 | lines: { name?: string; error?: string; job?: string }[]; |
| 966 | }; |
| 967 | |
| 968 | export const api = { |
| 969 | health: () => |
| 970 | request<{ status: string; name: string; version: string }>( |
| 971 | "/api/v1/health", |
| 972 | ), |
| 973 | me: () => request<User>("/api/v1/auth/me"), |
| 974 | alerts: (o: { q?: string; limit?: number; offset?: number }) => { |
| 975 | const p = new URLSearchParams(); |
| 976 | if (o.q?.trim()) p.set("q", o.q.trim()); |
| 977 | if (o.limit != null) p.set("limit", String(o.limit)); |
| 978 | if (o.offset) p.set("offset", String(o.offset)); |
| 979 | return request<{ items: AlertGroup[]; total: number }>( |
| 980 | `/api/v1/alerts?${p}`, |
| 981 | ); |
| 982 | }, |
| 983 | alertsUnread: () => request<{ unread: number }>("/api/v1/alerts/unread"), |
| 984 | alertReadGroup: (g: { |
| 985 | kb_id: string | null; |
| 986 | kind: string; |
| 987 | from: string; |
| 988 | to: string; |
| 989 | }) => |
| 990 | request<{ marked: number }>("/api/v1/alerts/read-group", { |
| 991 | method: "POST", |
| 992 | body: JSON.stringify(g), |
| 993 | }), |
| 994 | alertsReadAll: () => |
| 995 | request<{ ok: boolean }>("/api/v1/alerts/read-all", { method: "POST" }), |
| 996 | login: (email: string, password: string) => |
| 997 | request<{ user: User }>("/api/v1/auth/login", { |
| 998 | method: "POST", |
| 999 | body: JSON.stringify({ email, password }), |
| 1000 | }), |
| 1001 | register: (email: string, password: string, displayName: string) => |
| 1002 | request<{ user: User; workspace: Workspace }>("/api/v1/auth/register", { |
| 1003 | method: "POST", |
| 1004 | body: JSON.stringify({ email, password, display_name: displayName }), |
| 1005 | }), |
| 1006 | logout: () => |
| 1007 | request<{ ok: boolean }>("/api/v1/auth/logout", { method: "POST" }), |
| 1008 | updateMe: (displayName: string) => |
| 1009 | request<User>("/api/v1/auth/me", { |
| 1010 | method: "PATCH", |
| 1011 | body: JSON.stringify({ display_name: displayName }), |
| 1012 | }), |
| 1013 | changePassword: (currentPassword: string, newPassword: string) => |
| 1014 | request<{ ok: boolean }>("/api/v1/auth/password", { |
| 1015 | method: "POST", |
| 1016 | body: JSON.stringify({ |
| 1017 | current_password: currentPassword, |
| 1018 | new_password: newPassword, |
| 1019 | }), |
| 1020 | }), |
| 1021 | /** 我发过的个人令牌(0014)。撤销过的也在——撤过这件事本身要看得见 */ |
| 1022 | tokens: () => request<{ tokens: TokenView[] }>("/api/v1/me/tokens"), |
| 1023 | /** 发一枚。**明文只在这一次的响应里**,列表永远给不出它 */ |
| 1024 | issueToken: (body: { |
| 1025 | name: string; |
| 1026 | scope: "read" | "write"; |
| 1027 | /** 缺省 = 这个人能进的全部库 */ |
| 1028 | kb_ids?: string[] | null; |
| 1029 | /** 0 = 不过期 */ |
| 1030 | expires_in_days: number; |
| 1031 | }) => |
| 1032 | request<{ token: string; info: TokenView }>("/api/v1/me/tokens", { |
| 1033 | method: "POST", |
| 1034 | body: JSON.stringify(body), |
| 1035 | }), |
| 1036 | revokeToken: (tokenId: string) => |
| 1037 | request<{ ok: boolean }>(`/api/v1/me/tokens/${tokenId}`, { method: "DELETE" }), |
| 1038 | workspaces: () => request<Workspace[]>("/api/v1/workspaces"), |
| 1039 | |
| 1040 | kbs: (workspaceId: string) => |
| 1041 | request<Kb[]>(`/api/v1/workspaces/${workspaceId}/kbs`), |
| 1042 | /** 审计台账。**分页 + 筛选**——台账是合规材料,只看最近 100 条等于查不了历史。 |
| 1043 | * `action` 是前缀:`entity.` 捞出 entity.retyped / entity.renamed 一族。 */ |
| 1044 | kbAudit: ( |
| 1045 | kbId: string, |
| 1046 | opts: { |
| 1047 | action?: string; |
| 1048 | actor?: string; |
| 1049 | since?: string; |
| 1050 | until?: string; |
| 1051 | limit: number; |
| 1052 | offset: number; |
| 1053 | }, |
| 1054 | ) => { |
| 1055 | const p = new URLSearchParams({ |
| 1056 | limit: String(opts.limit), |
| 1057 | offset: String(opts.offset), |
| 1058 | }); |
| 1059 | if (opts.action) p.set("action", opts.action); |
| 1060 | if (opts.actor) p.set("actor", opts.actor); |
| 1061 | if (opts.since) p.set("since", opts.since); |
| 1062 | if (opts.until) p.set("until", opts.until); |
| 1063 | return request<{ |
| 1064 | events: AuditEvent[]; |
| 1065 | total: number; |
| 1066 | /** 这个库实际发生过的动作,筛选下拉按它填 */ |
| 1067 | actions: string[]; |
| 1068 | }>(`/api/v1/kbs/${kbId}/audit?${p}`); |
| 1069 | }, |
| 1070 | myKbs: (workspaceId: string) => |
| 1071 | request<{ kbs: MyKb[] }>(`/api/v1/workspaces/${workspaceId}/my-kbs`), |
| 1072 | createKb: ( |
| 1073 | workspaceId: string, |
| 1074 | body: { |
| 1075 | name: string; |
| 1076 | description?: string | null; |
| 1077 | visibility?: string; |
| 1078 | /** 预置本体包 id,顺序有意义:第一个会认领同名的种子类 */ |
| 1079 | ontology_packs?: string[]; |
| 1080 | }, |
| 1081 | ) => |
| 1082 | request<Kb>(`/api/v1/workspaces/${workspaceId}/kbs`, { |
| 1083 | method: "POST", |
| 1084 | body: JSON.stringify(body), |
| 1085 | }), |
| 1086 | |
| 1087 | ontologyPacks: () => |
| 1088 | request<{ packs: OntologyPack[] }>("/api/v1/ontology-packs"), |
| 1089 | |
| 1090 | kbDetail: (kbId: string) => request<Kb>(`/api/v1/kbs/${kbId}`), |
| 1091 | updateKb: (kbId: string, body: Record<string, unknown>) => |
| 1092 | request<Kb>(`/api/v1/kbs/${kbId}`, { |
| 1093 | method: "PATCH", |
| 1094 | body: JSON.stringify(body), |
| 1095 | }), |
| 1096 | deleteKb: (kbId: string) => |
| 1097 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}`, { method: "DELETE" }), |
| 1098 | kbMembers: (kbId: string) => |
| 1099 | request<{ members: KbMember[] }>(`/api/v1/kbs/${kbId}/members`), |
| 1100 | setKbMember: (kbId: string, userId: string, role: string) => |
| 1101 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/members/${userId}`, { |
| 1102 | method: "PUT", |
| 1103 | body: JSON.stringify({ role }), |
| 1104 | }), |
| 1105 | removeKbMember: (kbId: string, userId: string) => |
| 1106 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/members/${userId}`, { |
| 1107 | method: "DELETE", |
| 1108 | }), |
| 1109 | |
| 1110 | adminDeployment: () => |
| 1111 | request<{ |
| 1112 | open_registration: boolean; |
| 1113 | /** 外层兜底,防任务无限堆积;真正的节流是按模型的限额 */ |
| 1114 | worker_concurrency: number; |
| 1115 | default_model_concurrency: number; |
| 1116 | /** 新建知识库的本体语言默认值。**不是界面语言**——那个在客户端 */ |
| 1117 | default_ontology_lang: "en" | "zh"; |
| 1118 | model_limits: { |
| 1119 | base_url: string; |
| 1120 | model: string; |
| 1121 | max_concurrent: number; |
| 1122 | }[]; |
| 1123 | models_in_use: { base_url: string; model: string; kind: string }[]; |
| 1124 | }>("/api/v1/admin/deployment"), |
| 1125 | saveAdminDeployment: ( |
| 1126 | openRegistration: boolean, |
| 1127 | workerConcurrency?: number, |
| 1128 | defaultModelConcurrency?: number, |
| 1129 | modelLimit?: { |
| 1130 | base_url: string; |
| 1131 | model: string; |
| 1132 | max_concurrent: number | null; |
| 1133 | }, |
| 1134 | defaultOntologyLang?: "en" | "zh", |
| 1135 | ) => |
| 1136 | request<{ ok: boolean }>("/api/v1/admin/deployment", { |
| 1137 | method: "PUT", |
| 1138 | body: JSON.stringify({ |
| 1139 | open_registration: openRegistration, |
| 1140 | ...(workerConcurrency !== undefined |
| 1141 | ? { worker_concurrency: workerConcurrency } |
| 1142 | : {}), |
| 1143 | ...(defaultModelConcurrency !== undefined |
| 1144 | ? { default_model_concurrency: defaultModelConcurrency } |
| 1145 | : {}), |
| 1146 | ...(modelLimit ? { model_limit: modelLimit } : {}), |
| 1147 | ...(defaultOntologyLang |
| 1148 | ? { default_ontology_lang: defaultOntologyLang } |
| 1149 | : {}), |
| 1150 | }), |
| 1151 | }), |
| 1152 | adminCreateUser: (body: { |
| 1153 | email: string; |
| 1154 | display_name: string; |
| 1155 | password: string; |
| 1156 | role: string; |
| 1157 | }) => |
| 1158 | request<{ user: User }>("/api/v1/admin/users", { |
| 1159 | method: "POST", |
| 1160 | body: JSON.stringify(body), |
| 1161 | }), |
| 1162 | |
| 1163 | /** 已停用的账号。**没有它恢复就够不着**——那个人从所有列表里消失, |
| 1164 | * 而恢复接口要的正是他的 id */ |
| 1165 | deactivatedUsers: () => request<OrgUser[]>("/api/v1/users/deactivated"), |
| 1166 | /** 恢复一个停用的账号 */ |
| 1167 | adminReactivateUser: (userId: string) => |
| 1168 | request<{ ok: boolean }>(`/api/v1/admin/users/${userId}`, { |
| 1169 | method: "POST", |
| 1170 | }), |
| 1171 | /** 停用一个账号(软删除)。归因照旧查得到——审计、合并日志、改类账本都靠它 */ |
| 1172 | adminDeactivateUser: (userId: string) => |
| 1173 | request<{ ok: boolean }>(`/api/v1/admin/users/${userId}`, { |
| 1174 | method: "DELETE", |
| 1175 | }), |
| 1176 | adminDataSources: () => |
| 1177 | request<{ data_sources: DataSourceView[] }>("/api/v1/admin/data-sources"), |
| 1178 | adminCreateDataSource: (body: { name: string; conn_string: string }) => |
| 1179 | request<{ id: string }>("/api/v1/admin/data-sources", { |
| 1180 | method: "POST", |
| 1181 | body: JSON.stringify(body), |
| 1182 | }), |
| 1183 | adminDeleteDataSource: (id: string) => |
| 1184 | request<{ ok: boolean }>(`/api/v1/admin/data-sources/${id}`, { |
| 1185 | method: "DELETE", |
| 1186 | }), |
| 1187 | adminTestDataSource: (id: string) => |
| 1188 | request<{ ok: boolean }>(`/api/v1/admin/data-sources/${id}/test`, { |
| 1189 | method: "POST", |
| 1190 | }), |
| 1191 | /** 这个源授权给了哪些工作区(0014)。授权与挂载是两层: |
| 1192 | * 授权由系统管理员写,挂载由 KB 管理员在授权过的集合里挑 */ |
| 1193 | dataSourceGrants: (id: string) => |
| 1194 | request<{ workspaces: { id: string; name: string }[] }>( |
| 1195 | `/api/v1/admin/data-sources/${id}/grants`, |
| 1196 | ), |
| 1197 | grantDataSource: (id: string, workspaceId: string) => |
| 1198 | request<{ ok: boolean }>( |
| 1199 | `/api/v1/admin/data-sources/${id}/grants/${workspaceId}`, |
| 1200 | { method: "PUT" }, |
| 1201 | ), |
| 1202 | /** 收回授权。**连同该工作区里已挂上的一起卸掉**,返回卸了几个 */ |
| 1203 | revokeDataSource: (id: string, workspaceId: string) => |
| 1204 | request<{ ok: boolean; unmounted: number }>( |
| 1205 | `/api/v1/admin/data-sources/${id}/grants/${workspaceId}`, |
| 1206 | { method: "DELETE" }, |
| 1207 | ), |
| 1208 | |
| 1209 | /** 一页口径。**Viewer 就能看**——问数的答案直接由口径决定, |
| 1210 | * 看得见答案却看不见口径,等于要人信一个不给看的算法 */ |
| 1211 | mappings: ( |
| 1212 | kbId: string, |
| 1213 | opts: { |
| 1214 | status?: "proposed" | "confirmed" | "rejected"; |
| 1215 | q?: string; |
| 1216 | limit?: number; |
| 1217 | offset?: number; |
| 1218 | } = {}, |
| 1219 | ) => { |
| 1220 | const p = new URLSearchParams(); |
| 1221 | if (opts.status) p.set("status", opts.status); |
| 1222 | if (opts.q) p.set("q", opts.q); |
| 1223 | if (opts.limit != null) p.set("limit", String(opts.limit)); |
| 1224 | if (opts.offset != null) p.set("offset", String(opts.offset)); |
| 1225 | const qs = p.toString(); |
| 1226 | return request<{ |
| 1227 | items: ConceptMapping[]; |
| 1228 | total: number; |
| 1229 | counts: { proposed: number; confirmed: number; rejected: number }; |
| 1230 | }>(`/api/v1/kbs/${kbId}/mappings${qs ? `?${qs}` : ""}`); |
| 1231 | }, |
| 1232 | /** 改一条口径。改之前那一版自动进 revisions */ |
| 1233 | reviseMapping: ( |
| 1234 | kbId: string, |
| 1235 | mappingId: string, |
| 1236 | body: { |
| 1237 | table_name?: string | null; |
| 1238 | expr?: string | null; |
| 1239 | sql?: string | null; |
| 1240 | unit?: string | null; |
| 1241 | summary?: string | null; |
| 1242 | derived: boolean; |
| 1243 | }, |
| 1244 | ) => |
| 1245 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/mappings/${mappingId}`, { |
| 1246 | method: "PATCH", |
| 1247 | body: JSON.stringify(body), |
| 1248 | }), |
| 1249 | mappingRevisions: (kbId: string, mappingId: string) => |
| 1250 | request<{ revisions: MappingRevision[] }>( |
| 1251 | `/api/v1/kbs/${kbId}/mappings/${mappingId}/revisions`, |
| 1252 | ), |
| 1253 | kbDataSources: (kbId: string) => |
| 1254 | request<{ data_sources: DataSourceView[] }>( |
| 1255 | `/api/v1/kbs/${kbId}/data-sources`, |
| 1256 | ), |
| 1257 | kbDataSourcesAvailable: (kbId: string) => |
| 1258 | request<{ data_sources: DataSourceView[] }>( |
| 1259 | `/api/v1/kbs/${kbId}/data-sources/available`, |
| 1260 | ), |
| 1261 | /** 挂载。**`schema_error` 非空时挂载仍然成了**——源是真挂上的,只是它的 |
| 1262 | * 库表结构没摄进来,问数看不见有哪些表。同一件事会进告警中心 */ |
| 1263 | mountDataSource: (kbId: string, dsId: string) => |
| 1264 | request<{ |
| 1265 | ok: boolean; |
| 1266 | schema_tables: number; |
| 1267 | schema_error?: string | null; |
| 1268 | }>(`/api/v1/kbs/${kbId}/data-sources/${dsId}`, { |
| 1269 | method: "PUT", |
| 1270 | }), |
| 1271 | unmountDataSource: (kbId: string, dsId: string) => |
| 1272 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/data-sources/${dsId}`, { |
| 1273 | method: "DELETE", |
| 1274 | }), |
| 1275 | exploreMappings: (kbId: string) => |
| 1276 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/data-sources/explore`, { |
| 1277 | method: "POST", |
| 1278 | }), |
| 1279 | syncDataSourceSchema: (kbId: string, dsId: string) => |
| 1280 | request<{ ok: boolean; schema_tables: number }>( |
| 1281 | `/api/v1/kbs/${kbId}/data-sources/${dsId}/sync-schema`, |
| 1282 | { method: "POST" }, |
| 1283 | ), |
| 1284 | |
| 1285 | /** 文库一页。**服务端筛选与分页**——从前一次取回整库、前端切片, |
| 1286 | * 而客户端筛选只筛得到已经拿下来的那些 */ |
| 1287 | documents: ( |
| 1288 | kbId: string, |
| 1289 | opts: { |
| 1290 | source?: string; |
| 1291 | q?: string; |
| 1292 | graph?: string; |
| 1293 | limit: number; |
| 1294 | offset: number; |
| 1295 | }, |
| 1296 | ) => { |
| 1297 | const p = new URLSearchParams({ |
| 1298 | limit: String(opts.limit), |
| 1299 | offset: String(opts.offset), |
| 1300 | }); |
| 1301 | if (opts.source) p.set("source", opts.source); |
| 1302 | if (opts.q) p.set("q", opts.q); |
| 1303 | if (opts.graph) p.set("graph", opts.graph); |
| 1304 | return request<{ |
| 1305 | docs: Doc[]; |
| 1306 | total: number; |
| 1307 | /** 下面三个**只按来源作用域算**,不受名字/状态筛选影响—— |
| 1308 | * 它们是批量按钮的作用范围 */ |
| 1309 | ready: number; |
| 1310 | extracting: number; |
| 1311 | failed: number; |
| 1312 | }>(`/api/v1/kbs/${kbId}/documents?${p}`); |
| 1313 | }, |
| 1314 | /** 一键重试这个作用域里全部抽取失败的文档 */ |
| 1315 | retryFailedDocs: (kbId: string, source?: string) => |
| 1316 | request<{ queued: number; found: number }>( |
| 1317 | `/api/v1/kbs/${kbId}/documents/retry-failed${source ? `?source=${source}` : ""}`, |
| 1318 | { method: "POST" }, |
| 1319 | ), |
| 1320 | /** 整库一次取回:行数按 (文档 × 原因 × 对象) 聚合后很小,避免逐行发请求 */ |
| 1321 | extractionDrops: (kbId: string) => |
| 1322 | request<{ drops: ExtractionDrop[] }>( |
| 1323 | `/api/v1/kbs/${kbId}/extraction-drops`, |
| 1324 | ), |
| 1325 | upload: (kbId: string, files: File[], sourceId?: string) => { |
| 1326 | const form = new FormData(); |
| 1327 | for (const f of files) form.append("files", f, f.name); |
| 1328 | const qs = sourceId ? `?source=${sourceId}` : ""; |
| 1329 | return request<{ created: Doc[]; skipped: unknown[] }>( |
| 1330 | `/api/v1/kbs/${kbId}/documents${qs}`, |
| 1331 | { method: "POST", body: form }, |
| 1332 | ); |
| 1333 | }, |
| 1334 | deleteDocument: (id: string) => |
| 1335 | request<{ ok: boolean }>(`/api/v1/documents/${id}`, { method: "DELETE" }), |
| 1336 | |
| 1337 | search: (kbId: string, q: string) => |
| 1338 | request<{ results: SearchResult[] }>(`/api/v1/kbs/${kbId}/search`, { |
| 1339 | method: "POST", |
| 1340 | body: JSON.stringify({ q }), |
| 1341 | }), |
| 1342 | |
| 1343 | settings: (workspaceId: string) => |
| 1344 | request<LlmSettingsView>(`/api/v1/workspaces/${workspaceId}/settings`), |
| 1345 | saveSettings: (workspaceId: string, body: Record<string, unknown>) => |
| 1346 | request<{ ok: boolean }>(`/api/v1/workspaces/${workspaceId}/settings`, { |
| 1347 | method: "PUT", |
| 1348 | body: JSON.stringify(body), |
| 1349 | }), |
| 1350 | graphOverview: (kbId: string, limit?: number) => |
| 1351 | request<{ |
| 1352 | nodes: GraphNode[]; |
| 1353 | edges: GraphEdge[]; |
| 1354 | /** 库里一共有多少。**与 nodes.length 不是一回事**——画布只画度数最高的 |
| 1355 | * 那一批,把上限当成规模显示是这个接口从前最误导人的地方 */ |
| 1356 | total_nodes?: number; |
| 1357 | total_edges?: number; |
| 1358 | }>(`/api/v1/kbs/${kbId}/graph/overview${limit ? `?limit=${limit}` : ""}`), |
| 1359 | /** 邻域视图**没有总数**:它本来就只是一小片,说「共 325 个」没有意义。 |
| 1360 | * 两个字段声明成可选,好让调用方与总览共用一个类型 */ |
| 1361 | graphNeighborhood: (kbId: string, entityId: string) => |
| 1362 | request<{ |
| 1363 | nodes: GraphNode[]; |
| 1364 | edges: GraphEdge[]; |
| 1365 | total_nodes?: number; |
| 1366 | total_edges?: number; |
| 1367 | }>(`/api/v1/kbs/${kbId}/graph/neighborhood?entity=${entityId}&hops=2`), |
| 1368 | /** 按名字找实体。**一并回总数**——「宁分勿合」会造出一堆同名, |
| 1369 | * 固定十条时想找的那个可能根本不在这十条里 */ |
| 1370 | searchEntities: (kbId: string, q: string, limit = 10) => |
| 1371 | request<{ entities: GraphNode[]; total: number }>( |
| 1372 | `/api/v1/kbs/${kbId}/entities?q=${encodeURIComponent(q)}&limit=${limit}`, |
| 1373 | ), |
| 1374 | entityDetail: (kbId: string, entityId: string) => |
| 1375 | request<{ |
| 1376 | entity: GraphNode; |
| 1377 | facts: EntityFact[]; |
| 1378 | /** 推出来的那些**单独一个键**,不掺进 facts:混在同一个列表里, |
| 1379 | * 用户看不出「文档里写的」和「引擎推的」的区别 */ |
| 1380 | derived: DerivedFact[]; |
| 1381 | /** 没落地的派生(0017 §3):连 `derived_facts` 都不在,所以也单独一个键 */ |
| 1382 | blocked: BlockedDerivation[]; |
| 1383 | /** 同名的其他实体。**打开面板就给**——合并入口要长在能看见同名的地方, |
| 1384 | * 而不是藏在「改一次名」之后 */ |
| 1385 | same_name: GraphNode[]; |
| 1386 | }>(`/api/v1/kbs/${kbId}/entities/${entityId}`), |
| 1387 | /** 认知变更历史(记录时间轴):服务端分页 */ |
| 1388 | /** 人工修正实体的类型或名字。同名不拦——返回的 same_name 供界面提示是否合并。 */ |
| 1389 | updateEntity: ( |
| 1390 | kbId: string, |
| 1391 | entityId: string, |
| 1392 | body: { type_id?: string; canonical_name?: string }, |
| 1393 | ) => |
| 1394 | request<{ entity: GraphNode; same_name: GraphNode[] }>( |
| 1395 | `/api/v1/kbs/${kbId}/entities/${entityId}`, |
| 1396 | { method: "PATCH", body: JSON.stringify(body) }, |
| 1397 | ), |
| 1398 | |
| 1399 | entityHistory: (kbId: string, entityId: string, page: number, per = 30) => |
| 1400 | request<{ events: EntityHistoryEvent[]; total: number }>( |
| 1401 | `/api/v1/kbs/${kbId}/entities/${entityId}/history?page=${page}&per=${per}`, |
| 1402 | ), |
| 1403 | factEvidence: (kbId: string, factId: string) => |
| 1404 | request<{ evidence: Evidence[] }>( |
| 1405 | `/api/v1/kbs/${kbId}/facts/${factId}/evidence`, |
| 1406 | ), |
| 1407 | /** 一条派生事实的证明(0002 R2):前提按推导顺序,每条带证据,一路到原句。 |
| 1408 | * 派生已失效时回 null——不是错误 */ |
| 1409 | derivedProof: (kbId: string, derivedId: string) => |
| 1410 | request<{ proof: Proof | null }>( |
| 1411 | `/api/v1/kbs/${kbId}/derived/${derivedId}/proof`, |
| 1412 | ), |
| 1413 | /** 没落地的派生的证明链(0017 §3):前提在违规的 path 里 */ |
| 1414 | blockedProof: (kbId: string, violationId: string) => |
| 1415 | request<{ steps: ProofStep[] | null }>( |
| 1416 | `/api/v1/kbs/${kbId}/violations/${violationId}/proof`, |
| 1417 | ), |
| 1418 | documentDetail: (id: string) => |
| 1419 | request<{ document: Doc; chunks: ChunkFull[] }>(`/api/v1/documents/${id}`), |
| 1420 | extractDocument: (id: string) => |
| 1421 | request<{ job_id: number }>(`/api/v1/documents/${id}/extract`, { |
| 1422 | method: "POST", |
| 1423 | }), |
| 1424 | reprocessDocument: (id: string) => |
| 1425 | request<{ job_id: number }>(`/api/v1/documents/${id}/reprocess`, { |
| 1426 | method: "POST", |
| 1427 | }), |
| 1428 | /** 来源级全量重抽(增量语义:既有决策保留) */ |
| 1429 | reExtractSource: (kbId: string, sourceId: string) => |
| 1430 | request<{ queued: number }>( |
| 1431 | `/api/v1/kbs/${kbId}/sources/${sourceId}/re-extract`, |
| 1432 | { |
| 1433 | method: "POST", |
| 1434 | }, |
| 1435 | ), |
| 1436 | /** 图谱重建(清算语义:清空图层后全量重抽;KB admin) */ |
| 1437 | rebuildGraph: (kbId: string) => |
| 1438 | request<{ |
| 1439 | entities_removed: number; |
| 1440 | facts_removed: number; |
| 1441 | queued: number; |
| 1442 | }>(`/api/v1/kbs/${kbId}/graph/rebuild`, { method: "POST" }), |
| 1443 | |
| 1444 | ontology: (kbId: string) => |
| 1445 | request<{ |
| 1446 | entity_types: EntityTypeView[]; |
| 1447 | relation_types: RelationTypeView[]; |
| 1448 | misses: OntologyMiss[]; |
| 1449 | /** 已忽略的,连同它此后继续累积的计数。抑制照旧,只是看得见 */ |
| 1450 | dismissed_misses: OntologyMiss[]; |
| 1451 | }>(`/api/v1/kbs/${kbId}/ontology`), |
| 1452 | createEntityType: (kbId: string, body: Record<string, unknown>) => |
| 1453 | request<{ id: string }>(`/api/v1/kbs/${kbId}/ontology/entity-types`, { |
| 1454 | method: "POST", |
| 1455 | body: JSON.stringify(body), |
| 1456 | }), |
| 1457 | updateEntityType: (kbId: string, id: string, body: Record<string, unknown>) => |
| 1458 | request<{ ok: boolean }>( |
| 1459 | `/api/v1/kbs/${kbId}/ontology/entity-types/${id}`, |
| 1460 | { |
| 1461 | method: "PATCH", |
| 1462 | body: JSON.stringify(body), |
| 1463 | }, |
| 1464 | ), |
| 1465 | deleteEntityType: (kbId: string, id: string) => |
| 1466 | request<{ ok: boolean }>( |
| 1467 | `/api/v1/kbs/${kbId}/ontology/entity-types/${id}`, |
| 1468 | { |
| 1469 | method: "DELETE", |
| 1470 | }, |
| 1471 | ), |
| 1472 | typeEntities: (kbId: string, typeId: string, page: number, per = 12) => |
| 1473 | request<{ |
| 1474 | entities: { id: string; name: string; fact_count: number }[]; |
| 1475 | total: number; |
| 1476 | }>( |
| 1477 | `/api/v1/kbs/${kbId}/ontology/entity-types/${typeId}/entities?page=${page}&per=${per}`, |
| 1478 | ), |
| 1479 | createRelationType: (kbId: string, body: Record<string, unknown>) => |
| 1480 | request<{ id: string }>(`/api/v1/kbs/${kbId}/ontology/relation-types`, { |
| 1481 | method: "POST", |
| 1482 | body: JSON.stringify(body), |
| 1483 | }), |
| 1484 | updateRelationType: ( |
| 1485 | kbId: string, |
| 1486 | id: string, |
| 1487 | body: Record<string, unknown>, |
| 1488 | ) => |
| 1489 | request<{ ok: boolean }>( |
| 1490 | `/api/v1/kbs/${kbId}/ontology/relation-types/${id}`, |
| 1491 | { |
| 1492 | method: "PATCH", |
| 1493 | body: JSON.stringify(body), |
| 1494 | }, |
| 1495 | ), |
| 1496 | deleteRelationType: (kbId: string, id: string) => |
| 1497 | request<{ ok: boolean }>( |
| 1498 | `/api/v1/kbs/${kbId}/ontology/relation-types/${id}`, |
| 1499 | { |
| 1500 | method: "DELETE", |
| 1501 | }, |
| 1502 | ), |
| 1503 | /** 上次算出来、还没人表态的提案(0049)。刷新页面靠它,不必重跑模型 */ |
| 1504 | storedProposals: (kbId: string) => |
| 1505 | request<OntologyProposals>(`/api/v1/kbs/${kbId}/ontology/proposals`), |
| 1506 | /** 一条提案有人表态了。改状态不删行——拒绝留痕,下一轮 Suggest 不再刷回待看 */ |
| 1507 | decideProposal: ( |
| 1508 | kbId: string, |
| 1509 | section: string, |
| 1510 | key: string, |
| 1511 | status: "adopted" | "rejected", |
| 1512 | ) => |
| 1513 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/proposals`, { |
| 1514 | method: "POST", |
| 1515 | body: JSON.stringify({ section, key, status }), |
| 1516 | }), |
| 1517 | dismissMiss: (kbId: string, kind: string, key: string) => |
| 1518 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/dismiss`, { |
| 1519 | method: "POST", |
| 1520 | body: JSON.stringify({ kind, key }), |
| 1521 | }), |
| 1522 | restoreMiss: (kbId: string, kind: string, key: string) => |
| 1523 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/restore`, { |
| 1524 | method: "POST", |
| 1525 | body: JSON.stringify({ kind, key }), |
| 1526 | }), |
| 1527 | /** reason 只给人看,而人就在这次请求的另一端——所以语言由调用方说, |
| 1528 | 不是后端的设置(docs/decisions/0004)。description 的语言跟知识库走,服务端自己知道 */ |
| 1529 | suggestOntology: (kbId: string) => |
| 1530 | request<OntologyProposals>(`/api/v1/kbs/${kbId}/ontology/suggest`, { |
| 1531 | method: "POST", |
| 1532 | body: JSON.stringify({ locale: lang }), |
| 1533 | }), |
| 1534 | |
| 1535 | /** 上传本体文件只算出计划,一个字节都不写库 */ |
| 1536 | previewOntologyImport: (kbId: string, file: File) => { |
| 1537 | const form = new FormData(); |
| 1538 | form.append("file", file, file.name); |
| 1539 | return request<{ filename: string; plan: ImportPlan }>( |
| 1540 | `/api/v1/kbs/${kbId}/ontology/imports/preview`, |
| 1541 | { method: "POST", body: form }, |
| 1542 | ); |
| 1543 | }, |
| 1544 | /** 执行刚看过的那个计划——服务端重新算一遍,两条路径共用同一段代码 */ |
| 1545 | applyOntologyImport: (kbId: string, file: File) => { |
| 1546 | const form = new FormData(); |
| 1547 | form.append("file", file, file.name); |
| 1548 | return request<{ import_id: string; plan: ImportPlan }>( |
| 1549 | `/api/v1/kbs/${kbId}/ontology/imports`, |
| 1550 | { method: "POST", body: form }, |
| 1551 | ); |
| 1552 | }, |
| 1553 | ontologyImports: (kbId: string) => |
| 1554 | request<{ imports: OntologyImportView[] }>( |
| 1555 | `/api/v1/kbs/${kbId}/ontology/imports`, |
| 1556 | ), |
| 1557 | |
| 1558 | proposedPredicates: (kbId: string) => |
| 1559 | request<{ forms: ProposedPredicate[] }>( |
| 1560 | `/api/v1/kbs/${kbId}/ontology/proposed-predicates`, |
| 1561 | ), |
| 1562 | /** 最近一次自动扩本体做了什么,以及还能不能撤销(撤干净了返回 null) */ |
| 1563 | lastAutoExtension: (kbId: string) => |
| 1564 | request<{ |
| 1565 | run: { |
| 1566 | at: string; |
| 1567 | relations: string[] | null; |
| 1568 | classes: string[] | null; |
| 1569 | facts_remapped: number | null; |
| 1570 | batches: string[]; |
| 1571 | } | null; |
| 1572 | }>(`/api/v1/kbs/${kbId}/ontology/auto-extension`), |
| 1573 | /** 建关系 **并**把等着它的无谓词事实认过去——后半句才是收益 */ |
| 1574 | adoptPredicate: ( |
| 1575 | kbId: string, |
| 1576 | body: { |
| 1577 | key: string; |
| 1578 | /** true = key 指的是已有的关系/属性,只改写事实,不建新类型 */ |
| 1579 | existing?: boolean; |
| 1580 | /** attribute 走另一条改写路径:值要按 datatype 换算 */ |
| 1581 | kind?: "relation" | "attribute"; |
| 1582 | datatype?: string; |
| 1583 | unit?: string; |
| 1584 | label?: string; |
| 1585 | temporal?: string; |
| 1586 | functional?: boolean; |
| 1587 | description?: string; |
| 1588 | forms: string[]; |
| 1589 | }, |
| 1590 | ) => |
| 1591 | request<{ |
| 1592 | id: string; |
| 1593 | remapped: number; |
| 1594 | batch: string; |
| 1595 | /** 值换不动那个 datatype、因而没被改写的条数。改写了 3 条丢下 2 条, |
| 1596 | 只报前半句就是报喜不报忧 */ |
| 1597 | unconvertible?: number; |
| 1598 | }>(`/api/v1/kbs/${kbId}/ontology/adopt-predicate`, { |
| 1599 | method: "POST", |
| 1600 | body: JSON.stringify(body), |
| 1601 | }), |
| 1602 | /** 撤销一次采纳:新写的行作废、旧行复活。关系类型留着 */ |
| 1603 | unadoptPredicate: (kbId: string, batchId: string) => |
| 1604 | request<{ reverted: number }>( |
| 1605 | `/api/v1/kbs/${kbId}/ontology/adopt-predicate/${batchId}`, |
| 1606 | { method: "DELETE" }, |
| 1607 | ), |
| 1608 | |
| 1609 | sources: (kbId: string) => |
| 1610 | request<{ sources: SourceView[] }>(`/api/v1/kbs/${kbId}/sources`), |
| 1611 | createSource: (kbId: string, body: Record<string, unknown>) => |
| 1612 | request<{ source: SourceView; ingest_token?: string | null }>( |
| 1613 | `/api/v1/kbs/${kbId}/sources`, |
| 1614 | { |
| 1615 | method: "POST", |
| 1616 | body: JSON.stringify(body), |
| 1617 | }, |
| 1618 | ), |
| 1619 | sourceToken: (kbId: string, sourceId: string) => |
| 1620 | request<{ ingest_token: string | null }>( |
| 1621 | `/api/v1/kbs/${kbId}/sources/${sourceId}/token`, |
| 1622 | ), |
| 1623 | rotateSourceToken: (kbId: string, sourceId: string) => |
| 1624 | request<{ ingest_token: string }>( |
| 1625 | `/api/v1/kbs/${kbId}/sources/${sourceId}/rotate-token`, |
| 1626 | { method: "POST" }, |
| 1627 | ), |
| 1628 | updateSource: ( |
| 1629 | kbId: string, |
| 1630 | sourceId: string, |
| 1631 | body: Record<string, unknown>, |
| 1632 | ) => |
| 1633 | request<{ source: SourceView }>(`/api/v1/kbs/${kbId}/sources/${sourceId}`, { |
| 1634 | method: "PATCH", |
| 1635 | body: JSON.stringify(body), |
| 1636 | }), |
| 1637 | deleteSource: (kbId: string, sourceId: string) => |
| 1638 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/sources/${sourceId}`, { |
| 1639 | method: "DELETE", |
| 1640 | }), |
| 1641 | cleanupMissing: (kbId: string, sourceId: string) => |
| 1642 | request<{ deleted: number }>( |
| 1643 | `/api/v1/kbs/${kbId}/sources/${sourceId}/missing/cleanup`, |
| 1644 | { |
| 1645 | method: "POST", |
| 1646 | }, |
| 1647 | ), |
| 1648 | syncSource: (kbId: string, sourceId: string) => |
| 1649 | request<{ queued: boolean }>( |
| 1650 | `/api/v1/kbs/${kbId}/sources/${sourceId}/sync`, |
| 1651 | { |
| 1652 | method: "POST", |
| 1653 | }, |
| 1654 | ), |
| 1655 | sourceRuns: (kbId: string, sourceId: string) => |
| 1656 | request<{ runs: SyncRun[] }>( |
| 1657 | `/api/v1/kbs/${kbId}/sources/${sourceId}/runs`, |
| 1658 | ), |
| 1659 | documentExtractions: (docId: string) => |
| 1660 | request<{ facts: ChunkFact[] }>(`/api/v1/documents/${docId}/extractions`), |
| 1661 | |
| 1662 | /** 审核队列的各档**真实条数**。与列表分开取——列表有一页的上限,数数没有。 |
| 1663 | * 从前徽标读的是数组长度,而接口固定只回 100 条,于是 164 条写成 100。 */ |
| 1664 | review: (kbId: string, queue: ReviewQueue, limit: number, offset: number) => |
| 1665 | request<{ |
| 1666 | counts: ReviewCounts; |
| 1667 | queue: ReviewQueue; |
| 1668 | /** 只有当前这一档的一页。类型按档不同,调用处按 queue 收窄 */ |
| 1669 | items: unknown[]; |
| 1670 | }>( |
| 1671 | `/api/v1/kbs/${kbId}/review?queue=${queue}&limit=${limit}&offset=${offset}`, |
| 1672 | ), |
| 1673 | closeFact: (kbId: string, factId: string, validTo: string) => |
| 1674 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/close`, { |
| 1675 | method: "POST", |
| 1676 | body: JSON.stringify({ valid_to: validTo }), |
| 1677 | }), |
| 1678 | resolveConflict: ( |
| 1679 | kbId: string, |
| 1680 | conflictId: string, |
| 1681 | body: { action: "close" | "keep" | "reject_new"; close_at?: string }, |
| 1682 | ) => |
| 1683 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conflicts/${conflictId}`, { |
| 1684 | method: "POST", |
| 1685 | body: JSON.stringify(body), |
| 1686 | }), |
| 1687 | /** 一句记忆抽出的全部待确认项(0015)。空数组 = 这句话没有等着人的东西了 */ |
| 1688 | pendingForChunk: (kbId: string, chunkId: string) => |
| 1689 | request<{ items: PendingFactItem[] }>( |
| 1690 | `/api/v1/kbs/${kbId}/review/pending?chunk_id=${chunkId}`, |
| 1691 | ), |
| 1692 | decidePending: (kbId: string, pendingId: string, action: "confirm" | "reject") => |
| 1693 | request<{ ok: boolean; fact_id?: string; created?: boolean; conflicts?: number }>( |
| 1694 | `/api/v1/kbs/${kbId}/review/pending/${pendingId}`, |
| 1695 | { method: "POST", body: JSON.stringify({ action }) }, |
| 1696 | ), |
| 1697 | decideReview: (kbId: string, reviewId: string, action: "merge" | "keep") => |
| 1698 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/review/${reviewId}`, { |
| 1699 | method: "POST", |
| 1700 | body: JSON.stringify({ action }), |
| 1701 | }), |
| 1702 | /** 对一条数据映射口径表态(0011)。改状态不删行——拒绝留痕,下一轮探索不再提议它 */ |
| 1703 | decideMapping: ( |
| 1704 | kbId: string, |
| 1705 | mappingId: string, |
| 1706 | status: "confirmed" | "rejected", |
| 1707 | ) => |
| 1708 | request<{ ok: boolean }>( |
| 1709 | `/api/v1/kbs/${kbId}/review/mappings/${mappingId}`, |
| 1710 | { |
| 1711 | method: "POST", |
| 1712 | body: JSON.stringify({ status }), |
| 1713 | }, |
| 1714 | ), |
| 1715 | /** 跑一遍一致性检查。同步返回——纯计算,没有模型调用也没有网络 */ |
| 1716 | runConsistencyCheck: (kbId: string) => |
| 1717 | request<{ |
| 1718 | edges: number; |
| 1719 | /** **零和零不一样**:没有公理时结论是「没有判据」,不是「未发现矛盾」 */ |
| 1720 | predicates_with_axioms: number; |
| 1721 | found: number; |
| 1722 | inserted: number; |
| 1723 | cleared: number; |
| 1724 | classes: number; |
| 1725 | /** 本体自己的矛盾**单独回**,不加进 found:两个数不是一类东西 */ |
| 1726 | defects_found: number; |
| 1727 | defects_new: number; |
| 1728 | }>(`/api/v1/kbs/${kbId}/consistency/check`, { method: "POST" }), |
| 1729 | /** 对一处本体缺陷表态。**两个出路**——它压根没看数据,没有「数据错了」这条 */ |
| 1730 | decideDefect: ( |
| 1731 | kbId: string, |
| 1732 | defectId: string, |
| 1733 | resolution: "fixed" | "accepted", |
| 1734 | ) => |
| 1735 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/review/defects/${defectId}`, { |
| 1736 | method: "POST", |
| 1737 | body: JSON.stringify({ resolution }), |
| 1738 | }), |
| 1739 | /** 跑一遍推理(R1)。开关关着时后端回 inference_off */ |
| 1740 | /** 类型消解:**只算不写**。回执里带着送去检索的画像——检索找不着时, |
| 1741 | * 第一个要看的就是「我们拿什么去找的」 */ |
| 1742 | /** 手动合并:把 source 并进 target。**方向要紧**——source 消失, |
| 1743 | * 它的事实搬到 target 上;合并可整体回滚(entity_merges 记着快照) */ |
| 1744 | mergeEntities: (kbId: string, source: string, target: string) => |
| 1745 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/entities/merge`, { |
| 1746 | method: "POST", |
| 1747 | body: JSON.stringify({ source, target }), |
| 1748 | }), |
| 1749 | typeResolutionPreview: (kbId: string) => |
| 1750 | request<{ items: TypeSuggestion[] }>( |
| 1751 | `/api/v1/kbs/${kbId}/ontology/type-resolution/preview`, |
| 1752 | { method: "POST" }, |
| 1753 | ), |
| 1754 | /** 跑一遍并落库。三档分开回:自动改的、留给人的、说「都不是」的 */ |
| 1755 | typeResolutionApply: (kbId: string) => |
| 1756 | request<ResolutionOutcome>(`/api/v1/kbs/${kbId}/ontology/type-resolution`, { |
| 1757 | method: "POST", |
| 1758 | }), |
| 1759 | /** 认可一个「粗类 → 细类」的配对,并把带上的实体改过去。 |
| 1760 | * **认可的是类对,改的是实体**——认可一次,之后同一对不再进人工 */ |
| 1761 | approveRefinement: ( |
| 1762 | kbId: string, |
| 1763 | body: { from_type_id: string; to_type_id: string; entity_ids: string[] }, |
| 1764 | ) => |
| 1765 | request<{ retyped: number }>( |
| 1766 | `/api/v1/kbs/${kbId}/ontology/type-resolution/approve`, |
| 1767 | { method: "POST", body: JSON.stringify(body) }, |
| 1768 | ), |
| 1769 | /** 撤销一整批:把那批实体放回原来的类 */ |
| 1770 | typeResolutionUndo: (kbId: string, batchId: string) => |
| 1771 | request<{ reverted: number }>( |
| 1772 | `/api/v1/kbs/${kbId}/ontology/type-resolution/${batchId}`, |
| 1773 | { method: "DELETE" }, |
| 1774 | ), |
| 1775 | runInference: (kbId: string) => |
| 1776 | request<{ |
| 1777 | /** 编译出来的规则条数。为零时是「没有规则」而不是「推不出东西」 */ |
| 1778 | rules: number; |
| 1779 | edges: number; |
| 1780 | derived: number; |
| 1781 | inserted: number; |
| 1782 | /** 前提没了、跟着作废的 */ |
| 1783 | invalidated: number; |
| 1784 | /** 撞上单谓词上限、没推完的谓词个数 */ |
| 1785 | capped: number; |
| 1786 | }>(`/api/v1/kbs/${kbId}/inference/run`, { method: "POST" }), |
| 1787 | /** 对一处公理违规表态。三个出路——第三个是这一档独有的:可能是定义错了 */ |
| 1788 | decideViolation: ( |
| 1789 | kbId: string, |
| 1790 | violationId: string, |
| 1791 | resolution: ViolationResolution, |
| 1792 | closeAt?: string, |
| 1793 | ) => |
| 1794 | request<{ ok: boolean }>( |
| 1795 | `/api/v1/kbs/${kbId}/review/violations/${violationId}`, |
| 1796 | { |
| 1797 | method: "POST", |
| 1798 | body: JSON.stringify({ resolution, close_at: closeAt ?? null }), |
| 1799 | }, |
| 1800 | ), |
| 1801 | confirmFact: (kbId: string, factId: string) => |
| 1802 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/confirm`, { |
| 1803 | method: "POST", |
| 1804 | }), |
| 1805 | rejectFact: (kbId: string, factId: string) => |
| 1806 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/reject`, { |
| 1807 | method: "POST", |
| 1808 | }), |
| 1809 | revertMerge: (kbId: string, mergeId: string) => |
| 1810 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/merges/${mergeId}/revert`, { |
| 1811 | method: "POST", |
| 1812 | }), |
| 1813 | reviewHistory: (kbId: string, page: number, per = 20) => |
| 1814 | request<{ events: ReviewHistoryEvent[]; total: number }>( |
| 1815 | `/api/v1/kbs/${kbId}/review/history?page=${page}&per=${per}`, |
| 1816 | ), |
| 1817 | |
| 1818 | members: (workspaceId: string) => |
| 1819 | request<Member[]>(`/api/v1/workspaces/${workspaceId}/members`), |
| 1820 | orgUsers: () => request<OrgUser[]>("/api/v1/users"), |
| 1821 | setMemberRole: (workspaceId: string, userId: string, role: string) => |
| 1822 | request<{ ok: boolean }>( |
| 1823 | `/api/v1/workspaces/${workspaceId}/members/${userId}`, |
| 1824 | { |
| 1825 | method: "PUT", |
| 1826 | body: JSON.stringify({ role }), |
| 1827 | }, |
| 1828 | ), |
| 1829 | removeMember: (workspaceId: string, userId: string) => |
| 1830 | request<{ ok: boolean }>( |
| 1831 | `/api/v1/workspaces/${workspaceId}/members/${userId}`, |
| 1832 | { |
| 1833 | method: "DELETE", |
| 1834 | }, |
| 1835 | ), |
| 1836 | |
| 1837 | testSettings: (workspaceId: string) => |
| 1838 | request<{ |
| 1839 | chat: { ok: boolean; reply?: string; error?: string }; |
| 1840 | embed: { ok: boolean; dim?: number; error?: string }; |
| 1841 | }>(`/api/v1/workspaces/${workspaceId}/settings/test`, { method: "POST" }), |
| 1842 | }; |
| 1843 | |
| 1844 | /** RAG 对话:SSE 流式。返回中止函数。 */ |
| 1845 | export const conversationsApi = { |
| 1846 | /** **可搜可翻页**:标题会重(同一个问题问两次就重了),而固定一百条之后的 |
| 1847 | * 会话界面上根本不存在。搜的是标题与消息正文两处——人记得住的往往是 |
| 1848 | * 问过的那句话,不是标题 */ |
| 1849 | list: (kbId: string, q = "", limit = 30, offset = 0) => { |
| 1850 | const p = new URLSearchParams({ |
| 1851 | limit: String(limit), |
| 1852 | offset: String(offset), |
| 1853 | }); |
| 1854 | if (q.trim()) p.set("q", q.trim()); |
| 1855 | return request<{ conversations: ConversationRow[]; total: number }>( |
| 1856 | `/api/v1/kbs/${kbId}/conversations?${p}`, |
| 1857 | ); |
| 1858 | }, |
| 1859 | /** 改标题。标题本来是从第一句话自动取的,而一段对话跑偏是常态 */ |
| 1860 | rename: (kbId: string, id: string, title: string) => |
| 1861 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conversations/${id}`, { |
| 1862 | method: "PATCH", |
| 1863 | body: JSON.stringify({ title }), |
| 1864 | }), |
| 1865 | detail: (kbId: string, id: string) => |
| 1866 | request<{ messages: ConversationMessage[] }>( |
| 1867 | `/api/v1/kbs/${kbId}/conversations/${id}`, |
| 1868 | ), |
| 1869 | remove: (kbId: string, id: string) => |
| 1870 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conversations/${id}`, { |
| 1871 | method: "DELETE", |
| 1872 | }), |
| 1873 | }; |
| 1874 | |
| 1875 | export interface ChatHandlers { |
| 1876 | onConversation: (id: string) => void; |
| 1877 | onSources: (s: Source[]) => void; |
| 1878 | onStep: (s: ChatStep) => void; |
| 1879 | onDelta: (text: string) => void; |
| 1880 | onDone: () => void; |
| 1881 | onError: (message: string) => void; |
| 1882 | /** 接上一个已经在跑的回答:这是它此刻的样子,**覆盖,不是追加** */ |
| 1883 | onSnapshot?: (s: { content: string; steps: ChatStep[]; sources: Source[] }) => void; |
| 1884 | /** 这个会话没有在跑的生成——最常见的答案,不是错误 */ |
| 1885 | onIdle?: () => void; |
| 1886 | } |
| 1887 | |
| 1888 | /** 接上一个正在生成的回答(刷新页面之后走这里)。 |
| 1889 | * |
| 1890 | * 与 `streamChat` 读的是同一条事件流,只是多了开头那份快照。 */ |
| 1891 | export function reattachChat( |
| 1892 | kbId: string, |
| 1893 | conversationId: string, |
| 1894 | handlers: ChatHandlers, |
| 1895 | ): () => void { |
| 1896 | return consumeChatStream( |
| 1897 | (signal) => |
| 1898 | fetch(`/api/v1/kbs/${kbId}/conversations/${conversationId}/stream`, { |
| 1899 | credentials: "include", |
| 1900 | signal, |
| 1901 | }), |
| 1902 | handlers, |
| 1903 | ); |
| 1904 | } |
| 1905 | |
| 1906 | export function streamChat( |
| 1907 | kbId: string, |
| 1908 | body: { conversation_id?: string; message: string }, |
| 1909 | handlers: ChatHandlers, |
| 1910 | ): () => void { |
| 1911 | return consumeChatStream( |
| 1912 | (signal) => |
| 1913 | fetch(`/api/v1/kbs/${kbId}/chat`, { |
| 1914 | method: "POST", |
| 1915 | credentials: "include", |
| 1916 | headers: { "Content-Type": "application/json" }, |
| 1917 | body: JSON.stringify(body), |
| 1918 | signal, |
| 1919 | }), |
| 1920 | handlers, |
| 1921 | ); |
| 1922 | } |
| 1923 | |
| 1924 | /** SSE 的读法只有一份:发起请求的方式不同,收下来之后的处理完全一样。 */ |
| 1925 | function consumeChatStream( |
| 1926 | open: (signal: AbortSignal) => Promise<Response>, |
| 1927 | handlers: ChatHandlers, |
| 1928 | ): () => void { |
| 1929 | const controller = new AbortController(); |
| 1930 | (async () => { |
| 1931 | try { |
| 1932 | const res = await open(controller.signal); |
| 1933 | if (!res.ok || !res.body) { |
| 1934 | let message = res.statusText; |
| 1935 | try { |
| 1936 | const body = (await res.json()) as { error?: string }; |
| 1937 | if (body.error) message = body.error; |
| 1938 | } catch { |
| 1939 | /* ignore */ |
| 1940 | } |
| 1941 | handlers.onError(message); |
| 1942 | return; |
| 1943 | } |
| 1944 | const reader = res.body.getReader(); |
| 1945 | const decoder = new TextDecoder(); |
| 1946 | let buf = ""; |
| 1947 | for (;;) { |
| 1948 | const { done, value } = await reader.read(); |
| 1949 | if (done) break; |
| 1950 | buf += decoder.decode(value, { stream: true }); |
| 1951 | let idx: number; |
| 1952 | while ((idx = buf.indexOf("\n\n")) >= 0) { |
| 1953 | const frame = buf.slice(0, idx); |
| 1954 | buf = buf.slice(idx + 2); |
| 1955 | let event = "message"; |
| 1956 | let data = ""; |
| 1957 | for (const line of frame.split("\n")) { |
| 1958 | if (line.startsWith("event:")) event = line.slice(6).trim(); |
| 1959 | else if (line.startsWith("data:")) data += line.slice(5).trim(); |
| 1960 | } |
| 1961 | if (event === "conversation") |
| 1962 | handlers.onConversation((JSON.parse(data) as { id: string }).id); |
| 1963 | else if (event === "sources") |
| 1964 | handlers.onSources(JSON.parse(data || "[]")); |
| 1965 | else if (event === "step") |
| 1966 | handlers.onStep(JSON.parse(data) as ChatStep); |
| 1967 | else if (event === "delta") |
| 1968 | handlers.onDelta((JSON.parse(data) as { text: string }).text); |
| 1969 | else if (event === "snapshot") handlers.onSnapshot?.(JSON.parse(data)); |
| 1970 | else if (event === "idle") handlers.onIdle?.(); |
| 1971 | else if (event === "done") handlers.onDone(); |
| 1972 | else if (event === "error") handlers.onError(data); |
| 1973 | } |
| 1974 | } |
| 1975 | handlers.onDone(); |
| 1976 | } catch (e) { |
| 1977 | if (!controller.signal.aborted) handlers.onError(String(e)); |
| 1978 | } |
| 1979 | })(); |
| 1980 | return () => controller.abort(); |
| 1981 | } |