71f6311399
与新的「生产知识库绑定」(发布 源库→目标库)配对,避免两处同名分不清。 同步改分区标题、说明、错误文案与测试断言。
84 lines
5.8 KiB
TypeScript
84 lines
5.8 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
||
|
||
import { useOptionalAuth } from './auth'
|
||
import { fetchRoutingBindingOptions, fetchRoutingBindings, saveRoutingBindings } from './routingBindings'
|
||
import type { KnowledgeBaseOption, RoutingBindings } from './routingBindings'
|
||
|
||
export function RoutingBindingsSection() {
|
||
const auth = useOptionalAuth()
|
||
const isAdmin = auth?.identity?.role === 'admin'
|
||
const [bindings, setBindings] = useState<RoutingBindings | null>(null)
|
||
const [options, setOptions] = useState<KnowledgeBaseOption[]>([])
|
||
const [internalDraft, setInternalDraft] = useState('')
|
||
const [externalDraft, setExternalDraft] = useState('')
|
||
const [loading, setLoading] = useState(true)
|
||
const [saving, setSaving] = useState(false)
|
||
const [error, setError] = useState<string | null>(null)
|
||
const [message, setMessage] = useState('')
|
||
|
||
const load = useCallback(async () => {
|
||
setLoading(true); setError(null)
|
||
try {
|
||
const next = await fetchRoutingBindings()
|
||
setBindings(next)
|
||
setInternalDraft(next.internal_bound_by_ui ? next.internal_kb_id : '')
|
||
setExternalDraft(next.external_bound_by_ui ? next.external_kb_id : '')
|
||
try { setOptions(await fetchRoutingBindingOptions()) } catch { setOptions([]) }
|
||
} catch (cause) { setError(cause instanceof Error ? cause.message : '读取测试知识库绑定失败') }
|
||
finally { setLoading(false) }
|
||
}, [])
|
||
useEffect(() => { void load() }, [load])
|
||
|
||
const dirty = bindings !== null && (internalDraft !== (bindings.internal_bound_by_ui ? bindings.internal_kb_id : '') || externalDraft !== (bindings.external_bound_by_ui ? bindings.external_kb_id : ''))
|
||
const sameSelection = internalDraft !== '' && internalDraft === externalDraft
|
||
|
||
const save = async () => {
|
||
setSaving(true); setError(null); setMessage('')
|
||
try {
|
||
const next = await saveRoutingBindings({ internal_kb_id: internalDraft, external_kb_id: externalDraft })
|
||
setBindings(next)
|
||
const replan = next.replanned || next.retired_duplicates
|
||
? `;同时把 ${next.replanned} 项还没上传的待入库重新指向新库${next.retired_duplicates ? `,${next.retired_duplicates} 项因新库里已有同篇而被退休` : ''}`
|
||
: ''
|
||
setMessage(`绑定已生效:对内 ${next.internal_kb_name || next.internal_kb_id.slice(0, 8) || '未绑定'} · 对外 ${next.external_kb_name || next.external_kb_id.slice(0, 8) || '未绑定'}。此后“来源与目录”的标记按新绑定路由${replan}。`)
|
||
} catch (cause) { setError(cause instanceof Error ? cause.message : '保存测试知识库绑定失败') }
|
||
finally { setSaving(false) }
|
||
}
|
||
|
||
const optionLabel = (base: KnowledgeBaseOption) => `${base.name}(${base.knowledge_count} 文档 · ${base.id.slice(0, 8)}…)`
|
||
const renderSide = (label: string, boundID: string, boundName: string, fromEnv: boolean, boundByUI: boolean) => (
|
||
<div className={`kb-binding-side ${boundID ? 'bound' : 'warn'}`}>
|
||
<b>{label}</b>
|
||
<span>{boundID ? (boundName || `${boundID.slice(0, 8)}…`) : '未绑定 · 该方向的标记会被拒绝'}</span>
|
||
<small>{boundID ? (boundByUI ? (boundName ? `UI 绑定 · ${boundID.slice(0, 8)}…` : 'UI 绑定') : fromEnv ? '来自部署默认值(env)' : '') : '请绑定一个测试知识库'}</small>
|
||
</div>
|
||
)
|
||
const editorFor = (value: string, onChange: (next: string) => void, placeholder: string) => (
|
||
options.length > 0
|
||
? <select value={value} onChange={(event) => onChange(event.target.value)} aria-label={placeholder}>
|
||
<option value="">{placeholder}</option>
|
||
{options.map((base) => <option key={base.id} value={base.id}>{optionLabel(base)}</option>)}
|
||
</select>
|
||
: <input value={value} onChange={(event) => onChange(event.target.value.trim())} placeholder={`${placeholder}(输入知识库 ID)`} aria-label={placeholder} />
|
||
)
|
||
|
||
return <section className="settings-panel wide" id="settings-routing-bindings">
|
||
<div className="settings-section-heading"><div><h2>测试知识库绑定</h2><p>“来源与目录”里的对内 / 对外标记按这里的绑定分发到对应的测试 WeKnora 知识库;保存后立即生效,无需重启。发布写入的生产知识库见「生产知识库绑定」。</p></div><button onClick={() => void load()} disabled={loading}>{loading ? '读取中…' : '重新读取'}</button></div>
|
||
{error ? <p className="error-message" role="alert">{error}</p> : null}
|
||
{bindings ? <div className="kb-binding-grid">
|
||
{renderSide('对内知识库', bindings.internal_kb_id, bindings.internal_kb_name, bindings.internal_env_fallback, bindings.internal_bound_by_ui)}
|
||
{renderSide('对外知识库', bindings.external_kb_id, bindings.external_kb_name, bindings.external_env_fallback, bindings.external_bound_by_ui)}
|
||
</div> : null}
|
||
{bindings && !bindings.weknora_connected ? <p className="hint">当前读不到 WeKnora 的库列表,只能按 ID 填写;连接恢复后可改为下拉选择。</p> : null}
|
||
{isAdmin && bindings ? <div className="kb-binding-editor">
|
||
<label>对内绑定 {editorFor(internalDraft, setInternalDraft, '选择对内知识库')}</label>
|
||
<label>对外绑定 {editorFor(externalDraft, setExternalDraft, '选择对外知识库')}</label>
|
||
<p className="hint">留空 = 取消 UI 绑定,回退到部署默认值;对内与对外必须是不同的库。</p>
|
||
{sameSelection ? <p className="error-message">对内与对外必须绑定不同的知识库</p> : null}
|
||
<button onClick={() => void save()} disabled={saving || !dirty || sameSelection}>{saving ? '正在校验并保存…' : '保存绑定'}</button>
|
||
{message ? <p className="success-message" role="status">{message}</p> : null}
|
||
</div> : null}
|
||
{!isAdmin ? <p className="hint">仅管理员可修改测试知识库绑定。</p> : null}
|
||
</section>
|
||
}
|