Files
knowledge-governance/web/src/QAModelSettings.tsx
T
姚章浩 533db535f1
build / build (push) Successful in 1m12s
feat(qaops): 接入真实模型补缺任务与总表看板
按已确认需求提供独立安全模型配置、真实探测、正式字典字段评估与原问题译中,持久批次支持只补空白、暂停恢复、有限重试、幂等和逐字段依据。覆盖固定规则、本地语言、组织关联与主表有效看板共用治理侧读模型,保留源生产和冻结历史。

验证:完整 make verify 退出0,702项Go与329项前端通过;默认118项外部/隔离集成明确跳过,另跑完整镜像及组织隔离PG竞态。12并发、输入漂移、过期租约、暂停/恢复及有限重试通过。用户授权的百炼 deepseek-v4.1-flash 完成真实后台与浏览器评估/翻译;552行、16列、11块看板及完整PNG、独立配置页测试/保存、重开恢复和零JS异常通过。仅非业务样本外发,自动处理关闭,原payload不变。

上线前治理库已做导出快照原生备份并真实恢复,60表行数与17个QA表内容哈希一致;不修改DobotAI生产或隧道。本提交时尚未部署,线上验收须独立完成。
2026-10-19 14:29:49 +08:00

185 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from 'react'
import type { AISettings } from './qaAITypes'
import type { AISettingsInput } from './qaAI'
import { fetchAISettings, saveAISettings, testAISettings } from './qaAI'
export function qaAIKeyEntryAllowed(location: { protocol: string; hostname: string }) {
return location.protocol === 'https:' || ['localhost', '127.0.0.1', '[::1]', '::1'].includes(location.hostname)
}
const verificationLabels: Record<AISettings['verification'], string> = {
not_configured: '未配置可用密钥',
verified: '已通过真实连接测试',
unverified: '未完成核验,未启用调用',
rejected: '服务已拒绝,调用已阻断',
}
export function QAModelSettings({ initial }: { initial?: AISettings }) {
const [settings, setSettings] = useState(initial ?? null),
[base, setBase] = useState(initial?.base_url ?? ''),
[model, setModel] = useState(initial?.model ?? '')
const [keyAction, setKeyAction] = useState<AISettingsInput['key_action']>(
initial?.key_configured ? 'keep' : 'replace'
),
[key, setKey] = useState('')
const [busy, setBusy] = useState(false),
[error, setError] = useState<string | null>(null),
[message, setMessage] = useState<string | null>(null)
const secure = typeof window !== 'undefined' && qaAIKeyEntryAllowed(window.location)
useEffect(() => {
if (initial) return
const controller = new AbortController()
void fetchAISettings(controller.signal)
.then((value) => {
setSettings(value)
setBase(value.base_url)
setModel(value.model)
setKeyAction(value.key_configured ? 'keep' : 'replace')
})
.catch((reason) => {
if (!controller.signal.aborted) setError(reason instanceof Error ? reason.message : '模型配置读取失败')
})
return () => controller.abort()
}, [initial])
const dirty = () => {
setMessage(null)
setError(null)
}
const run = async (test: boolean) => {
if (!settings) return
setBusy(true)
setMessage(null)
setError(null)
const input: AISettingsInput = {
base_revision: settings.revision,
base_url: base,
model,
key_action: keyAction,
...(keyAction === 'replace' ? { api_key: key } : {}),
}
try {
const result = await (test ? testAISettings(input) : saveAISettings(input))
if (!test) {
setSettings(result)
setKey('')
setKeyAction(result.key_configured ? 'keep' : 'replace')
}
setMessage(
(test ? '连接测试:' : '配置已保存:') +
verificationLabels[result.verification] +
(test ? ';未保存、未启动任务' : ';保存不会启动全量评估')
)
} catch (reason) {
setError(reason instanceof Error ? reason.message : '模型配置操作失败')
} finally {
setBusy(false)
}
}
const ready =
Boolean(settings) &&
!busy &&
(keyAction !== 'replace' || (secure && Boolean(key) && Boolean(settings?.storage_ready)))
return (
<section className="qa-ai-model-settings" aria-label="问答评估模型">
<h3>问答评估模型</h3>
<p className="qa-ops-muted">
四字段分析和中文问题翻译共用此独立配置。保存不会启动全量评估,也不会自动开启新增处理。
</p>
{settings ? (
<>
<p>
{verificationLabels[settings.verification]};密钥:{settings.key_configured ? '已配置(不回显)' : '未配置'}
;自动分析/翻译:{settings.automatic_evaluation ? '开' : '关'} /{' '}
{settings.automatic_translation ? '开' : '关'}
</p>
<div className="safe-setting-grid">
<label>
API 基础地址
<input
value={base}
onChange={(event) => {
setBase(event.target.value)
dirty()
}}
placeholder="https://模型服务地址/v1"
autoComplete="off"
disabled={busy}
/>
<small>OpenAI 兼容基础地址,不含 /chat/completions;更换地址须重新录入密钥。</small>
</label>
<label>
模型名称
<input
value={model}
onChange={(event) => {
setModel(event.target.value)
dirty()
}}
placeholder="服务商提供的模型标识"
autoComplete="off"
disabled={busy}
/>
</label>
<label>
密钥操作
<select
value={keyAction}
onChange={(event) => {
setKeyAction(event.target.value as AISettingsInput['key_action'])
setKey('')
dirty()
}}
disabled={busy}
>
<option value="keep">保留现有密钥</option>
<option value="replace">更换密钥</option>
<option value="clear">清除密钥并停用AI</option>
</select>
</label>
<label>
API Key
<input
type="password"
name="qa_ai_key"
value={key}
onChange={(event) => {
setKey(event.target.value)
dirty()
}}
autoComplete="new-password"
spellCheck={false}
placeholder={settings.key_configured ? '已配置;保留时不必填写' : '仅用于此治理平台'}
disabled={busy || keyAction !== 'replace' || !secure || !settings.storage_ready}
/>
</label>
</div>
{!secure ? (
<p className="qa-ops-warning">
当前不是安全密钥录入连接。请使用有效HTTPS或本机 localhost
地址录入新密钥;不会通过失配证书或普通局域网HTTP发送新密钥。
</p>
) : null}
{!settings.storage_ready ? (
<p className="qa-ops-warning">
独立安全存储未就绪,请配置服务器的 QA_AI_MASTER_KEY_FILE;不会读取 Hermes 密钥。
</p>
) : null}
<div className="qa-ai-actions">
<button type="button" disabled={!ready || keyAction === 'clear'} onClick={() => void run(true)}>
{busy ? '正在真实核验…' : '测试连接'}
</button>
<button type="button" disabled={!ready} onClick={() => void run(false)}>
保存配置
</button>
</div>
</>
) : !error ? (
<p role="status">读取独立模型配置…</p>
) : null}
{message ? <p role="status">{message}</p> : null}
{error ? (
<p className="error-banner" role="alert">
{error}
</p>
) : null}
</section>
)
}