Files
shakespeare/src/lib/tools/TextEditorStrReplaceTool.ts
T
2025-07-25 03:01:14 -05:00

204 lines
6.7 KiB
TypeScript

import { join } from "@std/path";
import { z } from "zod";
import type { Tool, CallToolResult } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime";
interface TextEditorStrReplaceParams {
path: string;
old_str: string;
new_str: string;
normalize_whitespace?: boolean;
}
export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams> {
private fs: JSRuntimeFS;
private cwd: string;
readonly description = "Replace a string in a file with a new string";
readonly parameters = z.object({
path: z.string().describe('Path to the file, eg "src/index.ts"'),
old_str: z.string().describe("The string to replace"),
new_str: z.string().describe("The new string to insert"),
normalize_whitespace: z.boolean().optional().describe(
"If true, normalize whitespace and line endings for more flexible matching (default: true)"
),
});
constructor(fs: JSRuntimeFS, cwd: string) {
this.fs = fs;
this.cwd = cwd;
}
async execute(args: TextEditorStrReplaceParams): Promise<CallToolResult> {
const { path, old_str, new_str, normalize_whitespace = true } = args;
// Check for absolute paths and provide helpful error
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
return {
content: [{
type: "text",
text: `❌ Absolute paths are not supported.\n\nThe path "${path}" appears to be an absolute path.\n\n💡 Please use a relative path instead. Examples:\n- "src/index.ts" (relative to current directory)\n- "./src/index.ts" (explicit relative path)\n- "../other-project/src/index.ts" (relative to parent directory)\n\nCurrent working directory: ${this.cwd}`,
}],
isError: true,
};
}
// Ban editing dependencies in package.json
if (
path === "package.json" || path.endsWith("/package.json") ||
path.endsWith("\\package.json")
) {
const depKeys = [
'"dependencies"',
'"devDependencies"',
'"peerDependencies"',
'"optionalDependencies"',
];
if (
depKeys.some((key) =>
old_str.includes(key) || new_str.includes(key)
)
) {
return {
content: [{
type: "text",
text:
`🔒 Direct edits to dependencies in package.json are disallowed.\n👉 Please use npm_add_package or npm_remove_package tools for these changes.`,
}],
isError: true,
};
}
}
try {
const absolutePath = join(this.cwd, path);
const content = await this.fs.readFile(absolutePath, "utf8");
// Helper function to normalize whitespace
const normalizeWhitespace = (str: string): string => {
return str
.replace(/\r\n/g, '\n') // Convert CRLF to LF
.replace(/\r/g, '\n') // Convert CR to LF
.split('\n')
.map(line => line.trimEnd()) // Remove trailing whitespace from each line
.join('\n');
};
let searchContent = content;
let searchOldStr = old_str;
let searchNewStr = new_str;
if (normalize_whitespace) {
searchContent = normalizeWhitespace(content);
searchOldStr = normalizeWhitespace(old_str);
searchNewStr = normalizeWhitespace(new_str);
}
if (!searchContent.includes(searchOldStr)) {
// Try to provide helpful debugging information
const lines = searchContent.split('\n');
const oldLines = searchOldStr.split('\n');
// Find potential matches by looking for the first line of old_str
const firstLine = oldLines[0];
const matchingLineNumbers: number[] = [];
if (firstLine) {
lines.forEach((line, index) => {
if (line.includes(firstLine)) {
matchingLineNumbers.push(index + 1);
}
});
}
let errorMessage = `Error: The string to replace was not found in ${path}`;
if (matchingLineNumbers.length > 0) {
errorMessage += `\n\nPossible matches found at lines: ${matchingLineNumbers.join(', ')}`;
// Show context around the first potential match
const firstMatch = matchingLineNumbers[0]! - 1;
const contextStart = Math.max(0, firstMatch - 2);
const contextEnd = Math.min(lines.length, firstMatch + oldLines.length + 2);
const contextLines = lines.slice(contextStart, contextEnd);
errorMessage += `\n\nContext around line ${matchingLineNumbers[0]}:`;
contextLines.forEach((line, index) => {
const lineNum = contextStart + index + 1;
const marker = lineNum === matchingLineNumbers[0] ? '→ ' : ' ';
errorMessage += `\n${marker}${lineNum}: ${line}`;
});
}
if (normalize_whitespace) {
errorMessage += `\n\nNote: Whitespace normalization was applied. Try setting normalize_whitespace to false if the replacement still fails.`;
}
return {
content: [{
type: "text",
text: errorMessage,
}],
isError: true,
};
}
// Count occurrences (simple string counting)
let occurrences = 0;
let pos = 0;
while ((pos = searchContent.indexOf(searchOldStr, pos)) !== -1) {
occurrences++;
pos += searchOldStr.length;
}
// Perform the replacement
let newContent: string;
if (normalize_whitespace) {
// Apply the replacement to the normalized content, then restore original line endings
const normalizedResult = searchContent.replace(searchOldStr, searchNewStr);
// Try to preserve the original line ending style
const hasCarriageReturn = content.includes('\r');
if (hasCarriageReturn) {
if (content.includes('\r\n')) {
// Original had CRLF
newContent = normalizedResult.replace(/\n/g, '\r\n');
} else {
// Original had CR only
newContent = normalizedResult.replace(/\n/g, '\r');
}
} else {
// Original had LF only
newContent = normalizedResult;
}
} else {
newContent = content.replace(old_str, new_str);
}
await this.fs.writeFile(absolutePath, newContent, "utf8");
let successMessage = `String successfully replaced in ${path}`;
if (occurrences > 1) {
successMessage += ` (${occurrences} occurrences found, only first occurrence replaced)`;
}
return {
content: [{
type: "text",
text: successMessage,
}],
isError: false,
};
} catch (error) {
return {
content: [{
type: "text",
text: String(error),
}],
isError: true,
};
}
}
}