gcx/backend/services/ai-service/src/interface/http/controllers/ai.controller.ts

62 lines
2.2 KiB
TypeScript

import { Controller, Post, Get, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AiChatService } from '../../../application/services/ai-chat.service';
import { AiCreditService } from '../../../application/services/ai-credit.service';
import { AiPricingService } from '../../../application/services/ai-pricing.service';
import { AiAnomalyService } from '../../../application/services/ai-anomaly.service';
@ApiTags('AI')
@Controller('ai')
export class AiController {
constructor(
private readonly chatService: AiChatService,
private readonly creditService: AiCreditService,
private readonly pricingService: AiPricingService,
private readonly anomalyService: AiAnomalyService,
) {}
@Post('chat')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Chat with AI assistant' })
async chat(@Body() body: { userId: string; message: string; sessionId?: string }) {
return { code: 0, data: await this.chatService.chat(body) };
}
@Post('credit/score')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Get AI credit score' })
async creditScore(@Body() body: any) {
return { code: 0, data: await this.creditService.getScore(body) };
}
@Post('pricing/suggest')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Get AI pricing suggestion' })
async pricingSuggestion(@Body() body: any) {
return { code: 0, data: await this.pricingService.getSuggestion(body) };
}
@Post('anomaly/check')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiOperation({ summary: 'Check for anomalous activity' })
async anomalyCheck(@Body() body: any) {
return { code: 0, data: await this.anomalyService.check(body) };
}
@Get('health')
@ApiOperation({ summary: 'AI service health + external agent status' })
async health() {
let agentHealthy = false;
try {
const res = await fetch(`${process.env.AI_AGENT_CLUSTER_URL || 'http://localhost:8000'}/health`);
agentHealthy = res.ok;
} catch {}
return { code: 0, data: { service: 'ai-service', status: 'ok', externalAgent: agentHealthy ? 'connected' : 'unavailable' } };
}
}