MCPcopy Create free account
hub / github.com/codehamr/codehamr / EditFileSchema

Function EditFileSchema

internal/tools/edit.go:76–102  ·  view source on GitHub ↗

EditFileSchema is the OpenAI tool definition for edit_file. The description steers the model toward edit_file over write_file for small changes so it stops rewriting whole documents to fix a typo.

()

Source from the content-addressed store, hash-verified

74 return fmt.Sprintf("edited %s: -%d +%d bytes", path, len(oldString), len(newString))
75}
76
77// differsOnlyInWhitespace reports whether oldString matches content at exactly
78// one spot once every whitespace run is collapsed: i.e. the sole mismatch is
79// indentation/tabs/newlines. Bounded with spaces so a match can't straddle a
80// token boundary and mislabel an unrelated near-miss.
81func differsOnlyInWhitespace(content, oldString string) bool {
82 norm := func(s string) string { return " " + strings.Join(strings.Fields(s), " ") + " " }
83 return strings.Count(norm(content), norm(oldString)) == 1
84}
85
86// fuzzyWhitespaceEdit applies old_string when its whitespace-collapsed token
87// sequence matches a run of WHOLE content lines at exactly one place: the
88// retyped-indentation failure (Aider measured flexible application cutting
89// edit errors ~9x; pi and opencode ship the same fallback). Whole lines only,
90// so the splice boundary is a line boundary and every surrounding byte is
91// preserved exactly; a mid-line fragment or a 0/2+ match returns ok=false and
92// leaves the instructive failure to the caller. new_string goes in as given -
93// those are the bytes the model meant to write, same as an exact match.
94func fuzzyWhitespaceEdit(path, content, oldString, newString string) (string, bool) {
95 want := strings.Fields(oldString)
96 if len(want) == 0 {
97 return "", false
98 }
99 lines := strings.Split(content, "\n")
100 matches, matchStart, matchEnd := 0, -1, -1
101 for i := range lines {
102 if len(strings.Fields(lines[i])) == 0 {
103 continue // a match starts on a non-blank line, keeping the region tight
104 }
105 if end := fuzzyMatchAt(lines, i, want); end >= 0 {

Callers 2

buildToolsMethod · 0.92
TestEditFileSchemaShapeFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestEditFileSchemaShapeFunction · 0.68