8b900a9ffb
连接(地址 + 写-only 密钥)从文件引用搬进治理库:AES-GCM 密文 + 独立 0600 主密钥,
页面与接口只回显尾 4 位;测试连接真实列库并区分 地址不可达/密钥无效/无权限/成功,
换地址或换密钥必须测通才能保存(迁移 000083)。
可达性判定收敛为 weknora.HTTPAllowed,客户端与监控探活共用;新增
WEKNORA_ALLOW_PRIVATE_HTTP 只放行 RFC1918/回环/链路本地。生产绑定只保留下拉,
保存后回报冻结/未冻结条数,已冻结目标不被改写。
(cherry picked from commit ad02e8bb9c)
184 lines
6.6 KiB
TypeScript
184 lines
6.6 KiB
TypeScript
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<WeKnoraConnection | null>(null)
|
||
const [endpointDraft, setEndpointDraft] = useState('')
|
||
const [keyDraft, setKeyDraft] = useState('')
|
||
const [testing, setTesting] = useState(false)
|
||
const [saving, setSaving] = useState(false)
|
||
const [error, setError] = useState<string | null>(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 (
|
||
<div className="weknora-connection-card" id={`settings-weknora-connection-${environment}`}>
|
||
<div className="kb-binding-grid">
|
||
<div className={`kb-binding-side ${connection?.verification === 'ok' ? 'bound' : 'warn'}`}>
|
||
<b>{title}</b>
|
||
<span>连接:{verificationLabel(connection)}</span>
|
||
<small>
|
||
{connection?.endpoint
|
||
? `地址:${connection.endpoint} · 密钥:${keyLabel(connection)}`
|
||
: '还没有配置地址与密钥'}
|
||
</small>
|
||
</div>
|
||
</div>
|
||
{connection && !connection.storage_ready ? (
|
||
<p className="hint">密钥存储不可用(主密钥文件缺失或权限不是 0600):可以读取当前状态,但保存会被拒绝。</p>
|
||
) : null}
|
||
{isAdmin ? (
|
||
<div className="kb-binding-editor">
|
||
<label>
|
||
{title}地址
|
||
<input
|
||
value={endpointDraft}
|
||
onChange={(event) => {
|
||
setEndpointDraft(event.target.value)
|
||
setTestedEndpoint('')
|
||
}}
|
||
placeholder={placeholder}
|
||
aria-label={`${title}地址`}
|
||
/>
|
||
</label>
|
||
<label>
|
||
API Key
|
||
<PasswordInput
|
||
autoComplete="new-password"
|
||
value={keyDraft}
|
||
onChange={(event) => {
|
||
setKeyDraft(event.target.value)
|
||
setTestedEndpoint('')
|
||
}}
|
||
placeholder={connection?.key_configured ? '留空 = 保留原密钥' : '粘贴 API Key'}
|
||
aria-label={`${title} API Key`}
|
||
/>
|
||
</label>
|
||
<p className="hint">
|
||
「测试连接」会真实请求该实例的知识库列表,并区分地址不可达 / 密钥无效 / 无权限 /
|
||
成功;换了地址或密钥必须先测通才能保存。撤销绑定请把下方目标库留空。
|
||
</p>
|
||
<div className="kb-binding-actions">
|
||
<button onClick={() => void test()} disabled={!canTest}>
|
||
{testing ? '正在测试…' : '测试连接'}
|
||
</button>
|
||
<button onClick={() => void save(saveAction)} disabled={!canSave}>
|
||
{saving ? '正在保存…' : '保存连接'}
|
||
</button>
|
||
{connection?.key_configured ? (
|
||
<button className="danger" onClick={clearKey} disabled={saving || testing}>
|
||
清除密钥
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
{error ? (
|
||
<p className="error-message" role="alert">
|
||
{error}
|
||
</p>
|
||
) : null}
|
||
{message ? (
|
||
<p className="success-message" role="status">
|
||
{message}
|
||
</p>
|
||
) : null}
|
||
</div>
|
||
) : (
|
||
<p className="hint">仅管理员可修改连接与密钥;当前密钥只显示尾 4 位,任何接口都不会回读明文。</p>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|