-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogramming_tool_write_file.go
82 lines (70 loc) · 1.88 KB
/
programming_tool_write_file.go
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
package tools
import (
"fmt"
"os"
"path/filepath"
)
type WriteFileTool UserFunction
var WriteFile = WriteFileTool{
Name: "write_file",
Description: "Write content to a file. Creates the file if it doesn't exist, or overwrites it if it does.",
Inputs: &InputSchema{
Type: "object",
Properties: map[string]ParameterObject{
"file_path": {
Type: "string",
Description: "The path to the file to write to.",
},
"content": {
Type: "string",
Description: "The content to write to the file.",
},
"append": {
Type: "boolean",
Description: "If true, append to the file instead of overwriting it.",
},
},
Required: []string{"file_path", "content"},
},
}
func (w WriteFileTool) Call(input Input) (string, error) {
filePath, ok := input["file_path"].(string)
if !ok {
return "", fmt.Errorf("file_path must be a string")
}
content, ok := input["content"].(string)
if !ok {
return "", fmt.Errorf("content must be a string")
}
append := false
if input["append"] != nil {
append, ok = input["append"].(bool)
if !ok {
return "", fmt.Errorf("append must be a boolean")
}
}
// Ensure the directory exists
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, 0o755); err != nil {
return "", fmt.Errorf("failed to create directory: %w", err)
}
var flag int
if append {
flag = os.O_APPEND | os.O_CREATE | os.O_WRONLY
} else {
flag = os.O_TRUNC | os.O_CREATE | os.O_WRONLY
}
file, err := os.OpenFile(filePath, flag, 0o644)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
return "", fmt.Errorf("failed to write to file: %w", err)
}
return fmt.Sprintf("Successfully wrote %d bytes to %s", len(content), filePath), nil
}
func (w WriteFileTool) UserFunction() UserFunction {
return UserFunction(WriteFile)
}