Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit c0e4c91

Browse files
louis-janirfanpena
authored andcommitted
refactor: shorten cortex setup - save cortex cpp serve configs (#747)
1 parent 1971c19 commit c0e4c91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+379
-271
lines changed

cortex-js/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
},
1111
"scripts": {
1212
"dev": "nest dev",
13-
"build": "yarn build:extensions && nest build && cpx \"cpuinfo/bin/**\" dist/bin",
14-
"build:binary": "run-script-os",
15-
"build:binary:windows": "bun build --compile --target=bun-windows-x64 ./dist/src/command.js --outfile cortex.exe --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
16-
"build:binary:linux": "bun build --compile --target=bun-linux-x64 ./dist/src/command.js --outfile cortex --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
17-
"build:binary:macos": "bun build --compile --target=bun-darwin-arm64 ./dist/src/command.js --outfile cortex --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
13+
"build": "nest build && cpx \"cpuinfo/bin/**\" dist/bin",
14+
"build:binary": "run-script-os && cpx \"cpuinfo/bin/**\" dist/bin",
15+
"build:binary:windows": "bun build --compile --target=bun-windows-x64 ./src/command.ts --outfile cortex.exe --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
16+
"build:binary:linux": "bun build --compile --target=bun-linux-x64 ./src/command.ts --outfile cortex --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
17+
"build:binary:macos": "bun build --compile --target=bun-darwin-arm64 ./src/command.ts --outfile cortex --external @nestjs/microservices --external @nestjs/websockets/socket-module --external class-transformer/storage",
1818
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
1919
"build:extensions": "run-script-os",
2020
"build:extensions:windows": "powershell -command \"$jobs = Get-ChildItem -Path './src/extensions' -Directory | ForEach-Object { Start-Job -Name ($_.Name) -ScriptBlock { param($_dir); try { Set-Location $_dir; yarn; yarn build; Write-Output 'Build successful in ' + $_dir } catch { Write-Error 'Error in ' + $_dir; throw } } -ArgumentList $_.FullName }; $jobs | Wait-Job; $jobs | ForEach-Object { Receive-Job -Job $_ -Keep } | ForEach-Object { Write-Host $_ }; $failed = $jobs | Where-Object { $_.State -ne 'Completed' -or $_.ChildJobs[0].JobStateInfo.State -ne 'Completed' }; if ($failed) { Exit 1 }\"",

cortex-js/src/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { AppLoggerMiddleware } from './infrastructure/middlewares/app.logger.mid
1515
import { TelemetryModule } from './usecases/telemetry/telemetry.module';
1616
import { APP_FILTER } from '@nestjs/core';
1717
import { GlobalExceptionFilter } from './infrastructure/exception/global.exception';
18-
import { UtilModule } from './util/util.module';
1918
import { EventEmitterModule } from '@nestjs/event-emitter';
2019
import { EventsController } from './infrastructure/controllers/events.controller';
2120
import { AssistantsController } from './infrastructure/controllers/assistants.controller';
@@ -26,6 +25,7 @@ import { ThreadsController } from './infrastructure/controllers/threads.controll
2625
import { StatusController } from './infrastructure/controllers/status.controller';
2726
import { ProcessController } from './infrastructure/controllers/process.controller';
2827
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
28+
import { ContextModule } from './infrastructure/services/context/context.module';
2929

3030
@Module({
3131
imports: [
@@ -48,7 +48,7 @@ import { DownloadManagerModule } from './infrastructure/services/download-manage
4848
ExtensionModule,
4949
FileManagerModule,
5050
TelemetryModule,
51-
UtilModule,
51+
ContextModule,
5252
DownloadManagerModule,
5353
],
5454
controllers: [

cortex-js/src/command.module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { ModelStopCommand } from './infrastructure/commanders/models/model-stop.
1818
import { ModelGetCommand } from './infrastructure/commanders/models/model-get.command';
1919
import { ModelRemoveCommand } from './infrastructure/commanders/models/model-remove.command';
2020
import { RunCommand } from './infrastructure/commanders/shortcuts/run.command';
21-
import { InitCudaQuestions } from './infrastructure/commanders/questions/cuda.questions';
2221
import { ModelUpdateCommand } from './infrastructure/commanders/models/model-update.command';
2322
import { AssistantsModule } from './usecases/assistants/assistants.module';
2423
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
@@ -29,12 +28,12 @@ import { KillCommand } from './infrastructure/commanders/kill.command';
2928
import { PresetCommand } from './infrastructure/commanders/presets.command';
3029
import { TelemetryModule } from './usecases/telemetry/telemetry.module';
3130
import { TelemetryCommand } from './infrastructure/commanders/telemetry.command';
32-
import { UtilModule } from './util/util.module';
3331
import { EmbeddingCommand } from './infrastructure/commanders/embeddings.command';
3432
import { BenchmarkCommand } from './infrastructure/commanders/benchmark.command';
3533
import { EventEmitterModule } from '@nestjs/event-emitter';
3634
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
3735
import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command';
36+
import { ContextModule } from './infrastructure/services/context/context.module';
3837

3938
@Module({
4039
imports: [
@@ -54,7 +53,7 @@ import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve
5453
MessagesModule,
5554
FileManagerModule,
5655
TelemetryModule,
57-
UtilModule,
56+
ContextModule,
5857
DownloadManagerModule,
5958
],
6059
providers: [
@@ -71,7 +70,6 @@ import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve
7170

7271
// Questions
7372
InitRunModeQuestions,
74-
InitCudaQuestions,
7573

7674
// Model commands
7775
ModelStartCommand,

cortex-js/src/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CommandModule } from './command.module';
44
import { TelemetryUsecases } from './usecases/telemetry/telemetry.usecases';
55
import { TelemetrySource } from './domain/telemetry/telemetry.interface';
66
import { AsyncLocalStorage } from 'async_hooks';
7-
import { ContextService } from './util/context.service';
7+
import { ContextService } from '@/infrastructure/services/context/context.service';
88

99
export const asyncLocalStorage = new AsyncLocalStorage();
1010

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export interface Config {
22
dataFolderPath: string;
3+
initialized: boolean;
4+
cortexCppHost: string;
5+
cortexCppPort: number;
36
}

cortex-js/src/infrastructure/commanders/chat.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { exit } from 'node:process';
99
import { PSCliUsecases } from './usecases/ps.cli.usecases';
1010
import { ModelsUsecases } from '@/usecases/models/models.usecases';
1111
import { SetCommandContext } from './decorators/CommandContext';
12-
import { ContextService } from '@/util/context.service';
1312
import { ModelStat } from './types/model-stat.interface';
13+
import { ContextService } from '../services/context/context.service';
1414

1515
type ChatOptions = {
1616
threadId?: string;

cortex-js/src/infrastructure/commanders/cortex-command.commander.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import pkg from '@/../package.json';
1111
import { PresetCommand } from './presets.command';
1212
import { TelemetryCommand } from './telemetry.command';
1313
import { SetCommandContext } from './decorators/CommandContext';
14-
import { ContextService } from '@/util/context.service';
1514
import { EmbeddingCommand } from './embeddings.command';
1615
import { BenchmarkCommand } from './benchmark.command';
1716
import chalk from 'chalk';
1817
import { printSlogan } from '@/utils/logo';
18+
import { ContextService } from '../services/context/context.service';
1919

2020
@RootCommand({
2121
subCommands: [

cortex-js/src/infrastructure/commanders/init.command.ts

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { InitCliUsecases } from './usecases/init.cli.usecases';
88
import { InitOptions } from './types/init-options.interface';
99
import { SetCommandContext } from './decorators/CommandContext';
10-
import { ContextService } from '@/util/context.service';
10+
import { ContextService } from '../services/context/context.service';
1111

1212
@SubCommand({
1313
name: 'init',
@@ -30,59 +30,20 @@ export class InitCommand extends CommandRunner {
3030

3131
async run(passedParams: string[], options?: InitOptions): Promise<void> {
3232
if (options?.silent) {
33-
return this.initSilently(passedParams);
33+
const installationOptions =
34+
await this.initUsecases.defaultInstallationOptions();
35+
return this.initUsecases.installEngine(installationOptions);
3436
} else {
35-
return this.initPrompts(passedParams, options);
36-
}
37-
}
37+
options = await this.inquirerService.ask(
38+
'init-run-mode-questions',
39+
options,
40+
);
3841

39-
private initSilently = async (
40-
passedParams: string[],
41-
options: InitOptions = {},
42-
) => {
43-
const version = passedParams[0] ?? 'latest';
44-
if (process.platform === 'darwin') {
45-
const engineFileName = this.initUsecases.parseEngineFileName(options);
46-
return this.initUsecases.installEngine(engineFileName, version);
47-
}
48-
// If Nvidia Driver is installed -> GPU
49-
options.runMode = (await this.initUsecases.checkNvidiaGPUExist())
50-
? 'GPU'
51-
: 'CPU';
52-
// CPU Instructions detection
53-
options.gpuType = 'Nvidia';
54-
options.instructions = await this.initUsecases.detectInstructions();
55-
const engineFileName = this.initUsecases.parseEngineFileName(options);
56-
return this.initUsecases
57-
.installEngine(engineFileName, version)
58-
.then(() => this.initUsecases.installCudaToolkitDependency(options));
59-
};
42+
const version = passedParams[0] ?? 'latest';
6043

61-
/**
62-
* Manual initalization
63-
* To setup cortex's dependencies
64-
* @param input
65-
* @param options GPU | CPU / Nvidia | Others (Vulkan) / AVX | AVX2 | AVX512
66-
*/
67-
private initPrompts = async (input: string[], options?: InitOptions) => {
68-
options = await this.inquirerService.ask(
69-
'init-run-mode-questions',
70-
options,
71-
);
72-
73-
if (options.runMode === 'GPU' && !(await this.initUsecases.cudaVersion())) {
74-
options = await this.inquirerService.ask('init-cuda-questions', options);
44+
await this.initUsecases.installEngine(options, version);
7545
}
76-
77-
const version = input[0] ?? 'latest';
78-
79-
const engineFileName = this.initUsecases.parseEngineFileName(options);
80-
await this.initUsecases.installEngine(engineFileName, version);
81-
82-
if (options.installCuda === 'Yes') {
83-
await this.initUsecases.installCudaToolkitDependency(options);
84-
}
85-
};
46+
}
8647

8748
@Option({
8849
flags: '-s, --silent',

cortex-js/src/infrastructure/commanders/kill.command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommandRunner, SubCommand } from 'nest-commander';
22
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
33
import { SetCommandContext } from './decorators/CommandContext';
4-
import { ContextService } from '@/util/context.service';
4+
import { ContextService } from '../services/context/context.service';
55

66
@SubCommand({
77
name: 'kill',
@@ -16,6 +16,6 @@ export class KillCommand extends CommandRunner {
1616
super();
1717
}
1818
async run(): Promise<void> {
19-
this.usecases.stopCortex().then(console.log);
19+
return this.usecases.stopCortex().then(console.log);
2020
}
2121
}

cortex-js/src/infrastructure/commanders/models/model-get.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CommandRunner, SubCommand } from 'nest-commander';
22
import { ModelsCliUsecases } from '@commanders/usecases/models.cli.usecases';
33
import { exit } from 'node:process';
4-
import { ContextService } from '@/util/context.service';
54
import { SetCommandContext } from '../decorators/CommandContext';
5+
import { ContextService } from '@/infrastructure/services/context/context.service';
66

77
@SubCommand({
88
name: 'get',

0 commit comments

Comments
 (0)