Skip to content

refactor: restructure on-device AI into reusable backend/task layers#2280

Merged
enaboapps merged 1 commit into
mainfrom
refactor/reusable-on-device-ai-2279
May 22, 2026
Merged

refactor: restructure on-device AI into reusable backend/task layers#2280
enaboapps merged 1 commit into
mainfrom
refactor/reusable-on-device-ai-2279

Conversation

@enaboapps

Copy link
Copy Markdown
Collaborator

Summary

Restructures the on-device AI so features beyond the Reply Drafter can reuse it. The two backends previously hardcoded the Reply Drafter prompt and parser; a second AI feature would have meant duplicating backend methods and re-implementing the AICore-vs-MediaPipe selection logic.

Three layers:

  • AiBackend — a generic on-device model: availability() + generate(prompt, image): String. AiCoreManager -> AiCoreBackend, LlmManager -> MediaPipeBackend; neither knows about any feature now.
  • AiTask<T> — one feature is a prompt + a parse(raw): T. ReplyDrafterPrompt -> ReplyDrafterTask : AiTask<List<String>>.
  • OnDeviceAi.run(context, task, image) — picks the best available backend, downscales, generates, parses, returns AiResult<T>. The selection logic that was inline in ReplyDrafterViewModel now lives here, once.

A new AI feature is now just an AiTask (a prompt + a parser) plus a call to OnDeviceAi.run — no backend code, no selection logic.

Also: MediaPipeBackend.generate is a proper suspend function now — the synchronous-callback bridge in ReplyDrafterViewModel is gone.

No user-facing change — the Reply Drafter behaves identically, routed through OnDeviceAi.run(ReplyDrafterTask, ...). Kept deliberately minimal: no task registry, streaming, or config DSL.

Test plan

  • ./gradlew compileDebugKotlin testDebugUnitTest — compiles; 11 ReplyDrafterTaskTest tests pass.
  • On-device: the Reply Drafter still drafts and copies replies (AICore and MediaPipe paths unchanged in behaviour).

Closes #2279

Split the on-device AI into transport/task/orchestration layers so
features beyond the Reply Drafter can reuse it. No user-facing change.

- AiBackend: a generic on-device model (availability + generate); the
  backends (AiCoreBackend, MediaPipeBackend) no longer know any feature.
- AiTask<T>: one feature is a prompt plus a parser (ReplyDrafterTask).
- OnDeviceAi.run(): picks the best backend, downscales, generates and
  parses — the selection logic that was inline in ReplyDrafterViewModel.
- MediaPipeBackend.generate is now a suspend function, dropping the
  synchronous-callback bridge.
@enaboapps enaboapps added this to the v2.25.0 milestone May 22, 2026
@enaboapps enaboapps merged commit a7d04d9 into main May 22, 2026
@enaboapps enaboapps deleted the refactor/reusable-on-device-ai-2279 branch May 22, 2026 18:08
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.

Restructure on-device AI into a reusable backend/task/orchestrator layer

2 participants