File size: 10,548 Bytes
b1cc7ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
// 定义会话中单个节点的结构 (如果比纯文本更复杂)
export interface SessionNode {
id: string; // 或者其他唯一标识符
text: string;
translatedText?: string; // 可选的翻译文本
timestamp: number; // 时间戳
// 可以添加其他元数据,如语言、说话人等
}
// 定义存储在 Pinia 和用于 Modal 列表的会话摘要结构
export interface SessionSummary {
startTime: number; // 作为唯一 ID 和排序依据
title: string; // 第一句话的前10个字
outline: string[]; // 前两行内容
nodeCount: number; // 会话中的节点总数
}
const LOCAL_STORAGE_SESSION_PREFIX = 'rt_session_'; // 本地存储键前缀
export const useSessionStore = defineStore('session', () => {
// --- State ---
// 会话摘要列表,将由 pinia-plugin-persistedstate 自动持久化
const sessionSummaries = ref<SessionSummary[]>([]);
// 当前活动会话的节点 (不持久化)
const currentSessionNodes = ref<SessionNode[]>([]);
// 当前活动会话的开始时间 (不持久化)
const currentSessionStartTime = ref<number | null>(null);
// 标记会话是否正在进行中 (不持久化)
const isSessionActive = ref(false);
// --- Getters ---
// 按开始时间降序排列的会话摘要
const sortedSessionSummaries = computed(() => {
// 创建副本进行排序,避免直接修改响应式 ref
return [...sessionSummaries.value].sort((a, b) => b.startTime - a.startTime);
});
// --- Actions ---
/**
* 开始一个新的会话
*/
function startSession() {
if (isSessionActive.value) {
console.warn("尝试在已有活动会话时开始新会话。");
// 可以选择结束旧会话或直接返回
// endSession(); // 如果需要自动结束旧会话
return;
}
currentSessionStartTime.value = Date.now();
currentSessionNodes.value = [];
isSessionActive.value = true;
console.log(`新会话开始于: ${new Date(currentSessionStartTime.value).toLocaleString()}`);
}
/**
* 向当前活动会话添加一个节点
* @param node - 要添加的会话节点
*/
function addNode(node: SessionNode) {
if (!isSessionActive.value || !currentSessionStartTime.value) {
console.warn("没有活动的会话来添加节点。");
return;
}
currentSessionNodes.value.push(node);
// 可选:如果需要更强的容错性,可以在这里进行增量保存到 localStorage
// saveCurrentSessionToLocalStorage();
}
/**
* 结束当前活动会话,保存完整内容到 localStorage,并更新摘要列表
*/
function endSession() {
if (!isSessionActive.value || !currentSessionStartTime.value) {
console.log("没有活动的会话可以结束。");
// 确保状态被重置
isSessionActive.value = false;
currentSessionStartTime.value = null;
currentSessionNodes.value = [];
return;
}
const startTime = currentSessionStartTime.value;
const nodes = [...currentSessionNodes.value]; // 创建副本
// 重置当前会话状态
isSessionActive.value = false;
currentSessionStartTime.value = null;
currentSessionNodes.value = [];
if (nodes.length === 0) {
console.log("会话结束,但没有节点需要保存。");
return;
}
// 1. 生成摘要信息
const title = nodes[0]?.text.substring(0, 10) || '无标题会话';
const n1 = nodes[0];
const outline = [
`${n1?.text.substring(0, 56)}...\n`,
`${n1?.translatedText?.substring(0, 56)}...\n`,
]
// `${n1?.text.substring(0, 56)}\n${'-'.repeat(60)}\n${n1.translatedText?.substring(0, 56)}\n`
const summary: SessionSummary = {
startTime,
title,
outline,
nodeCount: nodes.length,
};
// 2. 保存完整会话内容到 Local Storage
try {
const storageKey = `${LOCAL_STORAGE_SESSION_PREFIX}${startTime}`;
localStorage.setItem(storageKey, JSON.stringify(nodes));
console.log(`完整会话 ${startTime} 已保存到 localStorage.`);
// 3. 更新 Pinia 中的摘要列表
// 检查是否已存在相同 startTime 的摘要 (理论上不应发生,除非手动操作或错误)
const existingIndex = sessionSummaries.value.findIndex(s => s.startTime === startTime);
if (existingIndex === -1) {
sessionSummaries.value.push(summary);
} else {
console.warn(`会话摘要 ${startTime} 已存在,将进行覆盖。`);
sessionSummaries.value[existingIndex] = summary;
}
// pinia-plugin-persistedstate 会自动处理 sessionSummaries 的持久化
console.log(`会话 ${startTime} 结束并已处理。`);
} catch (error) {
console.error("保存会话到 localStorage 时出错:", error);
// 这里可以添加用户反馈,例如提示存储空间不足
// 也许需要决定是否回滚摘要列表的添加
}
}
/**
* 从 Local Storage 加载指定会话的完整内容
* @param startTime - 会话的开始时间戳 (作为 ID)
* @returns SessionNode[] | null - 会话节点数组或在未找到/出错时返回 null
*/
function loadSessionContent(startTime: number): SessionNode[] | null {
try {
const storageKey = `${LOCAL_STORAGE_SESSION_PREFIX}${startTime}`;
const storedData = localStorage.getItem(storageKey);
if (storedData) {
const nodes = JSON.parse(storedData) as SessionNode[];
console.log(`从 localStorage 加载了会话 ${startTime} 的内容 (${nodes.length} 个节点)`);
return nodes;
}
console.warn(`在 localStorage 中未找到键为 ${storageKey} 的会话数据。`);
return null;
} catch (error) {
console.error(`从 localStorage 加载会话 ${startTime} 时出错:`, error);
return null;
}
}
/**
* 删除指定的会话 (包括摘要和本地存储的完整内容)
* @param startTime - 要删除的会话的开始时间戳
*/
function deleteSession(startTime: number) {
try {
// 1. 从摘要列表中移除
const index = sessionSummaries.value.findIndex(s => s.startTime === startTime);
if (index > -1) {
sessionSummaries.value.splice(index, 1);
console.log(`会话摘要 ${startTime} 已从 Pinia store 中移除。`);
// pinia-plugin-persistedstate 会自动更新持久化的摘要列表
} else {
console.warn(`尝试删除一个不存在的会话摘要: ${startTime}`);
}
// 2. 从 Local Storage 中移除完整内容
const storageKey = `${LOCAL_STORAGE_SESSION_PREFIX}${startTime}`;
localStorage.removeItem(storageKey);
console.log(`会话 ${startTime} 的完整内容已从 localStorage 中移除。`);
} catch (error) {
console.error(`删除会话 ${startTime} 时出错:`, error);
}
}
// --- 返回 State, Getters, Actions ---
return {
// State
sessionSummaries, // 摘要列表 (将被持久化)
currentSessionNodes, // 当前活动会话的节点 (用于可能的实时显示)
currentSessionStartTime, // 当前活动会话的开始时间
isSessionActive, // 会话是否活动
// Getters
sortedSessionSummaries, // 排序后的摘要列表
// Actions
startSession,
addNode,
endSession,
loadSessionContent, // 用于下载按钮点击时加载数据
deleteSession,
};
}, {
// Pinia 持久化配置
persist: {
// 只持久化 sessionSummaries 状态
paths: ['sessionSummaries'],
// 默认使用 localStorage,如果需要可以指定
// storage: localStorage,
},
});
/**
* 辅助函数:触发浏览器下载会话数据
* @param startTime - 会话开始时间,用于文件名
* @param nodes - 要下载的会话节点数据
* @param format - 'json' 或 'txt' (默认为 'json')
*/
export function downloadSessionData(startTime: number, nodes: SessionNode[], format: 'json' | 'txt' = 'json') {
if (!nodes || nodes.length === 0) {
console.error("没有数据可供下载:", startTime);
alert("没有内容可以下载。"); // 给用户反馈
return;
}
try {
const dateStr = new Date(startTime).toISOString().split('T')[0]; // YYYY-MM-DD
let dataStr: string;
let mimeType: string;
let fileExtension: string;
if (format === 'txt') {
dataStr = nodes.map(n => `${new Date(n.timestamp).toLocaleTimeString()} - ${n.text}`).join('\n');
mimeType = 'text/plain;charset=utf-8;';
fileExtension = 'txt';
} else { // 默认为 json
dataStr = JSON.stringify(nodes, null, 2); // 美化 JSON 输出
mimeType = 'application/json;charset=utf-8;';
fileExtension = 'json';
}
const filename = `session_${dateStr}_${startTime}.${fileExtension}`;
const blob = new Blob([dataStr], { type: mimeType });
const link = document.createElement("a");
// 使用 createObjectURL 创建一个临时的 URL 指向 Blob 对象
const url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click(); // 模拟点击下载链接
// 清理:移除链接并释放 URL 对象
document.body.removeChild(link);
URL.revokeObjectURL(url);
console.log(`已触发下载会话 ${startTime} 为 ${filename}`);
} catch (error) {
console.error(`下载会话 ${startTime} 时出错:`, error);
alert("下载文件时发生错误。"); // 给用户反馈
}
}
|