Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
Loading