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:
@@ -8,6 +8,7 @@ const { v4: uuidv4 } = require('uuid');
|
||||
const { WSClient } = require('@wecom/aibot-node-sdk');
|
||||
const WebSocket = require('ws');
|
||||
const crypto = require('crypto');
|
||||
const messageStore = require('./messageStore');
|
||||
|
||||
// 初始化配置存储
|
||||
const store = new Store({
|
||||
@@ -221,6 +222,24 @@ class WeComConnection {
|
||||
console.log('[OpenClaw] Sending via chat.send:', sessionKey);
|
||||
openclawConnection.send(JSON.stringify(chatSendMessage));
|
||||
console.log(`[Forward] WeCom -> OpenClaw: ${messageId}`);
|
||||
|
||||
// 记录消息到存储
|
||||
messageStore.saveMessage({
|
||||
direction: 'inbound',
|
||||
source: 'wecom',
|
||||
sessionId: sessionKey,
|
||||
chatId: chatId,
|
||||
senderId: body.from?.userid || '',
|
||||
senderName: body.from?.name || body.from?.userid || '',
|
||||
messageType: body.image ? 'image' : body.file ? 'file' : body.voice ? 'voice' : 'text',
|
||||
content: text,
|
||||
attachments: attachments,
|
||||
metadata: {
|
||||
wecomMsgId: messageId,
|
||||
chatType: chatType,
|
||||
reqId: reqId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
scheduleReconnect() {
|
||||
@@ -467,6 +486,24 @@ class OpenClawConnection {
|
||||
try {
|
||||
await wecomConn.sendMessage(chatId, text, finish, streamId);
|
||||
console.log(`[Forward] OpenClaw -> WeCom: ${chatId}`);
|
||||
|
||||
// 记录消息到存储
|
||||
const sessionKey = `${botId}:${chatId}`;
|
||||
messageStore.saveMessage({
|
||||
direction: 'outbound',
|
||||
source: 'openclaw',
|
||||
sessionId: sessionKey,
|
||||
chatId: chatId,
|
||||
senderId: 'openclaw',
|
||||
senderName: 'OpenClaw AI',
|
||||
messageType: 'text',
|
||||
content: text,
|
||||
metadata: {
|
||||
streamId: streamId,
|
||||
finish: finish,
|
||||
botId: botId
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Forward] Error:', error);
|
||||
}
|
||||
@@ -646,6 +683,44 @@ function setupIpcHandlers() {
|
||||
return { success: false, message: `发送失败:${error.message}` };
|
||||
}
|
||||
});
|
||||
|
||||
// ============ 消息存储相关 IPC 处理器 ============
|
||||
|
||||
// 获取消息列表
|
||||
ipcMain.handle('get-messages', (event, options = {}) => {
|
||||
return messageStore.getMessages(options);
|
||||
});
|
||||
|
||||
// 获取会话列表
|
||||
ipcMain.handle('get-sessions', () => {
|
||||
return messageStore.getSessions();
|
||||
});
|
||||
|
||||
// 搜索消息
|
||||
ipcMain.handle('search-messages', (event, query, options = {}) => {
|
||||
return messageStore.searchMessages(query, options);
|
||||
});
|
||||
|
||||
// 获取统计数据
|
||||
ipcMain.handle('get-message-stats', () => {
|
||||
return messageStore.getStats();
|
||||
});
|
||||
|
||||
// 标记消息为已读
|
||||
ipcMain.handle('mark-messages-read', (event, messageIds) => {
|
||||
return messageStore.markAsRead(messageIds);
|
||||
});
|
||||
|
||||
// 导出消息
|
||||
ipcMain.handle('export-messages', (event, options = {}) => {
|
||||
return messageStore.exportMessages(options);
|
||||
});
|
||||
|
||||
// 清空消息
|
||||
ipcMain.handle('clear-messages', (event, options = {}) => {
|
||||
messageStore.clear(options);
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
|
||||
Reference in New Issue
Block a user