Files
api_health/agent/infra/media/asr.js
T
lik 8c6feaf42d feat: 新增语音/图像识别能力(媒体会话、识别接口与路由)
识别只返回文本,不投递、不落库:语音支持 mp3/aac/wav/pcm,WebM 先按文件头识别再转 PCM;图片走 qwen-vl-ocr。新增 /health/ai/asr 与 /health/ai/ocr,登录后按用户限流 20 次/分钟,并对 /health/ai/ 前缀放宽请求体上限。
2026-10-31 17:02:42 +08:00

212 lines
7.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 语音识别适配器:会话工厂
*
* 一轮输入一个识别会话:首帧建立,分片推流,finish() 取定稿后关闭。
*
* 供应商接入方式(单家、配置切换):
* - 本地假 ASR / 自建服务:传入 `transcribe(audio, meta) => Promise<string>`
* - 通用 HTTP 供应商:配置 `endpoint`(+ 密钥),POST JSON
* `{ audio: base64, codec, rate, channels }`,响应取 `{ text }`
* - DashScope 实时:配置 `provider: "dashscope"` 与 `endpoint`(默认官方地址),
* 模型默认 fun-asr-realtime,密钥取 apiKey / apiKeyEnv
* - 都没有:finish() 抛「未配置」,由调用方回可读错误
*
* 密钥只从配置或环境变量读取,不回显给调用方。
*/
import logger from '../../../utils/logger.js';
import { createDashscopeRecognizer, DASHSCOPE_REALTIME_ENDPOINT, DASHSCOPE_DEFAULT_MODEL } from './asr_dashscope.js';
/** 音频参数(首版固定单声道,采样率由调用方按录制参数传入) */
export const DEFAULT_AUDIO_FORMAT = { codec: 'mp3', rate: 16000, channels: 1 };
/**
* 通用 HTTP 识别函数:把整段音频 POST 给供应商,取 `{ text }`
* @param {string} endpoint - 识别服务地址
* @param {string} apiKey - 密钥(可为空)
* @param {string} requestId - 请求标识(仅日志关联用)
* @returns {Function} transcribe(audio, meta)
*/
function makeHttpTranscriber(endpoint, apiKey, requestId = '') {
return async (audio, meta) => {
const headers = { 'Content-Type': 'application/json' };
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 60000);
try {
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
audio: audio.toString('base64'),
codec: meta.codec,
rate: meta.rate,
channels: meta.channels
}),
signal: controller.signal
});
if (!response.ok) {
throw new Error(`asr http ${response.status}`);
}
const data = await response.json();
return data?.text || '';
} catch (err) {
if (err.name === 'AbortError') {
throw new Error('asr request timeout');
}
throw err;
} finally {
clearTimeout(timer);
logger.debug('asr http transcriber done', { requestId });
}
};
}
/**
* 批式识别会话(HTTP / 自定义 transcribe)
*
* @param {Object} [options]
* @param {Function} [options.transcribe] - 自定义识别函数(优先于 endpoint)
* @param {string} [options.endpoint] - HTTP 识别服务地址
* @param {string} [options.apiKey] - 显式密钥(优先于环境变量)
* @param {string} [options.apiKeyEnv] - 密钥环境变量名
* @param {string} [options.codec] - 音频编码(默认 mp3)
* @param {number} [options.rate] - 采样率(默认 16000)
* @param {number} [options.channels] - 声道数(默认 1)
* @param {string} [options.requestId] - 请求标识(仅日志关联用)
* @param {Object} [options.logger] - 日志实例
* @param {Function} [options.onError] - 识别过程异步失败回调
* @returns {{ push: Function, finish: Function, abort: Function }}
*/
function createBatchRecognizer({
transcribe = null,
endpoint = '',
apiKey = '',
apiKeyEnv = '',
codec = DEFAULT_AUDIO_FORMAT.codec,
rate = DEFAULT_AUDIO_FORMAT.rate,
channels = DEFAULT_AUDIO_FORMAT.channels,
requestId = '',
logger: customLogger = null,
onError = null
} = {}) {
const _logger = customLogger || logger;
const key = apiKey || (apiKeyEnv ? process.env[apiKeyEnv] : '') || '';
const runTranscribe = transcribe || (endpoint ? makeHttpTranscriber(endpoint, key, requestId) : null);
const chunks = [];
let aborted = false;
let finished = false;
return {
/**
* 推入一个音频分片(原始字节)
* @param {Buffer} buffer - 分片
*/
push(buffer) {
if (aborted || finished || !buffer?.length) return;
chunks.push(buffer);
},
/**
* 结束推流并取定稿文本
* @returns {Promise<string>} 定稿(可能为空字符串,由调用方决定是否报错)
*/
async finish() {
if (aborted) return '';
finished = true;
const audio = Buffer.concat(chunks);
chunks.length = 0;
if (!runTranscribe) {
throw new Error('asr provider not configured');
}
if (audio.length === 0) return '';
const startedAt = Date.now();
try {
const text = (await runTranscribe(audio, { codec, rate, channels }) || '').trim();
_logger.info('asr finished', {
requestId,
bytes: audio.length,
codec,
elapsedMs: Date.now() - startedAt,
chars: text.length
});
return text;
} catch (err) {
_logger.warn('asr session failed', {
requestId,
bytes: audio.length,
codec,
elapsedMs: Date.now() - startedAt,
error: err.message
});
onError?.(err);
throw err;
}
},
/** 中止本轮识别:丢弃缓冲,之后 finish 返回空串 */
abort() {
aborted = true;
chunks.length = 0;
}
};
}
/**
* 创建一轮识别会话(按配置选择实时或批式供应商)
*
* @param {Object} [options]
* @param {string} [options.provider] - 供应商名(dashscope 时为实时 WebSocket)
* @param {Function} [options.transcribe] - 自定义识别函数(优先于 endpoint/provider)
* @param {string} [options.endpoint] - 识别服务地址(ws/wss 或 http/https)
* @param {string} [options.model] - 实时模型名(默认 fun-asr-realtime)
* @param {string} [options.apiKey] - 显式密钥(优先于环境变量)
* @param {string} [options.apiKeyEnv] - 密钥环境变量名
* @param {string} [options.codec] - 音频编码(默认 mp3)
* @param {number} [options.rate] - 采样率(默认 16000)
* @param {number} [options.channels] - 声道数(默认 1)
* @param {string} [options.requestId] - 请求标识(仅日志关联用)
* @param {Object} [options.logger] - 日志实例
* @param {Function} [options.onPartial] - 实时中间结果回调(仅实时供应商)
* @param {Function} [options.onError] - 识别过程异步失败回调
* @returns {{ push: Function, finish: Function, abort: Function }}
*/
export function createRecognizer(options = {}) {
const {
provider = '',
endpoint = '',
transcribe = null
} = options;
// 显式自定义识别函数优先:单测 / 本地假 ASR 仍走批式会话
if (transcribe) {
return createBatchRecognizer(options);
}
const useRealtime = provider === 'dashscope' ||
(!provider && /^wss?:\/\//i.test(endpoint));
if (useRealtime) {
return createDashscopeRecognizer({
...options,
endpoint: endpoint || DASHSCOPE_REALTIME_ENDPOINT
});
}
return createBatchRecognizer(options);
}
export {
createDashscopeRecognizer,
createBatchRecognizer,
DASHSCOPE_REALTIME_ENDPOINT,
DASHSCOPE_DEFAULT_MODEL
};
export default createRecognizer;