-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.go
More file actions
183 lines (167 loc) · 4.83 KB
/
Copy pathtools.go
File metadata and controls
183 lines (167 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
type ToolResult struct {
Success bool
Output string
}
var allowedCommands = []string{"go", "ls", "cat", "pwd", "echo", "grep"}
// Commands that will never be allowed, even if the base executable is in the allowed list.
var blockedCommands = []string{"go run", "./server", "go run ."}
func isAllowed(cmd string) bool {
cmd = strings.TrimSpace(cmd)
// Check blocked patterns first
for _, blocked := range blockedCommands {
if strings.HasPrefix(cmd, blocked) || strings.Contains(cmd, blocked) {
return false
}
}
parts := strings.Fields(cmd)
if len(parts) == 0 {
return false
}
base := parts[0]
for _, allowed := range allowedCommands {
if base == allowed {
return true
}
}
return false
}
func ExecuteToolFromCall(tc ToolCall) ToolResult {
switch tc.Name {
case "write_file":
path := tc.Args["path"]
content := tc.Args["content"]
err := os.WriteFile(path, []byte(content), 0644)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
return ToolResult{Success: true, Output: fmt.Sprintf("wrote %d bytes to %s", len(content), path)}
case "read_file":
path := tc.Args["path"]
data, err := os.ReadFile(path)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
return ToolResult{Success: true, Output: string(data)}
case "list_dir":
dir := tc.Args["dir"]
if dir == "" {
dir = "."
}
entries, err := os.ReadDir(dir)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
var names []string
for _, e := range entries {
if e.IsDir() {
names = append(names, e.Name()+"/")
} else {
names = append(names, e.Name())
}
}
return ToolResult{Success: true, Output: strings.Join(names, "\n")}
case "search_content":
pattern := tc.Args["pattern"]
searchPath := tc.Args["path"]
if searchPath == "" {
searchPath = "."
}
matches, err := grep(searchPath, pattern)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
if len(matches) == 0 {
return ToolResult{Success: true, Output: "no matches found"}
}
return ToolResult{Success: true, Output: strings.Join(matches, "\n")}
case "edit_file":
path := tc.Args["path"]
old := tc.Args["old"]
new := tc.Args["new"]
data, err := os.ReadFile(path)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
content := string(data)
if !strings.Contains(content, old) {
return ToolResult{Success: false, Output: fmt.Sprintf("old string not found in %s", path)}
}
updated := strings.Replace(content, old, new, 1) // replace first occurrence
err = os.WriteFile(path, []byte(updated), 0644)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
return ToolResult{Success: true, Output: fmt.Sprintf("edited %s: replaced 1 occurrence", path)}
case "replace_all":
path := tc.Args["path"]
old := tc.Args["old"]
new := tc.Args["new"]
data, err := os.ReadFile(path)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
content := string(data)
count := strings.Count(content, old)
if count == 0 {
return ToolResult{Success: false, Output: "old string not found"}
}
updated := strings.ReplaceAll(content, old, new)
err = os.WriteFile(path, []byte(updated), 0644)
if err != nil {
return ToolResult{Success: false, Output: err.Error()}
}
return ToolResult{Success: true, Output: fmt.Sprintf("replaced %d occurrences in %s", count, path)}
case "run_command":
cmdStr := tc.Args["command"]
if !isAllowed(cmdStr) {
return ToolResult{Success: false, Output: fmt.Sprintf("command not allowed: %s", cmdStr)}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "sh", "-c", cmdStr)
out, err := cmd.CombinedOutput()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return ToolResult{Success: false, Output: "command timed out after 5s"}
}
return ToolResult{Success: false, Output: fmt.Sprintf("command failed: %s\n%s", err, string(out))}
}
return ToolResult{Success: true, Output: string(out)}
case "finish":
return ToolResult{Success: true, Output: "task completed"}
default:
return ToolResult{Success: false, Output: fmt.Sprintf("unknown tool: %s", tc.Name)}
}
}
func grep(root, pattern string) ([]string, error) {
var matches []string
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return nil
}
if d.IsDir() {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
for i, line := range strings.Split(string(data), "\n") {
if strings.Contains(line, pattern) {
matches = append(matches, fmt.Sprintf("%s:%d: %s", path, i+1, line))
}
}
return nil
})
return matches, err
}