|
| 1 | +/** |
| 2 | + * Edit Corrector - 编辑纠错工具 |
| 3 | + * |
| 4 | + * 提供多种策略自动修复 LLM 在调用 Edit 工具时的常见错误: |
| 5 | + * - 转义字符过多(\\n → \n) |
| 6 | + * - 缩进不匹配(2 空格 vs 4 空格) |
| 7 | + * - 引号类型差异(智能引号 vs 普通引号) |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * 匹配策略枚举 |
| 12 | + */ |
| 13 | +export enum MatchStrategy { |
| 14 | + EXACT = 'exact', // 精确匹配 |
| 15 | + NORMALIZE_QUOTES = 'normalize_quotes', // 引号标准化匹配 |
| 16 | + UNESCAPE = 'unescape', // 反转义匹配 |
| 17 | + FLEXIBLE = 'flexible', // 弹性缩进匹配 |
| 18 | + FAILED = 'failed', // 所有策略都失败 |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * 匹配结果 |
| 23 | + */ |
| 24 | +export interface MatchResult { |
| 25 | + matched: string | null; // 匹配到的实际字符串(保持原文件格式) |
| 26 | + strategy: MatchStrategy; // 使用的匹配策略 |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * 反转义字符串 |
| 31 | + * 修复 LLM 过度转义的问题 |
| 32 | + * |
| 33 | + * @param input 输入字符串 |
| 34 | + * @returns 反转义后的字符串 |
| 35 | + * |
| 36 | + * @example |
| 37 | + * unescapeString('line1\\nline2') // → 'line1\nline2' |
| 38 | + * unescapeString('say \\"hello\\"') // → 'say "hello"' |
| 39 | + * unescapeString('\\`template\\`') // → '`template`' |
| 40 | + */ |
| 41 | +export function unescapeString(input: string): string { |
| 42 | + // 正则说明: |
| 43 | + // \\+ : 匹配一个或多个反斜杠 |
| 44 | + // (n|t|r|'|"|`|\\|\n) : 捕获组,匹配需要转义的字符 |
| 45 | + // - n, t, r: 字面字符(需要转换为 \n, \t, \r) |
| 46 | + // - ', ", `: 引号字符 |
| 47 | + // - \\: 反斜杠本身 |
| 48 | + // - \n: 真实的换行符 |
| 49 | + // g: 全局标志,替换所有匹配 |
| 50 | + |
| 51 | + return input.replace( |
| 52 | + /\\+(n|t|r|'|"|`|\\|\n)/g, |
| 53 | + (match, capturedChar) => { |
| 54 | + switch (capturedChar) { |
| 55 | + case 'n': |
| 56 | + return '\n'; // 换行符 |
| 57 | + case 't': |
| 58 | + return '\t'; // 制表符 |
| 59 | + case 'r': |
| 60 | + return '\r'; // 回车符 |
| 61 | + case "'": |
| 62 | + return "'"; // 单引号 |
| 63 | + case '"': |
| 64 | + return '"'; // 双引号 |
| 65 | + case '`': |
| 66 | + return '`'; // 反引号 |
| 67 | + case '\\': |
| 68 | + return '\\'; // 反斜杠 |
| 69 | + case '\n': |
| 70 | + return '\n'; // 真实换行符 |
| 71 | + default: |
| 72 | + return match; // 保持原样 |
| 73 | + } |
| 74 | + } |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * 弹性缩进匹配 |
| 80 | + * 忽略缩进差异,在文件内容中查找匹配的字符串 |
| 81 | + * |
| 82 | + * @param content 文件内容 |
| 83 | + * @param searchString 要搜索的字符串 |
| 84 | + * @returns 匹配到的实际字符串(保持原文件缩进),如果未找到则返回 null |
| 85 | + * |
| 86 | + * @example |
| 87 | + * const content = ' function foo() {\n return 1;\n }'; |
| 88 | + * const search = ' function foo() {\n return 1;\n }'; |
| 89 | + * flexibleMatch(content, search) // → ' function foo() {\n return 1;\n }' |
| 90 | + */ |
| 91 | +export function flexibleMatch( |
| 92 | + content: string, |
| 93 | + searchString: string |
| 94 | +): string | null { |
| 95 | + const searchLines = searchString.split('\n'); |
| 96 | + |
| 97 | + // 如果只有一行,无法使用弹性匹配 |
| 98 | + if (searchLines.length === 1) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + // 1. 提取搜索字符串的第一行缩进 |
| 103 | + const firstLine = searchLines[0]; |
| 104 | + const indentMatch = firstLine.match(/^(\s+)/); |
| 105 | + |
| 106 | + if (!indentMatch) { |
| 107 | + return null; // 第一行没有缩进,无法使用弹性匹配 |
| 108 | + } |
| 109 | + |
| 110 | + const searchIndent = indentMatch[1]; |
| 111 | + |
| 112 | + // 2. 去除搜索字符串的缩进 |
| 113 | + const deindentedSearchLines = searchLines.map(line => { |
| 114 | + if (line.startsWith(searchIndent)) { |
| 115 | + return line.slice(searchIndent.length); |
| 116 | + } |
| 117 | + return line; |
| 118 | + }); |
| 119 | + const deindentedSearch = deindentedSearchLines.join('\n'); |
| 120 | + |
| 121 | + // 3. 在文件内容中搜索 |
| 122 | + const contentLines = content.split('\n'); |
| 123 | + |
| 124 | + // 尝试在每个可能的位置匹配 |
| 125 | + for (let i = 0; i <= contentLines.length - searchLines.length; i++) { |
| 126 | + const lineIndentMatch = contentLines[i].match(/^(\s+)/); |
| 127 | + const fileIndent = lineIndentMatch ? lineIndentMatch[1] : ''; |
| 128 | + |
| 129 | + // 提取从当前行开始的内容片段(与搜索字符串行数相同) |
| 130 | + const snippet = contentLines.slice(i, i + searchLines.length); |
| 131 | + |
| 132 | + // 去除文件片段的缩进 |
| 133 | + const deindentedSnippet = snippet.map(line => { |
| 134 | + if (line.startsWith(fileIndent)) { |
| 135 | + return line.slice(fileIndent.length); |
| 136 | + } |
| 137 | + return line; |
| 138 | + }); |
| 139 | + const deindentedContent = deindentedSnippet.join('\n'); |
| 140 | + |
| 141 | + // 如果去除缩进后完全匹配 |
| 142 | + if (deindentedContent === deindentedSearch) { |
| 143 | + // 返回原文件中的实际字符串(保持原始缩进) |
| 144 | + return snippet.join('\n'); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return null; |
| 149 | +} |
0 commit comments