# dictation-toolbox/caster architecture

> Architecture diagram of the GitHub repository dictation-toolbox/caster, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dictation-toolbox/caster
- Repository: https://github.com/dictation-toolbox/caster

## Overview

##### Project Type and Purpose
- Caster is an open‑source, full‑application toolkit (Python package) for controlling your computer by voice.
- It builds on the Dragonfly speech‑recognition framework and integrates with multiple speech engines (Dragon NaturallySpeaking, Kaldi, Windows Speech Recognition).
- It provides a plugin‑style architecture of “rules” and “contexts” for universal editing/navigation, application‑specific commands, and language‑specific coding constructs.

##### Top‑Level Architectural Layers
A. External Interfaces
- Speech Engines (Dragonfly adapters for Kaldi, Dragon, WSR)
- Third‑party services: Sikuli (via XMLRPC server), Aenea, AutoHotkey
B. Core Caster Engine
- Control & Nexus (castervoice/lib/ctrl, nexus.py): orchestrates engine setup, grammar activation, continuous command recognition (CCR), dependency management
- Configuration (config_toml, config_base): loads user settings from TOML or default dir
- Grammar & Rule Manager (grammar_container, grammar_manager, managed_rule, rule_maker): dynamically loads rule modules, activates/deactivates them based on context
- CCR Merger (lib/merge/ccrmerging2): merges multiple small commands into continuous flows
- Transformers (transformers_runner, text_replacer): apply user or built‑in transformers to map voice specs to actions
C. Asynchronous Subsystem (castervoice/asynch)
- Events & Observables (e.g., hmc, mouse, HUD, settingswindow): run non‑blocking command flows, pop‑up dialogs, directory pickers
D. OS‑Interaction Layer
- Actions (lib/actions), Keyboard & Mouse controllers, Virtual Desktops, Clipboard, Window Management, Printer, Terminal integration

##### Component Relationships & Data Flow
- Audio Input → Speech Engine Adapter → Dragonfly → Caster Control
- Caster Control invokes Grammar Manager → finds matching Rule(s)
- Rule invocation → Action Module → OS‑Interaction (keystrokes, mouse moves, window ops)
- For CCR: incoming rule fragments → CCR Merger → Transformers → final command
- Asynchronous requests (e.g., “choose directory”) → Asynch subsystem → UI/Dialogs → result fed back into rule flow
- External integrations (Sikuli, Aenea) invoked via dedicated rule modules that communicate over XMLRPC or sockets

