feat(ai): 添加AI报告生成历史记录功能,支持每日生成限制和双API提供商
This commit is contained in:
@@ -8,12 +8,14 @@ import { AiReportService } from './services/ai-report.service';
|
||||
import { AiMessage } from './models/ai-message.model';
|
||||
import { AiConversation } from './models/ai-conversation.model';
|
||||
import { PostureAssessment } from './models/posture-assessment.model';
|
||||
import { AiReportHistory } from './models/ai-report-history.model';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { DietRecordsModule } from '../diet-records/diet-records.module';
|
||||
import { MedicationsModule } from '../medications/medications.module';
|
||||
import { WorkoutsModule } from '../workouts/workouts.module';
|
||||
import { MoodCheckinsModule } from '../mood-checkins/mood-checkins.module';
|
||||
import { WaterRecordsModule } from '../water-records/water-records.module';
|
||||
import { ChallengesModule } from '../challenges/challenges.module';
|
||||
import { CosService } from '../users/cos.service';
|
||||
|
||||
@Module({
|
||||
@@ -25,7 +27,8 @@ import { CosService } from '../users/cos.service';
|
||||
forwardRef(() => WorkoutsModule),
|
||||
forwardRef(() => MoodCheckinsModule),
|
||||
forwardRef(() => WaterRecordsModule),
|
||||
SequelizeModule.forFeature([AiConversation, AiMessage, PostureAssessment]),
|
||||
forwardRef(() => ChallengesModule),
|
||||
SequelizeModule.forFeature([AiConversation, AiMessage, PostureAssessment, AiReportHistory]),
|
||||
],
|
||||
controllers: [AiCoachController],
|
||||
providers: [AiCoachService, DietAnalysisService, AiReportService, CosService],
|
||||
|
||||
93
src/ai-coach/dto/ai-report-history.dto.ts
Normal file
93
src/ai-coach/dto/ai-report-history.dto.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString, IsNumber, Min, Max } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { ResponseCode } from '../../base.dto';
|
||||
|
||||
/**
|
||||
* AI 健康报告历史记录查询 DTO
|
||||
*/
|
||||
export class GetAiReportHistoryQueryDto {
|
||||
@ApiPropertyOptional({ description: '页码,从1开始', default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
|
||||
@ApiPropertyOptional({ description: '每页条数,默认10,最大50', default: 10 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
@Max(50)
|
||||
pageSize?: number = 10;
|
||||
|
||||
@ApiPropertyOptional({ description: '开始日期,格式 YYYY-MM-DD' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
startDate?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '结束日期,格式 YYYY-MM-DD' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
endDate?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '状态筛选: pending | processing | success | failed,不传则返回所有状态' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
status?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* AI 健康报告历史记录项
|
||||
*/
|
||||
export class AiReportHistoryItemDto {
|
||||
@ApiProperty({ description: '记录ID' })
|
||||
id: string;
|
||||
|
||||
@ApiProperty({ description: '报告日期,格式 YYYY-MM-DD' })
|
||||
reportDate: string;
|
||||
|
||||
@ApiProperty({ description: '生成的图片地址' })
|
||||
imageUrl: string;
|
||||
|
||||
@ApiProperty({ description: '生成状态: success | failed' })
|
||||
status: string;
|
||||
|
||||
@ApiProperty({ description: '创建时间' })
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* AI 健康报告历史记录列表响应
|
||||
*/
|
||||
export class AiReportHistoryListDto {
|
||||
@ApiProperty({ description: '记录列表', type: [AiReportHistoryItemDto] })
|
||||
records: AiReportHistoryItemDto[];
|
||||
|
||||
@ApiProperty({ description: '总记录数' })
|
||||
total: number;
|
||||
|
||||
@ApiProperty({ description: '当前页码' })
|
||||
page: number;
|
||||
|
||||
@ApiProperty({ description: '每页条数' })
|
||||
pageSize: number;
|
||||
|
||||
@ApiProperty({ description: '总页数' })
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 AI 健康报告历史记录响应 DTO
|
||||
*/
|
||||
export class GetAiReportHistoryResponseDto {
|
||||
@ApiProperty({ description: '响应状态码', example: ResponseCode.SUCCESS })
|
||||
declare code: ResponseCode;
|
||||
|
||||
@ApiProperty({ description: '响应消息', example: 'success' })
|
||||
declare message: string;
|
||||
|
||||
@ApiProperty({ description: '报告历史数据', type: AiReportHistoryListDto })
|
||||
declare data: AiReportHistoryListDto;
|
||||
}
|
||||
110
src/ai-coach/models/ai-report-history.model.ts
Normal file
110
src/ai-coach/models/ai-report-history.model.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { Column, DataType, Index, Model, PrimaryKey, Table } from 'sequelize-typescript';
|
||||
|
||||
/**
|
||||
* AI 报告生成状态枚举
|
||||
*/
|
||||
export enum AiReportStatus {
|
||||
PENDING = 'pending', // 未开始
|
||||
PROCESSING = 'processing', // 进行中
|
||||
SUCCESS = 'success', // 已完成
|
||||
FAILED = 'failed', // 已失败
|
||||
}
|
||||
|
||||
/**
|
||||
* AI 健康报告生成历史记录表
|
||||
* 记录每次生成的报告信息,包括 prompt 和图片地址
|
||||
*/
|
||||
@Table({
|
||||
tableName: 't_ai_report_history',
|
||||
underscored: true,
|
||||
})
|
||||
export class AiReportHistory extends Model {
|
||||
@PrimaryKey
|
||||
@Column({
|
||||
type: DataType.STRING(36),
|
||||
allowNull: false,
|
||||
comment: '记录ID',
|
||||
})
|
||||
declare id: string;
|
||||
|
||||
@Index
|
||||
@Column({
|
||||
type: DataType.STRING(36),
|
||||
allowNull: false,
|
||||
comment: '用户ID',
|
||||
})
|
||||
declare userId: string;
|
||||
|
||||
@Index
|
||||
@Column({
|
||||
type: DataType.DATEONLY,
|
||||
allowNull: false,
|
||||
comment: '报告日期',
|
||||
})
|
||||
declare reportDate: string;
|
||||
|
||||
@Column({
|
||||
type: DataType.TEXT,
|
||||
allowNull: true,
|
||||
comment: '生成图像使用的 Prompt',
|
||||
})
|
||||
declare prompt: string | null;
|
||||
|
||||
@Column({
|
||||
type: DataType.STRING(500),
|
||||
allowNull: true,
|
||||
comment: '生成的图片地址',
|
||||
})
|
||||
declare imageUrl: string | null;
|
||||
|
||||
@Column({
|
||||
type: DataType.STRING(20),
|
||||
allowNull: true,
|
||||
comment: 'API 提供商: openrouter | grsai',
|
||||
})
|
||||
declare apiProvider: string | null;
|
||||
|
||||
@Column({
|
||||
type: DataType.STRING(50),
|
||||
allowNull: true,
|
||||
comment: '使用的模型名称',
|
||||
})
|
||||
declare modelName: string | null;
|
||||
|
||||
@Column({
|
||||
type: DataType.INTEGER,
|
||||
allowNull: true,
|
||||
comment: '生成耗时(毫秒)',
|
||||
})
|
||||
declare generationTimeMs: number | null;
|
||||
|
||||
@Index
|
||||
@Column({
|
||||
type: DataType.STRING(20),
|
||||
allowNull: false,
|
||||
defaultValue: AiReportStatus.PENDING,
|
||||
comment: '生成状态: pending | processing | success | failed',
|
||||
})
|
||||
declare status: string;
|
||||
|
||||
@Column({
|
||||
type: DataType.TEXT,
|
||||
allowNull: true,
|
||||
comment: '失败原因(如果失败)',
|
||||
})
|
||||
declare errorMessage: string | null;
|
||||
|
||||
@Column({
|
||||
type: DataType.DATE,
|
||||
defaultValue: DataType.NOW,
|
||||
comment: '创建时间',
|
||||
})
|
||||
declare createdAt: Date;
|
||||
|
||||
@Column({
|
||||
type: DataType.DATE,
|
||||
defaultValue: DataType.NOW,
|
||||
comment: '更新时间',
|
||||
})
|
||||
declare updatedAt: Date;
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
import { Injectable, Logger, Inject, forwardRef } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AxiosResponse, AxiosRequestConfig, AxiosResponseHeaders } from 'axios';
|
||||
import { InjectModel } from '@nestjs/sequelize';
|
||||
import { Op } from 'sequelize';
|
||||
import * as dayjs from 'dayjs';
|
||||
import { OpenRouter } from '@openrouter/sdk';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import OpenAI from 'openai';
|
||||
import { CosService } from '../../users/cos.service';
|
||||
import { AiReportHistory, AiReportStatus } from '../models/ai-report-history.model';
|
||||
|
||||
// 假设各个模块的服务都已正确导出
|
||||
import { UsersService } from '../../users/users.service';
|
||||
@@ -12,6 +15,7 @@ import { DietRecordsService } from '../../diet-records/diet-records.service';
|
||||
import { WaterRecordsService } from '../../water-records/water-records.service';
|
||||
import { MoodCheckinsService } from '../../mood-checkins/mood-checkins.service';
|
||||
import { WorkoutsService } from '../../workouts/workouts.service';
|
||||
import { ChallengesService } from '../../challenges/challenges.service';
|
||||
|
||||
/**
|
||||
* 聚合的每日健康数据接口
|
||||
@@ -52,6 +56,9 @@ user?: {
|
||||
latestChest?: number;
|
||||
latestWaist?: number;
|
||||
};
|
||||
challenges: {
|
||||
activeChallengeCount: number;
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
@@ -60,6 +67,8 @@ export class AiReportService {
|
||||
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
@InjectModel(AiReportHistory)
|
||||
private readonly aiReportHistoryModel: typeof AiReportHistory,
|
||||
@Inject(forwardRef(() => UsersService))
|
||||
private readonly usersService: UsersService,
|
||||
@Inject(forwardRef(() => MedicationStatsService))
|
||||
@@ -72,31 +81,216 @@ export class AiReportService {
|
||||
private readonly moodCheckinsService: MoodCheckinsService,
|
||||
@Inject(forwardRef(() => WorkoutsService))
|
||||
private readonly workoutsService: WorkoutsService,
|
||||
@Inject(forwardRef(() => ChallengesService))
|
||||
private readonly challengesService: ChallengesService,
|
||||
private readonly cosService: CosService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 检查用户当天成功生成的报告数量
|
||||
* @param userId 用户ID
|
||||
* @param date 日期,格式 YYYY-MM-DD
|
||||
* @returns 当天成功生成的报告数量
|
||||
*/
|
||||
private async getTodaySuccessfulReportCount(userId: string, date: string): Promise<number> {
|
||||
const count = await this.aiReportHistoryModel.count({
|
||||
where: {
|
||||
userId,
|
||||
reportDate: date,
|
||||
status: AiReportStatus.SUCCESS,
|
||||
},
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主入口:生成用户的AI健康报告图片
|
||||
* 流程:
|
||||
* 0. 检查当天生成次数限制(最多2次)
|
||||
* 1. 创建记录,状态为 processing(进行中)
|
||||
* 2. 聚合数据、生成 Prompt、调用图像生成 API
|
||||
* 3. 根据结果更新记录状态为 success 或 failed
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param date 目标日期,格式 YYYY-MM-DD,默认为今天
|
||||
* @returns 图片的 URL 或 Base64 数据
|
||||
* @returns 包含记录ID和图片URL(如果成功)
|
||||
*/
|
||||
async generateHealthReportImage(userId: string, date?: string): Promise<{ imageUrl: string }> {
|
||||
async generateHealthReportImage(userId: string, date?: string): Promise<{ id: string; imageUrl: string }> {
|
||||
const targetDate = date || dayjs().format('YYYY-MM-DD');
|
||||
this.logger.log(`开始为用户 ${userId} 生成 ${targetDate} 的AI健康报告`);
|
||||
|
||||
// 1. 聚合数据
|
||||
const dailyData = await this.gatherDailyData(userId, targetDate);
|
||||
// Step 0: 检查当天成功生成的报告数量,最多允许2次
|
||||
const maxDailyReports = 2;
|
||||
const todaySuccessCount = await this.getTodaySuccessfulReportCount(userId, targetDate);
|
||||
if (todaySuccessCount >= maxDailyReports) {
|
||||
this.logger.warn(`用户 ${userId} 在 ${targetDate} 已成功生成 ${todaySuccessCount} 次报告,已达到每日上限`);
|
||||
throw new Error(`每天最多只能生成 ${maxDailyReports} 次健康报告`);
|
||||
}
|
||||
|
||||
// 2. 生成图像生成Prompt
|
||||
const imagePrompt = await this.generateImagePrompt(dailyData);
|
||||
this.logger.log(`为用户 ${userId} 生成了图像Prompt: ${imagePrompt}`);
|
||||
const startTime = Date.now();
|
||||
const apiProvider = this.configService.get<string>('IMAGE_API_PROVIDER') || 'openrouter';
|
||||
const recordId = uuidv4();
|
||||
|
||||
// 3. 调用图像生成API
|
||||
const imageUrl = await this.callImageGenerationApi(imagePrompt);
|
||||
this.logger.log(`为用户 ${userId} 成功生成图像: ${imageUrl}`);
|
||||
// Step 1: 创建记录,状态为 processing
|
||||
await this.createReportRecord({
|
||||
id: recordId,
|
||||
userId,
|
||||
reportDate: targetDate,
|
||||
apiProvider,
|
||||
status: AiReportStatus.PROCESSING,
|
||||
});
|
||||
|
||||
return { imageUrl };
|
||||
let imagePrompt = '';
|
||||
let imageUrl = '';
|
||||
|
||||
try {
|
||||
// Step 2: 聚合数据
|
||||
const dailyData = await this.gatherDailyData(userId, targetDate);
|
||||
|
||||
// Step 3: 获取用户语言偏好
|
||||
const userLanguage = await this.getUserLanguage(userId);
|
||||
|
||||
// Step 4: 生成图像生成Prompt
|
||||
imagePrompt = await this.generateImagePrompt(dailyData, userLanguage);
|
||||
this.logger.log(`为用户 ${userId} 生成了图像Prompt`);
|
||||
|
||||
// 更新 Prompt 到记录
|
||||
await this.updateReportRecord(recordId, { prompt: imagePrompt });
|
||||
|
||||
// Step 5: 调用图像生成API
|
||||
imageUrl = await this.callImageGenerationApi(imagePrompt);
|
||||
this.logger.log(`为用户 ${userId} 成功生成图像: ${imageUrl}`);
|
||||
|
||||
// Step 6: 更新记录状态为 success
|
||||
await this.updateReportRecord(recordId, {
|
||||
imageUrl,
|
||||
status: AiReportStatus.SUCCESS,
|
||||
generationTimeMs: Date.now() - startTime,
|
||||
modelName: apiProvider === 'grsai' ? 'nano-banana-pro' : 'google/gemini-3-pro-image-preview',
|
||||
});
|
||||
|
||||
return { id: recordId, imageUrl };
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
this.logger.error(`为用户 ${userId} 生成报告失败: ${errorMessage}`);
|
||||
|
||||
// Step 6: 更新记录状态为 failed
|
||||
await this.updateReportRecord(recordId, {
|
||||
prompt: imagePrompt || null,
|
||||
status: AiReportStatus.FAILED,
|
||||
errorMessage,
|
||||
generationTimeMs: Date.now() - startTime,
|
||||
modelName: apiProvider === 'grsai' ? 'nano-banana-pro' : 'google/gemini-3-pro-image-preview',
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建报告记录
|
||||
*/
|
||||
private async createReportRecord(data: {
|
||||
id: string;
|
||||
userId: string;
|
||||
reportDate: string;
|
||||
apiProvider: string;
|
||||
status: AiReportStatus;
|
||||
}): Promise<AiReportHistory> {
|
||||
try {
|
||||
const record = await this.aiReportHistoryModel.create({
|
||||
id: data.id,
|
||||
userId: data.userId,
|
||||
reportDate: data.reportDate,
|
||||
apiProvider: data.apiProvider,
|
||||
status: data.status,
|
||||
prompt: null,
|
||||
imageUrl: null,
|
||||
});
|
||||
this.logger.log(`创建报告记录成功,ID: ${data.id}, 状态: ${data.status}`);
|
||||
return record;
|
||||
} catch (error) {
|
||||
this.logger.error(`创建报告记录失败: ${error instanceof Error ? error.message : String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新报告记录
|
||||
*/
|
||||
private async updateReportRecord(
|
||||
recordId: string,
|
||||
data: Partial<{
|
||||
prompt: string | null;
|
||||
imageUrl: string | null;
|
||||
status: AiReportStatus;
|
||||
errorMessage: string | null;
|
||||
generationTimeMs: number;
|
||||
modelName: string;
|
||||
}>,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await this.aiReportHistoryModel.update(data, {
|
||||
where: { id: recordId },
|
||||
});
|
||||
this.logger.log(`更新报告记录成功,ID: ${recordId}, 更新字段: ${Object.keys(data).join(', ')}`);
|
||||
} catch (error) {
|
||||
// 更新失败不影响主流程,仅记录日志
|
||||
this.logger.error(`更新报告记录失败: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的报告生成历史(分页)
|
||||
* @param userId 用户ID
|
||||
* @param options 查询选项
|
||||
*/
|
||||
async getReportHistory(
|
||||
userId: string,
|
||||
options?: { page?: number; pageSize?: number; startDate?: string; endDate?: string; status?: string },
|
||||
): Promise<{ records: AiReportHistory[]; total: number; page: number; pageSize: number; totalPages: number }> {
|
||||
const { page = 1, pageSize = 10, startDate, endDate, status } = options || {};
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const where: any = { userId };
|
||||
|
||||
// 如果传了 status 参数则按指定状态筛选,否则返回所有状态
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
if (startDate || endDate) {
|
||||
where.reportDate = {};
|
||||
if (startDate) where.reportDate[Op.gte] = startDate;
|
||||
if (endDate) where.reportDate[Op.lte] = endDate;
|
||||
}
|
||||
|
||||
const { rows, count } = await this.aiReportHistoryModel.findAndCountAll({
|
||||
where,
|
||||
attributes: ['id', 'reportDate', 'imageUrl', 'status', 'createdAt'],
|
||||
order: [['createdAt', 'DESC']],
|
||||
limit: pageSize,
|
||||
offset,
|
||||
});
|
||||
|
||||
return {
|
||||
records: rows,
|
||||
total: count,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil(count / pageSize)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据记录ID获取报告详情
|
||||
* @param recordId 记录ID
|
||||
* @param userId 用户ID(用于权限校验)
|
||||
*/
|
||||
async getReportById(recordId: string, userId: string): Promise<AiReportHistory | null> {
|
||||
return this.aiReportHistoryModel.findOne({
|
||||
where: { id: recordId, userId },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,14 +300,15 @@ export class AiReportService {
|
||||
const dayStart = dayjs(date).startOf('day').toISOString();
|
||||
const dayEnd = dayjs(date).endOf('day').toISOString();
|
||||
|
||||
const [userProfile, medicationStats, dietHistory, waterStats, moodStats] = await Promise.all([
|
||||
const [userProfile, medicationStats, dietHistory, waterStats, moodStats, activeChallengeCount] = await Promise.all([
|
||||
this.usersService.getProfile({ sub: userId, email: '', isGuest: false } as any).then(p => p.data).catch(() => null),
|
||||
this.medicationStatsService.getDailyStats(userId, date).catch(() => null),
|
||||
// 获取当日饮食记录并聚合
|
||||
this.dietRecordsService.getDietHistory(userId, { startDate: dayStart, endDate: dayEnd, limit: 100 }).catch(() => ({ records: [] })),
|
||||
this.waterRecordsService.getWaterStats(userId, date).catch(() => null),
|
||||
this.moodCheckinsService.getDaily(userId, date).catch(() => null),
|
||||
// 获取最近的训练会话,并在后续筛选
|
||||
// 获取用户当前参与的活跃挑战数量
|
||||
this.challengesService.getActiveParticipatingChallengeCount(userId).catch(() => 0),
|
||||
]);
|
||||
|
||||
// 处理饮食数据聚合
|
||||
@@ -173,22 +368,30 @@ export class AiReportService {
|
||||
latestChest: latestChest?.value,
|
||||
latestWaist: latestWaist?.value,
|
||||
},
|
||||
challenges: {
|
||||
activeChallengeCount: activeChallengeCount,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. 生成固定格式的 prompt,适用于 Nano Banana Pro 模型
|
||||
* 优化:不再使用 LLM 生成 prompt,而是使用固定模版,并确保包含准确的中文文本
|
||||
* 优化:支持多语言,根据用户语言偏好生成对应的文本
|
||||
*/
|
||||
private async generateImagePrompt(data: DailyHealthData): Promise<string> {
|
||||
// 格式化日期为 "12月01日"
|
||||
const dateStr = dayjs(data.date).format('MM月DD日');
|
||||
private async generateImagePrompt(data: DailyHealthData, language: string): Promise<string> {
|
||||
const isEnglish = language.toLowerCase().startsWith('en');
|
||||
|
||||
// 格式化日期
|
||||
const dateStr = isEnglish
|
||||
? dayjs(data.date).format('MMM DD') // "Dec 01"
|
||||
: dayjs(data.date).format('MM月DD日'); // "12-01"
|
||||
|
||||
// 准备数据文本
|
||||
const moodText = this.translateMood(data.mood.primaryMood);
|
||||
const moodText = this.translateMood(data.mood.primaryMood, language);
|
||||
const medRate = Math.round(data.medications.completionRate); // 取整
|
||||
const calories = Math.round(data.diet.totalCalories);
|
||||
const water = Math.round(data.water.totalAmount);
|
||||
const challengeCount = data.challenges.activeChallengeCount;
|
||||
|
||||
// 根据性别调整角色描述
|
||||
let characterDesc = 'A happy cute character or animal mascot';
|
||||
@@ -198,19 +401,46 @@ export class AiReportService {
|
||||
characterDesc = 'A happy cute girl character in yoga outfit';
|
||||
}
|
||||
|
||||
// 构建多语言文本内容
|
||||
const textContent = isEnglish ? {
|
||||
languageInstruction: 'Please render the following specific text in English correctly:',
|
||||
title: `${dateStr} Health Report`,
|
||||
medication: `Medication: ${medRate}%`,
|
||||
diet: `Calories: ${calories} kcal`,
|
||||
water: `Water: ${water}ml`,
|
||||
mood: `Mood: ${moodText}`,
|
||||
challenges: challengeCount > 0 ? `Challenges: ${challengeCount}` : null,
|
||||
} : {
|
||||
languageInstruction: 'Please render the following specific text in Chinese correctly:',
|
||||
title: `${dateStr} 健康日报`,
|
||||
medication: `用药: ${medRate}%`,
|
||||
diet: `热量: ${calories}千卡`,
|
||||
water: `饮水: ${water}ml`,
|
||||
mood: `心情: ${moodText}`,
|
||||
challenges: challengeCount > 0 ? `挑战: ${challengeCount}个` : null,
|
||||
};
|
||||
|
||||
// 构建挑战相关的 prompt 部分
|
||||
const challengeTextSection = textContent.challenges
|
||||
? `- Challenge section text: "${textContent.challenges}"\n`
|
||||
: '';
|
||||
const challengeIconSection = challengeCount > 0
|
||||
? `- Icon for challenges (trophy or flag icon representing ${challengeCount} active ${challengeCount === 1 ? 'challenge' : 'challenges'}).\n`
|
||||
: '';
|
||||
|
||||
// 构建 Prompt
|
||||
// 格式:[风格描述] + [主体内容] + [文本渲染指令] + [细节描述]
|
||||
const prompt = `
|
||||
A cute, hand-drawn style health journal page illustration, kawaii aesthetic, soft pastel colors, warm lighting. Vertical 9:16 aspect ratio. High quality, 1k resolution.
|
||||
|
||||
The image features a cute layout with icons and text boxes.
|
||||
Please render the following specific text in Chinese correctly:
|
||||
- Title text: "${dateStr} 健康日报"
|
||||
- Medication section text: "用药: ${medRate}%"
|
||||
- Diet section text: "热量: ${calories}千卡"
|
||||
- Water section text: "饮水: ${water}ml"
|
||||
- Mood section text: "心情: ${moodText}"
|
||||
|
||||
${textContent.languageInstruction}
|
||||
- Title text: "${textContent.title}"
|
||||
- Medication section text: "${textContent.medication}"
|
||||
- Diet section text: "${textContent.diet}"
|
||||
- Water section text: "${textContent.water}"
|
||||
- Mood section text: "${textContent.mood}"
|
||||
${challengeTextSection}
|
||||
Visual elements:
|
||||
- ${characterDesc} representing the user.
|
||||
- Icon for medication (pill bottle or pills).
|
||||
@@ -218,7 +448,7 @@ Visual elements:
|
||||
- Icon for water (water glass or drop).
|
||||
- Icon for exercise (sneakers or dumbbell).
|
||||
- Icon for mood (a smiley face representing ${moodText}).
|
||||
|
||||
${challengeIconSection}
|
||||
Composition: Clean, organized, magazine layout style, decorative stickers and washi tape effects.
|
||||
`.trim();
|
||||
|
||||
@@ -226,10 +456,28 @@ Composition: Clean, organized, magazine layout style, decorative stickers and wa
|
||||
}
|
||||
|
||||
/**
|
||||
* 将心情类型翻译成中文(为了Prompt生成)
|
||||
* 根据语言翻译心情类型
|
||||
*/
|
||||
private translateMood(moodType?: string): string {
|
||||
const moodMap: Record<string, string> = {
|
||||
private translateMood(moodType?: string, language: string = 'zh-CN'): string {
|
||||
const isEnglish = language.toLowerCase().startsWith('en');
|
||||
|
||||
if (isEnglish) {
|
||||
const moodMapEn: Record<string, string> = {
|
||||
HAPPY: 'Happy',
|
||||
EXCITED: 'Excited',
|
||||
THRILLED: 'Thrilled',
|
||||
CALM: 'Calm',
|
||||
ANXIOUS: 'Anxious',
|
||||
SAD: 'Sad',
|
||||
LONELY: 'Lonely',
|
||||
WRONGED: 'Wronged',
|
||||
ANGRY: 'Angry',
|
||||
TIRED: 'Tired',
|
||||
};
|
||||
return moodMapEn[moodType || ''] || 'Calm';
|
||||
}
|
||||
|
||||
const moodMapZh: Record<string, string> = {
|
||||
HAPPY: '开心',
|
||||
EXCITED: '兴奋',
|
||||
THRILLED: '激动',
|
||||
@@ -241,107 +489,310 @@ Composition: Clean, organized, magazine layout style, decorative stickers and wa
|
||||
ANGRY: '生气',
|
||||
TIRED: '心累',
|
||||
};
|
||||
// 如果心情未知,返回"开心"以保持积极的氛围,或者使用"平和"
|
||||
return moodMap[moodType || ''] || '开心';
|
||||
return moodMapZh[moodType || ''] || '平静';
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. 调用 OpenRouter SDK 生成图片并上传到 COS
|
||||
* 获取用户语言偏好
|
||||
*/
|
||||
private async getUserLanguage(userId: string): Promise<string> {
|
||||
try {
|
||||
return await this.usersService.getUserLanguage(userId);
|
||||
} catch (error) {
|
||||
this.logger.error(`获取用户语言失败,使用默认中文: ${error instanceof Error ? error.message : String(error)}`);
|
||||
return 'zh-CN';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. 调用图像生成 API,支持 OpenRouter 和 GRSAI 两种提供商
|
||||
* 通过环境变量 IMAGE_API_PROVIDER 配置:'openrouter' | 'grsai',默认 'openrouter'
|
||||
*/
|
||||
private async callImageGenerationApi(prompt: string): Promise<string> {
|
||||
this.logger.log(`准备调用 OpenRouter SDK 生成图像`);
|
||||
const apiProvider = this.configService.get<string>('IMAGE_API_PROVIDER') || 'openrouter';
|
||||
this.logger.log(`准备调用 ${apiProvider} 生成图像`);
|
||||
this.logger.log(`使用Prompt: ${prompt}`);
|
||||
|
||||
const openRouterApiKey = this.configService.get<string>('OPENROUTER_API_KEY');
|
||||
if (!openRouterApiKey) {
|
||||
if (apiProvider === 'grsai') {
|
||||
return this.callGrsaiImageApi(prompt);
|
||||
} else {
|
||||
return this.callOpenRouterImageApi(prompt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用 OpenRouter API 生成图像
|
||||
*/
|
||||
private async callOpenRouterImageApi(prompt: string): Promise<string> {
|
||||
const apiKey = this.configService.get<string>('OPENROUTER_API_KEY');
|
||||
if (!apiKey) {
|
||||
this.logger.error('OpenRouter API Key 未配置');
|
||||
throw new Error('OpenRouter API Key 未配置');
|
||||
}
|
||||
|
||||
try {
|
||||
// 初始化 OpenRouter 客户端
|
||||
const openrouter = new OpenRouter({
|
||||
apiKey: openRouterApiKey,
|
||||
const client = new OpenAI({
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
apiKey,
|
||||
});
|
||||
|
||||
// 调用图像生成API
|
||||
const result = await openrouter.chat.send({
|
||||
model: "google/gemini-3-pro-image-preview",
|
||||
this.logger.log(`使用 OpenRouter API, 模型: google/gemini-3-pro-image-preview`);
|
||||
|
||||
const apiResponse = await client.chat.completions.create({
|
||||
model: 'google/gemini-3-pro-image-preview',
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
role: 'user' as const,
|
||||
content: prompt,
|
||||
},
|
||||
],
|
||||
// @ts-ignore - 扩展参数,用于支持图像生成
|
||||
modalities: ['image', 'text'],
|
||||
});
|
||||
|
||||
const message = result.choices[0].message;
|
||||
|
||||
// 处理不同格式的响应内容
|
||||
let imageData: string | undefined;
|
||||
let isBase64 = false;
|
||||
|
||||
if (typeof message.content === 'string') {
|
||||
// 检查是否为 base64 数据
|
||||
// base64 图像通常以 data:image/ 开头,或者是纯 base64 字符串
|
||||
if (message.content.startsWith('data:image/')) {
|
||||
// 完整的 data URL 格式:data:image/png;base64,xxxxx
|
||||
imageData = message.content;
|
||||
isBase64 = true;
|
||||
this.logger.log('检测到 Data URL 格式的 base64 图像');
|
||||
} else if (/^[A-Za-z0-9+/=]+$/.test(message.content.substring(0, 100))) {
|
||||
// 纯 base64 字符串(检查前100个字符)
|
||||
imageData = message.content;
|
||||
isBase64 = true;
|
||||
this.logger.log('检测到纯 base64 格式的图像数据');
|
||||
} else {
|
||||
// 尝试提取 HTTP URL
|
||||
const urlMatch = message.content.match(/https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp)/i);
|
||||
if (urlMatch) {
|
||||
imageData = urlMatch[0];
|
||||
isBase64 = false;
|
||||
this.logger.log(`检测到 HTTP URL: ${imageData}`);
|
||||
}
|
||||
}
|
||||
} else if (Array.isArray(message.content)) {
|
||||
// 检查内容数组中是否有图像项
|
||||
const imageItem = message.content.find(item => item.type === 'image_url');
|
||||
if (imageItem && imageItem.imageUrl) {
|
||||
imageData = imageItem.imageUrl.url;
|
||||
// 判断是 URL 还是 base64
|
||||
isBase64 = imageData.startsWith('data:image/') || /^[A-Za-z0-9+/=]+$/.test(imageData.substring(0, 100));
|
||||
}
|
||||
const message = apiResponse.choices[0].message;
|
||||
this.logger.log(`OpenRouter 收到响应: ${JSON.stringify(message, null, 2)}`);
|
||||
|
||||
const imageData = this.extractImageData(message);
|
||||
if (imageData) {
|
||||
return await this.processAndUploadImage(imageData);
|
||||
}
|
||||
|
||||
if (imageData) {
|
||||
if (isBase64) {
|
||||
// 处理 base64 数据并上传到 COS
|
||||
this.logger.log('开始处理 base64 图像数据');
|
||||
const cosImageUrl = await this.uploadBase64ToCos(imageData);
|
||||
this.logger.log(`Base64 图像上传到 COS 成功: ${cosImageUrl}`);
|
||||
return cosImageUrl;
|
||||
} else {
|
||||
// 下载 HTTP URL 图像并上传到 COS
|
||||
this.logger.log(`OpenRouter 返回图像 URL: ${imageData}`);
|
||||
const cosImageUrl = await this.downloadAndUploadToCos(imageData);
|
||||
this.logger.log(`图像上传到 COS 成功: ${cosImageUrl}`);
|
||||
return cosImageUrl;
|
||||
}
|
||||
} else {
|
||||
this.logger.error('OpenRouter 响应中未包含图像数据');
|
||||
this.logger.error(`实际响应内容类型: ${typeof message.content}`);
|
||||
this.logger.error(`实际响应内容: ${JSON.stringify(message.content).substring(0, 500)}`);
|
||||
throw new Error('图像生成失败:响应中未包含图像数据');
|
||||
}
|
||||
this.logger.error('OpenRouter 响应中未包含图像数据');
|
||||
this.logger.error(`实际响应内容: ${JSON.stringify(message).substring(0, 500)}`);
|
||||
throw new Error('图像生成失败:响应中未包含图像数据');
|
||||
} catch (error) {
|
||||
this.logger.error(`调用 OpenRouter SDK 失败: ${error.message}`);
|
||||
if (error.response) {
|
||||
this.logger.error(`OpenRouter 错误详情: ${JSON.stringify(error.response.data)}`);
|
||||
}
|
||||
this.logger.error(`调用 OpenRouter 失败: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用 GRSAI API 生成图像(提交任务 + 轮询结果)
|
||||
* API 文档:
|
||||
* - 提交任务: POST https://grsai.dakka.com.cn/v1/draw/nano-banana
|
||||
* - 查询结果: POST https://grsai.dakka.com.cn/v1/draw/result
|
||||
*/
|
||||
private async callGrsaiImageApi(prompt: string): Promise<string> {
|
||||
const apiKey = this.configService.get<string>('GRSAI_API_KEY');
|
||||
if (!apiKey) {
|
||||
this.logger.error('GRSAI API Key 未配置');
|
||||
throw new Error('GRSAI API Key 未配置');
|
||||
}
|
||||
|
||||
const baseURL = 'https://grsai.dakka.com.cn';
|
||||
const axios = require('axios');
|
||||
|
||||
// 通用请求头
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
};
|
||||
|
||||
try {
|
||||
// Step 1: 提交图像生成任务
|
||||
this.logger.log('GRSAI: 提交图像生成任务...');
|
||||
const submitResponse = await axios.post(
|
||||
`${baseURL}/v1/draw/nano-banana`,
|
||||
{
|
||||
model: 'nano-banana-pro',
|
||||
prompt: prompt,
|
||||
webHook: '-1', // 必填 -1,表示不使用 webhook
|
||||
},
|
||||
{ headers, timeout: 30000 },
|
||||
);
|
||||
|
||||
// 检查提交响应
|
||||
if (submitResponse.data.code !== 0) {
|
||||
this.logger.error(`GRSAI 任务提交失败: ${submitResponse.data.msg}`);
|
||||
throw new Error(`GRSAI 任务提交失败: ${submitResponse.data.msg}`);
|
||||
}
|
||||
|
||||
const taskId = submitResponse.data.data?.id;
|
||||
if (!taskId) {
|
||||
this.logger.error('GRSAI 任务提交成功但未返回任务ID');
|
||||
throw new Error('GRSAI 任务提交失败:未返回任务ID');
|
||||
}
|
||||
|
||||
this.logger.log(`GRSAI: 任务提交成功,任务ID: ${taskId}`);
|
||||
|
||||
// Step 2: 轮询获取结果
|
||||
const imageUrl = await this.pollGrsaiResult(baseURL, headers, taskId);
|
||||
|
||||
// Step 3: 下载图像并上传到 COS
|
||||
return await this.processAndUploadImage(imageUrl);
|
||||
} catch (error) {
|
||||
this.logger.error(`调用 GRSAI 失败: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询 GRSAI 任务结果
|
||||
* @param baseURL API 基础地址
|
||||
* @param headers 请求头
|
||||
* @param taskId 任务ID
|
||||
* @returns 生成的图像 URL
|
||||
*/
|
||||
private async pollGrsaiResult(
|
||||
baseURL: string,
|
||||
headers: Record<string, string>,
|
||||
taskId: string,
|
||||
): Promise<string> {
|
||||
const axios = require('axios');
|
||||
|
||||
// 轮询配置
|
||||
const maxAttempts = 60; // 最大尝试次数
|
||||
const pollInterval = 2000; // 轮询间隔 2 秒
|
||||
const maxWaitTime = 120000; // 最大等待时间 2 分钟
|
||||
|
||||
const startTime = Date.now();
|
||||
let attempts = 0;
|
||||
|
||||
while (attempts < maxAttempts) {
|
||||
attempts++;
|
||||
const elapsedTime = Date.now() - startTime;
|
||||
|
||||
// 检查是否超时
|
||||
if (elapsedTime > maxWaitTime) {
|
||||
this.logger.error(`GRSAI: 轮询超时,已等待 ${elapsedTime / 1000} 秒`);
|
||||
throw new Error('GRSAI 图像生成超时');
|
||||
}
|
||||
|
||||
this.logger.log(`GRSAI: 第 ${attempts} 次轮询,任务ID: ${taskId}`);
|
||||
|
||||
try {
|
||||
const resultResponse = await axios.post(
|
||||
`${baseURL}/v1/draw/result`,
|
||||
{ id: taskId },
|
||||
{ headers, timeout: 10000 },
|
||||
);
|
||||
|
||||
const data = resultResponse.data;
|
||||
|
||||
// 检查响应码
|
||||
if (data.code !== 0) {
|
||||
this.logger.error(`GRSAI 查询失败: ${data.msg}`);
|
||||
throw new Error(`GRSAI 查询失败: ${data.msg}`);
|
||||
}
|
||||
|
||||
const taskData = data.data;
|
||||
const status = taskData?.status;
|
||||
const progress = taskData?.progress || 0;
|
||||
|
||||
this.logger.log(`GRSAI: 任务状态: ${status}, 进度: ${progress}%`);
|
||||
|
||||
// 根据状态处理
|
||||
switch (status) {
|
||||
case 'succeeded':
|
||||
// 任务成功,提取图像 URL
|
||||
const results = taskData.results;
|
||||
if (results && results.length > 0 && results[0].url) {
|
||||
const imageUrl = results[0].url;
|
||||
this.logger.log(`GRSAI: 图像生成成功: ${imageUrl}`);
|
||||
return imageUrl;
|
||||
}
|
||||
throw new Error('GRSAI 任务成功但未返回图像URL');
|
||||
|
||||
case 'failed':
|
||||
// 任务失败
|
||||
const failureReason = taskData.failure_reason || taskData.error || '未知错误';
|
||||
this.logger.error(`GRSAI: 任务失败: ${failureReason}`);
|
||||
throw new Error(`GRSAI 图像生成失败: ${failureReason}`);
|
||||
|
||||
case 'pending':
|
||||
case 'processing':
|
||||
case 'running':
|
||||
// 任务进行中,继续轮询
|
||||
await this.sleep(pollInterval);
|
||||
break;
|
||||
|
||||
default:
|
||||
// 未知状态,继续轮询
|
||||
this.logger.warn(`GRSAI: 未知任务状态: ${status}`);
|
||||
await this.sleep(pollInterval);
|
||||
}
|
||||
} catch (error) {
|
||||
// 如果是网络错误,继续重试
|
||||
if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT') {
|
||||
this.logger.warn(`GRSAI: 轮询请求超时,继续重试...`);
|
||||
await this.sleep(pollInterval);
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`GRSAI 图像生成失败:超过最大轮询次数 ${maxAttempts}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 延时函数
|
||||
*/
|
||||
private sleep(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 API 响应中提取图像数据
|
||||
*/
|
||||
private extractImageData(message: any): string | undefined {
|
||||
let imageData: string | undefined;
|
||||
|
||||
// 检查 images 数组(OpenRouter/GRSAI 特有的响应格式)
|
||||
if (message.images && Array.isArray(message.images) && message.images.length > 0) {
|
||||
const firstImage = message.images[0];
|
||||
if (firstImage.image_url?.url) {
|
||||
imageData = firstImage.image_url.url;
|
||||
this.logger.log(`检测到 images 数组中的图像数据`);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果 images 数组中没有,检查 content 字段
|
||||
if (!imageData && typeof message.content === 'string') {
|
||||
// 检查是否为 base64 数据
|
||||
if (message.content.startsWith('data:image/')) {
|
||||
imageData = message.content;
|
||||
this.logger.log('检测到 Data URL 格式的 base64 图像');
|
||||
} else if (/^[A-Za-z0-9+/=]+$/.test(message.content.substring(0, 100))) {
|
||||
imageData = message.content;
|
||||
this.logger.log('检测到纯 base64 格式的图像数据');
|
||||
} else {
|
||||
// 尝试提取 HTTP URL
|
||||
const urlMatch = message.content.match(/https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp)/i);
|
||||
if (urlMatch) {
|
||||
imageData = urlMatch[0];
|
||||
this.logger.log(`检测到 HTTP URL: ${imageData}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return imageData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理图像数据并上传到 COS
|
||||
*/
|
||||
private async processAndUploadImage(imageData: string): Promise<string> {
|
||||
// 判断是 base64 还是 URL
|
||||
const isBase64 = imageData.startsWith('data:image/') ||
|
||||
(/^[A-Za-z0-9+/=]+$/.test(imageData.substring(0, 100)) && !imageData.startsWith('http'));
|
||||
|
||||
if (isBase64) {
|
||||
// 处理 base64 数据并上传到 COS
|
||||
this.logger.log('开始处理 base64 图像数据');
|
||||
const cosImageUrl = await this.uploadBase64ToCos(imageData);
|
||||
this.logger.log(`Base64 图像上传到 COS 成功: ${cosImageUrl}`);
|
||||
return cosImageUrl;
|
||||
} else {
|
||||
// 下载 HTTP URL 图像并上传到 COS
|
||||
this.logger.log(`API 返回图像 URL: ${imageData}`);
|
||||
const cosImageUrl = await this.downloadAndUploadToCos(imageData);
|
||||
this.logger.log(`图像上传到 COS 成功: ${cosImageUrl}`);
|
||||
return cosImageUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 base64 图像数据上传到 COS
|
||||
*/
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { APP_GUARD } from '@nestjs/core';
|
||||
import { AppController } from "./app.controller";
|
||||
import { AppService } from "./app.service";
|
||||
import { DatabaseModule } from "./database/database.module";
|
||||
import { UsersModule } from "./users/users.module";
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
|
||||
import { LoggerModule } from './common/logger/logger.module';
|
||||
import { CheckinsModule } from './checkins/checkins.module';
|
||||
import { AiCoachModule } from './ai-coach/ai-coach.module';
|
||||
@@ -30,6 +32,10 @@ import { MedicationsModule } from './medications/medications.module';
|
||||
envFilePath: '.env',
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
ThrottlerModule.forRoot([{
|
||||
ttl: 60000, // 时间窗口:60秒
|
||||
limit: 100, // 每个时间窗口最多100个请求
|
||||
}]),
|
||||
LoggerModule,
|
||||
DatabaseModule,
|
||||
UsersModule,
|
||||
@@ -51,6 +57,12 @@ import { MedicationsModule } from './medications/medications.module';
|
||||
MedicationsModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
providers: [
|
||||
AppService,
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: ThrottlerGuard,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule { }
|
||||
|
||||
@@ -788,6 +788,37 @@ export class ChallengesService {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户当前参与的活跃挑战数量(正在进行中且未过期)
|
||||
* @param userId 用户ID
|
||||
* @returns 活跃挑战数量
|
||||
*/
|
||||
async getActiveParticipatingChallengeCount(userId: string): Promise<number> {
|
||||
const now = new Date();
|
||||
|
||||
// 查询用户参与的所有活跃状态的挑战
|
||||
const participants = await this.participantModel.findAll({
|
||||
where: {
|
||||
userId,
|
||||
status: {
|
||||
[Op.in]: [ChallengeParticipantStatus.ACTIVE, ChallengeParticipantStatus.COMPLETED],
|
||||
},
|
||||
},
|
||||
include: [{
|
||||
model: Challenge,
|
||||
as: 'challenge',
|
||||
where: {
|
||||
challengeState: ChallengeState.ACTIVE,
|
||||
startAt: { [Op.lte]: now },
|
||||
endAt: { [Op.gte]: now },
|
||||
},
|
||||
required: true,
|
||||
}],
|
||||
});
|
||||
|
||||
return participants.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户加入的自定义挑战 ID 列表
|
||||
*/
|
||||
|
||||
@@ -22,7 +22,7 @@ import { ConfigService } from '@nestjs/config';
|
||||
collate: 'utf8mb4_0900_ai_ci',
|
||||
},
|
||||
autoLoadModels: true,
|
||||
synchronize: true,
|
||||
synchronize: false,
|
||||
}),
|
||||
}),
|
||||
],
|
||||
|
||||
@@ -29,7 +29,8 @@ async function bootstrap() {
|
||||
|
||||
res.on('finish', () => {
|
||||
const duration = Date.now() - startTime;
|
||||
const logMessage = `${req.method} ${req.originalUrl} ${res.statusCode} ${duration}ms`;
|
||||
const appVersion = req.headers['x-app-version'] || 'unknown';
|
||||
const logMessage = `${req.method} ${req.originalUrl} ${res.statusCode} ${duration}ms [v${appVersion}]`;
|
||||
|
||||
if (res.statusCode >= 400) {
|
||||
logger.error(`${logMessage} - Body: ${JSON.stringify(req.body)}`);
|
||||
|
||||
@@ -1,583 +0,0 @@
|
||||
# AI 药物识别功能说明
|
||||
|
||||
## 功能概述
|
||||
|
||||
AI 药物识别功能允许用户通过上传药品照片,自动识别药品信息并创建药物记录。系统使用 GLM-4V-Plus 视觉模型和 GLM-4-Flash 文本模型进行多阶段分析,提供完整的药品信息和健康建议。
|
||||
|
||||
## 核心特性
|
||||
|
||||
### 1. 多图片识别
|
||||
|
||||
- **正面图片**(必需):药品包装正面,包含药品名称
|
||||
- **侧面图片**(必需):药品包装侧面,包含规格信息
|
||||
- **辅助图片**(可选):药品说明书或其他辅助信息
|
||||
|
||||
### 2. 多阶段分析
|
||||
|
||||
系统分4个阶段进行识别和分析:
|
||||
|
||||
1. **产品识别** (0-40%):识别药品基本信息(名称、剂型、剂量等)
|
||||
2. **适宜人群分析** (40-60%):分析适合人群和禁忌人群
|
||||
3. **成分分析** (60-80%):分析主要成分和用途
|
||||
4. **副作用分析** (80-100%):分析副作用、储存建议和健康建议
|
||||
|
||||
### 3. 实时状态追踪
|
||||
|
||||
- 支持轮询查询识别进度
|
||||
- 提供详细的步骤描述
|
||||
- 实时更新进度百分比
|
||||
|
||||
### 4. 智能质量控制
|
||||
|
||||
系统会在识别前先判断图片质量:
|
||||
|
||||
- **图片可读性检查**:AI 会首先判断图片是否足够清晰可读
|
||||
- **置信度评估**:识别置信度低于 60% 时会自动失败
|
||||
- **严格验证**:宁可识别失败,也不提供不准确的药品信息
|
||||
- **友好提示**:失败时会给出明确的改进建议
|
||||
|
||||
### 5. 结构化输出
|
||||
|
||||
识别结果包含完整的药品信息:
|
||||
|
||||
- 质量指标:图片可读性、识别置信度
|
||||
- 基本信息:名称、剂型、剂量、服用次数、服药时间
|
||||
- 适宜性分析:适合人群、不适合人群
|
||||
- 成分分析:主要成分、主要用途
|
||||
- 安全信息:副作用、储存建议、健康建议
|
||||
|
||||
## API 接口
|
||||
|
||||
### 1. 创建识别任务
|
||||
|
||||
**接口**: `POST /medications/ai-recognize`
|
||||
|
||||
**权限要求**: 需要 VIP 会员或有 AI 使用次数
|
||||
|
||||
**请求参数**:
|
||||
|
||||
```json
|
||||
{
|
||||
"frontImageUrl": "https://cdn.example.com/front.jpg",
|
||||
"sideImageUrl": "https://cdn.example.com/side.jpg",
|
||||
"auxiliaryImageUrl": "https://cdn.example.com/auxiliary.jpg" // 可选
|
||||
}
|
||||
```
|
||||
|
||||
**响应示例**:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"message": "识别任务创建成功",
|
||||
"data": {
|
||||
"taskId": "task_user123_1234567890",
|
||||
"status": "pending"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**注意事项**:
|
||||
|
||||
- 必须提供正面和侧面图片
|
||||
- 任务创建成功后立即扣减 1 次 AI 使用次数
|
||||
- 识别过程异步执行,不阻塞当前请求
|
||||
|
||||
### 2. 查询识别状态
|
||||
|
||||
**接口**: `GET /medications/ai-recognize/:taskId/status`
|
||||
|
||||
**轮询建议**: 每 2-3 秒查询一次
|
||||
|
||||
**响应示例**:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"message": "查询成功",
|
||||
"data": {
|
||||
"taskId": "task_user123_1234567890",
|
||||
"status": "analyzing_product",
|
||||
"currentStep": "正在识别药品基本信息...",
|
||||
"progress": 25,
|
||||
"createdAt": "2025-01-20T12:00:00.000Z"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**状态说明**:
|
||||
|
||||
- `pending`: 任务已创建,等待处理
|
||||
- `analyzing_product`: 正在识别药品基本信息
|
||||
- `analyzing_suitability`: 正在分析适宜人群
|
||||
- `analyzing_ingredients`: 正在分析主要成分
|
||||
- `analyzing_effects`: 正在分析副作用和健康建议
|
||||
- `completed`: 识别完成
|
||||
- `failed`: 识别失败
|
||||
|
||||
**完成后的响应示例**:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"message": "查询成功",
|
||||
"data": {
|
||||
"taskId": "task_user123_1234567890",
|
||||
"status": "completed",
|
||||
"currentStep": "识别完成",
|
||||
"progress": 100,
|
||||
"result": {
|
||||
"name": "阿莫西林胶囊",
|
||||
"photoUrl": "https://cdn.example.com/front.jpg",
|
||||
"form": "capsule",
|
||||
"dosageValue": 0.25,
|
||||
"dosageUnit": "g",
|
||||
"timesPerDay": 3,
|
||||
"medicationTimes": ["08:00", "14:00", "20:00"],
|
||||
"suitableFor": ["成年人", "细菌感染患者"],
|
||||
"unsuitableFor": ["青霉素过敏者", "孕妇"],
|
||||
"mainIngredients": ["阿莫西林"],
|
||||
"mainUsage": "用于敏感菌引起的各种感染",
|
||||
"sideEffects": ["恶心", "呕吐", "腹泻"],
|
||||
"storageAdvice": ["密封保存", "室温避光"],
|
||||
"healthAdvice": ["按时服药", "多喝水"],
|
||||
"confidence": 0.95
|
||||
},
|
||||
"createdAt": "2025-01-20T12:00:00.000Z",
|
||||
"completedAt": "2025-01-20T12:01:30.000Z"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 确认并创建药物
|
||||
|
||||
**接口**: `POST /medications/ai-recognize/:taskId/confirm`
|
||||
|
||||
**说明**: 用户确认识别结果后创建药物记录,可以对识别结果进行调整
|
||||
|
||||
**请求参数** (可选,用于调整识别结果):
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "调整后的药品名称",
|
||||
"timesPerDay": 2,
|
||||
"medicationTimes": ["09:00", "21:00"],
|
||||
"startDate": "2025-01-20T00:00:00.000Z",
|
||||
"endDate": "2025-02-20T00:00:00.000Z",
|
||||
"note": "饭后服用"
|
||||
}
|
||||
```
|
||||
|
||||
**响应示例**:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 0,
|
||||
"message": "创建成功",
|
||||
"data": {
|
||||
"id": "med_abc123",
|
||||
"name": "阿莫西林胶囊",
|
||||
"form": "capsule",
|
||||
"dosageValue": 0.25,
|
||||
"dosageUnit": "g",
|
||||
"timesPerDay": 3,
|
||||
"medicationTimes": ["08:00", "14:00", "20:00"],
|
||||
"isActive": true,
|
||||
"aiAnalysis": "{...完整的AI分析结果...}",
|
||||
"createdAt": "2025-01-20T12:02:00.000Z"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 前端集成示例
|
||||
|
||||
### 完整流程
|
||||
|
||||
```typescript
|
||||
// 1. 上传图片并创建识别任务
|
||||
async function startRecognition(
|
||||
frontImage: string,
|
||||
sideImage: string,
|
||||
auxiliaryImage?: string
|
||||
) {
|
||||
const response = await fetch("/medications/ai-recognize", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
frontImageUrl: frontImage,
|
||||
sideImageUrl: sideImage,
|
||||
auxiliaryImageUrl: auxiliaryImage,
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
if (result.code === 0) {
|
||||
return result.data.taskId;
|
||||
}
|
||||
throw new Error(result.message);
|
||||
}
|
||||
|
||||
// 2. 轮询查询识别状态
|
||||
async function pollRecognitionStatus(taskId: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const pollInterval = setInterval(async () => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`/medications/ai-recognize/${taskId}/status`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const result = await response.json();
|
||||
if (result.code !== 0) {
|
||||
clearInterval(pollInterval);
|
||||
reject(new Error(result.message));
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
status,
|
||||
progress,
|
||||
currentStep,
|
||||
result: recognitionResult,
|
||||
errorMessage,
|
||||
} = result.data;
|
||||
|
||||
// 更新UI显示进度
|
||||
updateProgress(progress, currentStep);
|
||||
|
||||
if (status === "completed") {
|
||||
clearInterval(pollInterval);
|
||||
resolve(recognitionResult);
|
||||
} else if (status === "failed") {
|
||||
clearInterval(pollInterval);
|
||||
reject(new Error(errorMessage || "识别失败"));
|
||||
}
|
||||
} catch (error) {
|
||||
clearInterval(pollInterval);
|
||||
reject(error);
|
||||
}
|
||||
}, 2000); // 每2秒查询一次
|
||||
});
|
||||
}
|
||||
|
||||
// 3. 确认并创建药物
|
||||
async function confirmAndCreate(taskId: string, adjustments?: any) {
|
||||
const response = await fetch(`/medications/ai-recognize/${taskId}/confirm`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(adjustments || {}),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
if (result.code === 0) {
|
||||
return result.data;
|
||||
}
|
||||
throw new Error(result.message);
|
||||
}
|
||||
|
||||
// 完整使用示例
|
||||
async function recognizeAndCreateMedication() {
|
||||
try {
|
||||
// 步骤1: 创建识别任务
|
||||
showLoading("正在创建识别任务...");
|
||||
const taskId = await startRecognition(
|
||||
frontImageUrl,
|
||||
sideImageUrl,
|
||||
auxiliaryImageUrl
|
||||
);
|
||||
|
||||
// 步骤2: 轮询查询状态
|
||||
showProgress("识别进行中...", 0);
|
||||
const recognitionResult = await pollRecognitionStatus(taskId);
|
||||
|
||||
// 步骤3: 展示结果给用户确认
|
||||
const confirmed = await showConfirmationDialog(recognitionResult);
|
||||
if (!confirmed) return;
|
||||
|
||||
// 步骤4: 创建药物记录
|
||||
showLoading("正在创建药物记录...");
|
||||
const medication = await confirmAndCreate(taskId, userAdjustments);
|
||||
|
||||
showSuccess("药物创建成功!");
|
||||
navigateToMedicationDetail(medication.id);
|
||||
} catch (error) {
|
||||
showError(error.message);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### UI 交互建议
|
||||
|
||||
#### 1. 上传阶段
|
||||
|
||||
```typescript
|
||||
// 显示图片上传引导
|
||||
<div className="upload-guide">
|
||||
<div className="upload-item required">
|
||||
<Icon name="front" />
|
||||
<span>正面照片(必需)</span>
|
||||
<p>拍摄药品包装正面,确保药品名称清晰可见</p>
|
||||
</div>
|
||||
<div className="upload-item required">
|
||||
<Icon name="side" />
|
||||
<span>侧面照片(必需)</span>
|
||||
<p>拍摄药品包装侧面,确保规格剂量清晰可见</p>
|
||||
</div>
|
||||
<div className="upload-item optional">
|
||||
<Icon name="document" />
|
||||
<span>说明书(可选)</span>
|
||||
<p>拍摄药品说明书,可提高识别准确度</p>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
#### 2. 识别阶段
|
||||
|
||||
```typescript
|
||||
// 显示识别进度和当前步骤
|
||||
<div className="recognition-progress">
|
||||
<ProgressBar value={progress} />
|
||||
<div className="current-step">
|
||||
{status === 'analyzing_product' && '📦 正在识别药品信息...'}
|
||||
{status === 'analyzing_suitability' && '👥 正在分析适宜人群...'}
|
||||
{status === 'analyzing_ingredients' && '🧪 正在分析主要成分...'}
|
||||
{status === 'analyzing_effects' && '⚠️ 正在分析副作用...'}
|
||||
</div>
|
||||
<div className="progress-text">{progress}%</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
#### 3. 确认阶段
|
||||
|
||||
```typescript
|
||||
// 展示识别结果供用户确认和编辑
|
||||
<div className="recognition-result">
|
||||
<div className="confidence-badge">
|
||||
识别置信度: {(result.confidence * 100).toFixed(0)}%
|
||||
</div>
|
||||
|
||||
<EditableField
|
||||
label="药品名称"
|
||||
value={result.name}
|
||||
onChange={(v) => setAdjustments({...adjustments, name: v})}
|
||||
/>
|
||||
|
||||
<EditableField
|
||||
label="每日服用次数"
|
||||
value={result.timesPerDay}
|
||||
onChange={(v) => setAdjustments({...adjustments, timesPerDay: v})}
|
||||
/>
|
||||
|
||||
{/* 更多可编辑字段 */}
|
||||
|
||||
<div className="ai-analysis">
|
||||
<h3>AI 健康分析</h3>
|
||||
<Section title="适合人群" items={result.suitableFor} />
|
||||
<Section title="不适合人群" items={result.unsuitableFor} />
|
||||
<Section title="主要成分" items={result.mainIngredients} />
|
||||
<Section title="副作用" items={result.sideEffects} />
|
||||
<Section title="健康建议" items={result.healthAdvice} />
|
||||
</div>
|
||||
|
||||
<div className="action-buttons">
|
||||
<Button onClick={handleCancel}>取消</Button>
|
||||
<Button primary onClick={handleConfirm}>确认创建</Button>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
## 错误处理
|
||||
|
||||
### 常见错误及解决方案
|
||||
|
||||
| 错误码 | 错误信息 | 原因 | 解决方案 |
|
||||
| ------ | ---------------------- | ------------------ | ---------------------- |
|
||||
| 400 | 必须提供正面和侧面图片 | 缺少必需的图片 | 确保上传正面和侧面图片 |
|
||||
| 403 | 免费使用次数已用完 | AI 使用次数不足 | 引导用户开通 VIP 会员 |
|
||||
| 404 | 识别任务不存在 | 任务ID错误或已过期 | 重新创建识别任务 |
|
||||
| 500 | AI响应格式错误 | 模型返回异常 | 提示用户重试或联系客服 |
|
||||
|
||||
### 识别失败处理
|
||||
|
||||
当识别状态为 `failed` 时,系统会提供明确的失败原因:
|
||||
|
||||
#### 失败原因分类
|
||||
|
||||
1. **图片质量问题**:
|
||||
|
||||
- 错误信息:`图片模糊或光线不足,无法清晰识别药品信息`
|
||||
- 用户建议:
|
||||
- 在光线充足的环境下重新拍摄
|
||||
- 确保相机对焦清晰
|
||||
- 避免手抖造成的模糊
|
||||
- 避免反光和阴影
|
||||
|
||||
2. **药品信息不可见**:
|
||||
|
||||
- 错误信息:`无法从图片中识别出药品名称`
|
||||
- 用户建议:
|
||||
- 确保药品名称完整出现在画面中
|
||||
- 调整拍摄角度,避免遮挡
|
||||
- 拍摄药品包装正面和侧面
|
||||
- 如有说明书可一并拍摄
|
||||
|
||||
3. **识别置信度过低**:
|
||||
- 错误信息:`识别置信度过低 (XX%)。建议重新拍摄更清晰的照片`
|
||||
- 用户建议:
|
||||
- 拍摄更清晰的照片
|
||||
- 确保药品规格、剂量等信息清晰可见
|
||||
- 可选择手动输入药品信息
|
||||
|
||||
#### 前端处理建议
|
||||
|
||||
```typescript
|
||||
if (status === "failed") {
|
||||
// 根据错误信息提供针对性的指导
|
||||
if (errorMessage.includes("图片模糊")) {
|
||||
showTip("拍照小贴士", [
|
||||
"在明亮的环境下拍摄",
|
||||
"保持相机稳定,避免抖动",
|
||||
"等待相机对焦后再拍摄",
|
||||
]);
|
||||
} else if (errorMessage.includes("无法识别出药品名称")) {
|
||||
showTip("拍照小贴士", [
|
||||
"确保药品名称完整可见",
|
||||
"拍摄药品包装的正面",
|
||||
"避免手指或其他物体遮挡",
|
||||
]);
|
||||
} else if (errorMessage.includes("置信度过低")) {
|
||||
showTip("识别建议", [
|
||||
"重新拍摄更清晰的照片",
|
||||
"同时拍摄说明书可提高准确度",
|
||||
"或选择手动输入药品信息",
|
||||
]);
|
||||
}
|
||||
|
||||
// 提供重试和手动输入选项
|
||||
showActions([
|
||||
{ text: "重新拍摄", action: retryPhoto },
|
||||
{ text: "手动输入", action: manualInput },
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
||||
## 性能优化建议
|
||||
|
||||
### 1. 图片优化
|
||||
|
||||
- 上传前压缩图片(建议最大 2MB)
|
||||
- 使用 WebP 格式减小体积
|
||||
- 限制图片尺寸(建议 1920x1080 以内)
|
||||
|
||||
### 2. 轮询优化
|
||||
|
||||
- 使用指数退避策略(2s, 3s, 5s...)
|
||||
- 设置最大轮询次数(如 30 次,约 1 分钟)
|
||||
- 超时后提示用户刷新页面
|
||||
|
||||
### 3. 用户体验优化
|
||||
|
||||
- 显示预估完成时间(约 30-60 秒)
|
||||
- 支持后台识别,用户可离开页面
|
||||
- 完成后发送推送通知
|
||||
|
||||
## 数据安全
|
||||
|
||||
1. **图片安全**:
|
||||
|
||||
- 图片 URL 应使用腾讯云 COS 临时访问凭证
|
||||
- 识别完成后可选择删除图片
|
||||
|
||||
2. **数据隐私**:
|
||||
|
||||
- 识别任务数据仅用户本人可查看
|
||||
- AI 分析结果不会共享给第三方
|
||||
- 支持用户主动删除识别历史
|
||||
|
||||
3. **任务清理**:
|
||||
- 建议定期清理 30 天前的识别任务记录
|
||||
- 可通过定时任务自动清理
|
||||
|
||||
## 最佳实践
|
||||
|
||||
### 拍照技巧
|
||||
|
||||
1. 确保光线充足,避免反光
|
||||
2. 药品名称和规格清晰可见
|
||||
3. 尽量拍摄完整的包装盒
|
||||
4. 避免手指遮挡关键信息
|
||||
5. 保持相机稳定,避免模糊
|
||||
|
||||
### 识别准确度
|
||||
|
||||
**质量控制标准**:
|
||||
|
||||
- 图片必须清晰可读(isReadable = true)
|
||||
- 识别置信度必须 ≥ 60% 才能通过
|
||||
- 置信度 60-75%:会给出警告,建议用户核对
|
||||
- 置信度 ≥ 75%:可信度较高
|
||||
|
||||
**推荐使用标准**:
|
||||
|
||||
- 正面 + 侧面:准确度约 85-90%
|
||||
- 正面 + 侧面 + 说明书:准确度约 90-95%
|
||||
- 置信度 ≥ 0.8:可直接使用
|
||||
- 置信度 0.75-0.8:建议人工核对
|
||||
- 置信度 0.6-0.75:必须人工核对
|
||||
- 置信度 < 0.6:自动识别失败,需重新拍摄
|
||||
|
||||
**安全优先原则**:
|
||||
|
||||
- 宁可识别失败,也不提供不准确的药品信息
|
||||
- 当 AI 无法确认信息准确性时,会主动返回失败
|
||||
- 用户可选择手动输入以确保用药安全
|
||||
|
||||
## 技术架构
|
||||
|
||||
### 模型选择
|
||||
|
||||
- **GLM-4V-Plus**:视觉识别模型,识别药品图片
|
||||
- **GLM-4-Flash**:文本分析模型,深度分析和结构化输出
|
||||
|
||||
### 数据流
|
||||
|
||||
1. 客户端上传图片到 COS,获取 URL
|
||||
2. 调用 `/ai-recognize` 创建任务
|
||||
3. 服务端异步调用 AI 模型进行多阶段分析
|
||||
4. 客户端轮询查询状态和结果
|
||||
5. 用户确认后创建药物记录
|
||||
|
||||
### 状态管理
|
||||
|
||||
- 使用数据库表 `t_medication_recognition_tasks` 持久化状态
|
||||
- 支持断点续传和故障恢复
|
||||
- 任务完成后保留 7 天供查询
|
||||
|
||||
## 未来规划
|
||||
|
||||
1. **功能增强**:
|
||||
|
||||
- 支持批量识别多个药品
|
||||
- 支持视频识别
|
||||
- 支持语音输入药品名称
|
||||
|
||||
2. **模型优化**:
|
||||
|
||||
- 训练专用的药品识别模型
|
||||
- 提高中文药品识别准确度
|
||||
- 支持更多药品类型
|
||||
|
||||
3. **用户体验**:
|
||||
- 支持离线识别(边缘计算)
|
||||
- 实时预览识别结果
|
||||
- 智能纠错和建议
|
||||
@@ -207,17 +207,24 @@ export class MedicationAnalysisService {
|
||||
`用户 ${userId} 的用药计划分析结果: ${JSON.stringify(medicationAnalysis, null, 2)}`,
|
||||
);
|
||||
|
||||
// 获取用户语言配置
|
||||
const languageConfig = await this.getUserLanguageConfig(userId);
|
||||
|
||||
// 没有开启的用药计划时,不调用大模型
|
||||
if (!medicationAnalysis.length) {
|
||||
const noDataMessage = languageConfig.label === 'English'
|
||||
? 'No active medication plans found. Please add or activate medications to view insights.'
|
||||
: '当前没有开启的用药计划,请先添加或激活用药后再查看解读。';
|
||||
|
||||
const result = {
|
||||
medicationAnalysis,
|
||||
keyInsights: '当前没有开启的用药计划,请先添加或激活用药后再查看解读。',
|
||||
keyInsights: noDataMessage,
|
||||
};
|
||||
await this.saveSummary(userId, summaryDate, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const prompt = this.buildMedicationSummaryPrompt(medicationAnalysis);
|
||||
const prompt = this.buildMedicationSummaryPrompt(medicationAnalysis, languageConfig);
|
||||
|
||||
try {
|
||||
const response = await this.client.chat.completions.create({
|
||||
@@ -541,6 +548,7 @@ export class MedicationAnalysisService {
|
||||
*/
|
||||
private buildMedicationSummaryPrompt(
|
||||
medications: MedicationPlanItemDto[],
|
||||
languageConfig: LanguageConfig,
|
||||
): string {
|
||||
const medicationList = medications
|
||||
.map(
|
||||
@@ -549,12 +557,19 @@ export class MedicationAnalysisService {
|
||||
)
|
||||
.join('\n');
|
||||
|
||||
return `你是一名持证的临床医生与药学专家,请基于以下正在进行中的用药方案,输出中文“重点解读”一段话(200字以内),重点围绕计划 vs 实际的执行情况给出建议。
|
||||
const languageInstruction = languageConfig.label === 'English'
|
||||
? 'Please provide your response entirely in English.'
|
||||
: '请使用简体中文输出全部内容。';
|
||||
|
||||
const wordLimit = languageConfig.label === 'English' ? '300 words' : '400字';
|
||||
|
||||
return `你是一名持证的临床医生与药学专家,请基于以下正在进行中的用药方案,输出"重点解读"一段话(${wordLimit}以内),重点围绕计划 vs 实际的执行情况给出建议。
|
||||
|
||||
用药列表:
|
||||
${medicationList}
|
||||
|
||||
写作要求:
|
||||
- 语言:${languageInstruction}
|
||||
- 口吻:专业、温和、以患者安全为先
|
||||
- 内容:结合完成率与计划总次数,点评用药依从性、潜在风险与监测要点(胃肠道、肝肾功能、血压/血糖等),提出需要复诊或调整的建议
|
||||
- 形式:只输出一段文字,不要使用列表或分段,不要重复列出药品清单,不要添加免责声明`;
|
||||
@@ -608,7 +623,6 @@ ${medication.note ? `- 用户备注:${medication.note}` : ''}
|
||||
|
||||
**语言要求**:
|
||||
${languageConfig.analysisInstruction}
|
||||
- 将所有标题、要点、提醒翻译为${languageConfig.label}(保留药品名称、成分等专有名词的常用写法),不要混用其他语言
|
||||
|
||||
**输出格式要求**:
|
||||
|
||||
@@ -803,16 +817,16 @@ ${medication.note ? `- 用户备注:${medication.note}` : ''}
|
||||
if (normalized.startsWith('en')) {
|
||||
return {
|
||||
label: 'English',
|
||||
analysisInstruction: 'Respond entirely in English. Translate every section title, bullet point and paragraph; keep emojis unchanged.',
|
||||
jsonInstruction: 'Return all JSON values in English. Keep JSON keys exactly as defined in the schema.',
|
||||
analysisInstruction: 'Please respond entirely in English. Output all section titles, bullet points, paragraphs, and reminders in English. Keep emojis unchanged. Do not mix other languages.',
|
||||
jsonInstruction: 'Return all JSON field values in English. Keep JSON keys exactly as defined in the schema.',
|
||||
unableToIdentifyMessage: 'Unable to identify the medication, please provide a more accurate name or image.',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
label: '简体中文',
|
||||
analysisInstruction: '请使用简体中文输出全部内容(包括标题、要点和提醒),不要混用其他语言。',
|
||||
jsonInstruction: '请确保 JSON 中的值使用简体中文,字段名保持英文。',
|
||||
analysisInstruction: '请使用简体中文输出全部内容(包括所有标题、要点、段落和提醒),emoji 保持不变,不要混用其他语言。',
|
||||
jsonInstruction: '请确保 JSON 中所有字段的值都使用简体中文,字段名(key)保持英文不变。',
|
||||
unableToIdentifyMessage: '无法识别药品,请提供更准确的名称或图片。',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import { JwtAuthGuard } from 'src/common/guards/jwt-auth.guard';
|
||||
import { ResponseCode } from 'src/base.dto';
|
||||
import { CosService } from './cos.service';
|
||||
import { AiReportService } from '../ai-coach/services/ai-report.service';
|
||||
import { GetAiReportHistoryQueryDto, GetAiReportHistoryResponseDto } from '../ai-coach/dto/ai-report-history.dto';
|
||||
|
||||
|
||||
@ApiTags('users')
|
||||
@@ -497,6 +498,7 @@ export class UsersController {
|
||||
data: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string', example: '550e8400-e29b-41d4-a716-446655440000' },
|
||||
imageUrl: { type: 'string', example: 'https://example.com/generated-image.png' }
|
||||
}
|
||||
}
|
||||
@@ -506,7 +508,7 @@ export class UsersController {
|
||||
async generateAiHealthReport(
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
@Body() body: { date?: string },
|
||||
): Promise<{ code: ResponseCode; message: string; data: { imageUrl: string } }> {
|
||||
): Promise<{ code: ResponseCode; message: string; data: { id: string; imageUrl: string } }> {
|
||||
try {
|
||||
this.logger.log(`生成AI健康报告请求 - 用户ID: ${user.sub}, 日期: ${body.date || '今天'}`);
|
||||
|
||||
@@ -516,6 +518,7 @@ export class UsersController {
|
||||
code: ResponseCode.SUCCESS,
|
||||
message: 'AI健康报告生成成功',
|
||||
data: {
|
||||
id: result.id,
|
||||
imageUrl: result.imageUrl,
|
||||
},
|
||||
};
|
||||
@@ -531,10 +534,78 @@ export class UsersController {
|
||||
code: ResponseCode.ERROR,
|
||||
message: `生成失败: ${(error as Error).message}`,
|
||||
data: {
|
||||
id: '',
|
||||
imageUrl: '',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的 AI 健康报告历史列表
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('ai-report/history')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '获取用户的 AI 健康报告历史列表(分页)' })
|
||||
@ApiQuery({ name: 'page', required: false, description: '页码,从1开始,默认1' })
|
||||
@ApiQuery({ name: 'pageSize', required: false, description: '每页条数,默认10,最大50' })
|
||||
@ApiQuery({ name: 'startDate', required: false, description: '开始日期,格式 YYYY-MM-DD' })
|
||||
@ApiQuery({ name: 'endDate', required: false, description: '结束日期,格式 YYYY-MM-DD' })
|
||||
@ApiQuery({ name: 'status', required: false, description: '状态筛选: pending | processing | success | failed,不传则返回所有状态' })
|
||||
@ApiResponse({ status: 200, description: '成功获取报告历史列表', type: GetAiReportHistoryResponseDto })
|
||||
async getAiReportHistory(
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
@Query() query: GetAiReportHistoryQueryDto,
|
||||
): Promise<GetAiReportHistoryResponseDto> {
|
||||
try {
|
||||
this.logger.log(`获取AI健康报告历史 - 用户ID: ${user.sub}, 页码: ${query.page}, 每页: ${query.pageSize}`);
|
||||
|
||||
const result = await this.aiReportService.getReportHistory(user.sub, {
|
||||
page: query.page,
|
||||
pageSize: query.pageSize,
|
||||
startDate: query.startDate,
|
||||
endDate: query.endDate,
|
||||
status: query.status,
|
||||
});
|
||||
|
||||
return {
|
||||
code: ResponseCode.SUCCESS,
|
||||
message: 'success',
|
||||
data: {
|
||||
records: result.records.map(r => ({
|
||||
id: r.id,
|
||||
reportDate: r.reportDate,
|
||||
imageUrl: r.imageUrl || '', // 成功记录一定有 imageUrl
|
||||
status: r.status,
|
||||
createdAt: r.createdAt,
|
||||
})),
|
||||
total: result.total,
|
||||
page: result.page,
|
||||
pageSize: result.pageSize,
|
||||
totalPages: result.totalPages,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
this.winstonLogger.error('获取AI健康报告历史失败', {
|
||||
context: 'UsersController',
|
||||
userId: user?.sub,
|
||||
error: (error as Error).message,
|
||||
stack: (error as Error).stack,
|
||||
});
|
||||
|
||||
return {
|
||||
code: ResponseCode.ERROR,
|
||||
message: `获取失败: ${(error as Error).message}`,
|
||||
data: {
|
||||
records: [],
|
||||
total: 0,
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
totalPages: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user