# sengtha/iany architecture

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

- Interactive diagram: https://gitdiagram.com/sengtha/iany
- Repository: https://github.com/sengtha/iany

## Overview

#### Purpose and Entry Points
iAny is an offline-first Khmer/English knowledge and civic-AI suite centered on a browser RAG PWA: users ingest documents, retrieve locally, and optionally generate grounded answers. The primary React/Vite entry is `src/main.tsx`, which mounts `src/App.tsx`; standalone HTML pages expose focused tools such as Braille, voice, scan, and waste workflows. A React Native companion starts at `mobile/index.js` and `mobile/App.tsx`.

#### PWA UI and Feature Surface
The browser client is organized as route/page entry modules and reusable views. The knowledge-product shell includes chat, library, packs, settings, and radio views; specialized contribution and local vision tools are separate app flows. Core UI paths: `src/App.tsx`, `src/views/ChatView.tsx`, `src/views/LibraryView.tsx`. Khmer/English presentation is isolated in `src/i18n/index.tsx`, `src/i18n/km.ts`.

#### RAG Ingest and Question Pipeline
Document ingestion extracts text, Khmer-aware chunks it, requests embeddings, then persists document/chunk records. Asking embeds the question, retrieves hybrid-ranked passages, and invokes a local generator when available; otherwise it returns search results. The orchestration boundary is `src/rag/ingest.ts` and `src/rag/ask.ts`; chunk formation is `src/ai/chunker.ts`. This separates UI-facing workflows from worker-backed AI and database operations.

#### Local AI Runtime
AI workloads run behind a client/worker protocol, keeping model execution off the main UI thread. Transformers.js-backed embeddings support ingest and query retrieval; local Khmer/LLM generation, OCR, STT, TTS, radio voice, and vision flows share this AI layer. Key boundaries: `src/ai/client.ts`, `src/ai/worker.ts`, `src/ai/protocol.ts`. Dedicated workers exist for Khmer STT/OCR/TTS (`src/ai/khmerStt.worker.ts`, `src/ai/khmerocr.worker.ts`, `src/ai/khmertts.worker.ts`). Models are downloaded and cached locally; WebGPU enables capable generation, while search remains usable without it.

#### Browser Database and Retrieval
A dedicated DB worker hosts PGlite WASM Postgres persisted in IndexedDB. Its schema stores sync-ready document and chunk metadata, 256-dimensional embeddings, full-text indexes, and pack data. Search combines pgvector/HNSW semantic ranking with Postgres FTS and reciprocal-rank fusion; Khmer text is segmented before FTS indexing. Architectural paths: `src/db/worker.ts`, `src/db/schema.ts`, `src/db/search.ts`; document and pack persistence live in `src/db/documents.ts` and `src/db/packs.ts`.

#### Portability, Backup, and Shared Domain Core
Knowledge packs bundle documents, chunks, embeddings, and source text, allowing immediate offline import without re-embedding. Backup/model-sharing utilities support device transfer and encrypted cloud-backup workflows. The reusable package `packages/core/src/engine.ts` and `packages/core/src/pack.ts` represent shared core concepts, including Braille support in `packages/core/src/khmerbraille.ts`. The mobile app has parallel local AI, database, model-management, and pack layers at `mobile/src/ai/ask.ts`, `mobile/src/db/database.ts`, and `mobile/src/models/manager.ts`.

#### Deployment and External Boundaries
The app is an installable Vite PWA and deploys static assets to Cloudflare Workers. `worker/index.ts` is a pull-through model mirror: `/models/*` requests fetch Hugging Face model artifacts on cache miss, stream them to clients, and retain them in Cloudflare R2 for subsequent origin-hosted delivery. Worker infrastructure is configured by `wrangler.jsonc`. GitHub workflows cover EAS mobile builds and dataset publication: `.github/workflows/eas-build.yml` and `.github/workflows/publish-dataset.yml`.

