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

Commit ad545d1

Browse files
feat: replace typeorm by sequelize
1 parent 920a2e0 commit ad545d1

File tree

14 files changed

+247
-190
lines changed

14 files changed

+247
-190
lines changed

cortex-js/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@nestjs/event-emitter": "^2.0.4",
4646
"@nestjs/mapped-types": "*",
4747
"@nestjs/platform-express": "^10.0.0",
48+
"@nestjs/sequelize": "^10.0.1",
4849
"@nestjs/swagger": "^7.3.1",
4950
"@terascope/fetch-github-release": "^0.8.8",
5051
"axios": "^1.6.8",
@@ -60,9 +61,10 @@
6061
"readline": "^1.3.0",
6162
"reflect-metadata": "^0.2.0",
6263
"rxjs": "^7.8.1",
64+
"sequelize": "^6.37.3",
65+
"sequelize-typescript": "^2.1.6",
6366
"sqlite3": "^5.1.7",
6467
"systeminformation": "^5.22.11",
65-
"typeorm": "^0.3.20",
6668
"ulid": "^2.3.0",
6769
"uuid": "^9.0.1",
6870
"whatwg-url": "^14.0.0",
@@ -72,13 +74,13 @@
7274
"@nestjs/cli": "^10.0.0",
7375
"@nestjs/schematics": "^10.0.0",
7476
"@nestjs/testing": "^10.0.0",
75-
"@nestjs/typeorm": "^10.0.2",
7677
"@types/cli-progress": "^3.11.5",
7778
"@types/decompress": "^4.2.7",
7879
"@types/express": "^4.17.17",
7980
"@types/jest": "^29.5.2",
8081
"@types/js-yaml": "^4.0.9",
8182
"@types/node": "^20.12.9",
83+
"@types/sequelize": "^4.28.20",
8284
"@types/supertest": "^6.0.2",
8385
"@types/update-notifier": "^6.0.8",
8486
"@types/uuid": "^9.0.8",

cortex-js/src/command.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { ModelRemoveCommand } from './infrastructure/commanders/models/model-rem
2020
import { RunCommand } from './infrastructure/commanders/shortcuts/run.command';
2121
import { ModelUpdateCommand } from './infrastructure/commanders/models/model-update.command';
2222
import { AssistantsModule } from './usecases/assistants/assistants.module';
23-
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
2423
import { MessagesModule } from './usecases/messages/messages.module';
2524
import { FileManagerModule } from './infrastructure/services/file-manager/file-manager.module';
2625
import { PSCommand } from './infrastructure/commanders/ps.command';
@@ -34,6 +33,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
3433
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
3534
import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command';
3635
import { ContextModule } from './infrastructure/services/context/context.module';
36+
import { CliUsecasesModule } from './infrastructure/commanders/usecases/cli.usecases.module';
3737
import { ExtensionsModule } from './extensions/extensions.module';
3838
import { ConfigsCommand } from './infrastructure/commanders/configs.command';
3939
import { EnginesCommand } from './infrastructure/commanders/engines.command';
@@ -46,6 +46,7 @@ import { EnginesListCommand } from './infrastructure/commanders/engines/engines-
4646
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
4747
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';
4848

