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