|
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + Body, |
| 4 | + Controller, |
| 5 | + Delete, |
| 6 | + Get, |
| 7 | + Put, |
| 8 | + UseGuards, |
| 9 | +} from '@nestjs/common'; |
| 10 | +import { |
| 11 | + ApiHeader, |
| 12 | + ApiOperation, |
| 13 | + ApiResponse, |
| 14 | + ApiSecurity, |
| 15 | + ApiTags, |
| 16 | +} from '@nestjs/swagger'; |
| 17 | +import { AuthContext } from '../auth/auth-context.decorator'; |
| 18 | +import { HybridAuthGuard } from '../auth/hybrid-auth.guard'; |
| 19 | +import type { AuthContext as AuthContextType } from '../auth/types'; |
| 20 | +import { SaveAssistantChatHistoryDto } from './assistant-chat.dto'; |
| 21 | +import { AssistantChatService } from './assistant-chat.service'; |
| 22 | +import type { AssistantChatMessage } from './assistant-chat.types'; |
| 23 | + |
| 24 | +@ApiTags('Assistant Chat') |
| 25 | +@Controller({ path: 'assistant-chat', version: '1' }) |
| 26 | +@UseGuards(HybridAuthGuard) |
| 27 | +@ApiSecurity('apikey') |
| 28 | +@ApiHeader({ |
| 29 | + name: 'X-Organization-Id', |
| 30 | + description: |
| 31 | + 'Organization ID (required for JWT auth, optional for API key auth)', |
| 32 | + required: false, |
| 33 | +}) |
| 34 | +export class AssistantChatController { |
| 35 | + constructor(private readonly assistantChatService: AssistantChatService) {} |
| 36 | + |
| 37 | + private getUserScopedContext(auth: AuthContextType): { organizationId: string; userId: string } { |
| 38 | + // Defensive checks (should already be guaranteed by HybridAuthGuard + AuthContext decorator) |
| 39 | + if (!auth.organizationId) { |
| 40 | + throw new BadRequestException('Organization ID is required'); |
| 41 | + } |
| 42 | + |
| 43 | + if (auth.isApiKey) { |
| 44 | + throw new BadRequestException( |
| 45 | + 'Assistant chat history is only available for user-authenticated requests (Bearer JWT).', |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + if (!auth.userId) { |
| 50 | + throw new BadRequestException('User ID is required'); |
| 51 | + } |
| 52 | + |
| 53 | + return { organizationId: auth.organizationId, userId: auth.userId }; |
| 54 | + } |
| 55 | + |
| 56 | + @Get('history') |
| 57 | + @ApiOperation({ |
| 58 | + summary: 'Get assistant chat history', |
| 59 | + description: |
| 60 | + 'Returns the current user-scoped assistant chat history (ephemeral session context).', |
| 61 | + }) |
| 62 | + @ApiResponse({ |
| 63 | + status: 200, |
| 64 | + description: 'Chat history retrieved', |
| 65 | + schema: { |
| 66 | + type: 'object', |
| 67 | + properties: { |
| 68 | + messages: { type: 'array', items: { type: 'object' } }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }) |
| 72 | + async getHistory(@AuthContext() auth: AuthContextType): Promise<{ messages: AssistantChatMessage[] }> { |
| 73 | + const { organizationId, userId } = this.getUserScopedContext(auth); |
| 74 | + |
| 75 | + const messages = await this.assistantChatService.getHistory({ |
| 76 | + organizationId, |
| 77 | + userId, |
| 78 | + }); |
| 79 | + |
| 80 | + return { messages }; |
| 81 | + } |
| 82 | + |
| 83 | + @Put('history') |
| 84 | + @ApiOperation({ |
| 85 | + summary: 'Save assistant chat history', |
| 86 | + description: |
| 87 | + 'Replaces the current user-scoped assistant chat history (ephemeral session context).', |
| 88 | + }) |
| 89 | + async saveHistory( |
| 90 | + @AuthContext() auth: AuthContextType, |
| 91 | + @Body() dto: SaveAssistantChatHistoryDto, |
| 92 | + ): Promise<{ success: true }> { |
| 93 | + const { organizationId, userId } = this.getUserScopedContext(auth); |
| 94 | + |
| 95 | + await this.assistantChatService.saveHistory( |
| 96 | + { organizationId, userId }, |
| 97 | + dto.messages, |
| 98 | + ); |
| 99 | + |
| 100 | + return { success: true }; |
| 101 | + } |
| 102 | + |
| 103 | + @Delete('history') |
| 104 | + @ApiOperation({ |
| 105 | + summary: 'Clear assistant chat history', |
| 106 | + description: 'Deletes the current user-scoped assistant chat history.', |
| 107 | + }) |
| 108 | + async clearHistory(@AuthContext() auth: AuthContextType): Promise<{ success: true }> { |
| 109 | + const { organizationId, userId } = this.getUserScopedContext(auth); |
| 110 | + |
| 111 | + await this.assistantChatService.clearHistory({ |
| 112 | + organizationId, |
| 113 | + userId, |
| 114 | + }); |
| 115 | + |
| 116 | + return { success: true }; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
0 commit comments