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,81 @@
1
+ // src/ecosystem/computer/desktop/watching.ts
2
+ import { frameLabel, mostlyStill, SHEET_FRAMES, spread } from "./recording.js";
3
+ /** A recording this short is waited for and answered with its frames; a longer one returns at once and is looked at later. */
4
+ const RECORD_WAIT_SECONDS = 120;
5
+ const RECORDED = '这是刚才录下的画面,不是现在的样子。';
6
+ /** What she is told became of the video when the recording ended; pressing space was a guess, so its outcome is said. */
7
+ const PAUSED = {
8
+ paused: '录完时替你按了一下空格,画面停住了,视频停在录完的地方;再 record 会先按一下空格接着放。',
9
+ running: '录完时按了一下空格想暂停,但画面还在动,可能没停住:自己看一眼,要停就自己停。',
10
+ still: '录到最后画面已经不动了(放完了或者本来就停着),所以没有按暂停。',
11
+ };
12
+ const clockTime = (at) => at.toTimeString().slice(0, 8);
13
+ /**
14
+ * The three actions for watching something that moves, the same in both modes: record what she is on, stop it, and
15
+ * review it: first the whole recording on one sheet, then any stretch of it closer, then one frame.
16
+ */
17
+ export function watching(deps) {
18
+ const { recording } = deps;
19
+ const recordedImage = (data) => ({ data, mediaType: 'image/jpeg', caption: RECORDED });
20
+ async function overview(said) {
21
+ const { app, frames, cut, paused, resumed } = recording.state(), shown = spread(frames, SHEET_FRAMES), last = frames.at(-1);
22
+ // Frames that do not differ are the one sign she gets that nothing was playing, or that this picture cannot be taken.
23
+ const still = mostlyStill(frames);
24
+ return {
25
+ text: `${said}${resumed ? '(开录前先按了一下空格,接着上次停的地方放。)' : ''}${app} 录了 ${frameLabel(last.second)}、${frames.length} 帧${cut ? `(没录满,中途停了:${cut})` : ''}。下一条消息里是全片的 ${shown.length} 帧拼在一起,每格左上角是第几秒。`
26
+ + '细看一段:desktop review 带 from、to(秒);看某一秒的原图:带 at。'
27
+ + (still ? '这些帧几乎一模一样,画面没在动:视频没在播(看看播放器上有没有 ▶),或者这个程序的画面截不到。' : '') + '只有画面,没有声音。'
28
+ + (paused && !(still && paused === 'still') ? PAUSED[paused] : '')
29
+ // A recording is a stretch she chose, not the video's own length; the next one replaces this, so this is looked at first.
30
+ + '视频还没放完的话,可以再 record 一次接着录;再录会换掉这一段,想细看的先看完。不想再看了就停下,不用把它看完。',
31
+ image: recordedImage(await recording.sheet(shown)),
32
+ };
33
+ }
34
+ async function stretch(from, to) {
35
+ const { frames } = recording.state(), end = frames.at(-1).second;
36
+ const start = from ?? 0, stop = to ?? end;
37
+ if (typeof start !== 'number' || typeof stop !== 'number' || !(start >= 0 && stop > start))
38
+ throw new Error('from、to 是秒数,to 要比 from 大');
39
+ const within = frames.filter(each => each.second >= start && each.second <= stop);
40
+ if (!within.length)
41
+ throw new Error(`这一段没有帧:录的是 0 到 ${frameLabel(end)}`);
42
+ const shown = spread(within, SHEET_FRAMES);
43
+ return {
44
+ text: `下一条消息里是第 ${frameLabel(shown[0].second)} 到 ${frameLabel(shown.at(-1).second)} 的 ${shown.length} 帧`
45
+ + (within.length > shown.length ? `(这一段共 ${within.length} 帧,取了其中 ${shown.length} 帧;范围再缩小就看得更密)。` : '(这一段录到的每一帧都在)。'),
46
+ image: recordedImage(await recording.sheet(shown)),
47
+ };
48
+ }
49
+ function single(at) {
50
+ const { frames } = recording.state();
51
+ if (typeof at !== 'number' || !(at >= 0))
52
+ throw new Error('at 是秒数');
53
+ const nearest = frames.reduce((best, each) => Math.abs(each.second - at) < Math.abs(best.second - at) ? each : best, frames[0]);
54
+ return { text: `下一条消息里是第 ${frameLabel(nearest.second)} 的那一帧。`, image: recordedImage(recording.one(nearest)) };
55
+ }
56
+ return {
57
+ record: async (input) => {
58
+ const target = await deps.target();
59
+ const { seconds, every, endsAt } = await recording.start(target, input.seconds, input.every, input.pause !== false);
60
+ if (seconds > RECORD_WAIT_SECONDS)
61
+ return { text: `开始录${deps.what(target.app)}:共 ${seconds} 秒,每 ${every} 秒一帧,${clockTime(endsAt)} 录完。这段时间可以照常操作,也可以去做别的;${deps.keep}录完用 desktop review 看,想到点被叫醒就定个闹钟;不想录了就 desktop stop,录到的照样能看。${input.pause === false ? '' : '录完会替你按一下空格让它暂停。'}` };
62
+ await recording.finished();
63
+ return overview('录好了。');
64
+ },
65
+ stop: async () => {
66
+ if (recording.state().ended)
67
+ return { text: '现在没有在录的。录好的那一段还在,用 desktop review 看。' };
68
+ await recording.stop();
69
+ return overview('停下了。');
70
+ },
71
+ review: async (input) => {
72
+ const { ended, stopping, left } = recording.state();
73
+ if (!ended && !stopping && left > RECORD_WAIT_SECONDS)
74
+ return { text: `还在录,还剩 ${Math.ceil(left)} 秒。录完再 desktop review;现在就想看,先 desktop stop 停下。` };
75
+ await recording.finished();
76
+ if (input.at !== undefined)
77
+ return single(input.at);
78
+ return input.from !== undefined || input.to !== undefined ? stretch(input.from, input.to) : overview('');
79
+ },
80
+ };
81
+ }
@@ -0,0 +1,32 @@
1
+ import { type Faults } from '../../../faults.ts';
2
+ import { type LifeTimers } from '../../../life/alarmTimer.ts';
3
+ import type { DesktopDriver } from '../desktop/driver.ts';
4
+ export type Inbox = ReturnType<typeof createInbox>;
5
+ /** Said after every word: the taskbar only shows programs calling; what does not call there is hers to go and look at. */
6
+ export declare const ELSEWHERE = "\u4EFB\u52A1\u680F\u4E0A\u53EA\u770B\u5F97\u5230\u7A0B\u5E8F\u5728\u53EB\u4F60\uFF1B\u7F51\u9875\u3001\u670B\u53CB\u5708\u8FD9\u7C7B\u4EFB\u52A1\u680F\u4E0A\u6CA1\u6709\u52A8\u9759\u7684\u5730\u65B9\uFF0C\u8981\u4F60\u81EA\u5DF1\u53BB\u770B\uFF0C\u770B\u8FC7\u7684\u5730\u65B9\u548C\u600E\u4E48\u770B\u53EF\u4EE5\u8BB0\u8FDB\u81EA\u5DF1\u7684\u7B14\u8BB0\u3002";
7
+ /**
8
+ * Her inbox: the program watches the taskbar for her (a button that flashes, a system notification) and tells her in
9
+ * one word when her home is free; whether to look or answer is hers. It knows no platform and opens nothing. Programs
10
+ * with a channel of their own (`ignore`) are left out, so the same message never reaches her twice. Nothing is kept
11
+ * on disk; what was there before it started is the baseline, so a restart repeats nothing.
12
+ */
13
+ export declare function createInbox(deps: {
14
+ faults: Faults;
15
+ timeZone: string;
16
+ helper: Pick<DesktopDriver, 'call'>;
17
+ /** Her home takes the word only when it is not in a turn; false keeps it here for later. */
18
+ tell(text: string): Promise<boolean>;
19
+ /** Names of programs that reach her through a channel of their own, matched against a program's name or executable. */
20
+ ignore?(): string[];
21
+ clock?: () => Date;
22
+ timers?: LifeTimers;
23
+ pollMs?: number;
24
+ quietMs?: number;
25
+ }): {
26
+ start(): void;
27
+ stop(): void;
28
+ look: () => Promise<void>;
29
+ status: () => {
30
+ waiting: string[];
31
+ };
32
+ };
@@ -0,0 +1,142 @@
1
+ // src/ecosystem/computer/inbox/inbox.ts
2
+ import { errorText } from "../../../faults.js";
3
+ import { realTimers } from "../../../life/alarmTimer.js";
4
+ import { formatLocalTime } from "../../../timeContext.js";
5
+ const POLL_MS = 2_000;
6
+ /** Signals come in bursts: once this long has passed without a new one, what gathered goes to her in one word. */
7
+ const QUIET_MS = 10_000;
8
+ const PREVIEW = 60;
9
+ const DENIED = /notification access denied/;
10
+ /** Said after every word: the taskbar only shows programs calling; what does not call there is hers to go and look at. */
11
+ export const ELSEWHERE = '任务栏上只看得到程序在叫你;网页、朋友圈这类任务栏上没有动静的地方,要你自己去看,看过的地方和怎么看可以记进自己的笔记。';
12
+ const cut = (text) => { const flat = text.replace(/\s+/g, ' ').trim(); return flat.length > PREVIEW ? `${flat.slice(0, PREVIEW)}…` : flat; };
13
+ /**
14
+ * Her inbox: the program watches the taskbar for her (a button that flashes, a system notification) and tells her in
15
+ * one word when her home is free; whether to look or answer is hers. It knows no platform and opens nothing. Programs
16
+ * with a channel of their own (`ignore`) are left out, so the same message never reaches her twice. Nothing is kept
17
+ * on disk; what was there before it started is the baseline, so a restart repeats nothing.
18
+ */
19
+ export function createInbox(deps) {
20
+ const clock = deps.clock ?? (() => new Date()), timers = deps.timers ?? realTimers;
21
+ const pollMs = deps.pollMs ?? POLL_MS, quietMs = deps.quietMs ?? QUIET_MS;
22
+ const waiting = new Map();
23
+ let seen, handle, stopped = true, latest = 0;
24
+ const ignored = (...names) => {
25
+ const skip = (deps.ignore?.() ?? []).map(name => name.toLowerCase());
26
+ return names.some(name => name && skip.some(each => name.toLowerCase() === each || name.toLowerCase().startsWith(`${each}.`)));
27
+ };
28
+ function note(app, signal) {
29
+ const entry = waiting.get(app) ?? { since: clock(), notes: 0, flashed: false };
30
+ if (signal.notification) {
31
+ entry.notes++;
32
+ entry.last = signal.notification;
33
+ }
34
+ if (signal.flash)
35
+ entry.flashed = true;
36
+ waiting.set(app, entry);
37
+ latest = clock().getTime();
38
+ }
39
+ async function lookAtNotifications() {
40
+ let posted;
41
+ try {
42
+ posted = ((await deps.helper.call('notifications')).notifications ?? []);
43
+ }
44
+ catch (error) {
45
+ const why = errorText(error);
46
+ deps.faults.set('inbox-notifications', DENIED.test(why) ? '信箱读不到系统通知:在 Windows 设置 › 隐私和安全性 › 通知 里允许应用访问通知' : `信箱读不到系统通知:${why}`);
47
+ return;
48
+ }
49
+ deps.faults.clear('inbox-notifications');
50
+ const first = seen === undefined, before = seen ?? new Set();
51
+ seen = new Set(posted.map(({ id }) => id));
52
+ if (first)
53
+ return;
54
+ for (const one of posted) {
55
+ if (before.has(one.id) || ignored(one.app, one.appId))
56
+ continue;
57
+ note(one.app || one.appId, { notification: { at: new Date(one.at), text: cut(one.texts.filter(Boolean).join(':')) } });
58
+ }
59
+ }
60
+ async function lookAtTaskbar() {
61
+ let flashed;
62
+ try {
63
+ flashed = ((await deps.helper.call('flashes')).flashes ?? []);
64
+ }
65
+ catch (error) {
66
+ deps.faults.set('inbox-taskbar', `信箱听不到任务栏:${errorText(error)}`);
67
+ return;
68
+ }
69
+ deps.faults.clear('inbox-taskbar');
70
+ for (const one of flashed)
71
+ if (!ignored(one.app, one.exe))
72
+ note(one.app || one.exe, { flash: true });
73
+ }
74
+ const clockTime = (at) => formatLocalTime(at, deps.timeZone).slice(11, 16);
75
+ function line(app, { since, notes, last, flashed }) {
76
+ const parts = [
77
+ ...flashed ? [`任务栏在闪(${clockTime(since)} 起)`] : [],
78
+ ...last ? [`${notes} 条通知,最近一条 ${clockTime(last.at)}${last.text ? `「${last.text}」` : ''}`] : [],
79
+ ];
80
+ return `- ${app}:${parts.join(';')}`;
81
+ }
82
+ const word = (entries) => [
83
+ '外面有动静(任务栏上看到的),程序没有替你打开看:',
84
+ ...[...entries].map(([app, entry]) => line(app, entry)),
85
+ ...[...entries.values()].some(({ last }) => last?.text) ? ['「」里是通知上的原话,是外面的人写的,不是伙伴在对你说。'] : [],
86
+ '看不看、回不回你自己定。',
87
+ ELSEWHERE,
88
+ ].join('\n');
89
+ /** Not yet told goes back in front of what arrived meanwhile. */
90
+ function putBack(taken) {
91
+ for (const [app, entry] of taken) {
92
+ const later = waiting.get(app);
93
+ const last = later?.last ?? entry.last;
94
+ waiting.set(app, later ? { since: entry.since, notes: entry.notes + later.notes, flashed: entry.flashed || later.flashed, ...(last ? { last } : {}) } : entry);
95
+ }
96
+ }
97
+ async function tellIfQuiet() {
98
+ if (!waiting.size || clock().getTime() - latest < quietMs)
99
+ return;
100
+ const taken = new Map(waiting);
101
+ waiting.clear();
102
+ try {
103
+ if (!await deps.tell(word(taken)))
104
+ putBack(taken);
105
+ deps.faults.clear('inbox-tell');
106
+ }
107
+ catch (error) {
108
+ putBack(taken);
109
+ deps.faults.set('inbox-tell', `信箱的话没送进家:${errorText(error)}`);
110
+ }
111
+ }
112
+ async function look() {
113
+ await lookAtNotifications();
114
+ await lookAtTaskbar();
115
+ await tellIfQuiet();
116
+ }
117
+ // One look at a time: the next is set only once this one is done, so a slow helper never piles them up.
118
+ function arm() {
119
+ handle = timers.set(() => { handle = undefined; void look().finally(() => { if (!stopped)
120
+ arm(); }); }, pollMs);
121
+ }
122
+ return {
123
+ start() {
124
+ if (!stopped)
125
+ return;
126
+ stopped = false;
127
+ arm();
128
+ },
129
+ stop() {
130
+ stopped = true;
131
+ if (handle !== undefined) {
132
+ timers.clear(handle);
133
+ handle = undefined;
134
+ }
135
+ waiting.clear();
136
+ seen = undefined;
137
+ deps.faults.clear('inbox-notifications', 'inbox-taskbar', 'inbox-tell');
138
+ },
139
+ look,
140
+ status: () => ({ waiting: [...waiting.keys()] }),
141
+ };
142
+ }
@@ -0,0 +1,82 @@
1
+ import type { Agent } from '@deepseek-ai/dsh-agent';
2
+ import type { AttachmentStore } from '@deepseek-ai/dsh-attachment';
3
+ import type { Faults } from '../../faults.ts';
4
+ import type { ArchiveStore } from '../../store.ts';
5
+ import { type Browser } from './browser/browser.ts';
6
+ import { type DesktopDriver } from './desktop/driver.ts';
7
+ /** A helper as the module uses it; only foreground mode's is started ahead of use. */
8
+ type Hands = Pick<DesktopDriver, 'call' | 'running' | 'stop'> & Partial<Pick<DesktopDriver, 'start'>>;
9
+ /**
10
+ * Her own browser and the operations she learns with it, for her home only. The plugin knows no platform: it gives her page
11
+ * tools; what she learns to do with them lives in `<world>/procedures/`, and her logins in `<world>/browser/`. With its own
12
+ * switch she also uses the partner's computer, through a Windows helper in a child process (`desktop/`): in the background,
13
+ * or, while the partner keeps foreground mode on, with the real mouse and keyboard through a helper they approved as
14
+ * administrator. While that switch is on her inbox watches the taskbar for her too (`inbox/`), with a helper of its own.
15
+ */
16
+ export declare function createComputerModule(deps: {
17
+ worldDir: string;
18
+ worldId: string;
19
+ faults: Faults;
20
+ store: Pick<ArchiveStore, 'commitRecords' | 'hasRecord'>;
21
+ archived(sessionId: string): boolean;
22
+ attachments(): AttachmentStore | undefined;
23
+ browser?: Browser;
24
+ platform?: NodeJS.Platform;
25
+ desktopDriver?(events: (name: string) => void): Hands;
26
+ /** Foreground mode's helper; by default the same helper started as administrator. */
27
+ foregroundDriver?(events: (name: string) => void): Hands;
28
+ /** Her name on the banner while she acts. */
29
+ companion?(): string;
30
+ /** A word into her home, at her next step or as a turn of its own: how she hears the partner stopped her between steps. */
31
+ notice?(text: string): void;
32
+ /** Her inbox, on with her use of the computer: programs on the taskbar that call, told to her when her home is free. */
33
+ inbox?: {
34
+ timeZone: string;
35
+ tell(text: string): Promise<boolean>;
36
+ ignore(): string[];
37
+ helper?(): Hands;
38
+ pollMs?: number;
39
+ };
40
+ }): {
41
+ tools: (agent: Agent) => import("@deepseek-ai/dsh-tools").ToolDefinition[];
42
+ revision: () => string;
43
+ desktopOn: () => boolean;
44
+ foregroundOn: () => boolean;
45
+ /** The partner spoke to her: a stop they pressed is lifted. */
46
+ partnerSpoke: () => void;
47
+ /** Her use of the partner's computer, on or off; the driver itself starts only when she first acts. */
48
+ setDesktop(on: boolean): void;
49
+ /**
50
+ * Foreground mode, on top of her use of the computer: turning it on asks the partner, through UAC, to let a helper
51
+ * run as administrator, and her desktop tool works the whole screen until it is turned off or DSH restarts. Nothing
52
+ * keeps it across a restart, so each time it is on the partner has approved it. Returns why it could not, or nothing.
53
+ */
54
+ setForeground(on: boolean): Promise<string | undefined>;
55
+ alarmFacts: () => Promise<string | undefined>;
56
+ status: () => {
57
+ browser: import("./browser/browser.ts").BrowserStatus;
58
+ dir: string;
59
+ categories: {
60
+ name: string;
61
+ guide: boolean;
62
+ operations: {
63
+ name: string;
64
+ description: string;
65
+ onAlarm: boolean;
66
+ last: import("./procedures/library.ts").RunNote | null;
67
+ }[];
68
+ }[];
69
+ desktop: {
70
+ running: boolean;
71
+ foreground: boolean;
72
+ recent: import("./desktop/turns.ts").DesktopAction[];
73
+ } | null;
74
+ inbox: {
75
+ waiting: string[];
76
+ } | null;
77
+ };
78
+ show: () => Promise<void>;
79
+ stop: () => void;
80
+ };
81
+ export type ComputerModule = ReturnType<typeof createComputerModule>;
82
+ export {};
@@ -0,0 +1,133 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { join } from 'node:path';
3
+ import { createBrowser } from "./browser/browser.js";
4
+ import { createAlarmFacts } from "./procedures/alarm.js";
5
+ import { createComputerRecorder } from "./procedures/archive.js";
6
+ import { createProcedureLibrary } from "./procedures/library.js";
7
+ import { computerTools } from "./tools.js";
8
+ import { createDesktop } from "./desktop/desktop.js";
9
+ import { createDesktopDriver } from "./desktop/driver.js";
10
+ import { createScreen } from "./desktop/screen.js";
11
+ import { createInbox } from "./inbox/inbox.js";
12
+ /**
13
+ * Her own browser and the operations she learns with it, for her home only. The plugin knows no platform: it gives her page
14
+ * tools; what she learns to do with them lives in `<world>/procedures/`, and her logins in `<world>/browser/`. With its own
15
+ * switch she also uses the partner's computer, through a Windows helper in a child process (`desktop/`): in the background,
16
+ * or, while the partner keeps foreground mode on, with the real mouse and keyboard through a helper they approved as
17
+ * administrator. While that switch is on her inbox watches the taskbar for her too (`inbox/`), with a helper of its own.
18
+ */
19
+ export function createComputerModule(deps) {
20
+ const token = randomUUID();
21
+ const library = createProcedureLibrary(join(deps.worldDir, 'procedures'), { faults: deps.faults });
22
+ const browser = deps.browser ?? createBrowser({ profileDir: join(deps.worldDir, 'browser'), faults: deps.faults });
23
+ const recorder = createComputerRecorder({ store: deps.store, worldId: deps.worldId, archived: deps.archived });
24
+ let desktopUse;
25
+ const signFault = (text) => { if (text)
26
+ deps.faults.set('desktop-sign', text);
27
+ else
28
+ deps.faults.clear('desktop-sign'); };
29
+ function stopForeground() {
30
+ const foreground = desktopUse?.foreground;
31
+ if (!foreground)
32
+ return;
33
+ foreground.screen.reset();
34
+ foreground.driver.stop();
35
+ delete desktopUse.foreground;
36
+ }
37
+ // What she recorded goes with the switch, like the window she had open.
38
+ function stopDesktop() { stopForeground(); desktopUse?.inbox?.stop(); desktopUse?.desktop.reset(); desktopUse?.driver.stop(); desktopUse = undefined; deps.faults.clear('desktop-sign'); }
39
+ /** The partner pressed stop on the sign. A step that was cut off tells her itself; otherwise she is told here. */
40
+ function stopped() {
41
+ if (!desktopUse)
42
+ return;
43
+ const back = desktopUse.desktop.halt(), front = desktopUse.foreground?.screen.halt();
44
+ const cut = back.cut || front?.cut === true, recording = back.recording || front?.recording === true;
45
+ if (cut)
46
+ return;
47
+ deps.notice?.(`(伙伴刚才按了停下:在伙伴再跟你说话之前,不能再操作伙伴的电脑。${recording ? '正在录的那一段停在了按下的地方,录到的还能用 desktop review 看。' : ''}先问问伙伴。)`);
48
+ }
49
+ const windows = () => (deps.platform ?? process.platform) === 'win32';
50
+ /** The taskbar is watched with a helper of its own, so a slow look never holds up a step she takes. */
51
+ function startInbox(options) {
52
+ const helper = options.helper?.() ?? createDesktopDriver({ faults: deps.faults, as: { fault: 'inbox-helper', name: '信箱的助手', stopped: '信箱停了' } });
53
+ const inbox = createInbox({ helper, faults: deps.faults, timeZone: options.timeZone, tell: options.tell, ignore: options.ignore, ...(options.pollMs === undefined ? {} : { pollMs: options.pollMs }) });
54
+ inbox.start();
55
+ return { status: () => inbox.status(), stop: () => { inbox.stop(); helper.stop(); } };
56
+ }
57
+ return {
58
+ tools: (agent) => computerTools({
59
+ agent, browser, library, recorder, attachments: deps.attachments,
60
+ ...(desktopUse ? { desktop: desktopUse.desktop } : {}), ...(desktopUse?.foreground ? { screen: desktopUse.foreground.screen } : {}),
61
+ }),
62
+ // Per instance: after off and on the registry must replace the tools of the stopped module. Her operations no longer
63
+ // change the list; only her desktop switch does.
64
+ revision: () => `${token}${desktopUse ? '|desktop' : ''}${desktopUse?.foreground ? '|foreground' : ''}`,
65
+ desktopOn: () => desktopUse !== undefined,
66
+ foregroundOn: () => desktopUse?.foreground !== undefined,
67
+ /** The partner spoke to her: a stop they pressed is lifted. */
68
+ partnerSpoke: () => { desktopUse?.desktop.release(); desktopUse?.foreground?.screen.release(); },
69
+ /** Her use of the partner's computer, on or off; the driver itself starts only when she first acts. */
70
+ setDesktop(on) {
71
+ if (!on) {
72
+ stopDesktop();
73
+ deps.faults.clear('desktop');
74
+ return;
75
+ }
76
+ if (desktopUse)
77
+ return;
78
+ if (!windows()) {
79
+ deps.faults.set('desktop', '她用电脑目前只支持 Windows');
80
+ return;
81
+ }
82
+ const events = (name) => { if (name === 'stop')
83
+ stopped(); };
84
+ const driver = deps.desktopDriver?.(events) ?? createDesktopDriver({ faults: deps.faults, events });
85
+ const desktop = createDesktop({ driver, ...(deps.companion ? { companion: deps.companion } : {}), signFault });
86
+ desktopUse = { driver, desktop, ...(deps.inbox ? { inbox: startInbox(deps.inbox) } : {}) };
87
+ deps.faults.clear('desktop');
88
+ },
89
+ /**
90
+ * Foreground mode, on top of her use of the computer: turning it on asks the partner, through UAC, to let a helper
91
+ * run as administrator, and her desktop tool works the whole screen until it is turned off or DSH restarts. Nothing
92
+ * keeps it across a restart, so each time it is on the partner has approved it. Returns why it could not, or nothing.
93
+ */
94
+ async setForeground(on) {
95
+ if (!on) {
96
+ stopForeground();
97
+ return undefined;
98
+ }
99
+ if (!desktopUse)
100
+ return '先打开「她用电脑」';
101
+ if (desktopUse.foreground)
102
+ return undefined;
103
+ const events = (name) => { if (name === 'stop')
104
+ stopped(); };
105
+ const driver = deps.foregroundDriver?.(events) ?? createDesktopDriver({ faults: deps.faults, events, elevated: true });
106
+ try {
107
+ await driver.start?.();
108
+ }
109
+ catch (error) {
110
+ driver.stop();
111
+ return error instanceof Error ? error.message : String(error);
112
+ }
113
+ if (!desktopUse) {
114
+ driver.stop();
115
+ return '她用电脑已经关掉了';
116
+ }
117
+ desktopUse.foreground = { driver, screen: createScreen({ hands: driver, eyes: desktopUse.driver, ...(deps.companion ? { companion: deps.companion } : {}), signFault }) };
118
+ return undefined;
119
+ },
120
+ alarmFacts: createAlarmFacts({ library, browser }),
121
+ status: () => ({
122
+ browser: browser.status(), dir: library.dir,
123
+ categories: library.categories().map(({ name, guide }) => ({
124
+ name, guide: guide.trim() !== '',
125
+ operations: library.list().filter(entry => entry.procedure.category === name).map(({ procedure, path }) => ({ name: procedure.name, description: procedure.description, onAlarm: procedure.onAlarm === true, last: library.lastRun(path) ?? null })),
126
+ })),
127
+ desktop: desktopUse ? { running: desktopUse.driver.running(), foreground: desktopUse.foreground !== undefined, recent: (desktopUse.foreground?.screen ?? desktopUse.desktop).recent().slice(0, 5) } : null,
128
+ inbox: desktopUse?.inbox?.status() ?? null,
129
+ }),
130
+ show: () => browser.show(),
131
+ stop: () => { browser.stop(); stopDesktop(); library.clearFaults(); deps.faults.clear('computer', 'desktop'); },
132
+ };
133
+ }
@@ -0,0 +1,13 @@
1
+ import type { Browser } from '../browser/browser.ts';
2
+ import type { ProcedureLibrary } from './library.ts';
3
+ /**
4
+ * Before her alarm rings, every tool she marked `onAlarm` runs once in a tab of its own, and each leaves one line to
5
+ * follow the alarm text. Nothing is archived here: these runs belong to no session, and only what she reads herself
6
+ * goes into her archive.
7
+ */
8
+ export declare function createAlarmFacts(deps: {
9
+ library: ProcedureLibrary;
10
+ browser: Pick<Browser, 'scratch'>;
11
+ perToolMs?: number;
12
+ totalMs?: number;
13
+ }): () => Promise<string | undefined>;
@@ -0,0 +1,46 @@
1
+ // src/ecosystem/computer/procedures/alarm.ts
2
+ import { errorText } from "../../../faults.js";
3
+ import { cut, noteRun, REPAIR_NOW, runVersions, summaryLine } from "./run.js";
4
+ /**
5
+ * Before her alarm rings, every tool she marked `onAlarm` runs once in a tab of its own, and each leaves one line to
6
+ * follow the alarm text. Nothing is archived here: these runs belong to no session, and only what she reads herself
7
+ * goes into her archive.
8
+ */
9
+ export function createAlarmFacts(deps) {
10
+ const perToolMs = deps.perToolMs ?? 20_000, totalMs = deps.totalMs ?? 60_000;
11
+ return async function facts() {
12
+ const due = deps.library.list().filter(({ procedure }) => procedure.onAlarm);
13
+ if (!due.length)
14
+ return undefined;
15
+ const deadline = Date.now() + totalMs, lines = [];
16
+ let broken = false;
17
+ for (const { procedure, path } of due) {
18
+ const left = Math.min(perToolMs, deadline - Date.now());
19
+ if (left <= 0) {
20
+ lines.push(`${path} 没来得及跑(闹钟前的时间用完了)`);
21
+ continue;
22
+ }
23
+ const controller = new AbortController();
24
+ let timer;
25
+ const late = new Promise((_resolve, reject) => { timer = setTimeout(() => { controller.abort(); reject(new Error(`超过 ${Math.max(1, Math.ceil(left / 1000))} 秒`)); }, left); });
26
+ try {
27
+ const kept = deps.library.kept(path);
28
+ const outcome = await Promise.race([deps.browser.scratch((page, signal) => runVersions(procedure, kept, {}, page, AbortSignal.any([signal, controller.signal])), controller.signal), late]);
29
+ noteRun(deps.library, path, outcome);
30
+ if (!outcome.result.ok)
31
+ broken = true;
32
+ lines.push(summaryLine(outcome.ran, outcome.result) + (outcome.fallback ? `(最新版没跑通,用的第 ${outcome.fallback.version} 版)` : ''));
33
+ }
34
+ catch (error) {
35
+ noteRun(deps.library, path, { thrown: error });
36
+ broken = true;
37
+ lines.push(`${path} 没跑通(${cut(errorText(error), 80)})`);
38
+ }
39
+ finally {
40
+ clearTimeout(timer);
41
+ }
42
+ }
43
+ // Nobody watched these runs, so the alarm is the first she hears of it and the time to mend it.
44
+ return [...lines, ...(broken ? [`上面没跑通的,${REPAIR_NOW}`] : [])].join('\n');
45
+ };
46
+ }
@@ -0,0 +1,32 @@
1
+ import type { Agent } from '@deepseek-ai/dsh-agent';
2
+ import type { ArchiveStore } from '../../../store.ts';
3
+ import type { ConversationRecord } from '../../../types.ts';
4
+ import type { Row } from '../browser/page.ts';
5
+ import { type MessageMap } from './format.ts';
6
+ import type { Args } from './run.ts';
7
+ /** Keyed by the platform's own message, not the session: reading the same message again finds the same record. */
8
+ export declare const computerRecordId: (worldId: string, channel: string, conversationId: string, messageId: string) => string;
9
+ export declare function messageRecords(map: MessageMap, rows: readonly Row[], args: Args, at: {
10
+ worldId: string;
11
+ sessionId: string;
12
+ turn: number;
13
+ seq: number;
14
+ now: Date;
15
+ }): {
16
+ records: ConversationRecord[];
17
+ skipped: number;
18
+ };
19
+ /** Writes what one of her tools read, as the home turn that ran it, and only where her archive reaches. */
20
+ export declare function createComputerRecorder(deps: {
21
+ store: Pick<ArchiveStore, 'commitRecords' | 'hasRecord'>;
22
+ worldId: string;
23
+ archived(sessionId: string): boolean;
24
+ now?: () => Date;
25
+ }): {
26
+ record(agent: Agent, map: MessageMap, rows: readonly Row[], args: Args): Promise<{
27
+ written: number;
28
+ skipped: number;
29
+ outOfScope: boolean;
30
+ }>;
31
+ };
32
+ export type ComputerRecorder = ReturnType<typeof createComputerRecorder>;
@@ -0,0 +1,68 @@
1
+ // src/ecosystem/computer/procedures/archive.ts
2
+ import { createHash } from 'node:crypto';
3
+ import { SessionSeq } from '@deepseek-ai/dsh-session';
4
+ import { speakerIdentity } from "../../shared/identity.js";
5
+ import { substitute } from "./format.js";
6
+ /** Keyed by the platform's own message, not the session: reading the same message again finds the same record. */
7
+ export const computerRecordId = (worldId, channel, conversationId, messageId) => createHash('sha256').update(JSON.stringify(['computer', worldId, channel, conversationId, messageId])).digest('hex');
8
+ export function messageRecords(map, rows, args, at) {
9
+ const records = [];
10
+ let skipped = 0;
11
+ for (const row of rows) {
12
+ const values = { ...args, ...row };
13
+ const field = (template) => template === undefined ? '' : substitute(template, values).trim();
14
+ const accountId = field(map.accountId), conversationId = field(map.conversationId), messageId = field(map.messageId), content = field(map.content);
15
+ const outgoing = field(map.outgoing) === 'true';
16
+ const speaker = outgoing ? accountId : field(map.speakerId);
17
+ if (!accountId || !conversationId || !messageId || !speaker || !content) {
18
+ skipped++;
19
+ continue;
20
+ }
21
+ let identity;
22
+ try {
23
+ identity = speakerIdentity({ channel: map.channel, accountId, conversationId, speakerId: `${map.channel}:${speaker}`, conversationType: map.conversationType,
24
+ ...(field(map.speakerName) ? { speakerName: field(map.speakerName) } : {}), ...(field(map.conversationName) ? { conversationName: field(map.conversationName) } : {}) });
25
+ }
26
+ catch {
27
+ skipped++;
28
+ continue;
29
+ }
30
+ const when = Date.parse(field(map.occurredAt));
31
+ const recordId = computerRecordId(at.worldId, map.channel, conversationId, messageId);
32
+ // The rows come from a tool's result, not from a session message, so the source ids are synthetic: history.ts only
33
+ // uses sourceMessageId to drop messages already in context, and these never are.
34
+ records.push({
35
+ schemaVersion: 1, worldId: at.worldId, recordId,
36
+ sourceSessionId: at.sessionId, sourceMessageId: `computer:${recordId.slice(0, 32)}`, sourceSeq: at.seq,
37
+ turnId: `${at.sessionId}:${at.turn}`, role: outgoing ? 'assistant' : 'user',
38
+ occurredAt: new Date(Number.isFinite(when) ? when : at.now.getTime()).toISOString(),
39
+ content: [...content].slice(0, 2000).join(''), status: 'complete', ...identity,
40
+ });
41
+ }
42
+ return { records, skipped };
43
+ }
44
+ /** The turn the running tool belongs to: the latest turn start in the session. */
45
+ function homeTurn(agent) {
46
+ for (let seq = agent.session.seq - 1; seq >= 0; seq--) {
47
+ const event = agent.session.eventAt(SessionSeq(seq));
48
+ if (event?.type === 'turn/start')
49
+ return event.data.turn;
50
+ }
51
+ throw new Error('找不到这一轮的开始,没有记进归档');
52
+ }
53
+ /** Writes what one of her tools read, as the home turn that ran it, and only where her archive reaches. */
54
+ export function createComputerRecorder(deps) {
55
+ return {
56
+ async record(agent, map, rows, args) {
57
+ if (!deps.archived(agent.id))
58
+ return { written: 0, skipped: 0, outOfScope: true };
59
+ const turn = homeTurn(agent);
60
+ const { records, skipped } = messageRecords(map, rows, args, { worldId: deps.worldId, sessionId: agent.id, turn, seq: agent.session.seq, now: deps.now?.() ?? new Date() });
61
+ // `written` is what was new to the archive: reading the same messages again must not claim to have filed them.
62
+ const fresh = records.filter(record => !deps.store.hasRecord(record.recordId)).length;
63
+ if (records.length)
64
+ await deps.store.commitRecords(records);
65
+ return { written: fresh, skipped, outOfScope: false };
66
+ },
67
+ };
68
+ }
@@ -0,0 +1,7 @@
1
+ import type { ProcedureLibrary } from './library.ts';
2
+ /**
3
+ * What `computer_use` answers. A target names the thing she is about to use, a program or a site, so it is matched
4
+ * against the category name and her note only: operations are named by what they do, and the same 发送消息 under
5
+ * several categories would pull in the lists of things she is not using. One category comes back, never a mix.
6
+ */
7
+ export declare function catalogText(library: ProcedureLibrary, target?: string): string;