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.
- package/CHANGELOG.md +541 -446
- package/CONTRIBUTING.md +174 -163
- package/INSTALL.md +314 -314
- package/NOTES.md +98 -5
- package/README.md +332 -258
- package/client/index.js +1126 -986
- package/lib/compaction.d.ts +7 -1
- package/lib/compaction.js +8 -3
- 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 +8 -1
- package/lib/ecosystem/qq-native/images.js +34 -15
- package/lib/ecosystem/qq-native/index.js +5 -5
- 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 +6 -0
- package/lib/ecosystem/wechat/index.js +3 -1
- package/lib/ecosystem/wechat/presence.d.ts +5 -1
- package/lib/ecosystem/wechat/presence.js +22 -13
- package/lib/ecosystem/wechat/reading.d.ts +1 -1
- package/lib/ecosystem/wechat/reading.js +2 -2
- package/lib/elsewhere.d.ts +34 -0
- package/lib/elsewhere.js +40 -0
- package/lib/history.d.ts +5 -0
- package/lib/history.js +7 -1
- package/lib/hostContract.d.ts +6 -0
- package/lib/images.d.ts +30 -0
- package/lib/images.js +101 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +98 -6
- 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 +4 -2
- package/lib/memoryTools.js +25 -5
- package/lib/mood.d.ts +5 -1
- package/lib/mood.js +25 -8
- package/lib/promptSection.d.ts +3 -0
- package/lib/promptSection.js +29 -42
- package/lib/runtime.js +44 -17
- package/lib/sessionNews.d.ts +17 -0
- package/lib/sessionNews.js +55 -0
- package/lib/store.d.ts +6 -0
- package/lib/store.js +17 -3
- 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,178 @@
|
|
|
1
|
+
// src/ecosystem/computer/procedures/library.ts
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { errorText } from "../../../faults.js";
|
|
5
|
+
import { durableWrite } from "../../../life/lifeStore.js";
|
|
6
|
+
import { ENTRY_NAME, entryName, GUIDE_NAME, parseProcedure, procedurePath, ProcedureError, UNSORTED } from "./format.js";
|
|
7
|
+
import { MAX_FALLBACK } from "./run.js";
|
|
8
|
+
const OWN_FILE = /^([^.]+)\.json$/;
|
|
9
|
+
const MAX_GUIDE = 1000;
|
|
10
|
+
const GUIDE_FILE = `${GUIDE_NAME}.md`;
|
|
11
|
+
/** A folder counts as a category only if its name could have been saved as one. */
|
|
12
|
+
const ENTRY_OK = (name) => name !== GUIDE_NAME && ENTRY_NAME.test(name);
|
|
13
|
+
/**
|
|
14
|
+
* Her operations, one file each in `<world>/procedures/<category>/`, with her own `目录.md` per category. They are hers:
|
|
15
|
+
* the plugin writes a file only when she saves, and keeps what it replaces as `<name>.v<n>.json` (`目录.v<n>.md`), the
|
|
16
|
+
* newest two of them: a run never tries more, and mending often would otherwise fill the folder. She may
|
|
17
|
+
* also edit them by hand; the revision follows the files. Files the development build left flat in `procedures/` are
|
|
18
|
+
* read in place as the category 未分类 and never moved.
|
|
19
|
+
*/
|
|
20
|
+
export function createProcedureLibrary(dir, deps = {}) {
|
|
21
|
+
const runs = new Map();
|
|
22
|
+
const faulted = new Set();
|
|
23
|
+
let cache;
|
|
24
|
+
// Sorted, so the catalog and the console list her operations the same way on every disk.
|
|
25
|
+
const entries = (at) => { try {
|
|
26
|
+
return readdirSync(at, { withFileTypes: true }).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
} };
|
|
31
|
+
const folders = () => entries(dir).filter(entry => entry.isDirectory() && ENTRY_OK(entry.name)).map(entry => entry.name).sort();
|
|
32
|
+
/** Every operation file with its category; folders first, then the flat files, so a saved one shadows a flat one. */
|
|
33
|
+
const files = () => [
|
|
34
|
+
...folders().flatMap(category => entries(join(dir, category)).filter(entry => entry.isFile() && OWN_FILE.test(entry.name)).map(entry => ({ category, base: entry.name, file: join(dir, category, entry.name), label: `${category}/${entry.name}` }))),
|
|
35
|
+
...entries(dir).filter(entry => entry.isFile() && OWN_FILE.test(entry.name)).map(entry => ({ category: UNSORTED, base: entry.name, file: join(dir, entry.name), label: entry.name })),
|
|
36
|
+
];
|
|
37
|
+
const revision = () => files().flatMap(({ file }) => {
|
|
38
|
+
try {
|
|
39
|
+
const { mtimeMs, size } = statSync(file);
|
|
40
|
+
return [`${file}:${mtimeMs}:${size}`];
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return [];
|
|
44
|
+
} // deleted between listing and stat
|
|
45
|
+
}).join('|');
|
|
46
|
+
/** The `<name>.v<n>.<ext>` numbers in a folder, highest first. */
|
|
47
|
+
const keptNumbers = (folder, name, ext) => {
|
|
48
|
+
const pattern = new RegExp(`^${name}\\.v(\\d+)\\.${ext}$`, 'u');
|
|
49
|
+
return entries(folder).map(entry => Number(pattern.exec(entry.name)?.[1] ?? 0)).filter(Boolean).sort((a, b) => b - a);
|
|
50
|
+
};
|
|
51
|
+
/** Writes `file`, first keeping what it replaces as the next version in its folder. Returns the new version number. */
|
|
52
|
+
function replace(folder, name, ext, content) {
|
|
53
|
+
mkdirSync(folder, { recursive: true });
|
|
54
|
+
const file = join(folder, `${name}.${ext}`);
|
|
55
|
+
// Numbering continues after the highest kept version, so a gap or a deleted file never makes a save overwrite one.
|
|
56
|
+
const highest = keptNumbers(folder, name, ext)[0] ?? 0;
|
|
57
|
+
const replaced = existsSync(file);
|
|
58
|
+
if (replaced)
|
|
59
|
+
durableWrite(join(folder, `${name}.v${highest + 1}.${ext}`), readFileSync(file, 'utf8'));
|
|
60
|
+
durableWrite(file, content);
|
|
61
|
+
// Only on a save, and only this name: nothing is tidied that she did not just replace.
|
|
62
|
+
for (const old of keptNumbers(folder, name, ext).slice(MAX_FALLBACK)) {
|
|
63
|
+
const key = `procedure-old:${name}.v${old}.${ext}`;
|
|
64
|
+
try {
|
|
65
|
+
rmSync(join(folder, `${name}.v${old}.${ext}`));
|
|
66
|
+
deps.faults?.clear(key);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
deps.faults?.set(key, `旧版本 ${name}.v${old}.${ext} 没能删掉:${errorText(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
cache = undefined;
|
|
73
|
+
return highest + (replaced ? 2 : 1);
|
|
74
|
+
}
|
|
75
|
+
function list() {
|
|
76
|
+
const current = revision();
|
|
77
|
+
if (cache?.revision === current)
|
|
78
|
+
return cache.list;
|
|
79
|
+
const out = [], seen = new Set(), paths = new Set();
|
|
80
|
+
for (const { category, base, file, label } of files()) {
|
|
81
|
+
const key = `procedure:${label}`;
|
|
82
|
+
seen.add(key);
|
|
83
|
+
try {
|
|
84
|
+
const procedure = parseProcedure(JSON.parse(readFileSync(file, 'utf8')), category);
|
|
85
|
+
if (`${procedure.name}.json` !== base)
|
|
86
|
+
throw new ProcedureError(`文件名应该是 ${procedure.name}.json`);
|
|
87
|
+
if (procedure.category !== category)
|
|
88
|
+
throw new ProcedureError(`分类应该是 ${category},或者把文件挪进 ${procedure.category} 文件夹`);
|
|
89
|
+
const path = procedurePath(procedure);
|
|
90
|
+
if (!paths.has(path)) {
|
|
91
|
+
paths.add(path);
|
|
92
|
+
out.push({ procedure, path, file, modified: statSync(file).mtimeMs });
|
|
93
|
+
}
|
|
94
|
+
deps.faults?.clear(key);
|
|
95
|
+
faulted.delete(key);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
deps.faults?.set(key, `她的操作 ${label} 没能载入:${errorText(error)}`);
|
|
99
|
+
faulted.add(key);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const key of [...faulted])
|
|
103
|
+
if (!seen.has(key)) {
|
|
104
|
+
deps.faults?.clear(key);
|
|
105
|
+
faulted.delete(key);
|
|
106
|
+
}
|
|
107
|
+
cache = { revision: current, list: out };
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
const find = (path) => list().find(entry => entry.path === path);
|
|
111
|
+
return {
|
|
112
|
+
dir,
|
|
113
|
+
list,
|
|
114
|
+
get: (path) => find(path)?.procedure,
|
|
115
|
+
/** Categories with an operation or a note of hers, by name; `guide` is her `目录.md`, or empty. */
|
|
116
|
+
categories() {
|
|
117
|
+
const names = new Set([...list().map(entry => entry.procedure.category), ...folders().filter(name => existsSync(join(dir, name, GUIDE_FILE)))]);
|
|
118
|
+
return [...names].sort().map(name => {
|
|
119
|
+
const file = join(dir, name, GUIDE_FILE);
|
|
120
|
+
let guide = '';
|
|
121
|
+
try {
|
|
122
|
+
guide = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
123
|
+
}
|
|
124
|
+
catch { /* unreadable: shown as no note */ }
|
|
125
|
+
return { name, guide };
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
/**
|
|
129
|
+
* The versions her operation replaced, newest first, for when the newest fails. One that no longer parses or repeats
|
|
130
|
+
* a newer one is left out: they are fallbacks, and the files stay hers either way.
|
|
131
|
+
*/
|
|
132
|
+
kept(path) {
|
|
133
|
+
const stored = find(path);
|
|
134
|
+
if (!stored)
|
|
135
|
+
return [];
|
|
136
|
+
const folder = dirname(stored.file), name = stored.procedure.name;
|
|
137
|
+
const seen = new Set([JSON.stringify(stored.procedure)]);
|
|
138
|
+
return keptNumbers(folder, name, 'json').flatMap(version => {
|
|
139
|
+
try {
|
|
140
|
+
const procedure = parseProcedure(JSON.parse(readFileSync(join(folder, `${name}.v${version}.json`), 'utf8')), stored.procedure.category);
|
|
141
|
+
const key = JSON.stringify(procedure);
|
|
142
|
+
if (seen.has(key))
|
|
143
|
+
return [];
|
|
144
|
+
seen.add(key);
|
|
145
|
+
return [{ procedure, version }];
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
/** Checks and writes one operation into its category folder; what it replaces stays as the next `.v<n>.json`. */
|
|
153
|
+
save(json) {
|
|
154
|
+
let value;
|
|
155
|
+
try {
|
|
156
|
+
value = JSON.parse(json);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
throw new ProcedureError(`JSON 格式不对:${errorText(error)}`);
|
|
160
|
+
}
|
|
161
|
+
const procedure = parseProcedure(value);
|
|
162
|
+
const version = replace(join(dir, procedure.category), procedure.name, 'json', JSON.stringify(procedure, null, 2) + '\n');
|
|
163
|
+
return { procedure, path: procedurePath(procedure), version };
|
|
164
|
+
},
|
|
165
|
+
/** Her note for a category, whole; the one it replaces stays as `目录.v<n>.md`. Returns the new version number. */
|
|
166
|
+
saveGuide(category, text) {
|
|
167
|
+
entryName(category, 'category');
|
|
168
|
+
if (typeof text !== 'string' || !text.trim() || [...text].length > MAX_GUIDE)
|
|
169
|
+
throw new ProcedureError(`说明应该是 1 到 ${MAX_GUIDE} 字`);
|
|
170
|
+
return replace(join(dir, category), GUIDE_NAME, 'md', text);
|
|
171
|
+
},
|
|
172
|
+
/** For when the module stops: its load faults would otherwise outlive the operations they describe. */
|
|
173
|
+
clearFaults() { for (const key of faulted)
|
|
174
|
+
deps.faults?.clear(key); faulted.clear(); cache = undefined; },
|
|
175
|
+
note(path, ok, detail) { runs.set(path, { at: new Date().toISOString(), ok, ...(detail === undefined ? {} : { detail }) }); },
|
|
176
|
+
lastRun: (path) => runs.get(path),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type Row } from '../browser/page.ts';
|
|
2
|
+
import type { ProcedureLibrary } from './library.ts';
|
|
3
|
+
import type { DesktopInput, DesktopResult } from '../desktop/desktop.ts';
|
|
4
|
+
import { type Condition, type DesktopStep, type ExtractSpec, type Procedure, type Step, type Target } from './format.ts';
|
|
5
|
+
/** What replaying needs from a page; her browser's `Page` is one. */
|
|
6
|
+
export interface Driver {
|
|
7
|
+
open(url: string): Promise<void>;
|
|
8
|
+
click(target: Target): Promise<void>;
|
|
9
|
+
type(target: Target, text: string): Promise<void>;
|
|
10
|
+
press(key: string): Promise<void>;
|
|
11
|
+
wait(condition: Condition, timeoutMs: number, signal?: AbortSignal): Promise<void>;
|
|
12
|
+
has(condition: Condition): Promise<boolean>;
|
|
13
|
+
extract(spec: ExtractSpec): Promise<Row[]>;
|
|
14
|
+
evaluate(script: string, args: Record<string, unknown>): Promise<unknown>;
|
|
15
|
+
brief(): Promise<string>;
|
|
16
|
+
}
|
|
17
|
+
/** What replaying a desktop operation needs: her `desktop` hands, each action answered with a fresh picture. */
|
|
18
|
+
export interface Hands {
|
|
19
|
+
act(input: DesktopInput): Promise<DesktopResult>;
|
|
20
|
+
}
|
|
21
|
+
export type Args = Record<string, string | number | boolean>;
|
|
22
|
+
export type Failure = {
|
|
23
|
+
ok: false;
|
|
24
|
+
step: number;
|
|
25
|
+
action: string;
|
|
26
|
+
error: string;
|
|
27
|
+
outwardDone: boolean;
|
|
28
|
+
page: string;
|
|
29
|
+
};
|
|
30
|
+
export type RunResult = {
|
|
31
|
+
ok: true;
|
|
32
|
+
rows: Row[];
|
|
33
|
+
values: Record<string, unknown>;
|
|
34
|
+
} | Failure;
|
|
35
|
+
/** One of the versions she replaced, kept as `<name>.v<n>.json`. */
|
|
36
|
+
export interface KeptVersion {
|
|
37
|
+
procedure: Procedure;
|
|
38
|
+
version: number;
|
|
39
|
+
}
|
|
40
|
+
/** Which version a run ended on: the newest unless it failed before anything went out and a kept one worked. */
|
|
41
|
+
export interface VersionedRun {
|
|
42
|
+
result: RunResult;
|
|
43
|
+
ran: Procedure;
|
|
44
|
+
fallback?: {
|
|
45
|
+
version: number;
|
|
46
|
+
newest: Failure;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Kept versions tried after the newest fails, so a long repair history cannot make one call run for minutes. */
|
|
50
|
+
export declare const MAX_FALLBACK = 2;
|
|
51
|
+
export declare function checkArgs(procedure: Procedure, input: Record<string, unknown>): Args;
|
|
52
|
+
export declare function describeStep(step: Step | DesktopStep): string;
|
|
53
|
+
/**
|
|
54
|
+
* Replays her steps without the model: the first one that fails stops the run and says where, never retried. Coming up
|
|
55
|
+
* empty is failing too, unless the step allows it, so a broken tool is never read as "nothing new".
|
|
56
|
+
*/
|
|
57
|
+
export declare function runProcedure(procedure: Procedure, args: Args, driver: Driver | Hands, signal?: AbortSignal): Promise<RunResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Runs her newest version; when it fails before anything went out, her kept versions newest first, since a site may
|
|
60
|
+
* serve its old and new page side by side and a repair for one must not lose the other. The newest's failure is the
|
|
61
|
+
* one reported when none works. A kept version whose parameters no longer fit the call is skipped.
|
|
62
|
+
*/
|
|
63
|
+
export declare function runVersions(newest: Procedure, kept: KeptVersion[], input: Record<string, unknown>, driver: Driver | Hands, signal?: AbortSignal): Promise<VersionedRun>;
|
|
64
|
+
/** Told after a result that came from a kept version, so she knows her newest one is broken. */
|
|
65
|
+
export declare const fallbackText: ({ version, newest }: NonNullable<VersionedRun["fallback"]>) => string;
|
|
66
|
+
/** Said at the moment it breaks, when the task is still in front of her: doing it by hand is how the new version gets made. */
|
|
67
|
+
export declare const REPAIR_NOW = "\u73B0\u5728\u5C31\u4FEE\uFF1A\u5148\u7528 page_* \u6216 desktop \u628A\u8FD9\u4EF6\u4E8B\u505A\u5B8C\uFF0C\u518D\u628A\u8D70\u901A\u7684\u6B65\u9AA4\u7528\u539F\u6765\u7684\u540D\u5B57 procedure_save \u6210\u65B0\u7248\uFF08\u4E0D\u53E6\u8D77\u4E00\u4E2A\u65B0\u7684\uFF09\uFF0C\u5B58\u5B8C\u8DD1\u4E00\u6B21\u786E\u8BA4\u3002";
|
|
68
|
+
export declare function failureText(path: string, result: Failure): string;
|
|
69
|
+
/** Cuts by characters, not UTF-16 units, so an emoji is never split. */
|
|
70
|
+
export declare const cut: (text: string, max: number) => string;
|
|
71
|
+
/** What her tool hands back: the rows of its last extract and the values its scripts kept, cut to 2000 characters. */
|
|
72
|
+
export declare function resultText(result: Extract<RunResult, {
|
|
73
|
+
ok: true;
|
|
74
|
+
}>): string;
|
|
75
|
+
/** One line after her alarm: the tool's own summary, or its result; a failure is said as a failure, never as nothing. */
|
|
76
|
+
export declare function summaryLine(procedure: Procedure, result: RunResult): string;
|
|
77
|
+
/** The one place a run, or the error that cut it off, becomes the note the catalog and the console show. */
|
|
78
|
+
export declare function noteRun(library: Pick<ProcedureLibrary, 'note'>, path: string, outcome: VersionedRun | {
|
|
79
|
+
thrown: unknown;
|
|
80
|
+
}): void;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// src/ecosystem/computer/procedures/run.ts
|
|
2
|
+
import { errorText } from "../../../faults.js";
|
|
3
|
+
import { describeAnchor } from "../browser/page.js";
|
|
4
|
+
import { isOpenTooSoon, isWebAddress, procedurePath, substitute, WEB_ONLY } from "./format.js";
|
|
5
|
+
/** How long a `check` waits for the page a step led to, so a check right after sending sees the page it opened. */
|
|
6
|
+
const CHECK_MS = 3000;
|
|
7
|
+
/** Kept versions tried after the newest fails, so a long repair history cannot make one call run for minutes. */
|
|
8
|
+
export const MAX_FALLBACK = 2;
|
|
9
|
+
export function checkArgs(procedure, input) {
|
|
10
|
+
const out = {};
|
|
11
|
+
for (const key of Object.keys(input))
|
|
12
|
+
if (!(key in procedure.params))
|
|
13
|
+
throw new Error(`不认识的参数 ${key}`);
|
|
14
|
+
for (const [name, param] of Object.entries(procedure.params)) {
|
|
15
|
+
const value = input[name];
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
if (param.required)
|
|
18
|
+
throw new Error(`缺少参数 ${name}`);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value !== param.type)
|
|
22
|
+
throw new Error(`参数 ${name} 应该是 ${param.type}`);
|
|
23
|
+
out[name] = value;
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
const fill = (anchor, args) => Object.fromEntries(Object.entries(anchor).map(([key, value]) => [key, typeof value === 'string' ? substitute(value, args) : value]));
|
|
28
|
+
const fillExtract = (spec, args) => ({
|
|
29
|
+
...spec, list: fill(spec.list, args),
|
|
30
|
+
fields: Object.fromEntries(Object.entries(spec.fields).map(([name, field]) => [name, fill(field, args)])),
|
|
31
|
+
});
|
|
32
|
+
const fillCondition = (condition, args) => ({
|
|
33
|
+
...(condition.text === undefined ? {} : { text: substitute(condition.text, args) }),
|
|
34
|
+
...(condition.url === undefined ? {} : { url: substitute(condition.url, args) }),
|
|
35
|
+
...(condition.anchor === undefined ? {} : { anchor: fill(condition.anchor, args) }),
|
|
36
|
+
});
|
|
37
|
+
const describeCondition = (condition) => [condition.text && `文字「${condition.text}」`, condition.url && `地址含 ${condition.url}`, condition.anchor && describeAnchor(condition.anchor)].filter(Boolean).join('、');
|
|
38
|
+
const isEmpty = (value) => value === null || value === undefined || value === ''
|
|
39
|
+
|| (Array.isArray(value) ? value.every(isEmpty) : typeof value === 'object' ? Object.values(value).every(isEmpty) : false);
|
|
40
|
+
const EMPTY_HINT = '(页面可能改了;本来就可能为空的,在这一步加 allowEmpty: true)';
|
|
41
|
+
/** An extract that finds nothing, or a field blank in every row, is far likelier a changed page than an empty one. */
|
|
42
|
+
function emptyRows(spec, rows) {
|
|
43
|
+
if (!rows.length)
|
|
44
|
+
return `一条都没有提取到${EMPTY_HINT}`;
|
|
45
|
+
const blank = Object.keys(spec.fields).find(name => rows.every(row => isEmpty(row[name])));
|
|
46
|
+
return blank === undefined ? undefined : `提取到 ${rows.length} 条,但字段 ${blank} 每一条都是空的${EMPTY_HINT}`;
|
|
47
|
+
}
|
|
48
|
+
const at = (point) => point.x === undefined ? '' : `(${point.x}, ${point.y})`;
|
|
49
|
+
export function describeStep(step) {
|
|
50
|
+
if ('open' in step)
|
|
51
|
+
return `打开 ${step.open}`;
|
|
52
|
+
if ('click' in step)
|
|
53
|
+
return 'x' in step.click ? `点${at(step.click)}` : `点 ${describeAnchor(step.click)}`;
|
|
54
|
+
if ('type' in step)
|
|
55
|
+
return 'into' in step.type ? `在 ${describeAnchor(step.type.into)} 输入` : `输入${at(step.type)}`;
|
|
56
|
+
if ('drag' in step)
|
|
57
|
+
return `从${at(step.drag)}拖到${at({ x: step.drag.x2, y: step.drag.y2 })}`;
|
|
58
|
+
if ('press' in step)
|
|
59
|
+
return `按 ${step.press}`;
|
|
60
|
+
if ('scroll' in step)
|
|
61
|
+
return `往 ${step.scroll.direction} 滚${at(step.scroll)}`;
|
|
62
|
+
if ('wait' in step)
|
|
63
|
+
return 'seconds' in step.wait && !('text' in step.wait || 'url' in step.wait || 'anchor' in step.wait) ? `等 ${step.wait.seconds} 秒` : `等 ${describeCondition(step.wait)}`;
|
|
64
|
+
if ('check' in step)
|
|
65
|
+
return `确认 ${describeCondition(step.check)}`;
|
|
66
|
+
if ('extract' in step)
|
|
67
|
+
return `提取 ${describeAnchor(step.extract.list)}`;
|
|
68
|
+
return '页面脚本';
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Replays her steps without the model: the first one that fails stops the run and says where, never retried. Coming up
|
|
72
|
+
* empty is failing too, unless the step allows it, so a broken tool is never read as "nothing new".
|
|
73
|
+
*/
|
|
74
|
+
export async function runProcedure(procedure, args, driver, signal) {
|
|
75
|
+
if (procedure.where !== undefined) {
|
|
76
|
+
if (!('act' in driver))
|
|
77
|
+
throw new Error(`${procedurePath(procedure)} 是伙伴电脑上的操作,要在「她用电脑」里跑`);
|
|
78
|
+
return runDesktop(procedure, args, driver, signal);
|
|
79
|
+
}
|
|
80
|
+
if (!('open' in driver))
|
|
81
|
+
throw new Error(`${procedurePath(procedure)} 是网页上的操作,要在我的浏览器里跑`);
|
|
82
|
+
let rows = [], outwardDone = false;
|
|
83
|
+
const values = {};
|
|
84
|
+
for (const [index, step] of procedure.steps.entries()) {
|
|
85
|
+
signal?.throwIfAborted();
|
|
86
|
+
const outward = 'outward' in step && step.outward === true;
|
|
87
|
+
try {
|
|
88
|
+
if ('open' in step) {
|
|
89
|
+
const url = substitute(step.open, args);
|
|
90
|
+
if (!isWebAddress(url))
|
|
91
|
+
throw new Error(WEB_ONLY);
|
|
92
|
+
await driver.open(url);
|
|
93
|
+
}
|
|
94
|
+
else if ('click' in step)
|
|
95
|
+
await driver.click({ anchor: fill(step.click, args) });
|
|
96
|
+
else if ('type' in step)
|
|
97
|
+
await driver.type({ anchor: fill(step.type.into, args) }, substitute(step.type.text, args));
|
|
98
|
+
else if ('press' in step)
|
|
99
|
+
await driver.press(step.press);
|
|
100
|
+
else if ('wait' in step)
|
|
101
|
+
await driver.wait(fillCondition(step.wait, args), (step.wait.seconds ?? 10) * 1000, signal);
|
|
102
|
+
else if ('check' in step) {
|
|
103
|
+
const condition = fillCondition(step.check, args);
|
|
104
|
+
try {
|
|
105
|
+
await driver.wait(condition, CHECK_MS, signal);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (signal?.aborted)
|
|
109
|
+
throw error;
|
|
110
|
+
throw new Error(`等了 ${CHECK_MS / 1000} 秒,页面上还是没有${describeCondition(condition)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else if ('extract' in step) {
|
|
114
|
+
rows = await driver.extract(fillExtract(step.extract, args));
|
|
115
|
+
const empty = step.allowEmpty ? undefined : emptyRows(step.extract, rows);
|
|
116
|
+
if (empty)
|
|
117
|
+
throw new Error(empty);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
const value = await driver.evaluate(step.script, args);
|
|
121
|
+
if (step.as) {
|
|
122
|
+
if (!step.allowEmpty && isEmpty(value))
|
|
123
|
+
throw new Error(`页面脚本的结果 ${step.as} 是空的${EMPTY_HINT}`);
|
|
124
|
+
values[step.as] = value;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (outward)
|
|
128
|
+
outwardDone = true;
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (signal?.aborted)
|
|
132
|
+
throw error;
|
|
133
|
+
const page = await driver.brief().catch(() => '(读不到当前页面)');
|
|
134
|
+
// A failed outward step may still have gone out: it counts as done.
|
|
135
|
+
return { ok: false, step: index + 1, action: describeStep(step), error: errorText(error), outwardDone: outwardDone || outward, page };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return { ok: true, rows, values };
|
|
139
|
+
}
|
|
140
|
+
function desktopInput(step, args) {
|
|
141
|
+
if ('open' in step)
|
|
142
|
+
return { action: 'open', app: substitute(step.open, args) };
|
|
143
|
+
if ('click' in step)
|
|
144
|
+
return { action: 'click', ...step.click };
|
|
145
|
+
if ('drag' in step)
|
|
146
|
+
return { action: 'drag', ...step.drag };
|
|
147
|
+
if ('type' in step)
|
|
148
|
+
return { action: 'type', ...step.type, text: substitute(step.type.text, args) };
|
|
149
|
+
if ('press' in step)
|
|
150
|
+
return { action: 'press', key: step.press, ...(step.hold === undefined ? {} : { hold: step.hold }) };
|
|
151
|
+
return { action: 'scroll', ...step.scroll };
|
|
152
|
+
}
|
|
153
|
+
const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
154
|
+
const timer = setTimeout(resolve, ms);
|
|
155
|
+
signal?.addEventListener('abort', () => { clearTimeout(timer); reject(signal.reason); }, { once: true });
|
|
156
|
+
});
|
|
157
|
+
/**
|
|
158
|
+
* Her desktop steps through her own hands. Each step answers with a picture, which the caller keeps through `driver`,
|
|
159
|
+
* so she sees where it ended; positions are pixels of the picture, so they follow the window's size, not its place.
|
|
160
|
+
*/
|
|
161
|
+
async function runDesktop(procedure, args, hands, signal) {
|
|
162
|
+
let outwardDone = false;
|
|
163
|
+
// On the whole screen her positions are pixels of a picture of it: one is taken first, so they are measured on this screen.
|
|
164
|
+
if (procedure.where === 'screen')
|
|
165
|
+
await hands.act({ action: 'shot' });
|
|
166
|
+
for (const [index, step] of procedure.steps.entries()) {
|
|
167
|
+
signal?.throwIfAborted();
|
|
168
|
+
const outward = 'outward' in step && step.outward === true;
|
|
169
|
+
try {
|
|
170
|
+
if ('wait' in step)
|
|
171
|
+
await sleep(step.wait.seconds * 1000, signal);
|
|
172
|
+
else
|
|
173
|
+
await hands.act(desktopInput(step, args));
|
|
174
|
+
if (outward)
|
|
175
|
+
outwardDone = true;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (signal?.aborted)
|
|
179
|
+
throw error;
|
|
180
|
+
const early = isOpenTooSoon(procedure.steps, index) ? '(回放时步与步之间不停,可能是窗口还没出来;在这一步前面加 wait)' : '';
|
|
181
|
+
return { ok: false, step: index + 1, action: describeStep(step), error: errorText(error) + early, outwardDone: outwardDone || outward, page: '(最后一张截图附在下一条消息里)' };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { ok: true, rows: [], values: {} };
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Runs her newest version; when it fails before anything went out, her kept versions newest first, since a site may
|
|
188
|
+
* serve its old and new page side by side and a repair for one must not lose the other. The newest's failure is the
|
|
189
|
+
* one reported when none works. A kept version whose parameters no longer fit the call is skipped.
|
|
190
|
+
*/
|
|
191
|
+
export async function runVersions(newest, kept, input, driver, signal) {
|
|
192
|
+
const result = await runProcedure(newest, checkArgs(newest, input), driver, signal);
|
|
193
|
+
if (result.ok || result.outwardDone)
|
|
194
|
+
return { result, ran: newest };
|
|
195
|
+
for (const { procedure, version } of kept.slice(0, MAX_FALLBACK)) {
|
|
196
|
+
let args;
|
|
197
|
+
try {
|
|
198
|
+
args = checkArgs(procedure, input);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const older = await runProcedure(procedure, args, driver, signal);
|
|
204
|
+
if (older.ok)
|
|
205
|
+
return { result: older, ran: procedure, fallback: { version, newest: result } };
|
|
206
|
+
if (older.outwardDone)
|
|
207
|
+
break; // something may have gone out: no other version gets a turn
|
|
208
|
+
}
|
|
209
|
+
return { result, ran: newest };
|
|
210
|
+
}
|
|
211
|
+
/** Told after a result that came from a kept version, so she knows her newest one is broken. */
|
|
212
|
+
export const fallbackText = ({ version, newest }) => `最新版第 ${newest.step} 步(${newest.action})没跑通:${cut(newest.error, 80)};这次退回第 ${version} 版跑通了。`;
|
|
213
|
+
/** Said at the moment it breaks, when the task is still in front of her: doing it by hand is how the new version gets made. */
|
|
214
|
+
export const REPAIR_NOW = '现在就修:先用 page_* 或 desktop 把这件事做完,再把走通的步骤用原来的名字 procedure_save 成新版(不另起一个新的),存完跑一次确认。';
|
|
215
|
+
export function failureText(path, result) {
|
|
216
|
+
return `${path} 没跑通:第 ${result.step} 步(${result.action})${result.error}。`
|
|
217
|
+
+ (result.outwardDone ? `这一步或之前有向外的动作,可能已经发出,先看页面确认,不要直接重跑;确认之后再把它修好存成新版。` : `还没有任何向外的动作。${REPAIR_NOW}`)
|
|
218
|
+
+ `\n当前页面:\n${result.page}`;
|
|
219
|
+
}
|
|
220
|
+
/** Cuts by characters, not UTF-16 units, so an emoji is never split. */
|
|
221
|
+
export const cut = (text, max) => { const chars = [...text]; return chars.length > max ? chars.slice(0, max).join('') + '…(截断)' : text; };
|
|
222
|
+
/** What her tool hands back: the rows of its last extract and the values its scripts kept, cut to 2000 characters. */
|
|
223
|
+
export function resultText(result) {
|
|
224
|
+
const out = { ...(result.rows.length ? { rows: result.rows } : {}), ...(Object.keys(result.values).length ? { values: result.values } : {}) };
|
|
225
|
+
const text = Object.keys(out).length ? JSON.stringify(out) : '完成。';
|
|
226
|
+
return cut(text, 2000);
|
|
227
|
+
}
|
|
228
|
+
/** One line after her alarm: the tool's own summary, or its result; a failure is said as a failure, never as nothing. */
|
|
229
|
+
export function summaryLine(procedure, result) {
|
|
230
|
+
if (!result.ok)
|
|
231
|
+
return `${procedurePath(procedure)} 没跑通(第 ${result.step} 步:${cut(result.error, 60)})`;
|
|
232
|
+
const values = { ...Object.fromEntries(Object.entries(result.values).map(([key, value]) => [key, typeof value === 'string' ? value : JSON.stringify(value)])), count: String(result.rows.length) };
|
|
233
|
+
const body = procedure.summary ? substitute(procedure.summary, values) : resultText(result);
|
|
234
|
+
return `${procedurePath(procedure)}:${cut(body, 200)}`;
|
|
235
|
+
}
|
|
236
|
+
/** The one place a run, or the error that cut it off, becomes the note the catalog and the console show. */
|
|
237
|
+
export function noteRun(library, path, outcome) {
|
|
238
|
+
if ('thrown' in outcome)
|
|
239
|
+
return library.note(path, false, cut(errorText(outcome.thrown), 200));
|
|
240
|
+
const { result, fallback } = outcome;
|
|
241
|
+
if (result.ok)
|
|
242
|
+
library.note(path, true, fallback && cut(fallbackText(fallback), 200));
|
|
243
|
+
else
|
|
244
|
+
library.note(path, false, cut(`第 ${result.step} 步:${result.error}`, 200));
|
|
245
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
2
|
+
import type { AttachmentStore } from '@deepseek-ai/dsh-attachment';
|
|
3
|
+
import { type ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
4
|
+
import type { Browser } from './browser/browser.ts';
|
|
5
|
+
import type { ComputerRecorder } from './procedures/archive.ts';
|
|
6
|
+
import type { ProcedureLibrary } from './procedures/library.ts';
|
|
7
|
+
import { type Desktop } from './desktop/desktop.ts';
|
|
8
|
+
/**
|
|
9
|
+
* Her page tools and her catalog: what she learns is found with `computer_use` and run with `procedure_run`, so the tool
|
|
10
|
+
* list stays the same however much she learns. Home only (see `toolRegistry`).
|
|
11
|
+
*/
|
|
12
|
+
export declare function computerTools(deps: {
|
|
13
|
+
agent: Agent;
|
|
14
|
+
browser: Pick<Browser, 'run'>;
|
|
15
|
+
library: ProcedureLibrary;
|
|
16
|
+
recorder: Pick<ComputerRecorder, 'record'>;
|
|
17
|
+
attachments(): AttachmentStore | undefined;
|
|
18
|
+
desktop?: Pick<Desktop, 'act'>;
|
|
19
|
+
/** Foreground mode is on: the desktop tool works the whole screen through these instead; saved operations still replay through `desktop`. */
|
|
20
|
+
screen?: Pick<Desktop, 'act'>;
|
|
21
|
+
}): ToolDefinition[];
|