mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 21:44:27 +00:00
feat(webui): persist agent preference in localStorage
This commit is contained in:
@@ -1,8 +1,35 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
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 { client } from '@/ui/api/client'
|
||||||
import type { ChatMessage } from '@shumai/dtos'
|
import type { ChatMessage } from '@shumai/dtos'
|
||||||
|
|
||||||
|
const localStorageMock = (() => {
|
||||||
|
let store: Record<string, string> = {}
|
||||||
|
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', () => {
|
vi.mock('@/ui/api/client', () => {
|
||||||
const chatMock = {
|
const chatMock = {
|
||||||
$post: vi.fn(),
|
$post: vi.fn(),
|
||||||
@@ -30,6 +57,7 @@ vi.mock('@/ui/api/client', () => {
|
|||||||
describe('useChatbotStore', () => {
|
describe('useChatbotStore', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.resetAllMocks()
|
vi.resetAllMocks()
|
||||||
|
localStorage.clear()
|
||||||
useChatbotStore.setState({
|
useChatbotStore.setState({
|
||||||
isHistoryMode: false,
|
isHistoryMode: false,
|
||||||
isChatbotOpen: 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', () => {
|
it('should persist and reset inputText', () => {
|
||||||
expect(useChatbotStore.getState().inputText).toBe('')
|
expect(useChatbotStore.getState().inputText).toBe('')
|
||||||
useChatbotStore.getState().setInputText('Draft text in progress')
|
useChatbotStore.getState().setInputText('Draft text in progress')
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ import { client } from '@/ui/api/client'
|
|||||||
import type { ChatMessage, ChatSessionInfo } from '@shumai/dtos'
|
import type { ChatMessage, ChatSessionInfo } from '@shumai/dtos'
|
||||||
import { toast } from 'sonner'
|
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 {
|
export interface ChatAsset {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
@@ -74,8 +83,17 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
|||||||
setHistorySessions: (sessions) => set({ historySessions: sessions }),
|
setHistorySessions: (sessions) => set({ historySessions: sessions }),
|
||||||
isStreaming: false,
|
isStreaming: false,
|
||||||
setIsStreaming: (streaming) => set({ isStreaming: streaming }),
|
setIsStreaming: (streaming) => set({ isStreaming: streaming }),
|
||||||
selectedAgentId: null,
|
selectedAgentId: getInitialAgentId(),
|
||||||
setSelectedAgentId: (id) => set({ selectedAgentId: id }),
|
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: '',
|
inputText: '',
|
||||||
setInputText: (text) => set({ inputText: text }),
|
setInputText: (text) => set({ inputText: text }),
|
||||||
scrollTop: 0,
|
scrollTop: 0,
|
||||||
@@ -147,6 +165,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
startNewSession: () => {
|
startNewSession: () => {
|
||||||
|
const savedAgentId = getInitialAgentId()
|
||||||
set({
|
set({
|
||||||
currentSessionId: null,
|
currentSessionId: null,
|
||||||
messages: [],
|
messages: [],
|
||||||
@@ -154,6 +173,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
|||||||
inputText: '',
|
inputText: '',
|
||||||
scrollTop: 0,
|
scrollTop: 0,
|
||||||
isAtBottom: true,
|
isAtBottom: true,
|
||||||
|
...(savedAgentId ? { selectedAgentId: savedAgentId } : {}),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user