From 21e2b4ab0fb12a9899515db9bdb4e0d3c86bbb63 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Fri, 4 Dec 2026 16:01:52 +0530 Subject: [PATCH] feat(offline-ai): send chat messages so the plugin can use the model's chat format Plugin 1.0.2 builds the prompt from the format stored in the model. Older plugins ignore the extra parameters and use the plain prompt. --- .../helium314/keyboard/latin/ai/ChatParams.kt | 17 ++++++++++++++ .../keyboard/latin/utils/ProofreadService.kt | 3 ++- .../keyboard/latin/ChatParamsTest.kt | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/helium314/keyboard/latin/ai/ChatParams.kt create mode 100644 app/src/test/java/helium314/keyboard/latin/ChatParamsTest.kt diff --git a/app/src/main/java/helium314/keyboard/latin/ai/ChatParams.kt b/app/src/main/java/helium314/keyboard/latin/ai/ChatParams.kt new file mode 100644 index 000000000..1a3a7a44b --- /dev/null +++ b/app/src/main/java/helium314/keyboard/latin/ai/ChatParams.kt @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.latin.ai + +/** + * The request as chat messages, in addition to the plain prompt: a plugin from 1.0.2 on puts them into the chat format + * stored in the model file. Older plugins ignore these parameters. + */ +fun chatParams(instruction: String, text: String, thinking: Boolean): Map { + val (system, user) = when { + instruction.contains("{text}") -> null to instruction.replace("{text}", text) + // no text, e.g. a custom key that only uses the clipboard: the instruction is the request + text.isBlank() -> null to instruction.trim() + else -> instruction.trim() to text + } + val params = mapOf("chat_user" to user, "enable_thinking" to thinking, "show_thinking" to thinking) + return if (system == null) params else params + ("chat_system" to system) +} 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..2033e8de1 100644 --- a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt +++ b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadService.kt @@ -4,6 +4,7 @@ */ package helium314.keyboard.latin.utils +import helium314.keyboard.latin.ai.chatParams import kotlinx.coroutines.ensureActive import kotlinx.coroutines.currentCoroutineContext import helium314.keyboard.latin.ai.TextPieces @@ -515,7 +516,7 @@ class ProofreadService(private val context: Context) { "top_k" to topK, "min_p" to minP.toDouble(), "max_tokens" to effectiveMaxTokens - )) + ) + chatParams(systemPrompt, text, showThinkingVal)) } // Schedule unload after work is done diff --git a/app/src/test/java/helium314/keyboard/latin/ChatParamsTest.kt b/app/src/test/java/helium314/keyboard/latin/ChatParamsTest.kt new file mode 100644 index 000000000..667a2ff17 --- /dev/null +++ b/app/src/test/java/helium314/keyboard/latin/ChatParamsTest.kt @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.latin + +import helium314.keyboard.latin.ai.chatParams +import kotlin.test.Test +import kotlin.test.assertEquals + +class ChatParamsTest { + @Test fun `the instruction is the system message and the text the user message`() { + val params = chatParams(" Fix this. ", "he go", false) + assertEquals("Fix this.", params["chat_system"]) + assertEquals("he go", params["chat_user"]) + assertEquals(false, params["enable_thinking"]) + } + + @Test fun `a placeholder or a missing text leaves only a user message`() { + assertEquals("Shorten he go now", chatParams("Shorten {text} now", "he go", true)["chat_user"]) + assertEquals(null, chatParams("Shorten {text} now", "he go", true)["chat_system"]) + assertEquals("Tell a joke", chatParams("Tell a joke ", "", false)["chat_user"]) + assertEquals(null, chatParams("Tell a joke ", "", false)["chat_system"]) + } +}