schema def issues

This commit is contained in:
Marc Goodner 2024-12-04 20:45:43 -08:00
commit b04c9334bc

View file

@ -114,13 +114,23 @@ const EditOperation = z.object({
newText: z.string().describe('Text to replace the found text with'), newText: z.string().describe('Text to replace the found text with'),
}); });
const EditOptions = z.object({
preserveIndentation: z.boolean().default(true).describe('Preserve existing indentation patterns in the file'),
normalizeWhitespace: z.boolean().default(true).describe('Normalize whitespace while preserving structure'),
partialMatch: z.boolean().default(true).describe('Enable fuzzy matching with confidence scoring')
});
const EditFileArgsSchema = z.object({ const EditFileArgsSchema = z.object({
path: z.string(), path: z.string(),
edits: z.array(EditOperation), edits: z.array(EditOperation),
// Optional: preview changes without applying them // Optional: preview changes without applying them
dryRun: z.boolean().default(false).describe('Preview changes using git-style diff format') dryRun: z.boolean().default(false).describe('Preview changes using git-style diff format'),
// Optional: configure matching and formatting behavior
options: EditOptions.default({})
}); });
const CreateDirectoryArgsSchema = z.object({ const CreateDirectoryArgsSchema = z.object({
path: z.string(), path: z.string(),
}); });
@ -250,19 +260,13 @@ function normalizeWhitespace(text: string, preserveIndentation: boolean = true):
}).join('\n'); }).join('\n');
} }
interface EditOptions {
preserveIndentation?: boolean;
normalizeWhitespace?: boolean;
partialMatch?: boolean;
}
interface EditMatch { interface EditMatch {
start: number; start: number;
end: number; end: number;
confidence: number; confidence: number;
} }
function findBestMatch(content: string, searchText: string, options: EditOptions): EditMatch | null { function findBestMatch(content: string, searchText: string, options: z.infer<typeof EditOptions>): EditMatch | null {
const normalizedContent = normalizeLineEndings(content); const normalizedContent = normalizeLineEndings(content);
const normalizedSearch = normalizeLineEndings(searchText); const normalizedSearch = normalizeLineEndings(searchText);
@ -379,11 +383,7 @@ async function applyFileEdits(
filePath: string, filePath: string,
edits: Array<{oldText: string, newText: string}>, edits: Array<{oldText: string, newText: string}>,
dryRun = false, dryRun = false,
options: EditOptions = { options: z.infer<typeof EditOptions> = EditOptions.parse({})
preserveIndentation: true,
normalizeWhitespace: true,
partialMatch: true
}
): Promise<string> { ): Promise<string> {
const content = await fs.readFile(filePath, 'utf-8'); const content = await fs.readFile(filePath, 'utf-8');
let modifiedContent = content; let modifiedContent = content;
@ -485,12 +485,17 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
{ {
name: "edit_file", name: "edit_file",
description: description:
"Make selective edits to a text file using line-based pattern matching and replacement. " + "Make selective edits to a text file using advanced pattern matching and smart formatting preservation. Features include:\n" +
"Handles both single-line and multi-line edits, with smart positioning to handle multiple edits simultaneously. " + "- Line-based and multi-line content matching\n" +
"Shows changes in standard unified diff format with context lines, similar to git diff. " + "- Whitespace normalization with indentation preservation\n" +
"Provides detailed diff output for failed matches to aid debugging. " + "- Fuzzy matching with confidence scoring\n" +
"Use dry run mode to preview changes in patch format before applying them. " + "- Multiple simultaneous edits with correct positioning\n" +
"Only works within allowed directories.", "- Indentation style detection and preservation\n" +
"- Detailed diff output with context in git format\n" +
"- Dry run mode for previewing changes\n" +
"- Failed match debugging with match confidence scores\n\n" +
"Configure behavior with options.preserveIndentation, options.normalizeWhitespace, and options.partialMatch. " +
"See schema for detailed option descriptions. Only works within allowed directories.",
inputSchema: zodToJsonSchema(EditFileArgsSchema) as ToolInput, inputSchema: zodToJsonSchema(EditFileArgsSchema) as ToolInput,
}, },
{ {
@ -612,7 +617,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
throw new Error(`Invalid arguments for edit_file: ${parsed.error}`); throw new Error(`Invalid arguments for edit_file: ${parsed.error}`);
} }
const validPath = await validatePath(parsed.data.path); const validPath = await validatePath(parsed.data.path);
const result = await applyFileEdits(validPath, parsed.data.edits, parsed.data.dryRun); const result = await applyFileEdits(validPath, parsed.data.edits, parsed.data.dryRun, parsed.data.options);
// If it's a dry run, show the unified diff // If it's a dry run, show the unified diff
if (parsed.data.dryRun) { if (parsed.data.dryRun) {