Open source CapCut alternative.
TypeScript
feat(skills): rewrite the music skill around catalog resolution and verified playback
David Le8e84281bb0db
diff --git a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.mdindex 1d433945..9afbbfe3 100644--- a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md+++ b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md@@ -1,37 +1,75 @@# Musicid: music-description: Play music locally in Apple Music — never a web player — using the validated playback script.+description: Play music locally in Apple Music — never a web player — using the validated playback script first, then UI fallback only when needed.tags: media, music, playback, local-app-keywords: play, listen, music, song, track, tune, album, playlist, artist, band, audio, podcast, radio, pause, resume, volume+keywords: play, listen, music, song, track, tune, album, playlist, artist, band, audio, radio, pause, resume, skip, volumeapps: Music, Apple Music, com.apple.Music-tools: shell_exec, skill_run+tools: skill_run, shell_exec, vision.capture, vision.click, web.search-Use this for "play …" / "listen to …" requests. Playing music is low-risk and reversible — act directly, don't ask the user to confirm. Always play **locally in the Music app**; never open YouTube Music, Spotify web, or any web player.+Use this for any music playback request: "play baby don't cry", "play top 80s hits", "play the latest album from Taylor Swift", "play something by Coldplay", "play the song from Titanic", "play relaxing piano music", "pause the music", "turn it up", "what's playing?".-## Play with the validated script (preferred)+Playback is low-risk and reversible: act directly, don't ask for confirmation. Always play locally in the Music app; never open YouTube, Spotify web, Apple Music web, or any browser player. Convert the request into the best Apple Music search query, then play it. Prefer the most likely mainstream interpretation. Do not over-explain — the user wants the music to start.-This skill ships a validated script — `skillID=music`, `scriptID=scripts-play-media-by-search` — that searches the Music app for a query and starts playback in one step (it tries the user's local library first, then the Apple Music streaming catalog). Run it with `skill_run`, passing the query as the input.+## Step 1: Classify the playback intent-Choosing the query is what matters:+1. play_track — a specific song or likely song.+2. play_album — an album.+3. play_artist — an artist, or "something by" an artist.+4. play_playlist_or_station — hits, genre, mood, era, playlist, radio, "music like X".+5. transport_control — pause, resume, skip, previous, stop.+6. volume_control — louder, quieter, set volume.+7. now_playing — what is currently playing.-- **Vague artist / mood / genre request** ("play some coldplay", "play coldplay"): pass the **artist or seed word alone** as the query — e.g. `Coldplay`. A broad term matches whatever the user owns, so playback starts from their library. Do NOT over-specify with a particular song title the user didn't name; a title they don't own (and the word "by") makes the local match miss.-- **A specific named song/album**: pass the plain `Title Artist` (e.g. `Yellow Coldplay`) — space-separated, no "by".+For transport_control, volume_control, and now_playing use direct AppleScript (commands below) — never run a search for these. For all play intents run the validated script first, unless the request needs external resolution.-Read the script's returned status:-- `status=played` → playback started; tell the user what's playing (`playedTitle`/`playedArtist`).-- `status=not_found` → do NOT give up or ask yet. The script already searched, so Apple Music is showing results for the query on screen — finish the job: call `vision_control` with app "Music" and a goal like "play the top song result for <query>", and the vision agent will click a song. This is the feedback loop: an approach failed, so adjust and try another, don't stop.+## Step 2: Resolve the query when needed-Only ask the user when you have genuinely exhausted the local play, the script, AND vision — and even then, play a reasonable default by the same artist rather than asking which exact song.+Most requests pass straight to the script. Use web.search (or llm.generate for pure music knowledge) ONLY when information is missing or indirect: "latest album from X", "new song by X", "song from <movie/show/game>", "song that goes <lyrics>", "the viral song that says …", "top song by X", "the original version of …". Prefer reliable music sources (artist discographies, Wikipedia, Genius, MusicBrainz). Convert the result into a compact Apple Music query, include the artist when it reduces ambiguity, and don't mention the search step unless playback fails.-Verify with the script's own status fields or `osascript -e 'tell application "Music" to player state'`; don't use screenshots.+Examples: "play the latest album from Taylor Swift" → resolve the current latest album → `<that album> Taylor Swift`. "play the song from Titanic" → `My Heart Will Go On Celine Dion`. "the song that goes never gonna give you up" → `Never Gonna Give You Up Rick Astley`.-## Direct AppleScript (when you don't run the script)+If resolution is uncertain, still make the best reasonable attempt.-- Play whatever Coldplay is owned: `osascript -e 'tell application "Music" to play (item 1 of (search library playlist 1 for "Coldplay"))'`.-- Transport: `osascript -e 'tell application "Music" to playpause'`, `… to pause`, `… to set sound volume to 60`.-- What's playing: `osascript -e 'tell application "Music" to (get name of current track) & " — " & (get artist of current track)'`.+## Step 3: Run the validated script-## GUI fallback+`skill_run` with `skillID=music`, `scriptID=scripts-play-media-by-search`, input = the normalized query in plain words. The script plays from the user's library when they own a match (instant); otherwise it resolves the most popular catalog match for the query, deep-links straight to that track in Music, starts playback, and verifies playback really started before claiming success.-Only if scripting can't play it: focus Music, Cmd+F to search, type the query, press Return to submit and again to play the top result, then confirm `player state` is playing.+Good inputs: `baby don't cry` · `my heart will go on celine dion` · `coldplay` · `top 80s hits` · `abbey road beatles`.+Bad inputs: `play baby don't cry` · `listen to coldplay` — no command phrases, and no "by" unless it is part of the actual title.++## Step 4: Read the script status (the source of truth)++- `status=played` → playback started and was verified. Tell the user briefly what is playing, using `playedTitle`/`playedArtist`.+- `status=not_found` → read the script's `hint=` field; it states what happened and what to do next. Do NOT re-run the same query, and do not focus/open/activate Music — the script already left the right page on screen. If the hint suggests a visual click, do `vision.capture` then `vision.click` on the named song row, then verify playback. If it still won't start, say the likely reason plainly instead of retrying.+- Script error → don't loop. Try one improved query only if the error clearly indicates the query was malformed or too ambiguous; otherwise explain the failure briefly.++## Step 5: Verify playback++Prefer the script's own verification. Manual check: `osascript -e 'tell application "Music" to player position'` — the position must ADVANCE over time. Never trust `player state` alone: Music can report "playing" while no real track is loaded. Never verify with screenshots.++## Direct AppleScript (transport / volume / now playing)++- Pause: `osascript -e 'tell application "Music" to pause'` · resume: `… to play` · toggle: `… to playpause`+- Next: `… to next track` · previous: `… to previous track`+- Volume: `… to set sound volume to 60`+- Now playing: `… to (get name of current track) & " — " & (get artist of current track)`+- Library-only play: `… to play (item 1 of (search library playlist 1 for "Coldplay"))`++AppleScript library search only sees music the user owns — it cannot search or play the streaming catalog. If a library search comes back empty, do not keep scripting; run the validated script. Avoid hand-building shell commands around titles with apostrophes or quotes — the script takes raw titles safely.++## UI fallback in Music++Music's catalog UI is mostly NOT in the Accessibility tree: search-result tiles and album track rows don't appear in `ax.observe`, and `ax.click` fails there with "cannot be clicked" — do not retry AX clicks on catalog rows. Use `vision.capture` + `vision.click` instead, and only after the script's hint says the right page is on screen: capture → click the best visible song row or play button → verify player position advances → report. Perform one visual-click fallback only, unless the first click clearly selected the wrong element.++## Ambiguity++Pick the best likely match for ordinary ambiguity: "play hello" / "play baby don't cry" → the most popular likely match; "play drake" → owned Drake music or a popular catalog result; "play 80s music" → a popular 80s playlist/station. Ask only when the user explicitly wants to choose between versions, when interpretations are equally likely AND very different, or when the request isn't actually about music. Keep it short: "Which one did you mean: Adele, Lionel Richie, or another artist?"++## Failure behavior++Never fail silently; never say playback started unless it was verified; never retry the same query repeatedly; never open a web player; never claim catalog playback is possible through direct AppleScript. State the likely reason plainly: Apple Music streaming not active on this Mac, item unavailable, Music exposed no clickable result, the query couldn't be resolved, or no library match and catalog playback failed. Example: "I found the result in Music, but playback did not start. This usually means Apple Music streaming is not active on this Mac or the item is unavailable."++## Response style++Success: "Playing **{title}** by **{artist}**." Album: "Playing **{album}** by **{artist}**." Playlist/station: "Playing **{playlist/station}**." Transport/volume: "Paused." / "Resumed." / "Volume set to 60%." Keep responses short — the action matters more than the explanation.diff --git a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescriptindex 5b825f11..6841984b 100644--- a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript+++ b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript@@ -5,12 +5,10 @@ if trimmedQuery is "" thenreturn "status=not_found" & linefeed & "clarification.required=true" & linefeed & "clarification.question=What would you like me to play?"end if+-- 1) Local library first: instant, Automation-only, and a broad term (artist, mood word) plays+-- whatever the user owns. `search` matches the query across title/artist/album.tell application id "com.apple.Music"activate- -- Prefer the Music dictionary directly (Automation only, no UI keystrokes) so playback of music- -- the user OWNS is reliable and does not depend on Accessibility. `search` matches the query- -- across title/artist/album. This only covers the local library, so if it finds nothing we fall- -- back to the full-app search below, which reaches the Apple Music streaming catalog.set matches to (search library playlist 1 for trimmedQuery)if (count of matches) is not 0 thenset chosen to item 1 of matches@@ -20,46 +18,67 @@ tell application id "com.apple.Music"end ifend tell--- Streaming fallback: the dictionary `search` only sees the local library, so for tracks the user--- does not own we drive the Music app's own search field, which surfaces Apple Music streaming--- results. The first Return submits the search; the second activates the top playable result.-delay 0.3+-- 2) Not owned: resolve the MOST POPULAR catalog match via the iTunes Search API — one curl,+-- no UI, popularity-ranked, and it gives back the exact track/album link. This replaces the+-- old type-into-the-search-field dance, which could surface results but never start playback.+set resolvedTitle to ""+set resolvedArtist to ""+set resolvedURL to ""+try+ -- limit=5, first result taken: asking the API for limit=1 invokes a different (worse)+ -- ranking tier, e.g. returning a cover instead of the popular original.+ set apiJSON to do shell script "curl -s -m 4 -G 'https://itunes.apple.com/search' --data-urlencode 'entity=song' --data-urlencode 'limit=5' --data-urlencode " & quoted form of ("term=" & trimmedQuery)+ set firstResult to do shell script "printf '%s' " & quoted form of apiJSON & " | awk -F'\"wrapperType\"' '{print $2}'"+ set resolvedTitle to do shell script "printf '%s' " & quoted form of firstResult & " | sed -nE 's/.*\"trackName\":\"([^\"]+)\".*/\\1/p' | head -1"+ set resolvedArtist to do shell script "printf '%s' " & quoted form of firstResult & " | sed -nE 's/.*\"artistName\":\"([^\"]+)\".*/\\1/p' | head -1"+ set resolvedURL to do shell script "printf '%s' " & quoted form of firstResult & " | sed -nE 's/.*\"trackViewUrl\":\"([^\"]+)\".*/\\1/p' | head -1"+end try-tell application "System Events"- if not (exists process "Music") then- return "status=failed" & linefeed & "failureReason=music_process_unavailable"- end if+if resolvedURL does not start with "https://music.apple.com/" then set resolvedURL to ""- tell process "Music"- set frontmost to true+if resolvedURL is not "" then+ -- music:// routes the link into the Music app natively (https:// would not navigate it)+ -- and lands on the album page with the requested track highlighted.+ set deepLink to "music" & (text 6 thru -1 of resolvedURL)+ do shell script "open " & quoted form of deepLink+ delay 1.5+ tell application id "com.apple.Music"+ play+ -- Verify with the player position AND the track name, not `player state`: state can+ -- report "playing" while nothing is loaded (position pinned at 0, e.g. with no Apple+ -- Music subscription), and a bare `play` can resume a previously loaded track instead+ -- of the requested one.+ set verified to false+ repeat 5 times+ delay 0.7+ try+ if player state is playing and player position > 0.3 then+ set nowName to ""+ try+ set nowName to name of current track+ end try+ if nowName is not "" and (nowName contains resolvedTitle or resolvedTitle contains nowName) then+ set verified to true+ exit repeat+ end if+ end if+ end try+ end repeat+ if verified then+ set playedTitle to resolvedTitle+ set playedArtist to resolvedArtist+ try+ set playedTitle to name of current track+ set playedArtist to artist of current track+ end try+ return "status=played" & linefeed & "source=streaming" & linefeed & "query=" & trimmedQuery & linefeed & "playedTitle=" & playedTitle & linefeed & "playedArtist=" & playedArtist+ end ifend tell+ -- The exact track's album page is on screen but playback would not start from script-level+ -- `play`. A real click on the song row (the vision agent) is the remaining move; if even that+ -- does not play, it's a Music account/subscription problem the user must fix — say so.+ return "status=not_found" & linefeed & "query=" & trimmedQuery & linefeed & "resolvedTitle=" & resolvedTitle & linefeed & "resolvedArtist=" & resolvedArtist & linefeed & "hint=The track's album page is open in Music but scripted playback did not start. Vision-click the song row to play it. If playback still does not start, ask the user to check Music's sign-in / Apple Music subscription — do not keep retrying." & linefeed & "escalate.app=Music" & linefeed & "escalate.goal=Double-click the song row " & resolvedTitle & " on the album page now on screen, then verify player position advances"+end if- keystroke "f" using command down- delay 0.2- keystroke trimmedQuery- delay 0.2- key code 36- delay 1.0- key code 36-end tell---- Verify something actually started playing before claiming success; the keystroke path can't tell--- on its own whether a result was selected, so check the Music app's real player state.-delay 0.8-tell application id "com.apple.Music"- if player state is playing then- set playedTitle to trimmedQuery- set playedArtist to ""- try- set playedTitle to name of current track- set playedArtist to artist of current track- end try- return "status=played" & linefeed & "source=streaming" & linefeed & "query=" & trimmedQuery & linefeed & "playedTitle=" & playedTitle & linefeed & "playedArtist=" & playedArtist- end if-end tell---- The library had no match and the streaming keystrokes didn't confirm playback, but the search--- results ARE on screen now. Don't dead-end: signal the runtime to escalate to the vision agent,--- which sees the results and clicks a song. This is the structural feedback loop — the system--- adjusts, it doesn't give up.-return "status=not_found" & linefeed & "query=" & trimmedQuery & linefeed & "escalate.app=Music" & linefeed & "escalate.goal=Play the top song result for " & trimmedQuery & " from the search results on screen"+-- 3) Nothing owned and the catalog lookup failed (offline or no API match): report honestly.+return "status=not_found" & linefeed & "query=" & trimmedQuery & linefeed & "hint=No local library match and the catalog lookup returned nothing. Ask the user or try a different query wording." & linefeed & "escalate.app=Music" & linefeed & "escalate.goal=Search Music for " & trimmedQuery & " and play the top song result"