Documentation
¶
Index ¶
- Constants
- func CharDiff(a, b string) string
- func FindActualString(fileContent string, searchString string) string
- func LevenshteinSimilarity(a, b string) float64
- type FuzzyMatch
- type GitDiff
- type StructuredPatchHunk
- type Tool
- func (e *Tool) BackfillInput(ctx context.Context, input map[string]any) map[string]any
- func (e *Tool) Call(ctx context.Context, input tool.CallInput, permissionCheck types.CanUseToolFn) (tool.CallResult, error)
- func (e *Tool) CheckPermissions(ctx context.Context, input map[string]any, toolCtx tool.ToolUseContext) types.PermissionResult
- func (e *Tool) Definition() tool.Definition
- func (e *Tool) Description(ctx context.Context) (string, error)
- func (e *Tool) FormatResult(data any) string
- func (e *Tool) GetWorkingDir() string
- func (e *Tool) IsConcurrencySafe(input map[string]any) bool
- func (e *Tool) IsEnabled() bool
- func (e *Tool) IsReadOnly(input map[string]any) bool
- func (e *Tool) SetWorkingDir(dir string)
- func (e *Tool) ValidateInput(ctx context.Context, input map[string]any) (map[string]any, error)
Constants ¶
const ( // ToolName is the name of the edit tool ToolName = "edit_file" // ToolDescription is the description of the edit tool ToolDescription = "Edit an existing file by replacing an exact string with a new one. The canonical tool for all partial modifications to existing files.\n\n" + "## When to use\n\n" + "- Modifying any part of an existing file: function bodies, config values, imports, constants\n" + "- Renaming a symbol across a file (use replace_all)\n" + "- Any edit where you are changing less than the whole file\n\n" + "## When NOT to use\n\n" + "- Complete file rewrites — use FileWrite instead\n" + "- Creating a new file — use FileWrite instead\n\n" + "## Rules\n\n" + "- You must read the file with FileRead before editing. This tool errors if you have not read the file first.\n" + "- Match indentation exactly as it appears in the file (tabs vs. spaces). Never include line-number prefixes from Read output in old_string or new_string.\n" + "- old_string must be unique in the file. If it appears more than once, provide more surrounding lines for context, or use replace_all.\n" + "- Use replace_all to rename a variable or string across the entire file.\n" + "- Use the smallest old_string that is clearly unique — 2–4 adjacent lines is usually enough.\n" )
const ( FuzzyMatchMaxSearchLines = 20000 FuzzyMatchMaxSearchStringLen = 4000 )
FuzzyMatchMaxSearchLines / FuzzyMatchMaxSearchStringLen bound the cost of fuzzy search: it's O(totalLines * len(searchString)^2) in the worst case (a Levenshtein DP table per candidate window), which is fine for a normal edit_file call on a normal file but not something to run unbounded on a huge file or a huge old_string.
const FuzzyMatchMinSimilarity = 0.7
FuzzyMatchMinSimilarity is the similarity floor below which a fuzzy match isn't worth suggesting - a low-similarity "closest" match is more likely to mislead than help.
const SearchHint = "edit text in a file by finding and replacing strings"
SearchHint is a hint for tool search functionality.
Variables ¶
This section is empty.
Functions ¶
func CharDiff ¶ added in v1.1.0
CharDiff renders a's and b's difference as DesktopCommanderMCP-style inline markup: "common{-removed-}{+added+}common" - built by backtracking through the same Levenshtein DP table used for the similarity score, so the diff shown is guaranteed to be a minimal edit sequence, not a heuristic approximation.
func FindActualString ¶ added in v1.1.0
FindActualString finds searchString in fileContent, tolerating curly vs. straight quote differences (a model very commonly "corrects" one style to the other when recalling text). Returns the exact substring as it appears in fileContent (which may differ from searchString only in quote style), or "" if no match - including a quote-normalized one - exists.
func LevenshteinSimilarity ¶ added in v1.1.0
LevenshteinSimilarity returns a 0..1 similarity score (1 = identical) derived from Levenshtein edit distance, normalized by the longer string's length so unequal-length comparisons stay meaningful.
Types ¶
type FuzzyMatch ¶ added in v1.1.0
FuzzyMatch is the best candidate found for a failed exact/quote-normalized match, with the info needed to build an actionable error message.
func FindFuzzyMatch ¶ added in v1.1.0
func FindFuzzyMatch(fileContent, searchString string) (FuzzyMatch, bool)
FindFuzzyMatch searches fileContent for the substring most similar to searchString, using a sliding window sized to searchString's line count (edit blocks are almost always a handful of contiguous lines, so this is both cheap and matches how the tool is actually used - unlike a full rune-by-rune slide, which would be far more expensive for no benefit here). Returns ok=false when no candidate clears FuzzyMatchMinSimilarity, or when the search space exceeds the bounds above (silently skipped, not an error - the caller just won't get a suggestion).
type StructuredPatchHunk ¶
type StructuredPatchHunk struct {
OldStart int `json:"oldStart"`
OldLines int `json:"oldLines"`
NewStart int `json:"newStart"`
NewLines int `json:"newLines"`
Lines []string `json:"lines"`
}
StructuredPatchHunk represents a single structured diff hunk.
type Tool ¶
type Tool struct {
// contains filtered or unexported fields
}
Tool implements the edit file tool
func NewEditTool ¶
NewEditTool creates a new edit file tool
func (*Tool) BackfillInput ¶
BackfillInput enriches a shallow clone of the parsed input with derived fields.
func (*Tool) Call ¶
func (e *Tool) Call( ctx context.Context, input tool.CallInput, permissionCheck types.CanUseToolFn, ) (tool.CallResult, error)
Call executes the tool
func (*Tool) CheckPermissions ¶
func (e *Tool) CheckPermissions(ctx context.Context, input map[string]any, toolCtx tool.ToolUseContext) types.PermissionResult
CheckPermissions performs edit-specific permission checks before the global pipeline.
func (*Tool) Definition ¶
func (e *Tool) Definition() tool.Definition
Definition returns the tool definition
func (*Tool) Description ¶
Description returns the tool description
func (*Tool) FormatResult ¶
FormatResult serialises the tool output into the tool_result content string.
func (*Tool) GetWorkingDir ¶
GetWorkingDir returns the current working directory
func (*Tool) IsConcurrencySafe ¶
IsConcurrencySafe reports that edits are not safe to run concurrently.
func (*Tool) IsReadOnly ¶
IsReadOnly reports that edits modify state.
func (*Tool) SetWorkingDir ¶
SetWorkingDir sets the working directory for the tool