识别只返回文本,不投递、不落库:语音支持 mp3/aac/wav/pcm,WebM 先按文件头识别再转 PCM;图片走 qwen-vl-ocr。新增 /health/ai/asr 与 /health/ai/ocr,登录后按用户限流 20 次/分钟,并对 /health/ai/ 前缀放宽请求体上限。
134 lines
4.1 KiB
JavaScript
134 lines
4.1 KiB
JavaScript
import { randomUUID } from "node:crypto";
|
|
import {
|
|
MEDIA_LIMITS,
|
|
MediaInputError,
|
|
decodeBase64Media,
|
|
normalizeAudioMeta,
|
|
normalizeImageMeta,
|
|
isAsrConfigured,
|
|
isOcrConfigured,
|
|
recognizeAudio,
|
|
recognizeImage,
|
|
} from "../agent/infra/media/index.js";
|
|
import logger from "../utils/logger.js";
|
|
import ResponseUtil from "../utils/responseUtil.js";
|
|
|
|
/** 供应商未配置 / 配置错误:回 503,前端提示「服务暂不可用」而不是「重试」 */
|
|
const CONFIG_ERROR = /not configured|api key missing|invalid (asr|ocr) endpoint/i;
|
|
|
|
/**
|
|
* 语音 / 图像识别接口
|
|
*
|
|
* 只做识别:返回文本给客户端填入输入框,用户确认后再走原聊天链路;
|
|
* 音频/图片只在本次请求内存中存在,不落盘、不写日志正文。
|
|
*/
|
|
class HandlerAIMedia {
|
|
/**
|
|
* POST /health/ai/asr
|
|
* body: { audio: base64, format?: mp3|aac|wav|pcm, sampleRate?, channels? }
|
|
*/
|
|
async transcribe(ctx) {
|
|
const requestId = `asr_${randomUUID().replace(/-/g, "").slice(0, 12)}`;
|
|
const startedAt = Date.now();
|
|
let bytes = 0;
|
|
|
|
try {
|
|
if (!isAsrConfigured()) {
|
|
return ResponseUtil.error(ctx, "语音识别服务暂不可用", null, 503);
|
|
}
|
|
|
|
const body = ctx.request.body ?? {};
|
|
const meta = normalizeAudioMeta(body);
|
|
const audio = decodeBase64Media(body.audio, {
|
|
maxBytes: MEDIA_LIMITS.maxAudioBytes,
|
|
label: "音频",
|
|
});
|
|
bytes = audio.length;
|
|
|
|
const text = await recognizeAudio(audio, { ...meta, requestId });
|
|
if (!text) {
|
|
return ResponseUtil.badRequest(ctx, "未识别到语音内容,请重新录制");
|
|
}
|
|
|
|
logger.info("asr api finished", {
|
|
requestId,
|
|
bytes,
|
|
format: meta.format,
|
|
elapsedMs: Date.now() - startedAt,
|
|
chars: text.length,
|
|
});
|
|
return ResponseUtil.success(ctx, { text });
|
|
} catch (err) {
|
|
return this._fail(ctx, err, { kind: "asr", requestId, bytes, startedAt });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST /health/ai/ocr
|
|
* body: { image: base64, mime?: image/jpeg|png|webp, width?, height?, kind?: auto }
|
|
*/
|
|
async ocr(ctx) {
|
|
const requestId = `ocr_${randomUUID().replace(/-/g, "").slice(0, 12)}`;
|
|
const startedAt = Date.now();
|
|
let bytes = 0;
|
|
|
|
try {
|
|
if (!isOcrConfigured()) {
|
|
return ResponseUtil.error(ctx, "图片识别服务暂不可用", null, 503);
|
|
}
|
|
|
|
const body = ctx.request.body ?? {};
|
|
const meta = normalizeImageMeta(body);
|
|
const image = decodeBase64Media(body.image, {
|
|
maxBytes: MEDIA_LIMITS.maxImageBytes,
|
|
label: "图片",
|
|
});
|
|
bytes = image.length;
|
|
|
|
const text = await recognizeImage(image, { ...meta, requestId });
|
|
if (!text) {
|
|
return ResponseUtil.badRequest(ctx, "未识别到图片文字,请重新拍摄或上传更清晰的图片");
|
|
}
|
|
|
|
logger.info("ocr api finished", {
|
|
requestId,
|
|
bytes,
|
|
mime: meta.mime,
|
|
elapsedMs: Date.now() - startedAt,
|
|
chars: text.length,
|
|
});
|
|
return ResponseUtil.success(ctx, { text });
|
|
} catch (err) {
|
|
return this._fail(ctx, err, { kind: "ocr", requestId, bytes, startedAt });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 统一失败出口:参数错误回 400,配置缺失回 503,供应商失败回 502。
|
|
* 详细原因只进日志,不回传供应商报文,避免泄露内部实现与密钥相关信息。
|
|
*/
|
|
_fail(ctx, err, { kind, requestId, bytes, startedAt }) {
|
|
const elapsedMs = Date.now() - startedAt;
|
|
|
|
if (err instanceof MediaInputError || err.status === 400) {
|
|
return ResponseUtil.badRequest(ctx, err.message);
|
|
}
|
|
|
|
logger.warn(`${kind} api failed`, {
|
|
requestId,
|
|
bytes,
|
|
elapsedMs,
|
|
error: err?.message ?? String(err),
|
|
});
|
|
|
|
const label = kind === "asr" ? "语音" : "图片";
|
|
if (CONFIG_ERROR.test(err?.message ?? "")) {
|
|
return ResponseUtil.error(ctx, `${label}识别服务暂不可用`, null, 503);
|
|
}
|
|
return ResponseUtil.error(ctx, `${label}识别失败,请稍后重试`, null, 502);
|
|
}
|
|
}
|
|
|
|
export { HandlerAIMedia };
|
|
export default HandlerAIMedia;
|