- 表单下拉框不再用 label 包裹,改为 div + aria-label,避免点击后 下拉面板瞬间关闭 - Provider 卡片新增「从上游获取模型」和「修改」入口,支持编辑 base URL、API Key 与 api_mode,凭据写入对应的环境变量 - 服务端抽出 provider-model-discovery 统一不同 api_mode 的模型发现, 旧的 custom_providers 与新的 providers 字典结构都能正确更新 - Agent Bridge 改为后台启动,HTTP 接口在 Python 运行时就绪前即可用; worker 连接被拒时按类型重试,不重放可能已产生副作用的请求 - 断线重连后恢复 profiles、models 与会话列表,健康检查改为自调度 的 setTimeout,离线时 2 秒后重试 - 批量删除确认文案改用 Popconfirm content,避免变成侧栏触发器文本
194 lines
8.2 KiB
TypeScript
194 lines
8.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'
|
|
import { join } from 'path'
|
|
import { tmpdir } from 'os'
|
|
import YAML from 'js-yaml'
|
|
|
|
vi.mock('../../packages/server/src/services/hermes/hermes-cli', () => ({
|
|
restartGateway: vi.fn().mockResolvedValue(undefined),
|
|
}))
|
|
|
|
let hermesHome = ''
|
|
|
|
async function loadProvidersController() {
|
|
vi.resetModules()
|
|
process.env.HERMES_HOME = hermesHome
|
|
return import('../../packages/server/src/controllers/hermes/providers')
|
|
}
|
|
|
|
function makeCtx(poolKey: string, body: Record<string, any>, profile = 'research') {
|
|
return {
|
|
params: { poolKey: encodeURIComponent(poolKey) },
|
|
request: { body },
|
|
state: { profile: { name: profile } },
|
|
status: 200,
|
|
body: undefined as unknown,
|
|
}
|
|
}
|
|
|
|
function readYaml(filePath: string) {
|
|
return YAML.load(readFileSync(filePath, 'utf-8')) as any
|
|
}
|
|
|
|
describe('providers controller update', () => {
|
|
beforeEach(() => {
|
|
hermesHome = mkdtempSync(join(tmpdir(), 'hwui-provider-update-'))
|
|
mkdirSync(join(hermesHome, 'profiles', 'research'), { recursive: true })
|
|
writeFileSync(join(hermesHome, 'config.yaml'), 'model:\n provider: deepseek\n default: keep-default-model\n')
|
|
writeFileSync(join(hermesHome, '.env'), [
|
|
'DEEPSEEK_API_KEY=keep-default-key',
|
|
'',
|
|
].join('\n'))
|
|
writeFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), [
|
|
'model:',
|
|
' provider: custom:research-proxy',
|
|
' default: research-model',
|
|
'custom_providers:',
|
|
' - name: research-proxy',
|
|
' base_url: https://research.invalid/v1',
|
|
' api_key: old-research-custom-key',
|
|
' model: research-model',
|
|
'',
|
|
].join('\n'))
|
|
writeFileSync(join(hermesHome, 'profiles', 'research', '.env'), [
|
|
'DEEPSEEK_API_KEY=old-research-key',
|
|
'',
|
|
].join('\n'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
delete process.env.HERMES_HOME
|
|
vi.doUnmock('../../packages/server/src/controllers/hermes/providers')
|
|
vi.clearAllMocks()
|
|
if (hermesHome) rmSync(hermesHome, { recursive: true, force: true })
|
|
hermesHome = ''
|
|
})
|
|
|
|
it('updates built-in provider API keys in the request-scoped profile env only', async () => {
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('deepseek', { api_key: 'new-research-key' })
|
|
|
|
await update(ctx)
|
|
|
|
expect(ctx.body).toEqual({ success: true })
|
|
expect(readFileSync(join(hermesHome, '.env'), 'utf-8')).toContain('DEEPSEEK_API_KEY=keep-default-key')
|
|
expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf-8')).toContain('DEEPSEEK_API_KEY=new-research-key')
|
|
})
|
|
|
|
it('updates custom provider API keys in the request-scoped profile config only', async () => {
|
|
const defaultConfigPath = join(hermesHome, 'config.yaml')
|
|
writeFileSync(defaultConfigPath, [
|
|
'model:',
|
|
' provider: custom:research-proxy',
|
|
' default: default-model',
|
|
'custom_providers:',
|
|
' - name: research-proxy',
|
|
' base_url: https://default.invalid/v1',
|
|
' api_key: keep-default-custom-key',
|
|
' model: default-model',
|
|
'',
|
|
].join('\n'))
|
|
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('custom:research-proxy', { api_key: 'new-research-custom-key' })
|
|
|
|
await update(ctx)
|
|
|
|
expect(ctx.body).toEqual({ success: true })
|
|
const defaultConfig = readYaml(defaultConfigPath)
|
|
const researchConfig = readYaml(join(hermesHome, 'profiles', 'research', 'config.yaml'))
|
|
expect(defaultConfig.custom_providers[0].api_key).toBe('keep-default-custom-key')
|
|
expect(researchConfig.custom_providers[0].api_key).toBe('new-research-custom-key')
|
|
})
|
|
|
|
it('updates custom provider api_mode in the request-scoped profile config only', async () => {
|
|
const defaultConfigPath = join(hermesHome, 'config.yaml')
|
|
writeFileSync(defaultConfigPath, [
|
|
'model:',
|
|
' provider: custom:research-proxy',
|
|
' default: default-model',
|
|
'custom_providers:',
|
|
' - name: research-proxy',
|
|
' base_url: https://default.invalid/v1',
|
|
' api_key: keep-default-custom-key',
|
|
' model: default-model',
|
|
' api_mode: codex_responses',
|
|
'',
|
|
].join('\n'))
|
|
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('custom:research-proxy', { api_mode: 'chat_completions' })
|
|
|
|
await update(ctx)
|
|
|
|
expect(ctx.body).toEqual({ success: true })
|
|
const defaultConfig = readYaml(defaultConfigPath)
|
|
const researchConfig = readYaml(join(hermesHome, 'profiles', 'research', 'config.yaml'))
|
|
expect(defaultConfig.custom_providers[0].api_mode).toBe('codex_responses')
|
|
expect(researchConfig.custom_providers[0].api_mode).toBe('chat_completions')
|
|
})
|
|
it('updates a built-in Base URL and key without changing defaults or another profile', async () => {
|
|
const { update } = await loadProvidersController()
|
|
const before = readFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), 'utf8')
|
|
const ctx = makeCtx('minimax-cn', { base_url: 'https://proxy.invalid/anthropic/v1', api_key: 'new-key' })
|
|
await update(ctx)
|
|
expect(ctx.body).toEqual({ success: true })
|
|
const env = readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')
|
|
expect(env).toContain('MINIMAX_CN_API_KEY=new-key')
|
|
expect(env).toContain('MINIMAX_CN_BASE_URL=https://proxy.invalid/anthropic/v1')
|
|
expect(readFileSync(join(hermesHome, '.env'), 'utf8')).not.toContain('new-key')
|
|
expect(readFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), 'utf8')).toBe(before)
|
|
})
|
|
|
|
it('keeps an existing key when only the URL is edited', async () => {
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('deepseek', { base_url: 'https://new.invalid/v1' })
|
|
await update(ctx)
|
|
const env = readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')
|
|
expect(env).toContain('DEEPSEEK_API_KEY=old-research-key')
|
|
expect(env).toContain('DEEPSEEK_BASE_URL=https://new.invalid/v1')
|
|
})
|
|
|
|
it('edits a dict-backed provider in place and preserves its models and extra configuration', async () => {
|
|
const path = join(hermesHome, 'profiles', 'research', 'config.yaml')
|
|
writeFileSync(path, YAML.dump({
|
|
model: { provider: 'custom:dict-proxy', default: 'selected-model' },
|
|
providers: { original_key: {
|
|
name: 'dict-proxy', url: 'https://old.invalid/v1', api_key: 'old-key',
|
|
models: { 'selected-model': { context_length: 12345 } }, extra_body: { keep: true },
|
|
} },
|
|
}))
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('custom:dict-proxy', {
|
|
base_url: 'https://new.invalid/v1', api_key: 'new-key', source: 'providers', providerKey: 'original_key',
|
|
})
|
|
await update(ctx)
|
|
expect(ctx.body).toEqual({ success: true })
|
|
const config = readYaml(path)
|
|
expect(config.custom_providers).toBeUndefined()
|
|
expect(config.model).toEqual({ provider: 'custom:dict-proxy', default: 'selected-model' })
|
|
expect(config.providers.original_key).toMatchObject({
|
|
base_url: 'https://new.invalid/v1', api_key: 'new-key',
|
|
models: { 'selected-model': { context_length: 12345 } }, extra_body: { keep: true },
|
|
})
|
|
})
|
|
|
|
it('updates an env-backed custom key in the same profile', async () => {
|
|
const path = join(hermesHome, 'profiles', 'research', 'config.yaml')
|
|
writeFileSync(path, YAML.dump({ providers: { proxy: { base_url: 'https://proxy.invalid/v1', key_env: 'PROXY_KEY', model: 'keep' } } }))
|
|
const { update } = await loadProvidersController()
|
|
await update(makeCtx('custom:proxy', { api_key: 'new-key', source: 'providers', providerKey: 'proxy' }))
|
|
expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')).toContain('PROXY_KEY=new-key')
|
|
expect(readYaml(path).providers.proxy.model).toBe('keep')
|
|
})
|
|
|
|
it.each(['', 'bad-url', 'file:///tmp/model'])('rejects an invalid URL before writing the key: %s', async base_url => {
|
|
const { update } = await loadProvidersController()
|
|
const ctx = makeCtx('deepseek', { base_url, api_key: 'must-not-save' })
|
|
await update(ctx)
|
|
expect(ctx.status).toBe(400)
|
|
expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')).toContain('DEEPSEEK_API_KEY=old-research-key')
|
|
})
|
|
|
|
})
|