# azookey/azookey-desktop architecture

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

- Interactive diagram: https://gitdiagram.com/azookey/azookey-desktop
- Repository: https://github.com/azookey/azookey-desktop

## Overview

#### Purpose and Deployment

azooKey-Desktop is a macOS Japanese Input Method Editor (IME) using Zenzai neural kana-kanji conversion. It is packaged as an installable input source, with bundled language-model and GGUF inference resources. The Xcode app target is `azooKeyMac/`, while shared conversion logic and a separate converter process live in the Swift package under `Core/`.

Primary app lifecycle entry point: `azooKeyMac/AppDelegate.swift`. Input-method interaction begins in `azooKeyMac/InputController/azooKeyMacInputController.swift`. Distribution builds/install through `install.sh` and `pkgbuild.sh`.

#### Process Split and IPC Boundary

Conversion is isolated in a `ConverterServer` executable rather than performed directly in the input-method extension. Server startup is `Core/Sources/ConverterServer/main.swift`; `ConverterSession.swift` owns per-client conversion state. The server exposes snapshot, settings, and key-event operations through:
- `Core/Sources/ConverterServer/ConverterServer+KeyEvent.swift`
- `Core/Sources/ConverterServer/ConverterServer+Snapshot.swift`
- `Core/Sources/Core/XPC/ConverterServerXPCProtocol.swift`

The input controller consumes this service through `azooKeyMac/InputController/ConverterServerClient.swift`. This XPC boundary separates macOS text-client/UI concerns from model-backed stateful conversion and allows the server to run as a managed background process.

#### Input Event and Composition Pipeline

The input controller receives macOS keyboard events, maps them to core key representations, forwards conversion-relevant events to the server, then applies returned composition/candidate state to the host application. Key event adaptation and controller behavior are centered in:
- `azooKeyMac/InputController/azooKeyMacInputController.swift`
- `azooKeyMac/InputController/NSEvent.swift`
- `Core/Sources/Core/InputUtils/KeyEventCore.swift`

Core state transitions distinguish client-facing actions from user-editing actions via `Core/Sources/Core/InputUtils/Actions/ClientAction.swift` and `Core/Sources/Core/InputUtils/Actions/UserAction.swift`. `Core/Sources/Core/InputUtils/InputState.swift` represents conversion/composition state; `SegmentsManager.swift` manages segmented text, predictions, candidates, corrections, and selection.

#### Conversion Engine and Resources

The conversion stack is backed by bundled Zenzai resources under `azooKeyMac/Resources/base_n5_lm` and `azooKeyMac/Resources/gguf` (with additional model material supplied via Git submodules/LFS). The Core package orchestrates input segmentation and candidate presentation rather than embedding model resources directly:
- `Core/Sources/Core/InputUtils/SegmentsManager.swift`
- `Core/Sources/Core/InputUtils/CandidatePresentationContext.swift`
- `Core/Sources/Core/InputUtils/InputLanguage.swift`

Romaji/AZIK and punctuation/direct-input behavior are mediated by `Core/Sources/Core/KeyMap/KeyMap.swift`, `Core/Sources/Core/InputUtils/OptionDirectInputResolver.swift`, and configurable punctuation handling.

#### Candidate UI and Text Commitment

Candidate rendering is native macOS UI owned by the input-method target. The controller translates server snapshots into candidate-window state and commits selected or transformed text back through the InputMethodKit client boundary:
- `azooKeyMac/InputController/CandidateWindow/BaseCandidateViewController.swift`
- `azooKeyMac/InputController/CandidateWindow/CandidateView.swift`
- `azooKeyMac/InputController/azooKeyMacInputController+SelectedTextTransform.swift`

Window placement spans AppKit/CoreGraphics in `azooKeyMac/InputController/WindowPositioning+CoreGraphics.swift`, with shared positioning logic in `Core/Sources/Core/Windows/WindowPositioning.swift`.

#### Configuration, Personalization, and Dictionaries

Shared typed configuration is defined in `Core/Sources/Core/Configs/Config.swift` and persisted through config item types; app-private secrets use Keychain-oriented `azooKeyMac/Configs/SecureConfigItem.swift`. User dictionary storage/compilation is represented by:
- `Core/Sources/Core/UserDictionary/CompiledUserDictionaryStore.swift`
- `Core/Sources/Core/UserDictionary/SystemUserDictionaryHelper.swift`

