feat(mobile): add Siri and Shortcuts music intents

- Add Spotlight entity discovery and reliable intent execution
- Sync full music catalogs and make playlist creation idempotent
This commit is contained in:
joshuafhiggins committed 2026-10-22 19:31:25 -04:00 Verified
1 parent 92ea8fc04f
commit c7f354cd9f
26 files changed
+1253 -458

No files matched your search

+17 -3
View File
@@ -1,9 +1,9 @@
import Foundation
import AppIntents
internal import ExpoAppIntents
internal import ExpoModulesCore
/// Other files in this directory use a `.intent.swift` suffix. Expo's inline-module
/// scan treats every plain `.swift` basename as a Module subclass, so only this file
/// is registered. The rest are still compiled into the app target.
/// Registers the app-owned intent types and their system integrations.
enum MusicAppIntents {
static func register() {
Task {
@@ -21,8 +21,22 @@ final class AppIntentsSetup: Module {
public func definition() -> ExpoModulesCore.ModuleDefinition {
Name("AppIntentsSetup")
AsyncFunction("replaceCatalog") { (kind: String, json: String) in
try await MusicCatalogStorage.shared.replace(kind: kind, json: json)
}
AsyncFunction("catalogJSON") { (kind: String) in
let records = try await MusicCatalogStorage.shared.records(kind: kind)
return String(decoding: try JSONEncoder().encode(records), as: UTF8.self)
}
AsyncFunction("completeInvocation") { (token: String, error: String?) in
await MusicIntentExecution.shared.complete(token: token, error: error)
}
OnCreate {
MusicAppIntents.register()
MusicEntityRegistration.register()
}
}
}
@@ -507,7 +507,7 @@ private struct AudioLibraryContext {
@available(iOS 27.0, *)
@AppIntent(schema: .audio.playAudio)
struct PlayAudioIntent: AudioStartingIntent {
static let openAppWhenRun = false
static let openAppWhenRun = true
var audioEntity: AudioItem
var playbackAttributes: Set<PlaybackAttributes>
@@ -525,7 +525,7 @@ struct PlayAudioIntent: AudioStartingIntent {
case nil:
queue = "now"
}
await MusicCommands.play(
try await MusicCommands.play(
kind: audioEntity.kind,
id: audioEntity.itemId,
title: audioEntity.itemTitle,
@@ -541,14 +541,14 @@ struct PlayAudioIntent: AudioStartingIntent {
@available(iOS 27.0, *)
@AppIntent(schema: .audio.warmupAudioQueue)
struct WarmupAudioQueueIntent {
static let openAppWhenRun = false
static let openAppWhenRun = true
var audioEntity: AudioItem
var playbackAttributes: Set<PlaybackAttributes>
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<WarmupAudioQueueResultEntity> {
let id = await MusicCommands.warmup(
let id = try await MusicCommands.warmup(
kind: audioEntity.kind,
id: audioEntity.itemId,
title: audioEntity.itemTitle,
@@ -563,7 +563,7 @@ struct WarmupAudioQueueIntent {
@available(iOS 27.0, *)
@AppIntent(schema: .audio.addToPlaylist)
struct AddAudioToPlaylistIntent {
static let openAppWhenRun = false
static let openAppWhenRun = true
var audioEntity: AudioItem
var playlist: AudioPlaylistEntity
@@ -573,7 +573,7 @@ struct AddAudioToPlaylistIntent {
if audioEntity.kind == "playlist" && audioEntity.itemId == playlist.id {
throw MusicIntentFailure("Choose a different playlist.")
}
await MusicCommands.addToPlaylist(
try await MusicCommands.addToPlaylist(
playlistId: playlist.id,
sources: [(kind: audioEntity.kind, id: audioEntity.itemId)]
)
@@ -584,7 +584,7 @@ struct AddAudioToPlaylistIntent {
@available(iOS 27.0, *)
@AppIntent(schema: .audio.updateAudioAffinity)
struct UpdateAudioAffinityIntent {
static let openAppWhenRun = false
static let openAppWhenRun = true
var affinityState: AffinityState
var target: AudioItem
@@ -598,7 +598,7 @@ struct UpdateAudioAffinityIntent {
throw MusicIntentFailure("oTunes can love songs, albums, and artists.")
}
let state = affinityState == .like ? "like" : "unset"
await MusicCommands.updateAffinity(kind: target.kind, id: target.itemId, state: state)
try await MusicCommands.updateAffinity(kind: target.kind, id: target.itemId, state: state)
let verb = affinityState == .like ? "Loving" : "Unliking"
return .result(dialog: "\(verb) \(target.itemTitle).")
}
@@ -607,7 +607,7 @@ struct UpdateAudioAffinityIntent {
@available(iOS 27.0, *)
@AppIntent(schema: .audio.createStation)
struct CreateAudioStationIntent {
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
@@ -617,7 +617,7 @@ struct CreateAudioStationIntent {
if song.artistId.isEmpty {
throw MusicIntentFailure("oTunes can't find an artist for this song.")
}
await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
try await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
return .result(dialog: "Playing more by \(artist).")
}
@@ -17,7 +17,7 @@ enum MusicLinkTarget {
case playlist(String)
}
/// Reads the catalogs JavaScript publishes with `setEntityCatalogAsync` and turns
/// Reads the full disk-backed catalogs JavaScript publishes and turns
/// spoken requests into those records. The same kinds back Shortcuts pickers and,
/// on iOS 27, the audio schemas Siri uses for Apple Music-style requests.
enum MusicCatalog {
@@ -30,11 +30,11 @@ enum MusicCatalog {
static let favoritesPlaylistId = "otunes:favorites"
static func records(ofKind kind: String) async throws -> [AppIntentEntityRecord] {
try await AppIntentEntityStore.shared.entities(ofKind: kind)
try await MusicCatalogStorage.shared.records(kind: kind)
}
static func records(ofKind kind: String, matching identifiers: [String]) async throws -> [AppIntentEntityRecord] {
try await AppIntentEntityStore.shared.entities(ofKind: kind, matching: identifiers)
try await records(ofKind: kind).filter { identifiers.contains($0.id) }
}
static func currentSong() async throws -> SongEntity? {
@@ -225,7 +225,7 @@ enum MusicCommands {
repeatMode: String?,
queue: String,
warmupId: String?
) async -> String {
) async throws -> String {
let params: AppIntentParams = [
"kind": .string(kind),
"id": .string(id),
@@ -235,11 +235,11 @@ enum MusicCommands {
"repeat": repeatMode.map(AppIntentValue.string) ?? .null,
"warmupId": warmupId.map(AppIntentValue.string) ?? .null,
]
return await AppIntentDispatcher.shared.dispatch(name: "playMusic", params: params)
return try await MusicIntentExecution.shared.run(name: "playMusic", params: params)
}
static func warmup(kind: String, id: String, title: String, shuffle: Bool) async -> String {
await AppIntentDispatcher.shared.dispatch(
static func warmup(kind: String, id: String, title: String, shuffle: Bool) async throws -> String {
try await MusicIntentExecution.shared.run(
name: "warmupAudio",
params: [
"kind": .string(kind),
@@ -250,8 +250,8 @@ enum MusicCommands {
)
}
static func addToPlaylist(playlistId: String, sources: [(kind: String, id: String)]) async {
await AppIntentDispatcher.shared.dispatch(
static func addToPlaylist(playlistId: String, sources: [(kind: String, id: String)]) async throws {
try await MusicIntentExecution.shared.run(
name: "addToPlaylist",
params: [
"playlistId": .string(playlistId),
@@ -268,8 +268,8 @@ enum MusicCommands {
static func addToUpNext(
position: String,
sources: [(kind: String, id: String)]
) async {
await AppIntentDispatcher.shared.dispatch(
) async throws {
try await MusicIntentExecution.shared.run(
name: "addToUpNext",
params: [
"position": .string(position),
@@ -283,8 +283,8 @@ enum MusicCommands {
)
}
static func createPlaylist(name: String, sources: [(kind: String, id: String)]) async {
await AppIntentDispatcher.shared.dispatch(
static func createPlaylist(name: String, sources: [(kind: String, id: String)]) async throws {
try await MusicIntentExecution.shared.run(
name: "createPlaylist",
params: [
"name": .string(name),
@@ -298,12 +298,12 @@ enum MusicCommands {
)
}
static func clearUpNext() async {
await AppIntentDispatcher.shared.dispatch(name: "clearUpNext")
static func clearUpNext() async throws {
try await MusicIntentExecution.shared.run(name: "clearUpNext")
}
static func createStation(artistId: String, artistName: String) async {
await AppIntentDispatcher.shared.dispatch(
static func createStation(artistId: String, artistName: String) async throws {
try await MusicIntentExecution.shared.run(
name: "createStation",
params: [
"artistId": .string(artistId),
@@ -312,8 +312,8 @@ enum MusicCommands {
)
}
static func updateAffinity(kind: String, id: String, state: String) async {
await AppIntentDispatcher.shared.dispatch(
static func updateAffinity(kind: String, id: String, state: String) async throws {
try await MusicIntentExecution.shared.run(
name: "updateAffinity",
params: [
"kind": .string(kind),
@@ -323,23 +323,10 @@ enum MusicCommands {
)
}
static func transport(_ command: String) async {
await AppIntentDispatcher.shared.dispatch(
static func transport(_ command: String) async throws {
try await MusicIntentExecution.shared.run(
name: "transport",
params: ["command": .string(command)]
)
}
}
enum MusicIntentDiscovery {
/// Schema actions replace these Shortcuts entries on iOS 27. Older systems,
/// and builds made without the iOS 27 SDK, keep the custom actions visible.
static var hidesSchemaDuplicate: Bool {
#if compiler(>=6.4)
if #available(iOS 27.0, *) {
return true
}
#endif
return false
}
}
@@ -0,0 +1,245 @@
import AppIntents
@preconcurrency import CoreSpotlight
internal import ExpoAppIntents
internal import ExpoModulesCore
import UniformTypeIdentifiers
enum MusicEntityRegistration {
static func register() {
AppIntentDonationRegistry.shared.register("playMusic", as: PlayMusicIntent.self)
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("song", as: SongEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("song", as: SongEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("album", as: AlbumEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("album", as: AlbumEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("artist", as: ArtistEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("artist", as: ArtistEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("playlist", as: PlaylistEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("playlist", as: PlaylistEntity.self)
}
}
}
extension SongEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension SongEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = "\(artistName) · \(albumTitle)"
attributes.keywords = [title, artistName, albumTitle, "music", "song"]
return attributes
}
}
struct OpenSongIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Song"
static let openAppWhenRun = true
@Parameter(title: "Song")
var target: SongEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("song"), "id": .string(target.id)]
)
return .result()
}
}
extension AlbumEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension AlbumEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = artistName
attributes.keywords = [title, artistName, "music", "album"]
return attributes
}
}
struct OpenAlbumIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Album"
static let openAppWhenRun = true
@Parameter(title: "Album")
var target: AlbumEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("album"), "id": .string(target.id)]
)
return .result()
}
}
extension ArtistEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension ArtistEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = name
attributes.displayName = name
attributes.contentDescription = "oTunes artist"
attributes.keywords = [name, "music", "artist"]
return attributes
}
}
struct OpenArtistIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Artist"
static let openAppWhenRun = true
@Parameter(title: "Artist")
var target: ArtistEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("artist"), "id": .string(target.id)]
)
return .result()
}
}
extension PlaylistEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension PlaylistEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = "oTunes playlist"
attributes.keywords = [title, "music", "playlist"]
return attributes
}
}
struct OpenPlaylistIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Playlist"
static let openAppWhenRun = true
@Parameter(title: "Playlist")
var target: PlaylistEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("playlist"), "id": .string(target.id)]
)
return .result()
}
}
extension PlayMusicIntent: DonatableAppIntent {
struct DonationParams: Record {
@Field(.required) var kind: String = ""
@Field(.required) var id: String = ""
@Field var shuffle: Bool = false
}
init(donationParams: DonationParams) async throws {
self.init()
shuffle = donationParams.shuffle ? .songs : .off
switch donationParams.kind {
case "song":
guard let value = try await SongQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This song is no longer available.")
}
song = value
case "album":
guard let value = try await AlbumQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This album is no longer available.")
}
album = value
case "artist":
guard let value = try await ArtistQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This artist is no longer available.")
}
artist = value
case "playlist":
guard let value = try await PlaylistQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This playlist is no longer available.")
}
playlist = value
default:
throw MusicIntentFailure("Unknown music type.")
}
}
}
// The system can recover an evicted Spotlight index without waiting for the next
// catalog refresh. Rebuild only the compact catalog published to expo-app-intents.
#if compiler(>=6.4)
@available(iOS 27.0, *)
extension SongQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [SongEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "song", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "song")
}
}
@available(iOS 27.0, *)
extension AlbumQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [AlbumEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "album", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "album")
}
}
@available(iOS 27.0, *)
extension ArtistQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [ArtistEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "artist", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "artist")
}
}
@available(iOS 27.0, *)
extension PlaylistQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [PlaylistEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "playlist", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "playlist")
}
}
#endif
@@ -1,14 +1,13 @@
import AppIntents
import Foundation
// Apple Music runs these actions in the background. Leaving openAppWhenRun false
// keeps oTunes behind Siri and Shortcuts instead of taking the screen.
// Actions that use the React player or authenticated mutations open the app and
// wait for its acknowledgment. Native catalog reads can run without opening it.
struct PlayMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Play Music"
static let description = IntentDescription("Plays music using oTunes.")
static let openAppWhenRun = false
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@@ -39,7 +38,7 @@ struct PlayMusicIntent: AppIntent {
let kind = target?.0 ?? "resume"
let id = target?.1 ?? ""
let name = target?.2 ?? "your music"
await MusicCommands.play(
try await MusicCommands.play(
kind: kind,
id: id,
title: name,
@@ -57,8 +56,7 @@ struct AddToPlaylistIntent: AppIntent {
static let description = IntentDescription(
"Adds the items passed as input to the specified playlist."
)
static let openAppWhenRun = false
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@@ -86,7 +84,7 @@ struct AddToPlaylistIntent: AppIntent {
if sources.contains(where: { $0.id == playlist.id && $0.kind == "playlist" }) {
throw MusicIntentFailure("Choose a different playlist.")
}
await MusicCommands.addToPlaylist(
try await MusicCommands.addToPlaylist(
playlistId: playlist.id,
sources: sources.map { (kind: $0.kind, id: $0.id) }
)
@@ -99,7 +97,7 @@ struct AddToUpNextIntent: AppIntent {
static let description = IntentDescription(
"Adds the music passed as input to your Up Next queue."
)
static let openAppWhenRun = false
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@@ -124,7 +122,7 @@ struct AddToUpNextIntent: AppIntent {
artist: artist,
playlist: playlist
)
await MusicCommands.addToUpNext(
try await MusicCommands.addToUpNext(
position: position.rawValue,
sources: sources.map { (kind: $0.kind, id: $0.id) }
)
@@ -136,11 +134,11 @@ struct AddToUpNextIntent: AppIntent {
struct ClearUpNextIntent: AppIntent {
static let title: LocalizedStringResource = "Clear Up Next"
static let description = IntentDescription("Clears the Up Next queue.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
await MusicCommands.clearUpNext()
try await MusicCommands.clearUpNext()
return .result(dialog: "Clearing Up Next.")
}
}
@@ -150,7 +148,7 @@ struct CreatePlaylistIntent: AppIntent {
static let description = IntentDescription(
"Creates a new playlist, adding any items passed as input."
)
static let openAppWhenRun = false
static let openAppWhenRun = true
@Parameter(title: "Name")
var name: String
@@ -179,7 +177,7 @@ struct CreatePlaylistIntent: AppIntent {
artist.map { (kind: "artist", id: $0.id) },
playlist.map { (kind: "playlist", id: $0.id) },
].compactMap { $0 }
await MusicCommands.createPlaylist(name: trimmed, sources: sources)
try await MusicCommands.createPlaylist(name: trimmed, sources: sources)
return .result(dialog: "Creating \(trimmed).")
}
}
@@ -330,14 +328,14 @@ struct LoveSongIntent: AppIntent {
static let description = IntentDescription(
"Loves the song that is playing in oTunes. Also used for Favorite Song."
)
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "like")
try await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "like")
return .result(dialog: "Loving \(song.title).")
}
}
@@ -345,14 +343,14 @@ struct LoveSongIntent: AppIntent {
struct UnlikeSongIntent: AppIntent {
static let title: LocalizedStringResource = "Unlike Song"
static let description = IntentDescription("Removes the love from the song that is playing.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "unset")
try await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "unset")
return .result(dialog: "Unliking \(song.title).")
}
}
@@ -362,8 +360,7 @@ struct CreateStationIntent: AppIntent {
static let description = IntentDescription(
"Plays more music by the artist of the song that is playing."
)
static let openAppWhenRun = false
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
@@ -373,7 +370,7 @@ struct CreateStationIntent: AppIntent {
if song.artistId.isEmpty {
throw MusicIntentFailure("oTunes can't find an artist for this song.")
}
await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
try await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
return .result(dialog: "Playing more by \(artist).")
}
@@ -382,11 +379,11 @@ struct CreateStationIntent: AppIntent {
struct PauseMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Pause Music"
static let description = IntentDescription("Pauses playback in oTunes.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
await MusicCommands.transport("pause")
try await MusicCommands.transport("pause")
return .result(dialog: "Pausing.")
}
}
@@ -394,11 +391,11 @@ struct PauseMusicIntent: AppIntent {
struct ResumeMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Resume Music"
static let description = IntentDescription("Resumes playback in oTunes.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
await MusicCommands.transport("resume")
try await MusicCommands.transport("resume")
return .result(dialog: "Resuming.")
}
}
@@ -406,11 +403,11 @@ struct ResumeMusicIntent: AppIntent {
struct SkipMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Skip Song"
static let description = IntentDescription("Skips to the next song in oTunes.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
await MusicCommands.transport("next")
try await MusicCommands.transport("next")
return .result(dialog: "Skipping.")
}
}
@@ -418,11 +415,11 @@ struct SkipMusicIntent: AppIntent {
struct PreviousTrackIntent: AppIntent {
static let title: LocalizedStringResource = "Previous Song"
static let description = IntentDescription("Goes back to the previous song in oTunes.")
static let openAppWhenRun = false
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
await MusicCommands.transport("previous")
try await MusicCommands.transport("previous")
return .result(dialog: "Going back.")
}
}
@@ -0,0 +1,70 @@
import Foundation
internal import ExpoAppIntents
/// Full catalogs live in Application Support, never in UserDefaults. The package
/// catalog contains only a bounded selection for shortcut suggestions and Spotlight.
actor MusicCatalogStorage {
static let shared = MusicCatalogStorage()
private func location(_ kind: String) throws -> URL {
guard ["song", "album", "artist", "playlist", "nowPlaying"].contains(kind) else {
throw MusicIntentFailure("Unknown music catalog.")
}
let directory = try FileManager.default.url(
for: .applicationSupportDirectory, in: .userDomainMask,
appropriateFor: nil, create: true
).appendingPathComponent("MusicIntents", isDirectory: true)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
return directory.appendingPathComponent("\(kind).json")
}
func replace(kind: String, json: String) throws {
let data = Data(json.utf8)
let records = try JSONDecoder().decode([AppIntentEntityRecord].self, from: data)
guard Set(records.map(\.id)).count == records.count,
records.allSatisfy({ !$0.id.isEmpty && !$0.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
else { throw MusicIntentFailure("Invalid music catalog.") }
try data.write(to: location(kind), options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
}
func records(kind: String) throws -> [AppIntentEntityRecord] {
let url = try location(kind)
guard FileManager.default.fileExists(atPath: url.path) else { return [] }
return try JSONDecoder().decode([AppIntentEntityRecord].self, from: Data(contentsOf: url))
}
}
/// App-owned completion channel. Expo's dispatcher itself remains one-way.
actor MusicIntentExecution {
static let shared = MusicIntentExecution()
private var waiting = Set<String>()
private var results: [String: String] = [:]
func complete(token: String, error: String?) {
guard waiting.contains(token) else { return }
results[token] = error ?? ""
}
func run(name: String, params: AppIntentParams = [:]) async throws -> String {
let token = UUID().uuidString
let deadline = Date().addingTimeInterval(25)
waiting.insert(token)
defer {
waiting.remove(token)
results.removeValue(forKey: token)
}
var payload = params
payload["completionToken"] = .string(token)
payload["deadline"] = .double(deadline.timeIntervalSince1970 * 1000)
let id = await AppIntentDispatcher.shared.dispatch(name: name, params: payload)
while Date() < deadline {
try Task.checkCancellation()
if let error = results.removeValue(forKey: token) {
if !error.isEmpty { throw MusicIntentFailure(error) }
return id
}
try await Task.sleep(for: .milliseconds(100))
}
throw MusicIntentFailure("oTunes did not finish in time. Open the app, check your connection, and try again.")
}
}
+33
View File
@@ -0,0 +1,33 @@
# Siri and Shortcuts
This integration uses Expo 58 inline modules and `expo-app-intents`. Rebuild the native app after changing Swift declarations or registrations. An OTA JavaScript update cannot add these capabilities.
Playback and library changes open oTunes because they need its authenticated React runtime. Swift dispatches a request and waits for an app-owned completion acknowledgment. Playback waits for the native audio player to load the requested track; warmup waits for it to load while paused. Errors reach Siri, and requests that have expired are discarded before starting a side effect. Native catalog reads do not open the app.
The full entity catalog lives in Application Support. The backend supplies pages of 200 records. Expo's UserDefaults catalog holds at most 100 suggestions per kind, with favorites first, and mirrors those suggestions into Spotlight. Siri's native queries search the full disk catalog. Catalogs refresh on launch, foreground entry, favorite changes, and every five minutes while foregrounded.
Songs, albums, artists, and playlists have Spotlight open intents and on-screen entity associations. Associations require iOS 18.4 and Xcode 27; Spotlight indexing requires iOS 18. Playback donations are made after the player starts, preserve shuffle, and resolve real catalog entities. Removed records trigger donation cleanup. Sign-out clears catalogs, Spotlight entries, donations, and pending invocations. On iOS 27, the query types also handle system requests to rebuild Spotlight indexes.
Audio schema intents require Xcode 27 and iOS 27. Custom intents remain available on earlier supported versions and remain visible on iOS 27 because intent discovery metadata must be a compile-time constant.
## Backend rollout
Deploy the companion Convex changes before installing the new mobile build:
- `library.intentCatalogPage` supplies authenticated, paginated entity records.
- `appIntentPlaylists` records playlist-creation request IDs in the same transaction as creation. Replaying a request returns the original playlist ID.
- `playlists.createPlaylist` accepts an optional `invocationId`; existing callers can omit it.
## Verification
Run from the repository root:
```sh
pnpm --filter @otunes/mobile typecheck
pnpm --filter @otunes/convex typecheck
node --test apps/mobile/lib/app-intents.test.cjs packages/convex/convex/app-intent-playlists.test.cjs
```
Build iOS with Xcode and verify the app target's `ExtractAppIntentsMetadata` phase. TypeScript checks alone do not validate Swift declarations or Apple's schema contracts.
On a suitable device, verify cold and warm playback, paused warmup followed by playback, offline errors, request expiry, playlist replay, and a saved shortcut after upgrading. Check entity resolution after rename/delete and sign-out. Verify Spotlight opening, on-screen requests, and donations separately; metadata extraction does not establish system discovery or successful runtime execution.
+1 -2
View File
@@ -66,8 +66,7 @@
],
"expo-image",
"expo-status-bar",
"expo-app-intents",
"./plugins/with-app-intent-sources.js"
"expo-app-intents"
],
"experiments": {
"typedRoutes": true,
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useIsFocused, useLocalSearchParams } from "expo-router"
import { StatusBar } from "expo-status-bar"
import { memo, useCallback, useEffect, useMemo } from "react"
@@ -180,7 +181,7 @@ export default function AlbumScreen() {
indicatorStyle={palette.isDark ? "white" : "black"}
ListHeaderComponent={
<View>
<View style={styles.hero}>
<MusicEntityView kind="album" id={album.id} style={styles.hero}>
<CoverImage
uri={coverUri}
hasCover={album.cover != null}
@@ -244,7 +245,7 @@ export default function AlbumScreen() {
/>
</Pressable>
</View>
</View>
</MusicEntityView>
<CollectionDivider color={palette.divider} />
</View>
}
@@ -332,39 +333,44 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
}}
onAction={(action) => void onAction(track, album, action)}
>
<Pressable
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
<Text
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
<MusicEntityView kind="song" id={track.id}>
<Pressable
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
{number}
</Text>
<View
style={[styles.trackContent, { borderBottomColor: palette.divider }]}
>
<View style={styles.trackMeta}>
<Text
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.title}
</Text>
{track.artists ? (
<Text
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
>
{number}
</Text>
<View
style={[
styles.trackContent,
{ borderBottomColor: palette.divider },
]}
>
<View style={styles.trackMeta}>
<Text
style={{ color: palette.secondaryForeground }}
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.artists.join(", ")}
{track.title}
</Text>
) : null}
{track.artists ? (
<Text
style={{ color: palette.secondaryForeground }}
numberOfLines={1}
>
{track.artists.join(", ")}
</Text>
) : null}
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</View>
</Pressable>
</Pressable>
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useLocalSearchParams } from "expo-router"
import { Pressable, StyleSheet, Text, View } from "react-native"
import { Ionicons } from "@expo/vector-icons"
@@ -95,7 +96,11 @@ export default function ArtistScreen() {
paddingBottom={contentBottomInset}
contentInset={false}
ListHeaderComponent={
<View style={styles.hero}>
<MusicEntityView
kind="artist"
id={data.artist.id}
style={styles.hero}
>
<CoverImage
uri={artistCoverUrl(config.serverUrl, data.artist.id)}
hasCover={data.artist.photo != null}
@@ -138,7 +143,7 @@ export default function ArtistScreen() {
<Ionicons name="shuffle" size={24} color={colors.foreground} />
</Pressable>
</View>
</View>
</MusicEntityView>
}
/>
</>
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { useMutation } from "convex/react"
import { memo, useCallback, useEffect, useMemo } from "react"
import {
@@ -343,6 +344,7 @@ function PlaylistContent({
onScrollBeginDrag={swipeGroup.closeOpenRow}
ListHeaderComponent={
<PlaylistHero
entityId={playlistId ?? "otunes:favorites"}
coverUri={coverUri}
hasCover={playlist.cover != null}
title={playlist.title}
@@ -450,6 +452,7 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
})
const PlaylistHero = memo(function PlaylistHero({
entityId,
coverUri,
hasCover,
title,
@@ -461,6 +464,7 @@ const PlaylistHero = memo(function PlaylistHero({
onPlay,
onShuffle,
}: {
entityId: string
coverUri: string
hasCover: boolean
title: string
@@ -473,7 +477,7 @@ const PlaylistHero = memo(function PlaylistHero({
onShuffle: () => void
}) {
return (
<View>
<MusicEntityView kind="playlist" id={entityId}>
<View style={styles.hero}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.cover} />
<Text style={[styles.title, { color: palette.foreground }]}>
@@ -528,7 +532,7 @@ const PlaylistHero = memo(function PlaylistHero({
</View>
</View>
<CollectionDivider color={palette.divider} />
</View>
</MusicEntityView>
)
})
+7 -2
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { useRouter } from "expo-router"
import * as Haptics from "expo-haptics"
import { StatusBar } from "expo-status-bar"
@@ -216,7 +217,11 @@ export default function PlayerScreen() {
<Animated.View style={[styles.root, backgroundStyle, dismissStyle]}>
<StatusBar style={colors.isDark ? "light" : "dark"} />
<View style={[styles.safe, { paddingTop: insets.top }]}>
<MusicEntityView
kind="song"
id={currentSong.id}
style={[styles.safe, { paddingTop: insets.top }]}
>
<GestureDetector gesture={dismissGesture()}>
<Pressable
onPress={() => router.back()}
@@ -472,7 +477,7 @@ export default function PlayerScreen() {
</View>
</View>
</GestureDetector>
</View>
</MusicEntityView>
</Animated.View>
)
}
+195 -81
View File
@@ -2,6 +2,13 @@ import { useConvex, useQuery } from "convex/react"
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity, AppIntentInvocation } from "expo-app-intents"
import { useEffect, useMemo, useRef, useSyncExternalStore } from "react"
import { AppState } from "react-native"
import {
completeMusicInvocation,
publishMusicCatalog,
trackMusicCatalogSync,
} from "@/lib/app-intents"
import { useAppNavigation } from "@/lib/app-navigation"
import {
ANONYMOUS_CACHE_SCOPE,
@@ -34,6 +41,7 @@ const KNOWN_INTENTS = new Set([
"createStation",
"updateAffinity",
"transport",
"openMusic",
])
type MusicSource = {
@@ -79,6 +87,13 @@ function repeatChoice(value: unknown): RepeatMode | null {
throw new IntentRejected("Repeat must be None, One, or All.")
}
function ensureActive(invocation: AppIntentInvocation) {
const deadline = invocation.params.deadline
if (typeof deadline === "number" && Date.now() >= deadline) {
throw new IntentRejected("This request expired. Please try again.")
}
}
function isTransient(error: unknown) {
const message = (
error instanceof Error ? error.message : String(error)
@@ -128,10 +143,24 @@ function songEntity(song: {
}
export function AppIntentBridge() {
const scope = useCacheScope()
const store = useMemo(
() => getPlaybackStore(scope === ANONYMOUS_CACHE_SCOPE ? null : scope),
[scope]
)
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
const user = useQuery(api.auth.currentUser)
return user && snapshot.hydrated ? <ReadyAppIntentBridge key={scope} /> : null
}
function ReadyAppIntentBridge() {
const convex = useConvex()
const cacheScope = useCacheScope()
const playback = usePlayback()
const catalog = useQuery(api.library.intentCatalog, {})
const user = useQuery(api.auth.currentUser)
const navigation = useAppNavigation()
const favorites = useQuery(api.favorites.getUserFavorites)
const catalogUserKey = user ? JSON.stringify([user._id, favorites]) : null
const store = useMemo(
() =>
getPlaybackStore(
@@ -147,82 +176,57 @@ export function AppIntentBridge() {
playbackRef.current = playback
useEffect(() => {
if (!catalog || !AppIntents.isAvailable()) {
return
}
if (!AppIntents.isAvailable() || !user) return
const generation = ++publishGeneration.current
const snapshotCatalog = catalog
publishChain.current = publishChain.current
.catch(() => undefined)
.then(async () => {
if (generation !== publishGeneration.current) {
return
}
const writes = [
AppIntents.setEntityCatalogAsync(
"song",
snapshotCatalog.songs.map((song) =>
songEntity({
...song,
artistName: song.artistName,
let stopped = false
let refreshing = false
const refresh = () => {
if (refreshing || stopped) return
refreshing = true
publishChain.current = publishChain.current
.catch(() => undefined)
.then(async () => {
for (const kind of ["artist", "album", "song", "playlist"] as const) {
const entities: AppIntentEntity[] = []
let cursor: string | null = null
do {
const result: {
page: AppIntentEntity[]
isDone: boolean
continueCursor: string
} = await convex.query(api.library.intentCatalogPage, {
kind,
paginationOpts: { cursor, numItems: 200 },
})
)
),
AppIntents.setEntityCatalogAsync(
"album",
snapshotCatalog.albums.map((album) => ({
id: album.id,
title: album.title,
subtitle: album.artistName,
synonyms: album.artistName ? [album.artistName] : undefined,
metadata: {
artistName: album.artistName,
artistId: album.artistId,
year: String(album.year),
favorite: album.favorite ? "1" : "0",
},
}))
),
AppIntents.setEntityCatalogAsync(
"artist",
snapshotCatalog.artists.map((artist) => ({
id: artist.id,
title: artist.name,
metadata: { favorite: artist.favorite ? "1" : "0" },
}))
),
]
if (snapshotCatalog.authenticated) {
writes.push(
AppIntents.setEntityCatalogAsync(
"playlist",
snapshotCatalog.playlists.map((playlist) => ({
id: playlist.id,
title: playlist.title,
subtitle: playlist.ownerName,
synonyms:
playlist.id === FAVORITES_PLAYLIST_ID
? [
"favorites",
"favourite songs",
"liked songs",
"loved songs",
]
: undefined,
metadata: {
ownerName: playlist.ownerName,
createdByMe: playlist.createdByMe ? "1" : "0",
},
}))
)
)
}
await Promise.all(writes)
})
.catch((error) => {
console.warn("Could not publish the oTunes music catalog.", error)
})
}, [catalog])
if (stopped || generation !== publishGeneration.current) return
entities.push(...result.page)
cursor = result.isDone ? null : result.continueCursor
} while (cursor !== null)
await publishMusicCatalog(kind, entities)
}
})
.catch((error) =>
console.warn("Could not publish music catalog.", error)
)
.finally(() => {
refreshing = false
})
trackMusicCatalogSync(publishChain.current)
}
refresh()
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") refresh()
})
const timer = setInterval(() => {
if (AppState.currentState === "active") refresh()
}, 5 * 60_000)
return () => {
stopped = true
++publishGeneration.current
subscription.remove()
clearInterval(timer)
}
}, [convex, cacheScope, catalogUserKey])
const currentSongId = playback.queueSongIds[0] ?? null
const currentSong = playback.currentSong
@@ -232,7 +236,7 @@ export function AppIntentBridge() {
return
}
if (!currentSongId) {
void AppIntents.setEntityCatalogAsync("nowPlaying", []).catch((error) => {
void publishMusicCatalog("nowPlaying", []).catch((error) => {
console.warn("Could not clear the current song.", error)
})
return
@@ -240,7 +244,7 @@ export function AppIntentBridge() {
if (!currentSong || currentSong.id !== currentSongId) {
return
}
void AppIntents.setEntityCatalogAsync("nowPlaying", [
void publishMusicCatalog("nowPlaying", [
songEntity({
id: currentSong.id,
title: currentSong.title,
@@ -263,6 +267,8 @@ export function AppIntentBridge() {
for (const invocation of pendingIntents) {
if (handled.has(invocation.id)) {
const token = readString(invocation.params.completionToken)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
@@ -270,12 +276,42 @@ export function AppIntentBridge() {
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
const token = readString(invocation.params.completionToken)
try {
ensureActive(invocation)
await performInvocation(invocation)
if (
["playMusic", "warmupAudio", "transport", "createStation"].includes(
invocation.name
)
) {
const id = store.getState().queue[0]
const song = playbackRef.current.currentSong
if (!id) await publishMusicCatalog("nowPlaying", [])
else if (song?.id === id)
await publishMusicCatalog("nowPlaying", [
songEntity({
id: song.id,
title: song.title,
artistName: song.artist.name,
artistId: song.artist.id,
albumTitle: song.album.title,
albumId: song.album.id,
durationSeconds: song.durationSeconds,
}),
])
}
await rememberHandledIntentId(handled, invocation.id)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (error) {
if (error instanceof IntentRejected || !isTransient(error)) {
if (token) {
await completeMusicInvocation(
token,
error instanceof Error ? error.message : "oTunes couldn't do that."
)
}
if (token || error instanceof IntentRejected || !isTransient(error)) {
const message =
error instanceof Error ? error.message : "oTunes couldn't do that."
if (!(error instanceof IntentRejected) || message) {
@@ -350,11 +386,36 @@ export function AppIntentBridge() {
async function performInvocation(invocation: AppIntentInvocation) {
const actions = playbackRef.current
switch (invocation.name) {
case "openMusic": {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!id || !kind) throw new IntentRejected("Choose music to open.")
if (kind === "song") {
const song = await convex.query(api.library.getSong, {
id: id as Id<"songs">,
})
if (!song)
throw new IntentRejected("This song is no longer available.")
navigation.push(`/album/${song.album}`)
} else if (kind === "playlist" && id === FAVORITES_PLAYLIST_ID) {
navigation.push("/playlist/favorites")
} else if (["album", "artist", "playlist"].includes(kind)) {
navigation.push(`/${kind}/${id}`)
} else throw new IntentRejected("Unknown music type.")
return
}
case "playMusic":
await playMusic(invocation)
if (!["next", "tail"].includes(String(invocation.params.queue))) {
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
}
return
case "warmupAudio":
await warmup(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(false)
warmedAudioIds.add(invocation.id)
return
case "addToPlaylist":
await addToPlaylist(invocation)
@@ -363,6 +424,7 @@ export function AppIntentBridge() {
await addToUpNext(invocation)
return
case "clearUpNext":
ensureActive(invocation)
await actions.clearUpNext()
return
case "createPlaylist":
@@ -370,12 +432,24 @@ export function AppIntentBridge() {
return
case "createStation":
await createStation(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
return
case "updateAffinity":
await updateAffinity(invocation)
return
case "transport":
await transport(invocation)
if (store.getState().queue.length > 0) {
const command = invocation.params.command
await playbackRef.current.waitForPlaybackReady(
command === "resume"
? true
: command === "pause"
? false
: undefined
)
}
return
default:
return
@@ -388,6 +462,7 @@ export function AppIntentBridge() {
const warmupId = readString(invocation.params.warmupId)
const repeat = repeatChoice(invocation.params.repeat)
if (repeat) {
ensureActive(invocation)
await actions.setRepeatMode(repeat)
}
@@ -402,6 +477,7 @@ export function AppIntentBridge() {
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
const ids = await songIdsFor(kind, id)
@@ -409,8 +485,10 @@ export function AppIntentBridge() {
throw new IntentRejected("Nothing to add to Up Next.")
}
if (queue === "next") {
ensureActive(invocation)
await actions.addToQueueNext(ids)
} else {
ensureActive(invocation)
await actions.addToQueueLast(ids)
}
return
@@ -422,8 +500,10 @@ export function AppIntentBridge() {
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
@@ -435,6 +515,7 @@ export function AppIntentBridge() {
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
if (actions.queueSongIds.length === 0) {
@@ -450,12 +531,15 @@ export function AppIntentBridge() {
)
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(favorites.favoriteSongs)
} else {
ensureActive(invocation)
await actions.playInOrder(favorites.favoriteSongs)
}
return
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
@@ -486,6 +570,7 @@ export function AppIntentBridge() {
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await actions.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
@@ -500,8 +585,10 @@ export function AppIntentBridge() {
throw new IntentRejected("Nothing to play.")
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(ids)
} else {
ensureActive(invocation)
await actions.playInOrder(ids)
}
}
@@ -531,6 +618,7 @@ export function AppIntentBridge() {
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await playbackRef.current.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
@@ -542,9 +630,9 @@ export function AppIntentBridge() {
if (ids.length === 0) {
throw new IntentRejected("Nothing to get ready.")
}
ensureActive(invocation)
await playbackRef.current.prepareQueue(ids, shuffled)
}
warmedAudioIds.add(invocation.id)
}
async function addToPlaylist(invocation: AppIntentInvocation) {
@@ -560,20 +648,24 @@ export function AppIntentBridge() {
if (playlistId === FAVORITES_PLAYLIST_ID) {
for (const source of sources) {
if (source.kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: source.id as Id<"songs">,
})
} else if (source.kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: source.id as Id<"albums">,
})
} else if (source.kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: source.id as Id<"artists">,
})
} else if (source.kind === "playlist") {
const ids = await songIdsFor(source.kind, source.id)
if (ids.length > 0) {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSongs, {
songIds: ids,
})
@@ -586,6 +678,7 @@ export function AppIntentBridge() {
}
return
}
ensureActive(invocation)
await convex.mutation(api.playlists.addSongsToPlaylist, {
playlistId: playlistId as Id<"playlists">,
songIds,
@@ -604,8 +697,10 @@ export function AppIntentBridge() {
const position =
readString(invocation.params.position) === "last" ? "last" : "next"
if (position === "last") {
ensureActive(invocation)
await playbackRef.current.addToQueueLast(ids)
} else {
ensureActive(invocation)
await playbackRef.current.addToQueueNext(ids)
}
}
@@ -618,7 +713,12 @@ export function AppIntentBridge() {
const songIds = await uniqueSongIds(
readSources(invocation.params.sources)
)
await convex.mutation(api.playlists.createPlaylist, { name, songIds })
ensureActive(invocation)
await convex.mutation(api.playlists.createPlaylist, {
name,
songIds,
invocationId: invocation.id,
})
}
async function createStation(invocation: AppIntentInvocation) {
@@ -626,7 +726,11 @@ export function AppIntentBridge() {
if (!artistId) {
throw new IntentRejected("Nothing is playing in oTunes.")
}
await playbackRef.current.playArtistShuffled(artistId as Id<"artists">)
const ids = await songIdsFor("artist", artistId)
if (ids.length === 0)
throw new IntentRejected("This artist has no songs to play.")
ensureActive(invocation)
await playbackRef.current.playShuffled(ids)
}
async function updateAffinity(invocation: AppIntentInvocation) {
@@ -643,14 +747,17 @@ export function AppIntentBridge() {
}
if (state === "like") {
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: id as Id<"artists">,
})
@@ -662,14 +769,17 @@ export function AppIntentBridge() {
return
}
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteArtist, {
artistId: id as Id<"artists">,
})
@@ -682,6 +792,7 @@ export function AppIntentBridge() {
const actions = playbackRef.current
const command = readString(invocation.params.command)
if (command === "pause") {
ensureActive(invocation)
actions.setIsPlaying(false)
return
}
@@ -689,14 +800,17 @@ export function AppIntentBridge() {
if (actions.queueSongIds.length === 0) {
throw new IntentRejected("Nothing is queued in oTunes.")
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
if (command === "next") {
ensureActive(invocation)
await actions.skipNext()
return
}
if (command === "previous") {
ensureActive(invocation)
await actions.skipPrevious()
return
}
+10 -3
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Ionicons } from "@expo/vector-icons"
import { memo, useCallback, type ReactElement } from "react"
import {
@@ -78,7 +79,9 @@ export const AlbumCard = memo(function AlbumCard({
share={{ type: "album", albumId: album.id }}
onAction={(action) => void handleAlbumAction(album, action)}
>
{card}
<MusicEntityView kind="album" id={album.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -121,7 +124,9 @@ export const ArtistCard = memo(function ArtistCard({
share={{ type: "artist", artistId: artist.id }}
onAction={(action) => void handleArtistAction(artist, action)}
>
{card}
<MusicEntityView kind="artist" id={artist.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -170,7 +175,9 @@ export const PlaylistCard = memo(function PlaylistCard({
share={{ type: "playlist", playlistId: playlist.id }}
onAction={(action) => void handlePlaylistAction(playlist, action)}
>
{card}
<MusicEntityView kind="playlist" id={playlist.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -0,0 +1,27 @@
import { useIsFocused } from "expo-router"
import { AppEntityView } from "expo-app-intents"
import type { PropsWithChildren } from "react"
import { View, type ViewProps } from "react-native"
import type { MusicEntityKind } from "@/lib/app-intents"
/** Only the focused screen contributes entities to Siri's on-screen context. */
export function MusicEntityView({
kind,
id,
children,
...props
}: PropsWithChildren<
ViewProps & {
kind: MusicEntityKind
id: string
}
>) {
const focused = useIsFocused()
return focused ? (
<AppEntityView entity={kind} entityId={id} {...props}>
{children}
</AppEntityView>
) : (
<View {...props}>{children}</View>
)
}
+17 -4
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { collectionInset } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import { useServerConfig } from "@/contexts/server-config"
@@ -45,10 +46,16 @@ export const SongRow = memo(function SongRow({
const content = (
<>
<View style={styles.rowMeta}>
<Text style={[styles.rowTitle, { color: foreground }]} numberOfLines={1}>
<Text
style={[styles.rowTitle, { color: foreground }]}
numberOfLines={1}
>
{song.title}
</Text>
<Text style={[styles.rowSubtitle, { color: secondaryForeground }]} numberOfLines={1}>
<Text
style={[styles.rowSubtitle, { color: secondaryForeground }]}
numberOfLines={1}
>
{song.artist.name} · {song.album.title}
</Text>
</View>
@@ -82,12 +89,18 @@ export const SongRow = memo(function SongRow({
)
if (!contextMenu) {
return row
return (
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
)
}
return (
<MediaContextMenu {...contextMenu}>
{row}
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
</MediaContextMenu>
)
})
+6 -1
View File
@@ -5,6 +5,7 @@ import { useCallback, useMemo, useState, type ReactNode } from "react"
import { ConvexAuthSessionProvider } from "@/contexts/convex-auth-session"
import { PersistentCacheProvider } from "@/contexts/persistent-cache"
import { useServerConfig } from "@/contexts/server-config"
import { clearAppIntentCatalogs } from "@/lib/app-intent-handled"
import { clearStoredAuthTokens, secureStorage } from "@/lib/auth-storage"
export function ConvexAppProvider({ children }: { children: ReactNode }) {
@@ -21,7 +22,11 @@ export function ConvexAppProvider({ children }: { children: ReactNode }) {
return
}
await clearStoredAuthTokens(config.convexUrl)
setAuthEpoch((epoch) => epoch + 1)
try {
await clearAppIntentCatalogs()
} finally {
setAuthEpoch((epoch) => epoch + 1)
}
}, [config])
if (!client || !config) {
+77 -6
View File
@@ -39,6 +39,7 @@ import { getAuthCookieHeader } from "@/lib/auth-cookies"
import { flushPersistentCache } from "@/lib/persistent-cache"
import { loadPlaybackSongs } from "@/lib/playback-files"
import { rememberSongs, useSong, useSongs } from "@/lib/song-cache"
import { donateMusicPlay, type MusicEntityKind } from "@/lib/app-intents"
import { showError } from "@/lib/toast"
import { albumCoverUrl, songAudioSource } from "@/lib/urls"
import {
@@ -82,6 +83,7 @@ type PlaybackContextValue = {
currentTime: number
duration: number
seekTo: (seconds: number) => void
waitForPlaybackReady: (playing?: boolean) => Promise<void>
setIsPlaying: (playing: boolean) => void
setVolume: (volume: number) => void
playInOrder: (songIds: Id<"songs">[]) => Promise<void>
@@ -872,7 +874,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
recipe: (
state: playbackQueue.PlaybackQueueState
) => playbackQueue.PlaybackQueueState,
recorded: { playlistId?: Id<"playlists"> } = {}
recorded: {
playlistId?: Id<"playlists">
entity?: { kind: MusicEntityKind; id: string }
} = {}
) => {
playRequestRef.current += 1
const previousSongId = store.getState().queue[0]
@@ -884,6 +889,29 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
void seekTo(0)
}
setIsPlaying(true)
void (async () => {
const deadline = Date.now() + 20_000
while (
Date.now() < deadline &&
store.getState().queue[0] === firstSongId
) {
const player = playlistRef.current
if (
player?.isLoaded &&
player.playing &&
loadedSongIdsRef.current[player.currentIndex] === firstSongId
) {
await donateMusicPlay(
recorded.entity?.kind ??
(recorded.playlistId ? "playlist" : "song"),
recorded.entity?.id ?? recorded.playlistId ?? firstSongId,
store.getState().shuffleEnabled
)
return
}
await new Promise((resolve) => setTimeout(resolve, 250))
}
})()
recordPlay(
recorded.playlistId
? { playlistId: recorded.playlistId }
@@ -897,7 +925,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
async (
songIds: Id<"songs">[] | Promise<Id<"songs">[]>,
shuffled: boolean,
recorded: { playlistId?: Id<"playlists"> } = {}
recorded: {
playlistId?: Id<"playlists">
entity?: { kind: MusicEntityKind; id: string }
} = {}
) => {
const request = ++playRequestRef.current
let resolvedSongIds: Id<"songs">[]
@@ -1033,25 +1064,33 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const playAlbum = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), false),
playCollection(loadAlbumSongIds(albumId, songIds), false, {
entity: { kind: "album", id: albumId },
}),
[playCollection, loadAlbumSongIds]
)
const playAlbumShuffled = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), true),
playCollection(loadAlbumSongIds(albumId, songIds), true, {
entity: { kind: "album", id: albumId },
}),
[playCollection, loadAlbumSongIds]
)
const playArtist = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), false),
playCollection(loadArtistSongIds(artistId), false, {
entity: { kind: "artist", id: artistId },
}),
[playCollection, loadArtistSongIds]
)
const playArtistShuffled = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), true),
playCollection(loadArtistSongIds(artistId), true, {
entity: { kind: "artist", id: artistId },
}),
[playCollection, loadArtistSongIds]
)
@@ -1193,6 +1232,36 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
store.update(playbackQueue.skipPrevious)
}, [store, seekTo])
// Queue changes return before React has loaded the native audio source. Siri
// must observe the native player, rather than treating a state update as success.
const waitForPlaybackReady = useCallback(
async (playing?: boolean) => {
const expectedId = store.getState().queue[0]
if (!expectedId) throw new Error("Nothing is queued to play.")
const deadline = Date.now() + 20_000
while (Date.now() < deadline) {
if (store.getState().queue[0] !== expectedId) {
throw new Error(
"The playback queue changed before the request finished."
)
}
const player = playlistRef.current
if (
player &&
loadedSongIdsRef.current[player.currentIndex] === expectedId &&
player.isLoaded &&
(playing === undefined || player.playing === playing)
)
return
await new Promise((resolve) => setTimeout(resolve, 100))
}
throw new Error(
"The audio did not become ready. Check your connection and try again."
)
},
[store]
)
const actionsValue = useMemo<PlaybackActionsValue>(
() => ({
setIsPlaying,
@@ -1269,6 +1338,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const value = useMemo<PlaybackContextValue>(
() => ({
...actionsValue,
waitForPlaybackReady,
queueSongIds,
queueHistorySongIds,
currentSong,
@@ -1283,6 +1353,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
}),
[
actionsValue,
waitForPlaybackReady,
currentSong,
currentTime,
duration,
+8 -3
View File
@@ -1,5 +1,6 @@
import AsyncStorage from "@react-native-async-storage/async-storage"
import * as AppIntents from "expo-app-intents"
import { publishMusicCatalog } from "@/lib/app-intents"
export const FAVORITES_PLAYLIST_ID = "otunes:favorites"
@@ -18,9 +19,13 @@ export async function clearAppIntentCatalogs() {
if (!AppIntents.isAvailable()) {
return
}
await Promise.all(
CATALOG_KINDS.map((kind) => AppIntents.setEntityCatalogAsync(kind, []))
)
const results = await Promise.allSettled([
AppIntents.deleteDonationsAsync({ intent: "playMusic" }),
AppIntents.clearPendingInvocationsAsync(),
...CATALOG_KINDS.map((kind) => publishMusicCatalog(kind, [])),
])
const failed = results.find((result) => result.status === "rejected")
if (failed?.status === "rejected") throw failed.reason
}
export async function loadHandledIntentIds() {
+147
View File
@@ -0,0 +1,147 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const fs = require("node:fs")
const vm = require("node:vm")
const ts = require("typescript")
// Exercise the app's catalog lifecycle without requiring a React Native runtime.
function runtime() {
const disk = new Map()
const catalogs = new Map()
const deleted = []
const donated = []
const native = {
catalogJSON: async (kind) => JSON.stringify(disk.get(kind) ?? []),
replaceCatalog: async (kind, json) => disk.set(kind, JSON.parse(json)),
completeInvocation: async () => {},
}
const expo = {
isAvailable: () => true,
getEntityCatalogAsync: async (kind) => catalogs.get(kind) ?? [],
setEntityCatalogAsync: async (kind, entities) =>
catalogs.set(kind, entities),
deleteDonationsAsync: async (filter) => deleted.push(filter),
donateIntentAsync: async (name, params) => donated.push({ name, params }),
}
const module = { exports: {} }
const source = ts.transpileModule(
fs.readFileSync(`${__dirname}/app-intents.ts`, "utf8"),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}
).outputText
vm.runInNewContext(source, {
module,
exports: module.exports,
console: { warn() {} },
require: (name) => {
if (name === "expo") return { requireOptionalNativeModule: () => native }
if (name === "expo-app-intents") return expo
throw new Error(`Unexpected import ${name}`)
},
})
return { api: module.exports, native, expo, disk, catalogs, deleted, donated }
}
function song(id, favorite = false) {
return {
id: String(id),
title: `Song ${id}`,
metadata: { favorite: favorite ? "1" : "0" },
}
}
function deferred() {
let resolve
const promise = new Promise((done) => {
resolve = done
})
return { promise, resolve }
}
test("large libraries remain fully searchable on disk while suggestions stay bounded", async () => {
const { api, disk, catalogs } = runtime()
const songs = Array.from({ length: 10_000 }, (_, id) => song(id, id === 9999))
await api.publishMusicCatalog("song", songs)
assert.equal(disk.get("song").length, 10_000)
assert.equal(catalogs.get("song").length, 100)
assert.equal(catalogs.get("song")[0].id, "9999")
})
test("deleting a song outside the suggestion subset removes its donations", async () => {
const { api, deleted } = runtime()
const songs = Array.from({ length: 200 }, (_, id) => song(id))
await api.publishMusicCatalog("song", songs)
await api.publishMusicCatalog("song", songs.slice(0, -1))
assert.equal(deleted.length, 1)
assert.equal(deleted[0].entity, "song")
assert.equal(deleted[0].id, "199")
})
test("a queued clear finishes after an in-flight publication and leaves no stale index", async () => {
const { api, native, disk, catalogs } = runtime()
const gate = deferred()
const original = native.replaceCatalog
native.replaceCatalog = async (...args) => {
await gate.promise
return original(...args)
}
const publish = api.publishMusicCatalog("song", [song(1)])
const clear = api.publishMusicCatalog("song", [])
gate.resolve()
await Promise.all([publish, clear])
assert.equal(disk.get("song").length, 0)
assert.equal(catalogs.get("song").length, 0)
})
test("a storage failure does not poison subsequent catalog cleanup", async () => {
const { api, native, catalogs } = runtime()
const original = native.replaceCatalog
native.replaceCatalog = async () => {
throw new Error("Disk unavailable")
}
await assert.rejects(
api.publishMusicCatalog("song", [song(1)]),
/Disk unavailable/
)
native.replaceCatalog = original
await api.publishMusicCatalog("song", [])
assert.equal(catalogs.get("song").length, 0)
})
test("donations wait for catalog synchronization and failures do not reject playback", async () => {
const { api, donated, expo } = runtime()
const sync = deferred()
api.trackMusicCatalogSync(sync.promise)
const donation = api.donateMusicPlay("album", "album-1")
await new Promise((resolve) => setImmediate(resolve))
assert.equal(donated.length, 0)
sync.resolve()
await donation
assert.equal(donated[0].params.id, "album-1")
expo.donateIntentAsync = async () => {
throw new Error("Suggestions unavailable")
}
await api.donateMusicPlay("album", "album-1")
})
test("failed donation cleanup retains deleted records so the next sync can retry", async () => {
const { api, disk, expo, deleted } = runtime()
await api.publishMusicCatalog("song", [song(1), song(2)])
const cleanup = expo.deleteDonationsAsync
expo.deleteDonationsAsync = async () => {
throw new Error("Cleanup failed")
}
await assert.rejects(
api.publishMusicCatalog("song", [song(1)]),
/Cleanup failed/
)
assert.equal(disk.get("song").length, 2)
expo.deleteDonationsAsync = cleanup
await api.publishMusicCatalog("song", [song(1)])
assert.equal(deleted[0].id, "2")
assert.equal(disk.get("song").length, 1)
})
+74
View File
@@ -0,0 +1,74 @@
import { requireOptionalNativeModule } from "expo"
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity } from "expo-app-intents"
type MusicIntentModule = {
replaceCatalog: (kind: string, json: string) => Promise<void>
catalogJSON: (kind: string) => Promise<string>
completeInvocation: (token: string, error: string | null) => Promise<void>
}
const native = requireOptionalNativeModule<MusicIntentModule>("AppIntentsSetup")
export type MusicEntityKind = "song" | "album" | "artist" | "playlist"
const SPOTLIGHT_LIMIT = 100
let publishing: Promise<void> = Promise.resolve()
let catalogSync: Promise<void> = Promise.resolve()
export function trackMusicCatalogSync(sync: Promise<void>) {
catalogSync = sync
}
/** Serialize writes so sign-out cleanup cannot be overtaken by an older publish. */
export function publishMusicCatalog(kind: string, entities: AppIntentEntity[]) {
const write = publishing
.catch(() => undefined)
.then(async () => {
if (!native || !AppIntents.isAvailable()) return
const oldDiskCatalog = JSON.parse(
await native.catalogJSON(kind)
) as AppIntentEntity[]
const suggestions = [...entities]
.sort(
(a, b) =>
Number(b.metadata?.favorite === "1") -
Number(a.metadata?.favorite === "1")
)
.slice(0, SPOTLIGHT_LIMIT)
const previous = await AppIntents.getEntityCatalogAsync(kind)
const remaining = new Set(entities.map((entity) => entity.id))
for (const entity of new Map(
[...oldDiskCatalog, ...previous].map((item) => [item.id, item])
).values()) {
if (!remaining.has(entity.id) && kind !== "nowPlaying") {
await AppIntents.deleteDonationsAsync({ entity: kind, id: entity.id })
}
}
await native.replaceCatalog(kind, JSON.stringify(entities))
await AppIntents.setEntityCatalogAsync(kind, suggestions)
})
publishing = write
return write
}
export async function completeMusicInvocation(
token: string,
error: string | null
) {
await native?.completeInvocation(token, error)
}
/** Donation errors never turn a completed user action into a playback failure. */
export async function donateMusicPlay(
kind: MusicEntityKind,
id: string,
shuffle = false
) {
if (!native || !AppIntents.isAvailable()) return
try {
await catalogSync
await publishing
await AppIntents.donateIntentAsync("playMusic", { kind, id, shuffle })
} catch (error) {
console.warn("Could not donate music playback.", error)
}
}
@@ -1,96 +0,0 @@
const { createRequire } = require("module");
const fs = require("fs");
const path = require("path");
const expoRequire = createRequire(require.resolve("expo/package.json"));
const { withAppDelegate, withXcodeProject } = expoRequire("@expo/config-plugins");
const { createBuildSourceFile } = expoRequire(
"@expo/config-plugins/build/ios/XcodeProjectFile",
);
const { getProjectName } = expoRequire(
"@expo/config-plugins/build/ios/utils/Xcodeproj",
);
const REGISTER_CALL = "MusicAppIntents.register()";
/**
* Current Expo prebuild adds `experiments.inlineModules.watchedDirectories`
* to the app target. Copying those Swift files again would compile them twice.
* Older toolchains that do not do this still need the files in the app target,
* plus an AppDelegate call, because the setup module is not registered for them.
*/
function prebuildCompilesWatchedDirectories() {
try {
const cliRequire = createRequire(expoRequire.resolve("@expo/cli/package.json"));
cliRequire.resolve("@expo/inline-modules/package.json");
return true;
} catch {
return false;
}
}
function swiftSources(projectRoot) {
const sourceDir = path.join(projectRoot, "app-intents");
return fs
.readdirSync(sourceDir)
.filter((file) => file.endsWith(".swift"))
.sort()
.map((file) => ({
file,
contents: fs.readFileSync(path.join(sourceDir, file), "utf8"),
}));
}
function withCopiedAppIntentSources(config) {
return withXcodeProject(config, (mod) => {
const projectRoot = mod.modRequest.projectRoot;
const projectName = getProjectName(projectRoot);
fs.mkdirSync(path.join(mod.modRequest.platformProjectRoot, projectName), {
recursive: true,
});
for (const source of swiftSources(projectRoot)) {
mod.modResults = createBuildSourceFile({
project: mod.modResults,
nativeProjectRoot: mod.modRequest.platformProjectRoot,
filePath: path.join(projectName, source.file),
fileContents: source.contents,
overwrite: true,
});
}
return mod;
});
}
function withAppIntentRegistration(config) {
return withAppDelegate(config, (mod) => {
if (mod.modResults.language !== "swift") {
throw new Error(
"oTunes App Intents need the Swift AppDelegate so Siri shortcuts can refresh.",
);
}
if (!mod.modResults.contents.includes(REGISTER_CALL)) {
const anchor = "let delegate = ReactNativeDelegate()";
if (!mod.modResults.contents.includes(anchor)) {
throw new Error(
"AppDelegate.swift is missing its React Native startup, so App Intents could not be registered.",
);
}
mod.modResults.contents = mod.modResults.contents.replace(
anchor,
`${REGISTER_CALL}\n ${anchor}`,
);
}
return mod;
});
}
function withAppIntentSources(config) {
if (prebuildCompilesWatchedDirectories()) {
return config;
}
return withAppIntentRegistration(withCopiedAppIntentSources(config));
}
module.exports = withAppIntentSources;
@@ -0,0 +1,85 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const fs = require("node:fs")
const vm = require("node:vm")
const ts = require("typescript")
const values = require("convex/values")
function playlistMutation() {
const module = { exports: {} }
const source = ts.transpileModule(
fs.readFileSync(`${__dirname}/playlists.ts`, "utf8"),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}
).outputText
vm.runInNewContext(source, {
module,
exports: module.exports,
require: (name) => {
if (name === "convex/server") return { paginationOptsValidator: {} }
if (name === "convex/values") return values
if (name === "@convex-dev/auth/server")
return { getAuthUserId: async (ctx) => ctx.userId }
if (name === "./_generated/server")
return {
mutation: (definition) => definition,
query: (definition) => definition,
}
if (name === "./library") return {}
throw new Error(`Unexpected import ${name}`)
},
})
return module.exports.createPlaylist.handler
}
test("a delivered playlist request is committed once per account", async () => {
const createPlaylist = playlistMutation()
const tables = { playlists: [], appIntentPlaylists: [] }
const db = {
insert: async (table, document) => {
const id = `${table}-${tables[table].length + 1}`
tables[table].push({ ...document, _id: id })
return id
},
query: (table) => ({
withIndex: (_name, configure) => {
const filters = []
const index = {
eq: (key, value) => {
filters.push([key, value])
return index
},
}
configure(index)
return {
unique: async () =>
tables[table].find((row) =>
filters.every(([key, value]) => row[key] === value)
) ?? null,
}
},
}),
}
const request = {
name: "Test playlist",
songIds: ["song-1"],
invocationId: "request-1",
}
const first = await createPlaylist({ userId: "user-1", db }, request)
const replay = await createPlaylist({ userId: "user-1", db }, request)
assert.equal(first, replay)
assert.equal(tables.playlists.length, 1)
const otherAccount = await createPlaylist({ userId: "user-2", db }, request)
assert.notEqual(otherAccount, first)
assert.equal(tables.playlists.length, 2)
const nextRequest = await createPlaylist(
{ userId: "user-1", db },
{ ...request, invocationId: "request-2" }
)
assert.notEqual(nextRequest, first)
assert.equal(tables.playlists.length, 3)
})
+105 -150
View File
@@ -1049,170 +1049,125 @@ export const getSongsForPlayback = query({
const FAVORITES_PLAYLIST_ID = "otunes:favorites"
/**
* Compact library snapshot for Siri and Shortcuts. Playback still loads songs
* through the existing queries; this only carries the names those pickers match.
*/
export const intentCatalog = query({
args: {},
handler: async (ctx) => {
/** Bounded pages for the device's disk-backed Siri catalog. */
export const intentCatalogPage = query({
args: {
kind: v.union(
v.literal("song"),
v.literal("album"),
v.literal("artist"),
v.literal("playlist")
),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { kind, paginationOpts }) => {
const userId = await getAuthUserId(ctx)
const user = userId ? await ctx.db.get("users", userId) : null
const favoriteSongs = new Set(user?.favoriteSongs ?? [])
const favoriteAlbums = new Set(user?.favoriteAlbums ?? [])
const favoriteArtists = new Set(user?.favoriteArtists ?? [])
const artistDocs = await ctx.db.query("artists").collect()
const albumDocs = await ctx.db.query("albums").collect()
const songDocs = await ctx.db.query("songs").collect()
const artistMap = new Map(artistDocs.map((artist) => [artist._id, artist]))
const albumMap = new Map(albumDocs.map((album) => [album._id, album]))
const artists = artistDocs
.flatMap((artist) => {
const name = artist.name.trim()
if (!name) {
return []
}
return [
{
id: artist._id,
name,
favorite: favoriteArtists.has(artist._id),
},
]
})
.sort((a, b) => a.name.localeCompare(b.name))
const albums = albumDocs
.flatMap((album) => {
const artist = artistMap.get(album.artist)
const title = album.name.trim()
if (!artist || !title) {
return []
}
return [
{
id: album._id,
title,
artistName: artist.name,
artistId: artist._id,
year: album.year,
favorite: favoriteAlbums.has(album._id),
},
]
})
.sort((a, b) => a.title.localeCompare(b.title))
const songs = songDocs
.flatMap((song) => {
const album = albumMap.get(song.album)
const artist = artistMap.get(song.albumArtist)
const title = song.name.trim()
if (!album || !artist || !title) {
return []
}
if (!userId) throw new ConvexError("Not authenticated")
const user = await ctx.db.get("users", userId)
if (!user) throw new ConvexError("Not authenticated")
type Entity = {
id: string
title: string
subtitle?: string
synonyms?: string[]
metadata: Record<string, string>
}
const page: Entity[] = []
if (kind === "song") {
const result = await ctx.db.query("songs").paginate(paginationOpts)
for (const song of result.page) {
const album = await ctx.db.get("albums", song.album)
const artist = await ctx.db.get("artists", song.albumArtist)
if (!album || !artist || !song.name.trim()) continue
const credits = getSongArtistNames(song.artist)
const artistName = songArtistsMatchAlbum(credits, artist.name)
? artist.name
: credits.join(", ") || artist.name
return [
{
id: song._id,
title,
page.push({
id: song._id,
title: song.name,
subtitle: artistName,
metadata: {
artistName,
artistId: artist._id,
albumTitle: album.name,
albumId: album._id,
durationSeconds: Math.max(0, Math.round(song.length)),
genres: song.genres
.map((genre) => genre.trim())
.filter((genre) => genre.length > 0)
.join(", "),
year: song.year,
track: song.track,
diskNumber: song.diskNumber,
favorite: favoriteSongs.has(song._id),
duration: String(Math.max(0, Math.round(song.length))),
genres: song.genres.join(", "),
year: String(song.year),
track: String(song.track),
disk: String(song.diskNumber ?? ""),
favorite: user.favoriteSongs?.includes(song._id) ? "1" : "0",
},
]
})
.sort((a, b) => a.title.localeCompare(b.title))
const playlists: {
id: string
title: string
ownerName: string
createdByMe: boolean
songCount: number
}[] = []
if (user) {
const owned = await ctx.db
.query("playlists")
.withIndex("by_owner", (q) => q.eq("owner", user._id))
.collect()
const ownedIds = new Set(owned.map((playlist) => playlist._id))
const subscribed = (
await Promise.all(
(user.subscribedPlaylists ?? [])
.filter((playlistId) => !ownedIds.has(playlistId))
.map((playlistId) => ctx.db.get("playlists", playlistId))
)
).flatMap((playlist) => (playlist ? [playlist] : []))
const subscribedOwners = await Promise.all(
[...new Set(subscribed.map((playlist) => playlist.owner))].map(
(ownerId) => ctx.db.get("users", ownerId)
)
)
const ownerNames = new Map(
subscribedOwners.flatMap((owner) =>
owner ? [[owner._id, owner.name] as const] : []
)
)
playlists.push({
id: FAVORITES_PLAYLIST_ID,
title: "Favorites",
ownerName: user.name,
createdByMe: true,
songCount: user.favoriteSongs?.length ?? 0,
})
const listed = [
...owned.map((playlist) => ({
playlist,
createdByMe: true,
ownerName: user.name,
})),
...subscribed.map((playlist) => ({
playlist,
createdByMe: false,
ownerName: ownerNames.get(playlist.owner) ?? "",
})),
].sort((a, b) => a.playlist.name.localeCompare(b.playlist.name))
for (const entry of listed) {
const title = entry.playlist.name.trim()
if (!title) {
continue
}
playlists.push({
id: entry.playlist._id,
title,
ownerName: entry.ownerName,
createdByMe: entry.createdByMe,
songCount: entry.playlist.songs.length,
})
}
return { ...result, page }
}
return {
authenticated: user !== null,
songs,
albums,
artists,
playlists,
if (kind === "album") {
const result = await ctx.db.query("albums").paginate(paginationOpts)
for (const album of result.page) {
const artist = await ctx.db.get("artists", album.artist)
if (!artist || !album.name.trim()) continue
page.push({
id: album._id,
title: album.name,
subtitle: artist.name,
metadata: {
artistName: artist.name,
artistId: artist._id,
year: String(album.year),
favorite: user.favoriteAlbums?.includes(album._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
if (kind === "artist") {
const result = await ctx.db.query("artists").paginate(paginationOpts)
for (const artist of result.page) {
if (artist.name.trim())
page.push({
id: artist._id,
title: artist.name,
metadata: {
favorite: user.favoriteArtists?.includes(artist._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
const result = await ctx.db.query("playlists").paginate(paginationOpts)
for (const playlist of result.page) {
if (
playlist.owner !== userId &&
!user.subscribedPlaylists?.includes(playlist._id)
)
continue
if (!playlist.name.trim()) continue
const owner = await ctx.db.get("users", playlist.owner)
page.push({
id: playlist._id,
title: playlist.name,
subtitle: owner?.name ?? "",
metadata: {
ownerName: owner?.name ?? "",
createdByMe: playlist.owner === userId ? "1" : "0",
},
})
}
if (paginationOpts.cursor === null)
page.unshift({
id: FAVORITES_PLAYLIST_ID,
title: "Favorites",
synonyms: [
"favorites",
"favourite songs",
"liked songs",
"loved songs",
],
metadata: { ownerName: user.name, createdByMe: "1" },
})
return { ...result, page }
},
})
+19 -1
View File
@@ -358,10 +358,21 @@ export const getPlaylistSongIds = query({
export const createPlaylist = mutation({
args: {
name: v.string(),
invocationId: v.optional(v.string()),
songIds: v.optional(v.array(v.id("songs"))),
},
handler: async (ctx, args) => {
const userId = await requireAuth(ctx)
const invocationId = args.invocationId
if (invocationId) {
const previous = await ctx.db
.query("appIntentPlaylists")
.withIndex("by_user_invocation", (q) =>
q.eq("userId", userId).eq("invocationId", invocationId)
)
.unique()
if (previous) return previous.playlistId
}
const trimmed = args.name.trim()
if (!trimmed) {
throw new ConvexError("Playlist name cannot be empty")
@@ -369,12 +380,19 @@ export const createPlaylist = mutation({
const uniqueSongIds = [...new Set(args.songIds ?? [])]
return await ctx.db.insert("playlists", {
const playlistId = await ctx.db.insert("playlists", {
name: trimmed,
owner: userId,
cover: null,
songs: uniqueSongIds,
})
if (args.invocationId)
await ctx.db.insert("appIntentPlaylists", {
userId,
invocationId: args.invocationId,
playlistId,
})
return playlistId
},
})
+5
View File
@@ -40,6 +40,11 @@ export default defineSchema({
})
.index("signature", ["signature"])
.index("sessionId", ["sessionId"]),
appIntentPlaylists: defineTable({
userId: v.id("users"),
invocationId: v.string(),
playlistId: v.id("playlists"),
}).index("by_user_invocation", ["userId", "invocationId"]),
appSettings: defineTable({
disableAccountCreation: v.boolean(),
}),