dsh-world 0.7.5 → 0.8.0

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.
Files changed (115) hide show
  1. package/CHANGELOG.md +541 -446
  2. package/CONTRIBUTING.md +174 -163
  3. package/INSTALL.md +314 -314
  4. package/NOTES.md +98 -5
  5. package/README.md +332 -258
  6. package/client/index.js +1126 -986
  7. package/lib/compaction.d.ts +7 -1
  8. package/lib/compaction.js +8 -3
  9. package/lib/console/routes.d.ts +11 -3
  10. package/lib/console/routes.js +12 -7
  11. package/lib/console/snapshot.d.ts +2 -0
  12. package/lib/console/snapshot.js +1 -0
  13. package/lib/ecosystem/computer/browser/browser.d.ts +34 -0
  14. package/lib/ecosystem/computer/browser/browser.js +101 -0
  15. package/lib/ecosystem/computer/browser/cdp.d.ts +22 -0
  16. package/lib/ecosystem/computer/browser/cdp.js +91 -0
  17. package/lib/ecosystem/computer/browser/edge.d.ts +15 -0
  18. package/lib/ecosystem/computer/browser/edge.js +80 -0
  19. package/lib/ecosystem/computer/browser/page.d.ts +41 -0
  20. package/lib/ecosystem/computer/browser/page.js +140 -0
  21. package/lib/ecosystem/computer/browser/pageLib.d.ts +7 -0
  22. package/lib/ecosystem/computer/browser/pageLib.js +106 -0
  23. package/lib/ecosystem/computer/desktop/desktop.d.ts +91 -0
  24. package/lib/ecosystem/computer/desktop/desktop.js +322 -0
  25. package/lib/ecosystem/computer/desktop/driver.d.ts +34 -0
  26. package/lib/ecosystem/computer/desktop/driver.js +197 -0
  27. package/lib/ecosystem/computer/desktop/processes.d.ts +5 -0
  28. package/lib/ecosystem/computer/desktop/processes.js +15 -0
  29. package/lib/ecosystem/computer/desktop/recording.d.ts +77 -0
  30. package/lib/ecosystem/computer/desktop/recording.js +187 -0
  31. package/lib/ecosystem/computer/desktop/screen.d.ts +33 -0
  32. package/lib/ecosystem/computer/desktop/screen.js +182 -0
  33. package/lib/ecosystem/computer/desktop/turns.d.ts +58 -0
  34. package/lib/ecosystem/computer/desktop/turns.js +94 -0
  35. package/lib/ecosystem/computer/desktop/watching.d.ts +25 -0
  36. package/lib/ecosystem/computer/desktop/watching.js +81 -0
  37. package/lib/ecosystem/computer/inbox/inbox.d.ts +32 -0
  38. package/lib/ecosystem/computer/inbox/inbox.js +142 -0
  39. package/lib/ecosystem/computer/index.d.ts +82 -0
  40. package/lib/ecosystem/computer/index.js +133 -0
  41. package/lib/ecosystem/computer/procedures/alarm.d.ts +13 -0
  42. package/lib/ecosystem/computer/procedures/alarm.js +46 -0
  43. package/lib/ecosystem/computer/procedures/archive.d.ts +32 -0
  44. package/lib/ecosystem/computer/procedures/archive.js +68 -0
  45. package/lib/ecosystem/computer/procedures/catalog.d.ts +7 -0
  46. package/lib/ecosystem/computer/procedures/catalog.js +75 -0
  47. package/lib/ecosystem/computer/procedures/format.d.ts +167 -0
  48. package/lib/ecosystem/computer/procedures/format.js +332 -0
  49. package/lib/ecosystem/computer/procedures/library.d.ts +53 -0
  50. package/lib/ecosystem/computer/procedures/library.js +178 -0
  51. package/lib/ecosystem/computer/procedures/run.d.ts +80 -0
  52. package/lib/ecosystem/computer/procedures/run.js +245 -0
  53. package/lib/ecosystem/computer/tools.d.ts +21 -0
  54. package/lib/ecosystem/computer/tools.js +197 -0
  55. package/lib/ecosystem/qq-native/images.d.ts +8 -1
  56. package/lib/ecosystem/qq-native/images.js +34 -15
  57. package/lib/ecosystem/qq-native/index.js +5 -5
  58. package/lib/ecosystem/shared/turn.d.ts +3 -0
  59. package/lib/ecosystem/shared/turn.js +13 -3
  60. package/lib/ecosystem/wechat/home.d.ts +1 -0
  61. package/lib/ecosystem/wechat/home.js +9 -3
  62. package/lib/ecosystem/wechat/images.js +3 -5
  63. package/lib/ecosystem/wechat/index.d.ts +6 -0
  64. package/lib/ecosystem/wechat/index.js +3 -1
  65. package/lib/ecosystem/wechat/presence.d.ts +5 -1
  66. package/lib/ecosystem/wechat/presence.js +22 -13
  67. package/lib/ecosystem/wechat/reading.d.ts +1 -1
  68. package/lib/ecosystem/wechat/reading.js +2 -2
  69. package/lib/elsewhere.d.ts +34 -0
  70. package/lib/elsewhere.js +40 -0
  71. package/lib/history.d.ts +5 -0
  72. package/lib/history.js +7 -1
  73. package/lib/hostContract.d.ts +6 -0
  74. package/lib/images.d.ts +30 -0
  75. package/lib/images.js +101 -0
  76. package/lib/index.d.ts +4 -0
  77. package/lib/index.js +98 -6
  78. package/lib/life/photoTools.js +4 -15
  79. package/lib/life/runtime.d.ts +12 -2
  80. package/lib/life/runtime.js +93 -12
  81. package/lib/life/toolRegistry.d.ts +9 -3
  82. package/lib/life/toolRegistry.js +46 -27
  83. package/lib/life/turnCost.d.ts +16 -0
  84. package/lib/life/turnCost.js +44 -0
  85. package/lib/life/types.d.ts +17 -0
  86. package/lib/memoryTools.d.ts +4 -2
  87. package/lib/memoryTools.js +25 -5
  88. package/lib/mood.d.ts +5 -1
  89. package/lib/mood.js +25 -8
  90. package/lib/promptSection.d.ts +3 -0
  91. package/lib/promptSection.js +29 -42
  92. package/lib/runtime.js +44 -17
  93. package/lib/sessionNews.d.ts +17 -0
  94. package/lib/sessionNews.js +55 -0
  95. package/lib/store.d.ts +6 -0
  96. package/lib/store.js +17 -3
  97. package/lib/types.d.ts +4 -0
  98. package/lib/worldAdd.d.ts +9 -0
  99. package/lib/worldAdd.js +39 -0
  100. package/lib/worldPrompt.d.ts +3 -2
  101. package/lib/worldPrompt.js +3 -2
  102. package/lib/worldRoutes.d.ts +5 -2
  103. package/lib/worldRoutes.js +29 -4
  104. package/lib/worldSwitch.d.ts +8 -2
  105. package/lib/worldSwitch.js +11 -5
  106. package/package.json +132 -129
  107. package/src/ecosystem/README.md +2 -1
  108. package/src/ecosystem/computer/NOTICE.md +27 -0
  109. package/src/ecosystem/computer/README.md +244 -0
  110. package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
  111. package/src/ecosystem/wechat/windows/README.md +4 -2
  112. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  113. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  114. package/templates/world-prompt-memory.md +10 -10
  115. package/templates/world-prompt.md +65 -65
