# telosnex/fonnx architecture

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

- Interactive diagram: https://gitdiagram.com/telosnex/fonnx
- Repository: https://github.com/telosnex/fonnx

## Overview

##### Project type and purpose
- This is a Flutter plugin library (“FONNX”) plus an example Flutter app.
- Purpose: expose ONNX model inference across all major platforms (Android, iOS, Web, macOS, Linux, Windows) with native acceleration (Android NNAPI, iOS CoreML, WASM SIMD, FFI to ONNX C API on desktop).

##### File structure
- Top-level “lib/”: the Dart API surface, platform interface abstractions, method-channel bridge, FFI bindings, web shim, model-specific classes (Magika, MiniLM, Whisper, etc.).
- Platform plugin code directories:
- android/src/main/kotlin/com/telosnex/fonnx – Kotlin plugin registering a MethodChannel, loading ONNX AAR.
- ios/Classes – Swift plugin hooking into CocoaPods ONNX runtime.
- linux/, windows/ – C++ plugin entry points exposing a C API, loaded via dart:ffi.
- macos/ – Swift/CocoaPods plugin code.
- web (docs/ and example/web/) – JavaScript + WASM workers and glue code for ort-wasm.
- “onnx_runtime/” – headers and FFI config for building/linking the ONNX C API.
- “example/” – full Flutter app that uses FONNX, demonstrating multi-model inference (VAD, Whisper, embeddings, file-ID Magika).

