dsh-dot-life 0.17.0 → 0.17.4

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/README.md CHANGED
@@ -197,13 +197,13 @@ flowchart LR
197
197
  ```text
198
198
  /phone [accept|restart|screenshot]
199
199
  /interlude [now|advance|seed|preset <code>|canon]
200
- /dot [test|check]
200
+ /dot [test|check|prompts [stop]]
201
201
  /dot memory [now|diary|handoff|recall …]
202
202
  /dot people [approve <id>|drop <id>|forget <名字>]
203
203
  /qq [install|account|start|qr|backend|owner]
204
204
  ```
205
205
 
206
- **改提示词**:她收到的每段话都是 `src/prompts/` 里的纯文本,复制到 `~/dot/prompts/` 改就能覆盖;`npm run prompts` 打开本机页面直接改,见 [docs/prompts.md](docs/prompts.md)。
206
+ **改提示词和人设**:在 dsh 里发 `/dot prompts`,回一个本机网址(默认 http://127.0.0.1:7870/),打开就能改,第一项是人设 `canon.json`;`/dot prompts stop` 关掉。她收到的每段话都是 `src/prompts/` 里的纯文本,页面保存到 `~/dot/prompts/` 覆盖内置版本。开发时在仓库里 `npm run prompts` 也行,见 [docs/prompts.md](docs/prompts.md)。
207
207
 
208
208
  **状态面板**:会话顶部 ◎ 按钮打开右侧栏 dot 标签页——设备状态、交班进度、她此刻在做什么、今天的 token,以及**最近 30 场**(每次被叫醒到结束是一场,展开看她做了什么、说了什么)。
209
209
 
package/docs/DESIGN.md CHANGED
@@ -227,3 +227,9 @@ Kevin:"感觉现在很难给他长任务。他很容易直接结束然后就
227
227
  - **规则**:`dot.rules` 加一条:他连着发几条时等他说完、按最后一条的意思回、不逐条回;回到一半他又说话,先看。
228
228
  - **记忆只记发出去的**:`qq_send` 不再在 `tool/call` 时按参数记,而是等同一 `callId` 的 `tool/result`:出错或被拦(`isError`)→ 不记;否则记 `meta.said` / `meta.imagesSent`(`qq_send` 的 `output.presentationMeta`,落在持久的 `tool/result` 上:打断后没发的、丢掉的英文行、上传失败的图都不在里面)。没有 meta 的旧结果按 `sent N` 取前 N 行。时间用调用的时刻,按时间插回 `lines`(handoff 尾巴、recall 都读它),重放会话日志(`transcriptFromEvents`)走同一个 `qqSentText`。
229
229
 
230
+ ## v0.17.2(2026-11-04):图——她发的记成能发的样子,别人发的她看得见
231
+
232
+ 两件事,0.17.1 的标记过滤(`markerOf`)只是兜底,这里修根上的原因。
233
+
234
+ - **记下的就是能发的**:交接尾巴里她的旧话写着 `… [图片×1]`(记录用的写法),新一段她照着打了一条 `[图片]`,dot 把对话里的回复原样转给他。现在 `qq_send` 的结果(`presentationMeta`)带上真正发出去的图的 ref(`images`,QQ 拒收的不在里面),记忆把它们写成 `[表情包 sticker:<id>]` / `[图 phone:screen]` / `[图 phone:latest]` / `[图 <网址>]`——照着打出来就是那张图(`markerOf` 认得,`sendGate` 也把它当 `images` 审)。只有数量的旧结果取调用里的前 N 张。交接尾巴的说明(`memory.handoff` 的 `tail`)跟着改;`qq.channel.owner` 说清两条路:直接写的回复会原样发给他,图和表情包只能用 `images`。
235
+ - **别人发的图她看得见**:以前进她会话的只有 `[图片 <网址>]` / `[表情包 sticker:<id>]` 这几个字。dsh 的用户消息本来就能带图(dsh-llm 的 `ImageBlock { type: 'image', attachment }`,`attachment` 是附件仓库 `ctx.attachments`——`@deepseek-ai/dsh-attachment` 的 `AttachmentStore`,cordis 服务名 `attachments`——`saveImages` 返回的 `ImageAttachmentRef`;mobile-mcp 截图走的也是这条:dsh-mcp-client 用 `ctx.get('attachments').saveImages` 存 MCP 结果里的图)。dot/qq 在会叫醒她的消息上取图、存进仓库,`m.images: [{ ref, text }]`;dot/core 把他的话和通知的叫醒文本拆成内容数组,图紧跟在它的文字后面(`withImages`)。文字部分拼起来和原来一字不差,所以记忆(只取 text 部分)、recall 索引都不变,也不会记下附件 id。模型不收图时 dsh 的请求组装自己换成文字占位(`projectImagesForTextModel`)。
package/docs/SETTINGS.md CHANGED
@@ -142,6 +142,7 @@ setx DSH_DOT_COMPUTER off
142
142
  | `scoreThreshold` | `7` | `score` 模式的插话门槛(0–10) |
143
143
  | `sendPolicy` | `"reply"` | `reply` / `always` / `off` |
144
144
  | `replyWindowMinutes` | `30` | `reply` 模式下多久内算"对方找过她" |
145
+ | `imageSeeMax` | `4` | 别人发来的图和表情包,每条消息最多给她看几张;0 只给文字 |
145
146
  | `typingMsPerChar` | `60` | 分条发送的打字停顿,0 一次发完 |
146
147
  | `tidy` | `true` | 去掉句号、引号、破折号等书面符号 |
147
148
  | `dropLatinLeaks` | `true` | 不发整条都是英文的气泡(思考漏出来),短英文照发 |
@@ -186,6 +187,7 @@ setx DSH_DOT_COMPUTER off
186
187
  | `enabled` | `true` | `false` 关掉联系人层 |
187
188
  | `home` | `~/dot` | 和另外两处保持一致 |
188
189
  | `phoneCheckEveryMinutes` | `30` | 隔多久拿起手机看一眼,0 关 |
190
+ | `promptsPort` | `7870` | `/dot prompts` 提示词页面的端口(只监听 127.0.0.1) |
189
191
  | `notifyHours` | `""` | 只在这个时段内通知叫醒她;空 = 一直 |
190
192
  | `notifyBatchMs` | `1000` | 多少毫秒内的通知合并成一次叫醒(别人的;你的话见下面四项) |
191
193
  | `ownerQuietMs` | `1500` | 你的 QQ 消息:你停下多少毫秒没新消息才送给她,每来一条重新计时 |
package/docs/memory.md CHANGED
@@ -26,6 +26,8 @@
26
26
 
27
27
  `~/dot/memory/transcript.jsonl` 只记她对话里真实发生的:你说的话、她真正用 `qq_send` 发出去的话(发给谁;按工具结果记,记在调用的时刻:被拦下的一句不记,发到一半被他打断的只记发出去的几条,图片没发成的只记文字)、手机通知、跑腿留下的结论、定时任务。幕间写的虚构剧本不进这里,所以虚构的事不会变成「记忆」。`recall` 工具在这里、碎片和人物卡里找原话。叫醒她的消息开头带时间戳(`[11-04 04:51] …`、`[11-04 04:52 手机通知] …`,见 prompts.md),记进来时去掉——每行本来就有自己的时间,不记两遍。她对话里「现在」那行(`dot:now`)的「距上一条消息」也从这里算:上一条你说的或她说的话。
28
28
 
29
+ 记录里的图(0.17.2):她发出去的图记成它的 ref,写法本身就能再发——`[表情包 sticker:<id>]`、`[图 phone:screen]`、`[图 phone:latest]`、`[图 <网址>]`(`face:<n>`、手机/电脑路径同理),一行里跟在文字后面:`那家店我去过 / 下次带你去 [图 phone:latest]`。0.17.0 的结果只有张数,就取那次调用 `images` 里的前几张;以前的 `[图片×N]` 不再写。别人发给她的图照旧是消息里的文字:`[图片 <网址>]`、`[表情包 sticker:<id>]`。她看得到那些图(见 qq.md),但记录只取消息的文字部分,图本身(附件 id、字节)从不进 transcript、碎片、recall 索引。交接尾巴和 recall 读的就是这些行;`memory.handoff` 的尾巴说明告诉她方括号里的是什么。
30
+
29
31
  ## 整理
30
32
 
31
33
  每天 `dailyAt`(23:30)固定一次:把这一天新的记录整理掉,并让她写日记。
package/docs/prompts.md CHANGED
@@ -14,7 +14,7 @@
14
14
  - `dot.rules.computer` / `dot.rules.phone` 各两行:第一行接在开头那段里,第二行是一条规则。
15
15
  - `interlude.round` 是幕间一轮的整段系统提示词,从上到下就是模型看到的顺序(HDS-Interlude 1.0.1-rc57 的提示词,AGPL-3.0 © MOMOR1A/MomoiCore,译成中文后合并)。覆盖它就替换整段;`{phase}` `{length}` 这类占位符各对应一个 `interlude.round.<名字>` 小文件,空的那行省掉。
16
16
 
17
- **页面**:仓库根目录跑 `npm run prompts`,打开 http://127.0.0.1:7870/(`PORT=xxxx` 换端口)。左边按她看到的顺序列出全部文件,标着「内置」或「已改过」;右边是整个文件,「保存」写到 `<home>/prompts/`,「恢复内置」删掉覆盖文件,「预览」用样例数据跑真实代码(没保存的改动也能预览),「全貌」拼出她对话里插件加的整段系统提示词。`npm run prompts -- --src` 时「保存」直接写进 `src/prompts/`,改的是内置版本。
17
+ **页面**:在 dsh 里发 `/dot prompts`,打开它回的网址(默认 http://127.0.0.1:7870/,只监听本机,端口用 `dot.promptsPort` 换;日志 `~/.dsh/dsh-dot-prompts.log`),`/dot prompts stop` 关掉,dsh 退出时也跟着关。开发时在仓库根目录跑 `npm run prompts` 是同一个页面(`PORT=xxxx` 换端口)。左边第一项是人设(canon.json),其余按她看到的顺序列出全部文件,标着「内置」或「已改过」;右边是整个文件,「保存」写到 `<home>/prompts/`,「恢复内置」删掉覆盖文件,「预览」用样例数据跑真实代码(没保存的改动也能预览),「全貌」拼出她对话里插件加的整段系统提示词。`npm run prompts -- --src` 时「保存」直接写进 `src/prompts/`,改的是内置版本。
18
18
 
19
19
  | 分组 | 是什么 | 文件 |
20
20
  |---|---|---|
package/docs/qq.md CHANGED
@@ -49,7 +49,8 @@ QQ 的电脑登录码**不接受相册导入,只认摄像头**。她手机的
49
49
  | 她回 | `qq_send(to: "owner" \| "user:<号>" \| "group:<号>", text, images)`,**text 每一行一条**(`<sep/>` 也行),条与条之间按字数停一下(`typingMsPerChar`,默认 60 ms/字,最多 2.5 s)。dot 转发她在对话里的回答时同样按行拆。**审批**(`sendPolicy: reply`):发给你永远不问;回最近 30 分钟内给她发过消息的人或群(文字、网图)不问;发给没找过她的人、沉默的群,或者发她手机/电脑上的文件(`phone:*`、`/sdcard/…`、电脑路径)——先弹审批 |
50
50
  | 她发图 | `images` 里每一项直接作为**图片**发出(不是链接):网址(NapCat 下载)、`phone:screen`(手机此刻截图)、`phone:latest`(手机里最新的照片/截图/下载)、`/sdcard/…`(手机上的文件)、电脑上的绝对路径;≤10 MB。**先文字后图**,但所有图在发第一条之前就先准备好(读手机、查网址、取表情包):哪张准备不了,整次调用报错,一条都没发出去。图已准备好、QQ 上传时才失败的,如果前面的文字已经发出去了,不报错:返回 `failed: [{ image, error }]`,她看到的是「文字已发出;图片 X 没发成:…」,这样她不会把文字再发一遍(审计里的 `sent` 也记着 `failed`)。`phone:screen` 截到一张纯白或纯黑的图(亮屏中、切换动画中)时,隔 400 ms 再截一次 |
51
51
  | 图片网址规则 | 只接受公网 `http`/`https`、端口 80/443、不带账号密码;回环、私网、链路本地(含 169.254.169.254)、组播、`localhost`/`*.local`/`*.internal`/单段主机名一律拒绝并报错(NapCat 在 VM 里下载,防止她让它访问 VM 内部服务)。公网域名解析到内网(DNS rebinding)这里拦不住 |
52
- | 别人发图给她 | 叫醒文本里是 `[图片 <网址>]`,她想看就打开 |
52
+ | 别人发图给她 | **她直接看得到**:你(或任何一条会叫醒她的消息)发来的图和表情包,dot/qq 取到字节(图片走 QQ 给的网址,经同一套公网 URL 检查;表情包用 `stickers.js` 里已下载的那份,不再下第二次),存进 dsh 的附件仓库(`ctx.attachments.saveImages`),作为 `{ type: 'image', attachment }` 跟在消息里那张图的文字后面,按消息里的顺序。文字不变:`[表情包 sticker:<id>]`、`[图片 <网址>]`(网址留着,她想存成表情包就 `qq_sticker_keep`)。每条消息最多 `imageSeeMax`(4)张,每张不超过附件仓库的 `imageLimits.maxImageBytes`(没有就 4 MB),只收 PNG / JPEG / GIF / WebP;一次叫醒合起来也按 `imageSeeMax` 和 `maxImagesPerMessage` / `maxMessageImageBytes` 截断。有图的消息要等取图,后面来的消息排在它后面,顺序不乱。没有附件仓库、取不到、存不进:就是以前的纯文字,日志 warn 一次。模型不收图时,dsh 自己把图换成一行文字占位 |
53
+ | 她的话怎么发出去 | 两条路:`qq_send`;或者她在对话里直接写回复,dot 原样转给你(只发文字)。图和表情包只能走 `qq_send` 的 `images`——提示词(`qq.channel.owner`)这么说。写在文字里的标记(一整条气泡就是 `[表情包 sticker:<id>]`、`[图 <ref>]`、`[图片 <网址>]`)会被当成那张图发出去,`[图片]` 这种空标记丢掉(0.17.1 的兜底,仍在);`[图 /sdcard/…]` 这类本地文件和写在 `images` 里一样要审批 |
53
54
  | 表情包 | 别人发的表情包(QQ 标 `sub_type: 1`;QQ 自带的大表情 mface 的 summary 是 `[?]`、`[doge]` 这种短方括号,也算)进文本是 `[表情包 sticker:<id>]`,同时下载到 `~/.dsh/stickers/`,列表在 `~/.dsh/dsh-qq-stickers.json`(最多 300 张,先进先出);她自己也能存:手机 App 里长按保存图片后 `qq_sticker_keep(image: "phone:latest", note: "无语")`(或手机路径、网图)。发:`images: ["sticker:<id>"]`(以 base64 发出,标 sticker),`face:<编号>` 发 QQ 小黄脸;提示词里列最近 12 张;这两种不走审批。**手机上**:存下的每一张都同步到手机相册的 `dot-stickers`(`/sdcard/Pictures/dot-stickers/<id>.<ext>`;存了、新下载了、被挤掉了、手机开机时各同步一次,名字 + 大小一样的不重推),所以手机上任何 App 都能从相册选图发;手机 QQ 的「收藏」表情栏不会自己有它们——她得在 QQ 里长按 → 添加到表情,提示词(`qq.stickers` / `qq.channel.no-stickers` 说在哪,`phone.stickers` 说各 App 怎么加)只教不催。`/qq stickers` 看 `stickers: N saved · phone: N/M synced`,`/qq stickers sync` 立刻推一次。`phone:latest` 只看各相册目录下的文件本身(`ls -td`),dot-stickers 这个子目录不会被当成「刚存的图」。**冷启动**:她的表情包少于 5 张时,幕间知道这件事,可以写一个 `collect` 的 playIntent——去某个 App 逛逛顺手攒几张(见 interlude.md 的「玩」) |
54
55
  | dot 的提醒、幕间的主动联系 | 连上后优先走她的 QQ(发给 `owner`),dsh-im 的官方机器人退为后备 |
55
56
  | 手机上 QQ 的通知 | 连上后自动忽略(否则同一条叫两次);手机 QQ 照常可以看空间、看群、发图 |
@@ -122,6 +123,7 @@ QQ 的电脑登录码**不接受相册导入,只认摄像头**。她手机的
122
123
  | `dropLatinLeaks` | `true` | 跟着 `tidy` 生效:一条 ≥ 30 字、80% 以上是英文字母/标点/空格(网址不算)的气泡,是她的思考漏进了回复(比如 `hmm, actually don't over-explain. Let's just be brief…`),不发,日志里 warn 一行(前 60 字);`ok`、`lol`、`GG` 这种短英文照发。全被拦下又没有图时,调用报错让她重写 |
123
124
  | `sendPolicy` | `reply` | `qq_send` 审批:`reply`(见上表)/ `always`(除了你,发谁都问)/ `off`(不问) |
124
125
  | `replyWindowMinutes` | `30` | `reply` 策略里"最近给她发过消息"的时间窗 |
126
+ | `imageSeeMax` | `4` | 叫醒她的消息里,每条最多让她看几张图 / 表情包;0 = 只给文字 |
125
127
  | `githubProxy` / `DSH_DOT_GITHUB_PROXY` | 无 | 安装脚本下载 GitHub 资源的镜像前缀 |
126
128
  | `autoStart` | `true` | 电脑就绪后自动启动 |
127
129
  | `DSH_DOT_QQ=off` | | 不加载;电脑关了它也不加载 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-dot-life",
3
- "version": "0.17.0",
3
+ "version": "0.17.4",
4
4
  "description": "dot — a character who lives on her own phone, for DeepSeek Harness (dsh): an Android phone the plugin installs and runs, her own QQ, memory and a diary from real events, a life between conversations (幕间, after HDS-Interlude), an optional microVM computer with approval gates. One plugin, six components.",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-only",
@@ -291,4 +291,7 @@ export function serve(port = Number(process.env.PORT) || 7870, { src = process.a
291
291
  return server;
292
292
  }
293
293
 
294
- if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) serve();
294
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
295
+ serve();
296
+ process.on('disconnect', () => process.exit()); // started by /dot prompts: go when dsh goes
297
+ }
package/src/dot/core.js CHANGED
@@ -13,9 +13,12 @@
13
13
  // 3. dsh-budget blocks are announced on IM, and the `daily` block is lifted at local midnight;
14
14
  // 4. VM failures are announced on IM; /dot shows the status.
15
15
  import { PHONE_TOOL_PREFIX, VM_TOOL_PREFIX } from '../tool-prefixes.js';
16
- import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
16
+ import { closeSync, mkdirSync, openSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
17
+ import { spawn } from 'node:child_process';
18
+ import { get as httpGet } from 'node:http';
17
19
  import { homedir } from 'node:os';
18
- import { join, relative, isAbsolute } from 'node:path';
20
+ import { join, relative, isAbsolute, dirname } from 'node:path';
21
+ import { fileURLToPath } from 'node:url';
19
22
  import { createUserMessage } from '@deepseek-ai/dsh-llm';
20
23
  import { mountPanel } from './panel.js';
21
24
  import { sectionOf, setValues } from '../settings.js';
@@ -49,6 +52,27 @@ const firstAt = (items, now) => { const t = Date.parse(items?.[0]?.at ?? ''); re
49
52
  * `whileBusy` (steered into the turn she is in the middle of) adds one line saying so.
50
53
  */
51
54
  export const ownerWords = (items, now = Date.now(), { whileBusy = false } = {}) => prompt('dot.owner-words', { stamp: stamp(firstAt(items, now), now), text: items.map((n) => n.text).join('\n'), busy: whileBusy ? prompt('dot.owner-words.busy') : '' });
55
+ /**
56
+ * A wake text with the pictures in it: the text cut right after each picture's words (`[表情包 sticker:<id>]`,
57
+ * `[图片]`), one `{ type: 'image', attachment }` there, in message order. The text parts joined are the text
58
+ * exactly, so what memory records (text parts only) does not change. A picture whose words are not in
59
+ * the text, or past `max` / `maxBytes` for the whole message, is left out (its words stay).
60
+ * @param images - `[{ ref: ImageAttachmentRef, text }]` from dsh-dot/qq.
61
+ */
62
+ export function withImages(text, images = [], { max = Infinity, maxBytes = Infinity } = {}) {
63
+ const content = [];
64
+ let from = 0, n = 0, bytes = 0;
65
+ for (const im of images ?? []) {
66
+ if (!im?.ref || !im.text || n >= max || bytes + (Number(im.ref.bytes) || 0) > maxBytes) continue;
67
+ const i = text.indexOf(im.text, from);
68
+ if (i < 0) continue;
69
+ const end = i + im.text.length;
70
+ content.push({ type: 'text', text: text.slice(from, end) }, { type: 'image', attachment: im.ref });
71
+ from = end; n++; bytes += Number(im.ref.bytes) || 0;
72
+ }
73
+ if (from < text.length || !content.length) content.push({ type: 'text', text: text.slice(from) });
74
+ return content;
75
+ }
52
76
  /** The model-facing reason a qq_send to him is held back: he said something she has not read yet. */
53
77
  export const ownerInterruptText = () => prompt('dot.owner-interrupt');
54
78
  /** Is this qq_send `to` him? "owner", or his number (bare or user:/private:). */
@@ -86,7 +110,8 @@ export function groupNotifications(items) {
86
110
  const id = n.key || `${n.pkg}|${title}`;
87
111
  const g = groups.get(id) ?? { pkg: n.pkg, title, texts: [] };
88
112
  const text = String(n.text ?? '').trim();
89
- if (text && g.texts.at(-1) !== text) g.texts.push(text);
113
+ // The same words twice in a row are one line, unless they carry a picture (two [图片] are two pictures).
114
+ if (text && (g.texts.at(-1) !== text || n.images?.length)) g.texts.push(text);
90
115
  groups.set(id, g);
91
116
  }
92
117
  return [...groups.values()];
@@ -148,6 +173,7 @@ export const defaults = {
148
173
  errandMaxTokens: 60000, // the errand session is archived and a fresh one started past this
149
174
  // Notifications from these apps are conversations: they wake her conversation, not the errand.
150
175
  conversationApps: ['qq', 'com.tencent.mobileqq', 'com.tencent.mm'],
176
+ promptsPort: 7870, // /dot prompts: the prompt and persona editor page (scripts/prompts.mjs) on 127.0.0.1
151
177
  };
152
178
 
153
179
  /** Source of the note an errand leaves in her conversation (injected; it does not wake her). */
@@ -173,6 +199,55 @@ export function inHours(spec, date = new Date()) {
173
199
  const start = +m[1] * 60 + +m[2], end = +m[3] * 60 + +m[4], now = date.getHours() * 60 + date.getMinutes();
174
200
  return start <= end ? now >= start && now < end : now >= start || now < end;
175
201
  }
202
+ const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
203
+ const answers = (url) => new Promise((r) => {
204
+ const q = httpGet(url, { agent: false, timeout: 1000 }, (res) => { res.resume(); r(res.statusCode === 200); });
205
+ q.on('error', () => r(false)).on('timeout', () => q.destroy());
206
+ });
207
+ /**
208
+ * /dot prompts: scripts/prompts.mjs in a child process (it rewrites process.env.DSH_HOME and the
209
+ * prompt home when imported, which must not happen inside the dsh host). Same <home> as hers.
210
+ */
211
+ export function promptsPage({ home, port, timeoutMs = 20000 }) {
212
+ const url = `http://127.0.0.1:${port}/`;
213
+ const log = join(dshHome(), 'dsh-dot-prompts.log');
214
+ let child = null, starting = null;
215
+ async function start() {
216
+ if (child && child.exitCode === null) return url;
217
+ mkdirSync(dshHome(), { recursive: true });
218
+ try { if (statSync(log).size > 1_000_000) rmSync(log, { force: true }); } catch {}
219
+ const fd = openSync(log, 'a');
220
+ let failed = '';
221
+ try {
222
+ // dsh may be Electron: ELECTRON_RUN_AS_NODE makes its executable a plain Node.
223
+ child = spawn(process.execPath, [join(PACKAGE_ROOT, 'scripts', 'prompts.mjs')], {
224
+ cwd: PACKAGE_ROOT, stdio: ['ignore', fd, fd, 'ipc'], windowsHide: true,
225
+ env: { ...process.env, ELECTRON_RUN_AS_NODE: '1', DSH_DOT_HOME: home, PORT: String(port) },
226
+ });
227
+ } finally { closeSync(fd); }
228
+ const c = child;
229
+ c.unref(); c.channel?.unref(); // the channel only lets the page know when dsh is gone (see prompts.mjs)
230
+ c.on('error', (err) => { failed = err.message; });
231
+ c.on('exit', (code, sig) => { failed ||= `exited (${sig ?? code})`; if (child === c) child = null; });
232
+ for (const until = Date.now() + timeoutMs; Date.now() < until && !failed;) {
233
+ if (await answers(url)) return url;
234
+ await new Promise((r) => setTimeout(r, 200));
235
+ }
236
+ stop();
237
+ throw new Error(`${failed || `no answer within ${timeoutMs / 1000} s`} — log: ${log}`);
238
+ }
239
+ function stop() {
240
+ const c = child; child = null;
241
+ if (!c || c.exitCode !== null) return Promise.resolve(false);
242
+ return new Promise((r) => { c.once('exit', () => r(true)); c.kill(); });
243
+ }
244
+ return {
245
+ url,
246
+ start: () => (starting ??= start().finally(() => { starting = null; })),
247
+ stop,
248
+ };
249
+ }
250
+
176
251
  const today = (d = new Date()) => `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
177
252
 
178
253
  /** Text of a message content array (or string). */
@@ -502,7 +577,7 @@ export function apply(ctx, config = {}) {
502
577
  if (!items.length) return;
503
578
  // The people in these messages, as she remembers them (dsh-dot/memory).
504
579
  const cards = ctx.get('dotMemory')?.cardsFor?.(items) ?? '';
505
- found.agent.followup(createUserMessage({ content: [{ type: 'text', text: wakeText(items, { whileBusy, cards }) }], source: PHONE_NOTIFICATION_SOURCE }));
580
+ found.agent.followup(createUserMessage({ content: contentFor(wakeText(items, { whileBusy, cards }), items), source: PHONE_NOTIFICATION_SOURCE }));
506
581
  bump('woke', 1);
507
582
  audit('notification-woke', { session: id, count: items.length, pkgs: [...new Set(items.map((n) => n.pkg))], whileBusy, sinceSeenMs: Date.now() - Date.parse(items[0].at), sincePostedMs: items[0].postedAt ? Date.now() - items[0].postedAt : undefined });
508
583
  }).catch((err) => { logger.warn(`wake for notification failed: ${err.message}`); audit('notification-dropped', { pkgs, why: err.message }); });
@@ -512,11 +587,39 @@ export function apply(ctx, config = {}) {
512
587
  * steered into it, so she reads them at her next step instead of after the turn (an agent without
513
588
  * `steer` gets the followup).
514
589
  */
590
+ let agentShapeLogged = false;
515
591
  const toOwnerAgent = (agent, id, items) => {
516
- const steer = turns.has(id) && typeof agent.steer === 'function';
517
- const message = createUserMessage({ content: [{ type: 'text', text: ownerWords(items, Date.now(), { whileBusy: steer }) }], source: ownerSource() });
518
- if (steer) agent.steer(message); else agent.followup(message);
519
- audit('owner-message', { session: id, count: items.length, whileBusy: turns.has(id), via: steer ? 'steer' : 'followup' }); bump('owner', items.length);
592
+ const busy = turns.has(id);
593
+ // The host's agent handle may not expose `steer`: `send(message, 'next-step', true)` is the same
594
+ // boundary in dsh-agent's inbox API; failing both, the followup (after the turn, as before).
595
+ const via = busy && typeof agent.steer === 'function' ? 'steer' : busy && typeof agent.send === 'function' ? 'send' : 'followup';
596
+ if (!agentShapeLogged) {
597
+ agentShapeLogged = true;
598
+ const methods = [...new Set([...Object.keys(agent), ...Object.getOwnPropertyNames(Object.getPrototypeOf(agent) ?? {})])].filter((k) => typeof agent[k] === 'function');
599
+ logger.info(`her agent handle offers: ${methods.join(', ')} (busy now: ${busy}, open turns: ${[...turns.keys()].map((k) => k.slice(0, 16)).join(' ') || 'none'})`);
600
+ }
601
+ const message = createUserMessage({ content: contentFor(ownerWords(items, Date.now(), { whileBusy: via !== 'followup' }), items), source: ownerSource() });
602
+ try {
603
+ if (via === 'steer') agent.steer(message); else if (via === 'send') agent.send(message, 'next-step', true); else agent.followup(message);
604
+ } catch (err) { logger.warn(`${via} failed (${err.message}); followup instead`); agent.followup(message); }
605
+ audit('owner-message', { session: id, count: items.length, whileBusy: busy, via }); bump('owner', items.length);
606
+ };
607
+ // The pictures in what wakes her (dsh-dot/qq saved them in the attachment store): image blocks in the
608
+ // message, capped per message by qq's imageSeeMax and the store's imageLimits. Without a store: text only.
609
+ let warnedImages = false;
610
+ const contentFor = (text, items) => {
611
+ const images = items.flatMap((n) => n.images ?? []);
612
+ if (!images.length) return [{ type: 'text', text }];
613
+ const store = ctx.get('attachments');
614
+ if (!store) {
615
+ if (!warnedImages) { warnedImages = true; logger.warn('pictures arrived but no attachment store (ctx.attachments) is mounted: she reads them as text'); }
616
+ return [{ type: 'text', text }];
617
+ }
618
+ const limits = store.imageLimits ?? {};
619
+ const content = withImages(text, images, { max: Math.min(ctx.get('dshQq')?.imageSeeMax ?? 4, limits.maxImagesPerMessage || Infinity), maxBytes: limits.maxMessageImageBytes || Infinity });
620
+ const seen = content.filter((p) => p.type === 'image').length;
621
+ if (seen) audit('images-in-message', { count: seen, of: images.length });
622
+ return content;
520
623
  };
521
624
  let frozen = 0; let frozenQueue = []; // while her conversation is being handed over (dsh-dot/memory)
522
625
  const send = (id, items) => {
@@ -607,6 +710,7 @@ export function apply(ctx, config = {}) {
607
710
  // QQ messages (NapCat in the computer) take the same road as phone notifications: batch, hold while busy, group.
608
711
  ctx.inject(['dshQq'], (c) => c.effect(() => c.dshQq.onMessage?.((m) => wake({
609
712
  key: `qq:${m.kind}:${m.groupId || m.userId}`, pkg: 'qq', title: m.kind === 'group' ? `群 ${m.groupId} · ${m.who}` : `${m.who}(${m.userId})`, text: m.text, at: m.at, postedAt: Date.parse(m.at), fromOwner: Boolean(m.fromOwner),
713
+ ...(m.images?.length ? { images: m.images } : {}),
610
714
  handles: [`qq:${m.userId}`], platform: 'qq', name: m.who, kind: m.kind,
611
715
  })) ?? (() => {}), 'dsh-dot: qq messages'));
612
716
  ctx.inject(['dshQq'], (c) => c.effect(() => c.dshQq.onTyping?.(ownerTypingNow) ?? (() => {}), 'dsh-dot: qq typing'));
@@ -662,13 +766,16 @@ export function apply(ctx, config = {}) {
662
766
  if (s.state === 'needs-license') void im.send('📱 dot: 手机还没装好——需要先接受 Android SDK 许可。在 dsh 里发 /phone accept(默认 16 GB 存储,要别的大小发 /phone accept 32 这样,建好后不能改)。', 'plain');
663
767
  }) ?? (() => {}), 'dsh-dot: phone status'));
664
768
 
769
+ const prompts = promptsPage({ home, port: Number(cfg.promptsPort) || 7870 });
770
+ ctx.effect(() => () => { void prompts.stop(); }, 'dsh-dot: prompts page');
771
+
665
772
  // /dot
666
773
  // The command registry can come up after dot, so register through a child that waits for it.
667
774
  ctx.inject(['commands'], (commandCtx) => {
668
775
  commandCtx.effect(() => commandCtx.commands.register({
669
776
  name: 'dot',
670
- description: 'dot status; "test" sends a test message to your IM; "check" makes her pick up her phone now; "memory" / "people" show what she remembers',
671
- input: { hint: '[test | check | memory [now|diary|handoff|recall [backend|key|model|rerank|mirror|rebuild|test]] | people [approve|drop <id> | forget <name>]]' },
777
+ description: 'dot status; "test" sends a test message to your IM; "check" makes her pick up her phone now; "memory" / "people" show what she remembers; "prompts" opens the prompt and persona editor page',
778
+ input: { hint: '[test | check | prompts [stop] | memory [now|diary|handoff|recall [backend|key|model|rerank|mirror|rebuild|test]] | people [approve|drop <id> | forget <name>]]' },
672
779
  handler: async ({ rawInput }) => {
673
780
  const arg = String(rawInput ?? '').trim();
674
781
  // /dot memory … and /dot people … belong to dsh-dot/memory.
@@ -676,6 +783,10 @@ export function apply(ctx, config = {}) {
676
783
  const mem = ctx.get('dotMemory');
677
784
  return { kind: 'success', text: mem ? await mem.command(arg) : 'dsh-dot/memory is not loaded' };
678
785
  }
786
+ if (arg === 'prompts stop') return { kind: 'success', text: (await prompts.stop()) ? 'prompts page stopped' : 'prompts page was not running' };
787
+ if (arg === 'prompts') {
788
+ try { return { kind: 'success', text: `提示词和人设页面:${await prompts.start()}\n(只在本机打开;/dot prompts stop 关掉)` }; } catch (err) { return { kind: 'success', text: `prompts page failed: ${err.message}` }; }
789
+ }
679
790
  const t = await im.target();
680
791
  let test = '';
681
792
  if (arg === 'test') test = `\ntest message: ${(await im.send('👋 dot 测试消息:IM 通道正常。', 'plain')) ? 'sent' : 'FAILED'}`;
@@ -186,27 +186,29 @@ export function applyExtraction(memory, out, { limits = DEFAULT_LIMITS, now = Da
186
186
  /** His words as he typed them: without the line dot adds when they were steered into a running turn. */
187
187
  export const ownerSaid = (text) => { const busy = `\n${prompt('dot.owner-words.busy')}`; return text.endsWith(busy) ? text.slice(0, -busy.length) : text; };
188
188
 
189
+ /** An image she sent, as her transcript writes it — the ref itself, so the line can be sent again as it reads: `[表情包 sticker:<id>]`, else `[图 <ref>]` (phone:screen, phone:latest, a URL, face:<n>, a path). */
190
+ export const sentImage = (ref) => { const r = String(ref ?? '').trim(); return /^sticker:/i.test(r) ? `[表情包 ${r}]` : `[图 ${r}]`; };
189
191
  /**
190
192
  * What one qq_send really put on QQ, as a transcript line's text ('' = nothing went out), from its
191
193
  * `tool/result`: denied or failed → nothing; otherwise the bubbles qq_send reports as sent
192
- * (`meta.said`, `meta.imagesSent` — an interrupted send, a dropped leak line and an image QQ refused
193
- * are not in them). A result without that meta (older plugin version, a nested call): the first
194
- * `sent N` lines of the call's text, then images.
194
+ * (`meta.said`, `meta.images` — an interrupted send, a dropped leak line and an image QQ refused
195
+ * are not in them), the images as their refs (sentImage). A result without `meta.images` (0.17.0's
196
+ * `imagesSent` count, an older version, a nested call): the first that many of the call's images; a
197
+ * result with no meta at all: the first `sent N` lines of the call's text, then images.
195
198
  */
196
199
  export function qqSentText(args, result) {
197
200
  if (!result || result.isError || result.message?.isError) return '';
198
201
  const meta = result.meta && typeof result.meta === 'object' ? result.meta : null;
202
+ const asked = [].concat(args?.images ?? []).filter(Boolean).map(String);
199
203
  let said, images;
200
- if (Array.isArray(meta?.said)) { said = meta.said.map(String); images = Number(meta.imagesSent) || 0; }
204
+ if (Array.isArray(meta?.said)) { said = meta.said.map(String); images = Array.isArray(meta.images) ? meta.images.map(String) : asked.slice(0, Number(meta.imagesSent) || 0); }
201
205
  else {
202
206
  const parts = String(args?.text ?? '').split(/<sep\s*\/>|\n/i).map((x) => x.trim()).filter(Boolean);
203
- const refs = [].concat(args?.images ?? []).filter(Boolean).length;
204
207
  const m = /^sent (\d+) message/.exec(contentText(result.message?.content ?? result.content));
205
- const n = m ? Number(m[1]) : parts.length + refs;
206
- said = parts.slice(0, n); images = Math.min(refs, Math.max(0, n - parts.length));
208
+ const n = m ? Number(m[1]) : parts.length + asked.length;
209
+ said = parts.slice(0, n); images = asked.slice(0, Math.max(0, n - parts.length));
207
210
  }
208
- const text = said.join(' / ');
209
- return `${text}${images ? `${text ? ' ' : ''}[图片×${images}]` : ''}`;
211
+ return [said.join(' / '), ...images.map(sentImage)].filter(Boolean).join(' ');
210
212
  }
211
213
  const parseArgs = (s) => { try { return JSON.parse(s ?? '{}') ?? {}; } catch { return {}; } };
212
214
  /** Lines kept in time order: a qq_send is recorded at its result, with the time of its call. */
@@ -3,4 +3,4 @@ now=· 你此刻:{now}
3
3
  inFlight=· 手上还没做完的:
4
4
  promises=· 你答应过的:
5
5
  openQuestions=· 还没问清、或他还没回的:
6
- tail=· 上一段最后几句(原文):
6
+ tail=· 上一段最后几句(原文;[表情包 sticker:…]、[图 phone:latest] 这种是当时发的表情包和图,要发就把方括号里的 sticker:… / phone:latest 放进 qq_send 的 images;[图片 …] 是别人发来的图):
@@ -1 +1 @@
1
- QQ {owner} 是你的人:他私聊你的话会原样出现在对话里,就是他本人对你说的,回他用 qq_send(to: "owner")。
1
+ QQ {owner} 是你的人:他私聊你的话会原样出现在对话里,就是他本人对你说的,回他用 qq_send(to: "owner")。你直接写的回复也会原样发给他(dot 代发);带图、带表情包必须用 qq_send 的 images,写在文字里发不出去。
@@ -1,2 +1,2 @@
1
- 你的 QQ({qq})在你电脑上也登着。{owner}其他人发来的消息会以「[QQ] 谁:内容」叫醒你,回复用 qq_send(立刻送达,to 用 user:<QQ号> 或 owner)。{score}要给人看图就用 qq_send 的 images 直接发图片——网上的图给网址,手机屏幕给 "phone:screen",手机里刚存的给 "phone:latest"——别发链接让人自己点。别人发来的图片会带一个网址,想看就打开。手机上的 QQ 照常可以看空间、看群。
1
+ 你的 QQ({qq})在你电脑上也登着。{owner}其他人发来的消息会以「[QQ] 谁:内容」叫醒你,回复用 qq_send(立刻送达,to 用 user:<QQ号> 或 owner)。{score}要给人看图就用 qq_send 的 images 直接发图片——网上的图给网址,手机屏幕给 "phone:screen",手机里刚存的给 "phone:latest"——别发链接让人自己点。别人发的图和表情包你直接就能看到(消息里带着图);想保存就 qq_sticker_keep。手机上的 QQ 照常可以看空间、看群。
2
2
  聊天的样子:一句话一行,每行就是一条消息,短的几条比一大段好;想用表情就在 images 里发:{stickers};{faces}。可以只发一个表情不说话。
package/src/qq/qq.js CHANGED
@@ -60,6 +60,10 @@ export const defaults = {
60
60
  // could carry anything). 'always' asks for everything but the owner; 'off' never asks.
61
61
  sendPolicy: 'reply',
62
62
  replyWindowMinutes: 30,
63
+ // Pictures and stickers people send her (the owner and anyone whose message wakes her) go into the
64
+ // message as images she sees: at most this many per message, each within the attachment store's
65
+ // imageLimits (else 4 MB). 0 = text only ("[图片 <url>]", "[表情包 sticker:<id>]").
66
+ imageSeeMax: 4,
63
67
  reconnectMs: 5000,
64
68
  requestTimeoutMs: 15000,
65
69
  };
@@ -229,7 +233,8 @@ export function sendGate({ to, text = '', images = [] }, { owner, policy = 'repl
229
233
  const target = parseTarget(to, owner);
230
234
  if (!target) return null; // qq.send rejects it with a clear error
231
235
  if (owner && target.user_id === Number(owner)) return null;
232
- const local = [].concat(images ?? []).filter((r) => r && !/^(https?:\/\/|sticker:|face:)/i.test(String(r)));
236
+ // `[图 /sdcard/…]` typed as a bubble is sent as that image (qq.send), so it is judged as one here.
237
+ const local = [...[].concat(images ?? []), ...typedImages(text)].filter((r) => r && !/^(https?:\/\/|sticker:|face:)/i.test(String(r)));
233
238
  const who = target.group_id ? `group:${target.group_id}` : `user:${target.user_id}`;
234
239
  if (local.length) return `dsh-dot/qq: sending ${local.length} file(s) from her devices to ${who} (${local.map((r) => String(r).slice(0, 40)).join(', ')})`;
235
240
  if (policy === 'always') return `dsh-dot/qq: message to ${who}: ${String(text).slice(0, 80)}`;
@@ -253,6 +258,42 @@ export function tidyVoice(text) {
253
258
  }).join('\n').replace(/\n{3,}/g, '\n\n').trim();
254
259
  }
255
260
 
261
+ /**
262
+ * A whole bubble that is transcript notation: `[图片]`, `[图片×3]`, `[表情包 sticker:<id>]`, `[表情 …]`,
263
+ * or what her transcript records for an image she sent — `[图 <ref>]` (phone:screen, phone:latest, a
264
+ * URL, face:<n>, a path) — and `[图片 <url>]`, a picture someone sent her. `image` is the ref it stands for ('' = a bare picture marker, nothing to send).
265
+ */
266
+ export function markerOf(line) {
267
+ const s = String(line ?? '').trim();
268
+ const sent = /^\[图片?\s+([^\s\]]+)\]$/.exec(s);
269
+ if (sent) return imageSource(sent[1]) ? { image: sent[1] } : null;
270
+ const m = /^\[(图片|表情包?|image)(?:\s*[×x]\s*\d+)?(?:\s+sticker:([a-f0-9]{6,12}))?\]$/i.exec(s);
271
+ return m ? { image: m[2] ? `sticker:${m[2]}` : '' } : null;
272
+ }
273
+ /** Image refs typed as whole-bubble markers in a qq_send text (see markerOf), in order. */
274
+ export const typedImages = (text) => String(text ?? '').split(/<sep\s*\/>|\n/i).map(markerOf).filter((m) => m?.image).map((m) => m.image);
275
+ /** The ref of an image that went out, as she wrote it, normalised: `sticker:<id>`, `face:<n>`, a URL, phone:…, a path. */
276
+ export function sentRef(ref) {
277
+ const src = imageSource(ref);
278
+ if (!src) return String(ref ?? '').trim();
279
+ if (src.kind === 'sticker' || src.kind === 'face') return `${src.kind}:${src.ref}`;
280
+ if (src.kind === 'phone-screen') return 'phone:screen';
281
+ if (src.kind === 'phone-latest') return 'phone:latest';
282
+ return src.kind === 'phone' && !/^\/(sdcard|storage)\//.test(src.ref) ? `phone:${src.ref}` : src.ref;
283
+ }
284
+ /** An image ref as her transcript writes it — itself sendable (markerOf reads it back): `[表情包 sticker:<id>]`, else `[图 <ref>]`. */
285
+ export const imageMarker = (ref) => (/^sticker:/.test(String(ref)) ? `[表情包 ${ref}]` : `[图 ${ref}]`);
286
+
287
+ /** The media type of image bytes, from their first bytes ('' = not PNG, JPEG, GIF or WebP). */
288
+ export function imageType(buf) {
289
+ const b = Buffer.isBuffer(buf) ? buf : Buffer.from(buf ?? []);
290
+ if (b.length >= 8 && b[0] === 0x89 && b.toString('latin1', 1, 4) === 'PNG') return 'image/png';
291
+ if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return 'image/jpeg';
292
+ if (b.length >= 6 && b.toString('latin1', 0, 4) === 'GIF8') return 'image/gif';
293
+ if (b.length >= 12 && b.toString('latin1', 0, 4) === 'RIFF' && b.toString('latin1', 8, 12) === 'WEBP') return 'image/webp';
294
+ return '';
295
+ }
296
+
256
297
  /**
257
298
  * A bubble that is English prose, not chat: 30+ characters (URLs aside) of which ≥ 80% are ASCII
258
299
  * letters, punctuation or spaces. That is the model thinking aloud ("hmm, actually don't over-explain…").
@@ -881,7 +922,16 @@ export function apply(ctx, config = {}, deps = {}) {
881
922
  if (String(ev.user_id) === selfId) return;
882
923
  const isGroup = ev.message_type === 'group';
883
924
  const who = ev.sender?.card || ev.sender?.nickname || String(ev.user_id);
884
- const text = segmentsToText(Array.isArray(ev.message) ? ev.message.map((s) => (s?.type === 'image' && isSticker(s.data) ? keepSticker(s.data, who) : s)) : ev.message, selfId);
925
+ // Every picture in it, in order, with the words that stand for it in `text` — what she may get to see (seeImages).
926
+ const seen = [];
927
+ const segs = Array.isArray(ev.message) ? ev.message.map((s) => {
928
+ if (s?.type !== 'image') return s;
929
+ if (!isSticker(s.data)) { if (s.data?.url) seen.push({ url: s.data.url, text: segmentsToText([s]) }); return s; }
930
+ const k = keepSticker(s.data, who);
931
+ if (k.id) seen.push({ sticker: k.id, text: k.seg.data.text, fetching: k.fetching });
932
+ return k.seg;
933
+ }) : ev.message;
934
+ const text = segmentsToText(segs, selfId);
885
935
  if (!text) return;
886
936
  const wanted = isGroup ? groupWanted(ev, cfg.groupPolicy, selfId) : true;
887
937
  if (isGroup) {
@@ -895,24 +945,92 @@ export function apply(ctx, config = {}, deps = {}) {
895
945
  if (fromOwner) lastOwnerAt = Date.now();
896
946
  recent.set(isGroup ? `group:${msg.groupId}` : `user:${msg.userId}`, Date.now());
897
947
  if (recent.size > 500) recent.delete(recent.keys().next().value);
898
- if (wanted === 'score') { void scoreAndDeliver(msg); return; }
899
- deliver(msg);
948
+ if (wanted === 'score') { void scoreAndDeliver(msg, seen); return; }
949
+ deliver(msg, seen);
900
950
  }
901
951
  /** A sticker someone sent: into her collection (downloaded once, in the background) and named in the text she reads. */
902
952
  function keepSticker(d, who) {
903
953
  const got = urlRefusal(d.url) ? null : stickers.note({ file: d.file, url: d.url, from: who });
904
- if (!got) return { type: 'text', data: { text: '[表情包]' } };
905
- if (got.isNew) stickers.fetch(got.id).then((ok) => { if (ok) audit({ kind: 'sticker-kept', id: got.id, from: who }); }, (err) => logger.debug(`sticker ${got.id}: ${err.message}`));
906
- return { type: 'text', data: { text: `[表情包 sticker:${got.id}]` } };
954
+ if (!got) return { seg: { type: 'text', data: { text: '[表情包]' } } };
955
+ // `fetching`: the one download, which seeing it (seeImages) waits for instead of fetching again.
956
+ const fetching = got.isNew ? stickers.fetch(got.id).then((ok) => { if (ok) audit({ kind: 'sticker-kept', id: got.id, from: who }); return ok; }, (err) => { logger.debug(`sticker ${got.id}: ${err.message}`); return false; }) : undefined;
957
+ return { seg: { type: 'text', data: { text: `[表情包 sticker:${got.id}]` } }, id: got.id, fetching };
958
+ }
959
+ // ---- what she sees: pictures and stickers in a message that wakes her, as images in that message ----
960
+ const warned = new Set();
961
+ const warnOnce = (key, m) => { if (warned.has(key)) return; warned.add(key); logger.warn(m); };
962
+ const SEE_MAX_BYTES = 4 * 1024 * 1024;
963
+ const SEE_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'image/gif'];
964
+ /** The bytes of one picture in a message: a sticker from her collection (downloaded once), else its QQ URL. */
965
+ async function seenBytes(item, cap) {
966
+ let buf;
967
+ if (item.sticker) {
968
+ if (!(await (item.fetching ?? stickers.fetch(item.sticker).catch(() => false)))) throw new Error(`sticker ${item.sticker} not downloaded`);
969
+ buf = stickers.read(item.sticker);
970
+ if (!buf) throw new Error(`sticker ${item.sticker} not in her collection`);
971
+ } else {
972
+ const why = urlRefusal(item.url);
973
+ if (why) throw new Error(`image URL refused: ${why}`);
974
+ const res = await (deps.fetch ?? globalThis.fetch)(publicHttpUrl(item.url), { signal: AbortSignal.timeout(15_000) });
975
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
976
+ if (Number(res.headers?.get?.('content-length') ?? 0) > cap) throw new Error(`image over ${cap} bytes`);
977
+ buf = Buffer.from(await res.arrayBuffer());
978
+ }
979
+ if (!buf.length || buf.length > cap) throw new Error(`image is ${buf.length} bytes (1 – ${cap})`);
980
+ return buf;
981
+ }
982
+ /**
983
+ * The message with `images: [{ ref, text }]`: each picture she can see, saved in dsh's attachment store
984
+ * (`ctx.attachments`), with the words that stand for it in the message text (`[表情包 sticker:<id>]`,
985
+ * `[图片 <url>]` — the URL stays, for qq_sticker_keep). The text itself is unchanged. At most
986
+ * imageSeeMax, each within the store's imageLimits. No store, nothing fetchable, or every save
987
+ * refused: the message as it was (text only).
988
+ */
989
+ async function seeImages(msg, seen) {
990
+ if (!seen.length || !(cfg.imageSeeMax > 0)) return msg;
991
+ const store = ctx.get?.('attachments');
992
+ if (typeof store?.saveImages !== 'function') { warnOnce('no-store', 'no attachment store (ctx.attachments): pictures and stickers reach her as text only'); return msg; }
993
+ const limits = store.imageLimits ?? {};
994
+ const max = Math.min(cfg.imageSeeMax, limits.maxImagesPerMessage || Infinity);
995
+ const cap = limits.maxImageBytes || SEE_MAX_BYTES;
996
+ const types = limits.mediaTypes ?? SEE_TYPES;
997
+ const got = [];
998
+ for (const item of seen) {
999
+ if (got.length >= max) break;
1000
+ try {
1001
+ const data = await seenBytes(item, cap);
1002
+ const mediaType = imageType(data);
1003
+ if (!types.includes(mediaType)) throw new Error(`not an image she can see (${mediaType || 'unknown type'})`);
1004
+ got.push({ item, input: { data: new Uint8Array(data), mediaType, name: item.sticker ? `sticker-${item.sticker}` : 'qq-image' } });
1005
+ } catch (err) { logger.debug(`picture not seen: ${err.message}`); }
1006
+ }
1007
+ if (!got.length) return msg;
1008
+ const refs = new Map();
1009
+ try { (await store.saveImages(got.map((g) => g.input))).forEach((ref, i) => refs.set(got[i].item, ref)); } catch (err) {
1010
+ // The batch is all or nothing: one bad picture must not hide the others.
1011
+ for (const g of got) { try { refs.set(g.item, (await store.saveImages([g.input]))[0]); } catch (e) { logger.warn(`picture not saved for her to see: ${e.message}`); } }
1012
+ if (!refs.size) warnOnce(`save:${err.message}`, `attachment store refused the pictures (${err.message}): text only`);
1013
+ }
1014
+ if (!refs.size) return msg;
1015
+ const images = seen.filter((item) => refs.has(item)).map((item) => ({ ref: refs.get(item), text: item.text }));
1016
+ audit({ kind: 'images-seen', from: msg.userId, count: images.length });
1017
+ return { ...msg, images };
1018
+ }
1019
+ // Messages reach her in the order they came: one with pictures waits for them, and those after it wait too.
1020
+ let delivering = Promise.resolve(), queued = 0;
1021
+ function deliver(msg, seen = []) {
1022
+ if (!seen.length && !queued) { emit(msg); return; }
1023
+ queued++;
1024
+ delivering = delivering.then(() => seeImages(msg, seen)).catch((err) => { logger.warn(`pictures not seen: ${err.message}`); return msg; }).then((m) => emit(m)).finally(() => { queued--; });
907
1025
  }
908
- function deliver(msg) {
909
- audit({ kind: 'message', ...msg, text: msg.text.slice(0, 200) });
1026
+ function emit(msg) {
1027
+ audit({ kind: 'message', ...msg, text: msg.text.slice(0, 200), ...(msg.images ? { images: msg.images.length } : {}) });
910
1028
  for (const fn of listeners) { try { fn(msg); } catch (err) { logger.warn(`qq listener: ${err.message}`); } }
911
1029
  }
912
1030
  /** groupPolicy 'score': one short model call decides whether this group message wakes her. */
913
1031
  const groupLines = new Map();
914
1032
  const scoreBudget = new Map(); // group → [timestamps of calls in the last minute]
915
- async function scoreAndDeliver(msg) {
1033
+ async function scoreAndDeliver(msg, seen = []) {
916
1034
  const llm = ctx.get?.('llm');
917
1035
  const sel = (() => { try { return ctx.get?.('agentDefaultModel')?.currentSelection?.(); } catch { return null; } })();
918
1036
  if (!llm?.stream) return;
@@ -933,7 +1051,7 @@ export function apply(ctx, config = {}, deps = {}) {
933
1051
  } catch (err) { logger.debug(`group score failed: ${err.message}`); return; }
934
1052
  const score = parseScore(text);
935
1053
  audit({ kind: 'group-score', group: msg.groupId, from: msg.userId, score, raw: text.slice(0, 40), text: msg.text.slice(0, 80) });
936
- if (score >= cfg.scoreThreshold) deliver({ ...msg, score });
1054
+ if (score >= cfg.scoreThreshold) deliver({ ...msg, score }, seen);
937
1055
  }
938
1056
  function call(action, params) {
939
1057
  return new Promise((resolve, reject) => {
@@ -997,6 +1115,8 @@ export function apply(ctx, config = {}, deps = {}) {
997
1115
  return got;
998
1116
  };
999
1117
  qq.stickerCount = () => stickers.count();
1118
+ /** At most this many pictures per message she is woken with (dot caps a whole wake the same). */
1119
+ qq.imageSeeMax = cfg.imageSeeMax;
1000
1120
  qq.stickerSync = stickerSync;
1001
1121
  /**
1002
1122
  * Send to "user:123" | "group:456" | "owner" | a number: text (one bubble per line or <sep/>, paced) and/or images, in that order.
@@ -1004,7 +1124,7 @@ export function apply(ctx, config = {}, deps = {}) {
1004
1124
  * QQ then refuses an image after something was already delivered, the call does not throw (she would
1005
1125
  * resend the text): it returns `failed: [{ image, error }]` next to what was sent.
1006
1126
  * To him: if he says something while the bubbles are going out, the rest is not sent — `{ sent,
1007
- * interrupted: true, unsent: [lines…] }` (images not yet sent are listed as `[图片 <ref>]`).
1127
+ * interrupted: true, unsent: [lines…] }` (images not yet sent are listed as `[图 <ref>]` / `[表情包 sticker:<id>]`).
1008
1128
  */
1009
1129
  qq.send = async (to, text = '', images = []) => {
1010
1130
  const target = parseTarget(to, qq.owner);
@@ -1012,12 +1132,17 @@ export function apply(ctx, config = {}, deps = {}) {
1012
1132
  const action = target.group_id ? 'send_group_msg' : 'send_private_msg';
1013
1133
  let parts = String(text ?? '').split(/<sep\s*\/>|\n/i).map((t) => (cfg.tidy ? tidyVoice(t) : t.trim())).filter(Boolean);
1014
1134
  let dropped = 0;
1135
+ // Transcript notation typed as words ("[图片]", "[图片×2]", "[表情包 sticker:ab12]", "[图 phone:latest]"):
1136
+ // not chat. A marker that names an image becomes that image; a bare picture marker is dropped.
1137
+ const markers = parts.map(markerOf).filter(Boolean);
1138
+ if (markers.length) { parts = parts.filter((p) => !markerOf(p)); images = [...[].concat(images ?? []), ...markers.map((m) => m.image).filter(Boolean)]; }
1015
1139
  if (cfg.tidy && cfg.dropLatinLeaks !== false) {
1016
1140
  parts = parts.filter((p) => { if (!latinLeak(p)) return true; dropped++; logger.warn(`dropped an English-prose bubble (reasoning leak?): ${p.slice(0, 60)}`); return false; });
1017
1141
  }
1018
1142
  const refs = [].concat(images ?? []).filter(Boolean);
1019
1143
  const segments = [];
1020
1144
  for (const ref of refs) segments.push(await imageSegment(ref));
1145
+ if (!parts.length && !segments.length && markers.length) return { sent: 0, messageIds: [], said: [], imagesSent: 0, images: [], note: '只有 [图片] 这种记录标记,没有要发的内容;发图用 images' };
1021
1146
  if (!parts.length && !segments.length) throw new Error(dropped ? `nothing to send: ${dropped} line(s) were English prose, not chat, and were dropped — write what you mean to say to them` : 'nothing to send (text and images both empty)');
1022
1147
  const ids = [], failed = [];
1023
1148
  const started = Date.now();
@@ -1026,16 +1151,17 @@ export function apply(ctx, config = {}, deps = {}) {
1026
1151
  for (const [i, part] of parts.entries()) {
1027
1152
  if (i && cfg.typingMsPerChar > 0) await new Promise((r) => setTimeout(r, Math.min(2500, part.length * cfg.typingMsPerChar)));
1028
1153
  // He spoke while she was typing these out: stop, she reads him first.
1029
- if (i && toOwner && qq.ownerSaidSince(started)) { unsent = [...parts.slice(i), ...refs.map((r) => `[图片 ${String(r).slice(0, 80)}]`)]; break; }
1154
+ if (i && toOwner && qq.ownerSaidSince(started)) { unsent = [...parts.slice(i), ...refs.map((r) => imageMarker(sentRef(r).slice(0, 80)))]; break; }
1030
1155
  ids.push((await call(action, { ...target, message: [{ type: 'text', data: { text: part } }] }))?.message_id);
1031
1156
  }
1032
1157
  if (unsent) {
1033
1158
  qq.sentToOwnerAt = Date.now();
1034
1159
  audit({ kind: 'sent', to: target, text: parts.slice(0, ids.length).join(' / ').slice(0, 200), interrupted: true, unsent: unsent.length });
1035
- return { sent: ids.length, messageIds: ids, interrupted: true, unsent, said: parts.slice(0, ids.length), imagesSent: 0 };
1160
+ return { sent: ids.length, messageIds: ids, interrupted: true, unsent, said: parts.slice(0, ids.length), imagesSent: 0, images: [] };
1036
1161
  }
1162
+ const went = []; // the refs of the images that went out, in order (her transcript records them)
1037
1163
  for (const [i, seg] of segments.entries()) {
1038
- try { ids.push((await call(action, { ...target, message: [seg] }))?.message_id); } catch (err) {
1164
+ try { ids.push((await call(action, { ...target, message: [seg] }))?.message_id); went.push(sentRef(refs[i])); } catch (err) {
1039
1165
  if (!ids.length) throw err; // nothing delivered yet: an ordinary failure
1040
1166
  failed.push({ image: String(refs[i]).slice(0, 80), error: err.message });
1041
1167
  }
@@ -1043,7 +1169,7 @@ export function apply(ctx, config = {}, deps = {}) {
1043
1169
  const n = ids.length;
1044
1170
  if (qq.owner && target.user_id === Number(qq.owner)) qq.sentToOwnerAt = Date.now();
1045
1171
  audit({ kind: 'sent', to: target, text: parts.join(' / ').slice(0, 200), images: refs.map((r) => (String(r).startsWith('http') ? r : String(r).slice(0, 80))), ...(failed.length ? { failed } : {}), ...(dropped ? { dropped } : {}) });
1046
- return { sent: n, messageIds: ids, said: parts, imagesSent: segments.length - failed.length, ...(failed.length ? { failed, textSent: parts.length } : {}), ...(dropped ? { dropped } : {}) };
1172
+ return { sent: n, messageIds: ids, said: parts, imagesSent: went.length, images: went, ...(failed.length ? { failed, textSent: parts.length } : {}), ...(dropped ? { dropped } : {}) };
1047
1173
  };
1048
1174
  /** Her friend list: [{ uid, nickname, remark }] (OneBot get_friend_list). */
1049
1175
  qq.friends = async () => ((await call('get_friend_list', {})) ?? []).map((f) => ({ uid: String(f.user_id ?? ''), nickname: String(f.nickname ?? ''), remark: String(f.remark ?? '') })).filter((f) => f.uid);
@@ -1098,10 +1224,10 @@ export function apply(ctx, config = {}, deps = {}) {
1098
1224
  description: '用你的 QQ(电脑端在线)发消息:文字和/或图片。to: "user:<QQ号>"、"group:<群号>" 或 "owner"(和你说话的人)。text 里每一行是单独一条消息,像人打字那样一句一句发。images 是要发的图片,每张直接作为图片发出去(不是链接):"sticker:<id>"(你存的表情包,id 见收到的 [表情包 sticker:…]);"face:<编号>"(QQ 小黄脸);图片网址;"phone:screen"(你手机此刻的屏幕截图);"phone:latest"(你手机里最新的照片/截图/下载);手机上的路径 "/sdcard/...";或你电脑上的绝对路径。只在你确实想回或想说时用。',
1099
1225
  parameters: { type: 'object', properties: { to: { type: 'string' }, text: { type: 'string' }, images: { type: 'array', items: { type: 'string' } } }, required: ['to'], additionalProperties: false },
1100
1226
  // presentationMeta lands on the durable tool/result: what really went out, for her memory's transcript.
1101
- output: { schema: { type: 'object', properties: { sent: { type: 'number' } }, required: ['sent'], additionalProperties: true }, render: (_a, v) => [{ type: 'text', text: sendReport(v) }], presentationMeta: (_a, v) => ({ sent: v?.sent ?? 0, said: v?.said ?? [], imagesSent: v?.imagesSent ?? 0, ...(v?.interrupted ? { interrupted: true } : {}) }) },
1227
+ output: { schema: { type: 'object', properties: { sent: { type: 'number' } }, required: ['sent'], additionalProperties: true }, render: (_a, v) => [{ type: 'text', text: sendReport(v) }], presentationMeta: (_a, v) => ({ sent: v?.sent ?? 0, said: v?.said ?? [], imagesSent: v?.imagesSent ?? 0, images: v?.images ?? [], ...(v?.interrupted ? { interrupted: true } : {}) }) },
1102
1228
  async execute(args) {
1103
1229
  const r = await qq.send(args.to, args.text ?? '', args.images ?? []);
1104
- const out = { sent: r.sent, said: r.said ?? [], imagesSent: r.imagesSent ?? 0, ...(r.failed ? { failed: r.failed, textSent: r.textSent } : {}), ...(r.dropped ? { dropped: r.dropped } : {}), ...(r.interrupted ? { interrupted: true, unsent: r.unsent } : {}) };
1230
+ const out = { sent: r.sent, said: r.said ?? [], imagesSent: r.imagesSent ?? 0, images: r.images ?? [], ...(r.failed ? { failed: r.failed, textSent: r.textSent } : {}), ...(r.dropped ? { dropped: r.dropped } : {}), ...(r.interrupted ? { interrupted: true, unsent: r.unsent } : {}) };
1105
1231
  return r.failed || r.dropped || r.interrupted ? { ...out, note: sendReport(out).split('\n').slice(1).join('\n') } : out;
1106
1232
  },
1107
1233
  }), 'dsh-dot/qq: qq_send tool');