import type { ChatMessageVo } from '@/api/chat/types'; import { defineStore } from 'pinia'; import { getChatList } from '@/api'; import { useUserStore } from './user'; export const useChatStore = defineStore('chat', () => { const userStore = useUserStore(); // 用户头像 const avatar = computed(() => { const userInfo = userStore.userInfo; return userInfo?.avatar || 'https://avatars.githubusercontent.com/u/76239030?v=4'; }); // 是否开启深度思考 const isDeepThinking = ref(false); const setDeepThinking = (value: boolean) => { isDeepThinking.value = value; }; // 会议ID对应-聊天记录 map对象 const chatMap = ref>({}); const setChatMap = (id: string, data: ChatMessageVo[]) => { chatMap.value[id] = data?.map((item: ChatMessageVo) => { const isUser = item.role === 'user'; const thinkContent = extractThkContent(item.content as string); return { ...item, key: item.id, placement: isUser ? 'end' : 'start', isMarkdown: !isUser, // variant: 'shadow', // shape: 'corner', avatar: isUser ? avatar : 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png', avatarSize: '32px', typing: false, reasoning_content: thinkContent, thinkingStatus: 'end', content: extractThkContentAfter(item.content as string), thinlCollapse: false, }; }); }; // 获取当前会话的聊天记录 const requestChatList = async (sessionId: string) => { // 如果没有 token 则不查询聊天记录 if (!userStore.token) return; try { const res = await getChatList({ sessionId, userId: userStore.userInfo?.userId as number, }); if (res.rows) { setChatMap(sessionId, res.rows); } } catch (error) { console.error('getChatList:', error); } }; // 对思考中的内容回显做处理 function extractThkContent(content: string) { const regex = /(.*?)<\/think>/s; const matchResult = content.match(regex); // 把这些内容从 content 中移除 content = content.replace(regex, ''); return matchResult?.[1] ?? ''; } // 如果有 标签,则把 之后的 内容从 content 中返回 function extractThkContentAfter(content: string) { if (!content.includes('')) { return content; } const regex = /<\/think>(.*)/s; const matchResult = content.match(regex); // 把这些内容从 content 中移除 content = content.replace(regex, ''); return matchResult?.[1] ?? ''; } return { chatMap, requestChatList, isDeepThinking, setDeepThinking, }; });