#### Adjacent Offline Applications
The repository also contains independent “Grove” and “Trace” subsystems with core, web, and worker layers, likely designed around local structured data and worker persistence rather than the main RAG shell. Their defining contracts and execution points are `grove/core/grove.ts`, `grove/worker/handlers.ts`, `trace/core/trace.ts`, and `trace/worker/handlers.ts`.

Main entry points: [`src/rag/ingest.ts`](https://github.com/sengtha/iany/blob/main/src/rag/ingest.ts) (Document ingest), [`src/db/worker.ts`](https://github.com/sengtha/iany/blob/main/src/db/worker.ts) (PGlite DB worker), [`src/App.tsx`](https://github.com/sengtha/iany/blob/main/src/App.tsx) (Knowledge app shell), [`src/views/ChatView.tsx`](https://github.com/sengtha/iany/blob/main/src/views/ChatView.tsx) (Chat and library views)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_pwa["Browser PWA"]
  node_browser_entry["Vite React entry<br/>PWA entry<br/>[main.tsx]"]
  node_app_shell["Knowledge app shell<br/>React shell<br/>[App.tsx]"]
  node_knowledge_views["Chat and library views<br/>React views<br/>[ChatView.tsx]"]
  node_i18n["Khmer/English i18n<br/>presentation<br/>[index.tsx]"]
end

subgraph group_rag["RAG Pipeline"]
  node_ingest["Document ingest<br/>RAG workflow<br/>[ingest.ts]"]
  node_ask["Question answering<br/>RAG workflow<br/>[ask.ts]"]
  node_chunker["Khmer-aware chunker<br/>text processing<br/>[chunker.ts]"]
end

subgraph group_ai["Local AI Runtime"]
  node_ai_client["AI client protocol<br/>worker client<br/>[client.ts]"]
  node_ai_worker{{"AI worker<br/>model runtime<br/>[worker.ts]"}}
  node_ai_protocol["AI protocol<br/>worker contract<br/>[protocol.ts]"]
  node_khmer_workers{{"Khmer speech and OCR workers<br/>specialized AI workers<br/>[khmerStt.worker.ts]"}}
end

subgraph group_db["Local Data"]
  node_db_worker[("PGlite DB worker<br/>database worker<br/>[worker.ts]")]
  node_db_schema["Document and embedding schema<br/>Postgres schema<br/>[schema.ts]"]
  node_hybrid_search["Hybrid retrieval<br/>search engine<br/>[search.ts]"]
  node_pack_storage["Knowledge pack storage<br/>portable data<br/>[packs.ts]"]
end

subgraph group_platform["Shared &amp; Deployment"]
  node_shared_core["Shared domain core<br/>reusable package<br/>[engine.ts]"]
  node_mobile_app["React Native companion<br/>mobile app<br/>[App.tsx]"]
  node_model_mirror{{"Model mirror worker<br/>Cloudflare Worker<br/>[index.ts]"}}
end

subgraph group_adjacent["Adjacent Apps"]
  node_grove["Grove subsystem<br/>offline structured-data app<br/>[grove.ts]"]
  node_trace["Trace subsystem<br/>offline structured-data app<br/>[trace.ts]"]
end

node_browser_entry -->|"mounts"| node_app_shell
node_app_shell -->|"renders"| node_knowledge_views
node_app_shell -->|"localizes through"| node_i18n
node_knowledge_views -->|"ingests documents"| node_ingest
node_knowledge_views -->|"asks questions"| node_ask
node_ingest -->|"chunks text"| node_chunker
node_ingest -->|"requests embeddings"| node_ai_client
node_ask -->|"requests embeddings and generation"| node_ai_client
node_ai_client -->|"uses"| node_ai_protocol
node_ai_protocol -->|"dispatches to"| node_ai_worker
node_ai_worker -.->|"downloads models from"| node_model_mirror
node_ingest -->|"persists records"| node_db_worker
node_ask -->|"retrieves passages"| node_hybrid_search
node_hybrid_search -->|"queries"| node_db_worker
node_db_worker -->|"applies"| node_db_schema
node_db_worker -->|"stores packs"| node_pack_storage
node_shared_core -.->|"defines pack concepts"| node_pack_storage
node_mobile_app -.->|"shares domain concepts"| node_shared_core
node_khmer_workers -->|"uses worker contract"| node_ai_protocol
node_grove -.->|"parallel subsystem"| node_trace

click node_browser_entry "https://github.com/sengtha/iany/blob/main/src/main.tsx"
click node_app_shell "https://github.com/sengtha/iany/blob/main/src/App.tsx"
click node_knowledge_views "https://github.com/sengtha/iany/blob/main/src/views/ChatView.tsx"
click node_i18n "https://github.com/sengtha/iany/blob/main/src/i18n/index.tsx"
click node_ingest "https://github.com/sengtha/iany/blob/main/src/rag/ingest.ts"
click node_ask "https://github.com/sengtha/iany/blob/main/src/rag/ask.ts"
click node_chunker "https://github.com/sengtha/iany/blob/main/src/ai/chunker.ts"
click node_ai_client "https://github.com/sengtha/iany/blob/main/src/ai/client.ts"
click node_ai_worker "https://github.com/sengtha/iany/blob/main/src/ai/worker.ts"
click node_ai_protocol "https://github.com/sengtha/iany/blob/main/src/ai/protocol.ts"
click node_khmer_workers "https://github.com/sengtha/iany/blob/main/src/ai/khmerStt.worker.ts"
click node_db_worker "https://github.com/sengtha/iany/blob/main/src/db/worker.ts"
click node_db_schema "https://github.com/sengtha/iany/blob/main/src/db/schema.ts"
click node_hybrid_search "https://github.com/sengtha/iany/blob/main/src/db/search.ts"
click node_pack_storage "https://github.com/sengtha/iany/blob/main/src/db/packs.ts"
click node_shared_core "https://github.com/sengtha/iany/blob/main/packages/core/src/engine.ts"
click node_mobile_app "https://github.com/sengtha/iany/blob/main/mobile/App.tsx"
click node_model_mirror "https://github.com/sengtha/iany/blob/main/worker/index.ts"
click node_grove "https://github.com/sengtha/iany/blob/main/grove/core/grove.ts"
click node_trace "https://github.com/sengtha/iany/blob/main/trace/core/trace.ts"

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_browser_entry,node_app_shell,node_knowledge_views,node_i18n toneBlue
class node_ingest,node_ask,node_chunker toneAmber
class node_ai_client,node_ai_worker,node_ai_protocol,node_khmer_workers toneMint
class node_db_worker,node_db_schema,node_hybrid_search,node_pack_storage toneRose
class node_shared_core,node_mobile_app,node_model_mirror toneIndigo
class node_grove,node_trace toneTeal
```

## Components

### Browser PWA

- [Vite React entry](https://github.com/sengtha/iany/blob/main/src/main.tsx): PWA entry · Mounts the browser application. · `src/main.tsx`
- [Knowledge app shell](https://github.com/sengtha/iany/blob/main/src/App.tsx): React shell · Routes the knowledge-product experience. · `src/App.tsx`
- [Chat and library views](https://github.com/sengtha/iany/blob/main/src/views/ChatView.tsx): React views · Primary question and document-management UI. · `src/views/ChatView.tsx`
- [Khmer/English i18n](https://github.com/sengtha/iany/blob/main/src/i18n/index.tsx): presentation · `src/i18n/index.tsx`

### RAG Pipeline

- [Document ingest](https://github.com/sengtha/iany/blob/main/src/rag/ingest.ts): RAG workflow · Extracts, chunks, embeds, and persists documents. · `src/rag/ingest.ts`
- [Question answering](https://github.com/sengtha/iany/blob/main/src/rag/ask.ts): RAG workflow · Embeds questions, retrieves passages, and optionally generates answers. · `src/rag/ask.ts`
- [Khmer-aware chunker](https://github.com/sengtha/iany/blob/main/src/ai/chunker.ts): text processing · `src/ai/chunker.ts`

### Local AI Runtime

- [AI client protocol](https://github.com/sengtha/iany/blob/main/src/ai/client.ts): worker client · UI-facing bridge to off-main-thread AI. · `src/ai/client.ts`
- [AI worker](https://github.com/sengtha/iany/blob/main/src/ai/worker.ts): model runtime · Runs embeddings and local generation workloads. · `src/ai/worker.ts`
- [AI protocol](https://github.com/sengtha/iany/blob/main/src/ai/protocol.ts): worker contract · `src/ai/protocol.ts`
- [Khmer speech and OCR workers](https://github.com/sengtha/iany/blob/main/src/ai/khmerStt.worker.ts): specialized AI workers · Provides STT, OCR, and TTS for offline tools. · `src/ai/khmerStt.worker.ts`

### Local Data

- [PGlite DB worker](https://github.com/sengtha/iany/blob/main/src/db/worker.ts): database worker · Hosts WASM Postgres persisted in IndexedDB. · `src/db/worker.ts`
- [Document and embedding schema](https://github.com/sengtha/iany/blob/main/src/db/schema.ts): Postgres schema · Stores sync-ready documents, chunks, vectors, indexes, and packs. · `src/db/schema.ts`
- [Hybrid retrieval](https://github.com/sengtha/iany/blob/main/src/db/search.ts): search engine · Combines vector, FTS, and reciprocal-rank fusion. · `src/db/search.ts`
- [Knowledge pack storage](https://github.com/sengtha/iany/blob/main/src/db/packs.ts): portable data · Imports and exports offline document, chunk, and embedding bundles. · `src/db/packs.ts`

### Shared & Deployment

- [Shared domain core](https://github.com/sengtha/iany/blob/main/packages/core/src/engine.ts): reusable package · Defines portable knowledge-pack and Braille concepts. · `packages/core/src/engine.ts`
- [React Native companion](https://github.com/sengtha/iany/blob/main/mobile/App.tsx): mobile app · Parallel offline client with local AI, database, models, and packs. · `mobile/App.tsx`
- [Model mirror worker](https://github.com/sengtha/iany/blob/main/worker/index.ts): Cloudflare Worker · Caches Hugging Face model artifacts in R2 for model delivery. · `worker/index.ts`

### Adjacent Apps

- [Grove subsystem](https://github.com/sengtha/iany/blob/main/grove/core/grove.ts): offline structured-data app · Separate core and worker-based application. · `grove/core/grove.ts`
- [Trace subsystem](https://github.com/sengtha/iany/blob/main/trace/core/trace.ts): offline structured-data app · Separate core and worker-based application. · `trace/core/trace.ts`

## Connections

- Vite React entry → Knowledge app shell: mounts
- Knowledge app shell → Chat and library views: renders
- Knowledge app shell → Khmer/English i18n: localizes through
- Chat and library views → Document ingest: ingests documents
- Chat and library views → Question answering: asks questions
- Document ingest → Khmer-aware chunker: chunks text
- Document ingest → AI client protocol: requests embeddings
- Question answering → AI client protocol: requests embeddings and generation
- AI client protocol → AI protocol: uses
- AI protocol → AI worker: dispatches to
- AI worker → Model mirror worker: downloads models from
- Document ingest → PGlite DB worker: persists records
- Question answering → Hybrid retrieval: retrieves passages
- Hybrid retrieval → PGlite DB worker: queries
- PGlite DB worker → Document and embedding schema: applies
- PGlite DB worker → Knowledge pack storage: stores packs
- Shared domain core → Knowledge pack storage: defines pack concepts
- React Native companion → Shared domain core: shares domain concepts
- Khmer speech and OCR workers → AI protocol: uses worker contract
- Grove subsystem → Trace subsystem: parallel subsystem
