How charansaiponnada/candor works
Purpose and Runtime
Candor is a Flutter Android application for fully offline, two-tier on-device SLM chat. It selects between bundled quantized Qwen GGUF models according to query complexity, model confidence, battery, and thermal headroom. Release inference has no network dependency. Flutter owns application flow/UI; Android provides device-state and model-asset operations through a method channel. Entry points: lib/main.dart, android/app/src/main/kotlin/com/candor/candor/MainActivity.kt, and android/app/src/main/AndroidManifest.xml.
Startup, Native Boundary, and Model Assets
At startup, Flutter wires persistence, device monitoring, routing, and UI; the native bridge copies bundled GGUF assets into app storage before inference. Native channel candor/device exposes battery percentage, API-29 thermal status, asset-model discovery/sizes, and streaming model preparation. Bundled model location is android/app/src/main/assets/models; model download/bootstrap support is tools/download_models.ps1. Android is minSdk 29+ and uses Kotlin/Gradle under android/app/build.gradle.kts.
Query Dispatch: Tools, Skills, Then Models
Chat submissions first cross deterministic execution layers before generative inference:
• Platform-action detector/executor launches Android intents for app/settings navigation, timer, SMS/email drafts, and URLs: lib/services/tools.dart, android/app/src/main/AndroidManifest.xml.
• Utility-skill engine handles math, conversions, time/date, memos, text operations, and chance deterministically; these bypass models: lib/services/skills.dart, lib/services/stores.dart.
• Remaining prompts enter the tier router: lib/services/router.dart, with shared domain types in lib/models.dart.
Tool executions are rendered as action cards; skills become regular response text. Both remain offline.
Tier Routing and Reliability Policy
lib/services/router.dart is the primary decision boundary. It obtains constrained/headroom state from lib/services/device_state.dart, identifies depth-demanding prompts, and either:
1. sends complex prompts directly to Tier 2 when resources permit;
2. runs Tier 1 by default, then escalates if its parsed conf: result is low, empty, refusal-like, or hedge-like;
3. retains Tier 1 with an explicit constrained badge if battery is below 20% or thermal state is moderate/higher.
Tier-2 refusals fall back to Tier-1 output. Each routed query emits an EscalationRecord to app-documents JSONL for later analysis.
On-device Inference Lifecycle
lib/services/model_runner.dart wraps llama_flutter_android for token streaming, prompt assembly, confidence-tag extraction, tier-specific sampling, optional Vulkan offload, and benchmarks. The plugin has one process-global loaded model, so Candor loads on demand, keeps only the last-used tier resident, and unloads/reloads on tier or GPU/model-setting changes. Tier settings and persisted sampling parameters originate in lib/services/stores.dart; model/device options are surfaced in lib/screens/settings_screen.dart and lib/screens/prompt_lab_screen.dart.
State, Conversations, and Presentation
ChatStore persists conversations and last-turn context; SettingsStore persists onboarding, identity/persona, model choices, GPU preference, and sampling settings in app-document JSON files. Paths: lib/services/stores.dart, lib/models.dart, lib/screens/chat_screen.dart. The chat screen drives streaming state and displays tier/time/confidence attribution. Navigation between chat, history, skills, benchmarks, settings, prompt lab, onboarding, and diagnostics is centralized in lib/services/router.dart and screen modules under lib/screens/.
Diagnostics and Offline Capabilities
The benchmark flow measures per-bundled-model size, load time, and tokens/sec through the same model runner: lib/screens/benchmarks_screen.dart, lib/services/model_runner.dart. Debug UI exposes real versus simulated resource constraints and latency/readout data: lib/screens/debug_panel.dart, lib/services/device_state.dart. Voice capture uses Android speech recognition through lib/services/speech.dart; Android intent actions and model inference remain device-local. The release manifest intentionally omits INTERNET.
Main entry points: model_runner.dart Model runner · router.dart Tier router · chat_screen.dart Chat screen · tools.dart Platform tools
Components 19
Flutter app
Application flow, deterministic handling, routing, and presentation.
- Flutter startup · Wires persistence, monitoring, routing, and UI.
lib/main.dart - App screens · Chat, history, settings, labs, onboarding, and diagnostics UI.
lib/screens - Chat screen · Displays streamed responses and tier/confidence attribution.
lib/screens/chat_screen.dart - Platform tools · Detects actions and launches offline Android intents.
lib/services/tools.dart - Utility skills · Handles math, conversions, memos, text, and chance without a model.
lib/services/skills.dart - Tier router · Routes prompts, escalates low-confidence results, and applies constraints.
lib/services/router.dart - Device state · Supplies battery and thermal headroom to routing and diagnostics.
lib/services/device_state.dart - Domain models · Includes routed-query and escalation domain types.
lib/models.dart - Benchmark flow · Measures bundled-model size, load time, and tokens per second.
lib/screens/benchmarks_screen.dart - Voice capture · Uses Android speech recognition for input capture.
lib/services/speech.dart - Diagnostics panel · Shows real or simulated constraints and latency readouts.
lib/screens/debug_panel.dart
Android boundary
Device APIs, intents, and bundled model preparation.
- Device method channel · Uses candor/device for device state and model operations.
android/app/src/main/kotlin/com/candor/candor/MainActivity.kt - Model asset preparation · Copies bundled GGUF assets into app storage before inference.
android/app/src/main/assets/models - Android manifest · Declares offline-capable intent handling and omits INTERNET.
android/app/src/main/AndroidManifest.xml
On-device inference
Tiered local GGUF inference lifecycle.
- Model runner · Streams tokens and manages one resident model with tier-specific sampling.
lib/services/model_runner.dart - Tier 1 GGUF · Default fast model; retained under constrained resources.
android/app/src/main/assets/models - Tier 2 GGUF · Complex-query model with Tier-1 fallback on refusal.
android/app/src/main/assets/models
Persistent offline state
App-document data retained between sessions.
- Chat & settings stores · Persists conversations, settings, and sampling parameters.
lib/services/stores.dart - Escalation JSONL · One routing record is emitted per routed query.
Connections 21
- Flutter startup → Chat & settings stores: initializes
- Flutter startup → App screens: starts UI
- App screens → Chat screen: hosts
- Chat screen → Platform tools: submits prompt
- Platform tools → Utility skills: non-action prompt
- Platform tools → Android manifest: launches intents
- Utility skills → Tier router: unhandled prompt
- Tier router → Device state: checks headroom
- Device state → Device method channel: battery and thermal API
- Tier router → Model runner: selects tier
- Tier router → Escalation JSONL: writes route record
- Tier router → Domain models: uses shared types
- Model runner → Tier 1 GGUF: loads or streams
- Model runner → Tier 2 GGUF: loads or streams
- Chat & settings stores → Model runner: model and sampling settings
- Device method channel → Model asset preparation: prepares assets
- Model asset preparation → Model runner: app-storage GGUF
- Model runner → Chat screen: token stream and attribution
- Benchmark flow → Model runner: measures models
- Diagnostics panel → Device state: inspects or simulates
- Voice capture → Chat screen: provides input