feat: 首页开发

This commit is contained in:
2026-04-12 10:35:58 +08:00
parent 9a91093325
commit a351d0ac79

View File

@@ -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<Object>} 创建的会话对象
*/
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,
};
});