feat: 添加消息历史持久化和可视化查看功能

- 新增 messageStore.js 消息存储模块,支持自动保存所有收发消息
- 修改 main.js,在消息转发时自动记录到本地存储
- 修改 preload.js,暴露消息管理 IPC API
- 修改 App.js,添加消息历史查看界面
  - 统计信息面板(总数/接收/发送/会话数)
  - 会话列表和消息详情
  - 搜索、过滤、分页功能
  - 导出 JSON 和清空历史
- 新增完整文档(MESSAGE_HISTORY.md 等)
- 新增测试脚本 test-message-history.js

版本:v1.0.1
This commit is contained in:
2026-03-10 04:09:26 +08:00
parent 7c826af5d1
commit 0880813355
9 changed files with 1809 additions and 3 deletions

115
test-message-history.js Normal file
View File

@@ -0,0 +1,115 @@
#!/usr/bin/env node
/**
* 消息存储测试脚本
* 用于验证消息持久化功能是否正常工作
*/
const path = require('path');
const fs = require('fs');
// 模拟 app.getPath('userData')
const mockUserData = path.join(process.env.HOME || process.env.USERPROFILE, '.config', 'wecome-openclaw-client');
console.log('='.repeat(60));
console.log('消息存储功能测试');
console.log('='.repeat(60));
// 测试 1: 检查 messageStore.js 是否存在
const messageStorePath = path.join(__dirname, 'electron', 'messageStore.js');
console.log('\n✓ 测试 1: 检查 messageStore.js');
if (fs.existsSync(messageStorePath)) {
console.log(' ✅ messageStore.js 存在');
} else {
console.log(' ❌ messageStore.js 不存在');
process.exit(1);
}
// 测试 2: 检查 main.js 是否引入了 messageStore
const mainPath = path.join(__dirname, 'electron', 'main.js');
console.log('\n✓ 测试 2: 检查 main.js 是否引入 messageStore');
const mainContent = fs.readFileSync(mainPath, 'utf-8');
if (mainContent.includes("require('./messageStore')")) {
console.log(' ✅ main.js 已引入 messageStore');
} else {
console.log(' ❌ main.js 未引入 messageStore');
process.exit(1);
}
// 测试 3: 检查 preload.js 是否暴露了消息 API
const preloadPath = path.join(__dirname, 'electron', 'preload.js');
console.log('\n✓ 测试 3: 检查 preload.js 是否暴露消息 API');
const preloadContent = fs.readFileSync(preloadPath, 'utf-8');
const requiredAPIs = [
'getMessages',
'getSessions',
'searchMessages',
'getMessageStats',
'markMessagesRead',
'exportMessages',
'clearMessages'
];
let allAPIsPresent = true;
requiredAPIs.forEach(api => {
if (preloadContent.includes(api)) {
console.log(`${api} 已暴露`);
} else {
console.log(`${api} 未暴露`);
allAPIsPresent = false;
}
});
if (!allAPIsPresent) {
process.exit(1);
}
// 测试 4: 检查 App.js 是否包含消息历史 UI
const appPath = path.join(__dirname, 'renderer', 'src', 'App.js');
console.log('\n✓ 测试 4: 检查 App.js 是否包含消息历史 UI');
const appContent = fs.readFileSync(appPath, 'utf-8');
const uiComponents = [
'showMessageHistory',
'messageStats',
'💬 消息历史'
];
let allUIPresent = true;
uiComponents.forEach(component => {
if (appContent.includes(component)) {
console.log(`${component} 存在`);
} else {
console.log(`${component} 不存在`);
allUIPresent = false;
}
});
if (!allUIPresent) {
process.exit(1);
}
// 测试 5: 检查消息存储目录
console.log('\n✓ 测试 5: 检查消息存储目录');
const messagesDir = path.join(mockUserData, 'messages');
console.log(` 预期路径:${messagesDir}`);
console.log(' 目录将在首次运行时自动创建');
// 测试 6: 检查文档
console.log('\n✓ 测试 6: 检查文档');
const docPath = path.join(__dirname, 'MESSAGE_HISTORY.md');
if (fs.existsSync(docPath)) {
console.log(' ✅ MESSAGE_HISTORY.md 存在');
const docContent = fs.readFileSync(docPath, 'utf-8');
console.log(` 📄 文档大小:${docContent.length} 字节`);
} else {
console.log(' ⚠️ MESSAGE_HISTORY.md 不存在(可选)');
}
console.log('\n' + '='.repeat(60));
console.log('✅ 所有测试通过!');
console.log('='.repeat(60));
console.log('\n下一步');
console.log('1. 安装依赖cd renderer && npm install');
console.log('2. 启动应用npm start');
console.log('3. 点击 "💬 消息历史" 按钮查看功能');
console.log('');