Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(assistant): make the thread's agent authoritative so in-chat switching works
Akarshan Biswas42c46b70e0a3
diff --git a/web-app/src/containers/ChatInput.tsx b/web-app/src/containers/ChatInput.tsxindex 8683a6518..6c9593cfa 100644--- a/web-app/src/containers/ChatInput.tsx+++ b/web-app/src/containers/ChatInput.tsx@@ -334,8 +334,6 @@ const ChatInput = memo(function ChatInput({return}- const assistant = currentThread?.assistants?.[0]- setCurrentAssistant(assistant)const imageFiles = attachments.filter((att) => att.type === 'image' && att.dataUrl).map((att) => ({diff --git a/web-app/src/containers/SamplerPopover.tsx b/web-app/src/containers/SamplerPopover.tsxindex 9e2b3db86..471e6bd86 100644--- a/web-app/src/containers/SamplerPopover.tsx+++ b/web-app/src/containers/SamplerPopover.tsx@@ -71,13 +71,44 @@ export function SamplerPopover({return providers.filter((p) => p.active)}, [providers, providerId])- const params = currentAssistant?.parameters ?? {}+ // The header must match the switcher's checkmark in every context:+ // - inside a thread: the thread's assigned assistant, including "None"+ // (model-only) which must NOT fall back to the global default;+ // - off-thread with a switcher (home screen): the dropdown selection+ // (selectedAssistantId), since that's what a new chat will use;+ // - no switcher at all: the global current assistant.+ const inThread = !!assistantSwitcher?.currentThread+ const threadAssistant = assistantSwitcher?.currentThread?.assistants?.[0]+ const isThreadAssistant =+ !!threadAssistant && threadAssistant.id !== 'model-only'+ const selectedAssistant = assistantSwitcher+ ? assistantSwitcher.assistants.find(+ (a) => a.id === assistantSwitcher.selectedAssistantId+ )+ : currentAssistant+ const activeAssistant: Assistant | undefined = inThread+ ? isThreadAssistant+ ? threadAssistant+ : undefined+ : selectedAssistant++ const params = activeAssistant?.parameters ?? {}const samplerKeys = Object.keys(params).filter((k) => k in paramsSettings)const hasOverrides = samplerKeys.length > 0const writeParams = (next: Record<string, unknown>) => {- if (!currentAssistant) return- updateAssistant({ ...currentAssistant, parameters: next })+ if (!activeAssistant) return+ const updated = { ...activeAssistant, parameters: next }+ if (isThreadAssistant && assistantSwitcher) {+ // Sync the thread's copy so inference picks it up immediately; mirror+ // into the canonical assistant only when it still exists.+ assistantSwitcher.updateCurrentThreadAssistant(updated)+ if (assistantSwitcher.assistants.some((a) => a.id === updated.id)) {+ updateAssistant(updated)+ }+ } else {+ updateAssistant(updated)+ }}const handleToggle = (def: ParamDef) => {@@ -122,8 +153,8 @@ export function SamplerPopover({const triggerLabel = assistantsLoading? 'Loading assistant…'- : currentAssistant- ? `Sampling — ${currentAssistant.name}`+ : activeAssistant+ ? `Sampling — ${activeAssistant.name}`: 'Sampling'return (@@ -169,7 +200,7 @@ export function SamplerPopover({><div className="flex items-center justify-between gap-2 px-4 pt-3 pb-2 border-b"><AssistantHeader- currentAssistant={currentAssistant}+ currentAssistant={activeAssistant}assistantSwitcher={assistantSwitcher}/><div className="flex items-center gap-1 shrink-0">@@ -192,7 +223,7 @@ export function SamplerPopover({</Tooltip></div></div>- {currentAssistant ? (+ {activeAssistant ? (<div className="flex-1 min-h-0 overflow-y-auto px-4 py-3"><ParametersSectionparams={params}diff --git a/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts b/web-app/src/lib/__tests__/custom-chat-transport-class.test.tsindex c398fd1d0..0a07949de 100644--- a/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts+++ b/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts@@ -14,12 +14,17 @@ vi.mock('@/hooks/useModelProvider', () => ({useModelProvider: { getState: () => ({ selectedModel: null, selectedProvider: '', getProviderByName: () => null }) },}))+const mockState = vi.hoisted(() => ({+ currentAssistant: null as unknown,+ threads: {} as Record<string, unknown>,+}))+vi.mock('@/hooks/useAssistant', () => ({- useAssistant: { getState: () => ({ currentAssistant: null }) },+ useAssistant: { getState: () => ({ currentAssistant: mockState.currentAssistant }) },}))vi.mock('@/hooks/useThreads', () => ({- useThreads: { getState: () => ({ threads: {} }) },+ useThreads: { getState: () => ({ threads: mockState.threads }) },}))vi.mock('@/hooks/useAttachments', () => ({@@ -50,6 +55,8 @@ describe('CustomChatTransport', () => {let transport: CustomChatTransportbeforeEach(() => {+ mockState.currentAssistant = null+ mockState.threads = {}transport = new CustomChatTransport('You are helpful', 'thread-1')})@@ -126,6 +133,38 @@ describe('CustomChatTransport', () => {const result = transport.mapUserInlineAttachments(messages)expect(result[0].parts[0].text).toBe('Check')})++ describe('inference params follow the thread assistant', () => {+ type Resolvable = {+ getActiveInferenceParams: () => Record<string, unknown>+ }+ const resolve = (t: CustomChatTransport = transport) =>+ (t as unknown as Resolvable).getActiveInferenceParams()++ it('reads params from the thread assistant when set', () => {+ mockState.currentAssistant = { id: 'default', parameters: { temperature: 0.1 } }+ mockState.threads = {+ 'thread-1': { assistants: [{ id: 'agent-b', parameters: { temperature: 0.9 } }] },+ }+ expect(resolve()).toEqual({ temperature: 0.9 })+ })++ it('uses no params for a model-only thread, ignoring the global default', () => {+ mockState.currentAssistant = { id: 'default', parameters: { temperature: 0.1 } }+ mockState.threads = { 'thread-1': { assistants: [{ id: 'model-only' }] } }+ expect(resolve()).toEqual({})+ })++ it('falls back to the global assistant only when off-thread', () => {+ mockState.currentAssistant = { id: 'default', parameters: { temperature: 0.1 } }+ mockState.threads = {}+ expect(resolve(new CustomChatTransport('sys'))).toEqual({ temperature: 0.1 })+ })++ it('returns an empty object when nothing is set', () => {+ expect(resolve(new CustomChatTransport('sys'))).toEqual({})+ })+ })})describe('normalizeToolInputSchema edge cases', () => {diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.tsindex eb8f4ee7a..730f072a4 100644--- a/web-app/src/lib/custom-chat-transport.ts+++ b/web-app/src/lib/custom-chat-transport.ts@@ -566,6 +566,24 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {this.systemMessage = systemMessage}+ // Inference params follow the thread's assigned assistant so in-chat agent+ // switches take effect immediately. A thread with no real assistant+ // (model-only / "None") uses no assistant params — matching the switcher.+ // Only off-thread (no threadId / thread not yet in store) do we fall back to+ // the global current assistant.+ private getActiveInferenceParams(): Record<string, unknown> {+ const thread = this.threadId+ ? useThreads.getState().threads[this.threadId]+ : undefined+ if (thread) {+ const threadAssistant = thread.assistants?.[0]+ return threadAssistant && threadAssistant.id !== 'model-only'+ ? (threadAssistant.parameters ?? {})+ : {}+ }+ return useAssistant.getState().currentAssistant?.parameters ?? {}+ }+setOnTokenUsage(callback: TokenUsageCallback | undefined) {this.onTokenUsage = callback}@@ -832,8 +850,7 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {.getState().getProviderByName(providerId)- const currentAssistant = useAssistant.getState().currentAssistant- const inferenceParams = currentAssistant?.parameters+ const inferenceParams = this.getActiveInferenceParams()const selectedModel = useModelProvider.getState().selectedModelconst reasoningParams = buildLlamacppReasoningParams(@@ -942,7 +959,7 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {})})()- const inferenceParams = useAssistant.getState().currentAssistant?.parameters ?? {}+ const inferenceParams = this.getActiveInferenceParams()const selectedModel = useModelProvider.getState().selectedModel