Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(threads): persist OOM/backend banner per-thread across restart

Akarshan11f0f28dbf65
diff --git a/web-app/src/containers/dialogs/LlamacppOomListener.tsx b/web-app/src/containers/dialogs/LlamacppOomListener.tsx
index 5b9cf29fe..960c0b4ed 100644
--- a/web-app/src/containers/dialogs/LlamacppOomListener.tsx
+++ b/web-app/src/containers/dialogs/LlamacppOomListener.tsx
@@ -2,8 +2,29 @@ import { useEffect } from 'react'
import { listen } from '@tauri-apps/api/event'
import { useAppState } from '@/hooks/useAppState'
+import { useMessages } from '@/hooks/useMessages'
import { isPlatformTauri } from '@/lib/platform/utils'
+function stampErrorOnLastUserMessage(
+ field: 'oomError' | 'backendError',
+ value: string
+) {
+ const threadId = useAppState.getState().currentStreamThreadId
+ if (!threadId) return
+ const messages = useMessages.getState().getMessages(threadId)
+ for (let i = messages.length - 1; i >= 0; i--) {
+ const m = messages[i]
+ if (m.role !== 'user') continue
+ const meta = (m.metadata as Record<string, unknown> | undefined) ?? {}
+ if (meta[field] === value) return
+ useMessages.getState().updateMessage({
+ ...m,
+ metadata: { ...meta, [field]: value },
+ })
+ return
+ }
+}
+
function clearActiveWork() {
const app = useAppState.getState()
Object.values(app.abortControllers).forEach((c) => {
@@ -27,8 +48,10 @@ export default function LlamacppOomListener() {
useEffect(() => {
if (!isPlatformTauri()) return
const unlistenOom = listen<string>('llamacpp-router-oom', (event) => {
+ const payload = event.payload ?? ''
+ stampErrorOnLastUserMessage('oomError', payload)
clearActiveWork()
- useAppState.getState().setOomError(event.payload ?? '')
+ useAppState.getState().setOomError(payload)
}).catch((e) => {
console.warn('listen llamacpp-router-oom failed:', e)
return () => {}
@@ -36,8 +59,10 @@ export default function LlamacppOomListener() {
const unlistenBackend = listen<string>(
'llamacpp-router-backend-error',
(event) => {
+ const payload = event.payload ?? ''
+ stampErrorOnLastUserMessage('backendError', payload)
clearActiveWork()
- useAppState.getState().setBackendError(event.payload ?? '')
+ useAppState.getState().setBackendError(payload)
}
).catch((e) => {
console.warn('listen llamacpp-router-backend-error failed:', e)
diff --git a/web-app/src/routes/threads/$threadId.tsx b/web-app/src/routes/threads/$threadId.tsx
index 33e967f21..45e13f94c 100644
--- a/web-app/src/routes/threads/$threadId.tsx
+++ b/web-app/src/routes/threads/$threadId.tsx
@@ -617,6 +617,24 @@ function ThreadDetail() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
+ // Resync the OOM/backend banner from message metadata on every thread switch.
+ // Persisted by LlamacppOomListener at error time; unset state when this
+ // thread carries no such metadata so the banner doesn't leak across threads.
+ const threadMessagesForBanner = useMessages((s) => s.messages?.[threadId])
+ useEffect(() => {
+ let oom: string | undefined
+ let be: string | undefined
+ for (const m of threadMessagesForBanner ?? []) {
+ const meta = m.metadata as Record<string, unknown> | undefined
+ const o = meta?.oomError
+ if (typeof o === 'string' && o.length > 0) oom = o
+ const b = meta?.backendError
+ if (typeof b === 'string' && b.length > 0) be = b
+ }
+ useAppState.getState().setOomError(oom)
+ useAppState.getState().setBackendError(be)
+ }, [threadId, threadMessagesForBanner])
+
// Consolidated function to process and send a message
const processAndSendMessage = useCallback(
async (
@@ -829,6 +847,19 @@ function ThreadDetail() {
}
}, [threadId, processAndSendMessage])
+ const stripBannerMetadata = useCallback(() => {
+ const tmsgs = useMessages.getState().getMessages(threadId)
+ for (const m of tmsgs) {
+ const meta = m.metadata as Record<string, unknown> | undefined
+ if (!meta) continue
+ if (meta.oomError == null && meta.backendError == null) continue
+ const nextMeta = { ...meta }
+ delete nextMeta.oomError
+ delete nextMeta.backendError
+ updateMessage({ ...m, metadata: nextMeta })
+ }
+ }, [threadId, updateMessage])
+
// Handle submit from ChatInput
const handleSubmit = useCallback(
async (
@@ -837,21 +868,33 @@ function ThreadDetail() {
) => {
if (oomError) setOomError(undefined)
if (backendError) setBackendError(undefined)
+ if (oomError || backendError) stripBannerMetadata()
await processAndSendMessage(text, files)
},
- [processAndSendMessage, oomError, setOomError, backendError, setBackendError]
+ [
+ processAndSendMessage,
+ oomError,
+ setOomError,
+ backendError,
+ setBackendError,
+ stripBannerMetadata,
+ ]
)
// Handle regenerate from any message (user or assistant)
// - For user messages: keeps the user message, deletes all after, regenerates assistant response
// - For assistant messages: finds the closest preceding user message, deletes from there
const handleRegenerate = useCallback((messageId?: string) => {
+ const hadBannerError =
+ useAppState.getState().oomError != null ||
+ useAppState.getState().backendError != null
if (useAppState.getState().oomError) {
useAppState.getState().setOomError(undefined)
}
if (useAppState.getState().backendError) {
useAppState.getState().setBackendError(undefined)
}
+ if (hadBannerError) stripBannerMetadata()
// Cancel any in-flight title summarization before regenerating
titleAbortRef.current?.abort()
titleAbortRef.current = null
@@ -895,7 +938,7 @@ function ThreadDetail() {
// Call the AI SDK regenerate function - it will handle truncating the UI messages
// and generating a new response from the selected message
regenerate(messageId ? { messageId } : undefined)
- }, [threadId, deleteMessage, regenerate])
+ }, [threadId, deleteMessage, regenerate, stripBannerMetadata])
// Handle edit message - updates the message and regenerates from it
const handleEditMessage = useCallback(
diff --git a/web-app/src/routes/threads/__tests__/$threadId.test.tsx b/web-app/src/routes/threads/__tests__/$threadId.test.tsx
index 46fb0c53f..152339b9e 100644
--- a/web-app/src/routes/threads/__tests__/$threadId.test.tsx
+++ b/web-app/src/routes/threads/__tests__/$threadId.test.tsx
@@ -51,6 +51,8 @@ const h = vi.hoisted(() => {
const appStateState = {
ragToolNames: new Set<string>(),
mcpToolNames: new Set<string>(),
+ setOomError: vi.fn(),
+ setBackendError: vi.fn(),
}
const useAppStateMock: any = (selector: any) => selector(appStateState)
useAppStateMock.getState = () => appStateState