Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { exit } from 'node:process';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { createReadStream, existsSync, statSync, watchFile } from 'node:fs';
import { existsSync } from 'node:fs';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { join } from 'node:path';
import { Engines } from '../types/engine.interface';
Expand All @@ -14,11 +14,12 @@ import { isRemoteEngine } from '@/utils/normalize-model-id';
import { downloadProgress } from '@/utils/download-progress';
import { CortexClient } from '../services/cortex.client';
import { DownloadType } from '@/domain/models/download.interface';
import { printLastErrorLines } from '@/utils/logs';

type ModelStartOptions = {
attach: boolean;
preset?: string;
};

@SubCommand({
name: 'start',
description: 'Start a model by ID.',
Expand Down Expand Up @@ -77,16 +78,17 @@ export class ModelStartCommand extends BaseCommand {
await downloadProgress(this.cortex, undefined, DownloadType.Engine);
}

// Attached - stdout logs
if (options.attach) {
this.attachLogWatch();
}

const parsedPreset = await this.fileService.getPreset(options.preset);

const startingSpinner = ora('Loading model...').start();

await this.cortex.models
.start(modelId, parsedPreset)
.then(() => options.attach && ora('Model is running...').start());
.then(() => startingSpinner.succeed('Model loaded'))
.catch(async (error) => {
startingSpinner.fail(error.message ?? error);
printLastErrorLines(await this.fileService.getLogPath());
});
}

modelInquiry = async () => {
Expand All @@ -104,55 +106,11 @@ export class ModelStartCommand extends BaseCommand {
return model;
};

@Option({
flags: '-a, --attach',
description: 'Attach to interactive chat session',
defaultValue: false,
name: 'attach',
})
parseAttach() {
return true;
}

@Option({
flags: '-p, --preset <preset>',
description: 'Apply a chat preset to the chat session',
})
parseTemplate(value: string) {
return value;
}

/**
* Attach to the log file and watch for changes
*/
private async attachLogWatch() {
const logPath = await this.fileService.getLogPath();
const initialSize = statSync(logPath).size;
const logStream = createReadStream(logPath, {
start: initialSize,
encoding: 'utf-8',
autoClose: false,
});
logStream.on('data', (chunk) => {
console.log(chunk);
});
watchFile(logPath, (curr, prev) => {
// Check if the file size has increased
if (curr.size > prev.size) {
// Calculate the position to start reading from
const position = prev.size;

// Create a new read stream from the updated position
const updateStream = createReadStream(logPath, {
encoding: 'utf8',
start: position,
});

// Read the newly written content
updateStream.on('data', (chunk) => {
console.log(chunk);
});
}
});
}
}
5 changes: 4 additions & 1 deletion cortex-js/src/infrastructure/commanders/run.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CortexClient } from './services/cortex.client';
import { DownloadType } from '@/domain/models/download.interface';
import { isLocalFile } from '@/utils/urls';
import { parse } from 'node:path';
import { printLastErrorLines } from '@/utils/logs';

type RunOptions = {
threadId?: string;
Expand Down Expand Up @@ -109,8 +110,10 @@ export class RunCommand extends BaseCommand {
if (options.chat) this.chatClient.chat(modelId, options.threadId);
else console.log("To start a chat session, use the '--chat' flag");
})
.catch((e) => {
.catch(async (e) => {
startingSpinner.fail(e.message ?? e);

printLastErrorLines(await this.fileService.getLogPath());
});
}

Expand Down
4 changes: 2 additions & 2 deletions cortex-js/src/utils/huggingface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ export async function getHFModelMetadata(
const stopWord: string = metadata['tokenizer.ggml.tokens'][index] ?? '';
const name = metadata['general.name'];
const contextLength = metadata['llama.context_length'] ?? 4096;
const ngl = (metadata['llama.block_count'] ?? 32) + 1
const ngl = (metadata['llama.block_count'] ?? 32) + 1;
const version: number = metadata['version'];

return {
contextLength,
ngl,
Expand Down