| 1 | import { useEffect, useState } from "react"; |
| 2 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 3 | import { useNavigate, useSearch } from "@tanstack/react-router"; |
| 4 | import { Lock, Plus, X } from "lucide-react"; |
| 5 | import { api } from "../api"; |
| 6 | import { LANG_NAMES, S } from "../i18n"; |
| 7 | import { useKb } from "../kb"; |
| 8 | import { toast } from "../toast"; |
| 9 | import { SearchSelect } from "../ui"; |
| 10 | import { Members } from "./Members"; |
| 11 | |
| 12 | /** 一个源授权给了哪些工作区(0014)。 |
| 13 | * |
| 14 | * **授权与挂载是两层**:这里说「这个源可以给谁用」,KB 管理员再在授权过的 |
| 15 | * 集合里挑挂不挂。从前没有这一层——可挂载列表返回全部署每一个源,于是任何 |
| 16 | * 库的管理员都能把任意生产库挂进自己库。 |
| 17 | * |
| 18 | * 两层都是多对多:一个源可授权给多个工作区,一个工作区可拿到多个源。 */ |
| 19 | function SourceGrants({ sourceId }: { sourceId: string }) { |
| 20 | const queryClient = useQueryClient(); |
| 21 | const [picked, setPicked] = useState(""); |
| 22 | const [notice, setNotice] = useState<string | null>(null); |
| 23 | |
| 24 | const grants = useQuery({ |
| 25 | queryKey: ["dataSourceGrants", sourceId], |
| 26 | queryFn: () => api.dataSourceGrants(sourceId), |
| 27 | }); |
| 28 | const workspaces = useQuery({ |
| 29 | queryKey: ["workspaces"], |
| 30 | queryFn: api.workspaces, |
| 31 | }); |
| 32 | const invalidate = () => |
| 33 | queryClient.invalidateQueries({ queryKey: ["dataSourceGrants", sourceId] }); |
| 34 | |
| 35 | const grant = useMutation({ |
| 36 | mutationFn: (wsId: string) => api.grantDataSource(sourceId, wsId), |
| 37 | onSuccess: () => { |
| 38 | setPicked(""); |
| 39 | setNotice(null); |
| 40 | invalidate(); |
| 41 | }, |
| 42 | onError: (e: unknown) => toast.error((e as Error).message), |
| 43 | }); |
| 44 | const revoke = useMutation({ |
| 45 | mutationFn: (wsId: string) => api.revokeDataSource(sourceId, wsId), |
| 46 | // 卸了几个要说出来:收回授权会顺带断掉正在用的挂载, |
| 47 | // 悄悄断比断本身更糟 |
| 48 | onSuccess: (r) => { |
| 49 | setNotice(S.settings.datasources.grantRevoked(r.unmounted)); |
| 50 | invalidate(); |
| 51 | }, |
| 52 | onError: (e: unknown) => toast.error((e as Error).message), |
| 53 | }); |
| 54 | |
| 55 | const granted = grants.data?.workspaces ?? []; |
| 56 | const grantedIds = new Set(granted.map((w) => w.id)); |
| 57 | const grantable = (workspaces.data ?? []).filter( |
| 58 | (w) => !grantedIds.has(w.id), |
| 59 | ); |
| 60 | |
| 61 | return ( |
| 62 | <div className="border-t border-white/5 pt-3 space-y-2"> |
| 63 | <div className="flex items-baseline gap-2"> |
| 64 | <span className="text-[11px] text-neutral-500"> |
| 65 | {S.settings.datasources.grants} |
| 66 | </span> |
| 67 | {granted.length === 0 ? ( |
| 68 | <span className="text-[11px] text-neutral-600"> |
| 69 | {S.settings.datasources.grantsNone} |
| 70 | </span> |
| 71 | ) : ( |
| 72 | <div className="flex flex-wrap gap-1.5"> |
| 73 | {granted.map((w) => ( |
| 74 | <span |
| 75 | key={w.id} |
| 76 | className="u-chip u-chip-neutral text-[11px] flex items-center gap-1" |
| 77 | > |
| 78 | {w.name} |
| 79 | <button |
| 80 | className="text-neutral-500 hover:text-[var(--u-danger)]" |
| 81 | title={S.settings.datasources.grantRevoke} |
| 82 | disabled={revoke.isPending} |
| 83 | onClick={() => revoke.mutate(w.id)} |
| 84 | > |
| 85 | <X size={10} /> |
| 86 | </button> |
| 87 | </span> |
| 88 | ))} |
| 89 | </div> |
| 90 | )} |
| 91 | </div> |
| 92 | {grantable.length > 0 && ( |
| 93 | <div className="flex items-center gap-2"> |
| 94 | <SearchSelect |
| 95 | className="flex-1" |
| 96 | value={picked} |
| 97 | options={grantable.map((w) => ({ value: w.id, label: w.name }))} |
| 98 | onChange={(v) => { |
| 99 | setPicked(v); |
| 100 | if (v) grant.mutate(v); |
| 101 | }} |
| 102 | placeholder={S.settings.datasources.grantAdd} |
| 103 | /> |
| 104 | </div> |
| 105 | )} |
| 106 | {notice && <p className="text-[11px] text-neutral-500">{notice}</p>} |
| 107 | </div> |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /** 部署级配置:注册开关 + worker 并发。 */ |
| 112 | function DeploymentAdmin() { |
| 113 | const queryClient = useQueryClient(); |
| 114 | const dep = useQuery({ |
| 115 | queryKey: ["deployment"], |
| 116 | queryFn: api.adminDeployment, |
| 117 | }); |
| 118 | const [workers, setWorkers] = useState<number | null>(null); |
| 119 | const shown = workers ?? dep.data?.worker_concurrency ?? 32; |
| 120 | // 按模型的并发:缺省值 + 每个在用模型的覆盖 |
| 121 | const [modelDefault, setModelDefault] = useState<number | null>(null); |
| 122 | const shownDefault = |
| 123 | modelDefault ?? dep.data?.default_model_concurrency ?? 10; |
| 124 | const [perModel, setPerModel] = useState<Record<string, number>>({}); |
| 125 | const save = useMutation({ |
| 126 | mutationFn: (v: { |
| 127 | open: boolean; |
| 128 | workers?: number; |
| 129 | defaultModel?: number; |
| 130 | modelLimit?: { |
| 131 | base_url: string; |
| 132 | model: string; |
| 133 | max_concurrent: number | null; |
| 134 | }; |
| 135 | ontologyLang?: "en" | "zh"; |
| 136 | }) => |
| 137 | api.saveAdminDeployment( |
| 138 | v.open, |
| 139 | v.workers, |
| 140 | v.defaultModel, |
| 141 | v.modelLimit, |
| 142 | v.ontologyLang, |
| 143 | ), |
| 144 | onSuccess: () => { |
| 145 | setPerModel({}); |
| 146 | queryClient.invalidateQueries({ queryKey: ["deployment"] }); |
| 147 | }, |
| 148 | }); |
| 149 | const open = dep.data?.open_registration ?? true; |
| 150 | |
| 151 | return ( |
| 152 | <div className="glass rounded-xl p-4 space-y-4"> |
| 153 | <label className="flex items-start gap-3 cursor-pointer"> |
| 154 | <input |
| 155 | type="checkbox" |
| 156 | className="mt-0.5" |
| 157 | checked={open} |
| 158 | disabled={dep.isPending || save.isPending} |
| 159 | onChange={(e) => save.mutate({ open: e.target.checked })} |
| 160 | /> |
| 161 | <span> |
| 162 | <span className="block text-sm text-neutral-200"> |
| 163 | {S.settings.deployment.openReg} |
| 164 | </span> |
| 165 | <span className="block text-xs text-neutral-500 mt-0.5"> |
| 166 | {S.settings.deployment.openRegHint} |
| 167 | </span> |
| 168 | </span> |
| 169 | </label> |
| 170 | |
| 171 | {/* 新建库的本体语言。**不是界面语言**——界面语言是每个人自己在账户菜单里选的, |
| 172 | 根本不经过后端(docs/decisions/0004)。说明里必须把这句讲出来 */} |
| 173 | <div className="flex items-start justify-between gap-4 border-t border-white/10 pt-4"> |
| 174 | <div className="min-w-0"> |
| 175 | <span className="block text-sm text-neutral-200"> |
| 176 | {S.settings.deployment.ontologyLang} |
| 177 | </span> |
| 178 | <span className="block text-xs text-neutral-500 mt-0.5"> |
| 179 | {S.settings.deployment.ontologyLangHint} |
| 180 | </span> |
| 181 | </div> |
| 182 | <div className="flex gap-1 rounded-lg bg-white/5 p-1 h-fit shrink-0"> |
| 183 | {(["en", "zh"] as const).map((l) => ( |
| 184 | <button |
| 185 | key={l} |
| 186 | disabled={dep.isPending || save.isPending} |
| 187 | onClick={() => save.mutate({ open, ontologyLang: l })} |
| 188 | className={`rounded-md px-3 py-1 text-[12px] font-medium transition-colors ${ |
| 189 | (dep.data?.default_ontology_lang ?? "en") === l |
| 190 | ? "bg-white/10 text-neutral-100" |
| 191 | : "text-neutral-500 hover:text-neutral-300" |
| 192 | }`} |
| 193 | > |
| 194 | {LANG_NAMES[l]} |
| 195 | </button> |
| 196 | ))} |
| 197 | </div> |
| 198 | </div> |
| 199 | |
| 200 | <div className="flex items-start justify-between gap-4 border-t border-white/10 pt-4"> |
| 201 | <div className="min-w-0"> |
| 202 | <span className="block text-sm text-neutral-200"> |
| 203 | {S.settings.deployment.workers} |
| 204 | </span> |
| 205 | <span className="block text-xs text-neutral-500 mt-0.5"> |
| 206 | {S.settings.deployment.workersHint} |
| 207 | </span> |
| 208 | </div> |
| 209 | <div className="flex items-center gap-2 shrink-0"> |
| 210 | <input |
| 211 | type="number" |
| 212 | min={1} |
| 213 | max={32} |
| 214 | className="input-dark u-input-plain w-16 px-2 py-1.5 text-sm u-num text-center" |
| 215 | value={shown} |
| 216 | disabled={dep.isPending} |
| 217 | onChange={(e) => |
| 218 | setWorkers(Math.max(1, Math.min(32, Number(e.target.value) || 1))) |
| 219 | } |
| 220 | /> |
| 221 | <button |
| 222 | className="u-btn u-btn-ghost px-3 py-1.5 text-xs" |
| 223 | disabled={ |
| 224 | save.isPending || |
| 225 | workers === null || |
| 226 | workers === dep.data?.worker_concurrency |
| 227 | } |
| 228 | onClick={() => save.mutate({ open, workers: shown })} |
| 229 | > |
| 230 | {S.settings.deployment.workersApply} |
| 231 | </button> |
| 232 | </div> |
| 233 | </div> |
| 234 | {/* 按模型的并发才是真正的节流:约束来自供应商的速率限制,而那是按模型算的。 |
| 235 | 上面那个 worker 并发只是外层兜底,防任务无限堆积 */} |
| 236 | <div className="border-t border-white/10 pt-4"> |
| 237 | <div className="flex items-start justify-between gap-4"> |
| 238 | <div className="min-w-0"> |
| 239 | <span className="block text-sm text-neutral-200"> |
| 240 | {S.settings.deployment.modelConcurrency} |
| 241 | </span> |
| 242 | <span className="block text-xs text-neutral-500 mt-0.5"> |
| 243 | {S.settings.deployment.modelConcurrencyHint} |
| 244 | </span> |
| 245 | </div> |
| 246 | <div className="flex items-center gap-2 shrink-0"> |
| 247 | <span className="text-xs text-neutral-500"> |
| 248 | {S.settings.deployment.modelDefault} |
| 249 | </span> |
| 250 | <input |
| 251 | type="number" |
| 252 | min={1} |
| 253 | max={256} |
| 254 | className="input-dark u-input-plain w-16 px-2 py-1.5 text-sm u-num text-center" |
| 255 | value={shownDefault} |
| 256 | disabled={dep.isPending} |
| 257 | onChange={(e) => |
| 258 | setModelDefault( |
| 259 | Math.max(1, Math.min(256, Number(e.target.value) || 1)), |
| 260 | ) |
| 261 | } |
| 262 | /> |
| 263 | <button |
| 264 | className="u-btn u-btn-ghost px-3 py-1.5 text-xs" |
| 265 | disabled={ |
| 266 | save.isPending || |
| 267 | modelDefault === null || |
| 268 | modelDefault === dep.data?.default_model_concurrency |
| 269 | } |
| 270 | onClick={() => save.mutate({ open, defaultModel: shownDefault })} |
| 271 | > |
| 272 | {S.settings.deployment.workersApply} |
| 273 | </button> |
| 274 | </div> |
| 275 | </div> |
| 276 | |
| 277 | {!!dep.data?.models_in_use?.length && ( |
| 278 | <div className="mt-3 space-y-1.5"> |
| 279 | {dep.data.models_in_use.map((m) => { |
| 280 | const cur = |
| 281 | dep.data?.model_limits?.find( |
| 282 | (l) => l.base_url === m.base_url && l.model === m.model, |
| 283 | )?.max_concurrent ?? null; |
| 284 | const key = `${m.base_url}|${m.model}`; |
| 285 | const val = perModel[key] ?? cur ?? shownDefault; |
| 286 | return ( |
| 287 | <div key={key} className="flex items-center gap-2 text-xs"> |
| 288 | <span className="u-chip u-chip-neutral !text-[10px] !px-1.5 shrink-0"> |
| 289 | {m.kind} |
| 290 | </span> |
| 291 | <span className="font-mono text-neutral-300 truncate"> |
| 292 | {m.model} |
| 293 | </span> |
| 294 | <span className="text-neutral-600 truncate hidden sm:inline"> |
| 295 | {m.base_url} |
| 296 | </span> |
| 297 | <input |
| 298 | type="number" |
| 299 | min={1} |
| 300 | max={256} |
| 301 | className="input-dark u-input-plain ml-auto w-14 px-1.5 py-1 text-xs u-num text-center shrink-0" |
| 302 | value={val} |
| 303 | onChange={(e) => |
| 304 | setPerModel({ |
| 305 | ...perModel, |
| 306 | [key]: Math.max( |
| 307 | 1, |
| 308 | Math.min(256, Number(e.target.value) || 1), |
| 309 | ), |
| 310 | }) |
| 311 | } |
| 312 | /> |
| 313 | <button |
| 314 | className="u-btn u-btn-ghost px-2 py-1 text-[11px] shrink-0" |
| 315 | disabled={save.isPending || perModel[key] === undefined} |
| 316 | onClick={() => |
| 317 | save.mutate({ |
| 318 | open, |
| 319 | modelLimit: { |
| 320 | base_url: m.base_url, |
| 321 | model: m.model, |
| 322 | max_concurrent: val, |
| 323 | }, |
| 324 | }) |
| 325 | } |
| 326 | > |
| 327 | {S.settings.deployment.workersApply} |
| 328 | </button> |
| 329 | {cur !== null && ( |
| 330 | <button |
| 331 | className="u-btn u-btn-ghost px-2 py-1 text-[11px] shrink-0 text-neutral-500" |
| 332 | disabled={save.isPending} |
| 333 | title={S.settings.deployment.modelResetHint} |
| 334 | onClick={() => |
| 335 | save.mutate({ |
| 336 | open, |
| 337 | modelLimit: { |
| 338 | base_url: m.base_url, |
| 339 | model: m.model, |
| 340 | max_concurrent: null, |
| 341 | }, |
| 342 | }) |
| 343 | } |
| 344 | > |
| 345 | {S.settings.deployment.modelReset} |
| 346 | </button> |
| 347 | )} |
| 348 | </div> |
| 349 | ); |
| 350 | })} |
| 351 | </div> |
| 352 | )} |
| 353 | </div> |
| 354 | |
| 355 | {save.isError && ( |
| 356 | <p className="text-xs text-rose-400">{(save.error as Error).message}</p> |
| 357 | )} |
| 358 | </div> |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | /** 知识库管理(部署层):全部库总览 + 新建(建库是管理动作,切换器只切换)。 */ |
| 363 | function KbsAdmin() { |
| 364 | const navigate = useNavigate(); |
| 365 | const queryClient = useQueryClient(); |
| 366 | const { workspace, setKb } = useKb(); |
| 367 | const [creating, setCreating] = useState(false); |
| 368 | const list = useQuery({ |
| 369 | queryKey: ["myKbs", workspace?.id], |
| 370 | queryFn: () => api.myKbs(workspace!.id), |
| 371 | enabled: !!workspace, |
| 372 | }); |
| 373 | const rows = list.data?.kbs ?? []; |
| 374 | |
| 375 | return ( |
| 376 | <div className="space-y-4"> |
| 377 | <p className="text-xs text-neutral-500">{S.settings.kbs.hint}</p> |
| 378 | |
| 379 | <div className="glass rounded-xl divide-y divide-white/5"> |
| 380 | {rows.map(({ kb, doc_count, member_count }) => ( |
| 381 | <div key={kb.id} className="px-4 py-3 flex items-center gap-3"> |
| 382 | <div className="min-w-0 flex-1"> |
| 383 | <div className="flex items-center gap-2"> |
| 384 | <span className="text-sm text-neutral-200 truncate"> |
| 385 | {kb.name} |
| 386 | </span> |
| 387 | {kb.is_default && ( |
| 388 | <span className="u-chip u-chip-neutral !text-[10px]"> |
| 389 | {S.settings.kbs.defaultChip} |
| 390 | </span> |
| 391 | )} |
| 392 | {kb.visibility === "restricted" && ( |
| 393 | <span className="flex items-center gap-1 text-[10.5px] text-neutral-500"> |
| 394 | <Lock size={10} /> |
| 395 | {S.settings.kbs.visRestricted} |
| 396 | </span> |
| 397 | )} |
| 398 | </div> |
| 399 | <div className="mt-0.5 text-xs text-neutral-500"> |
| 400 | <span className="u-num"> |
| 401 | {S.account.kbStats(doc_count, member_count)} |
| 402 | </span> |
| 403 | </div> |
| 404 | </div> |
| 405 | <button |
| 406 | className="u-btn u-btn-ghost px-2.5 py-1 text-xs shrink-0" |
| 407 | onClick={() => { |
| 408 | setKb(kb.id); |
| 409 | navigate({ to: "/kb/$kbId/settings", params: { kbId: kb.id } }); |
| 410 | }} |
| 411 | > |
| 412 | {S.settings.kbs.openSettings} |
| 413 | </button> |
| 414 | </div> |
| 415 | ))} |
| 416 | {!list.isPending && rows.length === 0 && ( |
| 417 | <p className="px-4 py-6 text-sm text-neutral-500">—</p> |
| 418 | )} |
| 419 | </div> |
| 420 | |
| 421 | <button |
| 422 | onClick={() => setCreating(true)} |
| 423 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs flex items-center gap-1.5" |
| 424 | > |
| 425 | <Plus size={12} /> |
| 426 | {S.settings.kbs.newKb} |
| 427 | </button> |
| 428 | |
| 429 | {creating && workspace && ( |
| 430 | <NewKbModal |
| 431 | workspaceId={workspace.id} |
| 432 | onDone={(id) => { |
| 433 | setCreating(false); |
| 434 | queryClient.invalidateQueries({ |
| 435 | queryKey: ["myKbs", workspace.id], |
| 436 | }); |
| 437 | queryClient.invalidateQueries({ queryKey: ["kbs", workspace.id] }); |
| 438 | // 建完直达库设置:下一步几乎总是邀人/配置 |
| 439 | if (id) { |
| 440 | setKb(id); |
| 441 | navigate({ to: "/kb/$kbId/settings", params: { kbId: id } }); |
| 442 | } |
| 443 | }} |
| 444 | /> |
| 445 | )} |
| 446 | </div> |
| 447 | ); |
| 448 | } |
| 449 | |
| 450 | /** 新建知识库弹窗(管理员):缺省 restricted,不污染全员切换器。 */ |
| 451 | function NewKbModal({ |
| 452 | workspaceId, |
| 453 | onDone, |
| 454 | }: { |
| 455 | workspaceId: string; |
| 456 | onDone: (id?: string) => void; |
| 457 | }) { |
| 458 | const [name, setName] = useState(""); |
| 459 | const [desc, setDesc] = useState(""); |
| 460 | const [restricted, setRestricted] = useState(true); |
| 461 | // schema.org 默认勾选,可反选(0009)。删掉内置类之后不选任何包的库是真的空, |
| 462 | // 而空库仍然能用——但绝大多数人要的是一个已经能认出人、组织、产品的起点。 |
| 463 | // 一秒装完(0008 的批量插入),所以默认装得起 |
| 464 | const [packs, setPacks] = useState<string[]>(["schema-org"]); |
| 465 | |
| 466 | const available = useQuery({ |
| 467 | queryKey: ["ontologyPacks"], |
| 468 | queryFn: api.ontologyPacks, |
| 469 | }); |
| 470 | |
| 471 | // 勾选顺序即安装顺序:第一个包的类会认领同名的种子类, |
| 472 | // 后面的撞名才查得到对齐表。所以取消再勾会排到末尾——这是对的 |
| 473 | const toggle = (id: string) => |
| 474 | setPacks((prev) => |
| 475 | prev.includes(id) ? prev.filter((p) => p !== id) : [...prev, id], |
| 476 | ); |
| 477 | |
| 478 | const create = useMutation({ |
| 479 | mutationFn: () => |
| 480 | api.createKb(workspaceId, { |
| 481 | name: name.trim(), |
| 482 | description: desc.trim() || null, |
| 483 | visibility: restricted ? "restricted" : "open", |
| 484 | ontology_packs: packs, |
| 485 | }), |
| 486 | onSuccess: (kb) => onDone(kb.id), |
| 487 | }); |
| 488 | |
| 489 | return ( |
| 490 | <div |
| 491 | className="fixed inset-0 z-50 grid place-items-center bg-black/60 backdrop-blur-sm" |
| 492 | onMouseDown={(e) => { |
| 493 | if (e.target === e.currentTarget) onDone(); |
| 494 | }} |
| 495 | > |
| 496 | <div className="glass-strong w-[24rem] max-w-[calc(100vw-2rem)] rounded-2xl shadow-2xl p-5"> |
| 497 | <div className="flex items-center justify-between mb-4"> |
| 498 | <h2 className="u-title text-[15px]">{S.settings.kbs.newKb}</h2> |
| 499 | <button |
| 500 | onClick={() => onDone()} |
| 501 | className="text-neutral-500 hover:text-neutral-200" |
| 502 | > |
| 503 | <X size={15} /> |
| 504 | </button> |
| 505 | </div> |
| 506 | <input |
| 507 | autoFocus |
| 508 | className="input-dark w-full px-3 py-2 text-sm mb-2" |
| 509 | placeholder={S.settings.kbs.name} |
| 510 | value={name} |
| 511 | onChange={(e) => setName(e.target.value)} |
| 512 | /> |
| 513 | <input |
| 514 | className="input-dark w-full px-3 py-2 text-sm mb-3" |
| 515 | placeholder={S.settings.kbs.description} |
| 516 | value={desc} |
| 517 | onChange={(e) => setDesc(e.target.value)} |
| 518 | /> |
| 519 | <label className="flex items-center gap-2 text-xs text-neutral-400 mb-4"> |
| 520 | <input |
| 521 | type="checkbox" |
| 522 | checked={restricted} |
| 523 | onChange={(e) => setRestricted(e.target.checked)} |
| 524 | /> |
| 525 | {S.settings.kbs.visRestricted} |
| 526 | </label> |
| 527 | |
| 528 | <div className="mb-4"> |
| 529 | <div className="text-xs text-neutral-300 mb-1"> |
| 530 | {S.settings.kbs.packsLabel} |
| 531 | </div> |
| 532 | <p className="text-[11px] leading-relaxed text-neutral-500 mb-2"> |
| 533 | {S.settings.kbs.packsHint} |
| 534 | </p> |
| 535 | <div className="grid grid-cols-2 gap-1.5"> |
| 536 | {available.data?.packs.map((p) => { |
| 537 | const on = packs.includes(p.id); |
| 538 | return ( |
| 539 | <label |
| 540 | key={p.id} |
| 541 | // 选中态靠边框与底色,勾选框藏起来:五个并排时 |
| 542 | // 一排勾选框比内容本身还抢眼 |
| 543 | className={ |
| 544 | "cursor-pointer rounded-lg border px-2.5 py-2 transition-colors " + |
| 545 | (on |
| 546 | ? "border-white/25 bg-white/[0.07]" |
| 547 | : "border-white/10 hover:bg-white/5") |
| 548 | } |
| 549 | > |
| 550 | <input |
| 551 | type="checkbox" |
| 552 | className="sr-only" |
| 553 | checked={on} |
| 554 | onChange={() => toggle(p.id)} |
| 555 | /> |
| 556 | <span className="block text-xs text-neutral-200"> |
| 557 | {p.name} |
| 558 | </span> |
| 559 | <span className="block text-[11px] leading-snug text-neutral-500"> |
| 560 | {p.summary} |
| 561 | </span> |
| 562 | <span className="mt-0.5 block text-[10px] text-neutral-600"> |
| 563 | {S.settings.kbs.packsCount(p.classes, p.properties)} |
| 564 | </span> |
| 565 | </label> |
| 566 | ); |
| 567 | })} |
| 568 | </div> |
| 569 | {packs.length === 0 && ( |
| 570 | <p className="text-[11px] text-neutral-500 mt-1.5"> |
| 571 | {S.settings.kbs.packsNone} |
| 572 | </p> |
| 573 | )} |
| 574 | </div> |
| 575 | {create.isError && ( |
| 576 | <p className="text-xs text-rose-400 mb-2"> |
| 577 | {(create.error as Error).message} |
| 578 | </p> |
| 579 | )} |
| 580 | <div className="flex justify-end gap-2"> |
| 581 | <button |
| 582 | className="u-btn u-btn-ghost px-3.5 py-1.5 text-xs" |
| 583 | onClick={() => onDone()} |
| 584 | > |
| 585 | {S.library.cancel} |
| 586 | </button> |
| 587 | <button |
| 588 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs" |
| 589 | disabled={!name.trim() || create.isPending} |
| 590 | onClick={() => create.mutate()} |
| 591 | > |
| 592 | {S.settings.kbs.create} |
| 593 | </button> |
| 594 | </div> |
| 595 | </div> |
| 596 | </div> |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | /** 系统层数据源注册(问数):凭据只进不出,列表只显示 host:port/db 摘要。 */ |
| 601 | function DataSourcesAdmin() { |
| 602 | const queryClient = useQueryClient(); |
| 603 | const list = useQuery({ |
| 604 | queryKey: ["dataSources"], |
| 605 | queryFn: api.adminDataSources, |
| 606 | }); |
| 607 | const [name, setName] = useState(""); |
| 608 | const [conn, setConn] = useState(""); |
| 609 | const invalidate = () => |
| 610 | queryClient.invalidateQueries({ queryKey: ["dataSources"] }); |
| 611 | |
| 612 | const create = useMutation({ |
| 613 | mutationFn: () => |
| 614 | api.adminCreateDataSource({ |
| 615 | name: name.trim(), |
| 616 | conn_string: conn.trim(), |
| 617 | }), |
| 618 | onSuccess: () => { |
| 619 | setName(""); |
| 620 | setConn(""); |
| 621 | invalidate(); |
| 622 | }, |
| 623 | }); |
| 624 | const remove = useMutation({ |
| 625 | mutationFn: (id: string) => api.adminDeleteDataSource(id), |
| 626 | onSettled: invalidate, |
| 627 | }); |
| 628 | const test = useMutation({ |
| 629 | mutationFn: (id: string) => api.adminTestDataSource(id), |
| 630 | onSettled: invalidate, |
| 631 | }); |
| 632 | |
| 633 | return ( |
| 634 | <div className="space-y-4"> |
| 635 | <p className="text-xs text-neutral-500">{S.settings.datasources.hint}</p> |
| 636 | |
| 637 | <div className="glass rounded-xl divide-y divide-white/5"> |
| 638 | {(list.data?.data_sources ?? []).map((d) => ( |
| 639 | <div key={d.id} className="px-4 py-3 space-y-3"> |
| 640 | <div className="flex items-center gap-3"> |
| 641 | <div className="min-w-0 flex-1"> |
| 642 | <div className="text-sm text-neutral-200"> |
| 643 | {d.name} |
| 644 | <span className="ml-2 text-[11px] uppercase tracking-wide text-neutral-500"> |
| 645 | {d.engine} |
| 646 | </span> |
| 647 | </div> |
| 648 | <div className="text-xs text-neutral-500 font-mono truncate"> |
| 649 | {d.summary} |
| 650 | </div> |
| 651 | </div> |
| 652 | <span |
| 653 | className={`u-chip shrink-0 ${ |
| 654 | d.last_test_ok === true |
| 655 | ? "u-chip-success" |
| 656 | : d.last_test_ok === false |
| 657 | ? "u-chip-danger" |
| 658 | : "u-chip-neutral" |
| 659 | }`} |
| 660 | > |
| 661 | {d.last_test_ok === true |
| 662 | ? S.settings.datasources.testOk |
| 663 | : d.last_test_ok === false |
| 664 | ? S.settings.datasources.testFail |
| 665 | : S.settings.datasources.neverTested} |
| 666 | </span> |
| 667 | <button |
| 668 | className="u-btn u-btn-ghost px-2.5 py-1 text-xs shrink-0" |
| 669 | disabled={test.isPending} |
| 670 | onClick={() => test.mutate(d.id)} |
| 671 | > |
| 672 | {S.settings.datasources.test} |
| 673 | </button> |
| 674 | <button |
| 675 | className="text-xs text-neutral-500 hover:text-[var(--u-danger)] shrink-0" |
| 676 | disabled={remove.isPending} |
| 677 | onClick={() => remove.mutate(d.id)} |
| 678 | > |
| 679 | {S.settings.datasources.remove} |
| 680 | </button> |
| 681 | </div> |
| 682 | <SourceGrants sourceId={d.id} /> |
| 683 | </div> |
| 684 | ))} |
| 685 | {list.data?.data_sources.length === 0 && ( |
| 686 | <p className="px-4 py-6 text-sm text-neutral-500">—</p> |
| 687 | )} |
| 688 | </div> |
| 689 | |
| 690 | <div className="glass rounded-xl p-4 space-y-2"> |
| 691 | <input |
| 692 | className="input-dark w-full px-3 py-2 text-sm" |
| 693 | placeholder={S.settings.datasources.name} |
| 694 | value={name} |
| 695 | onChange={(e) => setName(e.target.value)} |
| 696 | /> |
| 697 | <input |
| 698 | className="input-dark w-full px-3 py-2 text-sm font-mono u-placeholder-sans" |
| 699 | placeholder={S.settings.datasources.connString} |
| 700 | value={conn} |
| 701 | onChange={(e) => setConn(e.target.value)} |
| 702 | /> |
| 703 | <p className="text-[11px] leading-5 text-neutral-500 font-mono whitespace-pre-line"> |
| 704 | {S.settings.datasources.connSchemes} |
| 705 | </p> |
| 706 | <div className="flex items-center gap-3"> |
| 707 | <button |
| 708 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs" |
| 709 | disabled={!name.trim() || !conn.trim() || create.isPending} |
| 710 | onClick={() => create.mutate()} |
| 711 | > |
| 712 | {S.settings.datasources.add} |
| 713 | </button> |
| 714 | {create.isError && ( |
| 715 | <span className="text-xs text-rose-400"> |
| 716 | {(create.error as Error).message} |
| 717 | </span> |
| 718 | )} |
| 719 | </div> |
| 720 | </div> |
| 721 | </div> |
| 722 | ); |
| 723 | } |
| 724 | |
| 725 | const PRESETS: Record< |
| 726 | string, |
| 727 | { chat: string; embed: string; chatModel: string; embedModel: string } |
| 728 | > = { |
| 729 | DeepSeek: { |
| 730 | chat: "https://api.deepseek.com/v1", |
| 731 | chatModel: "deepseek-chat", |
| 732 | embed: "", |
| 733 | embedModel: "", |
| 734 | }, |
| 735 | SiliconFlow: { |
| 736 | chat: "https://api.siliconflow.cn/v1", |
| 737 | chatModel: "deepseek-ai/DeepSeek-V3", |
| 738 | embed: "https://api.siliconflow.cn/v1", |
| 739 | embedModel: "BAAI/bge-m3", |
| 740 | }, |
| 741 | "Qwen (DashScope)": { |
| 742 | chat: "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 743 | chatModel: "qwen-plus", |
| 744 | embed: "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 745 | embedModel: "text-embedding-v3", |
| 746 | }, |
| 747 | Ollama: { |
| 748 | chat: "http://localhost:11434/v1", |
| 749 | chatModel: "qwen2.5:7b", |
| 750 | embed: "http://localhost:11434/v1", |
| 751 | embedModel: "bge-m3", |
| 752 | }, |
| 753 | OpenAI: { |
| 754 | chat: "https://api.openai.com/v1", |
| 755 | chatModel: "gpt-4o-mini", |
| 756 | embed: "https://api.openai.com/v1", |
| 757 | embedModel: "text-embedding-3-small", |
| 758 | }, |
| 759 | }; |
| 760 | |
| 761 | export function Settings() { |
| 762 | const { workspace } = useKb(); |
| 763 | const { tab: tabParam } = useSearch({ from: "/account/admin" }); |
| 764 | const [tab, setTab] = useState< |
| 765 | "models" | "members" | "kbs" | "datasources" | "deployment" |
| 766 | >(tabParam ?? "models"); |
| 767 | const queryClient = useQueryClient(); |
| 768 | const settings = useQuery({ |
| 769 | queryKey: ["settings", workspace?.id], |
| 770 | queryFn: () => api.settings(workspace!.id), |
| 771 | enabled: !!workspace, |
| 772 | }); |
| 773 | |
| 774 | const [form, setForm] = useState({ |
| 775 | chat_base_url: "", |
| 776 | chat_api_key: "", |
| 777 | chat_model: "", |
| 778 | embed_base_url: "", |
| 779 | embed_api_key: "", |
| 780 | embed_model: "", |
| 781 | }); |
| 782 | |
| 783 | useEffect(() => { |
| 784 | if (settings.data) { |
| 785 | setForm((f) => ({ |
| 786 | ...f, |
| 787 | chat_base_url: settings.data.chat_base_url ?? "", |
| 788 | chat_model: settings.data.chat_model ?? "", |
| 789 | embed_base_url: settings.data.embed_base_url ?? "", |
| 790 | embed_model: settings.data.embed_model ?? "", |
| 791 | })); |
| 792 | } |
| 793 | }, [settings.data]); |
| 794 | |
| 795 | const save = useMutation({ |
| 796 | mutationFn: () => api.saveSettings(workspace!.id, form), |
| 797 | onSuccess: () => |
| 798 | queryClient.invalidateQueries({ queryKey: ["settings", workspace?.id] }), |
| 799 | }); |
| 800 | |
| 801 | const test = useMutation({ |
| 802 | mutationFn: () => api.testSettings(workspace!.id), |
| 803 | }); |
| 804 | |
| 805 | if (!workspace) |
| 806 | return <div className="p-8 text-sm text-neutral-500">{S.nav.loading}</div>; |
| 807 | |
| 808 | const set = |
| 809 | (k: keyof typeof form) => (e: React.ChangeEvent<HTMLInputElement>) => |
| 810 | setForm({ ...form, [k]: e.target.value }); |
| 811 | |
| 812 | const input = "input-dark w-full px-3 py-2 text-sm"; |
| 813 | const label = "block text-xs font-medium text-neutral-400 mb-1"; |
| 814 | |
| 815 | return ( |
| 816 | <div className="h-full overflow-y-auto p-6"> |
| 817 | <div className="max-w-xl"> |
| 818 | <h2 className="text-lg font-bold text-neutral-100 mb-3"> |
| 819 | {S.settings.title} |
| 820 | </h2> |
| 821 | <div className="flex gap-1 mb-5 bg-white/5 rounded-lg p-1 w-fit"> |
| 822 | {( |
| 823 | [ |
| 824 | ["models", S.settings.tabModels], |
| 825 | ["members", S.settings.tabMembers], |
| 826 | ["kbs", S.settings.tabKbs], |
| 827 | ["datasources", S.settings.datasources.tab], |
| 828 | ["deployment", S.settings.tabDeployment], |
| 829 | ] as const |
| 830 | ).map(([key, tabLabel]) => ( |
| 831 | <button |
| 832 | key={key} |
| 833 | onClick={() => setTab(key)} |
| 834 | className={`rounded-md px-4 py-1.5 text-sm font-medium transition-colors ${ |
| 835 | tab === key |
| 836 | ? "bg-white/10 text-neutral-100" |
| 837 | : "text-neutral-500 hover:text-neutral-300" |
| 838 | }`} |
| 839 | > |
| 840 | {tabLabel} |
| 841 | </button> |
| 842 | ))} |
| 843 | </div> |
| 844 | |
| 845 | {tab === "members" && <Members workspaceId={workspace.id} />} |
| 846 | {tab === "kbs" && <KbsAdmin />} |
| 847 | {tab === "datasources" && <DataSourcesAdmin />} |
| 848 | {tab === "deployment" && <DeploymentAdmin />} |
| 849 | |
| 850 | {tab === "models" && ( |
| 851 | <> |
| 852 | <p className="text-sm text-neutral-400 mb-4"> |
| 853 | {S.settings.modelsIntro} |
| 854 | </p> |
| 855 | |
| 856 | <div className="mb-5 flex flex-wrap gap-2"> |
| 857 | {Object.entries(PRESETS).map(([name, p]) => ( |
| 858 | <button |
| 859 | key={name} |
| 860 | onClick={() => |
| 861 | setForm({ |
| 862 | ...form, |
| 863 | chat_base_url: p.chat, |
| 864 | chat_model: p.chatModel, |
| 865 | embed_base_url: p.embed, |
| 866 | embed_model: p.embedModel, |
| 867 | }) |
| 868 | } |
| 869 | className="rounded-full border border-white/15 bg-white/5 px-3 py-1 text-xs text-neutral-300 hover:border-[rgba(var(--u-accent-deep),0.5)] hover:text-[var(--u-accent)]" |
| 870 | > |
| 871 | {name} |
| 872 | </button> |
| 873 | ))} |
| 874 | </div> |
| 875 | |
| 876 | <div className="glass rounded-xl p-5 space-y-4"> |
| 877 | <h3 className="text-sm font-bold text-neutral-200"> |
| 878 | {S.settings.chatModel} |
| 879 | </h3> |
| 880 | <div> |
| 881 | <label className={label}>{S.settings.baseUrl}</label> |
| 882 | <input |
| 883 | className={input} |
| 884 | placeholder="https://api.deepseek.com/v1" |
| 885 | value={form.chat_base_url} |
| 886 | onChange={set("chat_base_url")} |
| 887 | /> |
| 888 | </div> |
| 889 | <div className="grid grid-cols-2 gap-3"> |
| 890 | <div> |
| 891 | <label className={label}>{S.settings.model}</label> |
| 892 | <input |
| 893 | className={input} |
| 894 | placeholder="deepseek-chat" |
| 895 | value={form.chat_model} |
| 896 | onChange={set("chat_model")} |
| 897 | /> |
| 898 | </div> |
| 899 | <div> |
| 900 | <label className={label}> |
| 901 | {S.settings.apiKey}{" "} |
| 902 | {settings.data?.has_chat_key && ( |
| 903 | <span className="text-[var(--u-accent)]"> |
| 904 | {S.settings.keyConfigured} |
| 905 | </span> |
| 906 | )} |
| 907 | </label> |
| 908 | <input |
| 909 | className={input} |
| 910 | type="password" |
| 911 | placeholder="sk-…" |
| 912 | value={form.chat_api_key} |
| 913 | onChange={set("chat_api_key")} |
| 914 | /> |
| 915 | </div> |
| 916 | </div> |
| 917 | |
| 918 | <h3 className="text-sm font-bold text-neutral-200 pt-2"> |
| 919 | {S.settings.embedModel} |
| 920 | </h3> |
| 921 | <div> |
| 922 | <label className={label}>{S.settings.baseUrl}</label> |
| 923 | <input |
| 924 | className={input} |
| 925 | placeholder="http://localhost:11434/v1" |
| 926 | value={form.embed_base_url} |
| 927 | onChange={set("embed_base_url")} |
| 928 | /> |
| 929 | </div> |
| 930 | <div className="grid grid-cols-2 gap-3"> |
| 931 | <div> |
| 932 | <label className={label}>{S.settings.model}</label> |
| 933 | <input |
| 934 | className={input} |
| 935 | placeholder="bge-m3" |
| 936 | value={form.embed_model} |
| 937 | onChange={set("embed_model")} |
| 938 | /> |
| 939 | </div> |
| 940 | <div> |
| 941 | <label className={label}> |
| 942 | {S.settings.apiKey}{" "} |
| 943 | {settings.data?.has_embed_key && ( |
| 944 | <span className="text-[var(--u-accent)]"> |
| 945 | {S.settings.keyConfigured} |
| 946 | </span> |
| 947 | )} |
| 948 | </label> |
| 949 | <input |
| 950 | className={input} |
| 951 | type="password" |
| 952 | value={form.embed_api_key} |
| 953 | onChange={set("embed_api_key")} |
| 954 | /> |
| 955 | </div> |
| 956 | </div> |
| 957 | |
| 958 | <div className="flex gap-2 pt-2"> |
| 959 | <button |
| 960 | onClick={() => save.mutate()} |
| 961 | disabled={save.isPending} |
| 962 | className="u-btn u-btn-primary px-4 py-2 text-sm" |
| 963 | > |
| 964 | {save.isPending ? S.settings.saving : S.settings.save} |
| 965 | </button> |
| 966 | <button |
| 967 | onClick={() => test.mutate()} |
| 968 | disabled={test.isPending} |
| 969 | className="u-btn u-btn-ghost px-4 py-2 text-sm" |
| 970 | > |
| 971 | {test.isPending ? S.settings.testing : S.settings.test} |
| 972 | </button> |
| 973 | </div> |
| 974 | |
| 975 | {save.isSuccess && ( |
| 976 | <p className="text-sm text-[var(--u-accent)]"> |
| 977 | {S.settings.saved} |
| 978 | </p> |
| 979 | )} |
| 980 | {save.isError && ( |
| 981 | <p className="text-sm text-rose-400"> |
| 982 | {(save.error as Error).message} |
| 983 | </p> |
| 984 | )} |
| 985 | {test.data && ( |
| 986 | <div className="text-sm space-y-1 pt-1"> |
| 987 | <p |
| 988 | className={ |
| 989 | test.data.chat.ok |
| 990 | ? "text-[var(--u-accent)]" |
| 991 | : "text-rose-400" |
| 992 | } |
| 993 | > |
| 994 | {S.settings.chatLabel}:{" "} |
| 995 | {test.data.chat.ok |
| 996 | ? S.settings.ok(test.data.chat.reply ?? "OK") |
| 997 | : test.data.chat.error} |
| 998 | </p> |
| 999 | <p |
| 1000 | className={ |
| 1001 | test.data.embed.ok |
| 1002 | ? "text-[var(--u-accent)]" |
| 1003 | : "text-neutral-400" |
| 1004 | } |
| 1005 | > |
| 1006 | {S.settings.embedLabel}:{" "} |
| 1007 | {test.data.embed.ok |
| 1008 | ? S.settings.okDim(test.data.embed.dim ?? 0) |
| 1009 | : test.data.embed.error} |
| 1010 | </p> |
| 1011 | </div> |
| 1012 | )} |
| 1013 | </div> |
| 1014 | </> |
| 1015 | )} |
| 1016 | </div> |
| 1017 | </div> |
| 1018 | ); |
| 1019 | } |