# hicozyty/callkitassistant architecture

> Architecture diagram of the GitHub repository hicozyty/callkitassistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hicozyty/callkitassistant
- Repository: https://github.com/hicozyty/callkitassistant

## Overview

1. **Product shape and runtime**
   - This is a **watchOS SwiftUI VoIP client** built in Xcode, not a server or a generic app.
   - The app’s execution boundary is the watch device: UI, call control, audio capture/playback, encryption, and UDP transport all happen locally on-device.

2. **Top-level app entry**
   - `callkitAssistantApp.swift` is the `@main` entry point that wires the app together.
   - The app is organized as a small set of managers rather than a deep view hierarchy, so the architecture is centered on service objects.

3. **UI and user-facing state**
   - `ContentView.swift` is the main UI surface for enrollment, call start/stop, server status, and debug actions.
   - It is the only obvious presentation layer; it delegates almost all real work to managers instead of containing call or networking logic.

4. **Call orchestration boundary**
   - `CallManager.swift` owns **CallKit** integration via `CXProvider` and `CXCallController`.
   - Its role is to translate user actions and call lifecycle events into system call behavior, especially audio session activation (`didActivate`) and call state changes.

5. **Audio pipeline**
   - `AudioStreamManager.swift` is the media subsystem: it manages `AVAudioEngine`, voice-processing I/O, playback, and capture.
   - It also handles **Voice Activity Detection** using `SoundAnalysis`, plus the pre-roll buffer and hangover timer that prevent clipping at speech boundaries.
   - This is where raw audio becomes encoded frames and where decrypted frames become speaker output.

6. **Codec and media flow**
   - The app uses **Opus** through the `swift-opus` package for low-latency speech compression.
   - The audio flow is: microphone input → VAD gate → Opus encode → encryption → UDP send; inbound packets reverse that path → decrypt → Opus decode → playback.

7. **Security and device identity**
   - `SecurityManager.swift` is the security subsystem and the most important trust boundary.
   - It handles device enrollment, Secure Enclave-backed key generation, CSR creation, certificate storage, and **mTLS** setup for authenticated connections.
   - It also manages session key establishment and **AES-GCM** encryption/decryption of audio payloads, so the transport layer never sees plaintext audio.

8. **Transport layer**
   - `UDPManager.swift` owns the **network data plane** using `NWConnection` over UDP.
   - It is responsible for sending/receiving audio packets, heartbeat traffic, and connection viability monitoring.
   - This is separate from the call UI and separate from the encryption logic; it simply moves authenticated encrypted frames between device and server.

9. **Configuration and environment**
   - `AppConfig.swift` centralizes server addressing and debug-time overrides.
   - The README indicates the app reads values such as `serverURL`, `localHostname`, and `enrollSecret` from `test-Watch-App-Info.plist`, which is effectively the environment/config boundary for server discovery and enrollment.

10. **Data models and app-wide extensions**
   - `Models.swift` contains Codable request/response types used for server enrollment and related API responses.
   - `Extensions.swift` appears to hold shared utility extensions, likely including notification names used to coordinate managers without tight coupling.

11. **External services and trust assumptions**
   - The app talks to a **Bun-based server** for enrollment and call/media signaling support.
   - Enrollment is a one-time provisioning flow: device generates a key in Secure Enclave, sends a CSR plus shared secret to `/enroll`, and receives a signed client certificate for future mTLS sessions.
   - The committed `ca.crt` is used to verify the server’s certificate chain.

12. **System integrations and platform constraints**
   - The project depends heavily on **CallKit**, **AVFoundation**, **SoundAnalysis**, **Network**, **SwiftUI**, and Apple’s certificate/crypto packages.
   - The Info.plist/build settings enable watch-specific behaviors like local network access, microphone access, Bonjour discovery, and `voip` background mode, which materially shape the app’s runtime model.

13. **Operational modes and debugging**
   - The README calls out a **simulator mode** that bypasses CallKit and exercises audio directly, so the audio pipeline can be tested independently of system calling behavior.
   - There is also a debug hostname override and a “reset enrollment” path, which indicate the app is designed to be re-provisioned and tested against changing servers.

