From d72d08a3a86f6ce2e9035fcc9e149c77d5536661 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 18 Jun 2024 14:44:44 +0700 Subject: [PATCH] feat: cortex single executable binary (#715) --- cortex-js/package.json | 9 ++++++++- cortex-js/src/app.module.ts | 2 +- cortex-js/src/command.module.ts | 2 +- cortex-js/src/command.ts | 2 +- .../infrastructure/controllers/chat.controller.spec.ts | 2 +- .../controllers/embeddings.controller.spec.ts | 2 +- .../src/infrastructure/controllers/events.controller.ts | 2 +- .../infrastructure/controllers/models.controller.spec.ts | 2 +- .../infrastructure/database/mysql-database.providers.ts | 5 ++++- .../infrastructure/database/sqlite-database.providers.ts | 5 ++++- .../infrastructure/repositories/models/model.module.ts | 2 +- .../download-manager/download-manager.module.ts | 0 .../download-manager/download-manager.service.spec.ts | 0 .../download-manager/download-manager.service.ts | 0 cortex-js/src/usecases/chat/chat.usecases.spec.ts | 2 +- cortex-js/src/usecases/models/models.module.ts | 2 +- cortex-js/src/usecases/models/models.usecases.spec.ts | 2 +- cortex-js/src/usecases/models/models.usecases.ts | 2 +- 18 files changed, 28 insertions(+), 15 deletions(-) rename cortex-js/src/{ => infrastructure/services}/download-manager/download-manager.module.ts (100%) rename cortex-js/src/{ => infrastructure/services}/download-manager/download-manager.service.spec.ts (100%) rename cortex-js/src/{ => infrastructure/services}/download-manager/download-manager.service.ts (100%) diff --git a/cortex-js/package.json b/cortex-js/package.json index df150593d..4d6ea116b 100644 --- a/cortex-js/package.json +++ b/cortex-js/package.json @@ -11,6 +11,7 @@ "scripts": { "dev": "nest dev", "build": "yarn build:extensions && nest build && cpx \"cpuinfo/bin/**\" dist/bin", + "build:binary": "yarn build && nexe dist/src/command.js --build --python=$(which python3) -o dist/cortex", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "build:extensions": "run-script-os", "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 }\"", @@ -19,7 +20,7 @@ "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", - "start:prod": "node dist/main", + "start:prod": "node dist/src/main --trace-deprecation", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", "test:watch": "jest --watch", @@ -84,8 +85,10 @@ "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", "hanbi": "^1.0.3", + "is-primitive": "^3.0.1", "jest": "^29.5.0", "nest-commander-testing": "^3.3.0", + "nexe": "^4.0.0-rc.6", "prettier": "^3.0.0", "run-script-os": "^1.1.6", "source-map-support": "^0.5.21", @@ -95,6 +98,10 @@ "tsconfig-paths": "^4.2.0", "typescript": "^5.1.3" }, + "resolutions": { + "ajv": "8.15.0", + "whatwg-url": "14.0.0" + }, "files": [ "dist" ], diff --git a/cortex-js/src/app.module.ts b/cortex-js/src/app.module.ts index 947d7af7d..189d8e487 100644 --- a/cortex-js/src/app.module.ts +++ b/cortex-js/src/app.module.ts @@ -26,7 +26,7 @@ import { ModelsController } from './infrastructure/controllers/models.controller import { ThreadsController } from './infrastructure/controllers/threads.controller'; import { StatusController } from './infrastructure/controllers/status.controller'; import { ProcessController } from './infrastructure/controllers/process.controller'; -import { DownloadManagerModule } from './download-manager/download-manager.module'; +import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module'; @Module({ imports: [ diff --git a/cortex-js/src/command.module.ts b/cortex-js/src/command.module.ts index f5a3f6474..3107eeac7 100644 --- a/cortex-js/src/command.module.ts +++ b/cortex-js/src/command.module.ts @@ -33,7 +33,7 @@ import { UtilModule } from './util/util.module'; import { EmbeddingCommand } from './infrastructure/commanders/embeddings.command'; import { BenchmarkCommand } from './infrastructure/commanders/benchmark.command'; import { EventEmitterModule } from '@nestjs/event-emitter'; -import { DownloadManagerModule } from './download-manager/download-manager.module'; +import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module'; import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command'; @Module({ diff --git a/cortex-js/src/command.ts b/cortex-js/src/command.ts index a9d5c6c06..2b79789c3 100644 --- a/cortex-js/src/command.ts +++ b/cortex-js/src/command.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env node --no-warnings +#!/usr/bin/env node import { CommandFactory } from 'nest-commander'; import { CommandModule } from './command.module'; import updateNotifier from 'update-notifier'; diff --git a/cortex-js/src/infrastructure/controllers/chat.controller.spec.ts b/cortex-js/src/infrastructure/controllers/chat.controller.spec.ts index 323d8b16a..8a453ff68 100644 --- a/cortex-js/src/infrastructure/controllers/chat.controller.spec.ts +++ b/cortex-js/src/infrastructure/controllers/chat.controller.spec.ts @@ -5,7 +5,7 @@ import { DatabaseModule } from '../database/database.module'; import { ExtensionModule } from '../repositories/extensions/extension.module'; import { ModelRepositoryModule } from '../repositories/models/model.module'; import { HttpModule } from '@nestjs/axios'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { TelemetryModule } from '@/usecases/telemetry/telemetry.module'; diff --git a/cortex-js/src/infrastructure/controllers/embeddings.controller.spec.ts b/cortex-js/src/infrastructure/controllers/embeddings.controller.spec.ts index dd6ae9938..dd089bc84 100644 --- a/cortex-js/src/infrastructure/controllers/embeddings.controller.spec.ts +++ b/cortex-js/src/infrastructure/controllers/embeddings.controller.spec.ts @@ -5,7 +5,7 @@ import { DatabaseModule } from '../database/database.module'; import { ModelRepositoryModule } from '../repositories/models/model.module'; import { ExtensionModule } from '../repositories/extensions/extension.module'; import { HttpModule } from '@nestjs/axios'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { TelemetryModule } from '@/usecases/telemetry/telemetry.module'; diff --git a/cortex-js/src/infrastructure/controllers/events.controller.ts b/cortex-js/src/infrastructure/controllers/events.controller.ts index 44c5516e2..1de23af20 100644 --- a/cortex-js/src/infrastructure/controllers/events.controller.ts +++ b/cortex-js/src/infrastructure/controllers/events.controller.ts @@ -9,7 +9,7 @@ import { ModelStatus, ModelStatusAndEvent, } from '@/domain/models/model.event'; -import { DownloadManagerService } from '@/download-manager/download-manager.service'; +import { DownloadManagerService } from '@/infrastructure/services/download-manager/download-manager.service'; import { ModelsUsecases } from '@/usecases/models/models.usecases'; import { Controller, Sse } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; diff --git a/cortex-js/src/infrastructure/controllers/models.controller.spec.ts b/cortex-js/src/infrastructure/controllers/models.controller.spec.ts index 578a743c0..4417536e9 100644 --- a/cortex-js/src/infrastructure/controllers/models.controller.spec.ts +++ b/cortex-js/src/infrastructure/controllers/models.controller.spec.ts @@ -7,7 +7,7 @@ import { FileManagerModule } from '@/infrastructure/services/file-manager/file-m import { HttpModule } from '@nestjs/axios'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { ModelRepositoryModule } from '../repositories/models/model.module'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { TelemetryModule } from '@/usecases/telemetry/telemetry.module'; import { UtilModule } from '@/util/util.module'; diff --git a/cortex-js/src/infrastructure/database/mysql-database.providers.ts b/cortex-js/src/infrastructure/database/mysql-database.providers.ts index 347c82ef5..ff756d5a8 100644 --- a/cortex-js/src/infrastructure/database/mysql-database.providers.ts +++ b/cortex-js/src/infrastructure/database/mysql-database.providers.ts @@ -1,5 +1,8 @@ import { databaseName } from '@/infrastructure/constants/cortex'; import { DataSource } from 'typeorm'; +import { ThreadEntity } from '../entities/thread.entity'; +import { AssistantEntity } from '../entities/assistant.entity'; +import { MessageEntity } from '../entities/message.entity'; export const mysqlDatabaseProviders = [ { @@ -12,7 +15,7 @@ export const mysqlDatabaseProviders = [ username: 'root', password: '', database: databaseName, - entities: [__dirname + '/../**/*.entity{.ts,.js}'], + entities: [ThreadEntity, AssistantEntity, MessageEntity], synchronize: process.env.NODE_ENV !== 'production', }); diff --git a/cortex-js/src/infrastructure/database/sqlite-database.providers.ts b/cortex-js/src/infrastructure/database/sqlite-database.providers.ts index 7de556809..e76b49b91 100644 --- a/cortex-js/src/infrastructure/database/sqlite-database.providers.ts +++ b/cortex-js/src/infrastructure/database/sqlite-database.providers.ts @@ -2,6 +2,9 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file- import { databaseFile } from '@/infrastructure/constants/cortex'; import { join } from 'path'; import { DataSource } from 'typeorm'; +import { ThreadEntity } from '../entities/thread.entity'; +import { AssistantEntity } from '../entities/assistant.entity'; +import { MessageEntity } from '../entities/message.entity'; export const sqliteDatabaseProviders = [ { @@ -14,7 +17,7 @@ export const sqliteDatabaseProviders = [ type: 'sqlite', database: sqlitePath, synchronize: process.env.NODE_ENV !== 'production', - entities: [__dirname + '/../**/*.entity{.ts,.js}'], + entities: [ThreadEntity, AssistantEntity, MessageEntity], }); return dataSource.initialize(); diff --git a/cortex-js/src/infrastructure/repositories/models/model.module.ts b/cortex-js/src/infrastructure/repositories/models/model.module.ts index 8463ed3f6..c5bcda706 100644 --- a/cortex-js/src/infrastructure/repositories/models/model.module.ts +++ b/cortex-js/src/infrastructure/repositories/models/model.module.ts @@ -4,7 +4,7 @@ import { HttpModule } from '@nestjs/axios'; import { ModelRepository } from '@/domain/repositories/model.interface'; import { ModelRepositoryImpl } from './model.repository'; import { FileManagerModule } from '@/infrastructure/services/file-manager/file-manager.module'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; @Module({ imports: [ diff --git a/cortex-js/src/download-manager/download-manager.module.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.module.ts similarity index 100% rename from cortex-js/src/download-manager/download-manager.module.ts rename to cortex-js/src/infrastructure/services/download-manager/download-manager.module.ts diff --git a/cortex-js/src/download-manager/download-manager.service.spec.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.spec.ts similarity index 100% rename from cortex-js/src/download-manager/download-manager.service.spec.ts rename to cortex-js/src/infrastructure/services/download-manager/download-manager.service.spec.ts diff --git a/cortex-js/src/download-manager/download-manager.service.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts similarity index 100% rename from cortex-js/src/download-manager/download-manager.service.ts rename to cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts diff --git a/cortex-js/src/usecases/chat/chat.usecases.spec.ts b/cortex-js/src/usecases/chat/chat.usecases.spec.ts index 6e99fdb1e..e8653ec9b 100644 --- a/cortex-js/src/usecases/chat/chat.usecases.spec.ts +++ b/cortex-js/src/usecases/chat/chat.usecases.spec.ts @@ -5,7 +5,7 @@ import { ExtensionModule } from '@/infrastructure/repositories/extensions/extens import { TelemetryModule } from '../telemetry/telemetry.module'; import { ModelRepositoryModule } from '@/infrastructure/repositories/models/model.module'; import { HttpModule } from '@nestjs/axios'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; describe('ChatService', () => { diff --git a/cortex-js/src/usecases/models/models.module.ts b/cortex-js/src/usecases/models/models.module.ts index a72fb0a28..b4ea0e328 100644 --- a/cortex-js/src/usecases/models/models.module.ts +++ b/cortex-js/src/usecases/models/models.module.ts @@ -8,7 +8,7 @@ import { TelemetryModule } from '../telemetry/telemetry.module'; import { UtilModule } from '@/util/util.module'; import { FileManagerModule } from '@/infrastructure/services/file-manager/file-manager.module'; import { ModelRepositoryModule } from '@/infrastructure/repositories/models/model.module'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; @Module({ imports: [ diff --git a/cortex-js/src/usecases/models/models.usecases.spec.ts b/cortex-js/src/usecases/models/models.usecases.spec.ts index f59750585..2f8053ab6 100644 --- a/cortex-js/src/usecases/models/models.usecases.spec.ts +++ b/cortex-js/src/usecases/models/models.usecases.spec.ts @@ -6,7 +6,7 @@ import { ExtensionModule } from '@/infrastructure/repositories/extensions/extens import { FileManagerModule } from '@/infrastructure/services/file-manager/file-manager.module'; import { HttpModule } from '@nestjs/axios'; import { ModelRepositoryModule } from '@/infrastructure/repositories/models/model.module'; -import { DownloadManagerModule } from '@/download-manager/download-manager.module'; +import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { TelemetryModule } from '../telemetry/telemetry.module'; import { UtilModule } from '@/util/util.module'; diff --git a/cortex-js/src/usecases/models/models.usecases.ts b/cortex-js/src/usecases/models/models.usecases.ts index 3b5080342..a14c3230a 100644 --- a/cortex-js/src/usecases/models/models.usecases.ts +++ b/cortex-js/src/usecases/models/models.usecases.ts @@ -36,9 +36,9 @@ import { getHFModelMetadata, } from '@/utils/huggingface'; import { DownloadType } from '@/domain/models/download.interface'; -import { DownloadManagerService } from '@/download-manager/download-manager.service'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { ModelId, ModelStatus } from '@/domain/models/model.event'; +import { DownloadManagerService } from '@/infrastructure/services/download-manager/download-manager.service'; @Injectable() export class ModelsUsecases {