2025-02-04 01:30:36 -05:00

77 lines
2.8 KiB
TypeScript

import { contextBridge, ipcRenderer } from 'electron';
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),
updateLLMConfig: async (config: LLMConfig): Promise<void> =>
ipcRenderer.invoke('update-llm-config', config),
getLLMConfig: async (): Promise<LLMConfig> =>
ipcRenderer.invoke('get-llm-config'),
getOllamaModels: async (): Promise<IpcResponse<string[]>> =>
ipcRenderer.invoke('get-ollama-models'),
// 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);
},
// Window Controls
minimizeWindow: () => ipcRenderer.invoke('window-minimize'),
maximizeWindow: () => ipcRenderer.invoke('window-maximize'),
closeWindow: () => ipcRenderer.invoke('window-close'),
});
export type { Directory, IpcResponse };