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

Commit eddfd76

Browse files
committed
Merge branch 'dev' into feat/cortex-publish-launchpad
2 parents 1d170d0 + b66b761 commit eddfd76

File tree

12 files changed

+144
-18
lines changed

12 files changed

+144
-18
lines changed

cortex-cpp/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ project(cortex-cpp C CXX)
44

55
# Build using CMAKE-JS
66
if(DEFINED CMAKE_JS_INC)
7+
if(WIN32)
8+
add_definitions(
9+
-DV8_COMPRESS_POINTERS
10+
-DV8_REVERSE_JSARGS
11+
-DV8_COMPRESS_POINTERS_IN_ISOLATE_CAGE
12+
)
13+
endif()
714
include_directories(${CMAKE_JS_INC})
815
endif()
916

@@ -76,7 +83,19 @@ if(DEFINED CMAKE_JS_INC)
7683

7784
add_library(${PROJECT_NAME} SHARED addon.cc
7885
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
86+
${CMAKE_JS_SRC}
7987
)
88+
89+
if(WIN32)
90+
target_link_libraries(${PROJECT_NAME}
91+
PRIVATE
92+
msvcprt.lib
93+
msvcrt.lib
94+
vcruntime.lib
95+
ucrt.lib
96+
${CMAKE_JS_LIB}
97+
)
98+
endif()
8099
else() # Official build
81100
add_executable(${PROJECT_NAME} main.cc
82101
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc

cortex-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"class-transformer": "^0.5.1",
5555
"class-validator": "^0.14.1",
5656
"cli-progress": "^3.12.0",
57-
"cortex-cpp": "0.4.25",
57+
"cortex-cpp": "0.4.34",
5858
"cortexso-node": "^0.0.4",
5959
"cpu-instructions": "^0.0.11",
6060
"decompress": "^4.2.1",

cortex-js/src/extensions/anthropic.engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
9393
return {
9494
choices: [
9595
{
96-
delta: {
96+
message: {
9797
content: data.content[0].text,
9898
},
9999
},

cortex-js/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@/infrastructure/constants/cortex';
66
import { getApp } from './app';
77
import chalk from 'chalk';
8+
import { CortexUsecases } from './usecases/cortex/cortex.usecases';
89

910
/**
1011
* Start the API server
@@ -17,6 +18,8 @@ export async function start(host?: string, port?: number) {
1718

1819
try {
1920
await app.listen(sPort, sHost);
21+
const cortexUsecases = await app.resolve(CortexUsecases);
22+
await cortexUsecases.startCortex();
2023
console.log(chalk.blue(`Started server at http://${sHost}:${sPort}`));
2124
console.log(
2225
chalk.blue(`API Playground available at http://${sHost}:${sPort}/api`),

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ import { Engines } from './types/engine.interface';
1919
import { join } from 'path';
2020
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
2121
import { FileManagerService } from '../services/file-manager/file-manager.service';
22-
import { isLocalModel } from '@/utils/normalize-model-id';
22+
import { isLocalModel, isRemoteEngine } from '@/utils/normalize-model-id';
2323

2424
type ChatOptions = {
2525
threadId?: string;
2626
message?: string;
2727
attach: boolean;
28+
preset?: string;
2829
};
2930

3031
@SubCommand({
@@ -89,6 +90,7 @@ export class ChatCommand extends BaseCommand {
8990
const engine = existingModel.engine || Engines.llamaCPP;
9091
// Pull engine if not exist
9192
if (
93+
!isRemoteEngine(engine) &&
9294
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
9395
) {
9496
await this.initUsecases.installEngine(undefined, 'latest', engine);
@@ -106,7 +108,7 @@ export class ChatCommand extends BaseCommand {
106108
);
107109
return this.cortexUsecases
108110
.startCortex()
109-
.then(() => this.modelsCliUsecases.startModel(modelId))
111+
.then(() => this.modelsCliUsecases.startModel(modelId, options.preset))
110112
.then(() =>
111113
this.chatCliUsecases.chat(
112114
modelId,
@@ -156,4 +158,12 @@ export class ChatCommand extends BaseCommand {
156158
parseAttach() {
157159
return true;
158160
}
161+
162+
@Option({
163+
flags: '-p, --preset <preset>',
164+
description: 'Apply a chat preset to the chat session',
165+
})
166+
parsePreset(value: string) {
167+
return value;
168+
}
159169
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pkg from '@/../package.json';
12
import { RootCommand, CommandRunner, Option } from 'nest-commander';
23
import { ChatCommand } from './chat.command';
34
import { ModelsCommand } from './models.command';
@@ -18,13 +19,15 @@ import { FileManagerService } from '../services/file-manager/file-manager.servic
1819
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
1920
import { ServeStopCommand } from './sub-commands/serve-stop.command';
2021
import ora from 'ora';
22+
import { printSlogan } from '@/utils/logo';
2123
import { EnginesSetCommand } from './engines/engines-set.command';
2224

2325
type ServeOptions = {
2426
address?: string;
2527
port?: number;
2628
logs?: boolean;
2729
dataFolder?: string;
30+
version?: boolean;
2831
};
2932

3033
@RootCommand({
@@ -58,8 +61,15 @@ export class CortexCommand extends CommandRunner {
5861
const host = options?.address || defaultCortexJsHost;
5962
const port = options?.port || defaultCortexJsPort;
6063
const showLogs = options?.logs || false;
64+
const showVersion = options?.version || false;
6165
const dataFolderPath = options?.dataFolder;
62-
66+
if (showVersion) {
67+
printSlogan();
68+
console.log('\n');
69+
console.log(`Cortex CLI - v${pkg.version}`);
70+
console.log(chalk.blue(`Github: ${pkg.homepage}`));
71+
return;
72+
}
6373
return this.startServer(host, port, showLogs, dataFolderPath);
6474
}
6575

@@ -114,13 +124,15 @@ export class CortexCommand extends CommandRunner {
114124
apiServerPort: port,
115125
dataFolderPath: dataFolderPath || config.dataFolderPath,
116126
});
127+
process.exit(1);
117128
} catch (e) {
118129
console.error(e);
119130
// revert the data folder path if it was set
120131
await this.fileManagerService.writeConfigFile({
121132
...config,
122133
});
123134
console.error(`Failed to start server. Is port ${port} in use?`);
135+
process.exit(0);
124136
}
125137
}
126138

@@ -155,4 +167,12 @@ export class CortexCommand extends CommandRunner {
155167
parseDataFolder(value: string) {
156168
return value;
157169
}
170+
171+
@Option({
172+
flags: '-v, --version',
173+
description: 'Show version',
174+
})
175+
parseVersion() {
176+
return true;
177+
}
158178
}
Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,86 @@
1-
import { CommandRunner, SubCommand } from 'nest-commander';
1+
import { SubCommand } from 'nest-commander';
22
import { ModelStartCommand } from './models/model-start.command';
33
import { ModelGetCommand } from './models/model-get.command';
44
import { ModelListCommand } from './models/model-list.command';
55
import { ModelStopCommand } from './models/model-stop.command';
6-
import { ModelPullCommand } from './models/model-pull.command';
76
import { ModelRemoveCommand } from './models/model-remove.command';
87
import { ModelUpdateCommand } from './models/model-update.command';
9-
import { RunCommand } from './shortcuts/run.command';
108
import { BaseCommand } from './base.command';
9+
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
10+
import { ModuleRef } from '@nestjs/core';
11+
import { ModelPullCommand } from './models/model-pull.command';
1112

1213
@SubCommand({
1314
name: 'models',
1415
subCommands: [
15-
ModelPullCommand,
1616
ModelStartCommand,
1717
ModelStopCommand,
1818
ModelListCommand,
1919
ModelGetCommand,
2020
ModelRemoveCommand,
2121
ModelUpdateCommand,
22-
RunCommand,
2322
],
2423
description: 'Subcommands for managing models',
2524
})
2625
export class ModelsCommand extends BaseCommand {
27-
async runCommand(): Promise<void> {
28-
this.command?.help();
26+
constructor(
27+
private readonly moduleRef: ModuleRef,
28+
readonly cortexUsecases: CortexUsecases,
29+
) {
30+
super(cortexUsecases);
31+
}
32+
33+
commandMap: { [key: string]: any } = {
34+
pull: ModelPullCommand,
35+
start: ModelStartCommand,
36+
stop: ModelStopCommand,
37+
list: ModelListCommand,
38+
get: ModelGetCommand,
39+
remove: ModelRemoveCommand,
40+
update: ModelUpdateCommand,
41+
};
42+
async runCommand(passedParam: string[], options: any) {
43+
const [parameter, command, ...restParams] = passedParam;
44+
if (command !== 'list' && !parameter) {
45+
console.error('Model id is required.');
46+
return;
47+
}
48+
49+
// Handle the commands accordingly
50+
const commandClass = this.commandMap[command as string];
51+
if (!commandClass) {
52+
this.command?.help();
53+
return;
54+
}
55+
const modelId = parameter;
56+
await this.runModelCommand(
57+
commandClass,
58+
[modelId, ...(restParams || [])],
59+
options,
60+
);
61+
}
62+
63+
private async runModelCommand(
64+
commandClass: any,
65+
params: string[] = [],
66+
options?: any,
67+
) {
68+
const commandInstance = this.moduleRef.get(commandClass, { strict: false });
69+
if (commandInstance) {
70+
await commandInstance.run(params, options);
71+
} else {
72+
console.error('Command not found.');
73+
}
74+
}
75+
76+
help() {
77+
console.log('Usage: cortex models <subcommand|parameter> [subcommand]');
78+
console.log('Commands:');
79+
console.log(' <model_id> start - Start a model by ID');
80+
console.log(' <model_id> stop - Stop a model by ID');
81+
console.log(' list - List all available models');
82+
console.log(' <model_id> get - Get model details by ID');
83+
console.log(' <model_id> remove - Remove a model by ID');
84+
console.log(' <model_id> update - Update a model by ID');
2985
}
3086
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Engines } from '../types/engine.interface';
1212
import { checkModelCompatibility } from '@/utils/model-check';
1313
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
1414
import { BaseCommand } from '../base.command';
15+
import { isRemoteEngine } from '@/utils/normalize-model-id';
1516

1617
type ModelStartOptions = {
1718
attach: boolean;
@@ -72,6 +73,7 @@ export class ModelStartCommand extends BaseCommand {
7273
const engine = existingModel.engine || Engines.llamaCPP;
7374
// Pull engine if not exist
7475
if (
76+
!isRemoteEngine(engine) &&
7577
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
7678
) {
7779
await this.initUsecases.installEngine(undefined, 'latest', engine);

cortex-js/src/infrastructure/commanders/shortcuts/run.command.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Engines } from '../types/engine.interface';
1212
import { checkModelCompatibility } from '@/utils/model-check';
1313
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
1414
import { BaseCommand } from '../base.command';
15+
import { isLocalModel, isRemoteEngine } from '@/utils/normalize-model-id';
1516

1617
type RunOptions = {
1718
threadId?: string;
@@ -69,11 +70,7 @@ export class RunCommand extends BaseCommand {
6970

7071
// Second check if model is available
7172
const existingModel = await this.modelsCliUsecases.getModel(modelId);
72-
if (
73-
!existingModel ||
74-
!Array.isArray(existingModel.files) ||
75-
/^(http|https):\/\/[^/]+\/.*/.test(existingModel.files[0])
76-
) {
73+
if (!existingModel || !isLocalModel(existingModel.files)) {
7774
checkingSpinner.fail(`Model is not available`);
7875
process.exit(1);
7976
}
@@ -82,6 +79,7 @@ export class RunCommand extends BaseCommand {
8279
const engine = existingModel.engine || Engines.llamaCPP;
8380
// Pull engine if not exist
8481
if (
82+
!isRemoteEngine(engine) &&
8583
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
8684
) {
8785
await this.initUsecases.installEngine(undefined, 'latest', engine);

cortex-js/src/infrastructure/commanders/types/engine.interface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ export const EngineNamesMap: {
1717
[Engines.onnx]: 'onnx',
1818
[Engines.tensorrtLLM]: 'tensorrt-llm',
1919
};
20+
21+
export const RemoteEngines: Engines[] = [
22+
Engines.groq,
23+
Engines.mistral,
24+
Engines.openai,
25+
Engines.anthropic,
26+
];

0 commit comments

Comments
 (0)