新增语音输入(mcp_asr)与健康检查服务标识

语音输入:输入框为空时发送键变麦克风,进入语音模式后按住说话、松手转写并发送;套壳 App 走原生录音桥,浏览器走 MediaRecorder(需 HTTPS)。转写在 mcp_asr,经 /api/asr/transcribe 直达,不经过模型、不消耗 token。

/api/health 增加 service/api/auth_required 字段并设 no-store,供套壳 App 校验手填的服务器地址;免认证路径容忍尾斜杠,避免 /api/health/ 被中间件 401。

接线与文档:compose 新增 mcp_asr;init.sh 默认复用 BIGMODEL_KEY 作为 ASR_API_KEY;README/AGENTS/mcp 文档同步。版本 2.3.0 → 2.4.0。
This commit is contained in:
2026-10-24 16:13:30 +08:00
parent 5b70f7d614
commit 53fa5da82b
24 changed files with 1810 additions and 29 deletions
+71
View File
@@ -0,0 +1,71 @@
// 语音识别代理:浏览器 / 套壳 App 录好的音频 → agent-core → mcp_asr → 文字。
//
// 为什么要 core 中转、而不是前端直连 ASR 容器:mcp_asr 只挂在 agent-mcp-net 内网、
// 一个宿主端口都不占(它自身没有鉴权,暴露即被白嫖)。对外唯一的入口仍是 core,
// 鉴权沿用全局登录态(auth.middleware 已挂在 /api 之前)。
//
// 开关:core 的 ASR_URL 为空 = 未部署语音识别,本路由一律 503,前端据此不显示麦克风。
const express = require('express');
const log = require('../log');
const router = express.Router();
const ASR_URL = String(process.env.ASR_URL || '').trim().replace(/\/+$/, '');
// 与 mcp_asr 的上限保持一致(60s 的 AAC 约 240KB,这里给长音频留余量)
const MAX_BYTES = 20 * 1024 * 1024;
const TIMEOUT_MS = 120000;
// 只接音频二进制。全局 express.json(2mb) 只解析 application/json,两者不冲突。
// 注意 WebView 可能发 audio/mp4、audio/webm;codecs=opus,统一按 audio/* 接。
const rawAudio = express.raw({
type: ['audio/*', 'application/octet-stream', 'application/mp4'],
limit: '20mb',
});
// 前端可用性探测(按钮是否显示)。不碰网络,纯看接线。
router.get('/status', function (req, res) {
res.json({ enabled: !!ASR_URL, max_bytes: MAX_BYTES });
});
router.post('/transcribe', rawAudio, async function (req, res) {
if (!ASR_URL) {
return res.status(503).json({ error: '语音识别未配置(core 未设置 ASR_URL)' });
}
const buf = req.body;
if (!Buffer.isBuffer(buf) || !buf.length) {
return res.status(400).json({ error: '未收到音频数据(Content-Type 需为 audio/*)' });
}
try {
const r = await fetch(ASR_URL + '/transcribe', {
method: 'POST',
headers: { 'Content-Type': req.headers['content-type'] || 'application/octet-stream' },
body: buf,
signal: AbortSignal.timeout(TIMEOUT_MS),
});
const text = await r.text();
if (!r.ok) {
// ASR 服务回的是 {error:string}:解出来再转,不然前端会看到两层套嵌的 JSON
let msg = text.slice(0, 300);
try { msg = JSON.parse(text).error || msg; } catch (_) { /* 非 JSON 保留原文 */ }
log.warn('asr', '识别失败 HTTP ' + r.status + ':' + msg);
// 4xx(静音 / 坏音频 / 过载)原样透传;5xx 统一当上游故障
const code = r.status >= 400 && r.status < 500 ? r.status : 502;
return res.status(code).json({ error: msg });
}
// 原样透传 ASR 的 {text, ms, backend}
res.type('application/json').send(text);
} catch (e) {
log.warn('asr', '调用 ASR 服务失败:' + String(e && e.message || e));
res.status(502).json({ error: '语音识别服务不可用:' + String(e && e.message || e) });
}
});
// express.raw 超限时抛 entity.too.large,默认会回 HTML;这里翻成前端能读的 JSON。
router.use(function (err, req, res, next) {
if (err && (err.type === 'entity.too.large' || err.status === 413)) {
return res.status(413).json({ error: '音频过大(上限 ' + Math.round(MAX_BYTES / 1048576) + 'MB)' });
}
next(err);
});
module.exports = router;
+1 -1
View File
@@ -15,7 +15,7 @@ const router = express.Router();
const STARTED_AT = Date.now();
// 版本以本文件字面量为准:容器部署只挂载 app/server,镜像里的 package.json 可能是旧的,
// 读它会一直显示发版时的版本号(之前 2.1.0 改完仍显 2.0.0 即此因)。发版时记得同步这里。
const VERSION = '2.3.0';
const VERSION = '2.4.0';
// git 提交号单独返回(提交时 post-commit 钩子自动生成),前端只显示简洁版本号
const VERSION_COMMIT = versionInfo.commit || '';