Skip to content

Commit

Permalink
feat: replace typeorm by sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored Jul 9, 2024
1 parent 920a2e0 commit ad545d1
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 190 deletions.
6 changes: 4 additions & 2 deletions cortex-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@nestjs/event-emitter": "^2.0.4",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/sequelize": "^10.0.1",
"@nestjs/swagger": "^7.3.1",
"@terascope/fetch-github-release": "^0.8.8",
"axios": "^1.6.8",
Expand All @@ -60,9 +61,10 @@
"readline": "^1.3.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"sequelize": "^6.37.3",
"sequelize-typescript": "^2.1.6",
"sqlite3": "^5.1.7",
"systeminformation": "^5.22.11",
"typeorm": "^0.3.20",
"ulid": "^2.3.0",
"uuid": "^9.0.1",
"whatwg-url": "^14.0.0",
Expand All @@ -72,13 +74,13 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@nestjs/typeorm": "^10.0.2",
"@types/cli-progress": "^3.11.5",
"@types/decompress": "^4.2.7",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.12.9",
"@types/sequelize": "^4.28.20",
"@types/supertest": "^6.0.2",
"@types/update-notifier": "^6.0.8",
"@types/uuid": "^9.0.8",
Expand Down
3 changes: 2 additions & 1 deletion cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { ModelRemoveCommand } from './infrastructure/commanders/models/model-rem
import { RunCommand } from './infrastructure/commanders/shortcuts/run.command';
import { ModelUpdateCommand } from './infrastructure/commanders/models/model-update.command';
import { AssistantsModule } from './usecases/assistants/assistants.module';
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
import { MessagesModule } from './usecases/messages/messages.module';
import { FileManagerModule } from './infrastructure/services/file-manager/file-manager.module';
import { PSCommand } from './infrastructure/commanders/ps.command';
Expand All @@ -34,6 +33,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command';
import { ContextModule } from './infrastructure/services/context/context.module';
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
import { ExtensionsModule } from './extensions/extensions.module';
import { ConfigsCommand } from './infrastructure/commanders/configs.command';
import { EnginesCommand } from './infrastructure/commanders/engines.command';
Expand All @@ -46,6 +46,7 @@ import { EnginesListCommand } from './infrastructure/commanders/engines/engines-
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';


@Module({
imports: [
ConfigModule.forRoot({
Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function bootstrap() {
contextService!.set('sessionId', anonymousData?.sessionId);
telemetryUseCase!.sendActivationEvent(TelemetrySource.CLI);
telemetryUseCase!.sendCrashReport();
return CommandFactory.runApplication(app);
await CommandFactory.runApplication(app);
});
}

Expand Down
25 changes: 0 additions & 25 deletions cortex-js/src/infrastructure/database/mysql-database.providers.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AssistantEntity } from '@/infrastructure/entities/assistant.entity';
import { DataSource } from 'typeorm';
import { Sequelize } from 'sequelize-typescript';

export const assistantProviders = [
{
provide: 'ASSISTANT_REPOSITORY',
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(AssistantEntity),
useFactory: async(sequelize: Sequelize) =>{
return sequelize.getRepository(AssistantEntity);
},
inject: ['DATA_SOURCE'],
},
];
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MessageEntity } from '@/infrastructure/entities/message.entity';
import { DataSource } from 'typeorm';
import { MessageEntity } from "@/infrastructure/entities/message.entity";
import { Sequelize } from "sequelize-typescript";

export const messageProviders = [
{
provide: 'MESSAGE_REPOSITORY',
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(MessageEntity),
useFactory: async(sequelize: Sequelize) =>{
return sequelize.getRepository(MessageEntity);
},
inject: ['DATA_SOURCE'],
},
];
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ThreadEntity } from '@/infrastructure/entities/thread.entity';
import { DataSource } from 'typeorm';
import { Sequelize } from 'sequelize-typescript';

export const threadProviders = [
{
provide: 'THREAD_REPOSITORY',
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(ThreadEntity),
useFactory: async(sequelize: Sequelize) =>{
return sequelize.getRepository(ThreadEntity);
},
inject: ['DATA_SOURCE'],
},
];

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
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';
import { AssistantEntity } from '../entities/assistant.entity';
import { Sequelize } from 'sequelize-typescript';

