| | |
| | | import { defineStore } from 'pinia' |
| | | import { computed, reactive, ref } from 'vue' |
| | | import * as chatApi from '@/api/chatApi' |
| | | import * as chatGroupApi from '@/api/chatGroupApi' |
| | | import { filenameFromDisposition, saveBlob } from '@/utils/download' |
| | | import { useAuthStore } from './authStore' |
| | | import { streamChat as openSseStream } from '@/utils/sse' |
| | | import { |
| | |
| | | */ |
| | | export const useChatStore = defineStore('chat', () => { |
| | | const chats = ref([]) |
| | | |
| | | /** |
| | | * 已归档的会话。 |
| | | * |
| | | * 单独一份而不是和 chats 混在一起靠字段过滤: |
| | | * 主列表与归档列表是**两个视图**,各自有独立的加载时机与搜索关键词, |
| | | * 混在一起后每次渲染都要过滤,很容易出现「归档了但主列表还显示」这类 |
| | | * 状态不一致。分开存放,归档动作就是把元素从一个数组搬到另一个。 |
| | | */ |
| | | const archivedChats = ref([]) |
| | | /** 归档列表是否已加载过 —— 用于区分「确实没有归档」和「还没查」 */ |
| | | const archiveLoaded = ref(false) |
| | | |
| | | /** |
| | | * 用户自建的分组(文件夹)。 |
| | | * |
| | | * 只存分组本身;「哪些会话在哪个分组里」由每条会话的 groupId 决定, |
| | | * 前端按它分桶。这样移动会话只需要刷新会话列表,不必再拉一次分组。 |
| | | */ |
| | | const chatGroups = ref([]) |
| | | |
| | | const activeChatId = ref(null) |
| | | const tree = ref([]) |
| | | |
| | |
| | | const path = computed(() => buildPath(tree.value, selections)) |
| | | |
| | | const activeChat = computed( |
| | | () => chats.value.find((chat) => chat.id === activeChatId.value) || null |
| | | () => |
| | | chats.value.find((chat) => chat.id === activeChatId.value) || |
| | | // 归档的会话不在主列表里,但用户可能刚从「归档对话」里点开它 —— |
| | | // 找不到就返回 null 会让标题栏、模型参数抽屉一起变空 |
| | | archivedChats.value.find((chat) => chat.id === activeChatId.value) || |
| | | null |
| | | ) |
| | | |
| | | // ============================================================ |
| | |
| | | // ============================================================ |
| | | |
| | | async function loadChats() { |
| | | chats.value = await chatApi.listChats() |
| | | chats.value = await chatApi.listChats(false) |
| | | return chats.value |
| | | } |
| | | |
| | | /** 加载归档列表 */ |
| | | async function loadArchivedChats() { |
| | | archivedChats.value = await chatApi.listChats(true) |
| | | archiveLoaded.value = true |
| | | return archivedChats.value |
| | | } |
| | | |
| | | // ---------------- 自建分组 ---------------- |
| | | |
| | | async function loadChatGroups() { |
| | | chatGroups.value = await chatGroupApi.listChatGroups() |
| | | return chatGroups.value |
| | | } |
| | | |
| | | async function createChatGroup(name) { |
| | | const group = await chatGroupApi.createChatGroup(name) |
| | | chatGroups.value.push(group) |
| | | return group |
| | | } |
| | | |
| | | async function renameChatGroup(groupId, name) { |
| | | const updated = await chatGroupApi.renameChatGroup(groupId, name) |
| | | const index = chatGroups.value.findIndex((g) => g.id === groupId) |
| | | if (index !== -1) { |
| | | chatGroups.value[index] = updated |
| | | } |
| | | return updated |
| | | } |
| | | |
| | | /** |
| | | * 删除分组:组内会话回到「未分组」。 |
| | | * |
| | | * 必须重新拉会话列表 —— 会话本身没变(还在原处、还是原来的时间), |
| | | * 但它们的 groupId 已经被后端清空,不刷新的话它们会挂在一个 |
| | | * 已经不存在分组下而整个消失。 |
| | | */ |
| | | async function deleteChatGroup(groupId) { |
| | | const result = await chatGroupApi.deleteChatGroup(groupId) |
| | | chatGroups.value = chatGroups.value.filter((g) => g.id !== groupId) |
| | | await loadChats() |
| | | return result?.movedChats ?? 0 |
| | | } |
| | | |
| | | /** 把会话移入 / 移出分组(groupId 传 null 表示移出) */ |
| | | async function moveChatToGroup(chatId, groupId) { |
| | | const updated = await chatApi.moveChatToGroup(chatId, groupId) |
| | | replaceInLists(updated) |
| | | // 分组数字要跟着变,同时刷新顺序(后端仍按置顶/活跃排序) |
| | | await Promise.all([loadChats(), loadChatGroups()]) |
| | | return updated |
| | | } |
| | | |
| | | // ---------------- 搜索历史对话 ---------------- |
| | | |
| | | /** 搜索关键词;为空表示未处于搜索态 */ |
| | | const searchKeyword = ref('') |
| | | /** 搜索结果(ChatDTO 数组,带 matchSnippet / matchType) */ |
| | | const searchResults = ref([]) |
| | | const searching = ref(false) |
| | | /** |
| | | * 请求序号。 |
| | | * 建一个独立的搜索器。 |
| | | * |
| | | * 搜索是「输入即查」,慢的旧请求可能后于新请求返回, |
| | | * 直接把旧结果写进去就会出现「输入了新词却显示旧结果」。 |
| | | * 每次发起递增并记住序号,回来时对不上就丢弃。 |
| | | * 主列表与归档列表要各有一份关键词与结果,所以做成工厂而不是两组变量: |
| | | * 两组变量意味着「请求序号、竞态丢弃、loading 状态」这套逻辑要写两遍, |
| | | * 而它恰恰是最容易写漏、写错的部分。 |
| | | */ |
| | | let searchSeq = 0 |
| | | function createSearcher(archived) { |
| | | /** 搜索关键词;为空表示未处于搜索态 */ |
| | | const keyword = ref('') |
| | | /** 搜索结果(ChatDTO 数组,带 matchSnippet / matchType) */ |
| | | const results = ref([]) |
| | | const loading = ref(false) |
| | | /** |
| | | * 请求序号。搜索是「输入即查」,慢的旧请求可能后于新请求返回, |
| | | * 直接把旧结果写进去就会出现「输入了新词却显示旧结果」。 |
| | | */ |
| | | let seq = 0 |
| | | |
| | | async function searchChats(keyword) { |
| | | const kw = (keyword || '').trim() |
| | | searchKeyword.value = kw |
| | | async function run(raw) { |
| | | const kw = (raw || '').trim() |
| | | keyword.value = kw |
| | | |
| | | if (!kw) { |
| | | searchResults.value = [] |
| | | searching.value = false |
| | | results.value = [] |
| | | loading.value = false |
| | | return [] |
| | | } |
| | | |
| | | const seq = ++searchSeq |
| | | searching.value = true |
| | | const mine = ++seq |
| | | loading.value = true |
| | | try { |
| | | const results = await chatApi.searchChats(kw) |
| | | if (seq !== searchSeq) { |
| | | const found = await chatApi.searchChats(kw, 30, archived) |
| | | if (mine !== seq) { |
| | | // 已有更新的请求发出,丢弃这次的结果 |
| | | return searchResults.value |
| | | return results.value |
| | | } |
| | | searchResults.value = Array.isArray(results) ? results : [] |
| | | return searchResults.value |
| | | results.value = Array.isArray(found) ? found : [] |
| | | return results.value |
| | | } catch (error) { |
| | | if (seq === searchSeq) { |
| | | searchResults.value = [] |
| | | if (mine === seq) { |
| | | results.value = [] |
| | | } |
| | | throw error |
| | | } finally { |
| | | if (seq === searchSeq) { |
| | | searching.value = false |
| | | if (mine === seq) { |
| | | loading.value = false |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** 退出搜索态,恢复显示完整会话列表 */ |
| | | function clearSearch() { |
| | | searchSeq++ // 让在途请求失效 |
| | | searchKeyword.value = '' |
| | | searchResults.value = [] |
| | | searching.value = false |
| | | function clear() { |
| | | seq++ // 让在途请求失效 |
| | | keyword.value = '' |
| | | results.value = [] |
| | | loading.value = false |
| | | } |
| | | |
| | | return { keyword, results, loading, run, clear } |
| | | } |
| | | |
| | | const mainSearch = createSearcher(false) |
| | | const archiveSearch = createSearcher(true) |
| | | |
| | | // 主列表搜索:保留扁平命名,侧边栏直接用这些。 |
| | | // |
| | | // 注意 Pinia 只对 **顶层** 返回的 ref 做自动解包:这里以顶层成员暴露, |
| | | // 组件里就能直接 `chatStore.searchKeyword`;如果把整个 searcher 对象 |
| | | // 暴露出去,里面每个 ref 都得写 `.value`,极易写错。 |
| | | const searchKeyword = mainSearch.keyword |
| | | const searchResults = mainSearch.results |
| | | const searching = mainSearch.loading |
| | | const searchChats = mainSearch.run |
| | | const clearSearch = mainSearch.clear |
| | | |
| | | // 归档列表的搜索:同样以顶层成员暴露 |
| | | const archiveKeyword = archiveSearch.keyword |
| | | const archiveResults = archiveSearch.results |
| | | const archiveSearching = archiveSearch.loading |
| | | const searchArchive = archiveSearch.run |
| | | const clearArchiveSearch = archiveSearch.clear |
| | | |
| | | async function createChat(payload = {}) { |
| | | const chat = await chatApi.createChat(payload) |
| | |
| | | return chat |
| | | } |
| | | |
| | | async function renameChat(chatId, title) { |
| | | const updated = await chatApi.updateChat(chatId, { title }) |
| | | const index = chats.value.findIndex((chat) => chat.id === chatId) |
| | | /** |
| | | * 用服务端返回的最新对象替换本地缓存里的同一条。 |
| | | * |
| | | * 同时查两份列表:一条会话可能既不在主列表(已归档)也不在归档列表 |
| | | * (刚恢复),只查 chats 会让归档对话的重命名「看起来没生效」。 |
| | | */ |
| | | function replaceInLists(updated) { |
| | | for (const list of [chats.value, archivedChats.value]) { |
| | | const index = list.findIndex((chat) => chat.id === updated.id) |
| | | if (index !== -1) { |
| | | // 用替换而不是 Object.assign:保证所有字段都与后端一致 |
| | | chats.value[index] = updated |
| | | list[index] = updated |
| | | } |
| | | } |
| | | return updated |
| | | } |
| | | |
| | | async function updateChatMeta(chatId, meta) { |
| | | const updated = await chatApi.updateChat(chatId, { meta }) |
| | | const index = chats.value.findIndex((chat) => chat.id === chatId) |
| | | if (index !== -1) { |
| | | chats.value[index] = updated |
| | | async function renameChat(chatId, title) { |
| | | return replaceInLists(await chatApi.updateChat(chatId, { title })) |
| | | } |
| | | |
| | | async function updateChatMeta(chatId, meta) { |
| | | return replaceInLists(await chatApi.updateChat(chatId, { meta })) |
| | | } |
| | | |
| | | /** |
| | | * 置顶 / 取消置顶。 |
| | | * |
| | | * 操作完重新拉取主列表,而不是本地改一个字段了事: |
| | | * 置顶会改变这条会话在列表中的**位置**(进入置顶区), |
| | | * 而排序规则在后端(pinned DESC, pinned_at DESC, updated_at DESC)。 |
| | | * 本地自己排一遍就会出现「前端顺序和后端顺序不一致」, |
| | | * 刷新一次又跳回去 —— 让唯一的排序权威留在后端,前端只负责分组。 |
| | | */ |
| | | async function setPinned(chatId, pinned) { |
| | | const updated = await chatApi.updateChat(chatId, { pinned }) |
| | | replaceInLists(updated) |
| | | await loadChats() |
| | | return updated |
| | | } |
| | | |
| | | /** |
| | | * 归档 / 取消归档。 |
| | | * |
| | | * 归档是一次「跨列表搬移」:主列表少一条、归档列表多一条。 |
| | | * 两份列表都重新拉,保证两边都一致(归档列表还要按归档时间倒序)。 |
| | | */ |
| | | async function setArchived(chatId, archived) { |
| | | const updated = await chatApi.updateChat(chatId, { archived }) |
| | | await Promise.all([loadChats(), loadArchivedChats()]) |
| | | return updated |
| | | } |
| | | |
| | | async function removeChat(chatId) { |
| | | await chatApi.deleteChat(chatId) |
| | | chats.value = chats.value.filter((chat) => chat.id !== chatId) |
| | | archivedChats.value = archivedChats.value.filter((chat) => chat.id !== chatId) |
| | | // 搜索结果里也可能残留这一条,一并清掉,避免点进去报 404 |
| | | mainSearch.results.value = mainSearch.results.value.filter((chat) => chat.id !== chatId) |
| | | archiveSearch.results.value = archiveSearch.results.value.filter((chat) => chat.id !== chatId) |
| | | |
| | | if (activeChatId.value === chatId) { |
| | | activeChatId.value = null |
| | | tree.value = [] |
| | | clearSelections() |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 下载会话为 json / txt / pdf。 |
| | | * |
| | | * 文件名优先用服务端 Content-Disposition 里的建议名(它带了标题与时间戳), |
| | | * 拿不到时才用本地拼的兜底名。 |
| | | * |
| | | * @returns {Promise<string>} 实际保存的文件名 |
| | | */ |
| | | async function exportChat(chatId, format) { |
| | | const response = await chatApi.exportChat(chatId, format) |
| | | const blob = response.data |
| | | const disposition = response.headers?.['content-disposition'] |
| | | const chat = chats.value.find((c) => c.id === chatId) |
| | | || archivedChats.value.find((c) => c.id === chatId) |
| | | const fallback = `${chat?.title || '对话'}_${chatId}.${format}` |
| | | |
| | | const filename = filenameFromDisposition(disposition) || fallback |
| | | saveBlob(blob, filename) |
| | | return filename |
| | | } |
| | | |
| | | // ============================================================ |
| | |
| | | await loadMessages(chatId, true) |
| | | } |
| | | await loadChats() |
| | | // 会话被归档时不在主列表里,标题等变化只体现在归档列表, |
| | | // 不刷新就会出现「在归档对话里聊完,标题还是旧的」 |
| | | if (archivedChats.value.some((chat) => chat.id === chatId)) { |
| | | await loadArchivedChats() |
| | | } |
| | | } catch { |
| | | // 刷新失败不影响已渲染的内容,静默处理 |
| | | } |
| | |
| | | return { |
| | | // state |
| | | chats, |
| | | archivedChats, |
| | | archiveLoaded, |
| | | chatGroups, |
| | | activeChatId, |
| | | tree, |
| | | streaming, |
| | |
| | | activeChat, |
| | | // actions |
| | | loadChats, |
| | | loadArchivedChats, |
| | | loadChatGroups, |
| | | createChatGroup, |
| | | renameChatGroup, |
| | | deleteChatGroup, |
| | | moveChatToGroup, |
| | | searchKeyword, |
| | | searchResults, |
| | | searching, |
| | | searchChats, |
| | | clearSearch, |
| | | // 归档列表的搜索(独立的另一套状态) |
| | | archiveKeyword, |
| | | archiveResults, |
| | | archiveSearching, |
| | | searchArchive, |
| | | clearArchiveSearch, |
| | | createChat, |
| | | renameChat, |
| | | updateChatMeta, |
| | | setPinned, |
| | | setArchived, |
| | | exportChat, |
| | | removeChat, |
| | | loadMessages, |
| | | switchBranch, |