# kwaroran/risuai architecture

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

- Interactive diagram: https://gitdiagram.com/kwaroran/risuai
- Repository: https://github.com/kwaroran/risuai

## Overview

##### Project Type and Purpose
- RisuAI is a full‑stack, cross‑platform AI chat client and web app.
- It targets three environments with a shared Svelte/TypeScript front end:
– Desktop (Tauri/Rust + optional Python subprocess)
– Mobile (Capacitor/Android)
– Web (static hosting + optional local Node.js server and cloud functions)

##### Top‑Level Components
A. Frontend (Svelte + TypeScript)
– src/ (App.svelte, LiteMain.svelte, UI, ChatScreens, Settings, SideBars, Playground, etc.)
– Uses web workers (public/assets/*.wasm + .js) for translation and tokenizer.
– Bundled via Vite (vite.config.ts) and styled with Tailwind (tailwind.config.js).

B. Host Adapters / Backends
1. Desktop Adapter (Tauri)
– src‑tauri/src/main.rs (Rust core)
– src‑tauri/src‑python (Python subprocess for advanced tasks)
– Communicates with the front end via IPC (Tauri APIs).
– Reads/writes via local FS (using chosen storage module).
2. Mobile Adapter (Capacitor / Android)
– android/app/src/main/java/.../MainActivity.java
– android/app/src/main/java/.../StreamedPlugin.java (native plugin for streaming LLM responses)
– Bridges Svelte code to Android native capabilities.
3. Web Adapter
– Static hosting of index.html + public/ (assets, sw.js, manifest.json)
– Optional local Node.js server (server/node/server.cjs) for proxying LLM requests, SSL termination, drive backups.
– Cloud Functions (functions/drive.js, functions/proxy*.js) to run on serverless platforms.

C. External Services
– LLM Providers: OpenAI, Claude, Ooba, OpenRouter, Cohere, etc. (configured via environment or UI)
– Google Drive (drive backup integration via functions/drive.js)
– TTS services (configurable per provider)

D. Storage Layer
– IndexedDB/OPFS (web) – src/ts/storage/opfsStorage.ts
– localStorage (browser fallback) – autoStorage.ts
– mobileStorage.ts (Capacitor plugin)
– nodeStorage.ts (Node.js FS)
– accountStorage.ts (user credentials)

##### Data Flow & Interactions
1. User interacts with UI (Svelte components)
2. UI invokes API layer (calls Tauri IPC, Capacitor plugin, or fetch to Node server/functions)
3. Adapter layer handles:
– Routing requests to LLM endpoints (via HTTP or TCP)
– Running translator and tokenizer workers in‑browser or via Python subprocess
– Persisting chats, settings, files via appropriate storage module
– Emitting streaming tokens to UI (via IPC or Capacitor streams)
4. External LLM responds; responses flow back through the same adapter to the UI for rendering.
5. Optional cloud functions manage secure token proxying and drive backups.

##### Architectural Patterns & Key Principles
– Shared‑UI, multi‑adapter architecture (“write once, run anywhere”)
– Adapter pattern for environment‑specific implementations
– Micro‑service‑like decomposition for cloud functions vs. monolithic Node server
– Use of web workers/WebAssembly for performance‑critical tasks (translation, tokenization)
– Modular front‑end: Svelte component library (ChatScreens, UI, Settings, Playground)
– Pluggable provider interfaces (see plugins.md)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Shared Front-End
    subgraph "Shared Front-End"
        FE["Svelte + TypeScript"]:::frontend
        subgraph "UI Components"
            App["App.svelte"]:::frontend
            Lite["LiteMain.svelte"]:::frontend
            Lib["src/lib/..."]:::frontend
            Lang["src/lang"]:::frontend
        end
        subgraph "Workers/WebAssembly"
            BWJS["bergamot-translator-worker.js"]:::frontend
            BWASM["bergamot-translator-worker.wasm"]:::frontend
            TWJS["translator-worker.js"]:::frontend
            RBGJS["rpack_bg.js"]:::frontend
            RBWASM["rpack_bg.wasm"]:::frontend
        end
        FE --> App
        FE --> Lite
        FE --> Lib
        FE --> Lang
        FE --> BWJS
        FE --> BWASM
        FE --> TWJS
        FE --> RBGJS
        FE --> RBWASM
    end

    %% Desktop Adapter
    subgraph "Desktop Adapter (Tauri)" 
        MR["src-tauri/src/main.rs"]:::adapter
        PYM["main.py"]:::adapter
        PYR["run.py"]:::adapter
        RTR["requirements.txt"]:::adapter
        CFG["tauri.conf.json"]:::adapter
        CARGO["Cargo.toml / Cargo.lock"]:::adapter
        MR --> PYM
        MR --> CFG
        MR --> CARGO
    end

    %% Mobile Adapter
    subgraph "Mobile Adapter (Capacitor/Android)"
        CAP["Capacitor Config"]:::adapter
        MA["MainActivity.java"]:::adapter
        SP["StreamedPlugin.java"]:::adapter
        CAP --> MA
        CAP --> SP
    end

    %% Web Adapter
    subgraph "Web Adapter"
        index["index.html"]:::server
        manifest["manifest.json"]:::server
        Public["public/"]:::server
        Nodef["server/node/server.cjs"]:::server
        SSL["server/node/ssl/"]:::server
        DFN["functions/drive.js"]:::server
        P1["functions/proxy.js"]:::server
        P2["functions/proxy2.js"]:::server
    end

    %% Storage Layer
    subgraph "Storage Layer"
        AS["accountStorage.ts"]:::storage
        ATS["autoStorage.ts"]:::storage
        MS["mobileStorage.ts"]:::storage
        NS["nodeStorage.ts"]:::storage
        OS["opfsStorage.ts"]:::storage
    end

    %% Plugins & Interfaces
    PI["plugins.md"]:::misc
    PTS["src/ts/plugins/plugins.ts"]:::misc

    %% Config Files
    subgraph "Config Files"
        TWC["tailwind.config.js"]:::misc
        PCC["postcss.config.cjs"]:::misc
        TSC["tsconfig.json"]:::misc
        TSCN["tsconfig.node.json"]:::misc
        PNPM["pnpm-lock.yaml"]:::misc
        PKG["package.json"]:::misc
    end

    %% External Services
    subgraph "External Services"
        LLM["LLM Providers"]:::external
        GD["Google Drive"]:::external
        TTS["TTS Services"]:::external
    end

    %% Connections
    User(("User")):::user
    User --> FE
    FE -->|"IPC/fetch/bridge"| MR
    FE -->|"IPC/fetch/bridge"| CAP
    FE -->|"Served Assets"| index
    FE -->|"HTTP POST Chat"| Nodef
    FE -->|"HTTP Trigger"| DFN
    MR -->|"HTTP/TCP Chat"| LLM
    CAP -->|"HTTP/TCP Chat"| LLM
    Nodef -->|"Proxy Chat"| LLM
    DFN -->|"Drive Backup"| GD
    Nodef --> NS
    MR --> AS
    AS --> OS
    MS --> FE
    ATS --> FE

    %% Click Events
    click App "https://github.com/kwaroran/risuai/blob/main/src/App.svelte"
    click Lite "https://github.com/kwaroran/risuai/blob/main/src/LiteMain.svelte"
    click Lib "https://github.com/kwaroran/risuai/tree/main/src/lib/"
    click Lang "https://github.com/kwaroran/risuai/tree/main/src/lang/"
    click BWJS "https://github.com/kwaroran/risuai/blob/main/public/assets/bergamot-translator-worker.js"
    click BWASM "https://github.com/kwaroran/risuai/blob/main/public/assets/bergamot-translator-worker.wasm"
    click TWJS "https://github.com/kwaroran/risuai/blob/main/public/assets/translator-worker.js"
    click RBGJS "https://github.com/kwaroran/risuai/blob/main/src/ts/rpack/rpack_bg.js"
    click RBWASM "https://github.com/kwaroran/risuai/blob/main/src/ts/rpack/rpack_bg.wasm"
    click MR "https://github.com/kwaroran/risuai/blob/main/src-tauri/src/main.rs"
    click PYM "https://github.com/kwaroran/risuai/blob/main/src-tauri/src-python/main.py"
    click PYR "https://github.com/kwaroran/risuai/blob/main/src-tauri/src-python/run.py"
    click RTR "https://github.com/kwaroran/risuai/blob/main/src-tauri/src-python/requirements.txt"
    click CFG "https://github.com/kwaroran/risuai/blob/main/src-tauri/tauri.conf.json"
    click CARGO "https://github.com/kwaroran/risuai/blob/main/src-tauri/Cargo.toml"
    click CAP "https://github.com/kwaroran/risuai/blob/main/capacitor.config.ts"
    click MA "https://github.com/kwaroran/risuai/blob/main/android/app/src/main/java/co/aiclient/risu/MainActivity.java"
    click SP "https://github.com/kwaroran/risuai/blob/main/android/app/src/main/java/co/aiclient/risu/StreamedPlugin.java"
    click index "https://github.com/kwaroran/risuai/blob/main/index.html"
    click manifest "https://github.com/kwaroran/risuai/blob/main/manifest.json"
    click Public "https://github.com/kwaroran/risuai/tree/main/public/"
    click Nodef "https://github.com/kwaroran/risuai/blob/main/server/node/server.cjs"
    click SSL "https://github.com/kwaroran/risuai/tree/main/server/node/ssl/"
    click DFN "https://github.com/kwaroran/risuai/blob/main/functions/drive.js"
    click P1 "https://github.com/kwaroran/risuai/blob/main/functions/proxy.js"
    click P2 "https://github.com/kwaroran/risuai/blob/main/functions/proxy2.js"
    click AS "https://github.com/kwaroran/risuai/blob/main/src/ts/storage/accountStorage.ts"
    click ATS "https://github.com/kwaroran/risuai/blob/main/src/ts/storage/autoStorage.ts"
    click MS "https://github.com/kwaroran/risuai/blob/main/src/ts/storage/mobileStorage.ts"
    click NS "https://github.com/kwaroran/risuai/blob/main/src/ts/storage/nodeStorage.ts"
    click OS "https://github.com/kwaroran/risuai/blob/main/src/ts/storage/opfsStorage.ts"
    click PI "https://github.com/kwaroran/risuai/blob/main/plugins.md"
    click PTS "https://github.com/kwaroran/risuai/blob/main/src/ts/plugins/plugins.ts"
    click TWC "https://github.com/kwaroran/risuai/blob/main/tailwind.config.js"
    click PCC "https://github.com/kwaroran/risuai/blob/main/postcss.config.cjs"
    click TSC "https://github.com/kwaroran/risuai/blob/main/tsconfig.json"
    click TSCN "https://github.com/kwaroran/risuai/blob/main/tsconfig.node.json"
    click PNPM "https://github.com/kwaroran/risuai/blob/main/pnpm-lock.yaml"
    click PKG "https://github.com/kwaroran/risuai/blob/main/package.json"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#006064,color:#000
    classDef adapter fill:#E8F5E9,stroke:#1B5E20,color:#000
    classDef server fill:#FFFDE7,stroke:#F57F17,color:#000
    classDef storage fill:#E1F5FE,stroke:#01579B,color:#000
    classDef external fill:#FFEBEE,stroke:#B71C1C,color:#000
    classDef misc fill:#F3E5F5,stroke:#4A148C,color:#000
    classDef user fill:#FFFFFF,stroke:#000000,color:#000
```
