screenhand 0.2.0 → 0.3.1

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 (212) hide show
  1. package/README.md +165 -446
  2. package/bin/darwin-arm64/macos-bridge +0 -0
  3. package/dist/mcp-desktop.js +3615 -400
  4. package/dist/scripts/export-help-center.js +112 -0
  5. package/dist/scripts/marketing-loop.js +117 -0
  6. package/dist/scripts/observer-daemon.js +288 -0
  7. package/dist/scripts/orchestrator-daemon.js +399 -0
  8. package/dist/scripts/threads-campaign.js +208 -0
  9. package/dist/src/community/fetcher.js +109 -0
  10. package/dist/src/community/index.js +6 -0
  11. package/dist/src/community/publisher.js +191 -0
  12. package/dist/src/community/remote-api.js +121 -0
  13. package/dist/src/community/types.js +3 -0
  14. package/dist/src/community/validator.js +95 -0
  15. package/dist/src/context-tracker.js +489 -0
  16. package/dist/src/ingestion/coverage-auditor.js +233 -0
  17. package/dist/src/ingestion/doc-parser.js +164 -0
  18. package/dist/src/ingestion/index.js +8 -0
  19. package/dist/src/ingestion/menu-scanner.js +152 -0
  20. package/dist/src/ingestion/reference-merger.js +186 -0
  21. package/dist/src/ingestion/shortcut-extractor.js +180 -0
  22. package/dist/src/ingestion/tutorial-extractor.js +170 -0
  23. package/dist/src/ingestion/types.js +3 -0
  24. package/dist/src/jobs/manager.js +82 -14
  25. package/dist/src/jobs/runner.js +138 -15
  26. package/dist/src/learning/engine.js +356 -0
  27. package/dist/src/learning/index.js +9 -0
  28. package/dist/src/learning/locator-policy.js +120 -0
  29. package/dist/src/learning/pattern-policy.js +89 -0
  30. package/dist/src/learning/recovery-policy.js +116 -0
  31. package/dist/src/learning/sensor-policy.js +115 -0
  32. package/dist/src/learning/timing-model.js +204 -0
  33. package/dist/src/learning/topology-policy.js +90 -0
  34. package/dist/src/learning/types.js +9 -0
  35. package/dist/src/logging/timeline-logger.js +4 -1
  36. package/dist/src/memory/playbook-seeds.js +200 -0
  37. package/dist/src/memory/recall.js +60 -8
  38. package/dist/src/memory/service.js +30 -5
  39. package/dist/src/memory/store.js +34 -5
  40. package/dist/src/native/bridge-client.js +253 -31
  41. package/dist/src/observer/state.js +199 -0
  42. package/dist/src/observer/types.js +43 -0
  43. package/dist/src/orchestrator/state.js +68 -0
  44. package/dist/src/orchestrator/types.js +22 -0
  45. package/dist/src/perception/ax-source.js +162 -0
  46. package/dist/src/perception/cdp-source.js +162 -0
  47. package/dist/src/perception/coordinator.js +771 -0
  48. package/dist/src/perception/frame-differ.js +287 -0
  49. package/dist/src/perception/index.js +22 -0
  50. package/dist/src/perception/manager.js +199 -0
  51. package/dist/src/perception/types.js +47 -0
  52. package/dist/src/perception/vision-source.js +399 -0
  53. package/dist/src/planner/deterministic.js +298 -0
  54. package/dist/src/planner/executor.js +870 -0
  55. package/dist/src/planner/goal-store.js +92 -0
  56. package/dist/src/planner/index.js +21 -0
  57. package/dist/src/planner/planner.js +520 -0
  58. package/dist/src/planner/tool-registry.js +71 -0
  59. package/dist/src/planner/types.js +22 -0
  60. package/dist/src/platform/explorer.js +213 -0
  61. package/dist/src/platform/help-center-markdown.js +527 -0
  62. package/dist/src/platform/learner.js +257 -0
  63. package/dist/src/playbook/engine.js +296 -11
  64. package/dist/src/playbook/mcp-recorder.js +204 -0
  65. package/dist/src/playbook/recorder.js +3 -2
  66. package/dist/src/playbook/runner.js +1 -1
  67. package/dist/src/playbook/store.js +139 -10
  68. package/dist/src/recovery/detectors.js +156 -0
  69. package/dist/src/recovery/engine.js +327 -0
  70. package/dist/src/recovery/index.js +20 -0
  71. package/dist/src/recovery/strategies.js +274 -0
  72. package/dist/src/recovery/types.js +20 -0
  73. package/dist/src/runtime/accessibility-adapter.js +55 -18
  74. package/dist/src/runtime/applescript-adapter.js +8 -2
  75. package/dist/src/runtime/cdp-chrome-adapter.js +1 -1
  76. package/dist/src/runtime/executor.js +23 -3
  77. package/dist/src/runtime/locator-cache.js +24 -2
  78. package/dist/src/runtime/service.js +59 -15
  79. package/dist/src/runtime/session-manager.js +4 -1
  80. package/dist/src/runtime/vision-adapter.js +2 -1
  81. package/dist/src/state/app-map-types.js +72 -0
  82. package/dist/src/state/app-map.js +1974 -0
  83. package/dist/src/state/entity-tracker.js +108 -0
  84. package/dist/src/state/fusion.js +96 -0
  85. package/dist/src/state/index.js +21 -0
  86. package/dist/src/state/ladder-generator.js +236 -0
  87. package/dist/src/state/persistence.js +156 -0
  88. package/dist/src/state/types.js +17 -0
  89. package/dist/src/state/world-model.js +1456 -0
  90. package/dist/src/util/atomic-write.js +19 -4
  91. package/dist/src/util/sanitize.js +146 -0
  92. package/dist-app-maps/com.figma.Desktop.json +959 -0
  93. package/dist-app-maps/com.hnc.Discord.json +1146 -0
  94. package/dist-app-maps/notion.id.json +2831 -0
  95. package/dist-playbooks/canva-screenhand-carousel.json +445 -0
  96. package/dist-playbooks/codex-desktop.json +76 -0
  97. package/dist-playbooks/competitor-research-stack.json +122 -0
  98. package/dist-playbooks/davinci-color-grade.json +153 -0
  99. package/dist-playbooks/davinci-edit-timeline.json +162 -0
  100. package/dist-playbooks/davinci-render.json +114 -0
  101. package/dist-playbooks/devto.json +52 -0
  102. package/dist-playbooks/discord.json +41 -0
  103. package/dist-playbooks/google-flow-create-project.json +59 -0
  104. package/dist-playbooks/google-flow-edit-image.json +90 -0
  105. package/dist-playbooks/google-flow-edit-video.json +90 -0
  106. package/dist-playbooks/google-flow-generate-image.json +68 -0
  107. package/dist-playbooks/google-flow-generate-video.json +191 -0
  108. package/dist-playbooks/google-flow-open-project.json +48 -0
  109. package/dist-playbooks/google-flow-open-scenebuilder.json +64 -0
  110. package/dist-playbooks/google-flow-search-assets.json +64 -0
  111. package/dist-playbooks/instagram.json +57 -0
  112. package/dist-playbooks/linkedin.json +52 -0
  113. package/dist-playbooks/n8n.json +43 -0
  114. package/dist-playbooks/reddit.json +52 -0
  115. package/dist-playbooks/threads.json +59 -0
  116. package/dist-playbooks/x-twitter.json +59 -0
  117. package/dist-playbooks/youtube.json +59 -0
  118. package/dist-references/canva.json +646 -0
  119. package/dist-references/codex-desktop.json +305 -0
  120. package/dist-references/davinci-resolve-keyboard.json +594 -0
  121. package/dist-references/davinci-resolve-menu-map.json +1139 -0
  122. package/dist-references/davinci-resolve-menus-batch1.json +116 -0
  123. package/dist-references/davinci-resolve-menus-batch2.json +372 -0
  124. package/dist-references/davinci-resolve-menus-batch3.json +330 -0
  125. package/dist-references/davinci-resolve-menus-batch4.json +297 -0
  126. package/dist-references/davinci-resolve-shortcuts.json +333 -0
  127. package/dist-references/devpost.json +186 -0
  128. package/dist-references/devto.json +317 -0
  129. package/dist-references/discord.json +549 -0
  130. package/dist-references/figma.json +1186 -0
  131. package/dist-references/finder.json +146 -0
  132. package/dist-references/google-ads-transparency.json +95 -0
  133. package/dist-references/google-flow.json +649 -0
  134. package/dist-references/instagram.json +341 -0
  135. package/dist-references/linkedin.json +324 -0
  136. package/dist-references/meta-ad-library.json +86 -0
  137. package/dist-references/n8n.json +387 -0
  138. package/dist-references/notes.json +27 -0
  139. package/dist-references/notion.json +163 -0
  140. package/dist-references/reddit.json +341 -0
  141. package/dist-references/threads.json +337 -0
  142. package/dist-references/x-twitter.json +403 -0
  143. package/dist-references/youtube.json +373 -0
  144. package/native/macos-bridge/Package.swift +22 -0
  145. package/native/macos-bridge/Sources/AccessibilityBridge.swift +482 -0
  146. package/native/macos-bridge/Sources/AppManagement.swift +339 -0
  147. package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +537 -0
  148. package/native/macos-bridge/Sources/ObserverBridge.swift +120 -0
  149. package/native/macos-bridge/Sources/StreamCapture.swift +136 -0
  150. package/native/macos-bridge/Sources/VisionBridge.swift +238 -0
  151. package/native/macos-bridge/Sources/main.swift +498 -0
  152. package/native/windows-bridge/AppManagement.cs +234 -0
  153. package/native/windows-bridge/InputBridge.cs +436 -0
  154. package/native/windows-bridge/Program.cs +270 -0
  155. package/native/windows-bridge/ScreenCapture.cs +453 -0
  156. package/native/windows-bridge/UIAutomationBridge.cs +571 -0
  157. package/native/windows-bridge/WindowsBridge.csproj +17 -0
  158. package/package.json +12 -1
  159. package/scripts/postinstall.cjs +127 -0
  160. package/dist/.audit-log.jsonl +0 -55
  161. package/dist/.screenhand/memory/.lock +0 -1
  162. package/dist/.screenhand/memory/actions.jsonl +0 -85
  163. package/dist/.screenhand/memory/errors.jsonl +0 -5
  164. package/dist/.screenhand/memory/errors.jsonl.bak +0 -4
  165. package/dist/.screenhand/memory/state.json +0 -35
  166. package/dist/.screenhand/memory/state.json.bak +0 -35
  167. package/dist/.screenhand/memory/strategies.jsonl +0 -12
  168. package/dist/agent/cli.js +0 -73
  169. package/dist/agent/loop.js +0 -258
  170. package/dist/config.js +0 -9
  171. package/dist/index.js +0 -56
  172. package/dist/logging/timeline-logger.js +0 -29
  173. package/dist/mcp/mcp-stdio-server.js +0 -448
  174. package/dist/mcp/server.js +0 -347
  175. package/dist/mcp-entry.js +0 -59
  176. package/dist/memory/recall.js +0 -160
  177. package/dist/memory/research.js +0 -98
  178. package/dist/memory/seeds.js +0 -89
  179. package/dist/memory/session.js +0 -161
  180. package/dist/memory/store.js +0 -391
  181. package/dist/memory/types.js +0 -4
  182. package/dist/monitor/codex-monitor.js +0 -377
  183. package/dist/monitor/task-queue.js +0 -84
  184. package/dist/monitor/types.js +0 -49
  185. package/dist/native/bridge-client.js +0 -174
  186. package/dist/native/macos-bridge-client.js +0 -5
  187. package/dist/npm-publish-helper.js +0 -117
  188. package/dist/npm-token-cdp.js +0 -113
  189. package/dist/npm-token-create.js +0 -135
  190. package/dist/npm-token-finish.js +0 -126
  191. package/dist/playbook/engine.js +0 -193
  192. package/dist/playbook/index.js +0 -4
  193. package/dist/playbook/recorder.js +0 -519
  194. package/dist/playbook/runner.js +0 -392
  195. package/dist/playbook/store.js +0 -166
  196. package/dist/playbook/types.js +0 -4
  197. package/dist/runtime/accessibility-adapter.js +0 -377
  198. package/dist/runtime/app-adapter.js +0 -48
  199. package/dist/runtime/applescript-adapter.js +0 -283
  200. package/dist/runtime/ax-role-map.js +0 -80
  201. package/dist/runtime/browser-adapter.js +0 -36
  202. package/dist/runtime/cdp-chrome-adapter.js +0 -505
  203. package/dist/runtime/composite-adapter.js +0 -205
  204. package/dist/runtime/executor.js +0 -250
  205. package/dist/runtime/locator-cache.js +0 -12
  206. package/dist/runtime/planning-loop.js +0 -47
  207. package/dist/runtime/service.js +0 -372
  208. package/dist/runtime/session-manager.js +0 -28
  209. package/dist/runtime/state-observer.js +0 -105
  210. package/dist/runtime/vision-adapter.js +0 -208
  211. package/dist/test-mcp-protocol.js +0 -138
  212. package/dist/types.js +0 -1
