返回 DeepSeek-Reasonix
write_declare.go
根目录 / internal / tool / builtin / write_declare.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "path/filepath"
8 "strings"
9
10 "reasonix/internal/sandbox"
11 "reasonix/internal/tool"
12 )
13
14 func effectiveWriteRoots(ctx context.Context, set *sandbox.WritableRootSet, fallback []string) []string {
15 if set != nil {
16 return set.Effective(ctx)
17 }
18 if extra := sandbox.PerCallWriteRoots(ctx); len(extra) > 0 {
19 return sandbox.CollapseWriteRoots(append(append([]string{}, fallback...), extra...))
20 }
21 return fallback
22 }
23
24 func declareParentWriteDirs(workDir string, paths ...string) (tool.WriteAccessDeclaration, error) {
25 var dirs []string
26 for _, p := range paths {
27 p = strings.TrimSpace(p)
28 if p == "" {
29 return tool.WriteAccessDeclaration{}, fmt.Errorf("path is required")
30 }
31 resolved := resolveIn(workDir, p)
32 dir := filepath.Dir(resolved)
33 if dir == "" || dir == "." {
34 continue
35 }
36 dirs = append(dirs, dir)
37 }
38 return tool.WriteAccessDeclaration{Directories: dirs}, nil
39 }
40
41 func declareFilePathWriteAccess(workDir string, args json.RawMessage) (tool.WriteAccessDeclaration, error) {
42 var p struct {
43 Path string `json:"path"`
44 }
45 if err := json.Unmarshal(args, &p); err != nil {
46 return tool.WriteAccessDeclaration{}, fmt.Errorf("invalid args: %w", err)
47 }
48 return declareParentWriteDirs(workDir, p.Path)
49 }
50
51 // BindWriteRootSet attaches a live writable-root manager to a built-in writer
52 // or bash tool so later session grants are visible without replacing the registry.
53 func BindWriteRootSet(tl tool.Tool, set *sandbox.WritableRootSet) tool.Tool {
54 if set == nil {
55 return tl
56 }
57 switch t := tl.(type) {
58 case writeFile:
59 t.rootSet = set
60 return t
61 case editFile:
62 t.rootSet = set
63 return t
64 case multiEdit:
65 t.rootSet = set
66 return t
67 case moveFile:
68 t.rootSet = set
69 return t
70 case notebookEdit:
71 t.rootSet = set
72 return t
73 case deleteRange:
74 t.rootSet = set
75 return t
76 case deleteSymbol:
77 t.rootSet = set
78 return t
79 case bash:
80 t.rootSet = set
81 return t
82 default:
83 return tl
84 }
85 }
86
86 lines GO