From 99ead8b16586163f493aebd9f962f882b2d3ca76 Mon Sep 17 00:00:00 2001 From: catlog22 Date: Tue, 9 Dec 2025 00:03:23 +0800 Subject: [PATCH] fix(tool): add smart JSON parser with Windows path handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Auto-fix Windows backslash paths in JSON input - Provide helpful hints when path escaping errors occur 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- ccw/src/commands/tool.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/ccw/src/commands/tool.js b/ccw/src/commands/tool.js index fbcc36f7..4ad4d818 100644 --- a/ccw/src/commands/tool.js +++ b/ccw/src/commands/tool.js @@ -97,6 +97,42 @@ async function readStdin() { }); } +/** + * Smart JSON parser with Windows path handling + */ +function parseJsonWithPathFix(jsonString) { + try { + // Try normal parse first + return JSON.parse(jsonString); + } catch (firstError) { + // If parsing fails, try to fix Windows paths + try { + // Pattern: "path": "X:\..." or "path":"X:\..." + const fixedJson = jsonString.replace( + /("(?:path|file|target|source|dest|destination)":\s*")([A-Za-z]:[^"]+)"/g, + (match, prefix, path) => { + // Convert backslashes to forward slashes (universal) + const fixedPath = path.replace(/\\/g, '/'); + return `${prefix}${fixedPath}"`; + } + ); + + return JSON.parse(fixedJson); + } catch (secondError) { + // If still fails, throw original error with helpful message + const errorMsg = firstError.message; + const hint = errorMsg.includes('escaped character') || errorMsg.includes('position') + ? '\n\n' + chalk.yellow('Hint: Windows paths in JSON need forward slashes or double backslashes:') + + '\n ' + chalk.green('✓ "D:/Claude_dms3/file.md"') + + '\n ' + chalk.green('✓ "D:\\\\Claude_dms3\\\\file.md"') + + '\n ' + chalk.red('✗ "D:\\Claude_dms3\\file.md"') + : ''; + + throw new Error(errorMsg + hint); + } + } +} + /** * Execute a tool with given parameters */ @@ -119,7 +155,7 @@ async function execAction(toolName, jsonInput, options) { if (jsonInput) { try { - params = JSON.parse(jsonInput); + params = parseJsonWithPathFix(jsonInput); } catch (error) { console.error(chalk.red(`Invalid JSON: ${error.message}`)); process.exit(1);