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
+111
-107
@@ -8,7 +8,7 @@
|
||||
| `id` | 是 | 反向域名,小写;禁止 `com.tracememo.` / `tracememo.` / `tm.` 前缀 |
|
||||
| `name` | 是 | 展示名,≤ 64 字 |
|
||||
| `version` | 是 | `x.y.z`(可带 prerelease) |
|
||||
| `apiVersion` | 是 | 必须为 `1` |
|
||||
| `apiVersion` | 是 | 必须为 `1`(向后兼容所有 v1 扩展) |
|
||||
| `minAppVersion` | 否 | 低于该版本拒绝安装 |
|
||||
| `description` / `author` / `homepage` | 否 | 展示用;homepage 仅接受 http(s) |
|
||||
| `permissions` | 是 | 见权限表;未知权限直接判 manifest 非法 |
|
||||
@@ -16,6 +16,7 @@
|
||||
| `entrypoints.background` | 二选一 | 后台入口相对路径 |
|
||||
| `subscriptions` | 条件 | 申请 `messages.subscribe` 时必填 |
|
||||
| `contributions.pages` | 否 | `[{ id, title, entry? }]`,`id` 形如 `settings` |
|
||||
| `contributions.navigation` | 否 | `{ label, icon?, pageId?, order? }`,供用户「固定到一级菜单」;`icon` 仅支持内置安全图标集 `spark / bot / digest / bell / media / puzzle / compass / message` |
|
||||
| `contributions.automationActions` | 否 | `[{ id, title, description? }]`,有此项必须提供 background |
|
||||
|
||||
路径规则:相对路径、不允许 `..`、反斜杠、盘符、控制字符、Windows 保留名;
|
||||
@@ -26,24 +27,31 @@
|
||||
| 权限 | 风险 | 能力 |
|
||||
| --- | --- | --- |
|
||||
| `messages.subscribe` | 高 | 接收 `message.received` |
|
||||
| `messages.query` | 高 | `tm.messages.query` |
|
||||
| `messages.query` | 高 | `tm.messages.query`(支持游标分页与类型过滤) |
|
||||
| `media.read` | 高 | `tm.media.readImage` / `readVoice` / `readVideo`(按需解密并写入扩展私有缓存目录) |
|
||||
| `media.ocr` | 中 | `tm.media.ocrImage`(纯本地系统 OCR,优先复用既有索引缓存) |
|
||||
| `media.transcribe` | 中 | `tm.media.transcribeVoice`(复用 `VoiceTranscriptionGateway` 与转写缓存) |
|
||||
| `contacts.list` | 中 | 配置页读取群列表(`contacts.listGroups`),不含聊天内容 |
|
||||
| `wechat.send.text` | 高 | `tm.wechat.send`(文本)与自动化 `reply` |
|
||||
| `wechat.send.image` | 高 | `tm.wechat.send({ type: 'image' })` |
|
||||
| `wechat.send.video` | 高 | `tm.wechat.send({ type: 'video' })`,原生 Host 走 `/SendMsg` video |
|
||||
| `wechat.send.voice` | 高 | `tm.wechat.send({ type: 'voice' })`,支持传入 `text` 用宿主 TTS 音色合成发送或传入音频 `path` |
|
||||
| `ai.chat` | 中 | `tm.ai.chat` |
|
||||
| `ai.vision` | 中 | `tm.media.analyzeImage` / `tm.ai.analyzeImage`(复用已配置的视觉 AI Provider) |
|
||||
| `render.html` | 低 | `tm.render.htmlToImage`(离线隔离渲染静态 HTML/CSS 为 PNG 长图) |
|
||||
| `notifications.create` | 中 | `tm.notifications.create`(写入扩展通知中心与未读角标) |
|
||||
| `storage` | 低 | `tm.storage.*` 与页面存储 |
|
||||
| `automation.action` | 中 | `tm.automation.registerAction` |
|
||||
| `secrets` | 中 | `tm.secrets.*`(经系统安全存储加密保存私有凭据) |
|
||||
|
||||
安装时只自动授予**低风险**权限;其余在「启用」确认后授予。停用后回落到低风险集合。
|
||||
安装时只自动授予**低风险**权限;其余在「启用」确认后授予。停用后回落到低风险集合。所有权限均由主进程独立校验,不依赖 SDK 侧检查。
|
||||
|
||||
## 后台 API
|
||||
|
||||
### tm.extension
|
||||
|
||||
- `id: string`
|
||||
- `dataDir: string`(扩展私有数据目录,媒体文件写这里才能通过发送校验)
|
||||
- `dataDir: string`(扩展私有数据目录,媒体与渲染产物落盘于此)
|
||||
- `permissions: string[]`(当前已授予)
|
||||
|
||||
### tm.events
|
||||
@@ -51,109 +59,114 @@
|
||||
- `on('message.received', handler): () => void`
|
||||
- `off('message.received', handler): void`
|
||||
|
||||
事件字段见 `TraceMemoMessageEvent`:`eventId / accountId / conversationId / messageId /
|
||||
senderId? / senderName? / isGroup / isSelf / type / text? / mentionsMe / timestamp(ms)`。
|
||||
事件字段见 `TraceMemoMessageEvent`:`eventId / messageRef / accountId / conversationId / messageId / senderId? / senderName? / isGroup / isSelf / type / text? / mentionsMe / timestamp(ms)`。
|
||||
其中 `messageRef` 与 `tm.messages.query` 返回的 `messageRef` 格式完全统一,包含账号、会话与消息身份隔离校验。
|
||||
|
||||
### tm.messages.query
|
||||
|
||||
```ts
|
||||
await tm.messages.query({ conversationId, startTime?, endTime?, limit? })
|
||||
// → { messages: [{ messageId, conversationId, senderId?, senderName?, isSelf, type, text?, timestamp }] }
|
||||
await tm.messages.query({
|
||||
conversationId,
|
||||
startTime?,
|
||||
endTime?,
|
||||
limit?,
|
||||
cursor?,
|
||||
order?, // 'asc' | 'desc';省略时保持 v1 默认最新一页语义并支持向更早历史续页
|
||||
types? // 如 ['text', 'image', 'voice']
|
||||
})
|
||||
// → { messages: [{ messageId, messageRef, conversationId, senderId?, senderName?, isSelf, type, text?, timestamp }], nextCursor?, hasMore }
|
||||
```
|
||||
|
||||
- 时间参数是 **epoch 毫秒**;默认最近 24 小时。
|
||||
- 单次窗口最多 7 天,`limit` 上限 200。
|
||||
- 时间参数是 **epoch 毫秒**;默认最近 24 小时,单次最大 35 天,`limit` 上限 500。
|
||||
- 游标以 `(timestamp, messageId)` 为确定性边界,同时间戳多条消息分页不遗漏、不重复。
|
||||
- 数据库未连接时抛 `DATABASE_NOT_READY`。
|
||||
|
||||
### tm.media(扩展媒体 Gateway)
|
||||
|
||||
```ts
|
||||
// 按需读取媒体副本(写入扩展私有目录,不暴露原始微信数据库路径与密钥)
|
||||
await tm.media.readImage({ messageRef, quality?: 'original' | 'thumbnail' })
|
||||
await tm.media.readVoice({ messageRef })
|
||||
await tm.media.readVideo({ messageRef })
|
||||
|
||||
// 本地 OCR / 语音转写 / AI 图片理解(自动复用主程序缓存与并发限制)
|
||||
await tm.media.ocrImage({ messageRef, language?, force? })
|
||||
await tm.media.transcribeVoice({ messageRef, force?, waitMs? })
|
||||
await tm.media.analyzeImage({ messageRef, prompt?, force? })
|
||||
|
||||
// 批量按需处理(单次最多 20 项,并发上限 4,单项失败隔离返回)
|
||||
await tm.media.batch({
|
||||
concurrency: 2,
|
||||
items: [
|
||||
{ op: 'ocrImage', messageRef: imgRef },
|
||||
{ op: 'transcribeVoice', messageRef: voiceRef }
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
- 跨账号 `messageRef` 触发 `ACCOUNT_MISMATCH`;消息类型不匹配触发 `MEDIA_UNSUPPORTED`;缺失文件触发 `MEDIA_NOT_FOUND`;并发满载触发 `CONCURRENCY_LIMITED`。
|
||||
- `tm.ai.analyzeImage` 是 `tm.media.analyzeImage` 的等价别名,均要求 `ai.vision` 权限。
|
||||
|
||||
### tm.render.htmlToImage
|
||||
|
||||
```ts
|
||||
await tm.render.htmlToImage({
|
||||
html: '<main><h1>晨报</h1></main>',
|
||||
css: 'main { padding: 20px; }',
|
||||
fileName: 'morning-2026-12-01.png', // 可选,仅接受安全相对路径
|
||||
width: 460,
|
||||
maxHeight: 16000,
|
||||
backgroundColor: '#ffffff',
|
||||
timeoutMs: 20000
|
||||
})
|
||||
// → { path, previewUrl, width, height, pixelWidth, pixelHeight, deviceScaleFactor, bytes, mimeType: 'image/png', sha256 }
|
||||
```
|
||||
|
||||
- 与官方群聊日报共用同一 `HtmlRenderService` 离线渲染引擎。
|
||||
- 仅接受静态 HTML/CSS:自动拦截 `<script>`、`<iframe>`、内联事件、`javascript:`、`srcset`/`xlink:href` 与外链资源,独立窗口 + 临时 Session + 严格 CSP,输出至 `<dataDir>/rendered/`;生成路径可直接交给 `tm.wechat.send({ type: 'image', to, path })`,`previewUrl`(`tm-extension://<extensionId>/_data/rendered/...`)可直接用于扩展沙箱页面 `<img>` 预览。
|
||||
|
||||
### tm.notifications
|
||||
|
||||
```ts
|
||||
await tm.notifications.create({
|
||||
title: '今日晨报已生成',
|
||||
body: '覆盖 3 个群聊,共提炼 5 条重点',
|
||||
level: 'success', // 'info' | 'success' | 'warning' | 'error'
|
||||
dedupeKey: 'morning-2026-12-01',
|
||||
pageId: 'digest'
|
||||
})
|
||||
// → { id, deduped, createdAt }
|
||||
|
||||
const ownItems = await tm.notifications.list()
|
||||
const { changed } = await tm.notifications.markRead({ id: ownItems[0]?.id })
|
||||
```
|
||||
|
||||
- 由宿主持久化管理;同扩展相同 `dedupeKey` 仅更新内容不重复累加未读数,并受滑动窗口防刷屏限频保护(超限抛 `CONCURRENCY_LIMITED`)。
|
||||
- `list` 与 `markRead` 强绑定调用方 `extensionId`,只能读写本扩展自己的通知。
|
||||
- 已固定到一级菜单的扩展在固定入口显示独立未读角标;未固定扩展聚合到「扩展」导航的未读角标,与红色 `!` 发送能力异常提示严格区分。
|
||||
- 停用扩展拒绝写入新通知(`EXTENSION_DISABLED`);卸载扩展自动清理其全部通知状态。
|
||||
|
||||
### tm.wechat.send
|
||||
|
||||
```ts
|
||||
// 文本
|
||||
await tm.wechat.send({ to, text, at?, dedupeKey? })
|
||||
|
||||
// 图片
|
||||
await tm.wechat.send({ type: 'image', to, path: '/abs/path/cover.png', dedupeKey? })
|
||||
|
||||
// 视频(原生 Host 以 {toWxid, type:'video', msg:<路径>} 提交)
|
||||
await tm.wechat.send({ type: 'video', to, path: '/abs/path/clip.mp4', dedupeKey? })
|
||||
|
||||
// 语音(传 text 由宿主用已配置的 TTS 音色合成发送,或传 path 发送音频文件)
|
||||
await tm.wechat.send({ type: 'voice', to, text: '你好呀', voiceId?, dedupeKey? })
|
||||
// → { actionId, status: 'sent' }
|
||||
```
|
||||
|
||||
按顺序检查:权限 → 参数(文本 ≤ 2000 字)→ manifest 会话范围 → 频率
|
||||
(同扩展 2 秒一条、每分钟 10 条)→ 本机发送能力 → 授权校验 → Gateway。
|
||||
任何一步不通过都抛带错误码的异常,不会「静默丢弃」。
|
||||
按顺序检查:权限 → 参数 → manifest 会话范围 → 频率 → 本机发送能力 → Pro 授权校验 → Action Gateway。任何一步不通过都抛带错误码的异常。
|
||||
|
||||
媒体路径额外约束:
|
||||
### tm.ai.chat / tm.storage / tm.secrets / tm.automation / tm.capabilities / tm.log
|
||||
|
||||
- 必须是**绝对路径**的普通文件,realpath 后仍位于**扩展数据目录**或**系统临时目录**;
|
||||
- 大小 1B–100MB;
|
||||
- 图片扩展名:jpg/jpeg/png/gif/webp/bmp;视频扩展名:mp4/mov/m4v;
|
||||
- 视频走 `type: 'video'`,不复用图片通道;发送失败不会降级成图片。
|
||||
|
||||
### tm.ai.chat
|
||||
|
||||
```ts
|
||||
await tm.ai.chat([{ role: 'user', content: '...' }])
|
||||
// → { text }
|
||||
```
|
||||
|
||||
使用 TraceMemo 已配置的 AI Provider;扩展拿不到 API Key。
|
||||
|
||||
### tm.storage
|
||||
|
||||
```ts
|
||||
await tm.storage.set('config', { ... })
|
||||
await tm.storage.get('config')
|
||||
await tm.storage.remove('config')
|
||||
await tm.storage.list()
|
||||
```
|
||||
|
||||
键:`[A-Za-z0-9._-]{1,64}`;单值 ≤ 256KB,总量 ≤ 1MB;只接受 JSON 值。
|
||||
|
||||
### tm.automation.registerAction
|
||||
|
||||
```ts
|
||||
tm.automation.registerAction({ id, title, description?, inputSchema?, handler })
|
||||
```
|
||||
|
||||
`id`:`[a-z0-9][a-z0-9._-]{1,63}`。`handler` 返回:
|
||||
|
||||
```ts
|
||||
{ success: boolean, output?: object, reply?: { type: 'text', text, at? }, error?: string }
|
||||
```
|
||||
|
||||
`inputSchema` 只支持 `object` + `string|number|boolean` 属性的浅校验;必填项缺失会直接拒绝。
|
||||
|
||||
执行超时默认 15 秒(宿主可配,最大 60 秒),超时返回 `ACTION_TIMEOUT`。
|
||||
|
||||
### tm.capabilities.get
|
||||
|
||||
返回只读快照:`enabled / permissions / platform / databaseReady / send{...} / ai{...}`。
|
||||
|
||||
### tm.log
|
||||
|
||||
`info / warn / error`,写入扩展日志(详情页可见,最多保留 200 条,单行 ≤ 500 字)。
|
||||
|
||||
## 页面桥
|
||||
|
||||
页面 → 宿主:
|
||||
|
||||
```ts
|
||||
{ source: 'tracememo-extension-page', extensionId, requestId, method, params }
|
||||
```
|
||||
|
||||
宿主 → 页面:
|
||||
|
||||
```ts
|
||||
{ source: 'tracememo-extension-host', requestId, ok: true, result }
|
||||
{ source: 'tracememo-extension-host', requestId, ok: false, error: { code, message } }
|
||||
```
|
||||
|
||||
页面方法白名单:`storage.get|set|remove|list`、`capabilities.get`、`contacts.listGroups`、`extension.info`。
|
||||
页面不能发送微信消息、不能查询消息、不能调用 AI。
|
||||
- `tm.ai.chat(messages)`:调用 TraceMemo 已配置的 AI Provider,不暴露 API Key。
|
||||
- `tm.storage.{get,set,remove,list}`:键 `[A-Za-z0-9._-]{1,64}`,单值 ≤ 256KB,总量 ≤ 1MB。
|
||||
- `tm.secrets.{get,set,delete,has}`:系统安全存储加密凭据。
|
||||
- `tm.automation.registerAction({ id, title, description?, inputSchema?, handler })`:注册自动化动作。
|
||||
- `tm.page.onRequest(method, handler)`:注册页面到同扩展后台的受限请求处理器(响应 ≤ 64KB 纯 JSON)。
|
||||
- `tm.capabilities.get()`:返回只读环境能力快照。
|
||||
- `tm.log.{info,warn,error}`:写入扩展日志(最多保留 200 条,单行 ≤ 500 字)。
|
||||
|
||||
## 错误码
|
||||
|
||||
@@ -163,29 +176,20 @@ tm.automation.registerAction({ id, title, description?, inputSchema?, handler })
|
||||
| `EXTENSION_DISABLED` | 扩展未启用 |
|
||||
| `EXTENSION_INCOMPATIBLE` | apiVersion 或 minAppVersion 不满足 |
|
||||
| `EXTENSION_INVALID_MANIFEST` | manifest 不合法 |
|
||||
| `EXTENSION_INVALID_PATH` | 入口 / 资源路径越界 |
|
||||
| `EXTENSION_INVALID_PATH` | 入口或输出路径越界 |
|
||||
| `EXTENSION_HOST_UNAVAILABLE` | 后台进程未运行 |
|
||||
| `PERMISSION_DENIED` | 未授予权限,或超出声明范围 |
|
||||
| `CAPABILITY_UNAVAILABLE` | 本机能力不具备(未绑定 / 未就绪) |
|
||||
| `LICENSE_REQUIRED` | 无有效授权 |
|
||||
| `PERMISSION_DENIED` | 未授予权限或超出声明范围 |
|
||||
| `CAPABILITY_UNAVAILABLE` | 本机能力不具备或未配置 |
|
||||
| `LICENSE_REQUIRED` | 无有效 Pro 授权 |
|
||||
| `DATABASE_NOT_READY` | 微信数据库未连接 |
|
||||
| `SEND_NOT_SUPPORTED` | 当前平台或内容类型不支持 |
|
||||
| `MEDIA_NOT_FOUND` | 找不到消息对应的本地媒体文件 |
|
||||
| `MEDIA_UNSUPPORTED` | 该消息类型不支持所请求的媒体操作 |
|
||||
| `ACCOUNT_MISMATCH` | `messageRef` 不属于当前已连接账号 |
|
||||
| `CONCURRENCY_LIMITED` | 媒体解析或渲染并发达到上限 |
|
||||
| `RENDER_FAILED` | 离线 HTML 渲染失败 |
|
||||
| `ACTION_TIMEOUT` | 处理超时 |
|
||||
| `ACTION_FAILED` | 执行失败 |
|
||||
| `RATE_LIMITED` | 触发频率限制 |
|
||||
| `INVALID_REQUEST` | 参数非法 |
|
||||
| `STORAGE_LIMIT_EXCEEDED` | 存储超限 |
|
||||
|
||||
## 自动化动作输入
|
||||
|
||||
规则里保存的静态 `input` 会与宿主注入字段合并,同名键以宿主为准:
|
||||
|
||||
```json
|
||||
{
|
||||
"url": "https://example.com/video",
|
||||
"conversationId": "xxx@chatroom",
|
||||
"isGroup": true,
|
||||
"accountId": "wxid_self",
|
||||
"message": { "eventId": "message:...", "text": "...", "mentionsMe": false }
|
||||
}
|
||||
```
|
||||
| `INVALID_REQUEST` | 参数非法或恶意输入被拒绝 |
|
||||
| `STORAGE_LIMIT_EXCEEDED` | 存储或产出文件体积超限 |
|
||||
Reference in new issue
Block a user