Files
FiDA_Front/src/stores/agent.ts

127 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 每一个存储的模块命名规则use开头store结尾
import router from '@/router'
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { removeLocal, setLocal } from '@/utils/local'
import MyEvent from '@/utils/myEvent'
// Agent 项目初始数据 store
type InitialProjectData = {
text: string
images: Array<{ url: string; name: string }>
type: string
area: string
style: string
useReport: boolean
needSuggestion: boolean
quoteList: Array<string>
tempImages: any[]
}
export const useAgentStore = defineStore('agent', () => {
const initialProjectData = ref<InitialProjectData | null>(null)
// 保存项目初始数据
const setInitialProjectData = (data: InitialProjectData) => {
initialProjectData.value = data
}
// 获取项目初始数据
const getInitialProjectData = computed(() => initialProjectData.value)
// 清空项目初始数据
const clearInitialProjectData = () => {
initialProjectData.value = null
}
// 会话缓存管理,最多保存 10 条会话
const conversationCache = new Map<string, any>()
const MAX_CACHE_ENTRIES = 10
const SESSION_KEY_PREFIX = 'agent_session_'
const pruneCache = () => {
const keys = Array.from(conversationCache.keys())
const total = conversationCache.size
if (total <= MAX_CACHE_ENTRIES) return
for (let i = 0; i < total - MAX_CACHE_ENTRIES; i++) {
conversationCache.delete(keys[i])
sessionStorage.removeItem(SESSION_KEY_PREFIX + keys[i])
}
}
const saveCacheConversation = (projectId: string, data: any) => {
if (!projectId) return
const finalData = {
...data,
updatedAt: Date.now()
}
if (conversationCache.has(projectId)) {
conversationCache.delete(projectId)
}
conversationCache.set(projectId, finalData)
try {
sessionStorage.setItem(SESSION_KEY_PREFIX + projectId, JSON.stringify(finalData))
} catch (e) {
console.warn('saveCacheConversation sessionStorage failed', e)
}
pruneCache()
}
const getCacheConversation = (projectId: string) => {
if (!projectId) return null
let data = conversationCache.get(projectId)
if (data) return data
try {
const raw = sessionStorage.getItem(SESSION_KEY_PREFIX + projectId)
if (!raw) return null
data = JSON.parse(raw)
conversationCache.set(projectId, data)
return data
} catch (e) {
console.warn('getCacheConversation failed', e)
return null
}
}
const clearAllCacheConversations = () => {
conversationCache.value.clear()
for (const key in window.sessionStorage) {
if (key.startsWith(SESSION_KEY_PREFIX)) {
sessionStorage.removeItem(key)
}
}
}
const removeCacheConversation = (projectId: string) => {
if (!projectId) return
conversationCache.value.delete(projectId)
sessionStorage.removeItem(SESSION_KEY_PREFIX + projectId)
}
const getCacheStats = () => {
return {
total: conversationCache.value.size,
keys: Array.from(conversationCache.value.keys())
}
}
return {
initialProjectData,
setInitialProjectData,
getInitialProjectData,
clearInitialProjectData,
conversationCache,
saveCacheConversation,
getCacheConversation,
removeCacheConversation,
clearAllCacheConversations,
getCacheStats
}
})