Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions core/store/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// ever touches the state DB — never write paper markdown/PDF bytes here.

import { existsSync, mkdirSync } from 'node:fs'
import { dirname } from 'node:path'
import { dirname, resolve } from 'node:path'

import Database from 'better-sqlite3'

Expand All @@ -15,10 +15,22 @@ import { runMigrations } from './migrate.js'
export interface OpenDbOptions {
/** Override the DB file path. Defaults to `data/app.db` under `cwd`. Pass `:memory:` for tests. */
path?: string
/** Override the native binding location. Auto-detected for Electron if omitted. */
nativeBinding?: string
}

const DEFAULT_DB_PATH = 'data/app.db'

function resolveNativeBinding(): string | undefined {
if (process.versions.electron) {
const electronBinding = resolve(process.cwd(), 'resources/native/better_sqlite3.electron.node')
if (existsSync(electronBinding)) {
return electronBinding
}
}
return undefined
}

/**
* Open (creating if needed) the Vellum state database and bring its schema
* up to date. Safe to call repeatedly — schema application is idempotent via
Expand All @@ -34,7 +46,8 @@ export function openDb(options: OpenDbOptions = {}): Database.Database {
}
}

const db = new Database(path)
const nativeBinding = options.nativeBinding ?? resolveNativeBinding()
const db = new Database(path, nativeBinding ? { nativeBinding } : undefined)
db.pragma('journal_mode = WAL')
db.pragma('foreign_keys = ON')

Expand Down
13 changes: 11 additions & 2 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'node:path'
import { defineConfig } from 'electron-vite'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'

// Single-package Electron layout:
Expand All @@ -8,17 +8,26 @@ import react from '@vitejs/plugin-react'
// core/ -> backend logic imported by the main process
export default defineConfig({
main: {
plugins: [
externalizeDepsPlugin({
exclude: ['@agentclientprotocol/sdk', 'unpdf', 'pdfjs-dist'],
}),
],
build: {
rollupOptions: {
input: resolve('electron/main.ts'),
output: { entryFileNames: 'index.js' },
output: {
entryFileNames: 'index.cjs',
format: 'cjs',
},
},
},
resolve: {
alias: { '@core': resolve('core') },
},
},
preload: {
plugins: [externalizeDepsPlugin()],
build: {
rollupOptions: {
input: resolve('electron/preload.ts'),
Expand Down
11 changes: 9 additions & 2 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { IpcMainInvokeEvent } from 'electron'
import { randomUUID } from 'node:crypto'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { existsSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import type { Database } from 'better-sqlite3'

Expand Down Expand Up @@ -35,13 +36,19 @@ const SLUG_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/
//
// Everything below is the minimal boot skeleton. Feature work attaches here.

function resolvePreloadPath(): string {
const mjs = join(__dirname, '../preload/preload.mjs')
if (existsSync(mjs)) return mjs
return join(__dirname, '../preload/index.js')
}

function createWindow(): void {
const win = new BrowserWindow({
width: 1440,
height: 900,
show: false,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
preload: resolvePreloadPath(),
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
Expand Down Expand Up @@ -102,7 +109,7 @@ ipcMain.handle('vellum:ingest', async (_event, input: unknown): Promise<IngestRe

const SORT_COLUMN_VALUES: readonly PaperSortColumn[] = ['addedAt', 'year', 'title']

// [P1-08] Library IPC seam. `options` arrives from the renderer as untyped
// [P1-08] Library grid data. `options` arrives from the renderer as untyped
// `unknown` — narrowed field-by-field here rather than trusted via a cast.
// core/library/repo.ts's listPapers() is independently defensive about a bad
// `sort` value (falls back to the added_at default), so this narrowing is
Expand Down
Loading
Loading