openai-compatible-task-master
Version:
使用MCP解析PRD文档并生成任务列表
57 lines • 2.99 kB
JavaScript
import * as path from 'path';
import { parsePRDWithLLM } from '../llm/llm_parse_prd.js';
import chalk from 'chalk';
import { resolveFullPath, ensureFileExists, readFileContent, ensureDirectoryExists, writeJsonFile } from '../utils/path_util.js';
export async function parsePRD(projectDir, prdPath, tasksPath, numTasks, openaiUrl, apiKey, model, streamMode, additionalPrompts) {
// 解析完整路径
console.debug(chalk.yellow(`输入路径 - projectDir: ${projectDir}, prdPath: ${prdPath}, tasksPath: ${tasksPath}`));
let fullPrdPath = '';
let prdContent = '';
const fullTasksPath = resolveFullPath(projectDir, tasksPath);
// 判断是否为多文件输入(以|分隔)
if (prdPath.includes('|')) {
// 处理多文件输入
const filePaths = prdPath.split('|').map(p => p.trim());
console.info(chalk.yellow(`检测到多文件输入,共 ${filePaths.length} 个文件`));
// 合并所有文件内容
let combinedContent = '';
for (const filePath of filePaths) {
const fullFilePath = resolveFullPath(projectDir, filePath);
// 检查文件是否存在
if (!await ensureFileExists(fullFilePath)) {
console.error(chalk.red(`PRD文件不存在: ${fullFilePath}`));
throw new Error(`PRD文件不存在: ${fullFilePath}`);
}
// 读取文件内容并合并
const content = await readFileContent(fullFilePath);
combinedContent += `\n--- ${path.basename(filePath)} ---\n${content}\n`;
console.info(chalk.green(`✓ 已读取文件: ${fullFilePath}`));
}
// 使用组合内容
prdContent = combinedContent;
fullPrdPath = filePaths.join('|'); // 用于元数据
}
else {
// 处理单文件输入
fullPrdPath = resolveFullPath(projectDir, prdPath);
console.debug(chalk.yellow(`解析后路径 - fullPrdPath: ${fullPrdPath}, fullTasksPath: ${fullTasksPath}`));
// 检查PRD文件是否存在
if (!await ensureFileExists(fullPrdPath)) {
console.error(chalk.red(`PRD文件不存在: ${fullPrdPath}`));
throw new Error(`PRD文件不存在: ${fullPrdPath}`);
}
// 读取PRD文件内容
prdContent = await readFileContent(fullPrdPath);
}
// 调用LLM生成任务,传入streamMode和additionalPrompts
const tasksData = await parsePRDWithLLM(prdContent, fullPrdPath, numTasks, openaiUrl, apiKey, model, streamMode, additionalPrompts);
// 确保任务输出目录存在
const tasksDir = path.dirname(fullTasksPath);
console.info(chalk.yellow(`任务目录路径: ${tasksDir}`));
await ensureDirectoryExists(tasksDir);
// 异步写入任务文件
await writeJsonFile(fullTasksPath, tasksData);
console.log(chalk.green(`任务已成功生成并写入到: ${fullTasksPath}`));
return tasksData;
}
//# sourceMappingURL=parse_prd.js.map