feat: 用缓存处理对话中的项目切换问题
This commit is contained in:
@@ -5,7 +5,6 @@ import { ref, computed } from 'vue'
|
||||
import { removeLocal, setLocal } from '@/utils/local'
|
||||
import MyEvent from '@/utils/myEvent'
|
||||
|
||||
|
||||
// Agent 项目初始数据 store
|
||||
type InitialProjectData = {
|
||||
text: string
|
||||
@@ -13,13 +12,13 @@ type InitialProjectData = {
|
||||
type: string
|
||||
area: string
|
||||
style: string
|
||||
useReport:boolean
|
||||
needSuggestion:boolean
|
||||
useReport: boolean
|
||||
needSuggestion: boolean
|
||||
quoteList: Array<string>
|
||||
tempImages: any[]
|
||||
}
|
||||
export const useAgentStore = defineStore('agent', () => {
|
||||
const initialProjectData = ref<InitialProjectData | null>(null)
|
||||
const initialProjectData = ref<InitialProjectData | null>(null)
|
||||
|
||||
// 保存项目初始数据
|
||||
const setInitialProjectData = (data: InitialProjectData) => {
|
||||
@@ -34,10 +33,94 @@ export const useAgentStore = defineStore('agent', () => {
|
||||
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
|
||||
clearInitialProjectData,
|
||||
conversationCache,
|
||||
saveCacheConversation,
|
||||
getCacheConversation,
|
||||
removeCacheConversation,
|
||||
clearAllCacheConversations,
|
||||
getCacheStats
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user