95 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-02-04 12:45:17 -05:00
const { contextBridge, ipcRenderer } = require('electron');
// Import types
2025-02-04 01:30:36 -05:00
import type { LLMConfig, DocumentMetadata } from './types';
interface Directory {
name: string;
path: string;
}
interface IpcResponse<T> {
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<void> =>
ipcRenderer.invoke('start-watching', dirPath),
stopWatching: async (dirPath: string): Promise<void> =>
ipcRenderer.invoke('stop-watching', dirPath),
addExcludedPath: async (path: string): Promise<void> =>
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<LLMConfig> =>
ipcRenderer.invoke('get-llm-config'),
// Vector Store Operations
getDocuments: async (): Promise<IpcResponse<DocumentMetadata[]>> =>
ipcRenderer.invoke('get-documents'),
addDocument: async (content: string, metadata: DocumentMetadata): Promise<void> =>
ipcRenderer.invoke('add-document', content, metadata),
deleteDocument: async (path: string): Promise<void> =>
ipcRenderer.invoke('delete-document', path),
updateDocument: async (content: string, metadata: DocumentMetadata): Promise<void> =>
ipcRenderer.invoke('update-document', content, metadata),
// File Processing
processFile: async (filePath: string): Promise<void> =>
ipcRenderer.invoke('process-file', filePath),
// System Paths
getUserHome: async (): Promise<IpcResponse<string>> =>
ipcRenderer.invoke('get-user-home'),
getAppPath: async (): Promise<string> =>
ipcRenderer.invoke('get-app-path'),
// Directory Operations
listDirectories: async (dirPath: string): Promise<IpcResponse<Directory[]>> =>
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);
},
2025-02-04 13:39:50 -05:00
checkOllama: async (): Promise<{ installed: boolean; running: boolean }> =>
ipcRenderer.invoke('check-ollama'),
2025-02-04 12:45:17 -05:00
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, status) => onProgress(status));
return ipcRenderer.invoke('pull-model', modelName).finally(() => {
ipcRenderer.removeListener(channel, onProgress);
});
},
2025-02-04 01:30:36 -05:00
// Window Controls
minimizeWindow: () => ipcRenderer.invoke('window-minimize'),
maximizeWindow: () => ipcRenderer.invoke('window-maximize'),
closeWindow: () => ipcRenderer.invoke('window-close'),
});
2025-02-04 12:45:17 -05:00
// Export types for TypeScript
2025-02-04 01:30:36 -05:00
export type { Directory, IpcResponse };
2025-02-04 12:45:17 -05:00
// For CommonJS compatibility
if (typeof module !== 'undefined' && module.exports) {
module.exports = {};
}