usecomputer 0.0.1 → 0.0.2

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 (50) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +37 -0
  3. package/bin.js +3 -1
  4. package/build.zig +52 -0
  5. package/build.zig.zon +21 -0
  6. package/dist/bridge-contract.test.d.ts +2 -0
  7. package/dist/bridge-contract.test.d.ts.map +1 -0
  8. package/dist/bridge-contract.test.js +74 -0
  9. package/dist/bridge.d.ts +7 -0
  10. package/dist/bridge.d.ts.map +1 -0
  11. package/dist/bridge.js +130 -0
  12. package/dist/cli-parsing.test.d.ts +2 -0
  13. package/dist/cli-parsing.test.d.ts.map +1 -0
  14. package/dist/cli-parsing.test.js +30 -0
  15. package/dist/cli.d.ts +5 -1
  16. package/dist/cli.d.ts.map +1 -1
  17. package/dist/cli.js +286 -335
  18. package/dist/command-parsers.d.ts +6 -0
  19. package/dist/command-parsers.d.ts.map +1 -0
  20. package/dist/command-parsers.js +54 -0
  21. package/dist/command-parsers.test.d.ts +2 -0
  22. package/dist/command-parsers.test.d.ts.map +1 -0
  23. package/dist/command-parsers.test.js +44 -0
  24. package/dist/darwin-arm64/usecomputer.node +0 -0
  25. package/dist/darwin-x64/usecomputer.node +0 -0
  26. package/dist/index.d.ts +4 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +5 -4
  29. package/dist/native-click-smoke.test.d.ts +2 -0
  30. package/dist/native-click-smoke.test.d.ts.map +1 -0
  31. package/dist/native-click-smoke.test.js +93 -0
  32. package/dist/native-lib.cjs +33 -0
  33. package/dist/native-lib.d.cts +7 -0
  34. package/dist/native-lib.d.ts +5 -0
  35. package/dist/native-lib.d.ts.map +1 -0
  36. package/dist/native-lib.js +27 -0
  37. package/dist/types.d.ts +80 -0
  38. package/dist/types.d.ts.map +1 -0
  39. package/dist/types.js +2 -0
  40. package/package.json +23 -12
  41. package/src/bridge-contract.test.ts +85 -0
  42. package/src/bridge.ts +159 -0
  43. package/src/cli.ts +329 -473
  44. package/src/command-parsers.test.ts +50 -0
  45. package/src/command-parsers.ts +60 -0
  46. package/src/index.ts +5 -4
  47. package/src/native-click-smoke.test.ts +131 -0
  48. package/src/native-lib.ts +38 -0
  49. package/src/types.ts +87 -0
  50. package/zig/src/lib.zig +367 -0
