Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

feat: implement LLM client caching in CallSessionController and add stable prompt caching for voice calls

Rishabh Bajpai50318be689cb
diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
index 005cd8c..4ef1eb8 100644
--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt
@@ -76,6 +76,14 @@ class AgentEngine(
var sessionId: String? = null
var tokenCount: Int = 0
+ /**
+ * Stable key for the provider's server-side prompt KV cache
+ * (`prompt_cache_key` / `X-Session-Id`), independent of [sessionId] which
+ * also serves as per-session file identity. Defaults to [sessionId] when
+ * unset (chat mode keeps the chat id, which is already stable across turns).
+ */
+ var promptCacheKey: String? = null
+
/**
* Structured records of completed runs in this session, newest last. Fed to
* the marketing copy LLM call for the "Share your Gotcha moment" poster.
@@ -455,7 +463,7 @@ class AgentEngine(
llm.chat(
messages,
ToolRegistry.toolsForAgent(agent, hiddenTools()),
- sessionId = sessionId
+ sessionId = promptCacheKey ?: sessionId
)
} catch (e: CancellationException) {
throw e
diff --git a/app/src/main/java/com/gotcha/service/CallSessionController.kt b/app/src/main/java/com/gotcha/service/CallSessionController.kt
index 2084aaa..bd5a141 100644
--- a/app/src/main/java/com/gotcha/service/CallSessionController.kt
+++ b/app/src/main/java/com/gotcha/service/CallSessionController.kt
@@ -74,6 +74,20 @@ class CallSessionController(
private var currentTurnJob: Job? = null
private var nextTranscriptId = 0L
+ /**
+ * Reused LLM client so the in-memory [com.gotcha.llm.LLMCache] survives
+ * across turns within a call (mirrors ChatViewModel's single `client`).
+ * Rebuilt only when the settings it depends on change; tracked by
+ * [cachedClientFingerprint] so [buildClient] can detect staleness.
+ * @Volatile because [buildClient] is reached from both the main thread
+ * (startCall) and the engine's coroutine (clientProvider).
+ */
+ @Volatile
+ private var cachedClient: LLMClient? = null
+
+ @Volatile
+ private var cachedClientFingerprint: String? = null
+
/** Gate for [awaitQuestionAnswer]: completed when the user taps mic stop. */
private var questionGate: CompletableDeferred<String>? = null
@@ -149,6 +163,11 @@ class CallSessionController(
)
newEngine.sessionId = java.util.UUID.randomUUID().toString()
newEngine.callMode = true
+ // Stable server-side prompt-cache key shared by every call: the static
+ // system prompt + <env> prefix is byte-identical across calls, so the
+ // provider can reuse its cached KV instead of re-warming per call. The
+ // random sessionId above stays as the per-call file/working-dir identity.
+ newEngine.promptCacheKey = CALL_PROMPT_CACHE_KEY
newEngine.setupWorkingDir()
engine = newEngine
_state.value = CallState.STARTING
@@ -550,16 +569,24 @@ class CallSessionController(
AudioProvider.ANDROID -> null
}
- private fun buildClient(): LLMClient? {
+ internal fun buildClient(): LLMClient? {
val s = settingsRepository.load()
return if (s.isConfigured) {
- LLMClient(
- apiKey = s.effectiveApiKey,
- baseUrl = s.effectiveBaseUrl,
- model = s.model,
- context = appContext,
- apiTimeoutSeconds = s.apiTimeoutSeconds
- )
+ val fingerprint = "${s.provider}|${s.effectiveApiKey}|${s.effectiveBaseUrl}|${s.model}|${s.apiTimeoutSeconds}"
+ if (cachedClient != null && cachedClientFingerprint == fingerprint) {
+ cachedClient
+ } else {
+ LLMClient(
+ apiKey = s.effectiveApiKey,
+ baseUrl = s.effectiveBaseUrl,
+ model = s.model,
+ context = appContext,
+ apiTimeoutSeconds = s.apiTimeoutSeconds
+ ).also {
+ cachedClient = it
+ cachedClientFingerprint = fingerprint
+ }
+ }
} else {
null
}
@@ -616,6 +643,15 @@ class CallSessionController(
private const val NARRATION_THROTTLE_MS = 3_000L
private const val CONFIRM_TIMEOUT_MS = 90_000L
val CALLS_WORKING_ROOT: String get() = com.gotcha.data.GotchaStorage.callsRoot().absolutePath
+
+ /**
+ * Stable server-side prompt-cache key for every voice call. The static
+ * system-prompt prefix is identical across calls, so sharing one key lets
+ * the provider reuse the cached KV instead of re-warming it per call.
+ * Distinct from the per-call random [AgentEngine.sessionId] (file identity).
+ */
+ private const val CALL_PROMPT_CACHE_KEY = "gotcha_voice_call"
+
private const val CAPTURE_SETTLE_MS = 350L
private const val QUESTION_TIMEOUT_MS = 30_000L
private const val TOOL_TEXT_LIMIT = 300
diff --git a/app/src/test/java/com/gotcha/service/CallSessionControllerTest.kt b/app/src/test/java/com/gotcha/service/CallSessionControllerTest.kt
index 2b32da9..7f24d62 100644
--- a/app/src/test/java/com/gotcha/service/CallSessionControllerTest.kt
+++ b/app/src/test/java/com/gotcha/service/CallSessionControllerTest.kt
@@ -115,4 +115,43 @@ class CallSessionControllerTest {
assertTrue("Expected startCall to succeed but got error: $errorMessage", started)
assertTrue(errorMessage == null)
}
+
+ @Test
+ fun `buildClient returns the same instance while settings are unchanged`() {
+ settingsRepository.save(
+ Settings(
+ provider = LlmProvider.OPENAI_COMPATIBLE,
+ baseUrl = "https://api.openai.com/v1",
+ apiKey = "llm-key"
+ )
+ )
+
+ val first = controller.buildClient()
+ val second = controller.buildClient()
+
+ assertTrue("cached LLMClient must be reused for an unchanged fingerprint", first === second)
+ }
+
+ @Test
+ fun `buildClient rebuilds when the settings fingerprint changes`() {
+ settingsRepository.save(
+ Settings(
+ provider = LlmProvider.OPENAI_COMPATIBLE,
+ baseUrl = "https://api.openai.com/v1",
+ apiKey = "llm-key"
+ )
+ )
+ val first = controller.buildClient()
+
+ settingsRepository.save(
+ Settings(
+ provider = LlmProvider.OPENAI_COMPATIBLE,
+ baseUrl = "https://api.openai.com/v1",
+ apiKey = "changed-key"
+ )
+ )
+ val second = controller.buildClient()
+
+ assertTrue("a changed API key must force a client rebuild", first !== second)
+ }
}