api-server/src/modules/ai/workflows/learning-trend.workflow.ts
WangDL 597c7b2310
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 59s
feat: implement P1 AI workflows (B7-B10)
B7 Feynman evaluation: POST /ai-analysis/feynman
B8 Knowledge import: replaces DocumentImport setTimeout mock with AI
B9 Review card generation: POST /reviews/generate-cards
B10 Learning trend analysis: GET /activity/trend

4 workflows, 4 prompts, 4 schemas, all registered in AiModule.
AiAnalysisRepository made generic to handle varied result shapes.
DocumentImportService now calls KnowledgeImportWorkflow + saves to DB.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-18 10:07:57 +08:00

66 lines
2.4 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { AiGatewayService } from '../gateway/ai-gateway.service';
import { LearningTrendResultSchema } from '../prompts/schemas/learning-trend.schema';
import type { LearningTrendResult } from '../prompts/schemas/learning-trend.schema';
export interface LearningTrendInput {
userId: string;
periodDays: number;
totalMinutes: number;
sessionsCount: number;
activeRecallCount: number;
reviewCount: number;
aiAnalysisCount: number;
completedLoopCount: number;
activityLevel: number;
activeDays: number;
dailyAverage: number;
previousPeriod?: {
totalMinutes: number;
activeRecallCount: number;
reviewCount: number;
activeDays: number;
};
}
@Injectable()
export class LearningTrendWorkflow {
constructor(private readonly gateway: AiGatewayService) {}
async execute(input: LearningTrendInput): Promise<LearningTrendResult> {
const prev = input.previousPeriod;
const userMessage = [
`【学习数据概览 — 最近 ${input.periodDays} 天】`,
`总学习时长:${input.totalMinutes} 分钟`,
`学习会话数:${input.sessionsCount}`,
`主动回忆次数:${input.activeRecallCount}`,
`复习卡片数:${input.reviewCount}`,
`AI 分析次数:${input.aiAnalysisCount}`,
`完成学习循环:${input.completedLoopCount}`,
`活跃天数:${input.activeDays}`,
`日均学习:${input.dailyAverage} 分钟`,
`活跃度评分:${input.activityLevel}/10`,
'',
prev ? `【上一周期对比(${input.periodDays}天前)】` : '',
prev ? `总时长:${prev.totalMinutes}分钟 | 主动回忆:${prev.activeRecallCount}次 | 复习:${prev.reviewCount}张 | 活跃天数:${prev.activeDays}` : '',
prev ? '' : '',
`请根据以上数据生成学习趋势分析报告。`,
prev ? '注意对比两个周期的变化趋势。' : '注意这是首次分析,没有历史对比数据,请关注绝对值和建议。',
].join('\n');
const response = await this.gateway.generate({
feature: 'learning-trend',
userId: input.userId,
tier: 'primary',
promptKey: 'learning-trend',
promptVersion: '1.0.0',
messages: [
{ role: 'user', content: userMessage },
],
outputSchema: LearningTrendResultSchema,
});
return response.parsed as unknown as LearningTrendResult;
}
}