import { defineStore } from 'pinia'; import { createSession, getAgentData } from '@/api/all/chat'; import { handleUserHome } from '@/utils/user'; import { glsWithCatch, slsWithCatch, rlsWithCatch } from '@/utils/stroage'; interface ChatState { searchValue?: string; agentInfo?: agentInfo; } type agentInfo = { agent_id?: string; name?: string; description?: string; }; export const useChatStore = defineStore('chat', { state: (): ChatState => ({ searchValue: '', agentInfo: (glsWithCatch('agentInfo') && JSON.parse(glsWithCatch('agentInfo') as string)) || {}, }), actions: { setSearchValue(searchValue: string) { this.searchValue = searchValue; }, clearSearchValue() { this.searchValue = ''; }, async getAgentInfo() { const { code, data } = await getAgentData(); if (code === 200) { this.setAgentInfo(data); } }, setAgentInfo(agentInfo: agentInfo) { this.agentInfo = agentInfo; slsWithCatch('agentInfo', JSON.stringify(agentInfo)); }, clearAgentInfo() { this.agentInfo = {}; this.searchValue = ''; }, async onCreateSession() { const { code, data } = await createSession(); if (code === 200) { handleUserHome({ conversationId: data.session_id, }); } }, }, });