package/src/bridge.ts ADDED
@@ -0,0 +1,159 @@
1
+ // Native bridge that maps typed TS calls to the Zig N-API command dispatcher.
2
+
3
+ import { native, type NativeModule } from './native-lib.js'
4
+ import type {
5
+ ClickInput,
6
+ DragInput,
7
+ Point,
8
+ PressInput,
9
+ ScreenshotInput,
10
+ ScreenshotResult,
11
+ ScrollInput,
12
+ TypeInput,
13
+ UseComputerBridge,
14
+ DisplayInfo,
15
+ } from './types.js'
16
+
17
+ const unavailableError =
18
+ 'Native backend is unavailable. Build it with `pnpm build:native` or `zig build` in usecomputer/.'
19
+
20
+ function execute<T>({
21
+ nativeModule,
22
+ command,
23
+ payload,
24
+ }: {
25
+ nativeModule: NativeModule
26
+ command: string
27
+ payload: unknown
28
+ }): Error | T {
29
+ const response = nativeModule.execute(command, JSON.stringify(payload))
30
+ const parsed = JSON.parse(response) as { ok: boolean; data?: T; error?: string }
31
+ if (!parsed.ok) {
32
+ return new Error(parsed.error || `Native command failed: ${command}`)
33
+ }
34
+ return parsed.data as T
35
+ }
36
+
37
+ function unavailableBridge(): UseComputerBridge {
38
+ const fail = async (): Promise<never> => {
39
+ throw new Error(unavailableError)
40
+ }
41
+
42
+ return {
43
+ screenshot: fail,
44
+ click: fail,
45
+ typeText: fail,
46
+ press: fail,
47
+ scroll: fail,
48
+ drag: fail,
49
+ hover: fail,
50
+ mouseMove: fail,
51
+ mouseDown: fail,
52
+ mouseUp: fail,
53
+ mousePosition: fail,
54
+ displayList: fail,
55
+ clipboardGet: fail,
56
+ clipboardSet: fail,
57
+ }
58
+ }
59
+
60
+ export function createBridgeFromNative({ nativeModule }: { nativeModule: NativeModule | null }): UseComputerBridge {
61
+ if (!nativeModule) {
62
+ return unavailableBridge()
63
+ }
64
+
65
+ return {
66
+ async screenshot(input: ScreenshotInput): Promise<ScreenshotResult> {
67
+ const result = execute<ScreenshotResult>({ nativeModule, command: 'screenshot', payload: input })
68
+ if (result instanceof Error) {
69
+ throw result
70
+ }
71
+ return result
72
+ },
73
+ async click(input: ClickInput): Promise<void> {
74
+ const result = execute<null>({ nativeModule, command: 'click', payload: input })
75
+ if (result instanceof Error) {
76
+ throw result
77
+ }
78
+ },
79
+ async typeText(input: TypeInput): Promise<void> {
80
+ const result = execute<null>({ nativeModule, command: 'type-text', payload: input })
81
+ if (result instanceof Error) {
82
+ throw result
83
+ }
84
+ },
85
+ async press(input: PressInput): Promise<void> {
86
+ const result = execute<null>({ nativeModule, command: 'press', payload: input })
87
+ if (result instanceof Error) {
88
+ throw result
89
+ }
90
+ },
91
+ async scroll(input: ScrollInput): Promise<void> {
92
+ const result = execute<null>({ nativeModule, command: 'scroll', payload: input })
93
+ if (result instanceof Error) {
94
+ throw result
95
+ }
96
+ },
97
+ async drag(input: DragInput): Promise<void> {
98
+ const result = execute<null>({ nativeModule, command: 'drag', payload: input })
99
+ if (result instanceof Error) {
100
+ throw result
101
+ }
102
+ },
103
+ async hover(input: Point): Promise<void> {
104
+ const result = execute<null>({ nativeModule, command: 'hover', payload: input })
105
+ if (result instanceof Error) {
106
+ throw result
107
+ }
108
+ },
109
+ async mouseMove(input: Point): Promise<void> {
110
+ const result = execute<null>({ nativeModule, command: 'mouse-move', payload: input })
111
+ if (result instanceof Error) {
112
+ throw result
113
+ }
114
+ },
115
+ async mouseDown(input: { button: 'left' | 'right' | 'middle' }): Promise<void> {
116
+ const result = execute<null>({ nativeModule, command: 'mouse-down', payload: input })
117
+ if (result instanceof Error) {
118
+ throw result
119
+ }
120
+ },
121
+ async mouseUp(input: { button: 'left' | 'right' | 'middle' }): Promise<void> {
122
+ const result = execute<null>({ nativeModule, command: 'mouse-up', payload: input })
123
+ if (result instanceof Error) {
124
+ throw result
125
+ }
126
+ },
127
+ async mousePosition(): Promise<Point> {
128
+ const result = execute<Point>({ nativeModule, command: 'mouse-position', payload: {} })
129
+ if (result instanceof Error) {
130
+ throw result
131
+ }
132
+ return result
133
+ },
134
+ async displayList(): Promise<DisplayInfo[]> {
135
+ const result = execute<DisplayInfo[]>({ nativeModule, command: 'display-list', payload: {} })
136
+ if (result instanceof Error) {
137
+ throw result
138
+ }
139
+ return result
140
+ },
141
+ async clipboardGet(): Promise<string> {
142
+ const result = execute<{ text: string }>({ nativeModule, command: 'clipboard-get', payload: {} })
143
+ if (result instanceof Error) {
144
+ throw result
145
+ }
146
+ return result.text
147
+ },
148
+ async clipboardSet(input: { text: string }): Promise<void> {
149
+ const result = execute<null>({ nativeModule, command: 'clipboard-set', payload: input })
150
+ if (result instanceof Error) {
151
+ throw result
152
+ }
153
+ },
154
+ }
155
+ }
156
+
157
+ export function createBridge(): UseComputerBridge {
158
+ return createBridgeFromNative({ nativeModule: native })
159
+ }