fix(chat): fetch context usage immediately on session change/connect (#746)

Clears stale context token states on session switch/creation and fetches

fresh context usage info immediately instead of waiting for the 5s poll.

Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andM57 authored and GitHub committed 2026-07-30 11:37:36 +03:00
1 parent 1b596746af
commit 698d2f6c3f
2 files changed
+16

No files matched your search

@@ -134,6 +134,10 @@ fun ChatScreen(
// Periodic session sync while connected. // Periodic session sync while connected.
LaunchedEffect(state.currentSessionId, state.connectionStatus) { LaunchedEffect(state.currentSessionId, state.connectionStatus) {
if (state.currentSessionId != null && state.connectionStatus == ConnectionStatus.CONNECTED) {
viewModel.syncCurrentSession()
viewModel.fetchContextUsage()
}
while (state.currentSessionId != null && state.connectionStatus == ConnectionStatus.CONNECTED) { while (state.currentSessionId != null && state.connectionStatus == ConnectionStatus.CONNECTED) {
delay(SESSION_SYNC_INTERVAL_MS) delay(SESSION_SYNC_INTERVAL_MS)
viewModel.syncCurrentSession() viewModel.syncCurrentSession()
@@ -595,6 +595,9 @@ class ChatViewModel(
isLoading = false, isLoading = false,
messages = emptyList(), messages = emptyList(),
chatTitle = "Hermes", chatTitle = "Hermes",
usedContextTokens = null,
fullContextTokens = null,
contextBreakdown = null,
) )
} }
// Mirror the active session id app-wide so session-scoped // Mirror the active session id app-wide so session-scoped
@@ -604,6 +607,7 @@ class ChatViewModel(
_streamingState.update { StreamingState() } _streamingState.update { StreamingState() }
addSystemMessage("Session created", persist = true) addSystemMessage("Session created", persist = true)
loadSessions() loadSessions()
fetchContextUsage()
} }
WsMethods.SESSION_BRANCH -> { WsMethods.SESSION_BRANCH -> {
@@ -616,6 +620,9 @@ class ChatViewModel(
isLoading = false, isLoading = false,
messages = emptyList(), messages = emptyList(),
chatTitle = (resultMap["title"] as? String)?.takeIf { t -> t.isNotBlank() } ?: "Hermes", chatTitle = (resultMap["title"] as? String)?.takeIf { t -> t.isNotBlank() } ?: "Hermes",
usedContextTokens = null,
fullContextTokens = null,
contextBreakdown = null,
) )
} }
ActiveSessionHolder.set(newId) ActiveSessionHolder.set(newId)
@@ -623,6 +630,7 @@ class ChatViewModel(
addSystemMessage("Session branched", persist = true) addSystemMessage("Session branched", persist = true)
loadSessionMessages(newId) loadSessionMessages(newId)
loadSessions() loadSessions()
fetchContextUsage()
} }
WsMethods.SESSION_LIST -> { WsMethods.SESSION_LIST -> {
@@ -684,6 +692,7 @@ class ChatViewModel(
// Mirror the active runtime session id app-wide (issue #532). // Mirror the active runtime session id app-wide (issue #532).
ActiveSessionHolder.set(runtimeSessionId ?: sessionId) ActiveSessionHolder.set(runtimeSessionId ?: sessionId)
addSystemMessage("Session resumed") addSystemMessage("Session resumed")
fetchContextUsage()
} }
WsMethods.SESSION_INTERRUPT -> { WsMethods.SESSION_INTERRUPT -> {
@@ -1447,6 +1456,9 @@ class ChatViewModel(
chatTitle = title, chatTitle = title,
showSessionPicker = false, showSessionPicker = false,
isAgentTyping = false, isAgentTyping = false,
usedContextTokens = null,
fullContextTokens = null,
contextBreakdown = null,
) )
} }
// Mirror the active session id app-wide (issue #532). // Mirror the active session id app-wide (issue #532).