dsh-world 0.7.5 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/CHANGELOG.md +541 -446
  2. package/CONTRIBUTING.md +174 -163
  3. package/INSTALL.md +314 -314
  4. package/NOTES.md +98 -5
  5. package/README.md +332 -258
  6. package/client/index.js +1126 -986
  7. package/lib/compaction.d.ts +7 -1
  8. package/lib/compaction.js +8 -3
  9. package/lib/console/routes.d.ts +11 -3
  10. package/lib/console/routes.js +12 -7
  11. package/lib/console/snapshot.d.ts +2 -0
  12. package/lib/console/snapshot.js +1 -0
  13. package/lib/ecosystem/computer/browser/browser.d.ts +34 -0
  14. package/lib/ecosystem/computer/browser/browser.js +101 -0
  15. package/lib/ecosystem/computer/browser/cdp.d.ts +22 -0
  16. package/lib/ecosystem/computer/browser/cdp.js +91 -0
  17. package/lib/ecosystem/computer/browser/edge.d.ts +15 -0
  18. package/lib/ecosystem/computer/browser/edge.js +80 -0
  19. package/lib/ecosystem/computer/browser/page.d.ts +41 -0
  20. package/lib/ecosystem/computer/browser/page.js +140 -0
  21. package/lib/ecosystem/computer/browser/pageLib.d.ts +7 -0
  22. package/lib/ecosystem/computer/browser/pageLib.js +106 -0
  23. package/lib/ecosystem/computer/desktop/desktop.d.ts +91 -0
  24. package/lib/ecosystem/computer/desktop/desktop.js +322 -0
  25. package/lib/ecosystem/computer/desktop/driver.d.ts +34 -0
  26. package/lib/ecosystem/computer/desktop/driver.js +197 -0
  27. package/lib/ecosystem/computer/desktop/processes.d.ts +5 -0
  28. package/lib/ecosystem/computer/desktop/processes.js +15 -0
  29. package/lib/ecosystem/computer/desktop/recording.d.ts +77 -0
  30. package/lib/ecosystem/computer/desktop/recording.js +187 -0
  31. package/lib/ecosystem/computer/desktop/screen.d.ts +33 -0
  32. package/lib/ecosystem/computer/desktop/screen.js +182 -0
  33. package/lib/ecosystem/computer/desktop/turns.d.ts +58 -0
  34. package/lib/ecosystem/computer/desktop/turns.js +94 -0
  35. package/lib/ecosystem/computer/desktop/watching.d.ts +25 -0
  36. package/lib/ecosystem/computer/desktop/watching.js +81 -0
  37. package/lib/ecosystem/computer/inbox/inbox.d.ts +32 -0
  38. package/lib/ecosystem/computer/inbox/inbox.js +142 -0
  39. package/lib/ecosystem/computer/index.d.ts +82 -0
  40. package/lib/ecosystem/computer/index.js +133 -0
  41. package/lib/ecosystem/computer/procedures/alarm.d.ts +13 -0
  42. package/lib/ecosystem/computer/procedures/alarm.js +46 -0
  43. package/lib/ecosystem/computer/procedures/archive.d.ts +32 -0
  44. package/lib/ecosystem/computer/procedures/archive.js +68 -0
  45. package/lib/ecosystem/computer/procedures/catalog.d.ts +7 -0
  46. package/lib/ecosystem/computer/procedures/catalog.js +75 -0
  47. package/lib/ecosystem/computer/procedures/format.d.ts +167 -0
  48. package/lib/ecosystem/computer/procedures/format.js +332 -0
  49. package/lib/ecosystem/computer/procedures/library.d.ts +53 -0
  50. package/lib/ecosystem/computer/procedures/library.js +178 -0
  51. package/lib/ecosystem/computer/procedures/run.d.ts +80 -0
  52. package/lib/ecosystem/computer/procedures/run.js +245 -0
  53. package/lib/ecosystem/computer/tools.d.ts +21 -0
  54. package/lib/ecosystem/computer/tools.js +197 -0
  55. package/lib/ecosystem/qq-native/images.d.ts +8 -1
  56. package/lib/ecosystem/qq-native/images.js +34 -15
  57. package/lib/ecosystem/qq-native/index.js +5 -5
  58. package/lib/ecosystem/shared/turn.d.ts +3 -0
  59. package/lib/ecosystem/shared/turn.js +13 -3
  60. package/lib/ecosystem/wechat/home.d.ts +1 -0
  61. package/lib/ecosystem/wechat/home.js +9 -3
  62. package/lib/ecosystem/wechat/images.js +3 -5
  63. package/lib/ecosystem/wechat/index.d.ts +6 -0
  64. package/lib/ecosystem/wechat/index.js +3 -1
  65. package/lib/ecosystem/wechat/presence.d.ts +5 -1
  66. package/lib/ecosystem/wechat/presence.js +22 -13
  67. package/lib/ecosystem/wechat/reading.d.ts +1 -1
  68. package/lib/ecosystem/wechat/reading.js +2 -2
  69. package/lib/elsewhere.d.ts +34 -0
  70. package/lib/elsewhere.js +40 -0
  71. package/lib/history.d.ts +5 -0
  72. package/lib/history.js +7 -1
  73. package/lib/hostContract.d.ts +6 -0
  74. package/lib/images.d.ts +30 -0
  75. package/lib/images.js +101 -0
  76. package/lib/index.d.ts +4 -0
  77. package/lib/index.js +98 -6
  78. package/lib/life/photoTools.js +4 -15
  79. package/lib/life/runtime.d.ts +12 -2
  80. package/lib/life/runtime.js +93 -12
  81. package/lib/life/toolRegistry.d.ts +9 -3
  82. package/lib/life/toolRegistry.js +46 -27
  83. package/lib/life/turnCost.d.ts +16 -0
  84. package/lib/life/turnCost.js +44 -0
  85. package/lib/life/types.d.ts +17 -0
  86. package/lib/memoryTools.d.ts +4 -2
  87. package/lib/memoryTools.js +25 -5
  88. package/lib/mood.d.ts +5 -1
  89. package/lib/mood.js +25 -8
  90. package/lib/promptSection.d.ts +3 -0
  91. package/lib/promptSection.js +29 -42
  92. package/lib/runtime.js +44 -17
  93. package/lib/sessionNews.d.ts +17 -0
  94. package/lib/sessionNews.js +55 -0
  95. package/lib/store.d.ts +6 -0
  96. package/lib/store.js +17 -3
  97. package/lib/types.d.ts +4 -0
  98. package/lib/worldAdd.d.ts +9 -0
  99. package/lib/worldAdd.js +39 -0
  100. package/lib/worldPrompt.d.ts +3 -2
  101. package/lib/worldPrompt.js +3 -2
  102. package/lib/worldRoutes.d.ts +5 -2
  103. package/lib/worldRoutes.js +29 -4
  104. package/lib/worldSwitch.d.ts +8 -2
  105. package/lib/worldSwitch.js +11 -5
  106. package/package.json +132 -129
  107. package/src/ecosystem/README.md +2 -1
  108. package/src/ecosystem/computer/NOTICE.md +27 -0
  109. package/src/ecosystem/computer/README.md +244 -0
  110. package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
  111. package/src/ecosystem/wechat/windows/README.md +4 -2
  112. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  113. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  114. package/templates/world-prompt-memory.md +10 -10
  115. package/templates/world-prompt.md +65 -65
