import { useCallback, useEffect, useState } from 'react' import { PasswordInput } from './PasswordInput' import { useOptionalAuth } from './auth' import { fetchWeKnoraConnections, keyLabel, saveWeKnoraConnection, testWeKnoraConnection, verificationLabel, } from './weknoraConnections' import type { WeKnoraConnection, WeKnoraEnvironment } from './weknoraConnections' interface Props { environment: WeKnoraEnvironment title: string placeholder: string onSaved?: () => void } // 连接卡:地址 + 写-only 密钥 + 真实列库测试。它只改「连哪台实例、用哪把钥匙」, // 不碰绑定,也不触发任何同步或发布。 export function WeKnoraConnections({ environment, title, placeholder, onSaved }: Props) { const auth = useOptionalAuth() const isAdmin = auth?.identity?.role === 'admin' const [connection, setConnection] = useState(null) const [endpointDraft, setEndpointDraft] = useState('') const [keyDraft, setKeyDraft] = useState('') const [testing, setTesting] = useState(false) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const [message, setMessage] = useState('') const [testedEndpoint, setTestedEndpoint] = useState('') const load = useCallback(async () => { try { const all = await fetchWeKnoraConnections() const found = all.find((item) => item.environment === environment) ?? null setConnection(found) setEndpointDraft(found?.endpoint ?? '') } catch (cause) { setError(cause instanceof Error ? cause.message : '读取连接失败') } }, [environment]) useEffect(() => { void load() }, [load]) const endpoint = endpointDraft.trim() const keyEntered = keyDraft.trim() !== '' const endpointChanged = connection !== null && endpoint !== connection.endpoint const canTest = endpoint !== '' && !testing && !saving const tested = testedEndpoint !== '' && testedEndpoint === endpoint const canSave = endpoint !== '' && !saving && !testing && (keyEntered || endpointChanged ? tested : true) const test = async () => { setTesting(true) setError(null) setMessage('') try { const result = await testWeKnoraConnection(environment, { endpoint, ...(keyEntered ? { api_key: keyDraft.trim(), key_action: 'replace' as const } : { key_action: 'keep' as const }), }) setMessage(result.message) setTestedEndpoint(result.outcome === 'ok' ? endpoint : '') if (result.outcome !== 'ok') setError(result.message) } catch (cause) { setError(cause instanceof Error ? cause.message : '测试连接失败') } finally { setTesting(false) } } const save = async (action: 'keep' | 'replace' | 'clear') => { setSaving(true) setError(null) try { const next = await saveWeKnoraConnection(environment, { endpoint, ...(action === 'replace' ? { api_key: keyDraft.trim() } : {}), key_action: action, base_revision: connection?.revision ?? 0, }) setConnection(next) setEndpointDraft(next.endpoint) setKeyDraft('') setTestedEndpoint('') setMessage(`已保存:${keyLabel(next)};最近一次测试:${verificationLabel(next)}。保存未触发任何同步或发布。`) onSaved?.() } catch (cause) { setError(cause instanceof Error ? cause.message : '保存连接失败') } finally { setSaving(false) } } const clearKey = () => { if (!window.confirm('清除后该侧将没有任何可用密钥,发布与列库都会失败。确定清除?')) return void save('clear') } const saveAction = keyEntered ? 'replace' : 'keep' return (
{title} 连接:{verificationLabel(connection)} {connection?.endpoint ? `地址:${connection.endpoint} · 密钥:${keyLabel(connection)}` : '还没有配置地址与密钥'}
{connection && !connection.storage_ready ? (

密钥存储不可用(主密钥文件缺失或权限不是 0600):可以读取当前状态,但保存会被拒绝。

) : null} {isAdmin ? (

「测试连接」会真实请求该实例的知识库列表,并区分地址不可达 / 密钥无效 / 无权限 / 成功;换了地址或密钥必须先测通才能保存。撤销绑定请把下方目标库留空。

{connection?.key_configured ? ( ) : null}
{error ? (

{error}

) : null} {message ? (

{message}

) : null}
) : (

仅管理员可修改连接与密钥;当前密钥只显示尾 4 位,任何接口都不会回读明文。

)}
) }