edit

package
v1.2.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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"
)
View Source
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.

View Source
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.

View Source
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

func CharDiff(a, b string) string

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

func FindActualString(fileContent string, searchString string) string

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

func LevenshteinSimilarity(a, b string) float64

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

type FuzzyMatch struct {
	Text       string
	Similarity float64
}

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 GitDiff

type GitDiff = shared.GitDiff

GitDiff is the shared git diff summary, re-aliased here for backward compat.

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

func NewEditTool(workingDir string) *Tool

NewEditTool creates a new edit file tool

func (*Tool) BackfillInput

func (e *Tool) BackfillInput(ctx context.Context, input map[string]any) map[string]any

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

func (e *Tool) Description(ctx context.Context) (string, error)

Description returns the tool description

func (*Tool) FormatResult

func (e *Tool) FormatResult(data any) string

FormatResult serialises the tool output into the tool_result content string.

func (*Tool) GetWorkingDir

func (e *Tool) GetWorkingDir() string

GetWorkingDir returns the current working directory

func (*Tool) IsConcurrencySafe

func (e *Tool) IsConcurrencySafe(input map[string]any) bool

IsConcurrencySafe reports that edits are not safe to run concurrently.

func (*Tool) IsEnabled

func (e *Tool) IsEnabled() bool

IsEnabled returns whether this tool is currently active.

func (*Tool) IsReadOnly

func (e *Tool) IsReadOnly(input map[string]any) bool

IsReadOnly reports that edits modify state.

func (*Tool) SetWorkingDir

func (e *Tool) SetWorkingDir(dir string)

SetWorkingDir sets the working directory for the tool

func (*Tool) ValidateInput

func (e *Tool) ValidateInput(ctx context.Context, input map[string]any) (map[string]any, error)

ValidateInput validates and normalizes edit input.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL