mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
feat: 扩展中心支持多模态消息、HTML 渲染、一级导航与应用内通知
This commit is contained in:
1 parent
b20e514c90
commit
4ea73c3414
63 files changed
+8360
-644
No files matched your search
@@ -1,37 +1,30 @@
|
||||
# 群聊机器人 (`com.example.group-chat-bot`)
|
||||
|
||||
> **这是模板壳,不是完整运行包。** 本目录的 `lib/bot-engine.js` 只含纯函数,不含
|
||||
> `activateBotRuntime` 运行时。直接把本目录打成 ZIP 安装,后台会走
|
||||
> `background/index.js` 的 fallback,预览与测试发送都会提示「当前功能暂不可用」。
|
||||
> 可安装的完整包必须包含带运行时的 `lib/bot-engine.js`;不要把本目录当成品分发。
|
||||
> **这是模板壳,不是完整运行包。** 本目录不含 `lib/bot-engine.js` 运行时核心。直接把本目录打成 ZIP 安装,后台会走 `background/index.js` 的静默壳逻辑,页面可预览配置但不会执行自动回复。
|
||||
|
||||
TraceMemo 官方扩展模板。结合 TraceMemo 已配置的 AI 模型与文字转语音(TTS)音色,在指定微信群聊中按角色设定自然接话,支持单群长期记忆与随机回复文字 / 随机发送语音。
|
||||
TraceMemo 官方扩展模板。结合 TraceMemo 已配置的 AI 模型、文字转语音(TTS)音色、本地系统 OCR、AI 视觉理解与语音转写能力,在指定微信群聊中按角色设定自然接话,支持单群长期记忆、按需解析图片与语音内容,以及随机回复文字 / 发送语音。
|
||||
|
||||
## 核心特性
|
||||
|
||||
1. **多套群聊人格(Persona)**:内置「日常群友」「技术搭子」「温柔捧场王」「幽默气氛组」四套预设人格,支持新建、修改或删除自定义人格(独立配置人设背景、说话风格、字数上限与追问约束),并可为不同微信群绑定不同人格。
|
||||
2. **短期上下文 + 单群长期记忆(Memory)**:
|
||||
- **短期记忆**:接话时自动读取本群最近 N 条文本消息作为多轮对话上下文。
|
||||
- **长期记忆**:按群独立维护「固定背景记忆(手动置顶的群规、群友称呼偏好)」与「AI 自动提炼的互动摘要(常聊话题、近期约定)」。可按对话轮数自动更新,也可在配置页随时手动触发 AI 提炼或修订。
|
||||
3. **自然群聊节奏控制**:支持「仅 @我」「命中唤醒词」「@我 或唤醒词」「自然活跃接话(@我必回 + 普通群消息按概率随机搭话)」四种触发模式,区分直接唤醒冷却与环境闲聊冷却,并支持重复表达抑制。
|
||||
4. **随机回复文字 / 随机发送语音 + TraceMemo 音色同步展示**:
|
||||
- 配置页直接展示 TraceMemo「设置 → 文字转语音」中当前选定的音色(名称、ID、标签、合成模型与样本试听)。
|
||||
- 支持「随机混合(按概率随机回复文字或发送语音)」「仅回复文字」「仅发送语音」三种模式,单群可独立覆盖;当语音未就绪或发送失败时支持自动降级为发送文字。
|
||||
1. **多套群聊人格(Persona)**:内置四套预设人格并支持自定义编辑,可按群绑定不同人格。
|
||||
2. **短期上下文 + 单群长期记忆(Memory)**:按群维护固定备注与 AI 提炼的互动摘要。
|
||||
3. **按需图片与语音理解(v1.1 新增)**:
|
||||
- 订阅范围已扩展至 `text` / `image` / `voice`,通过统一的 `messageRef` 按需调用 `tm.media.ocrImage`、`tm.media.analyzeImage` 与 `tm.media.transcribeVoice`。
|
||||
- 图片优先走本地系统 OCR(零联网消耗),按需降级/切换视觉模型;语音复用主程序转写队列与缓存。
|
||||
4. **随机回复文字 / 发送语音 + 动态一级导航**:声明 `contributions.navigation`,支持在扩展详情页一键「固定到一级菜单」。
|
||||
|
||||
## 申请权限
|
||||
|
||||
| 权限 | 用途 |
|
||||
| --- | --- |
|
||||
| `messages.subscribe` | 接收微信群聊新文本消息事件 |
|
||||
| `messages.query` | 读取触发群聊最近的上下文消息 |
|
||||
| `messages.subscribe` | 接收微信群聊文本、图片与语音新消息事件 |
|
||||
| `messages.query` | 分页读取触发群聊最近的上下文消息 |
|
||||
| `media.ocr` | 按需识别群聊图片中的文字(纯本地,不联网) |
|
||||
| `media.transcribe` | 按需将群聊语音转为文字(复用主程序转写缓存) |
|
||||
| `ai.vision` | 按需调用已配置的视觉模型理解图片内容 |
|
||||
| `contacts.list` | 在配置页读取群聊列表供勾选生效范围 |
|
||||
| `wechat.send.text` | 经 TraceMemo Gateway 发送群聊文字回复 |
|
||||
| `wechat.send.voice` | 经 TraceMemo 文字转语音服务与 Gateway 发送群聊语音气泡 |
|
||||
| `ai.chat` | 调用 TraceMemo 已配置的 AI Provider 进行思考回复与记忆提炼 |
|
||||
| `notifications.create` | 在扩展通知中心汇报关键状态 |
|
||||
| `storage` | 保存机器人配置、单群长期记忆与今日统计 |
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
node --test examples/extensions/group-chat-bot/tests/bot-engine.test.mjs
|
||||
```
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
/** 群聊机器人后台入口壳:核心逻辑由 lib/bot-engine 承载,缺失时保持静默空操作且不报错。 */
|
||||
/** 群聊机器人后台入口壳:核心逻辑由 lib/bot-engine 承载,并内置按需图片/语音解析适配。 */
|
||||
|
||||
let engine = null
|
||||
try {
|
||||
@@ -9,6 +9,86 @@ try {
|
||||
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 () => {
|
||||
@@ -19,9 +99,9 @@ function registerFallbackHandlers(tm) {
|
||||
tm.page.onRequest('testSend', unavailable)
|
||||
}
|
||||
|
||||
module.exports = function activate(api) {
|
||||
function activate(api) {
|
||||
if (engine && typeof engine.activateBotRuntime === 'function') {
|
||||
engine.activateBotRuntime(api)
|
||||
engine.activateBotRuntime(api, { resolveIncomingMediaText })
|
||||
} else {
|
||||
registerFallbackHandlers(api)
|
||||
}
|
||||
@@ -29,3 +109,6 @@ module.exports = function activate(api) {
|
||||
api.log.info('群聊机器人已启动')
|
||||
}
|
||||
}
|
||||
|
||||
activate.resolveIncomingMediaText = resolveIncomingMediaText
|
||||
module.exports = activate
|
||||
@@ -2,18 +2,22 @@
|
||||
"manifestVersion": 1,
|
||||
"id": "com.example.group-chat-bot",
|
||||
"name": "群聊机器人 - 模板(如需使用请找授权你的人",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"apiVersion": "1",
|
||||
"minAppVersion": "2.6.0",
|
||||
"description": "支持多套群聊人格设定、群聊长期与短期记忆、自然接话节奏,并可结合 TraceMemo 音色随机回复文字或发送语音。",
|
||||
"minAppVersion": "2.7.0",
|
||||
"description": "支持多套群聊人格设定、群聊长期与短期记忆、按需解析图片与语音内容,并可结合 TraceMemo 音色随机回复文字或发送语音。",
|
||||
"author": "TraceMemo Examples",
|
||||
"permissions": [
|
||||
"messages.subscribe",
|
||||
"messages.query",
|
||||
"media.ocr",
|
||||
"media.transcribe",
|
||||
"contacts.list",
|
||||
"wechat.send.text",
|
||||
"wechat.send.voice",
|
||||
"ai.chat",
|
||||
"ai.vision",
|
||||
"notifications.create",
|
||||
"storage"
|
||||
],
|
||||
"entrypoints": {
|
||||
@@ -22,7 +26,9 @@
|
||||
},
|
||||
"subscriptions": {
|
||||
"messageTypes": [
|
||||
"text"
|
||||
"text",
|
||||
"image",
|
||||
"voice"
|
||||
],
|
||||
"conversationIds": [],
|
||||
"requireMentionMe": false,
|
||||
@@ -34,6 +40,11 @@
|
||||
"id": "settings",
|
||||
"title": "群聊机器人设置"
|
||||
}
|
||||
]
|
||||
],
|
||||
"navigation": {
|
||||
"label": "群聊机器人",
|
||||
"icon": "bot",
|
||||
"pageId": "settings"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user