@@ -1,347 +0,0 @@
1
- export class MvpMcpServer {
2
- runtime;
3
- constructor(runtime) {
4
- this.runtime = runtime;
5
- }
6
- async invoke(request) {
7
- switch (request.tool) {
8
- case "session_start":
9
- return this.runtime.sessionStart(optionalString(request.args, "profile"));
10
- case "navigate": {
11
- const timeoutMs = optionalNumber(request.args, "timeoutMs");
12
- const input = {
13
- sessionId: requiredString(request.args, "sessionId"),
14
- url: requiredString(request.args, "url"),
15
- };
16
- if (typeof timeoutMs === "number") {
17
- input.timeoutMs = timeoutMs;
18
- }
19
- return this.runtime.navigate(input);
20
- }
21
- case "press": {
22
- const verify = parseOptionalWaitCondition(request.args.verify);
23
- const input = {
24
- sessionId: requiredString(request.args, "sessionId"),
25
- target: parseTarget(request.args.target),
26
- };
27
- if (verify) {
28
- input.verify = verify;
29
- }
30
- return this.runtime.press(input);
31
- }
32
- case "type_into": {
33
- const clear = optionalBoolean(request.args, "clear");
34
- const verify = parseOptionalWaitCondition(request.args.verify);
35
- const input = {
36
- sessionId: requiredString(request.args, "sessionId"),
37
- target: parseTarget(request.args.target),
38
- text: requiredString(request.args, "text"),
39
- };
40
- if (typeof clear === "boolean") {
41
- input.clear = clear;
42
- }
43
- if (verify) {
44
- input.verify = verify;
45
- }
46
- return this.runtime.typeInto(input);
47
- }
48
- case "wait_for": {
49
- const timeoutMs = optionalNumber(request.args, "timeoutMs");
50
- const input = {
51
- sessionId: requiredString(request.args, "sessionId"),
52
- condition: parseWaitCondition(request.args.condition),
53
- };
54
- if (typeof timeoutMs === "number") {
55
- input.timeoutMs = timeoutMs;
56
- }
57
- return this.runtime.waitFor(input);
58
- }
59
- case "extract":
60
- return this.runtime.extract({
61
- sessionId: requiredString(request.args, "sessionId"),
62
- target: parseTarget(request.args.target),
63
- format: parseExtractFormat(request.args.format),
64
- });
65
- case "screenshot": {
66
- const region = parseOptionalRegion(request.args.region);
67
- const input = {
68
- sessionId: requiredString(request.args, "sessionId"),
69
- };
70
- if (region) {
71
- input.region = region;
72
- }
73
- return this.runtime.screenshot(input);
74
- }
75
- // ── Desktop automation tools ──
76
- case "app_launch":
77
- return this.runtime.appLaunch({
78
- sessionId: requiredString(request.args, "sessionId"),
79
- bundleId: requiredString(request.args, "bundleId"),
80
- });
81
- case "app_focus":
82
- return this.runtime.appFocus({
83
- sessionId: requiredString(request.args, "sessionId"),
84
- bundleId: requiredString(request.args, "bundleId"),
85
- });
86
- case "app_list":
87
- return this.runtime.appList(requiredString(request.args, "sessionId"));
88
- case "window_list":
89
- return this.runtime.windowList(requiredString(request.args, "sessionId"));
90
- case "menu_click":
91
- return this.runtime.menuClick({
92
- sessionId: requiredString(request.args, "sessionId"),
93
- menuPath: requiredStringArray(request.args, "menuPath"),
94
- });
95
- case "key_combo":
96
- return this.runtime.keyCombo({
97
- sessionId: requiredString(request.args, "sessionId"),
98
- keys: requiredStringArray(request.args, "keys"),
99
- });
100
- case "element_tree": {
101
- const maxDepth = optionalNumber(request.args, "maxDepth");
102
- const root = request.args.root ? parseTarget(request.args.root) : undefined;
103
- const etInput = {
104
- sessionId: requiredString(request.args, "sessionId"),
105
- };
106
- if (typeof maxDepth === "number")
107
- etInput.maxDepth = maxDepth;
108
- if (root)
109
- etInput.root = root;
110
- return this.runtime.elementTree(etInput);
111
- }
112
- case "observe_start": {
113
- const events = request.args.events;
114
- const osInput = {
115
- sessionId: requiredString(request.args, "sessionId"),
116
- };
117
- if (Array.isArray(events)) {
118
- osInput.events = events;
119
- }
120
- return this.runtime.observeStart(osInput);
121
- }
122
- case "observe_stop":
123
- return this.runtime.observeStop({
124
- sessionId: requiredString(request.args, "sessionId"),
125
- });
126
- case "drag":
127
- return this.runtime.drag({
128
- sessionId: requiredString(request.args, "sessionId"),
129
- from: parseTarget(request.args.from),
130
- to: parseTarget(request.args.to),
131
- });
132
- case "scroll": {
133
- const scrollTarget = request.args.target ? parseTarget(request.args.target) : undefined;
134
- const scrollAmount = optionalNumber(request.args, "amount");
135
- const scrollInput = {
136
- sessionId: requiredString(request.args, "sessionId"),
137
- direction: requiredString(request.args, "direction"),
138
- };
139
- if (scrollTarget)
140
- scrollInput.target = scrollTarget;
141
- if (typeof scrollAmount === "number")
142
- scrollInput.amount = scrollAmount;
143
- return this.runtime.scroll(scrollInput);
144
- }
145
- default:
146
- throw new Error(`Unsupported tool: ${String(request.tool)}`);
147
- }
148
- }
149
- }
150
- function parseTarget(input) {
151
- if (typeof input === "string") {
152
- if (input.startsWith("css=")) {
153
- return { type: "selector", value: input.slice(4) };
154
- }
155
- if (input.startsWith("text=")) {
156
- return { type: "text", value: input.slice(5), exact: true };
157
- }
158
- if (input.startsWith("ax_id=")) {
159
- return { type: "ax_attribute", attribute: "identifier", value: input.slice(6) };
160
- }
161
- return { type: "text", value: input };
162
- }
163
- if (!isRecord(input)) {
164
- throw new Error("target must be a string or object");
165
- }
166
- if (typeof input.selector === "string") {
167
- return { type: "selector", value: input.selector };
168
- }
169
- if (typeof input.text === "string") {
170
- return {
171
- type: "text",
172
- value: input.text,
173
- exact: input.exact === true,
174
- };
175
- }
176
- if (typeof input.role === "string" && typeof input.name === "string") {
177
- return {
178
- type: "role",
179
- role: input.role,
180
- name: input.name,
181
- exact: input.exact === true,
182
- };
183
- }
184
- if (Array.isArray(input.path)) {
185
- return { type: "ax_path", path: input.path };
186
- }
187
- if (typeof input.attribute === "string" && typeof input.value === "string") {
188
- return { type: "ax_attribute", attribute: input.attribute, value: input.value };
189
- }
190
- if (typeof input.x === "number" && typeof input.y === "number") {
191
- return { type: "coordinates", x: input.x, y: input.y };
192
- }
193
- if (typeof input.base64 === "string") {
194
- const target = { type: "image", base64: input.base64 };
195
- if (typeof input.confidence === "number") {
196
- target.confidence = input.confidence;
197
- }
198
- return target;
199
- }
200
- throw new Error("target object must contain selector, text, role+name, path, attribute+value, x+y, or base64");
201
- }
202
- function parseWaitCondition(input) {
203
- if (!isRecord(input) || typeof input.type !== "string") {
204
- throw new Error("condition must be an object with a type");
205
- }
206
- switch (input.type) {
207
- case "selector_visible":
208
- return {
209
- type: "selector_visible",
210
- selector: requiredObjectString(input, "selector"),
211
- };
212
- case "selector_hidden":
213
- return {
214
- type: "selector_hidden",
215
- selector: requiredObjectString(input, "selector"),
216
- };
217
- case "url_matches":
218
- return {
219
- type: "url_matches",
220
- regex: requiredObjectString(input, "regex"),
221
- };
222
- case "text_appears":
223
- return {
224
- type: "text_appears",
225
- text: requiredObjectString(input, "text"),
226
- };
227
- case "spinner_disappears":
228
- return {
229
- type: "spinner_disappears",
230
- selector: requiredObjectString(input, "selector"),
231
- };
232
- case "element_exists":
233
- return {
234
- type: "element_exists",
235
- target: parseTarget(input.target),
236
- };
237
- case "element_gone":
238
- return {
239
- type: "element_gone",
240
- target: parseTarget(input.target),
241
- };
242
- case "window_title_matches":
243
- return {
244
- type: "window_title_matches",
245
- regex: requiredObjectString(input, "regex"),
246
- };
247
- case "app_idle": {
248
- const cond = {
249
- type: "app_idle",
250
- bundleId: requiredObjectString(input, "bundleId"),
251
- };
252
- if (typeof input.timeoutMs === "number") {
253
- cond.timeoutMs = input.timeoutMs;
254
- }
255
- return cond;
256
- }
257
- default:
258
- throw new Error(`Unsupported condition type: ${input.type}`);
259
- }
260
- }
261
- function parseOptionalWaitCondition(input) {
262
- if (typeof input === "undefined") {
263
- return undefined;
264
- }
265
- return parseWaitCondition(input);
266
- }
267
- function parseExtractFormat(input) {
268
- if (input === "text" || input === "table" || input === "json") {
269
- return input;
270
- }
271
- throw new Error("format must be one of: text, table, json");
272
- }
273
- function parseOptionalRegion(input) {
274
- if (typeof input === "undefined") {
275
- return undefined;
276
- }
277
- if (!isRecord(input)) {
278
- throw new Error("region must be an object");
279
- }
280
- return {
281
- x: requiredObjectNumber(input, "x"),
282
- y: requiredObjectNumber(input, "y"),
283
- width: requiredObjectNumber(input, "width"),
284
- height: requiredObjectNumber(input, "height"),
285
- };
286
- }
287
- function requiredString(input, key) {
288
- const value = input[key];
289
- if (typeof value !== "string") {
290
- throw new Error(`${key} must be a string`);
291
- }
292
- return value;
293
- }
294
- function requiredStringArray(input, key) {
295
- const value = input[key];
296
- if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) {
297
- throw new Error(`${key} must be an array of strings`);
298
- }
299
- return value;
300
- }
301
- function optionalString(input, key) {
302
- const value = input[key];
303
- if (typeof value === "undefined") {
304
- return undefined;
305
- }
306
- if (typeof value !== "string") {
307
- throw new Error(`${key} must be a string`);
308
- }
309
- return value;
310
- }
311
- function optionalNumber(input, key) {
312
- const value = input[key];
313
- if (typeof value === "undefined") {
314
- return undefined;
315
- }
316
- if (typeof value !== "number") {
317
- throw new Error(`${key} must be a number`);
318
- }
319
- return value;
320
- }
321
- function optionalBoolean(input, key) {
322
- const value = input[key];
323
- if (typeof value === "undefined") {
324
- return undefined;
325
- }
326
- if (typeof value !== "boolean") {
327
- throw new Error(`${key} must be a boolean`);
328
- }
329
- return value;
330
- }
331
- function requiredObjectString(input, key) {
332
- const value = input[key];
333
- if (typeof value !== "string") {
334
- throw new Error(`${key} must be a string`);
335
- }
336
- return value;
337
- }
338
- function requiredObjectNumber(input, key) {
339
- const value = input[key];
340
- if (typeof value !== "number") {
341
- throw new Error(`${key} must be a number`);
342
- }
343
- return value;
344
- }
345
- function isRecord(input) {
346
- return typeof input === "object" && input !== null;
347
- }
package/dist/mcp-entry.js DELETED
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * ScreenHand MCP Server — MODULAR entry point (alternative).
4
- *
5
- * NOTE: The primary/canonical MCP server is `mcp-desktop.ts` at the project root.
6
- * It has the full 40+ tool set (desktop, browser, memory, playbooks, codex monitor).
7
- *
8
- * This modular entrypoint exposes a smaller subset of tools via the runtime service
9
- * abstraction. It exists for adapter experimentation and future refactoring.
10
- *
11
- * For production use, prefer: npx tsx mcp-desktop.ts
12
- *
13
- * Environment variables:
14
- * SCREENHAND_ADAPTER - "accessibility" (default), "composite", "cdp", "placeholder"
15
- * SCREENHAND_HEADLESS - "1" to run browser in headless mode
16
- */
17
- import { PlaceholderAppAdapter } from "./runtime/app-adapter.js";
18
- import { CdpChromeAdapter } from "./runtime/cdp-chrome-adapter.js";
19
- import { TimelineLogger } from "./logging/timeline-logger.js";
20
- import { AutomationRuntimeService } from "./runtime/service.js";
21
- import { startMcpStdioServer } from "./mcp/mcp-stdio-server.js";
22
- async function createAdapter() {
23
- const adapterType = process.env.SCREENHAND_ADAPTER ?? process.env.AUTOMATOR_ADAPTER ?? "accessibility";
24
- switch (adapterType) {
25
- case "placeholder":
26
- return new PlaceholderAppAdapter();
27
- case "cdp":
28
- return new CdpChromeAdapter({
29
- headless: process.env.SCREENHAND_HEADLESS === "1" || process.env.AUTOMATOR_HEADLESS === "1",
30
- });
31
- case "composite": {
32
- const { BridgeClient } = await import("./native/bridge-client.js");
33
- const { CompositeAdapter } = await import("./runtime/composite-adapter.js");
34
- const bridge = new BridgeClient();
35
- return new CompositeAdapter(bridge, {
36
- headless: process.env.SCREENHAND_HEADLESS === "1" || process.env.AUTOMATOR_HEADLESS === "1",
37
- });
38
- }
39
- case "accessibility":
40
- default: {
41
- const { BridgeClient } = await import("./native/bridge-client.js");
42
- const { AccessibilityAdapter } = await import("./runtime/accessibility-adapter.js");
43
- const bridge = new BridgeClient();
44
- return new AccessibilityAdapter(bridge);
45
- }
46
- }
47
- }
48
- try {
49
- const adapter = await createAdapter();
50
- const logger = new TimelineLogger();
51
- const runtime = new AutomationRuntimeService(adapter, logger);
52
- process.stderr.write("ScreenHand MCP server starting...\n");
53
- await startMcpStdioServer(runtime);
54
- process.stderr.write("ScreenHand MCP server connected.\n");
55
- }
56
- catch (e) {
57
- process.stderr.write(`ScreenHand startup error: ${e instanceof Error ? e.message : String(e)}\n`);
58
- process.exit(1);
59
- }
@@ -1,160 +0,0 @@
1
- /**
2
- * Learning Memory — Recall engine (in-memory)
3
- *
4
- * All searches run against cached data — no disk IO.
5
- * Provides fast methods for the interceptor to call on every tool invocation.
6
- */
7
- import { MemoryStore } from "./store.js";
8
- export class RecallEngine {
9
- store;
10
- constructor(store) {
11
- this.store = store;
12
- }
13
- /**
14
- * Find strategies matching a task description (~0ms, in-memory).
15
- * Strategies with high fail rates are penalized.
16
- */
17
- recallStrategies(query, limit = 5) {
18
- const strategies = this.store.readStrategies();
19
- if (strategies.length === 0)
20
- return [];
21
- const queryTokens = tokenize(query);
22
- if (queryTokens.length === 0)
23
- return [];
24
- const scored = strategies.map((s) => {
25
- const targetTokens = new Set([
26
- ...tokenize(s.task),
27
- ...s.tags,
28
- ...s.steps.map((step) => step.tool),
29
- ...s.steps.flatMap((step) => Object.values(step.params)
30
- .filter((v) => typeof v === "string")
31
- .flatMap(tokenize)),
32
- ]);
33
- let matches = 0;
34
- for (const qt of queryTokens) {
35
- for (const tt of targetTokens) {
36
- if (tt.includes(qt) || qt.includes(tt)) {
37
- matches++;
38
- break;
39
- }
40
- }
41
- }
42
- const relevance = matches / queryTokens.length;
43
- const ageMs = Date.now() - new Date(s.lastUsed).getTime();
44
- const ageDays = ageMs / (1000 * 60 * 60 * 24);
45
- const recency = Math.max(0.5, 1.0 - ageDays / 365);
46
- const successBoost = 1 + Math.log2(Math.max(1, s.successCount)) * 0.1;
47
- // Penalty for strategies that have failed — reduces score proportionally
48
- const failCount = s.failCount ?? 0;
49
- const totalAttempts = s.successCount + failCount;
50
- const reliabilityPenalty = totalAttempts > 0
51
- ? s.successCount / totalAttempts
52
- : 1;
53
- const score = relevance * recency * successBoost * reliabilityPenalty;
54
- return { ...s, score };
55
- });
56
- return scored
57
- .filter((s) => s.score > 0)
58
- .sort((a, b) => b.score - a.score)
59
- .slice(0, limit);
60
- }
61
- /**
62
- * O(1) exact match by tool sequence fingerprint.
63
- * Returns the strategy if found and it has a positive reliability score.
64
- */
65
- recallByFingerprint(tools) {
66
- const fp = MemoryStore.makeFingerprint(tools);
67
- const strategy = this.store.lookupByFingerprint(fp);
68
- if (!strategy)
69
- return null;
70
- // Skip strategies that fail more than they succeed
71
- const failCount = strategy.failCount ?? 0;
72
- if (failCount > strategy.successCount)
73
- return null;
74
- return strategy;
75
- }
76
- /**
77
- * Quick error lookup for a tool — used by interceptor on every call (~0ms).
78
- * Returns the most relevant error pattern or null.
79
- */
80
- quickErrorCheck(tool) {
81
- const errors = this.store.readErrors();
82
- let best = null;
83
- for (const e of errors) {
84
- if (e.tool === tool && e.resolution) {
85
- if (!best || e.occurrences > best.occurrences)
86
- best = e;
87
- }
88
- }
89
- return best;
90
- }
91
- /**
92
- * Quick strategy hint for a tool sequence — used by interceptor.
93
- * Tries fingerprint prefix match first (O(1)), then falls back to scan.
94
- * Skips unreliable strategies (failCount > successCount).
95
- */
96
- quickStrategyHint(recentTools) {
97
- if (recentTools.length === 0)
98
- return null;
99
- const strategies = this.store.readStrategies();
100
- for (const s of strategies) {
101
- if (s.steps.length <= recentTools.length)
102
- continue;
103
- // Skip unreliable strategies
104
- const failCount = s.failCount ?? 0;
105
- if (failCount > s.successCount)
106
- continue;
107
- const strategyToolPrefix = s.steps.slice(0, recentTools.length).map((st) => st.tool);
108
- const matches = recentTools.every((t, i) => t === strategyToolPrefix[i]);
109
- if (matches) {
110
- return {
111
- strategy: s,
112
- nextStep: s.steps[recentTools.length],
113
- fingerprint: s.fingerprint ?? MemoryStore.makeFingerprint(s.steps.map((st) => st.tool)),
114
- };
115
- }
116
- }
117
- return null;
118
- }
119
- /** Find error patterns for a specific tool or all tools */
120
- recallErrors(tool, params) {
121
- const errors = this.store.readErrors();
122
- if (!tool)
123
- return errors;
124
- let filtered = errors.filter((e) => e.tool === tool);
125
- if (params && filtered.length > 1) {
126
- const paramStr = JSON.stringify(params).toLowerCase();
127
- filtered.sort((a, b) => {
128
- const aScore = stringSimilarity(paramStr, JSON.stringify(a.params).toLowerCase());
129
- const bScore = stringSimilarity(paramStr, JSON.stringify(b.params).toLowerCase());
130
- return bScore - aScore;
131
- });
132
- }
133
- return filtered;
134
- }
135
- }
136
- /** Tokenize a string into lowercase keywords (3+ chars) */
137
- function tokenize(text) {
138
- return text
139
- .toLowerCase()
140
- .split(/[\W_]+/)
141
- .filter((w) => w.length >= 3);
142
- }
143
- /** Simple string similarity: shared character bigrams / total bigrams */
144
- function stringSimilarity(a, b) {
145
- if (a === b)
146
- return 1;
147
- if (a.length < 2 || b.length < 2)
148
- return 0;
149
- const bigramsA = new Set();
150
- for (let i = 0; i < a.length - 1; i++)
151
- bigramsA.add(a.slice(i, i + 2));
152
- let matches = 0;
153
- let total = 0;
154
- for (let i = 0; i < b.length - 1; i++) {
155
- total++;
156
- if (bigramsA.has(b.slice(i, i + 2)))
157
- matches++;
158
- }
159
- return total > 0 ? matches / total : 0;
160
- }
@@ -1,98 +0,0 @@
1
- /**
2
- * Background web research — fire-and-forget resolution lookup.
3
- *
4
- * When a tool fails and no resolution exists, this searches for a fix
5
- * in the background (non-blocking) and saves it for next time.
6
- *
7
- * Resolution paths:
8
- * 1. Claude API (haiku) — if ANTHROPIC_API_KEY is set
9
- * 2. DuckDuckGo instant answer — free fallback, no auth
10
- */
11
- export function backgroundResearch(store, tool, params, errorMessage) {
12
- // Fire-and-forget — never blocks, never throws
13
- doResearch(store, tool, params, errorMessage).catch(() => { });
14
- }
15
- async function doResearch(store, tool, params, errorMessage) {
16
- const query = `macOS automation: "${tool}" failed with "${errorMessage.slice(0, 200)}"`;
17
- let resolution = null;
18
- const apiKey = process.env["ANTHROPIC_API_KEY"];
19
- if (apiKey) {
20
- resolution = await tryClaudeAPI(apiKey, query);
21
- }
22
- if (!resolution) {
23
- resolution = await tryDuckDuckGo(query);
24
- }
25
- if (!resolution)
26
- return;
27
- // Save resolution to error cache
28
- store.appendError({
29
- id: "err_research_" + Date.now().toString(36),
30
- tool,
31
- params,
32
- error: errorMessage,
33
- resolution,
34
- occurrences: 1,
35
- lastSeen: new Date().toISOString(),
36
- });
37
- // Save as a reusable strategy
38
- store.appendStrategy({
39
- id: "str_research_" + Date.now().toString(36),
40
- task: `Fix: ${tool} — ${errorMessage.slice(0, 100)}`,
41
- steps: [{ tool, params }],
42
- totalDurationMs: 0,
43
- successCount: 1,
44
- failCount: 0,
45
- lastUsed: new Date().toISOString(),
46
- tags: [tool, "research", "fix"],
47
- fingerprint: "",
48
- });
49
- }
50
- async function tryClaudeAPI(apiKey, query) {
51
- try {
52
- const controller = new AbortController();
53
- const timeout = setTimeout(() => controller.abort(), 10000);
54
- const response = await fetch("https://api.anthropic.com/v1/messages", {
55
- method: "POST",
56
- headers: {
57
- "Content-Type": "application/json",
58
- "x-api-key": apiKey,
59
- "anthropic-version": "2023-06-01",
60
- },
61
- body: JSON.stringify({
62
- model: "claude-haiku-4-5-20251001",
63
- max_tokens: 200,
64
- messages: [{
65
- role: "user",
66
- content: `You are a macOS automation expert. Give a brief fix (1-2 sentences) for this error:\n\n${query}`,
67
- }],
68
- }),
69
- signal: controller.signal,
70
- });
71
- clearTimeout(timeout);
72
- if (!response.ok)
73
- return null;
74
- const data = await response.json();
75
- const text = data.content?.[0]?.text;
76
- return text && text.length > 10 ? text.trim() : null;
77
- }
78
- catch {
79
- return null;
80
- }
81
- }
82
- async function tryDuckDuckGo(query) {
83
- try {
84
- const controller = new AbortController();
85
- const timeout = setTimeout(() => controller.abort(), 5000);
86
- const url = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1`;
87
- const response = await fetch(url, { signal: controller.signal });
88
- clearTimeout(timeout);
89
- if (!response.ok)
90
- return null;
91
- const data = await response.json();
92
- const text = data.AbstractText || data.Abstract || data.Answer;
93
- return text && text.length > 10 ? text.trim() : null;
94
- }
95
- catch {
96
- return null;
97
- }
98
- }