# marshaljlee/jxrouter architecture

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

- Interactive diagram: https://gitdiagram.com/marshaljlee/jxrouter
- Repository: https://github.com/marshaljlee/jxrouter

## Overview

#### Purpose and Entry Points

JXRouter is a native macOS SwiftUI/AppKit menu-bar proxy for redirecting Claude, OpenAI, Codex, and SDK API traffic to configured remote or local LLM providers. The application entry point is `Sources/JXRouter/JXRouterApp.swift`; its primary UI and lifecycle controls are in `Sources/JXRouter/JXRouterView.swift` and `Sources/JXRouter/StatusItemManager.swift`. Installation builds the Xcode project and installs CLI launchers via `install.sh`.

#### UI, Settings, and Persistent State

The settings UI configures providers, model-tier routes, application rules, proxy behavior, and certificate actions. `Sources/JXRouter/SettingsView.swift` presents this surface, while `Sources/JXRouter/ConfigManager.swift` owns non-secret configuration. API keys are isolated in the macOS Keychain through `Sources/JXRouter/KeychainManager.swift`; settings import may seed empty key slots from existing shell configuration. `Sources/JXRouter/ClaudeSettingsWriter.swift` manages the Claude Code loopback configuration boundary.

#### Proxy Lifecycle and System Integration

Starting the service creates a local HTTP/HTTPS proxy, defaulting to port 5255. `Sources/JXRouter/ProxyManager.swift` coordinates lifecycle and settings, and `Sources/JXRouter/ProxyServer.swift` accepts proxy traffic. Optional system-wide routing is delegated to macOS proxy settings through `Sources/JXRouter/SystemProxyManager.swift`; per-application policy is represented by `Sources/JXRouter/AppRouteRule.swift` and identified through `Sources/JXRouter/Services/AppIdentifier.swift`.

The design explicitly avoids DNS `/etc/hosts` changes and pf firewall rules. Files named `Sources/JXRouter/DNSRedirectionManager.swift` remain in the tree but are not part of the documented current routing approach.

#### Traffic Classification and Relay Boundary

Incoming requests are classified to distinguish targeted AI API traffic from ordinary connections by `Sources/JXRouter/RequestClassifier.swift`. Requests for supported AI hosts cross the interception/routing pipeline; unrelated HTTP(S) traffic is preserved as an unmodified raw relay through `Sources/JXRouter/NetworkRelay.swift`. HTTP parsing and outbound request mechanics are concentrated in `Sources/JXRouter/HTTPUtils.swift` and `Sources/JXRouter/CurlClient.swift`.

#### TLS Interception and Trust

TLS termination is restricted to the targeted AI API hosts and depends on a locally trusted CA. `Sources/JXRouter/CertificateAuthority.swift` creates/manages the CA, `Sources/JXRouter/PEMIdentityLoader.swift` loads identities, and `Sources/JXRouter/MITMHandler.swift` handles intercepted TLS sessions. Non-targeted TLS connections instead remain on the relay path; `Sources/JXRouter/DirectTLSHandler.swift` supports direct tunnel behavior.

#### Provider Routing and Protocol Translation

`Sources/JXRouter/ProviderRouter.swift` selects the active provider/model route and executes fallback chains. Provider definitions and validation are separated into `Sources/JXRouter/ProviderPresets.swift` and `Sources/JXRouter/ProviderValidator.swift`. The proxy adapts incoming API formats to provider-specific requests via `Sources/JXRouter/MessageTranslator.swift`, with request/response models in `Sources/JXRouter/MessagesRequest.swift` and `Sources/JXRouter/ProviderResponse.swift`; streaming responses are formatted through `Sources/JXRouter/SSEFormatter.swift`.

#### Local Models, External Providers, and Observability

Remote providers are external OpenAI- or Anthropic-compatible APIs authenticated by Keychain-held keys. Local backends are discovered and managed by `Sources/JXRouter/LocalProviderDetector.swift` and `Sources/JXRouter/LocalModelManager.swift`, covering Ollama and llama.cpp variants. Routing and operational events are recorded in `Sources/JXRouter/TrafficLog.swift`. The project has no third-party dependency runtime; it is built as a macOS Xcode application in `JXRouter.xcodeproj/project.pbxproj`.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["macOS App"]
  node_app_entry["JXRouter App<br/>SwiftUI entry point<br/>[JXRouterApp.swift]"]
  node_ui["Menu Bar UI<br/>SwiftUI/AppKit UI<br/>[JXRouterView.swift]"]
  node_status_item["Status Item<br/>menu-bar lifecycle"]
  node_settings["Settings<br/>[SettingsView.swift]"]
  node_config[("Config Manager<br/>non-secret state")]
  node_keychain[("macOS Keychain<br/>secret store")]
  node_claude_settings["Claude Settings Writer<br/>Claude Code boundary"]
  node_system_proxy["System Proxy Manager<br/>macOS network integration"]
