devlens-mcp 0.3.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 (175) hide show
  1. package/.claude/settings.json +12 -0
  2. package/.claude/settings.local.json +17 -0
  3. package/INSTALLATION_GUIDE.md +354 -0
  4. package/QUICK_START.md +153 -0
  5. package/README.md +354 -0
  6. package/bin/cli.ts +22 -0
  7. package/bin/register.ts +96 -0
  8. package/dist/bin/cli.d.ts +3 -0
  9. package/dist/bin/cli.d.ts.map +1 -0
  10. package/dist/bin/cli.js +20 -0
  11. package/dist/bin/cli.js.map +1 -0
  12. package/dist/bin/register.d.ts +10 -0
  13. package/dist/bin/register.d.ts.map +1 -0
  14. package/dist/bin/register.js +92 -0
  15. package/dist/bin/register.js.map +1 -0
  16. package/dist/src/config/devlens-config.d.ts +92 -0
  17. package/dist/src/config/devlens-config.d.ts.map +1 -0
  18. package/dist/src/config/devlens-config.js +70 -0
  19. package/dist/src/config/devlens-config.js.map +1 -0
  20. package/dist/src/index.d.ts +35 -0
  21. package/dist/src/index.d.ts.map +1 -0
  22. package/dist/src/index.js +8 -0
  23. package/dist/src/index.js.map +1 -0
  24. package/dist/src/metro/cdp-client.d.ts +48 -0
  25. package/dist/src/metro/cdp-client.d.ts.map +1 -0
  26. package/dist/src/metro/cdp-client.js +127 -0
  27. package/dist/src/metro/cdp-client.js.map +1 -0
  28. package/dist/src/metro/log-collector.d.ts +30 -0
  29. package/dist/src/metro/log-collector.d.ts.map +1 -0
  30. package/dist/src/metro/log-collector.js +114 -0
  31. package/dist/src/metro/log-collector.js.map +1 -0
  32. package/dist/src/metro/metro-bridge.d.ts +56 -0
  33. package/dist/src/metro/metro-bridge.d.ts.map +1 -0
  34. package/dist/src/metro/metro-bridge.js +255 -0
  35. package/dist/src/metro/metro-bridge.js.map +1 -0
  36. package/dist/src/metro/network-inspector.d.ts +34 -0
  37. package/dist/src/metro/network-inspector.d.ts.map +1 -0
  38. package/dist/src/metro/network-inspector.js +100 -0
  39. package/dist/src/metro/network-inspector.js.map +1 -0
  40. package/dist/src/platform/android/adb.d.ts +50 -0
  41. package/dist/src/platform/android/adb.d.ts.map +1 -0
  42. package/dist/src/platform/android/adb.js +137 -0
  43. package/dist/src/platform/android/adb.js.map +1 -0
  44. package/dist/src/platform/android/android-device.d.ts +21 -0
  45. package/dist/src/platform/android/android-device.d.ts.map +1 -0
  46. package/dist/src/platform/android/android-device.js +94 -0
  47. package/dist/src/platform/android/android-device.js.map +1 -0
  48. package/dist/src/platform/android/ui-automator.d.ts +17 -0
  49. package/dist/src/platform/android/ui-automator.d.ts.map +1 -0
  50. package/dist/src/platform/android/ui-automator.js +126 -0
  51. package/dist/src/platform/android/ui-automator.js.map +1 -0
  52. package/dist/src/platform/device-manager.d.ts +28 -0
  53. package/dist/src/platform/device-manager.d.ts.map +1 -0
  54. package/dist/src/platform/device-manager.js +185 -0
  55. package/dist/src/platform/device-manager.js.map +1 -0
  56. package/dist/src/platform/device.d.ts +86 -0
  57. package/dist/src/platform/device.d.ts.map +1 -0
  58. package/dist/src/platform/device.js +7 -0
  59. package/dist/src/platform/device.js.map +1 -0
  60. package/dist/src/platform/ios/accessibility.d.ts +17 -0
  61. package/dist/src/platform/ios/accessibility.d.ts.map +1 -0
  62. package/dist/src/platform/ios/accessibility.js +159 -0
  63. package/dist/src/platform/ios/accessibility.js.map +1 -0
  64. package/dist/src/platform/ios/ios-device.d.ts +22 -0
  65. package/dist/src/platform/ios/ios-device.d.ts.map +1 -0
  66. package/dist/src/platform/ios/ios-device.js +97 -0
  67. package/dist/src/platform/ios/ios-device.js.map +1 -0
  68. package/dist/src/platform/ios/simctl.d.ts +54 -0
  69. package/dist/src/platform/ios/simctl.d.ts.map +1 -0
  70. package/dist/src/platform/ios/simctl.js +192 -0
  71. package/dist/src/platform/ios/simctl.js.map +1 -0
  72. package/dist/src/server.d.ts +3 -0
  73. package/dist/src/server.d.ts.map +1 -0
  74. package/dist/src/server.js +176 -0
  75. package/dist/src/server.js.map +1 -0
  76. package/dist/src/snapshot/formatter.d.ts +18 -0
  77. package/dist/src/snapshot/formatter.d.ts.map +1 -0
  78. package/dist/src/snapshot/formatter.js +86 -0
  79. package/dist/src/snapshot/formatter.js.map +1 -0
  80. package/dist/src/snapshot/ref-registry.d.ts +67 -0
  81. package/dist/src/snapshot/ref-registry.d.ts.map +1 -0
  82. package/dist/src/snapshot/ref-registry.js +169 -0
  83. package/dist/src/snapshot/ref-registry.js.map +1 -0
  84. package/dist/src/snapshot/snapshot-differ.d.ts +57 -0
  85. package/dist/src/snapshot/snapshot-differ.d.ts.map +1 -0
  86. package/dist/src/snapshot/snapshot-differ.js +153 -0
  87. package/dist/src/snapshot/snapshot-differ.js.map +1 -0
  88. package/dist/src/tools/app-tools.d.ts +71 -0
  89. package/dist/src/tools/app-tools.d.ts.map +1 -0
  90. package/dist/src/tools/app-tools.js +97 -0
  91. package/dist/src/tools/app-tools.js.map +1 -0
  92. package/dist/src/tools/device-tools.d.ts +53 -0
  93. package/dist/src/tools/device-tools.d.ts.map +1 -0
  94. package/dist/src/tools/device-tools.js +86 -0
  95. package/dist/src/tools/device-tools.js.map +1 -0
  96. package/dist/src/tools/ds-tools.d.ts +65 -0
  97. package/dist/src/tools/ds-tools.d.ts.map +1 -0
  98. package/dist/src/tools/ds-tools.js +314 -0
  99. package/dist/src/tools/ds-tools.js.map +1 -0
  100. package/dist/src/tools/interaction-tools.d.ts +248 -0
  101. package/dist/src/tools/interaction-tools.d.ts.map +1 -0
  102. package/dist/src/tools/interaction-tools.js +391 -0
  103. package/dist/src/tools/interaction-tools.js.map +1 -0
  104. package/dist/src/tools/metro-tools.d.ts +115 -0
  105. package/dist/src/tools/metro-tools.d.ts.map +1 -0
  106. package/dist/src/tools/metro-tools.js +270 -0
  107. package/dist/src/tools/metro-tools.js.map +1 -0
  108. package/dist/src/tools/navigation-tools.d.ts +36 -0
  109. package/dist/src/tools/navigation-tools.d.ts.map +1 -0
  110. package/dist/src/tools/navigation-tools.js +60 -0
  111. package/dist/src/tools/navigation-tools.js.map +1 -0
  112. package/dist/src/tools/screenshot-tools.d.ts +298 -0
  113. package/dist/src/tools/screenshot-tools.d.ts.map +1 -0
  114. package/dist/src/tools/screenshot-tools.js +565 -0
  115. package/dist/src/tools/screenshot-tools.js.map +1 -0
  116. package/dist/src/tools/snapshot-tools.d.ts +161 -0
  117. package/dist/src/tools/snapshot-tools.d.ts.map +1 -0
  118. package/dist/src/tools/snapshot-tools.js +479 -0
  119. package/dist/src/tools/snapshot-tools.js.map +1 -0
  120. package/dist/src/utils/image-preprocess.d.ts +49 -0
  121. package/dist/src/utils/image-preprocess.d.ts.map +1 -0
  122. package/dist/src/utils/image-preprocess.js +322 -0
  123. package/dist/src/utils/image-preprocess.js.map +1 -0
  124. package/dist/src/utils/retry.d.ts +21 -0
  125. package/dist/src/utils/retry.d.ts.map +1 -0
  126. package/dist/src/utils/retry.js +33 -0
  127. package/dist/src/utils/retry.js.map +1 -0
  128. package/dist/src/visual/comparator.d.ts +51 -0
  129. package/dist/src/visual/comparator.d.ts.map +1 -0
  130. package/dist/src/visual/comparator.js +119 -0
  131. package/dist/src/visual/comparator.js.map +1 -0
  132. package/dist/src/visual/layout-analyzer.d.ts +64 -0
  133. package/dist/src/visual/layout-analyzer.d.ts.map +1 -0
  134. package/dist/src/visual/layout-analyzer.js +198 -0
  135. package/dist/src/visual/layout-analyzer.js.map +1 -0
  136. package/dist/src/visual/screenshot.d.ts +17 -0
  137. package/dist/src/visual/screenshot.d.ts.map +1 -0
  138. package/dist/src/visual/screenshot.js +39 -0
  139. package/dist/src/visual/screenshot.js.map +1 -0
  140. package/docs/figma-workflow.md +289 -0
  141. package/docs/setup-guide.md +360 -0
  142. package/docs/tool-reference.md +622 -0
  143. package/package.json +57 -0
  144. package/src/config/devlens-config.ts +76 -0
  145. package/src/index.ts +5 -0
  146. package/src/metro/cdp-client.ts +160 -0
  147. package/src/metro/log-collector.ts +137 -0
  148. package/src/metro/metro-bridge.ts +307 -0
  149. package/src/metro/network-inspector.ts +134 -0
  150. package/src/platform/android/adb.ts +200 -0
  151. package/src/platform/android/android-device.ts +116 -0
  152. package/src/platform/android/ui-automator.ts +141 -0
  153. package/src/platform/device-manager.ts +229 -0
  154. package/src/platform/device.ts +110 -0
  155. package/src/platform/ios/accessibility.ts +189 -0
  156. package/src/platform/ios/ios-device.ts +116 -0
  157. package/src/platform/ios/simctl.ts +244 -0
  158. package/src/server.ts +228 -0
  159. package/src/snapshot/formatter.ts +102 -0
  160. package/src/snapshot/ref-registry.ts +230 -0
  161. package/src/snapshot/snapshot-differ.ts +220 -0
  162. package/src/tools/app-tools.ts +111 -0
  163. package/src/tools/device-tools.ts +96 -0
  164. package/src/tools/ds-tools.ts +395 -0
  165. package/src/tools/interaction-tools.ts +467 -0
  166. package/src/tools/metro-tools.ts +320 -0
  167. package/src/tools/navigation-tools.ts +71 -0
  168. package/src/tools/screenshot-tools.ts +698 -0
  169. package/src/tools/snapshot-tools.ts +585 -0
  170. package/src/utils/image-preprocess.ts +430 -0
  171. package/src/utils/retry.ts +51 -0
  172. package/src/visual/comparator.ts +191 -0
  173. package/src/visual/layout-analyzer.ts +283 -0
  174. package/src/visual/screenshot.ts +49 -0
  175. package/tsconfig.json +20 -0
