| 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 | export interface Workspace { |
| 61 | id: string; |
| 62 | org_id: string; |
| 63 | name: string; |
| 64 | created_at: string; |
| 65 | } |
| 66 | |
| 67 | /** 建库时可选的预置本体包(`GET /ontology-packs`)。 */ |
| 68 | export type OntologyPack = { |
| 69 | id: string; |
| 70 | name: string; |
| 71 | summary: string; |
| 72 | classes: number; |
| 73 | properties: number; |
| 74 | }; |
| 75 | |
| 76 | export interface Kb { |
| 77 | id: string; |
| 78 | workspace_id: string; |
| 79 | name: string; |
| 80 | kind: string; |
| 81 | description: string | null; |
| 82 | visibility: "open" | "restricted"; |
| 83 | /** 部署的公共默认空间(第一个建的库):永远 open、不可删除 */ |
| 84 | is_default: boolean; |
| 85 | /** 抽取遇到本体外的说法时,是否允许系统自动补进本体并改写等它的事实。 |
| 86 | 关掉不影响"留意":未匹配统计照常累积可见,只是变成你点一下的提案 */ |
| 87 | auto_extend_ontology: boolean; |
| 88 | /** 内置本体按哪种语言播种、新描述写哪种语言。**跟语料走,不跟界面走** |
| 89 | (界面语言在客户端,见 docs/decisions/0004) */ |
| 90 | ontology_lang: "en" | "zh"; |
| 91 | /** 调用者在本库的角色(仅详情接口返回):前端据此门控破坏性入口 */ |
| 92 | my_role?: "viewer" | "editor" | "admin" | "owner" | null; |
| 93 | } |
| 94 | |
| 95 | /** 账户层"我的知识库"行:库 + 我的角色 + 加入信息 + 概览统计。 */ |
| 96 | export interface MyKb { |
| 97 | kb: Kb; |
| 98 | my_role: "viewer" | "editor" | "admin" | "owner" | null; |
| 99 | joined_at: string | null; |
| 100 | added_by_name: string | null; |
| 101 | doc_count: number; |
| 102 | member_count: number; |
| 103 | } |
| 104 | |
| 105 | /** 审计事件(纯审计展示)。 */ |
| 106 | export interface AuditEvent { |
| 107 | id: string; |
| 108 | action: string; |
| 109 | target_kind: string; |
| 110 | target_id: string | null; |
| 111 | detail: Record<string, unknown>; |
| 112 | actor_name: string | null; |
| 113 | created_at: string; |
| 114 | } |
| 115 | |
| 116 | export interface KbMember { |
| 117 | user_id: string; |
| 118 | email: string; |
| 119 | display_name: string; |
| 120 | role: "viewer" | "editor" | "admin"; |
| 121 | } |
| 122 | |
| 123 | export interface Doc { |
| 124 | id: string; |
| 125 | kb_id: string; |
| 126 | source_id: string | null; |
| 127 | filename: string; |
| 128 | mime: string; |
| 129 | size_bytes: number; |
| 130 | status: string; |
| 131 | graph_status: string; |
| 132 | /** 摄入管道的失败原因 */ |
| 133 | error: string | null; |
| 134 | /** 图谱抽取管道的失败原因(与 error 分列:两条管道各存各的) */ |
| 135 | graph_error: string | null; |
| 136 | chunk_count: number; |
| 137 | tags: string[]; |
| 138 | missing_since: string | null; |
| 139 | created_at: string; |
| 140 | } |
| 141 | |
| 142 | /** 一类抽取丢弃在一篇文档里的聚合:事实抽出来了,却没能落地。 */ |
| 143 | export interface ExtractionDrop { |
| 144 | document_id: string; |
| 145 | /** 稳定的原因码,前端按它查文案(attr_domain_mismatch / low_confidence / ...) */ |
| 146 | reason: string; |
| 147 | /** 该原因下的具体对象(属性 key、谓词名、"salary@organization") */ |
| 148 | detail: string; |
| 149 | count: number; |
| 150 | example: string | null; |
| 151 | } |
| 152 | |
| 153 | export interface SourceView { |
| 154 | id: string; |
| 155 | kind: "folder" | "url" | "rss" | "api" | "custom" | "memory" | "upload"; |
| 156 | name: string; |
| 157 | config: { urls?: string[]; feed_url?: string; endpoint?: string } | null; |
| 158 | icon: string | null; |
| 159 | sync_interval_minutes: number | null; |
| 160 | sync_cron: string | null; |
| 161 | last_sync_at: string | null; |
| 162 | last_sync_status: "never" | "queued" | "running" | "ok" | "failed"; |
| 163 | last_sync_error: string | null; |
| 164 | last_sync_added: number; |
| 165 | doc_count: number; |
| 166 | missing_count: number; |
| 167 | } |
| 168 | |
| 169 | export interface SearchResult { |
| 170 | id: string; |
| 171 | document_id: string; |
| 172 | seq: number; |
| 173 | text: string; |
| 174 | filename: string; |
| 175 | } |
| 176 | |
| 177 | export interface LlmSettingsView { |
| 178 | chat_base_url?: string | null; |
| 179 | chat_model?: string | null; |
| 180 | has_chat_key?: boolean; |
| 181 | embed_base_url?: string | null; |
| 182 | embed_model?: string | null; |
| 183 | embed_dim?: number | null; |
| 184 | has_embed_key?: boolean; |
| 185 | } |
| 186 | |
| 187 | export interface Member { |
| 188 | user_id: string; |
| 189 | email: string; |
| 190 | display_name: string; |
| 191 | role: string; |
| 192 | is_admin: boolean; |
| 193 | } |
| 194 | |
| 195 | export interface OrgUser { |
| 196 | id: string; |
| 197 | email: string; |
| 198 | display_name: string; |
| 199 | is_admin: boolean; |
| 200 | } |
| 201 | |
| 202 | /** 问数数据源(凭据不下发,只有 host:port/db 摘要)。 */ |
| 203 | export interface DataSourceView { |
| 204 | id: string; |
| 205 | name: string; |
| 206 | engine: string; |
| 207 | summary: string; |
| 208 | created_at: string; |
| 209 | last_test_at: string | null; |
| 210 | last_test_ok: boolean | null; |
| 211 | } |
| 212 | |
| 213 | export interface GraphNode { |
| 214 | id: string; |
| 215 | name: string; |
| 216 | // 没判出类型时为 null(0009) |
| 217 | type_key: string | null; |
| 218 | type_label: string | null; |
| 219 | color: string; |
| 220 | shape: "circle" | "square"; |
| 221 | degree: number; |
| 222 | disambiguator: string | null; |
| 223 | } |
| 224 | |
| 225 | export interface SyncRun { |
| 226 | id: string; |
| 227 | started_at: string; |
| 228 | finished_at: string | null; |
| 229 | status: "running" | "ok" | "failed"; |
| 230 | created_docs: number; |
| 231 | updated_docs: number; |
| 232 | error: string | null; |
| 233 | } |
| 234 | |
| 235 | /** 分块的抽取产物(文档查看器右栏)。 */ |
| 236 | export interface ChunkFact { |
| 237 | chunk_id: string; |
| 238 | fact_id: string; |
| 239 | subject_id: string; |
| 240 | subject: string; |
| 241 | predicate: string; |
| 242 | object_id: string | null; |
| 243 | object: string | null; |
| 244 | valid_from: string | null; |
| 245 | valid_to: string | null; |
| 246 | confidence: number; |
| 247 | } |
| 248 | |
| 249 | export interface ReviewSide { |
| 250 | id: string; |
| 251 | name: string; |
| 252 | // 没判出类型时为 null(0009) |
| 253 | type_label: string | null; |
| 254 | color: string; |
| 255 | disambiguator: string | null; |
| 256 | degree: number; |
| 257 | top_facts: string[]; |
| 258 | } |
| 259 | |
| 260 | export interface ReviewItem { |
| 261 | id: string; |
| 262 | score: number; |
| 263 | reason: string | null; |
| 264 | stage: "adjudicating" | "human"; |
| 265 | created_at: string; |
| 266 | left: ReviewSide; |
| 267 | right: ReviewSide; |
| 268 | } |
| 269 | |
| 270 | export interface FactReviewItem { |
| 271 | id: string; |
| 272 | subject_name: string; |
| 273 | predicate_label: string; |
| 274 | object_name: string | null; |
| 275 | valid_from: string | null; |
| 276 | valid_to: string | null; |
| 277 | confidence: number; |
| 278 | evidence_count: number; |
| 279 | quote: string | null; |
| 280 | } |
| 281 | |
| 282 | /** 时态冲突(自动闭合拿不准的那些):旧事实 vs 新事实。 */ |
| 283 | export interface ConflictItem { |
| 284 | id: string; |
| 285 | reason: "no_time" | "simultaneous" | "low_confidence"; |
| 286 | created_at: string; |
| 287 | predicate_label: string; |
| 288 | old_fact_id: string; |
| 289 | old_subject: string; |
| 290 | old_object: string | null; |
| 291 | old_valid_from: string | null; |
| 292 | new_fact_id: string; |
| 293 | new_subject: string; |
| 294 | new_object: string | null; |
| 295 | new_valid_from: string | null; |
| 296 | new_confidence: number; |
| 297 | } |
| 298 | |
| 299 | /** 决策台账事件(audit_events 的 review 域切片):detail 是决策时的自包含快照。 */ |
| 300 | export interface ReviewHistoryEvent { |
| 301 | id: string; |
| 302 | action: string; |
| 303 | target_kind: string; |
| 304 | target_id: string | null; |
| 305 | detail: Record<string, unknown>; |
| 306 | /** null = 系统(AI 裁决器) */ |
| 307 | actor_name: string | null; |
| 308 | created_at: string; |
| 309 | } |
| 310 | |
| 311 | export interface MergeLog { |
| 312 | id: string; |
| 313 | source_name: string; |
| 314 | target_name: string; |
| 315 | merged_by_name: string | null; |
| 316 | reason: string | null; |
| 317 | created_at: string; |
| 318 | reverted_at: string | null; |
| 319 | } |
| 320 | |
| 321 | export interface GraphEdge { |
| 322 | id: string; |
| 323 | source: string; |
| 324 | target: string; |
| 325 | predicate: string; |
| 326 | label: string; |
| 327 | valid_from: string | null; |
| 328 | valid_to: string | null; |
| 329 | confidence: number; |
| 330 | } |
| 331 | |
| 332 | export interface EntityFact { |
| 333 | id: string; |
| 334 | direction: "out" | "in"; |
| 335 | predicate_key: string; |
| 336 | predicate_label: string; |
| 337 | temporal: string; |
| 338 | other_id: string | null; |
| 339 | other_name: string | null; |
| 340 | /** 字面值宾语(属性事实/问数映射):{"value":…} 或 {"summary":…} */ |
| 341 | object_value: Record<string, unknown> | null; |
| 342 | valid_from: string | null; |
| 343 | valid_to: string | null; |
| 344 | valid_from_precision: string | null; |
| 345 | /** year | month | day,外加 unknown = 原文说它结束了但没说哪天 */ |
| 346 | valid_to_precision: string | null; |
| 347 | confidence: number; |
| 348 | evidence_count: number; |
| 349 | /** 证据全部停留在来源文档的旧版(未被现行内容确认;不代表事实失效) */ |
| 350 | stale: boolean; |
| 351 | /** 修正行:区间闭合来自引擎对账/人工裁决而非抽取原文 */ |
| 352 | corrected: boolean; |
| 353 | /** 证据集合里最新的文档时间(开放事实的"最后确认时间") */ |
| 354 | last_evidence_time: string | null; |
| 355 | } |
| 356 | |
| 357 | /** 实体的一次认知变更(记录时间轴上的事件,与 EntityFact 的有效时间轴正交)。 |
| 358 | * |
| 359 | * 不是每个事件都来自一条事实:retyped / retype_reverted 来自改类账本, |
| 360 | * 没有谓词、没有对方、没有方向。 */ |
| 361 | export interface EntityHistoryEvent { |
| 362 | /** 事实事件才有;改类事件为 null */ |
| 363 | fact_id: string | null; |
| 364 | /** 记录时刻:写入 = recorded_at,作废 = invalidated_at,改类 = 改类那一刻 */ |
| 365 | at: string; |
| 366 | kind: |
| 367 | | "asserted" |
| 368 | | "corrected" |
| 369 | | "rejected" |
| 370 | | "merged" |
| 371 | | "retyped" |
| 372 | | "retype_reverted"; |
| 373 | direction: "out" | "in" | null; |
| 374 | predicate_label: string | null; |
| 375 | other_name: string | null; |
| 376 | object_value: Record<string, unknown> | null; |
| 377 | valid_from: string | null; |
| 378 | valid_to: string | null; |
| 379 | valid_from_precision: string | null; |
| 380 | /** year | month | day,外加 unknown = 原文说它结束了但没说哪天 */ |
| 381 | valid_to_precision: string | null; |
| 382 | confidence: number | null; |
| 383 | /** null = 引擎自动(抽取写入 / 时态对账闭合 / 高置信自动改类) */ |
| 384 | actor_name: string | null; |
| 385 | action: string | null; |
| 386 | document_id: string | null; |
| 387 | filename: string | null; |
| 388 | quote: string | null; |
| 389 | /** 改类事件的两端。起点为 null = 从「未分类」改过来(0009 之后最常见的一种) */ |
| 390 | from_type_label: string | null; |
| 391 | to_type_label: string | null; |
| 392 | } |
| 393 | |
| 394 | export interface Evidence { |
| 395 | /** 模型在这一块里实际用的谓词说法。词表外谓词降级成 related_to 后,原意只剩这里 */ |
| 396 | proposed_predicate: string | null; |
| 397 | quote: string | null; |
| 398 | chunk_id: string; |
| 399 | document_id: string; |
| 400 | filename: string; |
| 401 | seq: number; |
| 402 | /** 证据出自文档的第几版 */ |
| 403 | doc_version: number; |
| 404 | /** 文档已有更新版本(证据停留在旧版;不代表事实失效) */ |
| 405 | stale: boolean; |
| 406 | } |
| 407 | |
| 408 | export interface ChunkFull { |
| 409 | id: string; |
| 410 | seq: number; |
| 411 | text: string; |
| 412 | } |
| 413 | |
| 414 | export interface EntityTypeView { |
| 415 | id: string; |
| 416 | key: string; |
| 417 | label: string; |
| 418 | color: string; |
| 419 | shape: "circle" | "square"; |
| 420 | builtin: boolean; |
| 421 | /** 全部父类(subClassOf 可以有多个) */ |
| 422 | parents: string[]; |
| 423 | /** 左栏画树时挂在哪一支下。不参与语义,只管展示 */ |
| 424 | primary_parent: string | null; |
| 425 | description: string; |
| 426 | usage: number; |
| 427 | } |
| 428 | |
| 429 | export interface RelationTypeView { |
| 430 | id: string; |
| 431 | key: string; |
| 432 | label: string; |
| 433 | temporal: string; |
| 434 | functional: boolean; |
| 435 | inverse_functional: boolean; |
| 436 | builtin: boolean; |
| 437 | description: string; |
| 438 | /** relation(宾语是实体)| attribute(宾语是字面值) */ |
| 439 | kind: "relation" | "attribute"; |
| 440 | /** 可以当主语的类。attribute 至少一个;relation 留空 = 不限 */ |
| 441 | domains: string[]; |
| 442 | /** 可以当宾语的类。只对 relation 有意义——attribute 的值域是 datatype */ |
| 443 | ranges: string[]; |
| 444 | datatype: "text" | "number" | "date" | "bool" | null; |
| 445 | unit: string | null; |
| 446 | usage: number; |
| 447 | } |
| 448 | |
| 449 | export interface OntologyMiss { |
| 450 | kind: "entity_type" | "relation_type"; |
| 451 | key: string; |
| 452 | example: string | null; |
| 453 | count: number; |
| 454 | } |
| 455 | |
| 456 | /** `description` 与 `reason` 不是一回事:description 逐字进抽取提示词,是模型判断 |
| 457 | "什么算这个类"的唯一依据;reason 只是给人看的"为什么该加"。喂错了这个类会成为 |
| 458 | 下一个倾倒场——实测 technology 就是这么来的。 */ |
| 459 | export interface OntologyProposals { |
| 460 | entity_types: { |
| 461 | key: string; |
| 462 | label: string; |
| 463 | description?: string; |
| 464 | reason?: string; |
| 465 | }[]; |
| 466 | relation_types: { |
| 467 | key: string; |
| 468 | label: string; |
| 469 | temporal?: string; |
| 470 | functional?: boolean; |
| 471 | description?: string; |
| 472 | reason?: string; |
| 473 | /** 这条关系归并了哪些表层说法。有它才谈得上把等待的事实改写过去 */ |
| 474 | forms?: string[]; |
| 475 | }[]; |
| 476 | /** |
| 477 | * 宾语是字面值的说法("成立日期 = 2015")。 |
| 478 | * |
| 479 | * 跟 relation_types 分开是因为它们要的东西不一样:属性有 datatype, |
| 480 | * 而把它当关系建出来,那个值就会变成一个假实体。domain 不在这里—— |
| 481 | * 服务端从事实的主语类型里取,猜错会让整条被丢弃 |
| 482 | */ |
| 483 | attribute_types?: { |
| 484 | key: string; |
| 485 | label: string; |
| 486 | datatype?: string; |
| 487 | unit?: string; |
| 488 | description?: string; |
| 489 | reason?: string; |
| 490 | forms?: string[]; |
| 491 | }[]; |
| 492 | /** |
| 493 | * 本体里**已经有**这个意思,只需把说法挂过去。 |
| 494 | * |
| 495 | * 跟 relation_types 的区别是不建东西:同一个意思长出第二个 key, |
| 496 | * 这批事实就永久分在两处,谁也认不出它们本是一回事。 |
| 497 | */ |
| 498 | map_to?: { |
| 499 | key: string; |
| 500 | /** 服务端标的:目标落在关系还是属性上。两条改写路径不一样, |
| 501 | 而模型只答得出一个 key,看不出它在哪一档 */ |
| 502 | kind?: string; |
| 503 | forms?: string[]; |
| 504 | reason?: string; |
| 505 | }[]; |
| 506 | } |
| 507 | |
| 508 | /** 原文说过、本体没有、事实降级成了 related_to 的谓词。 */ |
| 509 | export interface ProposedPredicate { |
| 510 | form: string; |
| 511 | fact_count: number; |
| 512 | example: string | null; |
| 513 | } |
| 514 | |
| 515 | /** 一个类/属性在这次导入里的去向。key_taken = key 被另一个 IRI 占着,报告但不动 */ |
| 516 | export interface PlannedItem { |
| 517 | iri: string; |
| 518 | key: string; |
| 519 | label: string; |
| 520 | has_description: boolean; |
| 521 | disposition: "create" | "update" | "key_taken"; |
| 522 | functional?: boolean; |
| 523 | conflict_with?: string | null; |
| 524 | } |
| 525 | |
| 526 | /** 预览与落库返回同一个计划:点确认之后发生的事就是刚看过的事 */ |
| 527 | export interface ImportPlan { |
| 528 | format: string; |
| 529 | triples: number; |
| 530 | classes: PlannedItem[]; |
| 531 | relations: PlannedItem[]; |
| 532 | attributes: PlannedItem[]; |
| 533 | /** 出现过但今天不消费的公理 → 次数。不是已跳过,是暂未投影 */ |
| 534 | unprojected: [string, number][]; |
| 535 | classes_without_description: number; |
| 536 | functional_relations: number; |
| 537 | } |
| 538 | |
| 539 | export interface OntologyImportView { |
| 540 | id: string; |
| 541 | filename: string; |
| 542 | format: string; |
| 543 | byte_size: number; |
| 544 | summary: Record<string, unknown>; |
| 545 | imported_by_name: string | null; |
| 546 | imported_at: string; |
| 547 | } |
| 548 | |
| 549 | export interface Source { |
| 550 | n: number; |
| 551 | /** 缺省 = 文档 chunk 引用;charter = 内置手册(跳 /docs/{slug}#{anchor}) */ |
| 552 | kind?: "charter"; |
| 553 | chunk_id?: string; |
| 554 | document_id?: string; |
| 555 | slug?: string; |
| 556 | anchor?: string; |
| 557 | heading?: string; |
| 558 | filename: string; |
| 559 | excerpt: string; |
| 560 | } |
| 561 | |
| 562 | /** Agentic 对话的行动轨迹(工具调用一步一条)。 */ |
| 563 | export interface ChatStep { |
| 564 | kind: "search" | "docs" | "entity" | "facts" | "changes" | "query" | "tool"; |
| 565 | label: string; |
| 566 | detail: string; |
| 567 | } |
| 568 | |
| 569 | /** 会话行(Chat 左栏列表)。 */ |
| 570 | export interface ConversationRow { |
| 571 | id: string; |
| 572 | title: string; |
| 573 | created_at: string; |
| 574 | updated_at: string; |
| 575 | message_count: number; |
| 576 | } |
| 577 | |
| 578 | /** 会话消息(含落库的行动轨迹与引用,历史回放用)。 */ |
| 579 | export interface ConversationMessage { |
| 580 | id: string; |
| 581 | role: "user" | "assistant"; |
| 582 | content: string; |
| 583 | steps: ChatStep[]; |
| 584 | sources: Source[]; |
| 585 | created_at: string; |
| 586 | } |
| 587 | |
| 588 | /** 告警中心的一组(0005):**连着的、同类的几次故障**。 |
| 589 | * |
| 590 | * 存储那边仍是一次故障一行,折叠在服务端读的时候做——这样翻页数的是组, |
| 591 | * 一段连续故障不会被页边界切断。 */ |
| 592 | export type AlertGroup = { |
| 593 | kb_id: string | null; |
| 594 | /** 系统级告警没有库名 */ |
| 595 | kb_name: string | null; |
| 596 | /** `source.sync_failed` / `llm.unreachable` —— 措辞在 i18n 里按这个查 */ |
| 597 | kind: string; |
| 598 | severity: "info" | "warning" | "error"; |
| 599 | /** 这一组几次 */ |
| 600 | count: number; |
| 601 | /** 其中我没读过的几次 */ |
| 602 | unread: number; |
| 603 | latest_at: string; |
| 604 | /** 跟 latest_at 一起圈出这一组,标已读时原样发回去 */ |
| 605 | earliest_at: string; |
| 606 | /** 明细,最多几条,新的在前 */ |
| 607 | lines: { name?: string; error?: string; job?: string }[]; |
| 608 | }; |
| 609 | |
| 610 | export const api = { |
| 611 | health: () => |
| 612 | request<{ status: string; name: string; version: string }>( |
| 613 | "/api/v1/health", |
| 614 | ), |
| 615 | me: () => request<User>("/api/v1/auth/me"), |
| 616 | alerts: (o: { q?: string; limit?: number; offset?: number }) => { |
| 617 | const p = new URLSearchParams(); |
| 618 | if (o.q?.trim()) p.set("q", o.q.trim()); |
| 619 | if (o.limit != null) p.set("limit", String(o.limit)); |
| 620 | if (o.offset) p.set("offset", String(o.offset)); |
| 621 | return request<{ items: AlertGroup[]; total: number }>( |
| 622 | `/api/v1/alerts?${p}`, |
| 623 | ); |
| 624 | }, |
| 625 | alertsUnread: () => request<{ unread: number }>("/api/v1/alerts/unread"), |
| 626 | alertReadGroup: (g: { |
| 627 | kb_id: string | null; |
| 628 | kind: string; |
| 629 | from: string; |
| 630 | to: string; |
| 631 | }) => |
| 632 | request<{ marked: number }>("/api/v1/alerts/read-group", { |
| 633 | method: "POST", |
| 634 | body: JSON.stringify(g), |
| 635 | }), |
| 636 | alertsReadAll: () => |
| 637 | request<{ ok: boolean }>("/api/v1/alerts/read-all", { method: "POST" }), |
| 638 | login: (email: string, password: string) => |
| 639 | request<{ user: User }>("/api/v1/auth/login", { |
| 640 | method: "POST", |
| 641 | body: JSON.stringify({ email, password }), |
| 642 | }), |
| 643 | register: (email: string, password: string, displayName: string) => |
| 644 | request<{ user: User; workspace: Workspace }>("/api/v1/auth/register", { |
| 645 | method: "POST", |
| 646 | body: JSON.stringify({ email, password, display_name: displayName }), |
| 647 | }), |
| 648 | logout: () => |
| 649 | request<{ ok: boolean }>("/api/v1/auth/logout", { method: "POST" }), |
| 650 | updateMe: (displayName: string) => |
| 651 | request<User>("/api/v1/auth/me", { |
| 652 | method: "PATCH", |
| 653 | body: JSON.stringify({ display_name: displayName }), |
| 654 | }), |
| 655 | changePassword: (currentPassword: string, newPassword: string) => |
| 656 | request<{ ok: boolean }>("/api/v1/auth/password", { |
| 657 | method: "POST", |
| 658 | body: JSON.stringify({ |
| 659 | current_password: currentPassword, |
| 660 | new_password: newPassword, |
| 661 | }), |
| 662 | }), |
| 663 | workspaces: () => request<Workspace[]>("/api/v1/workspaces"), |
| 664 | |
| 665 | kbs: (workspaceId: string) => |
| 666 | request<Kb[]>(`/api/v1/workspaces/${workspaceId}/kbs`), |
| 667 | kbAudit: (kbId: string) => |
| 668 | request<{ events: AuditEvent[] }>(`/api/v1/kbs/${kbId}/audit`), |
| 669 | myKbs: (workspaceId: string) => |
| 670 | request<{ kbs: MyKb[] }>(`/api/v1/workspaces/${workspaceId}/my-kbs`), |
| 671 | createKb: ( |
| 672 | workspaceId: string, |
| 673 | body: { |
| 674 | name: string; |
| 675 | description?: string | null; |
| 676 | visibility?: string; |
| 677 | /** 预置本体包 id,顺序有意义:第一个会认领同名的种子类 */ |
| 678 | ontology_packs?: string[]; |
| 679 | }, |
| 680 | ) => |
| 681 | request<Kb>(`/api/v1/workspaces/${workspaceId}/kbs`, { |
| 682 | method: "POST", |
| 683 | body: JSON.stringify(body), |
| 684 | }), |
| 685 | |
| 686 | ontologyPacks: () => |
| 687 | request<{ packs: OntologyPack[] }>("/api/v1/ontology-packs"), |
| 688 | |
| 689 | kbDetail: (kbId: string) => request<Kb>(`/api/v1/kbs/${kbId}`), |
| 690 | updateKb: (kbId: string, body: Record<string, unknown>) => |
| 691 | request<Kb>(`/api/v1/kbs/${kbId}`, { |
| 692 | method: "PATCH", |
| 693 | body: JSON.stringify(body), |
| 694 | }), |
| 695 | deleteKb: (kbId: string) => |
| 696 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}`, { method: "DELETE" }), |
| 697 | kbMembers: (kbId: string) => |
| 698 | request<{ members: KbMember[] }>(`/api/v1/kbs/${kbId}/members`), |
| 699 | setKbMember: (kbId: string, userId: string, role: string) => |
| 700 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/members/${userId}`, { |
| 701 | method: "PUT", |
| 702 | body: JSON.stringify({ role }), |
| 703 | }), |
| 704 | removeKbMember: (kbId: string, userId: string) => |
| 705 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/members/${userId}`, { |
| 706 | method: "DELETE", |
| 707 | }), |
| 708 | |
| 709 | adminDeployment: () => |
| 710 | request<{ |
| 711 | open_registration: boolean; |
| 712 | /** 外层兜底,防任务无限堆积;真正的节流是按模型的限额 */ |
| 713 | worker_concurrency: number; |
| 714 | default_model_concurrency: number; |
| 715 | /** 新建知识库的本体语言默认值。**不是界面语言**——那个在客户端 */ |
| 716 | default_ontology_lang: "en" | "zh"; |
| 717 | model_limits: { |
| 718 | base_url: string; |
| 719 | model: string; |
| 720 | max_concurrent: number; |
| 721 | }[]; |
| 722 | models_in_use: { base_url: string; model: string; kind: string }[]; |
| 723 | }>("/api/v1/admin/deployment"), |
| 724 | saveAdminDeployment: ( |
| 725 | openRegistration: boolean, |
| 726 | workerConcurrency?: number, |
| 727 | defaultModelConcurrency?: number, |
| 728 | modelLimit?: { |
| 729 | base_url: string; |
| 730 | model: string; |
| 731 | max_concurrent: number | null; |
| 732 | }, |
| 733 | defaultOntologyLang?: "en" | "zh", |
| 734 | ) => |
| 735 | request<{ ok: boolean }>("/api/v1/admin/deployment", { |
| 736 | method: "PUT", |
| 737 | body: JSON.stringify({ |
| 738 | open_registration: openRegistration, |
| 739 | ...(workerConcurrency !== undefined |
| 740 | ? { worker_concurrency: workerConcurrency } |
| 741 | : {}), |
| 742 | ...(defaultModelConcurrency !== undefined |
| 743 | ? { default_model_concurrency: defaultModelConcurrency } |
| 744 | : {}), |
| 745 | ...(modelLimit ? { model_limit: modelLimit } : {}), |
| 746 | ...(defaultOntologyLang |
| 747 | ? { default_ontology_lang: defaultOntologyLang } |
| 748 | : {}), |
| 749 | }), |
| 750 | }), |
| 751 | adminCreateUser: (body: { |
| 752 | email: string; |
| 753 | display_name: string; |
| 754 | password: string; |
| 755 | role: string; |
| 756 | }) => |
| 757 | request<{ user: User }>("/api/v1/admin/users", { |
| 758 | method: "POST", |
| 759 | body: JSON.stringify(body), |
| 760 | }), |
| 761 | |
| 762 | adminDataSources: () => |
| 763 | request<{ data_sources: DataSourceView[] }>("/api/v1/admin/data-sources"), |
| 764 | adminCreateDataSource: (body: { name: string; conn_string: string }) => |
| 765 | request<{ id: string }>("/api/v1/admin/data-sources", { |
| 766 | method: "POST", |
| 767 | body: JSON.stringify(body), |
| 768 | }), |
| 769 | adminDeleteDataSource: (id: string) => |
| 770 | request<{ ok: boolean }>(`/api/v1/admin/data-sources/${id}`, { |
| 771 | method: "DELETE", |
| 772 | }), |
| 773 | adminTestDataSource: (id: string) => |
| 774 | request<{ ok: boolean }>(`/api/v1/admin/data-sources/${id}/test`, { |
| 775 | method: "POST", |
| 776 | }), |
| 777 | kbDataSources: (kbId: string) => |
| 778 | request<{ data_sources: DataSourceView[] }>( |
| 779 | `/api/v1/kbs/${kbId}/data-sources`, |
| 780 | ), |
| 781 | kbDataSourcesAvailable: (kbId: string) => |
| 782 | request<{ data_sources: DataSourceView[] }>( |
| 783 | `/api/v1/kbs/${kbId}/data-sources/available`, |
| 784 | ), |
| 785 | mountDataSource: (kbId: string, dsId: string) => |
| 786 | request<{ ok: boolean; schema_tables: number }>( |
| 787 | `/api/v1/kbs/${kbId}/data-sources/${dsId}`, |
| 788 | { |
| 789 | method: "PUT", |
| 790 | }, |
| 791 | ), |
| 792 | unmountDataSource: (kbId: string, dsId: string) => |
| 793 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/data-sources/${dsId}`, { |
| 794 | method: "DELETE", |
| 795 | }), |
| 796 | exploreMappings: (kbId: string) => |
| 797 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/data-sources/explore`, { |
| 798 | method: "POST", |
| 799 | }), |
| 800 | syncDataSourceSchema: (kbId: string, dsId: string) => |
| 801 | request<{ ok: boolean; schema_tables: number }>( |
| 802 | `/api/v1/kbs/${kbId}/data-sources/${dsId}/sync-schema`, |
| 803 | { method: "POST" }, |
| 804 | ), |
| 805 | |
| 806 | documents: (kbId: string) => request<Doc[]>(`/api/v1/kbs/${kbId}/documents`), |
| 807 | /** 整库一次取回:行数按 (文档 × 原因 × 对象) 聚合后很小,避免逐行发请求 */ |
| 808 | extractionDrops: (kbId: string) => |
| 809 | request<{ drops: ExtractionDrop[] }>( |
| 810 | `/api/v1/kbs/${kbId}/extraction-drops`, |
| 811 | ), |
| 812 | upload: (kbId: string, files: File[], sourceId?: string) => { |
| 813 | const form = new FormData(); |
| 814 | for (const f of files) form.append("files", f, f.name); |
| 815 | const qs = sourceId ? `?source=${sourceId}` : ""; |
| 816 | return request<{ created: Doc[]; skipped: unknown[] }>( |
| 817 | `/api/v1/kbs/${kbId}/documents${qs}`, |
| 818 | { method: "POST", body: form }, |
| 819 | ); |
| 820 | }, |
| 821 | deleteDocument: (id: string) => |
| 822 | request<{ ok: boolean }>(`/api/v1/documents/${id}`, { method: "DELETE" }), |
| 823 | |
| 824 | search: (kbId: string, q: string) => |
| 825 | request<{ results: SearchResult[] }>(`/api/v1/kbs/${kbId}/search`, { |
| 826 | method: "POST", |
| 827 | body: JSON.stringify({ q }), |
| 828 | }), |
| 829 | |
| 830 | settings: (workspaceId: string) => |
| 831 | request<LlmSettingsView>(`/api/v1/workspaces/${workspaceId}/settings`), |
| 832 | saveSettings: (workspaceId: string, body: Record<string, unknown>) => |
| 833 | request<{ ok: boolean }>(`/api/v1/workspaces/${workspaceId}/settings`, { |
| 834 | method: "PUT", |
| 835 | body: JSON.stringify(body), |
| 836 | }), |
| 837 | graphOverview: (kbId: string) => |
| 838 | request<{ nodes: GraphNode[]; edges: GraphEdge[] }>( |
| 839 | `/api/v1/kbs/${kbId}/graph/overview`, |
| 840 | ), |
| 841 | graphNeighborhood: (kbId: string, entityId: string) => |
| 842 | request<{ nodes: GraphNode[]; edges: GraphEdge[] }>( |
| 843 | `/api/v1/kbs/${kbId}/graph/neighborhood?entity=${entityId}&hops=2`, |
| 844 | ), |
| 845 | searchEntities: (kbId: string, q: string) => |
| 846 | request<{ entities: GraphNode[] }>( |
| 847 | `/api/v1/kbs/${kbId}/entities?q=${encodeURIComponent(q)}`, |
| 848 | ), |
| 849 | entityDetail: (kbId: string, entityId: string) => |
| 850 | request<{ entity: GraphNode; facts: EntityFact[] }>( |
| 851 | `/api/v1/kbs/${kbId}/entities/${entityId}`, |
| 852 | ), |
| 853 | /** 认知变更历史(记录时间轴):服务端分页 */ |
| 854 | /** 人工修正实体的类型或名字。同名不拦——返回的 same_name 供界面提示是否合并。 */ |
| 855 | updateEntity: ( |
| 856 | kbId: string, |
| 857 | entityId: string, |
| 858 | body: { type_id?: string; canonical_name?: string }, |
| 859 | ) => |
| 860 | request<{ entity: GraphNode; same_name: GraphNode[] }>( |
| 861 | `/api/v1/kbs/${kbId}/entities/${entityId}`, |
| 862 | { method: "PATCH", body: JSON.stringify(body) }, |
| 863 | ), |
| 864 | |
| 865 | entityHistory: (kbId: string, entityId: string, page: number, per = 30) => |
| 866 | request<{ events: EntityHistoryEvent[]; total: number }>( |
| 867 | `/api/v1/kbs/${kbId}/entities/${entityId}/history?page=${page}&per=${per}`, |
| 868 | ), |
| 869 | factEvidence: (kbId: string, factId: string) => |
| 870 | request<{ evidence: Evidence[] }>( |
| 871 | `/api/v1/kbs/${kbId}/facts/${factId}/evidence`, |
| 872 | ), |
| 873 | documentDetail: (id: string) => |
| 874 | request<{ document: Doc; chunks: ChunkFull[] }>(`/api/v1/documents/${id}`), |
| 875 | extractDocument: (id: string) => |
| 876 | request<{ job_id: number }>(`/api/v1/documents/${id}/extract`, { |
| 877 | method: "POST", |
| 878 | }), |
| 879 | reprocessDocument: (id: string) => |
| 880 | request<{ job_id: number }>(`/api/v1/documents/${id}/reprocess`, { |
| 881 | method: "POST", |
| 882 | }), |
| 883 | /** 来源级全量重抽(增量语义:既有决策保留) */ |
| 884 | reExtractSource: (kbId: string, sourceId: string) => |
| 885 | request<{ queued: number }>( |
| 886 | `/api/v1/kbs/${kbId}/sources/${sourceId}/re-extract`, |
| 887 | { |
| 888 | method: "POST", |
| 889 | }, |
| 890 | ), |
| 891 | /** 图谱重建(清算语义:清空图层后全量重抽;KB admin) */ |
| 892 | rebuildGraph: (kbId: string) => |
| 893 | request<{ |
| 894 | entities_removed: number; |
| 895 | facts_removed: number; |
| 896 | queued: number; |
| 897 | }>(`/api/v1/kbs/${kbId}/graph/rebuild`, { method: "POST" }), |
| 898 | |
| 899 | ontology: (kbId: string) => |
| 900 | request<{ |
| 901 | entity_types: EntityTypeView[]; |
| 902 | relation_types: RelationTypeView[]; |
| 903 | misses: OntologyMiss[]; |
| 904 | /** 已忽略的,连同它此后继续累积的计数。抑制照旧,只是看得见 */ |
| 905 | dismissed_misses: OntologyMiss[]; |
| 906 | }>(`/api/v1/kbs/${kbId}/ontology`), |
| 907 | createEntityType: (kbId: string, body: Record<string, unknown>) => |
| 908 | request<{ id: string }>(`/api/v1/kbs/${kbId}/ontology/entity-types`, { |
| 909 | method: "POST", |
| 910 | body: JSON.stringify(body), |
| 911 | }), |
| 912 | updateEntityType: (kbId: string, id: string, body: Record<string, unknown>) => |
| 913 | request<{ ok: boolean }>( |
| 914 | `/api/v1/kbs/${kbId}/ontology/entity-types/${id}`, |
| 915 | { |
| 916 | method: "PATCH", |
| 917 | body: JSON.stringify(body), |
| 918 | }, |
| 919 | ), |
| 920 | deleteEntityType: (kbId: string, id: string) => |
| 921 | request<{ ok: boolean }>( |
| 922 | `/api/v1/kbs/${kbId}/ontology/entity-types/${id}`, |
| 923 | { |
| 924 | method: "DELETE", |
| 925 | }, |
| 926 | ), |
| 927 | typeEntities: (kbId: string, typeId: string, page: number, per = 12) => |
| 928 | request<{ |
| 929 | entities: { id: string; name: string; fact_count: number }[]; |
| 930 | total: number; |
| 931 | }>( |
| 932 | `/api/v1/kbs/${kbId}/ontology/entity-types/${typeId}/entities?page=${page}&per=${per}`, |
| 933 | ), |
| 934 | createRelationType: (kbId: string, body: Record<string, unknown>) => |
| 935 | request<{ id: string }>(`/api/v1/kbs/${kbId}/ontology/relation-types`, { |
| 936 | method: "POST", |
| 937 | body: JSON.stringify(body), |
| 938 | }), |
| 939 | updateRelationType: ( |
| 940 | kbId: string, |
| 941 | id: string, |
| 942 | body: Record<string, unknown>, |
| 943 | ) => |
| 944 | request<{ ok: boolean }>( |
| 945 | `/api/v1/kbs/${kbId}/ontology/relation-types/${id}`, |
| 946 | { |
| 947 | method: "PATCH", |
| 948 | body: JSON.stringify(body), |
| 949 | }, |
| 950 | ), |
| 951 | deleteRelationType: (kbId: string, id: string) => |
| 952 | request<{ ok: boolean }>( |
| 953 | `/api/v1/kbs/${kbId}/ontology/relation-types/${id}`, |
| 954 | { |
| 955 | method: "DELETE", |
| 956 | }, |
| 957 | ), |
| 958 | /** 上次算出来、还没人表态的提案(0049)。刷新页面靠它,不必重跑模型 */ |
| 959 | storedProposals: (kbId: string) => |
| 960 | request<OntologyProposals>(`/api/v1/kbs/${kbId}/ontology/proposals`), |
| 961 | /** 一条提案有人表态了。改状态不删行——拒绝留痕,下一轮 Suggest 不再刷回待看 */ |
| 962 | decideProposal: ( |
| 963 | kbId: string, |
| 964 | section: string, |
| 965 | key: string, |
| 966 | status: "adopted" | "rejected", |
| 967 | ) => |
| 968 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/proposals`, { |
| 969 | method: "POST", |
| 970 | body: JSON.stringify({ section, key, status }), |
| 971 | }), |
| 972 | dismissMiss: (kbId: string, kind: string, key: string) => |
| 973 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/dismiss`, { |
| 974 | method: "POST", |
| 975 | body: JSON.stringify({ kind, key }), |
| 976 | }), |
| 977 | restoreMiss: (kbId: string, kind: string, key: string) => |
| 978 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/ontology/misses/restore`, { |
| 979 | method: "POST", |
| 980 | body: JSON.stringify({ kind, key }), |
| 981 | }), |
| 982 | /** reason 只给人看,而人就在这次请求的另一端——所以语言由调用方说, |
| 983 | 不是后端的设置(docs/decisions/0004)。description 的语言跟知识库走,服务端自己知道 */ |
| 984 | suggestOntology: (kbId: string) => |
| 985 | request<OntologyProposals>(`/api/v1/kbs/${kbId}/ontology/suggest`, { |
| 986 | method: "POST", |
| 987 | body: JSON.stringify({ locale: lang }), |
| 988 | }), |
| 989 | |
| 990 | /** 上传本体文件只算出计划,一个字节都不写库 */ |
| 991 | previewOntologyImport: (kbId: string, file: File) => { |
| 992 | const form = new FormData(); |
| 993 | form.append("file", file, file.name); |
| 994 | return request<{ filename: string; plan: ImportPlan }>( |
| 995 | `/api/v1/kbs/${kbId}/ontology/imports/preview`, |
| 996 | { method: "POST", body: form }, |
| 997 | ); |
| 998 | }, |
| 999 | /** 执行刚看过的那个计划——服务端重新算一遍,两条路径共用同一段代码 */ |
| 1000 | applyOntologyImport: (kbId: string, file: File) => { |
| 1001 | const form = new FormData(); |
| 1002 | form.append("file", file, file.name); |
| 1003 | return request<{ import_id: string; plan: ImportPlan }>( |
| 1004 | `/api/v1/kbs/${kbId}/ontology/imports`, |
| 1005 | { method: "POST", body: form }, |
| 1006 | ); |
| 1007 | }, |
| 1008 | ontologyImports: (kbId: string) => |
| 1009 | request<{ imports: OntologyImportView[] }>( |
| 1010 | `/api/v1/kbs/${kbId}/ontology/imports`, |
| 1011 | ), |
| 1012 | |
| 1013 | proposedPredicates: (kbId: string) => |
| 1014 | request<{ forms: ProposedPredicate[] }>( |
| 1015 | `/api/v1/kbs/${kbId}/ontology/proposed-predicates`, |
| 1016 | ), |
| 1017 | /** 最近一次自动扩本体做了什么,以及还能不能撤销(撤干净了返回 null) */ |
| 1018 | lastAutoExtension: (kbId: string) => |
| 1019 | request<{ |
| 1020 | run: { |
| 1021 | at: string; |
| 1022 | relations: string[] | null; |
| 1023 | classes: string[] | null; |
| 1024 | facts_remapped: number | null; |
| 1025 | batches: string[]; |
| 1026 | } | null; |
| 1027 | }>(`/api/v1/kbs/${kbId}/ontology/auto-extension`), |
| 1028 | /** 建关系 **并**把等着它的 related_to 事实改写过去——后半句才是收益 */ |
| 1029 | adoptPredicate: ( |
| 1030 | kbId: string, |
| 1031 | body: { |
| 1032 | key: string; |
| 1033 | /** true = key 指的是已有的关系/属性,只改写事实,不建新类型 */ |
| 1034 | existing?: boolean; |
| 1035 | /** attribute 走另一条改写路径:值要按 datatype 换算 */ |
| 1036 | kind?: "relation" | "attribute"; |
| 1037 | datatype?: string; |
| 1038 | unit?: string; |
| 1039 | label?: string; |
| 1040 | temporal?: string; |
| 1041 | functional?: boolean; |
| 1042 | description?: string; |
| 1043 | forms: string[]; |
| 1044 | }, |
| 1045 | ) => |
| 1046 | request<{ |
| 1047 | id: string; |
| 1048 | remapped: number; |
| 1049 | batch: string; |
| 1050 | /** 值换不动那个 datatype、因而没被改写的条数。改写了 3 条丢下 2 条, |
| 1051 | 只报前半句就是报喜不报忧 */ |
| 1052 | unconvertible?: number; |
| 1053 | }>(`/api/v1/kbs/${kbId}/ontology/adopt-predicate`, |
| 1054 | { method: "POST", body: JSON.stringify(body) }, |
| 1055 | ), |
| 1056 | /** 撤销一次采纳:新写的行作废、旧行复活。关系类型留着 */ |
| 1057 | unadoptPredicate: (kbId: string, batchId: string) => |
| 1058 | request<{ reverted: number }>( |
| 1059 | `/api/v1/kbs/${kbId}/ontology/adopt-predicate/${batchId}`, |
| 1060 | { method: "DELETE" }, |
| 1061 | ), |
| 1062 | |
| 1063 | sources: (kbId: string) => |
| 1064 | request<{ sources: SourceView[] }>(`/api/v1/kbs/${kbId}/sources`), |
| 1065 | createSource: (kbId: string, body: Record<string, unknown>) => |
| 1066 | request<{ source: SourceView; ingest_token?: string | null }>( |
| 1067 | `/api/v1/kbs/${kbId}/sources`, |
| 1068 | { |
| 1069 | method: "POST", |
| 1070 | body: JSON.stringify(body), |
| 1071 | }, |
| 1072 | ), |
| 1073 | sourceToken: (kbId: string, sourceId: string) => |
| 1074 | request<{ ingest_token: string | null }>( |
| 1075 | `/api/v1/kbs/${kbId}/sources/${sourceId}/token`, |
| 1076 | ), |
| 1077 | rotateSourceToken: (kbId: string, sourceId: string) => |
| 1078 | request<{ ingest_token: string }>( |
| 1079 | `/api/v1/kbs/${kbId}/sources/${sourceId}/rotate-token`, |
| 1080 | { method: "POST" }, |
| 1081 | ), |
| 1082 | updateSource: ( |
| 1083 | kbId: string, |
| 1084 | sourceId: string, |
| 1085 | body: Record<string, unknown>, |
| 1086 | ) => |
| 1087 | request<{ source: SourceView }>(`/api/v1/kbs/${kbId}/sources/${sourceId}`, { |
| 1088 | method: "PATCH", |
| 1089 | body: JSON.stringify(body), |
| 1090 | }), |
| 1091 | deleteSource: (kbId: string, sourceId: string) => |
| 1092 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/sources/${sourceId}`, { |
| 1093 | method: "DELETE", |
| 1094 | }), |
| 1095 | cleanupMissing: (kbId: string, sourceId: string) => |
| 1096 | request<{ deleted: number }>( |
| 1097 | `/api/v1/kbs/${kbId}/sources/${sourceId}/missing/cleanup`, |
| 1098 | { |
| 1099 | method: "POST", |
| 1100 | }, |
| 1101 | ), |
| 1102 | syncSource: (kbId: string, sourceId: string) => |
| 1103 | request<{ queued: boolean }>( |
| 1104 | `/api/v1/kbs/${kbId}/sources/${sourceId}/sync`, |
| 1105 | { |
| 1106 | method: "POST", |
| 1107 | }, |
| 1108 | ), |
| 1109 | sourceRuns: (kbId: string, sourceId: string) => |
| 1110 | request<{ runs: SyncRun[] }>( |
| 1111 | `/api/v1/kbs/${kbId}/sources/${sourceId}/runs`, |
| 1112 | ), |
| 1113 | documentExtractions: (docId: string) => |
| 1114 | request<{ facts: ChunkFact[] }>(`/api/v1/documents/${docId}/extractions`), |
| 1115 | |
| 1116 | review: (kbId: string) => |
| 1117 | request<{ |
| 1118 | reviews: ReviewItem[]; |
| 1119 | facts: FactReviewItem[]; |
| 1120 | merges: MergeLog[]; |
| 1121 | conflicts: ConflictItem[]; |
| 1122 | unconfirmed: FactReviewItem[]; |
| 1123 | }>(`/api/v1/kbs/${kbId}/review`), |
| 1124 | closeFact: (kbId: string, factId: string, validTo: string) => |
| 1125 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/close`, { |
| 1126 | method: "POST", |
| 1127 | body: JSON.stringify({ valid_to: validTo }), |
| 1128 | }), |
| 1129 | resolveConflict: ( |
| 1130 | kbId: string, |
| 1131 | conflictId: string, |
| 1132 | body: { action: "close" | "keep" | "reject_new"; close_at?: string }, |
| 1133 | ) => |
| 1134 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conflicts/${conflictId}`, { |
| 1135 | method: "POST", |
| 1136 | body: JSON.stringify(body), |
| 1137 | }), |
| 1138 | decideReview: (kbId: string, reviewId: string, action: "merge" | "keep") => |
| 1139 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/review/${reviewId}`, { |
| 1140 | method: "POST", |
| 1141 | body: JSON.stringify({ action }), |
| 1142 | }), |
| 1143 | confirmFact: (kbId: string, factId: string) => |
| 1144 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/confirm`, { |
| 1145 | method: "POST", |
| 1146 | }), |
| 1147 | rejectFact: (kbId: string, factId: string) => |
| 1148 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/facts/${factId}/reject`, { |
| 1149 | method: "POST", |
| 1150 | }), |
| 1151 | revertMerge: (kbId: string, mergeId: string) => |
| 1152 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/merges/${mergeId}/revert`, { |
| 1153 | method: "POST", |
| 1154 | }), |
| 1155 | reviewHistory: (kbId: string, page: number, per = 20) => |
| 1156 | request<{ events: ReviewHistoryEvent[]; total: number }>( |
| 1157 | `/api/v1/kbs/${kbId}/review/history?page=${page}&per=${per}`, |
| 1158 | ), |
| 1159 | |
| 1160 | members: (workspaceId: string) => |
| 1161 | request<Member[]>(`/api/v1/workspaces/${workspaceId}/members`), |
| 1162 | orgUsers: () => request<OrgUser[]>("/api/v1/users"), |
| 1163 | setMemberRole: (workspaceId: string, userId: string, role: string) => |
| 1164 | request<{ ok: boolean }>( |
| 1165 | `/api/v1/workspaces/${workspaceId}/members/${userId}`, |
| 1166 | { |
| 1167 | method: "PUT", |
| 1168 | body: JSON.stringify({ role }), |
| 1169 | }, |
| 1170 | ), |
| 1171 | removeMember: (workspaceId: string, userId: string) => |
| 1172 | request<{ ok: boolean }>( |
| 1173 | `/api/v1/workspaces/${workspaceId}/members/${userId}`, |
| 1174 | { |
| 1175 | method: "DELETE", |
| 1176 | }, |
| 1177 | ), |
| 1178 | |
| 1179 | testSettings: (workspaceId: string) => |
| 1180 | request<{ |
| 1181 | chat: { ok: boolean; reply?: string; error?: string }; |
| 1182 | embed: { ok: boolean; dim?: number; error?: string }; |
| 1183 | }>(`/api/v1/workspaces/${workspaceId}/settings/test`, { method: "POST" }), |
| 1184 | }; |
| 1185 | |
| 1186 | /** RAG 对话:SSE 流式。返回中止函数。 */ |
| 1187 | export const conversationsApi = { |
| 1188 | list: (kbId: string) => |
| 1189 | request<{ conversations: ConversationRow[] }>( |
| 1190 | `/api/v1/kbs/${kbId}/conversations`, |
| 1191 | ), |
| 1192 | detail: (kbId: string, id: string) => |
| 1193 | request<{ messages: ConversationMessage[] }>( |
| 1194 | `/api/v1/kbs/${kbId}/conversations/${id}`, |
| 1195 | ), |
| 1196 | remove: (kbId: string, id: string) => |
| 1197 | request<{ ok: boolean }>(`/api/v1/kbs/${kbId}/conversations/${id}`, { |
| 1198 | method: "DELETE", |
| 1199 | }), |
| 1200 | }; |
| 1201 | |
| 1202 | export function streamChat( |
| 1203 | kbId: string, |
| 1204 | body: { conversation_id?: string; message: string }, |
| 1205 | handlers: { |
| 1206 | onConversation: (id: string) => void; |
| 1207 | onSources: (s: Source[]) => void; |
| 1208 | onStep: (s: ChatStep) => void; |
| 1209 | onDelta: (text: string) => void; |
| 1210 | onDone: () => void; |
| 1211 | onError: (message: string) => void; |
| 1212 | }, |
| 1213 | ): () => void { |
| 1214 | const controller = new AbortController(); |
| 1215 | (async () => { |
| 1216 | try { |
| 1217 | const res = await fetch(`/api/v1/kbs/${kbId}/chat`, { |
| 1218 | method: "POST", |
| 1219 | credentials: "include", |
| 1220 | headers: { "Content-Type": "application/json" }, |
| 1221 | body: JSON.stringify(body), |
| 1222 | signal: controller.signal, |
| 1223 | }); |
| 1224 | if (!res.ok || !res.body) { |
| 1225 | let message = res.statusText; |
| 1226 | try { |
| 1227 | const body = (await res.json()) as { error?: string }; |
| 1228 | if (body.error) message = body.error; |
| 1229 | } catch { |
| 1230 | /* ignore */ |
| 1231 | } |
| 1232 | handlers.onError(message); |
| 1233 | return; |
| 1234 | } |
| 1235 | const reader = res.body.getReader(); |
| 1236 | const decoder = new TextDecoder(); |
| 1237 | let buf = ""; |
| 1238 | for (;;) { |
| 1239 | const { done, value } = await reader.read(); |
| 1240 | if (done) break; |
| 1241 | buf += decoder.decode(value, { stream: true }); |
| 1242 | let idx: number; |
| 1243 | while ((idx = buf.indexOf("\n\n")) >= 0) { |
| 1244 | const frame = buf.slice(0, idx); |
| 1245 | buf = buf.slice(idx + 2); |
| 1246 | let event = "message"; |
| 1247 | let data = ""; |
| 1248 | for (const line of frame.split("\n")) { |
| 1249 | if (line.startsWith("event:")) event = line.slice(6).trim(); |
| 1250 | else if (line.startsWith("data:")) data += line.slice(5).trim(); |
| 1251 | } |
| 1252 | if (event === "conversation") |
| 1253 | handlers.onConversation((JSON.parse(data) as { id: string }).id); |
| 1254 | else if (event === "sources") |
| 1255 | handlers.onSources(JSON.parse(data || "[]")); |
| 1256 | else if (event === "step") |
| 1257 | handlers.onStep(JSON.parse(data) as ChatStep); |
| 1258 | else if (event === "delta") |
| 1259 | handlers.onDelta((JSON.parse(data) as { text: string }).text); |
| 1260 | else if (event === "done") handlers.onDone(); |
| 1261 | else if (event === "error") handlers.onError(data); |
| 1262 | } |
| 1263 | } |
| 1264 | handlers.onDone(); |
| 1265 | } catch (e) { |
| 1266 | if (!controller.signal.aborted) handlers.onError(String(e)); |
| 1267 | } |
| 1268 | })(); |
| 1269 | return () => controller.abort(); |
| 1270 | } |