/** * ============================================================ * src/tools/edit/multi-edit.ts —— MultiEdit 工具(原子化多处替换) * ------------------------------------------------------------ * 对应 kakadeai 主仓库的 FileEditTool.getPatchForEdits 设计思路, * 但比上游更严格:**all-or-nothing**。 * * 与 Edit 的关键差异: * 1. 一次处理多条 edit,按顺序在内存中应用,全部成功才写盘 * 2. 任何一条匹配失败 / 多义匹配 → 全部回退(磁盘不动一字) * 3. 依赖检测:edits[i].old_string 若是 edits[j; // ---------------- 2. JSON Schema ---------------- const parameters = toToolParameters(inputSchema); // ---------------- 3. Description ---------------- const description = `Performs multiple exact string replacements in a single file, applied atomically (all-or-nothing). Usage: - You MUST use your \`Read\` tool to read the current content of the file BEFORE calling MultiEdit. - Provide a list of \`edits\`, each with \`old_string\`, \`new_string\`, and optional \`replace_all\`. - All edits are applied sequentially in memory; if ANY edit fails (string not found, ambiguous match, or dependency conflict), the file on disk is UNTOUCHED. - Order matters: later edits operate on the result of earlier edits. If a later edit's \`old_string\` is a substring of an earlier edit's \`new_string\`, MultiEdit refuses (reorder or merge instead). - Each \`old_string\` must be unique in the current content (after prior edits) unless \`replace_all=true\`. - Empty \`old_string\` is NOT allowed in MultiEdit. To create a new file, use the \`Edit\` tool with a single empty-old_string call. - Preserve exact indentation (tabs/spaces).`; // ---------------- 4. call() ---------------- async function call(input: MultiEditInput, state?: FileReadState, cwd?: string): Promise { // 4a. 路径规范化 + 安全校验 const pathResult = resolveToolPath(input.file_path, cwd); if (!pathResult.ok) return pathResult; const filePath = pathResult.resolvedPath; // 4b. Pre-read guard —— 入口处一次性检查 + 注入 snippet(循环内多个 edit 不重复注入) const freshness = await assertFresh(filePath, {}, state); if (!freshness.ok) { const snap = await tryAutoSnapshot(filePath); if (snap.ok) { recordRead(filePath, { truncated: snap.truncated, partial: snap.truncated }, state); const tail = snap.truncated ? `\n\n⚠️ 仅注入了前 200 行(文件共 ${snap.totalLines} 行 / 上限 32KB)。若要改的位置在范围外,请显式 Read offset=X limit=Y 后再 MultiEdit。` : ''; return { ok: false, error: freshness.error + `\n\n以下是当前文件${snap.truncated ? '前 200 行' : '全部'}内容(带行号),请基于此重新构造各 edit 的 old_string 后立即重试 MultiEdit,不要再单独调 Read:\n\n${snap.snippet}` + tail, }; } const why = snap.reason === 'too-large' ? `文件 ${((snap.sizeBytes ?? 0) / 1024).toFixed(1)} KB 超过 32KB 自动注入上限` : snap.reason === 'binary' ? '文件命中二进制扩展名黑名单' : '文件读取失败'; return { ok: false, error: `${freshness.error}\n\n(${why},未注入内容快照,请用 Read 工具显式读取后再 MultiEdit)`, }; } // 4c. 文件必须存在(MultiEdit 不做"创建新文件"语义) if (!existsSync(filePath)) { return { ok: false, error: `文件不存在:${filePath}\n(MultiEdit 不支持创建新文件,请改用 Edit 工具)`, }; } // 4d. 依赖检测:edits[i].old_string 若是 edits[j>; let sourceEncoding: Awaited>['encoding']; try { const snapshot = await readEditSnapshot(filePath); editSnapshot = snapshot; originalContent = snapshot.content; sourceEncoding = snapshot.encoding; } catch (e) { return { ok: false, error: `读取失败:${(e as Error).message}` }; } // 4f. 在内存中顺序应用所有 edit(任一失败立即返回,磁盘不动) let currentContent = originalContent; let replacements = 0; let unchanged = 0; const lineEnding = detectLineEndingsForString(originalContent); const truncHint = wasTruncated(filePath, state) ? `\n\n⚠️ 上次 Read 该文件时被截断了(只看到部分内容)。当前 old_string 可能在截断范围外。请用更小的 limit + offset 精确读取目标行号附近后再 MultiEdit。` : ''; for (let i = 0; i < edits.length; i++) { const edit = edits[i]!; const replaceAll = edit.replace_all ?? false; // 引号风格归一化 let searchTarget = applyLineEnding(edit.old_string, lineEnding); let processedNewString = applyLineEnding(edit.new_string, lineEnding); const actualOld = findActualString(currentContent, searchTarget); if (actualOld === null) { return { ok: false, error: `edits[${i}] 未匹配到 old_string(在已应用前序 ${i} 处修改后的内容中找不到)。请先用 Read 工具核对当前内容(注意空格/缩进/换行),并检查 edit 顺序。${truncHint}`, }; } if (actualOld !== searchTarget) { processedNewString = preserveQuoteStyle(searchTarget, actualOld, processedNewString); searchTarget = actualOld; } const occurrences = countOccurrences(currentContent, searchTarget); if (occurrences === 0) { return { ok: false, error: `edits[${i}] 未匹配到 old_string(已应用前序 ${i} 处修改后内容中出现 0 次)。${truncHint}`, }; } if (occurrences > 1 && !replaceAll) { return { ok: false, error: `edits[${i}].old_string 在当前内容中出现 ${occurrences} 次,不唯一。请扩大 old_string 包含更多上下文,或显式传 replace_all=true。${truncHint}`, }; } if (searchTarget === processedNewString) { unchanged++; continue; } replacements += replaceAll ? occurrences : 1; currentContent = replaceAll ? splitReplaceAll(currentContent, searchTarget, processedNewString) : currentContent.replace(searchTarget, () => processedNewString); } // Replacement strings were normalized above; do not rewrite untouched lines. const finalContent = currentContent; try { await commitEdit(filePath, editSnapshot, finalContent); recordRead(filePath, { contentHash: hashFileBytes(encodeTextFile(finalContent, sourceEncoding)), }, state); emitFileMutation(filePath); } catch (e) { return { ok: false, error: `写入失败:${(e as Error).message}` }; } const patch = buildPatchSummary(originalContent, finalContent, input.file_path); return { ok: true, content: `已编辑 ${filePath}:${edits.length} 条操作,实际替换 ${replacements} 次,${unchanged} 条无变化。已回读核验提交内容。\n${patch.unifiedDiff ?? '[diff 未统计:文件过大;不代表无变化]'}`, metadata: await fileActivityMetadata(filePath, 'modified', 'not_run', 'support', cwd, patch), }; } // ---------------- 5. 导出 ---------------- export const multiEditTool: Tool = { name: 'MultiEdit', description, inputSchema, parameters, isReadOnly: false, isConcurrencySafe: false, maxResultSizeChars: DEFAULT_MAX_RESULT_SIZE_CHARS, interruptBehavior: () => 'block', call: (input) => call(input), callWithContext: (input, _signal, context: ToolCallContext) => call(input, context.sessionState?.fileReads, context.cwd), };