cortico-world-wechat-claw 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +10 -2
- package/describe.json +13 -3
- package/package.json +1 -1
- package/src/ENV_PROMPT.md +1 -0
- package/src/config.ts +2 -2
- package/src/protocol/inbound.ts +8 -7
- package/src/protocol/mime.ts +66 -1
- package/src/world.ts +99 -34
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.9
|
|
4
|
+
|
|
5
|
+
- 入站文件先按可确认的文件头识别 PDF、音频、视频和图片容器,再用实际 MIME 查询当前 Provider;补齐 AIFF、音频 WebM、MPEG、3GP、HEIF 等扩展名,修复改名或无扩展名的媒体被误判为普通二进制而未附的问题。
|
|
6
|
+
- 微信视频不再固定标为 MP4,语音按文件头与实际编码标记;支持的附件原样交给 Core,不支持的视频保留抽帧回退。
|
|
7
|
+
- `wechat_status` 新增可选 `mimes` 查询,返回实时模型能力、接收开关及最近附件投递结果;事件 `meta.media` 记录原生附件、抽帧或省略及原因。能力查询错误与确认不支持分开报告。
|
|
8
|
+
- OpenCode 原生 Gemini 还需配合 Provider 的工具回执附件修复;旧转换只保留 Core 外部事件的回执文字,会在生成请求时丢失附件。
|
|
9
|
+
|
|
3
10
|
## 0.1.8
|
|
4
11
|
|
|
5
12
|
- 连续语气标点、爱心与 emoji 作为完整符号串处理,修复 `~♡`、`……`、`?!`、`!!!` 在中途被切开并单独发送的问题;符号间仅有空白时也不会拆成碎片。
|
package/README.md
CHANGED
|
@@ -77,8 +77,8 @@ corepack pnpm add "<本地包目录,绝对路径,例如 E:\WorkShop\...\cort
|
|
|
77
77
|
| `proxyUrl` | 空 | `proxy` 模式的 HTTP/HTTPS 代理,例如 `http://127.0.0.1:7890` |
|
|
78
78
|
| `forceIpv4` | `false` | `direct`/`proxy` 下强制 IPv4 |
|
|
79
79
|
| `receiveImages` | `true` | 收图时下载并交给模型 |
|
|
80
|
-
| `receiveFiles` | `true` |
|
|
81
|
-
| `receiveVideos` | `false` |
|
|
80
|
+
| `receiveFiles` | `true` | 收文件时下载,Provider 接受实际 MIME 时附在消息中 |
|
|
81
|
+
| `receiveVideos` | `false` | 收微信视频消息时下载(体积大,默认关);原生理解也需开启 |
|
|
82
82
|
| `receiveVoices` | `true` | 收语音时下载 |
|
|
83
83
|
| `maxInboundMediaMb` | `20` | 单条入站媒体上限,超过只留占位;也是出站媒体的上限 |
|
|
84
84
|
| `inboxDir` | 空 | 收件目录。留空=桌宠工作区下的 `wechat-inbox`;相对路径按工作区解析。改完要重启 |
|
|
@@ -173,6 +173,14 @@ corepack pnpm add "<本地包目录,绝对路径,例如 E:\WorkShop\...\cort
|
|
|
173
173
|
|
|
174
174
|
## 视频抽帧
|
|
175
175
|
|
|
176
|
+
入站文件按可确认的文件头识别 PDF、图片、音频和视频容器;无法确认时使用文件名或微信语音编码提供的类型。每次收件都调用宿主的 `modelFacts.accepts(实际 MIME)`,随当前模型、Provider 协议和输入开关变化。支持时通过事件 `blobs` 提交实际字节、MIME 与文件名;未知类型不会冒充支持的格式。
|
|
177
|
+
|
|
178
|
+
可调用 `wechat_status`,传入 `{"mimes":["video/mp4","audio/wav","application/pdf"]}` 查看 `inputSupport`。`accepted` 是当前 Provider 的类型判断,`error` 表示查询失败;`receive` 是本扩展的下载开关。`lastInboundAttachments` 与事件的 `meta.media` 记录每份文件交给 Core 时的结果:`native` 为原样附件,`frames` 为视频拼图,`omitted` 为未附,原因在 `notes`。这不代表上游已经成功理解文件;字节、总请求大小等仍由 Provider 和上游校验。
|
|
179
|
+
|
|
180
|
+
OpenCode 原生 Gemini 会将支持的音频、视频、PDF 等附件编码到请求的 `contents[].parts[].inlineData`。请先在 OpenCode 获取模型列表以更新格式声明,并确认所需输入类型未关闭。微信视频消息还需在本扩展打开「收视频」;文件通道发送的视频由「收文件」控制。微信 SILK 语音只有 Provider 接受 `audio/silk` 才会附上,普通音频支持不保证支持 SILK;服务端转写仍保留。
|
|
181
|
+
|
|
182
|
+
OpenCode Provider 必须包含 Gemini 工具回执附件保留修复:Core 将外部事件投递为工具回执,旧版转换只发送回执文字,即使 World 已提供 `blobs`,生成请求时仍会丢失媒体。仅更新本扩展不能修复 Provider 内部的这一步。
|
|
183
|
+
|
|
176
184
|
当前 Provider 接受视频的实际 MIME 时,直接将视频作为事件附件交给 Core,不抽帧,也不需要 ffmpeg。只有 Provider 不接受视频时才走下面的抽帧流程。微信文件通道收到的 `.mp4`、`.mov` 等视频同样处理。
|
|
177
185
|
|
|
178
186
|
视频抽帧要 ffmpeg,但扩展不把它打进包里,**也绝不悄悄下载**。
|
package/describe.json
CHANGED
|
@@ -83,11 +83,21 @@
|
|
|
83
83
|
},
|
|
84
84
|
{
|
|
85
85
|
"name": "wechat_status",
|
|
86
|
-
"description": "Report the WeChat ClawBot connection: bound account, whether the long poll is connected, the last inbound message time, the last error and how many context tokens are held.",
|
|
86
|
+
"description": "Report the WeChat ClawBot connection: bound account, whether the long poll is connected, the last inbound message time, the last error and how many context tokens are held. Also reports live model input support, receive switches and the last attachment delivery results. Pass mimes to check specific file types against the active Provider.",
|
|
87
87
|
"parameters": {
|
|
88
88
|
"type": "object",
|
|
89
89
|
"additionalProperties": false,
|
|
90
|
-
"properties": {
|
|
90
|
+
"properties": {
|
|
91
|
+
"mimes": {
|
|
92
|
+
"type": "array",
|
|
93
|
+
"maxItems": 128,
|
|
94
|
+
"items": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"minLength": 1
|
|
97
|
+
},
|
|
98
|
+
"description": "Optional exact MIME types, such as audio/wav, video/mp4 or application/pdf."
|
|
99
|
+
}
|
|
100
|
+
}
|
|
91
101
|
},
|
|
92
102
|
"tags": [
|
|
93
103
|
"read",
|
|
@@ -154,7 +164,7 @@
|
|
|
154
164
|
"key": "worlds.wechat-claw.envPrompt",
|
|
155
165
|
"title": "微信 ClawBot · 环境提示词",
|
|
156
166
|
"role": "envPrompt",
|
|
157
|
-
"content": "微信 ClawBot 是一条一对一私聊通道,接在微信里,只能回复刚发消息的那个人。\n\n## 手边的状态\n\n{{wechat.state | (微信还没接上。)}}\n{{wechat.account | (还没有绑定的微信号。)}}\n{{wechat.lastMessageAt | (还没有收到过微信消息。)}}\n{{wechat.lastError | (最近没有连接错误。)}}\n\n## 收到的东西在哪\n\n对方发来的图片、文件、视频、语音都落在你的工作区里:\n\n```\n{{wechat.inbox | (收件目录还没解析出来。)}}\n```\n\n它下面按类型分成 `images/`、`files/`、`videos/`、`voices/`,文件名保留对方给的原文名,后面接一个本地时间戳。\n这些文件就是普通文件——**读得到、改得了、也发得回去**。想让对方看到改过之后的版本,改完直接\n用 `wechat_send_file` 发那个路径就行。\n\n## 什么时候用\n\n微信里来人说话时,会收到一条 `wechat-claw.message`,正文以 `[微信 时间 发送者]` 开头,媒体路径就写在里面。要回话就用 `wechat_send`;图片用 `wechat_send_image`,其他文件用 `wechat_send_file`,两者都接本机绝对路径。\n\n- 对方发来图片时,图片会作为附件一起到达,路径也写在正文里。看不到附件时,按路径自行读取。\n 正文说「图片未附:体积…/尺寸…/超出数量上限」时,那张图是真的没交给你,**不要猜图里有什么**。\n- 对方发来文件时,路径写在正文里;当前 Provider 支持的图片、音乐、视频和文档会同时作为附件到达,需要本地内容时按路径读。\n- 对方发来视频时,当前 Provider 支持原生视频就直接附上视频;不支持时保留抽帧方式,把几帧**拼成一张图**附上,\n 正文写明布局(如「2列×2行,从左上到右下按时间顺序」)、原视频分辨率与每格尺寸。\n 按这个顺序理解画面;每格都很小,别去描述看不清的细节。正文说「视频帧未抽取」就是真的没看过。\n- 对方发来语音时,正文保留微信服务端转写;Provider 接受实际音频格式时也会附上语音。微信语音通常是 SILK,原生音频能力不保证支持此编码。没有转写也没有可用附件时,不能猜说了什么。\n- 正文写明附件未附、下载失败或抽帧失败时,告诉对方具体原因;不要声称已听过或看过未提供的内容。模型请求被上游拒绝时,以 Core 报告的错误为准。\n\n## 能发什么\n\n只有你工作区里的文件能发到微信:\n\n```\n{{wechat.workspace | (工作区路径还没解析出来。)}}\n```\n\n收件目录在工作区里,所以收来的东西发得回去。工作区外面的路径会被直接拒绝——这不是故障,是边界:\n你的配置、密钥和这台机器的其他文件不在工作区的管辖范围内,不该被随手上传出去。\n\n要把工作区外的东西发出去,先把它复制进工作区(例如收件目录),再发复制之后那份。\n被拒绝时回执会列出允许的目录,照着调整路径就行,不要换个写法绕过它。\n\n## 约束\n\n这条通道只能回复最近发消息的人,不能主动找人,也不能发到别处。`wechat_send` 的回执会写明发给了谁。\n\n一条文本最多 4000 字,超出的部分会自动接着发成下一条,不必自己切。断行位置优先落在段落和句号上。\n\n## 分段发送\n\n这一段决定你说话的样子,值得花心思。\n\n打开分段发送时,你写的东西会被拆成好几条微信消息,一条一条发出去,条与条之间会停顿,让对方看着像你还在打字。\n\n拆的顺序按一次算,不会拆两次:\n\n- 你写了换行,就只按换行拆;换行会消失,段内不再按标点拆。紧挨换行的段尾句号也会被去掉,段内的句号留着。\n- 没写换行但有句号,就只按句号拆,句号会消失。\n- 都没写,但有 `!?…~` 或 `......` 这样的省略号,就按它们拆;连续符号只在整串末尾切一次,`~♡`、`……`、`!!!` 和尾随 emoji 都完整留着,换行前也保留省略号。\n- 都没有,就整条发出去。\n\n所以换行是你的节奏工具:**想让对方连看几行、想顿一下再说下一句,就写换行**。反过来,一句话里硬写换行会被拆成好几个气泡,看着像结巴。\n\n中文句号会被删掉,因为在微信里带句号显得生硬、像在写公文。这就是聊天里很少见句号的原因。别为了排版写句号。\n\n分段与间隔都是对方在配置页设的,你看不到也不必猜;照平时的习惯说话就行。\n\n语义段数达到配置阈值时,会恢复完整原文直接发送;超过 4000 字仍按微信上限分条。停顿按字数和「聊天节奏」计算,短句与断句失效的特长段直接发送。\n\n对方的 `context_token` 会过期。回复被拒并提示没有可用 token 时,等对方再说一句再回,不要反复重试。\n\n这里发出的一切都会真的进到对方的微信里。不确定要不要发的时候,先问清楚。\n"
|
|
167
|
+
"content": "微信 ClawBot 是一条一对一私聊通道,接在微信里,只能回复刚发消息的那个人。\n\n## 手边的状态\n\n{{wechat.state | (微信还没接上。)}}\n{{wechat.account | (还没有绑定的微信号。)}}\n{{wechat.lastMessageAt | (还没有收到过微信消息。)}}\n{{wechat.lastError | (最近没有连接错误。)}}\n\n## 收到的东西在哪\n\n对方发来的图片、文件、视频、语音都落在你的工作区里:\n\n```\n{{wechat.inbox | (收件目录还没解析出来。)}}\n```\n\n它下面按类型分成 `images/`、`files/`、`videos/`、`voices/`,文件名保留对方给的原文名,后面接一个本地时间戳。\n这些文件就是普通文件——**读得到、改得了、也发得回去**。想让对方看到改过之后的版本,改完直接\n用 `wechat_send_file` 发那个路径就行。\n\n## 什么时候用\n\n微信里来人说话时,会收到一条 `wechat-claw.message`,正文以 `[微信 时间 发送者]` 开头,媒体路径就写在里面。要回话就用 `wechat_send`;图片用 `wechat_send_image`,其他文件用 `wechat_send_file`,两者都接本机绝对路径。\n\n- 对方发来图片时,图片会作为附件一起到达,路径也写在正文里。看不到附件时,按路径自行读取。\n 正文说「图片未附:体积…/尺寸…/超出数量上限」时,那张图是真的没交给你,**不要猜图里有什么**。\n- 对方发来文件时,路径写在正文里;当前 Provider 支持的图片、音乐、视频和文档会同时作为附件到达,需要本地内容时按路径读。\n- 对方发来视频时,当前 Provider 支持原生视频就直接附上视频;不支持时保留抽帧方式,把几帧**拼成一张图**附上,\n 正文写明布局(如「2列×2行,从左上到右下按时间顺序」)、原视频分辨率与每格尺寸。\n 按这个顺序理解画面;每格都很小,别去描述看不清的细节。正文说「视频帧未抽取」就是真的没看过。\n- 对方发来语音时,正文保留微信服务端转写;Provider 接受实际音频格式时也会附上语音。微信语音通常是 SILK,原生音频能力不保证支持此编码。没有转写也没有可用附件时,不能猜说了什么。\n- 正文写明附件未附、下载失败或抽帧失败时,告诉对方具体原因;不要声称已听过或看过未提供的内容。模型请求被上游拒绝时,以 Core 报告的错误为准。\n- 想确认当前输入能力时,用 `wechat_status` 的 `mimes` 查询具体 MIME;它还报告接收开关和最近附件交给 Core 的结果。能力为真但没有附件时,检查是否未下载、超限或读取失败,不能把磁盘路径当作已发送的附件。\n\n## 能发什么\n\n只有你工作区里的文件能发到微信:\n\n```\n{{wechat.workspace | (工作区路径还没解析出来。)}}\n```\n\n收件目录在工作区里,所以收来的东西发得回去。工作区外面的路径会被直接拒绝——这不是故障,是边界:\n你的配置、密钥和这台机器的其他文件不在工作区的管辖范围内,不该被随手上传出去。\n\n要把工作区外的东西发出去,先把它复制进工作区(例如收件目录),再发复制之后那份。\n被拒绝时回执会列出允许的目录,照着调整路径就行,不要换个写法绕过它。\n\n## 约束\n\n这条通道只能回复最近发消息的人,不能主动找人,也不能发到别处。`wechat_send` 的回执会写明发给了谁。\n\n一条文本最多 4000 字,超出的部分会自动接着发成下一条,不必自己切。断行位置优先落在段落和句号上。\n\n## 分段发送\n\n这一段决定你说话的样子,值得花心思。\n\n打开分段发送时,你写的东西会被拆成好几条微信消息,一条一条发出去,条与条之间会停顿,让对方看着像你还在打字。\n\n拆的顺序按一次算,不会拆两次:\n\n- 你写了换行,就只按换行拆;换行会消失,段内不再按标点拆。紧挨换行的段尾句号也会被去掉,段内的句号留着。\n- 没写换行但有句号,就只按句号拆,句号会消失。\n- 都没写,但有 `!?…~` 或 `......` 这样的省略号,就按它们拆;连续符号只在整串末尾切一次,`~♡`、`……`、`!!!` 和尾随 emoji 都完整留着,换行前也保留省略号。\n- 都没有,就整条发出去。\n\n所以换行是你的节奏工具:**想让对方连看几行、想顿一下再说下一句,就写换行**。反过来,一句话里硬写换行会被拆成好几个气泡,看着像结巴。\n\n中文句号会被删掉,因为在微信里带句号显得生硬、像在写公文。这就是聊天里很少见句号的原因。别为了排版写句号。\n\n分段与间隔都是对方在配置页设的,你看不到也不必猜;照平时的习惯说话就行。\n\n语义段数达到配置阈值时,会恢复完整原文直接发送;超过 4000 字仍按微信上限分条。停顿按字数和「聊天节奏」计算,短句与断句失效的特长段直接发送。\n\n对方的 `context_token` 会过期。回复被拒并提示没有可用 token 时,等对方再说一句再回,不要反复重试。\n\n这里发出的一切都会真的进到对方的微信里。不确定要不要发的时候,先问清楚。\n"
|
|
158
168
|
}
|
|
159
169
|
]
|
|
160
170
|
}
|
package/package.json
CHANGED
package/src/ENV_PROMPT.md
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
按这个顺序理解画面;每格都很小,别去描述看不清的细节。正文说「视频帧未抽取」就是真的没看过。
|
|
32
32
|
- 对方发来语音时,正文保留微信服务端转写;Provider 接受实际音频格式时也会附上语音。微信语音通常是 SILK,原生音频能力不保证支持此编码。没有转写也没有可用附件时,不能猜说了什么。
|
|
33
33
|
- 正文写明附件未附、下载失败或抽帧失败时,告诉对方具体原因;不要声称已听过或看过未提供的内容。模型请求被上游拒绝时,以 Core 报告的错误为准。
|
|
34
|
+
- 想确认当前输入能力时,用 `wechat_status` 的 `mimes` 查询具体 MIME;它还报告接收开关和最近附件交给 Core 的结果。能力为真但没有附件时,检查是否未下载、超限或读取失败,不能把磁盘路径当作已发送的附件。
|
|
34
35
|
|
|
35
36
|
## 能发什么
|
|
36
37
|
|
package/src/config.ts
CHANGED
|
@@ -155,13 +155,13 @@ export const WECHAT_CLAW_CONFIG_GROUP: ConfigGroup = {
|
|
|
155
155
|
title: '收文件',
|
|
156
156
|
description:
|
|
157
157
|
'打开后,对方发来的文件会下载到收件目录(默认工作区下的 `wechat-inbox/files`),'
|
|
158
|
-
+ '
|
|
158
|
+
+ 'Provider 支持的图片、音频、视频、PDF 等按实际 MIME 附在消息中;路径也会写进上下文。',
|
|
159
159
|
'x-hot': true,
|
|
160
160
|
},
|
|
161
161
|
'worlds.wechat-claw.receiveVideos': {
|
|
162
162
|
type: 'boolean',
|
|
163
163
|
title: '收视频',
|
|
164
|
-
description: '
|
|
164
|
+
description: '打开后下载微信视频消息,Provider 支持实际视频格式时直接附上,不支持时抽帧。体积大,默认关闭;使用原生视频也需打开。',
|
|
165
165
|
'x-hot': true,
|
|
166
166
|
},
|
|
167
167
|
'worlds.wechat-claw.receiveVoices': {
|
package/src/protocol/inbound.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* `context_token` 是回复凭据,绝不进事件 meta、日志或模型上下文。
|
|
5
5
|
* 媒体按类型下载解密后落到**桌宠工作区里的收件目录**(见 `paths.ts`),路径写进事件正文,
|
|
6
|
-
*
|
|
6
|
+
* World 按实际 MIME 查询 Provider;受支持的字节作为事件附件交给 Core,其余保留路径与原因。
|
|
7
7
|
*
|
|
8
8
|
* 落盘位置与文件名都是给桌宠看的:目录按类型分,文件名保留对方给的原文名,
|
|
9
9
|
* 后面接一个本地时间戳。这样它读得到、改得了,也能原样发回去。
|
|
@@ -12,7 +12,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
|
|
|
12
12
|
import { join } from 'node:path';
|
|
13
13
|
|
|
14
14
|
import { datedFileName, uniqueFileName } from '../paths.ts';
|
|
15
|
-
import { extensionForInbound, imageSize,
|
|
15
|
+
import { extensionForInbound, imageSize, mimeForInbound, sniffImageMime } from './mime.ts';
|
|
16
16
|
import type { MediaTransfer } from './media.ts';
|
|
17
17
|
import { MessageItemType, type MessageItem, type WeixinMessage } from './types.ts';
|
|
18
18
|
|
|
@@ -216,7 +216,7 @@ export async function renderInbound(
|
|
|
216
216
|
lines.push(missText(fetched, `语音 ${secondsText}`, opts.maxBytes));
|
|
217
217
|
continue;
|
|
218
218
|
}
|
|
219
|
-
const enc = voiceEncodingName(voice?.encode_type);
|
|
219
|
+
const enc = fetched.mime === voiceMime(voice?.encode_type) ? voiceEncodingName(voice?.encode_type) : fetched.mime;
|
|
220
220
|
lines.push(`[语音 ${secondsText},${enc} 编码,已存为 ${fetched.path}]`);
|
|
221
221
|
media.push({ ...fetched, kind: 'voice', label: `[语音 ${secondsText}]` });
|
|
222
222
|
continue;
|
|
@@ -334,7 +334,7 @@ async function fetchItem(
|
|
|
334
334
|
maxBytes: opts.maxBytes,
|
|
335
335
|
});
|
|
336
336
|
const name = String(file?.file_name ?? '');
|
|
337
|
-
const mime =
|
|
337
|
+
const mime = mimeForInbound(bytes, name);
|
|
338
338
|
const path = await saveMedia({
|
|
339
339
|
bytes,
|
|
340
340
|
dir: join(opts.inboxDir, 'files'),
|
|
@@ -352,12 +352,13 @@ async function fetchItem(
|
|
|
352
352
|
aesKeyBase64: ref.aes_key,
|
|
353
353
|
maxBytes: opts.maxBytes,
|
|
354
354
|
});
|
|
355
|
+
const mime = mimeForInbound(bytes, '', 'video/mp4');
|
|
355
356
|
const path = await saveMedia({
|
|
356
357
|
bytes,
|
|
357
358
|
dir: join(opts.inboxDir, 'videos'),
|
|
358
|
-
name: datedFileName(null, 'wx-video',
|
|
359
|
+
name: datedFileName(null, 'wx-video', extensionForInbound(mime, bytes)),
|
|
359
360
|
});
|
|
360
|
-
return { ok: true, path, mime
|
|
361
|
+
return { ok: true, path, mime, bytes: bytes.length };
|
|
361
362
|
}
|
|
362
363
|
const voice = item.voice_item;
|
|
363
364
|
const ref = voice?.media;
|
|
@@ -368,7 +369,7 @@ async function fetchItem(
|
|
|
368
369
|
aesKeyBase64: ref.aes_key,
|
|
369
370
|
maxBytes: opts.maxBytes,
|
|
370
371
|
});
|
|
371
|
-
const mime = voiceMime(voice?.encode_type);
|
|
372
|
+
const mime = mimeForInbound(bytes, '', voiceMime(voice?.encode_type));
|
|
372
373
|
const path = await saveMedia({
|
|
373
374
|
bytes,
|
|
374
375
|
dir: join(opts.inboxDir, 'voices'),
|
package/src/protocol/mime.ts
CHANGED
|
@@ -13,12 +13,27 @@ const EXT_TO_MIME: Record<string, string> = {
|
|
|
13
13
|
'.gif': 'image/gif',
|
|
14
14
|
'.webp': 'image/webp',
|
|
15
15
|
'.bmp': 'image/bmp',
|
|
16
|
+
'.avif': 'image/avif',
|
|
17
|
+
'.heic': 'image/heic',
|
|
18
|
+
'.heif': 'image/heif',
|
|
19
|
+
'.tif': 'image/tiff',
|
|
20
|
+
'.tiff': 'image/tiff',
|
|
16
21
|
'.mp4': 'video/mp4',
|
|
17
22
|
'.mov': 'video/quicktime',
|
|
18
23
|
'.webm': 'video/webm',
|
|
19
24
|
'.mkv': 'video/x-matroska',
|
|
20
25
|
'.avi': 'video/x-msvideo',
|
|
26
|
+
'.mpeg': 'video/mpeg',
|
|
27
|
+
'.mpg': 'video/mpeg',
|
|
28
|
+
'.m4v': 'video/mp4',
|
|
29
|
+
'.3gp': 'video/3gpp',
|
|
30
|
+
'.3g2': 'video/3gpp2',
|
|
31
|
+
'.ogv': 'video/ogg',
|
|
21
32
|
'.mp3': 'audio/mpeg',
|
|
33
|
+
'.mpga': 'audio/mpeg',
|
|
34
|
+
'.aif': 'audio/aiff',
|
|
35
|
+
'.aiff': 'audio/aiff',
|
|
36
|
+
'.weba': 'audio/webm',
|
|
22
37
|
'.ogg': 'audio/ogg',
|
|
23
38
|
'.wav': 'audio/wav',
|
|
24
39
|
'.m4a': 'audio/mp4',
|
|
@@ -27,6 +42,8 @@ const EXT_TO_MIME: Record<string, string> = {
|
|
|
27
42
|
'.opus': 'audio/opus',
|
|
28
43
|
'.amr': 'audio/amr',
|
|
29
44
|
'.silk': 'audio/silk',
|
|
45
|
+
'.pcm': 'audio/pcm',
|
|
46
|
+
'.spx': 'audio/speex',
|
|
30
47
|
'.pdf': 'application/pdf',
|
|
31
48
|
'.doc': 'application/msword',
|
|
32
49
|
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
@@ -36,6 +53,11 @@ const EXT_TO_MIME: Record<string, string> = {
|
|
|
36
53
|
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
37
54
|
'.txt': 'text/plain',
|
|
38
55
|
'.md': 'text/markdown',
|
|
56
|
+
'.markdown': 'text/markdown',
|
|
57
|
+
'.html': 'text/html',
|
|
58
|
+
'.htm': 'text/html',
|
|
59
|
+
'.xml': 'application/xml',
|
|
60
|
+
'.rtf': 'application/rtf',
|
|
39
61
|
'.csv': 'text/csv',
|
|
40
62
|
'.json': 'application/json',
|
|
41
63
|
'.zip': 'application/zip',
|
|
@@ -80,10 +102,53 @@ export function mimeFromFilename(filename: string): string {
|
|
|
80
102
|
return EXT_TO_MIME[extname(String(filename ?? '')).toLowerCase()] ?? OCTET_STREAM;
|
|
81
103
|
}
|
|
82
104
|
|
|
105
|
+
/** 入站文件先按可确认的容器头判类型,无法确认时保留文件名或微信编码给出的 MIME。 */
|
|
106
|
+
export function mimeForInbound(bytes: Uint8Array, filename = '', fallback = OCTET_STREAM): string {
|
|
107
|
+
const hint = mimeFromFilename(filename);
|
|
108
|
+
const claimed = hint === OCTET_STREAM ? fallback : hint;
|
|
109
|
+
const b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
110
|
+
const head = b.toString('latin1', 0, 16);
|
|
111
|
+
const image = sniffImageMime(bytes);
|
|
112
|
+
if (image) return image;
|
|
113
|
+
if (head.startsWith('%PDF-')) return 'application/pdf';
|
|
114
|
+
if (head.startsWith('RIFF')) {
|
|
115
|
+
if (head.slice(8, 12) === 'WAVE') return 'audio/wav';
|
|
116
|
+
if (head.slice(8, 12) === 'AVI ') return 'video/x-msvideo';
|
|
117
|
+
}
|
|
118
|
+
if (head.startsWith('fLaC')) return 'audio/flac';
|
|
119
|
+
if (head.startsWith('OggS')) return claimed.startsWith('video/') ? 'video/ogg' : 'audio/ogg';
|
|
120
|
+
if (head.startsWith('FORM') && ['AIFF', 'AIFC'].includes(head.slice(8, 12))) return 'audio/aiff';
|
|
121
|
+
if (head.startsWith('#!AMR-WB')) return 'audio/amr-wb';
|
|
122
|
+
if (head.startsWith('#!AMR')) return 'audio/amr';
|
|
123
|
+
if (head.startsWith('#!SILK_V3') || head.startsWith('\x02#!SILK_V3')) return 'audio/silk';
|
|
124
|
+
if (head.startsWith('ID3')) return 'audio/mpeg';
|
|
125
|
+
if (b.length >= 2 && b[0] === 0xff) {
|
|
126
|
+
if ((b[1]! & 0xf6) === 0xf0) return 'audio/aac';
|
|
127
|
+
if ((b[1]! & 0xe0) === 0xe0) return 'audio/mpeg';
|
|
128
|
+
}
|
|
129
|
+
if (head.slice(4, 8) === 'ftyp') {
|
|
130
|
+
const brand = head.slice(8, 12);
|
|
131
|
+
if (['avif', 'avis'].includes(brand)) return 'image/avif';
|
|
132
|
+
if (['heic', 'heix', 'hevc', 'hevx'].includes(brand)) return 'image/heic';
|
|
133
|
+
if (['mif1', 'msf1'].includes(brand)) return claimed === 'image/heic' ? claimed : 'image/heif';
|
|
134
|
+
if (brand === 'qt ') return 'video/quicktime';
|
|
135
|
+
if (brand.startsWith('3g2')) return 'video/3gpp2';
|
|
136
|
+
if (brand.startsWith('3gp')) return 'video/3gpp';
|
|
137
|
+
// MP4、M4A 共用容器;音轨类型由品牌或原文件类型区分。
|
|
138
|
+
return brand === 'M4A ' || claimed.startsWith('audio/') ? 'audio/mp4' : 'video/mp4';
|
|
139
|
+
}
|
|
140
|
+
if (head.startsWith('\x1aE\xdf\xa3')) {
|
|
141
|
+
const prefix = b.toString('latin1', 0, Math.min(b.length, 4096));
|
|
142
|
+
if (prefix.includes('webm')) return claimed.startsWith('audio/') ? 'audio/webm' : 'video/webm';
|
|
143
|
+
if (prefix.includes('matroska')) return 'video/x-matroska';
|
|
144
|
+
}
|
|
145
|
+
return claimed;
|
|
146
|
+
}
|
|
147
|
+
|
|
83
148
|
/** MIME → 扩展名;不认识时 `.bin`。 */
|
|
84
149
|
export function extensionFromMime(mime: string): string {
|
|
85
150
|
const base = String(mime ?? '').split(';')[0].trim().toLowerCase();
|
|
86
|
-
return MIME_TO_EXT[base] ?? '.bin';
|
|
151
|
+
return MIME_TO_EXT[base] ?? Object.keys(EXT_TO_MIME).find((ext) => EXT_TO_MIME[ext] === base) ?? '.bin';
|
|
87
152
|
}
|
|
88
153
|
|
|
89
154
|
/** 按魔数猜图片类型,兜住入站图片没有文件名的情况。 */
|
package/src/world.ts
CHANGED
|
@@ -77,6 +77,18 @@ const PACKAGE_VERSION: string = pkg.version;
|
|
|
77
77
|
/** 发消息时上报的观测标识,形状是 UA 的 `Name/Version`。 */
|
|
78
78
|
const BOT_AGENT = `Cortico/6 (Coopanion; cortico-world-wechat-claw/${PACKAGE_VERSION})`;
|
|
79
79
|
|
|
80
|
+
type AttachmentBlob = Extract<BlobInput, { bytes: Uint8Array }>;
|
|
81
|
+
|
|
82
|
+
interface InboundAttachment {
|
|
83
|
+
kind: string;
|
|
84
|
+
name: string;
|
|
85
|
+
mime: string;
|
|
86
|
+
bytes: number;
|
|
87
|
+
delivery: 'native' | 'frames' | 'omitted';
|
|
88
|
+
attachedMimes: string[];
|
|
89
|
+
notes: string[];
|
|
90
|
+
}
|
|
91
|
+
|
|
80
92
|
/** 绑定流程的界面状态。 */
|
|
81
93
|
interface BindingState {
|
|
82
94
|
active: boolean;
|
|
@@ -174,8 +186,15 @@ export const WECHAT_CLAW_TOOL_DECLS: ReadonlyArray<Omit<ToolDef, 'handler'>> = [
|
|
|
174
186
|
tags: ['read', 'snapshot'],
|
|
175
187
|
description:
|
|
176
188
|
'Report the WeChat ClawBot connection: bound account, whether the long poll is connected, '
|
|
177
|
-
+ 'the last inbound message time, the last error and how many context tokens are held.'
|
|
178
|
-
|
|
189
|
+
+ 'the last inbound message time, the last error and how many context tokens are held. '
|
|
190
|
+
+ 'Also reports live model input support, receive switches and the last attachment delivery results. '
|
|
191
|
+
+ 'Pass mimes to check specific file types against the active Provider.',
|
|
192
|
+
parameters: {
|
|
193
|
+
type: 'object', additionalProperties: false,
|
|
194
|
+
properties: {
|
|
195
|
+
mimes: { type: 'array', maxItems: 128, items: { type: 'string', minLength: 1 }, description: 'Optional exact MIME types, such as audio/wav, video/mp4 or application/pdf.' },
|
|
196
|
+
},
|
|
197
|
+
},
|
|
179
198
|
},
|
|
180
199
|
{
|
|
181
200
|
name: 'wechat_events',
|
|
@@ -240,6 +259,7 @@ export class WechatClawWorld implements World {
|
|
|
240
259
|
/** 段间等待的唤醒钩子;stop() 用它打断睡眠。 */
|
|
241
260
|
#sleepWake: (() => void) | null = null;
|
|
242
261
|
#lastInboundTiming: { receivedAt: string; receiveDelayMs: number; queueMs: number; processingMs: number } | null = null;
|
|
262
|
+
#lastInboundAttachments: InboundAttachment[] = [];
|
|
243
263
|
#networkProblem = '';
|
|
244
264
|
#ffmpeg: FfmpegResolution | null = null;
|
|
245
265
|
/** ffmpeg 安装进度;null 表示没在装。 */
|
|
@@ -305,7 +325,7 @@ export class WechatClawWorld implements World {
|
|
|
305
325
|
case 'wechat_send': return await this.#toolSendText(args);
|
|
306
326
|
case 'wechat_send_image': return await this.#toolSendMedia(args, 'image');
|
|
307
327
|
case 'wechat_send_file': return await this.#toolSendMedia(args, 'file');
|
|
308
|
-
case 'wechat_status': return this.#toolStatus();
|
|
328
|
+
case 'wechat_status': return this.#toolStatus(args);
|
|
309
329
|
case 'wechat_events': return this.#toolEvents(args);
|
|
310
330
|
default: return `[wechat] 未知工具:${name}`;
|
|
311
331
|
}
|
|
@@ -524,8 +544,20 @@ export class WechatClawWorld implements World {
|
|
|
524
544
|
return [...new Set(roots.filter(Boolean))];
|
|
525
545
|
}
|
|
526
546
|
|
|
527
|
-
#toolStatus(): string {
|
|
547
|
+
#toolStatus(args: Record<string, unknown>): string {
|
|
528
548
|
const state = this.#publicState();
|
|
549
|
+
if (args.mimes !== undefined && (!Array.isArray(args.mimes) || args.mimes.length > 128
|
|
550
|
+
|| args.mimes.some((mime) => typeof mime !== 'string' || !mime.trim()))) {
|
|
551
|
+
throw new Error('mimes 必须是最多 128 项的 MIME 字符串数组。');
|
|
552
|
+
}
|
|
553
|
+
const mimes = [...new Set((args.mimes as string[] | undefined) ?? [
|
|
554
|
+
'image/png', 'image/jpeg', 'audio/mpeg', 'audio/wav', 'audio/silk', 'video/mp4', 'application/pdf', 'text/plain',
|
|
555
|
+
...this.#lastInboundAttachments.map((item) => item.mime),
|
|
556
|
+
])];
|
|
557
|
+
let model = '';
|
|
558
|
+
let modelError: string | undefined;
|
|
559
|
+
try { model = this.#host?.modelFacts.model() ?? ''; }
|
|
560
|
+
catch (err) { modelError = errorText(err); }
|
|
529
561
|
return JSON.stringify({
|
|
530
562
|
bound: state.bound,
|
|
531
563
|
connected: state.connected,
|
|
@@ -536,6 +568,14 @@ export class WechatClawWorld implements World {
|
|
|
536
568
|
userId: state.userId,
|
|
537
569
|
lastMessageAt: state.lastMessageAt,
|
|
538
570
|
lastInboundTiming: this.#lastInboundTiming,
|
|
571
|
+
lastInboundAttachments: this.#lastInboundAttachments,
|
|
572
|
+
inputSupport: {
|
|
573
|
+
model,
|
|
574
|
+
...(modelError ? { error: modelError } : {}),
|
|
575
|
+
mimes: Object.fromEntries(mimes.map((mime) => [mime, this.#mimeSupport(mime)])),
|
|
576
|
+
receive: { images: this.#cfg.receiveImages !== false, files: this.#cfg.receiveFiles !== false,
|
|
577
|
+
videos: this.#cfg.receiveVideos === true, voices: this.#cfg.receiveVoices !== false },
|
|
578
|
+
},
|
|
539
579
|
lastError: state.lastError,
|
|
540
580
|
contextTokens: state.contextTokens,
|
|
541
581
|
startedAt: state.startedAt,
|
|
@@ -778,6 +818,7 @@ export class WechatClawWorld implements World {
|
|
|
778
818
|
this.#contextCount = 0;
|
|
779
819
|
this.#lastMessageAt = '';
|
|
780
820
|
this.#lastInboundTiming = null;
|
|
821
|
+
this.#lastInboundAttachments = [];
|
|
781
822
|
this.#typingTickets.clear();
|
|
782
823
|
this.#lastError = '';
|
|
783
824
|
|
|
@@ -1381,42 +1422,55 @@ export class WechatClawWorld implements World {
|
|
|
1381
1422
|
maxBytes,
|
|
1382
1423
|
});
|
|
1383
1424
|
|
|
1384
|
-
const blobs:
|
|
1425
|
+
const blobs: AttachmentBlob[] = [];
|
|
1385
1426
|
const host = this.#host;
|
|
1386
1427
|
// 闸门里要写进正文的说明(压缩了哪张、哪张被丢掉、为什么)。
|
|
1387
1428
|
const fitNotes: string[] = [];
|
|
1429
|
+
const attachments: InboundAttachment[] = [];
|
|
1388
1430
|
// 一次投递累计附了几张图,用来卡上限。
|
|
1389
1431
|
let imageCount = 0;
|
|
1390
1432
|
const limits = this.#imageLimits();
|
|
1391
1433
|
|
|
1392
1434
|
for (const item of rendered.media) {
|
|
1393
1435
|
if (driver !== this.#driver || !this.#started) return;
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1436
|
+
const before = blobs.length;
|
|
1437
|
+
const noteIndex = fitNotes.length;
|
|
1438
|
+
try {
|
|
1439
|
+
// 微信的文件通道也可能装着图片、音乐或视频,按实际 MIME 投递。
|
|
1440
|
+
if (item.mime.startsWith('image/')) {
|
|
1441
|
+
imageCount += 1;
|
|
1442
|
+
if (imageCount > limits.maxCount) {
|
|
1443
|
+
fitNotes.push(`[图片未附:${overCountNote(imageCount - 1, limits)}]`);
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
await this.#attachImage(blobs, item, limits, fitNotes);
|
|
1399
1447
|
continue;
|
|
1400
1448
|
}
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
else
|
|
1414
|
-
|
|
1415
|
-
|
|
1449
|
+
const support = this.#mimeSupport(item.mime);
|
|
1450
|
+
if (support.error) {
|
|
1451
|
+
fitNotes.push(`${item.label} 未附:附件能力查询失败(${item.mime},${support.error})。`);
|
|
1452
|
+
} else if (support.accepted) {
|
|
1453
|
+
await this.#addBlob(blobs, item.path, item.mime, item.label, fitNotes);
|
|
1454
|
+
} else if (item.mime.startsWith('video/')) {
|
|
1455
|
+
const note = await this.#attachFrames(blobs, item.path, this.#acceptsMime('image/jpeg', fitNotes), limits, fitNotes, (n) => {
|
|
1456
|
+
imageCount += n;
|
|
1457
|
+
return imageCount;
|
|
1458
|
+
});
|
|
1459
|
+
if (note) fitNotes.push(note);
|
|
1460
|
+
else if (!(Number(this.#cfg.videoFrames) > 0)) fitNotes.push('[视频未附:当前 Provider 不接受此视频格式,且已关闭抽帧]');
|
|
1461
|
+
} else {
|
|
1462
|
+
fitNotes.push(`${item.label} 未附:当前 Provider 不接受 ${item.mime} 输入。`);
|
|
1463
|
+
}
|
|
1464
|
+
} finally {
|
|
1465
|
+
const attachedMimes = blobs.slice(before).map((blob) => blob.mime);
|
|
1466
|
+
attachments.push({ kind: item.kind, name: basename(item.path), mime: item.mime, bytes: item.bytes,
|
|
1467
|
+
delivery: attachedMimes.length ? (item.mime.startsWith('video/') && attachedMimes[0].startsWith('image/') ? 'frames' : 'native') : 'omitted',
|
|
1468
|
+
attachedMimes, notes: fitNotes.slice(noteIndex) });
|
|
1416
1469
|
}
|
|
1417
1470
|
}
|
|
1418
1471
|
|
|
1419
1472
|
if (driver !== this.#driver || !this.#started) return;
|
|
1473
|
+
this.#lastInboundAttachments = attachments;
|
|
1420
1474
|
this.#lastInboundTiming = {
|
|
1421
1475
|
receivedAt: new Date(receivedAtMs).toISOString(),
|
|
1422
1476
|
receiveDelayMs: Math.max(0, receivedAtMs - atMs),
|
|
@@ -1443,7 +1497,7 @@ export class WechatClawWorld implements World {
|
|
|
1443
1497
|
raw: rendered.raw,
|
|
1444
1498
|
...(rendered.transcript ? { transcript: rendered.transcript } : {}),
|
|
1445
1499
|
...(rendered.quote ? { quote: rendered.quote } : {}),
|
|
1446
|
-
media: rendered.media.map((item) => ({
|
|
1500
|
+
media: rendered.media.map((item, i) => ({ ...attachments[i], label: item.label })),
|
|
1447
1501
|
timing: this.#lastInboundTiming,
|
|
1448
1502
|
},
|
|
1449
1503
|
}, { deliver: true, trigger: 'debounce' });
|
|
@@ -1452,18 +1506,29 @@ export class WechatClawWorld implements World {
|
|
|
1452
1506
|
void this.#signalTyping(from, contextToken);
|
|
1453
1507
|
}
|
|
1454
1508
|
|
|
1455
|
-
#
|
|
1509
|
+
#mimeSupport(mime: string): { accepted: boolean; error?: string } {
|
|
1510
|
+
if (!this.#host?.modelFacts) return { accepted: false, error: 'World 未运行或宿主未提供模型能力查询。' };
|
|
1456
1511
|
try {
|
|
1457
|
-
return this.#host
|
|
1512
|
+
return { accepted: this.#host.modelFacts.accepts(mime) };
|
|
1458
1513
|
} catch (err) {
|
|
1459
|
-
|
|
1460
|
-
return false;
|
|
1514
|
+
return { accepted: false, error: errorText(err) };
|
|
1461
1515
|
}
|
|
1462
1516
|
}
|
|
1463
1517
|
|
|
1518
|
+
#acceptsMime(mime: string, notes: string[]): boolean {
|
|
1519
|
+
const result = this.#mimeSupport(mime);
|
|
1520
|
+
if (result.error) notes.push(`[附件能力查询失败:${mime},${result.error}]`);
|
|
1521
|
+
return result.accepted;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1464
1524
|
/** 读取失败或能力不支持时取消附件,并在事件正文告知原因。 */
|
|
1465
|
-
async #addBlob(blobs:
|
|
1466
|
-
|
|
1525
|
+
async #addBlob(blobs: AttachmentBlob[], path: string, mime: string, label: string, notes: string[]): Promise<void> {
|
|
1526
|
+
const support = this.#mimeSupport(mime);
|
|
1527
|
+
if (support.error) {
|
|
1528
|
+
notes.push(`${label} 未附:附件能力查询失败(${mime},${support.error})。`);
|
|
1529
|
+
return;
|
|
1530
|
+
}
|
|
1531
|
+
if (!support.accepted) {
|
|
1467
1532
|
notes.push(`${label} 未附:当前 Provider 不接受 ${mime} 输入。`);
|
|
1468
1533
|
return;
|
|
1469
1534
|
}
|
|
@@ -1498,7 +1563,7 @@ export class WechatClawWorld implements World {
|
|
|
1498
1563
|
* 先看元数据再决定读不读内容:32 MB 的图不该先整读进内存再判断。
|
|
1499
1564
|
*/
|
|
1500
1565
|
async #attachImage(
|
|
1501
|
-
blobs:
|
|
1566
|
+
blobs: AttachmentBlob[],
|
|
1502
1567
|
item: { path: string; mime: string; label: string; bytes?: number },
|
|
1503
1568
|
limits: ImageLimits,
|
|
1504
1569
|
notes: string[],
|
|
@@ -1536,7 +1601,7 @@ export class WechatClawWorld implements World {
|
|
|
1536
1601
|
* 抽不出来时返回一句写进正文的说明——**不假装看过视频**。
|
|
1537
1602
|
*/
|
|
1538
1603
|
async #attachFrames(
|
|
1539
|
-
blobs:
|
|
1604
|
+
blobs: AttachmentBlob[],
|
|
1540
1605
|
videoPath: string,
|
|
1541
1606
|
acceptsImages: boolean,
|
|
1542
1607
|
limits: ImageLimits,
|