##### Key Architectural Patterns & Principles
- Plugin/Modular pattern: rules for every application and language live in separate directories; loaded/unloaded at runtime
- Observer pattern: reload observables for hot‑reloading rules, timer/manual observables
- Strategy pattern: merging strategies (classic merging, compatibility, sorting) for CCR
- Separation of concerns: control flow, grammar management, merge/transform, action execution, async UI, and external integrations are in distinct packages
- Configuration abstraction: base vs TOML config providers

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Interfaces
    subgraph "External Interfaces"
        direction TB
        DragonflyAdapters["Dragonfly Adapters\n(Kaldi, Dragon, WSR)"]:::external
        SikuliController["Sikuli Controller"]:::external
        SikuliXMLRPCServer["Sikuli XMLRPC Server"]:::external
        Aenea["Aenea"]:::external
        AutoHotkey["AutoHotkey"]:::external
    end

    %% Core Caster Engine
    subgraph "Core Caster Engine"
        direction TB
        subgraph "Control & Nexus"
            direction TB
            ConfigureEngine["ConfigureEngine"]:::core
            Dependencies["Dependencies"]:::core
            Nexus["Nexus"]:::core
        end
        subgraph "Configuration Providers"
            direction TB
            ConfigBase["ConfigBase"]:::core
            ConfigTOML["ConfigTOML"]:::core
        end
        subgraph "Grammar & Rule Manager"
            direction TB
            GrammarManager["GrammarManager"]:::core
            GrammarContainer["GrammarContainer"]:::core
            ManagedRule["ManagedRule"]:::core
            RuleMaker["RuleMaker"]:::core
        end
        CCRMerger2["CCRMerger2"]:::core
        subgraph "Transformers"
            direction TB
            TransformersRunner["TransformersRunner"]:::core
            TextReplacer["TextReplacer"]:::core
        end
        UserConfig["User Configuration"]:::core
    end

    %% Asynchronous Subsystem
    subgraph "Asynchronous Subsystem"
        direction TB
        HMCObservables["HMC Observables"]:::async
        HUD["HUD"]:::async
        HUDSupport["HUD Support"]:::async
        MouseGrids["Mouse Grids"]:::async
        Legion["Legion"]:::async
    end

    %% OS-Interaction Layer
    subgraph "OS-Interaction Layer"
        direction TB
        Actions["Actions"]:::os
        Clipboard["Clipboard"]:::os
        VirtualDesktops["VirtualDesktops"]:::os
        WinVirtualDesktops["WindowsVirtualDesktops"]:::os
        Terminal["Terminal"]:::os
    end

    %% Data Flows
    AudioInput(["Audio Input"]):::external
    AudioInput -->|"audio"| DragonflyAdapters
    DragonflyAdapters -->|"voice commands"| Nexus
    Nexus -->|"invoke"| GrammarManager
    GrammarManager -->|"match rule"| ManagedRule
    ManagedRule -->|"execute"| Actions
    Nexus -->|"CCR fragments"| CCRMerger2
    CCRMerger2 -->|"merged text"| TransformersRunner
    TransformersRunner -->|"transformed"| Actions
    ManagedRule -- "async calls" --> HMCObservables
    HMCObservables -->|"event result"| ManagedRule
    ManagedRule -- "invoke external" --> SikuliController
    SikuliController -- "XMLRPC" --> SikuliXMLRPCServer
    ManagedRule -- "invoke external" --> Aenea
    Actions -->|"OS ops"| Clipboard
    Actions -->|"OS ops"| VirtualDesktops
    Actions -->|"OS ops"| WinVirtualDesktops
    Actions -->|"OS ops"| Terminal

    %% Click Events
    click SikuliController "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/sikuli/sikuli_controller.py"
    click SikuliXMLRPCServer "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/sikuli/server/xmlrpc_server.sikuli/xmlrpc_server.py"
    click ConfigureEngine "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/ctrl/configure_engine.py"
    click Dependencies "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/ctrl/dependencies.py"
    click Nexus "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/ctrl/nexus.py"
    click ConfigBase "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/config/config_base.py"
    click ConfigTOML "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/config/config_toml.py"
    click GrammarManager "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/ctrl/mgr/grammar_manager.py"
    click GrammarContainer "https://github.com/dictation-toolbox/caster/tree/master/castervoice/lib/ctrl/mgr/grammar_container/"
    click ManagedRule "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/ctrl/mgr/managed_rule.py"
    click RuleMaker "https://github.com/dictation-toolbox/caster/tree/master/castervoice/lib/ctrl/mgr/rule_maker/"
    click CCRMerger2 "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/merge/ccrmerging2/ccrmerger2.py"
    click TransformersRunner "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/merge/ccrmerging2/transformers_runner.py"
    click TextReplacer "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/merge/ccrmerging2/transformers/text_replacer/text_replacer.py"
    click HMCObservables "https://github.com/dictation-toolbox/caster/tree/master/castervoice/asynch/hmc/"
    click HUD "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/hud.py"
    click HUDSupport "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/hud_support.py"
    click MouseGrids "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/mouse/grids.py"
    click Legion "https://github.com/dictation-toolbox/caster/blob/master/castervoice/asynch/mouse/legion.py"
    click Actions "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/actions.py"
    click Clipboard "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/clipboard.py"
    click VirtualDesktops "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/virtual_desktops.py"
    click WinVirtualDesktops "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/windows_virtual_desktops.py"
    click Terminal "https://github.com/dictation-toolbox/caster/blob/master/castervoice/lib/terminal.py"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef core fill:#ccf,stroke:#333,stroke-width:1px
    classDef async fill:#cfc,stroke:#333,stroke-width:1px
    classDef os fill:#eee,stroke:#333,stroke-width:1px
```
