diff --git a/packages/webui/stores/chatbot.test.ts b/packages/webui/stores/chatbot.test.ts index 9d023fd7..3a85108e 100644 --- a/packages/webui/stores/chatbot.test.ts +++ b/packages/webui/stores/chatbot.test.ts @@ -1,8 +1,35 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import { useChatbotStore } from './chatbot' +import { useChatbotStore, AGENT_PREFERENCE_STORAGE_KEY } from './chatbot' import { client } from '@/ui/api/client' import type { ChatMessage } from '@shumai/dtos' +const localStorageMock = (() => { + let store: Record = {} + return { + getItem: (key: string) => store[key] || null, + setItem: (key: string, value: string) => { + store[key] = value.toString() + }, + removeItem: (key: string) => { + delete store[key] + }, + clear: () => { + store = {} + }, + } +})() + +if (typeof globalThis.localStorage === 'undefined') { + Object.defineProperty(globalThis, 'localStorage', { + value: localStorageMock, + }) +} +if (typeof globalThis.window === 'undefined') { + Object.defineProperty(globalThis, 'window', { + value: globalThis, + }) +} + vi.mock('@/ui/api/client', () => { const chatMock = { $post: vi.fn(), @@ -30,6 +57,7 @@ vi.mock('@/ui/api/client', () => { describe('useChatbotStore', () => { beforeEach(() => { vi.resetAllMocks() + localStorage.clear() useChatbotStore.setState({ isHistoryMode: false, isChatbotOpen: false, @@ -43,6 +71,30 @@ describe('useChatbotStore', () => { }) }) + it('should persist selectedAgentId to localStorage when setSelectedAgentId is called', () => { + useChatbotStore.getState().setSelectedAgentId('agent-pref-123') + expect(useChatbotStore.getState().selectedAgentId).toBe('agent-pref-123') + expect(localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY)).toBe('agent-pref-123') + + useChatbotStore.getState().setSelectedAgentId(null) + expect(useChatbotStore.getState().selectedAgentId).toBeNull() + expect(localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY)).toBeNull() + }) + + it('should restore preferred agent from localStorage on startNewSession', () => { + localStorage.setItem(AGENT_PREFERENCE_STORAGE_KEY, 'agent-pref-saved') + useChatbotStore.setState({ + selectedAgentId: 'agent-temporary-historical', + currentSessionId: 'sess-historical', + messages: [{ id: 'msg-1', role: 'user', content: 'test' } as unknown as ChatMessage], + }) + + useChatbotStore.getState().startNewSession() + const state = useChatbotStore.getState() + expect(state.currentSessionId).toBeNull() + expect(state.selectedAgentId).toBe('agent-pref-saved') + }) + it('should persist and reset inputText', () => { expect(useChatbotStore.getState().inputText).toBe('') useChatbotStore.getState().setInputText('Draft text in progress') diff --git a/packages/webui/stores/chatbot.ts b/packages/webui/stores/chatbot.ts index e0b7b581..6fa900a2 100644 --- a/packages/webui/stores/chatbot.ts +++ b/packages/webui/stores/chatbot.ts @@ -3,6 +3,15 @@ import { client } from '@/ui/api/client' import type { ChatMessage, ChatSessionInfo } from '@shumai/dtos' import { toast } from 'sonner' +export const AGENT_PREFERENCE_STORAGE_KEY = 'shumai_selected_agent_id' + +const getInitialAgentId = (): string | null => { + if (typeof window !== 'undefined' && window.localStorage) { + return localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY) + } + return null +} + export interface ChatAsset { id: string name: string @@ -74,8 +83,17 @@ export const useChatbotStore = create((set) => ({ setHistorySessions: (sessions) => set({ historySessions: sessions }), isStreaming: false, setIsStreaming: (streaming) => set({ isStreaming: streaming }), - selectedAgentId: null, - setSelectedAgentId: (id) => set({ selectedAgentId: id }), + selectedAgentId: getInitialAgentId(), + setSelectedAgentId: (id) => { + if (typeof window !== 'undefined' && window.localStorage) { + if (id) { + localStorage.setItem(AGENT_PREFERENCE_STORAGE_KEY, id) + } else { + localStorage.removeItem(AGENT_PREFERENCE_STORAGE_KEY) + } + } + set({ selectedAgentId: id }) + }, inputText: '', setInputText: (text) => set({ inputText: text }), scrollTop: 0, @@ -147,6 +165,7 @@ export const useChatbotStore = create((set) => ({ }, startNewSession: () => { + const savedAgentId = getInitialAgentId() set({ currentSessionId: null, messages: [], @@ -154,6 +173,7 @@ export const useChatbotStore = create((set) => ({ inputText: '', scrollTop: 0, isAtBottom: true, + ...(savedAgentId ? { selectedAgentId: savedAgentId } : {}), }) },