From a351d0ac795683370ed02505fe0cdd827af978a6 Mon Sep 17 00:00:00 2001 From: cirry <812852553@qq.com> Date: Sun, 12 Apr 2026 10:35:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/stores/history.js | 104 +++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/renderer/stores/history.js diff --git a/src/renderer/stores/history.js b/src/renderer/stores/history.js new file mode 100644 index 0000000..c8d6bec --- /dev/null +++ b/src/renderer/stores/history.js @@ -0,0 +1,104 @@ +import { defineStore } from 'pinia'; +import { ref } from 'vue'; +import axios from 'axios'; + +export const useHistoryStore = defineStore('history', () => { + // State + const historyItems = ref([]); + const isLoading = ref(false); + + // Getters + const getHistoryItems = () => historyItems.value; + const getHistoryById = (id) => historyItems.value.find((item) => item.id === id); + + // Actions + /** + * 加载历史会话列表 + */ + async function loadHistorySessions() { + console.log('[historyStore] 开始加载历史会话...'); + isLoading.value = true; + try { + const baseUrl = window.__opencodeBaseUrl || 'http://127.0.0.1:4096'; + console.log('[historyStore] baseUrl:', baseUrl); + const response = await axios.get(`${baseUrl}/session`); + console.log('[historyStore] 响应数据:', response.data); + const sessions = response.data || []; + console.log('[historyStore] 会话数量:', sessions.length); + + // 将会话列表转换为历史记录格式 + historyItems.value = sessions.map((session) => ({ + id: session.id, + name: session.title || `会话 ${session.slug || session.id.slice(0, 8)}...`, + slug: session.slug, + created: session.time?.created, + updated: session.time?.updated, + })); + console.log('[historyStore] 转换后的历史记录:', historyItems.value); + } catch (err) { + console.error('[historyStore] 加载历史会话失败:', err); + historyItems.value = []; + } finally { + isLoading.value = false; + } + } + + /** + * 清空历史记录 + */ + function clearHistory() { + historyItems.value = []; + } + + /** + * 添加单个历史记录项(用于新创建的会话) + */ + function addHistoryItem(item) { + // 检查是否已存在 + const exists = historyItems.value.find((h) => h.id === item.id); + if (!exists) { + historyItems.value.unshift({ + id: item.id, + name: item.title || `会话 ${item.slug || item.id.slice(0, 8)}...`, + slug: item.slug, + created: item.time?.created, + updated: item.time?.updated, + }); + } + } + + /** + * 创建新会话 + * @param {string} title - 会话标题 + * @returns {Promise} 创建的会话对象 + */ + async function createSession(title) { + console.log('[historyStore] 创建新会话,title:', title); + try { + const session = await window.opencode.createSession({ title }); + console.log('[historyStore] 创建会话成功:', session); + + // 添加到历史记录 + addHistoryItem(session); + + return session; + } catch (err) { + console.error('[historyStore] 创建会话失败:', err); + throw err; + } + } + + return { + // State + historyItems, + isLoading, + // Getters + getHistoryItems, + getHistoryById, + // Actions + loadHistorySessions, + clearHistory, + addHistoryItem, + createSession, + }; +});