| 1 | import { Body, Controller, Get, Post, Query, SetMetadata } from '@nestjs/common' |
| 2 | import { SSE_METADATA } from '@nestjs/common/constants' |
| 3 | import { ApiTags } from '@nestjs/swagger' |
| 4 | import { GetToken, Public, TokenInfo } from '@yikart/aitoearn-auth' |
| 5 | import { ApiDoc, UserType, ZodValidationPipe } from '@yikart/common' |
| 6 | import { AiLogChannel } from '@yikart/mongodb' |
| 7 | import { ChatCompletionDto, ChatStreamProxyDto, chatStreamProxyDtoSchema, ClaudeChatProxyDto, claudeChatProxyDtoSchema } from './chat.dto' |
| 8 | import { ChatService } from './chat.service' |
| 9 | import { chatCompletionChunkVoSchema, ChatCompletionVo, ChatModelConfigVo } from './chat.vo' |
| 10 | |
| 11 | @ApiTags('Me/Ai/Chat') |
| 12 | @Controller('ai') |
| 13 | export class ChatController { |
| 14 | constructor(private readonly chatService: ChatService) {} |
| 15 | |
| 16 | @ApiDoc({ |
| 17 | summary: 'Get Chat Model Parameters', |
| 18 | response: [ChatModelConfigVo], |
| 19 | }) |
| 20 | @Public() |
| 21 | @Get('/models/chat') |
| 22 | async getChatModels( |
| 23 | @GetToken() token?: TokenInfo, |
| 24 | @Query('channel') channel?: AiLogChannel, |
| 25 | @Query('scene') scene?: string, |
| 26 | ): Promise<ChatModelConfigVo[]> { |
| 27 | const response = await this.chatService.getChatModelConfig({ |
| 28 | userId: token?.id, |
| 29 | userType: UserType.User, |
| 30 | channel, |
| 31 | scene, |
| 32 | }) |
| 33 | return response.map(item => ChatModelConfigVo.create(item)) |
| 34 | } |
| 35 | |
| 36 | @ApiDoc({ |
| 37 | summary: 'AI Chat Conversation', |
| 38 | body: ChatCompletionDto.schema, |
| 39 | response: ChatCompletionVo, |
| 40 | }) |
| 41 | @Post('/chat') |
| 42 | async chat(@GetToken() token: TokenInfo, @Body() body: ChatCompletionDto): Promise<ChatCompletionVo> { |
| 43 | const response = await this.chatService.userChatCompletion({ |
| 44 | userId: token.id, |
| 45 | userType: UserType.User, |
| 46 | ...body, |
| 47 | }) |
| 48 | return ChatCompletionVo.create(response) |
| 49 | } |
| 50 | |
| 51 | @ApiDoc({ |
| 52 | summary: 'AI Chat Conversation (Stream)', |
| 53 | body: chatStreamProxyDtoSchema, |
| 54 | response: chatCompletionChunkVoSchema, |
| 55 | }) |
| 56 | @SetMetadata(SSE_METADATA, true) |
| 57 | @Post('/chat/stream') |
| 58 | async chatStream( |
| 59 | @GetToken() token: TokenInfo, |
| 60 | @Body(new ZodValidationPipe(chatStreamProxyDtoSchema)) body: ChatStreamProxyDto, |
| 61 | ) { |
| 62 | return await this.chatService.proxyChatStream({ |
| 63 | userId: token.id, |
| 64 | userType: UserType.User, |
| 65 | ...body, |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | @ApiDoc({ |
| 70 | summary: 'Claude Chat Conversation (Stream)', |
| 71 | body: claudeChatProxyDtoSchema, |
| 72 | }) |
| 73 | @SetMetadata(SSE_METADATA, true) |
| 74 | @Post('/chat/claude') |
| 75 | async claudeChatStream( |
| 76 | @GetToken() token: TokenInfo, |
| 77 | @Body(new ZodValidationPipe(claudeChatProxyDtoSchema)) body: ClaudeChatProxyDto, |
| 78 | ) { |
| 79 | return await this.chatService.proxyClaudeChatStream({ |
| 80 | userId: token.id, |
| 81 | userType: UserType.User, |
| 82 | ...body, |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 |