##### README for context
- Confirms cross-platform support, details per-platform integration (FFI on desktop, pods on iOS, AAR on Android, WASM+SharedArrayBuffer on Web).
- Describes use cases: speech-to-text (VAD + Whisper), file identification (Magika), text embeddings.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Example App UI Layer
    subgraph "Example App UI" 
        direction TB
        ExampleUI["Flutter App / UI"]:::frontend
        SttService["SttService"]:::frontend
        MagikaWidget["Magika Widget"]:::frontend
        MiniLML6Widget["MiniLM L6 v2 Widget"]:::frontend
        MiniShLabWidget["MiniShLab Widget"]:::frontend
        MSMARCOMiniLMWidget["MSMARCO MiniLM L6 v3 Widget"]:::frontend
        PyannoteWidget["Pyannote Widget"]:::frontend
        SileroVADWidget["Silero VAD Widget"]:::frontend
        WhisperWidget["Whisper Widget"]:::frontend
    end

    %% Dart API and Abstractions
    FONNX_API["FONNX Dart API Surface"]:::dartapi
    PlatformInterface["Platform Interface Abstraction"]:::dartapi

    %% Bridges
    subgraph "Bridges" 
        direction TB
        MethodChannelBridge["MethodChannel Bridge (Mobile)"]:::bridge
        FFIBridge["Dart FFI Bridge (Desktop)"]:::bridge
        WebBridge["Web JS/WASM Bridge"]:::bridge
    end

    %% Native Runtimes
    subgraph "Android" 
        direction TB
        AndroidPlugin["Android Plugin & NNAPI"]:::native
        AndroidSessions["Android Session Objects"]:::native
        AndroidHandlers["Android Model Handlers"]:::native
    end

    subgraph "iOS/macOS" 
        direction TB
        IOSPlugin["iOS Plugin & CoreML"]:::native
        MacOSPlugin["macOS Plugin"]:::native
    end

    subgraph "Linux/Windows" 
        direction TB
        LinuxPlugin["Linux Plugin (FFI)"]:::native
        WindowsPlugin["Windows Plugin (FFI)"]:::native
    end

    subgraph "Web Runtime"
        direction TB
        WASMWorker["ORT WASM Worker"]:::native
    end

    %% Assets
    ModelAssets["Model Assets (.onnx)"]:::asset

    %% Connections
    ExampleUI -->|uses| SttService
    SttService -->|calls| FONNX_API
    FONNX_API -->|invokes| PlatformInterface
    PlatformInterface -->|mobile| MethodChannelBridge
    PlatformInterface -->|desktop| FFIBridge
    PlatformInterface -->|web| WebBridge

    MethodChannelBridge -->|via AAR| AndroidPlugin
    MethodChannelBridge -->|via CocoaPods| IOSPlugin
    MethodChannelBridge -->|via CocoaPods| MacOSPlugin

    FFIBridge -->|loads .so/.dll| LinuxPlugin
    FFIBridge -->|loads .so/.dll| WindowsPlugin

    WebBridge -->|postMessage| WASMWorker

    AndroidPlugin -->|loads model| ModelAssets
    IOSPlugin -->|loads model| ModelAssets
    MacOSPlugin -->|loads model| ModelAssets
    LinuxPlugin -->|loads model| ModelAssets
    WindowsPlugin -->|loads model| ModelAssets
    WASMWorker -->|loads model| ModelAssets

    ModelAssets -->|returns tensors| PlatformInterface
    PlatformInterface -->|returns results| FONNX_API
    FONNX_API -->|returns results| SttService
    SttService -->|updates| ExampleUI

    %% Click Events
    click ExampleUI "https://github.com/telosnex/fonnx/blob/main/example/lib/main.dart"
    click SttService "https://github.com/telosnex/fonnx/blob/main/example/lib/stt_service.dart"
    click MagikaWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/magika_widget.dart"
    click MiniLML6Widget "https://github.com/telosnex/fonnx/blob/main/example/lib/minilml6v2_widget.dart"
    click MiniShLabWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/minishlab_widget.dart"
    click MSMARCOMiniLMWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/msmarco_minilm_l6v3_widget.dart"
    click PyannoteWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/pyannote_widget.dart"
    click SileroVADWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/silero_vad_widget.dart"
    click WhisperWidget "https://github.com/telosnex/fonnx/blob/main/example/lib/whisper_widget.dart"
    click FONNX_API "https://github.com/telosnex/fonnx/blob/main/lib/fonnx.dart"
    click PlatformInterface "https://github.com/telosnex/fonnx/blob/main/lib/fonnx_platform_interface.dart"
    click MethodChannelBridge "https://github.com/telosnex/fonnx/blob/main/lib/fonnx_method_channel.dart"
    click WebBridge "https://github.com/telosnex/fonnx/blob/main/lib/fonnx_web.dart"
    click FFIBridge "https://github.com/telosnex/fonnx/blob/main/lib/onnx/ort_ffi_bindings.dart"
    click FFIBridge "https://github.com/telosnex/fonnx/blob/main/lib/onnx/ort.dart"
    click AndroidPlugin "https://github.com/telosnex/fonnx/blob/main/android/src/main/kotlin/com/telosnex/fonnx/FonnxPlugin.kt"
    click AndroidSessions "https://github.com/telosnex/fonnx/blob/main/android/src/main/kotlin/com/telosnex/fonnx/OrtSessionObjects.kt"
    click IOSPlugin "https://github.com/telosnex/fonnx/blob/main/ios/Classes/FonnxPlugin.swift"
    click MacOSPlugin "https://github.com/telosnex/fonnx/blob/main/macos/Classes/FonnxPlugin.swift"
    click LinuxPlugin "https://github.com/telosnex/fonnx/blob/main/linux/fonnx_plugin.cc"
    click LinuxPlugin "https://github.com/telosnex/fonnx/blob/main/linux/include/fonnx/fonnx_plugin.h"
    click WindowsPlugin "https://github.com/telosnex/fonnx/blob/main/windows/fonnx_plugin_c_api.cpp"
    click WindowsPlugin "https://github.com/telosnex/fonnx/blob/main/windows/include/fonnx/fonnx_plugin_c_api.h"
    click ModelAssets "https://github.com/telosnex/fonnx/tree/main/example/assets/models"
    click ModelAssets "https://github.com/telosnex/fonnx/tree/main/docs/assets/models"

    %% Styles
    classDef frontend fill:#D6E4FF,stroke:#333,stroke-width:1px
    classDef dartapi fill:#B3E5FC,stroke:#333,stroke-width:1px
    classDef bridge fill:#C8E6C9,stroke:#333,stroke-width:1px
    classDef native fill:#FFE0B2,stroke:#333,stroke-width:1px
    classDef asset fill:#F5F5F5,stroke:#333,stroke-width:1px
```
