feat: 优化

This commit is contained in:
电摇小子
2025-12-31 09:50:46 +08:00
parent 755d11f71c
commit 7c3fa728f7
4 changed files with 24 additions and 47 deletions
+12 -12
View File
@@ -6,6 +6,17 @@ import { WechatDb, Contact, WechatMessage } from './wechat-db'
let wechatDb: WechatDb | null = null
const MSG_TYPE_DICT: Record<number, string> = {
1: '普通文本',
3: '图片',
34: '语音',
43: '视频',
47: '表情包',
48: '位置',
49: '分享消息',
10000: '系统消息'
}
function createWindow(): void {
// 创建浏览器窗口
const mainWindow = new BrowserWindow({
@@ -117,17 +128,6 @@ app.whenReady().then(() => {
const groupMembers = wechatDb.getAllGroupMembers()
return rawMessages.map((msg: WechatMessage) => {
const typeDict: Record<number, string> = {
1: '普通文本',
3: '图片',
34: '语音',
43: '视频',
47: '表情包',
48: '位置',
49: '分享消息',
10000: '系统消息'
}
const msgType = parseInt(msg.messageType)
const createTime = parseInt(msg.msgCreateTime)
const date = new Date(createTime * 1000)
@@ -162,7 +162,7 @@ app.whenReady().then(() => {
return {
id: msg.mesLocalID || Math.random().toString(),
from: msg.mesDes === '1' ? 'user' : 'assistant', // 1 通常是接收到的,0 是发送的?需要验证。Swift 说:[1: "user", 0: "assistant"]
type: typeDict[msgType] || msg.messageType,
type: MSG_TYPE_DICT[msgType] || msg.messageType,
datetime: date.toLocaleString('zh-CN', { hour12: false }),
content: content,
isSender: msg.mesDes === '0',
+4 -24
View File
@@ -62,7 +62,6 @@ export class WechatDb {
}
private connectDb(dbPath: string): Database | null {
// 1. 路径预检查
const targetPath = path.resolve(dbPath)
if (!fs.existsSync(targetPath)) {
console.error(`❌ 错误:数据库文件不存在于路径:${targetPath}`)
@@ -70,45 +69,26 @@ export class WechatDb {
}
try {
// console.log(`🔌 正在尝试连接至:${path.basename(targetPath)}`)
// console.log('路径是', targetPath)
// 2. 初始化数据库句柄
// 关键:使用 better-sqlite3-multiple-ciphers 的构造函数
// verbose 选项有助于调试 Pragma 的执行情况
const db = new DatabaseConstructor(targetPath, {
// verbose: console.log
})
// 此时 db 实例已创建,但因为未提供密钥,任何数据读取都会失败。
// 必须立即配置加密参数。
// 3. 配置加密方案 (Cipher Scheme)
// 必须显式切换到 'sqlcipher',因为该库默认可能使用 'chacha20'
// 配置加密方案 (Cipher Scheme)
db.pragma("cipher='sqlcipher'")
// 4. 配置页大小 (Page Size)
// macOS 版 WeChat 使用 1024 字节。这必须在设置密钥前(或紧随其后)生效。
db.pragma('cipher_page_size = 1024')
// 5. 配置兼容性与算法细节
// 显式指定 SQLCipher 3 的行为
db.pragma('legacy=3')
db.pragma('kdf_iter = 64000')
db.pragma('cipher_hmac_algorithm = HMAC_SHA1')
db.pragma('cipher_kdf_algorithm = PBKDF2_HMAC_SHA1')
// 6. 应用密钥
// 使用正确的 hex 语法: key = "x'HEXSTRING'"
const processedKey = this.processRawKey(this.rawKey)
// console.log(`🔑 应用密钥 (前6位): ${processedKey.substring(0, 6)}...`)
db.pragma(`key = "x'${processedKey}'"`)
// 7. 验证连接
// 如果参数错误,这里将抛出 "file is not a database" 或 "HMAC check failed"
// 验证连接
db.prepare('SELECT count(*) as count FROM sqlite_master').get()
// console.log(`✅ 连接成功!当前数据库 ${path.basename(targetPath)} 包含 ${row.count} 张表。`)
return db
} catch (error) {
console.error(`❌ 连接失败:`, error)
@@ -241,7 +221,7 @@ export class WechatDb {
public getGroupMember(wxid: string): GroupMemberInfo | null {
if (!this.correctUserId) return null
// 1. 检查缓存
// 检查缓存
if (this.groupMemberCache.has(wxid)) {
return this.groupMemberCache.get(wxid) || null
}
@@ -261,7 +241,7 @@ export class WechatDb {
db.close()
// 2. 写入缓存
// 写入缓存
this.groupMemberCache.set(wxid, result || null)
return result || null
+7 -10
View File
@@ -28,6 +28,7 @@ const systemPrompt = `你是一个中文的群聊总结的助手,你可以为
2. 使用中文冒号
3. 无需大标题
4. 开始给出本群讨论风格的整体评价,例如活跃、太水、太黄、太暴力、话题不集中、无聊诸如此类
5. 每个话题详细写出参与者
最后总结下今日最活跃的前五个发言者`
@@ -149,16 +150,12 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
alert('当前没有消息可供总结')
return
}
const filteredMessages = messages.filter((msg) => {
delete msg.img
// @ts-ignore 暂时去除
delete msg.from
// @ts-ignore 暂时去除
delete msg.id
// @ts-ignore 暂时去除
delete msg.isSender
return !'分享消息,图片,表情包,视频'.split(',').includes(msg.type)
})
const filteredMessages = messages
.filter((msg) => !'分享消息,图片,表情包,视频'.split(',').includes(msg.type))
.map((msg) => {
const { img, id, isSender, ...rest } = msg
return rest
})
const recentMessages = filteredMessages
.map((msg) => {
return `${msg.datetime} ${msg.from}: ${msg.content}`