feat(settings): M3 全局设置 + voice 动态路由 + env 收敛
M3 完成:env 业务配置全进 DB,WebUI 可改,改完立即生效(无需重启)。
新增:
- controller/settings.go: 3 个 handler
- GET /api/settings: 读所有设置,api_key 永远打码
- PUT /api/settings: 部分更新(白名单字段);default_speaker 改时校验
音色必须在 voices 表里(避免引用不存在);改完调 LoadRuntimeConfig 即时生效
- PUT /api/settings/api-key: 独立端点,只改 api_key
改造:
- setting/config.go: 用 LoadRuntimeConfig(store) 替代 InitTTSConfig
- 不再读 BYTEDANCE_TTS_* env,完全从 store.Settings 拿
- 必填项缺失返 error,设 TTSConfigErr(/v1/audio/speech 返 503)
- 定义 Store 接口(避免 setting → store 循环 import)
- 字段映射: BYTEDANCE_TTS_API_KEY → api_key, BYTEDANCE_TTS_RESOURCE_ID
→ default_resource_id, ... 全部一一对应
- controller/tts.go: voice 路由
- voice 为空 → 用 setting.TTSOptions.Speaker (即 store.default_speaker)
- voice 非空 → 查 voices 表,替换 opts.Speaker / ResourceID / Model
- 命中但 enabled=0 → 仍可用(显式 voice 覆盖)
- 未命中 → 400 unknown_voice
- voice 路由日志: "[tts] voice=xxx 命中 (speaker=... resource=... model=...)"
- router/admin.html: 加 #settings tab (24.78KB, 仍 <30KB 预算)
- API key 区:打码显示 + 独立更新按钮
- 路由/格式区:default_resource_id / default_speaker (下拉源 /api/voices) /
default_format / sample_rate / model
- "保存" 按钮 PUT /api/settings + 立即 reload (settingsOk 提示)
- router/router.go: 挂 /api/settings[/{api-key}] 三路由
- main.go: Detect 之后调 LoadRuntimeConfig,失败也允许进入
(env 阶段无 db,LogStartupSummary 仍显示 BYTEDANCE_TTS_* 必填项用于诊断)
测试(都被 .gitignore 排除,本地保留):
- 已有 store/installer/middleware/router/telemetry 测试保持全绿
- e2e 验证(本机跑通 9 场景):
- install 写入 3 voices + 7 settings
- 重启后 main.go log: "TTS 运行时配置已加载(api_key=***, speaker=default-voice, resource=volc.megatts.icl, format=wav)" — 确认走 DB 不读 env
- GET /api/settings 返所有字段
- voice=clone-voice-1 命中 S_clone_1 + seed-tts-2.0-expressive
- voice=clone-voice-2 命中 S_clone_2 + volc.megatts.icl-2
- voice=unknown 返 400 unknown_voice
- PUT /api/settings 改 default_speaker 即时生效
- PUT /api/settings/api-key 改 key 即时生效
- PUT /api/settings default_speaker=ghost 返 400 default_speaker_missing
注意: e2e 里的 TTS 500 是因为我用假 key 调上游,路由逻辑本身正确。
换真火山 API key 即可 work。
二进制大小: ~17MB → ~18MB (+1MB)
未 push(等用户测试 M1+M2+speaker fix 全部稳定后,跟 51bd7ae+70865a3 一起或单独 push)
This commit is contained in:
+118
-1
@@ -155,6 +155,7 @@
|
||||
<div class="tabs">
|
||||
<div class="tab" :class="{active: tab==='dashboard'}" @click="tab='dashboard'">仪表盘</div>
|
||||
<div class="tab" :class="{active: tab==='voices'}" @click="tab='voices'">音色</div>
|
||||
<div class="tab" :class="{active: tab==='settings'}" @click="tab='settings'">设置</div>
|
||||
</div>
|
||||
|
||||
<!-- Dashboard -->
|
||||
@@ -224,6 +225,64 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Settings -->
|
||||
<div v-if="tab==='settings'">
|
||||
<div class="card">
|
||||
<div class="card-title">火山 TTS 凭证</div>
|
||||
<div class="card-desc" style="color: var(--text-dim); font-size: 12px; margin-bottom: 16px;">改完点保存,立即生效,无需重启服务。</div>
|
||||
|
||||
<div class="field">
|
||||
<label>API Key (打码显示: <span style="color: var(--accent); font-family: monospace;">{{ settings.api_key || '(未设置)' }}</span>)</label>
|
||||
<div style="display: flex; gap: 8px;">
|
||||
<input type="password" v-model="apiKeyInput" :placeholder="settings.api_key_set ? '不改留空' : '输入新 API Key'" style="flex: 1;">
|
||||
<button class="btn btn-primary" @click="saveApiKey" :disabled="!apiKeyInput">更新 Key</button>
|
||||
</div>
|
||||
<div class="hint">独立端点 /api/settings/api-key,与其他字段分开传输</div>
|
||||
</div>
|
||||
|
||||
<div style="height: 1px; background: var(--border); margin: 20px 0;"></div>
|
||||
|
||||
<div class="card-title" style="font-size: 13px;">路由与格式</div>
|
||||
<div class="field">
|
||||
<label>默认资源 ID</label>
|
||||
<input v-model="settingsForm.default_resource_id" placeholder="volc.megatts.default">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>默认音色 (name)</label>
|
||||
<select v-model="settingsForm.default_speaker">
|
||||
<option value="">—</option>
|
||||
<option v-for="v in voices" :key="v.name" :value="v.name">{{ v.name }} ({{ v.speaker }})</option>
|
||||
</select>
|
||||
<div class="hint">未传 voice 时使用。必须是"音色" tab 里存在的 name</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>默认输出格式</label>
|
||||
<select v-model="settingsForm.default_format">
|
||||
<option value="mp3">mp3</option>
|
||||
<option value="wav">wav</option>
|
||||
<option value="opus">opus (ogg_opus)</option>
|
||||
<option value="pcm">pcm</option>
|
||||
<option value="aac">aac</option>
|
||||
<option value="flac">flac</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>采样率 (Hz)</label>
|
||||
<input type="number" v-model.number="settingsForm.sample_rate" min="8000" max="48000" step="1000">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Model (可选)</label>
|
||||
<input v-model="settingsForm.model" placeholder="seed-tts-2.0-standard">
|
||||
</div>
|
||||
<div v-if="settingsErr" class="err">{{ settingsErr }}</div>
|
||||
<div v-if="settingsOk" class="ok" style="color: var(--success); font-size: 12px; margin-top: 8px;">✓ 已保存,运行时已 reload</div>
|
||||
<div style="margin-top: 16px;">
|
||||
<button class="btn btn-primary" @click="saveSettings" :disabled="savingSettings">{{ savingSettings ? '保存中...' : '保存设置' }}</button>
|
||||
<button class="btn" @click="resetSettingsForm" style="margin-left: 8px;">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="actionErr" class="err" style="margin-top: 16px;">⚠ {{ actionErr }}</div>
|
||||
</div>
|
||||
|
||||
@@ -268,6 +327,14 @@
|
||||
const addErr = ref('');
|
||||
const form = ref({ name: '', speaker: '', resource_id: 'volc.megatts.icl', model: 'seed-tts-2.0-standard', language: '', description: '' });
|
||||
|
||||
// 设置页 (M3)
|
||||
const settings = ref({});
|
||||
const settingsForm = ref({ default_resource_id: '', default_speaker: '', default_format: 'mp3', sample_rate: 24000, model: '' });
|
||||
const apiKeyInput = ref('');
|
||||
const savingSettings = ref(false);
|
||||
const settingsErr = ref('');
|
||||
const settingsOk = ref(false);
|
||||
|
||||
const http = axios.create({ baseURL: '/api' });
|
||||
http.interceptors.request.use(c => {
|
||||
if (apiKey.value) c.headers.Authorization = 'Bearer ' + apiKey.value;
|
||||
@@ -281,7 +348,7 @@
|
||||
return Promise.reject(err);
|
||||
});
|
||||
|
||||
watch(tab, v => { location.hash = v; });
|
||||
watch(tab, v => { location.hash = v; if (v === 'settings') loadSettings(); });
|
||||
window.addEventListener('hashchange', () => { tab.value = location.hash.slice(1) || 'dashboard'; });
|
||||
|
||||
const login = async () => {
|
||||
@@ -307,8 +374,56 @@
|
||||
voices.value = r.data.voices || [];
|
||||
} catch (e) { actionErr.value = '加载音色失败: ' + e.message; }
|
||||
};
|
||||
const loadSettings = async () => {
|
||||
try {
|
||||
const r = await http.get('/settings');
|
||||
settings.value = r.data;
|
||||
settingsForm.value = {
|
||||
default_resource_id: r.data.default_resource_id || '',
|
||||
default_speaker: r.data.default_speaker || '',
|
||||
default_format: r.data.default_format || 'mp3',
|
||||
sample_rate: r.data.sample_rate || 24000,
|
||||
model: r.data.model || '',
|
||||
};
|
||||
settingsOk.value = false;
|
||||
} catch (e) { settingsErr.value = '加载设置失败: ' + (e.response?.data?.error?.message || e.message); }
|
||||
};
|
||||
const reloadAll = () => { loadOverview(); loadVoices(); };
|
||||
|
||||
const saveSettings = async () => {
|
||||
settingsErr.value = ''; settingsOk.value = false;
|
||||
savingSettings.value = true;
|
||||
try {
|
||||
await http.put('/settings', {
|
||||
default_resource_id: settingsForm.value.default_resource_id,
|
||||
default_speaker: settingsForm.value.default_speaker,
|
||||
default_format: settingsForm.value.default_format,
|
||||
sample_rate: settingsForm.value.sample_rate,
|
||||
model: settingsForm.value.model,
|
||||
});
|
||||
await loadSettings();
|
||||
await loadOverview();
|
||||
settingsOk.value = true;
|
||||
setTimeout(() => settingsOk.value = false, 3000);
|
||||
} catch (e) {
|
||||
settingsErr.value = e.response?.data?.error?.message || e.message;
|
||||
} finally { savingSettings.value = false; }
|
||||
};
|
||||
const saveApiKey = async () => {
|
||||
settingsErr.value = ''; settingsOk.value = false;
|
||||
if (!apiKeyInput.value) { settingsErr.value = 'API Key 不能为空'; return; }
|
||||
try {
|
||||
await http.put('/settings/api-key', { api_key: apiKeyInput.value });
|
||||
apiKeyInput.value = '';
|
||||
await loadSettings();
|
||||
settingsOk.value = true;
|
||||
setTimeout(() => settingsOk.value = false, 3000);
|
||||
} catch (e) {
|
||||
settingsErr.value = e.response?.data?.error?.message || e.message;
|
||||
}
|
||||
};
|
||||
const resetSettingsForm = () => { loadSettings(); };
|
||||
|
||||
const openAdd = () => {
|
||||
form.value = { name: '', speaker: '', resource_id: 'volc.megatts.icl', model: 'seed-tts-2.0-standard', language: '', description: '' };
|
||||
addErr.value = '';
|
||||
@@ -351,6 +466,8 @@
|
||||
|
||||
return { apiKey, keyInput, loginErr, login, logout, tab, overview, voices, actionErr,
|
||||
showAdd, form, addErr, adding, openAdd, submitAdd, toggle, remove,
|
||||
settings, settingsForm, apiKeyInput, savingSettings, settingsErr, settingsOk,
|
||||
loadSettings, saveSettings, saveApiKey, resetSettingsForm,
|
||||
formatUptime, shortPath, reloadAll };
|
||||
},
|
||||
}).mount('#app');
|
||||
|
||||
Reference in New Issue
Block a user