Native configuration and editing windows expose these concerns, including user dictionaries and custom romaji tables: `azooKeyMac/Windows/UserDictionaryEditorWindow.swift` and `azooKeyMac/Windows/RomajiTableEditorWindow.swift`.

#### Optional LLM “Magic Conversion”

A separate magic-conversion layer supports LLM-assisted transformations. It abstracts providers in `Core/Sources/Core/MagicConversion/AIBackend.swift`, with clients for Apple Foundation Models and OpenAI:
- `Core/Sources/Core/MagicConversion/FoundationModelsClient.swift`
- `Core/Sources/Core/MagicConversion/OpenAIClient.swift`

Prompt UI and history are app-side in `azooKeyMac/Windows/PromptInput/PromptInputWindow.swift` and `azooKeyMac/Windows/PromptInput/PromptInputView.swift`; provider credentials are expected to remain on the app/secure-config side rather than crossing ordinary conversion state flows.

Main entry points: [`azooKeyMac/InputController/azooKeyMacInputController.swift`](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/azooKeyMacInputController.swift) (Input controller), [`Core/Sources/ConverterServer/main.swift`](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/main.swift) (Converter server), [`azooKeyMac/InputController/ConverterServerClient.swift`](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/ConverterServerClient.swift) (Converter client), [`Core/Sources/ConverterServer/ConverterSession.swift`](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterSession.swift) (Conversion session)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["macOS Input Method"]
  node_app_delegate["App lifecycle<br/>AppDelegate<br/>[AppDelegate.swift]"]
  node_input_controller["Input controller<br/>InputMethodKit controller"]
  node_event_adapter["macOS event adapter<br/>NSEvent mapping<br/>[NSEvent.swift]"]
  node_xpc_client["Converter client<br/>XPC client"]
  node_candidate_ui["Candidate window<br/>native candidate UI"]
  node_text_commit["Text commit &amp; transform<br/>InputMethodKit output"]
  node_dictionary_editor["Dictionary editor<br/>native settings window"]
end

subgraph group_ipc["Converter Process"]
  node_converter_server{{"Converter server<br/>ConverterServer executable<br/>[main.swift]"}}
  node_converter_session["Conversion session<br/>per-client state"]
  node_xpc_protocol["Converter XPC protocol<br/>IPC contract"]
  node_key_events["Server key-event API<br/>XPC operation"]
  node_snapshots["Server snapshot API<br/>XPC operation"]
end

subgraph group_core["Core Conversion"]
  node_key_event_core["Core key events<br/>input representation<br/>[KeyEventCore.swift]"]
  node_actions_state["Actions &amp; input state<br/>composition state machine<br/>[InputState.swift]"]
  node_segments["Segments manager<br/>conversion orchestration"]
  node_keymap["Key map &amp; direct input<br/>romaji/AZIK mapping<br/>[KeyMap.swift]"]
  node_model_resources[("Zenzai model resources<br/>bundled LM and GGUF")]
end

subgraph group_data["Configuration &amp; Dictionaries"]
  node_config["Shared configuration<br/>typed config<br/>[Config.swift]"]
  node_user_dictionary[("Compiled user dictionary<br/>personal dictionary store")]
  node_secure_config["Secure credentials<br/>Keychain config"]
end

subgraph group_magic["Magic Conversion"]
  node_magic_backend["AI backend abstraction<br/>LLM provider interface<br/>[AIBackend.swift]"]
  node_openai_client["OpenAI provider<br/>AI backend client<br/>[OpenAIClient.swift]"]
  node_prompt_ui["Magic prompt UI<br/>native prompt window"]
end

