Skip to content

Commit

Permalink
Merge pull request #71 from reorproject/multi-dir-attempt-2
Browse files Browse the repository at this point in the history
Multi dir attempt 2
  • Loading branch information
samlhuillier authored Feb 20, 2024
2 parents 26c6c47 + fa7e662 commit 5d94126
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 134 deletions.
91 changes: 52 additions & 39 deletions electron/main/Files/registerFilesHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { BrowserWindow, ipcMain } from "electron";
import { StoreKeys, StoreSchema } from "../Store/storeConfig";
import Store from "electron-store";
import { ipcMain } from "electron";
import * as path from "path";
import { FileInfoTree } from "./Types";
import {
Expand All @@ -9,25 +7,32 @@ import {
writeFileSyncRecursive,
} from "./Filesystem";
import * as fs from "fs";
import { LanceDBTableWrapper } from "../database/LanceTableWrapper";
import { updateFileInTable } from "../database/TableHelperFunctions";
import {
getVaultDirectoryForContents,
getWindowInfoForContents,
activeWindows,
} from "../windowManager";
import { errorToString } from "../Generic/error";

export const registerFileHandlers = (
store: Store<StoreSchema>,
dbTable: LanceDBTableWrapper,
win: BrowserWindow
) => {
export const registerFileHandlers = () => {
ipcMain.handle("join-path", (event, ...args) => {
return path.join(...args);
});

ipcMain.handle("get-files", async (): Promise<FileInfoTree> => {
const directoryPath: string = store.get(StoreKeys.UserDirectory);
if (!directoryPath) return [];
ipcMain.handle(
"get-files-for-window",
async (event): Promise<FileInfoTree> => {
const directoryPath = getVaultDirectoryForContents(
activeWindows,
event.sender
);
if (!directoryPath) return [];

const files: FileInfoTree = GetFilesInfoTree(directoryPath);
return files;
});
const files: FileInfoTree = GetFilesInfoTree(directoryPath);
return files;
}
);

ipcMain.handle(
"read-file",
Expand All @@ -38,28 +43,36 @@ export const registerFileHandlers = (

ipcMain.handle(
"write-file",
async (event, filePath: string, content: string): Promise<void> => {
console.log("Writing file", filePath);
await fs.writeFileSync(filePath, content, "utf-8");
console.log("Done writing file", filePath);
async (event, filePath: string, content: string) => {
try {
await updateFileInTable(dbTable, filePath, content);
win?.webContents.send("vector-database-update");
fs.writeFileSync(filePath, content, "utf-8");

const windowInfo = getWindowInfoForContents(
activeWindows,
event.sender
);
if (!windowInfo) {
throw new Error("Window info not found.");
}

// Update file in table
await updateFileInTable(windowInfo.dbTableClient, filePath, content);

// Respond directly to the sender
event.sender.send("vector-database-update");
} catch (error) {
console.error("Error updating file in table:", error);

// Optionally, send an error response back to the sender
event.sender.send("vector-database-error", errorToString(error));
}
}
);

ipcMain.handle(
"create-file",
async (event, filePath: string, content: string): Promise<void> => {
console.log("Creating file", filePath);
if (!fs.existsSync(filePath)) {
writeFileSyncRecursive(filePath, content, "utf-8");
} else {
console.log("File already exists:", filePath);
}
writeFileSyncRecursive(filePath, content, "utf-8");
}
);

Expand All @@ -78,26 +91,26 @@ export const registerFileHandlers = (
}
};

try {
if (!fs.existsSync(dirPath)) {
mkdirRecursiveSync(dirPath);
} else {
console.log("Directory already exists:", dirPath);
}
} catch (error) {
console.error("Error creating directory:", dirPath, error);
if (!fs.existsSync(dirPath)) {
mkdirRecursiveSync(dirPath);
} else {
console.log("Directory already exists:", dirPath);
}
}
);

ipcMain.handle(
"move-file-or-dir",
async (event, sourcePath: string, destinationPath: string) => {
try {
orchestrateEntryMove(dbTable, sourcePath, destinationPath);
} catch (error) {
console.error("Error moving file or directory:", error);
const windowInfo = getWindowInfoForContents(activeWindows, event.sender);
if (!windowInfo) {
throw new Error("Window info not found.");
}
orchestrateEntryMove(
windowInfo.dbTableClient,
sourcePath,
destinationPath
);
}
);
};
6 changes: 4 additions & 2 deletions electron/main/Store/storeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export interface RAGConfig {

export interface StoreSchema {
user: {
directory?: string;
vaultDirectories: string[];
directoryFromPreviousSession?: string;
};
aiModels: {
[modelName: string]: AIModelConfig;
Expand All @@ -34,7 +35,8 @@ export interface StoreSchema {
}

export enum StoreKeys {
UserDirectory = "user.directory",
VaultDirectories = "user.vaultDirectories",
DirectoryFromPreviousSession = "user.directoryFromPreviousSession",
AIModels = "aiModels",
DefaultAIModel = "defaultAIModel",
DefaultEmbedFuncRepo = "defaultEmbedFuncRepo",
Expand Down
25 changes: 17 additions & 8 deletions electron/main/Store/storeHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import { ipcMain } from "electron";
import { AIModelConfig, StoreKeys, StoreSchema } from "../Store/storeConfig";
import Store from "electron-store";
import { validateAIModelConfig } from "../llm/llmConfig";
import { FSWatcher } from "fs";
import {
setupDirectoryFromPreviousSessionIfUnused,
getVaultDirectoryForContents,
setVaultDirectoryForContents,
activeWindows,
} from "../windowManager";

export const registerStoreHandlers = (
store: Store<StoreSchema>,
fileWatcher: FSWatcher | null
store: Store<StoreSchema>
// fileWatcher: FSWatcher | null
) => {
setupDefaultStoreValues(store);
ipcMain.on(
"set-user-directory",
async (event, userDirectory: string): Promise<void> => {
console.log("setting user directory", userDirectory);
store.set(StoreKeys.UserDirectory, userDirectory);
if (fileWatcher) {
fileWatcher.close();
}
setVaultDirectoryForContents(activeWindows, event.sender, userDirectory);

event.returnValue = "success";
}
Expand Down Expand Up @@ -69,7 +71,14 @@ export const registerStoreHandlers = (
);

ipcMain.on("get-user-directory", (event) => {
const path = store.get(StoreKeys.UserDirectory);
let path = getVaultDirectoryForContents(activeWindows, event.sender);
if (!path) {
path = setupDirectoryFromPreviousSessionIfUnused(
activeWindows,
event.sender,
store
);
}
event.returnValue = path;
});

Expand Down
30 changes: 26 additions & 4 deletions electron/main/database/dbSessionHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ipcMain } from "electron";
import { LanceDBTableWrapper } from "./LanceTableWrapper";
import { createRAGPrompt } from "../Prompts/Prompts";
import { DBEntry, DatabaseFields } from "./Schema";
import { LLMSessions } from "../llm/llmSessionHandlers";
import { StoreKeys, StoreSchema } from "../Store/storeConfig";
import Store from "electron-store";
import { getWindowInfoForContents, activeWindows } from "../windowManager";

export const registerDBSessionHandlers = (
dbTable: LanceDBTableWrapper,
// dbTable: LanceDBTableWrapper,
store: Store<StoreSchema>
) => {
ipcMain.handle(
Expand All @@ -19,7 +19,18 @@ export const registerDBSessionHandlers = (
filter?: string
): Promise<DBEntry[]> => {
try {
const searchResults = await dbTable.search(query, limit, filter);
const windowInfo = getWindowInfoForContents(
activeWindows,
event.sender
);
if (!windowInfo) {
throw new Error("Window info not found.");
}
const searchResults = await windowInfo.dbTableClient.search(
query,
limit,
filter
);
return searchResults;
} catch (error) {
console.error("Error searching database:", error);
Expand All @@ -39,9 +50,20 @@ export const registerDBSessionHandlers = (
try {
let searchResults: DBEntry[] = [];
const maxRAGExamples: number = store.get(StoreKeys.MaxRAGExamples);
const windowInfo = getWindowInfoForContents(
activeWindows,
event.sender
);
if (!windowInfo) {
throw new Error("Window info not found.");
}

if (maxRAGExamples && maxRAGExamples > 0) {
searchResults = await dbTable.search(query, maxRAGExamples, filter);
searchResults = await windowInfo.dbTableClient.search(
query,
maxRAGExamples,
filter
);
} else {
throw new Error("Max RAG examples is not set or is invalid.");
}
Expand Down
Loading

0 comments on commit 5d94126

Please sign in to comment.