end

subgraph group_proxy["Proxy Pipeline"]
  node_proxy_manager["Proxy Manager<br/>lifecycle coordinator<br/>[ProxyManager.swift]"]
  node_proxy_server["Local Proxy Server<br/>HTTP/HTTPS listener<br/>[ProxyServer.swift]"]
  node_classifier["Request Classifier<br/>traffic classifier"]
  node_mitm["Targeted TLS Interception<br/>MITM handler<br/>[MITMHandler.swift]"]
  node_certificate_authority["Local Certificate Authority<br/>CA management"]
  node_relay["Raw Relay &amp; Direct TLS<br/>pass-through transport<br/>[NetworkRelay.swift]"]
  node_http_client["HTTP &amp; Curl Client<br/>outbound transport<br/>[CurlClient.swift]"]
end

subgraph group_routing["Routing &amp; Providers"]
  node_provider_router["Provider Router<br/>route and fallback engine"]
  node_translator["Message Translator<br/>protocol adapter"]
  node_providers{{"Remote &amp; Local Providers<br/>LLM backends"}}
  node_traffic_log[("Traffic Log<br/>observability<br/>[TrafficLog.swift]")]
end

node_app_entry -->|"launches"| node_ui
node_ui -->|"controls"| node_status_item
node_ui -->|"opens"| node_settings
node_settings -->|"reads/writes"| node_config
node_settings -->|"manages keys"| node_keychain
node_settings -->|"configures"| node_claude_settings
node_settings -->|"starts/configures"| node_proxy_manager
node_proxy_manager -->|"runs"| node_proxy_server
node_proxy_manager -.->|"optional routing"| node_system_proxy
node_proxy_server -->|"classifies requests"| node_classifier
node_classifier -->|"targeted AI TLS"| node_mitm
node_classifier -->|"ordinary traffic"| node_relay
node_certificate_authority -->|"trust identity"| node_mitm
node_mitm -->|"intercepted API requests"| node_provider_router
node_provider_router -->|"selected route"| node_translator
node_translator -->|"provider request"| node_http_client
node_http_client -->|"calls"| node_providers
node_keychain -->|"API credentials"| node_providers
node_relay -->|"outbound relay"| node_http_client
node_provider_router -->|"routing events"| node_traffic_log
node_proxy_manager -->|"lifecycle events"| node_traffic_log

