Skip to content

Commit

Permalink
feat(plugins): Add nitro plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro-v authored and louis-jan committed Sep 30, 2023
1 parent 89288bb commit 9b14b74
Show file tree
Hide file tree
Showing 7 changed files with 9,512 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ dist
build
.DS_Store
electron/renderer


# Nitro
electron/core/plugins/nitro-plugin/nitro
electron/core/plugins/nitro-plugin/uploads
electron/core/plugins/nitro-plugin/*.log
25 changes: 25 additions & 0 deletions electron/core/plugins/nitro-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const MODULE_PATH = "nitro-plugin/dist/module.js";

const installModel = async (product) =>
new Promise(async (resolve) => {
if (window.electronAPI) {
window.electronAPI
.invokePluginFunc(MODULE_PATH, "installModel", product)
.then((res) => resolve(res));
}
});

const uninstallModel = async (product) =>
new Promise(async (resolve) => {
if (window.electronAPI) {
window.electronAPI
.invokePluginFunc(MODULE_PATH, "uninstallModel", product)
.then((res) => resolve(res));
}
});

// Register all the above functions and objects with the relevant extension points
export function init({ register }) {
register("installModel", "installModel", installModel);
register("uninstallModel", "uninstallModel", uninstallModel);
}
87 changes: 87 additions & 0 deletions electron/core/plugins/nitro-plugin/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { spawn } = require('child_process');
const fs = require('fs');

class NitroPlugin {
constructor() {
this.subprocess = null;
this.binaryFolder = `${__dirname}/nitro`; // Current directory by default
this.config = {};
}

/**
* Install a model by writing a JSON file and executing a binary.
* @param {string} modelPath - Path to the model.
*/
installModel(modelPath) {
// Check if there's an existing subprocess
if (this.subprocess) {
console.error('A subprocess is already running. Please uninstall the current model first.');
return;
}

// Read the existing config
const configFilePath = `${this.binaryFolder}/config/config.json`;
let config = {};
if (fs.existsSync(configFilePath)) {
const rawData = fs.readFileSync(configFilePath, 'utf-8');
config = JSON.parse(rawData);
}

// Update the llama_model_path
if (!config.custom_config) {
config.custom_config = {};
}
config.custom_config.llama_model_path = modelPath;

// Write the updated config back to the file
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 4));

// Execute the binary
this.subprocess = spawn(`${this.binaryFolder}/nitro`, [configFilePath]);

// Handle subprocess output
this.subprocess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});

this.subprocess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});

this.subprocess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
this.subprocess = null;
});
}

/**
* Uninstall the model by killing the subprocess.
*/
uninstallModel() {
if (this.subprocess) {
this.subprocess.kill();
this.subprocess = null;
console.log('Subprocess terminated.');
} else {
console.error('No subprocess is currently running.');
}
}
}


const test = async () => {
const nitro = new NitroPlugin();
nitro.installModel('/Users/nam/Documents/janai/code/jan/models/llama-2-7b.Q4_K_S.gguf');
// nitro.uninstallModel();
}
test()
// Export the functions
// module.exports = {
// NitroPlugin,
// installModel: (modelPath) => {
// nitro.installModel(modelPath);
// },
// uninstallModel: () => {
// nitro.uninstallModel();
// }
// };
36 changes: 36 additions & 0 deletions electron/core/plugins/nitro-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "nitro-plugin",
"version": "0.0.1",
"description": "",
"main": "dist/index.js",
"author": "Hiro",
"license": "MIT",
"activationPoints": [
"init"
],
"scripts": {
"build": "webpack --config webpack.config.js",
"build:package": "rimraf ./*.tgz && npm run build && cpx \"module.js\" \"dist\" && npm pack",
"build:publish": "yarn build:package && cpx *.tgz ../../pre-install"
},
"devDependencies": {
"cpx": "^1.5.0",
"rimraf": "^3.0.2",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"bundledDependencies": [
"electron-is-dev"
],
"dependencies": {
"electron-is-dev": "^2.0.0"
},
"engines": {
"node": ">=18.0.0"
},
"files": [
"dist/*",
"package.json",
"README.md"
]
}
25 changes: 25 additions & 0 deletions electron/core/plugins/nitro-plugin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require("path");

module.exports = {
experiments: { outputModule: true },
entry: "./index.js", // Adjust the entry point to match your project's main file
mode: "production",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
],
},
output: {
filename: "index.js", // Adjust the output file name as needed
path: path.resolve(__dirname, "dist"),
library: { type: "module" }, // Specify ESM output format
},
resolve: {
extensions: [".js"],
},
// Add loaders and other configuration as needed for your project
};
Loading

0 comments on commit 9b14b74

Please sign in to comment.