Skip to content

Commit 2339996

Browse files
committed
the modules with controllers were modified to be dynamic
1 parent 461868c commit 2339996

File tree

13 files changed

+18130
-20353
lines changed

13 files changed

+18130
-20353
lines changed

apps/api/src/app/chat/chat.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ export const assistantParams: AssistantCreateParams = {
1313
export const assistantConfig: AssistantConfigParams = {
1414
id: process.env['ASSISTANT_ID'] || '',
1515
params: assistantParams,
16+
assistantPrefix: 'assistant-prefix',
1617
filesDir: './apps/api/src/app/knowledge',
1718
toolResources: {
1819
fileSearch: {
19-
fileNames: ['33-things-to-ask-your-digital-product-development-partner.md'],
20+
fileNames: [
21+
'33-things-to-ask-your-digital-product-development-partner.md',
22+
],
2023
},
2124
codeInterpreter: {
2225
fileNames: [],

apps/api/src/app/chat/chat.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AgentsModule } from './agents/agents.module';
55
import { ChatSockets } from './chat.sockets';
66

77
@Module({
8-
imports: [AgentsModule, AssistantModule.forRoot(assistantConfig)],
8+
imports: [AgentsModule, AssistantModule.register(assistantConfig)],
99
providers: [ChatSockets],
1010
})
1111
export class ChatModule {}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule, Module } from '@nestjs/common';
22
import { AiService } from './ai.service';
33
import { AiController } from './ai.controller';
4+
import { createControllerWithPrefix } from '../../utils/controllers';
45

56
@Module({
67
providers: [AiService],
78
controllers: [AiController],
89
exports: [AiService],
910
})
10-
export class AiModule {}
11+
export class AiModule {
12+
static register(prefix: string): DynamicModule {
13+
return {
14+
module: AiModule,
15+
providers: [
16+
AiService,
17+
{
18+
provide: 'PREFIX',
19+
useValue: prefix,
20+
},
21+
],
22+
controllers: [createControllerWithPrefix(AiController, prefix)],
23+
exports: [AiService],
24+
};
25+
}
26+
}

libs/openai-assistant/src/lib/assistant/assistant-files.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createReadStream } from 'fs';
44
import { AiService } from '../ai';
55
import { ConfigService } from '../config';
66
import { AssistantCreateParams, VectorStore } from 'openai/resources/beta';
7-
import ToolResources = AssistantCreateParams.ToolResources;
87
import { AssistantUpdateParams } from 'openai/resources/beta/assistants';
98
import {
109
AssistantCodeInterpreter,
@@ -41,7 +40,7 @@ export class AssistantFilesService {
4140
async getCodeInterpreterResources(
4241
data: AssistantCodeInterpreter,
4342
fileDir = '',
44-
): Promise<ToolResources.CodeInterpreter> {
43+
): Promise<AssistantCreateParams.ToolResources.CodeInterpreter> {
4544
const files = await this.getFiles(data?.fileNames, fileDir);
4645

4746
return { file_ids: files.map(({ id }) => id) };
@@ -50,7 +49,7 @@ export class AssistantFilesService {
5049
async getFileSearchResources(
5150
data: AssistantFileSearch,
5251
fileDir = '',
53-
): Promise<ToolResources.FileSearch> {
52+
): Promise<AssistantCreateParams.ToolResources.FileSearch> {
5453
if (!data) {
5554
return { vector_store_ids: [] };
5655
}

libs/openai-assistant/src/lib/assistant/assistant.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface AssistantConfigParams {
66
params: AssistantCreateParams;
77
options?: RequestOptions;
88
filesDir?: string;
9+
assistantPrefix?: string;
910
toolResources?: AssistantToolResources | null;
1011
}
1112

libs/openai-assistant/src/lib/assistant/assistant.module.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,14 @@ const sharedServices = [
2626
AssistantMemoryService,
2727
];
2828

29-
const sharedModules = [
29+
const sharedModulesWithoutControllers = [AgentModule, RunModule];
30+
const sharedModulesWithControllers = [
3031
AiModule,
31-
AgentModule,
32-
RunModule,
33-
FilesModule,
3432
ThreadsModule,
33+
FilesModule,
3534
ChatModule,
3635
];
3736

38-
@Module({
39-
imports: [ConfigModule, HttpModule, ...sharedModules],
40-
providers: [...sharedServices],
41-
exports: [...sharedServices, ...sharedModules],
42-
})
4337
export class AssistantModule implements OnModuleInit {
4438
constructor(
4539
private readonly assistantService: AssistantService,
@@ -56,14 +50,29 @@ export class AssistantModule implements OnModuleInit {
5650
await this.assistantService.init();
5751
}
5852

59-
static forRoot(config: AssistantConfigParams): DynamicModule {
53+
static register(config: AssistantConfigParams): DynamicModule {
6054
return {
6155
module: AssistantModule,
56+
imports: [
57+
ConfigModule,
58+
HttpModule,
59+
...sharedModulesWithoutControllers,
60+
...sharedModulesWithControllers.map(module =>
61+
module.register(config?.assistantPrefix || ''),
62+
),
63+
],
6264
providers: [
6365
{
6466
provide: 'config',
6567
useValue: config,
6668
},
69+
...sharedServices,
70+
],
71+
72+
exports: [
73+
...sharedServices,
74+
...sharedModulesWithoutControllers,
75+
...sharedModulesWithControllers,
6776
],
6877
};
6978
}

libs/openai-assistant/src/lib/chat/chat.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Body, Controller, Post } from '@nestjs/common';
2+
import { Type } from '@nestjs/common/interfaces';
3+
24
import { ApiBody, ApiResponse, ApiTags } from '@nestjs/swagger';
35
import { ChatCallDto, ChatCallResponseDto } from './chat.model';
46
import { ChatService } from './chat.service';
57

68
@ApiTags('Chat')
7-
@Controller('assistant/chat')
9+
@Controller(`assistant/chat`)
810
export class ChatController {
911
constructor(public readonly chatsService: ChatService) {}
1012

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule } from '@nestjs/common';
22
import { AiModule } from '../ai';
33
import { RunModule } from '../run';
44
import { ChatHelpers } from './chat.helpers';
55
import { ChatService } from './chat.service';
66
import { SocketModule } from '@nestjs/websockets/socket-module';
77
import { ChatController } from './chat.controller';
8+
import { createControllerWithPrefix } from '../../utils/controllers';
89

910
export const sharedServices = [ChatHelpers, ChatService];
1011

11-
@Module({
12-
imports: [SocketModule, AiModule, RunModule],
13-
providers: [...sharedServices],
14-
controllers: [ChatController],
15-
exports: [...sharedServices],
16-
})
17-
export class ChatModule {}
12+
export class ChatModule {
13+
static register(prefix: string): DynamicModule {
14+
return {
15+
module: ChatModule,
16+
imports: [SocketModule, AiModule, RunModule],
17+
providers: [
18+
...sharedServices,
19+
{
20+
provide: 'PREFIX',
21+
useValue: prefix,
22+
},
23+
],
24+
controllers: [createControllerWithPrefix(ChatController, prefix)],
25+
exports: [...sharedServices],
26+
};
27+
}
28+
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule, Module } from '@nestjs/common';
22
import { FilesService } from './files.service';
33
import { FilesController } from './files.controller';
44
import { AiModule } from '../ai';
5+
import { createControllerWithPrefix } from '../../utils/controllers';
56

67
@Module({
78
imports: [AiModule],
89
providers: [FilesService],
910
controllers: [FilesController],
1011
exports: [FilesService],
1112
})
12-
export class FilesModule {}
13+
export class FilesModule {
14+
static register(prefix: string): DynamicModule {
15+
return {
16+
module: FilesModule,
17+
providers: [
18+
AiModule,
19+
{
20+
provide: 'PREFIX',
21+
useValue: prefix,
22+
},
23+
],
24+
controllers: [createControllerWithPrefix(FilesController, prefix)],
25+
exports: [FilesService],
26+
};
27+
}
28+
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule, Module } from '@nestjs/common';
22
import { ThreadsController } from './threads.controller';
33
import { ThreadsService } from './threads.service';
44
import { AiModule } from '../ai';
5+
import { createControllerWithPrefix } from '../../utils/controllers';
56

67
@Module({
78
imports: [AiModule],
89
providers: [ThreadsService],
910
controllers: [ThreadsController],
1011
exports: [ThreadsService],
1112
})
12-
export class ThreadsModule {}
13+
export class ThreadsModule {
14+
static register(prefix: string): DynamicModule {
15+
return {
16+
module: ThreadsModule,
17+
providers: [
18+
ThreadsService,
19+
{
20+
provide: 'PREFIX',
21+
useValue: prefix,
22+
},
23+
],
24+
controllers: [createControllerWithPrefix(ThreadsController, prefix)],
25+
exports: [ThreadsService],
26+
};
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { createControllerWithPrefix } from './controllers';
3+
import 'reflect-metadata';
4+
5+
@Controller('test')
6+
class TestController {
7+
@Get()
8+
getTest(): string {
9+
return 'Original Controller';
10+
}
11+
}
12+
13+
describe('createControllerWithPrefix', () => {
14+
it('should create a new controller with the prefixed route', () => {
15+
const PrefixedController = createControllerWithPrefix(
16+
TestController,
17+
'api',
18+
);
19+
20+
const originalPath = Reflect.getMetadata('path', TestController);
21+
const prefixedPath = Reflect.getMetadata('path', PrefixedController);
22+
23+
expect(originalPath).toBe('test');
24+
25+
expect(prefixedPath).toBe('api/test');
26+
});
27+
28+
it('should throw an error if the controller does not have @Controller decorator', () => {
29+
class InvalidController {}
30+
31+
expect(() => createControllerWithPrefix(InvalidController, 'api')).toThrow(
32+
'The provided controller does not have a @Controller decorator.',
33+
);
34+
});
35+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Controller } from '@nestjs/common';
2+
import { Type } from '@nestjs/common/interfaces';
3+
import 'reflect-metadata';
4+
5+
export function createControllerWithPrefix<T extends object>(
6+
controller: Type<T>,
7+
prefix: string,
8+
): Type<T> {
9+
const originalPath = Reflect.getMetadata('path', controller) as
10+
| string
11+
| undefined;
12+
13+
if (!originalPath) {
14+
throw new Error(
15+
'The provided controller does not have a @Controller decorator.',
16+
);
17+
}
18+
19+
const DynamicController = class extends controller {};
20+
21+
Controller(`${prefix}/${originalPath}`)(DynamicController);
22+
23+
return DynamicController;
24+
}

0 commit comments

Comments
 (0)