refactor: restructure on-device AI into reusable backend/task layers#2280
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aprompt+ aparse(raw): T.ReplyDrafterPrompt->ReplyDrafterTask : AiTask<List<String>>.OnDeviceAi.run(context, task, image)— picks the best available backend, downscales, generates, parses, returnsAiResult<T>. The selection logic that was inline inReplyDrafterViewModelnow lives here, once.A new AI feature is now just an
AiTask(a prompt + a parser) plus a call toOnDeviceAi.run— no backend code, no selection logic.Also:
MediaPipeBackend.generateis a proper suspend function now — the synchronous-callback bridge inReplyDrafterViewModelis 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; 11ReplyDrafterTaskTesttests pass.Closes #2279