openai-compatible-task-master
Version:
使用MCP解析PRD文档并生成任务列表
131 lines • 4.93 kB
JavaScript
import chalk from 'chalk';
import { resolveFullPath, ensureFileExists, readJsonFile } from '../utils/path_util.js';
/**
* 解析复合ID,返回父任务ID和子任务路径
* @param compositeId 复合ID,如 "1.2.3"
* @returns 包含父任务ID和子任务路径的对象
*/
function parseCompositeId(compositeId) {
const parts = compositeId.split('.');
const parentId = parts[0];
const subTaskPath = parts.slice(1);
return { parentId, subTaskPath };
}
/**
* 递归查找子任务
* @param task 父任务
* @param subTaskPath 子任务路径,如 ["1", "2"]
* @returns 找到的子任务或null
*/
function findSubTask(task, subTaskPath) {
// 如果没有子任务路径或路径为空,则返回null
// 这是一个防御性检查,确保我们不会在没有子任务路径的情况下返回父任务
if (!subTaskPath || subTaskPath.length === 0) {
return null;
}
// 如果当前任务没有子任务,则返回null
if (!task.subTasks || task.subTasks.length === 0) {
return null;
}
// 获取当前层级的子任务索引
const currentIndex = parseInt(subTaskPath[0]) - 1; // 子任务索引从1开始,数组索引从0开始
// 检查索引是否有效
if (isNaN(currentIndex) || currentIndex < 0 || currentIndex >= task.subTasks.length) {
return null;
}
// 获取当前层级的子任务
const currentSubTask = task.subTasks[currentIndex];
// 如果已经到达路径末尾,则返回当前子任务
if (subTaskPath.length === 1) {
return currentSubTask;
}
// 否则继续递归查找下一层级的子任务
return findSubTask(currentSubTask, subTaskPath.slice(1));
}
/**
* 判断是否为复合ID
* @param taskId 任务ID
* @returns 是否为复合ID
*/
function isCompositeId(taskId) {
return taskId.includes('.');
}
export async function readTask(projectDir, tasksPath, taskId) {
try {
console.debug(chalk.yellow(`读取任务文件 - projectDir: ${projectDir}, tasksPath: ${tasksPath}`));
// 构建完整路径
const fullTasksPath = resolveFullPath(projectDir, tasksPath);
console.debug(chalk.yellow(`解析后路径 - fullTasksPath: ${fullTasksPath}`));
// 检查文件是否存在
if (!await ensureFileExists(fullTasksPath)) {
return {
task: null,
message: `任务文件不存在: ${fullTasksPath}`
};
}
// 读取任务文件
const tasksData = await readJsonFile(fullTasksPath);
// 检查是否为复合ID(如"1.1")
if (isCompositeId(taskId)) {
const { parentId, subTaskPath } = parseCompositeId(taskId);
// 确保子任务路径不为空
if (!subTaskPath || subTaskPath.length === 0) {
return {
task: null,
message: `无效的子任务ID: ${taskId}`
};
}
// 先找到父任务
const parentTask = tasksData.tasks.find(t => String(t.id) === parentId);
if (!parentTask) {
return {
task: null,
message: `未找到ID为 ${parentId} 的父任务`
};
}
// 递归查找子任务
const subTask = findSubTask(parentTask, subTaskPath);
if (!subTask) {
return {
task: null,
message: `未找到ID为 ${taskId} 的子任务`
};
}
// 确保返回任务中包含completionSummary字段(如果存在)
return {
task: {
...subTask,
// 确保completionSummary字段被保留,如果存在的话
completionSummary: subTask.completionSummary
},
message: '成功读取子任务详情'
};
}
else {
// 查找指定ID的任务,统一使用字符串比较
const task = tasksData.tasks.find(t => String(t.id) === taskId);
if (!task) {
return {
task: null,
message: `未找到ID为 ${taskId} 的任务`
};
}
// 确保返回任务中包含completionSummary字段(如果存在)
return {
task: {
...task,
// 确保completionSummary字段被保留,如果存在的话
completionSummary: task.completionSummary
},
message: '成功读取任务详情'
};
}
}
catch (error) {
return {
task: null,
message: `读取任务时发生错误: ${error.message}`
};
}
}
//# sourceMappingURL=read_task.js.map