import { Injectable, Logger, NotFoundException } from '@nestjs/common'; import { ActiveRecallRepository } from './active-recall.repository'; import { ActiveRecallAnalysisWorkflow } from '../ai/workflows/active-recall-analysis.workflow'; @Injectable() export class ActiveRecallService { private readonly logger = new Logger(ActiveRecallService.name); constructor( private readonly repository: ActiveRecallRepository, private readonly analysisWorkflow: ActiveRecallAnalysisWorkflow, ) {} async findByUserId(userId: string) { return this.repository.findByUserId(userId); } async submit(userId: string, questionId: string, body: { answerText: string }) { const question = await this.repository.findById(questionId); if (!question) throw new NotFoundException('问题不存在'); const answer = await this.repository.createAnswer(userId, questionId, body); this.analysisWorkflow.execute({ userId, questionText: question.questionText, knowledgeItemContent: '', userAnswer: body.answerText, }).then((result) => { this.logger.log(`Analysis complete for answer ${answer.id}: score=${result.score}`); }).catch((err) => { this.logger.error(`Analysis failed for answer ${answer.id}: ${err.message}`); }); return answer; } }