Files
DeepSeek-Reasonix/internal/tool/builtin/editfile.go
T
yhhandClaude Opus 4.8 7de6a2474f feat: import Go implementation as v2 kernel (renamed duo → reasonix)
Bring in the finished Go rewrite (developed under the placeholder name
"duo") as the v2 codebase. Code-only import — no upstream git history.

Config- and plugin-driven coding agent: registry core, OpenAI-compatible
providers, builtin tools, MCP (stdio + HTTP) plugins, permission + sandbox,
custom slash commands, @-references, two-model collaboration.

Renamed throughout: duo→reasonix, Duo→Reasonix, DUO→REASONIX — module path,
imports, CLI name, cmd/ dirs, reasonix.toml, .reasonix/commands, and
$REASONIX_* env prefixes. Kept the v2 MIT LICENSE (the Go project had none).

Verified: go build ./... · go vet ./... · go test ./... all green; gofmt clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-05-29 18:13:20 -07:00

71 lines
2.1 KiB
Go

package builtin
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"reasonix/internal/tool"
)
func init() { tool.RegisterBuiltin(editFile{}) }
// editFile replaces an exact string in a file. roots confines the target to the
// workspace when non-empty (see writeFile).
type editFile struct{ roots []string }
func (editFile) Name() string { return "edit_file" }
func (editFile) Description() string {
return "Replace an exact string in a file with another. old_string must occur exactly once; add surrounding context to disambiguate. Use for targeted edits instead of rewriting the whole file."
}
func (editFile) Schema() json.RawMessage {
return json.RawMessage(`{"type":"object","properties":{"path":{"type":"string","description":"File path"},"old_string":{"type":"string","description":"Exact text to replace (must be unique in the file)"},"new_string":{"type":"string","description":"Replacement text (may be empty to delete)"}},"required":["path","old_string","new_string"]}`)
}
func (editFile) ReadOnly() bool { return false }
func (e editFile) Execute(ctx context.Context, args json.RawMessage) (string, error) {
var p struct {
Path string `json:"path"`
OldString string `json:"old_string"`
NewString string `json:"new_string"`
}
if err := json.Unmarshal(args, &p); err != nil {
return "", fmt.Errorf("invalid args: %w", err)
}
if p.Path == "" {
return "", fmt.Errorf("path is required")
}
if p.OldString == "" {
return "", fmt.Errorf("old_string is required")
}
if err := confine(e.roots, p.Path); err != nil {
return "", err
}
b, err := os.ReadFile(p.Path)
if err != nil {
return "", fmt.Errorf("read %s: %w", p.Path, err)
}
content := string(b)
switch strings.Count(content, p.OldString) {
case 0:
return "", fmt.Errorf("old_string not found in %s", p.Path)
case 1:
// ok
default:
return "", fmt.Errorf("old_string is not unique in %s; add more surrounding context", p.Path)
}
updated := strings.Replace(content, p.OldString, p.NewString, 1)
if err := os.WriteFile(p.Path, []byte(updated), 0o644); err != nil {
return "", fmt.Errorf("write %s: %w", p.Path, err)
}
return fmt.Sprintf("edited %s", p.Path), nil
}