export const sqliteDatabaseProviders = [
{
Expand All @@ -13,14 +13,13 @@ export const sqliteDatabaseProviders = [
useFactory: async (fileManagerService: FileManagerService) => {
const dataFolderPath = await fileManagerService.getDataFolderPath();
const sqlitePath = join(dataFolderPath, databaseFile);
const dataSource = new DataSource({
type: 'sqlite',
database: sqlitePath,
synchronize: process.env.NODE_ENV !== 'production',
entities: [ThreadEntity, AssistantEntity, MessageEntity],
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: sqlitePath,
logging: false,
});

return dataSource.initialize();
sequelize.addModels([ThreadEntity, MessageEntity, AssistantEntity]);
return sequelize;
},
},
];
73 changes: 56 additions & 17 deletions cortex-js/src/infrastructure/entities/assistant.entity.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,90 @@
import { Table, Column, Model, PrimaryKey, DataType } from 'sequelize-typescript';
import { Assistant } from '@/domain/models/assistant.interface';
import type {
AssistantToolResources,
AssistantResponseFormatOption,
} from '@/domain/models/assistant.interface';
import { Column, Entity, PrimaryColumn } from 'typeorm';

@Entity('assistants')
export class AssistantEntity implements Assistant {
@PrimaryColumn({ type: String })
@Table({ tableName: 'assistants', timestamps: false})
export class AssistantEntity extends Model implements Assistant {
@PrimaryKey
@Column({
type: DataType.STRING,
})
id: string;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
avatar?: string;

@Column({ type: String })
@Column({
type: DataType.STRING,
defaultValue: 'assistant',
})
object: 'assistant';

@Column({ type: Number })
@Column({
type: DataType.INTEGER,
})
created_at: number;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
name: string | null;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
description: string | null;

@Column({ type: String })
@Column({
type: DataType.STRING,
})
model: string;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
instructions: string | null;

@Column({ type: 'simple-json' })
@Column({
type: DataType.JSON,
})
tools: any;

@Column({ type: 'simple-json', nullable: true })
@Column({
type: DataType.JSON,
allowNull: true,
})
metadata: any | null;

@Column({ type: Number, nullable: true })
@Column({
type: DataType.FLOAT,
allowNull: true,
})
top_p: number | null;

@Column({ type: Number, nullable: true })
@Column({
type: DataType.FLOAT,
allowNull: true,
})
temperature: number | null;

@Column({ type: 'simple-json', nullable: true })
@Column({
type: DataType.JSON,
allowNull: true,
})
response_format: AssistantResponseFormatOption | null;

@Column({ type: 'simple-json', nullable: true })
@Column({
type: DataType.JSON,
allowNull: true,
})
tool_resources: AssistantToolResources | null;
}
70 changes: 53 additions & 17 deletions cortex-js/src/infrastructure/entities/message.entity.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,88 @@
import { Table, Column, Model, PrimaryKey, DataType } from 'sequelize-typescript';
import type {
Message,
MessageContent,
MessageIncompleteDetails,
MessageAttachment,
} from '@/domain/models/message.interface';
import { Column, Entity, PrimaryColumn } from 'typeorm';

@Entity('messages')
export class MessageEntity implements Message {
@PrimaryColumn({ type: String })
@Table({ tableName: 'messages', timestamps: false})
export class MessageEntity extends Model implements Message {
@PrimaryKey
@Column({
type: DataType.STRING,
})
id: string;

@Column({ type: String })
@Column({
type: DataType.STRING,
defaultValue: 'thread.message',
})
object: 'thread.message';

@Column({ type: String })
@Column({
type: DataType.STRING,
})
thread_id: string;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
assistant_id: string | null;

@Column({ type: String })
@Column({
type: DataType.STRING,
})
role: 'user' | 'assistant';

@Column({ type: String })
@Column({
type: DataType.STRING,
})
status: 'in_progress' | 'incomplete' | 'completed';

@Column({ type: 'simple-json', nullable: true })
@Column({
type: DataType.JSON,
allowNull: true,
})
metadata: any | null;

@Column({ type: String, nullable: true })
@Column({
type: DataType.STRING,
allowNull: true,
})
run_id: string | null;

@Column({ type: Number, nullable: true })
@Column({
type: DataType.INTEGER,
allowNull: true,
})
completed_at: number | null;

@Column({ type: 'simple-json' })
@Column({
type: DataType.JSON,
})
content: MessageContent[];

@Column({ type: 'simple-json', nullable: true })
@Column({
type: DataType.JSON,
allowNull: true,
})
incomplete_details: MessageIncompleteDetails | null;

@Column({ type: Number })
@Column({
type: DataType.INTEGER,
})
created_at: number;

@Column({ type: 'simple-json' })
@Column({
type: DataType.JSON,
})
attachments: MessageAttachment[];

@Column({ type: Number, nullable: true })
@Column({
type: DataType.INTEGER,
allowNull: true,
})
incomplete_at: number | null;
}
Loading

0 comments on commit ad545d1

Please sign in to comment.