feat: embed cortex into jan as a js module (#3305)

This commit is contained in:
Louis 2024-08-08 20:04:47 +07:00 committed by GitHub
parent 709204b2bc
commit b348110fb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 79 deletions

View File

@ -200,6 +200,9 @@ jobs:
# run: | # run: |
# make update-playwright-config REPORT_PORTAL_URL=${{ secrets.REPORT_PORTAL_URL }} REPORT_PORTAL_API_KEY=${{ secrets.REPORT_PORTAL_API_KEY }} REPORT_PORTAL_PROJECT_NAME=${{ secrets.REPORT_PORTAL_PROJECT_NAME }} REPORT_PORTAL_LAUNCH_NAME="Jan App Windows" REPORT_PORTAL_DESCRIPTION="${{env.REPORT_PORTAL_DESCRIPTION}}" # make update-playwright-config REPORT_PORTAL_URL=${{ secrets.REPORT_PORTAL_URL }} REPORT_PORTAL_API_KEY=${{ secrets.REPORT_PORTAL_API_KEY }} REPORT_PORTAL_PROJECT_NAME=${{ secrets.REPORT_PORTAL_PROJECT_NAME }} REPORT_PORTAL_LAUNCH_NAME="Jan App Windows" REPORT_PORTAL_DESCRIPTION="${{env.REPORT_PORTAL_DESCRIPTION}}"
- name: Setup node-gyp - distutils
run: pip3 install --upgrade setuptools
- name: Linter and test - name: Linter and test
shell: powershell shell: powershell
run: | run: |

View File

@ -1,28 +0,0 @@
import { app } from 'electron'
import { join as joinPath } from 'path'
import { platform } from 'os'
const getPlatform = (): string => {
switch (platform()) {
case 'darwin':
case 'sunos':
return 'mac'
case 'win32':
return 'win'
default:
return 'linux'
}
}
const resourceFolderName = getPlatform() === 'mac' ? 'Resources' : 'resources'
const execPath = app.isPackaged
? joinPath(app.getAppPath(), '..', '..', resourceFolderName, 'bin')
: joinPath(__dirname, '..', 'resources', getPlatform())
const cortexName = 'cortex'
const cortexBinaryName =
getPlatform() === 'win' ? `${cortexName}.exe` : cortexName
export const cortexPath = `${joinPath(execPath, cortexBinaryName)}`

View File

@ -1,8 +1,7 @@
import { app, BrowserWindow } from 'electron' import { app, BrowserWindow } from 'electron'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import { exec, execSync, ChildProcess } from 'child_process' import { execSync } from 'child_process'
import { cortexPath } from './cortex-runner'
/** /**
* Managers * Managers
@ -27,6 +26,8 @@ import { setupReactDevTool } from './utils/dev'
import log from 'electron-log' import log from 'electron-log'
import { start } from 'cortexso'
const preloadPath = join(__dirname, 'preload.js') const preloadPath = join(__dirname, 'preload.js')
const rendererPath = join(__dirname, '..', 'renderer') const rendererPath = join(__dirname, '..', 'renderer')
const mainPath = join(rendererPath, 'index.html') const mainPath = join(rendererPath, 'index.html')
@ -56,8 +57,6 @@ log.info('Log from the main process')
// replace all console.log to log // replace all console.log to log
Object.assign(console, log.functions) Object.assign(console, log.functions)
let cortexService: ChildProcess | undefined = undefined
const cortexJsPort = 1338 const cortexJsPort = 1338
const cortexCppPort = 3940 const cortexCppPort = 3940
const host = '127.0.0.1' const host = '127.0.0.1'
@ -92,33 +91,7 @@ app
const appConfiguration = getAppConfigurations() const appConfiguration = getAppConfigurations()
const janDataFolder = appConfiguration.data_folder const janDataFolder = appConfiguration.data_folder
const cortexParams: Record<string, string> = { start('jan', host, cortexJsPort, cortexCppPort, janDataFolder)
'-n': 'jan',
'-a': host,
'-p': cortexJsPort.toString(),
'-ep': cortexCppPort.toString(),
'--dataFolder': janDataFolder,
}
// add cortex parameters to the command
const command = Object.entries(cortexParams).reduce(
(acc, [key, value]) => `${acc} ${key} ${value}`,
`${cortexPath}`
)
log.info('Starting cortex with command:', command)
// init cortex
cortexService = exec(`${command}`, (error, stdout, stderr) => {
if (error) {
log.error(`error: ${error.message}`)
return
}
if (stderr) {
log.error(`stderr: ${stderr}`)
return
}
log.info(`stdout: ${stdout}`)
})
}) })
.then(createUserSpace) .then(createUserSpace)
.then(migrate) .then(migrate)
@ -152,23 +125,9 @@ app.once('quit', async () => {
app.once('window-all-closed', async () => { app.once('window-all-closed', async () => {
await stopApiServer() await stopApiServer()
await stopCortexService()
cleanUpAndQuit() cleanUpAndQuit()
}) })
async function stopCortexService() {
try {
const pid = cortexService?.pid
if (!pid) {
console.log('No cortex service to stop.')
return
}
process.kill(pid)
console.log(`Service with PID ${pid} has been terminated.`)
} catch (error) {
console.error('Error killing service:', error)
}
}
async function stopApiServer() { async function stopApiServer() {
// this function is not meant to be success. It will throw an error. // this function is not meant to be success. It will throw an error.

View File

@ -88,10 +88,10 @@
"lint": "eslint . --ext \".js,.jsx,.ts,.tsx\"", "lint": "eslint . --ext \".js,.jsx,.ts,.tsx\"",
"test:e2e": "playwright test --workers=1", "test:e2e": "playwright test --workers=1",
"copy:assets": "rimraf --glob \"./pre-install/*.tgz\" && cpx \"../pre-install/*.tgz\" \"./pre-install\"", "copy:assets": "rimraf --glob \"./pre-install/*.tgz\" && cpx \"../pre-install/*.tgz\" \"./pre-install\"",
"dev": "yarn copy:assets && yarn downloadcortex && tsc -p . && electron .", "dev": "yarn copy:assets && tsc -p . && electron .",
"compile": "tsc -p .", "compile": "tsc -p .",
"start": "electron .", "start": "electron .",
"build:test": "yarn copy:assets && yarn downloadcortex && run-script-os", "build:test": "yarn copy:assets && run-script-os",
"build:test:darwin": "tsc -p . && electron-builder -p never -m --dir", "build:test:darwin": "tsc -p . && electron-builder -p never -m --dir",
"build:test:win32": "tsc -p . && electron-builder -p never -w --dir", "build:test:win32": "tsc -p . && electron-builder -p never -w --dir",
"build:test:linux": "tsc -p . && electron-builder -p never -l --dir", "build:test:linux": "tsc -p . && electron-builder -p never -l --dir",
@ -99,7 +99,7 @@
"downloadcortex:linux": "CORTEX_VERSION=$(cat ./resources/version.txt) && echo https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz && download https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz -e -o ./resources/linux && rm -rf ./resources/linux/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz && chmod +x ./resources/linux/cortex", "downloadcortex:linux": "CORTEX_VERSION=$(cat ./resources/version.txt) && echo https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz && download https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz -e -o ./resources/linux && rm -rf ./resources/linux/cortex-${CORTEX_VERSION}-amd64-linux.tar.gz && chmod +x ./resources/linux/cortex",
"downloadcortex:darwin": "CORTEX_VERSION=$(cat ./resources/version.txt) && ARCH=$(node -e \"console.log(process.arch === 'arm64' ? 'arm64' : 'amd64')\") && echo https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz && download https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz -e -o ./resources/mac/ && rm -rf ./resources/mac/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz && chmod +x ./resources/mac/cortex", "downloadcortex:darwin": "CORTEX_VERSION=$(cat ./resources/version.txt) && ARCH=$(node -e \"console.log(process.arch === 'arm64' ? 'arm64' : 'amd64')\") && echo https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz && download https://github.com/janhq/cortex/releases/download/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz -e -o ./resources/mac/ && rm -rf ./resources/mac/cortex-${CORTEX_VERSION}-${ARCH}-mac.tar.gz && chmod +x ./resources/mac/cortex",
"downloadcortex:win32": "download.bat", "downloadcortex:win32": "download.bat",
"build": "yarn copy:assets && yarn downloadcortex && run-script-os", "build": "yarn copy:assets && run-script-os",
"build:darwin": "tsc -p . && electron-builder -p never -m", "build:darwin": "tsc -p . && electron-builder -p never -m",
"build:win32": "tsc -p . && electron-builder -p never -w", "build:win32": "tsc -p . && electron-builder -p never -w",
"build:linux": "tsc -p . && electron-builder -p never -l deb -l AppImage", "build:linux": "tsc -p . && electron-builder -p never -l deb -l AppImage",
@ -121,7 +121,8 @@
"pacote": "^17.0.4", "pacote": "^17.0.4",
"request": "^2.88.2", "request": "^2.88.2",
"request-progress": "^3.0.0", "request-progress": "^3.0.0",
"@kirillvakalov/nut-tree__nut-js": "4.2.1-2" "@kirillvakalov/nut-tree__nut-js": "4.2.1-2",
"cortexso": "v0.5.0-40"
}, },
"devDependencies": { "devDependencies": {
"@types/js-yaml": "4.0.9", "@types/js-yaml": "4.0.9",