@@ -0,0 +1,197 @@
1
+ import { defineTool } from '@deepseek-ai/dsh-tools';
2
+ import { errorText } from "../../faults.js";
3
+ import { imagesMessage, keepImages, shownImage } from "../../images.js";
4
+ import { PAGE_KEYS } from "./browser/page.js";
5
+ import { catalogText } from "./procedures/catalog.js";
6
+ import { checkArgs, cut, failureText, fallbackText, noteRun, resultText, runVersions } from "./procedures/run.js";
7
+ import { desktopAdvice, isWebAddress, WEB_ONLY } from "./procedures/format.js";
8
+ import { DESKTOP_KEYS } from "./desktop/desktop.js";
9
+ const output = { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] };
10
+ const DESKTOP_NOTE = `用伙伴的电脑(后台模式):看当前窗口的截图,按图上的像素操作;输入直接投给那个窗口,不动伙伴的鼠标、不抢焦点,伙伴用着电脑我也能用。学会的操作先用 computer_use 找。以管理员权限运行的程序(很多游戏)后台够不着,要用就请伙伴切到前台模式。action 取一种:open(app 写程序名,打开它当作当前窗口,托盘里的会点开;不写就列出开着的窗口)|shot(再截一张)|click(x、y 是截图上的像素;double 双击,right 右键,hold 按住几秒是长按)|drag(从 x、y 按住拖到 x2、y2 松开;hold 先按住几秒再拖,right 用右键)|type(text;给 x、y 就先点那里;submit 再回车)|press(key:${DESKTOP_KEYS};hold 按住几秒)|scroll(direction 为 up/down/left/right,可加 x、y)|zoom(截图上 x、y 到 x2、y2 放大看)|record(看视频这类会动的:先让它播起来,录 seconds 秒,最多 600;every 每几秒一帧,默认 1;pause 为 false 时录完不暂停)|stop(停下正在录的)|review(看录好的:不带参数看全片,from、to 细看一段,at 看某一秒)。每步之后都有新截图,自己看做对了没有;没反应就换个办法,别重复同一下。`;
11
+ /** Foreground mode: the same actions over the whole screen with the real mouse and keyboard. */
12
+ const SCREEN_NOTE = `用伙伴的电脑(前台模式):伙伴把电脑交给了我。看整个屏幕的截图,用真的鼠标键盘按图上的像素操作,游戏和管理员程序也能用;伙伴的鼠标会跟着动。伙伴连按两下 Esc 或点屏幕顶上的「停下」,我就停。action 和后台模式一样:open(把程序放到最前面,没开的会启动;不写 app 就列出窗口)|shot|click(x、y,double、right,hold 长按几秒)|drag(x、y 拖到 x2、y2;hold 先按住几秒,right)|type(text,可先给 x、y,submit)|press(key:${DESKTOP_KEYS};hold 按住几秒,比如游戏里按住 w 走路)|scroll(direction,可加 x、y)|zoom(x、y 到 x2、y2)|record(seconds、every、pause)|stop|review(from、to 或 at)。走通的步骤可以 procedure_save 存成 where:"screen" 的操作,以后在前台模式下用 procedure_run 回放。`;
13
+ const FORMAT_NOTE = '格式不对时保存会被拒绝并说明哪里不对。文件是 JSON:name(操作名,中文也行,不含 / 和 .)、category(分类,比如 wechat、bilibili)、description、params({名字:{type,required?}})、steps。步骤各取一种:open|click|type {into,text}|press|wait {text|anchor|url,seconds?}|check|extract {list,fields,limit?}|script(可 return,as 存成值)。锚点 {role?,name?,text?,css?,nth?(从 0 数)},role、name 用 page_read 里看到的;extract 的字段可以只写 attr,读列表项自己。文字里的 {{参数}} 会代入。会向外发出的步骤标 outward:true。extract 一条都没有、某个字段每条都空、带 as 的 script 结果为空,都算没跑通;本来就可能为空的(比如没有未读)在那一步加 allowEmpty:true。check 最多等 3 秒。没跑通而且还没有向外的动作时,会依次试最近 2 个旧版本。onAlarm:true 表示闹钟前自动跑(不能有必填参数和 outward 步骤);summary 可用 {{as 名}} 和 {{count}}。messages 把 extract 的字段写进归档:channel、conversationType 写死,其余是 {{字段}} 模板,outgoing 算出 true 表示是你自己说的。\n伙伴电脑上用 desktop 走通的:后台模式写 where:"desktop",前台模式写 where:"screen"(只能在前台模式下跑)。步骤各取一种:open(程序名或窗口标题,比如「微信」「朋友圈」)|click {x,y,double?,right?,hold?}|drag {x,y,x2,y2,right?,hold?}|type {text,x?,y?}|press(同 desktop 的按键,可加 hold)|scroll {direction,x?,y?}|wait {seconds}。x、y 是 desktop 截图上的像素(desktop 是窗口的截图,screen 是整个屏幕的截图),大小变了会按比例跟着变。回放时步与步之间不停,手动做时等过的地方(点完才弹出的窗口)要写 wait。不能用 onAlarm、summary、messages。跑完会给最后一张截图,自己看做成了没有。';
14
+ /**
15
+ * Her page tools and her catalog: what she learns is found with `computer_use` and run with `procedure_run`, so the tool
16
+ * list stays the same however much she learns. Home only (see `toolRegistry`).
17
+ */
18
+ export function computerTools(deps) {
19
+ const run = (work) => deps.browser.run(work);
20
+ // Checked before anything is queued, so a bad argument never launches or waits on the browser.
21
+ const whole = (value, message) => { if (!Number.isSafeInteger(value) || value < 1)
22
+ throw new Error(message); return value; };
23
+ const REF = 'ref 是 page_read 里方括号中的编号';
24
+ const ref = (value) => ({ ref: whole(value, REF) });
25
+ const where = (page) => page.brief().then(text => text.split('\n')[0]);
26
+ const page = [
27
+ defineTool({ name: 'page_open', description: '在我自己的浏览器里打开一个地址(浏览器没开会先打开它,窗口在桌面上,不用伙伴的鼠标键盘),返回页面上的元素和文字,同 page_read。登录要扫码时在那个窗口里扫。', parameters: { url: { type: 'string', required: true } }, output,
28
+ execute: async ({ url }) => { if (!isWebAddress(url))
29
+ throw new Error(WEB_ONLY); return run(async (p) => { await p.open(url); return p.read(); }); } }),
30
+ defineTool({ name: 'page_read', description: '读当前页面:每个可操作元素一行,[编号] 角色 名称;编号在页面不跳转时一直有效。page 翻看更多元素,ref 只看某个元素里面。之后附可见文字(截断)。', parameters: { page: { type: 'integer' }, ref: { type: 'integer' } }, output,
31
+ execute: async ({ page: index, ref: scope }) => {
32
+ const options = { ...(index === undefined ? {} : { page: whole(index, 'page 应该是 1 以上的整数') }), ...(scope === undefined ? {} : ref(scope)) };
33
+ return run(p => p.read(options));
34
+ } }),
35
+ defineTool({ name: 'page_click', description: '点 page_read 给出的编号。', parameters: { ref: { type: 'integer', required: true } }, output,
36
+ execute: async (args) => { const target = ref(args.ref); return run(async (p) => { await p.click(target); return `已点击 [${args.ref}],现在:${await where(p)}`; }); } }),
37
+ defineTool({ name: 'page_type', description: '在编号对应的输入框里输入文字(整段输入,中文也行),框里原有的内容会被替换;submit 为 true 时再按回车。', parameters: { ref: { type: 'integer', required: true }, text: { type: 'string', required: true }, submit: { type: 'boolean' } }, output,
38
+ execute: async (args) => { const target = ref(args.ref); return run(async (p) => { await p.type(target, args.text); if (args.submit)
39
+ await p.press('Enter'); return `已输入,现在:${await where(p)}`; }); } }),
40
+ defineTool({ name: 'page_press', description: `按一个键:${PAGE_KEYS.join('、')}。`, parameters: { key: { type: 'string', required: true } }, output,
41
+ execute: async ({ key }) => run(async (p) => { await p.press(key); return `已按 ${key},现在:${await where(p)}`; }) }),
42
+ defineTool({ name: 'page_scroll', description: '滚动一屏左右:direction 为 up 或 down;给 ref 就在那个元素上滚。', parameters: { direction: { type: 'string', required: true }, ref: { type: 'integer' } }, output,
43
+ execute: async (args) => {
44
+ if (args.direction !== 'up' && args.direction !== 'down')
45
+ throw new Error('direction 只能是 up 或 down');
46
+ const target = args.ref === undefined ? undefined : ref(args.ref);
47
+ return run(async (p) => { await p.scroll(args.direction, target); return '已滚动。'; });
48
+ } }),
49
+ defineTool({ name: 'page_wait', description: '等某段文字出现(text,最多等 seconds 秒,默认 10);或检查某个编号的元素还在(ref,立即返回)。seconds 最多 60。', parameters: { text: { type: 'string' }, ref: { type: 'integer' }, seconds: { type: 'number' } }, output,
50
+ execute: async ({ text, ref: id, seconds = 10 }) => {
51
+ if (text === undefined && id === undefined)
52
+ throw new Error('要给 text 或 ref');
53
+ if (text !== undefined && !text.trim())
54
+ throw new Error('text 不能是空的');
55
+ const target = id === undefined ? undefined : whole(id, REF);
56
+ if (typeof seconds !== 'number' || seconds <= 0 || seconds > 60)
57
+ throw new Error('seconds 应该在 0 到 60 之间');
58
+ return run(async (p, signal) => {
59
+ if (target !== undefined) {
60
+ await p.read({ ref: target });
61
+ return `[${target}] 还在。`;
62
+ }
63
+ await p.wait({ text: text }, seconds * 1000, signal);
64
+ return `等到了「${text}」。`;
65
+ });
66
+ } }),
67
+ defineTool({ name: 'page_shot', description: '截当前页面一张图附在下一条消息里;只在读文字看不明白时用,图片很占地方。', parameters: {}, output,
68
+ execute: async (_args, context) => {
69
+ const attachments = imageStore();
70
+ await attach(attachments, { data: await run(p => p.shot()), mediaType: 'image/png' }, '我的浏览器.png', '这是我浏览器当前页面的截图。', context);
71
+ return '截图附在下一条消息里。';
72
+ } }),
73
+ defineTool({ name: 'page_eval', description: '在当前页面里执行一段 JS(可 return 一个能转成 JSON 的值),只在我的浏览器页面里,碰不到电脑上的文件。十秒超时,结果截到 2000 字。', parameters: { script: { type: 'string', required: true } }, output,
74
+ execute: async ({ script }) => run(async (p) => cut(JSON.stringify(await p.evaluate(script, {})) ?? 'null', 2000)) }),
75
+ ];
76
+ const procedures = [
77
+ defineTool({ name: 'computer_use', description: '用电脑之前先说这次要用的程序或网站(target 只写对象,比如「微信」「B站」,不写要做的事),返回我写的这一类的说明、操作名和文件所在的文件夹;要看某个操作的参数和步骤就读它的文件。不说就列出我现在能用的模块和学会的操作有哪些分类。', parameters: { target: { type: 'string' } }, output,
78
+ // Asked without naming anything, she is first told which of her modules she has right now: a name each, no instructions.
79
+ execute: async ({ target }) => target?.trim() ? catalogText(deps.library, target) : `${modules()}\n${catalogText(deps.library)}` }),
80
+ defineTool({ name: 'procedure_run', description: '跑一个学会的操作:path 写「分类/操作」,args 是它声明的参数。不调模型,直接回放;没跑通会说第几步、为什么。', parameters: { path: { type: 'string', required: true }, args: { type: 'object', additionalProperties: true } }, output,
81
+ execute: async ({ path, args }, context) => runStored(path, (args ?? {}), context) }),
82
+ // The format is long and only needed when she writes one, so it is handed over on asking rather than sent with every step.
83
+ defineTool({ name: 'procedure_save', description: `把用 page_* 或 desktop 走通的步骤存成一个操作,以后用 procedure_run 直接跑;同名覆盖、留下旧版本。文件在 ${deps.library.dir},也可以直接改。json 是整份操作;第一次写或记不清格式,就不带 json 调一次,会给出完整的格式说明。`, parameters: { json: { type: 'string' } }, output,
84
+ execute: async ({ json }) => {
85
+ if (typeof json !== 'string' || !json.trim())
86
+ return `操作文件的格式:\n${FORMAT_NOTE}`;
87
+ try {
88
+ const { path, version } = deps.library.save(json);
89
+ const advice = desktopAdvice(deps.library.get(path));
90
+ return `已保存 ${path}(第 ${version} 版),可以用 procedure_run 跑了。${advice.length ? `\n先看看这几处:\n${advice.map(line => `- ${line}`).join('\n')}` : ''}`;
91
+ }
92
+ catch (error) {
93
+ throw new Error(`没有保存:${errorText(error)}(完整格式:不带 json 调一次 procedure_save)`);
94
+ }
95
+ } }),
96
+ defineTool({ name: 'procedure_note', description: '写某一类操作的说明:这一类对应哪个程序或网站(写上程序名、网址和别称,computer_use 靠它找到这一类)、要注意什么。整份替换,旧的留作版本,最多 1000 字。', parameters: { category: { type: 'string', required: true }, text: { type: 'string', required: true } }, output,
97
+ execute: async ({ category, text }) => {
98
+ try {
99
+ return `已写好 ${category} 的说明(第 ${deps.library.saveGuide(category, text)} 版)。`;
100
+ }
101
+ catch (error) {
102
+ throw new Error(`没有保存:${errorText(error)}`);
103
+ }
104
+ } }),
105
+ ];
106
+ const live = deps.screen ?? deps.desktop;
107
+ const desktop = !live ? [] : [
108
+ defineTool({ name: 'desktop', description: deps.screen ? SCREEN_NOTE : DESKTOP_NOTE, output, parameters: {
109
+ action: { type: 'string', required: true }, app: { type: 'string' }, x: { type: 'number' }, y: { type: 'number' }, x2: { type: 'number' }, y2: { type: 'number' },
110
+ double: { type: 'boolean' }, right: { type: 'boolean' }, hold: { type: 'number' }, text: { type: 'string' }, submit: { type: 'boolean' }, key: { type: 'string' }, direction: { type: 'string' },
111
+ seconds: { type: 'number' }, every: { type: 'number' }, from: { type: 'number' }, to: { type: 'number' }, at: { type: 'number' }, pause: { type: 'boolean' },
112
+ }, execute: async (input, context) => {
113
+ context.signal.throwIfAborted();
114
+ const result = await live.act(input);
115
+ if (!result.image)
116
+ return result.text;
117
+ // The action already happened, so a missing image store costs her the picture, never the result.
118
+ const attachments = deps.attachments();
119
+ if (!attachments)
120
+ return `${result.text}(宿主图片存储不可用,看不到截图)`;
121
+ await attach(attachments, result.image, result.image.caption ? '录下的画面.jpg' : '伙伴的电脑.png', result.image.caption ?? (deps.screen ? '这是伙伴电脑整个屏幕的截图。' : '这是伙伴电脑上当前窗口的截图。'), context);
122
+ return result.text;
123
+ } }),
124
+ ];
125
+ return [...page, ...procedures, ...desktop];
126
+ /** The modules she can use now; the desktop line follows its switch and mode. */
127
+ function modules() {
128
+ const desktop = deps.screen ? 'desktop(伙伴的电脑,前台模式:整个屏幕,真的鼠标键盘)' : deps.desktop ? 'desktop(伙伴的电脑,后台模式:一个窗口,不动伙伴的鼠标)' : '';
129
+ return `现在能用:page_*(你自己的浏览器)、${desktop ? `${desktop}、` : ''}procedure_*(学会的操作)。`;
130
+ }
131
+ function imageStore() {
132
+ const attachments = deps.attachments();
133
+ if (!attachments)
134
+ throw new Error('宿主图片存储不可用');
135
+ return attachments;
136
+ }
137
+ /** Tool results carry text only, so an image goes to her in the next message. */
138
+ async function attach(attachments, image, name, caption, context) {
139
+ const [attachment] = await keepImages(attachments, [{ data: image.data, mediaType: image.mediaType, name }]);
140
+ context.signal.throwIfAborted();
141
+ context.deferContext(imagesMessage(shownImage(caption, attachment)));
142
+ }
143
+ /** The picture a desktop run ended on; a missing image store costs her the picture, never the result. */
144
+ async function lastPicture(image, context) {
145
+ const attachments = deps.attachments();
146
+ if (!attachments)
147
+ return '\n(宿主图片存储不可用,看不到最后的截图)';
148
+ await attach(attachments, image, '伙伴的电脑.png', '这是操作跑完时伙伴电脑上当前窗口的截图。', context);
149
+ return '\n最后一张截图附在下一条消息里,看看做成了没有。';
150
+ }
151
+ /** Filing what she read must never cost her the read itself, so a failure is a line after the result. */
152
+ async function archiveNote(map, rows, args) {
153
+ try {
154
+ const archived = await deps.recorder.record(deps.agent, map, rows, args);
155
+ return archived.outOfScope ? '\n(这个会话不在归档范围里,没有记)' : `\n新记进归档 ${archived.written} 条${archived.skipped ? `,跳过 ${archived.skipped} 条缺字段的` : ''}。`;
156
+ }
157
+ catch (error) {
158
+ return `\n(没能记进归档:${errorText(error)})`;
159
+ }
160
+ }
161
+ /** One of her operations, newest version first, falling back to the ones it replaced. */
162
+ async function runStored(path, input, context) {
163
+ const procedure = deps.library.get(path);
164
+ if (!procedure)
165
+ throw new Error(`没有 ${path} 这个操作,先用 computer_use 看看有哪些`);
166
+ checkArgs(procedure, input);
167
+ const kept = deps.library.kept(path);
168
+ // Each mode replays what was found in it: one window in the background, the whole screen in the foreground.
169
+ const live = procedure.where === 'screen' ? deps.screen : procedure.where === 'desktop' ? deps.desktop : undefined;
170
+ if (procedure.where === 'desktop' && !live)
171
+ throw new Error(`${path} 是伙伴电脑上的操作(后台模式),要先打开「她用电脑」`);
172
+ if (procedure.where === 'screen' && !live)
173
+ throw new Error(`${path} 是在整个屏幕上做的操作(前台模式),要请伙伴把「她用电脑」切到「前台」`);
174
+ let outcome, last;
175
+ // On the desktop every step answers with a picture; the last one is what she checks the run by.
176
+ const hands = live && { act: async (input) => { const result = await live.act(input); last = result.image ?? last; return result; } };
177
+ try {
178
+ outcome = procedure.where !== undefined
179
+ ? await runVersions(procedure, kept, input, hands, context.signal)
180
+ : await run((p, signal) => runVersions(procedure, kept, input, p, AbortSignal.any([signal, context.signal])));
181
+ }
182
+ catch (error) {
183
+ noteRun(deps.library, path, { thrown: error });
184
+ throw error;
185
+ }
186
+ noteRun(deps.library, path, outcome);
187
+ const { result, ran, fallback } = outcome;
188
+ const seen = last ? await lastPicture(last, context) : '';
189
+ if (!result.ok)
190
+ return failureText(path, result) + seen;
191
+ if (procedure.where !== undefined)
192
+ return '做完了。' + (fallback ? `(${fallbackText(fallback)}趁现在用 desktop 把最新版走通再存。)` : '') + (seen || '(没有截图,用 desktop shot 看看结果)');
193
+ // The version that ran declares what it read, and its own arguments are what fill the records.
194
+ const archived = ran.messages && result.rows.length ? await archiveNote(ran.messages, result.rows, checkArgs(ran, input)) : '';
195
+ return resultText(result) + archived + (fallback ? `\n(${fallbackText(fallback)}页面可能改了,或新旧页面并存。趁现在修:用 page_* 看看现在的页面,改好再 procedure_save,新旧页面都要能跑。)` : '');
196
+ }
197
+ }
@@ -1,9 +1,16 @@
1
1
  import type { Context } from '@deepseek-ai/cordis';
