diff --git a/src/modules/ai/ai.controller.ts b/src/modules/ai/ai.controller.ts index a990781..84c6163 100644 --- a/src/modules/ai/ai.controller.ts +++ b/src/modules/ai/ai.controller.ts @@ -1,45 +1,39 @@ -import { Controller, Post, Get, Body } from '@nestjs/common'; -import { ApiTags, ApiOperation } from '@nestjs/swagger'; -import { ActiveRecallAnalysisWorkflow } from './workflows/active-recall-analysis.workflow'; +import { Controller, Get, UseGuards } from '@nestjs/common'; +import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; import { ModelRouter } from './model-router'; -import { Public } from '../../common/decorators/public.decorator'; +import { PromptTemplateService } from './prompts/prompt-template.service'; +import { AiUsageLogService } from './usage/ai-usage-log.service'; +import { AdminAuthGuard } from '../../common/guards/admin-auth.guard'; +import { AdminRolesGuard } from '../../common/guards/admin-roles.guard'; +import { AdminRoles } from '../../common/decorators/admin-roles.decorator'; +import type { AdminRole } from '../../common/types/admin-role.enum'; -@ApiTags('ai') -@Controller('ai') -export class AiController { +@ApiTags('admin-ai-gateway') +@Controller('admin-api/ai-gateway') +@UseGuards(AdminAuthGuard, AdminRolesGuard) +@ApiBearerAuth() +export class AdminAiGatewayController { constructor( - private readonly workflow: ActiveRecallAnalysisWorkflow, - private readonly modelRouter: ModelRouter, + private readonly router: ModelRouter, + private readonly prompts: PromptTemplateService, + private readonly usage: AiUsageLogService, ) {} - @Post('analyze-recall') - @ApiOperation({ summary: '分析主动回忆回答' }) - async analyzeRecall(@Body() body: { - questionText: string; - knowledgeItemContent: string; - userAnswer: string; - userId?: string; - }) { - return this.workflow.execute({ - userId: body.userId || 'anonymous', - questionText: body.questionText, - knowledgeItemContent: body.knowledgeItemContent, - userAnswer: body.userAnswer, - }); + @Get('status') + @AdminRoles('SUPER_ADMIN' as AdminRole) + @ApiOperation({ summary: 'AI Gateway 状态' }) + async status() { + return { + providers: ['deepseek', 'minimax'], + tiers: this.router['tiers'], + prompts: this.prompts.list?.() || [], + }; } - @Public() - @Get('models') - @ApiOperation({ summary: '查看可用模型与分流策略' }) - getModels() { - return ['cheap', 'primary', 'strong'].map((tier) => { - const config = this.modelRouter.resolve(tier as any); - return { - tier, - preferred: config.preferred, - fallback: config.fallback, - maxRetries: config.maxRetries, - }; - }); + @Get('usage') + @AdminRoles('SUPER_ADMIN' as AdminRole) + @ApiOperation({ summary: 'AI 调用日志' }) + async usageLog() { + return this.usage['prisma']?.aiUsageLog?.findMany?.({ orderBy: { createdAt: 'desc' }, take: 50 }) || []; } }