-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbash_tool_file.go
53 lines (48 loc) · 1.23 KB
/
bash_tool_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
package tools
import (
"fmt"
"os/exec"
)
type FileTypeTool UserFunction
var FileType = FileTypeTool{
Name: "file_type",
Description: "Determine the file type of a given file. Uses the linux command 'file'.",
Inputs: &InputSchema{
Type: "object",
Properties: map[string]ParameterObject{
"file_path": {
Type: "string",
Description: "The path to the file to analyze.",
},
"mime_type": {
Type: "boolean",
Description: "Whether to display the MIME type of the file.",
},
},
Required: []string{"file_path"},
},
}
func (f FileTypeTool) Call(input Input) (string, error) {
filePath, ok := input["file_path"].(string)
if !ok {
return "", fmt.Errorf("file_path must be a string")
}
cmd := exec.Command("file", filePath)
if input["mime_type"] != nil {
mimeType, ok := input["mime_type"].(bool)
if !ok {
return "", fmt.Errorf("mime_type must be a boolean")
}
if mimeType {
cmd.Args = append(cmd.Args, "--mime-type")
}
}
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run file command: %w, output: %v", err, string(output))
}
return string(output), nil
}
func (f FileTypeTool) UserFunction() UserFunction {
return UserFunction(FileType)
}