返回 DeepSeek-Reasonix
editfile.go
根目录 / internal / tool / builtin / editfile.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "reasonix/internal/sandbox"
9 "reasonix/internal/tool"
10 )
11
12 func init() { tool.RegisterBuiltin(editFile{}) }
13
14 // editFile replaces an exact string in a file. roots confines the target to the
15 // workspace when non-empty (see writeFile); guard rejects Reasonix session-data
16 // targets (see SessionDataGuard); workDir, when non-empty, is the directory a
17 // relative path resolves against (see resolveIn).
18 type editFile struct {
19 roots []string
20 rootSet *sandbox.WritableRootSet
21 guard SessionDataGuard
22 managed ManagedConfigPaths
23 workDir string
24 overlay FileOverlay
25 }
26
27 func (editFile) Name() string { return "edit_file" }
28
29 func (editFile) Description() string {
30 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."
31 }
32
33 func (editFile) Schema() json.RawMessage {
34 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"]}`)
35 }
36
37 func (editFile) ReadOnly() bool { return false }
38
39 func (e editFile) DeclareWriteAccess(args json.RawMessage) (tool.WriteAccessDeclaration, error) {
40 return declareFilePathWriteAccess(e.workDir, args)
41 }
42
43 func (e editFile) Execute(ctx context.Context, args json.RawMessage) (string, error) {
44 var p struct {
45 Path string `json:"path"`
46 OldString string `json:"old_string"`
47 NewString string `json:"new_string"`
48 }
49 if err := json.Unmarshal(args, &p); err != nil {
50 return "", fmt.Errorf("invalid args: %w", err)
51 }
52 if p.Path == "" {
53 return "", fmt.Errorf("path is required")
54 }
55 if p.OldString == "" {
56 return "", fmt.Errorf("old_string is required")
57 }
58 p.Path = resolveIn(e.workDir, p.Path)
59 if err := confineWrite(ctx, effectiveWriteRoots(ctx, e.rootSet, e.roots), e.guard, e.managed, p.Path); err != nil {
60 return "", err
61 }
62 unlock := lockMutationPath(p.Path)
63 defer unlock()
64
65 src, err := readEditSource(ctx, e.overlay, p.Path)
66 if err != nil {
67 return "", fmt.Errorf("read %s: %w", p.Path, err)
68 }
69 if err := src.requireObserved(ctx, e.overlay, p.Path); err != nil {
70 return "", err
71 }
72
73 applied := applyOldStringEdit(src.content, p.OldString, p.NewString, false)
74 switch {
75 case applied.applied == 1:
76 // ok
77 case applied.matches == 0:
78 return "", oldStringNotFoundError(p.Path, p.OldString, src.content)
79 default:
80 return "", oldStringNotUniqueError(p.Path, p.OldString, src.content, applied.matches, false)
81 }
82
83 if err := src.write(ctx, e.overlay, p.Path, applied.updated); err != nil {
84 return "", fmt.Errorf("write %s: %w", p.Path, err)
85 }
86 summary := fmt.Sprintf("edited %s", p.Path)
87 if applied.fuzzy {
88 summary += " (fuzzy match)"
89 }
90 return withActualPostWriteReceipts(summary, []editReplacementReceipt{applied.receipt}), nil
91 }
92
92 lines GO