-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
9,512 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// } | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Oops, something went wrong.