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

Commit f82f5d7

Browse files
refactor: cortex cli as client - communicate with API server via cortexjs (#906)
Co-authored-by: marknguyen1302 <nguyenvu1302.work@gmail.com>
1 parent 2004d9e commit f82f5d7

Some content is hidden

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

51 files changed

+1046
-1327
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-node": "^0.0.4",
58+
"@cortexso/cortex.js": "^0.1.1",
5959
"cpu-instructions": "^0.0.11",
6060
"decompress": "^4.2.1",
6161
"js-yaml": "^4.1.0",

cortex-js/src/command.module.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Module } from '@nestjs/common';
2-
import { ModelsModule } from './usecases/models/models.module';
3-
import { DatabaseModule } from './infrastructure/database/database.module';
42
import { ConfigModule } from '@nestjs/config';
53
import { CortexModule } from './usecases/cortex/cortex.module';
64
import { ModelsCommand } from './infrastructure/commanders/models.command';
7-
import { ExtensionModule } from './infrastructure/repositories/extensions/extension.module';
85
import { HttpModule } from '@nestjs/axios';
96
import { InitRunModeQuestions } from './infrastructure/commanders/questions/init.questions';
107
import { ModelListCommand } from './infrastructure/commanders/models/model-list.command';
@@ -15,26 +12,18 @@ import { ModelStartCommand } from './infrastructure/commanders/models/model-star
1512
import { ModelStopCommand } from './infrastructure/commanders/models/model-stop.command';
1613
import { ModelGetCommand } from './infrastructure/commanders/models/model-get.command';
1714
import { ModelRemoveCommand } from './infrastructure/commanders/models/model-remove.command';
18-
import { RunCommand } from './infrastructure/commanders/shortcuts/run.command';
15+
import { RunCommand } from './infrastructure/commanders/run.command';
1916
import { ModelUpdateCommand } from './infrastructure/commanders/models/model-update.command';
20-
import { AssistantsModule } from './usecases/assistants/assistants.module';
21-
import { MessagesModule } from './usecases/messages/messages.module';
2217
import { FileManagerModule } from './infrastructure/services/file-manager/file-manager.module';
2318
import { PSCommand } from './infrastructure/commanders/ps.command';
2419
import { PresetCommand } from './infrastructure/commanders/presets.command';
2520
import { TelemetryModule } from './usecases/telemetry/telemetry.module';
2621
import { TelemetryCommand } from './infrastructure/commanders/telemetry.command';
2722
import { EmbeddingCommand } from './infrastructure/commanders/embeddings.command';
2823
import { BenchmarkCommand } from './infrastructure/commanders/benchmark.command';
29-
import { EventEmitterModule } from '@nestjs/event-emitter';
30-
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
31-
import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command';
24+
import { ServeStopCommand } from './infrastructure/commanders/serve-stop.command';
3225
import { ContextModule } from './infrastructure/services/context/context.module';
33-
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
34-
import { ExtensionsModule } from './extensions/extensions.module';
3526
import { EnginesCommand } from './infrastructure/commanders/engines.command';
36-
import { ConfigsModule } from './usecases/configs/configs.module';
37-
import { EnginesModule } from './usecases/engines/engines.module';
3827
import { EnginesListCommand } from './infrastructure/commanders/engines/engines-list.command';
3928
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
4029
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';
@@ -47,22 +36,11 @@ import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-s
4736
envFilePath:
4837
process.env.NODE_ENV !== 'production' ? '.env.development' : '.env',
4938
}),
50-
EventEmitterModule.forRoot(),
51-
DatabaseModule,
52-
ModelsModule,
5339
CortexModule,
54-
ExtensionModule,
5540
HttpModule,
56-
CliUsecasesModule,
57-
AssistantsModule,
58-
MessagesModule,
5941
FileManagerModule,
6042
TelemetryModule,
6143
ContextModule,
62-
DownloadManagerModule,
63-
ExtensionsModule,
64-
ConfigsModule,
65-
EnginesModule,
6644
],
6745
providers: [
6846
CortexCommand,
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Assistant as OpenAiAssistant } from 'cortexso-node/resources/beta/assistants';
2-
import { AssistantResponseFormatOption as OpenAIAssistantResponseFormatOption } from 'cortexso-node/resources/beta/threads/threads';
1+
import { Cortex } from '@cortexso/cortex.js';
2+
// TODO: Why we need to alias these?
33

