Skip to content

Commit

Permalink
Merge pull request #4342 from janhq/chore/local-engine-select-default…
Browse files Browse the repository at this point in the history
…-engine

feat: local engine - symlink bundled variants
  • Loading branch information
louis-jan authored Dec 29, 2024
2 parents a1fc178 + 2be92c7 commit e45e0eb
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 290 deletions.
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

0 comments on commit e45e0eb

Please sign in to comment.