49+
4950
@Module({
5051
imports: [
5152
ConfigModule.forRoot({

cortex-js/src/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function bootstrap() {
3939
contextService!.set('sessionId', anonymousData?.sessionId);
4040
telemetryUseCase!.sendActivationEvent(TelemetrySource.CLI);
4141
telemetryUseCase!.sendCrashReport();
42-
return CommandFactory.runApplication(app);
42+
await CommandFactory.runApplication(app);
4343
});
4444
}
4545

cortex-js/src/infrastructure/database/mysql-database.providers.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { AssistantEntity } from '@/infrastructure/entities/assistant.entity';
2-
import { DataSource } from 'typeorm';
2+
import { Sequelize } from 'sequelize-typescript';
33

44
export const assistantProviders = [
55
{
66
provide: 'ASSISTANT_REPOSITORY',
7-
useFactory: (dataSource: DataSource) =>
8-
dataSource.getRepository(AssistantEntity),
7+
useFactory: async(sequelize: Sequelize) =>{
8+
return sequelize.getRepository(AssistantEntity);
9+
},
910
inject: ['DATA_SOURCE'],
1011
},
1112
];
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { MessageEntity } from '@/infrastructure/entities/message.entity';
2-
import { DataSource } from 'typeorm';
1+
import { MessageEntity } from "@/infrastructure/entities/message.entity";
2+
import { Sequelize } from "sequelize-typescript";
33

44
export const messageProviders = [
55
{
66
provide: 'MESSAGE_REPOSITORY',
7-
useFactory: (dataSource: DataSource) =>
8-
dataSource.getRepository(MessageEntity),
7+
useFactory: async(sequelize: Sequelize) =>{
8+
return sequelize.getRepository(MessageEntity);
9+
},
910
inject: ['DATA_SOURCE'],
1011
},
1112
];
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { ThreadEntity } from '@/infrastructure/entities/thread.entity';
2-
import { DataSource } from 'typeorm';
2+
import { Sequelize } from 'sequelize-typescript';
33

44
export const threadProviders = [
55
{
66
provide: 'THREAD_REPOSITORY',
7-
useFactory: (dataSource: DataSource) =>
8-
dataSource.getRepository(ThreadEntity),
7+
useFactory: async(sequelize: Sequelize) =>{
8+
return sequelize.getRepository(ThreadEntity);
9+
},
910
inject: ['DATA_SOURCE'],
1011
},
1112
];
13+
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
22
import { databaseFile } from '@/infrastructure/constants/cortex';
33
import { join } from 'path';
4-
import { DataSource } from 'typeorm';
54
import { ThreadEntity } from '../entities/thread.entity';
6-
import { AssistantEntity } from '../entities/assistant.entity';
75
import { MessageEntity } from '../entities/message.entity';
6+
import { AssistantEntity } from '../entities/assistant.entity';
7+
import { Sequelize } from 'sequelize-typescript';
88

99
export const sqliteDatabaseProviders = [
1010
{
@@ -13,14 +13,13 @@ export const sqliteDatabaseProviders = [
1313
useFactory: async (fileManagerService: FileManagerService) => {
1414
const dataFolderPath = await fileManagerService.getDataFolderPath();
1515
const sqlitePath = join(dataFolderPath, databaseFile);
16-
const dataSource = new DataSource({
17-
type: 'sqlite',
18-
database: sqlitePath,
19-
synchronize: process.env.NODE_ENV !== 'production',
20-
entities: [ThreadEntity, AssistantEntity, MessageEntity],
16+
const sequelize = new Sequelize({
17+
dialect: 'sqlite',
18+
storage: sqlitePath,
19+
logging: false,
2120
});
22-
23-
return dataSource.initialize();
21+
sequelize.addModels([ThreadEntity, MessageEntity, AssistantEntity]);
22+
return sequelize;
2423
},
2524
},
2625
];
Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,90 @@
1+
import { Table, Column, Model, PrimaryKey, DataType } from 'sequelize-typescript';
12
import { Assistant } from '@/domain/models/assistant.interface';
23
import type {
34
AssistantToolResources,
45
AssistantResponseFormatOption,
56
} from '@/domain/models/assistant.interface';
6-
import { Column, Entity, PrimaryColumn } from 'typeorm';
77

8-
@Entity('assistants')
9-
export class AssistantEntity implements Assistant {
10-
@PrimaryColumn({ type: String })
8+
@Table({ tableName: 'assistants', timestamps: false})
9+
export class AssistantEntity extends Model implements Assistant {
10+
@PrimaryKey
11+
@Column({
12+
type: DataType.STRING,
13+
})
1114
id: string;
1215

13-
@Column({ type: String, nullable: true })
16+
@Column({
17+
type: DataType.STRING,
18+
allowNull: true,
19+
})
1420
avatar?: string;
1521

16-
@Column({ type: String })
22+
@Column({
23+
type: DataType.STRING,
24+
defaultValue: 'assistant',
25+
})
1726
object: 'assistant';
1827

19-
@Column({ type: Number })
28+
@Column({
29+
type: DataType.INTEGER,
30+
})
2031
created_at: number;
2132

22-
@Column({ type: String, nullable: true })
33+
@Column({
34+
type: DataType.STRING,
35+
allowNull: true,
36+
})
2337
name: string | null;
2438

25-
@Column({ type: String, nullable: true })
39+
@Column({
40+
type: DataType.STRING,
41+
allowNull: true,
42+
})
2643
description: string | null;
2744

28-
@Column({ type: String })
45+
@Column({
46+
type: DataType.STRING,
47+
})
2948
model: string;
3049

31-
@Column({ type: String, nullable: true })
50+
@Column({
51+
type: DataType.STRING,
52+
allowNull: true,
53+
})
3254
instructions: string | null;
3355

34-
@Column({ type: 'simple-json' })
56+
@Column({
57+
type: DataType.JSON,
58+
})
3559
tools: any;
3660

37-
@Column({ type: 'simple-json', nullable: true })
61+
@Column({
62+
type: DataType.JSON,
63+
allowNull: true,
64+
})
3865
metadata: any | null;
3966

40-
@Column({ type: Number, nullable: true })
67+
@Column({
68+
type: DataType.FLOAT,
69+
allowNull: true,
70+
})
4171
top_p: number | null;
4272

43-
@Column({ type: Number, nullable: true })
73+
@Column({
74+
type: DataType.FLOAT,
75+
allowNull: true,
76+
})
4477
temperature: number | null;
4578

46-
@Column({ type: 'simple-json', nullable: true })
79+
@Column({
80+
type: DataType.JSON,
81+
allowNull: true,
82+
})
4783
response_format: AssistantResponseFormatOption | null;
4884

49-
@Column({ type: 'simple-json', nullable: true })
85+
@Column({
86+
type: DataType.JSON,
87+
allowNull: true,
88+
})
5089
tool_resources: AssistantToolResources | null;
5190
}
Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,88 @@
1+
import { Table, Column, Model, PrimaryKey, DataType } from 'sequelize-typescript';
12
import type {
23
Message,
34
MessageContent,
45
MessageIncompleteDetails,
56
MessageAttachment,
67
} from '@/domain/models/message.interface';
7-
import { Column, Entity, PrimaryColumn } from 'typeorm';
88

9-
@Entity('messages')
10-
export class MessageEntity implements Message {
11-
@PrimaryColumn({ type: String })
9+
@Table({ tableName: 'messages', timestamps: false})
10+
export class MessageEntity extends Model implements Message {
11+
@PrimaryKey
12+
@Column({
13+
type: DataType.STRING,
14+
})
1215
id: string;
1316

14-
@Column({ type: String })
17+
@Column({
18+
type: DataType.STRING,
19+
defaultValue: 'thread.message',
20+
})
1521
object: 'thread.message';
1622

17-
@Column({ type: String })
23+
@Column({
24+
type: DataType.STRING,
25+
})
1826
thread_id: string;
1927

20-
@Column({ type: String, nullable: true })
28+
@Column({
29+
type: DataType.STRING,
30+
allowNull: true,
31+
})
2132
assistant_id: string | null;
2233

23-
@Column({ type: String })
34+
@Column({
35+
type: DataType.STRING,
36+
})
2437
role: 'user' | 'assistant';
2538

26-
@Column({ type: String })
39+
@Column({
40+
type: DataType.STRING,
41+
})
2742
status: 'in_progress' | 'incomplete' | 'completed';
2843

29-
@Column({ type: 'simple-json', nullable: true })
44+
@Column({
45+
type: DataType.JSON,
46+
allowNull: true,
47+
})
3048
metadata: any | null;
3149

32-
@Column({ type: String, nullable: true })
50+
@Column({
51+
type: DataType.STRING,
52+
allowNull: true,
53+
})
3354
run_id: string | null;
3455

35-
@Column({ type: Number, nullable: true })
56+
@Column({
57+
type: DataType.INTEGER,
58+
allowNull: true,
59+
})
3660
completed_at: number | null;
3761

38-
@Column({ type: 'simple-json' })
62+
@Column({
63+
type: DataType.JSON,
64+
})
3965
content: MessageContent[];
4066

41-
@Column({ type: 'simple-json', nullable: true })
67+
@Column({
68+
type: DataType.JSON,
69+
allowNull: true,
70+
})
4271
incomplete_details: MessageIncompleteDetails | null;
4372

44-
@Column({ type: Number })
73+
@Column({
74+
type: DataType.INTEGER,
75+
})
4576
created_at: number;
4677

47-
@Column({ type: 'simple-json' })
78+
@Column({
79+
type: DataType.JSON,
80+
})
4881
attachments: MessageAttachment[];
4982

50-
@Column({ type: Number, nullable: true })
83+
@Column({
84+
type: DataType.INTEGER,
85+
allowNull: true,
86+
})
5187
incomplete_at: number | null;
5288
}

0 commit comments

Comments
 (0)