/** * 图像识别适配器:会话工厂 * * 与 asr.js 保持一致:一轮一张(或一页)图片一个会话,接口统一为 * { push(buffer), finish() -> Promise, abort() },调用方不区分模态。 * * 供应商接入方式(单家、配置切换): * - 通义千问(provider=qwen,缺省):DashScope OpenAI 兼容 chat/completions 整图识别 * - 自建服务:传入 `recognize(image, meta) => Promise`(本地 PaddleOCR 等) * - 通用 HTTP:配置 `endpoint`,POST JSON `{ image: base64, mime, kind }`,响应取 `{ text }` * - 三者都没有:finish() 抛「未配置」,由调用方回可读错误 * * 图片不做流式:分片只用于搬运大文件,`finish()` 时一次性提交; * 原图只在本轮内存里存在,不落盘、不写日志。 */ import logger from '../../../utils/logger.js'; import { createQwenVisionSession, DASHSCOPE_CHAT_ENDPOINT, DEFAULT_VISION_MODEL, IMAGE_KINDS } from './ocr_qwen.js'; /** 图片参数(首版固定:JPEG/PNG/WebP,客户端负责压缩与纠偏) */ export const DEFAULT_IMAGE_FORMAT = { mimes: ['image/jpeg', 'image/png', 'image/webp'] }; export { IMAGE_KINDS, DASHSCOPE_CHAT_ENDPOINT, DEFAULT_VISION_MODEL }; /** * 通用 HTTP 识别函数:把整张图 POST 给供应商,取 `{ text }` * @param {string} endpoint - 识别服务地址 * @param {string} apiKey - 密钥(可为空) * @returns {Function} recognize(image, meta) */ function makeHttpRecognizer(endpoint, apiKey) { return async (image, 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({ image: image.toString('base64'), mime: meta.mime, kind: meta.kind, width: meta.width, height: meta.height }), signal: controller.signal }); if (!response.ok) throw new Error(`ocr http ${response.status}`); const data = await response.json(); return data?.text || ''; } catch (err) { if (err.name === 'AbortError') throw new Error('ocr request timeout'); throw err; } finally { clearTimeout(timer); } }; } /** * 批式识别会话(自定义 recognize / 通用 HTTP) * * @param {Object} [options] * @param {Function} [options.recognize] - 自定义识别函数(优先于 endpoint) * @param {string} [options.endpoint] - HTTP 识别服务地址 * @param {string} [options.apiKey] - 显式密钥(优先于环境变量) * @param {string} [options.apiKeyEnv] - 密钥环境变量名 * @param {string} [options.kind] - auto / report / ecg / imaging * @param {string} [options.mime] - 图片 MIME * @param {number} [options.width] - 像素宽 * @param {number} [options.height] - 像素高 * @param {string} [options.turnId] - 本轮标识(仅日志关联用) * @param {Object} [options.logger] - 日志实例 * @param {Function} [options.onError] - 识别过程异步失败回调 * @returns {{ push: Function, finish: Function, abort: Function }} */ export function createBatchOcrSession({ recognize = null, endpoint = '', apiKey = '', apiKeyEnv = '', kind = 'auto', mime = 'image/jpeg', width = 0, height = 0, turnId = '', logger: customLogger = null, onError = null } = {}) { const _logger = customLogger || logger; const key = apiKey || (apiKeyEnv ? process.env[apiKeyEnv] : '') || ''; const runRecognize = recognize || (endpoint ? makeHttpRecognizer(endpoint, key) : 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} 可投递文本 */ async finish() { if (aborted) return ''; finished = true; const image = Buffer.concat(chunks); chunks.length = 0; if (image.length === 0) return ''; if (!runRecognize) throw new Error('ocr provider not configured'); const startedAt = Date.now(); try { const text = (await runRecognize(image, { mime, kind, width, height }) || '').trim(); _logger.info('ocr finished', { turnId, bytes: image.length, elapsedMs: Date.now() - startedAt, chars: text.length }); return text; } catch (err) { _logger.warn('ocr session failed', { turnId, bytes: image.length, 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] - 供应商名(qwen 走视觉模型,其余走批式) * @param {Function} [options.recognize] - 自定义识别函数(优先于 provider/endpoint) * @param {string} [options.endpoint] - 识别服务地址(http/https) * @param {string} [options.baseUrl] - OpenAI 兼容 baseUrl(自动补 /chat/completions) * @param {string} [options.model] - 模型名(默认 qwen-vl-ocr) * @param {string} [options.profile] - full(转写 + 解读)/ transcribe(只转写,默认) * @param {string} [options.kind] - auto / report / ecg / imaging * @param {string} [options.mime] - 图片 MIME * @param {number} [options.width] - 像素宽 * @param {number} [options.height] - 像素高 * @param {string} [options.turnId] - 本轮标识(仅日志关联用) * @param {string} [options.apiKeyEnv] - 密钥环境变量名 * @param {Object} [options.parameters] - 供应商附加参数 * @param {Object} [options.logger] - 日志实例 * @param {Function} [options.fetchImpl] - 测试注入的 fetch * @returns {{ push: Function, finish: Function, abort: Function }} */ export function createOcrSession(options = {}) { const { provider = '', recognize = null, endpoint = '', baseUrl = '', logger: customLogger = null } = options; // 显式自定义识别函数优先:单测 / 本地 PaddleOCR 等自建服务 if (recognize) { return createBatchOcrSession({ ...options, recognize, logger: customLogger }); } // 缺省走通义千问:显式 provider=qwen,或未指定供应商且没有独立 HTTP endpoint const isQwen = provider === 'qwen' || (provider === '' && endpoint === ''); if (isQwen) { return createQwenVisionSession({ ...options, logger: customLogger }); } return createBatchOcrSession({ ...options, logger: customLogger }); } export { createQwenVisionSession }; export default createOcrSession;