Skip to content

Open source CapCut alternative.

TypeScript

feat(app): add app_commands command exposing an app's scripting dictionary digest

David Lee35939785950
diff --git a/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift b/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
index e43106b4..5aee6489 100644
--- a/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/DonkeyCommandLayer.swift
@@ -19,6 +19,7 @@ public enum DonkeyCommandLayer {
case shellExec = "shell_exec"
case appsList = "apps_list"
case appSkill = "app_skill"
+ case appCommands = "app_commands"
case skillRun = "skill_run"
}
@@ -79,6 +80,23 @@ public enum DonkeyCommandLayer {
requiredPermissions: [.skillLookup],
safetyClass: .readOnly
),
+ HarnessToolDescriptor(
+ name: Command.appCommands.rawValue,
+ pluginID: pluginID,
+ summary: "Read an app's real AppleScript vocabulary — the commands, parameters, classes, and enumerations its scripting dictionary actually declares — before generating any AppleScript for it. Call this for any scriptable app you have no loaded skill for; scripts written from this digest use terminology the app is guaranteed to understand. When the digest marks a suite as condensed or truncated, call again with `suite` set to that suite's name for full detail. If `scriptable` comes back false, do not generate AppleScript; drive the app with accessibility/vision tools instead.",
+ inputSchema: [
+ "app": "The app's display name or bundle identifier.",
+ "suite": "Optional: a suite name from a previous result, to get that suite at full detail."
+ ],
+ optionalInputKeys: ["suite"],
+ outputSchema: [
+ "scriptable": "\"true\", \"false\", or \"unknown\" — whether the app can be driven via AppleScript.",
+ "digest": "The bounded scripting-dictionary digest (commands with parameter names/types/optionality, classes, enumerations).",
+ "suites": "Comma-separated suite names available for the `suite` drill-down."
+ ],
+ requiredPermissions: [.appLookup],
+ safetyClass: .readOnly
+ ),
HarnessToolDescriptor(
name: Command.skillRun.rawValue,
pluginID: pluginID,
diff --git a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
index b4bdc995..47a89b73 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
@@ -29,11 +29,70 @@ public enum DonkeyCommandBackends {
return listApps(context)
case .appSkill:
return appSkill(context)
+ case .appCommands:
+ return await appCommands(context)
case .skillRun:
return await runSkillScript(context)
}
}
+ // MARK: - app_commands
+
+ /// Surface the app's REAL AppleScript vocabulary from its parsed scripting dictionary, so the
+ /// model writes scripts against declared terminology instead of guessing it. Non-scriptable
+ /// apps answer deterministically with the accessibility/vision redirection.
+ private static func appCommands(_ context: HarnessToolExecutionContext) async -> HarnessToolResult {
+ guard let app = trimmed(context.call.input["app"]) else {
+ return invalidInput(context, "app_commands requires an `app` display name or bundle identifier.")
+ }
+ let service = AppScriptingDictionaryService.shared
+ let lookup = await service.lookup(appName: app, bundleIdentifier: app)
+
+ guard lookup.scriptability != .notScriptable else {
+ return success(
+ context,
+ summary: "\(app) is not AppleScript-scriptable. Do not generate AppleScript for it; drive it with accessibility/vision tools instead.",
+ facts: ["appCommands.\(app)": "notScriptable"],
+ metadata: ["scriptable": "false", "app": app]
+ )
+ }
+ guard let dictionary = lookup.dictionary else {
+ let scriptable = lookup.scriptability == .scriptable ? "true" : "unknown"
+ return success(
+ context,
+ summary: "No scripting dictionary could be read for \(app). Prefer accessibility/vision tools, or shell_exec for system-level actions.",
+ facts: ["appCommands.\(app)": "noDictionary"],
+ metadata: ["scriptable": scriptable, "app": app]
+ )
+ }
+
+ let suiteNames = dictionary.suites.map(\.name).joined(separator: ", ")
+ if let suiteName = trimmed(context.call.input["suite"]) {
+ guard let suiteDigest = await service.suiteDigest(
+ appName: app, bundleIdentifier: app, suiteName: suiteName
+ ) else {
+ return failed(
+ context,
+ "\(app) has no suite named \"\(suiteName)\". Available suites: \(suiteNames).",
+ reason: "suiteNotFound"
+ )
+ }
+ return success(
+ context,
+ summary: suiteDigest,
+ facts: ["appCommands.\(app)": "loaded"],
+ metadata: ["scriptable": "true", "app": app, "digest": suiteDigest, "suites": suiteNames]
+ )
+ }
+
+ return success(
+ context,
+ summary: lookup.digest,
+ facts: ["appCommands.\(app)": "loaded"],
+ metadata: ["scriptable": "true", "app": app, "digest": lookup.digest, "suites": suiteNames]
+ )
+ }
+
// MARK: - app_skill
/// Surface the installed operating playbook for an app, discovered from the
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/AppCommandsCommandTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/AppCommandsCommandTests.swift
new file mode 100644
index 00000000..eba33bd5
--- /dev/null
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/AppCommandsCommandTests.swift
@@ -0,0 +1,69 @@
+import DonkeyContracts
+import DonkeyHarness
+import DonkeyRuntime
+import Foundation
+import Testing
+
+@Suite
+struct AppCommandsCommandTests {
+ @MainActor
+ private func run(_ input: [String: String]) async -> HarnessToolResult? {
+ let descriptor = DonkeyCommandLayer.descriptors.first { $0.name == "app_commands" }!
+ let context = HarnessToolExecutionContext(
+ taskID: "test",
+ call: HarnessToolCall(name: "app_commands", input: input),
+ descriptor: descriptor,
+ worldModel: HarnessWorldModel(),
+ grantedPermissions: [.appLookup]
+ )
+ return await DonkeyCommandBackends.makeExecutor()(context)
+ }
+
+ @Test
+ @MainActor
+ func requiresAnAppInput() async {
+ let result = await run([:])
+ #expect(result?.status == .invalidInput)
+ }
+
+ @Test
+ @MainActor
+ func unknownAppReportsNoDictionaryWithoutFailing() async {
+ let result = await run(["app": "Definitely Not An Installed App 9000"])
+ #expect(result?.status == .succeeded)
+ #expect(result?.metadata["scriptable"] != "true")
+ #expect(result?.metadata["digest"] == nil)
+ }
+
+ @Test
+ @MainActor
+ func readsARealDictionaryWithSuiteNamesAndTerminology() async {
+ guard FileManager.default.fileExists(atPath: "/System/Applications/Notes.app") else { return }
+ let result = await run(["app": "com.apple.Notes"])
+ #expect(result?.status == .succeeded)
+ #expect(result?.metadata["scriptable"] == "true")
+ let digest = result?.metadata["digest"] ?? ""
+ #expect(digest.contains("scripting dictionary"))
+ #expect(digest.contains("Notes Suite"))
+ let suites = result?.metadata["suites"] ?? ""
+ #expect(suites.contains("Notes Suite"))
+ // The digest IS the planner-facing summary.
+ #expect(result?.summary == digest)
+ }
+
+ @Test
+ @MainActor
+ func drillsIntoOneSuiteAtFullDetail() async {
+ guard FileManager.default.fileExists(atPath: "/System/Applications/Notes.app") else { return }
+ let result = await run(["app": "Notes", "suite": "notes suite"])
+ #expect(result?.status == .succeeded)
+ let digest = result?.metadata["digest"] ?? ""
+ #expect(digest.contains("Notes Suite"))
+ #expect(!digest.contains("Standard Suite"))
+
+ let missing = await run(["app": "Notes", "suite": "No Such Suite"])
+ #expect(missing?.status == .failed)
+ #expect(missing?.metadata["reason"] == "suiteNotFound")
+ #expect(missing?.summary.contains("Notes Suite") == true)
+ }
+}