Files
reason-flow/server/controllers/queryModelController.js
T
2025-11-06 11:08:59 +01:00

350 lines
7.7 KiB
JavaScript

const queryModelService = require('../services/queryModelService');
const { Plan, ToolExecution, Conversation, Message } = require('../models');
const logger = require('../utils/logger');
const executePlan = async (req, res) => {
try {
const { planId, options = {} } = req.body;
const userId = req.user.userId;
if (!planId) {
return res.status(400).json({
success: false,
error: 'Plan ID is required'
});
}
// Verify plan belongs to user
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'
});
}
if (plan.status !== 'approved') {
return res.status(400).json({
success: false,
error: 'Plan must be approved before execution'
});
}
// Execute plan using QUERYMODEL
const result = await queryModelService.executePlan(planId, options);
logger.info(`QUERYMODEL execution completed for plan: ${planId}`);
res.json({
success: true,
data: result
});
} catch (error) {
logger.error('QUERYMODEL execution error:', error);
res.status(500).json({
success: false,
error: error.message
});
}
};
const executeTool = async (req, res) => {
try {
const { planId, toolName, toolType, inputParameters } = req.body;
const userId = req.user.userId;
if (!planId || !toolName || !toolType || !inputParameters) {
return res.status(400).json({
success: false,
error: 'Plan ID, tool name, type, and input parameters are required'
});
}
// Verify plan belongs to user
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'
});
}
// Execute tool using QUERYMODEL
const result = await queryModelService.executeTool(toolName, toolType, inputParameters, planId);
logger.info(`Tool executed: ${toolName} for plan: ${planId}`);
res.json({
success: true,
data: { toolExecution: result }
});
} catch (error) {
logger.error('Execute tool error:', error);
res.status(500).json({
success: false,
error: 'Internal server error'
});
}
};
const orchestratePlan = async (req, res) => {
try {
const { planId, options = {} } = req.body;
const userId = req.user.userId;
if (!planId) {
return res.status(400).json({
success: false,
error: 'Plan ID is required'
});
}
// Verify plan belongs to user
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'
});
}
if (plan.status !== 'approved') {
return res.status(400).json({
success: false,
error: 'Plan must be approved before orchestration'
});
}
// Get the original user query from the conversation
const originalMessage = await Message.findOne({
where: {
conversation_id: plan.conversation_id,
role: 'user'
},
order: [['created_at', 'DESC']]
});
const originalQuery = originalMessage ? originalMessage.content : plan.title;
// Execute orchestration using QUERYMODEL
const result = await queryModelService.executeOrchestrate({
query: originalQuery,
category: 'engineering',
topK: options.topK || 5,
generateReport: true
});
logger.info(`QUERYMODEL orchestration completed for plan: ${planId}`);
res.json({
success: true,
data: { toolExecution: result }
});
} catch (error) {
logger.error('QUERYMODEL orchestration error:', error);
res.status(500).json({
success: false,
error: error.message
});
}
};
const getExecutionStatus = 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'
});
}
// Get tool executions for this plan
const toolExecutions = await ToolExecution.findAll({
where: {
plan_id: planId
},
order: [['created_at', 'DESC']]
});
res.json({
success: true,
data: {
plan,
toolExecutions
}
});
} catch (error) {
logger.error('Get execution status error:', error);
res.status(500).json({
success: false,
error: 'Internal server error'
});
}
};
const getToolExecutions = async (req, res) => {
try {
const { planId, toolType, status, page = 1, limit = 10 } = req.query;
const userId = req.user.userId;
const whereClause = {};
if (planId) whereClause.plan_id = planId;
if (toolType) whereClause.tool_type = toolType;
if (status) whereClause.status = status;
// Verify plan belongs to user if planId provided
if (planId) {
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 offset = (page - 1) * limit;
const { count, rows: executions } = await ToolExecution.findAndCountAll({
where: whereClause,
include: [
{
model: Plan,
where: {
conversation: {
user_id: userId
}
},
include: [
{
model: Conversation,
attributes: ['id', 'title', 'user_id']
}
]
}
],
order: [['created_at', 'DESC']],
limit: parseInt(limit),
offset: parseInt(offset)
});
res.json({
success: true,
data: {
executions,
pagination: {
page: parseInt(page),
limit: parseInt(limit),
total: count,
pages: Math.ceil(count / limit)
}
}
});
} catch (error) {
logger.error('Get tool executions error:', error);
res.status(500).json({
success: false,
error: 'Internal server error'
});
}
};
const getModelStatus = async (req, res) => {
try {
const status = await queryModelService.getModelStatus();
res.json({
success: true,
data: status
});
} catch (error) {
logger.error('QUERYMODEL status error:', error);
res.status(500).json({
success: false,
error: error.message
});
}
};
module.exports = {
executePlan,
executeTool,
orchestratePlan,
getExecutionStatus,
getToolExecutions,
getModelStatus
};