feat(plugins): Add nitro plugin

This commit is contained in:
vuonghoainam 2023-09-29 08:46:40 +07:00 committed by Louis
parent 89288bb4ca
commit 9b14b743d1
7 changed files with 9512 additions and 5 deletions

6
.gitignore vendored
View File

@ -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

View File

@ -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);
}

View File

@ -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();
// }
// };

View File

@ -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"
]
}

View File

@ -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
};

1612
node_modules/.yarn-integrity generated vendored

File diff suppressed because it is too large Load Diff

7726
yarn.lock

File diff suppressed because it is too large Load Diff