feat: 更新AI教练控制器,增加用户聊天次数管理功能
- 在AI教练控制器中引入用户聊天次数的检查,确保用户在进行对话前有足够的聊天次数。 - 新增用户服务方法以获取和扣减用户的聊天次数,优化用户体验。 - 调整默认免费聊天次数为5次,提升系统的使用限制管理。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Query, Res, StreamableFile, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, HttpException, HttpStatus, Logger, Param, Post, Query, Res, StreamableFile, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBody, ApiQuery, ApiParam } from '@nestjs/swagger';
|
||||
import { Response } from 'express';
|
||||
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
||||
@@ -7,12 +7,17 @@ import { AccessTokenPayload } from '../users/services/apple-auth.service';
|
||||
import { AiCoachService } from './ai-coach.service';
|
||||
import { AiChatRequestDto, AiChatResponseDto, AiResponseDataDto } from './dto/ai-chat.dto';
|
||||
import { PostureAssessmentRequestDto, PostureAssessmentResponseDto } from './dto/posture-assessment.dto';
|
||||
import { UsersService } from '../users/users.service';
|
||||
|
||||
@ApiTags('ai-coach')
|
||||
@Controller('ai-coach')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class AiCoachController {
|
||||
constructor(private readonly aiCoachService: AiCoachService) { }
|
||||
private readonly logger = new Logger(AiCoachController.name);
|
||||
constructor(
|
||||
private readonly aiCoachService: AiCoachService,
|
||||
private readonly usersService: UsersService,
|
||||
) { }
|
||||
|
||||
@Post('chat')
|
||||
@ApiOperation({ summary: '流式大模型对话(普拉提教练)' })
|
||||
@@ -26,6 +31,13 @@ export class AiCoachController {
|
||||
const stream = body.stream !== false; // 默认流式
|
||||
const userContent = body.messages?.[body.messages.length - 1]?.content || '';
|
||||
|
||||
// 判断用户是否有聊天次数
|
||||
const usageCount = await this.usersService.getUserUsageCount(userId);
|
||||
if (usageCount <= 0) {
|
||||
this.logger.error(`chat: ${userId} has no usage count`);
|
||||
throw new HttpException('用户没有聊天次数', HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
// 创建或沿用会话ID,并保存用户消息
|
||||
const { conversationId } = await this.aiCoachService.createOrAppendMessages({
|
||||
userId,
|
||||
@@ -45,6 +57,8 @@ export class AiCoachController {
|
||||
confirmationData: body.confirmationData,
|
||||
});
|
||||
|
||||
await this.usersService.deductUserUsageCount(userId);
|
||||
|
||||
// 检查是否返回结构化数据(如确认选项)
|
||||
// 结构化数据必须使用非流式模式返回
|
||||
if (typeof result === 'object' && 'type' in result) {
|
||||
|
||||
Reference in New Issue
Block a user