node_app_delegate -->|"starts input method"| node_input_controller
node_input_controller -->|"adapts macOS events"| node_event_adapter
node_event_adapter -->|"maps to core event"| node_key_event_core
node_input_controller -->|"forwards conversion events"| node_xpc_client
node_xpc_client -->|"uses"| node_xpc_protocol
node_xpc_protocol -.->|"IPC boundary"| node_converter_server
node_converter_server -->|"creates"| node_converter_session
node_converter_server -->|"serves"| node_key_events
node_converter_server -->|"serves"| node_snapshots
node_key_events -->|"consumes"| node_key_event_core
node_converter_session -->|"updates"| node_actions_state
node_actions_state -->|"drives"| node_segments
node_keymap -->|"interprets input"| node_key_event_core
node_segments -->|"uses conversion resources"| node_model_resources
node_snapshots -->|"returns composition state"| node_xpc_client
node_xpc_client -->|"updates candidates"| node_candidate_ui
node_input_controller -->|"commits selected text"| node_text_commit
node_config -->|"applies settings"| node_converter_session
node_user_dictionary -->|"supplies user entries"| node_segments
node_dictionary_editor -->|"edits"| node_user_dictionary
node_prompt_ui -->|"requests transformation"| node_magic_backend
node_magic_backend -->|"provider implementation"| node_openai_client
node_secure_config -->|"provides credentials"| node_openai_client

