feat(settings): AI 模型连接改为当前生效与独立草稿编辑
为什么:交互稿要求顶部只读已保存模型与真实核验态,下方编辑独立草稿;测试草稿 不得改写当前连接,保存一律回显服务端结果,草稿变动或配置被他人改动后旧测试失效。 原来只有一张表单和一个 busy 状态,测试与保存无法区分,清除密钥也与保存混在一起。 改了什么:QAModelSettings 拆成 saved / draft 两层;已配置密钥默认收起,仅更换或 首次录入展示输入框,换地址强制重新录入;清除并停用收进折叠区、二次确认且只提交 已保存连接;409 冲突重新拉取已保存配置并作废草稿测试。样式迁到 systemSettings.css, 删除 qaOps.css 里同名残留;合并分区与组件内重复说明。 怎么验的:npm test 119 文件 600 例全过(新增 mounted 交互回归 5 例);npm run build 通过;make verify 全量门禁 17 项 success(identity 本地 skipped)。未推送、未部署。
This commit is contained in:
@@ -1,5 +1,23 @@
|
||||
# Changelog
|
||||
|
||||
## 开发快照 0.1.401(AI 模型连接:当前生效 + 独立草稿)
|
||||
|
||||
### Changed
|
||||
|
||||
- 「系统设置 → AI 模型连接」按交互稿重排:顶部只读「当前生效」(模型 + 服务端真实核验态与地址),
|
||||
下方地址/模型是独立草稿;测试草稿只回显核验结果,不更新当前连接,「测试中」与「保存中」分开提示。
|
||||
- 保存成功一律用服务端响应回显当前信息;草稿改动或版本冲突(409)都会作废上一次草稿测试,
|
||||
冲突时载入最新已保存配置并要求按新版本重测。
|
||||
- 已配置密钥默认收起不回显,只有「更换密钥」或首次录入才出现输入框;更换基础地址强制重新录入密钥,
|
||||
取消更换无副作用。安全传输与独立密钥存储限制保持不变。
|
||||
- 「清除密钥并停用」收进单独折叠区,写明对问答分析与中文翻译的影响并二次确认;只提交已保存连接,
|
||||
未保存草稿不会被夹带,取消不产生任何请求。
|
||||
- 合并分区标题与组件内重复的说明文字,模型连接说明只保留一处。
|
||||
|
||||
### Fixed
|
||||
|
||||
- 模型连接的测试/保存不再共用同一个忙碌状态,避免把草稿测试结果误当成已生效配置。
|
||||
|
||||
## 开发快照 0.1.399(系统设置分区调整:AI 模型连接 / 知识库映射)
|
||||
|
||||
### Changed
|
||||
|
||||
@@ -127,7 +127,7 @@ make list # 查看检查清单与适用范围
|
||||
|
||||
## 问答运营模型配置
|
||||
|
||||
管理员在「系统设置 → AI 模型连接」填写 OpenAI 兼容基础地址、模型和 API Key。测试连接发送最小非业务请求;保存不会启动历史全量任务,自动分析/翻译默认关闭。密钥不回显,保留、更换和清除分别选择;录入只允许有效 HTTPS 或本机回环连接,不允许跳过证书校验。
|
||||
管理员在「系统设置 → AI 模型连接」填写 OpenAI 兼容基础地址、模型和 API Key。页面顶部显示当前已保存的模型与服务端核验结果,下方编辑的是独立草稿:测试连接只核验草稿,保存才更新当前连接,保存成功后按服务端响应回显。测试连接发送最小非业务请求;保存不会启动历史全量任务,自动分析/翻译默认关闭。密钥不回显、默认收起,只有「更换密钥」或首次录入才显示输入框,更换基础地址须重新录入密钥;「清除密钥并停用」在单独折叠区里二次确认,只作用于已保存连接。录入只允许有效 HTTPS 或本机回环连接,不允许跳过证书校验。
|
||||
|
||||
API 和普通 worker 须只读挂载同一个独立的 **32 字节、0600 普通文件**,以 `QA_AI_MASTER_KEY_FILE` 指向容器内路径。文件属主应为实际运行 UID;不要复用 Agent 会话存储或把主密钥写进 Compose/Git。治理库只保存加密后的服务密钥;恢复时必须同时恢复治理库和这份独立主密钥。未配置主密钥时如实显示安全存储未就绪。
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act } from 'react'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QAModelSettings } from './QAModelSettings'
|
||||
import type { AISettings } from './qaAITypes'
|
||||
let root: Root, host: HTMLDivElement
|
||||
type Call = { path: string; method: string; body: Record<string, unknown> | null }
|
||||
let calls: Call[]
|
||||
let responder: (call: Call) => { status: number; data?: AISettings; error?: string }
|
||||
const saved: AISettings = {
|
||||
revision: 1,
|
||||
base_url: 'https://model.example.invalid/v1',
|
||||
model: 'example-model',
|
||||
key_configured: true,
|
||||
verification: 'verified',
|
||||
checked_at: '2026-11-18T10:00:00Z',
|
||||
automatic_evaluation: true,
|
||||
automatic_translation: false,
|
||||
storage_ready: true,
|
||||
}
|
||||
const applied = (call: Call, extra: Partial<AISettings> = {}): AISettings => ({
|
||||
...saved,
|
||||
revision: saved.revision + 1,
|
||||
base_url: String(call.body?.base_url),
|
||||
model: String(call.body?.model),
|
||||
key_configured: call.body?.key_action !== 'clear',
|
||||
verification: call.body?.key_action === 'clear' ? 'not_configured' : 'verified',
|
||||
...extra,
|
||||
})
|
||||
const ok = (data: AISettings) => ({ status: 200, data })
|
||||
beforeEach(() => {
|
||||
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
|
||||
host = document.createElement('div')
|
||||
document.body.append(host)
|
||||
root = createRoot(host)
|
||||
calls = []
|
||||
responder = (call) => ok(call.method === 'GET' ? saved : applied(call))
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async (path: string, init?: RequestInit) => {
|
||||
const call: Call = {
|
||||
path,
|
||||
method: init?.method ?? 'GET',
|
||||
body: init?.body ? JSON.parse(String(init.body)) : null,
|
||||
}
|
||||
calls.push(call)
|
||||
const result = responder(call)
|
||||
return new Response(JSON.stringify(result.error ? { error: result.error } : { data: result.data }), {
|
||||
status: result.status,
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
afterEach(async () => {
|
||||
await act(async () => root.unmount())
|
||||
host.remove()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
const button = (label: string) => {
|
||||
const found = [...host.querySelectorAll('button')].find((item) => item.textContent?.trim() === label)
|
||||
expect(found, label).toBeTruthy()
|
||||
return found!
|
||||
}
|
||||
const click = async (label: string) => {
|
||||
const target = button(label)
|
||||
await act(async () => target.click())
|
||||
await act(async () => {})
|
||||
}
|
||||
const type = async (label: string, value: string) => {
|
||||
const input = [...host.querySelectorAll('label')]
|
||||
.find((item) => item.textContent?.startsWith(label))
|
||||
?.querySelector('input')
|
||||
expect(input, label).toBeTruthy()
|
||||
await act(async () => {
|
||||
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value)
|
||||
input!.dispatchEvent(new Event('input', { bubbles: true }))
|
||||
})
|
||||
}
|
||||
const current = () => host.querySelector('.qa-ai-model-current')?.textContent ?? ''
|
||||
const writes = () => calls.filter((call) => call.method === 'POST')
|
||||
const disable = async () => {
|
||||
await click('清除密钥并停用…')
|
||||
await click('确认清除并停用')
|
||||
}
|
||||
describe('AI 模型连接草稿编辑器', () => {
|
||||
it('tests the draft without touching the saved connection and echoes the server after save', async () => {
|
||||
await act(async () => root.render(<QAModelSettings />))
|
||||
expect(current()).toContain('example-model · 已通过真实连接测试')
|
||||
await type('模型名称', 'draft-model')
|
||||
await click('测试连接')
|
||||
const test = writes().at(-1)!
|
||||
expect(test.path).toBe('/api/v1/qa-ops/ai/settings/test')
|
||||
expect(test.body).toEqual({
|
||||
base_revision: 1,
|
||||
base_url: saved.base_url,
|
||||
model: 'draft-model',
|
||||
key_action: 'keep',
|
||||
})
|
||||
expect(host.textContent).toContain('草稿测试:已通过真实连接测试')
|
||||
expect(host.textContent).toContain('草稿未保存,当前生效配置未改变')
|
||||
// 测试不更新「当前生效」,也不产生保存。
|
||||
expect(current()).toContain('example-model · 已通过真实连接测试')
|
||||
expect(writes().filter((call) => call.path.endsWith('/settings'))).toHaveLength(0)
|
||||
// 再改草稿 ⇒ 上一次测试结论失效。
|
||||
await type('模型名称', 'saved-model')
|
||||
expect(host.textContent).not.toContain('草稿测试:已通过真实连接测试')
|
||||
await click('保存配置')
|
||||
const save = writes().at(-1)!
|
||||
expect(save.path).toBe('/api/v1/qa-ops/ai/settings')
|
||||
expect(save.body).toEqual({ base_revision: 1, base_url: saved.base_url, model: 'saved-model', key_action: 'keep' })
|
||||
// 保存后用服务端响应回显当前信息,而不是本地草稿转写。
|
||||
expect(current()).toContain('saved-model · 已通过真实连接测试')
|
||||
expect(host.textContent).toContain('已保存并更新当前连接:saved-model · 已通过真实连接测试')
|
||||
expect(host.textContent).toContain('草稿与已保存配置一致')
|
||||
})
|
||||
it('keeps a configured key collapsed and sends a replacement only when asked', async () => {
|
||||
await act(async () => root.render(<QAModelSettings />))
|
||||
expect(host.querySelector('input[type=password]')).toBeNull()
|
||||
await click('更换密钥')
|
||||
await type('新 API 密钥', 'test-only-replacement-key')
|
||||
await click('保存配置')
|
||||
expect(writes().at(-1)!.body).toEqual({
|
||||
base_revision: 1,
|
||||
base_url: saved.base_url,
|
||||
model: saved.model,
|
||||
key_action: 'replace',
|
||||
api_key: 'test-only-replacement-key',
|
||||
})
|
||||
expect(host.querySelector('input[type=password]')).toBeNull()
|
||||
expect(host.textContent).not.toContain('test-only-replacement-key')
|
||||
await click('更换密钥')
|
||||
await click('取消更换')
|
||||
expect(host.querySelector('input[type=password]')).toBeNull()
|
||||
expect(host.textContent).toContain('草稿与已保存配置一致')
|
||||
})
|
||||
it('requires a fresh key when the address changes and hides cancelling', async () => {
|
||||
await act(async () => root.render(<QAModelSettings />))
|
||||
await type('API 基础地址', 'https://other.example.invalid/v1')
|
||||
expect(host.querySelector('input[type=password]')).toBeTruthy()
|
||||
expect(host.textContent).toContain('已更换基础地址:须重新录入密钥')
|
||||
expect([...host.querySelectorAll('button')].some((item) => item.textContent?.trim() === '取消更换')).toBe(false)
|
||||
expect(button('保存配置').disabled).toBe(true)
|
||||
await type('新 API 密钥', 'second-secret')
|
||||
await click('保存配置')
|
||||
expect(writes().at(-1)!.body).toEqual({
|
||||
base_revision: 1,
|
||||
base_url: 'https://other.example.invalid/v1',
|
||||
model: saved.model,
|
||||
key_action: 'replace',
|
||||
api_key: 'second-secret',
|
||||
})
|
||||
})
|
||||
it('disables only the saved connection, and cancelling the confirmation has no effect', async () => {
|
||||
await act(async () => root.render(<QAModelSettings />))
|
||||
await type('模型名称', 'unsaved-draft')
|
||||
await click('清除密钥并停用…')
|
||||
await click('取消')
|
||||
expect(writes()).toHaveLength(0)
|
||||
expect(current()).toContain('example-model · 已通过真实连接测试')
|
||||
expect([...host.querySelectorAll('input')].find((item) => item.value === 'unsaved-draft')).toBeTruthy()
|
||||
await disable()
|
||||
const clear = writes().at(-1)!
|
||||
// 草稿里的 unsaved-draft 不会被夹带成停用提交。
|
||||
expect(clear.body).toEqual({
|
||||
base_revision: 1,
|
||||
base_url: saved.base_url,
|
||||
model: saved.model,
|
||||
key_action: 'clear',
|
||||
})
|
||||
expect(current()).toContain('未配置可用密钥')
|
||||
expect(host.textContent).toContain('已清除密钥并停用连接')
|
||||
expect(host.querySelector('input[type=password]')).toBeTruthy()
|
||||
})
|
||||
it('drops a stale draft test when the saved revision changed elsewhere', async () => {
|
||||
let gets = 0
|
||||
responder = (call) => {
|
||||
if (call.method === 'GET') {
|
||||
gets += 1
|
||||
return ok(gets === 1 ? saved : { ...saved, revision: 2, model: 'other-model' })
|
||||
}
|
||||
return call.path.endsWith('/settings/test')
|
||||
? { status: 409, error: '配置、输入、人工复核或预览已变化,请刷新;现有结论未被覆盖' }
|
||||
: ok(applied(call))
|
||||
}
|
||||
await act(async () => root.render(<QAModelSettings />))
|
||||
await type('模型名称', 'stale-draft')
|
||||
await click('测试连接')
|
||||
expect(host.textContent).toContain('已载入最新已保存配置(版本 2),草稿未保存,请重新测试')
|
||||
expect(current()).toContain('other-model')
|
||||
expect(host.textContent).not.toContain('草稿测试:')
|
||||
// 之后的保存基于刷新后的版本号,不会拿旧 revision 覆盖别人的修改。
|
||||
await type('模型名称', 'after-conflict')
|
||||
await click('保存配置')
|
||||
expect(writes().at(-1)!.body).toMatchObject({ base_revision: 2, model: 'after-conflict' })
|
||||
})
|
||||
})
|
||||
@@ -1,37 +1,58 @@
|
||||
import { it, expect } from 'vitest'
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { QAModelSettings, qaAIKeyEntryAllowed } from './QAModelSettings'
|
||||
it('provides independent write-only model settings and does not equate save with bulk execution', () => {
|
||||
import type { AISettings } from './qaAITypes'
|
||||
const saved: AISettings = {
|
||||
revision: 3,
|
||||
base_url: 'https://model.example.invalid/v1',
|
||||
model: 'fixture-model',
|
||||
key_configured: true,
|
||||
verification: 'verified',
|
||||
checked_at: '2026-10-19T00:00:00Z',
|
||||
automatic_evaluation: false,
|
||||
automatic_translation: false,
|
||||
storage_ready: true,
|
||||
}
|
||||
it('shows the saved connection and keeps a configured key collapsed until replaced', () => {
|
||||
const html = renderToStaticMarkup(<QAModelSettings initial={saved} />)
|
||||
for (const text of [
|
||||
'当前生效',
|
||||
'fixture-model · 已通过真实连接测试',
|
||||
'已配置(不回显)',
|
||||
'更换密钥',
|
||||
'API 基础地址',
|
||||
'模型名称',
|
||||
'测试连接',
|
||||
'保存配置',
|
||||
'清除密钥并停用连接',
|
||||
'草稿与已保存配置一致',
|
||||
])
|
||||
expect(html).toContain(text)
|
||||
// 已配置的密钥默认收起:没有密码框,也没有把任何密钥值渲染出来。
|
||||
expect(html).not.toContain('type="password"')
|
||||
expect(html).not.toContain('保留现有密钥')
|
||||
expect(html).not.toContain('fixture-only-api-key')
|
||||
})
|
||||
it('asks for a key on first entry and describes the disable impact', () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<QAModelSettings
|
||||
initial={{
|
||||
revision: 1,
|
||||
base_url: 'https://model.example.invalid/v1',
|
||||
model: 'fixture',
|
||||
key_configured: true,
|
||||
verification: 'verified',
|
||||
checked_at: '2026-10-19T00:00:00Z',
|
||||
automatic_evaluation: false,
|
||||
automatic_translation: false,
|
||||
storage_ready: true,
|
||||
...saved,
|
||||
revision: 0,
|
||||
base_url: '',
|
||||
model: '',
|
||||
key_configured: false,
|
||||
verification: 'not_configured',
|
||||
checked_at: null,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
for (const text of [
|
||||
'问答评估模型',
|
||||
'API 基础地址',
|
||||
'模型名称',
|
||||
'API Key',
|
||||
'type="password"',
|
||||
'测试连接',
|
||||
'保存配置',
|
||||
'保留现有密钥',
|
||||
'更换密钥',
|
||||
'清除密钥',
|
||||
'保存不会启动全量评估',
|
||||
])
|
||||
expect(html).toContain(text)
|
||||
expect(html).not.toContain('value="fixture-only-api-key"')
|
||||
expect(html).toContain('type="password"')
|
||||
expect(html).toContain('API 密钥 未配置')
|
||||
expect(html).toContain('未配置可用密钥')
|
||||
expect(html).toContain('无法调用此模型')
|
||||
expect(html).toContain('未保存的草稿不会被提交')
|
||||
expect(html).toContain('当前没有已保存的密钥')
|
||||
})
|
||||
it('permits new-key entry only on HTTPS or browser-local loopback origins', () => {
|
||||
expect(qaAIKeyEntryAllowed({ protocol: 'https:', hostname: 'kg.example.invalid' })).toBe(true)
|
||||
|
||||
+242
-110
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'
|
||||
import type { AISettings } from './qaAITypes'
|
||||
import type { AISettingsInput } from './qaAI'
|
||||
import { fetchAISettings, saveAISettings, testAISettings } from './qaAI'
|
||||
import { QARequestError } from './qaOps'
|
||||
export function qaAIKeyEntryAllowed(location: { protocol: string; hostname: string }) {
|
||||
return location.protocol === 'https:' || ['localhost', '127.0.0.1', '[::1]', '::1'].includes(location.hostname)
|
||||
}
|
||||
@@ -11,172 +12,303 @@ const verificationLabels: Record<AISettings['verification'], string> = {
|
||||
unverified: '未完成核验,未启用调用',
|
||||
rejected: '服务已拒绝,调用已阻断',
|
||||
}
|
||||
const when = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false })
|
||||
type Busy = '' | 'testing' | 'saving'
|
||||
type Notice = { tone: 'ok' | 'error'; text: string }
|
||||
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)
|
||||
// saved 始终是服务端已生效的那一份(顶部「当前生效」与停用操作的唯一依据);
|
||||
// 下面的输入只是草稿,测试草稿不回写 saved,避免把未保存的值伪装成当前连接。
|
||||
const [saved, setSaved] = useState<AISettings | null>(initial ?? null)
|
||||
const [base, setBase] = useState(initial?.base_url ?? '')
|
||||
const [model, setModel] = useState(initial?.model ?? '')
|
||||
const [replace, setReplace] = useState(!initial?.key_configured)
|
||||
const [key, setKey] = useState('')
|
||||
const [busy, setBusy] = useState<Busy>('')
|
||||
const [tested, setTested] = useState<AISettings | null>(null)
|
||||
const [confirmingDisable, setConfirmingDisable] = useState(false)
|
||||
const [notice, setNotice] = useState<Notice | null>(null)
|
||||
const [loadError, setLoadError] = useState<string | null>(null)
|
||||
useEffect(() => {
|
||||
if (initial) return
|
||||
const controller = new AbortController()
|
||||
void fetchAISettings(controller.signal)
|
||||
.then((value) => {
|
||||
setSettings(value)
|
||||
if (controller.signal.aborted) return
|
||||
setSaved(value)
|
||||
setBase(value.base_url)
|
||||
setModel(value.model)
|
||||
setKeyAction(value.key_configured ? 'keep' : 'replace')
|
||||
setReplace(!value.key_configured)
|
||||
})
|
||||
.catch((reason) => {
|
||||
if (!controller.signal.aborted) setError(reason instanceof Error ? reason.message : '模型配置读取失败')
|
||||
if (!controller.signal.aborted) setLoadError(reason instanceof Error ? reason.message : '模型配置读取失败')
|
||||
})
|
||||
return () => controller.abort()
|
||||
}, [initial])
|
||||
const dirty = () => {
|
||||
setMessage(null)
|
||||
setError(null)
|
||||
const secure = typeof window !== 'undefined' && qaAIKeyEntryAllowed(window.location)
|
||||
const configured = Boolean(saved?.key_configured)
|
||||
// 服务端规则:保留旧密钥时地址不许变(旧密钥不会被发往新地址),所以换地址强制重新录入。
|
||||
const baseChanged = Boolean(saved && base !== saved.base_url)
|
||||
const keyRequired = !configured || baseChanged || replace
|
||||
const keyAction: AISettingsInput['key_action'] = keyRequired ? 'replace' : 'keep'
|
||||
const storageReady = Boolean(saved?.storage_ready)
|
||||
const keyEntryBlocked = !secure || !storageReady
|
||||
const dirty = Boolean(
|
||||
saved && (base !== saved.base_url || model !== saved.model || key !== '' || (configured && replace))
|
||||
)
|
||||
const ready =
|
||||
Boolean(saved) &&
|
||||
busy === '' &&
|
||||
base.trim() !== '' &&
|
||||
model.trim() !== '' &&
|
||||
(!keyRequired || (!keyEntryBlocked && key !== ''))
|
||||
// 任何草稿改动都让上一次测试结果失效:旧草稿的核验结论不能贴在新草稿上。
|
||||
const edit = (apply: () => void) => {
|
||||
apply()
|
||||
setTested(null)
|
||||
setNotice(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 } : {}),
|
||||
const input = (): AISettingsInput => ({
|
||||
base_revision: saved!.revision,
|
||||
base_url: base,
|
||||
model,
|
||||
key_action: keyAction,
|
||||
...(keyAction === 'replace' ? { api_key: key } : {}),
|
||||
})
|
||||
const apply = (result: AISettings, prefix: string) => {
|
||||
setSaved(result)
|
||||
setBase(result.base_url)
|
||||
setModel(result.model)
|
||||
setKey('')
|
||||
setReplace(!result.key_configured)
|
||||
setTested(null)
|
||||
setConfirmingDisable(false)
|
||||
setNotice({
|
||||
tone: 'ok',
|
||||
text: `${prefix}:${result.model || '未设置模型'} · ${verificationLabels[result.verification]}`,
|
||||
})
|
||||
}
|
||||
const fail = async (reason: unknown) => {
|
||||
const message = reason instanceof Error ? reason.message : '模型配置操作失败'
|
||||
if (!(reason instanceof QARequestError) || reason.status !== 409) {
|
||||
setNotice({ tone: 'error', text: message })
|
||||
return
|
||||
}
|
||||
// 版本冲突:别人已经改过配置,这份草稿的测试结论作废;草稿本身保留,但必须按最新版本重测。
|
||||
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 ? ';未保存、未启动任务' : ';保存不会启动全量评估')
|
||||
const fresh = await fetchAISettings()
|
||||
setSaved(fresh)
|
||||
setReplace((current) => current || !fresh.key_configured)
|
||||
setTested(null)
|
||||
setNotice({
|
||||
tone: 'error',
|
||||
text: `${message};已载入最新已保存配置(版本 ${fresh.revision}),草稿未保存,请重新测试。`,
|
||||
})
|
||||
} catch {
|
||||
setNotice({ tone: 'error', text: message })
|
||||
}
|
||||
}
|
||||
const runTest = async () => {
|
||||
if (!saved || !ready) return
|
||||
setBusy('testing')
|
||||
setNotice(null)
|
||||
setTested(null)
|
||||
try {
|
||||
setTested(await testAISettings(input()))
|
||||
} catch (reason) {
|
||||
await fail(reason)
|
||||
} finally {
|
||||
setBusy('')
|
||||
}
|
||||
}
|
||||
const runSave = async () => {
|
||||
if (!saved || !ready) return
|
||||
setBusy('saving')
|
||||
setNotice(null)
|
||||
try {
|
||||
apply(await saveAISettings(input()), '已保存并更新当前连接')
|
||||
} catch (reason) {
|
||||
await fail(reason)
|
||||
} finally {
|
||||
setBusy('')
|
||||
}
|
||||
}
|
||||
const runDisable = async () => {
|
||||
if (!saved || busy !== '') return
|
||||
setBusy('saving')
|
||||
setNotice(null)
|
||||
try {
|
||||
// 只停用已保存连接:地址与模型取服务端当前值,未保存草稿不参与这次提交。
|
||||
apply(
|
||||
await saveAISettings({
|
||||
base_revision: saved.revision,
|
||||
base_url: saved.base_url,
|
||||
model: saved.model,
|
||||
key_action: 'clear',
|
||||
}),
|
||||
'已清除密钥并停用连接'
|
||||
)
|
||||
} catch (reason) {
|
||||
setError(reason instanceof Error ? reason.message : '模型配置操作失败')
|
||||
await fail(reason)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
setBusy('')
|
||||
}
|
||||
}
|
||||
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 ? (
|
||||
<section className="qa-ai-model-settings" aria-label="AI 模型连接" aria-busy={busy !== ''}>
|
||||
{saved ? (
|
||||
<>
|
||||
<p>
|
||||
{verificationLabels[settings.verification]};密钥:{settings.key_configured ? '已配置(不回显)' : '未配置'}
|
||||
;自动分析/翻译:{settings.automatic_evaluation ? '开' : '关'} /{' '}
|
||||
{settings.automatic_translation ? '开' : '关'}
|
||||
</p>
|
||||
<div className="safe-setting-grid">
|
||||
<div className="qa-ai-model-current">
|
||||
<strong>当前生效</strong>
|
||||
<span>
|
||||
{saved.model ? `${saved.model} · ` : ''}
|
||||
{verificationLabels[saved.verification]}
|
||||
</span>
|
||||
<small>
|
||||
{saved.base_url || '未设置地址'}
|
||||
{saved.checked_at ? ` · 最近核验 ${when(saved.checked_at)}` : ''}
|
||||
</small>
|
||||
</div>
|
||||
<div className="qa-ai-model-fields">
|
||||
<label>
|
||||
API 基础地址
|
||||
<input
|
||||
value={base}
|
||||
onChange={(event) => {
|
||||
setBase(event.target.value)
|
||||
dirty()
|
||||
}}
|
||||
onChange={(event) => edit(() => setBase(event.target.value))}
|
||||
placeholder="https://模型服务地址/v1"
|
||||
autoComplete="off"
|
||||
disabled={busy}
|
||||
disabled={busy !== ''}
|
||||
/>
|
||||
<small>OpenAI 兼容基础地址,不含 /chat/completions;更换地址须重新录入密钥。</small>
|
||||
</label>
|
||||
<label>
|
||||
模型名称
|
||||
<input
|
||||
value={model}
|
||||
onChange={(event) => {
|
||||
setModel(event.target.value)
|
||||
dirty()
|
||||
}}
|
||||
onChange={(event) => edit(() => setModel(event.target.value))}
|
||||
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}
|
||||
disabled={busy !== ''}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<small className="qa-ai-model-hint">OpenAI 兼容基础地址,不含 /chat/completions。</small>
|
||||
<div className="qa-ai-model-key">
|
||||
<span>API 密钥 {configured ? '已配置(不回显)' : '未配置'}</span>
|
||||
{configured && !keyRequired ? (
|
||||
<button
|
||||
type="button"
|
||||
className="link"
|
||||
onClick={() => edit(() => setReplace(true))}
|
||||
disabled={busy !== ''}
|
||||
>
|
||||
更换密钥
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
{keyRequired ? (
|
||||
<div className="qa-ai-model-key-edit">
|
||||
<label>
|
||||
{configured ? '新 API 密钥' : 'API 密钥'}
|
||||
<input
|
||||
type="password"
|
||||
name="qa_ai_key"
|
||||
value={key}
|
||||
onChange={(event) => edit(() => setKey(event.target.value))}
|
||||
autoComplete="new-password"
|
||||
spellCheck={false}
|
||||
placeholder={configured ? '输入新密钥;保存后才生效' : '仅用于此治理平台'}
|
||||
disabled={busy !== '' || keyEntryBlocked}
|
||||
/>
|
||||
</label>
|
||||
{configured && replace && !baseChanged ? (
|
||||
<button
|
||||
type="button"
|
||||
className="link"
|
||||
onClick={() =>
|
||||
edit(() => {
|
||||
setReplace(false)
|
||||
setKey('')
|
||||
})
|
||||
}
|
||||
disabled={busy !== ''}
|
||||
>
|
||||
取消更换
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{baseChanged && configured ? (
|
||||
<p className="qa-ops-warning">已更换基础地址:须重新录入密钥,原密钥不会发往新地址。</p>
|
||||
) : null}
|
||||
{!secure ? (
|
||||
<p className="qa-ops-warning">
|
||||
当前不是安全密钥录入连接。请使用有效HTTPS或本机 localhost
|
||||
地址录入新密钥;不会通过失配证书或普通局域网HTTP发送新密钥。
|
||||
</p>
|
||||
) : null}
|
||||
{!settings.storage_ready ? (
|
||||
{!storageReady ? (
|
||||
<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 ? '正在真实核验…' : '测试连接'}
|
||||
<details className="qa-ai-model-disable">
|
||||
<summary>清除密钥并停用连接</summary>
|
||||
<p>
|
||||
清除后问答四字段分析与中文问题翻译将无法调用此模型;自动任务开关保持不变,但因缺少可用密钥而不会执行。
|
||||
这里只停用已保存的连接,上方未保存的草稿不会被提交。
|
||||
</p>
|
||||
{confirmingDisable ? (
|
||||
<div className="qa-ai-model-disable-actions">
|
||||
<button type="button" className="danger" onClick={() => void runDisable()} disabled={busy !== ''}>
|
||||
{busy === 'saving' ? '正在清除…' : '确认清除并停用'}
|
||||
</button>
|
||||
<button type="button" onClick={() => setConfirmingDisable(false)} disabled={busy !== ''}>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
) : configured ? (
|
||||
<button
|
||||
type="button"
|
||||
className="danger"
|
||||
onClick={() => setConfirmingDisable(true)}
|
||||
disabled={busy !== ''}
|
||||
>
|
||||
清除密钥并停用…
|
||||
</button>
|
||||
) : (
|
||||
<small>当前没有已保存的密钥。</small>
|
||||
)}
|
||||
</details>
|
||||
<div className="qa-ai-model-actions">
|
||||
<button type="button" onClick={() => void runTest()} disabled={!ready}>
|
||||
{busy === 'testing' ? '正在测试草稿…' : '测试连接'}
|
||||
</button>
|
||||
<button type="button" disabled={!ready} onClick={() => void run(false)}>
|
||||
保存配置
|
||||
<button type="button" className="primary" onClick={() => void runSave()} disabled={!ready || !dirty}>
|
||||
{busy === 'saving' ? '正在保存配置…' : '保存配置'}
|
||||
</button>
|
||||
<small>{dirty ? '有未保存的修改' : '草稿与已保存配置一致'}</small>
|
||||
</div>
|
||||
<output aria-live="polite">
|
||||
{tested ? (
|
||||
<span className="qa-ai-model-test">
|
||||
草稿测试:{verificationLabels[tested.verification]};草稿未保存,当前生效配置未改变。
|
||||
</span>
|
||||
) : null}
|
||||
{notice ? (
|
||||
<span
|
||||
className={notice.tone === 'ok' ? 'success-message' : 'error-banner'}
|
||||
role={notice.tone === 'ok' ? 'status' : 'alert'}
|
||||
>
|
||||
{notice.text}
|
||||
</span>
|
||||
) : null}
|
||||
</output>
|
||||
</>
|
||||
) : !error ? (
|
||||
<p role="status">读取独立模型配置…</p>
|
||||
) : !loadError ? (
|
||||
<p role="status">正在读取已保存的模型连接…</p>
|
||||
) : null}
|
||||
{message ? <p role="status">{message}</p> : null}
|
||||
{error ? (
|
||||
{loadError ? (
|
||||
<p className="error-banner" role="alert">
|
||||
{error}
|
||||
{loadError}
|
||||
</p>
|
||||
) : null}
|
||||
</section>
|
||||
|
||||
@@ -143,7 +143,12 @@ describe('SystemSettingsView AI 模型连接', () => {
|
||||
)
|
||||
it('keeps the model connection alone in AI 模型连接 and points WeKnora to 知识库映射', () => {
|
||||
const adminHtml = view('admin')
|
||||
for (const text of ['AI 模型连接', '问答评估模型', 'WeKnora 的知识库连接已移到「知识库映射」']) {
|
||||
for (const text of [
|
||||
'AI 模型连接',
|
||||
'正在读取已保存的模型连接',
|
||||
'问答四字段分析与中文问题翻译共用此连接',
|
||||
'WeKnora 的知识库连接已移到「知识库映射」',
|
||||
]) {
|
||||
expect(adminHtml).toContain(text)
|
||||
}
|
||||
// 地址类配置已迁走:这里不再出现 WeKnora 连接表单,也不出现旧的“服务配置”分区名。
|
||||
@@ -152,7 +157,7 @@ describe('SystemSettingsView AI 模型连接', () => {
|
||||
const engineerHtml = view('engineer')
|
||||
expect(engineerHtml).toContain('仅管理员可修改模型连接')
|
||||
// 工程师看不到模型编辑表单(QAModelSettings 只在管理员下挂载)。
|
||||
expect(engineerHtml).not.toContain('问答评估模型')
|
||||
expect(engineerHtml).not.toContain('正在读取已保存的模型连接')
|
||||
})
|
||||
})
|
||||
describe('SystemSettingsView', () => {
|
||||
|
||||
@@ -208,13 +208,13 @@ export function SystemSettingsView({
|
||||
<div className="settings-section-heading">
|
||||
<div>
|
||||
<h2>AI 模型连接</h2>
|
||||
<p>问答四字段分析与中文问题翻译共用的模型服务;WeKnora 的知识库连接已移到「知识库映射」。</p>
|
||||
<p>
|
||||
问答四字段分析与中文问题翻译共用此连接;保存只更新这份连接,不会自动开启自动分析或自动翻译。
|
||||
WeKnora 的知识库连接已移到「知识库映射」。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{isAdmin ? <QAModelSettings /> : <small>仅管理员可修改模型连接;密钥不回显。</small>}
|
||||
<p className="settings-hint-wide">
|
||||
保存只更新这份模型连接;自动分析与自动翻译仍按问答运营里的任务开关执行,不会因为保存连接自动打开。
|
||||
</p>
|
||||
</section>
|
||||
) : null}
|
||||
{section === 'knowledge-map' ? (
|
||||
|
||||
@@ -588,13 +588,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.qa-ai-model-settings {
|
||||
padding: 14px 0;
|
||||
border-block: 1px solid var(--border-base);
|
||||
}
|
||||
.qa-ai-model-settings h3 {
|
||||
margin: 0 0 6px;
|
||||
}
|
||||
.qa-ai-workbench {
|
||||
border: 1px solid var(--border-base);
|
||||
border-radius: 5px;
|
||||
|
||||
@@ -907,3 +907,93 @@ button.link {
|
||||
color: var(--muted-foreground);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
/* AI 模型连接:上方「当前生效」只读服务端已保存值,下方是独立草稿 */
|
||||
.qa-ai-model-settings {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
padding: 12px 0;
|
||||
border-block: 1px solid var(--border-base);
|
||||
}
|
||||
.qa-ai-model-current {
|
||||
display: grid;
|
||||
gap: 3px;
|
||||
}
|
||||
.qa-ai-model-current > span {
|
||||
font-size: 13px;
|
||||
}
|
||||
.qa-ai-model-current > small {
|
||||
color: var(--ink-mute);
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.qa-ai-model-fields {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
.qa-ai-model-fields label,
|
||||
.qa-ai-model-key-edit label {
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
font-size: 11px;
|
||||
}
|
||||
.qa-ai-model-fields input,
|
||||
.qa-ai-model-key-edit input {
|
||||
border: 1px solid var(--border-strong);
|
||||
border-radius: 7px;
|
||||
padding: 8px;
|
||||
background: var(--bg-panel);
|
||||
color: var(--ink-base);
|
||||
font: inherit;
|
||||
}
|
||||
.qa-ai-model-hint,
|
||||
.qa-ai-model-actions small {
|
||||
color: var(--ink-mute);
|
||||
}
|
||||
.qa-ai-model-key {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
font-size: 12px;
|
||||
}
|
||||
.qa-ai-model-key-edit {
|
||||
display: flex;
|
||||
align-items: end;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.qa-ai-model-key-edit label {
|
||||
flex: 1 1 280px;
|
||||
}
|
||||
.qa-ai-model-disable p {
|
||||
margin: 6px 0;
|
||||
color: var(--ink-mute);
|
||||
font-size: 11px;
|
||||
}
|
||||
.qa-ai-model-disable-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.qa-ai-model-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.qa-ai-model-test {
|
||||
display: block;
|
||||
margin-top: 6px;
|
||||
color: var(--c-run);
|
||||
font-size: 12px;
|
||||
}
|
||||
.qa-ai-model-settings output {
|
||||
display: block;
|
||||
}
|
||||
@media (max-width: 650px) {
|
||||
.qa-ai-model-fields {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user