Integrating your music app with Apple Intelligence
At a glance
| Item | Summary |
|---|---|
| Purpose | Adopt the audio and clock schemas so people can play music and set alarms with Siri. |
| App architecture | A Swift sample bundle with entry-bearing project variants CosmoTunes, CosmoTunesWidget, each leading to AppIntents APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Builder, Actor isolation |
| Project style | 117 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, @MainActor, Task closure isolated to MainActor, Task, Sendable or @Sendable; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | AppIntents, SwiftUI, Foundation, SwiftData, OSLog; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── CosmoTunes/
│ ├── CosmoTunesApp.swift
│ ├── Managers/
│ │ ├── AudioEngine.swift
│ │ ├── ModelManager.swift
│ │ ├── TrackAudioRenderer.swift
│ │ ├── CosmoTunesMediaSession.swift
│ │ ├── MIDIGenerationManager.swift
│ │ ├── NavigationManager.swift
│ │ └── UserPreferencesManager.swift
│ └── Views/
│ ├── Alarms/
│ │ └── AlarmsView.swift
│ └── Library/
│ ├── LibrarySegments.swift
│ └── PlaylistDetailView.swift
└── CosmoTunesWidget/
└── CosmoTunesWidgetBundle.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 6 project/configuration file(s) and 172 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["CosmoTunes"]
V2["CosmoTunesWidget"]
Boundary["AppIntents APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
CosmoTunes/CosmoTunesApp.swift:14 — architecture anchor
@main
struct CosmoTunesApp: App {
// ...
let model: ModelManager
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
AffinitySnippetView o-- SongEntity : song
AffinitySnippetView *-- Bool : isLiked
AffinitySnippetView *-- GeneratedTrack : track
PlaylistSnippetView o-- PlaylistEntity : playlist
Ownership evidence
CosmoTunes/AppIntents/Audio/Snippets/AffinitySnippetView.swift:17 — stored dependency or nearest verified ownership anchor
struct AffinitySnippetView: View {
// ...
let song: SongEntity
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AffinitySnippetView |
SongEntity (song) |
stores or receives | Initialized by the owner; the binding is immutable |
AffinitySnippetView |
Bool (isLiked) |
owns value state | Initialized by the owner; the binding is immutable |
AffinitySnippetView |
GeneratedTrack (track) |
creates and retains | Initialized by the owner; the binding is immutable |
PlaylistSnippetView |
PlaylistEntity (playlist) |
stores or receives | Initialized by the owner; the binding is immutable |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:44 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | CosmoTunes/AppIntents/Audio/Intents/AddToLibraryIntent.swift:29 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | CosmoTunes/AppIntents/Clock/Alarms/Intents/DeleteAlarmIntent.swift:44 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | CosmoTunes/AppIntents/Clock/Alarms/Intents/DeleteAlarmIntent.swift:44 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | CosmoTunes/GenerableTypes/MIDINoteSequence.swift:12 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:44 — representative execution boundary
func displayRepresentation(with components: DisplayRepresentation.Components) async -> DisplayRepresentation {
// ...
subtitle: "\(artistName)",
image: components.contains(.image) ? artworkImage : nil,
synonyms: synonyms
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | @Observable |
Observation macro publishes source-visible changes. | CosmoTunes/Managers/AlarmSchedulingManager.swift:26 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | CosmoTunes/Views/Alarms/AddAlarmView.swift:16 |
| Source import | AppIntents |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:7 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/AppIntents/Audio/Intents/AddToLibraryIntent.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:9 |
| Source import | SwiftData |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/CosmoTunesApp.swift:12 |
| Source import | OSLog |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/AppIntents/Audio/Intents/PlayAudioIntent.swift:9 |
| Source import | CoreSpotlight |
The cited file imports this module; runtime use and architectural role are not inferred. | CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:8 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
CosmoTunes/Managers/CoreSpotlightWrapper.swift:136 — representative type boundary
protocol SpotlightWrapping: Sendable {
// ...
func fetchLastClientState() async -> Data?
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CosmoTunesApp |
Application entry and top-level composition | App |
AudioEngine |
Owns processing or simulation work | Concrete collaborators/imported frameworks |
MIDIFileBuilder |
Incrementally constructs a framework value or graph | Concrete collaborators/imported frameworks |
ModelManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
AlarmsView |
User-interface presentation and input forwarding | View |
AlarmsListView |
User-interface presentation and input forwarding | View |
TrackAudioRenderer |
Owns drawing, GPU, or presentation processing | Concrete collaborators/imported frameworks |
TracksSegmentView |
User-interface presentation and input forwarding | View |
PlaylistsSegmentView |
User-interface presentation and input forwarding | View |
AlbumsSegmentView |
User-interface presentation and input forwarding | View |
The source explicitly defines local protocol relationships: CoreSpotlightWrapper → SpotlightWrapping.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
artworkImage (CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:57) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
artworkImage (CosmoTunes/AppIntents/Audio/Entities/PlaylistEntity.swift:55) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
backingMIDINotes (CosmoTunes/AppIntents/Audio/Entities/SongEntity.swift:39) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
backingTempo (CosmoTunes/AppIntents/Audio/Entities/SongEntity.swift:42) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift:57 — representative boundary
private var artworkImage: DisplayRepresentation.Image {
DisplayRepresentation.Image(systemName: "music.note.square.stack")
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | CosmoTunesApp |
The source’s App suffix makes this role explicit. |
| Incrementally constructs a framework value or graph | MIDIFileBuilder |
The source’s Builder suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | PlaybackController, TimerController |
The source’s Controller suffix makes this role explicit. |
| Owns processing or simulation work | AudioEngine |
The source’s Engine suffix makes this role explicit. |
| Long-lived feature or framework coordination | AlarmSchedulingManager, DonationManager, MIDIGenerationManager, ModelManager |
The source’s Manager suffix makes this role explicit. |
| Supplies a capability or framework resource | Provider |
The source’s Provider suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | ArtworkRenderer, TrackAudioRenderer |
The source’s Renderer suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | CosmoTunes/Managers/PlaybackController.swift:16 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | CosmoTunes/Managers/CoreSpotlightWrapper.swift:250 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Builder | CosmoTunes/Managers/AudioEngine.swift:380 |
A builder-named type owns incremental construction. |
| Actor isolation | CosmoTunes/Managers/MIDIGenerationManager.swift:17 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
participant ModelManager
participant Task
participant indexer
participant IntentDonationManager
ModelManager->>Task: Start asynchronous work
ModelManager->>indexer: apply()
ModelManager->>IntentDonationManager: deleteDonations()
ModelManager->>ModelManager: renderer()
Reference code
CosmoTunes/Managers/ModelManager.swift:604 — deleteTracks()
@MainActor
@Observable
final class ModelManager {
// ...
try await IntentDonationManager.shared.deleteDonations(matching: .entityIdentifiers(songIDs))
// ...
}Naming conventions
- Types: App: CosmoTunesApp; Builder: MIDIFileBuilder; Controller: PlaybackController, TimerController; Engine: AudioEngine; Manager: AlarmSchedulingManager, DonationManager, MIDIGenerationManager, ModelManager, NavigationManager; Provider: Provider; Renderer: ArtworkRenderer, TrackAudioRenderer; Session: CosmoTunesMediaSession, Session.
- Protocols:
SpotlightWrapping. - Methods:
configureAudioSession,setupEngine,loadAndPlay,pause,resume,stop,resetEngineState,seek. - Files:
CosmoTunes/CosmoTunesApp.swift,CosmoTunesWidget/CosmoTunesWidgetBundle.swift,CosmoTunes/Managers/AudioEngine.swift,CosmoTunes/Managers/ModelManager.swift,CosmoTunes/Views/Alarms/AlarmsView.swift,CosmoTunes/Managers/TrackAudioRenderer.swift.
Architecture takeaways
CosmoTunesAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches AppIntents, SwiftUI, SwiftData, CoreSpotlight through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
CosmoTunes/CosmoTunesApp.swift |
Cited implementation, SwiftData, CosmoTunesApp |
CosmoTunes/AppIntents/Audio/Snippets/AffinitySnippetView.swift |
Cited implementation, AffinitySnippetView |
CosmoTunes/Managers/CoreSpotlightWrapper.swift |
SpotlightWrapping, Cited implementation, SpotlightStateHelper, SpotlightChange, CoreSpotlightWrapper |
CosmoTunes/AppIntents/Audio/Entities/AlbumEntity.swift |
Cited implementation, async declaration or closure, AppIntents, Foundation, CoreSpotlight, AlbumEntity, AlbumQuery |
CosmoTunes/AppIntents/Audio/Entities/PlaylistEntity.swift |
Cited implementation, PlaylistEntity, PlaylistQuery |
CosmoTunes/AppIntents/Audio/Entities/SongEntity.swift |
Cited implementation, SongEntity, SongTransferError, SongQuery |
CosmoTunes/Managers/PlaybackController.swift |
PlaybackController |
CosmoTunes/Managers/AudioEngine.swift |
MIDIFileBuilder, AudioEngine, ScheduledEvent, MIDIEvent, MIDIFileBuilderError |
CosmoTunes/Managers/MIDIGenerationManager.swift |
MIDIGenerationManager, MIDIGenerationError |
CosmoTunes/AppIntents/Audio/Intents/AddToLibraryIntent.swift |
@MainActor, SwiftUI, AddToLibraryIntent |
CosmoTunes/AppIntents/Clock/Alarms/Intents/DeleteAlarmIntent.swift |
Task closure isolated to MainActor, Task, DeleteAlarmIntent |
CosmoTunes/GenerableTypes/MIDINoteSequence.swift |
Sendable or @Sendable, MIDINoteSequence, MIDINote, MIDINoteData |
CosmoTunes/Managers/AlarmSchedulingManager.swift |
@Observable, AlarmSchedulingManager |
CosmoTunes/Views/Alarms/AddAlarmView.swift |
SwiftUI state property wrapper, AddAlarmView |
CosmoTunes/AppIntents/Audio/Intents/PlayAudioIntent.swift |
OSLog, PlayAudioIntent, AudioIntentError |
CosmoTunesWidget/CosmoTunesWidgetBundle.swift |
CosmoTunesWidgetBundle |
CosmoTunes/Managers/ModelManager.swift |
deleteTracks |