Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / translate / AGENTS.md
| 1 | # AGENTS.md — translate/ (':translate') |
| 2 | |
| 3 | > Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `translate/` + allowed shared modules. Do not root-scan. |
| 4 | |
| 5 | _Install: ./install translate (dev by default)._ |
| 6 | - Gradle: :translate / dir: translate/ |
| 7 | - Package roots present: ui, data, domain, platform, intents |
| 8 | - Entry files: MainActivity.kt, Route.kt, Navigation.kt |
| 9 | - Deps: :library:ml, :library:downloadservice, :library:ocr, :library:network |
| 10 | - Metadata: metadata_data/translate.md (present) |
| 11 | - Rust: no / screenshotTest: yes |
| 12 | |
| 13 | ## Overview |
| 14 | **Purpose:** Fully offline translator (NLLB-200, 202 languages) with camera OCR overlay, speech input, TTS output, and selection-menu translation from any app. Internet only for one-time SHA-256-verified model downloads. |
| 15 | **Key features:** |
| 16 | - Text translation over the on-device NLLB engine (`NllbTranslator` + `NllbModel`); language catalog, recents, and Auto source-language detection (`detectLanguageCode`, English fallback when it finds no trusted evidence) in `domain/` |
| 17 | - Camera mode: `CameraPreviewLayer` + `CameraOverlay` over `:library:ocr`; speech in via `SpeechRecognizerEngine`, spoken output via `TtsSpeaker` |
| 18 | - History + persistent settings (`data/TranslateSettings`); translation available from the system selection menu. Platform backup carries history/favourites + language settings through `platform/AppBackupAgent` (SqlCipherDbCodec plaintext export/re-encrypt); the raw SQLCipher DB, `secure_prefs` and the downloaded model packs are excluded from both backup channels in `res/xml/data_extraction_rules.xml` |
| 19 | - OpenAssistant `translate_text` via `intents/TranslateIntent`: input truncated at 1,000 chars with a flag, output capped at 2,000 chars, source/target codes validated against `domain/Languages` (target never `auto`); a missing model returns the typed `MODEL_NOT_DOWNLOADED` error in `library.intents.translate.TranslateResultData` |
| 20 | **Architecture / data-flow:** `ui/` text/camera/language pages → `platform/TranslateViewModel` (+ `TranslateUiContract`) → `domain/TranslationEngine` → `platform/NllbTranslator` on the `:library:ml` runtime; models fetched once via `:library:downloadservice` and hash-verified before first use. Translator loading is a terminal state machine (`platform/TranslatorLoader` + `TranslatorLoadState`): the ViewModel is created before the download gate passes, so the text/camera/image pages re-attempt the load on entry, and every attempt ends in Ready or a Failed error with retry (Retry action / scaffold pull-to-refresh) — never an endless loading state. |
| 21 | **Entry points + reading order:** `Route` → `MainActivity.kt` → `Navigation.kt` → `ui/TextTranslateScreen.kt` → `platform/TranslateViewModel.kt` → `domain/TranslationEngine.kt` → `platform/NllbTranslator.kt`. |
| 22 | **Gotchas:** |
| 23 | - First run without downloaded models must degrade gracefully — never assume the NLLB weights exist |
| 24 | - Camera overlay coordinates must track preview rotation; test on rotation, not just portrait |
| 25 | - Long input is translated in sentence-sized chunks (`domain/TranslationChunks` via `NllbTranslator`): the native decoder caps one call's output and returns hitting the cap as a successful partial translation, so a whole paragraph in one call silently loses its tail |
| 26 | |
| 27 | ## Key files |
| 28 | ### Root — entry + navigation |
| 29 | - com/vayunmathur/translate/MainActivity.kt |
| 30 | - com/vayunmathur/translate/Navigation.kt |
| 31 | - com/vayunmathur/translate/Route.kt |
| 32 | ### data/ + domain/ — settings, languages, engine contract |
| 33 | - com/vayunmathur/translate/data/TranslateDatabase.kt — `TranslationRecord`/`TranslationDao` + `translateDbConfigs` for the BackupAgent |
| 34 | - com/vayunmathur/translate/data/TranslateRepository.kt |
| 35 | - com/vayunmathur/translate/data/TranslateSettings.kt |
| 36 | - com/vayunmathur/translate/domain/TranslationEngine.kt |
| 37 | - com/vayunmathur/translate/domain/TranslationChunks.kt |
| 38 | - com/vayunmathur/translate/domain/LanguageDetection.kt |
| 39 | - com/vayunmathur/translate/domain/Languages.kt |
| 40 | - com/vayunmathur/translate/domain/RecentLanguages.kt |
| 41 | ### platform/ — ViewModel, NLLB, speech, TTS |
| 42 | - com/vayunmathur/translate/platform/AppBackupAgent.kt |
| 43 | - com/vayunmathur/translate/platform/TranslateViewModel.kt |
| 44 | - com/vayunmathur/translate/platform/TranslateUiContract.kt |
| 45 | - com/vayunmathur/translate/platform/TranslatorLoader.kt |
| 46 | - com/vayunmathur/translate/platform/NllbTranslator.kt |
| 47 | - com/vayunmathur/translate/platform/NllbModel.kt |
| 48 | - com/vayunmathur/translate/platform/SpeechRecognizerEngine.kt |
| 49 | - com/vayunmathur/translate/platform/TtsSpeaker.kt |
| 50 | ### intents/ — assistant IPC |
| 51 | - com/vayunmathur/translate/intents/TranslateIntent.kt — headless `translate_text` provider (per-call `NllbTranslator`, closed after use) |
| 52 | - com/vayunmathur/translate/intents/TranslateContract.kt — validated contract (`validateTranslateQuery`, `capTranslateOutput`, `translateForAssistant` over `TranslationEngine`) |
| 53 | ### ui/ — text, camera, image, picker |
| 54 | - com/vayunmathur/translate/ui/TextTranslateScreen.kt |
| 55 | - com/vayunmathur/translate/ui/TextTranslatePage.kt |
| 56 | - com/vayunmathur/translate/ui/CameraTranslateScreen.kt |
| 57 | - com/vayunmathur/translate/ui/CameraPreviewLayer.kt |
| 58 | - com/vayunmathur/translate/ui/CameraOverlay.kt |
| 59 | - com/vayunmathur/translate/ui/CameraBinding.kt |
| 60 | - com/vayunmathur/translate/ui/CameraFrameUtils.kt |
| 61 | - com/vayunmathur/translate/ui/ImageTranslateScreen.kt |
| 62 | - com/vayunmathur/translate/ui/ImageTranslatePage.kt |
| 63 | - com/vayunmathur/translate/ui/ImagePicker.kt |
| 64 | - com/vayunmathur/translate/ui/LanguagePickerScreen.kt |
| 65 | - com/vayunmathur/translate/ui/LanguagePickerPage.kt |
| 66 | - screenshotTest `ui/MetadataPreviews.kt` — one deterministic capture per `Route` leaf (`1-text`, `2-text-listening`, `3-text-translating`, `4-language-picker`, `5-history`, `6-camera`, `7-image`); `Camera`/`Image` render the shared `CameraOverlay` slice over a fixed stage (no CameraX viewfinder or URI decode in Layoutlib) |
| 67 | |
| 68 | ## Verify (this module only) |
| 69 | ``` |
| 70 | ./gradlew :translate:compileDevKotlin |
| 71 | ./gradlew :translate:lint |
| 72 | ./gradlew :translate:checkMetadata |
| 73 | ``` |