From bd3a9445d93fa5b47dc0f115c3177ee01ed12873 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Fri, 4 Dec 2026 16:01:52 +0530 Subject: [PATCH 1/2] fix(offline-ai): stop the model when a request is cancelled Cancel only dropped the result: the model kept computing and the next request waited for it. Plugin 1.0.2 can stop a generation. --- .../keyboard/latin/ai/OfflineAiLoader.kt | 16 ++++++++++++++++ .../keyboard/latin/utils/ProofreadHelper.kt | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt b/app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt index e05864df8..187ed8f18 100644 --- a/app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt +++ b/app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt @@ -298,6 +298,22 @@ object OfflineAiLoader { /** the number of requests that use the model */ val busy = java.util.concurrent.atomic.AtomicInteger() + /** + * Stops the running generation if the plugin can (1.0.2 on; not in the interface, so called by name). Other plugins + * finish the text in the background, and the next request waits for it. + */ + fun cancelGeneration() { + if (busy.get() == 0) return + val provider = activeProvider ?: return + try { + provider.javaClass.getMethod("cancelGeneration").invoke(provider) + Log.i(TAG, "Generation cancelled in the plugin") + } catch (_: NoSuchMethodException) { + } catch (e: Exception) { + Log.w(TAG, "Could not cancel the generation in the plugin", e) + } + } + fun onTrimMemory() { if (busy.get() > 0) { Log.i(TAG, "Skipping memory trim: offline AI is currently busy") diff --git a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt index f6f1f5f8f..724159d37 100644 --- a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt +++ b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt @@ -60,7 +60,8 @@ object ProofreadHelper { */ fun cancelCurrentOperation() { if (currentJob?.isActive == true) { - currentJob?.cancel() + currentJob?.cancel() // the result is dropped + helium314.keyboard.latin.ai.OfflineAiLoader.cancelGeneration() // and the model stops computing it currentJob = null mainHandler.post { KeyboardSwitcher.getInstance().hideLoadingAnimation() From 244162760e6406ae71aa6fc093ba0b4a3fdd28ef Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Fri, 4 Dec 2026 16:08:22 +0530 Subject: [PATCH 2/2] fix(offline-ai): do not log a cancelled request as a failure --- .../java/helium314/keyboard/latin/utils/ProofreadService.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt index e4921007e..83f8f9d82 100644 --- a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt +++ b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt @@ -555,7 +555,8 @@ class ProofreadService(private val context: Context) { } } catch (e: Throwable) { - Log.e(TAG, "Proofread failed", e) + // a stopped generation throws too + if (isActive) Log.e(TAG, "Proofread failed", e) else Log.i(TAG, "Proofread cancelled") ModelHolder.scheduleUnload(context) // Ensure we still schedule unload on error Result.failure(ProofreadException(e.message ?: "Unknown error")) }