Skip to content

Commit 850fe73

Browse files
committed
fix: assistant with last used and fix metadata
1 parent 432c942 commit 850fe73

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

web-app/src/constants/localStorage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export const localStorageKey = {
1818
toolAvailability: 'tool-availability',
1919
mcpGlobalPermissions: 'mcp-global-permissions',
2020
lastUsedModel: 'last-used-model',
21+
lastUsedAssistant: 'last-used-assistant',
2122
}

web-app/src/hooks/useAssistant.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
import { createAssistant, deleteAssistant } from '@/services/assistants'
22
import { Assistant as CoreAssistant } from '@janhq/core'
33
import { create } from 'zustand'
4+
import { localStorageKey } from '@/constants/localStorage'
45

56
interface AssistantState {
67
assistants: Assistant[]
78
currentAssistant: Assistant
89
addAssistant: (assistant: Assistant) => void
910
updateAssistant: (assistant: Assistant) => void
1011
deleteAssistant: (id: string) => void
11-
setCurrentAssistant: (assistant: Assistant) => void
12+
setCurrentAssistant: (assistant: Assistant, saveToStorage?: boolean) => void
1213
setAssistants: (assistants: Assistant[]) => void
14+
getLastUsedAssistant: () => string | null
15+
setLastUsedAssistant: (assistantId: string) => void
16+
initializeWithLastUsed: () => void
17+
}
18+
19+
// Helper functions for localStorage
20+
const getLastUsedAssistantId = (): string | null => {
21+
try {
22+
return localStorage.getItem(localStorageKey.lastUsedAssistant)
23+
} catch (error) {
24+
console.debug('Failed to get last used assistant from localStorage:', error)
25+
return null
26+
}
27+
}
28+
29+
const setLastUsedAssistantId = (assistantId: string) => {
30+
try {
31+
localStorage.setItem(localStorageKey.lastUsedAssistant, assistantId)
32+
} catch (error) {
33+
console.debug('Failed to set last used assistant in localStorage:', error)
34+
}
1335
}
1436

1537
export const defaultAssistant: Assistant = {
@@ -19,9 +41,9 @@ export const defaultAssistant: Assistant = {
1941
parameters: {},
2042
avatar: '👋',
2143
description:
22-
'Jan is a helpful desktop assistant that can reason through complex tasks and use tools to complete them on the users behalf.',
44+
'Jan is a helpful desktop assistant that can reason through complex tasks and use tools to complete them on the user\'s behalf.',
2345
instructions:
24-
'You are a helpful AI assistant. Your primary goal is to assist users with their questions and tasks to the best of your abilities.\n\nWhen responding:\n- Answer directly from your knowledge when you can\n- Be concise, clear, and helpful\n- Admit when youre unsure rather than making things up\n\nIf tools are available to you:\n- Only use tools when they add real value to your response\n- Use tools when the user explicitly asks (e.g., "search for...", "calculate...", "run this code")\n- Use tools for information you dont know or that needs verification\n- Never use tools just because theyre available\n\nWhen using tools:\n- Use one tool at a time and wait for results\n- Use actual values as arguments, not variable names\n- Learn from each result before deciding next steps\n- Avoid repeating the same tool call with identical parameters\n\nRemember: Most questions can be answered without tools. Think first whether you need them.',
46+
'You are a helpful AI assistant. Your primary goal is to assist users with their questions and tasks to the best of your abilities.\n\nWhen responding:\n- Answer directly from your knowledge when you can\n- Be concise, clear, and helpful\n- Admit when you\'re unsure rather than making things up\n\nIf tools are available to you:\n- Only use tools when they add real value to your response\n- Use tools when the user explicitly asks (e.g., "search for...", "calculate...", "run this code")\n- Use tools for information you don\'t know or that needs verification\n- Never use tools just because they\'re available\n\nWhen using tools:\n- Use one tool at a time and wait for results\n- Use actual values as arguments, not variable names\n- Learn from each result before deciding next steps\n- Avoid repeating the same tool call with identical parameters\n\nRemember: Most questions can be answered without tools. Think first whether you need them.',
2547
}
2648

2749
export const useAssistant = create<AssistantState>()((set, get) => ({
@@ -51,17 +73,50 @@ export const useAssistant = create<AssistantState>()((set, get) => ({
5173
})
5274
},
5375
deleteAssistant: (id) => {
76+
const state = get()
5477
deleteAssistant(
55-
get().assistants.find((e) => e.id === id) as unknown as CoreAssistant
78+
state.assistants.find((e) => e.id === id) as unknown as CoreAssistant
5679
).catch((error) => {
5780
console.error('Failed to delete assistant:', error)
5881
})
59-
set({ assistants: get().assistants.filter((a) => a.id !== id) })
82+
83+
// Check if we're deleting the current assistant
84+
const wasCurrentAssistant = state.currentAssistant.id === id
85+
86+
set({ assistants: state.assistants.filter((a) => a.id !== id) })
87+
88+
// If the deleted assistant was current, fallback to default and update localStorage
89+
if (wasCurrentAssistant) {
90+
set({ currentAssistant: defaultAssistant })
91+
setLastUsedAssistantId(defaultAssistant.id)
92+
}
6093
},
61-
setCurrentAssistant: (assistant) => {
94+
setCurrentAssistant: (assistant, saveToStorage = true) => {
6295
set({ currentAssistant: assistant })
96+
if (saveToStorage) {
97+
setLastUsedAssistantId(assistant.id)
98+
}
6399
},
64100
setAssistants: (assistants) => {
65101
set({ assistants })
66102
},
103+
getLastUsedAssistant: () => {
104+
return getLastUsedAssistantId()
105+
},
106+
setLastUsedAssistant: (assistantId) => {
107+
setLastUsedAssistantId(assistantId)
108+
},
109+
initializeWithLastUsed: () => {
110+
const lastUsedId = getLastUsedAssistantId()
111+
if (lastUsedId) {
112+
const lastUsedAssistant = get().assistants.find(a => a.id === lastUsedId)
113+
if (lastUsedAssistant) {
114+
set({ currentAssistant: lastUsedAssistant })
115+
} else {
116+
// Fallback to default if last used assistant was deleted
117+
set({ currentAssistant: defaultAssistant })
118+
setLastUsedAssistantId(defaultAssistant.id)
119+
}
120+
}
121+
},
67122
}))

web-app/src/hooks/useChat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ export const useChat = () => {
399399
accumulatedText,
400400
{
401401
tokenSpeed: useAppState.getState().tokenSpeed,
402+
assistant: currentAssistant,
402403
}
403404
)
404405

web-app/src/providers/DataProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function DataProvider() {
2424
const { setMessages } = useMessages()
2525
const { checkForUpdate } = useAppUpdater()
2626
const { setServers } = useMCPServers()
27-
const { setAssistants } = useAssistant()
27+
const { setAssistants, initializeWithLastUsed } = useAssistant()
2828
const { setThreads } = useThreads()
2929
const navigate = useNavigate()
3030

@@ -37,6 +37,7 @@ export function DataProvider() {
3737
// Only update assistants if we have valid data
3838
if (data && Array.isArray(data) && data.length > 0) {
3939
setAssistants(data as unknown as Assistant[])
40+
initializeWithLastUsed()
4041
}
4142
})
4243
.catch((error) => {

0 commit comments

Comments
 (0)