mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
196 lines
9.9 KiB
Markdown
196 lines
9.9 KiB
Markdown
# 扩展 API 参考(v1)
|
||
|
||
## manifest.json
|
||
|
||
| 字段 | 必填 | 说明 |
|
||
| --- | --- | --- |
|
||
| `manifestVersion` | 是 | 固定 `1` |
|
||
| `id` | 是 | 反向域名,小写;禁止 `com.tracememo.` / `tracememo.` / `tm.` 前缀 |
|
||
| `name` | 是 | 展示名,≤ 64 字 |
|
||
| `version` | 是 | `x.y.z`(可带 prerelease) |
|
||
| `apiVersion` | 是 | 必须为 `1`(向后兼容所有 v1 扩展) |
|
||
| `minAppVersion` | 否 | 低于该版本拒绝安装 |
|
||
| `description` / `author` / `homepage` | 否 | 展示用;homepage 仅接受 http(s) |
|
||
| `permissions` | 是 | 见权限表;未知权限直接判 manifest 非法 |
|
||
| `entrypoints.page` | 二选一 | 页面 HTML 相对路径 |
|
||
| `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 保留名;
|
||
安装时还会用 realpath 复核入口仍在扩展目录内。
|
||
|
||
## 权限
|
||
|
||
| 权限 | 风险 | 能力 |
|
||
| --- | --- | --- |
|
||
| `messages.subscribe` | 高 | 接收 `message.received` |
|
||
| `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`(扩展私有数据目录,媒体与渲染产物落盘于此)
|
||
- `permissions: string[]`(当前已授予)
|
||
|
||
### tm.events
|
||
|
||
- `on('message.received', handler): () => void`
|
||
- `off('message.received', handler): void`
|
||
|
||
事件字段见 `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?,
|
||
cursor?,
|
||
order?, // 'asc' | 'desc';省略时保持 v1 默认最新一页语义并支持向更早历史续页
|
||
types? // 如 ['text', 'image', 'voice']
|
||
})
|
||
// → { messages: [{ messageId, messageRef, conversationId, senderId?, senderName?, isSelf, type, text?, timestamp }], nextCursor?, hasMore }
|
||
```
|
||
|
||
- 时间参数是 **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? })
|
||
await tm.wechat.send({ type: 'video', to, path: '/abs/path/clip.mp4', dedupeKey? })
|
||
await tm.wechat.send({ type: 'voice', to, text: '你好呀', voiceId?, dedupeKey? })
|
||
// → { actionId, status: 'sent' }
|
||
```
|
||
|
||
按顺序检查:权限 → 参数 → manifest 会话范围 → 频率 → 本机发送能力 → Pro 授权校验 → Action Gateway。任何一步不通过都抛带错误码的异常。
|
||
|
||
### tm.ai.chat / tm.storage / tm.secrets / tm.automation / tm.capabilities / tm.log
|
||
|
||
- `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 字)。
|
||
|
||
## 错误码
|
||
|
||
| 码 | 含义 |
|
||
| --- | --- |
|
||
| `EXTENSION_NOT_FOUND` | 扩展不存在 / 已卸载 |
|
||
| `EXTENSION_DISABLED` | 扩展未启用 |
|
||
| `EXTENSION_INCOMPATIBLE` | apiVersion 或 minAppVersion 不满足 |
|
||
| `EXTENSION_INVALID_MANIFEST` | manifest 不合法 |
|
||
| `EXTENSION_INVALID_PATH` | 入口或输出路径越界 |
|
||
| `EXTENSION_HOST_UNAVAILABLE` | 后台进程未运行 |
|
||
| `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` | 存储或产出文件体积超限 |
|