chat.routes.tsā¢4.27 kB
import { Router, Request, Response } from 'express';
import { InfrastructureService } from '../services/infrastructure.service';
import { NLPService } from '../services/nlp.service';
import { logger } from '../utils/logger';
export const createChatRoutes = (infrastructureService: InfrastructureService): Router => {
const router = Router();
const nlpService = new NLPService();
/**
* @swagger
* /api/chat/message:
* post:
* summary: Process natural language command
* tags: [Chat]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - message
* properties:
* message:
* type: string
* description: Natural language command
* responses:
* 200:
* description: Command processed successfully
* 400:
* description: Invalid message
* 500:
* description: Server error
*/
router.post('/message', async (req: Request, res: Response) => {
try {
const { message } = req.body;
if (!message || typeof message !== 'string') {
return res.status(400).json({
success: false,
message: 'A valid message is required'
});
}
logger.info(`Processing chat message: ${message}`);
const parsedCommand = nlpService.parseNaturalLanguage(message);
if (parsedCommand.intent === 'list') {
const resources = infrastructureService.getAllResources();
return res.json({
success: true,
intent: 'list',
message: 'Resources obtained successfully',
data: { resources }
});
}
if (parsedCommand.intent === 'unknown' || parsedCommand.confidence < 0.5) {
return res.json({
success: false,
intent: 'help',
message: nlpService.generateHelpResponse(message),
data: { parsedCommand }
});
}
const resourceQuery = nlpService.toResourceQuery(parsedCommand);
if (!resourceQuery) {
return res.json({
success: false,
intent: 'help',
message: 'Could not process your command. Please be more specific.',
data: { parsedCommand }
});
}
const result = await infrastructureService.processQuery(resourceQuery);
res.json({
success: result.success,
intent: parsedCommand.intent,
resourceType: parsedCommand.resourceType,
message: result.message,
data: {
resourceId: result.resourceId,
details: result.details,
parameters: parsedCommand.parameters
}
});
} catch (error: any) {
logger.error('Error processing chat message:', error);
res.status(500).json({
success: false,
message: error.message || 'Error processing the message'
});
}
});
/**
* @swagger
* /api/chat/suggestions:
* get:
* summary: Get command suggestions
* tags: [Chat]
* responses:
* 200:
* description: List of command suggestions
*/
router.get('/suggestions', (req: Request, res: Response) => {
const suggestions = [
{
command: 'Create an S3 bucket named my-application',
description: 'Create a new S3 bucket',
category: 's3'
},
{
command: 'Deploy a t2.micro EC2 instance',
description: 'Create a small EC2 instance',
category: 'ec2'
},
{
command: 'Create a DynamoDB table for users',
description: 'Create a DynamoDB table',
category: 'dynamodb'
},
{
command: 'Deploy a Lambda function in Node.js',
description: 'Create a Lambda function',
category: 'lambda'
},
{
command: 'Create an IAM role for Lambda',
description: 'Create an IAM role',
category: 'iam'
},
{
command: 'Show all resources',
description: 'List all deployed resources',
category: 'general'
}
];
res.json({ suggestions });
});
return router;
};