语音识别上游参数改为随请求带入,init.sh 不再绑定平台
- mcp_asr 不再从容器 env 读 key/base_url/model,改为逐请求 header (Authorization / X-ASR-Base-Url / X-ASR-Model),容器不存任何平台密钥 - core 的 /api/asr 从 config.yml 的 mcp_servers[asr].headers 取值转发, /status 增加 reason(未接线时前端 console 直接打印原因) - init.sh 只自动启用模型平台与 web_search;image_task / video_task / asr 改为带默认 url 与 header 名的注释模板,删掉 ASR_API_* 输入与 BIGMODEL_KEY 复用 - docker-compose 去掉 mcp_asr 的三个上游 env - 同步 README / AGENTS / app/config.example.yml / mcp 文档
This commit is contained in:
@@ -4,8 +4,20 @@
|
||||
// 一个宿主端口都不占(它自身没有鉴权,暴露即被白嫖)。对外唯一的入口仍是 core,
|
||||
// 鉴权沿用全局登录态(auth.middleware 已挂在 /api 之前)。
|
||||
//
|
||||
// 开关:core 的 ASR_URL 为空 = 未部署语音识别,本路由一律 503,前端据此不显示麦克风。
|
||||
// 上游参数全部来自 config.yml 的 mcp_servers 里那条 asr(与 image_task 完全同构):
|
||||
// - name: asr
|
||||
// url: "http://mcp_asr:3000/mcp"
|
||||
// headers:
|
||||
// Authorization: Bearer <key> # 免鉴权的自建服务可省
|
||||
// X-ASR-Base-Url: "https://.../v1"
|
||||
// X-ASR-Model: "whisper-1"
|
||||
// mcp_asr 自己不存任何平台密钥(容器 env 一条都不读),所以要换 ASR 平台只改这个文件对应的
|
||||
// YAML 段 + 重启 core,不碰 .env、不重建镜像。头名与 mcp/asr/src/server.js 的常量一一对应。
|
||||
//
|
||||
// 开关:ASR_URL 为空(未部署 mcp_asr)或配置里没有可用的 asr 段 = 关闭,本路由一律 503,
|
||||
// 前端据此不显示麦克风。ASR_URL 留在 env 是部署接线(容器地址),不属于平台密钥。
|
||||
const express = require('express');
|
||||
const engine = require('../engine');
|
||||
const log = require('../log');
|
||||
|
||||
const router = express.Router();
|
||||
@@ -15,6 +27,39 @@ const ASR_URL = String(process.env.ASR_URL || '').trim().replace(/\/+$/, '');
|
||||
const MAX_BYTES = 20 * 1024 * 1024;
|
||||
const TIMEOUT_MS = 120000;
|
||||
|
||||
// 只转发 ASR 约定的三个头:config.yml 里该条目的其它 header 与本服务无关,没必要透传进内网。
|
||||
const PASS_HEADERS = ['authorization', 'x-asr-base-url', 'x-asr-model'];
|
||||
|
||||
// 取当前生效的 asr 配置段。配置冷加载(engine 里那份是启动时解析的),所以这里读到的就是
|
||||
// 运行中的值;重启前改 config.yml 不会影响已生效的这份,与其它配置项一致。
|
||||
function asrEntry() {
|
||||
let cfg = null;
|
||||
try { cfg = engine.getConfig(); } catch (_) { cfg = null; } // 启动早期 engine 还没拿到 cfg
|
||||
const list = (cfg && cfg.mcp_servers) || [];
|
||||
const s = list.find(function (x) { return x && x.name === 'asr'; });
|
||||
// mode: disable = 明确关闭(与模型侧工具同一开关:关了就别给麦克风)
|
||||
if (!s || s.mode === 'disable') return null;
|
||||
return s;
|
||||
}
|
||||
|
||||
function asrHeaders() {
|
||||
const s = asrEntry();
|
||||
if (!s) return null;
|
||||
const out = {};
|
||||
for (const [k, v] of Object.entries(s.headers || {})) {
|
||||
if (PASS_HEADERS.indexOf(String(k).toLowerCase()) >= 0) out[k] = String(v);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// 有没有真的能转写:缺 base_url 就转不了(model 也缺的话 mcp_asr 会给出更具体的报错)。
|
||||
// 只判 base_url 是为了让「配了但漏填 model」在探测阶段仍显示入口,用户点了能拿到明确提示。
|
||||
function hasUpstream(headers) {
|
||||
return Object.keys(headers || {}).some(function (k) {
|
||||
return k.toLowerCase() === 'x-asr-base-url' && String(headers[k] || '').trim();
|
||||
});
|
||||
}
|
||||
|
||||
// 只接音频二进制。全局 express.json(2mb) 只解析 application/json,两者不冲突。
|
||||
// 注意 WebView 可能发 audio/mp4、audio/webm;codecs=opus,统一按 audio/* 接。
|
||||
const rawAudio = express.raw({
|
||||
@@ -24,12 +69,22 @@ const rawAudio = express.raw({
|
||||
|
||||
// 前端可用性探测(按钮是否显示)。不碰网络,纯看接线。
|
||||
router.get('/status', function (req, res) {
|
||||
res.json({ enabled: !!ASR_URL, max_bytes: MAX_BYTES });
|
||||
if (!ASR_URL) return res.json({ enabled: false, reason: 'core 未设置 ASR_URL' });
|
||||
if (!asrEntry()) return res.json({ enabled: false, reason: 'config.yml 的 mcp_servers 里没有启用的 asr' });
|
||||
const headers = asrHeaders();
|
||||
if (!hasUpstream(headers)) {
|
||||
return res.json({ enabled: false, reason: 'config.yml 的 asr.headers 缺 X-ASR-Base-Url' });
|
||||
}
|
||||
res.json({ enabled: true, max_bytes: MAX_BYTES });
|
||||
});
|
||||
|
||||
router.post('/transcribe', rawAudio, async function (req, res) {
|
||||
if (!ASR_URL) {
|
||||
return res.status(503).json({ error: '语音识别未配置(core 未设置 ASR_URL)' });
|
||||
return res.status(503).json({ error: '语音识别未部署(core 未设置 ASR_URL)' });
|
||||
}
|
||||
const headers = asrHeaders();
|
||||
if (!headers || !hasUpstream(headers)) {
|
||||
return res.status(503).json({ error: '语音识别未配置上游(config.yml 的 asr.headers 需设 X-ASR-Base-Url / X-ASR-Model)' });
|
||||
}
|
||||
const buf = req.body;
|
||||
if (!Buffer.isBuffer(buf) || !buf.length) {
|
||||
@@ -38,7 +93,7 @@ router.post('/transcribe', rawAudio, async function (req, res) {
|
||||
try {
|
||||
const r = await fetch(ASR_URL + '/transcribe', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': req.headers['content-type'] || 'application/octet-stream' },
|
||||
headers: Object.assign({ 'Content-Type': req.headers['content-type'] || 'application/octet-stream' }, headers),
|
||||
body: buf,
|
||||
signal: AbortSignal.timeout(TIMEOUT_MS),
|
||||
});
|
||||
@@ -52,7 +107,7 @@ router.post('/transcribe', rawAudio, async function (req, res) {
|
||||
const code = r.status >= 400 && r.status < 500 ? r.status : 502;
|
||||
return res.status(code).json({ error: msg });
|
||||
}
|
||||
// 原样透传 ASR 的 {text, ms, backend}
|
||||
// 原样透传 ASR 的 {text, ms, model}
|
||||
res.type('application/json').send(text);
|
||||
} catch (e) {
|
||||
log.warn('asr', '调用 ASR 服务失败:' + String(e && e.message || e));
|
||||
|
||||
Reference in New Issue
Block a user