const { contextBridge, ipcRenderer } = require('electron'); // Import types import type { IpcRendererEvent } from 'electron'; import type { LLMConfig, DocumentMetadata } from './types'; interface Directory { name: string; path: string; } interface IpcResponse { success: boolean; data?: T; error?: string; } // Expose protected methods that allow the renderer process to use // the ipcRenderer without exposing the entire object contextBridge.exposeInMainWorld('electron', { // File System Operations startWatching: async (dirPath: string): Promise => ipcRenderer.invoke('start-watching', dirPath), stopWatching: async (dirPath: string): Promise => ipcRenderer.invoke('stop-watching', dirPath), addExcludedPath: async (path: string): Promise => ipcRenderer.invoke('add-excluded-path', path), // LLM Operations queryLLM: async (question: string): Promise<{ answer: string; sources: DocumentMetadata[]; }> => ipcRenderer.invoke('query-llm', question), getLLMConfig: async (): Promise => ipcRenderer.invoke('get-llm-config'), // Vector Store Operations getDocuments: async (): Promise> => ipcRenderer.invoke('get-documents'), addDocument: async (content: string, metadata: DocumentMetadata): Promise => ipcRenderer.invoke('add-document', content, metadata), deleteDocument: async (path: string): Promise => ipcRenderer.invoke('delete-document', path), updateDocument: async (content: string, metadata: DocumentMetadata): Promise => ipcRenderer.invoke('update-document', content, metadata), // File Processing processFile: async (filePath: string): Promise => ipcRenderer.invoke('process-file', filePath), // System Paths getUserHome: async (): Promise> => ipcRenderer.invoke('get-user-home'), getAppPath: async (): Promise => ipcRenderer.invoke('get-app-path'), // Directory Operations listDirectories: async (dirPath: string): Promise> => ipcRenderer.invoke('list-directories', dirPath), // Event Handling on: (channel: string, callback: (event: unknown, ...args: any[]) => void) => { ipcRenderer.on(channel, callback); }, off: (channel: string, callback: (event: unknown, ...args: any[]) => void) => { ipcRenderer.removeListener(channel, callback); }, checkOllama: async (): Promise<{ installed: boolean; running: boolean }> => ipcRenderer.invoke('check-ollama'), openExternal: (url: string) => ipcRenderer.invoke('open-external', url), // Model Operations checkModel: (modelName: string) => ipcRenderer.invoke('check-model', modelName), pullModel: (modelName: string, onProgress: (status: string) => void) => { const channel = `pull-model-progress-${modelName}`; ipcRenderer.on(channel, (_event: IpcRendererEvent, status: string) => onProgress(status)); return ipcRenderer.invoke('pull-model', modelName).finally(() => { ipcRenderer.removeListener(channel, onProgress); }); }, // Window Controls minimizeWindow: () => ipcRenderer.invoke('window-minimize'), maximizeWindow: () => ipcRenderer.invoke('window-maximize'), closeWindow: () => ipcRenderer.invoke('window-close'), }); // Export types for TypeScript export type { Directory, IpcResponse }; // For CommonJS compatibility if (typeof module !== 'undefined' && module.exports) { module.exports = {}; }