cortico-world-wechat-claw 0.1.6 → 0.1.8
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 +14 -0
- package/README.md +59 -22
- package/describe.json +3 -3
- package/package.json +4 -4
- package/src/ENV_PROMPT.md +7 -4
- package/src/config.ts +16 -38
- package/src/definition.ts +2 -2
- package/src/driver.ts +3 -2
- package/src/protocol/inbound.ts +16 -2
- package/src/protocol/mime.ts +13 -0
- package/src/segment.ts +46 -61
- package/src/store.ts +11 -0
- package/src/text.ts +3 -3
- package/src/world.ts +100 -103
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.8
|
|
4
|
+
|
|
5
|
+
- 连续语气标点、爱心与 emoji 作为完整符号串处理,修复 `~♡`、`……`、`?!`、`!!!` 在中途被切开并单独发送的问题;符号间仅有空白时也不会拆成碎片。
|
|
6
|
+
- ASCII 省略号 `......` 完整保留,不再误删最后一个点,换行前也不再被整串清除;保留普通句号、小数、域名与相对路径的原有处理。
|
|
7
|
+
|
|
8
|
+
## 0.1.7
|
|
9
|
+
|
|
10
|
+
- 语义段数达到阈值时取消分段,恢复完整原文;超 4000 字仍分条,避免合并后的尾段超限而发送失败。
|
|
11
|
+
- 固定与随机间隔改为「聊天节奏」:按字数决定停顿,短句和特长段立即发送。旧间隔配置不再使用,新节奏默认 1。
|
|
12
|
+
- 按 Provider 声明的实际 MIME 支持投递图片、音乐、语音、视频和文档,文件通道内的媒体也能附上。语音按实际编码保存。
|
|
13
|
+
- 原生视频优先;Provider 不接受视频时继续抽帧。附件未提供的具体原因写进事件正文。
|
|
14
|
+
- 最近消息时间按部署时区显示为 `MM/DD HH:mm`,重启后恢复,解绑时清除。
|
|
15
|
+
- `wechat_status` 和入站日志增加收取延迟、批内排队与本地处理耗时。收信仍参与 Core 的 debounce 合批。
|
|
16
|
+
|
|
3
17
|
## 0.1.6
|
|
4
18
|
|
|
5
19
|
收来的文件从「插件自己的数据目录」搬进**桌宠工作区**,并把「能发什么」收紧到工作区,
|
package/README.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
通过微信 ClawBot(iLink)和 Coopanion 桌宠一对一聊天。**文字、图片、文件双向收发。**
|
|
5
5
|
|
|
6
|
+
文件这条路是这么走的:微信里发来的东西**落在桌宠自己的工作区里**(`wechat-inbox/`),所以它读得到、
|
|
7
|
+
改得了、也能改完再发回去;反过来,**能发到微信的只有工作区里的文件**——工作区外面(凭据、系统目录、
|
|
8
|
+
浏览器数据)不在范围里,模型再被说服也上传不出去。
|
|
9
|
+
|
|
6
10
|
这是独立开发的实现,不复用 `cortico-world-clawbot` 的代码。协议形状对齐腾讯官方
|
|
7
11
|
[`@tencent-weixin/openclaw-weixin`](https://www.npmjs.com/package/@tencent-weixin/openclaw-weixin)
|
|
8
12
|
2.4.10 与它的[后端协议文档](https://github.com/Tencent/openclaw-weixin/blob/main/docs/protocol_zh_CN.md)。
|
|
@@ -12,15 +16,16 @@
|
|
|
12
16
|
| | 现成 `cortico-world-clawbot` 0.1.0 | 这个扩展 |
|
|
13
17
|
|---|---|---|
|
|
14
18
|
| 文本收发 | 支持 | 支持 |
|
|
15
|
-
| 收图片 | 只显示 `[图片]` 占位 |
|
|
16
|
-
| 发图片 | 不支持 | `wechat_send_image`:加密上传到微信 CDN
|
|
17
|
-
| 收文件 | 只显示 `[文件 名字]` 占位 |
|
|
18
|
-
| 发文件 | 不支持 | `wechat_send_file
|
|
19
|
-
|
|
|
19
|
+
| 收图片 | 只显示 `[图片]` 占位 | 解密落到桌宠工作区的 `wechat-inbox/images/`,路径进上下文,模型支持图片时作为附件到达 |
|
|
20
|
+
| 发图片 | 不支持 | `wechat_send_image`:加密上传到微信 CDN 再发;**路径必须在工作区内** |
|
|
21
|
+
| 收文件 | 只显示 `[文件 名字]` 占位 | 解密落到 `wechat-inbox/files/`,保留对方给的原文名;她读得到、改得了,也能原样发回去 |
|
|
22
|
+
| 发文件 | 不支持 | `wechat_send_file`:同上,带原文件名与明文长度;同样只限工作区 |
|
|
23
|
+
| 上传来源 | —— | 只限工作区+收件目录+`sendExtraDirs`;先 `realpath` 再判定,`..` 与符号链接都绕不出去 |
|
|
24
|
+
| 语音 | 占位 | 保留服务端转写文本;原音频落到 `wechat-inbox/voices/`,Provider 支持实际编码时同时附上 |
|
|
20
25
|
| 长文本 | 超 4000 字被拒 | 自动切成多条发送,优先在段落与句号处断开 |
|
|
21
26
|
| 出站视频 | 不支持 | 按 MIME 走视频通道,微信里显示成视频而不是附件 |
|
|
22
27
|
| 入站超限 | 无区分 | 超限、缺参数、下载失败三种原因分别措辞 |
|
|
23
|
-
| 视频 | 占位 |
|
|
28
|
+
| 视频 | 占位 | 可选下载(默认关);Provider 接受时直接附视频,否则按原逻辑抽帧拼图,失败写明原因 |
|
|
24
29
|
| 网络出口 | inherit / direct / proxy | 相同,且把「配置要的出口做不到」如实报出来 |
|
|
25
30
|
| 令牌失效 | 直接断开并清凭据 | 按官方约定暂停一小时后重试,只有 HTTP 401/403 才判定会话过期 |
|
|
26
31
|
| 绑定面板 | 有 | 有,走内建 UI 原语,带二维码、扫码状态、验证码输入 |
|
|
@@ -30,14 +35,14 @@
|
|
|
30
35
|
| 工具 | 作用 |
|
|
31
36
|
|---|---|
|
|
32
37
|
| `wechat_send` | 回复最近一条微信消息;只能发给刚发消息的人,单条上限 4000 字 |
|
|
33
|
-
| `wechat_send_image` |
|
|
34
|
-
| `wechat_send_file` |
|
|
35
|
-
| `wechat_status` |
|
|
38
|
+
| `wechat_send_image` | 把工作区里的一张图发到微信,可带一句说明;工作区外的路径直接拒绝 |
|
|
39
|
+
| `wechat_send_file` | 把工作区里的一个文件发到微信,可带一句说明;同样只限工作区 |
|
|
40
|
+
| `wechat_status` | 连接、账号、最近消息、错误、持有多少条会话令牌,以及工作区/收件目录/允许发送的目录 |
|
|
36
41
|
| `wechat_events` | 按类型和时间查已归档的微信事件 |
|
|
37
42
|
|
|
38
43
|
| 事件 | 作用 |
|
|
39
44
|
|---|---|
|
|
40
|
-
| `wechat-claw.message` |
|
|
45
|
+
| `wechat-claw.message` | 一条微信私聊消息;媒体路径在正文里,Provider 支持的媒体与文档作为附件随事件到达 |
|
|
41
46
|
| `wechat-claw.link` | 绑定成功或会话过期 |
|
|
42
47
|
|
|
43
48
|
## 安装
|
|
@@ -46,9 +51,11 @@
|
|
|
46
51
|
|
|
47
52
|
```bash
|
|
48
53
|
cd %APPDATA%\Coopanion\extensions
|
|
49
|
-
corepack pnpm add "
|
|
54
|
+
corepack pnpm add "<本地包目录,绝对路径,例如 E:\WorkShop\...\cortico-world-wechat-claw>"
|
|
50
55
|
```
|
|
51
56
|
|
|
57
|
+
这条说的是**安装本地包**要填包所在目录的绝对路径,与「发出去的文件路径」无关。
|
|
58
|
+
|
|
52
59
|
装好不等于启用:配置段默认 `enabled: false`,要去 World 总览激活。
|
|
53
60
|
|
|
54
61
|
## 绑定
|
|
@@ -83,11 +90,8 @@ corepack pnpm add "<这个包所在的绝对路径>"
|
|
|
83
90
|
| `imageMaxCount` | `10` | 一次事件投递最多附几张图 |
|
|
84
91
|
| `ffmpegProxyUrl` | 空 | 下载 ffmpeg 用的代理;留空直连(不继承只给模型用的那条代理) |
|
|
85
92
|
| `segmentEnabled` | `true` | 分段发送:一次回复拆成多条微信消息 |
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `segmentDelayMinSec` | `1` | 随机间隔下限,秒 |
|
|
89
|
-
| `segmentDelayMaxSec` | `4` | 随机间隔上限,秒 |
|
|
90
|
-
| `segmentMaxParts` | `12` | 单次最多拆几段,超出并进最后一段 |
|
|
93
|
+
| `segmentPace` | `1` | 聊天节奏,0~10;越大停顿越久,0 不等待 |
|
|
94
|
+
| `segmentMaxParts` | `12` | 语义段数达到此阈值就恢复完整原文,仅按 4000 字上限分条;0 不限 |
|
|
91
95
|
| `segmentMaxTotalSec` | `30` | 累计等待上限,到点不再等但剩下的照常发完 |
|
|
92
96
|
|
|
93
97
|
`bot_token`、同步游标和 `context_token` 都在 `<dataDir>/wechat-claw/`,不进配置、不进日志、不进事件 `meta.raw`。
|
|
@@ -121,7 +125,7 @@ corepack pnpm add "<这个包所在的绝对路径>"
|
|
|
121
125
|
这是本扩展的安全边界:`wechat_send_image` 和 `wechat_send_file` 只接受**工作区、收件目录,
|
|
122
126
|
以及 `sendExtraDirs` 里额外放行的目录**内的路径。
|
|
123
127
|
|
|
124
|
-
|
|
128
|
+
理由很实在:模型能被说服。原来把本机任意路径交给它,等于把 `auth.json`、`context-tokens.json`、
|
|
125
129
|
浏览器 cookie 一起交出去。现在这些文件在工作区外面,边界一挡就拿不到了。
|
|
126
130
|
|
|
127
131
|
- 判断前先做 `realpath`,所以 `..` 和符号链接都绕不出去。
|
|
@@ -140,7 +144,7 @@ corepack pnpm add "<这个包所在的绝对路径>"
|
|
|
140
144
|
|---|---|---|
|
|
141
145
|
| 换行 `\n` | 只按换行 | 删掉 |
|
|
142
146
|
| 没有换行,有句号 `。..` | 只按句号 | 删掉 |
|
|
143
|
-
| 都没有,有 `!?!?…~~` | 只按这些 | **保留** |
|
|
147
|
+
| 都没有,有 `!?!?…~~` 或 ASCII 省略号 | 只按这些 | **保留** |
|
|
144
148
|
| 都没有 | 整条发 | —— |
|
|
145
149
|
|
|
146
150
|
换行和句号同时出现时(「三层:\n第一。\n第二。」),按换行拆完就结束,**不会再多拆一次**;
|
|
@@ -148,14 +152,29 @@ corepack pnpm add "<这个包所在的绝对路径>"
|
|
|
148
152
|
|
|
149
153
|
**为什么删句号**:微信里带句号显得生硬,像在写公文,真人不这么打字。`!?…~` 留着,因为那是语气。
|
|
150
154
|
|
|
151
|
-
|
|
155
|
+
**连续符号完整保留**:`~♡`、`……`、`......`、`?!`、`!!!` 等只在整串末尾切一次,尾随的爱心、emoji 和右引号跟着前一句。符号之间只有空格时也一起保留,例如 `好呀~♡ ......再说` 会发成「好呀~♡ ......」「再说」。
|
|
156
|
+
|
|
157
|
+
**ASCII `.` 有额外保护**:单个点只在后面跟空白或位于结尾时才当句号。所以 `3.14`、`docs.nonebot.dev`、`report.2026.11.md` 不会被切坏。连续两个及以上的点按省略号保留,换行前也不会删掉;`../`、`..\` 这类路径片段不参与省略号分段。
|
|
158
|
+
|
|
159
|
+
语义段数**达到** `segmentMaxParts` 时取消语义分段,保留原文的换行和标点直接发送。原文超过微信单条 4000 字时仍按长度分条,每条均不超限。累计等待超过 `segmentMaxTotalSec` 后不再等待,其余消息照常发完。
|
|
152
160
|
|
|
153
|
-
|
|
161
|
+
「聊天节奏」控制每条消息发送前的停顿,默认 1:
|
|
162
|
+
|
|
163
|
+
| 每个语义段的字数 | 停顿 |
|
|
164
|
+
|---|---|
|
|
165
|
+
| 0~20 | 立即发送 |
|
|
166
|
+
| 21~80 | 0.6 秒 × 聊天节奏 |
|
|
167
|
+
| 81~300 | 1.4 秒 × 聊天节奏 |
|
|
168
|
+
| 超过 300 | 视作未能断句,立即发送,包括按 4000 字拆出的各条 |
|
|
169
|
+
|
|
170
|
+
达到分段阈值或关闭分段时不等待。旧的 `segmentDelayMode`、`segmentDelayFixedSec`、`segmentDelayMinSec`、`segmentDelayMaxSec` 不再使用,已有配置可删除;没有 `segmentPace` 时使用默认值 1。
|
|
154
171
|
|
|
155
172
|
不想要就在配置页关掉「分段发送」。
|
|
156
173
|
|
|
157
174
|
## 视频抽帧
|
|
158
175
|
|
|
176
|
+
当前 Provider 接受视频的实际 MIME 时,直接将视频作为事件附件交给 Core,不抽帧,也不需要 ffmpeg。只有 Provider 不接受视频时才走下面的抽帧流程。微信文件通道收到的 `.mp4`、`.mov` 等视频同样处理。
|
|
177
|
+
|
|
159
178
|
视频抽帧要 ffmpeg,但扩展不把它打进包里,**也绝不悄悄下载**。
|
|
160
179
|
|
|
161
180
|
### 装 ffmpeg
|
|
@@ -253,6 +272,23 @@ AES-128-ECB 加密 → POST 密文到微信 CDN → 从响应头 `x-encrypted-pa
|
|
|
253
272
|
落到 `<工作区>/wechat-inbox/{images,files,videos,voices}/`(原文名 + 时间戳)→ 路径写进事件正文 →
|
|
254
273
|
模型接受该 MIME 时字节作为附件随事件到达。
|
|
255
274
|
|
|
275
|
+
能力查询使用 WorldHost 的 `modelFacts.accepts(mime)`,每条消息读取当前 Provider,不按模型名字猜能力。文件通道里的图片、音乐、视频和 Provider 接受的文档也会附上。图片仍经过体积、尺寸和数量限制;语音使用消息声明的实际编码,不把 MP3 误标成 SILK。不支持的编码或文件、读取失败与图片超限都会取消该附件,并把原因写进正文供桌宠告知用户。
|
|
276
|
+
|
|
277
|
+
附件由 Core 和 Provider 转成上游请求。Provider 的 MIME 声明与请求实现必须同时支持该格式;上游拒绝请求时由 Core 显示具体错误,World 不在后台另发模型请求或重试。仅上游模型增加能力、Provider 仍声明只收图片时,原生音视频不会启用。
|
|
278
|
+
|
|
279
|
+
## 最近消息与收信延迟
|
|
280
|
+
|
|
281
|
+
最近消息徽章按部署时区显示,例如 `11/16 16:37`。时间保存在部署数据目录的 `wechat-claw/status.json`,重启后恢复;旧版没有该文件时从当前绑定用户的归档事件恢复。解绑会清除状态。
|
|
282
|
+
|
|
283
|
+
长轮询的 35 秒是请求等待上限,有消息时由服务端提前返回,并非固定每 35 秒收一次。入站事件使用 `debounce`,本地 Cortico 默认静默等待 2.5 秒合批,连续来消息时最长 15 秒;实际值取决于部署的 `batching` 配置。Core 正在调用模型或工具时还要等待相应的轮次边界。
|
|
284
|
+
|
|
285
|
+
媒体下载、压缩和视频抽帧在入站处理里依次执行,处理结束后才继续轮询,较大媒体会拖慢后续消息。原生视频路径跳过抽帧,减少这部分处理。`wechat_status.lastInboundTiming`、事件 `meta.timing` 与「微信入站消息已处理」日志分别提供:
|
|
286
|
+
|
|
287
|
+
- `receivedAt`:这一批长轮询响应到达本机的时间。
|
|
288
|
+
- `receiveDelayMs`:微信消息时间到响应到达的差值,包括服务端、网络与上一次轮询前的等待;两端时钟偏差也会影响它。
|
|
289
|
+
- `queueMs`:同一批消息在本地开始处理前的排队时间。
|
|
290
|
+
- `processingMs`:保存状态、下载和准备附件所用的时间,不含 Core 合批与模型响应。
|
|
291
|
+
|
|
256
292
|
正文里的图片尺寸读自落盘文件本身(JPEG 的 SOF 段、PNG 的 IHDR 等)。微信消息里的
|
|
257
293
|
`thumb_width` / `thumb_height` 是缩略图尺寸,普遍只有 210 像素宽,和真图差一个数量级,
|
|
258
294
|
所以不用它。
|
|
@@ -261,9 +297,10 @@ AES-128-ECB 加密 → POST 密文到微信 CDN → 从响应头 `x-encrypted-pa
|
|
|
261
297
|
|
|
262
298
|
- 只支持 1v1 私聊;没有群聊、好友列表、朋友圈、转账或红包。
|
|
263
299
|
- 只能回复,不能主动发起会话。回复需要对方最近一条消息的 `context_token`,约 24 小时后失效。
|
|
300
|
+
- **只能发工作区里的文件**。要发外面的素材,先把目录写进 `sendExtraDirs`,或把文件复制进工作区。
|
|
264
301
|
- 单条文本上限 4000 字;更长的回复由扩展自动分条,断行优先落在段落与句号上。
|
|
265
|
-
-
|
|
266
|
-
-
|
|
302
|
+
- 微信语音通常是 SILK 编码,本扩展只落盘不解码;转写用服务端给的 `voice_item.text`,Provider 支持实际编码时附上原音频。没有转写且不支持该编码时,模型无法听到语音内容。
|
|
303
|
+
- Provider 不接受原生视频时,抽帧需要 ffmpeg;没有可用 ffmpeg 时不抽帧,正文会写明。不做本地音频转写。
|
|
267
304
|
- 微信 ClawBot 是腾讯面向 OpenClaw 的官方通道,不是面向任意第三方 agent 的公开 SDK。条款、灰度范围、额度与接口都可能变化;发布前请核对当前条款。这个扩展不是腾讯或 Coopanion 的官方扩展。
|
|
268
305
|
|
|
269
306
|
## 开发
|
package/describe.json
CHANGED
|
@@ -136,8 +136,8 @@
|
|
|
136
136
|
{
|
|
137
137
|
"type": "wechat-claw.message",
|
|
138
138
|
"description": {
|
|
139
|
-
"zh": "
|
|
140
|
-
"en": "A WeChat 1:1 message
|
|
139
|
+
"zh": "一条微信私聊消息;媒体路径写在正文里,Provider 支持的图片、音频、视频和文件作为附件到达。不支持原生视频时尝试抽帧,附件未提供的原因写在正文里。",
|
|
140
|
+
"en": "A WeChat 1:1 message with local media paths. Images, audio, video and files arrive as attachments when accepted by the Provider. Video falls back to frames; unavailable attachments include a reason."
|
|
141
141
|
},
|
|
142
142
|
"trigger": "debounce"
|
|
143
143
|
},
|
|
@@ -154,7 +154,7 @@
|
|
|
154
154
|
"key": "worlds.wechat-claw.envPrompt",
|
|
155
155
|
"title": "微信 ClawBot · 环境提示词",
|
|
156
156
|
"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-
|
|
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"
|
|
158
158
|
}
|
|
159
159
|
]
|
|
160
160
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-wechat-claw",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "在微信里和桌宠一对一聊天:文字、图片、文件双向收发,按 Provider 能力传入原生音视频,不支持视频时抽帧理解。",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "云纪"
|
|
8
8
|
},
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
"locales": {
|
|
47
47
|
"zh": {
|
|
48
48
|
"displayName": "微信 ClawBot",
|
|
49
|
-
"description": "
|
|
49
|
+
"description": "在微信里和桌宠一对一聊天:文字、图片、文件双向收发,按 Provider 能力传入原生音视频,不支持视频时抽帧理解。"
|
|
50
50
|
},
|
|
51
51
|
"en": {
|
|
52
52
|
"displayName": "WeChat ClawBot",
|
|
53
|
-
"description": "Chat with the pet one-to-one in WeChat: text, images and files both ways,
|
|
53
|
+
"description": "Chat with the pet one-to-one in WeChat: text, images and files both ways, native audio and video when accepted by the Provider, and video frames as a fallback."
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
},
|
package/src/ENV_PROMPT.md
CHANGED
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
|
|
26
26
|
- 对方发来图片时,图片会作为附件一起到达,路径也写在正文里。看不到附件时,按路径自行读取。
|
|
27
27
|
正文说「图片未附:体积…/尺寸…/超出数量上限」时,那张图是真的没交给你,**不要猜图里有什么**。
|
|
28
|
-
-
|
|
29
|
-
-
|
|
28
|
+
- 对方发来文件时,路径写在正文里;当前 Provider 支持的图片、音乐、视频和文档会同时作为附件到达,需要本地内容时按路径读。
|
|
29
|
+
- 对方发来视频时,当前 Provider 支持原生视频就直接附上视频;不支持时保留抽帧方式,把几帧**拼成一张图**附上,
|
|
30
30
|
正文写明布局(如「2列×2行,从左上到右下按时间顺序」)、原视频分辨率与每格尺寸。
|
|
31
31
|
按这个顺序理解画面;每格都很小,别去描述看不清的细节。正文说「视频帧未抽取」就是真的没看过。
|
|
32
|
-
-
|
|
32
|
+
- 对方发来语音时,正文保留微信服务端转写;Provider 接受实际音频格式时也会附上语音。微信语音通常是 SILK,原生音频能力不保证支持此编码。没有转写也没有可用附件时,不能猜说了什么。
|
|
33
|
+
- 正文写明附件未附、下载失败或抽帧失败时,告诉对方具体原因;不要声称已听过或看过未提供的内容。模型请求被上游拒绝时,以 Core 报告的错误为准。
|
|
33
34
|
|
|
34
35
|
## 能发什么
|
|
35
36
|
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
|
|
62
63
|
- 你写了换行,就只按换行拆;换行会消失,段内不再按标点拆。紧挨换行的段尾句号也会被去掉,段内的句号留着。
|
|
63
64
|
- 没写换行但有句号,就只按句号拆,句号会消失。
|
|
64
|
-
- 都没写,但有
|
|
65
|
+
- 都没写,但有 `!?…~` 或 `......` 这样的省略号,就按它们拆;连续符号只在整串末尾切一次,`~♡`、`……`、`!!!` 和尾随 emoji 都完整留着,换行前也保留省略号。
|
|
65
66
|
- 都没有,就整条发出去。
|
|
66
67
|
|
|
67
68
|
所以换行是你的节奏工具:**想让对方连看几行、想顿一下再说下一句,就写换行**。反过来,一句话里硬写换行会被拆成好几个气泡,看着像结巴。
|
|
@@ -70,6 +71,8 @@
|
|
|
70
71
|
|
|
71
72
|
分段与间隔都是对方在配置页设的,你看不到也不必猜;照平时的习惯说话就行。
|
|
72
73
|
|
|
74
|
+
语义段数达到配置阈值时,会恢复完整原文直接发送;超过 4000 字仍按微信上限分条。停顿按字数和「聊天节奏」计算,短句与断句失效的特长段直接发送。
|
|
75
|
+
|
|
73
76
|
对方的 `context_token` 会过期。回复被拒并提示没有可用 token 时,等对方再说一句再回,不要反复重试。
|
|
74
77
|
|
|
75
78
|
这里发出的一切都会真的进到对方的微信里。不确定要不要发的时候,先问清楚。
|
package/src/config.ts
CHANGED
|
@@ -60,15 +60,9 @@ export interface WechatClawConfigSection {
|
|
|
60
60
|
* 中文句号删掉(社交软件里带句号显得生硬),情绪标点保留。
|
|
61
61
|
*/
|
|
62
62
|
segmentEnabled: boolean;
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
segmentDelayFixedSec: number;
|
|
67
|
-
/** 随机间隔下限,秒。 */
|
|
68
|
-
segmentDelayMinSec: number;
|
|
69
|
-
/** 随机间隔上限,秒。 */
|
|
70
|
-
segmentDelayMaxSec: number;
|
|
71
|
-
/** 单次回复最多拆几条;超出就把剩下的合并进最后一条。 */
|
|
63
|
+
/** 按字数计算等待时长的倍率;0 表示不等待。 */
|
|
64
|
+
segmentPace: number;
|
|
65
|
+
/** 语义段数达到此值就取消语义分段,仍遵守单条字数上限;0 表示不限。 */
|
|
72
66
|
segmentMaxParts: number;
|
|
73
67
|
/** 一次发送累计等待上限,秒;到点就把剩下的一起发完。 */
|
|
74
68
|
segmentMaxTotalSec: number;
|
|
@@ -93,10 +87,7 @@ export const WECHAT_CLAW_DEFAULTS: WechatClawConfigSection = {
|
|
|
93
87
|
imageMaxCount: 10,
|
|
94
88
|
ffmpegProxyUrl: '',
|
|
95
89
|
segmentEnabled: true,
|
|
96
|
-
|
|
97
|
-
segmentDelayFixedSec: 2,
|
|
98
|
-
segmentDelayMinSec: 1,
|
|
99
|
-
segmentDelayMaxSec: 4,
|
|
90
|
+
segmentPace: 1,
|
|
100
91
|
segmentMaxParts: 12,
|
|
101
92
|
segmentMaxTotalSec: 30,
|
|
102
93
|
};
|
|
@@ -246,43 +237,30 @@ export const WECHAT_CLAW_CONFIG_GROUP: ConfigGroup = {
|
|
|
246
237
|
+ '中文句号会删掉(社交软件里带句号显得生硬),!?…~ 保留语气。修改后需重启。',
|
|
247
238
|
'x-hot': false,
|
|
248
239
|
},
|
|
249
|
-
'worlds.wechat-claw.
|
|
250
|
-
type: 'string',
|
|
251
|
-
title: '间隔方式',
|
|
252
|
-
description: '固定间隔,或在区间内随机取值。只在「分段发送」打开时有效。',
|
|
253
|
-
enum: ['fixed', 'random'],
|
|
254
|
-
'x-hot': true,
|
|
255
|
-
},
|
|
256
|
-
'worlds.wechat-claw.segmentDelayFixedSec': {
|
|
257
|
-
type: 'number',
|
|
258
|
-
title: '固定间隔(秒)',
|
|
259
|
-
description: '每一段发完后等这么久。0 表示连着发完。',
|
|
260
|
-
'x-hot': true,
|
|
261
|
-
},
|
|
262
|
-
'worlds.wechat-claw.segmentDelayMinSec': {
|
|
263
|
-
type: 'number',
|
|
264
|
-
title: '随机下限(秒)',
|
|
265
|
-
description: '随机间隔的下界。和上限写反了会自动换过来。',
|
|
266
|
-
'x-hot': true,
|
|
267
|
-
},
|
|
268
|
-
'worlds.wechat-claw.segmentDelayMaxSec': {
|
|
240
|
+
'worlds.wechat-claw.segmentPace': {
|
|
269
241
|
type: 'number',
|
|
270
|
-
title: '
|
|
271
|
-
description: '
|
|
242
|
+
title: '聊天节奏',
|
|
243
|
+
description: '按每条消息的字数决定发送前的停顿。1 为默认,越大越慢,0 不等待。'
|
|
244
|
+
+ '20 字以内立即发送;21~80 字稍作停顿;81~300 字停顿更久;超过 300 字直接发送。'
|
|
245
|
+
+ '达到分段阈值或关闭分段时直接发送。',
|
|
246
|
+
minimum: 0,
|
|
247
|
+
maximum: 10,
|
|
248
|
+
multipleOf: 0.1,
|
|
272
249
|
'x-hot': true,
|
|
273
250
|
},
|
|
274
251
|
'worlds.wechat-claw.segmentMaxParts': {
|
|
275
252
|
type: 'number',
|
|
276
|
-
title: '
|
|
253
|
+
title: '分段阈值',
|
|
277
254
|
description:
|
|
278
|
-
'
|
|
255
|
+
'语义段数达到这个值就取消分段,直接发送完整原文;超过微信单条 4000 字时仍按字数分条。0 表示不限。',
|
|
256
|
+
minimum: 0,
|
|
279
257
|
'x-hot': true,
|
|
280
258
|
},
|
|
281
259
|
'worlds.wechat-claw.segmentMaxTotalSec': {
|
|
282
260
|
type: 'number',
|
|
283
261
|
title: '总等待上限(秒)',
|
|
284
262
|
description:
|
|
285
|
-
'
|
|
263
|
+
'一次发送里所有停顿加起来的上限。到点不再等待,剩下的消息照常发完。0 表示不限。',
|
|
286
264
|
'x-hot': true,
|
|
287
265
|
},
|
|
288
266
|
},
|
package/src/definition.ts
CHANGED
|
@@ -44,8 +44,8 @@ export const WECHAT_CLAW: WorldDefinition<WechatClawConfigSection> = {
|
|
|
44
44
|
{
|
|
45
45
|
type: EVENT_MESSAGE,
|
|
46
46
|
description: {
|
|
47
|
-
zh: '
|
|
48
|
-
en: 'A WeChat 1:1 message
|
|
47
|
+
zh: '一条微信私聊消息;媒体路径写在正文里,Provider 支持的图片、音频、视频和文件作为附件到达。不支持原生视频时尝试抽帧,附件未提供的原因写在正文里。',
|
|
48
|
+
en: 'A WeChat 1:1 message with local media paths. Images, audio, video and files arrive as attachments when accepted by the Provider. Video falls back to frames; unavailable attachments include a reason.',
|
|
49
49
|
},
|
|
50
50
|
trigger: 'debounce',
|
|
51
51
|
},
|
package/src/driver.ts
CHANGED
|
@@ -46,7 +46,7 @@ export interface DriverOptions {
|
|
|
46
46
|
api: ILinkApi;
|
|
47
47
|
store: CredentialStore;
|
|
48
48
|
log: Logger;
|
|
49
|
-
onMessage: (message: WeixinMessage) => void | Promise<void>;
|
|
49
|
+
onMessage: (message: WeixinMessage, receivedAtMs: number) => void | Promise<void>;
|
|
50
50
|
onStatus: (status: DriverStatus) => void;
|
|
51
51
|
pollTimeoutMs?: number;
|
|
52
52
|
reconnectBaseMs?: number;
|
|
@@ -140,6 +140,7 @@ export class ILinkDriver {
|
|
|
140
140
|
signal: controller.signal,
|
|
141
141
|
});
|
|
142
142
|
if (this.#stopped) return;
|
|
143
|
+
const receivedAtMs = Date.now();
|
|
143
144
|
|
|
144
145
|
const errcode = Number(response?.errcode ?? 0);
|
|
145
146
|
const ret = Number(response?.ret ?? 0);
|
|
@@ -198,7 +199,7 @@ export class ILinkDriver {
|
|
|
198
199
|
for (const message of messages) {
|
|
199
200
|
if (this.#stopped) break;
|
|
200
201
|
try {
|
|
201
|
-
await this.#onMessage(message);
|
|
202
|
+
await this.#onMessage(message, receivedAtMs);
|
|
202
203
|
} catch (err) {
|
|
203
204
|
this.#log.warn('入站消息处理失败', { error: errorText(err) });
|
|
204
205
|
}
|
package/src/protocol/inbound.ts
CHANGED
|
@@ -111,6 +111,19 @@ function voiceEncodingName(encodeType: number | undefined): string {
|
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
function voiceMime(encodeType: number | undefined): string {
|
|
115
|
+
switch (Number(encodeType ?? 6)) {
|
|
116
|
+
case 1: return 'audio/pcm';
|
|
117
|
+
case 2: return 'audio/adpcm';
|
|
118
|
+
case 4: return 'audio/speex';
|
|
119
|
+
case 5: return 'audio/amr';
|
|
120
|
+
case 6: return 'audio/silk';
|
|
121
|
+
case 7: return 'audio/mpeg';
|
|
122
|
+
case 8: return 'audio/ogg';
|
|
123
|
+
default: return 'application/octet-stream';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
114
127
|
export interface RenderOptions {
|
|
115
128
|
media: MediaTransfer | null;
|
|
116
129
|
/** 收件目录,绝对路径。媒体按类型落到它下面的 images/files/videos/voices。 */
|
|
@@ -355,12 +368,13 @@ async function fetchItem(
|
|
|
355
368
|
aesKeyBase64: ref.aes_key,
|
|
356
369
|
maxBytes: opts.maxBytes,
|
|
357
370
|
});
|
|
371
|
+
const mime = voiceMime(voice?.encode_type);
|
|
358
372
|
const path = await saveMedia({
|
|
359
373
|
bytes,
|
|
360
374
|
dir: join(opts.inboxDir, 'voices'),
|
|
361
|
-
name: datedFileName(null, 'wx-voice',
|
|
375
|
+
name: datedFileName(null, 'wx-voice', extensionForInbound(mime, bytes)),
|
|
362
376
|
});
|
|
363
|
-
return { ok: true, path, mime
|
|
377
|
+
return { ok: true, path, mime, bytes: bytes.length };
|
|
364
378
|
} catch (err) {
|
|
365
379
|
// 超限与取回失败要分开:前者是配置决定的,后者是网络或服务端问题。
|
|
366
380
|
const tooLarge = err instanceof Error && /超过上限/.test(err.message);
|
package/src/protocol/mime.ts
CHANGED
|
@@ -22,6 +22,11 @@ const EXT_TO_MIME: Record<string, string> = {
|
|
|
22
22
|
'.ogg': 'audio/ogg',
|
|
23
23
|
'.wav': 'audio/wav',
|
|
24
24
|
'.m4a': 'audio/mp4',
|
|
25
|
+
'.flac': 'audio/flac',
|
|
26
|
+
'.aac': 'audio/aac',
|
|
27
|
+
'.opus': 'audio/opus',
|
|
28
|
+
'.amr': 'audio/amr',
|
|
29
|
+
'.silk': 'audio/silk',
|
|
25
30
|
'.pdf': 'application/pdf',
|
|
26
31
|
'.doc': 'application/msword',
|
|
27
32
|
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
@@ -54,6 +59,14 @@ const MIME_TO_EXT: Record<string, string> = {
|
|
|
54
59
|
'audio/ogg': '.ogg',
|
|
55
60
|
'audio/wav': '.wav',
|
|
56
61
|
'audio/mp4': '.m4a',
|
|
62
|
+
'audio/flac': '.flac',
|
|
63
|
+
'audio/aac': '.aac',
|
|
64
|
+
'audio/opus': '.opus',
|
|
65
|
+
'audio/amr': '.amr',
|
|
66
|
+
'audio/silk': '.silk',
|
|
67
|
+
'audio/pcm': '.pcm',
|
|
68
|
+
'audio/adpcm': '.adpcm',
|
|
69
|
+
'audio/speex': '.spx',
|
|
57
70
|
'application/pdf': '.pdf',
|
|
58
71
|
'application/zip': '.zip',
|
|
59
72
|
'text/plain': '.txt',
|
package/src/segment.ts
CHANGED
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
* ## 删谁不删谁
|
|
19
19
|
*
|
|
20
20
|
* - `\n`、`。`(U+3002)、`.`(U+FF0E) 删掉:中文社交软件里句号显得生硬,换行是版式。
|
|
21
|
-
* - `.`(U+002E) **只在后面跟空白或位于结尾时**才算句号,删掉。否则 `3.14`、
|
|
21
|
+
* - 单个 `.`(U+002E) **只在后面跟空白或位于结尾时**才算句号,删掉。否则 `3.14`、
|
|
22
22
|
* `docs.nonebot.dev`、`report.2026.11.md` 会被切坏。
|
|
23
|
-
* - `!?!?…~~`
|
|
23
|
+
* - `!?!?…~~` 与连续 ASCII 点组成的省略号只拆不删;连续语气符号整体保留。
|
|
24
24
|
*
|
|
25
25
|
* ## 哪条规则同时命中
|
|
26
26
|
*
|
|
@@ -34,16 +34,44 @@ const HARD_STOPS = new Set(['。', '.']);
|
|
|
34
34
|
/** 情绪标点:只拆不删。 */
|
|
35
35
|
const SOFT_STOPS = new Set(['!', '?', '!', '?', '…', '~', '~']);
|
|
36
36
|
|
|
37
|
+
/** 语气符号后的装饰、emoji 组成字符及右括号/右引号,不吸收下一句的左括号或货币符号。 */
|
|
38
|
+
const SOFT_SUFFIX = /[\p{So}\p{M}\p{Emoji_Modifier}\p{Pe}\p{Pf}\u200d\u{e0020}-\u{e007f}]/u;
|
|
39
|
+
|
|
37
40
|
/** 空白字符(用来判断 ASCII 句点后面是不是断句)。 */
|
|
38
41
|
const isSpace = (ch: string | undefined): boolean => ch === undefined || /\s/.test(ch);
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
44
|
* 判断位置 `i` 的 ASCII 句点算不算句号。
|
|
42
45
|
*
|
|
43
|
-
*
|
|
46
|
+
* 单点后面是空白或已到结尾才算;连续点属于省略号,不能删掉最后一位。
|
|
44
47
|
*/
|
|
45
48
|
function isAsciiStop(text: string, i: number): boolean {
|
|
46
|
-
return isSpace(text[i + 1]);
|
|
49
|
+
return text[i - 1] !== '.' && isSpace(text[i + 1]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 语气标点的长度;连续点整体计数,保护 ../ 与 ..\\ 这类路径片段。 */
|
|
53
|
+
function softStopLength(text: string, i: number): number {
|
|
54
|
+
if (SOFT_STOPS.has(text[i]!)) return 1;
|
|
55
|
+
if (text[i] !== '.' || text[i - 1] === '.' || text[i + 1] !== '.') return 0;
|
|
56
|
+
let end = i + 2;
|
|
57
|
+
while (text[end] === '.') end += 1;
|
|
58
|
+
if (/[/\\]/.test(text[i - 1] ?? '') || /[/\\]/.test(text[end] ?? '')) return 0;
|
|
59
|
+
return end - i;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 只在整串符号之后切一次;符号间的空白保留,末尾的空白留给分段时清理。 */
|
|
63
|
+
function softClusterEnd(text: string, start: number, length: number): number {
|
|
64
|
+
let end = start + length;
|
|
65
|
+
while (end < text.length) {
|
|
66
|
+
let next = end;
|
|
67
|
+
while (next < text.length && /[^\S\r\n]/.test(text[next]!)) next += 1;
|
|
68
|
+
if (next === text.length) break;
|
|
69
|
+
const ch = String.fromCodePoint(text.codePointAt(next)!);
|
|
70
|
+
const size = softStopLength(text, next) || (SOFT_SUFFIX.test(ch) ? ch.length : 0);
|
|
71
|
+
if (!size) break;
|
|
72
|
+
end = next + size;
|
|
73
|
+
}
|
|
74
|
+
return end;
|
|
47
75
|
}
|
|
48
76
|
|
|
49
77
|
/** 一个切点:分隔符起点、终点,以及切完要不要把它留在前一段里。 */
|
|
@@ -80,15 +108,11 @@ function findCuts(text: string, kind: 'newline' | 'stop' | 'soft'): CutPoint[] {
|
|
|
80
108
|
continue;
|
|
81
109
|
}
|
|
82
110
|
// kind === 'soft':只拆不删,标点留在前一段。
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
i += 1;
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
cuts.push({ start: i, end: i + 1, keep: true });
|
|
111
|
+
const length = softStopLength(text, i);
|
|
112
|
+
if (length) {
|
|
113
|
+
const end = softClusterEnd(text, i, length);
|
|
114
|
+
cuts.push({ start: i, end, keep: true });
|
|
115
|
+
i = end - 1;
|
|
92
116
|
}
|
|
93
117
|
}
|
|
94
118
|
return cuts;
|
|
@@ -109,13 +133,14 @@ function pickKind(text: string): 'newline' | 'stop' | 'soft' | null {
|
|
|
109
133
|
* 所以每段取完后剥掉尾部连续的 `。`/`.`/`.`。
|
|
110
134
|
*
|
|
111
135
|
* **只剥段尾**:`插件成熟度第一。文档齐全。` 里的第一个句号必须留着,否则句子粘在一起。
|
|
112
|
-
* ASCII
|
|
136
|
+
* ASCII 连续点是省略号,整串保留;只有单个句点会被剥掉。
|
|
113
137
|
*/
|
|
114
138
|
function trimStopTail(piece: string): string {
|
|
115
139
|
let out = piece;
|
|
116
140
|
while (out) {
|
|
117
141
|
const last = out[out.length - 1]!;
|
|
118
142
|
if (!HARD_STOPS.has(last) && last !== '.') break;
|
|
143
|
+
if (last === '.' && out[out.length - 2] === '.') break;
|
|
119
144
|
out = out.slice(0, -1);
|
|
120
145
|
}
|
|
121
146
|
return out.trimEnd();
|
|
@@ -158,51 +183,11 @@ export function splitSegments(text: string): string[] {
|
|
|
158
183
|
return parts;
|
|
159
184
|
}
|
|
160
185
|
|
|
161
|
-
/**
|
|
162
|
-
export
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
*/
|
|
169
|
-
export function segmentDelayOf(cfg: {
|
|
170
|
-
segmentDelayMode?: string;
|
|
171
|
-
segmentDelayFixedSec?: number;
|
|
172
|
-
segmentDelayMinSec?: number;
|
|
173
|
-
segmentDelayMaxSec?: number;
|
|
174
|
-
}): SegmentDelay {
|
|
175
|
-
if (cfg.segmentDelayMode === 'random') {
|
|
176
|
-
return {
|
|
177
|
-
mode: 'random',
|
|
178
|
-
min: numOr0(cfg.segmentDelayMinSec),
|
|
179
|
-
max: numOr0(cfg.segmentDelayMaxSec),
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
return { mode: 'fixed', seconds: numOr0(cfg.segmentDelayFixedSec) };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const numOr0 = (value: unknown): number => {
|
|
186
|
-
const n = Number(value);
|
|
187
|
-
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 算第 `index` 段发完之后该等多久(毫秒)。
|
|
192
|
-
*
|
|
193
|
-
* `index` 从 0 开始;最后一段之后不需要等待,调用方自己判断。
|
|
194
|
-
* 这里只做**纯计算**,不 sleep,方便测试与调用方控制总时长。
|
|
195
|
-
*
|
|
196
|
-
* 随机口径用 `random` 注入,默认 `Math.random`。
|
|
197
|
-
*/
|
|
198
|
-
export function segmentDelayMs(delay: SegmentDelay, index: number, random: () => number = Math.random): number {
|
|
199
|
-
void index;
|
|
200
|
-
if (delay.mode === 'fixed') {
|
|
201
|
-
return Math.max(0, Math.round(delay.seconds * 1000));
|
|
202
|
-
}
|
|
203
|
-
const lo = Math.min(delay.min, delay.max);
|
|
204
|
-
const hi = Math.max(delay.min, delay.max);
|
|
205
|
-
const span = Math.max(0, hi - lo);
|
|
206
|
-
const pick = lo + span * Math.min(Math.max(random(), 0), 1);
|
|
207
|
-
return Math.max(0, Math.round(pick * 1000));
|
|
186
|
+
/** 每条消息发送前的停顿;特长段视作未能断句,直接发送。 */
|
|
187
|
+
export function segmentDelayMs(text: string, pace = 1): number {
|
|
188
|
+
const length = Array.from(text).length;
|
|
189
|
+
const value = Number(pace);
|
|
190
|
+
const scale = Number.isFinite(value) ? Math.min(10, Math.max(0, value)) : 1;
|
|
191
|
+
if (length <= 20 || length > 300) return 0;
|
|
192
|
+
return Math.round((length <= 80 ? 600 : 1400) * scale);
|
|
208
193
|
}
|
package/src/store.ts
CHANGED
|
@@ -112,12 +112,23 @@ export class CredentialStore {
|
|
|
112
112
|
return Object.keys(await this.loadContextTokens()).length;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
async loadLastMessageAt(userId: string): Promise<string> {
|
|
116
|
+
const data = await this.#readJson<{ userId: string; lastMessageAt: string }>('status.json');
|
|
117
|
+
const ms = Date.parse(data?.lastMessageAt ?? '');
|
|
118
|
+
return data?.userId === userId && Number.isFinite(ms) ? new Date(ms).toISOString() : '';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async saveLastMessageAt(userId: string, lastMessageAt: string): Promise<void> {
|
|
122
|
+
await this.#writeJson('status.json', { userId, lastMessageAt });
|
|
123
|
+
}
|
|
124
|
+
|
|
115
125
|
/** 会话过期时清掉全部凭据,让用户重新扫码。 */
|
|
116
126
|
async clearSession(): Promise<void> {
|
|
117
127
|
await Promise.all([
|
|
118
128
|
rm(this.#path('auth.json'), { force: true }),
|
|
119
129
|
rm(this.#path('context-tokens.json'), { force: true }),
|
|
120
130
|
rm(this.#path('sync.json'), { force: true }),
|
|
131
|
+
rm(this.#path('status.json'), { force: true }),
|
|
121
132
|
]);
|
|
122
133
|
}
|
|
123
134
|
}
|
package/src/text.ts
CHANGED
|
@@ -12,7 +12,7 @@ const SENTENCE_END = /[。!?;…!?;\n]/;
|
|
|
12
12
|
* 把 `text` 切成每段不超过 `limit` 的若干段。
|
|
13
13
|
* 不超限时原样返回单元素数组,调用方不必区分两种情况。
|
|
14
14
|
*/
|
|
15
|
-
export function splitText(text: string, limit: number): string[] {
|
|
15
|
+
export function splitText(text: string, limit: number, preserveWhitespace = false): string[] {
|
|
16
16
|
const max = Math.max(1, Math.floor(limit));
|
|
17
17
|
if (text.length <= max) return [text];
|
|
18
18
|
|
|
@@ -35,9 +35,9 @@ export function splitText(text: string, limit: number): string[] {
|
|
|
35
35
|
// 都不合适就硬切,但别切在代理对中间。
|
|
36
36
|
if (cut < max * 0.3) cut = safeCut(rest, max);
|
|
37
37
|
|
|
38
|
-
const piece = rest.slice(0, cut).trimEnd();
|
|
38
|
+
const piece = preserveWhitespace ? rest.slice(0, cut) : rest.slice(0, cut).trimEnd();
|
|
39
39
|
if (piece) parts.push(piece);
|
|
40
|
-
rest = rest.slice(cut).trimStart();
|
|
40
|
+
rest = preserveWhitespace ? rest.slice(cut) : rest.slice(cut).trimStart();
|
|
41
41
|
}
|
|
42
42
|
if (rest) parts.push(rest);
|
|
43
43
|
return parts.length ? parts : [text];
|
package/src/world.ts
CHANGED
|
@@ -50,7 +50,7 @@ import { CredentialStore, type AuthRecord } from './store.ts';
|
|
|
50
50
|
import { pickChannel, splitText } from './text.ts';
|
|
51
51
|
import { fitImage, overCountNote, probeImage, type ImageLimits } from './media/imagefit.ts';
|
|
52
52
|
import { MAX_FRAMES } from './media/layout.ts';
|
|
53
|
-
import { segmentDelayMs,
|
|
53
|
+
import { segmentDelayMs, splitSegments } from './segment.ts';
|
|
54
54
|
import { ensureFfmpeg, installFfmpeg, installedSize, removeFfmpeg, type FfmpegResolution, type InstallProgress } from './media/ffmpeg.ts';
|
|
55
55
|
import { extractFrameStrip } from './media/frames.ts';
|
|
56
56
|
import {
|
|
@@ -239,8 +239,7 @@ export class WechatClawWorld implements World {
|
|
|
239
239
|
#contextCount = 0;
|
|
240
240
|
/** 段间等待的唤醒钩子;stop() 用它打断睡眠。 */
|
|
241
241
|
#sleepWake: (() => void) | null = null;
|
|
242
|
-
|
|
243
|
-
#random: () => number = Math.random;
|
|
242
|
+
#lastInboundTiming: { receivedAt: string; receiveDelayMs: number; queueMs: number; processingMs: number } | null = null;
|
|
244
243
|
#networkProblem = '';
|
|
245
244
|
#ffmpeg: FfmpegResolution | null = null;
|
|
246
245
|
/** ffmpeg 安装进度;null 表示没在装。 */
|
|
@@ -356,45 +355,37 @@ export class WechatClawWorld implements World {
|
|
|
356
355
|
const contextToken = await this.#requireContextToken(to);
|
|
357
356
|
const api = this.#requireApi();
|
|
358
357
|
|
|
359
|
-
const { chunks,
|
|
358
|
+
const { chunks, segmentationCancelled, waits } = this.#planChunks(text);
|
|
360
359
|
|
|
361
360
|
let sleptMs = 0;
|
|
362
361
|
const maxTotalMs = Math.max(0, Number(this.#cfg.segmentMaxTotalSec) || 0) * 1000;
|
|
363
362
|
let budgetCut = false;
|
|
364
363
|
|
|
365
364
|
for (let i = 0; i < chunks.length; i += 1) {
|
|
366
|
-
await api.sendMessage(textMessage({ to, text: chunks[i]!, contextToken }));
|
|
367
|
-
|
|
368
|
-
const isLast = i === chunks.length - 1;
|
|
369
|
-
if (isLast) break;
|
|
370
|
-
|
|
371
365
|
const waitMs = waits[i] ?? 0;
|
|
372
|
-
if (waitMs
|
|
373
|
-
|
|
374
|
-
// 预算不够就**不再等**,但剩下的段必须照常发完——不能因为等不起就不发了。
|
|
375
|
-
if (maxTotalMs > 0 && sleptMs + waitMs > maxTotalMs) {
|
|
366
|
+
if (waitMs > 0 && maxTotalMs > 0 && sleptMs + waitMs > maxTotalMs) {
|
|
376
367
|
budgetCut = true;
|
|
377
|
-
|
|
368
|
+
} else if (waitMs > 0 && !budgetCut) {
|
|
369
|
+
void this.#signalTyping(to, contextToken);
|
|
370
|
+
await this.#sleepMs(waitMs);
|
|
371
|
+
sleptMs += waitMs;
|
|
378
372
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
void this.#signalTyping(to, contextToken);
|
|
382
|
-
await this.#sleepMs(waitMs);
|
|
383
|
-
sleptMs += waitMs;
|
|
373
|
+
if (!this.#started) throw new Error(`微信发送已停止:已发送 ${i}/${chunks.length} 条。`);
|
|
374
|
+
await api.sendMessage(textMessage({ to, text: chunks[i]!, contextToken }));
|
|
384
375
|
}
|
|
385
376
|
|
|
386
377
|
this.#host?.log?.info?.('微信已发送文本', {
|
|
387
378
|
to: shortId(to),
|
|
388
379
|
chars: text.length,
|
|
389
380
|
parts: chunks.length,
|
|
390
|
-
|
|
381
|
+
segmentationCancelled,
|
|
391
382
|
sleptMs,
|
|
392
383
|
budgetCut,
|
|
393
384
|
});
|
|
394
385
|
|
|
395
386
|
const bits: string[] = [];
|
|
396
387
|
if (chunks.length > 1) bits.push(`分 ${chunks.length} 条`);
|
|
397
|
-
if (
|
|
388
|
+
if (segmentationCancelled) bits.push('达到分段阈值,已取消语义分段并发送完整原文');
|
|
398
389
|
if (budgetCut) bits.push('等待到上限,其余连着发完');
|
|
399
390
|
if (sleptMs > 0) bits.push(`共等待 ${(sleptMs / 1000).toFixed(1)} 秒`);
|
|
400
391
|
const suffix = bits.length ? `,${bits.join(',')}` : '';
|
|
@@ -406,46 +397,25 @@ export class WechatClawWorld implements World {
|
|
|
406
397
|
*
|
|
407
398
|
* - 语义分段(`splitSegments`)
|
|
408
399
|
* - 每段仍超 4000 字时交给 `splitText` 再切,保证发得出去
|
|
409
|
-
* -
|
|
410
|
-
* -
|
|
400
|
+
* - 语义段数达到阈值时恢复原文,仅按微信字数上限分条
|
|
401
|
+
* - 给出每段发送前该等多久(毫秒)
|
|
411
402
|
*
|
|
412
403
|
* 分段关闭时退化成原来的行为:只按长度切、不等。
|
|
413
404
|
*/
|
|
414
|
-
#planChunks(text: string): { chunks: string[]; waits: number[];
|
|
405
|
+
#planChunks(text: string): { chunks: string[]; waits: number[]; segmentationCancelled: boolean } {
|
|
415
406
|
const enabled = this.#cfg.segmentEnabled !== false;
|
|
416
|
-
|
|
417
|
-
// 语义段 → 长度段。
|
|
418
407
|
const seedParts = enabled ? splitSegments(text) : [text];
|
|
419
|
-
let chunks: string[] = [];
|
|
420
|
-
for (const part of seedParts.length ? seedParts : [text]) {
|
|
421
|
-
for (const piece of splitText(part, MAX_TEXT_CHARS)) {
|
|
422
|
-
if (piece) chunks.push(piece);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
if (!chunks.length) chunks = [text];
|
|
426
|
-
|
|
427
|
-
// 段数上限:多的并进最后一段。
|
|
428
408
|
const maxParts = Math.floor(Number(this.#cfg.segmentMaxParts) || 0);
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
if (enabled) {
|
|
440
|
-
const delay = segmentDelayOf(this.#cfg);
|
|
441
|
-
for (let i = 0; i < chunks.length - 1; i += 1) {
|
|
442
|
-
waits.push(segmentDelayMs(delay, i, this.#random));
|
|
443
|
-
}
|
|
444
|
-
} else {
|
|
445
|
-
for (let i = 0; i < chunks.length - 1; i += 1) waits.push(0);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return { chunks, waits, merged };
|
|
409
|
+
const segmentationCancelled = enabled && maxParts > 0 && seedParts.length >= maxParts;
|
|
410
|
+
const semantic = enabled && !segmentationCancelled;
|
|
411
|
+
const parts = semantic && seedParts.length ? seedParts : [text];
|
|
412
|
+
const planned = parts.flatMap((part) => {
|
|
413
|
+
const waitMs = semantic ? segmentDelayMs(part, this.#cfg.segmentPace) : 0;
|
|
414
|
+
return splitText(part, MAX_TEXT_CHARS, true).map((text) => ({ text, waitMs }));
|
|
415
|
+
});
|
|
416
|
+
const chunks = planned.map((part) => part.text);
|
|
417
|
+
const waits = planned.map((part) => part.waitMs);
|
|
418
|
+
return { chunks, waits, segmentationCancelled };
|
|
449
419
|
}
|
|
450
420
|
|
|
451
421
|
/** 等一会儿;`stop()` 时会被唤醒,避免关机干等半分钟。 */
|
|
@@ -565,6 +535,7 @@ export class WechatClawWorld implements World {
|
|
|
565
535
|
accountId: state.accountId,
|
|
566
536
|
userId: state.userId,
|
|
567
537
|
lastMessageAt: state.lastMessageAt,
|
|
538
|
+
lastInboundTiming: this.#lastInboundTiming,
|
|
568
539
|
lastError: state.lastError,
|
|
569
540
|
contextTokens: state.contextTokens,
|
|
570
541
|
startedAt: state.startedAt,
|
|
@@ -806,6 +777,7 @@ export class WechatClawWorld implements World {
|
|
|
806
777
|
this.#auth = null;
|
|
807
778
|
this.#contextCount = 0;
|
|
808
779
|
this.#lastMessageAt = '';
|
|
780
|
+
this.#lastInboundTiming = null;
|
|
809
781
|
this.#typingTickets.clear();
|
|
810
782
|
this.#lastError = '';
|
|
811
783
|
|
|
@@ -1079,7 +1051,7 @@ export class WechatClawWorld implements World {
|
|
|
1079
1051
|
{ label: t.badgeAccount, value: state.bound ? (who || '—') : '—', tone: state.bound ? 'on' : 'off' },
|
|
1080
1052
|
{
|
|
1081
1053
|
label: t.badgeLast,
|
|
1082
|
-
value: state.lastMessageLocal ? state.lastMessageLocal.slice(
|
|
1054
|
+
value: state.lastMessageLocal ? state.lastMessageLocal.slice(5, 16).replace('-', '/') : '—',
|
|
1083
1055
|
tone: state.lastMessageAt ? 'on' : 'off',
|
|
1084
1056
|
},
|
|
1085
1057
|
{ label: t.badgeState, value: stateText, tone: state.connected ? 'on' : 'off' },
|
|
@@ -1138,6 +1110,16 @@ export class WechatClawWorld implements World {
|
|
|
1138
1110
|
|
|
1139
1111
|
this.#auth = await this.#store.loadAuth();
|
|
1140
1112
|
this.#contextCount = await this.#store.countContextTokens().catch(() => 0);
|
|
1113
|
+
this.#lastMessageAt = '';
|
|
1114
|
+
if (this.#auth?.userId) {
|
|
1115
|
+
this.#lastMessageAt = await this.#store.loadLastMessageAt(this.#auth.userId);
|
|
1116
|
+
// 旧版未保存状态时,从当前绑定用户的归档消息恢复。
|
|
1117
|
+
if (!this.#lastMessageAt) {
|
|
1118
|
+
const archived = host.store.range({ source: ID, senderKey: this.#auth.userId, limit: 100 });
|
|
1119
|
+
const last = archived.filter((event) => event.type === EVENT_MESSAGE).at(-1);
|
|
1120
|
+
this.#lastMessageAt = parseIso(last?.ts) ?? '';
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1141
1123
|
await this.#rebuildClient();
|
|
1142
1124
|
|
|
1143
1125
|
this.#started = true;
|
|
@@ -1147,7 +1129,7 @@ export class WechatClawWorld implements World {
|
|
|
1147
1129
|
host.log.info('微信尚未绑定,等待扫码');
|
|
1148
1130
|
}
|
|
1149
1131
|
|
|
1150
|
-
// ffmpeg
|
|
1132
|
+
// 查找 ffmpeg 不阻塞收信;这里只查找,不下载。
|
|
1151
1133
|
void this.#resolveFfmpeg();
|
|
1152
1134
|
}
|
|
1153
1135
|
|
|
@@ -1258,7 +1240,7 @@ export class WechatClawWorld implements World {
|
|
|
1258
1240
|
api,
|
|
1259
1241
|
store: this.#store,
|
|
1260
1242
|
log: this.#host?.log ?? nullLoggerShim(),
|
|
1261
|
-
onMessage: (message): Promise<void> => this.#handleInbound(driver, message),
|
|
1243
|
+
onMessage: (message, receivedAtMs): Promise<void> => this.#handleInbound(driver, message, receivedAtMs),
|
|
1262
1244
|
onStatus: (status): void => this.#handleStatus(driver, status),
|
|
1263
1245
|
onStalled: (): void => this.#handleStalled(driver),
|
|
1264
1246
|
});
|
|
@@ -1355,7 +1337,7 @@ export class WechatClawWorld implements World {
|
|
|
1355
1337
|
return true;
|
|
1356
1338
|
}
|
|
1357
1339
|
|
|
1358
|
-
async #handleInbound(driver: ILinkDriver, message: WeixinMessage): Promise<void> {
|
|
1340
|
+
async #handleInbound(driver: ILinkDriver, message: WeixinMessage, receivedAtMs: number): Promise<void> {
|
|
1359
1341
|
if (driver !== this.#driver) return;
|
|
1360
1342
|
// 自己发出去的消息不回到自己这里。
|
|
1361
1343
|
if (Number(message?.message_type) === 2) return;
|
|
@@ -1368,6 +1350,11 @@ export class WechatClawWorld implements World {
|
|
|
1368
1350
|
// 并且「回复」的语义会被引到错误的人身上。
|
|
1369
1351
|
if (!this.#acceptSender(from)) return;
|
|
1370
1352
|
|
|
1353
|
+
const processingAtMs = Date.now();
|
|
1354
|
+
const atMs = messageTimeMs(message);
|
|
1355
|
+
const messageAt = new Date(atMs).toISOString();
|
|
1356
|
+
if (!this.#lastMessageAt || atMs > Date.parse(this.#lastMessageAt)) this.#lastMessageAt = messageAt;
|
|
1357
|
+
|
|
1371
1358
|
const contextToken = String(message?.context_token ?? '').trim();
|
|
1372
1359
|
if (contextToken) {
|
|
1373
1360
|
try {
|
|
@@ -1377,8 +1364,11 @@ export class WechatClawWorld implements World {
|
|
|
1377
1364
|
}
|
|
1378
1365
|
}
|
|
1379
1366
|
|
|
1380
|
-
|
|
1381
|
-
|
|
1367
|
+
try {
|
|
1368
|
+
await this.#store.saveLastMessageAt(from, this.#lastMessageAt);
|
|
1369
|
+
} catch (err) {
|
|
1370
|
+
this.#host?.log?.warn?.('最近消息时间未保存', { error: errorText(err) });
|
|
1371
|
+
}
|
|
1382
1372
|
|
|
1383
1373
|
const maxBytes = Math.max(1, Number(this.#cfg.maxInboundMediaMb) || 20) * 1024 * 1024;
|
|
1384
1374
|
const rendered = await renderInbound(message, {
|
|
@@ -1393,10 +1383,6 @@ export class WechatClawWorld implements World {
|
|
|
1393
1383
|
|
|
1394
1384
|
const blobs: BlobInput[] = [];
|
|
1395
1385
|
const host = this.#host;
|
|
1396
|
-
const acceptsImages = host?.modelFacts?.accepts?.('image/png') ?? false;
|
|
1397
|
-
// 语音只按模型实际支持的 MIME 附;不支持音频就别把 SILK 字节塞进去。
|
|
1398
|
-
const acceptsAudio = host?.modelFacts?.accepts?.('audio/silk') ?? false;
|
|
1399
|
-
const videoNotes: string[] = [];
|
|
1400
1386
|
// 闸门里要写进正文的说明(压缩了哪张、哪张被丢掉、为什么)。
|
|
1401
1387
|
const fitNotes: string[] = [];
|
|
1402
1388
|
// 一次投递累计附了几张图,用来卡上限。
|
|
@@ -1404,8 +1390,9 @@ export class WechatClawWorld implements World {
|
|
|
1404
1390
|
const limits = this.#imageLimits();
|
|
1405
1391
|
|
|
1406
1392
|
for (const item of rendered.media) {
|
|
1407
|
-
if (
|
|
1408
|
-
|
|
1393
|
+
if (driver !== this.#driver || !this.#started) return;
|
|
1394
|
+
// 微信的文件通道也可能装着图片、音乐或视频,按实际 MIME 投递。
|
|
1395
|
+
if (item.mime.startsWith('image/')) {
|
|
1409
1396
|
imageCount += 1;
|
|
1410
1397
|
if (imageCount > limits.maxCount) {
|
|
1411
1398
|
fitNotes.push(`[图片未附:${overCountNote(imageCount - 1, limits)}]`);
|
|
@@ -1414,27 +1401,37 @@ export class WechatClawWorld implements World {
|
|
|
1414
1401
|
await this.#attachImage(blobs, item, limits, fitNotes);
|
|
1415
1402
|
continue;
|
|
1416
1403
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
await this.#addBlob(blobs, item.path,
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
if (item.kind === 'video') {
|
|
1423
|
-
const note = await this.#attachFrames(blobs, item.path, acceptsImages, limits, fitNotes, (n) => {
|
|
1404
|
+
const accepted = this.#acceptsMime(item.mime, fitNotes);
|
|
1405
|
+
if (accepted) {
|
|
1406
|
+
await this.#addBlob(blobs, item.path, item.mime, item.label, fitNotes);
|
|
1407
|
+
} else if (item.mime.startsWith('video/')) {
|
|
1408
|
+
const note = await this.#attachFrames(blobs, item.path, this.#acceptsMime('image/jpeg', fitNotes), limits, fitNotes, (n) => {
|
|
1424
1409
|
imageCount += n;
|
|
1425
1410
|
return imageCount;
|
|
1426
1411
|
});
|
|
1427
|
-
if (note)
|
|
1412
|
+
if (note) fitNotes.push(note);
|
|
1413
|
+
else if (!(Number(this.#cfg.videoFrames) > 0)) fitNotes.push('[视频未附:当前 Provider 不接受此视频格式,且已关闭抽帧]');
|
|
1414
|
+
} else {
|
|
1415
|
+
fitNotes.push(`${item.label} 未附:当前 Provider 不接受 ${item.mime} 输入。`);
|
|
1428
1416
|
}
|
|
1429
1417
|
}
|
|
1430
1418
|
|
|
1419
|
+
if (driver !== this.#driver || !this.#started) return;
|
|
1420
|
+
this.#lastInboundTiming = {
|
|
1421
|
+
receivedAt: new Date(receivedAtMs).toISOString(),
|
|
1422
|
+
receiveDelayMs: Math.max(0, receivedAtMs - atMs),
|
|
1423
|
+
queueMs: Math.max(0, processingAtMs - receivedAtMs),
|
|
1424
|
+
processingMs: Date.now() - processingAtMs,
|
|
1425
|
+
};
|
|
1426
|
+
host?.log?.info?.('微信入站消息已处理', { ...this.#lastInboundTiming, attachments: blobs.length });
|
|
1427
|
+
|
|
1431
1428
|
const prefix = `[微信 ${clockText(atMs, this.#timezone)} ${shortId(from)}]`;
|
|
1432
1429
|
const quoteLine = rendered.quote ? `(回复 ${rendered.quote})` : '';
|
|
1433
1430
|
const base = rendered.text || '[空消息]';
|
|
1434
|
-
const body =
|
|
1431
|
+
const body = fitNotes.length ? `${base}\n${fitNotes.join('\n')}` : base;
|
|
1435
1432
|
await this.#pushEvent({
|
|
1436
1433
|
type: EVENT_MESSAGE,
|
|
1437
|
-
ts:
|
|
1434
|
+
ts: messageAt,
|
|
1438
1435
|
source: ID,
|
|
1439
1436
|
origin: 'external',
|
|
1440
1437
|
text: `${prefix}${quoteLine} ${body}`,
|
|
@@ -1447,6 +1444,7 @@ export class WechatClawWorld implements World {
|
|
|
1447
1444
|
...(rendered.transcript ? { transcript: rendered.transcript } : {}),
|
|
1448
1445
|
...(rendered.quote ? { quote: rendered.quote } : {}),
|
|
1449
1446
|
media: rendered.media.map((item) => ({ kind: item.kind, mime: item.mime, bytes: item.bytes, label: item.label })),
|
|
1447
|
+
timing: this.#lastInboundTiming,
|
|
1450
1448
|
},
|
|
1451
1449
|
}, { deliver: true, trigger: 'debounce' });
|
|
1452
1450
|
|
|
@@ -1454,13 +1452,26 @@ export class WechatClawWorld implements World {
|
|
|
1454
1452
|
void this.#signalTyping(from, contextToken);
|
|
1455
1453
|
}
|
|
1456
1454
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1455
|
+
#acceptsMime(mime: string, notes: string[]): boolean {
|
|
1456
|
+
try {
|
|
1457
|
+
return this.#host?.modelFacts?.accepts(mime) ?? false;
|
|
1458
|
+
} catch (err) {
|
|
1459
|
+
notes.push(`[附件能力查询失败:${errorText(err)}]`);
|
|
1460
|
+
return false;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
/** 读取失败或能力不支持时取消附件,并在事件正文告知原因。 */
|
|
1465
|
+
async #addBlob(blobs: BlobInput[], path: string, mime: string, label: string, notes: string[]): Promise<void> {
|
|
1466
|
+
if (!this.#acceptsMime(mime, notes)) {
|
|
1467
|
+
notes.push(`${label} 未附:当前 Provider 不接受 ${mime} 输入。`);
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1459
1470
|
try {
|
|
1460
1471
|
const bytes = await readFile(path);
|
|
1461
1472
|
blobs.push({ bytes: new Uint8Array(bytes), mime, name: basename(path), fallbackText: label });
|
|
1462
|
-
} catch {
|
|
1463
|
-
|
|
1473
|
+
} catch (err) {
|
|
1474
|
+
notes.push(`${label} 未附:附件读取失败(${errorText(err)})。`);
|
|
1464
1475
|
}
|
|
1465
1476
|
}
|
|
1466
1477
|
|
|
@@ -1492,6 +1503,10 @@ export class WechatClawWorld implements World {
|
|
|
1492
1503
|
limits: ImageLimits,
|
|
1493
1504
|
notes: string[],
|
|
1494
1505
|
): Promise<void> {
|
|
1506
|
+
if (!this.#acceptsMime(item.mime, notes) && !this.#acceptsMime('image/jpeg', notes)) {
|
|
1507
|
+
notes.push(`${item.label} 未附:当前 Provider 不接受 ${item.mime} 输入。`);
|
|
1508
|
+
return;
|
|
1509
|
+
}
|
|
1495
1510
|
const probe = await probeImage(item.path, item.mime);
|
|
1496
1511
|
if (!probe) {
|
|
1497
1512
|
notes.push(`[${item.label} 未附:文件读不出来]`);
|
|
@@ -1511,17 +1526,7 @@ export class WechatClawWorld implements World {
|
|
|
1511
1526
|
}
|
|
1512
1527
|
if (fitted.note) notes.push(`[${item.label} ${fitted.note}]`);
|
|
1513
1528
|
|
|
1514
|
-
|
|
1515
|
-
const bytes = await readFile(fitted.path);
|
|
1516
|
-
blobs.push({
|
|
1517
|
-
bytes: new Uint8Array(bytes),
|
|
1518
|
-
mime: fitted.mime,
|
|
1519
|
-
name: basename(fitted.path),
|
|
1520
|
-
fallbackText: item.label,
|
|
1521
|
-
});
|
|
1522
|
-
} catch {
|
|
1523
|
-
notes.push(`[${item.label} 未附:附件读不出来]`);
|
|
1524
|
-
}
|
|
1529
|
+
await this.#addBlob(blobs, fitted.path, fitted.mime, item.label, notes);
|
|
1525
1530
|
}
|
|
1526
1531
|
|
|
1527
1532
|
/**
|
|
@@ -1576,17 +1581,9 @@ export class WechatClawWorld implements World {
|
|
|
1576
1581
|
}
|
|
1577
1582
|
if (fitted.note) notes.push(`[视频拼图 ${fitted.note}]`);
|
|
1578
1583
|
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
bytes: new Uint8Array(bytes),
|
|
1583
|
-
mime: fitted.mime,
|
|
1584
|
-
name: basename(fitted.path),
|
|
1585
|
-
fallbackText: '视频拼图',
|
|
1586
|
-
});
|
|
1587
|
-
} catch {
|
|
1588
|
-
return '[视频帧未抽取:拼图读不出来]';
|
|
1589
|
-
}
|
|
1584
|
+
const before = blobs.length;
|
|
1585
|
+
await this.#addBlob(blobs, fitted.path, fitted.mime, '视频拼图', notes);
|
|
1586
|
+
if (blobs.length === before) return '';
|
|
1590
1587
|
|
|
1591
1588
|
const sizeNote = strip.videoSize
|
|
1592
1589
|
? `原视频 ${strip.videoSize.width}×${strip.videoSize.height},`
|