Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local engine - symlink bundled variants #4342

Open
wants to merge 5 commits into
base: feat/local-engine-management
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions extensions/engine-management-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,43 @@
"version": "1.0.0",
"description": "This extension enables manage engines",
"main": "dist/index.js",
"node": "dist/node/index.cjs.js",
"author": "Jan <[email protected]>",
"license": "MIT",
"scripts": {
"test": "jest",
"build": "tsc -b . && webpack --config webpack.config.js",
"build:publish": "rimraf *.tgz --glob && yarn build && npm pack && cpx *.tgz ../../pre-install"
"build": "rolldown -c rolldown.config.mjs",
"build:publish": "rimraf *.tgz --glob && yarn build && ../../.github/scripts/auto-sign.sh && npm pack && cpx *.tgz ../../pre-install"
},
"exports": {
".": "./dist/index.js",
"./main": "./dist/module.js"
},
"devDependencies": {
"@rollup/plugin-replace": "^6.0.2",
"cpx": "^1.5.0",
"rimraf": "^3.0.2",
"rolldown": "^1.0.0-beta.1",
"ts-loader": "^9.5.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@janhq/core": "file:../../core",
"cpu-instructions": "^0.0.13",
"ky": "^1.7.2",
"p-queue": "^8.0.1"
},
"bundledDependencies": [
"cpu-instructions",
"@janhq/core"
],
"engines": {
"node": ">=18.0.0"
},
"files": [
"dist/*",
"package.json",
"README.md"
],
"bundleDependencies": []
]
}
45 changes: 45 additions & 0 deletions extensions/engine-management-extension/rolldown.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from 'rolldown'
import replace from '@rollup/plugin-replace'
import pkgJson from './package.json' with { type: 'json' }

export default defineConfig([
{
input: 'src/index.ts',
output: {
format: 'esm',
file: 'dist/index.js',
},
plugins: [
replace({
NODE: JSON.stringify(`${pkgJson.name}/${pkgJson.node}`),
API_URL: JSON.stringify('http://127.0.0.1:39291'),
SOCKET_URL: JSON.stringify('ws://127.0.0.1:39291'),
CORTEX_ENGINE_VERSION: JSON.stringify('v0.1.42'),
}),
],
},
{
input: 'src/node/index.ts',
external: ['@janhq/core/node'],
output: {
format: 'cjs',
file: 'dist/node/index.cjs.js',
},
plugins: [
replace({
CORTEX_ENGINE_VERSION: JSON.stringify('v0.1.42'),
}),
],
},
{
input: 'src/node/cpuInfo.ts',
output: {
format: 'cjs',
file: 'dist/node/cpuInfo.js',
},
external: ['cpu-instructions'],
resolve: {
extensions: ['.ts', '.js', '.svg'],
},
},
])
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare global {
declare const API_URL: string
declare const CORTEX_ENGINE_VERSION: string
declare const SOCKET_URL: string
declare const NODE: string

interface Core {
api: APIFunctions
Expand Down
10 changes: 10 additions & 0 deletions extensions/engine-management-extension/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Custom Engine Error
*/
export class EngineError extends Error {
message: string
constructor(message: string) {
super()
this.message = message
}
}
43 changes: 39 additions & 4 deletions extensions/engine-management-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
Engines,
EngineVariant,
EngineReleased,
executeOnMain,
systemInformation,
} from '@janhq/core'
import ky, { HTTPError } from 'ky'
import PQueue from 'p-queue'
import { EngineError } from './error'

/**
* JSONEngineManagementExtension is a EngineManagementExtension implementation that provides
Expand All @@ -20,13 +23,34 @@ export default class JSONEngineManagementExtension extends EngineManagementExten
* Called when the extension is loaded.
*/
async onLoad() {
// Symlink Engines Directory
await executeOnMain(NODE, 'symlinkEngines')
// Run Healthcheck
this.queue.add(() => this.healthz())
try {
await this.getDefaultEngineVariant(InferenceEngine.cortex_llamacpp)
const variant = await this.getDefaultEngineVariant(
InferenceEngine.cortex_llamacpp
)
// Check whether should use bundled version or installed version
// Only use larger version
if (this.compareVersions(CORTEX_ENGINE_VERSION, variant.version) > 0) {
throw new EngineError(
'Default engine version is smaller than bundled version'
)
}
} catch (error) {
if (error instanceof HTTPError && error.response.status === 400) {
if (
(error instanceof HTTPError && error.response.status === 400) ||
error instanceof EngineError
) {
const systemInfo = await systemInformation()
const variant = await executeOnMain(
NODE,
'engineVariant',
systemInfo.gpuSetting
)
await this.setDefaultEngineVariant(InferenceEngine.cortex_llamacpp, {
variant: 'mac-arm64',
variant: variant,
version: `${CORTEX_ENGINE_VERSION}`,
})
} else {
Expand Down Expand Up @@ -174,11 +198,22 @@ export default class JSONEngineManagementExtension extends EngineManagementExten
* Do health check on cortex.cpp
* @returns
*/
healthz(): Promise<void> {
async healthz(): Promise<void> {
return ky
.get(`${API_URL}/healthz`, {
retry: { limit: 20, delay: () => 500, methods: ['get'] },
})
.then(() => {})
}

private compareVersions(version1: string, version2: string): number {
const parseVersion = (version: string) => version.split('.').map(Number)

const [major1, minor1, patch1] = parseVersion(version1.replace(/^v/, ''))
const [major2, minor2, patch2] = parseVersion(version2.replace(/^v/, ''))

if (major1 !== major2) return major1 - major2
if (minor1 !== minor2) return minor1 - minor2
return patch1 - patch2
}
}
Loading
Loading