Skip to content

feat: AI next-word suggestions (on-device first, default off) - #422

Draft
Lloyd-Jackman-UKPL wants to merge 2 commits into
LeanBitLab:mainfrom
Lloyd-Jackman-UKPL:feat/ai-next-word
Draft

feat: AI next-word suggestions (on-device first, default off)#422
Lloyd-Jackman-UKPL wants to merge 2 commits into
LeanBitLab:mainfrom
Lloyd-Jackman-UKPL:feat/ai-next-word

Conversation

@Lloyd-Jackman-UKPL

Copy link
Copy Markdown

AI next-word suggestions (on-device first, off by default)

Implements Closes #421 — an AI candidate source that adds LLM-driven next-word suggestions to the existing AOSP suggestion strip. Functional across all three flavors, disabled by default (pref_ai_next_word = false); when off, behaviour is identical to before.

What it does

  • AINextWordDictionary (dictionary/AINextWordDictionary.kt) — a Dictionary subclass that only contributes in next-word mode (composedData.mTypedWord empty, i.e. exactly when the existing AOSP prediction hook runs after each word commit). It builds a prompt from the preceding words (NgramContext.extractPrevWordsContext), serves cached candidates, and kicks an async LLM fetch on its own coroutine scope — never blocks the suggestion thread. AOSP suggestions always stay primary (isInitialized() is false until the engine is ready).
  • AINextWordEngine interface + per-flavor AINextWordEngineFactory:
    • offline — real on-device causal completion via the embedded llama.cpp/GGUF runtime (LlamaHelper/ModelHolder already used by proofreading), plain-text prompt, no network, single shared loaded model (no double-load).
    • standard — OpenAI-compatible chat completion (reuses the proofreading HTTP path/endpoint/provider).
    • offlinelite — no-op (flavor ships no AI).
  • Registration in DictionaryFacilitatorImpl.getSuggestions, next-word mode only, gated by the pref at dictionary reset.
  • Settings toggle "AI next-word suggestions" (AI Integration + Advanced screens) + string resources.
  • Unit tests (app/src/test/.../AINextWordDictionaryTest.kt, 8 cases) covering prompt building + candidate parsing + dedupe/cap — pure JVM, no instrumented APIs.

On your device

Toggle AI next-word suggestions on, then (for offline: load a GGUF model) — candidates appear after the current word alongside AOSP suggestions.

Known limitations (draft)

  • No live re-suggest on the current word yet: the async fetch fills the per-context cache, but fresh LLM candidates surface on the next prediction pass rather than nudging a mid-pass refresh (postUpdateSuggestions). Usable, but not "instant" — flagged for a follow-up.
  • No per-request rate-limit: the LRU cache bounds memory, not call frequency.
  • Offline path is reflection-based off ModelHolder by design (reuse one loaded model), and needs tuning on a real device (params, top-k choice, timeout).
  • Prompt/candidate quality is naive (single continuation → first 3 tokens) — deliberately minimal for this first PR.

Build + packaging verified locally for all three flavors (offline / standard / offlinelite) plus the unit tests.

AI next-word prediction source added to the suggestion strip, off by default.
- AINextWordDictionary: async candidate source active only in next-word mode,
  cache-backed, never blocks the suggestion thread; AOSP suggestions stay primary.
- AINextWordEngine + flavor factories: offline = on-device GGUF causal completion
  (reuses proofread ModelHolder, no network); standard = OpenAI-compatible chat;
  offlinelite = no-op.
- Registered in DictionaryFacilitatorImpl gated by pref_ai_next_word; toggle in
  AI integration / advanced settings; unit tests for prompt + candidate parsing.
Include the AI next-word pref in usesSameSettings so toggling it triggers a
dictionary reset; previously the AI source would not (re)build until the IME
restarted, so the feature appeared not to activate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: AI next-word suggestions (on-device first)

1 participant