14. **Overall architecture to diagram**
   - The clearest diagram is a layered flow: **SwiftUI UI → CallKit controller → audio engine → security/encryption → UDP transport → remote server**.
   - The key architectural boundary is that **call control, media processing, and secure transport are separate subsystems**, coupled only through manager interfaces and shared state/events.

Main entry points: [`callkitAssistant App/SecurityManager.swift`](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/SecurityManager.swift) (Security manager), [`callkitAssistant App/AudioStreamManager.swift`](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/AudioStreamManager.swift) (Audio stream), [`callkitAssistant App/CallManager.swift`](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/CallManager.swift) (Call manager), [`callkitAssistant App/callkitAssistantApp.swift`](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/callkitAssistantApp.swift) (App entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App shell"]
  node_callkitassistantapp(("App entry"))
  node_contentview["Main UI<br/>SwiftUI view<br/>[ContentView.swift]"]
  node_callmanager["Call manager<br/>CallKit coordinator<br/>[CallManager.swift]"]
  node_appconfig["App config<br/>configuration<br/>[AppConfig.swift]"]
  node_models["Models<br/>codable types<br/>[Models.swift]"]
  node_extensions["Extensions<br/>shared helpers<br/>[Extensions.swift]"]
  node_info_plist["Env plist<br/>runtime config"]
end

subgraph group_media["Media pipeline"]
  node_audiostreammanager[("Audio stream<br/>audio engine")]
end

subgraph group_security["Security"]
  node_securitymanager{{"Security manager<br/>identity + crypto"}}
  node_ca_crt["CA cert<br/>trust anchor<br/>[ca.crt]"]
end

subgraph group_network["Transport"]
  node_udpmanager["UDP manager<br/>network transport<br/>[UDPManager.swift]"]
end

subgraph group_platform["Platform"]
  node_opussdk{{"Opus codec<br/>external package"}}
  node_callkit["CallKit<br/>system framework"]
  node_avfoundation["AVFoundation<br/>system framework"]
  node_soundanalysis["SoundAnalysis<br/>system framework"]
  node_networkframework["Network<br/>system framework"]
  node_server[("Bun server<br/>remote service")]
end

node_callkitassistantapp -->|"hosts"| node_contentview
node_callkitassistantapp -->|"wires"| node_callmanager
node_callkitassistantapp -->|"wires"| node_securitymanager
node_callkitassistantapp -->|"wires"| node_udpmanager
node_contentview -->|"controls"| node_callmanager
node_contentview -->|"enrolls"| node_securitymanager
node_callmanager -->|"uses"| node_callkit
node_callmanager -->|"activates"| node_audiostreammanager
node_audiostreammanager -->|"uses"| node_avfoundation
node_audiostreammanager -->|"detects"| node_soundanalysis
node_audiostreammanager -->|"encodes/decodes"| node_opussdk
node_audiostreammanager -->|"protects"| node_securitymanager
node_securitymanager -->|"enrolls with"| node_server
node_securitymanager -->|"verifies"| node_ca_crt
node_securitymanager -->|"secures"| node_udpmanager
node_udpmanager -->|"sends"| node_networkframework
node_udpmanager -->|"exchanges"| node_server
node_appconfig -->|"reads"| node_info_plist
node_models -.->|"shapes"| node_server
node_extensions -.->|"supports"| node_contentview
node_extensions -.->|"supports"| node_callmanager
node_extensions -.->|"supports"| node_securitymanager

click node_callkitassistantapp "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/callkitAssistantApp.swift"
click node_contentview "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/ContentView.swift"
click node_callmanager "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/CallManager.swift"
click node_audiostreammanager "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/AudioStreamManager.swift"
click node_securitymanager "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/SecurityManager.swift"
click node_udpmanager "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/UDPManager.swift"
click node_appconfig "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/AppConfig.swift"
click node_models "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/Models.swift"
click node_extensions "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/Extensions.swift"
click node_info_plist "https://github.com/hicozyty/callkitassistant/blob/main/test-Watch-App-Info.plist"
click node_ca_crt "https://github.com/hicozyty/callkitassistant/blob/main/callkitAssistant App/ca.crt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_callkitassistantapp,node_contentview,node_callmanager,node_appconfig,node_models,node_extensions,node_info_plist toneBlue
class node_audiostreammanager toneAmber
class node_securitymanager,node_ca_crt toneMint
class node_udpmanager toneRose
class node_opussdk,node_callkit,node_avfoundation,node_soundanalysis,node_networkframework,node_server toneIndigo
```

## Components

### App shell

Watch app entry, UI, and shared app state

- [App entry](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/callkitAssistantApp.swift): watchOS app entry · Main app bootstrap and dependency wiring · `callkitAssistant App/callkitAssistantApp.swift`
- [Main UI](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/ContentView.swift): SwiftUI view · Enrollment, call controls, status, and debug actions · `callkitAssistant App/ContentView.swift`
- [Call manager](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/CallManager.swift): CallKit coordinator · Translates UI intent and call lifecycle into system call behavior · `callkitAssistant App/CallManager.swift`
- [App config](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/AppConfig.swift): configuration · Server addressing and debug-time overrides · `callkitAssistant App/AppConfig.swift`
- [Models](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/Models.swift): codable types · Enrollment and response request/response models · `callkitAssistant App/Models.swift`
- [Extensions](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/Extensions.swift): shared helpers · Cross-cutting utilities and app coordination helpers · `callkitAssistant App/Extensions.swift`
- [Env plist](https://github.com/hicozyty/callkitassistant/blob/main/test-Watch-App-Info.plist): runtime config · Server URL, hostname, and enrollment secret source · `test-Watch-App-Info.plist`

### Media pipeline

Call media capture, encoding, and playback

- [Audio stream](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/AudioStreamManager.swift): audio engine · Capture, playback, VAD gating, pre-roll, and hangover logic · `callkitAssistant App/AudioStreamManager.swift`

### Security

Provisioning, identity, and payload protection

- [Security manager](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/SecurityManager.swift): identity + crypto · Enrollment, mTLS, session keys, and AES-GCM protection · `callkitAssistant App/SecurityManager.swift`
- [CA cert](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/ca.crt): trust anchor · Server certificate chain verification root · `callkitAssistant App/ca.crt`

### Transport

UDP packet movement and heartbeat

- [UDP manager](https://github.com/hicozyty/callkitassistant/blob/HEAD/callkitAssistant%20App/UDPManager.swift): network transport · Sends and receives encrypted audio packets and heartbeats · `callkitAssistant App/UDPManager.swift`

### Platform

Apple frameworks and external server boundary

- **Opus codec**: external package · Low-latency speech compression and decompression
- **CallKit**: system framework · System call control and audio session activation
- **AVFoundation**: system framework · Audio engine, I/O, and playback APIs
- **SoundAnalysis**: system framework · Voice activity detection support
- **Network**: system framework · UDP connections and viability monitoring
- **Bun server**: remote service · Enrollment and media/signaling backend

## Connections

- App entry → Main UI: hosts · Bootstraps the main SwiftUI surface
- App entry → Call manager: wires · Creates and shares call orchestration state
- App entry → Security manager: wires · Initializes device identity and trust services
- App entry → UDP manager: wires · Connects transport layer services
- Main UI → Call manager: controls · Start/stop and enrollment actions flow through call control
- Main UI → Security manager: enrolls · Triggers provisioning and reset actions
- Call manager → CallKit: uses · Owns provider/controller integration and call lifecycle
- Call manager → Audio stream: activates · Coordinates audio session activation and call audio state
- Audio stream → AVFoundation: uses · Captures and renders audio through the engine
- Audio stream → SoundAnalysis: detects · Uses VAD to gate speech frames
- Audio stream → Opus codec: encodes/decodes · Compresses outbound audio and expands inbound frames
- Audio stream → Security manager: protects · Passes encoded frames for encryption and receives decrypted frames
- Security manager → Bun server: enrolls with · Creates CSR and provisions device certificate
- Security manager → CA cert: verifies · Uses CA certificate to validate server chain
- Security manager → UDP manager: secures · Provides session keys and payload protection for transport
- UDP manager → Network: sends · Implements UDP data plane over NWConnection
- UDP manager → Bun server: exchanges · Moves encrypted audio packets and heartbeats
- App config → Env plist: reads · Derives server and debug environment values
- Models → Bun server: shapes · Defines enrollment and response payloads
- Extensions → Main UI: supports · Shared coordination helpers used across managers
- Extensions → Call manager: supports · Cross-cutting utilities and notification wiring
- Extensions → Security manager: supports · Shared helper methods and app-wide events
