199 lines
4.3 KiB
JavaScript
199 lines
4.3 KiB
JavaScript
const model1Service = require('../services/model1Service');
|
|
const { Plan, Conversation } = require('../models');
|
|
const logger = require('../utils/logger');
|
|
|
|
const generatePlan = async (req, res) => {
|
|
try {
|
|
const { query, conversationId, context = {} } = req.body;
|
|
const userId = req.user.userId;
|
|
|
|
if (!query) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Query is required'
|
|
});
|
|
}
|
|
|
|
// Verify conversation belongs to user
|
|
if (conversationId) {
|
|
const conversation = await Conversation.findOne({
|
|
where: {
|
|
id: conversationId,
|
|
user_id: userId
|
|
}
|
|
});
|
|
|
|
if (!conversation) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: 'Conversation not found'
|
|
});
|
|
}
|
|
}
|
|
|
|
// Generate plan using MODEL1
|
|
const planData = await model1Service.generatePlan(query, context);
|
|
|
|
// Save plan if conversationId provided
|
|
let savedPlan = null;
|
|
if (conversationId) {
|
|
savedPlan = await model1Service.savePlan(planData, conversationId, userId);
|
|
}
|
|
|
|
logger.info(`MODEL1 plan generated for user: ${userId}`);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
plan: savedPlan || planData,
|
|
processingTime: planData.processingTime,
|
|
tokensUsed: planData.tokensUsed,
|
|
model: planData.model
|
|
}
|
|
});
|
|
} catch (error) {
|
|
logger.error('MODEL1 plan generation error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
};
|
|
|
|
const validatePlan = async (req, res) => {
|
|
try {
|
|
const { planId } = req.params;
|
|
const { feedback = [] } = req.body;
|
|
|
|
const result = await model1Service.validatePlan(planId, feedback);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result
|
|
});
|
|
} catch (error) {
|
|
logger.error('MODEL1 plan validation error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
};
|
|
|
|
const getPlan = async (req, res) => {
|
|
try {
|
|
const { planId } = req.params;
|
|
const userId = req.user.userId;
|
|
|
|
const plan = await Plan.findOne({
|
|
where: {
|
|
id: planId
|
|
},
|
|
include: [
|
|
{
|
|
model: Conversation,
|
|
where: {
|
|
user_id: userId
|
|
},
|
|
attributes: ['id', 'title', 'user_id']
|
|
}
|
|
]
|
|
});
|
|
|
|
if (!plan) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: 'Plan not found'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { plan }
|
|
});
|
|
} catch (error) {
|
|
logger.error('Get plan error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Internal server error'
|
|
});
|
|
}
|
|
};
|
|
|
|
const updatePlan = async (req, res) => {
|
|
try {
|
|
const { planId } = req.params;
|
|
const { title, description, steps, status, approvalFeedback } = req.body;
|
|
const userId = req.user.userId;
|
|
|
|
const plan = await Plan.findOne({
|
|
where: {
|
|
id: planId
|
|
},
|
|
include: [
|
|
{
|
|
model: Conversation,
|
|
where: {
|
|
user_id: userId
|
|
},
|
|
attributes: ['id', 'title', 'user_id']
|
|
}
|
|
]
|
|
});
|
|
|
|
if (!plan) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: 'Plan not found'
|
|
});
|
|
}
|
|
|
|
const updateData = {};
|
|
if (title) updateData.title = title;
|
|
if (description) updateData.description = description;
|
|
if (steps) updateData.steps = steps;
|
|
if (status) updateData.status = status;
|
|
if (approvalFeedback) updateData.approval_feedback = approvalFeedback;
|
|
|
|
await plan.update(updateData);
|
|
|
|
logger.info(`Plan updated: ${planId}, status: ${status}`);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { plan }
|
|
});
|
|
} catch (error) {
|
|
logger.error('Update plan error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Internal server error'
|
|
});
|
|
}
|
|
};
|
|
|
|
const getModelStatus = async (req, res) => {
|
|
try {
|
|
const status = await model1Service.getModelStatus();
|
|
|
|
res.json({
|
|
success: true,
|
|
data: status
|
|
});
|
|
} catch (error) {
|
|
logger.error('MODEL1 status error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
generatePlan,
|
|
validatePlan,
|
|
getPlan,
|
|
updatePlan,
|
|
getModelStatus
|
|
};
|