Louis 9d64e9bef4
chore: merge release/v0.5.16 into dev (#4833)
* chore: bump cortex 1.0.11-rc10

* chore: bump to latest cortex release

* feat: Cortex API Authorization

* chore: correct CI CD repo name

* chore: correct new menloresearch repo name

* feat: rotate api token for each run (#4820)

* feat: rotate api token for each run

* chore: correct github repo url

* chore: correct github api url

* chore: should not filter out models first launch

* chore: bump cortex release

* chore: should get hardware information on launch (#4821)

* chore: should have an option to not revalidate hardware information

* chore: cortex.cpp gpu activation could cause a race condition (#4825)

* fix: jan beta logo displayed in jan release (#4828)

---------

Co-authored-by: David <davidpt.janai@gmail.com>
Co-authored-by: Nguyen Ngoc Minh <91668012+Minh141120@users.noreply.github.com>
2025-03-24 11:53:30 +07:00

104 lines
2.6 KiB
TypeScript

import path from 'path'
import { appResourcePath, getJanDataFolderPath, log } from '@janhq/core/node'
import { ProcessWatchdog } from './watchdog'
let watchdog: ProcessWatchdog | undefined = undefined
/**
* Spawns a Nitro subprocess.
* @returns A promise that resolves when the Nitro subprocess is started.
*/
function run(): Promise<any> {
log(`[CORTEX]:: Spawning cortex subprocess...`)
return new Promise<void>(async (resolve, reject) => {
// let gpuVisibleDevices = systemInfo?.gpuSetting?.gpus_in_use.join(',') ?? ''
let binaryName = `cortex-server${
process.platform === 'win32' ? '.exe' : ''
}`
const binPath = path.join(__dirname, '..', 'bin')
const executablePath = path.join(binPath, binaryName)
addEnvPaths(binPath)
const sharedPath = path.join(appResourcePath(), 'shared')
// Execute the binary
log(`[CORTEX]:: Spawn cortex at path: ${executablePath}`)
const dataFolderPath = getJanDataFolderPath()
if (watchdog) {
watchdog.terminate()
}
// The HOST address to use for the cortex subprocess
const LOCAL_PORT = CORTEX_API_URL.split(':').pop() ?? '39291'
watchdog = new ProcessWatchdog(
executablePath,
[
'--start-server',
'--port',
LOCAL_PORT.toString(),
'--config_file_path',
`${path.join(dataFolderPath, '.janrc')}`,
'--data_folder_path',
dataFolderPath,
'config',
'--api_keys',
process.env.appToken ?? 'cortex.cpp',
],
{
env: {
...process.env,
// CUDA_VISIBLE_DEVICES: gpuVisibleDevices,
// // Vulkan - Support 1 device at a time for now
// ...(gpuVisibleDevices?.length > 0 && {
// GGML_VK_VISIBLE_DEVICES: gpuVisibleDevices,
// }),
},
cwd: sharedPath,
}
)
watchdog.start()
resolve()
})
}
/**
* Every module should have a dispose function
* This will be called when the extension is unloaded and should clean up any resources
* Also called when app is closed
*/
function dispose() {
watchdog?.terminate()
}
/**
* Set the environment paths for the cortex subprocess
* @param dest
*/
function addEnvPaths(dest: string) {
// Add engine path to the PATH and LD_LIBRARY_PATH
if (process.platform === 'win32') {
process.env.PATH = (process.env.PATH || '').concat(path.delimiter, dest)
} else {
process.env.LD_LIBRARY_PATH = (process.env.LD_LIBRARY_PATH || '').concat(
path.delimiter,
dest
)
}
}
/**
* Cortex process info
*/
export interface CortexProcessInfo {
isRunning: boolean
}
export default {
run,
dispose,
}