'use strict' /** 群聊机器人后台入口壳:核心逻辑由 lib/bot-engine 承载,并内置按需图片/语音解析适配。 */ let engine = null try { engine = require('../lib/bot-engine') } catch { engine = null } function hasGrantedPermission(tm, permission) { const perms = tm && tm.extension && Array.isArray(tm.extension.permissions) ? tm.extension.permissions : [] return perms.includes(permission) } /** 按需将图片或语音消息解析为可供机器人理解的纯文本摘要,缺权限或失败时安全降级为 null。 */ async function resolveIncomingMediaText(tm, event, options = {}) { if (!event || typeof event !== 'object') return null if (event.type === 'text') { const text = String(event.text || '').trim() return text ? { text, source: 'text' } : null } const messageRef = String(event.messageRef || '').trim() if (!messageRef || !tm || !tm.media) return null if (event.type === 'image') { const imageMode = options.imageMode || 'ocr' if (imageMode === 'off') return null if (imageMode === 'vision' && hasGrantedPermission(tm, 'ai.vision')) { try { const analyzed = await tm.media.analyzeImage({ messageRef }) const parts = [analyzed && analyzed.description, analyzed && analyzed.ocrText] .map((item) => String(item || '').trim()) .filter(Boolean) if (parts.length > 0) { return { text: `[图片内容] ${parts.join(';')}`, source: 'image_vision' } } } catch { // 视觉理解失败时降级到本地 OCR。 } } if (hasGrantedPermission(tm, 'media.ocr')) { try { const ocr = await tm.media.ocrImage({ messageRef }) const ocrText = String((ocr && ocr.text) || '').trim() if (ocrText) { return { text: `[图片文字] ${ocrText}`, source: 'image_ocr' } } } catch { // 本地 OCR 不可用时继续尝试可选视觉回退。 } } if (options.visionFallback === true && hasGrantedPermission(tm, 'ai.vision')) { try { const analyzed = await tm.media.analyzeImage({ messageRef }) const desc = String((analyzed && analyzed.description) || '').trim() if (desc) return { text: `[图片内容] ${desc}`, source: 'image_vision' } } catch { return null } } return null } if (event.type === 'voice') { if (options.voiceTranscribeEnabled === false) return null if (!hasGrantedPermission(tm, 'media.transcribe')) return null try { const result = await tm.media.transcribeVoice({ messageRef, waitMs: Number(options.voiceWaitMs) > 0 ? Number(options.voiceWaitMs) : 12_000 }) if (result && result.state === 'transcribed' && String(result.transcript || '').trim()) { return { text: `[语音转文字] ${String(result.transcript).trim()}`, source: 'voice_transcript' } } } catch { return null } } return null } function registerFallbackHandlers(tm) { if (!tm || !tm.page || typeof tm.page.onRequest !== 'function') return const unavailable = async () => { throw new Error('当前功能暂不可用') } tm.page.onRequest('previewReply', unavailable) tm.page.onRequest('refreshGroupMemory', unavailable) tm.page.onRequest('testSend', unavailable) } function activate(api) { if (engine && typeof engine.activateBotRuntime === 'function') { engine.activateBotRuntime(api, { resolveIncomingMediaText }) } else { registerFallbackHandlers(api) } if (api && api.log && typeof api.log.info === 'function') { api.log.info('群聊机器人已启动') } } activate.resolveIncomingMediaText = resolveIncomingMediaText module.exports = activate