Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace typeorm by sequelize #856

Merged
merged 65 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
69a02a0
debug ps command
marknguyen1302 Jul 1, 2024
c85031c
debug ps command
marknguyen1302 Jul 1, 2024
6a99270
debug ps command
marknguyen1302 Jul 1, 2024
6b25c4e
debug ps command
marknguyen1302 Jul 1, 2024
92d12ae
debug ps command
marknguyen1302 Jul 1, 2024
3552019
debug ps command
marknguyen1302 Jul 1, 2024
112b3ac
revert debug
marknguyen1302 Jul 1, 2024
5b83a7a
revert debug
marknguyen1302 Jul 1, 2024
2aa5276
remove database module
marknguyen1302 Jul 1, 2024
7e664a4
remove database module
marknguyen1302 Jul 1, 2024
582e4e6
remove telemetry
marknguyen1302 Jul 1, 2024
6c0d5db
remove telemetry
marknguyen1302 Jul 1, 2024
a5e67f7
remove telemetry
marknguyen1302 Jul 1, 2024
400be00
remove telemetry
marknguyen1302 Jul 1, 2024
740b208
remove telemetry
marknguyen1302 Jul 1, 2024
32e6a8b
test
marknguyen1302 Jul 1, 2024
000bdf9
test
marknguyen1302 Jul 1, 2024
120e665
test
marknguyen1302 Jul 1, 2024
093fb3e
test
marknguyen1302 Jul 1, 2024
5382765
test
marknguyen1302 Jul 1, 2024
21ba088
test
marknguyen1302 Jul 1, 2024
bbacb44
test
marknguyen1302 Jul 1, 2024
9dd5871
a
marknguyen1302 Jul 1, 2024
aac4b91
debug import time
marknguyen1302 Jul 1, 2024
ee70206
debug import db module time
marknguyen1302 Jul 1, 2024
f3b47a8
debug import db module time
marknguyen1302 Jul 1, 2024
7ca3201
debug telemetry import time
marknguyen1302 Jul 1, 2024
b52a5c5
debug telemetry import time
marknguyen1302 Jul 1, 2024
6d5d899
debug telemetry import time
marknguyen1302 Jul 1, 2024
c6cb079
debug telemetry import time
marknguyen1302 Jul 1, 2024
edf6f40
remove some import
marknguyen1302 Jul 1, 2024
f0a4a73
remove filemanager
marknguyen1302 Jul 1, 2024
84ab940
remove filemanager
marknguyen1302 Jul 1, 2024
9e68ba6
remove filemanager
marknguyen1302 Jul 1, 2024
4a0b46b
remove filemanager
marknguyen1302 Jul 1, 2024
3a47dee
remove filemanager
marknguyen1302 Jul 2, 2024
781498c
revert database
marknguyen1302 Jul 2, 2024
f2b1664
revert database
marknguyen1302 Jul 2, 2024
7e41125
remove load extension
marknguyen1302 Jul 2, 2024
2dcf854
remove load extension
marknguyen1302 Jul 2, 2024
1eec8cb
debug load module
marknguyen1302 Jul 2, 2024
7f0a2a1
comment all module
marknguyen1302 Jul 2, 2024
099ca97
comment all module
marknguyen1302 Jul 2, 2024
bd121bc
comment all module
marknguyen1302 Jul 2, 2024
6ba87a4
comment all module
marknguyen1302 Jul 2, 2024
a8fa3dd
comment all module
marknguyen1302 Jul 2, 2024
cdbba33
comment all module
marknguyen1302 Jul 2, 2024
7ceb8ab
comment all module
marknguyen1302 Jul 2, 2024
b8955a3
please work
marknguyen1302 Jul 2, 2024
375b30d
please work
marknguyen1302 Jul 2, 2024
70c8339
please work
marknguyen1302 Jul 2, 2024
1997e43
please work
marknguyen1302 Jul 2, 2024
ccb2017
add time require
marknguyen1302 Jul 2, 2024
d57830c
add time require package
marknguyen1302 Jul 2, 2024
0989c3a
remove type orm
marknguyen1302 Jul 3, 2024
f0af975
update package.json
marknguyen1302 Jul 3, 2024
81baf88
add traces
marknguyen1302 Jul 3, 2024
2bf81c5
replace typeorm to sequelize
marknguyen1302 Jul 8, 2024
19df8fe
clean up
marknguyen1302 Jul 8, 2024
858c019
remove debug packages
marknguyen1302 Jul 8, 2024
4b87d7f
fix error
marknguyen1302 Jul 8, 2024
d83677d
clean up
marknguyen1302 Jul 8, 2024
498d25d
revert debug change
marknguyen1302 Jul 8, 2024
d4ffcbb
merge dev
marknguyen1302 Jul 8, 2024
8580774
change naming
marknguyen1302 Jul 8, 2024
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
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
Loading