@kata-sh/cli 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 (199) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +156 -0
  3. package/dist/app-paths.d.ts +4 -0
  4. package/dist/app-paths.js +6 -0
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +56 -0
  7. package/dist/loader.d.ts +2 -0
  8. package/dist/loader.js +95 -0
  9. package/dist/resource-loader.d.ts +18 -0
  10. package/dist/resource-loader.js +50 -0
  11. package/dist/wizard.d.ts +15 -0
  12. package/dist/wizard.js +159 -0
  13. package/package.json +50 -21
  14. package/pkg/dist/modes/interactive/theme/dark.json +85 -0
  15. package/pkg/dist/modes/interactive/theme/light.json +84 -0
  16. package/pkg/dist/modes/interactive/theme/theme-schema.json +335 -0
  17. package/pkg/dist/modes/interactive/theme/theme.d.ts +78 -0
  18. package/pkg/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  19. package/pkg/dist/modes/interactive/theme/theme.js +949 -0
  20. package/pkg/dist/modes/interactive/theme/theme.js.map +1 -0
  21. package/pkg/package.json +8 -0
  22. package/scripts/postinstall.js +45 -0
  23. package/src/resources/AGENTS.md +108 -0
  24. package/src/resources/KATA-WORKFLOW.md +661 -0
  25. package/src/resources/agents/researcher.md +29 -0
  26. package/src/resources/agents/scout.md +56 -0
  27. package/src/resources/agents/worker.md +31 -0
  28. package/src/resources/extensions/ask-user-questions.ts +200 -0
  29. package/src/resources/extensions/bg-shell/index.ts +2758 -0
  30. package/src/resources/extensions/browser-tools/BROWSER-TOOLS-V2-PROPOSAL.md +1277 -0
  31. package/src/resources/extensions/browser-tools/core.js +1057 -0
  32. package/src/resources/extensions/browser-tools/index.ts +4916 -0
  33. package/src/resources/extensions/browser-tools/package.json +20 -0
  34. package/src/resources/extensions/context7/index.ts +428 -0
  35. package/src/resources/extensions/context7/package.json +11 -0
  36. package/src/resources/extensions/get-secrets-from-user.ts +352 -0
  37. package/src/resources/extensions/github/formatters.ts +207 -0
  38. package/src/resources/extensions/github/gh-api.ts +537 -0
  39. package/src/resources/extensions/github/index.ts +778 -0
  40. package/src/resources/extensions/kata/activity-log.ts +88 -0
  41. package/src/resources/extensions/kata/auto.ts +2786 -0
  42. package/src/resources/extensions/kata/commands.ts +355 -0
  43. package/src/resources/extensions/kata/crash-recovery.ts +85 -0
  44. package/src/resources/extensions/kata/dashboard-overlay.ts +516 -0
  45. package/src/resources/extensions/kata/docs/preferences-reference.md +103 -0
  46. package/src/resources/extensions/kata/doctor.ts +683 -0
  47. package/src/resources/extensions/kata/files.ts +730 -0
  48. package/src/resources/extensions/kata/gitignore.ts +165 -0
  49. package/src/resources/extensions/kata/guided-flow.ts +976 -0
  50. package/src/resources/extensions/kata/index.ts +556 -0
  51. package/src/resources/extensions/kata/metrics.ts +397 -0
  52. package/src/resources/extensions/kata/observability-validator.ts +408 -0
  53. package/src/resources/extensions/kata/package.json +11 -0
  54. package/src/resources/extensions/kata/paths.ts +346 -0
  55. package/src/resources/extensions/kata/preferences.ts +695 -0
  56. package/src/resources/extensions/kata/prompt-loader.ts +50 -0
  57. package/src/resources/extensions/kata/prompts/complete-milestone.md +25 -0
  58. package/src/resources/extensions/kata/prompts/complete-slice.md +27 -0
  59. package/src/resources/extensions/kata/prompts/discuss.md +151 -0
  60. package/src/resources/extensions/kata/prompts/doctor-heal.md +29 -0
  61. package/src/resources/extensions/kata/prompts/execute-task.md +64 -0
  62. package/src/resources/extensions/kata/prompts/guided-complete-slice.md +1 -0
  63. package/src/resources/extensions/kata/prompts/guided-discuss-milestone.md +3 -0
  64. package/src/resources/extensions/kata/prompts/guided-discuss-slice.md +59 -0
  65. package/src/resources/extensions/kata/prompts/guided-execute-task.md +1 -0
  66. package/src/resources/extensions/kata/prompts/guided-plan-milestone.md +23 -0
  67. package/src/resources/extensions/kata/prompts/guided-plan-slice.md +1 -0
  68. package/src/resources/extensions/kata/prompts/guided-research-slice.md +11 -0
  69. package/src/resources/extensions/kata/prompts/guided-resume-task.md +1 -0
  70. package/src/resources/extensions/kata/prompts/plan-milestone.md +47 -0
  71. package/src/resources/extensions/kata/prompts/plan-slice.md +63 -0
  72. package/src/resources/extensions/kata/prompts/queue.md +85 -0
  73. package/src/resources/extensions/kata/prompts/reassess-roadmap.md +48 -0
  74. package/src/resources/extensions/kata/prompts/replan-slice.md +39 -0
  75. package/src/resources/extensions/kata/prompts/research-milestone.md +37 -0
  76. package/src/resources/extensions/kata/prompts/research-slice.md +28 -0
  77. package/src/resources/extensions/kata/prompts/run-uat.md +109 -0
  78. package/src/resources/extensions/kata/prompts/system.md +341 -0
  79. package/src/resources/extensions/kata/session-forensics.ts +550 -0
  80. package/src/resources/extensions/kata/skill-discovery.ts +137 -0
  81. package/src/resources/extensions/kata/state.ts +509 -0
  82. package/src/resources/extensions/kata/templates/context.md +76 -0
  83. package/src/resources/extensions/kata/templates/decisions.md +8 -0
  84. package/src/resources/extensions/kata/templates/milestone-summary.md +73 -0
  85. package/src/resources/extensions/kata/templates/plan.md +133 -0
  86. package/src/resources/extensions/kata/templates/preferences.md +15 -0
  87. package/src/resources/extensions/kata/templates/project.md +31 -0
  88. package/src/resources/extensions/kata/templates/reassessment.md +28 -0
  89. package/src/resources/extensions/kata/templates/requirements.md +81 -0
  90. package/src/resources/extensions/kata/templates/research.md +46 -0
  91. package/src/resources/extensions/kata/templates/roadmap.md +118 -0
  92. package/src/resources/extensions/kata/templates/slice-context.md +58 -0
  93. package/src/resources/extensions/kata/templates/slice-summary.md +99 -0
  94. package/src/resources/extensions/kata/templates/state.md +19 -0
  95. package/src/resources/extensions/kata/templates/task-plan.md +52 -0
  96. package/src/resources/extensions/kata/templates/task-summary.md +57 -0
  97. package/src/resources/extensions/kata/templates/uat.md +54 -0
  98. package/src/resources/extensions/kata/tests/activity-log-prune.test.ts +327 -0
  99. package/src/resources/extensions/kata/tests/auto-preflight.test.ts +97 -0
  100. package/src/resources/extensions/kata/tests/auto-supervisor.test.mjs +53 -0
  101. package/src/resources/extensions/kata/tests/complete-milestone.test.ts +317 -0
  102. package/src/resources/extensions/kata/tests/cost-projection.test.ts +160 -0
  103. package/src/resources/extensions/kata/tests/derive-state-deps.test.ts +477 -0
  104. package/src/resources/extensions/kata/tests/derive-state.test.ts +1013 -0
  105. package/src/resources/extensions/kata/tests/doctor.test.ts +718 -0
  106. package/src/resources/extensions/kata/tests/idle-recovery.test.ts +490 -0
  107. package/src/resources/extensions/kata/tests/metrics-io.test.ts +254 -0
  108. package/src/resources/extensions/kata/tests/metrics.test.ts +217 -0
  109. package/src/resources/extensions/kata/tests/must-have-parser.test.ts +309 -0
  110. package/src/resources/extensions/kata/tests/parsers.test.ts +1257 -0
  111. package/src/resources/extensions/kata/tests/plan-milestone.test.ts +185 -0
  112. package/src/resources/extensions/kata/tests/plan-quality-validator.test.ts +386 -0
  113. package/src/resources/extensions/kata/tests/reassess-prompt.test.ts +208 -0
  114. package/src/resources/extensions/kata/tests/replan-slice.test.ts +686 -0
  115. package/src/resources/extensions/kata/tests/requirements.test.ts +151 -0
  116. package/src/resources/extensions/kata/tests/resolve-ts-hooks.mjs +17 -0
  117. package/src/resources/extensions/kata/tests/resolve-ts.mjs +11 -0
  118. package/src/resources/extensions/kata/tests/run-uat.test.ts +383 -0
  119. package/src/resources/extensions/kata/tests/unit-runtime.test.ts +388 -0
  120. package/src/resources/extensions/kata/tests/workspace-index.test.ts +118 -0
  121. package/src/resources/extensions/kata/tests/worktree.test.ts +222 -0
  122. package/src/resources/extensions/kata/types.ts +159 -0
  123. package/src/resources/extensions/kata/unit-runtime.ts +163 -0
  124. package/src/resources/extensions/kata/workspace-index.ts +203 -0
  125. package/src/resources/extensions/kata/worktree.ts +182 -0
  126. package/src/resources/extensions/mac-tools/index.ts +852 -0
  127. package/src/resources/extensions/mac-tools/swift-cli/Package.swift +22 -0
  128. package/src/resources/extensions/mac-tools/swift-cli/Sources/main.swift +1318 -0
  129. package/src/resources/extensions/search-the-web/cache.ts +78 -0
  130. package/src/resources/extensions/search-the-web/format.ts +258 -0
  131. package/src/resources/extensions/search-the-web/http.ts +238 -0
  132. package/src/resources/extensions/search-the-web/index.ts +68 -0
  133. package/src/resources/extensions/search-the-web/tool-fetch-page.ts +519 -0
  134. package/src/resources/extensions/search-the-web/tool-llm-context.ts +404 -0
  135. package/src/resources/extensions/search-the-web/tool-search.ts +503 -0
  136. package/src/resources/extensions/search-the-web/url-utils.ts +91 -0
  137. package/src/resources/extensions/shared/confirm-ui.ts +126 -0
  138. package/src/resources/extensions/shared/interview-ui.ts +822 -0
  139. package/src/resources/extensions/shared/next-action-ui.ts +235 -0
  140. package/src/resources/extensions/shared/progress-widget.ts +282 -0
  141. package/src/resources/extensions/shared/thinking-widget.ts +107 -0
  142. package/src/resources/extensions/shared/ui.ts +400 -0
  143. package/src/resources/extensions/shared/wizard-ui.ts +551 -0
  144. package/src/resources/extensions/slash-commands/audit.ts +92 -0
  145. package/src/resources/extensions/slash-commands/create-extension.ts +375 -0
  146. package/src/resources/extensions/slash-commands/create-slash-command.ts +280 -0
  147. package/src/resources/extensions/slash-commands/index.ts +12 -0
  148. package/src/resources/extensions/slash-commands/kata-run.ts +34 -0
  149. package/src/resources/extensions/subagent/agents.ts +126 -0
  150. package/src/resources/extensions/subagent/index.ts +1293 -0
  151. package/src/resources/skills/debug-like-expert/SKILL.md +231 -0
  152. package/src/resources/skills/debug-like-expert/references/debugging-mindset.md +253 -0
  153. package/src/resources/skills/debug-like-expert/references/hypothesis-testing.md +373 -0
  154. package/src/resources/skills/debug-like-expert/references/investigation-techniques.md +337 -0
  155. package/src/resources/skills/debug-like-expert/references/verification-patterns.md +425 -0
  156. package/src/resources/skills/debug-like-expert/references/when-to-research.md +361 -0
  157. package/src/resources/skills/frontend-design/SKILL.md +45 -0
  158. package/src/resources/skills/swiftui/SKILL.md +208 -0
  159. package/src/resources/skills/swiftui/references/animations.md +921 -0
  160. package/src/resources/skills/swiftui/references/architecture.md +1561 -0
  161. package/src/resources/skills/swiftui/references/layout-system.md +1186 -0
  162. package/src/resources/skills/swiftui/references/navigation.md +1492 -0
  163. package/src/resources/skills/swiftui/references/networking-async.md +214 -0
  164. package/src/resources/skills/swiftui/references/performance.md +1706 -0
  165. package/src/resources/skills/swiftui/references/platform-integration.md +204 -0
  166. package/src/resources/skills/swiftui/references/state-management.md +1443 -0
  167. package/src/resources/skills/swiftui/references/swiftdata.md +297 -0
  168. package/src/resources/skills/swiftui/references/testing-debugging.md +247 -0
  169. package/src/resources/skills/swiftui/references/uikit-appkit-interop.md +218 -0
  170. package/src/resources/skills/swiftui/workflows/add-feature.md +191 -0
  171. package/src/resources/skills/swiftui/workflows/build-new-app.md +311 -0
  172. package/src/resources/skills/swiftui/workflows/debug-swiftui.md +192 -0
  173. package/src/resources/skills/swiftui/workflows/optimize-performance.md +197 -0
  174. package/src/resources/skills/swiftui/workflows/ship-app.md +203 -0
  175. package/src/resources/skills/swiftui/workflows/write-tests.md +235 -0
  176. package/dist/commands/task.d.ts +0 -9
  177. package/dist/commands/task.d.ts.map +0 -1
  178. package/dist/commands/task.js +0 -129
  179. package/dist/commands/task.js.map +0 -1
  180. package/dist/commands/task.test.d.ts +0 -2
  181. package/dist/commands/task.test.d.ts.map +0 -1
  182. package/dist/commands/task.test.js +0 -169
  183. package/dist/commands/task.test.js.map +0 -1
  184. package/dist/e2e/task-e2e.test.d.ts +0 -2
  185. package/dist/e2e/task-e2e.test.d.ts.map +0 -1
  186. package/dist/e2e/task-e2e.test.js +0 -173
  187. package/dist/e2e/task-e2e.test.js.map +0 -1
  188. package/dist/index.d.ts +0 -3
  189. package/dist/index.d.ts.map +0 -1
  190. package/dist/index.js +0 -93
  191. package/dist/index.js.map +0 -1
  192. package/dist/slug.d.ts +0 -2
  193. package/dist/slug.d.ts.map +0 -1
  194. package/dist/slug.js +0 -12
  195. package/dist/slug.js.map +0 -1
  196. package/dist/slug.test.d.ts +0 -2
  197. package/dist/slug.test.d.ts.map +0 -1
  198. package/dist/slug.test.js +0 -32
  199. package/dist/slug.test.js.map +0 -1
