feat: 对话缓存机制

This commit is contained in:
2026-04-02 14:50:20 +08:00
parent 59a9046333
commit 04611c60d8
6 changed files with 67 additions and 211 deletions

View File

@@ -8,7 +8,7 @@
<!-- <SvgIcon name="equal" color="#0d0d0d" size="24" /> -->
</div>
<div class="agent-body flex-1 flex flex-col">
<List ref="listRef" :message-list="messageList" @regenerate="handleRegenerate" />
<List ref="listRef" :message-list="messageList" />
<Input
ref="inputRef"
is-agent-mode
@@ -80,55 +80,7 @@
{ deep: true }
)
const saveSession = (projectId: string) => {
if (!projectId) return
agentStore.saveCacheConversation(projectId, {
messageList: messageList.value,
sketchList: sketchList.value,
params: {
...params,
projectID: projectId
},
isGenerating: isGenerating.value,
isPaused: isPaused.value
})
}
const restoreSession = (projectId: string) => {
if (!projectId) return false
const payload = agentStore.getCacheConversation(projectId)
if (!payload) return false
messageList.value = payload.messageList || []
sketchList.value = payload.sketchList || []
if (payload.params) {
params.projectID = payload.params.projectID || projectId
params.message = payload.params.message || ''
params.versionID = payload.params.versionID || ''
params.configParams = payload.params.configParams || params.configParams
params.needSuggestion = payload.params.needSuggestion || false
params.useReport = payload.params.useReport || false
params.imageUrlList = payload.params.imageUrlList || []
params.quotaUrl = payload.params.quotaUrl || []
}
isGenerating.value = payload.isGenerating || false
isPaused.value = payload.isPaused || false
return true
}
const clearSession = (projectId: string) => {
if (!projectId) return
agentStore.removeCacheConversation(projectId)
}
const handleReset = (force = false) => {
if (!force && params.projectID) {
saveSession(params.projectID)
}
if (force && params.projectID) {
clearSession(params.projectID)
}
const handleReset = () => {
messageList.value = []
sketchList.value = []
params.versionID = ''
@@ -142,7 +94,6 @@
style: ''
}
isGenerating.value = false
isPaused.value = false
}
// 每次请求时创建新的 AbortController
@@ -158,10 +109,7 @@
}
onUnmounted(() => {
if (params.projectID) {
saveSession(params.projectID)
}
abort?.abort()
// abort?.abort()
MyEvent.remove('resetAgent', handleReset)
})
@@ -410,7 +358,14 @@
}
if (jsonData.title) {
emits('setTitle', jsonData.title)
MyEvent.emit('newTitle', jsonData.title)
console.log('发送title', {
title: jsonData.title,
id: params.projectID
})
MyEvent.emit('newTitle', {
title: jsonData.title,
id: params.projectID
})
}
if (hasSketch) {
@@ -505,39 +460,36 @@
isPaused.value = true
isGenerating.value = false
abort?.abort()
if (params.projectID) {
saveSession(params.projectID)
}
MyEvent.emit('stopChat')
}
const handleRegenerate = async (aiMessage: any) => {
// 找到当前 AI 消息在列表中的索引
const aiIndex = messageList.value.findIndex((msg) => msg.id === aiMessage.id)
if (aiIndex === -1) return
// const handleRegenerate = async (aiMessage: any) => {
// // 找到当前 AI 消息在列表中的索引
// const aiIndex = messageList.value.findIndex((msg) => msg.id === aiMessage.id)
// if (aiIndex === -1) return
// 找到对应的用户消息AI 消息前面的最近一条用户消息)
let userMessage = null
for (let i = aiIndex - 1; i >= 0; i--) {
if (messageList.value[i].isUser) {
userMessage = messageList.value[i]
break
}
}
if (!userMessage) return
// // 找到对应的用户消息AI 消息前面的最近一条用户消息)
// let userMessage = null
// for (let i = aiIndex - 1; i >= 0; i--) {
// if (messageList.value[i].isUser) {
// userMessage = messageList.value[i]
// break
// }
// }
// if (!userMessage) return
// 删除当前的 AI 回复消息
messageList.value.splice(aiIndex, 1)
// // 删除当前的 AI 回复消息
// messageList.value.splice(aiIndex, 1)
// 重新调用 API跳过用户消息添加因为用户消息已存在
await handleSendMessage(
{
text: userMessage.text,
images: userMessage.images || []
},
true
)
}
// // 重新调用 API跳过用户消息添加因为用户消息已存在
// await handleSendMessage(
// {
// text: userMessage.text,
// images: userMessage.images || []
// },
// true
// )
// }
// 处理对话列表,将连续的 assistant 消息合并为一条
const processDialogue = (dialogue, startIndex, existingImgList, sessionId) => {
@@ -598,7 +550,7 @@
thinkingText: combinedThinkingText,
text: combinedContent,
image_url: combinedImageUrl,
webAddress: webAddress ? JSON.parse(webAddress) : null,
webAddress: !!webAddress ? JSON.parse(webAddress) : null,
isUser: false,
id: result.length + 1,
sessionId: sessionId
@@ -674,7 +626,7 @@
item.text += `<slot slot-name="sketch"></slot>`
}
})
console.log('ancestorslist', ancestorsList)
// console.log('ancestorslist', ancestorsList)
messageList.value = [...ancestorsList]
params.versionID = current?.id
sketchList.value = imgList
@@ -682,10 +634,7 @@
}
defineExpose({
setChatInfo,
saveSession,
restoreSession,
clearSession
setChatInfo
})
</script>