diff options
| author | Clawd <ai@clawd.bot> | 2026-02-28 07:27:49 -0800 |
|---|---|---|
| committer | Clawd <ai@clawd.bot> | 2026-02-28 07:27:49 -0800 |
| commit | 66f66d1c17213f55aa56d69c0cccc309b16f3362 (patch) | |
| tree | 5a6464c7f36e7c731f6d07995856df00bce257a4 /src/main/index.ts | |
| parent | 332e5cec2992fefb302251962a3ceca38437a110 (diff) | |
Phase 3: IPC layer
- Implement src/main/preload.ts with typed API bridge
- Projects, sessions, messages CRUD
- Chat send/interrupt
- Workflow review/advance/permissions
- Artifact read/write
- Directory picker dialog
- Claude message event subscription
- Implement src/main/ipc/handlers.ts
- All IPC handlers with proper error handling
- Message forwarding to renderer
- Assistant message storage
- Update src/main/index.ts
- Initialize database on startup
- Register IPC handlers
- Clean database close on exit
Diffstat (limited to 'src/main/index.ts')
| -rw-r--r-- | src/main/index.ts | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/src/main/index.ts b/src/main/index.ts index b164f15..f0b23f7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts | |||
| @@ -1,63 +1,61 @@ | |||
| 1 | import Database from 'better-sqlite3' | 1 | import { app, BrowserWindow } from "electron"; |
| 2 | import { app, BrowserWindow } from 'electron' | 2 | import path from "node:path"; |
| 3 | import fs from 'node:fs' | 3 | import { getDb, closeDb } from "./db"; |
| 4 | import path from 'node:path' | 4 | import { registerIpcHandlers } from "./ipc/handlers"; |
| 5 | 5 | ||
| 6 | const isDev = !app.isPackaged // reliable dev/prod switch. [oai_citation:2‡Electron](https://electronjs.org/docs/latest/api/app?utm_source=chatgpt.com) | 6 | const isDev = !app.isPackaged; |
| 7 | let mainWindow: BrowserWindow | null = null; | ||
| 7 | 8 | ||
| 8 | function createWindow() { | 9 | function createWindow() { |
| 9 | const win = new BrowserWindow({ | 10 | mainWindow = new BrowserWindow({ |
| 10 | width: 800, | 11 | width: 1400, |
| 11 | height: 600, | 12 | height: 900, |
| 13 | minWidth: 1000, | ||
| 14 | minHeight: 600, | ||
| 12 | show: false, | 15 | show: false, |
| 16 | titleBarStyle: "hiddenInset", | ||
| 13 | webPreferences: { | 17 | webPreferences: { |
| 14 | contextIsolation: true, | 18 | contextIsolation: true, |
| 15 | nodeIntegration: false, | 19 | nodeIntegration: false, |
| 16 | preload: path.join(__dirname, 'preload.js'), | 20 | preload: path.join(__dirname, "preload.js"), |
| 17 | }, | 21 | }, |
| 18 | }) | 22 | }); |
| 23 | |||
| 24 | registerIpcHandlers(mainWindow); | ||
| 19 | 25 | ||
| 20 | if (isDev) { | 26 | if (isDev) { |
| 21 | const url = process.env.VITE_DEV_SERVER_URL ?? 'http://localhost:5173' | 27 | const url = process.env.VITE_DEV_SERVER_URL ?? "http://localhost:5173"; |
| 22 | win.loadURL(url).finally(() => { | 28 | mainWindow.loadURL(url).finally(() => { |
| 23 | win.show() | 29 | mainWindow!.show(); |
| 24 | win.webContents.openDevTools({ mode: 'detach' }) | 30 | mainWindow!.webContents.openDevTools({ mode: "detach" }); |
| 25 | }) | 31 | }); |
| 26 | } else { | 32 | } else { |
| 27 | const indexHtml = path.join( | 33 | const indexHtml = path.join( |
| 28 | app.getAppPath(), | 34 | app.getAppPath(), |
| 29 | 'renderer', | 35 | "renderer", |
| 30 | 'dist', | 36 | "dist", |
| 31 | 'index.html' | 37 | "index.html" |
| 32 | ) | 38 | ); |
| 33 | win.loadFile(indexHtml).finally(() => win.show()) | 39 | mainWindow.loadFile(indexHtml).finally(() => mainWindow!.show()); |
| 34 | } | 40 | } |
| 35 | } | 41 | } |
| 36 | 42 | ||
| 37 | function initDb() { | ||
| 38 | const dbDir = app.getPath('userData') | ||
| 39 | if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true }) | ||
| 40 | const dbPath = path.join(dbDir, 'app.db') | ||
| 41 | |||
| 42 | const db = new Database(dbPath) | ||
| 43 | db.pragma('journal_mode = WAL') | ||
| 44 | db.prepare('CREATE TABLE IF NOT EXISTS messages (text TEXT)').run() | ||
| 45 | db.prepare('INSERT INTO messages (text) VALUES (?)').run( | ||
| 46 | 'hello from better-sqlite3' | ||
| 47 | ) | ||
| 48 | const row = db.prepare('SELECT text FROM messages LIMIT 1').get() | ||
| 49 | console.log('Selected row:', row) | ||
| 50 | db.close() | ||
| 51 | } | ||
| 52 | |||
| 53 | app.whenReady().then(() => { | 43 | app.whenReady().then(() => { |
| 54 | initDb() | 44 | // Initialize database |
| 55 | createWindow() | 45 | getDb(); |
| 56 | app.on( | 46 | |
| 57 | 'activate', | 47 | createWindow(); |
| 58 | () => BrowserWindow.getAllWindows().length === 0 && createWindow() | 48 | |
| 59 | ) | 49 | app.on("activate", () => { |
| 60 | }) | 50 | if (BrowserWindow.getAllWindows().length === 0) { |
| 61 | app.on('window-all-closed', () => { | 51 | createWindow(); |
| 62 | if (process.platform !== 'darwin') app.quit() | 52 | } |
| 63 | }) | 53 | }); |
| 54 | }); | ||
| 55 | |||
| 56 | app.on("window-all-closed", () => { | ||
| 57 | closeDb(); | ||
| 58 | if (process.platform !== "darwin") { | ||
| 59 | app.quit(); | ||
| 60 | } | ||
| 61 | }); | ||