@@ -0,0 +1,852 @@
1
+ /**
2
+ * mac-tools — pi extension
3
+ *
4
+ * Gives the agent macOS automation capabilities via a Swift CLI that interfaces
5
+ * with Accessibility APIs, NSWorkspace, and CGWindowList.
6
+ *
7
+ * Architecture:
8
+ * - Swift CLI (`swift-cli/`) handles all macOS API calls
9
+ * - JSON protocol: stdin `{ command, params }` → stdout `{ success, data?, error? }`
10
+ * - TS extension invokes CLI per-command via execFileSync
11
+ * - Mtime-based compilation caching: recompiles only when source files change
12
+ * - All Swift debug output goes to stderr; only JSON on stdout
13
+ */
14
+
15
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
16
+ import { StringEnum } from "@mariozechner/pi-ai";
17
+ import { Type } from "@sinclair/typebox";
18
+ import { execFileSync } from "node:child_process";
19
+ import { statSync, readdirSync } from "node:fs";
20
+ import path from "node:path";
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // Paths
24
+ // ---------------------------------------------------------------------------
25
+
26
+ const EXTENSION_DIR = path.dirname(new URL(import.meta.url).pathname);
27
+ const SWIFT_CLI_DIR = path.join(EXTENSION_DIR, "swift-cli");
28
+ const SOURCES_DIR = path.join(SWIFT_CLI_DIR, "Sources");
29
+ const BINARY_PATH = path.join(SWIFT_CLI_DIR, ".build", "release", "mac-agent");
30
+ const PACKAGE_SWIFT = path.join(SWIFT_CLI_DIR, "Package.swift");
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Compilation caching
34
+ // ---------------------------------------------------------------------------
35
+
36
+ /** Get the latest mtime (ms) across all Swift source files and Package.swift. */
37
+ function getSourceMtime(): number {
38
+ let latest = 0;
39
+ // Check Package.swift
40
+ try {
41
+ latest = Math.max(latest, statSync(PACKAGE_SWIFT).mtimeMs);
42
+ } catch {}
43
+ // Check all files in Sources/
44
+ try {
45
+ const files = readdirSync(SOURCES_DIR);
46
+ for (const f of files) {
47
+ try {
48
+ const mt = statSync(path.join(SOURCES_DIR, f)).mtimeMs;
49
+ if (mt > latest) latest = mt;
50
+ } catch {}
51
+ }
52
+ } catch {}
53
+ return latest;
54
+ }
55
+
56
+ /** Get the binary mtime (ms), or 0 if it doesn't exist. */
57
+ function getBinaryMtime(): number {
58
+ try {
59
+ return statSync(BINARY_PATH).mtimeMs;
60
+ } catch {
61
+ return 0;
62
+ }
63
+ }
64
+
65
+ /** Compile the Swift CLI if source files are newer than the binary. */
66
+ function ensureCompiled(): void {
67
+ const srcMtime = getSourceMtime();
68
+ const binMtime = getBinaryMtime();
69
+
70
+ if (binMtime > 0 && binMtime >= srcMtime) {
71
+ return; // Binary is up-to-date
72
+ }
73
+
74
+ const action = binMtime === 0 ? "Compiling" : "Recompiling";
75
+ try {
76
+ execFileSync("swift", ["build", "-c", "release"], {
77
+ cwd: SWIFT_CLI_DIR,
78
+ timeout: 30_000,
79
+ stdio: ["pipe", "pipe", "pipe"],
80
+ });
81
+ } catch (err: any) {
82
+ const stderr = err.stderr?.toString() || "";
83
+ const stdout = err.stdout?.toString() || "";
84
+ throw new Error(
85
+ `Swift compilation failed (${action.toLowerCase()}):\n${stderr || stdout || err.message}`
86
+ );
87
+ }
88
+ }
89
+
90
+ // ---------------------------------------------------------------------------
91
+ // CLI invocation
92
+ // ---------------------------------------------------------------------------
93
+
94
+ interface MacAgentResponse {
95
+ success: boolean;
96
+ data?: Record<string, any>;
97
+ error?: string;
98
+ }
99
+
100
+ /**
101
+ * Invoke the mac-agent CLI with a command and optional params.
102
+ * Handles compilation caching, stdin/stdout JSON protocol, and error surfacing.
103
+ */
104
+ function execMacAgent(command: string, params?: Record<string, any>): MacAgentResponse {
105
+ ensureCompiled();
106
+
107
+ const input = JSON.stringify({ command, params: params ?? {} });
108
+ let stdout: string;
109
+ let stderr: string = "";
110
+
111
+ // Interaction commands (click, type) can block while the target app
112
+ // processes the action — e.g. TextEdit's AXPress on "New Document"
113
+ // takes ~12s while it dismisses the Open dialog and creates a window.
114
+ // Screenshots can also be slow for large retina windows.
115
+ const slowCommands = new Set(["clickElement", "typeText", "screenshotWindow"]);
116
+ const timeout = slowCommands.has(command) ? 30_000 : 10_000;
117
+
118
+ try {
119
+ const result = execFileSync(BINARY_PATH, [], {
120
+ input,
121
+ timeout,
122
+ encoding: "utf-8",
123
+ stdio: ["pipe", "pipe", "pipe"],
124
+ maxBuffer: 5 * 1024 * 1024, // 5MB — needed for retina screenshot base64 payloads
125
+ });
126
+ stdout = typeof result === "string" ? result : result.toString();
127
+ } catch (err: any) {
128
+ stderr = err.stderr?.toString() || "";
129
+ const isTimeout = err.killed || err.signal === "SIGTERM";
130
+ // If the process exited non-zero but produced stdout, try to parse it
131
+ if (err.stdout) {
132
+ stdout = err.stdout.toString();
133
+ } else if (isTimeout) {
134
+ throw new Error(
135
+ `mac-agent timed out after ${timeout / 1000}s (command: ${command}). ` +
136
+ `The target app may be slow to respond — AXPress can block while the app processes the action.`
137
+ );
138
+ } else {
139
+ throw new Error(
140
+ `mac-agent CLI failed (command: ${command}):\n${stderr || err.message}`
141
+ );
142
+ }
143
+ }
144
+
145
+ try {
146
+ return JSON.parse(stdout.trim()) as MacAgentResponse;
147
+ } catch {
148
+ throw new Error(
149
+ `mac-agent returned invalid JSON (command: ${command}):\nstdout: ${stdout}\nstderr: ${stderr}`
150
+ );
151
+ }
152
+ }
153
+
154
+ // ---------------------------------------------------------------------------
155
+ // Extension entry point
156
+ // ---------------------------------------------------------------------------
157
+
158
+ export default function (pi: ExtensionAPI) {
159
+ // -----------------------------------------------------------------
160
+ // mac_check_permissions
161
+ // -----------------------------------------------------------------
162
+ pi.registerTool({
163
+ name: "mac_check_permissions",
164
+ label: "Mac Permissions",
165
+ description:
166
+ "Check whether macOS Accessibility and Screen Recording permissions are enabled for the current terminal. " +
167
+ "Returns { accessibilityEnabled, screenRecordingEnabled }. Accessibility is required for UI automation; " +
168
+ "Screen Recording is required for mac_screenshot. Both are granted in System Settings > Privacy & Security.",
169
+ promptGuidelines: [
170
+ "Run this first if any mac tool returns a permission error.",
171
+ ],
172
+ parameters: Type.Object({}),
173
+
174
+ async execute(_toolCallId: any) {
175
+ const result = execMacAgent("checkPermissions");
176
+ if (!result.success) {
177
+ throw new Error("mac_check_permissions: " + result.error);
178
+ }
179
+ const accessibility = result.data?.accessibilityEnabled ?? false;
180
+ const screenRecording = result.data?.screenRecordingEnabled ?? false;
181
+
182
+ const lines: string[] = [];
183
+ lines.push(accessibility
184
+ ? "✅ Accessibility: enabled"
185
+ : "❌ Accessibility: NOT enabled — grant in System Settings > Privacy & Security > Accessibility");
186
+ lines.push(screenRecording
187
+ ? "✅ Screen Recording: enabled"
188
+ : "❌ Screen Recording: NOT enabled — grant in System Settings > Privacy & Security > Screen Recording");
189
+
190
+ return {
191
+ content: [{ type: "text" as const, text: lines.join("\n") }],
192
+ details: result.data,
193
+ };
194
+ },
195
+ });
196
+
197
+ // -----------------------------------------------------------------
198
+ // mac_list_apps
199
+ // -----------------------------------------------------------------
200
+ pi.registerTool({
201
+ name: "mac_list_apps",
202
+ label: "List Apps",
203
+ description:
204
+ "List all running macOS applications. Returns an array of { name, bundleId, pid, isActive } " +
205
+ "for user-facing apps (regular activation policy). Set includeBackground to true to also " +
206
+ "include accessory/background apps.",
207
+ promptGuidelines: [
208
+ "Use to discover what apps are running before interacting with them.",
209
+ ],
210
+ parameters: Type.Object({
211
+ includeBackground: Type.Optional(Type.Boolean({ description: "Include background/accessory apps (default: false)" })),
212
+ }),
213
+
214
+ async execute(_toolCallId: any, { includeBackground }: { includeBackground?: boolean }) {
215
+ const result = execMacAgent("listApps", includeBackground ? { includeBackground: true } : undefined);
216
+ if (!result.success) {
217
+ throw new Error("mac_list_apps: " + result.error);
218
+ }
219
+ const apps = result.data as unknown as Array<{ name: string; bundleId: string; pid: number; isActive: boolean }>;
220
+ const summary = apps.map(a => `${a.name} (${a.bundleId}) pid:${a.pid}${a.isActive ? " [active]" : ""}`).join("\n");
221
+ return {
222
+ content: [{ type: "text" as const, text: `${apps.length} running apps:\n${summary}` }],
223
+ details: { apps },
224
+ };
225
+ },
226
+ });
227
+
228
+ // -----------------------------------------------------------------
229
+ // mac_launch_app
230
+ // -----------------------------------------------------------------
231
+ pi.registerTool({
232
+ name: "mac_launch_app",
233
+ label: "Launch App",
234
+ description:
235
+ "Launch a macOS application by name or bundle ID. " +
236
+ "Returns { launched, name, bundleId, pid } on success. " +
237
+ "Provide either 'name' (e.g. 'TextEdit') or 'bundleId' (e.g. 'com.apple.TextEdit').",
238
+ promptGuidelines: [
239
+ "Use app name for well-known apps; use bundleId when the name is ambiguous.",
240
+ ],
241
+ parameters: Type.Object({
242
+ name: Type.Optional(Type.String({ description: "Application name (e.g. 'TextEdit', 'Safari')" })),
243
+ bundleId: Type.Optional(Type.String({ description: "Bundle identifier (e.g. 'com.apple.TextEdit')" })),
244
+ }),
245
+
246
+ async execute(_toolCallId: any, { name, bundleId }: { name?: string; bundleId?: string }) {
247
+ if (!name && !bundleId) {
248
+ throw new Error("mac_launch_app: provide either 'name' or 'bundleId' parameter");
249
+ }
250
+ const params: Record<string, string> = {};
251
+ if (name) params.name = name;
252
+ if (bundleId) params.bundleId = bundleId;
253
+
254
+ const result = execMacAgent("launchApp", params);
255
+ if (!result.success) {
256
+ throw new Error("mac_launch_app: " + result.error);
257
+ }
258
+ const d = result.data!;
259
+ return {
260
+ content: [{ type: "text" as const, text: `Launched ${d.name} (${d.bundleId}) pid:${d.pid}` }],
261
+ details: result.data,
262
+ };
263
+ },
264
+ });
265
+
266
+ // -----------------------------------------------------------------
267
+ // mac_activate_app
268
+ // -----------------------------------------------------------------
269
+ pi.registerTool({
270
+ name: "mac_activate_app",
271
+ label: "Activate App",
272
+ description:
273
+ "Bring a running macOS application to the front. " +
274
+ "Returns { activated, name } on success. Errors if the app is not running. " +
275
+ "Provide either 'name' or 'bundleId'.",
276
+ promptGuidelines: [
277
+ "Activate an app before interacting with its UI to ensure it is frontmost.",
278
+ ],
279
+ parameters: Type.Object({
280
+ name: Type.Optional(Type.String({ description: "Application name" })),
281
+ bundleId: Type.Optional(Type.String({ description: "Bundle identifier" })),
282
+ }),
283
+
284
+ async execute(_toolCallId: any, { name, bundleId }: { name?: string; bundleId?: string }) {
285
+ if (!name && !bundleId) {
286
+ throw new Error("mac_activate_app: provide either 'name' or 'bundleId' parameter");
287
+ }
288
+ const params: Record<string, string> = {};
289
+ if (name) params.name = name;
290
+ if (bundleId) params.bundleId = bundleId;
291
+
292
+ const result = execMacAgent("activateApp", params);
293
+ if (!result.success) {
294
+ throw new Error("mac_activate_app: " + result.error);
295
+ }
296
+ return {
297
+ content: [{ type: "text" as const, text: `Activated ${result.data?.name}` }],
298
+ details: result.data,
299
+ };
300
+ },
301
+ });
302
+
303
+ // -----------------------------------------------------------------
304
+ // mac_quit_app
305
+ // -----------------------------------------------------------------
306
+ pi.registerTool({
307
+ name: "mac_quit_app",
308
+ label: "Quit App",
309
+ description:
310
+ "Quit a running macOS application. " +
311
+ "Returns { quit, name } on success. Errors if the app is not running. " +
312
+ "Provide either 'name' or 'bundleId'.",
313
+ promptGuidelines: [
314
+ "Use to clean up apps launched during automation — don't leave apps running unnecessarily.",
315
+ ],
316
+ parameters: Type.Object({
317
+ name: Type.Optional(Type.String({ description: "Application name" })),
318
+ bundleId: Type.Optional(Type.String({ description: "Bundle identifier" })),
319
+ }),
320
+
321
+ async execute(_toolCallId: any, { name, bundleId }: { name?: string; bundleId?: string }) {
322
+ if (!name && !bundleId) {
323
+ throw new Error("mac_quit_app: provide either 'name' or 'bundleId' parameter");
324
+ }
325
+ const params: Record<string, string> = {};
326
+ if (name) params.name = name;
327
+ if (bundleId) params.bundleId = bundleId;
328
+
329
+ const result = execMacAgent("quitApp", params);
330
+ if (!result.success) {
331
+ throw new Error("mac_quit_app: " + result.error);
332
+ }
333
+ return {
334
+ content: [{ type: "text" as const, text: `Quit ${result.data?.name}` }],
335
+ details: result.data,
336
+ };
337
+ },
338
+ });
339
+
340
+ // -----------------------------------------------------------------
341
+ // mac_list_windows
342
+ // -----------------------------------------------------------------
343
+ pi.registerTool({
344
+ name: "mac_list_windows",
345
+ label: "List Windows",
346
+ description:
347
+ "List all on-screen windows for a macOS application. " +
348
+ "Returns an array of { windowId, title, bounds: {x,y,width,height}, isOnScreen, layer }. " +
349
+ "The windowId can be used with getWindowInfo for detailed inspection or with screenshotWindow for capture. " +
350
+ "Returns an empty array (not error) if the app is running but has no visible windows. " +
351
+ "Errors if the app is not running.",
352
+ promptGuidelines: [
353
+ "Use to get windowId values needed by mac_screenshot.",
354
+ ],
355
+ parameters: Type.Object({
356
+ app: Type.String({ description: "Application name (e.g. 'TextEdit') or bundle identifier (e.g. 'com.apple.TextEdit')" }),
357
+ }),
358
+
359
+ async execute(_toolCallId: any, { app }: { app: string }) {
360
+ const result = execMacAgent("listWindows", { app });
361
+ if (!result.success) {
362
+ throw new Error("mac_list_windows: " + result.error);
363
+ }
364
+ const data = result.data as { windows: Array<{ windowId: number; title: string; bounds: Record<string, number>; isOnScreen: boolean; layer: number }>; app: string; pid: number };
365
+ const windows = data.windows ?? [];
366
+ if (windows.length === 0) {
367
+ return {
368
+ content: [{ type: "text" as const, text: `${data.app} (pid:${data.pid}) has no visible windows.` }],
369
+ details: data,
370
+ };
371
+ }
372
+ const summary = windows.map(w =>
373
+ ` windowId:${w.windowId} "${w.title}" ${w.bounds.width}x${w.bounds.height} at (${w.bounds.x},${w.bounds.y}) layer:${w.layer}`
374
+ ).join("\n");
375
+ return {
376
+ content: [{ type: "text" as const, text: `${data.app} (pid:${data.pid}) — ${windows.length} window(s):\n${summary}` }],
377
+ details: data,
378
+ };
379
+ },
380
+ });
381
+
382
+ // -----------------------------------------------------------------
383
+ // mac_find
384
+ // -----------------------------------------------------------------
385
+ pi.registerTool({
386
+ name: "mac_find",
387
+ label: "Find Elements",
388
+ description:
389
+ "Find UI elements in a macOS application's accessibility tree. Three modes:\n" +
390
+ "- 'search' (default): Find elements matching role/title/value/identifier criteria. Returns a numbered list of matches.\n" +
391
+ "- 'tree': Dump the full accessibility subtree as an indented tree. Use maxDepth/maxCount to bound output.\n" +
392
+ "- 'focused': Get the currently focused element in the app. No criteria needed.\n" +
393
+ "The 'app' param accepts an app name (e.g. 'Finder') or bundle ID (e.g. 'com.apple.Finder').",
394
+ promptGuidelines: [
395
+ "Prefer for targeted element search — use role/title/value criteria to narrow results.",
396
+ "Use mode:focused to check the current focus target without search criteria.",
397
+ "Use mac_get_tree instead of mode:tree when you just need to understand app structure.",
398
+ ],
399
+ parameters: Type.Object({
400
+ app: Type.String({ description: "Application name or bundle identifier" }),
401
+ mode: Type.Optional(StringEnum(["search", "tree", "focused"] as const, { description: "'search' (default), 'tree', or 'focused'" })),
402
+ role: Type.Optional(Type.String({ description: "AX role to match (e.g. 'AXButton', 'AXTextArea')" })),
403
+ title: Type.Optional(Type.String({ description: "AX title to match" })),
404
+ value: Type.Optional(Type.String({ description: "AX value to match" })),
405
+ identifier: Type.Optional(Type.String({ description: "AX identifier to match" })),
406
+ matchType: Type.Optional(Type.String({ description: "'exact' (default) or 'contains'" })),
407
+ maxDepth: Type.Optional(Type.Number({ description: "Maximum tree depth to traverse (default: 10)" })),
408
+ maxCount: Type.Optional(Type.Number({ description: "Maximum elements to return/visit (default: 100)" })),
409
+ }),
410
+
411
+ async execute(_toolCallId: any, args: {
412
+ app: string;
413
+ mode?: string;
414
+ role?: string;
415
+ title?: string;
416
+ value?: string;
417
+ identifier?: string;
418
+ matchType?: string;
419
+ maxDepth?: number;
420
+ maxCount?: number;
421
+ }) {
422
+ const mode = args.mode ?? "search";
423
+
424
+ // --- Focused mode ---
425
+ if (mode === "focused") {
426
+ const result = execMacAgent("getFocusedElement", { app: args.app });
427
+ if (!result.success) {
428
+ throw new Error("mac_find (focused): " + result.error);
429
+ }
430
+ const el = result.data as Record<string, any>;
431
+ const parts = [el.role ?? "unknown"];
432
+ if (el.title) parts.push(`"${el.title}"`);
433
+ if (el.value !== undefined) parts.push(`[${el.value}]`);
434
+ return {
435
+ content: [{ type: "text" as const, text: `Focused element: ${parts.join(" ")}` }],
436
+ details: result.data,
437
+ };
438
+ }
439
+
440
+ // --- Tree mode ---
441
+ if (mode === "tree") {
442
+ const params: Record<string, any> = { app: args.app };
443
+ if (args.maxDepth !== undefined) params.maxDepth = args.maxDepth;
444
+ if (args.maxCount !== undefined) params.maxCount = args.maxCount;
445
+
446
+ const result = execMacAgent("getTree", params);
447
+ if (!result.success) {
448
+ throw new Error("mac_find (tree): " + result.error);
449
+ }
450
+
451
+ const data = result.data as { tree: any[]; totalElements: number; truncated: boolean };
452
+ const lines: string[] = [];
453
+
454
+ function renderTree(nodes: any[], indent: number) {
455
+ for (const node of nodes) {
456
+ const parts = [node.role ?? "?"];
457
+ if (node.title) parts.push(`"${node.title}"`);
458
+ if (node.value !== undefined && node.value !== "") parts.push(`[${node.value}]`);
459
+ lines.push(" ".repeat(indent) + parts.join(" "));
460
+ if (node.children?.length) {
461
+ renderTree(node.children, indent + 1);
462
+ }
463
+ }
464
+ }
465
+
466
+ renderTree(data.tree ?? [], 0);
467
+ const truncNote = data.truncated ? `\n(truncated — ${data.totalElements} elements visited)` : "";
468
+ return {
469
+ content: [{ type: "text" as const, text: `${lines.join("\n")}${truncNote}` }],
470
+ details: result.data,
471
+ };
472
+ }
473
+
474
+ // --- Search mode (default) ---
475
+ const params: Record<string, any> = { app: args.app };
476
+ if (args.role) params.role = args.role;
477
+ if (args.title) params.title = args.title;
478
+ if (args.value) params.value = args.value;
479
+ if (args.identifier) params.identifier = args.identifier;
480
+ if (args.matchType) params.matchType = args.matchType;
481
+ if (args.maxDepth !== undefined) params.maxDepth = args.maxDepth;
482
+ if (args.maxCount !== undefined) params.maxCount = args.maxCount;
483
+
484
+ const result = execMacAgent("findElements", params);
485
+ if (!result.success) {
486
+ throw new Error("mac_find (search): " + result.error);
487
+ }
488
+
489
+ const data = result.data as { elements: any[]; totalVisited: number; truncated: boolean };
490
+ const elements = data.elements ?? [];
491
+
492
+ if (elements.length === 0) {
493
+ const criteria = [args.role, args.title, args.value, args.identifier].filter(Boolean).join(", ");
494
+ return {
495
+ content: [{ type: "text" as const, text: `No elements found matching: ${criteria || "(no criteria)"}` }],
496
+ details: result.data,
497
+ };
498
+ }
499
+
500
+ const lines = elements.map((el: any, i: number) => {
501
+ const parts = [`${i + 1}. ${el.role ?? "?"}`];
502
+ if (el.title) parts.push(`"${el.title}"`);
503
+ if (el.value !== undefined && el.value !== "") parts.push(`[${el.value}]`);
504
+ return parts.join(" ");
505
+ });
506
+ const truncNote = data.truncated ? `\n(truncated — search stopped at limit)` : "";
507
+ return {
508
+ content: [{ type: "text" as const, text: `${elements.length} element(s) found:\n${lines.join("\n")}${truncNote}` }],
509
+ details: result.data,
510
+ };
511
+ },
512
+ });
513
+
514
+ // -----------------------------------------------------------------
515
+ // mac_get_tree
516
+ // -----------------------------------------------------------------
517
+ pi.registerTool({
518
+ name: "mac_get_tree",
519
+ label: "Get UI Tree",
520
+ description:
521
+ "Get a compact accessibility tree of a macOS application's UI structure. " +
522
+ "Returns an indented tree showing role, title, and value of each element. " +
523
+ "Tighter defaults than mac_find's tree mode — designed for quick structure inspection. " +
524
+ "Each line: `role \"title\" [value]` with 2-space indent per depth level. " +
525
+ "Omits title/value when nil or empty.",
526
+ promptGuidelines: [
527
+ "Use for understanding app UI structure — start with low limits and increase if needed.",
528
+ "Prefer mac_find search mode when you know what you're looking for.",
529
+ "Check the truncation note to know if the tree was cut short.",
530
+ ],
531
+ parameters: Type.Object({
532
+ app: Type.String({ description: "Application name or bundle identifier" }),
533
+ maxDepth: Type.Optional(Type.Number({ description: "Maximum tree depth to traverse (default: 3)" })),
534
+ maxCount: Type.Optional(Type.Number({ description: "Maximum elements to include (default: 50)" })),
535
+ }),
536
+
537
+ async execute(_toolCallId: any, args: { app: string; maxDepth?: number; maxCount?: number }) {
538
+ const params: Record<string, any> = { app: args.app };
539
+ params.maxDepth = args.maxDepth ?? 3;
540
+ params.maxCount = args.maxCount ?? 50;
541
+
542
+ const result = execMacAgent("getTree", params);
543
+ if (!result.success) {
544
+ throw new Error("mac_get_tree: " + result.error);
545
+ }
546
+
547
+ const data = result.data as { tree: any[]; totalElements: number; truncated: boolean };
548
+ const lines: string[] = [];
549
+
550
+ function renderNode(nodes: any[], indent: number) {
551
+ for (const node of nodes) {
552
+ const parts = [node.role ?? "?"];
553
+ if (node.title) parts.push(`"${node.title}"`);
554
+ if (node.value !== undefined && node.value !== null && node.value !== "") parts.push(`[${node.value}]`);
555
+ lines.push(" ".repeat(indent) + parts.join(" "));
556
+ if (node.children?.length) {
557
+ renderNode(node.children, indent + 1);
558
+ }
559
+ }
560
+ }
561
+
562
+ renderNode(data.tree ?? [], 0);
563
+ if (data.truncated) {
564
+ lines.push(`\n(truncated — ${data.totalElements} elements visited, increase maxDepth or maxCount for more)`);
565
+ }
566
+ return {
567
+ content: [{ type: "text" as const, text: lines.join("\n") }],
568
+ details: { totalElements: data.totalElements, truncated: data.truncated },
569
+ };
570
+ },
571
+ });
572
+
573
+ // -----------------------------------------------------------------
574
+ // mac_click
575
+ // -----------------------------------------------------------------
576
+ pi.registerTool({
577
+ name: "mac_click",
578
+ label: "Click Element",
579
+ description:
580
+ "Click a UI element in a macOS application by performing AXPress. " +
581
+ "Finds the first element matching the given criteria (role, title, value, identifier) and clicks it. " +
582
+ "At least one criterion is required. Returns the clicked element's attributes.",
583
+ promptGuidelines: [
584
+ "Verify the click worked by reading the resulting state with mac_find or mac_read.",
585
+ "Use mac_find first to discover the right role/title/value criteria before clicking.",
586
+ ],
587
+ parameters: Type.Object({
588
+ app: Type.String({ description: "Application name or bundle identifier" }),
589
+ role: Type.Optional(Type.String({ description: "AX role (e.g. 'AXButton', 'AXMenuItem')" })),
590
+ title: Type.Optional(Type.String({ description: "AX title to match" })),
591
+ value: Type.Optional(Type.String({ description: "AX value to match" })),
592
+ identifier: Type.Optional(Type.String({ description: "AX identifier to match" })),
593
+ matchType: Type.Optional(Type.String({ description: "'exact' (default) or 'contains'" })),
594
+ }),
595
+
596
+ async execute(_toolCallId: any, args: {
597
+ app: string;
598
+ role?: string;
599
+ title?: string;
600
+ value?: string;
601
+ identifier?: string;
602
+ matchType?: string;
603
+ }) {
604
+ if (!args.role && !args.title && !args.value && !args.identifier) {
605
+ throw new Error("mac_click: provide at least one search criterion (role, title, value, or identifier)");
606
+ }
607
+ const params: Record<string, any> = { app: args.app };
608
+ if (args.role) params.role = args.role;
609
+ if (args.title) params.title = args.title;
610
+ if (args.value) params.value = args.value;
611
+ if (args.identifier) params.identifier = args.identifier;
612
+ if (args.matchType) params.matchType = args.matchType;
613
+
614
+ const result = execMacAgent("clickElement", params);
615
+ if (!result.success) {
616
+ throw new Error("mac_click: " + result.error);
617
+ }
618
+
619
+ const el = result.data?.element as Record<string, any> | undefined;
620
+ const parts = [el?.role ?? "element"];
621
+ if (el?.title) parts.push(`'${el.title}'`);
622
+ return {
623
+ content: [{ type: "text" as const, text: `Clicked ${parts.join(" ")}` }],
624
+ details: result.data,
625
+ };
626
+ },
627
+ });
628
+
629
+ // -----------------------------------------------------------------
630
+ // mac_type
631
+ // -----------------------------------------------------------------
632
+ pi.registerTool({
633
+ name: "mac_type",
634
+ label: "Type Text",
635
+ description:
636
+ "Type text into a UI element in a macOS application by setting its AXValue attribute. " +
637
+ "Finds the first element matching the given criteria and sets its value. " +
638
+ "Returns the actual value after setting (read-back verification). " +
639
+ "At least one criterion is required.",
640
+ promptGuidelines: [
641
+ "Read back the value after typing to verify — the return value includes actual content.",
642
+ "Target text fields/areas by role (AXTextArea, AXTextField) for reliability.",
643
+ ],
644
+ parameters: Type.Object({
645
+ app: Type.String({ description: "Application name or bundle identifier" }),
646
+ text: Type.String({ description: "Text to type into the element" }),
647
+ role: Type.Optional(Type.String({ description: "AX role (e.g. 'AXTextArea', 'AXTextField')" })),
648
+ title: Type.Optional(Type.String({ description: "AX title to match" })),
649
+ value: Type.Optional(Type.String({ description: "AX value to match" })),
650
+ identifier: Type.Optional(Type.String({ description: "AX identifier to match" })),
651
+ matchType: Type.Optional(Type.String({ description: "'exact' (default) or 'contains'" })),
652
+ }),
653
+
654
+ async execute(_toolCallId: any, args: {
655
+ app: string;
656
+ text: string;
657
+ role?: string;
658
+ title?: string;
659
+ value?: string;
660
+ identifier?: string;
661
+ matchType?: string;
662
+ }) {
663
+ if (!args.role && !args.title && !args.value && !args.identifier) {
664
+ throw new Error("mac_type: provide at least one search criterion (role, title, value, or identifier)");
665
+ }
666
+ const params: Record<string, any> = { app: args.app, text: args.text };
667
+ if (args.role) params.role = args.role;
668
+ if (args.title) params.title = args.title;
669
+ if (args.value) params.value = args.value;
670
+ if (args.identifier) params.identifier = args.identifier;
671
+ if (args.matchType) params.matchType = args.matchType;
672
+
673
+ const result = execMacAgent("typeText", params);
674
+ if (!result.success) {
675
+ throw new Error("mac_type: " + result.error);
676
+ }
677
+
678
+ const el = result.data?.element as Record<string, any> | undefined;
679
+ const actualValue = result.data?.value;
680
+ const parts = [el?.role ?? "element"];
681
+ if (el?.title) parts.push(`'${el.title}'`);
682
+ return {
683
+ content: [{ type: "text" as const, text: `Typed into ${parts.join(" ")} — value is now: ${actualValue}` }],
684
+ details: result.data,
685
+ };
686
+ },
687
+ });
688
+
689
+ // -----------------------------------------------------------------
690
+ // mac_screenshot
691
+ // -----------------------------------------------------------------
692
+ pi.registerTool({
693
+ name: "mac_screenshot",
694
+ label: "Screenshot Window",
695
+ description:
696
+ "Take a screenshot of a macOS application window by its window ID (from mac_list_windows). " +
697
+ "Returns the screenshot as an image content block for visual analysis, alongside text metadata " +
698
+ "(dimensions and format). Requires Screen Recording permission — use mac_check_permissions to verify.",
699
+ promptGuidelines: [
700
+ "Use for visual verification when accessibility attributes aren't sufficient.",
701
+ "Prefer nominal resolution unless retina detail is needed — retina doubles payload size.",
702
+ "Requires Screen Recording permission — run mac_check_permissions first if screenshot fails.",
703
+ ],
704
+ parameters: Type.Object({
705
+ windowId: Type.Number({ description: "Window ID from mac_list_windows output" }),
706
+ format: Type.Optional(StringEnum(["jpeg", "png"] as const, { description: "'jpeg' (default) or 'png'" })),
707
+ quality: Type.Optional(Type.Number({ description: "JPEG compression quality 0-1 (default: 0.8)" })),
708
+ retina: Type.Optional(Type.Boolean({ description: "Capture at full pixel resolution (default: false)" })),
709
+ }),
710
+
711
+ async execute(_toolCallId: any, args: { windowId: number; format?: string; quality?: number; retina?: boolean }) {
712
+ const params: Record<string, any> = { windowId: args.windowId };
713
+ if (args.format) params.format = args.format;
714
+ if (args.quality !== undefined) params.quality = args.quality;
715
+ if (args.retina !== undefined) params.retina = args.retina;
716
+
717
+ const result = execMacAgent("screenshotWindow", params);
718
+ if (!result.success) {
719
+ throw new Error("mac_screenshot: " + result.error);
720
+ }
721
+
722
+ const data = result.data!;
723
+ const imageData = data.imageData as string;
724
+ const format = data.format as string;
725
+ const width = data.width as number;
726
+ const height = data.height as number;
727
+ const mimeType = format === "png" ? "image/png" : "image/jpeg";
728
+
729
+ return {
730
+ content: [
731
+ { type: "text" as const, text: `Screenshot: ${width}x${height} ${format}` },
732
+ { type: "image" as const, data: imageData, mimeType },
733
+ ],
734
+ details: { width, height, format, mimeType },
735
+ };
736
+ },
737
+ });
738
+
739
+ // -----------------------------------------------------------------
740
+ // mac_read
741
+ // -----------------------------------------------------------------
742
+ pi.registerTool({
743
+ name: "mac_read",
744
+ label: "Read Attribute",
745
+ description:
746
+ "Read one or more accessibility attributes from a UI element in a macOS application. " +
747
+ "Finds the first element matching the given criteria and reads the named attribute(s). " +
748
+ "AXValue subtypes (CGPoint, CGSize, CGRect, CFRange) are automatically unpacked to structured dicts. " +
749
+ "Use 'attribute' for a single attribute or 'attributes' for multiple. At least one search criterion is required.",
750
+ promptGuidelines: [
751
+ "Use to verify state after actions — read AXValue to confirm text was typed, AXEnabled to check if a button is active.",
752
+ ],
753
+ parameters: Type.Object({
754
+ app: Type.String({ description: "Application name or bundle identifier" }),
755
+ attribute: Type.Optional(Type.String({ description: "Single attribute name to read (e.g. 'AXValue', 'AXPosition', 'AXRole')" })),
756
+ attributes: Type.Optional(Type.Array(Type.String(), { description: "Multiple attribute names to read" })),
757
+ role: Type.Optional(Type.String({ description: "AX role (e.g. 'AXButton', 'AXTextArea')" })),
758
+ title: Type.Optional(Type.String({ description: "AX title to match" })),
759
+ value: Type.Optional(Type.String({ description: "AX value to match" })),
760
+ identifier: Type.Optional(Type.String({ description: "AX identifier to match" })),
761
+ matchType: Type.Optional(Type.String({ description: "'exact' (default) or 'contains'" })),
762
+ }),
763
+
764
+ async execute(_toolCallId: any, args: {
765
+ app: string;
766
+ attribute?: string;
767
+ attributes?: string[];
768
+ role?: string;
769
+ title?: string;
770
+ value?: string;
771
+ identifier?: string;
772
+ matchType?: string;
773
+ }) {
774
+ if (!args.attribute && (!args.attributes || args.attributes.length === 0)) {
775
+ throw new Error("mac_read: provide 'attribute' (single) or 'attributes' (array) parameter");
776
+ }
777
+ if (!args.role && !args.title && !args.value && !args.identifier) {
778
+ throw new Error("mac_read: provide at least one search criterion (role, title, value, or identifier)");
779
+ }
780
+ const params: Record<string, any> = { app: args.app };
781
+ if (args.attribute) params.attribute = args.attribute;
782
+ if (args.attributes) params.attributes = args.attributes;
783
+ if (args.role) params.role = args.role;
784
+ if (args.title) params.title = args.title;
785
+ if (args.value) params.value = args.value;
786
+ if (args.identifier) params.identifier = args.identifier;
787
+ if (args.matchType) params.matchType = args.matchType;
788
+
789
+ const result = execMacAgent("readAttribute", params);
790
+ if (!result.success) {
791
+ throw new Error("mac_read: " + result.error);
792
+ }
793
+
794
+ // Format output based on single vs multi attribute
795
+ if (args.attribute && !args.attributes) {
796
+ const val = result.data?.value;
797
+ const formatted = typeof val === "object" ? JSON.stringify(val) : String(val);
798
+ return {
799
+ content: [{ type: "text" as const, text: `${args.attribute}: ${formatted}` }],
800
+ details: result.data,
801
+ };
802
+ }
803
+
804
+ // Multi-attribute: format as key: value lines
805
+ const values = result.data?.values as Record<string, any> | undefined;
806
+ if (values) {
807
+ const lines = Object.entries(values).map(([k, v]) => {
808
+ const formatted = typeof v === "object" ? JSON.stringify(v) : String(v);
809
+ return `${k}: ${formatted}`;
810
+ });
811
+ return {
812
+ content: [{ type: "text" as const, text: lines.join("\n") }],
813
+ details: result.data,
814
+ };
815
+ }
816
+
817
+ // Fallback
818
+ return {
819
+ content: [{ type: "text" as const, text: JSON.stringify(result.data) }],
820
+ details: result.data,
821
+ };
822
+ },
823
+ });
824
+
825
+ // -----------------------------------------------------------------
826
+ // System prompt injection — mac-tools usage guidelines
827
+ // -----------------------------------------------------------------
828
+ pi.on("before_agent_start", async (event) => {
829
+ const guidelines = `
830
+
831
+ [SYSTEM CONTEXT — Mac Tools]
832
+
833
+ ## Native macOS App Interaction
834
+
835
+ You have mac-tools for controlling native macOS applications (Finder, TextEdit, Safari, Xcode, etc.) via Accessibility APIs.
836
+
837
+ **Mac-tools vs browser-tools:** Use mac-tools for native macOS apps. Use browser-tools for web pages inside a browser. If you need to interact with a website in Safari or Chrome, use browser-tools — mac-tools controls the browser's native UI chrome (menus, tabs, address bar), not web page content.
838
+
839
+ **Permissions:** If any mac tool returns a permission error, run \`mac_check_permissions\` to diagnose. Accessibility and Screen Recording permissions are granted in System Settings > Privacy & Security.
840
+
841
+ **Interaction pattern — discover → act → verify:**
842
+ 1. **Discover** the UI structure with \`mac_find\` (search for specific elements) or \`mac_get_tree\` (see overall layout)
843
+ 2. **Act** with \`mac_click\` (press buttons/menus) or \`mac_type\` (enter text into fields)
844
+ 3. **Verify** the result with \`mac_read\` (check attribute values) or \`mac_screenshot\` (visual confirmation)
845
+
846
+ **Tree queries:** Start with default limits (mac_get_tree: maxDepth:3, maxCount:50). Increase only if the element you need isn't visible in the output. Large trees waste context.
847
+
848
+ **Screenshots:** Use \`mac_screenshot\` only when visual verification is genuinely needed — the image payload is large. Prefer \`mac_read\` or \`mac_find\` for checking text values and element state.`;
849
+
850
+ return { systemPrompt: event.systemPrompt + guidelines };
851
+ });
852
+ }