| 1 | // 本体编辑器:master-detail 双栏(与 Library 的 SourcesRail 同构)。 |
| 2 | // 左栏 = filter + Classes/Properties 两小节 + 底部 Unmatched 入口; |
| 3 | // 右侧 = 选中项的表单 / 未匹配信号面板 / 概览。 |
| 4 | import { useEffect, useMemo, useRef, useState } from "react"; |
| 5 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 6 | import { Link } from "@tanstack/react-router"; |
| 7 | import { |
| 8 | ChevronRight, |
| 9 | Inbox, |
| 10 | Plus, |
| 11 | Search, |
| 12 | Upload, |
| 13 | Wand2, |
| 14 | } from "lucide-react"; |
| 15 | import { |
| 16 | api, |
| 17 | type EntityTypeView, |
| 18 | type ImportPlan, |
| 19 | type OntologyMiss, |
| 20 | type PlannedItem, |
| 21 | type OntologyProposals, |
| 22 | type ResolutionOutcome, |
| 23 | type TypeSuggestion, |
| 24 | type RelationTypeView, |
| 25 | } from "../api"; |
| 26 | import { S } from "../i18n"; |
| 27 | import { useKb } from "../kb"; |
| 28 | import { toast } from "../toast"; |
| 29 | import { |
| 30 | Button, |
| 31 | Chip, |
| 32 | ColorPicker, |
| 33 | colorForKey, |
| 34 | DangerConfirm, |
| 35 | Dropdown, |
| 36 | Input, |
| 37 | Loading, |
| 38 | MultiSearchSelect, |
| 39 | Pager, |
| 40 | PageTitle, |
| 41 | RAIL_CLS, |
| 42 | SearchSelect, |
| 43 | cn, |
| 44 | pageSlice, |
| 45 | } from "../ui"; |
| 46 | |
| 47 | /** 左栏行高(py-1.5 + 13px 文字 + space-y 间隙)与底部预留(新建行 + 分页器) */ |
| 48 | const RAIL_ROW_H = 34; |
| 49 | const RAIL_RESERVED = 80; |
| 50 | /** 兜底页行数(首帧未量到高度时用) */ |
| 51 | const RAIL_PAGE = 14; |
| 52 | /** 过滤模式两节混排时每节的行数 */ |
| 53 | const RAIL_PAGE_MIXED = 6; |
| 54 | |
| 55 | /** 右侧详情区当前展示什么 */ |
| 56 | type Sel = |
| 57 | | { kind: "class"; id: string } |
| 58 | | { kind: "relation"; id: string } |
| 59 | | { kind: "new-class"; parentId: string | null } |
| 60 | | { kind: "new-relation" } |
| 61 | | { kind: "misses" } |
| 62 | // 类型消解:把「大致对」的类换成更具体的那个 |
| 63 | | { kind: "refine" } |
| 64 | | { kind: "import" } |
| 65 | | null; |
| 66 | |
| 67 | export function Ontology() { |
| 68 | const { kb } = useKb(); |
| 69 | const queryClient = useQueryClient(); |
| 70 | const [sel, setSel] = useState<Sel>(null); |
| 71 | const [railTab, setRailTab] = useState<"classes" | "properties">("classes"); |
| 72 | const [filter, setFilter] = useState(""); |
| 73 | const [collapsed, setCollapsed] = useState<Set<string>>(new Set()); |
| 74 | // 每页行数按列表区实际高度动态算:窗口多高铺多满,不滚动也不留大空 |
| 75 | const listRef = useRef<HTMLDivElement>(null); |
| 76 | const [railRows, setRailRows] = useState(RAIL_PAGE); |
| 77 | useEffect(() => { |
| 78 | const el = listRef.current; |
| 79 | if (!el) return; |
| 80 | const ro = new ResizeObserver(() => { |
| 81 | setRailRows( |
| 82 | Math.max(5, Math.floor((el.clientHeight - RAIL_RESERVED) / RAIL_ROW_H)), |
| 83 | ); |
| 84 | }); |
| 85 | ro.observe(el); |
| 86 | return () => ro.disconnect(); |
| 87 | }, []); |
| 88 | |
| 89 | const data = useQuery({ |
| 90 | queryKey: ["ontology", kb?.id], |
| 91 | queryFn: () => api.ontology(kb!.id), |
| 92 | enabled: !!kb, |
| 93 | }); |
| 94 | |
| 95 | const refresh = () => |
| 96 | queryClient.invalidateQueries({ queryKey: ["ontology", kb?.id] }); |
| 97 | // 错误统一走全局 toast,不再用页面内嵌错误行 |
| 98 | const onError = (e: unknown) => toast.error((e as Error).message); |
| 99 | |
| 100 | if (!kb) return <Loading>{S.nav.loading}</Loading>; |
| 101 | if (data.isPending) return <Loading>{S.nav.loading}</Loading>; |
| 102 | if (data.isError) return <Loading>{(data.error as Error).message}</Loading>; |
| 103 | |
| 104 | const { entity_types, relation_types, misses, dismissed_misses } = data.data; |
| 105 | // 属性不进 Properties 列表:它们挂在类下,在类详情区编辑 |
| 106 | const relations = relation_types.filter((r) => r.kind !== "attribute"); |
| 107 | const selectedClass = |
| 108 | sel?.kind === "class" |
| 109 | ? (entity_types.find((t) => t.id === sel.id) ?? null) |
| 110 | : null; |
| 111 | const selectedProp = |
| 112 | sel?.kind === "relation" |
| 113 | ? (relation_types.find((r) => r.id === sel.id) ?? null) |
| 114 | : null; |
| 115 | |
| 116 | return ( |
| 117 | <div className="h-full flex"> |
| 118 | {/* 左栏:filter + 两小节 + Unmatched */} |
| 119 | <aside className={`${RAIL_CLS} flex flex-col`}> |
| 120 | <div className="px-3 pt-3 pb-2.5"> |
| 121 | <div className="relative"> |
| 122 | <Search |
| 123 | size={12} |
| 124 | className="absolute left-2.5 top-1/2 -translate-y-1/2 text-neutral-600" |
| 125 | /> |
| 126 | <input |
| 127 | className="input-dark w-full pl-7 pr-2 py-1.5 text-xs" |
| 128 | placeholder={S.ontology.filter} |
| 129 | value={filter} |
| 130 | onChange={(e) => setFilter(e.target.value)} |
| 131 | /> |
| 132 | </div> |
| 133 | </div> |
| 134 | {/* 分段切换:与登录页模式切换/日程选择器同一语汇(bg-white/5 容器 + 激活反白); |
| 135 | 过滤时列表例外:两节混排同时给出命中 */} |
| 136 | <div className="mx-3 mb-1 flex gap-1 rounded-lg bg-white/5 p-1"> |
| 137 | {( |
| 138 | [ |
| 139 | ["classes", S.ontology.tabClasses], |
| 140 | ["properties", S.ontology.tabProperties], |
| 141 | ] as const |
| 142 | ).map(([k, label]) => ( |
| 143 | <button |
| 144 | key={k} |
| 145 | onClick={() => setRailTab(k)} |
| 146 | className={cn( |
| 147 | "flex-1 rounded-md py-1 text-[12px] font-medium text-center transition-colors", |
| 148 | railTab === k |
| 149 | ? "bg-white/10 text-neutral-100" |
| 150 | : "text-neutral-500 hover:text-neutral-300", |
| 151 | )} |
| 152 | > |
| 153 | {label} |
| 154 | </button> |
| 155 | ))} |
| 156 | </div> |
| 157 | <div |
| 158 | ref={listRef} |
| 159 | className="flex-1 min-h-0 overflow-hidden px-2 pt-1.5 pb-2 flex flex-col" |
| 160 | > |
| 161 | {/* 新建行置顶:随当前段建类/建关系 */} |
| 162 | {!filter.trim() && ( |
| 163 | <button |
| 164 | onClick={() => |
| 165 | railTab === "classes" |
| 166 | ? setSel({ kind: "new-class", parentId: null }) |
| 167 | : setSel({ kind: "new-relation" }) |
| 168 | } |
| 169 | className="w-full flex items-center gap-1.5 rounded-lg px-2 py-2 mb-0.5 text-[13px] text-neutral-500 hover:bg-white/[0.05] hover:text-neutral-200 transition-colors" |
| 170 | > |
| 171 | <Plus size={13} /> |
| 172 | {railTab === "classes" |
| 173 | ? S.ontology.newClass |
| 174 | : S.ontology.newProperty} |
| 175 | </button> |
| 176 | )} |
| 177 | {filter.trim() ? ( |
| 178 | <> |
| 179 | <div className="px-2 pt-2 pb-1 text-[10px] font-medium uppercase tracking-[0.08em] text-neutral-600"> |
| 180 | {S.ontology.tabClasses} |
| 181 | </div> |
| 182 | <ClassTree |
| 183 | types={entity_types} |
| 184 | filter={filter} |
| 185 | collapsed={collapsed} |
| 186 | onToggle={() => {}} |
| 187 | selectedId={selectedClass?.id ?? null} |
| 188 | onSelect={(id) => setSel({ kind: "class", id })} |
| 189 | pageSize={RAIL_PAGE_MIXED} |
| 190 | /> |
| 191 | <div className="px-2 pt-3 pb-1 text-[10px] font-medium uppercase tracking-[0.08em] text-neutral-600"> |
| 192 | {S.ontology.tabProperties} |
| 193 | </div> |
| 194 | <PropertyList |
| 195 | relations={relations} |
| 196 | filter={filter} |
| 197 | selectedId={selectedProp?.id ?? null} |
| 198 | onSelect={(id) => setSel({ kind: "relation", id })} |
| 199 | pageSize={RAIL_PAGE_MIXED} |
| 200 | /> |
| 201 | </> |
| 202 | ) : railTab === "classes" ? ( |
| 203 | <ClassTree |
| 204 | types={entity_types} |
| 205 | filter={filter} |
| 206 | collapsed={collapsed} |
| 207 | onToggle={(id) => { |
| 208 | const next = new Set(collapsed); |
| 209 | if (next.has(id)) next.delete(id); |
| 210 | else next.add(id); |
| 211 | setCollapsed(next); |
| 212 | }} |
| 213 | selectedId={selectedClass?.id ?? null} |
| 214 | onSelect={(id) => setSel({ kind: "class", id })} |
| 215 | pageSize={railRows} |
| 216 | /> |
| 217 | ) : ( |
| 218 | <PropertyList |
| 219 | relations={relations} |
| 220 | filter={filter} |
| 221 | selectedId={selectedProp?.id ?? null} |
| 222 | onSelect={(id) => setSel({ kind: "relation", id })} |
| 223 | pageSize={railRows} |
| 224 | /> |
| 225 | )} |
| 226 | </div> |
| 227 | {/* 底部常驻:两个"关于本体"的入口——从外部拿一份本体,或看抽取顶回来的信号 */} |
| 228 | <button |
| 229 | onClick={() => setSel({ kind: "import" })} |
| 230 | className={cn( |
| 231 | "shrink-0 border-t border-white/10 px-4 py-2.5 flex items-center gap-2 text-[13px] transition-colors", |
| 232 | sel?.kind === "import" |
| 233 | ? "u-nav-active" |
| 234 | : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200", |
| 235 | )} |
| 236 | > |
| 237 | <Upload size={14} className="text-neutral-500" /> |
| 238 | <span>{S.ontology.importShort}</span> |
| 239 | </button> |
| 240 | {/* 类型消解:把「大致对」的类换成更具体的那个。**紧挨着未匹配**—— |
| 241 | 两者都是「本体与数据对不齐」的处置,只是方向相反:那个是本体缺东西, |
| 242 | 这个是本体有更好的选项没被用上 */} |
| 243 | <button |
| 244 | onClick={() => setSel({ kind: "refine" })} |
| 245 | className={cn( |
| 246 | "shrink-0 border-t border-white/10 px-4 py-2.5 flex items-center gap-2 text-[13px] transition-colors", |
| 247 | sel?.kind === "refine" |
| 248 | ? "u-nav-active" |
| 249 | : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200", |
| 250 | )} |
| 251 | > |
| 252 | <Wand2 size={14} className="text-neutral-500" /> |
| 253 | <span>{S.ontology.refineShort}</span> |
| 254 | </button> |
| 255 | {/* 底部常驻:抽取未匹配信号(有存量时带数量徽标) */} |
| 256 | <button |
| 257 | onClick={() => setSel({ kind: "misses" })} |
| 258 | className={cn( |
| 259 | "shrink-0 border-t border-white/10 px-4 py-2.5 flex items-center gap-2 text-[13px] transition-colors", |
| 260 | sel?.kind === "misses" |
| 261 | ? "u-nav-active" |
| 262 | : "text-neutral-400 hover:bg-white/[0.05] hover:text-neutral-200", |
| 263 | )} |
| 264 | > |
| 265 | <Inbox size={14} className="text-neutral-500" /> |
| 266 | <span>{S.ontology.missesShort}</span> |
| 267 | {misses.length > 0 && ( |
| 268 | <span className="ml-auto u-num text-[10.5px] text-neutral-500 bg-white/[0.08] rounded-full px-1.5 py-px"> |
| 269 | {misses.length} |
| 270 | </span> |
| 271 | )} |
| 272 | </button> |
| 273 | </aside> |
| 274 | |
| 275 | {/* 右侧:详情。选中类时表单 + 实例列表双栏铺开,提高宽屏利用率 */} |
| 276 | <div className="flex-1 min-w-0 overflow-y-auto u-scroll px-8 py-6"> |
| 277 | {/* 放宽到 6xl 供三列铺开;misses/关系/概览各自带 max-w-xl 内衬不受影响 */} |
| 278 | <div className="max-w-6xl"> |
| 279 | {sel?.kind === "import" ? ( |
| 280 | <div className="max-w-xl"> |
| 281 | <ImportPanel kbId={kb.id} onChanged={refresh} onError={onError} /> |
| 282 | </div> |
| 283 | ) : sel?.kind === "refine" ? ( |
| 284 | <div className="max-w-2xl"> |
| 285 | <RefinePanel kbId={kb.id} onChanged={refresh} onError={onError} /> |
| 286 | </div> |
| 287 | ) : sel?.kind === "misses" ? ( |
| 288 | <div className="max-w-xl"> |
| 289 | <MissesPanel |
| 290 | kbId={kb.id} |
| 291 | misses={misses} |
| 292 | dismissedMisses={dismissed_misses ?? []} |
| 293 | onChanged={refresh} |
| 294 | onError={onError} |
| 295 | /> |
| 296 | </div> |
| 297 | ) : sel?.kind === "new-class" || selectedClass ? ( |
| 298 | /* lg 两列(表单 | 属性+实例堆叠);xl 三列并排(包装器 xl:contents 解散入栅格) */ |
| 299 | <div className="grid gap-4 items-start lg:grid-cols-[minmax(0,24rem)_minmax(0,1fr)] xl:grid-cols-[minmax(0,22rem)_minmax(0,1fr)_minmax(0,1fr)]"> |
| 300 | <div className="glass rounded-xl p-4"> |
| 301 | <ClassForm |
| 302 | key={ |
| 303 | selectedClass?.id ?? |
| 304 | `new-${sel?.kind === "new-class" ? sel.parentId : "root"}` |
| 305 | } |
| 306 | kbId={kb.id} |
| 307 | existing={selectedClass} |
| 308 | parentId={ |
| 309 | sel?.kind === "new-class" |
| 310 | ? sel.parentId |
| 311 | : (selectedClass?.primary_parent ?? null) |
| 312 | } |
| 313 | allTypes={entity_types} |
| 314 | onNewSub={ |
| 315 | selectedClass |
| 316 | ? () => |
| 317 | setSel({ |
| 318 | kind: "new-class", |
| 319 | parentId: selectedClass.id, |
| 320 | }) |
| 321 | : undefined |
| 322 | } |
| 323 | onDone={(createdId) => { |
| 324 | // 新建成功即选中它:立刻能看到、能继续编辑 |
| 325 | if (sel?.kind === "new-class") |
| 326 | setSel( |
| 327 | createdId ? { kind: "class", id: createdId } : null, |
| 328 | ); |
| 329 | refresh(); |
| 330 | }} |
| 331 | onError={onError} |
| 332 | /> |
| 333 | </div> |
| 334 | {/* lg 右列堆叠属性+实例;xl 解散为两个独立栅格列 */} |
| 335 | {selectedClass && ( |
| 336 | <div className="grid gap-4 items-start xl:contents"> |
| 337 | <AttributesCard |
| 338 | kbId={kb.id} |
| 339 | type={selectedClass} |
| 340 | attributes={relation_types.filter( |
| 341 | (r) => |
| 342 | r.kind === "attribute" && |
| 343 | r.domains.includes(selectedClass.id), |
| 344 | )} |
| 345 | onChanged={refresh} |
| 346 | onError={onError} |
| 347 | /> |
| 348 | <InstancesCard kbId={kb.id} type={selectedClass} /> |
| 349 | </div> |
| 350 | )} |
| 351 | </div> |
| 352 | ) : sel?.kind === "new-relation" || selectedProp ? ( |
| 353 | <div className="glass rounded-xl p-4 max-w-xl"> |
| 354 | <PropertyForm |
| 355 | key={selectedProp?.id ?? "new"} |
| 356 | kbId={kb.id} |
| 357 | existing={selectedProp} |
| 358 | allTypes={entity_types} |
| 359 | allRelations={relations} |
| 360 | onDone={(createdId) => { |
| 361 | if (sel?.kind === "new-relation") |
| 362 | setSel( |
| 363 | createdId ? { kind: "relation", id: createdId } : null, |
| 364 | ); |
| 365 | refresh(); |
| 366 | }} |
| 367 | onError={onError} |
| 368 | /> |
| 369 | </div> |
| 370 | ) : ( |
| 371 | /* 概览:未选中任何条目 */ |
| 372 | <div className="glass rounded-xl p-6 max-w-xl"> |
| 373 | <PageTitle className="mb-1">{S.ontology.title}</PageTitle> |
| 374 | <p className="text-xs text-neutral-500 u-num"> |
| 375 | {S.ontology.overviewStats( |
| 376 | entity_types.length, |
| 377 | relations.length, |
| 378 | )} |
| 379 | </p> |
| 380 | <p className="mt-3 text-sm text-neutral-400"> |
| 381 | {S.ontology.overviewHint} |
| 382 | </p> |
| 383 | </div> |
| 384 | )} |
| 385 | </div> |
| 386 | </div> |
| 387 | </div> |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | /* ---------- 实例列表:选中类的实体(服务端分页,点击进图谱) ---------- */ |
| 392 | |
| 393 | function InstancesCard({ kbId, type }: { kbId: string; type: EntityTypeView }) { |
| 394 | const PER = 12; |
| 395 | const [page, setPage] = useState(0); |
| 396 | useEffect(() => setPage(0), [type.id]); |
| 397 | const q = useQuery({ |
| 398 | queryKey: ["type-entities", kbId, type.id, page], |
| 399 | queryFn: () => api.typeEntities(kbId, type.id, page, PER), |
| 400 | }); |
| 401 | const total = q.data?.total ?? 0; |
| 402 | const rows = q.data?.entities ?? []; |
| 403 | if (!q.isPending && total === 0) return null; // 没有实例时不占版面 |
| 404 | |
| 405 | return ( |
| 406 | <div className="glass rounded-xl p-4"> |
| 407 | <div className="mb-1.5 flex items-baseline gap-2"> |
| 408 | <h3 className="text-sm font-bold text-neutral-200"> |
| 409 | {S.ontology.instances} |
| 410 | </h3> |
| 411 | <span className="u-num text-xs text-neutral-500">{total}</span> |
| 412 | </div> |
| 413 | <div className="divide-y divide-white/[0.06]"> |
| 414 | {rows.map((e) => ( |
| 415 | <Link |
| 416 | key={e.id} |
| 417 | to="/kb/$kbId/graph" |
| 418 | params={{ kbId }} |
| 419 | search={{ entity: e.id }} |
| 420 | className="flex items-center gap-2 py-1.5 text-sm text-neutral-300 hover:text-white" |
| 421 | > |
| 422 | <span |
| 423 | className={`h-2 w-2 shrink-0 ${type.shape === "square" ? "" : "rounded-full"}`} |
| 424 | style={{ background: type.color }} |
| 425 | /> |
| 426 | <span className="truncate">{e.name}</span> |
| 427 | <span className="ml-auto shrink-0 u-num text-[10.5px] text-neutral-600"> |
| 428 | {S.ontology.instanceFacts(e.fact_count)} |
| 429 | </span> |
| 430 | </Link> |
| 431 | ))} |
| 432 | </div> |
| 433 | <Pager total={total} pageSize={PER} page={page} onPage={setPage} /> |
| 434 | </div> |
| 435 | ); |
| 436 | } |
| 437 | |
| 438 | /* ---------- 属性卡片:选中类的字面值字段(行内增改删) ---------- */ |
| 439 | |
| 440 | function AttributesCard({ |
| 441 | kbId, |
| 442 | type, |
| 443 | attributes, |
| 444 | onChanged, |
| 445 | onError, |
| 446 | }: { |
| 447 | kbId: string; |
| 448 | type: EntityTypeView; |
| 449 | attributes: RelationTypeView[]; |
| 450 | onChanged: () => void; |
| 451 | onError: (e: unknown) => void; |
| 452 | }) { |
| 453 | // 行内编辑:一次只展开一行(属性 id 或 "new") |
| 454 | const [editing, setEditing] = useState<string | null>(null); |
| 455 | useEffect(() => setEditing(null), [type.id]); |
| 456 | |
| 457 | return ( |
| 458 | <div className="glass rounded-xl p-4"> |
| 459 | <div className="mb-1 flex items-baseline gap-2"> |
| 460 | <h3 className="text-sm font-bold text-neutral-200"> |
| 461 | {S.ontology.attributes} |
| 462 | </h3> |
| 463 | {attributes.length > 0 && ( |
| 464 | <span className="u-num text-xs text-neutral-500"> |
| 465 | {attributes.length} |
| 466 | </span> |
| 467 | )} |
| 468 | </div> |
| 469 | <p className="text-xs text-neutral-500 mb-2"> |
| 470 | {S.ontology.attributesHint} |
| 471 | </p> |
| 472 | <div className="divide-y divide-white/[0.06]"> |
| 473 | {attributes.map((a) => |
| 474 | editing === a.id ? ( |
| 475 | <AttributeForm |
| 476 | key={a.id} |
| 477 | kbId={kbId} |
| 478 | typeId={type.id} |
| 479 | existing={a} |
| 480 | onDone={() => { |
| 481 | setEditing(null); |
| 482 | onChanged(); |
| 483 | }} |
| 484 | onCancel={() => setEditing(null)} |
| 485 | onError={onError} |
| 486 | /> |
| 487 | ) : ( |
| 488 | <button |
| 489 | key={a.id} |
| 490 | onClick={() => setEditing(a.id)} |
| 491 | className="w-full flex items-center gap-2 py-1.5 text-sm text-left text-neutral-300 hover:text-white" |
| 492 | > |
| 493 | <span className="truncate">{a.label}</span> |
| 494 | <Chip tone="neutral"> |
| 495 | {S.ontology.datatypeNames[a.datatype ?? "text"]} |
| 496 | </Chip> |
| 497 | {a.unit && ( |
| 498 | <span className="text-xs text-neutral-500 shrink-0"> |
| 499 | {a.unit} |
| 500 | </span> |
| 501 | )} |
| 502 | {a.functional && <Chip tone="info">1:1</Chip>} |
| 503 | <span className="ml-auto shrink-0 u-num text-[10.5px] text-neutral-600"> |
| 504 | {S.ontology.usage(a.usage)} |
| 505 | </span> |
| 506 | </button> |
| 507 | ), |
| 508 | )} |
| 509 | </div> |
| 510 | {editing === "new" ? ( |
| 511 | <div className="pt-2"> |
| 512 | <AttributeForm |
| 513 | kbId={kbId} |
| 514 | typeId={type.id} |
| 515 | existing={null} |
| 516 | onDone={() => { |
| 517 | setEditing(null); |
| 518 | onChanged(); |
| 519 | }} |
| 520 | onCancel={() => setEditing(null)} |
| 521 | onError={onError} |
| 522 | /> |
| 523 | </div> |
| 524 | ) : ( |
| 525 | <button |
| 526 | onClick={() => setEditing("new")} |
| 527 | className="mt-1.5 flex items-center gap-1.5 text-[13px] text-neutral-500 hover:text-neutral-200 transition-colors" |
| 528 | > |
| 529 | <Plus size={13} /> |
| 530 | {S.ontology.newAttribute} |
| 531 | </button> |
| 532 | )} |
| 533 | </div> |
| 534 | ); |
| 535 | } |
| 536 | |
| 537 | function AttributeForm({ |
| 538 | kbId, |
| 539 | typeId, |
| 540 | existing, |
| 541 | onDone, |
| 542 | onCancel, |
| 543 | onError, |
| 544 | }: { |
| 545 | kbId: string; |
| 546 | typeId: string; |
| 547 | existing: RelationTypeView | null; |
| 548 | onDone: () => void; |
| 549 | onCancel: () => void; |
| 550 | onError: (e: unknown) => void; |
| 551 | }) { |
| 552 | const [key, setKey] = useState(existing?.key ?? ""); |
| 553 | const [label, setLabel] = useState(existing?.label ?? ""); |
| 554 | const [datatype, setDatatype] = useState(existing?.datatype ?? "text"); |
| 555 | const [unit, setUnit] = useState(existing?.unit ?? ""); |
| 556 | // 单值 = functional:新值经时态引擎闭合旧值(属性历史的来源)。多数属性如此,默认开 |
| 557 | const [single, setSingle] = useState(existing?.functional ?? true); |
| 558 | const [description, setDescription] = useState(existing?.description ?? ""); |
| 559 | |
| 560 | const save = useMutation({ |
| 561 | mutationFn: async (): Promise<unknown> => |
| 562 | existing |
| 563 | ? api.updateRelationType(kbId, existing.id, { |
| 564 | label, |
| 565 | temporal: existing.temporal, |
| 566 | functional: single, |
| 567 | inverse_functional: false, |
| 568 | description, |
| 569 | datatype, |
| 570 | unit, |
| 571 | }) |
| 572 | : api.createRelationType(kbId, { |
| 573 | key, |
| 574 | label, |
| 575 | kind: "attribute", |
| 576 | domains: [typeId], |
| 577 | temporal: "state", |
| 578 | functional: single, |
| 579 | inverse_functional: false, |
| 580 | description, |
| 581 | datatype, |
| 582 | unit, |
| 583 | }), |
| 584 | onSuccess: () => { |
| 585 | toast.success(existing ? S.toast.saved : S.toast.created); |
| 586 | onDone(); |
| 587 | }, |
| 588 | onError, |
| 589 | }); |
| 590 | const remove = useMutation({ |
| 591 | mutationFn: () => api.deleteRelationType(kbId, existing!.id), |
| 592 | onSuccess: () => { |
| 593 | toast.success(S.toast.deleted); |
| 594 | onDone(); |
| 595 | }, |
| 596 | onError, |
| 597 | }); |
| 598 | |
| 599 | const lbl = "block text-xs font-medium text-neutral-500 mb-1"; |
| 600 | return ( |
| 601 | <div className="py-2.5 space-y-2.5"> |
| 602 | {!existing && ( |
| 603 | <div className="flex gap-2"> |
| 604 | <div className="flex-1"> |
| 605 | <label className={lbl}>{S.ontology.key}</label> |
| 606 | <Input |
| 607 | value={key} |
| 608 | onChange={(e) => setKey(e.target.value)} |
| 609 | className="w-full" |
| 610 | placeholder="salary" |
| 611 | /> |
| 612 | </div> |
| 613 | <div className="flex-1"> |
| 614 | <label className={lbl}>{S.ontology.label}</label> |
| 615 | <Input |
| 616 | value={label} |
| 617 | onChange={(e) => setLabel(e.target.value)} |
| 618 | className="w-full" |
| 619 | /> |
| 620 | </div> |
| 621 | </div> |
| 622 | )} |
| 623 | {existing && ( |
| 624 | <div> |
| 625 | <label className={lbl}>{S.ontology.label}</label> |
| 626 | <Input |
| 627 | value={label} |
| 628 | onChange={(e) => setLabel(e.target.value)} |
| 629 | className="w-full" |
| 630 | /> |
| 631 | </div> |
| 632 | )} |
| 633 | <div className="flex gap-2"> |
| 634 | <div className="flex-1"> |
| 635 | <label className={lbl}>{S.ontology.attrDatatype}</label> |
| 636 | <Dropdown |
| 637 | value={datatype} |
| 638 | onChange={(v) => setDatatype(v as typeof datatype)} |
| 639 | className="w-full" |
| 640 | options={(["text", "number", "date", "bool"] as const).map((d) => ({ |
| 641 | value: d, |
| 642 | label: S.ontology.datatypeNames[d], |
| 643 | }))} |
| 644 | /> |
| 645 | </div> |
| 646 | <div className="flex-1"> |
| 647 | <label className={lbl}> |
| 648 | {S.ontology.attrUnit}{" "} |
| 649 | <span className="text-neutral-600"> |
| 650 | ({S.ontology.attrUnitHint}) |
| 651 | </span> |
| 652 | </label> |
| 653 | <Input |
| 654 | value={unit} |
| 655 | onChange={(e) => setUnit(e.target.value)} |
| 656 | className="w-full" |
| 657 | /> |
| 658 | </div> |
| 659 | </div> |
| 660 | <label className="flex items-center gap-2 text-[13px] text-neutral-300"> |
| 661 | <input |
| 662 | type="checkbox" |
| 663 | checked={single} |
| 664 | onChange={(e) => setSingle(e.target.checked)} |
| 665 | /> |
| 666 | {S.ontology.attrSingle} |
| 667 | </label> |
| 668 | <div> |
| 669 | <label className={lbl}>{S.ontology.description}</label> |
| 670 | <textarea |
| 671 | className="input-dark w-full px-3 py-2 text-sm min-h-[3.5rem] resize-y" |
| 672 | value={description} |
| 673 | onChange={(e) => setDescription(e.target.value)} |
| 674 | /> |
| 675 | </div> |
| 676 | <div className="flex gap-2"> |
| 677 | <Button |
| 678 | size="sm" |
| 679 | onClick={() => save.mutate()} |
| 680 | disabled={ |
| 681 | save.isPending || !label.trim() || (!existing && !key.trim()) |
| 682 | } |
| 683 | > |
| 684 | {S.ontology.save} |
| 685 | </Button> |
| 686 | <Button size="sm" variant="ghost" onClick={onCancel}> |
| 687 | {S.ontology.cancel} |
| 688 | </Button> |
| 689 | {existing && ( |
| 690 | <Button |
| 691 | size="sm" |
| 692 | variant="ghost" |
| 693 | className="ml-auto" |
| 694 | disabled={existing.usage > 0} |
| 695 | title={existing.usage > 0 ? S.ontology.deleteBlocked : undefined} |
| 696 | onClick={() => remove.mutate()} |
| 697 | > |
| 698 | {S.ontology.delete} |
| 699 | </Button> |
| 700 | )} |
| 701 | </div> |
| 702 | </div> |
| 703 | ); |
| 704 | } |
| 705 | |
| 706 | /* ---------- 左栏小节头 ---------- */ |
| 707 | |
| 708 | /* ---------- 类层级树(可折叠;filter 时拍平) ---------- */ |
| 709 | |
| 710 | function ClassTree({ |
| 711 | types, |
| 712 | filter, |
| 713 | collapsed, |
| 714 | onToggle, |
| 715 | selectedId, |
| 716 | onSelect, |
| 717 | pageSize, |
| 718 | }: { |
| 719 | types: EntityTypeView[]; |
| 720 | filter: string; |
| 721 | collapsed: Set<string>; |
| 722 | onToggle: (id: string) => void; |
| 723 | selectedId: string | null; |
| 724 | onSelect: (id: string) => void; |
| 725 | pageSize: number; |
| 726 | }) { |
| 727 | const rows = useMemo(() => { |
| 728 | const q = filter.trim().toLowerCase(); |
| 729 | if (q) { |
| 730 | // filter 模式:拍平命中项(label/key 都参与匹配) |
| 731 | return types |
| 732 | .filter( |
| 733 | (t) => |
| 734 | t.label.toLowerCase().includes(q) || |
| 735 | t.key.toLowerCase().includes(q), |
| 736 | ) |
| 737 | .map((t) => ({ t, depth: 0, hasChildren: false })); |
| 738 | } |
| 739 | const children = new Map<string | null, EntityTypeView[]>(); |
| 740 | for (const t of types) { |
| 741 | const p = t.primary_parent ?? null; |
| 742 | if (!children.has(p)) children.set(p, []); |
| 743 | children.get(p)!.push(t); |
| 744 | } |
| 745 | const out: { t: EntityTypeView; depth: number; hasChildren: boolean }[] = |
| 746 | []; |
| 747 | const walk = (parent: string | null, depth: number) => { |
| 748 | for (const t of children.get(parent) ?? []) { |
| 749 | const kids = children.get(t.id) ?? []; |
| 750 | out.push({ t, depth, hasChildren: kids.length > 0 }); |
| 751 | if (!collapsed.has(t.id)) walk(t.id, depth + 1); |
| 752 | } |
| 753 | }; |
| 754 | walk(null, 0); |
| 755 | return out; |
| 756 | }, [types, filter, collapsed]); |
| 757 | |
| 758 | // 半屏分页:过滤词变化回第一页 |
| 759 | const [page, setPage] = useState(0); |
| 760 | useEffect(() => setPage(0), [filter]); |
| 761 | const { rows: paged, safe } = pageSlice(rows, page, pageSize); |
| 762 | |
| 763 | return ( |
| 764 | <div className="space-y-0.5"> |
| 765 | {paged.map(({ t, depth, hasChildren }) => ( |
| 766 | <button |
| 767 | key={t.id} |
| 768 | onClick={() => onSelect(t.id)} |
| 769 | style={{ paddingLeft: `${6 + depth * 14}px` }} |
| 770 | className={cn( |
| 771 | "w-full text-left rounded-lg py-1.5 pr-2 text-[13px] flex items-center gap-1.5", |
| 772 | selectedId === t.id |
| 773 | ? "u-nav-active" |
| 774 | : "hover:bg-white/[0.05] text-neutral-400 hover:text-neutral-200", |
| 775 | )} |
| 776 | > |
| 777 | {/* 折叠柄:有子类才渲染,点击不选中 */} |
| 778 | {hasChildren ? ( |
| 779 | <span |
| 780 | onClick={(e) => { |
| 781 | e.stopPropagation(); |
| 782 | onToggle(t.id); |
| 783 | }} |
| 784 | className="shrink-0 text-neutral-600 hover:text-neutral-300" |
| 785 | > |
| 786 | <ChevronRight |
| 787 | size={12} |
| 788 | className={cn( |
| 789 | "transition-transform", |
| 790 | !collapsed.has(t.id) && "rotate-90", |
| 791 | )} |
| 792 | /> |
| 793 | </span> |
| 794 | ) : ( |
| 795 | <span className="w-3 shrink-0" /> |
| 796 | )} |
| 797 | {/* 方形是直角:与圆形拉开区分度(图谱节点同理) */} |
| 798 | <span |
| 799 | className={`h-2.5 w-2.5 shrink-0 ${t.shape === "square" ? "" : "rounded-full"}`} |
| 800 | style={{ background: t.color }} |
| 801 | /> |
| 802 | {/* 不在列表里放逐项用量读数:数量级上来后统计和渲染都是负担,用量看表单 */} |
| 803 | <span className="truncate">{t.label}</span> |
| 804 | </button> |
| 805 | ))} |
| 806 | <Pager |
| 807 | total={rows.length} |
| 808 | pageSize={pageSize} |
| 809 | page={safe} |
| 810 | onPage={setPage} |
| 811 | /> |
| 812 | </div> |
| 813 | ); |
| 814 | } |
| 815 | |
| 816 | /* ---------- 关系列表 ---------- */ |
| 817 | |
| 818 | function PropertyList({ |
| 819 | relations, |
| 820 | filter, |
| 821 | selectedId, |
| 822 | onSelect, |
| 823 | pageSize, |
| 824 | }: { |
| 825 | relations: RelationTypeView[]; |
| 826 | filter: string; |
| 827 | selectedId: string | null; |
| 828 | onSelect: (id: string) => void; |
| 829 | pageSize: number; |
| 830 | }) { |
| 831 | const q = filter.trim().toLowerCase(); |
| 832 | const rows = q |
| 833 | ? relations.filter( |
| 834 | (r) => |
| 835 | r.label.toLowerCase().includes(q) || r.key.toLowerCase().includes(q), |
| 836 | ) |
| 837 | : relations; |
| 838 | // 半屏分页:过滤词变化回第一页 |
| 839 | const [page, setPage] = useState(0); |
| 840 | useEffect(() => setPage(0), [filter]); |
| 841 | const { rows: paged, safe } = pageSlice(rows, page, pageSize); |
| 842 | return ( |
| 843 | <div className="space-y-0.5"> |
| 844 | {paged.map((r) => ( |
| 845 | <button |
| 846 | key={r.id} |
| 847 | onClick={() => onSelect(r.id)} |
| 848 | style={{ paddingLeft: "6px" }} |
| 849 | className={cn( |
| 850 | "w-full text-left rounded-lg py-1.5 pr-2 text-[13px] flex items-center gap-1.5", |
| 851 | selectedId === r.id |
| 852 | ? "u-nav-active" |
| 853 | : "hover:bg-white/[0.05] text-neutral-400 hover:text-neutral-200", |
| 854 | )} |
| 855 | > |
| 856 | {/* 前导只留折叠柄槽:文字起点对齐类行"标识点"的左端 */} |
| 857 | <span className="w-3 shrink-0" /> |
| 858 | <span className="truncate">{r.label}</span> |
| 859 | {r.functional && <Chip tone="info">1:1</Chip>} |
| 860 | </button> |
| 861 | ))} |
| 862 | <Pager |
| 863 | total={rows.length} |
| 864 | pageSize={pageSize} |
| 865 | page={safe} |
| 866 | onPage={setPage} |
| 867 | /> |
| 868 | </div> |
| 869 | ); |
| 870 | } |
| 871 | |
| 872 | /* ---------- 类表单 ---------- */ |
| 873 | |
| 874 | /** 父类下拉的候选:树序 + 缩进(层级可见),排除自己与全部后代(防成环)。 */ |
| 875 | function parentOptions( |
| 876 | allTypes: EntityTypeView[], |
| 877 | selfId: string | undefined, |
| 878 | ): { value: string; label: string; indent: number }[] { |
| 879 | const excluded = new Set<string>(); |
| 880 | if (selfId) { |
| 881 | excluded.add(selfId); |
| 882 | // 收后代:反复扫描直到收敛(类的数量级很小,O(n²) 无所谓) |
| 883 | let grew = true; |
| 884 | while (grew) { |
| 885 | grew = false; |
| 886 | for (const t of allTypes) { |
| 887 | if (t.parents.some((p) => excluded.has(p)) && !excluded.has(t.id)) { |
| 888 | excluded.add(t.id); |
| 889 | grew = true; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | const children = new Map<string | null, EntityTypeView[]>(); |
| 895 | for (const t of allTypes) { |
| 896 | const p = t.primary_parent ?? null; |
| 897 | if (!children.has(p)) children.set(p, []); |
| 898 | children.get(p)!.push(t); |
| 899 | } |
| 900 | const out: { value: string; label: string; indent: number }[] = []; |
| 901 | const walk = (parent: string | null, depth: number) => { |
| 902 | for (const t of children.get(parent) ?? []) { |
| 903 | if (excluded.has(t.id)) continue; |
| 904 | out.push({ value: t.id, label: t.label, indent: depth }); |
| 905 | walk(t.id, depth + 1); |
| 906 | } |
| 907 | }; |
| 908 | walk(null, 0); |
| 909 | return out; |
| 910 | } |
| 911 | |
| 912 | function ClassForm({ |
| 913 | kbId, |
| 914 | existing, |
| 915 | parentId, |
| 916 | allTypes, |
| 917 | onNewSub, |
| 918 | onDone, |
| 919 | onError, |
| 920 | }: { |
| 921 | kbId: string; |
| 922 | existing: EntityTypeView | null; |
| 923 | parentId: string | null; |
| 924 | allTypes: EntityTypeView[]; |
| 925 | /** 编辑已有类时提供:以当前类为父级新建子类 */ |
| 926 | onNewSub?: () => void; |
| 927 | /** 创建成功时携带新 id,编辑成功时为 undefined */ |
| 928 | onDone: (createdId?: string) => void; |
| 929 | onError: (e: unknown) => void; |
| 930 | }) { |
| 931 | const [key, setKey] = useState(existing?.key ?? ""); |
| 932 | const [label, setLabel] = useState(existing?.label ?? ""); |
| 933 | // 新建时颜色跟着 key 走(与后端 color_for_key 同一个规则),不是一个固定默认值。 |
| 934 | // 用户当然可以改;但**不改的话,手动建的类和导入建的类配色体系一致** |
| 935 | const [color, setColor] = useState( |
| 936 | existing?.color ?? colorForKey(existing?.key ?? ""), |
| 937 | ); |
| 938 | const [colorTouched, setColorTouched] = useState(Boolean(existing?.color)); |
| 939 | const [shape, setShape] = useState<"circle" | "square">( |
| 940 | existing?.shape ?? "circle", |
| 941 | ); |
| 942 | const [parents, setParents] = useState<string[]>( |
| 943 | existing?.parents ?? (parentId ? [parentId] : []), |
| 944 | ); |
| 945 | const [description, setDescription] = useState(existing?.description ?? ""); |
| 946 | // 互斥:声明「不可能同时是」。一致性检查据此报不可满足的类(0002)—— |
| 947 | // 一个类继承了两个互斥的祖先,就永远不可能有实例,而它不报错,只是永远空着 |
| 948 | const [disjoint, setDisjoint] = useState<string[]>(existing?.disjoint ?? []); |
| 949 | |
| 950 | // 从左栏点"+ 子类"进来时预填那个父。多父下它是第一个,也就是主父 |
| 951 | useEffect(() => setParents(parentId ? [parentId] : []), [parentId]); |
| 952 | |
| 953 | const save = useMutation({ |
| 954 | mutationFn: async (): Promise<unknown> => |
| 955 | existing |
| 956 | ? api.updateEntityType(kbId, existing.id, { |
| 957 | label, |
| 958 | color, |
| 959 | shape, |
| 960 | parents, |
| 961 | disjoint, |
| 962 | description, |
| 963 | }) |
| 964 | : api.createEntityType(kbId, { |
| 965 | key, |
| 966 | label, |
| 967 | color, |
| 968 | shape, |
| 969 | parents, |
| 970 | disjoint, |
| 971 | description, |
| 972 | }), |
| 973 | onSuccess: (res) => { |
| 974 | toast.success(existing ? S.toast.saved : S.toast.created); |
| 975 | onDone(existing ? undefined : (res as { id?: string })?.id); |
| 976 | }, |
| 977 | onError, |
| 978 | }); |
| 979 | const remove = useMutation({ |
| 980 | mutationFn: () => api.deleteEntityType(kbId, existing!.id), |
| 981 | onSuccess: () => { |
| 982 | toast.success(S.toast.deleted); |
| 983 | onDone(); |
| 984 | }, |
| 985 | onError, |
| 986 | }); |
| 987 | |
| 988 | const lbl = "block text-xs font-medium text-neutral-500 mb-1"; |
| 989 | return ( |
| 990 | <div className="space-y-3"> |
| 991 | <div className="flex items-center gap-2"> |
| 992 | <span |
| 993 | className={`h-3 w-3 ${shape === "square" ? "" : "rounded-full"}`} |
| 994 | style={{ background: color }} |
| 995 | /> |
| 996 | <span className="font-bold text-neutral-100"> |
| 997 | {existing?.label ?? S.ontology.newClass} |
| 998 | </span> |
| 999 | {/* key 是纯技术标识:已存在时干脆不展示,只在创建时输入 */} |
| 1000 | {existing?.builtin && <Chip tone="neutral">{S.ontology.builtin}</Chip>} |
| 1001 | {existing && ( |
| 1002 | <span className="ml-auto text-xs text-neutral-500"> |
| 1003 | {S.ontology.usage(existing.usage)} |
| 1004 | </span> |
| 1005 | )} |
| 1006 | </div> |
| 1007 | {!existing && ( |
| 1008 | <div> |
| 1009 | <label className={lbl}> |
| 1010 | {S.ontology.key}{" "} |
| 1011 | <span className="text-neutral-600">({S.ontology.keyHint})</span> |
| 1012 | </label> |
| 1013 | <Input |
| 1014 | value={key} |
| 1015 | onChange={(e) => { |
| 1016 | setKey(e.target.value); |
| 1017 | // 用户没自己挑过色,就让颜色跟着 key 走——与后端同一个规则 |
| 1018 | if (!colorTouched) setColor(colorForKey(e.target.value)); |
| 1019 | }} |
| 1020 | className="w-full" |
| 1021 | placeholder="contract" |
| 1022 | /> |
| 1023 | </div> |
| 1024 | )} |
| 1025 | <div> |
| 1026 | <label className={lbl}>{S.ontology.label}</label> |
| 1027 | <Input |
| 1028 | value={label} |
| 1029 | onChange={(e) => setLabel(e.target.value)} |
| 1030 | className="w-full" |
| 1031 | /> |
| 1032 | </div> |
| 1033 | <div> |
| 1034 | <label className={lbl}>{S.ontology.shapeColor}</label> |
| 1035 | <div className="flex items-center gap-2"> |
| 1036 | <ColorPicker value={color} onChange={(c: string) => { setColor(c); setColorTouched(true); }} shape={shape} /> |
| 1037 | {/* 形状:与图谱节点渲染一一对应(circle=四层圆 / square=四层方) */} |
| 1038 | <div className="flex rounded-lg overflow-hidden border border-white/10"> |
| 1039 | {(["circle", "square"] as const).map((sh) => ( |
| 1040 | <button |
| 1041 | key={sh} |
| 1042 | onClick={() => setShape(sh)} |
| 1043 | title={sh} |
| 1044 | className={`h-8 w-10 grid place-items-center transition-colors ${ |
| 1045 | shape === sh |
| 1046 | ? "bg-white/[0.12] text-white" |
| 1047 | : "text-neutral-500 hover:bg-white/[0.05] hover:text-neutral-300" |
| 1048 | }`} |
| 1049 | > |
| 1050 | <span |
| 1051 | className={`h-3 w-3 border-[1.5px] border-current ${ |
| 1052 | sh === "circle" ? "rounded-full" : "" |
| 1053 | }`} |
| 1054 | /> |
| 1055 | </button> |
| 1056 | ))} |
| 1057 | </div> |
| 1058 | </div> |
| 1059 | </div> |
| 1060 | {/* 多父:subClassOf 可以有多条。左栏按树画,一个类只能出现一次—— |
| 1061 | 所以第一个当主父。界面说明这条,不另加一个"选主父"的控件 */} |
| 1062 | <div> |
| 1063 | <label className={lbl}>{S.ontology.parent}</label> |
| 1064 | <MultiSearchSelect |
| 1065 | values={parents} |
| 1066 | options={parentOptions(allTypes, existing?.id)} |
| 1067 | onToggle={(id) => |
| 1068 | setParents((v) => |
| 1069 | v.includes(id) ? v.filter((x) => x !== id) : [...v, id], |
| 1070 | ) |
| 1071 | } |
| 1072 | placeholder={S.ontology.searchTypes} |
| 1073 | emptyHint={S.ontology.noParent} |
| 1074 | /> |
| 1075 | {parents.length > 1 && ( |
| 1076 | <p className="mt-1 text-[11px] text-neutral-600"> |
| 1077 | {S.ontology.primaryParentHint} |
| 1078 | </p> |
| 1079 | )} |
| 1080 | </div> |
| 1081 | {/* 互斥:**声明「不可能同时是」**。紧挨着父类,因为两者是同一件事的 |
| 1082 | 两面——父类说「也是」,互斥说「不可能同时是」,而一致性检查正是 |
| 1083 | 在这两者打架时报出「这个类永远不可能有实例」 */} |
| 1084 | <div> |
| 1085 | <label className={lbl}>{S.ontology.disjoint}</label> |
| 1086 | <p className="text-[11px] leading-relaxed text-neutral-600 mb-1.5"> |
| 1087 | {S.ontology.disjointHint} |
| 1088 | </p> |
| 1089 | <MultiSearchSelect |
| 1090 | values={disjoint} |
| 1091 | options={parentOptions(allTypes, existing?.id)} |
| 1092 | onToggle={(id) => |
| 1093 | setDisjoint((v) => |
| 1094 | v.includes(id) ? v.filter((x) => x !== id) : [...v, id], |
| 1095 | ) |
| 1096 | } |
| 1097 | placeholder={S.ontology.searchTypes} |
| 1098 | emptyHint={S.ontology.noDisjoint} |
| 1099 | /> |
| 1100 | {/* 跟自己的父类互斥 = 这个类永远不可能有实例。当场说, |
| 1101 | 比让人跑一遍一致性检查再发现要快 */} |
| 1102 | {disjoint.some((d) => parents.includes(d)) && ( |
| 1103 | <p className="mt-1.5 text-[11px] text-[var(--u-danger)]"> |
| 1104 | {S.ontology.disjointWithParent} |
| 1105 | </p> |
| 1106 | )} |
| 1107 | </div> |
| 1108 | <div> |
| 1109 | <label className={lbl}>{S.ontology.description}</label> |
| 1110 | {/* 语义指引:整段注入抽取 prompt,直接影响抽取归类质量 */} |
| 1111 | <textarea |
| 1112 | className="input-dark w-full px-3 py-2 text-sm min-h-[4.5rem] resize-y" |
| 1113 | value={description} |
| 1114 | onChange={(e) => setDescription(e.target.value)} |
| 1115 | /> |
| 1116 | <p className="mt-1 text-[10.5px] text-neutral-600"> |
| 1117 | {S.ontology.descriptionHint} |
| 1118 | </p> |
| 1119 | </div> |
| 1120 | <div className="flex gap-2 pt-1"> |
| 1121 | <Button |
| 1122 | size="sm" |
| 1123 | onClick={() => save.mutate()} |
| 1124 | disabled={save.isPending || !label.trim()} |
| 1125 | > |
| 1126 | {S.ontology.save} |
| 1127 | </Button> |
| 1128 | {onNewSub && ( |
| 1129 | <Button size="sm" variant="ghost" onClick={onNewSub}> |
| 1130 | {S.ontology.newSubClass} |
| 1131 | </Button> |
| 1132 | )} |
| 1133 | {existing && !existing.builtin && ( |
| 1134 | <Button |
| 1135 | size="sm" |
| 1136 | variant="ghost" |
| 1137 | disabled={existing.usage > 0} |
| 1138 | title={existing.usage > 0 ? S.ontology.deleteBlocked : undefined} |
| 1139 | onClick={() => remove.mutate()} |
| 1140 | > |
| 1141 | {S.ontology.delete} |
| 1142 | </Button> |
| 1143 | )} |
| 1144 | </div> |
| 1145 | </div> |
| 1146 | ); |
| 1147 | } |
| 1148 | |
| 1149 | /* ---------- 关系表单 ---------- */ |
| 1150 | |
| 1151 | function PropertyForm({ |
| 1152 | kbId, |
| 1153 | existing, |
| 1154 | allTypes, |
| 1155 | allRelations, |
| 1156 | onDone, |
| 1157 | onError, |
| 1158 | }: { |
| 1159 | kbId: string; |
| 1160 | existing: RelationTypeView | null; |
| 1161 | allTypes: EntityTypeView[]; |
| 1162 | /** 本库的关系(不含属性)。逆与子属性的下拉从这里取——**属性不在其中**, |
| 1163 | * 它的宾语是字面值,反过来无从谈起 */ |
| 1164 | allRelations: RelationTypeView[]; |
| 1165 | onDone: (createdId?: string) => void; |
| 1166 | onError: (e: unknown) => void; |
| 1167 | }) { |
| 1168 | const [key, setKey] = useState(existing?.key ?? ""); |
| 1169 | const [label, setLabel] = useState(existing?.label ?? ""); |
| 1170 | const [temporal, setTemporal] = useState(existing?.temporal ?? "state"); |
| 1171 | const [functional, setFunctional] = useState(existing?.functional ?? false); |
| 1172 | const [inverseFunctional, setInverseFunctional] = useState( |
| 1173 | existing?.inverse_functional ?? false, |
| 1174 | ); |
| 1175 | // 其余四条 OWL 公理。**推理机的判据全在这里**——从前只能靠导入 OWL 带进来, |
| 1176 | // 在界面上手工建本体的人永远开不了那台机器(0002) |
| 1177 | const [transitive, setTransitive] = useState(existing?.is_transitive ?? false); |
| 1178 | const [symmetric, setSymmetric] = useState(existing?.is_symmetric ?? false); |
| 1179 | const [asymmetric, setAsymmetric] = useState(existing?.is_asymmetric ?? false); |
| 1180 | const [irreflexive, setIrreflexive] = useState( |
| 1181 | existing?.is_irreflexive ?? false, |
| 1182 | ); |
| 1183 | // 同一族的后两条,形状不同:指向另一个关系。空串 = 没声明 |
| 1184 | const [inverseOf, setInverseOf] = useState(existing?.inverse_of ?? ""); |
| 1185 | const [subPropertyOf, setSubPropertyOf] = useState( |
| 1186 | existing?.sub_property_of ?? "", |
| 1187 | ); |
| 1188 | const [description, setDescription] = useState(existing?.description ?? ""); |
| 1189 | const [domains, setDomains] = useState<string[]>(existing?.domains ?? []); |
| 1190 | const [ranges, setRanges] = useState<string[]>(existing?.ranges ?? []); |
| 1191 | // 显示标签,不显示 key。**进提示词的 key 由服务端从库里取**,与界面显示什么无关; |
| 1192 | // 而类树、属性列表也都显示标签,这里没有理由例外——中文库里用户该看到 |
| 1193 | // "发票记录" 而不是 invoice_record |
| 1194 | const typeOpts = useMemo( |
| 1195 | () => parentOptions(allTypes, undefined), |
| 1196 | [allTypes], |
| 1197 | ); |
| 1198 | // 两个下拉的选项:本库的其它关系。 |
| 1199 | // |
| 1200 | // **自己不进列表**,两条都是。子属性指向自己数据库直接拒(那是个环); |
| 1201 | // 逆指向自己在语义上合法——但它等于 `symmetric`,而那个复选框就在上面, |
| 1202 | // 在这里提供第二条路只会让人写出 R0 要报的东西。 |
| 1203 | // |
| 1204 | // 唯一的例外是**当前值就是自己**:OWL 导入进来的可以长这样,从列表里漏掉 |
| 1205 | // 它就会让下拉显示空白,而空白一保存就把已声明的抹了 |
| 1206 | const linkOptions = (current: string) => [ |
| 1207 | { value: "", label: S.ontology.noLink }, |
| 1208 | // 当前值就是自己时把自己补回列表,否则下拉显示空白, |
| 1209 | // 而空白一保存就把已声明的抹了 |
| 1210 | ...(existing && current === existing.id |
| 1211 | ? [{ value: existing.id, label: existing.label, hint: existing.key }] |
| 1212 | : []), |
| 1213 | ...allRelations |
| 1214 | .filter((r) => r.id !== existing?.id) |
| 1215 | .map((r) => ({ value: r.id, label: r.label, hint: r.key })), |
| 1216 | ]; |
| 1217 | /** 下拉里选中那条的显示名。找不到就回落到 id——宁可难看,不要空着 */ |
| 1218 | const nameOf = (id: string) => |
| 1219 | allRelations.find((r) => r.id === id)?.label ?? id; |
| 1220 | const toggle = ( |
| 1221 | set: React.Dispatch<React.SetStateAction<string[]>>, |
| 1222 | id: string, |
| 1223 | ) => set((v) => (v.includes(id) ? v.filter((x) => x !== id) : [...v, id])); |
| 1224 | |
| 1225 | const save = useMutation({ |
| 1226 | mutationFn: async (): Promise<unknown> => |
| 1227 | existing |
| 1228 | ? api.updateRelationType(kbId, existing.id, { |
| 1229 | label, |
| 1230 | temporal, |
| 1231 | functional, |
| 1232 | inverse_functional: inverseFunctional, |
| 1233 | is_transitive: transitive, |
| 1234 | is_symmetric: symmetric, |
| 1235 | is_asymmetric: asymmetric, |
| 1236 | is_irreflexive: irreflexive, |
| 1237 | // 空串要变成 null 再送——服务端收 `Option<Uuid>`, |
| 1238 | // `""` 解不成 UUID,会是一个 422 而不是「清空」 |
| 1239 | inverse_of: inverseOf || null, |
| 1240 | sub_property_of: subPropertyOf || null, |
| 1241 | description, |
| 1242 | domains, |
| 1243 | ranges, |
| 1244 | }) |
| 1245 | : api.createRelationType(kbId, { |
| 1246 | key, |
| 1247 | label, |
| 1248 | temporal, |
| 1249 | functional, |
| 1250 | inverse_functional: inverseFunctional, |
| 1251 | is_transitive: transitive, |
| 1252 | is_symmetric: symmetric, |
| 1253 | is_asymmetric: asymmetric, |
| 1254 | is_irreflexive: irreflexive, |
| 1255 | // 空串要变成 null 再送——服务端收 `Option<Uuid>`, |
| 1256 | // `""` 解不成 UUID,会是一个 422 而不是「清空」 |
| 1257 | inverse_of: inverseOf || null, |
| 1258 | sub_property_of: subPropertyOf || null, |
| 1259 | description, |
| 1260 | domains, |
| 1261 | ranges, |
| 1262 | }), |
| 1263 | onSuccess: (res) => { |
| 1264 | toast.success(existing ? S.toast.saved : S.toast.created); |
| 1265 | onDone(existing ? undefined : (res as { id?: string })?.id); |
| 1266 | }, |
| 1267 | onError, |
| 1268 | }); |
| 1269 | const remove = useMutation({ |
| 1270 | mutationFn: () => api.deleteRelationType(kbId, existing!.id), |
| 1271 | onSuccess: () => { |
| 1272 | toast.success(S.toast.deleted); |
| 1273 | onDone(); |
| 1274 | }, |
| 1275 | onError, |
| 1276 | }); |
| 1277 | |
| 1278 | const lbl = "block text-xs font-medium text-neutral-500 mb-1"; |
| 1279 | return ( |
| 1280 | <div className="space-y-3"> |
| 1281 | <div className="flex items-center gap-2"> |
| 1282 | <span className="font-bold text-neutral-100"> |
| 1283 | {existing?.label ?? S.ontology.newProperty} |
| 1284 | </span> |
| 1285 | {existing?.builtin && <Chip tone="neutral">{S.ontology.builtin}</Chip>} |
| 1286 | {existing && ( |
| 1287 | <span className="ml-auto text-xs text-neutral-500"> |
| 1288 | {S.ontology.usage(existing.usage)} |
| 1289 | </span> |
| 1290 | )} |
| 1291 | </div> |
| 1292 | {!existing && ( |
| 1293 | <div> |
| 1294 | <label className={lbl}> |
| 1295 | {S.ontology.key}{" "} |
| 1296 | <span className="text-neutral-600">({S.ontology.keyHint})</span> |
| 1297 | </label> |
| 1298 | <Input |
| 1299 | value={key} |
| 1300 | onChange={(e) => setKey(e.target.value)} |
| 1301 | className="w-full" |
| 1302 | placeholder="signed_with" |
| 1303 | /> |
| 1304 | </div> |
| 1305 | )} |
| 1306 | <div> |
| 1307 | <label className={lbl}>{S.ontology.label}</label> |
| 1308 | <Input |
| 1309 | value={label} |
| 1310 | onChange={(e) => setLabel(e.target.value)} |
| 1311 | className="w-full" |
| 1312 | /> |
| 1313 | </div> |
| 1314 | {/* 类型签名。界面显示标签,而进提示词的是 key —— 那一步在服务端, |
| 1315 | 与这里显示什么无关(docs/decisions/0004 定的是提示词里必须用 key) */} |
| 1316 | <div> |
| 1317 | <label className={lbl}>{S.ontology.signature}</label> |
| 1318 | <p className="text-[11px] leading-relaxed text-neutral-600 mb-1.5"> |
| 1319 | {S.ontology.signatureHint} |
| 1320 | </p> |
| 1321 | <div className="grid gap-2 sm:grid-cols-2"> |
| 1322 | <div className="min-w-0"> |
| 1323 | <div className="text-[10px] uppercase tracking-[0.08em] text-neutral-600 mb-1"> |
| 1324 | {S.ontology.domainLabel} |
| 1325 | </div> |
| 1326 | <MultiSearchSelect |
| 1327 | values={domains} |
| 1328 | options={typeOpts} |
| 1329 | onToggle={(id) => toggle(setDomains, id)} |
| 1330 | placeholder={S.ontology.searchTypes} |
| 1331 | emptyHint={S.ontology.anyType} |
| 1332 | /> |
| 1333 | </div> |
| 1334 | <div className="min-w-0"> |
| 1335 | <div className="text-[10px] uppercase tracking-[0.08em] text-neutral-600 mb-1"> |
| 1336 | {S.ontology.rangeLabel} |
| 1337 | </div> |
| 1338 | <MultiSearchSelect |
| 1339 | values={ranges} |
| 1340 | options={typeOpts} |
| 1341 | onToggle={(id) => toggle(setRanges, id)} |
| 1342 | placeholder={S.ontology.searchTypes} |
| 1343 | emptyHint={S.ontology.anyType} |
| 1344 | /> |
| 1345 | </div> |
| 1346 | </div> |
| 1347 | </div> |
| 1348 | <div> |
| 1349 | <label className={lbl}>{S.ontology.temporal}</label> |
| 1350 | <Dropdown |
| 1351 | value={temporal} |
| 1352 | onChange={setTemporal} |
| 1353 | className="w-full" |
| 1354 | options={[ |
| 1355 | { value: "state", label: S.ontology.temporalState }, |
| 1356 | { value: "event", label: S.ontology.temporalEvent }, |
| 1357 | { value: "eternal", label: S.ontology.temporalEternal }, |
| 1358 | ]} |
| 1359 | /> |
| 1360 | </div> |
| 1361 | {/* 六条公理并成一组。**它们本来就是同一族**——推理机(0002)拿它们当 |
| 1362 | 判据,散在表单各处会让人以为前两条和后四条是两回事。 |
| 1363 | 每一条底下写清「勾了会发生什么」:这些开关不是描述,是会改变系统行为的 |
| 1364 | 声明——`functional` 让时态引擎自动闭合旧值,`transitive` 让推理机往图里 |
| 1365 | 加边。看不出后果的开关,人只会照着直觉乱勾。 */} |
| 1366 | <div> |
| 1367 | <label className={lbl}>{S.ontology.axioms}</label> |
| 1368 | <p className="text-[11px] leading-relaxed text-neutral-600 mb-1.5"> |
| 1369 | {S.ontology.axiomsHint} |
| 1370 | </p> |
| 1371 | <div className="space-y-1.5"> |
| 1372 | {( |
| 1373 | [ |
| 1374 | [functional, setFunctional, S.ontology.functional, S.ontology.functionalHint], |
| 1375 | [ |
| 1376 | inverseFunctional, |
| 1377 | setInverseFunctional, |
| 1378 | S.ontology.inverseFunctional, |
| 1379 | S.ontology.inverseFunctionalHint, |
| 1380 | ], |
| 1381 | [transitive, setTransitive, S.ontology.transitive, S.ontology.transitiveHint], |
| 1382 | [symmetric, setSymmetric, S.ontology.symmetric, S.ontology.symmetricHint], |
| 1383 | [asymmetric, setAsymmetric, S.ontology.asymmetric, S.ontology.asymmetricHint], |
| 1384 | [ |
| 1385 | irreflexive, |
| 1386 | setIrreflexive, |
| 1387 | S.ontology.irreflexive, |
| 1388 | S.ontology.irreflexiveHint, |
| 1389 | ], |
| 1390 | ] as const |
| 1391 | ).map(([on, set, title, hint], i) => ( |
| 1392 | <label key={i} className="flex items-start gap-2 cursor-pointer"> |
| 1393 | <input |
| 1394 | type="checkbox" |
| 1395 | className="mt-0.5 accent-[var(--u-accent)]" |
| 1396 | checked={on} |
| 1397 | onChange={(e) => set(e.target.checked)} |
| 1398 | /> |
| 1399 | <span className="min-w-0"> |
| 1400 | <span className="block text-[13px] text-neutral-200">{title}</span> |
| 1401 | <span className="block text-[11px] leading-relaxed text-neutral-500"> |
| 1402 | {hint} |
| 1403 | </span> |
| 1404 | </span> |
| 1405 | </label> |
| 1406 | ))} |
| 1407 | </div> |
| 1408 | {/* 对称与反对称同时勾是自相矛盾的(只对空关系成立)。本体自洽性检查 |
| 1409 | 会报出来,但在这里当场说一句比让人跑一遍检查再发现要快 */} |
| 1410 | {symmetric && asymmetric && ( |
| 1411 | <p className="mt-1.5 text-[11px] text-[var(--u-danger)]"> |
| 1412 | {S.ontology.axiomConflict} |
| 1413 | </p> |
| 1414 | )} |
| 1415 | {/* 同一组的后两条,只是形状不同:它们指向**另一个关系**,所以是下拉 |
| 1416 | 不是复选框。放在这里而不是单开一节——推理机的四种规则源里,两条是 |
| 1417 | 上面的勾,两条是下面的选,分开会让人以为它们是两回事(0002) */} |
| 1418 | <div className="mt-3 space-y-2.5 border-t border-white/5 pt-3"> |
| 1419 | {( |
| 1420 | [ |
| 1421 | [ |
| 1422 | inverseOf, |
| 1423 | setInverseOf, |
| 1424 | S.ontology.inverseOf, |
| 1425 | S.ontology.inverseOfHint, |
| 1426 | linkOptions(inverseOf), |
| 1427 | ], |
| 1428 | [ |
| 1429 | subPropertyOf, |
| 1430 | setSubPropertyOf, |
| 1431 | S.ontology.subPropertyOf, |
| 1432 | S.ontology.subPropertyOfHint, |
| 1433 | linkOptions(subPropertyOf), |
| 1434 | ], |
| 1435 | ] as const |
| 1436 | ).map(([value, set, title, hint, options], i) => ( |
| 1437 | <div key={i}> |
| 1438 | <div className="text-[13px] text-neutral-200">{title}</div> |
| 1439 | <p className="text-[11px] leading-relaxed text-neutral-500 mb-1"> |
| 1440 | {hint} |
| 1441 | </p> |
| 1442 | <SearchSelect |
| 1443 | value={value} |
| 1444 | onChange={set} |
| 1445 | options={options} |
| 1446 | size="sm" |
| 1447 | className="w-full" |
| 1448 | placeholder={S.ontology.noLink} |
| 1449 | /> |
| 1450 | </div> |
| 1451 | ))} |
| 1452 | {/* 选了之后当场把话说全。**这两条推出来的事实主宾未必同向**—— |
| 1453 | 逆要对调,子属性不对调,只看名字分不出来,写出来就分得出 */} |
| 1454 | {(inverseOf || subPropertyOf) && ( |
| 1455 | <div className="text-[11px] leading-relaxed text-neutral-500 space-y-0.5"> |
| 1456 | {inverseOf && ( |
| 1457 | <div> |
| 1458 | {S.ontology.linkMeansInverse( |
| 1459 | label.trim() || key || "?", |
| 1460 | nameOf(inverseOf), |
| 1461 | )} |
| 1462 | </div> |
| 1463 | )} |
| 1464 | {subPropertyOf && ( |
| 1465 | <div> |
| 1466 | {S.ontology.linkMeansSuper( |
| 1467 | label.trim() || key || "?", |
| 1468 | nameOf(subPropertyOf), |
| 1469 | )} |
| 1470 | </div> |
| 1471 | )} |
| 1472 | </div> |
| 1473 | )} |
| 1474 | </div> |
| 1475 | </div> |
| 1476 | <div> |
| 1477 | <label className={lbl}>{S.ontology.description}</label> |
| 1478 | <textarea |
| 1479 | className="input-dark w-full px-3 py-2 text-sm min-h-[4.5rem] resize-y" |
| 1480 | value={description} |
| 1481 | onChange={(e) => setDescription(e.target.value)} |
| 1482 | /> |
| 1483 | <p className="mt-1 text-[10.5px] text-neutral-600"> |
| 1484 | {S.ontology.descriptionHint} |
| 1485 | </p> |
| 1486 | </div> |
| 1487 | <div className="flex gap-2 pt-1"> |
| 1488 | <Button |
| 1489 | size="sm" |
| 1490 | onClick={() => save.mutate()} |
| 1491 | disabled={save.isPending || !label.trim()} |
| 1492 | > |
| 1493 | {S.ontology.save} |
| 1494 | </Button> |
| 1495 | {existing && !existing.builtin && ( |
| 1496 | <Button |
| 1497 | size="sm" |
| 1498 | variant="ghost" |
| 1499 | disabled={existing.usage > 0} |
| 1500 | title={existing.usage > 0 ? S.ontology.deleteBlocked : undefined} |
| 1501 | onClick={() => remove.mutate()} |
| 1502 | > |
| 1503 | {S.ontology.delete} |
| 1504 | </Button> |
| 1505 | )} |
| 1506 | </div> |
| 1507 | </div> |
| 1508 | ); |
| 1509 | } |
| 1510 | |
| 1511 | /* ---------- 未匹配信号 + AI 建议 ---------- */ |
| 1512 | |
| 1513 | |
| 1514 | /** 类型消解:把「大致对」的类换成更具体的那个。 |
| 1515 | * |
| 1516 | * **两步走,跟本体导入同一个形状**:先看一遍会发生什么,再决定落不落。这里还多 |
| 1517 | * 一层理由——改类不进时间轴,不像事实改写那样在实体历史里自己显形,所以 |
| 1518 | * 「先看一眼」是唯一能看见它的时机。 |
| 1519 | * |
| 1520 | * 跑完分三档,各有各的处置:自动改掉的(可整批撤销)、跨了分类轴留给人的、 |
| 1521 | * 裁决说「都不是」的。**最后那一档带着理由**——这一步押在「选择都不是是个 |
| 1522 | * 体面答案」上,不记理由,最大的那一档就是不透明的。 |
| 1523 | */ |
| 1524 | function RefinePanel({ |
| 1525 | kbId, |
| 1526 | onChanged, |
| 1527 | onError, |
| 1528 | }: { |
| 1529 | kbId: string; |
| 1530 | onChanged: () => void; |
| 1531 | onError: (e: Error) => void; |
| 1532 | }) { |
| 1533 | const [preview, setPreview] = useState<TypeSuggestion[] | null>(null); |
| 1534 | const [outcome, setOutcome] = useState<ResolutionOutcome | null>(null); |
| 1535 | |
| 1536 | const look = useMutation({ |
| 1537 | mutationFn: () => api.typeResolutionPreview(kbId), |
| 1538 | onSuccess: (d) => { |
| 1539 | setPreview(d.items); |
| 1540 | setOutcome(null); |
| 1541 | }, |
| 1542 | onError, |
| 1543 | }); |
| 1544 | const run = useMutation({ |
| 1545 | mutationFn: () => api.typeResolutionApply(kbId), |
| 1546 | onSuccess: (d) => { |
| 1547 | setOutcome(d); |
| 1548 | setPreview(null); |
| 1549 | onChanged(); |
| 1550 | }, |
| 1551 | onError, |
| 1552 | }); |
| 1553 | const approve = useMutation({ |
| 1554 | mutationFn: (v: { |
| 1555 | from_type_id: string; |
| 1556 | to_type_id: string; |
| 1557 | entity_ids: string[]; |
| 1558 | }) => api.approveRefinement(kbId, v), |
| 1559 | onSuccess: () => { |
| 1560 | toast.success(S.toast.saved); |
| 1561 | onChanged(); |
| 1562 | }, |
| 1563 | onError, |
| 1564 | }); |
| 1565 | const undo = useMutation({ |
| 1566 | mutationFn: (batch: string) => api.typeResolutionUndo(kbId, batch), |
| 1567 | onSuccess: (d) => { |
| 1568 | toast.success(S.ontology.refineUndone(d.reverted)); |
| 1569 | setOutcome(null); |
| 1570 | onChanged(); |
| 1571 | }, |
| 1572 | onError, |
| 1573 | }); |
| 1574 | |
| 1575 | const busy = look.isPending || run.isPending; |
| 1576 | |
| 1577 | return ( |
| 1578 | <div className="space-y-4"> |
| 1579 | <div> |
| 1580 | <h3 className="u-title text-lg mb-1">{S.ontology.refineTitle}</h3> |
| 1581 | <p className="text-xs leading-relaxed text-neutral-500 max-w-xl"> |
| 1582 | {S.ontology.refineHint} |
| 1583 | </p> |
| 1584 | </div> |
| 1585 | |
| 1586 | <div className="flex gap-2"> |
| 1587 | <button |
| 1588 | className="u-btn text-xs" |
| 1589 | disabled={busy} |
| 1590 | onClick={() => look.mutate()} |
| 1591 | > |
| 1592 | {look.isPending ? S.ontology.refineLooking : S.ontology.refinePreview} |
| 1593 | </button> |
| 1594 | <button |
| 1595 | className="u-btn u-btn-primary text-xs" |
| 1596 | disabled={busy} |
| 1597 | onClick={() => run.mutate()} |
| 1598 | > |
| 1599 | {run.isPending ? S.ontology.refineRunning : S.ontology.refineRun} |
| 1600 | </button> |
| 1601 | </div> |
| 1602 | |
| 1603 | {/* ---- 只算不写的那一步 */} |
| 1604 | {preview && ( |
| 1605 | <div className="space-y-2"> |
| 1606 | <p className="text-xs text-neutral-500"> |
| 1607 | {preview.length === 0 |
| 1608 | ? S.ontology.refineNothing |
| 1609 | : S.ontology.refineCandidates(preview.length)} |
| 1610 | </p> |
| 1611 | {preview.map((s) => ( |
| 1612 | <div key={s.entity_id} className="glass rounded-xl p-3"> |
| 1613 | <div className="flex items-baseline gap-2 flex-wrap"> |
| 1614 | <span className="text-sm text-neutral-100">{s.name}</span> |
| 1615 | <span className="text-[11px] text-neutral-500"> |
| 1616 | {s.coarse ?? S.graph.untyped} |
| 1617 | </span> |
| 1618 | {s.specific_type && ( |
| 1619 | <span className="text-[11px] text-[var(--u-warn)]"> |
| 1620 | {S.ontology.refineModelSays(s.specific_type)} |
| 1621 | </span> |
| 1622 | )} |
| 1623 | <span className="ml-auto u-num text-[10.5px] text-neutral-600"> |
| 1624 | {S.review.factsCount(s.fact_count)} |
| 1625 | </span> |
| 1626 | </div> |
| 1627 | {/* **把送去检索的那段字显示出来**:找不着的时候,第一个要看的 |
| 1628 | 就是我们拿什么去找的,而不是猜画像还是类描述的问题 */} |
| 1629 | <p className="mt-1 text-[11px] text-neutral-500 line-clamp-2"> |
| 1630 | {s.profile} |
| 1631 | </p> |
| 1632 | <div className="mt-1.5 flex flex-wrap gap-1"> |
| 1633 | {s.candidates.slice(0, 6).map((c) => ( |
| 1634 | <span |
| 1635 | key={c.id} |
| 1636 | title={c.description} |
| 1637 | className="u-chip u-chip-neutral u-num text-[10.5px]" |
| 1638 | > |
| 1639 | {c.label} {c.distance.toFixed(2)} |
| 1640 | </span> |
| 1641 | ))} |
| 1642 | {s.candidates.length === 0 && ( |
| 1643 | <span className="text-[11px] text-neutral-600"> |
| 1644 | {S.ontology.refineNoCandidates} |
| 1645 | </span> |
| 1646 | )} |
| 1647 | </div> |
| 1648 | </div> |
| 1649 | ))} |
| 1650 | </div> |
| 1651 | )} |
| 1652 | |
| 1653 | {/* ---- 落库之后的三档 */} |
| 1654 | {outcome && ( |
| 1655 | <div className="space-y-3"> |
| 1656 | <div className="flex items-center gap-3 flex-wrap"> |
| 1657 | <span className="text-xs text-neutral-300"> |
| 1658 | {S.ontology.refineRetyped(outcome.retyped)} |
| 1659 | </span> |
| 1660 | {outcome.batch && outcome.retyped > 0 && ( |
| 1661 | <button |
| 1662 | className="u-btn u-btn-ghost text-xs" |
| 1663 | disabled={undo.isPending} |
| 1664 | onClick={() => undo.mutate(outcome.batch!)} |
| 1665 | > |
| 1666 | {S.ontology.refineUndo} |
| 1667 | </button> |
| 1668 | )} |
| 1669 | </div> |
| 1670 | |
| 1671 | {outcome.for_review.length > 0 && ( |
| 1672 | <div className="space-y-2"> |
| 1673 | <p className="text-xs text-neutral-500"> |
| 1674 | {S.ontology.refineForReview(outcome.for_review.length)} |
| 1675 | </p> |
| 1676 | {outcome.for_review.map((r) => ( |
| 1677 | <div key={r.entity_id} className="glass rounded-xl p-3"> |
| 1678 | <div className="flex items-baseline gap-2 flex-wrap"> |
| 1679 | <span className="text-sm text-neutral-100">{r.name}</span> |
| 1680 | <span className="text-[11px] text-neutral-500"> |
| 1681 | {r.coarse ?? S.graph.untyped} → {r.choice} |
| 1682 | </span> |
| 1683 | {r.crosses_axis && ( |
| 1684 | <span className="u-chip u-chip-warn text-[10.5px]"> |
| 1685 | {S.ontology.refineCrossesAxis} |
| 1686 | </span> |
| 1687 | )} |
| 1688 | <span className="ml-auto u-num text-[10.5px] text-neutral-600"> |
| 1689 | {Math.round(r.confidence * 100)}% |
| 1690 | </span> |
| 1691 | </div> |
| 1692 | {r.reason && ( |
| 1693 | <p className="mt-1 text-[11px] text-neutral-500"> |
| 1694 | {r.reason} |
| 1695 | </p> |
| 1696 | )} |
| 1697 | {/* **认可的是这一对类,不是这一个实体。** 认可一次, |
| 1698 | 之后同一对不再进人工——那正是这一档大部分条目的成因 */} |
| 1699 | {r.from_type_id && ( |
| 1700 | <button |
| 1701 | className="u-btn u-btn-primary mt-2 text-xs" |
| 1702 | disabled={approve.isPending} |
| 1703 | onClick={() => |
| 1704 | approve.mutate({ |
| 1705 | from_type_id: r.from_type_id!, |
| 1706 | to_type_id: r.to_type_id, |
| 1707 | entity_ids: [r.entity_id], |
| 1708 | }) |
| 1709 | } |
| 1710 | > |
| 1711 | {S.ontology.refineApprovePair} |
| 1712 | </button> |
| 1713 | )} |
| 1714 | </div> |
| 1715 | ))} |
| 1716 | </div> |
| 1717 | )} |
| 1718 | |
| 1719 | {outcome.left_alone.length > 0 && ( |
| 1720 | <div className="space-y-1.5"> |
| 1721 | <p className="text-xs text-neutral-500"> |
| 1722 | {S.ontology.refineLeftAlone(outcome.left_alone.length)} |
| 1723 | </p> |
| 1724 | {outcome.left_alone.map((d, i) => ( |
| 1725 | <div key={i} className="glass rounded-xl px-3 py-2"> |
| 1726 | <div className="flex items-baseline gap-2 flex-wrap"> |
| 1727 | <span className="text-[13px] text-neutral-200"> |
| 1728 | {d.name} |
| 1729 | </span> |
| 1730 | <span className="text-[11px] text-neutral-500"> |
| 1731 | {d.coarse ?? S.graph.untyped} |
| 1732 | </span> |
| 1733 | </div> |
| 1734 | {/* 理由与头一个候选一起给:理由说不通时,看候选就知道是 |
| 1735 | 检索没找着还是裁决没看上 */} |
| 1736 | {d.reason && ( |
| 1737 | <p className="mt-0.5 text-[11px] text-neutral-500"> |
| 1738 | {d.reason} |
| 1739 | </p> |
| 1740 | )} |
| 1741 | {d.top_candidate && ( |
| 1742 | <p className="mt-0.5 text-[11px] text-neutral-600"> |
| 1743 | {S.ontology.refineTopCandidate(d.top_candidate)} |
| 1744 | </p> |
| 1745 | )} |
| 1746 | </div> |
| 1747 | ))} |
| 1748 | </div> |
| 1749 | )} |
| 1750 | </div> |
| 1751 | )} |
| 1752 | </div> |
| 1753 | ); |
| 1754 | } |
| 1755 | function MissesPanel({ |
| 1756 | kbId, |
| 1757 | misses, |
| 1758 | dismissedMisses, |
| 1759 | onChanged, |
| 1760 | onError, |
| 1761 | }: { |
| 1762 | kbId: string; |
| 1763 | misses: OntologyMiss[]; |
| 1764 | dismissedMisses: OntologyMiss[]; |
| 1765 | onChanged: () => void; |
| 1766 | onError: (e: unknown) => void; |
| 1767 | }) { |
| 1768 | // 默认收起:已忽略的是**背景信息**,不该跟待处理的挤在一起抢注意力 |
| 1769 | const [showDismissed, setShowDismissed] = useState(false); |
| 1770 | const [proposals, setProposals] = useState<OntologyProposals | null>(null); |
| 1771 | // 上次算出来、还没人表态的那些:刷新页面之后从库里捞回来(0049)。 |
| 1772 | // |
| 1773 | // 从前这里只有上面那个 useState——刷新一次、切走一次,整批提议就没了, |
| 1774 | // 想再看只能重跑一次模型,而重跑未必给出同一批归并。归并了哪些说法正是 |
| 1775 | // 唯一能查证过并的东西(0003 的 optimized_for → runs_on 就是这么抓出来的) |
| 1776 | const storedProposals = useQuery({ |
| 1777 | queryKey: ["storedProposals", kbId], |
| 1778 | queryFn: () => api.storedProposals(kbId), |
| 1779 | }); |
| 1780 | useEffect(() => { |
| 1781 | // 只在还没有本地结果时回填。刚点完 Suggest 的那一批更新,不该被覆盖 |
| 1782 | if (proposals === null && storedProposals.data) { |
| 1783 | const d = storedProposals.data; |
| 1784 | const empty = |
| 1785 | !d.entity_types?.length && |
| 1786 | !d.relation_types?.length && |
| 1787 | !d.attribute_types?.length; |
| 1788 | if (!empty) setProposals(d); |
| 1789 | } |
| 1790 | }, [storedProposals.data, proposals]); |
| 1791 | // 最近一次采纳,供撤销。只留最近一次——旧批次要撤销走审计台账, |
| 1792 | // 那里本来就记着每次采纳动了哪个关系、多少条 |
| 1793 | const [lastAdopt, setLastAdopt] = useState<{ |
| 1794 | batches: string[]; |
| 1795 | key: string; |
| 1796 | moved: number; |
| 1797 | } | null>(null); |
| 1798 | // 撤销要二次确认:一次改回成批事实 |
| 1799 | const [confirmUndo, setConfirmUndo] = useState<{ |
| 1800 | batches: string[]; |
| 1801 | moved: number; |
| 1802 | } | null>(null); |
| 1803 | // 系统自动扩过本体没有——横幅要据此显示,撤干净了后端返回 null |
| 1804 | const autoRun = useQuery({ |
| 1805 | queryKey: ["auto-extension", kbId], |
| 1806 | queryFn: () => api.lastAutoExtension(kbId), |
| 1807 | }); |
| 1808 | // 待认领的表层谓词:提案的影响面("将改写 57 条")从这里算 |
| 1809 | const surface = useQuery({ |
| 1810 | queryKey: ["proposed-predicates", kbId], |
| 1811 | queryFn: () => api.proposedPredicates(kbId), |
| 1812 | }); |
| 1813 | const factsWaiting = (forms: string[]) => { |
| 1814 | const byForm = new Map( |
| 1815 | (surface.data?.forms ?? []).map((f) => [f.form, f.fact_count]), |
| 1816 | ); |
| 1817 | return forms.reduce((n, f) => n + (byForm.get(f) ?? 0), 0); |
| 1818 | }; |
| 1819 | |
| 1820 | const suggest = useMutation({ |
| 1821 | mutationFn: () => api.suggestOntology(kbId), |
| 1822 | onSuccess: setProposals, |
| 1823 | onError, |
| 1824 | }); |
| 1825 | const dismiss = useMutation({ |
| 1826 | mutationFn: ({ kind, key }: { kind: string; key: string }) => |
| 1827 | api.dismissMiss(kbId, kind, key), |
| 1828 | onSuccess: onChanged, |
| 1829 | onError, |
| 1830 | }); |
| 1831 | const restore = useMutation({ |
| 1832 | mutationFn: ({ kind, key }: { kind: string; key: string }) => |
| 1833 | api.restoreMiss(kbId, kind, key), |
| 1834 | onSuccess: onChanged, |
| 1835 | onError, |
| 1836 | }); |
| 1837 | const approveEntity = useMutation({ |
| 1838 | mutationFn: (p: { key: string; label: string; description?: string }) => |
| 1839 | api.createEntityType(kbId, { |
| 1840 | key: p.key, |
| 1841 | label: p.label, |
| 1842 | description: p.description, |
| 1843 | }), |
| 1844 | onSuccess: (_data, p) => { |
| 1845 | // 已采纳:从提案列表移除,并顺带清掉对应的未匹配统计 chip(本体已覆盖) |
| 1846 | toast.success(S.toast.added); |
| 1847 | setProposals( |
| 1848 | (prev) => |
| 1849 | prev && { |
| 1850 | ...prev, |
| 1851 | entity_types: prev.entity_types.filter((x) => x.key !== p.key), |
| 1852 | }, |
| 1853 | ); |
| 1854 | api.dismissMiss(kbId, "entity_type", p.key).catch(() => {}); |
| 1855 | // 提案表态落库(0049):下一轮 Suggest 不会把它刷回待看 |
| 1856 | api.decideProposal(kbId, "entity_types", p.key, "adopted").catch(() => {}); |
| 1857 | onChanged(); |
| 1858 | }, |
| 1859 | onError, |
| 1860 | }); |
| 1861 | const approveRelation = useMutation({ |
| 1862 | // 带 forms 的提案走 adopt:建关系顺带把等着它的无谓词事实认过去。 |
| 1863 | // 只建关系的话本体长大了、图没变好——那些事实会继续是"有关联" |
| 1864 | mutationFn: (p: { |
| 1865 | key: string; |
| 1866 | label: string; |
| 1867 | temporal?: string; |
| 1868 | functional?: boolean; |
| 1869 | description?: string; |
| 1870 | forms?: string[]; |
| 1871 | }) => |
| 1872 | p.forms?.length |
| 1873 | ? api.adoptPredicate(kbId, { |
| 1874 | key: p.key, |
| 1875 | label: p.label, |
| 1876 | temporal: p.temporal ?? "state", |
| 1877 | functional: p.functional ?? false, |
| 1878 | description: p.description, |
| 1879 | forms: p.forms, |
| 1880 | }) |
| 1881 | : api.createRelationType(kbId, { |
| 1882 | key: p.key, |
| 1883 | label: p.label, |
| 1884 | temporal: p.temporal ?? "state", |
| 1885 | functional: p.functional ?? false, |
| 1886 | description: p.description, |
| 1887 | }), |
| 1888 | onSuccess: (data, p) => { |
| 1889 | const d = data as { remapped?: number; batch?: string }; |
| 1890 | const moved = d.remapped ?? 0; |
| 1891 | toast.success(moved > 0 ? S.ontology.adopted(moved) : S.toast.added); |
| 1892 | // 撤销的把手:采纳改写了成批事实,没有回头路的话没人敢点第一下 |
| 1893 | if (moved > 0 && d.batch) |
| 1894 | setLastAdopt({ batches: [d.batch], key: p.key, moved }); |
| 1895 | setProposals( |
| 1896 | (prev) => |
| 1897 | prev && { |
| 1898 | ...prev, |
| 1899 | relation_types: prev.relation_types.filter((x) => x.key !== p.key), |
| 1900 | }, |
| 1901 | ); |
| 1902 | api.dismissMiss(kbId, "relation_type", p.key).catch(() => {}); |
| 1903 | api.decideProposal(kbId, "relation_types", p.key, "adopted").catch(() => {}); |
| 1904 | onChanged(); |
| 1905 | }, |
| 1906 | onError, |
| 1907 | }); |
| 1908 | |
| 1909 | // 逐条串行而不是加个批量端点:每个谓词各有自己的批次和撤销粒度, |
| 1910 | // 而且部分失败能如实报告("5 个成功,1 个 key 已存在")而不是整批回滚 |
| 1911 | // 属性提案:宾语是字面值的那些。走同一个采纳入口,但值要按 datatype |
| 1912 | // 换算,换不动的不改写——所以回执里的 unconvertible 必须说出来 |
| 1913 | const approveAttribute = useMutation({ |
| 1914 | mutationFn: (p: { |
| 1915 | key: string; |
| 1916 | label: string; |
| 1917 | datatype?: string; |
| 1918 | unit?: string; |
| 1919 | description?: string; |
| 1920 | forms?: string[]; |
| 1921 | }) => |
| 1922 | api.adoptPredicate(kbId, { |
| 1923 | key: p.key, |
| 1924 | kind: "attribute", |
| 1925 | label: p.label, |
| 1926 | datatype: p.datatype ?? "text", |
| 1927 | unit: p.unit, |
| 1928 | description: p.description, |
| 1929 | forms: p.forms ?? [], |
| 1930 | }), |
| 1931 | onSuccess: (data, p) => { |
| 1932 | const moved = data.remapped ?? 0; |
| 1933 | const left = data.unconvertible ?? 0; |
| 1934 | toast.success( |
| 1935 | left > 0 |
| 1936 | ? S.ontology.adoptedPartly(moved, left) |
| 1937 | : moved > 0 |
| 1938 | ? S.ontology.adopted(moved) |
| 1939 | : S.toast.added, |
| 1940 | ); |
| 1941 | if (moved > 0 && data.batch) |
| 1942 | setLastAdopt({ batches: [data.batch], key: p.key, moved }); |
| 1943 | setProposals( |
| 1944 | (prev) => |
| 1945 | prev && { |
| 1946 | ...prev, |
| 1947 | attribute_types: (prev.attribute_types ?? []).filter( |
| 1948 | (x) => x.key !== p.key, |
| 1949 | ), |
| 1950 | }, |
| 1951 | ); |
| 1952 | for (const form of p.forms ?? []) |
| 1953 | api.dismissMiss(kbId, "attribute_type", form).catch(() => {}); |
| 1954 | api.decideProposal(kbId, "attribute_types", p.key, "adopted").catch(() => {}); |
| 1955 | onChanged(); |
| 1956 | }, |
| 1957 | onError, |
| 1958 | }); |
| 1959 | // 映射到已有类型:不建东西,只把这些说法的事实挂过去。 |
| 1960 | // 跟新建走同一个采纳入口,因为它对图做的事一模一样——也因此同样可撤销 |
| 1961 | const approveMapping = useMutation({ |
| 1962 | mutationFn: (p: { key: string; kind?: string; forms?: string[] }) => |
| 1963 | api.adoptPredicate(kbId, { |
| 1964 | key: p.key, |
| 1965 | existing: true, |
| 1966 | // 目标是属性时值要按它的 datatype 换算,服务端据此分道 |
| 1967 | kind: p.kind === "attribute" ? "attribute" : "relation", |
| 1968 | forms: p.forms ?? [], |
| 1969 | }), |
| 1970 | onSuccess: (data, p) => { |
| 1971 | const moved = data.remapped ?? 0; |
| 1972 | const left = data.unconvertible ?? 0; |
| 1973 | toast.success( |
| 1974 | left > 0 |
| 1975 | ? S.ontology.adoptedPartly(moved, left) |
| 1976 | : moved > 0 |
| 1977 | ? S.ontology.adopted(moved) |
| 1978 | : S.toast.saved, |
| 1979 | ); |
| 1980 | if (moved > 0 && data.batch) |
| 1981 | setLastAdopt({ batches: [data.batch], key: p.key, moved }); |
| 1982 | setProposals( |
| 1983 | (prev) => |
| 1984 | prev && { |
| 1985 | ...prev, |
| 1986 | map_to: (prev.map_to ?? []).filter((x) => x.key !== p.key), |
| 1987 | }, |
| 1988 | ); |
| 1989 | for (const form of p.forms ?? []) |
| 1990 | api.dismissMiss(kbId, "relation_type", form).catch(() => {}); |
| 1991 | onChanged(); |
| 1992 | }, |
| 1993 | onError, |
| 1994 | }); |
| 1995 | const addAll = useMutation({ |
| 1996 | mutationFn: async (all: OntologyProposals) => { |
| 1997 | const batches: string[] = []; |
| 1998 | let moved = 0; |
| 1999 | const failed: string[] = []; |
| 2000 | for (const p of all.entity_types) { |
| 2001 | try { |
| 2002 | await api.createEntityType(kbId, { key: p.key, label: p.label }); |
| 2003 | } catch { |
| 2004 | failed.push(p.key); |
| 2005 | } |
| 2006 | } |
| 2007 | for (const p of all.relation_types) { |
| 2008 | try { |
| 2009 | if (p.forms?.length) { |
| 2010 | const r = await api.adoptPredicate(kbId, { |
| 2011 | key: p.key, |
| 2012 | label: p.label, |
| 2013 | temporal: p.temporal ?? "state", |
| 2014 | functional: p.functional ?? false, |
| 2015 | description: p.description, |
| 2016 | forms: p.forms, |
| 2017 | }); |
| 2018 | moved += r.remapped; |
| 2019 | if (r.remapped > 0) batches.push(r.batch); |
| 2020 | } else { |
| 2021 | await api.createRelationType(kbId, { |
| 2022 | key: p.key, |
| 2023 | label: p.label, |
| 2024 | temporal: p.temporal ?? "state", |
| 2025 | functional: p.functional ?? false, |
| 2026 | description: p.description, |
| 2027 | }); |
| 2028 | } |
| 2029 | } catch { |
| 2030 | failed.push(p.key); |
| 2031 | } |
| 2032 | } |
| 2033 | for (const p of all.attribute_types ?? []) { |
| 2034 | if (!p.forms?.length) continue; |
| 2035 | try { |
| 2036 | const r = await api.adoptPredicate(kbId, { |
| 2037 | key: p.key, |
| 2038 | kind: "attribute", |
| 2039 | label: p.label, |
| 2040 | datatype: p.datatype ?? "text", |
| 2041 | unit: p.unit, |
| 2042 | description: p.description, |
| 2043 | forms: p.forms, |
| 2044 | }); |
| 2045 | moved += r.remapped; |
| 2046 | if (r.remapped > 0) batches.push(r.batch); |
| 2047 | } catch { |
| 2048 | failed.push(p.key); |
| 2049 | } |
| 2050 | } |
| 2051 | for (const p of all.map_to ?? []) { |
| 2052 | if (!p.forms?.length) continue; |
| 2053 | try { |
| 2054 | const r = await api.adoptPredicate(kbId, { |
| 2055 | key: p.key, |
| 2056 | existing: true, |
| 2057 | kind: p.kind === "attribute" ? "attribute" : "relation", |
| 2058 | forms: p.forms, |
| 2059 | }); |
| 2060 | moved += r.remapped; |
| 2061 | if (r.remapped > 0) batches.push(r.batch); |
| 2062 | } catch { |
| 2063 | failed.push(p.key); |
| 2064 | } |
| 2065 | } |
| 2066 | return { batches, moved, failed }; |
| 2067 | }, |
| 2068 | onSuccess: (r) => { |
| 2069 | if (r.failed.length) toast.error(S.ontology.addAllPartial(r.failed)); |
| 2070 | else toast.success(S.ontology.adopted(r.moved)); |
| 2071 | if (r.batches.length) |
| 2072 | setLastAdopt({ |
| 2073 | batches: r.batches, |
| 2074 | key: S.ontology.addAllLabel, |
| 2075 | moved: r.moved, |
| 2076 | }); |
| 2077 | setProposals(null); |
| 2078 | onChanged(); |
| 2079 | }, |
| 2080 | onError, |
| 2081 | }); |
| 2082 | |
| 2083 | const unadopt = useMutation({ |
| 2084 | mutationFn: async (batches: string[]) => { |
| 2085 | let reverted = 0; |
| 2086 | for (const b of batches) |
| 2087 | reverted += (await api.unadoptPredicate(kbId, b)).reverted; |
| 2088 | return { reverted }; |
| 2089 | }, |
| 2090 | onSuccess: (r) => { |
| 2091 | toast.success(S.ontology.reverted(r.reverted)); |
| 2092 | setLastAdopt(null); |
| 2093 | setConfirmUndo(null); |
| 2094 | autoRun.refetch(); |
| 2095 | onChanged(); |
| 2096 | }, |
| 2097 | onError, |
| 2098 | }); |
| 2099 | |
| 2100 | return ( |
| 2101 | <div className="glass rounded-xl p-4"> |
| 2102 | <div className="flex items-center gap-3 mb-1"> |
| 2103 | <h3 className="text-sm font-bold text-neutral-200"> |
| 2104 | {S.ontology.misses} |
| 2105 | </h3> |
| 2106 | {misses.length > 0 && ( |
| 2107 | <Button |
| 2108 | size="sm" |
| 2109 | variant="ghost" |
| 2110 | onClick={() => suggest.mutate()} |
| 2111 | disabled={suggest.isPending} |
| 2112 | > |
| 2113 | {suggest.isPending ? S.ontology.suggesting : S.ontology.suggest} |
| 2114 | </Button> |
| 2115 | )} |
| 2116 | </div> |
| 2117 | <p className="text-xs text-neutral-500 mb-3">{S.ontology.missesHint}</p> |
| 2118 | |
| 2119 | {misses.length === 0 ? ( |
| 2120 | <p className="text-sm text-neutral-500">{S.ontology.noMisses}</p> |
| 2121 | ) : ( |
| 2122 | <div className="flex flex-wrap gap-1.5"> |
| 2123 | {misses.map((m) => ( |
| 2124 | <span |
| 2125 | key={`${m.kind}:${m.key}`} |
| 2126 | className="glass rounded-full px-2.5 py-1 text-xs flex items-center gap-1.5" |
| 2127 | title={m.example ?? ""} |
| 2128 | > |
| 2129 | <Chip tone={m.kind === "entity_type" ? "info" : "violet"}> |
| 2130 | {m.kind === "entity_type" ? "C" : "P"} |
| 2131 | </Chip> |
| 2132 | <span className="font-mono text-neutral-300">{m.key}</span> |
| 2133 | <span className="text-neutral-500">×{m.count}</span> |
| 2134 | <button |
| 2135 | onClick={() => dismiss.mutate({ kind: m.kind, key: m.key })} |
| 2136 | className="text-neutral-600 hover:text-neutral-300" |
| 2137 | > |
| 2138 | ✕ |
| 2139 | </button> |
| 2140 | </span> |
| 2141 | ))} |
| 2142 | </div> |
| 2143 | )} |
| 2144 | {dismissedMisses.length > 0 && ( |
| 2145 | <div className="mt-3 border-t border-white/5 pt-3"> |
| 2146 | <button |
| 2147 | onClick={() => setShowDismissed((v) => !v)} |
| 2148 | className="text-xs text-neutral-500 hover:text-neutral-300" |
| 2149 | > |
| 2150 | {showDismissed ? "▾" : "▸"} {S.ontology.dismissed(dismissedMisses.length)} |
| 2151 | </button> |
| 2152 | {showDismissed && ( |
| 2153 | <> |
| 2154 | <p className="text-xs text-neutral-600 mt-1.5 mb-2"> |
| 2155 | {S.ontology.dismissedHint} |
| 2156 | </p> |
| 2157 | <div className="flex flex-wrap gap-1.5"> |
| 2158 | {dismissedMisses.map((m) => ( |
| 2159 | <span |
| 2160 | key={`d:${m.kind}:${m.key}`} |
| 2161 | className="glass rounded-full px-2.5 py-1 text-xs flex items-center gap-1.5 opacity-60" |
| 2162 | title={m.example ?? ""} |
| 2163 | > |
| 2164 | <Chip tone={m.kind === "entity_type" ? "info" : "violet"}> |
| 2165 | {m.kind === "entity_type" ? "C" : "P"} |
| 2166 | </Chip> |
| 2167 | <span className="font-mono text-neutral-400 line-through"> |
| 2168 | {m.key} |
| 2169 | </span> |
| 2170 | <span className="text-neutral-500">×{m.count}</span> |
| 2171 | <button |
| 2172 | onClick={() => restore.mutate({ kind: m.kind, key: m.key })} |
| 2173 | className="text-neutral-600 hover:text-neutral-200" |
| 2174 | title={S.ontology.restore} |
| 2175 | > |
| 2176 | ↺ |
| 2177 | </button> |
| 2178 | </span> |
| 2179 | ))} |
| 2180 | </div> |
| 2181 | </> |
| 2182 | )} |
| 2183 | </div> |
| 2184 | )} |
| 2185 | {/* 系统自己动了本体,必须让人看见——只记在审计台账里不算可见。 |
| 2186 | 默认开启的前提是它的动作可见且可退,这条横幅是"可见"那一半 */} |
| 2187 | {autoRun.data?.run && !lastAdopt && ( |
| 2188 | <div className="mt-3 rounded-lg border border-[var(--u-accent)]/25 bg-[var(--u-accent)]/[0.06] px-3 py-2.5"> |
| 2189 | <div className="flex items-start gap-2"> |
| 2190 | <div className="min-w-0 flex-1"> |
| 2191 | <p className="text-xs text-neutral-200"> |
| 2192 | {S.ontology.autoRanTitle} |
| 2193 | </p> |
| 2194 | <p className="mt-0.5 text-[11px] text-neutral-400"> |
| 2195 | {S.ontology.autoRanBody( |
| 2196 | autoRun.data.run.relations ?? [], |
| 2197 | autoRun.data.run.facts_remapped ?? 0, |
| 2198 | )} |
| 2199 | </p> |
| 2200 | <p className="mt-0.5 text-[11px] text-neutral-600"> |
| 2201 | {S.ontology.autoRanOff} |
| 2202 | </p> |
| 2203 | </div> |
| 2204 | <Button |
| 2205 | size="sm" |
| 2206 | variant="ghost" |
| 2207 | disabled={unadopt.isPending} |
| 2208 | onClick={() => |
| 2209 | setConfirmUndo({ |
| 2210 | batches: autoRun.data!.run!.batches, |
| 2211 | moved: autoRun.data!.run!.facts_remapped ?? 0, |
| 2212 | }) |
| 2213 | } |
| 2214 | > |
| 2215 | {S.ontology.undoAdoptBtn} |
| 2216 | </Button> |
| 2217 | </div> |
| 2218 | </div> |
| 2219 | )} |
| 2220 | |
| 2221 | {/* 采纳改写了成批事实——没有回头路的话没人敢点第一下 */} |
| 2222 | {lastAdopt && ( |
| 2223 | <div className="mt-3 flex items-center gap-2 rounded-lg border border-white/10 bg-white/[0.03] px-3 py-2"> |
| 2224 | <span className="text-xs text-neutral-300"> |
| 2225 | {S.ontology.undoAdopt(lastAdopt.key, lastAdopt.moved)} |
| 2226 | </span> |
| 2227 | <span className="text-[11px] text-neutral-600"> |
| 2228 | {S.ontology.undoKeepsRelation} |
| 2229 | </span> |
| 2230 | <Button |
| 2231 | size="sm" |
| 2232 | variant="ghost" |
| 2233 | className="ml-auto" |
| 2234 | disabled={unadopt.isPending} |
| 2235 | onClick={() => |
| 2236 | setConfirmUndo({ |
| 2237 | batches: lastAdopt.batches, |
| 2238 | moved: lastAdopt.moved, |
| 2239 | }) |
| 2240 | } |
| 2241 | > |
| 2242 | {S.ontology.undoAdoptBtn} |
| 2243 | </Button> |
| 2244 | </div> |
| 2245 | )} |
| 2246 | |
| 2247 | {/* 撤销一次改回成批事实:轻确认——它本身也是可逆的,不必打字解锁 */} |
| 2248 | {confirmUndo && ( |
| 2249 | <DangerConfirm |
| 2250 | title={S.ontology.undoTitle} |
| 2251 | hint={S.ontology.undoHint(confirmUndo.moved)} |
| 2252 | confirmLabel={S.ontology.undoConfirm} |
| 2253 | cancelLabel={S.ontology.undoCancel} |
| 2254 | busy={unadopt.isPending} |
| 2255 | onConfirm={() => unadopt.mutate(confirmUndo.batches)} |
| 2256 | onCancel={() => setConfirmUndo(null)} |
| 2257 | /> |
| 2258 | )} |
| 2259 | {proposals && ( |
| 2260 | <div className="mt-4 border-t border-white/10 pt-3"> |
| 2261 | <div className="mb-2 flex items-center gap-2"> |
| 2262 | <h4 className="text-xs font-bold text-neutral-400"> |
| 2263 | {S.ontology.proposals} |
| 2264 | </h4> |
| 2265 | {/* 常见情形是"这些都对"——一条条点是把一个决定拆成八个 */} |
| 2266 | {proposals.relation_types.length + |
| 2267 | proposals.entity_types.length + |
| 2268 | (proposals.attribute_types?.length ?? 0) + |
| 2269 | (proposals.map_to?.length ?? 0) > |
| 2270 | 1 && ( |
| 2271 | <Button |
| 2272 | size="sm" |
| 2273 | variant="ghost" |
| 2274 | className="ml-auto" |
| 2275 | disabled={addAll.isPending} |
| 2276 | onClick={() => addAll.mutate(proposals)} |
| 2277 | > |
| 2278 | {addAll.isPending |
| 2279 | ? S.ontology.addingAll |
| 2280 | : S.ontology.addAll( |
| 2281 | proposals.relation_types.length + |
| 2282 | proposals.entity_types.length + |
| 2283 | (proposals.attribute_types?.length ?? 0) + |
| 2284 | (proposals.map_to?.length ?? 0), |
| 2285 | )} |
| 2286 | </Button> |
| 2287 | )} |
| 2288 | </div> |
| 2289 | <div className="space-y-1.5"> |
| 2290 | {/* 排在最前:它说的是"本体已经有了",而这正是最该先看见的一句。 |
| 2291 | 排在新建后面的话,人一路点下来就把重复建出来了 */} |
| 2292 | {(proposals.map_to ?? []).map((p) => ( |
| 2293 | <div key={`map-${p.key}`} className="flex items-center gap-2 text-sm"> |
| 2294 | <Chip tone="success">=</Chip> |
| 2295 | <span className="font-mono text-neutral-300">{p.key}</span> |
| 2296 | {!!p.forms?.length && ( |
| 2297 | <span |
| 2298 | className="text-xs text-neutral-400 truncate" |
| 2299 | title={p.forms.join(" · ")} |
| 2300 | > |
| 2301 | {p.forms.join(" · ")} |
| 2302 | </span> |
| 2303 | )} |
| 2304 | {!!p.forms?.length && ( |
| 2305 | <span className="text-xs text-[var(--u-accent)]"> |
| 2306 | {S.ontology.willRemap(factsWaiting(p.forms))} |
| 2307 | </span> |
| 2308 | )} |
| 2309 | {p.reason && ( |
| 2310 | <span className="text-xs text-neutral-500 truncate"> |
| 2311 | {p.reason} |
| 2312 | </span> |
| 2313 | )} |
| 2314 | <Button |
| 2315 | size="sm" |
| 2316 | className="ml-auto" |
| 2317 | onClick={() => approveMapping.mutate(p)} |
| 2318 | disabled={approveMapping.isPending} |
| 2319 | > |
| 2320 | {S.ontology.mapOver} |
| 2321 | </Button> |
| 2322 | </div> |
| 2323 | ))} |
| 2324 | {proposals.entity_types.map((p) => ( |
| 2325 | <div key={p.key} className="flex items-center gap-2 text-sm"> |
| 2326 | <Chip tone="info">C</Chip> |
| 2327 | <span className="font-mono text-neutral-300">{p.key}</span> |
| 2328 | <span className="text-neutral-200">{p.label}</span> |
| 2329 | {p.reason && ( |
| 2330 | <span className="text-xs text-neutral-500 truncate"> |
| 2331 | {p.reason} |
| 2332 | </span> |
| 2333 | )} |
| 2334 | <Button |
| 2335 | size="sm" |
| 2336 | className="ml-auto" |
| 2337 | onClick={() => approveEntity.mutate(p)} |
| 2338 | disabled={approveEntity.isPending} |
| 2339 | > |
| 2340 | {S.ontology.approve} |
| 2341 | </Button> |
| 2342 | </div> |
| 2343 | ))} |
| 2344 | {proposals.relation_types.map((p) => ( |
| 2345 | <div key={p.key} className="flex items-center gap-2 text-sm"> |
| 2346 | <Chip tone="violet">P</Chip> |
| 2347 | <span className="font-mono text-neutral-300">{p.key}</span> |
| 2348 | <span className="text-neutral-200">{p.label}</span> |
| 2349 | {p.temporal && <Chip tone="neutral">{p.temporal}</Chip>} |
| 2350 | {/* 影响面:采纳后会改写多少条、归并了哪些写法。没有这个, |
| 2351 | "approve" 就只是凭空多一个空关系 */} |
| 2352 | {!!p.forms?.length && ( |
| 2353 | <span |
| 2354 | className="text-xs text-[var(--u-accent)]" |
| 2355 | title={p.forms.join(" · ")} |
| 2356 | > |
| 2357 | {S.ontology.willRemap(factsWaiting(p.forms))} |
| 2358 | </span> |
| 2359 | )} |
| 2360 | {p.reason && ( |
| 2361 | <span className="text-xs text-neutral-500 truncate"> |
| 2362 | {p.reason} |
| 2363 | </span> |
| 2364 | )} |
| 2365 | <Button |
| 2366 | size="sm" |
| 2367 | className="ml-auto" |
| 2368 | onClick={() => approveRelation.mutate(p)} |
| 2369 | disabled={approveRelation.isPending} |
| 2370 | > |
| 2371 | {S.ontology.approve} |
| 2372 | </Button> |
| 2373 | </div> |
| 2374 | ))} |
| 2375 | {(proposals.attribute_types ?? []).map((p) => ( |
| 2376 | <div key={`attr-${p.key}`} className="flex items-center gap-2 text-sm"> |
| 2377 | {/* A 而不是 P:字面值那一档跟关系是两回事,界面上分得清 */} |
| 2378 | <Chip tone="warn">A</Chip> |
| 2379 | <span className="font-mono text-neutral-300">{p.key}</span> |
| 2380 | <span className="text-neutral-200">{p.label}</span> |
| 2381 | <Chip tone="neutral">{p.datatype ?? "text"}</Chip> |
| 2382 | {p.unit && <Chip tone="neutral">{p.unit}</Chip>} |
| 2383 | {!!p.forms?.length && ( |
| 2384 | <span |
| 2385 | className="text-xs text-[var(--u-accent)]" |
| 2386 | title={p.forms.join(" · ")} |
| 2387 | > |
| 2388 | {S.ontology.willRemap(factsWaiting(p.forms))} |
| 2389 | </span> |
| 2390 | )} |
| 2391 | {p.reason && ( |
| 2392 | <span className="text-xs text-neutral-500 truncate"> |
| 2393 | {p.reason} |
| 2394 | </span> |
| 2395 | )} |
| 2396 | <Button |
| 2397 | size="sm" |
| 2398 | className="ml-auto" |
| 2399 | onClick={() => approveAttribute.mutate(p)} |
| 2400 | disabled={approveAttribute.isPending} |
| 2401 | > |
| 2402 | {S.ontology.approve} |
| 2403 | </Button> |
| 2404 | </div> |
| 2405 | ))} |
| 2406 | {proposals.entity_types.length === 0 && |
| 2407 | proposals.relation_types.length === 0 && |
| 2408 | !proposals.attribute_types?.length && |
| 2409 | !proposals.map_to?.length && ( |
| 2410 | <p className="text-sm text-neutral-500">—</p> |
| 2411 | )} |
| 2412 | </div> |
| 2413 | </div> |
| 2414 | )} |
| 2415 | </div> |
| 2416 | ); |
| 2417 | } |
| 2418 | |
| 2419 | /* ---------- 本体导入:上传 → 预览计划 → 确认落库 ---------- */ |
| 2420 | /* 预览与落库共用服务端同一个 plan。这个面板的全部工作是把计划里 |
| 2421 | **会咬人的三件事**放到人点确认之前:函数性关系(错误的唯一性声明会造出 |
| 2422 | 成队假冲突)、没有描述的类(description 逐字进抽取提示词,缺了就静默抽差)、 |
| 2423 | key 撞车(报告不解决——自动改名会让下次重导入认不出自己上次建的是哪个)。 */ |
| 2424 | |
| 2425 | function ImportPanel({ |
| 2426 | kbId, |
| 2427 | onChanged, |
| 2428 | onError, |
| 2429 | }: { |
| 2430 | kbId: string; |
| 2431 | onChanged: () => void; |
| 2432 | onError: (e: unknown) => void; |
| 2433 | }) { |
| 2434 | const [file, setFile] = useState<File | null>(null); |
| 2435 | const pick = useRef<HTMLInputElement>(null); |
| 2436 | const queryClient = useQueryClient(); |
| 2437 | |
| 2438 | const history = useQuery({ |
| 2439 | queryKey: ["ontology-imports", kbId], |
| 2440 | queryFn: () => api.ontologyImports(kbId), |
| 2441 | }); |
| 2442 | |
| 2443 | const preview = useMutation({ |
| 2444 | mutationFn: (f: File) => api.previewOntologyImport(kbId, f), |
| 2445 | onError: (e) => { |
| 2446 | setFile(null); |
| 2447 | onError(e); |
| 2448 | }, |
| 2449 | }); |
| 2450 | |
| 2451 | const apply = useMutation({ |
| 2452 | mutationFn: (f: File) => api.applyOntologyImport(kbId, f), |
| 2453 | onSuccess: (res) => { |
| 2454 | const p = res.plan; |
| 2455 | toast.success( |
| 2456 | S.ontology.importDone( |
| 2457 | p.classes.filter((c) => c.disposition === "create").length, |
| 2458 | p.classes.filter((c) => c.disposition === "update").length, |
| 2459 | ), |
| 2460 | ); |
| 2461 | setFile(null); |
| 2462 | preview.reset(); |
| 2463 | queryClient.invalidateQueries({ queryKey: ["ontology-imports", kbId] }); |
| 2464 | onChanged(); |
| 2465 | }, |
| 2466 | onError, |
| 2467 | }); |
| 2468 | |
| 2469 | const choose = (f: File | undefined) => { |
| 2470 | if (!f) return; |
| 2471 | setFile(f); |
| 2472 | preview.mutate(f); |
| 2473 | }; |
| 2474 | |
| 2475 | const plan = preview.data?.plan ?? null; |
| 2476 | const busy = preview.isPending || apply.isPending; |
| 2477 | const empty = |
| 2478 | plan && |
| 2479 | plan.classes.length === 0 && |
| 2480 | plan.relations.length === 0 && |
| 2481 | plan.attributes.length === 0; |
| 2482 | |
| 2483 | return ( |
| 2484 | <div className="glass rounded-xl p-4"> |
| 2485 | <h3 className="text-sm font-bold text-neutral-200 mb-1"> |
| 2486 | {S.ontology.importTitle} |
| 2487 | </h3> |
| 2488 | <p className="text-xs text-neutral-500 mb-3">{S.ontology.importHint}</p> |
| 2489 | |
| 2490 | <input |
| 2491 | ref={pick} |
| 2492 | type="file" |
| 2493 | accept=".owl,.rdf,.ttl,.xml,.n3" |
| 2494 | className="hidden" |
| 2495 | onChange={(e) => { |
| 2496 | choose(e.target.files?.[0]); |
| 2497 | e.target.value = ""; |
| 2498 | }} |
| 2499 | /> |
| 2500 | <div className="flex items-center gap-2"> |
| 2501 | <Button |
| 2502 | size="sm" |
| 2503 | variant="ghost" |
| 2504 | disabled={busy} |
| 2505 | onClick={() => pick.current?.click()} |
| 2506 | > |
| 2507 | {file ? S.ontology.importChange : S.ontology.importPick} |
| 2508 | </Button> |
| 2509 | {file && ( |
| 2510 | <span className="text-xs text-neutral-400 truncate"> |
| 2511 | <span className="font-mono">{file.name}</span> |
| 2512 | <span className="text-neutral-600"> |
| 2513 | {" "} |
| 2514 | · {S.ontology.importSize(file.size)} |
| 2515 | </span> |
| 2516 | </span> |
| 2517 | )} |
| 2518 | {preview.isPending && ( |
| 2519 | <span className="text-xs text-neutral-500"> |
| 2520 | {S.ontology.importReading} |
| 2521 | </span> |
| 2522 | )} |
| 2523 | </div> |
| 2524 | |
| 2525 | {plan && ( |
| 2526 | <div className="mt-4"> |
| 2527 | <p className="text-[11px] uppercase tracking-[0.08em] text-neutral-600 u-num"> |
| 2528 | {S.ontology.importParsed(plan.format, plan.triples)} |
| 2529 | </p> |
| 2530 | |
| 2531 | {empty ? ( |
| 2532 | <p className="mt-2 text-sm text-neutral-500"> |
| 2533 | {S.ontology.importNothing} |
| 2534 | </p> |
| 2535 | ) : ( |
| 2536 | <> |
| 2537 | {/* 三条警告在计数之前:人只会读第一屏 */} |
| 2538 | <Warning |
| 2539 | show={plan.functional_relations > 0} |
| 2540 | tone="warn" |
| 2541 | title={S.ontology.warnFunctional(plan.functional_relations)} |
| 2542 | body={S.ontology.warnFunctionalBody} |
| 2543 | items={plan.relations |
| 2544 | .filter((r) => r.functional) |
| 2545 | .map((r) => r.key)} |
| 2546 | /> |
| 2547 | <Warning |
| 2548 | show={plan.classes_without_description > 0} |
| 2549 | tone="warn" |
| 2550 | title={S.ontology.warnNoDescription( |
| 2551 | plan.classes_without_description, |
| 2552 | )} |
| 2553 | body={S.ontology.warnNoDescriptionBody} |
| 2554 | items={plan.classes |
| 2555 | .filter((c) => !c.has_description) |
| 2556 | .map((c) => c.key)} |
| 2557 | /> |
| 2558 | <Warning |
| 2559 | show={takenCount(plan) > 0} |
| 2560 | tone="danger" |
| 2561 | title={S.ontology.warnKeyTaken(takenCount(plan))} |
| 2562 | body={S.ontology.warnKeyTakenBody} |
| 2563 | items={[...plan.classes, ...plan.relations, ...plan.attributes] |
| 2564 | .filter((i) => i.disposition === "key_taken") |
| 2565 | .map( |
| 2566 | (i) => |
| 2567 | `${i.key} — ${S.ontology.importTakenBy(i.conflict_with ?? null)}`, |
| 2568 | )} |
| 2569 | /> |
| 2570 | |
| 2571 | <div className="mt-3 grid gap-2"> |
| 2572 | <PlanRow |
| 2573 | label={S.ontology.importClasses} |
| 2574 | items={plan.classes} |
| 2575 | /> |
| 2576 | <PlanRow |
| 2577 | label={S.ontology.importRelations} |
| 2578 | items={plan.relations} |
| 2579 | /> |
| 2580 | <PlanRow |
| 2581 | label={S.ontology.importAttributes} |
| 2582 | items={plan.attributes} |
| 2583 | note={ |
| 2584 | plan.attributes.length > 0 |
| 2585 | ? S.ontology.importAttributesLater |
| 2586 | : undefined |
| 2587 | } |
| 2588 | /> |
| 2589 | </div> |
| 2590 | |
| 2591 | {plan.unprojected.length > 0 && ( |
| 2592 | <details className="mt-3"> |
| 2593 | <summary className="cursor-pointer text-xs text-neutral-500 hover:text-neutral-300"> |
| 2594 | {S.ontology.importUnprojected} ({plan.unprojected.length}) |
| 2595 | </summary> |
| 2596 | <p className="mt-1.5 text-[11px] text-neutral-600"> |
| 2597 | {S.ontology.importUnprojectedBody} |
| 2598 | </p> |
| 2599 | <ul className="mt-1.5 space-y-0.5"> |
| 2600 | {plan.unprojected.map(([iri, n]) => ( |
| 2601 | <li key={iri} className="flex gap-2 text-[11px]"> |
| 2602 | <span |
| 2603 | className="font-mono text-neutral-500 truncate" |
| 2604 | title={iri} |
| 2605 | > |
| 2606 | {shortIri(iri)} |
| 2607 | </span> |
| 2608 | <span className="u-num text-neutral-600 shrink-0"> |
| 2609 | ×{n} |
| 2610 | </span> |
| 2611 | </li> |
| 2612 | ))} |
| 2613 | </ul> |
| 2614 | </details> |
| 2615 | )} |
| 2616 | |
| 2617 | <div className="mt-4 flex items-center gap-2"> |
| 2618 | <Button |
| 2619 | size="sm" |
| 2620 | disabled={busy} |
| 2621 | onClick={() => file && apply.mutate(file)} |
| 2622 | > |
| 2623 | {apply.isPending |
| 2624 | ? S.ontology.importApplying |
| 2625 | : S.ontology.importApply} |
| 2626 | </Button> |
| 2627 | <Button |
| 2628 | size="sm" |
| 2629 | variant="ghost" |
| 2630 | disabled={busy} |
| 2631 | onClick={() => { |
| 2632 | setFile(null); |
| 2633 | preview.reset(); |
| 2634 | }} |
| 2635 | > |
| 2636 | {S.ontology.importCancel} |
| 2637 | </Button> |
| 2638 | </div> |
| 2639 | </> |
| 2640 | )} |
| 2641 | </div> |
| 2642 | )} |
| 2643 | |
| 2644 | {/* 导入历史:谁在什么时候拿哪个文件动过本体。原文按 sha256 存着 */} |
| 2645 | <div className="mt-5 border-t border-white/10 pt-3"> |
| 2646 | <h4 className="text-xs font-medium text-neutral-400 mb-2"> |
| 2647 | {S.ontology.importHistory} |
| 2648 | </h4> |
| 2649 | {!history.data?.imports.length ? ( |
| 2650 | <p className="text-xs text-neutral-600"> |
| 2651 | {S.ontology.importNoHistory} |
| 2652 | </p> |
| 2653 | ) : ( |
| 2654 | <ul className="space-y-1.5"> |
| 2655 | {history.data.imports.map((im) => ( |
| 2656 | <li key={im.id} className="flex items-baseline gap-2 text-xs"> |
| 2657 | <span className="font-mono text-neutral-300 truncate"> |
| 2658 | {im.filename} |
| 2659 | </span> |
| 2660 | <span className="u-num text-neutral-600 shrink-0"> |
| 2661 | {S.ontology.importSize(im.byte_size)} |
| 2662 | </span> |
| 2663 | <span className="ml-auto text-[11px] text-neutral-600 shrink-0"> |
| 2664 | {S.ontology.importBy( |
| 2665 | im.imported_by_name ?? "—", |
| 2666 | new Date(im.imported_at).toLocaleDateString(), |
| 2667 | )} |
| 2668 | </span> |
| 2669 | </li> |
| 2670 | ))} |
| 2671 | </ul> |
| 2672 | )} |
| 2673 | </div> |
| 2674 | </div> |
| 2675 | ); |
| 2676 | } |
| 2677 | |
| 2678 | function takenCount(p: ImportPlan) { |
| 2679 | return [...p.classes, ...p.relations, ...p.attributes].filter( |
| 2680 | (i) => i.disposition === "key_taken", |
| 2681 | ).length; |
| 2682 | } |
| 2683 | |
| 2684 | /** IRI 尾巴才是人认得出的部分,前缀在列表里只占宽度 */ |
| 2685 | function shortIri(iri: string) { |
| 2686 | const i = Math.max(iri.lastIndexOf("#"), iri.lastIndexOf("/")); |
| 2687 | return i < 0 ? iri : iri.slice(i + 1); |
| 2688 | } |
| 2689 | |
| 2690 | /** 一条警告:标题给数,正文一句给后果,条目折在 details 里 */ |
| 2691 | function Warning({ |
| 2692 | show, |
| 2693 | tone, |
| 2694 | title, |
| 2695 | body, |
| 2696 | items, |
| 2697 | }: { |
| 2698 | show: boolean; |
| 2699 | tone: "warn" | "danger"; |
| 2700 | title: string; |
| 2701 | body: string; |
| 2702 | items: string[]; |
| 2703 | }) { |
| 2704 | if (!show) return null; |
| 2705 | return ( |
| 2706 | <div |
| 2707 | className={cn( |
| 2708 | "mt-3 rounded-lg border px-3 py-2.5", |
| 2709 | tone === "danger" |
| 2710 | ? "border-rose-500/25 bg-rose-500/[0.06]" |
| 2711 | : "border-amber-500/25 bg-amber-500/[0.06]", |
| 2712 | )} |
| 2713 | > |
| 2714 | <p className="text-xs text-neutral-200">{title}</p> |
| 2715 | <p className="mt-0.5 text-[11px] text-neutral-400">{body}</p> |
| 2716 | {items.length > 0 && ( |
| 2717 | <details className="mt-1.5"> |
| 2718 | <summary className="cursor-pointer text-[11px] text-neutral-500 hover:text-neutral-300"> |
| 2719 | {items.length > 1 ? `${items.length} items` : "1 item"} |
| 2720 | </summary> |
| 2721 | <ul className="mt-1 space-y-0.5"> |
| 2722 | {items.map((it) => ( |
| 2723 | <li key={it} className="font-mono text-[11px] text-neutral-400"> |
| 2724 | {it} |
| 2725 | </li> |
| 2726 | ))} |
| 2727 | </ul> |
| 2728 | </details> |
| 2729 | )} |
| 2730 | </div> |
| 2731 | ); |
| 2732 | } |
| 2733 | |
| 2734 | /** 一段的去向计数:新建 / 更新 / 跳过,零的不显示 */ |
| 2735 | function PlanRow({ |
| 2736 | label, |
| 2737 | items, |
| 2738 | note, |
| 2739 | }: { |
| 2740 | label: string; |
| 2741 | items: PlannedItem[]; |
| 2742 | note?: string; |
| 2743 | }) { |
| 2744 | if (items.length === 0) return null; |
| 2745 | const n = (d: PlannedItem["disposition"]) => |
| 2746 | items.filter((i) => i.disposition === d).length; |
| 2747 | return ( |
| 2748 | <div className="rounded-lg bg-white/[0.03] px-3 py-2"> |
| 2749 | <div className="flex items-center gap-2"> |
| 2750 | <span className="text-xs text-neutral-300">{label}</span> |
| 2751 | <span className="ml-auto flex items-center gap-1.5"> |
| 2752 | {n("create") > 0 && ( |
| 2753 | <Chip tone="success"> |
| 2754 | {S.ontology.importWillCreate(n("create"))} |
| 2755 | </Chip> |
| 2756 | )} |
| 2757 | {n("update") > 0 && ( |
| 2758 | <Chip tone="info">{S.ontology.importWillUpdate(n("update"))}</Chip> |
| 2759 | )} |
| 2760 | {n("key_taken") > 0 && ( |
| 2761 | <Chip tone="neutral"> |
| 2762 | {S.ontology.importKeyTaken(n("key_taken"))} |
| 2763 | </Chip> |
| 2764 | )} |
| 2765 | </span> |
| 2766 | </div> |
| 2767 | {note && <p className="mt-1 text-[11px] text-neutral-600">{note}</p>} |
| 2768 | </div> |
| 2769 | ); |
| 2770 | } |