refactor(voice): 语音配置改用新版 API Key 鉴权(弃旧版 appid+token)
用户提醒:火山新版走 API Key 鉴权,不用旧版 appid+access_token。新版 WS 握手只带两个
header——Authorization(Bearer <APIKey>) + X-Api-Resource-Id。
配置从 {appid, access_token, 2×resource-id, voice} 收敛为 {api_key, 2×resource-id,
voice}:APIKey 走 secrets AES 加密入库;ASREnabled/TTSEnabled 改为只看 api_key+对应
resource-id。admin 配置页两个字段并一个 API Key 字段,清单同步改为新版口径。build+tsc 绿。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -267,10 +267,9 @@ export async function saveWechatMP(body: { appid: string; app_secret: string; to
|
|||||||
return { enabled: !!d.enabled };
|
return { enabled: !!d.enabled };
|
||||||
}
|
}
|
||||||
|
|
||||||
// —— 语音(火山豆包)配置 ——
|
// —— 语音(火山豆包)配置 · 新版 API Key 鉴权 ——
|
||||||
export interface VoiceConfig {
|
export interface VoiceConfig {
|
||||||
appid: string;
|
api_key: string; // 明文回显(单管理员后台)
|
||||||
access_token: string; // 明文回显(单管理员后台)
|
|
||||||
asr_resource_id: string;
|
asr_resource_id: string;
|
||||||
tts_resource_id: string;
|
tts_resource_id: string;
|
||||||
tts_voice_type: string;
|
tts_voice_type: string;
|
||||||
@@ -283,15 +282,15 @@ export async function getVoiceConfig(): Promise<VoiceConfig> {
|
|||||||
const d = (await res.json().catch(() => ({}))) as Partial<VoiceConfig> & { error?: string };
|
const d = (await res.json().catch(() => ({}))) as Partial<VoiceConfig> & { error?: string };
|
||||||
if (!res.ok) throw new Error(d.error ?? `voice config failed: ${res.status}`);
|
if (!res.ok) throw new Error(d.error ?? `voice config failed: ${res.status}`);
|
||||||
return {
|
return {
|
||||||
appid: d.appid ?? "", access_token: d.access_token ?? "", asr_resource_id: d.asr_resource_id ?? "",
|
api_key: d.api_key ?? "", asr_resource_id: d.asr_resource_id ?? "",
|
||||||
tts_resource_id: d.tts_resource_id ?? "", tts_voice_type: d.tts_voice_type ?? "",
|
tts_resource_id: d.tts_resource_id ?? "", tts_voice_type: d.tts_voice_type ?? "",
|
||||||
asr_enabled: !!d.asr_enabled, tts_enabled: !!d.tts_enabled,
|
asr_enabled: !!d.asr_enabled, tts_enabled: !!d.tts_enabled,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// access_token 传空串 = 沿用已保存的。
|
// api_key 传空串 = 沿用已保存的。
|
||||||
export async function saveVoiceConfig(body: {
|
export async function saveVoiceConfig(body: {
|
||||||
appid: string; access_token: string; asr_resource_id: string; tts_resource_id: string; tts_voice_type: string;
|
api_key: string; asr_resource_id: string; tts_resource_id: string; tts_voice_type: string;
|
||||||
}): Promise<{ asr_enabled: boolean; tts_enabled: boolean }> {
|
}): Promise<{ asr_enabled: boolean; tts_enabled: boolean }> {
|
||||||
const res = guard(await fetch(`${ADMIN}/voice`, { method: "PUT", headers: authHeaders(true), body: JSON.stringify(body) }));
|
const res = guard(await fetch(`${ADMIN}/voice`, { method: "PUT", headers: authHeaders(true), body: JSON.stringify(body) }));
|
||||||
const d = (await res.json().catch(() => ({}))) as { asr_enabled?: boolean; tts_enabled?: boolean; error?: string };
|
const d = (await res.json().catch(() => ({}))) as { asr_enabled?: boolean; tts_enabled?: boolean; error?: string };
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import { getVoiceConfig, saveVoiceConfig } from "../api";
|
|||||||
// 运维 · 语音设置:火山引擎豆包语音(流式 ASR 耳朵 + 双向流式 TTS 嘴)。
|
// 运维 · 语音设置:火山引擎豆包语音(流式 ASR 耳朵 + 双向流式 TTS 嘴)。
|
||||||
// AccessToken 明文回显(单管理员后台,方便核对;密文仍加密入库)。设计见 VOICE_DESIGN.md。
|
// AccessToken 明文回显(单管理员后台,方便核对;密文仍加密入库)。设计见 VOICE_DESIGN.md。
|
||||||
export function VoiceConfigPage() {
|
export function VoiceConfigPage() {
|
||||||
const [appid, setAppid] = useState("");
|
const [apiKey, setApiKey] = useState("");
|
||||||
const [token, setToken] = useState("");
|
|
||||||
const [asrRes, setAsrRes] = useState("");
|
const [asrRes, setAsrRes] = useState("");
|
||||||
const [ttsRes, setTtsRes] = useState("");
|
const [ttsRes, setTtsRes] = useState("");
|
||||||
const [voice, setVoice] = useState("");
|
const [voice, setVoice] = useState("");
|
||||||
@@ -18,8 +17,7 @@ export function VoiceConfigPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getVoiceConfig()
|
getVoiceConfig()
|
||||||
.then((c) => {
|
.then((c) => {
|
||||||
setAppid(c.appid);
|
setApiKey(c.api_key);
|
||||||
setToken(c.access_token);
|
|
||||||
setAsrRes(c.asr_resource_id);
|
setAsrRes(c.asr_resource_id);
|
||||||
setTtsRes(c.tts_resource_id);
|
setTtsRes(c.tts_resource_id);
|
||||||
setVoice(c.tts_voice_type);
|
setVoice(c.tts_voice_type);
|
||||||
@@ -36,8 +34,7 @@ export function VoiceConfigPage() {
|
|||||||
setOk(false);
|
setOk(false);
|
||||||
try {
|
try {
|
||||||
const r = await saveVoiceConfig({
|
const r = await saveVoiceConfig({
|
||||||
appid: appid.trim(),
|
api_key: apiKey,
|
||||||
access_token: token,
|
|
||||||
asr_resource_id: asrRes.trim(),
|
asr_resource_id: asrRes.trim(),
|
||||||
tts_resource_id: ttsRes.trim(),
|
tts_resource_id: ttsRes.trim(),
|
||||||
tts_voice_type: voice.trim(),
|
tts_voice_type: voice.trim(),
|
||||||
@@ -75,13 +72,9 @@ export function VoiceConfigPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||||
<label className="text-xs text-gray-500">
|
<label className="text-xs text-gray-500 md:col-span-2">
|
||||||
AppID(X-Api-App-Key)
|
API Key(新版鉴权,Authorization: Bearer <APIKey>)
|
||||||
<input value={appid} onChange={(e) => setAppid(e.target.value)} placeholder="火山应用 AppID" className={field} />
|
<input value={apiKey} onChange={(e) => setApiKey(e.target.value)} placeholder="火山控制台生成的 API Key" className={field} />
|
||||||
</label>
|
|
||||||
<label className="text-xs text-gray-500">
|
|
||||||
Access Token(X-Api-Access-Key)
|
|
||||||
<input value={token} onChange={(e) => setToken(e.target.value)} placeholder="火山 Access Token" className={field} />
|
|
||||||
</label>
|
</label>
|
||||||
<label className="text-xs text-gray-500">
|
<label className="text-xs text-gray-500">
|
||||||
ASR Resource-Id
|
ASR Resource-Id
|
||||||
@@ -108,9 +101,9 @@ export function VoiceConfigPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="rounded-xl border border-amber-100 bg-amber-50/40 p-5 text-xs leading-relaxed text-gray-600">
|
<div className="rounded-xl border border-amber-100 bg-amber-50/40 p-5 text-xs leading-relaxed text-gray-600">
|
||||||
<h4 className="mb-2 text-sm font-semibold text-gray-700">去哪拿这些参数</h4>
|
<h4 className="mb-2 text-sm font-semibold text-gray-700">去哪拿这些参数(新版 API Key 鉴权)</h4>
|
||||||
<ol className="list-decimal space-y-1.5 pl-4">
|
<ol className="list-decimal space-y-1.5 pl-4">
|
||||||
<li>火山「豆包语音控制台」→ 你的应用详情页,拿 <b>AppID</b> 与 <b>Access Token</b>(ASR/TTS 通常同一应用共用)</li>
|
<li>火山控制台生成 <b>API Key</b>(新版鉴权,一个 Key 走 Authorization 头,不再用旧版 appid+token)</li>
|
||||||
<li><b>ASR Resource-Id</b>:在「流式语音识别 2.0」文档的鉴权小节(X-Api-Resource-Id 那一栏)</li>
|
<li><b>ASR Resource-Id</b>:在「流式语音识别 2.0」文档的鉴权小节(X-Api-Resource-Id 那一栏)</li>
|
||||||
<li><b>TTS Resource-Id</b>:在「双向流式语音合成」文档的鉴权小节</li>
|
<li><b>TTS Resource-Id</b>:在「双向流式语音合成」文档的鉴权小节</li>
|
||||||
<li><b>音色</b>:控制台「音色管理」里挑一个(JARVIS 建议沉稳男声),填其 voice_type</li>
|
<li><b>音色</b>:控制台「音色管理」里挑一个(JARVIS 建议沉稳男声),填其 voice_type</li>
|
||||||
|
|||||||
@@ -28,12 +28,11 @@ func (h *Handler) loadVoiceConfig(ctx context.Context) voice.Config {
|
|||||||
return c.DecryptFromStore()
|
return c.DecryptFromStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdminGetVoiceConfig: GET /api/v1/admin/voice —— 回显语音配置(token 明文,RequireAdmin 已拦)。
|
// AdminGetVoiceConfig: GET /api/v1/admin/voice —— 回显语音配置(api_key 明文,RequireAdmin 已拦)。
|
||||||
func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
|
func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
|
||||||
cfg := h.loadVoiceConfig(c.Request.Context())
|
cfg := h.loadVoiceConfig(c.Request.Context())
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"appid": cfg.AppID,
|
"api_key": cfg.APIKey,
|
||||||
"access_token": cfg.AccessToken,
|
|
||||||
"asr_resource_id": cfg.ASRResourceID,
|
"asr_resource_id": cfg.ASRResourceID,
|
||||||
"tts_resource_id": cfg.TTSResourceID,
|
"tts_resource_id": cfg.TTSResourceID,
|
||||||
"tts_voice_type": cfg.TTSVoiceType,
|
"tts_voice_type": cfg.TTSVoiceType,
|
||||||
@@ -42,11 +41,10 @@ func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdminSaveVoiceConfig: PUT /api/v1/admin/voice —— 保存语音配置(token 空串=沿用已存)。
|
// AdminSaveVoiceConfig: PUT /api/v1/admin/voice —— 保存语音配置(api_key 空串=沿用已存)。
|
||||||
func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
|
func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
|
||||||
var b struct {
|
var b struct {
|
||||||
AppID string `json:"appid"`
|
APIKey string `json:"api_key"`
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
ASRResourceID string `json:"asr_resource_id"`
|
ASRResourceID string `json:"asr_resource_id"`
|
||||||
TTSResourceID string `json:"tts_resource_id"`
|
TTSResourceID string `json:"tts_resource_id"`
|
||||||
TTSVoiceType string `json:"tts_voice_type"`
|
TTSVoiceType string `json:"tts_voice_type"`
|
||||||
@@ -56,13 +54,12 @@ func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx := c.Request.Context()
|
ctx := c.Request.Context()
|
||||||
token := strings.TrimSpace(b.AccessToken)
|
key := strings.TrimSpace(b.APIKey)
|
||||||
if token == "" {
|
if key == "" {
|
||||||
token = h.loadVoiceConfig(ctx).AccessToken // 留空=沿用已存
|
key = h.loadVoiceConfig(ctx).APIKey // 留空=沿用已存
|
||||||
}
|
}
|
||||||
cfg := voice.Config{
|
cfg := voice.Config{
|
||||||
AppID: strings.TrimSpace(b.AppID),
|
APIKey: key,
|
||||||
AccessToken: token,
|
|
||||||
ASRResourceID: strings.TrimSpace(b.ASRResourceID),
|
ASRResourceID: strings.TrimSpace(b.ASRResourceID),
|
||||||
TTSResourceID: strings.TrimSpace(b.TTSResourceID),
|
TTSResourceID: strings.TrimSpace(b.TTSResourceID),
|
||||||
TTSVoiceType: strings.TrimSpace(b.TTSVoiceType),
|
TTSVoiceType: strings.TrimSpace(b.TTSVoiceType),
|
||||||
|
|||||||
@@ -2,48 +2,45 @@ package voice
|
|||||||
|
|
||||||
import "github.com/sundynix/sundynix-shared/secrets"
|
import "github.com/sundynix/sundynix-shared/secrets"
|
||||||
|
|
||||||
// Config 是语音交互(火山引擎豆包语音)所需配置。AccessToken 加密入库、后台明文回显(同微信配置)。
|
// Config 是语音交互(火山引擎豆包语音)所需配置。APIKey 加密入库、后台明文回显(同微信配置)。
|
||||||
//
|
//
|
||||||
// 火山鉴权走 WS 握手 header:X-Api-App-Key(AppID) / X-Api-Access-Key(AccessToken) /
|
// 用**新版 API Key 鉴权**(非旧版 appid+access_token):WS 握手只带两个 header——
|
||||||
// X-Api-Resource-Id(区分服务,ASR 与双向 TTS 各一个)。AppID/Token 通常同一应用共用。
|
// Authorization(Bearer <APIKey>)+ X-Api-Resource-Id(区分服务,ASR 与双向 TTS 各一个)。
|
||||||
// 端点/音频格式(PCM 16k 单声道等)由代码固定,不入用户配置。
|
// 端点/音频格式(PCM 16k 单声道等)由代码固定,不入用户配置。
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string `json:"appid"` // 火山应用 AppID(X-Api-App-Key)
|
APIKey string `json:"api_key"` // 新版 API Key(Authorization: Bearer <APIKey>,密文入库)
|
||||||
AccessToken string `json:"access_token"` // 火山 Access Token(X-Api-Access-Key,密文入库)
|
|
||||||
ASRResourceID string `json:"asr_resource_id"` // 流式语音识别 X-Api-Resource-Id
|
ASRResourceID string `json:"asr_resource_id"` // 流式语音识别 X-Api-Resource-Id
|
||||||
TTSResourceID string `json:"tts_resource_id"` // 双向流式 TTS X-Api-Resource-Id
|
TTSResourceID string `json:"tts_resource_id"` // 双向流式 TTS X-Api-Resource-Id
|
||||||
TTSVoiceType string `json:"tts_voice_type"` // 音色(如 zh_male_… / BV700_streaming)
|
TTSVoiceType string `json:"tts_voice_type"` // 音色(如 zh_male_… / BV700_streaming)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ASREnabled / TTSEnabled 分别报告耳朵、嘴是否配齐(可各自独立启用)。
|
// ASREnabled / TTSEnabled 分别报告耳朵、嘴是否配齐(共用同一 API Key,各自还需对应 resource-id)。
|
||||||
func (c Config) ASREnabled() bool {
|
func (c Config) ASREnabled() bool { return c.APIKey != "" && c.ASRResourceID != "" }
|
||||||
return c.AppID != "" && c.AccessToken != "" && c.ASRResourceID != ""
|
|
||||||
}
|
|
||||||
func (c Config) TTSEnabled() bool {
|
func (c Config) TTSEnabled() bool {
|
||||||
return c.AppID != "" && c.AccessToken != "" && c.TTSResourceID != "" && c.TTSVoiceType != ""
|
return c.APIKey != "" && c.TTSResourceID != "" && c.TTSVoiceType != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enabled 报告语音整体是否可用(耳朵 + 嘴都配齐)。
|
// Enabled 报告语音整体是否可用(耳朵 + 嘴都配齐)。
|
||||||
func (c Config) Enabled() bool { return c.ASREnabled() && c.TTSEnabled() }
|
func (c Config) Enabled() bool { return c.ASREnabled() && c.TTSEnabled() }
|
||||||
|
|
||||||
// EncryptedForStore 返回 AccessToken 已加密的副本,用于落库。
|
// EncryptedForStore 返回 APIKey 已加密的副本,用于落库。
|
||||||
func (c Config) EncryptedForStore() (Config, error) {
|
func (c Config) EncryptedForStore() (Config, error) {
|
||||||
if c.AccessToken == "" {
|
if c.APIKey == "" {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
enc, err := secrets.Encrypt(c.AccessToken)
|
enc, err := secrets.Encrypt(c.APIKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
c.AccessToken = enc
|
c.APIKey = enc
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecryptFromStore 把库内密文 AccessToken 还原为明文。
|
// DecryptFromStore 把库内密文 APIKey 还原为明文。
|
||||||
func (c Config) DecryptFromStore() Config {
|
func (c Config) DecryptFromStore() Config {
|
||||||
if c.AccessToken != "" {
|
if c.APIKey != "" {
|
||||||
if plain, err := secrets.Decrypt(c.AccessToken); err == nil {
|
if plain, err := secrets.Decrypt(c.APIKey); err == nil {
|
||||||
c.AccessToken = plain
|
c.APIKey = plain
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
|
|||||||
Reference in New Issue
Block a user