How sengtha/iany works
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: ingest.ts Document ingest · worker.ts PGlite DB worker · App.tsx Knowledge app shell · ChatView.tsx Chat and library views
Components 20
Browser PWA
- Vite React entry · Mounts the browser application.
src/main.tsx - Knowledge app shell · Routes the knowledge-product experience.
src/App.tsx - Chat and library views · Primary question and document-management UI.
src/views/ChatView.tsx - Khmer/English i18n
src/i18n/index.tsx
RAG Pipeline
- Document ingest · Extracts, chunks, embeds, and persists documents.
src/rag/ingest.ts - Question answering · Embeds questions, retrieves passages, and optionally generates answers.
src/rag/ask.ts - Khmer-aware chunker
src/ai/chunker.ts
Local AI Runtime
- AI client protocol · UI-facing bridge to off-main-thread AI.
src/ai/client.ts - AI worker · Runs embeddings and local generation workloads.
src/ai/worker.ts - AI protocol
src/ai/protocol.ts - Khmer speech and OCR workers · Provides STT, OCR, and TTS for offline tools.
src/ai/khmerStt.worker.ts
Local Data
- PGlite DB worker · Hosts WASM Postgres persisted in IndexedDB.
src/db/worker.ts - Document and embedding schema · Stores sync-ready documents, chunks, vectors, indexes, and packs.
src/db/schema.ts - Hybrid retrieval · Combines vector, FTS, and reciprocal-rank fusion.
src/db/search.ts - Knowledge pack storage · Imports and exports offline document, chunk, and embedding bundles.
src/db/packs.ts
Shared & Deployment
- Shared domain core · Defines portable knowledge-pack and Braille concepts.
packages/core/src/engine.ts - React Native companion · Parallel offline client with local AI, database, models, and packs.
mobile/App.tsx - Model mirror worker · Caches Hugging Face model artifacts in R2 for model delivery.
worker/index.ts
Adjacent Apps
- Grove subsystem · Separate core and worker-based application.
grove/core/grove.ts - Trace subsystem · Separate core and worker-based application.
trace/core/trace.ts
Connections 20
- 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