4-
export interface Assistant extends OpenAiAssistant {
4+
export interface Assistant extends Cortex.Beta.Assistant {
55
avatar?: string;
66
}
77

8-
export type AssistantResponseFormatOption = OpenAIAssistantResponseFormatOption;
8+
export type AssistantResponseFormatOption =
9+
Cortex.Beta.Threads.AssistantResponseFormatOption;
910

10-
export interface AssistantToolResources extends OpenAiAssistant.ToolResources {}
11+
export interface AssistantToolResources
12+
extends Cortex.Beta.Assistant.ToolResources {}

cortex-js/src/domain/models/download.interface.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export class DownloadState {
2424
*/
2525
error?: string;
2626

27+
/**
28+
* The progress of the download. It is a number between 0 and 100.
29+
*/
30+
progress: number;
31+
2732
/**
2833
* The actual downloads. [DownloadState] is just a group to supporting for download multiple files.
2934
*/
@@ -53,6 +58,8 @@ export class DownloadItem {
5358
transferred: number;
5459
};
5560

61+
progress: number;
62+
5663
checksum?: string;
5764

5865
status: DownloadStatus;
@@ -69,5 +76,5 @@ export interface DownloadStateEvent {
6976
export enum DownloadType {
7077
Model = 'model',
7178
Miscelanous = 'miscelanous',
72-
Engine = 'engine'
79+
Engine = 'engine',
7380
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import {
2-
Message as OpenAiMessage,
3-
MessageContent as OpenAiMessageContent,
4-
TextContentBlock as OpenAiTextContentBlock,
5-
} from 'cortexso-node/resources/beta/threads/messages';
1+
import { Cortex } from '@cortexso/cortex.js';
62

7-
export interface Message extends OpenAiMessage {}
3+
export interface Message extends Cortex.Beta.Threads.Message {}
84

9-
export type MessageContent = OpenAiMessageContent;
5+
export type MessageContent = Cortex.Beta.Threads.MessageContent;
106

11-
export type TextContentBlock = OpenAiTextContentBlock;
7+
export type TextContentBlock = Cortex.Beta.Threads.TextContentBlock;
128

139
export interface MessageIncompleteDetails
14-
extends OpenAiMessage.IncompleteDetails {}
10+
extends Cortex.Beta.Threads.Message.IncompleteDetails {}
1511

16-
export interface MessageAttachment extends OpenAiMessage.Attachment {}
12+
export interface MessageAttachment
13+
extends Cortex.Beta.Threads.Message.Attachment {}

cortex-js/src/domain/models/model.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Model as OpenAiModel } from 'cortexso-node/resources/models';
1+
import { Cortex } from '@cortexso/cortex.js';
22

33
export interface Model
4-
extends OpenAiModel,
4+
extends Cortex.Model,
55
ModelSettingParams,
66
ModelRuntimeParams {
77
/**

cortex-js/src/domain/models/thread.interface.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Thread as OpenAiThread } from 'cortexso-node/resources/beta/threads/threads';
21
import { Assistant } from './assistant.interface';
2+
import { Cortex } from '@cortexso/cortex.js';
33

4-
export interface ThreadToolResources extends OpenAiThread.ToolResources {}
4+
export interface ThreadToolResources
5+
extends Cortex.Beta.Threads.Thread.ToolResources {}
56

6-
export interface Thread extends OpenAiThread {
7+
export interface Thread extends Cortex.Beta.Threads.Thread {
78
title: string;
89

910
assistants: Assistant[];

cortex-js/src/domain/repositories/telemetry.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export abstract class TelemetryRepository {
4444
hardware: BenchmarkHardware;
4545
results: any;
4646
metrics: any;
47-
model: ModelStat;
47+
model: any;
4848
sessionId: string;
4949
}): Promise<void>;
5050
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import { CommandRunner } from 'nest-commander';
22
import { Injectable } from '@nestjs/common';
33
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
4+
import Cortex from '@cortexso/cortex.js';
45
import ora from 'ora';
6+
import { FileManagerService } from '../services/file-manager/file-manager.service';
7+
import { cortexNamespace, cortexServerAPI } from '../constants/cortex';
58

69
@Injectable()
710
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+
815
constructor(readonly cortexUseCases: CortexUsecases) {
916
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+
});
1030
}
1131
protected abstract runCommand(
1232
passedParam: string[],

0 commit comments

Comments
 (0)