|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + Headers, |
| 7 | + Param, |
| 8 | + Post, |
| 9 | + Req, |
| 10 | + UnauthorizedException, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { ConfigService } from '@nestjs/config'; |
| 13 | +import { JwtService } from '@nestjs/jwt'; |
| 14 | +import type { Request } from 'express'; |
| 15 | +import type { AuthUser } from '../auth/types/auth-user.type'; |
| 16 | +import { AiChatService } from './ai-chat.service'; |
| 17 | +import { SendAiChatMessageDto } from './dto/send-ai-chat-message.dto'; |
| 18 | +import { StartAiChatSessionDto } from './dto/start-ai-chat-session.dto'; |
| 19 | + |
| 20 | +@Controller('ai-chat') |
| 21 | +export class AiChatController { |
| 22 | + constructor( |
| 23 | + private readonly aiChatService: AiChatService, |
| 24 | + private readonly configService: ConfigService, |
| 25 | + private readonly jwtService: JwtService, |
| 26 | + ) {} |
| 27 | + |
| 28 | + @Get('status') |
| 29 | + getStatus(@Headers('x-trace-id') traceId?: string) { |
| 30 | + return this.aiChatService.getStatus(traceId); |
| 31 | + } |
| 32 | + |
| 33 | + @Post('session') |
| 34 | + startSession( |
| 35 | + @Req() req: Request, |
| 36 | + @Body() dto: StartAiChatSessionDto, |
| 37 | + @Headers('x-trace-id') traceId?: string, |
| 38 | + ) { |
| 39 | + const user = this.resolveUser(req); |
| 40 | + return this.aiChatService.startSession(user, dto, traceId); |
| 41 | + } |
| 42 | + |
| 43 | + @Post('messages') |
| 44 | + sendMessage( |
| 45 | + @Req() req: Request, |
| 46 | + @Body() dto: SendAiChatMessageDto, |
| 47 | + @Headers('x-trace-id') traceId?: string, |
| 48 | + ) { |
| 49 | + const user = this.resolveUser(req); |
| 50 | + return this.aiChatService.sendMessage(user, dto, traceId); |
| 51 | + } |
| 52 | + |
| 53 | + @Delete('session/:sessionId') |
| 54 | + closeSession( |
| 55 | + @Req() req: Request, |
| 56 | + @Param('sessionId') sessionId: string, |
| 57 | + @Headers('x-trace-id') traceId?: string, |
| 58 | + ) { |
| 59 | + const user = this.resolveUser(req); |
| 60 | + return this.aiChatService.closeSession(user, sessionId, traceId); |
| 61 | + } |
| 62 | + |
| 63 | + private resolveUser(req: Request): AuthUser { |
| 64 | + const token = this.readSignedCookie(req, 'access_token'); |
| 65 | + |
| 66 | + if (token) { |
| 67 | + try { |
| 68 | + const payload = this.jwtService.verify<{ |
| 69 | + sub: string; |
| 70 | + email: string; |
| 71 | + role: AuthUser['role']; |
| 72 | + }>(token, { |
| 73 | + secret: this.configService.getOrThrow<string>('JWT_SECRET'), |
| 74 | + }); |
| 75 | + |
| 76 | + return { |
| 77 | + id: payload.sub, |
| 78 | + email: payload.email, |
| 79 | + role: payload.role, |
| 80 | + }; |
| 81 | + } catch { |
| 82 | + if (!this.isDevModeEnabled()) { |
| 83 | + throw new UnauthorizedException('Invalid access token'); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (this.isDevModeEnabled()) { |
| 89 | + return { |
| 90 | + id: |
| 91 | + this.configService.get<string>('AI_CHAT_DEV_USER_ID') || |
| 92 | + 'local-ai-demo', |
| 93 | + email: |
| 94 | + this.configService.get<string>('AI_CHAT_DEV_EMAIL') || |
| 95 | + 'local-ai-demo@localhost', |
| 96 | + role: null, |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + throw new UnauthorizedException('Sign in required'); |
| 101 | + } |
| 102 | + |
| 103 | + private readSignedCookie( |
| 104 | + req: Request, |
| 105 | + cookieName: string, |
| 106 | + ): string | undefined { |
| 107 | + const signedCookies = req.signedCookies as |
| 108 | + | Record<string, string> |
| 109 | + | undefined; |
| 110 | + |
| 111 | + return signedCookies?.[cookieName]; |
| 112 | + } |
| 113 | + |
| 114 | + private isDevModeEnabled(): boolean { |
| 115 | + const rawValue = ( |
| 116 | + this.configService.get<string>('AI_CHAT_DEV_MODE') || '' |
| 117 | + ).toLowerCase(); |
| 118 | + |
| 119 | + return ['1', 'true', 'yes', 'on'].includes(rawValue); |
| 120 | + } |
| 121 | +} |
0 commit comments