Skip to content

Commit

Permalink
chore: refactor - invoke plugin function
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Sep 25, 2023
1 parent a296be1 commit 444b5d9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
14 changes: 7 additions & 7 deletions electron/core/data-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Provide an async method to manipulate the price provided by the extension point
const PLUGIN_NAME = "data-plugin";
const MODULE_PATH = "data-plugin/dist/module.js";

const getConversations = () =>
new Promise<any>((resolve) => {
if (window && window.electronAPI) {
window.electronAPI
.invokePluginFunc(PLUGIN_NAME, "getConversations")
.invokePluginFunc(MODULE_PATH, "getConversations")
.then((res: any[]) => resolve(res));
} else {
resolve([]);
Expand All @@ -15,7 +15,7 @@ const getConversationMessages = (id: any) =>
new Promise((resolve) => {
if (window && window.electronAPI) {
window.electronAPI
.invokePluginFunc(PLUGIN_NAME, "getConversationMessages", id)
.invokePluginFunc(MODULE_PATH, "getConversationMessages", id)
.then((res: any[]) => resolve(res));
} else {
resolve([]);
Expand All @@ -26,7 +26,7 @@ const createConversation = (conversation: any) =>
new Promise((resolve) => {
if (window && window.electronAPI) {
window.electronAPI
.invokePluginFunc(PLUGIN_NAME, "storeConversation", conversation)
.invokePluginFunc(MODULE_PATH, "storeConversation", conversation)
.then((res: any) => {
resolve(res);
});
Expand All @@ -38,7 +38,7 @@ const createMessage = (message: any) =>
new Promise((resolve) => {
if (window && window.electronAPI) {
window.electronAPI
.invokePluginFunc(PLUGIN_NAME, "storeMessage", message)
.invokePluginFunc("storeMessage", message)
.then((res: any) => resolve(res));
} else {
resolve("-");
Expand All @@ -49,7 +49,7 @@ const deleteConversation = (id: any) =>
new Promise((resolve) => {
if (window && window.electronAPI) {
window.electronAPI
.invokePluginFunc(PLUGIN_NAME, "deleteConversation", id)
.invokePluginFunc(MODULE_PATH, "deleteConversation", id)
.then((res: any) => {
resolve(res);
});
Expand All @@ -59,7 +59,7 @@ const deleteConversation = (id: any) =>
});

const setupDb = () => {
window.electronAPI.invokePluginFunc(PLUGIN_NAME, "init");
window.electronAPI.invokePluginFunc(MODULE_PATH, "init");
};

const getButton = (text: string, func: () => void) => {
Expand Down
Binary file modified electron/core/pre-install/data-plugin.tgz
Binary file not shown.
51 changes: 23 additions & 28 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { readdirSync, createWriteStream, unlink, lstatSync } from "fs";
import isDev = require("electron-is-dev");
import request = require("request");
import progress = require("request-progress");
import { init, getStore } from "./core/plugin-manager/pluginMgr";
import { init } from "./core/plugin-manager/pluginMgr";

let modelSession = undefined;
let modelName = "llama-2-7b-chat.gguf.q4_0.bin";
Expand Down Expand Up @@ -75,33 +75,25 @@ const createMainWindow = () => {
});
});

ipcMain.handle("invokePluginFunc", async (event, plugin, method, ...args) => {
const plg = getStore()
.getActivePlugins()
.filter((p) => p.name === plugin)[0];
const pluginPath = join(
app.getPath("userData"),
"plugins",
plg.name,
"dist/module.js"
);
return await import(
/* webpackIgnore: true */
pluginPath
)
.then((plugin) => {
if (typeof plugin[method] === "function") {
return plugin[method](...args);
} else {
console.log(plugin[method]);
console.error(`Function "${method}" does not exist in the module.`);
}
})
.then((res) => {
return res;
})
.catch((err) => console.log(err));
});
ipcMain.handle(
"invokePluginFunc",
async (event, modulePath, method, ...args) => {
const module = join(app.getPath("userData"), "plugins", modulePath);
return await import(/* webpackIgnore: true */ module)
.then((plugin) => {
if (typeof plugin[method] === "function") {
return plugin[method](...args);
} else {
console.log(plugin[method]);
console.error(`Function "${method}" does not exist in the module.`);
}
})
.then((res) => {
return res;
})
.catch((err) => console.log(err));
}
);

const startURL = isDev
? "http://localhost:3000"
Expand All @@ -124,6 +116,9 @@ app.whenReady().then(() => {
ipcMain.handle("userData", async (event) => {
return join(__dirname, "../");
});
ipcMain.handle("pluginPath", async (event) => {
return join(app.getPath("userData"), "plugins");
});

ipcMain.handle("downloadModel", async (event, url) => {
const userDataPath = app.getPath("userData");
Expand Down
1 change: 1 addition & 0 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.invoke("invokePluginFunc", plugin, method, ...args),

userData: () => ipcRenderer.invoke("userData"),
pluginPath: () => ipcRenderer.invoke("pluginPath"),

sendInquiry: (question: string) =>
ipcRenderer.invoke("sendInquiry", question),
Expand Down

0 comments on commit 444b5d9

Please sign in to comment.