Skip to content

Commit

Permalink
feat: embed cortex into jan as a js module (#3305)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Aug 8, 2024
1 parent 709204b commit b348110
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/jan-electron-linter-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ jobs:
# 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}}"

- name: Setup node-gyp - distutils
run: pip3 install --upgrade setuptools

- name: Linter and test
shell: powershell
run: |
Expand Down
28 changes: 0 additions & 28 deletions electron/cortex-runner.ts

This file was deleted.

53 changes: 6 additions & 47 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { app, BrowserWindow } from 'electron'

import { join, resolve } from 'path'
import { exec, execSync, ChildProcess } from 'child_process'
import { cortexPath } from './cortex-runner'
import { execSync } from 'child_process'

/**
* Managers
Expand All @@ -27,6 +26,8 @@ import { setupReactDevTool } from './utils/dev'

import log from 'electron-log'

import { start } from 'cortexso'

const preloadPath = join(__dirname, 'preload.js')
const rendererPath = join(__dirname, '..', 'renderer')
const mainPath = join(rendererPath, 'index.html')
Expand Down Expand Up @@ -56,8 +57,6 @@ log.info('Log from the main process')
// replace all console.log to log
Object.assign(console, log.functions)

let cortexService: ChildProcess | undefined = undefined

const cortexJsPort = 1338
const cortexCppPort = 3940
const host = '127.0.0.1'
Expand Down Expand Up @@ -91,35 +90,9 @@ app
.then(() => {
const appConfiguration = getAppConfigurations()
const janDataFolder = appConfiguration.data_folder

const cortexParams: Record<string, string> = {
'-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}`)
})
})

start('jan', host, cortexJsPort, cortexCppPort, janDataFolder)
})
.then(createUserSpace)
.then(migrate)
.then(setupMenu)
Expand Down Expand Up @@ -152,23 +125,9 @@ app.once('quit', async () => {

app.once('window-all-closed', async () => {
await stopApiServer()
await stopCortexService()
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() {
// this function is not meant to be success. It will throw an error.
Expand Down
9 changes: 5 additions & 4 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@
"lint": "eslint . --ext \".js,.jsx,.ts,.tsx\"",
"test:e2e": "playwright test --workers=1",
"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 .",
"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:win32": "tsc -p . && electron-builder -p never -w --dir",
"build:test:linux": "tsc -p . && electron-builder -p never -l --dir",
"downloadcortex": "run-script-os",
"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: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:win32": "tsc -p . && electron-builder -p never -w",
"build:linux": "tsc -p . && electron-builder -p never -l deb -l AppImage",
Expand All @@ -121,7 +121,8 @@
"pacote": "^17.0.4",
"request": "^2.88.2",
"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": {
"@types/js-yaml": "4.0.9",
Expand Down

0 comments on commit b348110

Please sign in to comment.