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(chat): keep embedding shimmer across thread navigation

Akarshan Biswas40d54d6bfdb3
diff --git a/web-app/src/hooks/useAppState.ts b/web-app/src/hooks/useAppState.ts
index a7fb0fefb..beda84ff7 100644
--- a/web-app/src/hooks/useAppState.ts
+++ b/web-app/src/hooks/useAppState.ts
@@ -35,6 +35,7 @@ type AppState = {
cancelToolCalls: Record<string, () => void>
errorMessages: Record<string, AppErrorMessage>
busyThreads: Record<string, boolean>
+ embeddingThreads: Record<string, boolean>
currentStreamThreadId?: string
oomError?: string
backendError?: string
@@ -76,6 +77,7 @@ type AppState = {
clearThreadState: (threadId: string) => void
setCurrentStreamThreadId: (threadId: string | undefined) => void
setThreadBusy: (threadId: string, busy: boolean) => void
+ setThreadEmbedding: (threadId: string, embedding: boolean) => void
}
export const useAppState = create<AppState>()((set) => ({
@@ -95,6 +97,7 @@ export const useAppState = create<AppState>()((set) => ({
cancelToolCalls: {},
errorMessages: {},
busyThreads: {},
+ embeddingThreads: {},
currentStreamThreadId: undefined,
setCurrentStreamThreadId: (threadId) => set({ currentStreamThreadId: threadId }),
setOomError: (line) => set({ oomError: line }),
@@ -106,6 +109,13 @@ export const useAppState = create<AppState>()((set) => ({
else delete next[threadId]
return { busyThreads: next }
}),
+ setThreadEmbedding: (threadId, embedding) =>
+ set((state) => {
+ const next = { ...state.embeddingThreads }
+ if (embedding) next[threadId] = true
+ else delete next[threadId]
+ return { embeddingThreads: next }
+ }),
updateStreamingContent: (content: ThreadMessage | undefined) => {
set(() => ({
streamingContent: content
@@ -220,12 +230,14 @@ export const useAppState = create<AppState>()((set) => ({
const cancelToolCalls = { ...state.cancelToolCalls }
const errorMessages = { ...state.errorMessages }
const busyThreads = { ...state.busyThreads }
+ const embeddingThreads = { ...state.embeddingThreads }
delete streamingContents[threadId]
delete loadingModels[threadId]
delete promptProgresses[threadId]
delete cancelToolCalls[threadId]
delete errorMessages[threadId]
delete busyThreads[threadId]
+ delete embeddingThreads[threadId]
return {
streamingContents,
loadingModels,
@@ -233,6 +245,7 @@ export const useAppState = create<AppState>()((set) => ({
cancelToolCalls,
errorMessages,
busyThreads,
+ embeddingThreads,
}
}),
}))
diff --git a/web-app/src/routes/threads/$threadId.tsx b/web-app/src/routes/threads/$threadId.tsx
index 4f36571aa..208e2c568 100644
--- a/web-app/src/routes/threads/$threadId.tsx
+++ b/web-app/src/routes/threads/$threadId.tsx
@@ -194,7 +194,11 @@ function ThreadDetail() {
const [pendingContinueMessage, setPendingContinueMessage] =
useState<UIMessage | null>(null)
const [contextLimitError, setContextLimitError] = useState<Error | null>(null)
- const [processingEmbeddings, setProcessingEmbeddings] = useState(false)
+ // Per-thread so the shimmer survives navigating away and back while the
+ // embedding run is still in flight.
+ const processingEmbeddings = useAppState(
+ (s) => !!s.embeddingThreads[threadId]
+ )
const { t } = useTranslation()
// llama-server's overflow string is raw English; localize it, interpolating
@@ -842,7 +846,7 @@ function ThreadDetail() {
const projectId = thread?.metadata?.project?.id
if (combinedAttachments.length > 0) {
if (hasEmbeddingDocuments) {
- setProcessingEmbeddings(true)
+ useAppState.getState().setThreadEmbedding(threadId, true)
useAppState.getState().setThreadBusy(threadId, true)
}
try {
@@ -878,7 +882,7 @@ function ThreadDetail() {
}
return
} finally {
- setProcessingEmbeddings(false)
+ useAppState.getState().setThreadEmbedding(threadId, false)
useAppState.getState().setThreadBusy(threadId, false)
}
}
diff --git a/web-app/src/routes/threads/__tests__/$threadId.test.tsx b/web-app/src/routes/threads/__tests__/$threadId.test.tsx
index 212411014..4c2b55f86 100644
--- a/web-app/src/routes/threads/__tests__/$threadId.test.tsx
+++ b/web-app/src/routes/threads/__tests__/$threadId.test.tsx
@@ -53,6 +53,10 @@ const h = vi.hoisted(() => {
mcpToolNames: new Set<string>(),
setOomError: vi.fn(),
setBackendError: vi.fn(),
+ busyThreads: {} as Record<string, boolean>,
+ embeddingThreads: {} as Record<string, boolean>,
+ setThreadBusy: vi.fn(),
+ setThreadEmbedding: vi.fn(),
}
const useAppStateMock: any = (selector: any) => selector(appStateState)
useAppStateMock.getState = () => appStateState