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,322 @@
|
|
|
1
|
+
// src/ecosystem/computer/desktop/desktop.ts
|
|
2
|
+
import { readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { isRunning } from "./processes.js";
|
|
6
|
+
import { createRecording } from "./recording.js";
|
|
7
|
+
import { createTurns } from "./turns.js";
|
|
8
|
+
import { watching } from "./watching.js";
|
|
9
|
+
export { STOPPED } from "./turns.js";
|
|
10
|
+
/** The screenshot's long edge; her coordinates are in this image's pixels and are mapped back to the window's. */
|
|
11
|
+
export const IMAGE_EDGE = 1280;
|
|
12
|
+
/** A zoomed part is enlarged at most this many times, past which it only gets blurrier. */
|
|
13
|
+
export const ZOOM_GROW = 4;
|
|
14
|
+
const WINDOWS_SHOWN = 30;
|
|
15
|
+
/** How long a program has to repaint after input before the next picture, and to show a window after a launch. */
|
|
16
|
+
const SETTLE_MS = 400, APPEAR_MS = 10_000, GIVE_BACK_TRIES = 3;
|
|
17
|
+
/** Mouse-wheel notches per scroll: about a screenful in most programs. */
|
|
18
|
+
export const NOTCHES = 3;
|
|
19
|
+
export const VK = {
|
|
20
|
+
return: 0x0d, tab: 0x09, escape: 0x1b, space: 0x20, backspace: 0x08, delete: 0x2e, home: 0x24, end: 0x23,
|
|
21
|
+
pageup: 0x21, pagedown: 0x22, left: 0x25, up: 0x26, right: 0x27, down: 0x28,
|
|
22
|
+
};
|
|
23
|
+
const MODIFIER_VK = { ctrl: 0x11, shift: 0x10, alt: 0x12 };
|
|
24
|
+
const KEY_ALIASES = { enter: 'return', esc: 'escape', pgup: 'pageup', pgdn: 'pagedown', del: 'delete' };
|
|
25
|
+
export const DESKTOP_KEYS = 'return、tab、escape、space、backspace、delete、home、end、pageup、pagedown、方向键 up/down/left/right、f1–f12、单个字母或数字,可加 ctrl+、shift+、alt+';
|
|
26
|
+
/** `D:\\微信\\Weixin\\Weixin.exe` → `Weixin`; a Store app's id has no executable. */
|
|
27
|
+
const executable = (appId) => /([^\\/"]+)\.exe$/i.exec(appId)?.[1];
|
|
28
|
+
const area = (window) => window.width * window.height;
|
|
29
|
+
export const largest = (windows) => [...windows].sort((a, b) => area(b) - area(a))[0];
|
|
30
|
+
const pause = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
31
|
+
/** A key as people write it (`Enter`, `ctrl+f`) to its virtual-key code and modifiers; anything else is refused. */
|
|
32
|
+
export function desktopKey(text) {
|
|
33
|
+
const parts = text.toLowerCase().split('+').map(part => KEY_ALIASES[part.trim()] ?? part.trim());
|
|
34
|
+
const last = parts.pop() ?? '';
|
|
35
|
+
const vk = VK[last] ?? (/^f([1-9]|1[0-2])$/.test(last) ? 0x6f + Number(last.slice(1)) : /^[a-z0-9]$/.test(last) ? last.toUpperCase().charCodeAt(0) : undefined);
|
|
36
|
+
if (vk === undefined || !parts.every(part => part in MODIFIER_VK))
|
|
37
|
+
throw new Error(`不认识按键 ${text},可用:${DESKTOP_KEYS}`);
|
|
38
|
+
return { vk, modifiers: parts.map(part => MODIFIER_VK[part]) };
|
|
39
|
+
}
|
|
40
|
+
/** The longest she can hold a button or a key: the helper does nothing else meanwhile, and cannot be stopped mid-press. */
|
|
41
|
+
export const MAX_HOLD_SECONDS = 10;
|
|
42
|
+
/** `hold` (seconds) as milliseconds, 0 for a plain press; out of range is refused. */
|
|
43
|
+
export function holdMs(hold) {
|
|
44
|
+
if (hold === undefined)
|
|
45
|
+
return 0;
|
|
46
|
+
if (!(hold > 0 && hold <= MAX_HOLD_SECONDS))
|
|
47
|
+
throw new Error(`hold 是按住的秒数,大于 0、最多 ${MAX_HOLD_SECONDS}`);
|
|
48
|
+
return Math.round(hold * 1000);
|
|
49
|
+
}
|
|
50
|
+
/** What she is told she did: a press, or a press held for so long. */
|
|
51
|
+
/** What she is told after a drag: where it went, and how long it was held first. */
|
|
52
|
+
export const dragged = (hold) => hold === undefined ? '已拖过去。' : `已按住 ${hold} 秒再拖过去。`;
|
|
53
|
+
export const pressed = (what, hold) => hold === undefined ? `已${what}。` : `已${what === '点击' ? '长按' : what},按住 ${hold} 秒。`;
|
|
54
|
+
/** The program's windows, by her name for it: an exact title, the executable, then a title that starts with it. */
|
|
55
|
+
export function windowsNamed(windows, name) {
|
|
56
|
+
const wanted = name.toLowerCase();
|
|
57
|
+
const hit = windows.find(entry => entry.title.toLowerCase() === wanted) ?? windows.find(entry => entry.exe.toLowerCase() === wanted)
|
|
58
|
+
?? windows.find(entry => entry.title.toLowerCase().startsWith(wanted));
|
|
59
|
+
return hit ? windows.filter(entry => entry.pid === hit.pid) : [];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Her turn on the partner's computer: one current window, its last picture, and what she did. She sees screenshots
|
|
63
|
+
* and acts by their pixels, the way a person does. Input is posted to the window, so the partner's pointer and focus
|
|
64
|
+
* stay theirs; a window that is brought up or launched hands the focus straight back. One action at a time, in order.
|
|
65
|
+
*/
|
|
66
|
+
export function createDesktop(deps) {
|
|
67
|
+
const running = deps.running ?? isRunning, settleMs = deps.settleMs ?? SETTLE_MS, appearMs = deps.appearMs ?? APPEAR_MS;
|
|
68
|
+
let window, view, shots = 0;
|
|
69
|
+
/** The program's windows as last seen, and her last input: what changed after it, and how soon, is told to her. */
|
|
70
|
+
let known, acted;
|
|
71
|
+
const call = (op, args = {}) => deps.driver.call(op, args).catch((error) => {
|
|
72
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
73
|
+
// Windows keeps an ordinary program away from an administrator's windows; background mode has no way round that.
|
|
74
|
+
if (/ELEVATED/.test(why))
|
|
75
|
+
throw new Error(`${window?.app ?? '这个窗口'}是以管理员权限运行的程序,后台模式截不到、也点不了它。要操作它,请伙伴切到前台模式。`);
|
|
76
|
+
throw new Error(`没做成:${why}`);
|
|
77
|
+
});
|
|
78
|
+
const recording = createRecording({ call, ...(deps.secondMs === undefined ? {} : { secondMs: deps.secondMs }) });
|
|
79
|
+
async function list() {
|
|
80
|
+
const data = await call('windows');
|
|
81
|
+
return { windows: data.windows ?? [], foreground: Number(data.foreground ?? 0) };
|
|
82
|
+
}
|
|
83
|
+
function current() {
|
|
84
|
+
if (!window)
|
|
85
|
+
throw new Error('还没有打开窗口:先 desktop open 一个程序');
|
|
86
|
+
return window;
|
|
87
|
+
}
|
|
88
|
+
/** A picture pixel to a window pixel; outside the picture is refused, it would land in some other window. */
|
|
89
|
+
function onWindow(x, y) {
|
|
90
|
+
current();
|
|
91
|
+
if (!view)
|
|
92
|
+
throw new Error('还没看过这个窗口:先 desktop shot');
|
|
93
|
+
if (typeof x !== 'number' || typeof y !== 'number')
|
|
94
|
+
throw new Error('要给 x、y(截图上的像素)');
|
|
95
|
+
if (x < 0 || y < 0 || x > view.width || y > view.height)
|
|
96
|
+
throw new Error(`x、y 要在截图里面(${view.width}×${view.height})`);
|
|
97
|
+
return { x: Math.round(x * view.scale), y: Math.round(y * view.scale) };
|
|
98
|
+
}
|
|
99
|
+
/** A window that went away (closed, minimized, into the tray) is never pictured; she is told and opens it again. */
|
|
100
|
+
async function visible() {
|
|
101
|
+
const open = current(), { windows } = await list();
|
|
102
|
+
if (windows.some(entry => entry.hwnd === open.hwnd && !entry.minimized))
|
|
103
|
+
return { open, windows };
|
|
104
|
+
window = undefined;
|
|
105
|
+
view = undefined;
|
|
106
|
+
throw new Error(`${open.app} 的窗口不见了(关掉了、最小化了或收进了托盘):先 desktop open 它`);
|
|
107
|
+
}
|
|
108
|
+
/** A picture holds one window only, so a dialog or panel the program opened beside it is named, or she would never know. */
|
|
109
|
+
function others({ open, windows }) {
|
|
110
|
+
const titles = [...new Set(windows.filter(entry => entry.pid === open.pid && entry.hwnd !== open.hwnd && !entry.minimized).map(entry => `「${entry.title}」`))];
|
|
111
|
+
return titles.length ? `\n${open.app} 还开着别的窗口:${titles.join('、')},不在这张截图里;要操作它就 desktop open 它的标题。` : '';
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A dialog or panel opens a moment after the input that asked for it. By hand she never notices, since she looks
|
|
115
|
+
* between steps; a replay does not wait unless she wrote it down. So each change is told with the time it took.
|
|
116
|
+
*/
|
|
117
|
+
function changes({ open, windows }) {
|
|
118
|
+
const now = new Map(windows.filter(entry => entry.pid === open.pid && !entry.minimized).map(entry => [entry.hwnd, entry.title]));
|
|
119
|
+
const before = known;
|
|
120
|
+
known = now;
|
|
121
|
+
if (!before || !acted)
|
|
122
|
+
return '';
|
|
123
|
+
const appeared = [...now].filter(([hwnd]) => !before.has(hwnd)).map(([, title]) => `「${title}」`);
|
|
124
|
+
const gone = [...before].filter(([hwnd]) => !now.has(hwnd)).map(([, title]) => `「${title}」`);
|
|
125
|
+
if (!appeared.length && !gone.length)
|
|
126
|
+
return '';
|
|
127
|
+
const what = [appeared.length ? `出现了${appeared.join('、')}` : '', gone.length ? `关掉了${gone.join('、')}` : ''].filter(Boolean).join(',');
|
|
128
|
+
return `\n窗口变了:${what}(在上一次${acted.what}之后 ${((Date.now() - acted.at) / 1000).toFixed(1)} 秒内)。窗口是过一会儿才变的;存成操作时,用到它的那一步前面要加 wait,回放时步与步之间不停。`;
|
|
129
|
+
}
|
|
130
|
+
async function picture(region, mark) {
|
|
131
|
+
const seen = await visible(), { hwnd } = seen.open;
|
|
132
|
+
const file = join(tmpdir(), `dsh-world-desktop-${process.pid}-${++shots}.png`);
|
|
133
|
+
try {
|
|
134
|
+
const shot = await call('shot', { hwnd, file, edge: IMAGE_EDGE, grow: region ? ZOOM_GROW : 1, ...(region ?? { x: 0, y: 0, w: 0, h: 0 }), markX: mark?.x ?? -1, markY: mark?.y ?? -1 });
|
|
135
|
+
return { data: readFileSync(file), width: Number(shot.width), height: Number(shot.height), clientWidth: Number(shot.clientWidth), also: changes(seen) + others(seen) };
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
rmSync(file, { force: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async function shot(said = '', mark) {
|
|
142
|
+
const taken = await picture(undefined, mark);
|
|
143
|
+
if (mark)
|
|
144
|
+
said += '图上的红圈是刚才点到的地方,没点中就看偏了多少,改了再点。';
|
|
145
|
+
view = { scale: taken.clientWidth / taken.width, width: taken.width, height: taken.height };
|
|
146
|
+
return { text: `${said}${current().app} 的截图附在下一条消息里(${taken.width}×${taken.height})。按图上的像素用 desktop click 带 x、y;字太小就 desktop zoom。${taken.also}`, image: { data: taken.data, mediaType: 'image/png' } };
|
|
147
|
+
}
|
|
148
|
+
/** After acting she sees the result: a wrong click is caught by her, not assumed fine. */
|
|
149
|
+
async function after(done, clicked) {
|
|
150
|
+
await pause(settleMs);
|
|
151
|
+
return shot(done, clicked);
|
|
152
|
+
}
|
|
153
|
+
/** Waits for a window of the program to show up, as one does after a click on the tray or a launch. */
|
|
154
|
+
async function appear(name, exe, before) {
|
|
155
|
+
for (const deadline = Date.now() + appearMs;;) {
|
|
156
|
+
const { windows } = await list();
|
|
157
|
+
const fresh = windows.filter(entry => !before.has(entry.hwnd));
|
|
158
|
+
const mine = windowsNamed(windows, name).length ? windowsNamed(windows, name)
|
|
159
|
+
: exe ? windows.filter(entry => entry.exe.toLowerCase() === exe.toLowerCase()) : [];
|
|
160
|
+
if (mine.length)
|
|
161
|
+
return mine;
|
|
162
|
+
if (Date.now() >= deadline)
|
|
163
|
+
return fresh.length === 1 ? fresh : [];
|
|
164
|
+
await pause(Math.min(500, appearMs));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** The partner's window goes back in front; a sentence for her when that did not work. */
|
|
168
|
+
async function giveBack(foreground) {
|
|
169
|
+
if (!foreground)
|
|
170
|
+
return '原来在前面的是桌面,焦点留在新窗口上。';
|
|
171
|
+
// The program pulls itself to the front a moment after its window shows (WeChat from the tray does), which undoes
|
|
172
|
+
// a hand-back made too early; so it is checked after a pause and made again.
|
|
173
|
+
for (let attempt = 0; attempt < GIVE_BACK_TRIES; attempt++) {
|
|
174
|
+
await pause(settleMs);
|
|
175
|
+
await call('refocus', { hwnd: foreground });
|
|
176
|
+
await pause(settleMs);
|
|
177
|
+
if ((await list()).foreground === foreground)
|
|
178
|
+
return '焦点还给了伙伴原来的窗口。';
|
|
179
|
+
}
|
|
180
|
+
return '焦点没能还给伙伴原来的窗口。';
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* A program closed to the tray has no window, and launching it again opens a second copy (WeChat 4.x does), so
|
|
184
|
+
* its tray icon is clicked: that is the program showing itself, which a window forced visible from outside is not.
|
|
185
|
+
*/
|
|
186
|
+
async function fromTray(name, foreground, before) {
|
|
187
|
+
const cursor = (await call('cursor')).cursor;
|
|
188
|
+
const tray = await call('tray', { name });
|
|
189
|
+
if (tray.result !== 'opened')
|
|
190
|
+
return { windows: [], told: '' };
|
|
191
|
+
const windows = await appear(name, undefined, before);
|
|
192
|
+
if (!windows.length)
|
|
193
|
+
throw new Error(`点了 ${name} 的托盘图标,但 ${appearMs / 1000} 秒内没有出现它的窗口`);
|
|
194
|
+
const moved = JSON.stringify(tray.cursor) !== JSON.stringify(cursor) ? '(伙伴的鼠标位置变了,可能是伙伴自己在动。)' : '';
|
|
195
|
+
return { windows, told: `它收在托盘里,已经从托盘图标点开;${await giveBack(foreground)}${moved}\n` };
|
|
196
|
+
}
|
|
197
|
+
async function launch(name, foreground, before) {
|
|
198
|
+
const apps = (await call('apps')).apps ?? [];
|
|
199
|
+
const wanted = name.toLowerCase();
|
|
200
|
+
const app = apps.find(entry => entry.name.toLowerCase() === wanted) ?? apps.find(entry => entry.name.toLowerCase().includes(wanted));
|
|
201
|
+
if (!app)
|
|
202
|
+
throw new Error(`找不到叫「${name}」的程序:开着的窗口、托盘图标和开始菜单里都没有。不带 app 调 desktop open 看看开着哪些`);
|
|
203
|
+
const exe = executable(app.appId);
|
|
204
|
+
if (exe && await running(exe))
|
|
205
|
+
throw new Error(`${app.name} 开着,但没有窗口,托盘里也找不到它的图标;再启动会多开一个,所以不启动。请伙伴把它打开。`);
|
|
206
|
+
await call('launch', { appId: app.appId });
|
|
207
|
+
const windows = await appear(app.name, exe, before);
|
|
208
|
+
if (!windows.length)
|
|
209
|
+
throw new Error(`${app.name} 启动了,但 ${appearMs / 1000} 秒内没有出现窗口`);
|
|
210
|
+
return { windows, told: `${app.name} 没开,已经启动;${await giveBack(foreground)}\n` };
|
|
211
|
+
}
|
|
212
|
+
async function open(app) {
|
|
213
|
+
const { windows, foreground } = await list();
|
|
214
|
+
if (!app?.trim()) {
|
|
215
|
+
const titles = [...new Set(windows.map(entry => entry.title))].slice(0, WINDOWS_SHOWN);
|
|
216
|
+
return { text: `开着的窗口:${titles.join('、') || '(没有)'}。收在托盘里的、没开的,写它的名字也能打开。` };
|
|
217
|
+
}
|
|
218
|
+
const name = app.trim(), before = new Set(windows.map(entry => entry.hwnd));
|
|
219
|
+
let mine = windowsNamed(windows, name), told = '';
|
|
220
|
+
if (!mine.length)
|
|
221
|
+
({ windows: mine, told } = await fromTray(name, foreground, before));
|
|
222
|
+
if (!mine.length)
|
|
223
|
+
({ windows: mine, told } = await launch(name, foreground, before));
|
|
224
|
+
// Her name may be one window's own title (a panel such as 朋友圈); otherwise the program's main window.
|
|
225
|
+
const titled = mine.find(entry => entry.title.toLowerCase() === name.toLowerCase() && !entry.minimized);
|
|
226
|
+
let chosen = titled ?? largest(mine.filter(entry => !entry.minimized && !entry.owner)) ?? largest(mine.filter(entry => !entry.minimized));
|
|
227
|
+
if (!chosen) {
|
|
228
|
+
const minimized = largest(mine);
|
|
229
|
+
if ((await call('restore', { hwnd: minimized.hwnd })).restored !== true)
|
|
230
|
+
throw new Error(`${name} 最小化着,还原不出来`);
|
|
231
|
+
chosen = minimized;
|
|
232
|
+
told += '它原来最小化着,已经还原,没有抢伙伴的焦点。\n';
|
|
233
|
+
}
|
|
234
|
+
// Switching between one program's windows keeps what was seen; another program starts afresh.
|
|
235
|
+
if (window?.pid !== chosen.pid)
|
|
236
|
+
known = undefined;
|
|
237
|
+
window = { hwnd: chosen.hwnd, pid: chosen.pid, app: name };
|
|
238
|
+
view = undefined;
|
|
239
|
+
return shot(told);
|
|
240
|
+
}
|
|
241
|
+
// The sign: a banner the program words (never her text) and a frame around the window she is on, while she acts.
|
|
242
|
+
async function sign(show) {
|
|
243
|
+
if (!deps.companion || (show && !window))
|
|
244
|
+
return;
|
|
245
|
+
try {
|
|
246
|
+
await deps.driver.call('sign', show && window ? { show: true, hwnd: window.hwnd, text: `${deps.companion()}正在操作`, hint: '连按两下 Esc 停下', stop: '停下', stopped: '已停下' } : { show: false });
|
|
247
|
+
deps.signFault?.(undefined);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
deps.signFault?.(`她操作时的提示没能显示:${error instanceof Error ? error.message : String(error)}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const watch = watching({
|
|
254
|
+
recording, what: app => ` ${app} 的窗口`, keep: '窗口不能最小化、不能收进托盘。',
|
|
255
|
+
target: async () => {
|
|
256
|
+
const { open: target } = await visible();
|
|
257
|
+
return {
|
|
258
|
+
key: `window:${target.hwnd}`, app: target.app,
|
|
259
|
+
capture: async (file, edge) => String((await call('shot', { hwnd: target.hwnd, file, edge, grow: 1, x: 0, y: 0, w: 0, h: 0, markX: -1, markY: -1 })).print ?? ''),
|
|
260
|
+
space: () => call('key', { hwnd: target.hwnd, vk: VK.space, modifiers: [] }),
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
const run = {
|
|
265
|
+
record: input => watch.record(input),
|
|
266
|
+
stop: () => watch.stop(),
|
|
267
|
+
review: input => watch.review(input),
|
|
268
|
+
open: input => open(input.app),
|
|
269
|
+
shot: () => shot(),
|
|
270
|
+
click: async (input) => {
|
|
271
|
+
const at = onWindow(input.x, input.y);
|
|
272
|
+
await call('click', { hwnd: current().hwnd, ...at, right: input.right === true, count: input.double ? 2 : 1, hold: holdMs(input.hold) });
|
|
273
|
+
return after(pressed('点击', input.hold), at);
|
|
274
|
+
},
|
|
275
|
+
drag: async (input) => {
|
|
276
|
+
const from = onWindow(input.x, input.y), to = onWindow(input.x2, input.y2);
|
|
277
|
+
await call('drag', { hwnd: current().hwnd, x: from.x, y: from.y, x2: to.x, y2: to.y, right: input.right === true, hold: holdMs(input.hold) });
|
|
278
|
+
return after(dragged(input.hold), to);
|
|
279
|
+
},
|
|
280
|
+
type: async (input) => {
|
|
281
|
+
if (typeof input.text !== 'string' || !input.text)
|
|
282
|
+
throw new Error('text 不能是空的');
|
|
283
|
+
const at = input.x !== undefined || input.y !== undefined ? onWindow(input.x, input.y) : undefined;
|
|
284
|
+
if (at)
|
|
285
|
+
await call('click', { hwnd: current().hwnd, ...at, right: false, count: 1 });
|
|
286
|
+
await call('type', { hwnd: current().hwnd, text: input.text });
|
|
287
|
+
if (input.submit)
|
|
288
|
+
await call('key', { hwnd: current().hwnd, vk: VK.return, modifiers: [] });
|
|
289
|
+
return after(input.submit ? '已输入并回车。' : '已输入。', at);
|
|
290
|
+
},
|
|
291
|
+
press: async (input) => {
|
|
292
|
+
await call('key', { hwnd: current().hwnd, ...desktopKey(String(input.key ?? '')), hold: holdMs(input.hold) });
|
|
293
|
+
return after(pressed(`按 ${input.key}`, input.hold));
|
|
294
|
+
},
|
|
295
|
+
scroll: async (input) => {
|
|
296
|
+
const step = { up: [NOTCHES, 0], down: [-NOTCHES, 0], left: [0, -NOTCHES], right: [0, NOTCHES] }[String(input.direction)];
|
|
297
|
+
if (!step)
|
|
298
|
+
throw new Error('direction 只能是 up、down、left 或 right');
|
|
299
|
+
if (!view)
|
|
300
|
+
throw new Error('还没看过这个窗口:先 desktop shot');
|
|
301
|
+
const at = input.x === undefined && input.y === undefined ? onWindow(view.width / 2, view.height / 2) : onWindow(input.x, input.y);
|
|
302
|
+
await call('scroll', { hwnd: current().hwnd, ...at, up: step[0], right: step[1] });
|
|
303
|
+
return after('已滚动。');
|
|
304
|
+
},
|
|
305
|
+
zoom: async (input) => {
|
|
306
|
+
const from = onWindow(input.x, input.y), to = onWindow(input.x2, input.y2);
|
|
307
|
+
if (!(to.x > from.x && to.y > from.y))
|
|
308
|
+
throw new Error('zoom 要给左上角 x、y 和右下角 x2、y2(截图上的像素)');
|
|
309
|
+
const part = await picture({ x: from.x, y: from.y, w: to.x - from.x, h: to.y - from.y });
|
|
310
|
+
return { text: '放大的局部附在下一条消息里;在它上面量的坐标不能直接点,回到整张截图上点。', image: { data: part.data, mediaType: 'image/png' } };
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
const turns = createTurns({
|
|
314
|
+
run, sign, recording, app: () => window?.app, ...(deps.lingerMs === undefined ? {} : { lingerMs: deps.lingerMs }),
|
|
315
|
+
acted: what => { acted = { what, at: Date.now() }; },
|
|
316
|
+
});
|
|
317
|
+
return {
|
|
318
|
+
actions: turns.actions, act: turns.act, recent: turns.recent, halt: turns.halt, release: turns.release, halted: turns.halted,
|
|
319
|
+
/** When the switch goes off: nothing she opened stays "current" for a later turn, and what she recorded is deleted. */
|
|
320
|
+
reset() { window = undefined; view = undefined; known = undefined; acted = undefined; recording.clear(); turns.reset(); },
|
|
321
|
+
};
|
|
322
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Faults } from '../../../faults.ts';
|
|
2
|
+
export type DesktopDriver = ReturnType<typeof createDesktopDriver>;
|
|
3
|
+
export declare const HELPER_SCRIPT: string;
|
|
4
|
+
/**
|
|
5
|
+
* The Windows helper (`win32.ps1`) in a child process, started on first use and ended with the module. A crash or a
|
|
6
|
+
* hung call fails what is pending, is a fault, and the next call starts it again; starts that keep dying stop there,
|
|
7
|
+
* so a broken machine is said once instead of relaunched on every step. Her inbox runs a helper of its own (`as`), so
|
|
8
|
+
* a slow step of hers and a look at the notifications never wait on each other.
|
|
9
|
+
*
|
|
10
|
+
* `elevated` is foreground mode's helper: started as administrator, so the partner approves it once through UAC. A UAC
|
|
11
|
+
* prompt cannot share stdin and stdout, so it dials back to a pipe opened here and proves itself with a one-time key.
|
|
12
|
+
* It is never restarted behind the partner's back: once it is gone, foreground mode is over until it is turned on again.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createDesktopDriver(deps: {
|
|
15
|
+
faults: Faults;
|
|
16
|
+
command?: string[];
|
|
17
|
+
callMs?: number;
|
|
18
|
+
as?: {
|
|
19
|
+
fault: string;
|
|
20
|
+
name: string;
|
|
21
|
+
stopped: string;
|
|
22
|
+
};
|
|
23
|
+
events?(name: string): void;
|
|
24
|
+
elevated?: boolean;
|
|
25
|
+
elevateMs?: number;
|
|
26
|
+
}): {
|
|
27
|
+
/** One helper operation by name, e.g. `windows` or `click`; a refusal rejects with the helper's reason. */
|
|
28
|
+
call(op: string, args?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
29
|
+
/** Starts it now rather than on first use: foreground mode asks for administrator rights when it is turned on. */
|
|
30
|
+
start: () => Promise<void>;
|
|
31
|
+
running: () => boolean;
|
|
32
|
+
/** Ends the helper and forgets a halted start, so turning the switch on again tries afresh. */
|
|
33
|
+
stop(): void;
|
|
34
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// src/ecosystem/computer/desktop/driver.ts
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { randomBytes } from 'node:crypto';
|
|
4
|
+
import { createServer } from 'node:net';
|
|
5
|
+
import { createInterface } from 'node:readline';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { errorText } from "../../../faults.js";
|
|
8
|
+
/** A call that hangs this long has an unknown outcome; the helper is killed rather than trusted again. */
|
|
9
|
+
const CALL_MS = 30_000;
|
|
10
|
+
/** How long the partner has to answer the UAC prompt that starts foreground mode's helper. */
|
|
11
|
+
const ELEVATE_MS = 120_000;
|
|
12
|
+
/** Starts that end before any reply, in a row, after which it stays down until the switch is turned off and on. */
|
|
13
|
+
const MAX_FAILED_STARTS = 2;
|
|
14
|
+
// Shipped as source (package.json `files`), so the same path holds from `src/` and from the built `lib/`.
|
|
15
|
+
export const HELPER_SCRIPT = fileURLToPath(new URL('../../../../src/ecosystem/computer/desktop/win32.ps1', import.meta.url));
|
|
16
|
+
const POWERSHELL = ['powershell.exe', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', HELPER_SCRIPT];
|
|
17
|
+
/** Words the elevated helper's launcher says when the partner declines the UAC prompt (English and Chinese Windows). */
|
|
18
|
+
const DECLINED = /canceled by the user|cancelled by the user|已被用户取消|操作已取消/i;
|
|
19
|
+
/**
|
|
20
|
+
* The Windows helper (`win32.ps1`) in a child process, started on first use and ended with the module. A crash or a
|
|
21
|
+
* hung call fails what is pending, is a fault, and the next call starts it again; starts that keep dying stop there,
|
|
22
|
+
* so a broken machine is said once instead of relaunched on every step. Her inbox runs a helper of its own (`as`), so
|
|
23
|
+
* a slow step of hers and a look at the notifications never wait on each other.
|
|
24
|
+
*
|
|
25
|
+
* `elevated` is foreground mode's helper: started as administrator, so the partner approves it once through UAC. A UAC
|
|
26
|
+
* prompt cannot share stdin and stdout, so it dials back to a pipe opened here and proves itself with a one-time key.
|
|
27
|
+
* It is never restarted behind the partner's back: once it is gone, foreground mode is over until it is turned on again.
|
|
28
|
+
*/
|
|
29
|
+
export function createDesktopDriver(deps) {
|
|
30
|
+
const [program, ...programArgs] = deps.command ?? POWERSHELL, callMs = deps.callMs ?? CALL_MS;
|
|
31
|
+
const { fault: FAULT, name, stopped } = deps.as ?? (deps.elevated
|
|
32
|
+
? { fault: 'computer-foreground', name: '前台模式的助手', stopped: '前台模式关掉了' }
|
|
33
|
+
: { fault: 'computer-desktop', name: '她用电脑的助手', stopped: '她用电脑的开关关掉了' });
|
|
34
|
+
const pending = new Map();
|
|
35
|
+
let link, starting, nextId = 1, failedStarts = 0, answered = false, halted;
|
|
36
|
+
function failAll(why) {
|
|
37
|
+
for (const [id, entry] of pending) {
|
|
38
|
+
clearTimeout(entry.timer);
|
|
39
|
+
entry.reject(new Error(why));
|
|
40
|
+
pending.delete(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function lost(gone, why) {
|
|
44
|
+
if (link !== gone)
|
|
45
|
+
return;
|
|
46
|
+
link = undefined;
|
|
47
|
+
if (deps.elevated)
|
|
48
|
+
halted = `${name}退出了:${why}。要再用前台模式,请伙伴把它关掉再打开(会再确认一次管理员权限)`;
|
|
49
|
+
else if (!answered && ++failedStarts >= MAX_FAILED_STARTS)
|
|
50
|
+
halted = `${name}起不来:${why}`;
|
|
51
|
+
deps.faults.set(FAULT, halted ?? `${name}退出了:${why},下一次用时重新启动`);
|
|
52
|
+
failAll(`${name}退出了:${why}`);
|
|
53
|
+
}
|
|
54
|
+
function settle(line) {
|
|
55
|
+
let message;
|
|
56
|
+
try {
|
|
57
|
+
message = JSON.parse(line);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// Not an answer: something the helper saw happen, such as the partner stopping her.
|
|
63
|
+
if (typeof message.event === 'string') {
|
|
64
|
+
deps.events?.(message.event);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const entry = message.id === undefined ? undefined : pending.get(message.id);
|
|
68
|
+
if (!entry)
|
|
69
|
+
return;
|
|
70
|
+
pending.delete(message.id);
|
|
71
|
+
clearTimeout(entry.timer);
|
|
72
|
+
answered = true;
|
|
73
|
+
failedStarts = 0;
|
|
74
|
+
deps.faults.clear(FAULT);
|
|
75
|
+
if (message.error !== undefined)
|
|
76
|
+
entry.reject(new Error(message.error));
|
|
77
|
+
else
|
|
78
|
+
entry.resolve(message.data ?? {});
|
|
79
|
+
}
|
|
80
|
+
function startChild() {
|
|
81
|
+
answered = false;
|
|
82
|
+
const started = spawn(program, programArgs, { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true });
|
|
83
|
+
let stderr = '';
|
|
84
|
+
const made = { write: line => started.stdin.write(line), end: () => { started.stdin?.end(); started.kill(); } };
|
|
85
|
+
started.stderr.on('data', (chunk) => { stderr = (stderr + chunk.toString('utf8')).slice(-500); });
|
|
86
|
+
createInterface({ input: started.stdout }).on('line', settle);
|
|
87
|
+
started.stdin.on('error', () => { });
|
|
88
|
+
started.on('error', error => lost(made, errorText(error)));
|
|
89
|
+
started.on('exit', (code, signal) => lost(made, stderr.trim().split('\n').at(-1)?.trim() || `退出码 ${code ?? signal}`));
|
|
90
|
+
return made;
|
|
91
|
+
}
|
|
92
|
+
/** Opens a pipe only this run knows, starts the helper through UAC, and takes the first connection that says the key. */
|
|
93
|
+
function startElevated() {
|
|
94
|
+
answered = false;
|
|
95
|
+
const pipe = `dsh-world-${randomBytes(16).toString('hex')}`, key = randomBytes(24).toString('hex');
|
|
96
|
+
const elevateMs = deps.elevateMs ?? ELEVATE_MS;
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
let settled = false;
|
|
99
|
+
const server = createServer((socket) => {
|
|
100
|
+
if (settled) {
|
|
101
|
+
socket.destroy();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
let proven = false;
|
|
105
|
+
createInterface({ input: socket }).on('line', line => {
|
|
106
|
+
if (proven) {
|
|
107
|
+
settle(line);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
let hello;
|
|
111
|
+
try {
|
|
112
|
+
hello = JSON.parse(line).hello;
|
|
113
|
+
}
|
|
114
|
+
catch { /* not the helper */ }
|
|
115
|
+
// Anything else that found the pipe is not the helper this run started.
|
|
116
|
+
if (hello !== key || settled) {
|
|
117
|
+
socket.destroy();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
proven = true;
|
|
121
|
+
settled = true;
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
server.close();
|
|
124
|
+
const made = { write: text => socket.write(text), end: () => socket.destroy() };
|
|
125
|
+
socket.on('close', () => lost(made, '和 DSH 的连接断了'));
|
|
126
|
+
socket.on('error', () => { });
|
|
127
|
+
resolve(made);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
const fail = (why) => { if (settled)
|
|
131
|
+
return; settled = true; clearTimeout(timer); server.close(); reject(new Error(why)); };
|
|
132
|
+
const timer = setTimeout(() => fail(`${Math.round(elevateMs / 1000)} 秒内没有确认管理员权限`), elevateMs);
|
|
133
|
+
server.on('error', error => fail(errorText(error)));
|
|
134
|
+
server.listen(`\\\\.\\pipe\\${pipe}`, () => {
|
|
135
|
+
// The launcher only asks Windows to start the helper as administrator; the helper itself dials the pipe.
|
|
136
|
+
const quoted = (text) => `'${text.replace(/'/g, "''")}'`;
|
|
137
|
+
const args = ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', `"${HELPER_SCRIPT}"`, '-Pipe', pipe, '-Key', key];
|
|
138
|
+
const launcher = spawn('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', `Start-Process -FilePath powershell.exe -Verb RunAs -WindowStyle Hidden -ArgumentList ${args.map(quoted).join(',')}`], { stdio: ['ignore', 'ignore', 'pipe'], windowsHide: true });
|
|
139
|
+
let said = '';
|
|
140
|
+
launcher.stderr.on('data', (chunk) => { said += chunk.toString('utf8'); });
|
|
141
|
+
launcher.on('error', error => fail(errorText(error)));
|
|
142
|
+
launcher.on('exit', code => { if (code)
|
|
143
|
+
fail(DECLINED.test(said) ? '伙伴没有同意管理员权限' : said.trim().split('\n').find(Boolean)?.trim() || `启动失败(退出码 ${code})`); });
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function send(target, op, args) {
|
|
148
|
+
const id = nextId++;
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const timer = setTimeout(() => {
|
|
151
|
+
pending.delete(id);
|
|
152
|
+
reject(new Error(`助手 ${callMs / 1000} 秒没有回应,结果不明:先 desktop shot 看看现在的样子`));
|
|
153
|
+
if (link === target) {
|
|
154
|
+
target.end();
|
|
155
|
+
lost(target, '一次调用卡住了');
|
|
156
|
+
}
|
|
157
|
+
}, callMs);
|
|
158
|
+
pending.set(id, { resolve, reject, timer });
|
|
159
|
+
target.write(`${JSON.stringify({ id, op, args })}\n`);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function ready() {
|
|
163
|
+
if (halted)
|
|
164
|
+
return Promise.reject(new Error(halted));
|
|
165
|
+
if (link)
|
|
166
|
+
return Promise.resolve(link);
|
|
167
|
+
if (!deps.elevated)
|
|
168
|
+
return Promise.resolve(link = startChild());
|
|
169
|
+
return starting ??= startElevated().then(made => { link = made; deps.faults.clear(FAULT); return made; }, (error) => {
|
|
170
|
+
halted = `前台模式没有打开:${errorText(error)}`;
|
|
171
|
+
deps.faults.set(FAULT, halted);
|
|
172
|
+
throw new Error(halted);
|
|
173
|
+
}).finally(() => { starting = undefined; });
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
/** One helper operation by name, e.g. `windows` or `click`; a refusal rejects with the helper's reason. */
|
|
177
|
+
call(op, args = {}) {
|
|
178
|
+
// A helper already up takes the call at once, so stopping right after a call still fails it.
|
|
179
|
+
if (!link && !deps.elevated && !halted)
|
|
180
|
+
link = startChild();
|
|
181
|
+
return link ? send(link, op, args) : ready().then(target => send(target, op, args));
|
|
182
|
+
},
|
|
183
|
+
/** Starts it now rather than on first use: foreground mode asks for administrator rights when it is turned on. */
|
|
184
|
+
start: async () => { await ready(); },
|
|
185
|
+
running: () => link !== undefined,
|
|
186
|
+
/** Ends the helper and forgets a halted start, so turning the switch on again tries afresh. */
|
|
187
|
+
stop() {
|
|
188
|
+
const ending = link;
|
|
189
|
+
link = undefined;
|
|
190
|
+
halted = undefined;
|
|
191
|
+
failedStarts = 0;
|
|
192
|
+
failAll(stopped);
|
|
193
|
+
ending?.end();
|
|
194
|
+
deps.faults.clear(FAULT);
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/ecosystem/computer/desktop/processes.ts
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
/**
|
|
4
|
+
* Whether a program with this executable name is running, windows or not: WeChat closed to the tray has none, and
|
|
5
|
+
* launching it then would open a second copy.
|
|
6
|
+
*/
|
|
7
|
+
export function isRunning(executable) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
execFile('tasklist', ['/FI', `IMAGENAME eq ${executable}.exe`, '/FO', 'CSV', '/NH'], { windowsHide: true, timeout: 10_000 }, (error, stdout) => {
|
|
10
|
+
if (error)
|
|
11
|
+
return reject(new Error(`查不到 ${executable} 有没有在运行:${error.message}`));
|
|
12
|
+
resolve(stdout.toLowerCase().includes(`"${executable.toLowerCase()}.exe"`));
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export type Recording = ReturnType<typeof createRecording>;
|
|
2
|
+
/**
|
|
3
|
+
* What is recorded: a window in background mode, the whole screen in foreground mode. `key` tells one target from
|
|
4
|
+
* another, `capture` pictures it into a file (FRAME_EDGE long) and gives its print, `space` presses the space bar on it.
|
|
5
|
+
*/
|
|
6
|
+
export interface RecordTarget {
|
|
7
|
+
key: string;
|
|
8
|
+
app: string;
|
|
9
|
+
capture(file: string, edge: number): Promise<string>;
|
|
10
|
+
space(): Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** `print` is the helper's 16×16 grey thumbnail of the frame, as hex: what two frames are compared by. */
|
|
13
|
+
export interface Frame {
|
|
14
|
+
second: number;
|
|
15
|
+
file: string;
|
|
16
|
+
print: string;
|
|
17
|
+
}
|
|
18
|
+
/** What became of the video when the recording ended: stopped by the space bar, still running after it, or already not moving. */
|
|
19
|
+
export type Paused = 'paused' | 'running' | 'still';
|
|
20
|
+
/** A recording is at most this long and this many frames: ten minutes at one a second is some tens of megabytes. */
|
|
21
|
+
export declare const MAX_SECONDS = 600, MAX_FRAMES = 600;
|
|
22
|
+
/** Frames on one sheet, and a tile's width: twelve tiles read as one picture, more turn to mush. */
|
|
23
|
+
export declare const SHEET_FRAMES = 12;
|
|
24
|
+
/** `65s 1:05`: the second she passes back, and the clock reading a person would look for. */
|
|
25
|
+
export declare const frameLabel: (second: number) => string;
|
|
26
|
+
/** Up to `max` of the frames, spread evenly, first and last included. */
|
|
27
|
+
export declare function spread<T>(frames: readonly T[], max: number): T[];
|
|
28
|
+
/** Whether the picture changed between two frames; prints that cannot be compared count as changed, never as a false "still". */
|
|
29
|
+
export declare function moved(before: string, after: string): boolean;
|
|
30
|
+
/** A recording of something that was not playing: nearly every frame is the one before it. */
|
|
31
|
+
export declare function mostlyStill(frames: readonly Frame[]): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* How she watches something that moves: the window (or the screen) is pictured at a steady pace into a folder of frames, and what
|
|
34
|
+
* she looks at afterwards is always that folder, never the program again. One recording at a time; a new one, or the
|
|
35
|
+
* switch going off, deletes the last. The frames are pictures of the window only, with no sound.
|
|
36
|
+
*
|
|
37
|
+
* A video goes on playing while she reads what was recorded, and is somewhere else by the time she records again. So
|
|
38
|
+
* when a recording ends on a moving picture the space bar is pressed for her, which pauses nearly every player, and
|
|
39
|
+
* the next recording of the same window presses it again to go on. It is a guess: what came of it is looked at and told.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createRecording(deps: {
|
|
42
|
+
call(op: string, args?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
43
|
+
secondMs?: number;
|
|
44
|
+
}): {
|
|
45
|
+
/** Starts over a target; the first frame is taken before this returns, so one that cannot be pictured fails here. */
|
|
46
|
+
start(target: RecordTarget, seconds: unknown, every?: unknown, pauseAfter?: boolean): Promise<{
|
|
47
|
+
seconds: number;
|
|
48
|
+
every: number;
|
|
49
|
+
endsAt: Date;
|
|
50
|
+
resumed: boolean;
|
|
51
|
+
}>;
|
|
52
|
+
/** How it stands: what is recorded so far, whether it is over, and why it stopped early if it did. */
|
|
53
|
+
state(): {
|
|
54
|
+
app: string;
|
|
55
|
+
seconds: number;
|
|
56
|
+
every: number;
|
|
57
|
+
frames: Frame[];
|
|
58
|
+
ended: boolean;
|
|
59
|
+
stopping: boolean;
|
|
60
|
+
left: number;
|
|
61
|
+
resumed: boolean;
|
|
62
|
+
cut?: string;
|
|
63
|
+
paused?: Paused;
|
|
64
|
+
};
|
|
65
|
+
/** Resolves when the recording is over. */
|
|
66
|
+
finished: () => Promise<void>;
|
|
67
|
+
/** Ends it where it is and keeps what was recorded; over once the frame being taken, if any, is in. */
|
|
68
|
+
stop(quiet?: boolean): Promise<void>;
|
|
69
|
+
/** Whether a recording is still going. */
|
|
70
|
+
running: () => boolean;
|
|
71
|
+
/** She touched the window herself, so whether it is paused is no longer this module's to know. */
|
|
72
|
+
touched(): void;
|
|
73
|
+
/** Some frames as one picture, each with its time in the corner. */
|
|
74
|
+
sheet(frames: readonly Frame[]): Promise<Buffer>;
|
|
75
|
+
one: (target: Frame) => Buffer;
|
|
76
|
+
clear: () => void;
|
|
77
|
+
};
|