Skip to content

Commit

Permalink
255: Cloud native
Browse files Browse the repository at this point in the history
  • Loading branch information
John authored and John committed Oct 30, 2023
1 parent 8b10aa2 commit 6be6789
Show file tree
Hide file tree
Showing 24 changed files with 528 additions and 45 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
39 changes: 39 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM node:20-bullseye AS base

# 1. Install dependencies only when needed
FROM base AS deps
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN yarn install

# # 2. Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# This will do the trick, use the corresponding env file for each environment.
RUN yarn workspace server install
RUN yarn server:prod

# 3. Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production

# RUN addgroup -g 1001 -S nodejs;
COPY --from=builder /app/server/build ./

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/server/node_modules ./node_modules
COPY --from=builder /app/server/package.json ./package.json

EXPOSE 4000 3928

ENV PORT 4000
ENV APPDATA /app/data

CMD ["node", "main.js"]
5 changes: 3 additions & 2 deletions electron/core/plugin-manager/execution/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Plugin from "./Plugin";
import { register } from "./activation-manager";
import plugins from "../../../../web/public/plugins/plugin.json"

/**
* @typedef {Object.<string, any>} installOptions The {@link https://www.npmjs.com/package/pacote|pacote options}
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function getActive() {
return;
}
// eslint-disable-next-line no-undef
const plgList = await window.pluggableElectronIpc.getActive();
const plgList = await window.pluggableElectronIpc?.getActive() ?? plugins;
return plgList.map(
(plugin) =>
new Plugin(
Expand All @@ -90,7 +91,7 @@ export async function registerActive() {
return;
}
// eslint-disable-next-line no-undef
const plgList = await window.pluggableElectronIpc.getActive();
const plgList = await getActive()
plgList.forEach((plugin) =>
register(
new Plugin(
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"workspaces": {
"packages": [
"electron",
"web"
"web",
"server"
],
"nohoist": [
"electron",
"electron/**",
"web",
"web/**"
"web/**",
"server",
"server/**"
]
},
"scripts": {
Expand All @@ -32,7 +35,10 @@
"build:publish": "yarn build:web && yarn workspace jan build:publish",
"build:publish-darwin": "yarn build:web && yarn workspace jan build:publish-darwin",
"build:publish-win32": "yarn build:web && yarn workspace jan build:publish-win32",
"build:publish-linux": "yarn build:web && yarn workspace jan build:publish-linux"
"build:publish-linux": "yarn build:web && yarn workspace jan build:publish-linux",
"build:web-plugins": "yarn build:web && yarn build:plugins && mkdir -p \"./web/out/plugins/data-plugin\" && cp \"./plugins/data-plugin/dist/esm/index.js\" \"./web/out/plugins/data-plugin\" && mkdir -p \"./web/out/plugins/inference-plugin\" && cp \"./plugins/inference-plugin/dist/index.js\" \"./web/out/plugins/inference-plugin\" && mkdir -p \"./web/out/plugins/model-management-plugin\" && cp \"./plugins/model-management-plugin/dist/index.js\" \"./web/out/plugins/model-management-plugin\" && mkdir -p \"./web/out/plugins/monitoring-plugin\" && cp \"./plugins/monitoring-plugin/dist/index.js\" \"./web/out/plugins/monitoring-plugin\"",
"server:prod": "yarn workspace server build && yarn build:web-plugins && cpx \"web/out/**\" \"server/build/renderer/\" && mkdir -p ./server/build/@janhq && cp -r ./plugins/* ./server/build/@janhq",
"start:server": "yarn server:prod && node server/build/main.js"
},
"devDependencies": {
"concurrently": "^8.2.1",
Expand Down
9 changes: 8 additions & 1 deletion plugins/data-plugin/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const dbs: Record<string, any> = {};
*/
function createCollection(name: string, schema?: { [key: string]: any }): Promise<void> {
return new Promise<void>((resolve) => {
const dbPath = path.join(app.getPath("userData"), "databases");
const dbPath = path.join(appPath(), "databases");
if (!fs.existsSync(dbPath)) fs.mkdirSync(dbPath);
const db = new PouchDB(`${path.join(dbPath, name)}`);
dbs[name] = db;
Expand Down Expand Up @@ -226,6 +226,13 @@ function findMany(
.then((data) => data.docs); // Return documents
}

function appPath() {
if (app) {
return app.getPath("userData");
}
return process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share");
}

module.exports = {
createCollection,
deleteCollection,
Expand Down
38 changes: 21 additions & 17 deletions plugins/inference-plugin/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,22 @@ const initModel = (fileName) => {
let binaryFolder = path.join(__dirname, "nitro"); // Current directory by default
let binaryName;

if (process.platform === "win32") {
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "nitro_start_windows.bat";
} else if (process.platform === "darwin") {
// Mac OS platform
binaryName =
process.arch === "arm64"
? "nitro_mac_arm64"
: "nitro_mac_intel";
} else {
// Linux
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "nitro_start_linux.sh"; // For other platforms
}
if (process.platform === "win32") {
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "nitro_start_windows.bat";
} else if (process.platform === "darwin") {
// Mac OS platform
binaryName = process.arch === "arm64" ? "nitro_mac_arm64" : "nitro_mac_intel";
} else {
// Linux
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "nitro_start_linux.sh"; // For other platforms
}

const binaryPath = path.join(binaryFolder, binaryName);

// Execute the binary
subprocess = spawn(binaryPath, { cwd: binaryFolder });
// Execute the binary
subprocess = spawn(binaryPath,["0.0.0.0", PORT], { cwd: binaryFolder });

// Handle subprocess output
subprocess.stdout.on("data", (data) => {
Expand All @@ -61,7 +58,7 @@ const initModel = (fileName) => {
})
.then(() => tcpPortUsed.waitUntilUsed(PORT, 300, 30000))
.then(() => {
const llama_model_path = path.join(app.getPath("userData"), fileName);
const llama_model_path = path.join(appPath(), fileName);

const config = {
llama_model_path,
Expand Down Expand Up @@ -107,6 +104,13 @@ function killSubprocess() {
}
}

function appPath() {
if (app) {
return app.getPath("userData");
}
return process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share");
}

module.exports = {
initModel,
killSubprocess,
Expand Down
2 changes: 1 addition & 1 deletion plugins/inference-plugin/nitro/nitro_start_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#!/bin/bash

# Attempt to run the nitro_linux_amd64_cuda file and if it fails, run nitro_linux_amd64
./nitro_linux_amd64_cuda || (echo "nitro_linux_amd64_cuda encountered an error, attempting to run nitro_linux_amd64..." && ./nitro_linux_amd64)
./nitro_linux_amd64_cuda "$@" || (echo "nitro_linux_amd64_cuda encountered an error, attempting to run nitro_linux_amd64..." && ./nitro_linux_amd64 "$@")
48 changes: 47 additions & 1 deletion plugins/model-management-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,56 @@ import {
downloadFile,
deleteFile,
store,
EventName,
events
} from "@janhq/core";
import { parseToModel } from "./helper";

const downloadModel = (product) =>
const downloadModel = (product) => {
downloadFile(product.downloadUrl, product.fileName);
checkDownloadProgress(product.fileName);
}

async function checkDownloadProgress(fileName: string) {
if (typeof window !== "undefined" && typeof (window as any).electronAPI === "undefined") {
const intervalId = setInterval(() => {
fetchDownloadProgress(fileName, intervalId);
}, 3000);
}
}

async function fetchDownloadProgress(fileName: string, intervalId: NodeJS.Timeout): Promise<string> {
const response = await fetch("/api/v1/downloadProgress", {
method: 'POST',
body: JSON.stringify({ fileName: fileName }),
headers: { 'Content-Type': 'application/json', 'Authorization': '' }
});

if (!response.ok) {
events.emit(EventName.OnDownloadError, null);
clearInterval(intervalId);
return;
}
const json = await response.json();
if (isEmptyObject(json)) {
if (!fileName && intervalId) {
clearInterval(intervalId);
}
return Promise.resolve("");
}
if (json.success === true) {
events.emit(EventName.OnDownloadSuccess, json);
clearInterval(intervalId);
return Promise.resolve("");
} else {
events.emit(EventName.OnDownloadUpdate, json);
return Promise.resolve(json.fileName);
}
}

function isEmptyObject(ojb: any): boolean {
return Object.keys(ojb).length === 0;
}

const deleteModel = (path) => deleteFile(path);

Expand Down Expand Up @@ -87,6 +132,7 @@ function getModelById(modelId: string): Promise<any> {

function onStart() {
store.createCollection("models", {});
fetchDownloadProgress(null, null).then((fileName: string) => fileName && checkDownloadProgress(fileName));
}

// Register all the above functions and objects with the relevant extension points
Expand Down
Loading

0 comments on commit 6be6789

Please sign in to comment.