mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
Move Electron-specific code (main, preload, shared) and configs (electron.vite, electron-builder) into apps/desktop/ to prepare for multi-platform support (Web Browser standalone). - Create apps/desktop/ with dedicated package.json and tsconfig - Split Electron deps (electron, electron-updater, better-sqlite3, etc.) from shared frontend deps in root package.json - Declare better-sqlite3 and @node-rs/jieba in packages/node-runtime - Add stream-json to packages/parser where it belongs - Update all path aliases (@electron/* → apps/desktop/*) in tsconfig, vite configs, and relative imports in preload/main - Keep slim root tsconfig.node.json as shared base for packages - Update CI/CD release workflow for new directory layout - Fix pre-existing type errors (pi-ai Model generic, SQLLabTab LLMChatStreamChunk, filter.test.ts empty methods)
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
/**
|
|
* 临时数据库模块
|
|
* 用于合并导入时的临时文件处理
|
|
*/
|
|
|
|
import Database from 'better-sqlite3'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
|
|
// 在 Worker 线程中,无法直接使用 electron 的 app 模块
|
|
// 需要通过其他方式获取临时目录
|
|
function getTempDir(): string {
|
|
// 在 worker 中,使用系统临时目录
|
|
const tmpDir = process.env.TMPDIR || process.env.TMP || '/tmp'
|
|
const chatLabTmp = path.join(tmpDir, 'chatlab-temp')
|
|
if (!fs.existsSync(chatLabTmp)) {
|
|
fs.mkdirSync(chatLabTmp, { recursive: true })
|
|
}
|
|
return chatLabTmp
|
|
}
|
|
|
|
/**
|
|
* 创建临时数据库用于合并
|
|
*/
|
|
export function createTempDatabase(): { db: Database.Database; path: string } {
|
|
const tempDir = getTempDir()
|
|
const tempPath = path.join(tempDir, `merge_${Date.now()}_${Math.random().toString(36).slice(2)}.db`)
|
|
const db = new Database(tempPath)
|
|
|
|
db.pragma('journal_mode = WAL')
|
|
db.pragma('synchronous = NORMAL')
|
|
db.pragma('cache_size = -64000')
|
|
|
|
// 创建临时表结构
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS member (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
platform_id TEXT NOT NULL UNIQUE,
|
|
account_name TEXT,
|
|
group_nickname TEXT,
|
|
aliases TEXT DEFAULT '[]',
|
|
avatar TEXT,
|
|
roles TEXT DEFAULT '[]'
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS message (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
sender_platform_id TEXT NOT NULL,
|
|
sender_account_name TEXT,
|
|
sender_group_nickname TEXT,
|
|
ts INTEGER NOT NULL,
|
|
type INTEGER NOT NULL,
|
|
content TEXT,
|
|
reply_to_message_id TEXT DEFAULT NULL,
|
|
platform_message_id TEXT DEFAULT NULL
|
|
);
|
|
`)
|
|
|
|
return { db, path: tempPath }
|
|
}
|
|
|
|
/**
|
|
* 关闭并清理临时数据库
|
|
*/
|
|
export function cleanupTempDatabase(dbPath: string): void {
|
|
try {
|
|
// 尝试删除 WAL 和 SHM 文件
|
|
const walPath = `${dbPath}-wal`
|
|
const shmPath = `${dbPath}-shm`
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
fs.unlinkSync(walPath)
|
|
}
|
|
if (fs.existsSync(shmPath)) {
|
|
fs.unlinkSync(shmPath)
|
|
}
|
|
if (fs.existsSync(dbPath)) {
|
|
fs.unlinkSync(dbPath)
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to clean up temp databases:', err)
|
|
}
|
|
}
|
|
|
|
// Re-export the canonical dedup key generator from core
|
|
export { generateMessageKey } from '@openchatlab/core'
|