@@ -0,0 +1,230 @@
1
+ import type { SnapshotNode, Bounds } from "../platform/device.js";
2
+
3
+ /**
4
+ * RefRegistry — the Playwright-style ref assignment system.
5
+ *
6
+ * Assigns monotonically increasing refs (e1, e2, ...) to every interactive
7
+ * or meaningful node in the accessibility tree. Maintains a map from ref
8
+ * to node info (bounds, center coords) so interaction tools can resolve
9
+ * "tap ref=e5" to actual screen coordinates.
10
+ */
11
+
12
+ export interface RefEntry {
13
+ ref: string;
14
+ node: SnapshotNode;
15
+ centerX: number;
16
+ centerY: number;
17
+ bounds: Bounds;
18
+ }
19
+
20
+ export interface ValidationIssue {
21
+ type: "zero-size" | "invisible" | "empty-container";
22
+ nodeType: string;
23
+ text?: string;
24
+ label?: string;
25
+ resourceId?: string;
26
+ bounds: Bounds;
27
+ detail: string;
28
+ }
29
+
30
+ export interface ValidationReport {
31
+ issues: ValidationIssue[];
32
+ totalNodes: number;
33
+ visibleNodes: number;
34
+ zeroSizeNodes: number;
35
+ interactiveNodes: number;
36
+ }
37
+
38
+ export class RefRegistry {
39
+ private refMap: Map<string, RefEntry> = new Map();
40
+ private counter: number = 0;
41
+
42
+ /** Clear all refs and reset counter (call before each new snapshot) */
43
+ reset(): void {
44
+ this.refMap.clear();
45
+ this.counter = 0;
46
+ }
47
+
48
+ /**
49
+ * Walk the snapshot tree and assign refs to all interactive or content-bearing nodes.
50
+ * Returns a new tree with refs attached (as annotations in the formatter output).
51
+ */
52
+ assignRefs(root: SnapshotNode): Map<SnapshotNode, string> {
53
+ this.reset();
54
+ const nodeToRef = new Map<SnapshotNode, string>();
55
+ this.walkAndAssign(root, nodeToRef);
56
+ return nodeToRef;
57
+ }
58
+
59
+ private walkAndAssign(
60
+ node: SnapshotNode,
61
+ nodeToRef: Map<SnapshotNode, string>
62
+ ): void {
63
+ if (this.shouldAssignRef(node)) {
64
+ const ref = `e${++this.counter}`;
65
+ const centerX = node.bounds.x + node.bounds.width / 2;
66
+ const centerY = node.bounds.y + node.bounds.height / 2;
67
+
68
+ this.refMap.set(ref, {
69
+ ref,
70
+ node,
71
+ centerX,
72
+ centerY,
73
+ bounds: node.bounds,
74
+ });
75
+ nodeToRef.set(node, ref);
76
+ }
77
+
78
+ for (const child of node.children) {
79
+ this.walkAndAssign(child, nodeToRef);
80
+ }
81
+ }
82
+
83
+ /** Determine if a node should get a ref */
84
+ private shouldAssignRef(node: SnapshotNode): boolean {
85
+ // Skip invisible/zero-size elements
86
+ if (node.bounds.width === 0 || node.bounds.height === 0) return false;
87
+
88
+ // Always assign refs to interactive elements
89
+ if (node.interactive) return true;
90
+
91
+ // Assign refs to elements with meaningful text content
92
+ if (node.text && node.text.trim().length > 0) return true;
93
+
94
+ // Assign refs to elements with accessibility labels
95
+ if (node.label && node.label.trim().length > 0) return true;
96
+
97
+ // Skip purely structural containers
98
+ return false;
99
+ }
100
+
101
+ /** Resolve a ref string to its entry (coordinates + node info) */
102
+ resolve(ref: string): RefEntry | undefined {
103
+ return this.refMap.get(ref);
104
+ }
105
+
106
+ /** Get all current ref entries */
107
+ getAllRefs(): RefEntry[] {
108
+ return Array.from(this.refMap.values());
109
+ }
110
+
111
+ /** Get the total number of assigned refs */
112
+ get size(): number {
113
+ return this.refMap.size;
114
+ }
115
+
116
+ /**
117
+ * Validate the snapshot tree for visibility issues.
118
+ * Flags zero-size elements that should be visible, explicitly invisible elements,
119
+ * and empty containers that might indicate missing content.
120
+ */
121
+ validateTree(root: SnapshotNode): ValidationReport {
122
+ const issues: ValidationIssue[] = [];
123
+ let totalNodes = 0;
124
+ let visibleNodes = 0;
125
+ let interactiveNodes = 0;
126
+ let zeroSizeNodes = 0;
127
+
128
+ const walk = (node: SnapshotNode): void => {
129
+ totalNodes++;
130
+
131
+ const isZeroSize =
132
+ node.bounds.width === 0 || node.bounds.height === 0;
133
+
134
+ if (isZeroSize) {
135
+ zeroSizeNodes++;
136
+ // Only flag if the node has content that SHOULD be visible
137
+ if (node.text || node.label || node.interactive) {
138
+ issues.push({
139
+ type: "zero-size",
140
+ nodeType: node.type,
141
+ text: node.text,
142
+ label: node.label,
143
+ resourceId: node.resourceId,
144
+ bounds: node.bounds,
145
+ detail:
146
+ `${node.type}${node.text ? ` "${node.text}"` : ""}${node.label ? ` label="${node.label}"` : ""}` +
147
+ ` has zero size (${node.bounds.width}x${node.bounds.height}) — renders as invisible`,
148
+ });
149
+ }
150
+ } else {
151
+ visibleNodes++;
152
+ }
153
+
154
+ if (node.visible === false) {
155
+ issues.push({
156
+ type: "invisible",
157
+ nodeType: node.type,
158
+ text: node.text,
159
+ label: node.label,
160
+ resourceId: node.resourceId,
161
+ bounds: node.bounds,
162
+ detail:
163
+ `${node.type}${node.text ? ` "${node.text}"` : ""} has visible=false`,
164
+ });
165
+ }
166
+
167
+ if (node.interactive) interactiveNodes++;
168
+
169
+ // Empty container with a resource ID — likely an intentional component
170
+ // that should have content (e.g., missing icon, empty image)
171
+ if (
172
+ node.children.length === 0 &&
173
+ node.resourceId &&
174
+ !node.text &&
175
+ !node.label &&
176
+ !isZeroSize
177
+ ) {
178
+ issues.push({
179
+ type: "empty-container",
180
+ nodeType: node.type,
181
+ resourceId: node.resourceId,
182
+ bounds: node.bounds,
183
+ detail:
184
+ `${node.type} id="${node.resourceId}" is an empty container (no children, no text) — may be a missing icon or placeholder`,
185
+ });
186
+ }
187
+
188
+ for (const child of node.children) {
189
+ walk(child);
190
+ }
191
+ };
192
+
193
+ walk(root);
194
+ return { issues, totalNodes, visibleNodes, zeroSizeNodes, interactiveNodes };
195
+ }
196
+
197
+ /**
198
+ * Search the full snapshot tree (including zero-size nodes) for text/label matches.
199
+ * Used by find_element to detect invisible elements that the refMap skips.
200
+ */
201
+ findInTree(
202
+ root: SnapshotNode,
203
+ query: { text?: string; label?: string }
204
+ ): SnapshotNode[] {
205
+ const matches: SnapshotNode[] = [];
206
+ const walk = (node: SnapshotNode): void => {
207
+ const isZeroSize =
208
+ node.bounds.width === 0 || node.bounds.height === 0;
209
+ if (isZeroSize) {
210
+ let matched = false;
211
+ if (query.text && node.text) {
212
+ matched = node.text
213
+ .toLowerCase()
214
+ .includes(query.text.toLowerCase());
215
+ }
216
+ if (query.label && node.label) {
217
+ matched =
218
+ matched ||
219
+ node.label.toLowerCase().includes(query.label.toLowerCase());
220
+ }
221
+ if (matched) matches.push(node);
222
+ }
223
+ for (const child of node.children) {
224
+ walk(child);
225
+ }
226
+ };
227
+ walk(root);
228
+ return matches;
229
+ }
230
+ }
@@ -0,0 +1,220 @@
1
+ import type { SnapshotNode } from "../platform/device.js";
2
+
3
+ /**
4
+ * Incremental snapshot differ — compares two snapshot trees and returns
5
+ * only the changed portions. Saves 60-80% tokens in iterative workflows.
6
+ */
7
+
8
+ export interface SnapshotDiff {
9
+ /** Nodes that were added since the last snapshot */
10
+ added: SnapshotNode[];
11
+ /** Nodes that were removed since the last snapshot */
12
+ removed: SnapshotNode[];
13
+ /** Nodes whose properties changed (text, value, focus, etc.) */
14
+ changed: Array<{
15
+ node: SnapshotNode;
16
+ changes: string[];
17
+ }>;
18
+ /** Elements that are the same content but moved to a different position */
19
+ moved: Array<{
20
+ node: SnapshotNode;
21
+ from: { x: number; y: number };
22
+ to: { x: number; y: number };
23
+ deltaX: number;
24
+ deltaY: number;
25
+ }>;
26
+ /** Elements that kept position but changed size */
27
+ resized: Array<{
28
+ node: SnapshotNode;
29
+ from: { width: number; height: number };
30
+ to: { width: number; height: number };
31
+ }>;
32
+ /** Whether the tree structure changed significantly */
33
+ structuralChange: boolean;
34
+ }
35
+
36
+ /**
37
+ * Content-based signature — used for element identity.
38
+ * Intentionally excludes position/size so moved elements are matched
39
+ * to their previous version rather than treated as add+remove.
40
+ */
41
+ function contentSignature(node: SnapshotNode): string {
42
+ return [node.type, node.resourceId || "", node.text || "", node.label || ""].join("|");
43
+ }
44
+
45
+ /**
46
+ * Compute a diff between two snapshot trees.
47
+ *
48
+ * Identity matching: uses content signature (type + resourceId + text + label).
49
+ * Elements with matching content but different position are reported as "moved".
50
+ * Elements with matching content+position but different size are reported as "resized".
51
+ * Elements present in current but not previous are "added".
52
+ * Elements present in previous but not current are "removed".
53
+ */
54
+ export function diffSnapshots(
55
+ previous: SnapshotNode,
56
+ current: SnapshotNode
57
+ ): SnapshotDiff {
58
+ const prevNodes = flattenTree(previous);
59
+ const currNodes = flattenTree(current);
60
+
61
+ const prevByContent = new Map<string, SnapshotNode>();
62
+ for (const node of prevNodes) {
63
+ prevByContent.set(contentSignature(node), node);
64
+ }
65
+
66
+ const currByContent = new Map<string, SnapshotNode>();
67
+ for (const node of currNodes) {
68
+ currByContent.set(contentSignature(node), node);
69
+ }
70
+
71
+ const added: SnapshotNode[] = [];
72
+ const removed: SnapshotNode[] = [];
73
+ const changed: Array<{ node: SnapshotNode; changes: string[] }> = [];
74
+ const moved: SnapshotDiff["moved"] = [];
75
+ const resized: SnapshotDiff["resized"] = [];
76
+
77
+ // Pass 1: find added, moved, resized, changed nodes
78
+ for (const [sig, currNode] of currByContent) {
79
+ const prevNode = prevByContent.get(sig);
80
+
81
+ if (!prevNode) {
82
+ added.push(currNode);
83
+ continue;
84
+ }
85
+
86
+ const posChanged =
87
+ prevNode.bounds.x !== currNode.bounds.x ||
88
+ prevNode.bounds.y !== currNode.bounds.y;
89
+
90
+ const sizeChanged =
91
+ prevNode.bounds.width !== currNode.bounds.width ||
92
+ prevNode.bounds.height !== currNode.bounds.height;
93
+
94
+ if (posChanged) {
95
+ moved.push({
96
+ node: currNode,
97
+ from: { x: prevNode.bounds.x, y: prevNode.bounds.y },
98
+ to: { x: currNode.bounds.x, y: currNode.bounds.y },
99
+ deltaX: currNode.bounds.x - prevNode.bounds.x,
100
+ deltaY: currNode.bounds.y - prevNode.bounds.y,
101
+ });
102
+ }
103
+
104
+ if (sizeChanged) {
105
+ resized.push({
106
+ node: currNode,
107
+ from: { width: prevNode.bounds.width, height: prevNode.bounds.height },
108
+ to: { width: currNode.bounds.width, height: currNode.bounds.height },
109
+ });
110
+ }
111
+
112
+ const changes = detectChanges(prevNode, currNode);
113
+ if (changes.length > 0) {
114
+ changed.push({ node: currNode, changes });
115
+ }
116
+ }
117
+
118
+ // Pass 2: find removed nodes
119
+ for (const [sig, node] of prevByContent) {
120
+ if (!currByContent.has(sig)) {
121
+ removed.push(node);
122
+ }
123
+ }
124
+
125
+ const structuralChange =
126
+ added.length > prevNodes.length * 0.3 ||
127
+ removed.length > prevNodes.length * 0.3;
128
+
129
+ return { added, removed, changed, moved, resized, structuralChange };
130
+ }
131
+
132
+ /** Detect property-level changes between two nodes with the same identity */
133
+ function detectChanges(prev: SnapshotNode, curr: SnapshotNode): string[] {
134
+ const changes: string[] = [];
135
+
136
+ if (prev.text !== curr.text) {
137
+ changes.push(`text: "${prev.text}" → "${curr.text}"`);
138
+ }
139
+ if (prev.value !== curr.value) {
140
+ changes.push(`value: "${prev.value}" → "${curr.value}"`);
141
+ }
142
+ if (prev.focused !== curr.focused) {
143
+ changes.push(`focused: ${prev.focused} → ${curr.focused}`);
144
+ }
145
+ if (prev.enabled !== curr.enabled) {
146
+ changes.push(`enabled: ${prev.enabled} → ${curr.enabled}`);
147
+ }
148
+ if (prev.label !== curr.label) {
149
+ changes.push(`label: "${prev.label}" → "${curr.label}"`);
150
+ }
151
+
152
+ return changes;
153
+ }
154
+
155
+ /** Flatten a tree into a list of all nodes */
156
+ function flattenTree(node: SnapshotNode): SnapshotNode[] {
157
+ const result: SnapshotNode[] = [node];
158
+ for (const child of node.children) {
159
+ result.push(...flattenTree(child));
160
+ }
161
+ return result;
162
+ }
163
+
164
+ /** Format a diff for LLM consumption */
165
+ export function formatDiff(diff: SnapshotDiff): string {
166
+ if (diff.structuralChange) {
167
+ return "[Major structural change — use full snapshot]";
168
+ }
169
+
170
+ const lines: string[] = [];
171
+
172
+ if (diff.added.length > 0) {
173
+ lines.push("ADDED:");
174
+ for (const node of diff.added) {
175
+ lines.push(` + ${node.type} ${node.text ? `"${node.text}"` : ""}`);
176
+ }
177
+ }
178
+
179
+ if (diff.removed.length > 0) {
180
+ lines.push("REMOVED:");
181
+ for (const node of diff.removed) {
182
+ lines.push(` - ${node.type} ${node.text ? `"${node.text}"` : ""}`);
183
+ }
184
+ }
185
+
186
+ if (diff.moved.length > 0) {
187
+ lines.push("MOVED:");
188
+ for (const m of diff.moved) {
189
+ const label = m.node.text ? `"${m.node.text}"` : m.node.type;
190
+ const dx = m.deltaX > 0 ? `+${m.deltaX}` : `${m.deltaX}`;
191
+ const dy = m.deltaY > 0 ? `+${m.deltaY}` : `${m.deltaY}`;
192
+ lines.push(` -> ${label} moved by (${dx}, ${dy})px`);
193
+ }
194
+ }
195
+
196
+ if (diff.resized.length > 0) {
197
+ lines.push("RESIZED:");
198
+ for (const r of diff.resized) {
199
+ const label = r.node.text ? `"${r.node.text}"` : r.node.type;
200
+ lines.push(
201
+ ` <> ${label}: ${r.from.width}x${r.from.height} → ${r.to.width}x${r.to.height}`
202
+ );
203
+ }
204
+ }
205
+
206
+ if (diff.changed.length > 0) {
207
+ lines.push("CHANGED:");
208
+ for (const { node, changes } of diff.changed) {
209
+ lines.push(
210
+ ` ~ ${node.type} ${node.text ? `"${node.text}"` : ""}: ${changes.join(", ")}`
211
+ );
212
+ }
213
+ }
214
+
215
+ if (lines.length === 0) {
216
+ return "[No changes detected]";
217
+ }
218
+
219
+ return lines.join("\n");
220
+ }
@@ -0,0 +1,111 @@
1
+ import { z } from "zod";
2
+ import type { DeviceManager } from "../platform/device-manager.js";
3
+
4
+ export const appToolSchemas = {
5
+ devlens_launch_app: {
6
+ description:
7
+ "Launch an app on the device by its bundle ID (iOS) or package name (Android). The app must already be installed.",
8
+ parameters: z.object({
9
+ appId: z
10
+ .string()
11
+ .describe(
12
+ "Bundle ID (iOS, e.g., com.example.app) or package name (Android, e.g., com.example.app)"
13
+ ),
14
+ }),
15
+ },
16
+ devlens_terminate_app: {
17
+ description: "Force stop a running app on the device.",
18
+ parameters: z.object({
19
+ appId: z.string().describe("Bundle ID or package name of the app to stop"),
20
+ }),
21
+ },
22
+ devlens_install_app: {
23
+ description:
24
+ "Install an app from a local file path. Supports .apk (Android), .app/.ipa (iOS).",
25
+ parameters: z.object({
26
+ path: z
27
+ .string()
28
+ .describe("Local file path to the app binary (.apk, .app, or .ipa)"),
29
+ }),
30
+ },
31
+ devlens_list_apps: {
32
+ description:
33
+ "List all third-party apps installed on the device. Returns app IDs and names.",
34
+ parameters: z.object({}),
35
+ },
36
+ };
37
+
38
+ export function createAppToolHandlers(deviceManager: DeviceManager) {
39
+ return {
40
+ devlens_launch_app: async (params: { appId: string }) => {
41
+ const device = await deviceManager.getDevice();
42
+ await device.launchApp(params.appId);
43
+
44
+ return {
45
+ content: [
46
+ {
47
+ type: "text" as const,
48
+ text: `Launched app: ${params.appId}`,
49
+ },
50
+ ],
51
+ };
52
+ },
53
+
54
+ devlens_terminate_app: async (params: { appId: string }) => {
55
+ const device = await deviceManager.getDevice();
56
+ await device.terminateApp(params.appId);
57
+
58
+ return {
59
+ content: [
60
+ {
61
+ type: "text" as const,
62
+ text: `Terminated app: ${params.appId}`,
63
+ },
64
+ ],
65
+ };
66
+ },
67
+
68
+ devlens_install_app: async (params: { path: string }) => {
69
+ const device = await deviceManager.getDevice();
70
+ await device.installApp(params.path);
71
+
72
+ return {
73
+ content: [
74
+ {
75
+ type: "text" as const,
76
+ text: `Installed app from: ${params.path}`,
77
+ },
78
+ ],
79
+ };
80
+ },
81
+
82
+ devlens_list_apps: async () => {
83
+ const device = await deviceManager.getDevice();
84
+ const apps = await device.listApps();
85
+
86
+ if (apps.length === 0) {
87
+ return {
88
+ content: [
89
+ {
90
+ type: "text" as const,
91
+ text: "No third-party apps found on the device.",
92
+ },
93
+ ],
94
+ };
95
+ }
96
+
97
+ const lines = apps.map(
98
+ (app) => `${app.name} — ${app.appId}${app.isRunning ? " (running)" : ""}`
99
+ );
100
+
101
+ return {
102
+ content: [
103
+ {
104
+ type: "text" as const,
105
+ text: `Installed apps (${apps.length}):\n\n${lines.join("\n")}`,
106
+ },
107
+ ],
108
+ };
109
+ },
110
+ };
111
+ }
@@ -0,0 +1,96 @@
1
+ import { z } from "zod";
2
+ import type { DeviceManager } from "../platform/device-manager.js";
3
+
4
+ export const deviceToolSchemas = {
5
+ devlens_list_devices: {
6
+ description:
7
+ "List all running iOS Simulators and Android Emulators. Returns device IDs, names, platforms, and OS versions.",
8
+ parameters: z.object({}),
9
+ },
10
+ devlens_device_info: {
11
+ description:
12
+ "Get detailed information about a specific device: screen size, OS version, orientation. If no deviceId is provided, uses the first available device.",
13
+ parameters: z.object({
14
+ deviceId: z
15
+ .string()
16
+ .optional()
17
+ .describe("Device ID from devlens_list_devices. Optional."),
18
+ }),
19
+ },
20
+ devlens_set_orientation: {
21
+ description: "Set the device orientation to portrait or landscape.",
22
+ parameters: z.object({
23
+ orientation: z
24
+ .enum(["portrait", "landscape"])
25
+ .describe("Target orientation"),
26
+ }),
27
+ },
28
+ };
29
+
30
+ export function createDeviceToolHandlers(deviceManager: DeviceManager) {
31
+ return {
32
+ devlens_list_devices: async () => {
33
+ const devices = await deviceManager.discoverDevices();
34
+ if (devices.length === 0) {
35
+ return {
36
+ content: [
37
+ {
38
+ type: "text" as const,
39
+ text: "No running simulators or emulators found.\n\nTo start a device:\n- Android: Open Android Studio → Device Manager → Start an emulator\n- iOS: Open Xcode → Window → Devices and Simulators → Boot a simulator",
40
+ },
41
+ ],
42
+ };
43
+ }
44
+
45
+ const lines = devices.map(
46
+ (d) =>
47
+ `${d.isBooted ? "●" : "○"} ${d.name} (${d.platform}, ${d.osVersion}) — id: ${d.id}`
48
+ );
49
+
50
+ return {
51
+ content: [
52
+ {
53
+ type: "text" as const,
54
+ text: `Found ${devices.length} device(s):\n\n${lines.join("\n")}`,
55
+ },
56
+ ],
57
+ };
58
+ },
59
+
60
+ devlens_device_info: async (params: { deviceId?: string }) => {
61
+ const device = await deviceManager.getDevice(params.deviceId);
62
+ const info = await device.getInfo();
63
+
64
+ return {
65
+ content: [
66
+ {
67
+ type: "text" as const,
68
+ text: [
69
+ `Device: ${info.name}`,
70
+ `Platform: ${info.platform}`,
71
+ `OS Version: ${info.osVersion}`,
72
+ `Screen: ${info.screenWidth}x${info.screenHeight}`,
73
+ `Orientation: ${info.orientation}`,
74
+ `ID: ${info.id}`,
75
+ ].join("\n"),
76
+ },
77
+ ],
78
+ };
79
+ },
80
+
81
+ devlens_set_orientation: async (params: {
82
+ orientation: "portrait" | "landscape";
83
+ }) => {
84
+ const device = await deviceManager.getDevice();
85
+ await device.setOrientation(params.orientation);
86
+ return {
87
+ content: [
88
+ {
89
+ type: "text" as const,
90
+ text: `Orientation set to ${params.orientation}.`,
91
+ },
92
+ ],
93
+ };
94
+ },
95
+ };
96
+ }