click node_app_entry "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/JXRouterApp.swift"
click node_ui "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/JXRouterView.swift"
click node_status_item "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/StatusItemManager.swift"
click node_settings "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/SettingsView.swift"
click node_config "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ConfigManager.swift"
click node_keychain "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/KeychainManager.swift"
click node_claude_settings "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ClaudeSettingsWriter.swift"
click node_proxy_manager "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProxyManager.swift"
click node_proxy_server "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProxyServer.swift"
click node_classifier "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/RequestClassifier.swift"
click node_mitm "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/MITMHandler.swift"
click node_certificate_authority "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/CertificateAuthority.swift"
click node_relay "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/NetworkRelay.swift"
click node_http_client "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/CurlClient.swift"
click node_provider_router "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProviderRouter.swift"
click node_translator "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/MessageTranslator.swift"
click node_providers "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/LocalModelManager.swift"
click node_system_proxy "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/SystemProxyManager.swift"
click node_traffic_log "https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/TrafficLog.swift"

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_app_entry,node_ui,node_status_item,node_settings,node_config,node_keychain,node_claude_settings,node_system_proxy toneBlue
class node_proxy_manager,node_proxy_server,node_classifier,node_mitm,node_certificate_authority,node_relay,node_http_client toneAmber
class node_provider_router,node_translator,node_providers,node_traffic_log toneMint
```

## Components

### macOS App

- [JXRouter App](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/JXRouterApp.swift): SwiftUI entry point · `Sources/JXRouter/JXRouterApp.swift`
- [Menu Bar UI](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/JXRouterView.swift): SwiftUI/AppKit UI · `Sources/JXRouter/JXRouterView.swift`
- [Status Item](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/StatusItemManager.swift): menu-bar lifecycle · `Sources/JXRouter/StatusItemManager.swift`
- [Settings](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/SettingsView.swift): settings UI · `Sources/JXRouter/SettingsView.swift`
- [Config Manager](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ConfigManager.swift): non-secret state · `Sources/JXRouter/ConfigManager.swift`
- [macOS Keychain](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/KeychainManager.swift): secret store · Stores provider API keys. · `Sources/JXRouter/KeychainManager.swift`
- [Claude Settings Writer](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ClaudeSettingsWriter.swift): Claude Code boundary · Maintains Claude Code loopback proxy configuration. · `Sources/JXRouter/ClaudeSettingsWriter.swift`
- [System Proxy Manager](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/SystemProxyManager.swift): macOS network integration · Optionally applies system-wide proxy settings. · `Sources/JXRouter/SystemProxyManager.swift`

### Proxy Pipeline

- [Proxy Manager](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProxyManager.swift): lifecycle coordinator · Starts and configures the local proxy. · `Sources/JXRouter/ProxyManager.swift`
- [Local Proxy Server](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProxyServer.swift): HTTP/HTTPS listener · Accepts local traffic on the configured port. · `Sources/JXRouter/ProxyServer.swift`
- [Request Classifier](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/RequestClassifier.swift): traffic classifier · Separates supported AI hosts from ordinary traffic. · `Sources/JXRouter/RequestClassifier.swift`
- [Targeted TLS Interception](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/MITMHandler.swift): MITM handler · Terminates TLS only for supported AI API hosts. · `Sources/JXRouter/MITMHandler.swift`
- [Local Certificate Authority](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/CertificateAuthority.swift): CA management · Creates and manages the locally trusted CA. · `Sources/JXRouter/CertificateAuthority.swift`
- [Raw Relay & Direct TLS](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/NetworkRelay.swift): pass-through transport · Relays unrelated HTTP(S) traffic without modification. · `Sources/JXRouter/NetworkRelay.swift`
- [HTTP & Curl Client](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/CurlClient.swift): outbound transport · `Sources/JXRouter/CurlClient.swift`

### Routing & Providers

- [Provider Router](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/ProviderRouter.swift): route and fallback engine · Selects provider/model routes and fallback chains. · `Sources/JXRouter/ProviderRouter.swift`
- [Message Translator](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/MessageTranslator.swift): protocol adapter · Transforms client API formats into provider requests and streaming responses. · `Sources/JXRouter/MessageTranslator.swift`
- [Remote & Local Providers](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/LocalModelManager.swift): LLM backends · OpenAI/Anthropic-compatible APIs plus Ollama and llama.cpp backends. · `Sources/JXRouter/LocalModelManager.swift`
- [Traffic Log](https://github.com/marshaljlee/jxrouter/blob/main/Sources/JXRouter/TrafficLog.swift): observability · Records routing and operational events. · `Sources/JXRouter/TrafficLog.swift`

## Connections

- JXRouter App → Menu Bar UI: launches
- Menu Bar UI → Status Item: controls
- Menu Bar UI → Settings: opens
- Settings → Config Manager: reads/writes
- Settings → macOS Keychain: manages keys
- Settings → Claude Settings Writer: configures
- Settings → Proxy Manager: starts/configures
- Proxy Manager → Local Proxy Server: runs
- Proxy Manager → System Proxy Manager: optional routing
- Local Proxy Server → Request Classifier: classifies requests
- Request Classifier → Targeted TLS Interception: targeted AI TLS
- Request Classifier → Raw Relay & Direct TLS: ordinary traffic
- Local Certificate Authority → Targeted TLS Interception: trust identity
- Targeted TLS Interception → Provider Router: intercepted API requests
- Provider Router → Message Translator: selected route
- Message Translator → HTTP & Curl Client: provider request
- HTTP & Curl Client → Remote & Local Providers: calls
- macOS Keychain → Remote & Local Providers: API credentials
- Raw Relay & Direct TLS → HTTP & Curl Client: outbound relay
- Provider Router → Traffic Log: routing events
- Proxy Manager → Traffic Log: lifecycle events
