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(cowork): show model loading progress and relabel encoder stage

Akarshan Biswas63a2e53dc028
diff --git a/web-app/src/components/PromptProgress.tsx b/web-app/src/components/PromptProgress.tsx
index bebdd0bf4..bd89f7cd6 100644
--- a/web-app/src/components/PromptProgress.tsx
+++ b/web-app/src/components/PromptProgress.tsx
@@ -26,6 +26,10 @@ export function PromptProgress({
(threadId ? state.promptProgresses[threadId] : undefined) ??
state.promptProgress
)
+ // Cowork keeps its load state in useCoworkRun's session-keyed mirror (fed by
+ // CoworkChatTransport and the load-progress listener), so a cowork surface
+ // reads only that. A chat surface reads useAppState's thread slot, falling
+ // back to the global slot for an off-thread stream.
const chatLoadingModel = useAppState((state) =>
(threadId ? state.loadingModels[threadId] : undefined) ?? state.loadingModel
)
@@ -63,7 +67,7 @@ export function PromptProgress({
loadingModel && loadProgress ? Math.round(loadProgress.value * 100) : undefined
// Only worth naming the stage when the load actually has more than one
- // (vision encoder and/or speculative-decoding draft model on top of the
+ // (multimodal encoder and/or speculative-decoding draft model on top of the
// main weights) - a plain text-only load is always a single "text_model"
// stage for its entire duration, so calling that out would be noise.
const stageLabel =
@@ -115,7 +119,7 @@ function describeLoadStage(stage: string | undefined): string | undefined {
case 'text_model':
return 'text model'
case 'mmproj_model':
- return 'vision encoder'
+ return 'encoder'
case 'spec_model':
return 'draft model'
default:
diff --git a/web-app/src/components/__tests__/PromptProgress.test.tsx b/web-app/src/components/__tests__/PromptProgress.test.tsx
index 1924f07d9..ef4baba1f 100644
--- a/web-app/src/components/__tests__/PromptProgress.test.tsx
+++ b/web-app/src/components/__tests__/PromptProgress.test.tsx
@@ -113,7 +113,7 @@ describe('PromptProgress', () => {
render(<PromptProgress />)
- expect(screen.getByText('Loading vision encoder: 80%')).toBeInTheDocument()
+ expect(screen.getByText('Loading encoder: 80%')).toBeInTheDocument()
})
it('should not name the stage for a plain single-stage text-only load', () => {
diff --git a/web-app/src/containers/dialogs/LlamacppOomListener.tsx b/web-app/src/containers/dialogs/LlamacppOomListener.tsx
index 7ccc2ad13..433eb6b75 100644
--- a/web-app/src/containers/dialogs/LlamacppOomListener.tsx
+++ b/web-app/src/containers/dialogs/LlamacppOomListener.tsx
@@ -2,6 +2,7 @@ import { useEffect } from 'react'
import { listen } from '@tauri-apps/api/event'
import { useAppState } from '@/hooks/useAppState'
+import { useCoworkRun } from '@/hooks/useCoworkRun'
import { isPlatformTauri } from '@/lib/platform/utils'
import {
clearActiveWork,
@@ -52,8 +53,15 @@ export default function LlamacppOomListener() {
(event) => {
const { model, stage, stages, value } = event.payload
const progress = { modelId: model, stage, stages, value }
+ const { currentStreamThreadId: threadId, currentStreamIsCowork } =
+ useAppState.getState()
+ // A Cowork run owns the current stream: feed its session mirror, not
+ // this store's global/thread slots (which chat surfaces read).
+ if (threadId && currentStreamIsCowork) {
+ useCoworkRun.getState().setSessionModelLoadProgress(threadId, progress)
+ return
+ }
useAppState.getState().updateModelLoadProgress(progress)
- const threadId = useAppState.getState().currentStreamThreadId
if (threadId) {
useAppState.getState().updateThreadModelLoadProgress(threadId, progress)
}
diff --git a/web-app/src/containers/dialogs/__tests__/LlamacppOomListener.test.tsx b/web-app/src/containers/dialogs/__tests__/LlamacppOomListener.test.tsx
index 2847298b0..dcaf940d5 100644
--- a/web-app/src/containers/dialogs/__tests__/LlamacppOomListener.test.tsx
+++ b/web-app/src/containers/dialogs/__tests__/LlamacppOomListener.test.tsx
@@ -3,6 +3,7 @@ import { render } from '@testing-library/react'
import { act } from '@testing-library/react'
import LlamacppOomListener from '../LlamacppOomListener'
import { useAppState } from '@/hooks/useAppState'
+import { useCoworkRun } from '@/hooks/useCoworkRun'
let loadProgressHandler: ((event: { payload: unknown }) => void) | undefined
let unloadHandler: ((event: { payload: unknown }) => void) | undefined
@@ -31,7 +32,9 @@ describe('LlamacppOomListener - model load progress', () => {
modelLoadProgress: undefined,
modelLoadProgressByThread: {},
currentStreamThreadId: undefined,
+ currentStreamIsCowork: false,
})
+ useCoworkRun.setState({ modelLoadProgress: {} })
})
})
@@ -82,6 +85,35 @@ describe('LlamacppOomListener - model load progress', () => {
value: 0.3,
})
})
+
+ it('routes to the cowork mirror (not useAppState) for a cowork stream', async () => {
+ act(() => {
+ useAppState.setState({
+ currentStreamThreadId: 'session-1',
+ currentStreamIsCowork: true,
+ })
+ })
+ render(<LlamacppOomListener />)
+ await act(async () => {
+ await Promise.resolve()
+ })
+
+ act(() => {
+ loadProgressHandler?.({
+ payload: { model: 'model-1', value: 0.6 },
+ })
+ })
+
+ expect(useCoworkRun.getState().modelLoadProgress['session-1']).toEqual({
+ modelId: 'model-1',
+ stage: undefined,
+ value: 0.6,
+ })
+ expect(useAppState.getState().modelLoadProgress).toBeUndefined()
+ expect(
+ useAppState.getState().modelLoadProgressByThread['session-1']
+ ).toBeUndefined()
+ })
})
describe('LlamacppOomListener - model unloaded', () => {
diff --git a/web-app/src/hooks/useAppState.ts b/web-app/src/hooks/useAppState.ts
index 89341fab3..6efacdae3 100644
--- a/web-app/src/hooks/useAppState.ts
+++ b/web-app/src/hooks/useAppState.ts
@@ -55,6 +55,10 @@ type AppState = {
busyThreads: Record<string, boolean>
embeddingThreads: Record<string, boolean>
currentStreamThreadId?: string
+ // The stream named by currentStreamThreadId is a Cowork run, so its
+ // load-progress events route to useCoworkRun's session mirror rather than
+ // this store's thread-keyed slots (which feed chat-thread active detection).
+ currentStreamIsCowork: boolean
oomError?: string
backendError?: string
@@ -104,7 +108,10 @@ type AppState = {
error: AppErrorMessage | undefined
) => void
clearThreadState: (threadId: string) => void
- setCurrentStreamThreadId: (threadId: string | undefined) => void
+ setCurrentStreamThreadId: (
+ threadId: string | undefined,
+ isCowork?: boolean
+ ) => void
setThreadBusy: (threadId: string, busy: boolean) => void
setThreadEmbedding: (threadId: string, embedding: boolean) => void
}
@@ -130,7 +137,12 @@ export const useAppState = create<AppState>()((set) => ({
busyThreads: {},
embeddingThreads: {},
currentStreamThreadId: undefined,
- setCurrentStreamThreadId: (threadId) => set({ currentStreamThreadId: threadId }),
+ currentStreamIsCowork: false,
+ setCurrentStreamThreadId: (threadId, isCowork = false) =>
+ set({
+ currentStreamThreadId: threadId,
+ currentStreamIsCowork: threadId ? isCowork : false,
+ }),
setOomError: (line) => set({ oomError: line }),
setBackendError: (line) => set({ backendError: line }),
setThreadBusy: (threadId, busy) =>
diff --git a/web-app/src/lib/coworkTransport.ts b/web-app/src/lib/coworkTransport.ts
index 814928ab5..03dfb2328 100644
--- a/web-app/src/lib/coworkTransport.ts
+++ b/web-app/src/lib/coworkTransport.ts
@@ -1,5 +1,7 @@
import type { Tool } from 'ai'
import { CustomChatTransport } from '@/lib/custom-chat-transport'
+import { useCoworkRun } from '@/hooks/useCoworkRun'
+import type { ModelLoadProgress } from '@/hooks/useAppState'
import { CHAT_SLOT_ID, coworkThreadId } from '@/constants/models'
import {
getMemoryCatalog,
@@ -64,6 +66,28 @@ export class CoworkChatTransport extends CustomChatTransport {
this.config = config
}
+ /**
+ * Load state (and the `llamacpp-model-load-progress` events keyed off
+ * `currentStreamThreadId`) route to useCoworkRun's session mirror, keyed by
+ * session id — never into useAppState's thread-keyed slots, which drive
+ * chat-thread active detection. `threadId` here is the session id (the value
+ * passed to the base constructor).
+ */
+ protected override get streamRoutesToCowork(): boolean {
+ return true
+ }
+
+ protected override setLoadingModel(threadId: string, loading: boolean): void {
+ useCoworkRun.getState().setSessionLoadingModel(threadId, loading)
+ }
+
+ protected override setModelLoadProgress(
+ threadId: string,
+ progress: ModelLoadProgress | undefined
+ ): void {
+ useCoworkRun.getState().setSessionModelLoadProgress(threadId, progress)
+ }
+
/** Drop the freeze so the next run re-reads the config. */
unfreezeTools() {
this.frozenTools = null
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index bcecf5ec5..c192e800d 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -38,7 +38,7 @@ import {
refreshMemoryDigest,
sandboxEnforces,
} from '@/lib/agentTools'
-import { useAppState } from '@/hooks/useAppState'
+import { useAppState, type ModelLoadProgress } from '@/hooks/useAppState'
import { unloadLlamaModel, getLoadedModels } from '@janhq/tauri-plugin-llamacpp-api'
import { engineFailure } from '@/lib/engineError'
import { ExtensionManager } from '@/lib/extension'
@@ -837,6 +837,32 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
this.lastUserMessage = message
}
+ /**
+ * Whether this transport's load state and its `llamacpp-model-load-progress`
+ * events belong to a Cowork run. Cowork overrides the two writers below and
+ * this flag so the load card is fed from useCoworkRun's session mirror,
+ * keeping session ids out of useAppState's thread-keyed slots (which drive
+ * chat-thread active detection).
+ */
+ protected get streamRoutesToCowork(): boolean {
+ return false
+ }
+
+ /** Route the model-loading flag. Base writes useAppState (global + thread). */
+ protected setLoadingModel(threadId: string, loading: boolean): void {
+ useAppState.getState().updateLoadingModel(loading)
+ useAppState.getState().updateThreadLoadingModel(threadId, loading)
+ }
+
+ /** Route model-load progress. Base writes useAppState (global + thread). */
+ protected setModelLoadProgress(
+ threadId: string,
+ progress: ModelLoadProgress | undefined
+ ): void {
+ useAppState.getState().updateModelLoadProgress(progress)
+ useAppState.getState().updateThreadModelLoadProgress(threadId, progress)
+ }
+
updateSystemMessage(systemMessage: string | undefined) {
this.systemMessage = systemMessage
}
@@ -1248,7 +1274,9 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
): Promise<ReadableStream<UIMessageChunk>> {
const threadId = this.threadId ?? options.chatId
const myGeneration = ++this.streamGeneration
- useAppState.getState().setCurrentStreamThreadId(threadId)
+ useAppState
+ .getState()
+ .setCurrentStreamThreadId(threadId, this.streamRoutesToCowork)
// Capture the effective provider name early so the Anthropic serial
// tool-use repair later uses the same value that was used to create the
// model, even if the user switches provider mid-request.
@@ -1284,10 +1312,8 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
try {
const loaded = await getLoadedModels()
if (!loaded.includes(modelId)) {
- useAppState.getState().updateLoadingModel(true)
- useAppState.getState().updateThreadLoadingModel(threadId, true)
- useAppState.getState().updateModelLoadProgress(undefined)
- useAppState.getState().updateThreadModelLoadProgress(threadId, undefined)
+ this.setLoadingModel(threadId, true)
+ this.setModelLoadProgress(threadId, undefined)
}
} catch {
// Ignore probe failures; the router will still load on demand
@@ -1336,15 +1362,11 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
providerId,
options.abortSignal
)
- useAppState.getState().updateLoadingModel(false)
- useAppState.getState().updateThreadLoadingModel(threadId, false)
- useAppState.getState().updateModelLoadProgress(undefined)
- useAppState.getState().updateThreadModelLoadProgress(threadId, undefined)
+ this.setLoadingModel(threadId, false)
+ this.setModelLoadProgress(threadId, undefined)
} catch (error) {
- useAppState.getState().updateLoadingModel(false)
- useAppState.getState().updateThreadLoadingModel(threadId, false)
- useAppState.getState().updateModelLoadProgress(undefined)
- useAppState.getState().updateThreadModelLoadProgress(threadId, undefined)
+ this.setLoadingModel(threadId, false)
+ this.setModelLoadProgress(threadId, undefined)
console.error('Failed to create model:', error)
// Preserve AbortError identity so callers/UI can tell a user-initiated
// Stop from an actual model-load failure.
@@ -1604,9 +1626,8 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
// state the newer request already owns.
if (this.streamGeneration === myGeneration) {
useAppState.getState().updatePromptProgress(undefined)
- useAppState.getState().updateLoadingModel(false)
useAppState.getState().updateThreadPromptProgress(threadId, undefined)
- useAppState.getState().updateThreadLoadingModel(threadId, false)
+ this.setLoadingModel(threadId, false)
useAppState.getState().updateLiveTokenStats(undefined)
useAppState.getState().updateThreadLiveTokenStats(threadId, undefined)
if (useAppState.getState().currentStreamThreadId === threadId) {
@@ -1632,9 +1653,8 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
onFinish: ({ responseMessage }) => {
if (this.streamGeneration === myGeneration) {
useAppState.getState().updatePromptProgress(undefined)
- useAppState.getState().updateLoadingModel(false)
useAppState.getState().updateThreadPromptProgress(threadId, undefined)
- useAppState.getState().updateThreadLoadingModel(threadId, false)
+ this.setLoadingModel(threadId, false)
useAppState.getState().updateLiveTokenStats(undefined)
useAppState.getState().updateThreadLiveTokenStats(threadId, undefined)
if (useAppState.getState().currentStreamThreadId === threadId) {
diff --git a/web-app/src/routes/cowork.tsx b/web-app/src/routes/cowork.tsx
index 396858406..4a3c580c2 100644
--- a/web-app/src/routes/cowork.tsx
+++ b/web-app/src/routes/cowork.tsx
@@ -57,7 +57,6 @@ import { isContextOverflowMessage } from '@/utils/error'
import { importAttachedFiles, withAttachedFiles } from '@/lib/coworkAttachments'
import { useToolCallRuntime, withToolTiming } from '@/hooks/useToolCallRuntime'
import { PromptProgress } from '@/components/PromptProgress'
-import { useAppState } from '@/hooks/useAppState'
import { useAutoScroll } from '@/hooks/useAutoScroll'
import {
Conversation,
@@ -529,8 +528,8 @@ function CoworkPage() {
try {
const loaded = await getLoadedModels()
if (!loaded.includes(selectedModel.id)) {
- useAppState.getState().updateModelLoadProgress(undefined)
- useAppState.getState().updateLoadingModel(true)
+ useCoworkRun.getState().setSessionModelLoadProgress(sid, undefined)
+ useCoworkRun.getState().setSessionLoadingModel(sid, true)
}
} catch {
// Probe failed; skip the card rather than flash it every run.
@@ -905,7 +904,9 @@ function CoworkPage() {
errorText: e instanceof Error ? e.message : String(e),
}
} finally {
- useAppState.getState().updateLoadingModel(false)
+ // clearCodeRun below also drops the session's load state; this makes the
+ // card disappear the instant the run ends, before that broader reset.
+ useCoworkRun.getState().setSessionLoadingModel(sid, false)
useCoworkSessions
.getState()
.commitTurns(