fix(analyze-complexity): pass projectRoot through analyze-complexity flow

Modified analyze-task-complexity.js core function, direct function, and analyze.js tool to correctly pass projectRoot. Fixed import error in tools/index.js. Added debug logging to _resolveApiKey in ai-services-unified.js. This enables the .env API key fallback for analyze_project_complexity.
This commit is contained in:
Eyal Toledano
2025-05-01 14:18:44 -04:00
parent 2a07d366be
commit c7158d4910
5 changed files with 401 additions and 344 deletions

View File

@@ -4,120 +4,142 @@
*/
import { z } from 'zod';
import { handleApiResult, createErrorResponse } from './utils.js';
import { analyzeTaskComplexityDirect } from '../core/direct-functions/analyze-task-complexity.js';
import { findTasksJsonPath } from '../core/utils/path-utils.js';
import path from 'path';
import fs from 'fs';
import fs from 'fs'; // Import fs for directory check/creation
import {
handleApiResult,
createErrorResponse,
getProjectRootFromSession // Assuming this is in './utils.js' relative to this file
} from './utils.js';
import { analyzeTaskComplexityDirect } from '../core/task-master-core.js'; // Assuming core functions are exported via task-master-core.js
import { findTasksJsonPath } from '../core/utils/path-utils.js';
/**
* Register the analyze tool with the MCP server
* Register the analyze_project_complexity tool
* @param {Object} server - FastMCP server instance
*/
export function registerAnalyzeTool(server) {
export function registerAnalyzeProjectComplexityTool(server) {
server.addTool({
name: 'analyze_project_complexity',
description:
'Analyze task complexity and generate expansion recommendations',
'Analyze task complexity and generate expansion recommendations.',
parameters: z.object({
threshold: z.coerce // Use coerce for number conversion from string if needed
.number()
.int()
.min(1)
.max(10)
.optional()
.default(5) // Default threshold
.describe('Complexity score threshold (1-10) to recommend expansion.'),
research: z
.boolean()
.optional()
.default(false)
.describe('Use Perplexity AI for research-backed analysis.'),
output: z
.string()
.optional()
.describe(
'Output file path relative to project root (default: scripts/task-complexity-report.json)'
),
threshold: z.coerce
.number()
.min(1)
.max(10)
.optional()
.describe(
'Minimum complexity score to recommend expansion (1-10) (default: 5)'
'Output file path relative to project root (default: scripts/task-complexity-report.json).'
),
file: z
.string()
.optional()
.describe(
'Absolute path to the tasks file in the /tasks folder inside the project root (default: tasks/tasks.json)'
'Path to the tasks file relative to project root (default: tasks/tasks.json).'
),
research: z
.boolean()
.optional()
.default(false)
.describe('Use research role for complexity analysis'),
projectRoot: z
.string()
.describe('The directory of the project. Must be an absolute path.')
}),
execute: async (args, { log, session }) => {
const toolName = 'analyze_project_complexity'; // Define tool name for logging
try {
log.info(
`Executing analyze_project_complexity tool with args: ${JSON.stringify(args)}`
`Executing ${toolName} tool with args: ${JSON.stringify(args)}`
);
// 1. Get Project Root (Mandatory for this tool)
const rootFolder = args.projectRoot;
if (!rootFolder) {
return createErrorResponse('projectRoot is required.');
}
if (!path.isAbsolute(rootFolder)) {
return createErrorResponse('projectRoot must be an absolute path.');
if (!rootFolder || !path.isAbsolute(rootFolder)) {
log.error(
`${toolName}: projectRoot is required and must be absolute.`
);
return createErrorResponse(
'projectRoot is required and must be absolute.'
);
}
log.info(`${toolName}: Project root: ${rootFolder}`);
// 2. Resolve Paths relative to projectRoot
let tasksJsonPath;
try {
// Note: findTasksJsonPath expects 'file' relative to root, or absolute
tasksJsonPath = findTasksJsonPath(
{ projectRoot: rootFolder, file: args.file },
{ projectRoot: rootFolder, file: args.file }, // Pass root and optional relative file path
log
);
log.info(`${toolName}: Resolved tasks path: ${tasksJsonPath}`);
} catch (error) {
log.error(`Error finding tasks.json: ${error.message}`);
log.error(`${toolName}: Error finding tasks.json: ${error.message}`);
return createErrorResponse(
`Failed to find tasks.json within project root '${rootFolder}': ${error.message}`
);
}
const outputPath = args.output
? path.resolve(rootFolder, args.output)
: path.resolve(rootFolder, 'scripts', 'task-complexity-report.json');
? path.resolve(rootFolder, args.output) // Resolve relative output path
: path.resolve(rootFolder, 'scripts', 'task-complexity-report.json'); // Default location resolved relative to root
log.info(`${toolName}: Report output path: ${outputPath}`);
// Ensure output directory exists
const outputDir = path.dirname(outputPath);
try {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
log.info(`Created output directory: ${outputDir}`);
log.info(`${toolName}: Created output directory: ${outputDir}`);
}
} catch (dirError) {
log.error(
`Failed to create output directory ${outputDir}: ${dirError.message}`
`${toolName}: Failed to create output directory ${outputDir}: ${dirError.message}`
);
return createErrorResponse(
`Failed to create output directory: ${dirError.message}`
);
}
// 3. Call Direct Function - Pass projectRoot in first arg object
const result = await analyzeTaskComplexityDirect(
{
// Pass resolved absolute paths and other args
tasksJsonPath: tasksJsonPath,
outputPath: outputPath,
outputPath: outputPath, // Pass resolved absolute path
threshold: args.threshold,
research: args.research
research: args.research,
projectRoot: rootFolder // <<< Pass projectRoot HERE
},
log,
{ session }
{ session } // Pass context object with session
);
if (result.success) {
log.info(`Tool analyze_project_complexity finished successfully.`);
} else {
log.error(
`Tool analyze_project_complexity failed: ${result.error?.message || 'Unknown error'}`
);
}
return handleApiResult(result, log, 'Error analyzing task complexity');
// 4. Handle Result
log.info(
`${toolName}: Direct function result: success=${result.success}`
);
return handleApiResult(
result,
log,
'Error analyzing task complexity' // Consistent error prefix
);
} catch (error) {
log.error(`Critical error in analyze tool execute: ${error.message}`);
return createErrorResponse(`Internal tool error: ${error.message}`);
log.error(
`Critical error in ${toolName} tool execute: ${error.message}`
);
return createErrorResponse(
`Internal tool error (${toolName}): ${error.message}`
);
}
}
});