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)
91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
/**
|
|
* FTS5 full-text search index management — Electron adapter layer.
|
|
*
|
|
* Core FTS operations are delegated to @openchatlab/node-runtime.
|
|
* This module adds session-level DB connection management.
|
|
*/
|
|
|
|
import Database from 'better-sqlite3'
|
|
import { BetterSqliteAdapter } from '@openchatlab/node-runtime'
|
|
import {
|
|
hasFtsTable,
|
|
createFtsTable as coreCreateFtsTable,
|
|
buildFtsIndex as coreBuildFtsIndex,
|
|
rebuildFtsIndex as coreRebuildFtsIndex,
|
|
insertFtsEntries as coreInsertFtsEntries,
|
|
searchByFts as coreSearchByFts,
|
|
} from '@openchatlab/node-runtime'
|
|
import { getDbPath, openDatabase } from '../core'
|
|
|
|
function openWritableDb(sessionId: string): Database.Database | null {
|
|
const dbPath = getDbPath(sessionId)
|
|
try {
|
|
const db = new Database(dbPath)
|
|
db.pragma('journal_mode = WAL')
|
|
return db
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function hasFtsIndex(sessionId: string): boolean {
|
|
const db = openDatabase(sessionId)
|
|
if (!db) return false
|
|
try {
|
|
return hasFtsTable(new BetterSqliteAdapter(db))
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export function createFtsTable(db: Database.Database): void {
|
|
coreCreateFtsTable(new BetterSqliteAdapter(db))
|
|
}
|
|
|
|
export function buildFtsIndex(sessionId: string): { indexed: number } {
|
|
const db = openWritableDb(sessionId)
|
|
if (!db) return { indexed: 0 }
|
|
|
|
try {
|
|
return coreBuildFtsIndex(new BetterSqliteAdapter(db))
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export function rebuildFtsIndex(sessionId: string): { indexed: number } {
|
|
const db = openWritableDb(sessionId)
|
|
if (!db) return { indexed: 0 }
|
|
|
|
try {
|
|
return coreRebuildFtsIndex(new BetterSqliteAdapter(db))
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export function insertFtsEntries(sessionId: string, entries: Array<{ id: number; content: string | null }>): void {
|
|
const db = openWritableDb(sessionId)
|
|
if (!db) return
|
|
|
|
try {
|
|
coreInsertFtsEntries(new BetterSqliteAdapter(db), entries)
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export function searchByFts(
|
|
sessionId: string,
|
|
keywords: string[],
|
|
limit = 1000,
|
|
offset = 0
|
|
): { rowids: number[]; total: number } {
|
|
if (keywords.length === 0) return { rowids: [], total: 0 }
|
|
|
|
const db = openDatabase(sessionId)
|
|
if (!db) return { rowids: [], total: 0 }
|
|
|
|
return coreSearchByFts(new BetterSqliteAdapter(db), keywords, limit, offset)
|
|
}
|