import { useCallback, useEffect, useState } from 'react' import { useOptionalAuth } from './auth' import { fetchProductionBindingOptions, fetchProductionBindings, saveProductionBindings } from './productionBindings' import type { ProductionBindings } from './productionBindings' import type { KnowledgeBaseOption } from './routingBindings' // 「生产知识库绑定」:这里同时涉及两条独立路径,页面必须分开写清楚: // 1) 整库发布目标(封存/同步/恢复真正写入的库与端点)——来自部署配置,只读; // 2) 文档级绑定(旧流程:单篇文档发布写哪个库)——可在这里编辑。 // 把 2 写成"发布写入此库"会让操作者以为整库发布会写进 0b217e8a 这类 2000+ 文档库。 export interface ProductionBindingsViewProps { bindings: ProductionBindings | null options: KnowledgeBaseOption[] internalDraft: string externalDraft: string isAdmin: boolean loading: boolean saving: boolean error: string | null message: string dirty: boolean sameSelection: boolean diagnosticsOnly?: boolean onInternalChange: (next: string) => void onExternalChange: (next: string) => void onSave: () => void onReload: () => void } export function ProductionBindingsView(props: ProductionBindingsViewProps) { const { bindings, options, internalDraft, externalDraft, isAdmin, loading, saving, error, message, dirty, sameSelection, diagnosticsOnly = false, onInternalChange, onExternalChange, onSave, onReload, } = props const optionLabel = (base: KnowledgeBaseOption) => `${base.name}(${base.knowledge_count} 文档)` const renderSide = (label: string, boundID: string, boundName: string) => (
{label} {boundID ? boundName || `${boundID.slice(0, 8)}…` : '未绑定 · 按同 ID 语义发布(目标端须存在同 ID 库)'} {boundID ? '文档级绑定 · 只影响单篇文档发布,不改整库目标' : '未绑定:沿用测试库 ID'}
) const whole = bindings?.whole_target const wholeRows: [string, string][] = whole ? [ ['对内目标知识库', whole.internal_kb_id || '未配置'], ['对外目标知识库', whole.external_kb_id || '未配置'], ['目标数据库', whole.database || '—'], ['检索索引', whole.elastic_index || '—'], ['对象存储桶', whole.object_bucket || '—'], ['租户', whole.tenant_id ? String(whole.tenant_id) : '—'], ['配置来源', whole.source || '—'], ] : [] const editorFor = (value: string, onChange: (next: string) => void, placeholder: string) => options.length > 0 ? ( ) : ( onChange(event.target.value.trim())} placeholder={`${placeholder}(输入生产知识库 ID)`} aria-label={placeholder} /> ) return (

生产知识库绑定

这里有两件不同的事,页面分开显示:整库发布目标(封存/同步/恢复真正写入的目标, 由部署配置决定,只读)与文档级绑定(旧流程:单篇文档发布写哪个库,可在这里改)。

{error ? (

{/api key file/i.test(error) ? '生产 API 读取凭据未配置:无法在此列出生产知识库。整库同步走独立执行器通道,不受这条读取凭据影响。' : error}

) : null} {bindings ? (

整库发布目标(实际写入,只读)

{whole?.configured === false ? (

整库目标配置不全:{whole.missing_reason}。整库封存/同步/恢复在补齐部署配置前会被拒绝, 页面不会用下面的文档级绑定顶上。

) : null} {wholeRows.length ? (
{wholeRows.map(([label, value]) => (
{label}
{value}
))}
) : null}

这份目标来自部署环境,不在页面上编辑;改它需要改部署配置并重启 api/worker,不会由本页保存。

) : null} {bindings ? (
{bindings.legacy_scope || '文档级发布(单篇文档写生产)· 旧路径'} · 展开编辑;整库封存/同步/恢复不读取这组配置
{renderSide('对内文档级目标', bindings.internal_kb_id, bindings.internal_kb_name)} {renderSide('对外文档级目标', bindings.external_kb_id, bindings.external_kb_name)}
) : null} {bindings && !bindings.production_connected ? (

生产端核对不可用: {/api key file/i.test(bindings.production_message) ? '生产 API 读取凭据未配置(不影响整库执行器;整库同步走独立数据库/索引/存储通道)' : bindings.production_message} 。{bindings.production_base_url ? `(生产地址:${bindings.production_base_url})` : ''}

) : null} {bindings && bindings.production_connected && bindings.production_base_url ? (

生产端:{bindings.production_base_url} {bindings.updated_by ? ` · 最近修改:${bindings.updated_by}` : ''}

) : null} {!diagnosticsOnly && isAdmin && bindings ? (

保存时只改文档级绑定;整库发布目标由部署配置决定,本页不改它。

保存时会向生产端核对所选的库是否真实存在;对不上会拒收,生产端不可达则只提示。

{sameSelection ?

对内与对外必须绑定不同的生产知识库

: null} {message ? (

{message}

) : null}
) : null} {!diagnosticsOnly && !isAdmin ?

仅管理员可修改文档级发布目标。

: null}
) } // 容器:读取 + 保存。整库目标只读展示,保存动作只写文档级绑定。 export function ProductionBindingsSection({ diagnosticsOnly = false }: { diagnosticsOnly?: boolean } = {}) { const auth = useOptionalAuth() const isAdmin = auth?.identity?.role === 'admin' const [bindings, setBindings] = useState(null) const [options, setOptions] = useState([]) const [internalDraft, setInternalDraft] = useState('') const [externalDraft, setExternalDraft] = useState('') const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const [message, setMessage] = useState('') const load = useCallback(async () => { setLoading(true) setError(null) try { const next = await fetchProductionBindings() setBindings(next) setInternalDraft(next.internal_kb_id) setExternalDraft(next.external_kb_id) try { setOptions(await fetchProductionBindingOptions()) } catch { setOptions([]) } } catch (cause) { setError(cause instanceof Error ? cause.message : '读取生产知识库绑定失败') } finally { setLoading(false) } }, []) useEffect(() => { void load() }, [load]) const dirty = bindings !== null && (internalDraft !== bindings.internal_kb_id || externalDraft !== bindings.external_kb_id) const sameSelection = internalDraft !== '' && internalDraft === externalDraft const save = async () => { setSaving(true) setError(null) setMessage('') try { const next = await saveProductionBindings({ internal_kb_id: internalDraft, external_kb_id: externalDraft }) setBindings(next) const where = `对内 ${next.internal_kb_name || next.internal_kb_id.slice(0, 8) || '未绑定'} · 对外 ${next.external_kb_name || next.external_kb_id.slice(0, 8) || '未绑定'}` setMessage( next.production_connected ? `文档级发布目标已更新:${where}。只影响单篇文档发布;整库发布写的是上面那份「整库发布目标」。` : `文档级发布目标已保存:${where};但当前核对不了生产端(${next.production_message}),下次发布前请确认。` ) } catch (cause) { setError(cause instanceof Error ? cause.message : '保存生产知识库绑定失败') } finally { setSaving(false) } } return ( void save()} onReload={() => void load()} /> ) }