@@ -0,0 +1,140 @@
1
+ import { PAGE_LIB } from "./pageLib.js";
2
+ const PER_PAGE = 40, TEXT_BUDGET = 1200;
3
+ const KEYS = {
4
+ Enter: { code: 'Enter', keyCode: 13, text: '\r' }, Tab: { code: 'Tab', keyCode: 9 }, Escape: { code: 'Escape', keyCode: 27 },
5
+ Backspace: { code: 'Backspace', keyCode: 8 }, Delete: { code: 'Delete', keyCode: 46 }, Home: { code: 'Home', keyCode: 36 }, End: { code: 'End', keyCode: 35 },
6
+ ArrowUp: { code: 'ArrowUp', keyCode: 38 }, ArrowDown: { code: 'ArrowDown', keyCode: 40 }, ArrowLeft: { code: 'ArrowLeft', keyCode: 37 }, ArrowRight: { code: 'ArrowRight', keyCode: 39 },
7
+ PageUp: { code: 'PageUp', keyCode: 33 }, PageDown: { code: 'PageDown', keyCode: 34 },
8
+ };
9
+ export const PAGE_KEYS = Object.keys(KEYS);
10
+ export const describeAnchor = (anchor) => [anchor.role, anchor.name && `「${anchor.name}」`, anchor.text && `文字「${anchor.text}」`, anchor.css && `选择器 ${anchor.css}`, anchor.nth && `第 ${anchor.nth + 1} 个`].filter(Boolean).join(' ');
11
+ const describeTarget = (target) => 'ref' in target ? `编号 ${target.ref}` : describeAnchor(target.anchor);
12
+ const missing = (target) => new Error('ref' in target
13
+ ? `编号 ${target.ref} 对应的元素已经不在页面上了,先 page_read 再用新编号`
14
+ : `页面上找不到 ${describeAnchor(target.anchor)}`);
15
+ /** One numbered line per control, `perPage` of them, then the text she can see; what `page_read` returns. */
16
+ export function formatRead(snapshot, page = 1, perPage = PER_PAGE, textBudget = TEXT_BUDGET) {
17
+ const pages = Math.max(1, Math.ceil(snapshot.controls.length / perPage));
18
+ const shown = snapshot.controls.slice((page - 1) * perPage, page * perPage);
19
+ const lines = [`${snapshot.title || '(无标题)'} · ${snapshot.url}`];
20
+ for (const control of shown)
21
+ lines.push(`[${control.ref}] ${control.role} ${control.name}${control.value ? ` = ${control.value}` : ''}${control.view ? '' : '(屏外)'}`);
22
+ if (!snapshot.controls.length)
23
+ lines.push('(没有可操作的元素)');
24
+ if (pages > 1)
25
+ lines.push(`(控件第 ${Math.min(page, pages)}/${pages} 页;${page < pages ? `page=${page + 1} 看后面` : '已是最后一页'})`);
26
+ if (snapshot.text)
27
+ lines.push('—— 页面文字 ——', snapshot.text.length > textBudget ? snapshot.text.slice(0, textBudget) + '…' : snapshot.text);
28
+ return lines.join('\n');
29
+ }
30
+ export function createPage(cdp) {
31
+ // A new document restarts the page's numbering; carrying the highest number seen keeps an old number from ever naming another element.
32
+ let highest = 0;
33
+ async function call(expression) {
34
+ const result = await cdp.send('Runtime.evaluate', {
35
+ expression: `(() => {window.__dshBase = ${highest};${PAGE_LIB};\nreturn (${expression});\n})()`, returnByValue: true, awaitPromise: true,
36
+ });
37
+ if (result.exceptionDetails)
38
+ throw new Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text);
39
+ return result.result.value;
40
+ }
41
+ async function snapshot(ref) {
42
+ if (ref !== undefined && !Number.isSafeInteger(ref))
43
+ throw new Error(`编号 ${String(ref)} 不是有效的元素编号`);
44
+ const data = await call(`window.__dshWorld.snapshot(${JSON.stringify(ref ?? null)}, ${TEXT_BUDGET})`);
45
+ if ('missing' in data)
46
+ throw missing({ ref: ref });
47
+ highest = data.controls.reduce((top, control) => Number.isSafeInteger(control.ref) && control.ref > top ? control.ref : top, highest);
48
+ return data;
49
+ }
50
+ async function pointOf(target) {
51
+ const point = await call(`window.__dshWorld.point(${JSON.stringify(target)})`);
52
+ if (!point)
53
+ throw missing(target);
54
+ if ('covered' in point)
55
+ throw new Error(`${describeTarget(target)} 被别的东西挡住了(${point.by}),先关掉它或滚动一下`);
56
+ return point;
57
+ }
58
+ const mouse = (type, x, y, extra = {}) => cdp.send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1, pointerType: 'mouse', ...extra });
59
+ async function has(condition) { return await call(`window.__dshWorld.has(${JSON.stringify(condition)})`); }
60
+ return {
61
+ async open(url) {
62
+ await cdp.send('Page.enable');
63
+ let release = () => { };
64
+ const loaded = new Promise(resolve => {
65
+ const timer = setTimeout(() => { release(); resolve(); }, 15_000);
66
+ const off = cdp.on(method => { if (method === 'Page.loadEventFired') {
67
+ release();
68
+ resolve();
69
+ } });
70
+ release = () => { clearTimeout(timer); off(); };
71
+ });
72
+ try {
73
+ const navigated = await cdp.send('Page.navigate', { url });
74
+ if (navigated.errorText)
75
+ throw new Error(`打不开 ${url}:${navigated.errorText}`);
76
+ await loaded;
77
+ }
78
+ finally {
79
+ release();
80
+ }
81
+ },
82
+ async read({ page = 1, ref } = {}) { return formatRead(await snapshot(ref), page); },
83
+ async brief() { return formatRead(await snapshot(), 1, 15, 300); },
84
+ async click(target) {
85
+ const { x, y } = await pointOf(target);
86
+ await mouse('mouseMoved', x, y, { button: 'none', clickCount: 0 });
87
+ await mouse('mousePressed', x, y, { buttons: 1 });
88
+ await mouse('mouseReleased', x, y);
89
+ },
90
+ async type(target, text) {
91
+ const focused = await call(`window.__dshWorld.focus(${JSON.stringify(target)})`);
92
+ if (focused === 'missing')
93
+ throw missing(target);
94
+ if (focused === 'unfocusable')
95
+ throw new Error(`${describeTarget(target)} 这个元素不能输入`);
96
+ await cdp.send('Input.insertText', { text });
97
+ },
98
+ async press(key) {
99
+ const spec = KEYS[key];
100
+ if (!spec)
101
+ throw new Error(`不认识按键 ${key},可用:${PAGE_KEYS.join('、')}`);
102
+ const base = { key, code: spec.code, windowsVirtualKeyCode: spec.keyCode, nativeVirtualKeyCode: spec.keyCode };
103
+ await cdp.send('Input.dispatchKeyEvent', { type: 'keyDown', ...base, ...(spec.text ? { text: spec.text } : {}) });
104
+ await cdp.send('Input.dispatchKeyEvent', { type: 'keyUp', ...base });
105
+ },
106
+ async scroll(direction, target) {
107
+ const { x, y } = target ? await pointOf(target) : await call('({ x: innerWidth / 2, y: innerHeight / 2 })');
108
+ await mouse('mouseWheel', x, y, { button: 'none', clickCount: 0, deltaX: 0, deltaY: direction === 'down' ? 600 : -600 });
109
+ },
110
+ async wait(condition, timeoutMs, signal) {
111
+ const deadline = Date.now() + timeoutMs;
112
+ let lastError = '';
113
+ for (;;) {
114
+ signal?.throwIfAborted();
115
+ try {
116
+ if (await has(condition))
117
+ return;
118
+ }
119
+ catch (error) {
120
+ lastError = error instanceof Error ? error.message : String(error); /* the page may be between documents */
121
+ }
122
+ if (Date.now() >= deadline)
123
+ throw new Error(`等了 ${Math.round(timeoutMs / 1000)} 秒没等到${lastError ? `(最后一次出错:${lastError})` : ''}`);
124
+ await new Promise(resolve => setTimeout(resolve, 250));
125
+ }
126
+ },
127
+ has,
128
+ async extract(spec) { return await call(`window.__dshWorld.extract(${JSON.stringify(spec)})`); },
129
+ async evaluate(script, args) {
130
+ const result = await cdp.send('Runtime.evaluate', {
131
+ expression: `Promise.race([(async (args) => {\n${script}\n})(${JSON.stringify(args)}), new Promise((_, reject) => setTimeout(() => reject(new Error('页面脚本超过十秒没有结束')), 10000))])`, returnByValue: true, awaitPromise: true, timeout: 10_000,
132
+ });
133
+ if (result.exceptionDetails)
134
+ throw new Error(`页面脚本出错:${result.exceptionDetails.exception?.description ?? result.exceptionDetails.text}`);
135
+ return result.result.value ?? null;
136
+ },
137
+ async shot() { return Buffer.from((await cdp.send('Page.captureScreenshot', { format: 'png' })).data, 'base64'); },
138
+ async bringToFront() { await cdp.send('Page.bringToFront'); },
139
+ };
140
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Installed into her page on every call; a no-op while the same version is already there, which is what keeps an
3
+ * element's number the same across reads until the page navigates. Roles and names are computed here once, so a read
4
+ * and an anchor in one of her tools mean the same thing. The registry, roles, names and visibility rules are ported
5
+ * from dsh-inner-browser-use's read.js (MIT, see ../NOTICE.md).
6
+ */
7
+ export declare const PAGE_LIB: string;
@@ -0,0 +1,106 @@
1
+ // src/ecosystem/computer/browser/pageLib.ts
2
+ /**
3
+ * Installed into her page on every call; a no-op while the same version is already there, which is what keeps an
4
+ * element's number the same across reads until the page navigates. Roles and names are computed here once, so a read
5
+ * and an anchor in one of her tools mean the same thing. The registry, roles, names and visibility rules are ported
6
+ * from dsh-inner-browser-use's read.js (MIT, see ../NOTICE.md).
7
+ */
8
+ export const PAGE_LIB = String.raw `(() => {
9
+ const VERSION = 1;
10
+ if (window.__dshWorld && window.__dshWorld.version === VERSION) return;
11
+ const SELECTOR = 'a[href],button,input:not([type="hidden"]),select,textarea,summary,label[for],[contenteditable="true"],[tabindex]:not([tabindex="-1"]),[role="button"],[role="link"],[role="tab"],[role="checkbox"],[role="radio"],[role="switch"],[role="combobox"],[role="menuitem"],[role="option"],[role="textbox"],[role="searchbox"]';
12
+ const refs = new Map(); const ids = new WeakMap(); let seq = Number(window.__dshBase) || 0;
13
+ const clean = s => String(s == null ? '' : s).replace(/\s+/g, ' ').trim();
14
+ const id = el => { const known = ids.get(el); if (known !== undefined && refs.get(known) === el) return known; seq += 1; ids.set(el, seq); refs.set(seq, el); return seq; };
15
+ const roots = () => { const out = [document]; for (let i = 0; i < out.length; i++) for (const el of out[i].querySelectorAll('*')) if (el.shadowRoot) out.push(el.shadowRoot); return out; };
16
+ // Walking the shadow roots is the costly part, so each exported call does it once and passes the list down.
17
+ const all = (selector, scope, rs) => scope ? Array.from(scope.querySelectorAll(selector)) : rs.flatMap(root => Array.from(root.querySelectorAll(selector)));
18
+ const role = el => {
19
+ const explicit = el.getAttribute('role'); if (explicit) return explicit;
20
+ const tag = el.tagName.toLowerCase();
21
+ if (tag === 'a') return 'link'; if (tag === 'summary') return 'button'; if (tag === 'li') return 'listitem';
22
+ if (tag === 'textarea' || el.isContentEditable) return 'textbox';
23
+ if (tag === 'input') { const type = String(el.type || 'text'); return type === 'submit' || type === 'button' ? 'button' : type === 'checkbox' || type === 'radio' ? type : 'textbox'; }
24
+ return tag;
25
+ };
26
+ const name = el => {
27
+ const labelled = clean(String(el.getAttribute('aria-labelledby') || '').split(/\s+/).filter(Boolean).map(x => { const o = el.ownerDocument.getElementById(x); return o ? (o.innerText || o.textContent) : ''; }).join(' '));
28
+ const label = el.labels && el.labels[0] ? clean(el.labels[0].innerText) : '';
29
+ const value = el.tagName === 'INPUT' && el.type !== 'password' ? el.value : '';
30
+ return clean(labelled || el.getAttribute('aria-label') || label || el.getAttribute('title') || el.getAttribute('placeholder') || el.innerText || value || el.getAttribute('alt') || '').slice(0, 140);
31
+ };
32
+ const shown = el => { const b = el.getBoundingClientRect(); if (b.width < 2 || b.height < 2) return false; const s = getComputedStyle(el); return s.visibility !== 'hidden' && s.display !== 'none' && Number(s.opacity) > 0.05; };
33
+ const inView = el => { const b = el.getBoundingClientRect(); return b.bottom > 0 && b.right > 0 && b.top < innerHeight && b.left < innerWidth; };
34
+ const matches = (el, a) => (!a.role || role(el) === a.role) && (!a.name || name(el).includes(a.name)) && (!a.text || (clean(el.textContent).includes(a.text) && clean(el.innerText).includes(a.text)));
35
+ const visibleMatches = (a, scope, rs) => all(a.css || '*', scope, rs).filter(el => matches(el, a) && shown(el));
36
+ // The innermost of nested matches: a page wraps the same words in several boxes.
37
+ const innermost = hits => hits.filter(el => !hits.some(other => other !== el && el.contains(other)));
38
+ const locate = (a, scope, rs) => innermost(visibleMatches(a, scope, rs))[a.nth || 0] || null;
39
+ const find = ref => { const el = refs.get(ref); if (!el || !el.isConnected) { refs.delete(ref); return null; } return el; };
40
+ const target = (t, rs) => t.ref !== undefined ? find(t.ref) : locate(t.anchor, null, rs);
41
+ const reaches = (el, hit) => { for (let n = el; n; n = n.getRootNode().host) if (n === hit || n.contains(hit)) return true; return false; };
42
+ const controls = (scope, rs) => {
43
+ const out = [];
44
+ for (const el of all(SELECTOR, scope, rs)) {
45
+ if (!shown(el)) continue;
46
+ const label = name(el);
47
+ if (!label && !['INPUT', 'SELECT', 'TEXTAREA'].includes(el.tagName)) continue;
48
+ out.push({ ref: id(el), role: role(el), name: label, view: inView(el), value: el.tagName === 'INPUT' && el.type !== 'password' ? clean(el.value).slice(0, 80) : '' });
49
+ }
50
+ return out.sort((a, b) => Number(b.view) - Number(a.view));
51
+ };
52
+ const visibleText = (scope, budget) => {
53
+ const parts = []; let used = 0;
54
+ const walker = document.createTreeWalker(scope || document.querySelector('main, article, [role="main"]') || document.body, NodeFilter.SHOW_TEXT);
55
+ for (let node = walker.nextNode(); node && used < budget; node = walker.nextNode()) {
56
+ const words = clean(node.nodeValue); const parent = node.parentElement;
57
+ if (!words || !parent || !shown(parent) || !inView(parent)) continue;
58
+ parts.push(words); used += words.length + 1;
59
+ }
60
+ return parts.join('\n');
61
+ };
62
+ window.__dshWorld = {
63
+ version: VERSION,
64
+ snapshot(ref, budget) {
65
+ const scope = ref == null ? null : find(ref);
66
+ if (ref != null && !scope) return { missing: true };
67
+ return { url: location.href, title: document.title, controls: controls(scope, scope ? null : roots()), text: visibleText(scope, budget + 1) };
68
+ },
69
+ point(t) {
70
+ const el = target(t, roots()); if (!el) return null;
71
+ el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
72
+ const b = el.getClientRects()[0] || el.getBoundingClientRect();
73
+ const x = b.left + b.width / 2, y = b.top + b.height / 2;
74
+ const hit = document.elementFromPoint(x, y);
75
+ if (!(hit && reaches(el, hit))) return { covered: true, by: hit ? hit.tagName.toLowerCase() + ' ' + name(hit).slice(0, 30) : '页面之外' };
76
+ return { x, y };
77
+ },
78
+ focus(t) {
79
+ const el = target(t, roots()); if (!el) return 'missing';
80
+ el.scrollIntoView({ block: 'center', behavior: 'instant' }); el.focus();
81
+ if (!(document.activeElement === el || el.contains(document.activeElement))) return 'unfocusable';
82
+ // Typing replaces what is there, as a person would after selecting it.
83
+ try {
84
+ if (el.select) el.select();
85
+ else if (el.isContentEditable) { const range = document.createRange(); range.selectNodeContents(el); const sel = getSelection(); sel.removeAllRanges(); sel.addRange(range); }
86
+ } catch (e) { /* an input type that cannot select: typing then adds */ }
87
+ return 'ok';
88
+ },
89
+ has(c) {
90
+ return (!c.text || (!!document.body && clean(document.body.innerText).includes(c.text))) && (!c.url || location.href.includes(c.url)) && (!c.anchor || !!locate(c.anchor, null, roots()));
91
+ },
92
+ extract(spec) {
93
+ const rs = roots();
94
+ // Every visible item in document order; nested items keep their parents.
95
+ return visibleMatches(spec.list, null, rs).slice(0, spec.limit || 8).map(item => {
96
+ const row = {};
97
+ for (const key of Object.keys(spec.fields)) {
98
+ const field = spec.fields[key];
99
+ const el = !field.role && !field.name && !field.text && !field.css ? item : locate(field, item, null);
100
+ row[key] = !el ? '' : field.attr ? clean(el.getAttribute(field.attr)).slice(0, 200) : clean(el.innerText || el.textContent).slice(0, 200);
101
+ }
102
+ return row;
103
+ });
104
+ },
105
+ };
106
+ })()`;
@@ -0,0 +1,91 @@
1
+ import type { DesktopDriver } from './driver.ts';
2
+ import { type DesktopResult } from './turns.ts';
3
+ export { STOPPED, type DesktopAction, type DesktopResult } from './turns.ts';
4
+ export type Desktop = ReturnType<typeof createDesktop>;
5
+ /** The screenshot's long edge; her coordinates are in this image's pixels and are mapped back to the window's. */
6
+ export declare const IMAGE_EDGE = 1280;
7
+ /** A zoomed part is enlarged at most this many times, past which it only gets blurrier. */
8
+ export declare const ZOOM_GROW = 4;
9
+ /** Mouse-wheel notches per scroll: about a screenful in most programs. */
10
+ export declare const NOTCHES = 3;
11
+ export declare const VK: Record<string, number>;
12
+ export declare const DESKTOP_KEYS = "return\u3001tab\u3001escape\u3001space\u3001backspace\u3001delete\u3001home\u3001end\u3001pageup\u3001pagedown\u3001\u65B9\u5411\u952E up/down/left/right\u3001f1\u2013f12\u3001\u5355\u4E2A\u5B57\u6BCD\u6216\u6570\u5B57\uFF0C\u53EF\u52A0 ctrl+\u3001shift+\u3001alt+";
13
+ /** `owner` is the window it belongs to (0 for a main window): dialogs and panels such as WeChat's Moments composer. */
14
+ export interface Listed {
15
+ hwnd: number;
16
+ owner: number;
17
+ pid: number;
18
+ exe: string;
19
+ title: string;
20
+ minimized: boolean;
21
+ width: number;
22
+ height: number;
23
+ }
24
+ export declare const largest: (windows: Listed[]) => Listed;
25
+ /** A key as people write it (`Enter`, `ctrl+f`) to its virtual-key code and modifiers; anything else is refused. */
26
+ export declare function desktopKey(text: string): {
27
+ vk: number;
28
+ modifiers: number[];
29
+ };
30
+ /** The longest she can hold a button or a key: the helper does nothing else meanwhile, and cannot be stopped mid-press. */
31
+ export declare const MAX_HOLD_SECONDS = 10;
32
+ /** `hold` (seconds) as milliseconds, 0 for a plain press; out of range is refused. */
33
+ export declare function holdMs(hold: number | undefined): number;
34
+ /** What she is told she did: a press, or a press held for so long. */
35
+ /** What she is told after a drag: where it went, and how long it was held first. */
36
+ export declare const dragged: (hold: number | undefined) => string;
37
+ export declare const pressed: (what: string, hold: number | undefined) => string;
38
+ /** The program's windows, by her name for it: an exact title, the executable, then a title that starts with it. */
39
+ export declare function windowsNamed(windows: Listed[], name: string): Listed[];
40
+ /**
41
+ * Her turn on the partner's computer: one current window, its last picture, and what she did. She sees screenshots
42
+ * and acts by their pixels, the way a person does. Input is posted to the window, so the partner's pointer and focus
43
+ * stay theirs; a window that is brought up or launched hands the focus straight back. One action at a time, in order.
44
+ */
45
+ export declare function createDesktop(deps: {
46
+ driver: Pick<DesktopDriver, 'call'>;
47
+ running?(executable: string): Promise<boolean>;
48
+ settleMs?: number;
49
+ appearMs?: number;
50
+ secondMs?: number;
51
+ /** Her name on the banner the partner sees while she acts; without it there is no banner. */
52
+ companion?(): string;
53
+ /** A banner that could not be drawn: said in the console's health, never a failed action of hers. */
54
+ signFault?(text: string | undefined): void;
55
+ lingerMs?: number;
56
+ }): {
57
+ actions: string[];
58
+ act: (input: DesktopInput) => Promise<DesktopResult>;
59
+ recent: () => import("./turns.ts").DesktopAction[];
60
+ halt: () => {
61
+ cut: boolean;
62
+ recording: boolean;
63
+ };
64
+ release: () => void;
65
+ halted: () => boolean;
66
+ /** When the switch goes off: nothing she opened stays "current" for a later turn, and what she recorded is deleted. */
67
+ reset(): void;
68
+ };
69
+ export interface DesktopInput {
70
+ action: string;
71
+ app?: string;
72
+ x?: number;
73
+ y?: number;
74
+ x2?: number;
75
+ y2?: number;
76
+ text?: string;
77
+ submit?: boolean;
78
+ key?: string;
79
+ direction?: string;
80
+ double?: boolean;
81
+ right?: boolean;
82
+ /** For `click`, `drag` and `press`: seconds to keep the button or the key down (a long press, or walking in a game). */
83
+ hold?: number;
84
+ /** For `record`: how long, and how far apart the frames are. For `review`: a stretch of the recording, or one moment of it. */
85
+ seconds?: number;
86
+ every?: number;
87
+ from?: number;
88
+ to?: number;
89
+ at?: number;
90
+ pause?: boolean;
91
+ }