2
2
  import type { ContentBlock } from '@deepseek-ai/dsh-llm';
3
3
  import type { SpeakerIdentity } from '../shared/identity.ts';
4
+ /** Pictures of one batch put in front of her at once. */
4
5
  export declare const MAX_BATCH_IMAGES = 4;
5
- export interface QqImage {
6
+ /** Pictures of one batch that are fetched and kept: QQ's links expire, so one not fetched on arrival is gone. */
7
+ export declare const MAX_KEPT_IMAGES = 12;
8
+ /** One image segment as QQ sent it: the id get_image takes, and the link some images (shop stickers, animated ones) carry instead. */
9
+ export interface QqImageSegment {
6
10
  file: unknown;
11
+ url: unknown;
12
+ }
13
+ export interface QqImage extends QqImageSegment {
7
14
  identity: SpeakerIdentity;
8
15
  }
9
16
  export declare function admitQqImages(ctx: Context, images: readonly QqImage[], getImage?: (file: string) => Promise<string>): Promise<ContentBlock[]>;
@@ -1,10 +1,15 @@
1
+ import { keepImages, keptLines, shownImage } from "../../images.js";
2
+ /** Pictures of one batch put in front of her at once. */
1
3
  export const MAX_BATCH_IMAGES = 4;
4
+ /** Pictures of one batch that are fetched and kept: QQ's links expire, so one not fetched on arrival is gone. */
5
+ export const MAX_KEPT_IMAGES = 12;
2
6
  const DOWNLOAD_TIMEOUT_MS = 10_000;
3
- const QQ_IMAGE_ORIGIN = 'https://multimedia.nt.qq.com.cn';
7
+ // QQ's own image hosts: the CDN behind get_image (multimedia.nt.qq.com.cn) and the sticker shop (gxh.vip.qq.com) (#44).
8
+ const QQ_IMAGE_DOMAINS = ['qq.com', 'qq.com.cn'];
9
+ const onQqHost = (url) => QQ_IMAGE_DOMAINS.some(domain => url.hostname === domain || url.hostname.endsWith(`.${domain}`));
4
10
  async function downloadImage(link, limits) {
5
- // shortcut: only the verified SnowLuma CDN is supported, add hosts when their gateway responses are verified.
6
11
  const url = new URL(link);
7
- if (url.protocol !== 'https:' || url.origin !== QQ_IMAGE_ORIGIN || url.username || url.password)
12
+ if (url.protocol !== 'https:' || url.port || !onQqHost(url) || url.username || url.password)
8
13
  throw new Error('QQ 图片链接不受支持');
9
14
  const controller = new AbortController();
10
15
  try {
@@ -32,37 +37,51 @@ async function downloadImage(link, limits) {
32
37
  controller.abort();
33
38
  }
34
39
  }
40
+ // get_image first, as verified for ordinary pictures; the segment's own link when get_image has no usable answer.
41
+ async function fetchQqImage({ file, url }, limits, getImage) {
42
+ const viaFile = typeof file === 'string' && /^[a-z0-9_-][a-z0-9_.-]{0,255}$/i.test(file) && getImage
43
+ ? getImage(file).then(link => downloadImage(link, limits)) : Promise.reject(new Error('QQ 图片标识无效'));
44
+ return viaFile.catch(error => {
45
+ if (typeof url !== 'string' || !url)
46
+ throw error;
47
+ return downloadImage(url, limits);
48
+ });
49
+ }
35
50
  export async function admitQqImages(ctx, images, getImage) {
36
51
  if (!images.length)
37
52
  return [];
38
53
  const unavailable = { type: 'text', text: '本次有 QQ 图片未能读取,请勿假装看过。' };
39
54
  const store = ctx.get('attachments');
40
- if (!store || !getImage)
55
+ if (!store)
41
56
  return [unavailable];
42
57
  const limits = store.imageLimits;
43
- const selected = images.slice(0, Math.min(MAX_BATCH_IMAGES, limits.maxImagesPerMessage));
44
- const downloads = await Promise.allSettled(selected.map(async ({ file }) => {
45
- if (typeof file !== 'string' || !/^[a-z0-9_-][a-z0-9_.-]{0,255}$/i.test(file))
46
- throw new Error('QQ 图片标识无效');
47
- return downloadImage(await getImage(file), limits);
48
- }));
58
+ const selected = images.slice(0, MAX_KEPT_IMAGES), shownMax = Math.min(MAX_BATCH_IMAGES, limits.maxImagesPerMessage);
59
+ const downloads = await Promise.allSettled(selected.map(image => fetchQqImage(image, limits, getImage)));
49
60
  const content = [];
61
+ const kept = [];
50
62
  let remainingBytes = limits.maxMessageImageBytes;
51
- let saved = 0;
63
+ let saved = 0, shown = 0;
52
64
  for (const [index, result] of downloads.entries()) {
53
- if (result.status === 'rejected' || result.value.data.byteLength > remainingBytes)
65
+ if (result.status === 'rejected')
54
66
  continue;
55
67
  try {
56
- const [attachment] = await store.saveImages([result.value]);
68
+ const [attachment] = await keepImages(store, [result.value]);
57
69
  if (!attachment)
58
70
  continue;
71
+ saved++;
72
+ // Every picture sent to her is kept; only as many as one message may carry are put in front of her now.
73
+ if (shown >= shownMax || result.value.data.byteLength > remainingBytes) {
74
+ kept.push(attachment);
75
+ continue;
76
+ }
59
77
  const { platformMessageId, speakerId, speakerName } = selected[index].identity;
60
- content.push({ type: 'text', text: `下面是本次收到的第 ${index + 1} 张 QQ 图片,来源:${JSON.stringify({ messageId: platformMessageId, speakerId, speakerName })}。` }, { type: 'image', attachment });
78
+ content.push(...shownImage(`下面是本次收到的第 ${index + 1} 张 QQ 图片,来源:${JSON.stringify({ messageId: platformMessageId, speakerId, speakerName })}。`, attachment));
61
79
  remainingBytes -= result.value.data.byteLength;
62
- saved++;
80
+ shown++;
63
81
  }
64
82
  catch { /* 一张无法保存时仍交付同批的其他图片和文字。 */ }
65
83
  }
84
+ content.push(...keptLines(`这次还有 ${kept.length} 张 QQ 图片存下了,没有直接附上:`, kept));
66
85
  if (saved < images.length)
67
86
  content.push(unavailable);
68
87
  return content;
@@ -4,7 +4,7 @@ import { dirname, join } from 'node:path';
4
4
  import { speakerIdentity, conversationKey } from "../shared/identity.js";
5
5
  import { createBridge } from "./snowluma.js";
6
6
  import { createQqHome } from "./home.js";
7
- import { admitQqImages, MAX_BATCH_IMAGES } from "./images.js";
7
+ import { admitQqImages, MAX_KEPT_IMAGES } from "./images.js";
8
8
  import { createBatcher } from "./batch.js";
9
9
  import { createOutbox } from "./outbox.js";
10
10
  import { planReply, stripHostNoise } from "./replies.js";
@@ -219,9 +219,9 @@ export function createQqNativeModule(deps) {
219
219
  pushContext(key, { id: messageId || String(Date.now()), occurredAt: new Date().toISOString(), text, identity }, config.contextMessages);
220
220
  return;
221
221
  }
222
- const imageFiles = (message.segments ?? []).filter(segment => segment?.type === 'image')
223
- .slice(0, MAX_BATCH_IMAGES + 1).map(segment => segment.data?.file);
224
- batches.add(key, { text, identity, who: speakerName, imageFiles });
222
+ const images = (message.segments ?? []).filter(segment => segment?.type === 'image')
223
+ .slice(0, MAX_KEPT_IMAGES + 1).map(segment => ({ file: segment.data?.file, url: segment.data?.url }));
224
+ batches.add(key, { text, identity, who: speakerName, images });
225
225
  }
226
226
  async function answer(key, gathered) {
227
227
  const config = settings();
@@ -234,7 +234,7 @@ export function createQqNativeModule(deps) {
234
234
  const text = gathered.length === 1 ? gathered[0].text : gathered.map(item => isGroup && item.who ? `${item.who}: ${item.text}` : item.text).join('\n');
235
235
  answering.add(key);
236
236
  try {
237
- const images = gathered.flatMap(item => item.imageFiles.map(file => ({ file, identity: item.identity })));
237
+ const images = gathered.flatMap(item => item.images.map(image => ({ ...image, identity: item.identity })));
238
238
  const reply = await home.ask(text, identity, contexts.get(key) ?? [], images.length
239
239
  ? () => admitQqImages(deps.ctx, images, client?.getImage?.bind(client))
240
240
  : undefined);
@@ -12,6 +12,7 @@ export declare function createChannelQueue(name: string, options: {
12
12
  limit: number;
13
13
  timeoutMs: number;
14
14
  timers?: LifeTimers | undefined;
15
+ timeoutMessage?: string;
15
16
  }): {
16
17
  run: <T>(work: (cancelled: Promise<never>) => Promise<T>) => Promise<T>;
17
18
  readonly stopped: boolean;
@@ -37,4 +38,6 @@ export declare function askTurn(ctx: Context, agent: Agent, words: UserMessage,
37
38
  context?: UserMessage | undefined;
38
39
  cancelled?: Promise<never> | undefined;
39
40
  answered?: ((source: UserMessage['source']) => boolean) | undefined;
41
+ steer?: boolean | undefined;
42
+ untilTaken?: boolean | undefined;
40
43
  }): Promise<string>;
@@ -26,7 +26,7 @@ export function createChannelQueue(name, options) {
26
26
  let cancel;
27
27
  const cancelled = new Promise((_resolve, reject) => {
28
28
  cancel = () => reject(stoppedError());
29
- timer = timers.set(() => reject(new Error(`${name} home reply timed out`)), options.timeoutMs);
29
+ timer = timers.set(() => reject(new Error(options.timeoutMessage ?? `${name} home reply timed out`)), options.timeoutMs);
30
30
  });
31
31
  cancelled.catch(() => { });
32
32
  cancellations.add(cancel);
@@ -77,8 +77,14 @@ export async function askTurn(ctx, agent, words, options) {
77
77
  if (event.type === 'turn/end')
78
78
  last = `end/${event.data.reason.kind}`;
79
79
  if (event.type === 'user/message') {
80
- if (event.data.id === words.id)
80
+ if (event.data.id === words.id) {
81
81
  admitted = true;
82
+ // The door only needed her to have the words; her answer leaves by its own tool, whenever she gives it.
83
+ if (options.untilTaken) {
84
+ finish();
85
+ return;
86
+ }
87
+ }
82
88
  else {
83
89
  last = `inject/${event.data.source.kind}`;
84
90
  if (admitted && event.data.source.kind === 'user') {
@@ -112,7 +118,11 @@ export async function askTurn(ctx, agent, words, options) {
112
118
  admit(options.inWorld, agent);
113
119
  if (options.context)
114
120
  agent.inject(options.context);
115
- agent.followup(words);
121
+ // Steered words are taken in at her next step, in the turn under way, as the partner's words in DSH are.
122
+ if (options.steer)
123
+ agent.steer(words);
124
+ else
125
+ agent.followup(words);
116
126
  await Promise.race([finished, cancelled]).catch((error) => { throw new Error(`${error.message}(${trace()})`, { cause: error }); });
117
127
  if (!admitted || failed || (cut && !reply.trim()))
118
128
  throw new Error(`${options.name} home request did not finish(${trace()})`);
@@ -15,6 +15,7 @@ export declare function createWechatHome(deps: {
15
15
  home(identity: SpeakerIdentity): Promise<Agent>;
16
16
  inWorld?: InWorld;
17
17
  timers?: LifeTimers;
18
+ midTurn?(agent: Agent): boolean;
18
19
  images?(identity: SpeakerIdentity, context: ChannelContextMessage[], reference?: ChannelReference): Promise<ContentBlock[]>;
19
20
  }): {
20
21
  ask: (text: string, suppliedIdentity: SpeakerIdentity, suppliedContext?: ChannelContextMessage[], suppliedMedia?: ChannelContextMessage[], suppliedReference?: ChannelReference) => Promise<{
@@ -22,13 +22,19 @@ export function createWechatHome(deps) {
22
22
  handedOver.catch(() => { });
23
23
  // Checked again after every wait: the session may leave her world meanwhile.
24
24
  const agent = admit(deps.inWorld, await Promise.race([deps.home(identity), handedOver]));
25
- await Promise.race([agent.whenIdle(), handedOver]);
25
+ const content = incomingText(text, reference);
26
+ // Her home takes words at her next step, the way the partner's words in DSH reach her while she is busy; any other
27
+ // session, and a picture with no words (context, not someone speaking), waits for the turn under way to end.
28
+ // Taken in, the request is done: the adapter reads the chat one message at a time, so waiting here for her answer
29
+ // (which leaves through wechat_send anyway) held back every later message, and timed out into a blocked chat.
30
+ const midTurn = Boolean(content) && deps.midTurn?.(agent) === true;
31
+ if (!midTurn)
32
+ await Promise.race([agent.whenIdle(), handedOver]);
26
33
  admit(deps.inWorld, agent);
27
34
  if ((reference?.image || [...context, ...media].some(message => message.image || message.reference?.image)) && !deps.images)
28
35
  throw new Error('Image admission is unavailable');
29
36
  const images = await Promise.race([deps.images?.(identity, [...context, ...media], reference) ?? Promise.resolve([]), handedOver]);
30
37
  queue.alive();
31
- const content = incomingText(text, reference);
32
38
  const source = content ? { kind: 'user', identity } : { kind: 'dsh-world-channel-context', form: 'recall' };
33
39
  const words = createUserMessage({ source, content: [{ type: 'text', text: identityPrefix(identity) + (content || '【收到微信图片】') }] });
34
40
  // How to reply lives in the WeChat tools' descriptions, which are sent once; only what this message brings along comes here.
@@ -42,7 +48,7 @@ export function createWechatHome(deps) {
42
48
  : undefined;
43
49
  // What she sends to this chat herself is her answer; the rest of her turn is not this request's to wait for.
44
50
  const answered = (sent) => sent.kind === 'dsh-world-wechat-outgoing' && sent.identity.conversationId === identity.conversationId;
45
- const reply = await askTurn(deps.ctx, agent, words, { name: 'WeChat', context: instructions, cancelled, inWorld: deps.inWorld, answered });
51
+ const reply = await askTurn(deps.ctx, agent, words, { name: 'WeChat', context: instructions, cancelled, inWorld: deps.inWorld, answered, steer: midTurn, untilTaken: midTurn });
46
52
  queue.alive();
47
53
  return { id: words.id, text: reply };
48
54
  });
@@ -1,6 +1,7 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { open, realpath } from 'node:fs/promises';
3
3
  import { join, relative } from 'node:path';
4
+ import { keepImages, shownImage } from "../../images.js";
4
5
  import { channelContext, channelMedia, channelReference } from "../shared/context.js";
5
6
  import { conversationKey } from "../shared/identity.js";
6
7
  export async function admitContextImages(ctx, incomingDir, identity, context, suppliedReference) {
@@ -41,9 +42,6 @@ export async function admitContextImages(ctx, incomingDir, identity, context, su
41
42
  await handle.close();
42
43
  }
43
44
  }
44
- const refs = await attachments.saveImages(inputs);
45
- return refs.flatMap((attachment, index) => [
46
- { type: 'text', text: `下面这张微信图片的来源:${JSON.stringify({ ...images[index].source, quality: images[index].image.quality })}。thumbnail 为缩略图,cached 为微信本机缓存版本,original 为原图档。${images[index].image.firstFrame ? '动图仅查看首帧,尚未查看完整动画。' : ''}` },
47
- { type: 'image', attachment },
48
- ]);
45
+ const refs = await keepImages(attachments, inputs);
46
+ return refs.flatMap((attachment, index) => shownImage(`下面这张微信图片的来源:${JSON.stringify({ ...images[index].source, quality: images[index].image.quality })}。thumbnail 为缩略图,cached 为微信本机缓存版本,original 为原图档。${images[index].image.firstFrame ? '动图仅查看首帧,尚未查看完整动画。' : ''}`, attachment));
49
47
  }
@@ -7,6 +7,7 @@ import { type InWorld } from '../../worldScope.ts';
7
7
  export declare function createWechatModule(deps: {
8
8
  ctx: Context;
9
9
  home(identity: SpeakerIdentity): Promise<Agent>;
10
+ midTurn?(agent: Agent): boolean;
10
11
  file: string;
11
12
  worldId?: string;
12
13
  inWorld?: InWorld;
@@ -20,7 +21,12 @@ export declare function createWechatModule(deps: {
20
21
  record?(reply: PreparedReply): Promise<string>;
21
22
  receipt?(receipt: WechatReceipt): Promise<void>;
22
23
  }): {
24
+ /** The quiet groups, for her alarm; groups with unread messages are news from elsewhere instead. */
23
25
  presence: (now: Date) => string | undefined;
26
+ elsewhere: {
27
+ look: string;
28
+ news: (now: Date) => import("../../elsewhere.ts").ElsewhereNews[];
29
+ };
24
30
  status(): {
25
31
  contextMessages: number;
26
32
  bufferSeconds: number;
@@ -180,7 +180,9 @@ export function createWechatModule(deps) {
180
180
  }
181
181
  }
182
182
  return {
183
- presence: (now) => !stopped && accountState === 'online' ? presence.summary(now) : undefined,
183
+ /** The quiet groups, for her alarm; groups with unread messages are news from elsewhere instead. */
184
+ presence: (now) => !stopped && accountState === 'online' ? presence.quiet(now) : undefined,
185
+ elsewhere: { look: '微信群用 wechat_history 看', news: now => !stopped && accountState === 'online' ? presence.unread(now) : [] },
184
186
  status() { return { ...sending.status(), lastContactAt: lastContactAt ?? null, account: accountState ?? null, ...settings() }; },
185
187
  start() {
186
188
  durableWrite(deps.file, JSON.stringify({ origin: deps.origin(), token, path, pid: process.pid, protocol: 4, references: true }, null, 2));
@@ -1,6 +1,10 @@
1
+ import { type ElsewhereNews } from '../../elsewhere.ts';
1
2
  /** Ephemeral host view of the adapter's durable, account-bound counters. */
2
3
  export declare function createWechatPresence(): {
3
4
  clear(): void;
4
5
  accept(value: unknown): void;
5
- summary(now: Date): string | undefined;
6
+ /** Groups with messages she has not read: news from elsewhere, told once per change. */
7
+ unread(now: Date): ElsewhereNews[];
8
+ /** How long the groups with nothing unread have been quiet, for her alarm: whether to speak up there is hers. */
9
+ quiet(now: Date): string | undefined;
6
10
  };
@@ -1,13 +1,16 @@
1
+ import { ago } from "../../elsewhere.js";
1
2
  const clean = (value) => [...value.replace(/[\r\n\u0000-\u001f「」]/g, ' ')].slice(0, 40).join('');
2
3
  // The adapter permits a 60-second poll; leave one poll and a control-request margin before expiry.
3
4
  const PRESENCE_TTL_MS = 2 * 60_000 + 5_000;
4
- function ago(milliseconds) {
5
- const minutes = Math.max(0, Math.floor(milliseconds / 60_000));
6
- return minutes < 1 ? '刚刚' : minutes < 60 ? `${minutes} 分钟前` : minutes < 1440 ? `${Math.floor(minutes / 60)} 小时前` : `${Math.floor(minutes / 1440)} 天前`;
7
- }
8
5
  /** Ephemeral host view of the adapter's durable, account-bound counters. */
9
6
  export function createWechatPresence() {
10
7
  let groups = [], observedAt = 0;
8
+ /** What the adapter last reported, while it is recent enough to stand for now; stale data cannot assert a quiet group. */
9
+ const current = (now) => {
10
+ const age = now.getTime() - observedAt;
11
+ return age < -60_000 || age > PRESENCE_TTL_MS ? [] : groups;
12
+ };
13
+ const speaker = (row) => row.lastSelf ? '我自己' : row.lastSpeaker;
11
14
  return {
12
15
  clear() { groups = []; observedAt = 0; },
13
16
  accept(value) {
@@ -38,18 +41,24 @@ export function createWechatPresence() {
38
41
  groups = next;
39
42
  observedAt = at;
40
43
  },
41
- summary(now) {
42
- const age = now.getTime() - observedAt;
43
- if (!groups.length || age < -60_000 || age > PRESENCE_TTL_MS)
44
+ /** Groups with messages she has not read: news from elsewhere, told once per change. */
45
+ unread(now) {
46
+ return current(now).filter(row => row.count > 0).sort((a, b) => b.lastAt - a.lastAt).map(row => ({
47
+ key: `wechat:${row.chatId}`, mark: `${row.count}:${row.lastAt}`,
48
+ text: `微信群「${row.name}」${row.count > 99 ? '99+' : row.count} 条我还没看到,最后是${speaker(row)}(${ago(now.getTime() - row.lastAt)})`,
49
+ }));
50
+ },
51
+ /** How long the groups with nothing unread have been quiet, for her alarm: whether to speak up there is hers. */
52
+ quiet(now) {
53
+ const rows = current(now).filter(row => !row.count);
54
+ if (!rows.length)
44
55
  return;
45
- const entries = [...groups].sort((a, b) => Number(b.count > 0) - Number(a.count > 0) || b.lastAt - a.lastAt).slice(0, 8).map(row => {
46
- const speaker = row.lastSelf ? '我自己' : row.lastSpeaker;
56
+ const entries = [...rows].sort((a, b) => b.lastAt - a.lastAt).slice(0, 8).map(row => {
47
57
  const elapsed = ago(now.getTime() - row.lastAt);
48
- return row.count ? `「${row.name}」${row.count > 99 ? '99+' : row.count} 条未读,最后是${speaker}(${elapsed})`
49
- : `「${row.name}」${elapsed.endsWith('前') ? elapsed.slice(0, -1) + '没人说话' : '刚刚有发言'},最后是${speaker}`;
58
+ return `「${row.name}」${elapsed.endsWith('前') ? elapsed.slice(0, -1) + '没人说话' : '刚刚有发言'},最后是${speaker(row)}`;
50
59
  });
51
- if (groups.length > entries.length)
52
- entries.push(`另有 ${groups.length - entries.length} 个群`);
60
+ if (rows.length > entries.length)
61
+ entries.push(`另有 ${rows.length - entries.length} 个群`);
53
62
  return '微信:' + entries.join(';');
54
63
  },
55
64
  };
@@ -1,4 +1,4 @@
1
- import { type ContentBlock } from '@deepseek-ai/dsh-llm';
1
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm';
2
2
  import { type ToolRunContext } from '@deepseek-ai/dsh-tools';
3
3
  import { type ChannelContextMessage } from '../shared/context.ts';
4
4
  import type { SpeakerIdentity } from '../shared/identity.ts';
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomBytes } from 'node:crypto';
2
- import { createUserMessage } from '@deepseek-ai/dsh-llm';
2
+ import { imagesMessage } from "../../images.js";
3
3
  import { defineTool } from '@deepseek-ai/dsh-tools';
4
4
  import { channelContext } from "../shared/context.js";
5
5
  import { wechatTextOutput } from "./messages.js";
@@ -71,7 +71,7 @@ export function createWechatReading(deps) {
71
71
  context.signal.throwIfAborted();
72
72
  if (!blocks.some(block => block.type === 'image'))
73
73
  throw new Error('图片没能交给模型');
74
- context.deferContext(createUserMessage({ source: { kind: 'dsh-world-channel-context', form: 'recall' }, content: blocks }));
74
+ context.deferContext(imagesMessage(blocks));
75
75
  return `图片 ${entry.id}(${entry.identity.speakerName ?? entry.identity.speakerId},${entry.occurredAt})已附在下一条消息里。`;
76
76
  },
77
77
  }),
@@ -0,0 +1,34 @@
1
+ import type { Session } from '@deepseek-ai/dsh-session';
2
+ /** One place she is not, as facts only (how many she has not seen, who spoke last, how long ago), never the words. */
3
+ export interface ElsewhereNews {
4
+ /** Stable for the place, e.g. `wechat:family@chatroom`. */
5
+ key: string;
6
+ /** Changes whenever there is something new there; the same mark is not told twice. */
7
+ mark: string;
8
+ text: string;
9
+ }
10
+ /** Where news comes from: a WeChat account's groups, her other sessions, later other platforms. */
11
+ export interface ElsewhereSource {
12
+ /** How she looks for herself, e.g. `微信群用 wechat_history 看`. */
13
+ look: string;
14
+ /** What is pending for this session now; a place with nothing she has not seen is left out. */
15
+ news(now: Date, session: Session): ElsewhereNews[];
16
+ }
17
+ export declare function ago(milliseconds: number): string;
18
+ export declare const ELSEWHERE_HEADER = "\u3010\u522B\u5904\u7684\u52A8\u9759\u3011\u6211\u4E0D\u5728\u573A\u7684\u5730\u65B9\u6512\u4E0B\u7684\u6D88\u606F\uFF0C\u53EA\u662F\u8BA9\u6211\u77E5\u9053\uFF0C\u4E0D\u662F\u5BF9\u6211\u8BF4\u7684\u8BDD\uFF1B\u8981\u4E0D\u8981\u770B\u3001\u8981\u4E0D\u8981\u56DE\uFF0C\u6211\u81EA\u5DF1\u51B3\u5B9A\u3002";
19
+ export declare function renderElsewhere(groups: readonly {
20
+ look: string;
21
+ news: readonly ElsewhereNews[];
22
+ }[]): string;
23
+ /**
24
+ * The one way news from elsewhere reaches her: sources register here, and each session is told what is new since it
25
+ * was last told. An event is told once; whatever she has not looked at comes back with the next news from that place.
26
+ */
27
+ export declare function createElsewhere(): {
28
+ register(source: ElsewhereSource): () => void;
29
+ /** The block of what is new for this session, marking it told; undefined when nothing is. */
30
+ take(session: Session, now: Date): string | undefined;
31
+ /** What she was told is gone from her view (compaction, leaving her world): tell again what is still pending. */
32
+ retell(session: Session): void;
33
+ };
34
+ export type Elsewhere = ReturnType<typeof createElsewhere>;