|
| 1 | +import { Body, Controller, Inject, Injectable, Post, Res, UseInterceptors } from '@nestjs/common'; |
| 2 | +import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 3 | +import { SkipThrottle } from '@nestjs/throttler'; |
| 4 | +import { Response } from 'express'; |
| 5 | +import { UseCaseType } from '../../common/data-injection.tokens.js'; |
| 6 | +import { SlugUuid } from '../../decorators/slug-uuid.decorator.js'; |
| 7 | +import { Timeout, TimeoutDefaults } from '../../decorators/timeout.decorator.js'; |
| 8 | +import { InTransactionEnum } from '../../enums/in-transaction.enum.js'; |
| 9 | +import { isTest } from '../../helpers/app/is-test.js'; |
| 10 | +import { SentryInterceptor } from '../../interceptors/sentry.interceptor.js'; |
| 11 | +import { |
| 12 | + AiConnectionContextRO, |
| 13 | + AiQueryResultRO, |
| 14 | + PermissionAllowedRO, |
| 15 | + ValidatedUserTokenRO, |
| 16 | +} from './data-structures/agents-responses.ds.js'; |
| 17 | +import { |
| 18 | + AiDataRequestBaseDto, |
| 19 | + ExecuteAiAggregationPipelineDto, |
| 20 | + ExecuteAiRawQueryDto, |
| 21 | + GetAiTableStructureDto, |
| 22 | +} from './dto/agents-ai-data.dtos.js'; |
| 23 | +import { ValidateConnectionEditDto, ValidateTableAiRequestDto, ValidateUserTokenDto } from './dto/agents-auth.dtos.js'; |
| 24 | +import { |
| 25 | + IExecuteAiAggregationPipeline, |
| 26 | + IExecuteAiRawQuery, |
| 27 | + IGetAiConnectionContext, |
| 28 | + IGetAiTableStructure, |
| 29 | + IScanAndCreateSettings, |
| 30 | + IValidateConnectionEdit, |
| 31 | + IValidateTableAiRequest, |
| 32 | + IValidateUserToken, |
| 33 | +} from './use-cases/agents-use-cases.interface.js'; |
| 34 | + |
| 35 | +@UseInterceptors(SentryInterceptor) |
| 36 | +@SkipThrottle() |
| 37 | +@Timeout() |
| 38 | +@ApiTags('agents microservice') |
| 39 | +@Controller('internal/agents') |
| 40 | +@Injectable() |
| 41 | +export class AgentsController { |
| 42 | + constructor( |
| 43 | + @Inject(UseCaseType.AGENTS_VALIDATE_USER_TOKEN) |
| 44 | + private readonly validateUserTokenUseCase: IValidateUserToken, |
| 45 | + @Inject(UseCaseType.AGENTS_VALIDATE_TABLE_AI_REQUEST) |
| 46 | + private readonly validateTableAiRequestUseCase: IValidateTableAiRequest, |
| 47 | + @Inject(UseCaseType.AGENTS_VALIDATE_CONNECTION_EDIT) |
| 48 | + private readonly validateConnectionEditUseCase: IValidateConnectionEdit, |
| 49 | + @Inject(UseCaseType.AGENTS_GET_AI_CONNECTION_CONTEXT) |
| 50 | + private readonly getAiConnectionContextUseCase: IGetAiConnectionContext, |
| 51 | + @Inject(UseCaseType.AGENTS_GET_AI_TABLE_STRUCTURE) |
| 52 | + private readonly getAiTableStructureUseCase: IGetAiTableStructure, |
| 53 | + @Inject(UseCaseType.AGENTS_EXECUTE_AI_RAW_QUERY) |
| 54 | + private readonly executeAiRawQueryUseCase: IExecuteAiRawQuery, |
| 55 | + @Inject(UseCaseType.AGENTS_EXECUTE_AI_AGGREGATION_PIPELINE) |
| 56 | + private readonly executeAiAggregationPipelineUseCase: IExecuteAiAggregationPipeline, |
| 57 | + @Inject(UseCaseType.AGENTS_SCAN_AND_CREATE_SETTINGS) |
| 58 | + private readonly scanAndCreateSettingsUseCase: IScanAndCreateSettings, |
| 59 | + ) {} |
| 60 | + |
| 61 | + @ApiOperation({ summary: 'Validate an end-user JWT on behalf of the agents microservice' }) |
| 62 | + @ApiResponse({ status: 201, type: ValidatedUserTokenRO }) |
| 63 | + @ApiBody({ type: ValidateUserTokenDto }) |
| 64 | + @Post('/auth/validate-user-token') |
| 65 | + public async validateUserToken(@Body() body: ValidateUserTokenDto): Promise<ValidatedUserTokenRO> { |
| 66 | + return await this.validateUserTokenUseCase.execute(body.token, InTransactionEnum.OFF); |
| 67 | + } |
| 68 | + |
| 69 | + @ApiOperation({ summary: 'Check Cedar permission for an AI request on a table' }) |
| 70 | + @ApiResponse({ status: 201, type: PermissionAllowedRO }) |
| 71 | + @ApiBody({ type: ValidateTableAiRequestDto }) |
| 72 | + @Post('/auth/validate-table-ai-request') |
| 73 | + public async validateTableAiRequest(@Body() body: ValidateTableAiRequestDto): Promise<PermissionAllowedRO> { |
| 74 | + return await this.validateTableAiRequestUseCase.execute( |
| 75 | + { userId: body.userId, connectionId: body.connectionId, tableName: body.tableName }, |
| 76 | + InTransactionEnum.OFF, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @ApiOperation({ summary: 'Check Cedar permission for editing a connection' }) |
| 81 | + @ApiResponse({ status: 201, type: PermissionAllowedRO }) |
| 82 | + @ApiBody({ type: ValidateConnectionEditDto }) |
| 83 | + @Post('/auth/validate-connection-edit') |
| 84 | + public async validateConnectionEdit(@Body() body: ValidateConnectionEditDto): Promise<PermissionAllowedRO> { |
| 85 | + return await this.validateConnectionEditUseCase.execute( |
| 86 | + { userId: body.userId, connectionId: body.connectionId }, |
| 87 | + InTransactionEnum.OFF, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @ApiOperation({ summary: 'Get AI-relevant connection context (type, schema, MongoDB flag)' }) |
| 92 | + @ApiResponse({ status: 201, type: AiConnectionContextRO }) |
| 93 | + @ApiBody({ type: AiDataRequestBaseDto }) |
| 94 | + @Post('/ai/data/:connectionId/context') |
| 95 | + public async getAiConnectionContext( |
| 96 | + @SlugUuid('connectionId') connectionId: string, |
| 97 | + @Body() body: AiDataRequestBaseDto, |
| 98 | + ): Promise<AiConnectionContextRO> { |
| 99 | + return await this.getAiConnectionContextUseCase.execute( |
| 100 | + { connectionId, userId: body.userId, masterPassword: body.masterPassword ?? null }, |
| 101 | + InTransactionEnum.OFF, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + @ApiOperation({ summary: 'Get permission-aware table structure for the AI tool loop' }) |
| 106 | + @ApiResponse({ status: 201, description: 'Table structure with related tables.' }) |
| 107 | + @ApiBody({ type: GetAiTableStructureDto }) |
| 108 | + @Timeout(!isTest() ? TimeoutDefaults.EXTENDED : TimeoutDefaults.EXTENDED_TEST) |
| 109 | + @Post('/ai/data/:connectionId/table-structure') |
| 110 | + public async getAiTableStructure( |
| 111 | + @SlugUuid('connectionId') connectionId: string, |
| 112 | + @Body() body: GetAiTableStructureDto, |
| 113 | + ): Promise<Record<string, unknown>> { |
| 114 | + return await this.getAiTableStructureUseCase.execute( |
| 115 | + { |
| 116 | + connectionId, |
| 117 | + userId: body.userId, |
| 118 | + masterPassword: body.masterPassword ?? null, |
| 119 | + tableName: body.tableName, |
| 120 | + }, |
| 121 | + InTransactionEnum.OFF, |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @ApiOperation({ summary: 'Validate and execute a read-only SQL query for the AI tool loop' }) |
| 126 | + @ApiResponse({ status: 201, type: AiQueryResultRO }) |
| 127 | + @ApiBody({ type: ExecuteAiRawQueryDto }) |
| 128 | + @Timeout(!isTest() ? TimeoutDefaults.EXTENDED : TimeoutDefaults.EXTENDED_TEST) |
| 129 | + @Post('/ai/data/:connectionId/raw-query') |
| 130 | + public async executeAiRawQuery( |
| 131 | + @SlugUuid('connectionId') connectionId: string, |
| 132 | + @Body() body: ExecuteAiRawQueryDto, |
| 133 | + ): Promise<AiQueryResultRO> { |
| 134 | + return await this.executeAiRawQueryUseCase.execute( |
| 135 | + { |
| 136 | + connectionId, |
| 137 | + userId: body.userId, |
| 138 | + masterPassword: body.masterPassword ?? null, |
| 139 | + tableName: body.tableName, |
| 140 | + query: body.query, |
| 141 | + }, |
| 142 | + InTransactionEnum.OFF, |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + @ApiOperation({ summary: 'Validate and execute a read-only MongoDB aggregation pipeline for the AI tool loop' }) |
| 147 | + @ApiResponse({ status: 201, type: AiQueryResultRO }) |
| 148 | + @ApiBody({ type: ExecuteAiAggregationPipelineDto }) |
| 149 | + @Timeout(!isTest() ? TimeoutDefaults.EXTENDED : TimeoutDefaults.EXTENDED_TEST) |
| 150 | + @Post('/ai/data/:connectionId/aggregation-pipeline') |
| 151 | + public async executeAiAggregationPipeline( |
| 152 | + @SlugUuid('connectionId') connectionId: string, |
| 153 | + @Body() body: ExecuteAiAggregationPipelineDto, |
| 154 | + ): Promise<AiQueryResultRO> { |
| 155 | + return await this.executeAiAggregationPipelineUseCase.execute( |
| 156 | + { |
| 157 | + connectionId, |
| 158 | + userId: body.userId, |
| 159 | + masterPassword: body.masterPassword ?? null, |
| 160 | + tableName: body.tableName, |
| 161 | + pipeline: body.pipeline, |
| 162 | + }, |
| 163 | + InTransactionEnum.OFF, |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + @ApiOperation({ summary: 'Run the AI settings/widgets scan, streaming progress chunks' }) |
| 168 | + @ApiResponse({ status: 201, description: 'Streams progress as newline-delimited JSON chunks.' }) |
| 169 | + @ApiBody({ type: AiDataRequestBaseDto }) |
| 170 | + @Timeout(!isTest() ? TimeoutDefaults.AI : TimeoutDefaults.AI_TEST) |
| 171 | + @Post('/ai/data/:connectionId/settings-scan') |
| 172 | + public async scanAndCreateSettings( |
| 173 | + @SlugUuid('connectionId') connectionId: string, |
| 174 | + @Body() body: AiDataRequestBaseDto, |
| 175 | + @Res({ passthrough: true }) response: Response, |
| 176 | + ): Promise<void> { |
| 177 | + return await this.scanAndCreateSettingsUseCase.execute( |
| 178 | + { |
| 179 | + connectionId, |
| 180 | + userId: body.userId, |
| 181 | + masterPassword: body.masterPassword ?? null, |
| 182 | + response, |
| 183 | + }, |
| 184 | + InTransactionEnum.OFF, |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments