feat: Add ReplaceFileContent function with line count verification #79
+262
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds two new AI tool functions for improved file editing workflows:
Also includes a bug fix for string array deserialization in FunctionFactory.
Motivation
ReplaceFileContent
The current workflow for replacing file contents requires:
This is tedious when you know you want to replace the entire file.
ReplaceMultipleInFile
When making several related changes to a file, calling ReplaceOneInFile multiple times is inefficient and error-prone. This function validates all patterns first, then applies all changes atomically.
Solution
ReplaceFileContent
New function with AI-CAPTCHA verification:
oldContentLineCountparameterReplaceMultipleInFile
Atomic multi-pattern replacement:
oldStringsandnewStringsBug Fix
Fixed string array deserialization in FunctionFactory where JSON arrays were incorrectly parsed using
GetRawText()instead ofGetString(), causing quoted strings to include the quotes.Example Usage
ReplaceFileContent
ReplaceMultipleInFile
Testing
? Comprehensive test suite added in
tests/cycod-yaml/file-replacement-functions.yaml:Note: All tests are marked as
optional: needsAIand require proper AI API configuration (Copilot, OpenAI, Azure, or Anthropic). They can be run manually with:The functions themselves have been manually tested and verified to work correctly.
Changes
src/cycod/FunctionCallingTools/StrReplaceEditorHelperFunctions.cs: Added ReplaceFileContent and ReplaceMultipleInFile functionssrc/cycod/FunctionCalling/FunctionFactory.cs: Fixed string array deserialization bugtests/cycod-yaml/file-replacement-functions.yaml: Comprehensive test coverage for new functions