click node_app_delegate "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/AppDelegate.swift"
click node_input_controller "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/azooKeyMacInputController.swift"
click node_event_adapter "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/NSEvent.swift"
click node_xpc_client "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/ConverterServerClient.swift"
click node_candidate_ui "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/CandidateWindow/BaseCandidateViewController.swift"
click node_text_commit "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/azooKeyMacInputController%2BSelectedTextTransform.swift"
click node_converter_server "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/main.swift"
click node_converter_session "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterSession.swift"
click node_xpc_protocol "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/XPC/ConverterServerXPCProtocol.swift"
click node_key_events "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterServer%2BKeyEvent.swift"
click node_snapshots "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterServer%2BSnapshot.swift"
click node_key_event_core "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/KeyEventCore.swift"
click node_actions_state "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/InputState.swift"
click node_segments "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/SegmentsManager.swift"
click node_keymap "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/KeyMap/KeyMap.swift"
click node_model_resources "https://github.com/azookey/azookey-desktop/tree/main/azooKeyMac/Resources/base_n5_lm"
click node_config "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/Configs/Config.swift"
click node_user_dictionary "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/UserDictionary/CompiledUserDictionaryStore.swift"
click node_dictionary_editor "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Windows/UserDictionaryEditorWindow.swift"
click node_secure_config "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Configs/SecureConfigItem.swift"
click node_magic_backend "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/MagicConversion/AIBackend.swift"
click node_openai_client "https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/MagicConversion/OpenAIClient.swift"
click node_prompt_ui "https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Windows/PromptInput/PromptInputWindow.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_delegate,node_input_controller,node_event_adapter,node_xpc_client,node_candidate_ui,node_text_commit,node_dictionary_editor toneBlue
class node_converter_server,node_converter_session,node_xpc_protocol,node_key_events,node_snapshots toneAmber
class node_key_event_core,node_actions_state,node_segments,node_keymap,node_model_resources toneMint
class node_config,node_user_dictionary,node_secure_config toneRose
class node_magic_backend,node_openai_client,node_prompt_ui toneIndigo
```

## Components

### macOS Input Method

Xcode target handling InputMethodKit integration and native UI.

- [App lifecycle](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/AppDelegate.swift): AppDelegate · `azooKeyMac/AppDelegate.swift`
- [Input controller](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/azooKeyMacInputController.swift): InputMethodKit controller · Receives host keyboard events and applies composition results. · `azooKeyMac/InputController/azooKeyMacInputController.swift`
- [macOS event adapter](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/NSEvent.swift): NSEvent mapping · `azooKeyMac/InputController/NSEvent.swift`
- [Converter client](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/ConverterServerClient.swift): XPC client · `azooKeyMac/InputController/ConverterServerClient.swift`
- [Candidate window](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/CandidateWindow/BaseCandidateViewController.swift): native candidate UI · `azooKeyMac/InputController/CandidateWindow/BaseCandidateViewController.swift`
- [Text commit & transform](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/InputController/azooKeyMacInputController%2BSelectedTextTransform.swift): InputMethodKit output · `azooKeyMac/InputController/azooKeyMacInputController+SelectedTextTransform.swift`
- [Dictionary editor](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Windows/UserDictionaryEditorWindow.swift): native settings window · `azooKeyMac/Windows/UserDictionaryEditorWindow.swift`

### Converter Process

Managed background conversion service behind XPC.

- [Converter server](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/main.swift): ConverterServer executable · Owns a stateful conversion session per client. · `Core/Sources/ConverterServer/main.swift`
- [Conversion session](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterSession.swift): per-client state · `Core/Sources/ConverterServer/ConverterSession.swift`
- [Converter XPC protocol](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/XPC/ConverterServerXPCProtocol.swift): IPC contract · Carries key events, snapshots, and settings. · `Core/Sources/Core/XPC/ConverterServerXPCProtocol.swift`
- [Server key-event API](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterServer%2BKeyEvent.swift): XPC operation · `Core/Sources/ConverterServer/ConverterServer+KeyEvent.swift`
- [Server snapshot API](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/ConverterServer/ConverterServer%2BSnapshot.swift): XPC operation · `Core/Sources/ConverterServer/ConverterServer+Snapshot.swift`

### Core Conversion

Shared Swift package conversion state and input logic.

- [Core key events](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/KeyEventCore.swift): input representation · `Core/Sources/Core/InputUtils/KeyEventCore.swift`
- [Actions & input state](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/InputState.swift): composition state machine · Separates client-facing actions from user editing actions. · `Core/Sources/Core/InputUtils/InputState.swift`
- [Segments manager](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/InputUtils/SegmentsManager.swift): conversion orchestration · Manages segments, predictions, candidates, corrections, and selection. · `Core/Sources/Core/InputUtils/SegmentsManager.swift`
- [Key map & direct input](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/KeyMap/KeyMap.swift): romaji/AZIK mapping · `Core/Sources/Core/KeyMap/KeyMap.swift`
- [Zenzai model resources](https://github.com/azookey/azookey-desktop/tree/main/azooKeyMac/Resources/base_n5_lm): bundled LM and GGUF · Language-model and inference assets bundled with the input source. · `azooKeyMac/Resources/base_n5_lm`

### Configuration & Dictionaries

Persistent personalization and configuration concerns.

- [Shared configuration](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/Configs/Config.swift): typed config · `Core/Sources/Core/Configs/Config.swift`
- [Compiled user dictionary](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/UserDictionary/CompiledUserDictionaryStore.swift): personal dictionary store · `Core/Sources/Core/UserDictionary/CompiledUserDictionaryStore.swift`
- [Secure credentials](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Configs/SecureConfigItem.swift): Keychain config · Keeps provider credentials app-side. · `azooKeyMac/Configs/SecureConfigItem.swift`

### Magic Conversion

Optional LLM-assisted text transformation.

- [AI backend abstraction](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/MagicConversion/AIBackend.swift): LLM provider interface · `Core/Sources/Core/MagicConversion/AIBackend.swift`
- [OpenAI provider](https://github.com/azookey/azookey-desktop/blob/main/Core/Sources/Core/MagicConversion/OpenAIClient.swift): AI backend client · `Core/Sources/Core/MagicConversion/OpenAIClient.swift`
- [Magic prompt UI](https://github.com/azookey/azookey-desktop/blob/main/azooKeyMac/Windows/PromptInput/PromptInputWindow.swift): native prompt window · `azooKeyMac/Windows/PromptInput/PromptInputWindow.swift`

## Connections

- App lifecycle → Input controller: starts input method
- Input controller → macOS event adapter: adapts macOS events
- macOS event adapter → Core key events: maps to core event
- Input controller → Converter client: forwards conversion events
- Converter client → Converter XPC protocol: uses
- Converter XPC protocol → Converter server: IPC boundary
- Converter server → Conversion session: creates
- Converter server → Server key-event API: serves
- Converter server → Server snapshot API: serves
- Server key-event API → Core key events: consumes
- Conversion session → Actions & input state: updates
- Actions & input state → Segments manager: drives
- Key map & direct input → Core key events: interprets input
- Segments manager → Zenzai model resources: uses conversion resources
- Server snapshot API → Converter client: returns composition state
- Converter client → Candidate window: updates candidates
- Input controller → Text commit & transform: commits selected text
- Shared configuration → Conversion session: applies settings
- Compiled user dictionary → Segments manager: supplies user entries
- Dictionary editor → Compiled user dictionary: edits
- Magic prompt UI → AI backend abstraction: requests transformation
- AI backend abstraction → OpenAI provider: provider implementation
- Secure credentials → OpenAI provider: provides credentials
