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

Commit 7683f5f

Browse files
chore: update default api server config (#921)
Co-authored-by: marknguyen1302 <nguyenvu1302.work@gmail.com>
1 parent ffd8aaf commit 7683f5f

23 files changed

+98
-56
lines changed

cortex-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"class-validator": "^0.14.1",
5656
"cli-progress": "^3.12.0",
5757
"cortex-cpp": "0.4.34",
58-
"@cortexso/cortex.js": "^0.1.1",
58+
"@cortexso/cortex.js": "^0.1.2",
5959
"cpu-instructions": "^0.0.11",
6060
"decompress": "^4.2.1",
6161
"js-yaml": "^4.1.0",

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,11 @@ import { Injectable } from '@nestjs/common';
33
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
44
import Cortex from '@cortexso/cortex.js';
55
import ora from 'ora';
6-
import { FileManagerService } from '../services/file-manager/file-manager.service';
7-
import { cortexNamespace, cortexServerAPI } from '../constants/cortex';
86

97
@Injectable()
108
export abstract class BaseCommand extends CommandRunner {
11-
// Cortex client instance to communicate with cortex API server
12-
cortex: Cortex;
13-
serverConfigs: { host: string; port: number };
14-
159
constructor(readonly cortexUseCases: CortexUsecases) {
1610
super();
17-
// No need to inject services, since there is no nested dependencies
18-
const fileManagerService: FileManagerService = new FileManagerService();
19-
this.serverConfigs = fileManagerService.getServerConfig();
20-
21-
// Instantiate cortex client, it will be use throughout the command
22-
this.cortex = new Cortex({
23-
apiKey: cortexNamespace,
24-
baseURL: cortexServerAPI(
25-
this.serverConfigs.host,
26-
this.serverConfigs.port,
27-
),
28-
timeout: 20 * 1000,
29-
});
3011
}
3112
protected abstract runCommand(
3213
passedParam: string[],
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
3+
import Cortex from '@cortexso/cortex.js';
4+
import { FileManagerService } from '../services/file-manager/file-manager.service';
5+
import { cortexNamespace, cortexServerAPI } from '../constants/cortex';
6+
import { BaseCommand } from './base.command';
7+
8+
@Injectable()
9+
export abstract class BaseSubCommand extends BaseCommand {
10+
// Cortex client instance to communicate with cortex API server
11+
cortex: Cortex;
12+
serverConfigs: { host: string; port: number };
13+
14+
constructor(readonly cortexUseCases: CortexUsecases) {
15+
super(cortexUseCases);
16+
// No need to inject services, since there is no nested dependencies
17+
const fileManagerService: FileManagerService = new FileManagerService();
18+
this.serverConfigs = fileManagerService.getServerConfig();
19+
20+
// Instantiate cortex client, it will be use throughout the command
21+
this.cortex = new Cortex({
22+
apiKey: cortexNamespace,
23+
baseURL: cortexServerAPI(
24+
this.serverConfigs.host,
25+
this.serverConfigs.port,
26+
),
27+
timeout: 20 * 1000,
28+
});
29+
}
30+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface';
1616
import { defaultBenchmarkConfiguration } from '../constants/benchmark';
1717
import { inspect } from 'util';
1818
import { Cortex } from '@cortexso/cortex.js';
19+
import { BaseSubCommand } from './base.subcommand';
1920

2021
@SubCommand({
2122
name: 'benchmark',
@@ -26,7 +27,7 @@ import { Cortex } from '@cortexso/cortex.js';
2627
description:
2728
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
2829
})
29-
export class BenchmarkCommand extends BaseCommand {
30+
export class BenchmarkCommand extends BaseSubCommand {
3031
constructor(
3132
private readonly cortexUsecases: CortexUsecases,
3233
private readonly fileService: FileManagerService,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { isRemoteEngine } from '@/utils/normalize-model-id';
1717
import { Cortex } from '@cortexso/cortex.js';
1818
import { ChatClient } from './services/chat-client';
1919
import { downloadModelProgress } from '@/utils/pull-model';
20+
import { BaseSubCommand } from './base.subcommand';
2021

2122
type ChatOptions = {
2223
threadId?: string;
@@ -36,7 +37,7 @@ type ChatOptions = {
3637
},
3738
})
3839
@SetCommandContext()
39-
export class ChatCommand extends BaseCommand {
40+
export class ChatCommand extends BaseSubCommand {
4041
chatClient: ChatClient;
4142

4243
constructor(

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type ServeOptions = {
4949
})
5050
@SetCommandContext()
5151
export class CortexCommand extends CommandRunner {
52+
host: string;
53+
port: number;
54+
configHost: string;
55+
configPort: number;
5256
constructor(
5357
readonly contextService: ContextService,
5458
readonly fileManagerService: FileManagerService,
@@ -58,8 +62,17 @@ export class CortexCommand extends CommandRunner {
5862
}
5963

6064
async run(passedParams: string[], options?: ServeOptions): Promise<void> {
61-
const host = options?.address || defaultCortexJsHost;
62-
const port = options?.port || defaultCortexJsPort;
65+
const {
66+
apiServerHost: configApiServerHost,
67+
apiServerPort: configApiServerPort,
68+
} = await this.fileManagerService.getConfig();
69+
70+
this.configHost = configApiServerHost || defaultCortexJsHost;
71+
this.configPort = configApiServerPort || defaultCortexJsPort;
72+
73+
this.host = options?.address || configApiServerHost || defaultCortexJsHost;
74+
this.port = options?.port || configApiServerPort || defaultCortexJsPort;
75+
6376
const showLogs = options?.logs || false;
6477
const showVersion = options?.version || false;
6578
const dataFolderPath = options?.dataFolder;
@@ -70,12 +83,10 @@ export class CortexCommand extends CommandRunner {
7083
console.log(chalk.blue(`Github: ${pkg.homepage}`));
7184
return;
7285
}
73-
return this.startServer(host, port, showLogs, dataFolderPath);
86+
return this.startServer(showLogs, dataFolderPath);
7487
}
7588

7689
private async startServer(
77-
host: string,
78-
port: number,
7990
attach: boolean,
8091
dataFolderPath?: string,
8192
) {
@@ -89,13 +100,9 @@ export class CortexCommand extends CommandRunner {
89100
startEngineSpinner.succeed('Cortex started successfully');
90101
const isServerOnline = await this.cortexUseCases.isAPIServerOnline();
91102
if (isServerOnline) {
92-
const {
93-
apiServerHost: configApiServerHost,
94-
apiServerPort: configApiServerPort,
95-
} = await this.fileManagerService.getConfig();
96103
console.log(
97104
chalk.blue(
98-
`Server is already running at http://${configApiServerHost}:${configApiServerPort}. Please use 'cortex stop' to stop the server.`,
105+
`Server is already running at http://${this.configHost}:${this.configPort}. Please use 'cortex stop' to stop the server.`,
99106
),
100107
);
101108
process.exit(0);
@@ -110,18 +117,18 @@ export class CortexCommand extends CommandRunner {
110117
}
111118
if (attach) {
112119
const app = await getApp();
113-
await app.listen(port, host);
120+
await app.listen(this.port, this.host);
114121
} else {
115-
await this.cortexUseCases.startServerDetached(host, port);
122+
await this.cortexUseCases.startServerDetached(this.host, this.port);
116123
}
117-
console.log(chalk.blue(`Started server at http://${host}:${port}`));
124+
console.log(chalk.blue(`Started server at http://${this.host}:${this.port}`));
118125
console.log(
119-
chalk.blue(`API Playground available at http://${host}:${port}/api`),
126+
chalk.blue(`API Playground available at http://${this.host}:${this.port}/api`),
120127
);
121128
await this.fileManagerService.writeConfigFile({
122129
...config,
123-
apiServerHost: host,
124-
apiServerPort: port,
130+
apiServerHost: this.host,
131+
apiServerPort: this.port,
125132
dataFolderPath: dataFolderPath || config.dataFolderPath,
126133
});
127134
if (!attach) process.exit(0);
@@ -131,7 +138,7 @@ export class CortexCommand extends CommandRunner {
131138
await this.fileManagerService.writeConfigFile({
132139
...config,
133140
});
134-
console.error(`Failed to start server. Is port ${port} in use?`);
141+
console.error(`Failed to start server. Is port ${this.port} in use?`);
135142
process.exit(1);
136143
}
137144
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
44
import { BaseCommand } from './base.command';
55
import { Cortex } from '@cortexso/cortex.js';
66
import ora from 'ora';
7+
import { BaseSubCommand } from './base.subcommand';
78

89
interface EmbeddingCommandOptions {
910
encoding_format?: string;
@@ -20,7 +21,7 @@ interface EmbeddingCommandOptions {
2021
'Model to use for embedding. If not provided, it will prompt to select from running models.',
2122
},
2223
})
23-
export class EmbeddingCommand extends BaseCommand {
24+
export class EmbeddingCommand extends BaseSubCommand {
2425
constructor(
2526
private readonly inquirerService: InquirerService,
2627
readonly cortexUsecases: CortexUsecases,

cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
44
import { EngineNamesMap, Engines } from '../types/engine.interface';
55
import { BaseCommand } from '../base.command';
66
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
7+
import { BaseSubCommand } from '../base.subcommand';
78

89
@SubCommand({
910
name: '<name> get',
@@ -13,7 +14,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
1314
},
1415
})
1516
@SetCommandContext()
16-
export class EnginesGetCommand extends BaseCommand {
17+
export class EnginesGetCommand extends BaseSubCommand {
1718
constructor(
1819
readonly contextService: ContextService,
1920
readonly cortexUsecases: CortexUsecases,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file-
88
import { BaseCommand } from '../base.command';
99
import { defaultInstallationOptions } from '@/utils/init';
1010
import { Presets, SingleBar } from 'cli-progress';
11+
import { BaseSubCommand } from '../base.subcommand';
1112

1213
@SubCommand({
1314
name: '<name> init',
@@ -17,7 +18,7 @@ import { Presets, SingleBar } from 'cli-progress';
1718
},
1819
})
1920
@SetCommandContext()
20-
export class EnginesInitCommand extends BaseCommand {
21+
export class EnginesInitCommand extends BaseSubCommand {
2122
constructor(
2223
private readonly cortexUsecases: CortexUsecases,
2324
private readonly fileManagerService: FileManagerService,

cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
44
import { EngineNamesMap } from '../types/engine.interface';
55
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
66
import { BaseCommand } from '../base.command';
7+
import { BaseSubCommand } from '../base.subcommand';
78

89
@SubCommand({
910
name: 'list',
1011
description: 'Get all cortex engines',
1112
})
1213
@SetCommandContext()
13-
export class EnginesListCommand extends BaseCommand {
14+
export class EnginesListCommand extends BaseSubCommand {
1415
constructor(
1516
readonly contextService: ContextService,
1617
readonly cortexUseCases: CortexUsecases,

0 commit comments

Comments
 (0)