screenhand 0.1.0 → 0.1.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 (103) hide show
  1. package/.claude/commands/automate.md +28 -0
  2. package/.claude/commands/debug-ui.md +19 -0
  3. package/.claude/commands/screenshot.md +15 -0
  4. package/.github/FUNDING.yml +1 -0
  5. package/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  7. package/.mcp.json +8 -0
  8. package/DESKTOP_MCP_GUIDE.md +92 -0
  9. package/LICENSE +661 -21
  10. package/README.md +97 -292
  11. package/SECURITY.md +44 -0
  12. package/docs/architecture.md +47 -0
  13. package/install-skills.sh +19 -0
  14. package/mcp-bridge.ts +271 -0
  15. package/mcp-desktop.ts +1221 -0
  16. package/native/macos-bridge/Package.swift +21 -0
  17. package/native/macos-bridge/Sources/AccessibilityBridge.swift +261 -0
  18. package/native/macos-bridge/Sources/AppManagement.swift +129 -0
  19. package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +242 -0
  20. package/native/macos-bridge/Sources/ObserverBridge.swift +120 -0
  21. package/native/macos-bridge/Sources/VisionBridge.swift +80 -0
  22. package/native/macos-bridge/Sources/main.swift +345 -0
  23. package/native/windows-bridge/AppManagement.cs +234 -0
  24. package/native/windows-bridge/InputBridge.cs +436 -0
  25. package/native/windows-bridge/Program.cs +265 -0
  26. package/native/windows-bridge/ScreenCapture.cs +329 -0
  27. package/native/windows-bridge/UIAutomationBridge.cs +571 -0
  28. package/native/windows-bridge/WindowsBridge.csproj +17 -0
  29. package/package.json +3 -14
  30. package/playbooks/devpost.json +186 -0
  31. package/playbooks/instagram.json +41 -0
  32. package/playbooks/instagram_v2.json +201 -0
  33. package/playbooks/x_v1.json +211 -0
  34. package/scripts/devpost-live-loop.mjs +421 -0
  35. package/src/config.ts +30 -0
  36. package/src/index.ts +92 -0
  37. package/src/logging/timeline-logger.ts +55 -0
  38. package/src/mcp/server.ts +449 -0
  39. package/src/memory/recall.ts +191 -0
  40. package/src/memory/research.ts +146 -0
  41. package/src/memory/seeds.ts +123 -0
  42. package/src/memory/session.ts +201 -0
  43. package/src/memory/store.ts +434 -0
  44. package/src/memory/types.ts +69 -0
  45. package/src/native/bridge-client.ts +239 -0
  46. package/src/native/macos-bridge-client.ts +22 -0
  47. package/src/runtime/accessibility-adapter.ts +487 -0
  48. package/src/runtime/app-adapter.ts +169 -0
  49. package/src/runtime/applescript-adapter.ts +376 -0
  50. package/src/runtime/ax-role-map.ts +102 -0
  51. package/src/runtime/browser-adapter.ts +129 -0
  52. package/src/runtime/cdp-chrome-adapter.ts +676 -0
  53. package/src/runtime/composite-adapter.ts +274 -0
  54. package/src/runtime/executor.ts +396 -0
  55. package/src/runtime/locator-cache.ts +33 -0
  56. package/src/runtime/planning-loop.ts +81 -0
  57. package/src/runtime/service.ts +448 -0
  58. package/src/runtime/session-manager.ts +50 -0
  59. package/src/runtime/state-observer.ts +136 -0
  60. package/src/runtime/vision-adapter.ts +297 -0
  61. package/src/types.ts +297 -0
  62. package/tests/bridge-client.test.ts +176 -0
  63. package/tests/browser-stealth.test.ts +210 -0
  64. package/tests/composite-adapter.test.ts +64 -0
  65. package/tests/mcp-server.test.ts +151 -0
  66. package/tests/memory-recall.test.ts +339 -0
  67. package/tests/memory-research.test.ts +159 -0
  68. package/tests/memory-seeds.test.ts +120 -0
  69. package/tests/memory-store.test.ts +392 -0
  70. package/tests/types.test.ts +92 -0
  71. package/tsconfig.check.json +17 -0
  72. package/tsconfig.json +19 -0
  73. package/vitest.config.ts +8 -0
  74. package/dist/config.js +0 -9
  75. package/dist/index.js +0 -55
  76. package/dist/logging/timeline-logger.js +0 -29
  77. package/dist/mcp/mcp-stdio-server.js +0 -284
  78. package/dist/mcp/server.js +0 -347
  79. package/dist/mcp-entry.js +0 -62
  80. package/dist/memory/recall.js +0 -160
  81. package/dist/memory/research.js +0 -98
  82. package/dist/memory/seeds.js +0 -89
  83. package/dist/memory/session.js +0 -161
  84. package/dist/memory/store.js +0 -391
  85. package/dist/memory/types.js +0 -4
  86. package/dist/native/bridge-client.js +0 -173
  87. package/dist/native/macos-bridge-client.js +0 -5
  88. package/dist/runtime/accessibility-adapter.js +0 -377
  89. package/dist/runtime/app-adapter.js +0 -48
  90. package/dist/runtime/applescript-adapter.js +0 -283
  91. package/dist/runtime/ax-role-map.js +0 -80
  92. package/dist/runtime/browser-adapter.js +0 -36
  93. package/dist/runtime/cdp-chrome-adapter.js +0 -505
  94. package/dist/runtime/composite-adapter.js +0 -205
  95. package/dist/runtime/executor.js +0 -250
  96. package/dist/runtime/locator-cache.js +0 -12
  97. package/dist/runtime/planning-loop.js +0 -47
  98. package/dist/runtime/service.js +0 -372
  99. package/dist/runtime/session-manager.js +0 -28
  100. package/dist/runtime/state-observer.js +0 -105
  101. package/dist/runtime/vision-adapter.js +0 -208
  102. package/dist/test-mcp-protocol.js +0 -138
  103. package/dist/types.js +0 -1
