text_formatter
Format text files by applying proper indentation and line spacing to improve readability and structure.
Instructions
Format text with proper indentation and line spacing
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to the input text file | |
| outputDir | Yes | Directory where formatted file should be saved |
Implementation Reference
- src/tools/txtTools.ts:160-201 (handler)Implements the core logic for the 'text_formatter' tool: reads UTF-8 text file, formats by trimming lines and removing consecutive empty lines, generates unique output filename, saves to specified directory.export async function formatText(inputPath: string, outputDir: string) { try { console.error(`Starting text formatting...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); // 確保輸出目錄存在 try { await fs.access(outputDir); console.error(`Output directory exists: ${outputDir}`); } catch { console.error(`Creating output directory: ${outputDir}`); await fs.mkdir(outputDir, { recursive: true }); console.error(`Created output directory: ${outputDir}`); } const uniqueId = generateUniqueId(); const content = await fs.readFile(inputPath, "utf-8"); // 基本格式化:移除多餘空白行,統一縮排 const formatted = content .split("\n") .map((line) => line.trim()) .filter((line, index, array) => !(line === "" && array[index - 1] === "")) .join("\n"); const outputPath = path.join(outputDir, `formatted_${uniqueId}.txt`); await fs.writeFile(outputPath, formatted); console.error(`Written formatted text to ${outputPath}`); return { success: true, data: `Successfully formatted text: ${outputPath}`, }; } catch (error) { console.error(`Error in formatText:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/txtTools.ts:41-58 (schema)Defines the Tool object for 'text_formatter' including name, description, and inputSchema specifying required inputPath and outputDir parameters.export const TEXT_FORMAT_TOOL: Tool = { name: "text_formatter", description: "Format text with proper indentation and line spacing", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input text file", }, outputDir: { type: "string", description: "Directory where formatted file should be saved", }, }, required: ["inputPath", "outputDir"], }, };
- src/index.ts:265-281 (registration)Registers and dispatches 'text_formatter' tool calls within the main CallToolRequest handler by invoking the formatText function and formatting the response.if (name === "text_formatter") { const { inputPath, outputDir } = args as { inputPath: string; outputDir: string; }; const result = await formatText(inputPath, outputDir); if (!result.success) { return { content: [{ type: "text", text: `Error: ${result.error}` }], isError: true, }; } return { content: [{ type: "text", text: fileOperationResponse(result.data) }], isError: false, }; }
- src/tools/_index.ts:9-9 (registration)Includes TEXT_FORMAT_TOOL in the exported 'tools' array used by ListToolsRequest to advertise available tools.export const tools = [DOCUMENT_READER_TOOL, PDF_MERGE_TOOL, PDF_SPLIT_TOOL, DOCX_TO_PDF_TOOL, DOCX_TO_HTML_TOOL, HTML_CLEAN_TOOL, HTML_TO_TEXT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, TEXT_DIFF_TOOL, TEXT_SPLIT_TOOL, TEXT_FORMAT_TOOL, TEXT_ENCODING_CONVERT_TOOL, EXCEL_READ_TOOL, FORMAT_CONVERTER_TOOL];
- src/tools/txtTools.ts:8-10 (helper)Helper function to generate unique IDs for output filenames, used in the text_formatter handler.function generateUniqueId(): string { return randomBytes(9).toString("hex"); }