feat: profile-aware routes, provider sync, channel settings improvements

- Add hermes-profile.ts for dynamic profile path resolution (all backend
  routes now read from active profile directory instead of hardcoded ~/.hermes/)
- Add profile switcher dropdown in sidebar, reload page on switch
- Sync PROVIDER_PRESETS with Hermes CLI (fix keys: kimi-coding→kimi-for-coding,
  kilocode→kilo, ai-gateway→vercel, opencode-zen→opencode; remove moonshot)
- Sync PROVIDER_ENV_MAP with Hermes models.dev + overlays (correct env var names)
- Add gateway restart after adding model provider
- Don't write GLM_BASE_URL/KIMI_BASE_URL for zai/kimi (let Hermes auto-detect)
- Write API keys to .env and credential_pool for all providers
- Built-in providers skip custom_providers in config.yaml
- Add debounce + per-field loading state for channel settings inputs
- Run hermes setup --reset for profiles without config.yaml
- Create empty .env for new profiles (not copied from default)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ekko
2026-04-16 13:51:42 +08:00
parent 014168864f
commit 99a47cf1ad
23 changed files with 712 additions and 185 deletions
+12 -11
View File
@@ -1,10 +1,10 @@
import Router from '@koa/router'
import { readFile, writeFile, copyFile } from 'fs/promises'
import { chmod } from 'fs/promises'
import { resolve } from 'path'
import { homedir } from 'os'
import { join } from 'path'
import YAML from 'js-yaml'
import { restartGateway } from '../../services/hermes-cli'
import { getActiveConfigPath, getActiveEnvPath, getActiveProfileDir } from '../../services/hermes-profile'
// Platform sections that require gateway restart after config change
const PLATFORM_SECTIONS = new Set([
@@ -12,8 +12,8 @@ const PLATFORM_SECTIONS = new Set([
'weixin', 'wecom', 'feishu', 'dingtalk',
])
const configPath = resolve(homedir(), '.hermes/config.yaml')
const envPath = resolve(homedir(), '.hermes/.env')
const configPath = () => getActiveConfigPath()
const envPath = () => getActiveEnvPath()
// Env var → (platform, configPath in PlatformConfig) mapping
// Matches hermes _apply_env_overrides() in gateway/config.py
@@ -81,7 +81,7 @@ function getNested(obj: Record<string, any>, path: string): any {
async function readEnvPlatforms(): Promise<Record<string, any>> {
try {
const raw = await readFile(envPath, 'utf-8')
const raw = await readFile(envPath(), 'utf-8')
const env = parseEnv(raw)
const platforms: Record<string, any> = {}
for (const [envKey, [platform, cfgPath]] of Object.entries(envPlatformMap)) {
@@ -103,7 +103,7 @@ async function readEnvPlatforms(): Promise<Record<string, any>> {
async function saveEnvValue(key: string, value: string): Promise<void> {
let raw: string
try {
raw = await readFile(envPath, 'utf-8')
raw = await readFile(envPath(), 'utf-8')
} catch {
raw = ''
}
@@ -144,25 +144,26 @@ async function saveEnvValue(key: string, value: string): Promise<void> {
// Remove trailing empty lines, keep exactly one trailing newline
let output = result.join('\n').replace(/\n{3,}/g, '\n\n').replace(/\n+$/, '') + '\n'
await writeFile(envPath, output, 'utf-8')
await writeFile(envPath(), output, 'utf-8')
// Set permissions to 0600 (owner only), matching hermes behavior
try { await chmod(envPath, 0o600) } catch { /* ignore */ }
try { await chmod(envPath(), 0o600) } catch { /* ignore */ }
}
async function readConfig(): Promise<Record<string, any>> {
const raw = await readFile(configPath, 'utf-8')
const raw = await readFile(configPath(), 'utf-8')
return (YAML.load(raw) as Record<string, any>) || {}
}
async function writeConfig(data: Record<string, any>): Promise<void> {
await copyFile(configPath, configPath + '.bak')
const cp = configPath()
await copyFile(cp, cp + '.bak')
const yamlStr = YAML.dump(data, {
lineWidth: -1,
noRefs: true,
quotingType: '"',
forceQuotes: false,
})
await writeFile(configPath, yamlStr, 'utf-8')
await writeFile(cp, yamlStr, 'utf-8')
}
export const configRoutes = new Router()
+120 -35
View File
@@ -1,8 +1,64 @@
import Router from '@koa/router'
import { readdir, readFile, stat, writeFile, mkdir, copyFile } from 'fs/promises'
import { join, resolve } from 'path'
import { homedir } from 'os'
import YAML from 'js-yaml'
import { getActiveProfileDir, getActiveConfigPath, getActiveAuthPath, getActiveEnvPath } from '../../services/hermes-profile'
import * as hermesCli from '../../services/hermes-cli'
// --- Provider env var mapping (from hermes providers.py HERMES_OVERLAYS + config.py) ---
// Maps provider key → { api_key_envs: all env var aliases for API key, base_url_env: env var for base URL }
const PROVIDER_ENV_MAP: Record<string, { api_key_env: string; base_url_env: string }> = {
openrouter: { api_key_env: 'OPENROUTER_API_KEY', base_url_env: 'OPENROUTER_BASE_URL' },
zai: { api_key_env: 'ZAI_API_KEY', base_url_env: '' },
'kimi-for-coding': { api_key_env: 'KIMI_API_KEY', base_url_env: '' },
minimax: { api_key_env: 'MINIMAX_API_KEY', base_url_env: 'MINIMAX_BASE_URL' },
'minimax-cn': { api_key_env: 'MINIMAX_API_KEY', base_url_env: 'MINIMAX_CN_BASE_URL' },
deepseek: { api_key_env: 'DEEPSEEK_API_KEY', base_url_env: 'DEEPSEEK_BASE_URL' },
alibaba: { api_key_env: 'DASHSCOPE_API_KEY', base_url_env: 'DASHSCOPE_BASE_URL' },
anthropic: { api_key_env: 'ANTHROPIC_API_KEY', base_url_env: '' },
xai: { api_key_env: 'XAI_API_KEY', base_url_env: 'XAI_BASE_URL' },
xiaomi: { api_key_env: 'XIAOMI_API_KEY', base_url_env: 'XIAOMI_BASE_URL' },
gemini: { api_key_env: 'GEMINI_API_KEY', base_url_env: '' },
kilo: { api_key_env: 'KILO_API_KEY', base_url_env: 'KILOCODE_BASE_URL' },
vercel: { api_key_env: 'AI_GATEWAY_API_KEY', base_url_env: '' },
opencode: { api_key_env: 'OPENCODE_API_KEY', base_url_env: 'OPENCODE_ZEN_BASE_URL' },
'opencode-go': { api_key_env: 'OPENCODE_API_KEY', base_url_env: 'OPENCODE_GO_BASE_URL' },
huggingface: { api_key_env: 'HF_TOKEN', base_url_env: 'HF_BASE_URL' },
}
async function saveEnvValue(key: string, value: string): Promise<void> {
const envPath = getActiveEnvPath()
let raw: string
try {
raw = await readFile(envPath, 'utf-8')
} catch {
raw = ''
}
const remove = !value
const lines = raw.split('\n')
let found = false
const result: string[] = []
for (const line of lines) {
const trimmed = line.trim()
if (trimmed.startsWith('#') && trimmed.startsWith(`# ${key}=`)) {
if (!remove) result.push(`${key}=${value}`)
found = true
} else {
const eqIdx = trimmed.indexOf('=')
if (eqIdx !== -1 && trimmed.slice(0, eqIdx).trim() === key) {
if (!remove) result.push(`${key}=${value}`)
found = true
} else {
result.push(line)
}
}
}
if (!found && !remove) {
result.push(`${key}=${value}`)
}
let output = result.join('\n').replace(/\n{3,}/g, '\n\n').replace(/\n+$/, '') + '\n'
await writeFile(envPath, output, 'utf-8')
}
// --- Auth / Credential Pool ---
@@ -18,11 +74,11 @@ interface AuthJson {
credential_pool?: Record<string, CredentialPoolEntry[]>
}
const authPath = resolve(homedir(), '.hermes', 'auth.json')
const authPath = () => getActiveAuthPath()
async function loadAuthJson(): Promise<AuthJson | null> {
try {
const raw = await readFile(authPath, 'utf-8')
const raw = await readFile(authPath(), 'utf-8')
return JSON.parse(raw) as AuthJson
} catch {
return null
@@ -30,7 +86,7 @@ async function loadAuthJson(): Promise<AuthJson | null> {
}
async function saveAuthJson(auth: AuthJson): Promise<void> {
await writeFile(authPath, JSON.stringify(auth, null, 2) + '\n', 'utf-8')
await writeFile(authPath(), JSON.stringify(auth, null, 2) + '\n', 'utf-8')
}
async function fetchProviderModels(baseUrl: string, apiKey: string): Promise<string[]> {
@@ -62,7 +118,7 @@ const PROVIDER_MODEL_CATALOG = buildProviderModelMap()
export const fsRoutes = new Router()
const hermesDir = resolve(homedir(), '.hermes')
const hermesDir = () => getActiveProfileDir()
// --- Types ---
@@ -123,29 +179,30 @@ async function safeStat(filePath: string): Promise<{ mtime: number } | null> {
// --- Config YAML helpers ---
const configPath = resolve(homedir(), '.hermes/config.yaml')
const configPath = () => getActiveConfigPath()
async function readConfigYaml(): Promise<Record<string, any>> {
const raw = await safeReadFile(configPath)
const raw = await safeReadFile(configPath())
if (!raw) return {}
return (YAML.load(raw) as Record<string, any>) || {}
}
async function writeConfigYaml(config: Record<string, any>): Promise<void> {
await copyFile(configPath, configPath + '.bak')
const cp = configPath()
await copyFile(cp, cp + '.bak')
const yamlStr = YAML.dump(config, {
lineWidth: -1,
noRefs: true,
quotingType: '"',
})
await writeFile(configPath, yamlStr, 'utf-8')
await writeFile(cp, yamlStr, 'utf-8')
}
// --- Skills Routes ---
// List all skills grouped by category
fsRoutes.get('/api/hermes/skills', async (ctx) => {
const skillsDir = join(hermesDir, 'skills')
const skillsDir = join(hermesDir(), 'skills')
try {
// Read disabled skills list from config.yaml
@@ -250,7 +307,7 @@ async function listFilesRecursive(dir: string, prefix: string): Promise<{ path:
fsRoutes.get('/api/hermes/skills/:category/:skill/files', async (ctx) => {
const { category, skill } = ctx.params
const skillDir = join(hermesDir, 'skills', category, skill)
const skillDir = join(hermesDir(), 'skills', category, skill)
try {
const allFiles = await listFilesRecursive(skillDir, '')
@@ -265,9 +322,10 @@ fsRoutes.get('/api/hermes/skills/:category/:skill/files', async (ctx) => {
// Read a specific file under skills/ (must be registered after the /files route)
fsRoutes.get('/api/hermes/skills/{*path}', async (ctx) => {
const filePath = (ctx.params as any).path
const fullPath = resolve(join(hermesDir, 'skills', filePath))
const hd = hermesDir()
const fullPath = resolve(join(hd, 'skills', filePath))
if (!fullPath.startsWith(join(hermesDir, 'skills'))) {
if (!fullPath.startsWith(join(hd, 'skills'))) {
ctx.status = 403
ctx.body = { error: 'Access denied' }
return
@@ -286,21 +344,27 @@ fsRoutes.get('/api/hermes/skills/{*path}', async (ctx) => {
// --- Memory Routes ---
fsRoutes.get('/api/hermes/memory', async (ctx) => {
const memoryPath = join(hermesDir, 'memories', 'MEMORY.md')
const userPath = join(hermesDir, 'memories', 'USER.md')
const hd = hermesDir()
const memoryPath = join(hd, 'memories', 'MEMORY.md')
const userPath = join(hd, 'memories', 'USER.md')
const soulPath = join(hd, 'SOUL.md')
const [memory, user, memoryStat, userStat] = await Promise.all([
const [memory, user, soul, memoryStat, userStat, soulStat] = await Promise.all([
safeReadFile(memoryPath),
safeReadFile(userPath),
safeReadFile(soulPath),
safeStat(memoryPath),
safeStat(userPath),
safeStat(soulPath),
])
ctx.body = {
memory: memory || '',
user: user || '',
soul: soul || '',
memory_mtime: memoryStat?.mtime || null,
user_mtime: userStat?.mtime || null,
soul_mtime: soulStat?.mtime || null,
}
})
@@ -313,14 +377,19 @@ fsRoutes.post('/api/hermes/memory', async (ctx) => {
return
}
if (section !== 'memory' && section !== 'user') {
if (section !== 'memory' && section !== 'user' && section !== 'soul') {
ctx.status = 400
ctx.body = { error: 'Section must be "memory" or "user"' }
ctx.body = { error: 'Section must be "memory", "user", or "soul"' }
return
}
const fileName = section === 'memory' ? 'MEMORY.md' : 'USER.md'
const filePath = join(hermesDir, 'memories', fileName)
let filePath: string
if (section === 'soul') {
filePath = join(hermesDir(), 'SOUL.md')
} else {
const fileName = section === 'memory' ? 'MEMORY.md' : 'USER.md'
filePath = join(hermesDir(), 'memories', fileName)
}
try {
await writeFile(filePath, content, 'utf-8')
@@ -532,26 +601,27 @@ fsRoutes.post('/api/hermes/config/providers', async (ctx) => {
}
try {
// 1. Write to config.yaml custom_providers
const config = await readConfigYaml()
if (!Array.isArray(config.custom_providers)) {
config.custom_providers = []
}
config.custom_providers.push({ name, base_url, api_key, model })
await writeConfigYaml(config)
// 2. Write to auth.json credential_pool
// Determine if this is a built-in provider or a custom one
const poolKey = providerKey
|| `custom:${name.trim().toLowerCase().replace(/ /g, '-')}`
const isBuiltin = poolKey in PROVIDER_ENV_MAP
if (!isBuiltin) {
// Custom provider: write to config.yaml custom_providers
const config = await readConfigYaml()
if (!Array.isArray(config.custom_providers)) {
config.custom_providers = []
}
config.custom_providers.push({ name, base_url, api_key, model })
await writeConfigYaml(config)
}
// Write to auth.json credential_pool (all providers)
const auth = await loadAuthJson() || { credential_pool: {} }
if (!auth.credential_pool) auth.credential_pool = {}
if (!auth.credential_pool[poolKey]) {
auth.credential_pool[poolKey] = []
}
auth.credential_pool[poolKey].push({
id: `${poolKey}-${Date.now()}`,
label: name,
@@ -559,10 +629,18 @@ fsRoutes.post('/api/hermes/config/providers', async (ctx) => {
access_token: api_key,
last_status: null,
})
await saveAuthJson(auth)
// 3. Auto-switch model to the newly added provider
// Write API key to .env (built-in providers only)
const envMapping = PROVIDER_ENV_MAP[poolKey] || PROVIDER_ENV_MAP[providerKey || '']
if (envMapping) {
await saveEnvValue(envMapping.api_key_env, api_key)
if (envMapping.base_url_env) {
await saveEnvValue(envMapping.base_url_env, base_url)
}
}
// Auto-switch model to the newly added provider
const config2 = await readConfigYaml()
if (typeof config2.model !== 'object' || config2.model === null) {
config2.model = {}
@@ -571,6 +649,13 @@ fsRoutes.post('/api/hermes/config/providers', async (ctx) => {
config2.model.provider = poolKey
await writeConfigYaml(config2)
// Restart gateway to pick up .env and config.yaml changes
try {
await hermesCli.restartGateway()
} catch (e: any) {
console.error('[Provider] Gateway restart failed:', e.message)
}
ctx.body = { success: true }
} catch (err: any) {
ctx.status = 500
+61 -7
View File
@@ -1,9 +1,40 @@
import Router from '@koa/router'
import { createReadStream, existsSync, unlinkSync } from 'fs'
import { createReadStream, existsSync, unlinkSync, readFileSync, writeFileSync, mkdirSync, copyFileSync } from 'fs'
import { basename, join } from 'path'
import { tmpdir } from 'os'
import { tmpdir, homedir } from 'os'
import YAML from 'js-yaml'
import * as hermesCli from '../../services/hermes-cli'
const apiServerDefaults = {
enabled: true,
host: '127.0.0.1',
port: 8642,
key: '',
cors_origins: '*',
}
function ensureApiServerConfig(profilePath: string) {
const configPath = join(profilePath, 'config.yaml')
try {
if (!existsSync(configPath)) {
// Profile has no config.yaml — run hermes setup --reset to generate full defaults,
// then inject api_server config (setup itself doesn't add it)
console.log(`[Profile] No config.yaml for ${profilePath}, running setup --reset`)
return { needSetup: true, path: profilePath }
}
const content = readFileSync(configPath, 'utf-8')
const cfg = YAML.load(content) as any || {}
if (!cfg.platforms) cfg.platforms = {}
if (!cfg.platforms.api_server) {
cfg.platforms.api_server = { ...apiServerDefaults }
writeFileSync(configPath, YAML.dump(cfg), 'utf-8')
console.log(`[Profile] Ensured api_server config for: ${profilePath}`)
}
return { needSetup: false, path: profilePath }
} catch { }
return { needSetup: false, path: profilePath }
}
export const profileRoutes = new Router()
// GET /api/profiles - List all profiles
@@ -109,10 +140,10 @@ profileRoutes.put('/api/hermes/profiles/active', async (ctx) => {
}
try {
// 1. Stop gateway (try launchd/systemd first, ignore if unavailable e.g. WSL)
// 1. Stop gateway
try { await hermesCli.stopGateway() } catch { }
// 2. Kill gateway by port if still running (for WSL / background mode)
// 2. Kill gateway by port if still running
try {
const { execSync } = await import('child_process')
const isWin = process.platform === 'win32'
@@ -138,11 +169,34 @@ profileRoutes.put('/api/hermes/profiles/active', async (ctx) => {
const output = await hermesCli.useProfile(name)
await new Promise(r => setTimeout(r, 1000))
// 4. Start gateway — try launchd/systemd first, fall back to background mode
// 4. Ensure api_server config for new profile
try {
await hermesCli.restartGateway()
const detail = await hermesCli.getProfile(name)
console.log(`[Profile] detail.path = ${detail.path}`)
const result = ensureApiServerConfig(detail.path)
if (result?.needSetup) {
// No config.yaml — run setup --reset to create full default config,
// then ensure api_server is present
try { await hermesCli.setupReset() } catch { }
ensureApiServerConfig(detail.path)
}
// Create .env if target has none
const profileEnv = join(detail.path, '.env')
console.log(`[Profile] .env exists: ${existsSync(profileEnv)}, path: ${profileEnv}`)
if (!existsSync(profileEnv)) {
writeFileSync(profileEnv, '# Hermes Agent Environment Configuration\n', 'utf-8')
console.log(`[Profile] Created .env for: ${detail.path}`)
}
} catch (err: any) {
console.error(`[Profile] Ensure config failed:`, err.message)
}
// 5. Start gateway
try {
await hermesCli.startGateway()
console.log('[Profile] Gateway started')
} catch {
// Fallback for WSL / environments without launchd/systemd
// Fallback: background mode (for WSL etc.)
try {
const pid = await hermesCli.startGatewayBackground()
await new Promise(r => setTimeout(r, 3000))
+6 -5
View File
@@ -3,10 +3,10 @@ import axios from 'axios'
import { readFile, writeFile } from 'fs/promises'
import { chmod } from 'fs/promises'
import { resolve } from 'path'
import { homedir } from 'os'
import { restartGateway } from '../../services/hermes-cli'
import { getActiveEnvPath } from '../../services/hermes-profile'
const envPath = resolve(homedir(), '.hermes/.env')
const envPath = () => getActiveEnvPath()
const ILINK_BASE = 'https://ilinkai.weixin.qq.com'
export const weixinRoutes = new Router()
@@ -84,7 +84,7 @@ weixinRoutes.post('/api/hermes/weixin/save', async (ctx) => {
try {
let raw: string
try {
raw = await readFile(envPath, 'utf-8')
raw = await readFile(envPath(), 'utf-8')
} catch {
raw = ''
}
@@ -124,8 +124,9 @@ weixinRoutes.post('/api/hermes/weixin/save', async (ctx) => {
}
let output = result.join('\n').replace(/\n{3,}/g, '\n\n').replace(/\n+$/, '') + '\n'
await writeFile(envPath, output, 'utf-8')
try { await chmod(envPath, 0o600) } catch { /* ignore */ }
const ep = envPath()
await writeFile(ep, output, 'utf-8')
try { await chmod(ep, 0o600) } catch { /* ignore */ }
await restartGateway()
ctx.body = { success: true }