@@ -0,0 +1,239 @@
1
+ // Copyright (C) 2025 Clazro Technology Private Limited
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+ //
4
+ // This file is part of ScreenHand.
5
+ //
6
+ // ScreenHand is free software: you can redistribute it and/or modify
7
+ // it under the terms of the GNU Affero General Public License as
8
+ // published by the Free Software Foundation, version 3.
9
+ //
10
+ // ScreenHand is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ import { spawn, type ChildProcess } from "node:child_process";
19
+ import { EventEmitter } from "node:events";
20
+ import path from "node:path";
21
+ import { createInterface } from "node:readline";
22
+
23
+ interface JsonRpcRequest {
24
+ id: number;
25
+ method: string;
26
+ params?: Record<string, unknown>;
27
+ }
28
+
29
+ interface JsonRpcResponse {
30
+ id: number;
31
+ result?: unknown;
32
+ error?: { code: number; message: string };
33
+ event?: Record<string, unknown>;
34
+ }
35
+
36
+ interface PendingRequest {
37
+ resolve: (value: unknown) => void;
38
+ reject: (error: Error) => void;
39
+ timer: ReturnType<typeof setTimeout>;
40
+ }
41
+
42
+ /**
43
+ * Per-method timeout overrides (ms).
44
+ * Methods not listed here use the default 10s timeout.
45
+ */
46
+ const METHOD_TIMEOUTS: Record<string, number> = {
47
+ "app.launch": 30_000,
48
+ "cg.captureScreen": 15_000,
49
+ "cg.captureWindow": 15_000,
50
+ "vision.ocr": 20_000,
51
+ "vision.findText": 20_000,
52
+ };
53
+
54
+ /**
55
+ * Resolves the correct native bridge binary path for the current platform.
56
+ */
57
+ function defaultBinaryPath(): string {
58
+ const base = import.meta.dirname ?? process.cwd();
59
+
60
+ if (process.platform === "win32") {
61
+ return path.resolve(
62
+ base,
63
+ "../../native/windows-bridge/bin/Release/net8.0-windows/windows-bridge.exe",
64
+ );
65
+ }
66
+
67
+ // macOS (default)
68
+ return path.resolve(
69
+ base,
70
+ "../../native/macos-bridge/.build/release/macos-bridge",
71
+ );
72
+ }
73
+
74
+ /**
75
+ * Platform-aware native bridge client.
76
+ * Spawns the correct bridge binary (macOS Swift or Windows C#) based on the OS,
77
+ * communicating via the same JSON-RPC-over-stdio protocol.
78
+ *
79
+ * Drop-in replacement for the original MacOSBridgeClient.
80
+ */
81
+ export class BridgeClient extends EventEmitter {
82
+ private process: ChildProcess | null = null;
83
+ private nextId = 1;
84
+ private pending = new Map<number, PendingRequest>();
85
+ private readonly binaryPath: string;
86
+ private restarting = false;
87
+ private started = false;
88
+
89
+ constructor(binaryPath?: string) {
90
+ super();
91
+ this.binaryPath = binaryPath ?? defaultBinaryPath();
92
+ }
93
+
94
+ async start(): Promise<void> {
95
+ if (this.started) return;
96
+ await this.spawn();
97
+ this.started = true;
98
+ }
99
+
100
+ async stop(): Promise<void> {
101
+ this.started = false;
102
+ if (this.process) {
103
+ this.process.kill();
104
+ this.process = null;
105
+ }
106
+ // Reject all pending
107
+ for (const [id, pending] of this.pending) {
108
+ clearTimeout(pending.timer);
109
+ pending.reject(new Error("Bridge stopped"));
110
+ this.pending.delete(id);
111
+ }
112
+ }
113
+
114
+ async call<T = unknown>(method: string, params?: Record<string, unknown>, timeoutMs?: number): Promise<T> {
115
+ const effectiveTimeout = timeoutMs ?? METHOD_TIMEOUTS[method] ?? 10_000;
116
+ if (!this.process || this.process.exitCode !== null) {
117
+ await this.restart();
118
+ }
119
+
120
+ const id = this.nextId++;
121
+ const request: JsonRpcRequest = { id, method };
122
+ if (params) {
123
+ request.params = params;
124
+ }
125
+
126
+ return new Promise<T>((resolve, reject) => {
127
+ const timer = setTimeout(() => {
128
+ this.pending.delete(id);
129
+ reject(new Error(`Bridge call "${method}" timed out after ${effectiveTimeout}ms`));
130
+ }, effectiveTimeout);
131
+
132
+ this.pending.set(id, {
133
+ resolve: resolve as (value: unknown) => void,
134
+ reject,
135
+ timer,
136
+ });
137
+
138
+ const line = JSON.stringify(request) + "\n";
139
+ this.process!.stdin!.write(line);
140
+ });
141
+ }
142
+
143
+ async ping(): Promise<{ pong: boolean; pid: number; accessible: boolean }> {
144
+ return this.call("ping");
145
+ }
146
+
147
+ async checkPermissions(): Promise<{ trusted: boolean }> {
148
+ return this.call("check_permissions");
149
+ }
150
+
151
+ private async spawn(): Promise<void> {
152
+ const child = spawn(this.binaryPath, [], {
153
+ stdio: ["pipe", "pipe", "pipe"],
154
+ });
155
+
156
+ child.on("error", (err) => {
157
+ this.emit("error", err);
158
+ if (this.started) {
159
+ this.restart().catch(() => {});
160
+ }
161
+ });
162
+
163
+ child.on("exit", (code) => {
164
+ this.emit("exit", code);
165
+ if (this.started && !this.restarting) {
166
+ this.restart().catch(() => {});
167
+ }
168
+ });
169
+
170
+ // Parse stdout line by line
171
+ const rl = createInterface({ input: child.stdout! });
172
+ rl.on("line", (line) => {
173
+ this.handleLine(line);
174
+ });
175
+
176
+ // Log stderr
177
+ child.stderr?.on("data", (data: Buffer) => {
178
+ this.emit("stderr", data.toString());
179
+ });
180
+
181
+ this.process = child;
182
+ }
183
+
184
+ private handleLine(line: string): void {
185
+ let response: JsonRpcResponse;
186
+ try {
187
+ response = JSON.parse(line) as JsonRpcResponse;
188
+ } catch {
189
+ return; // Ignore malformed lines
190
+ }
191
+
192
+ // Event (streaming notification from observer)
193
+ if (response.event) {
194
+ this.emit("ax-event", response.event);
195
+ return;
196
+ }
197
+
198
+ // Response to a pending request
199
+ const pending = this.pending.get(response.id);
200
+ if (!pending) return;
201
+
202
+ this.pending.delete(response.id);
203
+ clearTimeout(pending.timer);
204
+
205
+ if (response.error) {
206
+ pending.reject(new Error(response.error.message));
207
+ } else {
208
+ pending.resolve(response.result);
209
+ }
210
+ }
211
+
212
+ private async restart(): Promise<void> {
213
+ if (this.restarting) return;
214
+ this.restarting = true;
215
+
216
+ // Reject all pending requests
217
+ for (const [id, pending] of this.pending) {
218
+ clearTimeout(pending.timer);
219
+ pending.reject(new Error("Bridge process crashed, restarting"));
220
+ this.pending.delete(id);
221
+ }
222
+
223
+ try {
224
+ if (this.process) {
225
+ this.process.kill();
226
+ this.process = null;
227
+ }
228
+ await this.spawn();
229
+ this.emit("restart");
230
+ } finally {
231
+ this.restarting = false;
232
+ }
233
+ }
234
+ }
235
+
236
+ /**
237
+ * @deprecated Use BridgeClient instead. This alias exists for backward compatibility.
238
+ */
239
+ export const MacOSBridgeClient = BridgeClient;
@@ -0,0 +1,22 @@
1
+ // Copyright (C) 2025 Clazro Technology Private Limited
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+ //
4
+ // This file is part of ScreenHand.
5
+ //
6
+ // ScreenHand is free software: you can redistribute it and/or modify
7
+ // it under the terms of the GNU Affero General Public License as
8
+ // published by the Free Software Foundation, version 3.
9
+ //
10
+ // ScreenHand is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ /**
19
+ * @deprecated Import from "./bridge-client.js" instead.
20
+ * This file re-exports for backward compatibility.
21
+ */
22
+ export { BridgeClient, BridgeClient as MacOSBridgeClient } from "./bridge-client.js";