dsh-world 0.7.6 → 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.
- package/CHANGELOG.md +541 -479
- package/CONTRIBUTING.md +174 -174
- package/INSTALL.md +314 -314
- package/NOTES.md +57 -3
- package/README.md +332 -259
- package/client/index.js +1126 -991
- package/lib/compaction.d.ts +1 -0
- package/lib/compaction.js +1 -1
- package/lib/console/routes.d.ts +11 -3
- package/lib/console/routes.js +12 -7
- package/lib/console/snapshot.d.ts +2 -0
- package/lib/console/snapshot.js +1 -0
- package/lib/ecosystem/computer/browser/browser.d.ts +34 -0
- package/lib/ecosystem/computer/browser/browser.js +101 -0
- package/lib/ecosystem/computer/browser/cdp.d.ts +22 -0
- package/lib/ecosystem/computer/browser/cdp.js +91 -0
- package/lib/ecosystem/computer/browser/edge.d.ts +15 -0
- package/lib/ecosystem/computer/browser/edge.js +80 -0
- package/lib/ecosystem/computer/browser/page.d.ts +41 -0
- package/lib/ecosystem/computer/browser/page.js +140 -0
- package/lib/ecosystem/computer/browser/pageLib.d.ts +7 -0
- package/lib/ecosystem/computer/browser/pageLib.js +106 -0
- package/lib/ecosystem/computer/desktop/desktop.d.ts +91 -0
- package/lib/ecosystem/computer/desktop/desktop.js +322 -0
- package/lib/ecosystem/computer/desktop/driver.d.ts +34 -0
- package/lib/ecosystem/computer/desktop/driver.js +197 -0
- package/lib/ecosystem/computer/desktop/processes.d.ts +5 -0
- package/lib/ecosystem/computer/desktop/processes.js +15 -0
- package/lib/ecosystem/computer/desktop/recording.d.ts +77 -0
- package/lib/ecosystem/computer/desktop/recording.js +187 -0
- package/lib/ecosystem/computer/desktop/screen.d.ts +33 -0
- package/lib/ecosystem/computer/desktop/screen.js +182 -0
- package/lib/ecosystem/computer/desktop/turns.d.ts +58 -0
- package/lib/ecosystem/computer/desktop/turns.js +94 -0
- package/lib/ecosystem/computer/desktop/watching.d.ts +25 -0
- package/lib/ecosystem/computer/desktop/watching.js +81 -0
- package/lib/ecosystem/computer/inbox/inbox.d.ts +32 -0
- package/lib/ecosystem/computer/inbox/inbox.js +142 -0
- package/lib/ecosystem/computer/index.d.ts +82 -0
- package/lib/ecosystem/computer/index.js +133 -0
- package/lib/ecosystem/computer/procedures/alarm.d.ts +13 -0
- package/lib/ecosystem/computer/procedures/alarm.js +46 -0
- package/lib/ecosystem/computer/procedures/archive.d.ts +32 -0
- package/lib/ecosystem/computer/procedures/archive.js +68 -0
- package/lib/ecosystem/computer/procedures/catalog.d.ts +7 -0
- package/lib/ecosystem/computer/procedures/catalog.js +75 -0
- package/lib/ecosystem/computer/procedures/format.d.ts +167 -0
- package/lib/ecosystem/computer/procedures/format.js +332 -0
- package/lib/ecosystem/computer/procedures/library.d.ts +53 -0
- package/lib/ecosystem/computer/procedures/library.js +178 -0
- package/lib/ecosystem/computer/procedures/run.d.ts +80 -0
- package/lib/ecosystem/computer/procedures/run.js +245 -0
- package/lib/ecosystem/computer/tools.d.ts +21 -0
- package/lib/ecosystem/computer/tools.js +197 -0
- package/lib/ecosystem/qq-native/images.d.ts +3 -0
- package/lib/ecosystem/qq-native/images.js +18 -6
- package/lib/ecosystem/qq-native/index.js +2 -2
- package/lib/ecosystem/shared/turn.d.ts +3 -0
- package/lib/ecosystem/shared/turn.js +13 -3
- package/lib/ecosystem/wechat/home.d.ts +1 -0
- package/lib/ecosystem/wechat/home.js +9 -3
- package/lib/ecosystem/wechat/images.js +3 -5
- package/lib/ecosystem/wechat/index.d.ts +1 -0
- package/lib/ecosystem/wechat/reading.d.ts +1 -1
- package/lib/ecosystem/wechat/reading.js +2 -2
- package/lib/history.js +2 -1
- package/lib/images.d.ts +30 -0
- package/lib/images.js +101 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +90 -5
- package/lib/life/photoTools.js +4 -15
- package/lib/life/runtime.d.ts +12 -2
- package/lib/life/runtime.js +93 -12
- package/lib/life/toolRegistry.d.ts +9 -3
- package/lib/life/toolRegistry.js +46 -27
- package/lib/life/turnCost.d.ts +16 -0
- package/lib/life/turnCost.js +44 -0
- package/lib/life/types.d.ts +17 -0
- package/lib/memoryTools.d.ts +2 -1
- package/lib/memoryTools.js +16 -3
- package/lib/promptSection.d.ts +1 -0
- package/lib/promptSection.js +29 -42
- package/lib/runtime.js +1 -1
- package/lib/types.d.ts +4 -0
- package/lib/worldAdd.d.ts +9 -0
- package/lib/worldAdd.js +39 -0
- package/lib/worldPrompt.d.ts +3 -2
- package/lib/worldPrompt.js +3 -2
- package/lib/worldRoutes.d.ts +5 -2
- package/lib/worldRoutes.js +29 -4
- package/lib/worldSwitch.d.ts +8 -2
- package/lib/worldSwitch.js +11 -5
- package/package.json +132 -129
- package/src/ecosystem/README.md +2 -1
- package/src/ecosystem/computer/NOTICE.md +27 -0
- package/src/ecosystem/computer/README.md +244 -0
- package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
- package/src/ecosystem/wechat/windows/README.md +4 -2
- package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
- package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
- package/templates/world-prompt-memory.md +10 -10
- package/templates/world-prompt.md +65 -65
|
@@ -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;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/ecosystem/computer/procedures/catalog.ts
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { cut } from "./run.js";
|
|
4
|
+
const GUIDE_SHOWN = 500;
|
|
5
|
+
const START_HINT = '用 page_* 自己摸索,走通了用 procedure_save 存进一个分类,再用 procedure_note 写这一类的说明。';
|
|
6
|
+
/** Two-letter overlap a misspelt word still needs, so bilbili finds bilibili but a stray shared letter finds nothing. */
|
|
7
|
+
const SIMILAR = 0.6;
|
|
8
|
+
/**
|
|
9
|
+
* The words in a text, lower-cased; one letter alone names nothing. Chinese runs stay whole: a word like 微信 has no
|
|
10
|
+
* spaces around it, so a run of hers is matched by containment below, not split.
|
|
11
|
+
*/
|
|
12
|
+
const words = (text) => text.toLowerCase().split(/[\s,,、。.;;::/|()()「」【】!!??]+/u).filter(word => [...word].length > 1);
|
|
13
|
+
const pairs = (word) => { const chars = [...word]; return new Set(chars.slice(1).map((ch, i) => chars[i] + ch)); };
|
|
14
|
+
/** How much of `a` and `b` is the same: the shorter one's length when one holds the other, else by shared letter pairs. */
|
|
15
|
+
function closeness(a, b) {
|
|
16
|
+
const shorter = Math.min([...a].length, [...b].length);
|
|
17
|
+
if (a.includes(b) || b.includes(a))
|
|
18
|
+
return shorter;
|
|
19
|
+
const x = pairs(a), y = pairs(b);
|
|
20
|
+
const dice = 2 * [...x].filter(pair => y.has(pair)).length / (x.size + y.size);
|
|
21
|
+
return dice >= SIMILAR ? dice * shorter : 0;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* How well `target` names the object `haystack` describes: each word of hers counts by its closest word there.
|
|
25
|
+
* Word against word, never letters scattered through her note, or 消息 in 消息框 would pull in 发送消息.
|
|
26
|
+
*/
|
|
27
|
+
function score(target, haystack) {
|
|
28
|
+
const hay = words(haystack);
|
|
29
|
+
return words(target).reduce((sum, word) => sum + Math.max(0, ...hay.map(other => closeness(word, other))), 0);
|
|
30
|
+
}
|
|
31
|
+
function groups(library) {
|
|
32
|
+
const all = library.list();
|
|
33
|
+
// Newest first: what she saved or fixed last is likeliest what she is doing now.
|
|
34
|
+
const newest = (a, b) => b.modified - a.modified;
|
|
35
|
+
return library.categories().map(({ name, guide }) => ({ name, guide, operations: all.filter(entry => entry.procedure.category === name).sort(newest) }));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A category as she needs it to pick an operation: her note, the operation names and the folder. The names are short
|
|
39
|
+
* and say what each does; parameters and steps are in the files, which she reads only for the one she picks.
|
|
40
|
+
*/
|
|
41
|
+
function section(library, group) {
|
|
42
|
+
const lines = [group.name];
|
|
43
|
+
if (group.guide.trim())
|
|
44
|
+
lines.push(cut(group.guide.trim(), GUIDE_SHOWN));
|
|
45
|
+
lines.push(group.operations.length ? `操作:${group.operations.map(({ procedure }) => procedure.name).join('、')}` : '(这一类还没有操作)');
|
|
46
|
+
// Files the development build left flat sit in the root, not in the 未分类 folder.
|
|
47
|
+
const folders = group.operations.length ? [...new Set(group.operations.map(({ file }) => dirname(file)))] : [join(library.dir, group.name)];
|
|
48
|
+
lines.push(`文件在 ${folders.join('、')},一个操作一个 <名字>.json;要看参数和步骤就读它。`);
|
|
49
|
+
return `${lines.join('\n')}\n\n用 procedure_run("${group.name}/操作名", 参数) 直接跑;没有合适的就用 page_* 自己做,走通了 procedure_save 存进来。`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* What `computer_use` answers. A target names the thing she is about to use, a program or a site, so it is matched
|
|
53
|
+
* against the category name and her note only: operations are named by what they do, and the same 发送消息 under
|
|
54
|
+
* several categories would pull in the lists of things she is not using. One category comes back, never a mix.
|
|
55
|
+
*/
|
|
56
|
+
export function catalogText(library, target) {
|
|
57
|
+
const all = groups(library);
|
|
58
|
+
if (!target?.trim()) {
|
|
59
|
+
if (!all.length)
|
|
60
|
+
return `还没有学会的操作。${START_HINT}`;
|
|
61
|
+
return `学会的操作:${all.map(group => `${group.name}(${group.operations.length} 个)`).join('、')}。要用某一类,用 computer_use 说出它的名字。`;
|
|
62
|
+
}
|
|
63
|
+
const named = target.trim();
|
|
64
|
+
const ranked = all.map(group => ({ group, score: score(named, `${group.name} ${group.guide}`) })).filter(entry => entry.score > 0)
|
|
65
|
+
.sort((a, b) => b.score - a.score);
|
|
66
|
+
if (!ranked.length) {
|
|
67
|
+
if (!all.length)
|
|
68
|
+
return `还没有学会的操作。${START_HINT}`;
|
|
69
|
+
return `没有对得上「${named}」的分类(已有:${all.map(group => group.name).join('、')})。是其中一类的话,用 procedure_note 在它的说明里写上这个叫法;不是就${START_HINT}`;
|
|
70
|
+
}
|
|
71
|
+
const best = ranked.filter(entry => entry.score === ranked[0].score);
|
|
72
|
+
if (best.length > 1)
|
|
73
|
+
return `「${named}」对得上不止一类:${best.map(({ group }) => group.name).join('、')}。用 computer_use 说出其中一类的名字。`;
|
|
74
|
+
return section(library, ranked[0].group);
|
|
75
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/** Finds an element by what a person sees: its role and name, text it shows, or a CSS selector; `nth` picks among several. */
|
|
2
|
+
export interface Anchor {
|
|
3
|
+
role?: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
text?: string;
|
|
6
|
+
css?: string;
|
|
7
|
+
nth?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface Condition {
|
|
10
|
+
text?: string;
|
|
11
|
+
anchor?: Anchor;
|
|
12
|
+
url?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ExtractSpec {
|
|
15
|
+
list: Anchor;
|
|
16
|
+
fields: Record<string, Anchor & {
|
|
17
|
+
attr?: string;
|
|
18
|
+
}>;
|
|
19
|
+
limit?: number;
|
|
20
|
+
}
|
|
21
|
+
export type Target = {
|
|
22
|
+
ref: number;
|
|
23
|
+
} | {
|
|
24
|
+
anchor: Anchor;
|
|
25
|
+
};
|
|
26
|
+
export type Step = {
|
|
27
|
+
open: string;
|
|
28
|
+
} | {
|
|
29
|
+
click: Anchor;
|
|
30
|
+
outward?: boolean;
|
|
31
|
+
} | {
|
|
32
|
+
type: {
|
|
33
|
+
into: Anchor;
|
|
34
|
+
text: string;
|
|
35
|
+
};
|
|
36
|
+
outward?: boolean;
|
|
37
|
+
} | {
|
|
38
|
+
press: string;
|
|
39
|
+
outward?: boolean;
|
|
40
|
+
} | {
|
|
41
|
+
wait: Condition & {
|
|
42
|
+
seconds?: number;
|
|
43
|
+
};
|
|
44
|
+
} | {
|
|
45
|
+
check: Condition;
|
|
46
|
+
} | {
|
|
47
|
+
extract: ExtractSpec;
|
|
48
|
+
allowEmpty?: boolean;
|
|
49
|
+
} | {
|
|
50
|
+
script: string;
|
|
51
|
+
as?: string;
|
|
52
|
+
outward?: boolean;
|
|
53
|
+
allowEmpty?: boolean;
|
|
54
|
+
};
|
|
55
|
+
export interface Param {
|
|
56
|
+
type: 'string' | 'number' | 'boolean';
|
|
57
|
+
required?: boolean;
|
|
58
|
+
description?: string;
|
|
59
|
+
}
|
|
60
|
+
/** How the rows of the last `extract` become archive records; every value is a template over the row and the arguments. */
|
|
61
|
+
export interface MessageMap {
|
|
62
|
+
channel: string;
|
|
63
|
+
accountId: string;
|
|
64
|
+
conversationType: 'private' | 'group';
|
|
65
|
+
conversationId: string;
|
|
66
|
+
messageId: string;
|
|
67
|
+
speakerId: string;
|
|
68
|
+
speakerName?: string;
|
|
69
|
+
conversationName?: string;
|
|
70
|
+
content: string;
|
|
71
|
+
occurredAt?: string;
|
|
72
|
+
/** Evaluates to `true` for her own messages. */
|
|
73
|
+
outgoing?: string;
|
|
74
|
+
}
|
|
75
|
+
/** A pixel on the screenshot `desktop` gives of the current window (long edge about 1280), so it follows the window's size. */
|
|
76
|
+
export interface Point {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
}
|
|
80
|
+
export type Direction = 'up' | 'down' | 'left' | 'right';
|
|
81
|
+
/** Steps on the partner's computer, the `desktop` actions written down: nothing to read there, only pictures. */
|
|
82
|
+
export type DesktopStep = {
|
|
83
|
+
open: string;
|
|
84
|
+
} | {
|
|
85
|
+
click: Point & {
|
|
86
|
+
double?: boolean;
|
|
87
|
+
right?: boolean;
|
|
88
|
+
hold?: number;
|
|
89
|
+
};
|
|
90
|
+
outward?: boolean;
|
|
91
|
+
} | {
|
|
92
|
+
drag: Point & {
|
|
93
|
+
x2: number;
|
|
94
|
+
y2: number;
|
|
95
|
+
right?: boolean;
|
|
96
|
+
hold?: number;
|
|
97
|
+
};
|
|
98
|
+
outward?: boolean;
|
|
99
|
+
} | {
|
|
100
|
+
type: {
|
|
101
|
+
text: string;
|
|
102
|
+
} & Partial<Point>;
|
|
103
|
+
outward?: boolean;
|
|
104
|
+
} | {
|
|
105
|
+
press: string;
|
|
106
|
+
hold?: number;
|
|
107
|
+
outward?: boolean;
|
|
108
|
+
} | {
|
|
109
|
+
scroll: {
|
|
110
|
+
direction: Direction;
|
|
111
|
+
} & Partial<Point>;
|
|
112
|
+
} | {
|
|
113
|
+
wait: {
|
|
114
|
+
seconds: number;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
interface Common {
|
|
118
|
+
name: string;
|
|
119
|
+
category: string;
|
|
120
|
+
description: string;
|
|
121
|
+
params: Record<string, Param>;
|
|
122
|
+
}
|
|
123
|
+
export interface PageProcedure extends Common {
|
|
124
|
+
where?: undefined;
|
|
125
|
+
steps: Step[];
|
|
126
|
+
onAlarm?: boolean;
|
|
127
|
+
summary?: string;
|
|
128
|
+
messages?: MessageMap;
|
|
129
|
+
}
|
|
130
|
+
/** `desktop` is replayed in background mode (one window), `screen` in foreground mode (the whole screen, real input). */
|
|
131
|
+
export type Computer = 'desktop' | 'screen';
|
|
132
|
+
export interface DesktopProcedure extends Common {
|
|
133
|
+
where: Computer;
|
|
134
|
+
steps: DesktopStep[];
|
|
135
|
+
onAlarm?: undefined;
|
|
136
|
+
summary?: undefined;
|
|
137
|
+
messages?: undefined;
|
|
138
|
+
}
|
|
139
|
+
export type Procedure = PageProcedure | DesktopProcedure;
|
|
140
|
+
export declare class ProcedureError extends Error {
|
|
141
|
+
name: string;
|
|
142
|
+
}
|
|
143
|
+
/** A category or operation name is also a folder or file name, so it holds no path characters. */
|
|
144
|
+
export declare const ENTRY_NAME: RegExp;
|
|
145
|
+
/** Her own notes for a category live in `<category>/目录.md`, so no operation may take that name. */
|
|
146
|
+
export declare const GUIDE_NAME = "\u76EE\u5F55";
|
|
147
|
+
/** Where files the development build left flat in `procedures/` belong. */
|
|
148
|
+
export declare const UNSORTED = "\u672A\u5206\u7C7B";
|
|
149
|
+
export declare const substitute: (text: string, values: Record<string, unknown>) => string;
|
|
150
|
+
/** Her browser reads the web, not the computer: no file:, chrome:, edge:, javascript: or data: pages. */
|
|
151
|
+
export declare const WEB_ONLY = "\u53EA\u80FD\u6253\u5F00 http \u6216 https \u5730\u5740";
|
|
152
|
+
export declare const isWebAddress: (url: string) => boolean;
|
|
153
|
+
export declare const procedurePath: (procedure: Pick<Procedure, "category" | "name">) => string;
|
|
154
|
+
/** A desktop step right after input that opens the window it needs: by hand there was time between them, in a replay none. */
|
|
155
|
+
export declare const isOpenTooSoon: (steps: DesktopStep[], index: number) => boolean;
|
|
156
|
+
/**
|
|
157
|
+
* What a saved desktop operation is likely to trip on, said when she saves it rather than refused: she may know
|
|
158
|
+
* better, and she learns it either way. A page operation has its own waits and checks.
|
|
159
|
+
*/
|
|
160
|
+
export declare function desktopAdvice(procedure: Procedure): string[];
|
|
161
|
+
export declare function entryName(value: unknown, path: string): string;
|
|
162
|
+
/**
|
|
163
|
+
* Her tool file, checked through: a mistake is refused with where it is, so nothing half-valid is ever saved or run.
|
|
164
|
+
* `category` is the folder the file sits in, used when the file does not say.
|
|
165
|
+
*/
|
|
166
|
+
export declare function parseProcedure(value: unknown, category?: string): Procedure;
|
|
167
|
+
export {};
|