feat(settings): 知识库绑定按环境一次保存连接与内外库

原「知识库映射 → WeKnora 连接」把地址、文件和绑定分散在多处,页面无法输入密钥,
也看不到候选实例真实可读的库。现在测试 090 / 生产 090 各自一个工作区:草稿填地址
与 API Key,服务器真实分页列库并区分空库/无权限/不可达,选内外库后一次保存连接与
绑定,回读真实生效值。

服务端以修订号 + 行锁 + 单事务写连接、绑定与待入库重排;旧部署无新行时继续使用文件
凭据/默认绑定。密钥只存 AES-256-GCM 密文,主密钥在独立文件;换地址必须重录,旧密钥
不转发、不跨环境取用。消费端(readiness/上传轮询/列库/探活/生产列库)统一从该存储
解析当前连接,避免只保存了页面数据。

验证:go test ./...;前端 606 项;隔离 PostgreSQL 集成用例覆盖保存/冲突/重排/拒错;
make verify 在本提交上执行(结果见交付说明)。
This commit is contained in:
姚章浩
2026-11-19 14:43:51 +08:00
parent df69b278ce
commit ce2a155ebc
53 changed files with 3367 additions and 601 deletions
+70
View File
@@ -0,0 +1,70 @@
import { describe, expect, it } from 'vitest'
import {
duplicateSelection,
keyActionFor,
knowledgeBaseLabel,
parseKBBindingSnapshot,
shortKnowledgeBaseID,
} from './knowledgeBaseBindings'
import type { KBEnvironmentBinding } from './knowledgeBaseBindings'
const binding = (environment: 'test' | 'production'): KBEnvironmentBinding => ({
environment,
revision: 1,
base_url: `https://${environment}.example`,
key_configured: true,
key_source: 'sealed',
internal_kb_id: `${environment}-in`,
internal_kb_name: '内部资料',
external_kb_id: `${environment}-out`,
external_kb_name: '对外资料',
verification: 'verified',
checked_at: null,
updated_by: 'admin',
updated_at: null,
source: 'web',
storage_ready: true,
replanned: 0,
retired_duplicates: 0,
})
describe('knowledgeBaseBindings', () => {
it('parses both environments and never projects a key field', () => {
const parsed = parseKBBindingSnapshot({
data: {
environments: [binding('test'), binding('production')],
storage_ready: true,
},
})
expect(parsed.test.base_url).toBe('https://test.example')
expect(parsed.production.internal_kb_name).toBe('内部资料')
expect(parsed.storage_ready).toBe(true)
const asRecord = parsed.test as unknown as Record<string, unknown>
expect(asRecord.api_key).toBeUndefined()
expect(asRecord.key_ciphertext).toBeUndefined()
})
it('rejects a snapshot missing an environment instead of inventing one', () => {
expect(() => parseKBBindingSnapshot({ data: { environments: [binding('test')], storage_ready: true } })).toThrow()
})
it('shows a short id next to the name so same-name bases are distinguishable', () => {
expect(shortKnowledgeBaseID('0b217e8a-1234-5678')).toBe('0b217e8a…')
expect(knowledgeBaseLabel('0b217e8a-1234-5678', '客户帮助中心')).toBe('客户帮助中心(0b217e8a…)')
expect(knowledgeBaseLabel('', '')).toBe('未绑定')
})
it('rejects using one base for both sides', () => {
expect(duplicateSelection('kb-1', 'kb-1')).toBe(true)
expect(duplicateSelection('kb-1', 'kb-2')).toBe(false)
expect(duplicateSelection('', '')).toBe(false)
})
it('forces a new key for any address other than the saved one', () => {
const saved = binding('test')
expect(keyActionFor(saved, 'https://test.example', false)).toBe('keep')
expect(keyActionFor(saved, 'https://other.example', false)).toBe('replace')
expect(keyActionFor({ ...saved, key_configured: false }, 'https://test.example', false)).toBe('replace')
expect(keyActionFor(saved, 'https://test.example', true)).toBe('replace')
})
})