/** * 读取根目录下的文本文件 */ const path = require('path') const fs = require('fs') const projectRoot = path.resolve(__dirname, '..', '..', '..') /** 构建绝对路径:绝对路径直接返回,否则相对于 folderPath 或项目根 */ function buildFilePath(filePath, folderPath) { if (filePath.startsWith('/') || filePath.includes(':')) return filePath return folderPath ? path.join(folderPath, filePath) : filePath } function readTextFile(filePath) { const fullPath = path.isAbsolute(filePath) ? filePath : path.resolve(projectRoot, filePath) const content = fs.readFileSync(fullPath, 'utf8') return { success: true, content } } /** 执行读取文本文件 */ async function executeReadTxt({ filePath, folderPath }) { if (!filePath) return { success: false, error: 'read-txt 缺少 filePath 参数' } const absoluteFilePath = buildFilePath(filePath, folderPath) const result = readTextFile(absoluteFilePath) return { success: true, content: result.content || '' } } module.exports = { executeReadTxt, readTextFile }