From f777535abf39ca075d6dad591cafd66d7db7e6af Mon Sep 17 00:00:00 2001 From: alejandro-angulo Date: Sat, 7 Jun 2025 15:08:46 -0700 Subject: [PATCH] Added base64 encode tool --- main.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 881bac7..c0c1425 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "context" + "encoding/base64" "encoding/json" "fmt" "os" @@ -24,7 +25,12 @@ func main() { } return scanner.Text(), true } - tools := []ToolDefinition{ReadFileDefinition, ListFilesDefinition, EditFileDefinition} + tools := []ToolDefinition{ + ReadFileDefinition, + ListFilesDefinition, + EditFileDefinition, + Base64EncodeFileDefinition, + } agent := NewAgent(&client, getUserMessage, tools) err := agent.Run(context.TODO()) @@ -336,3 +342,36 @@ func createNewFile(filePath, content string) (string, error) { return fmt.Sprintf("Successfully created file %s", filePath), nil } + +var Base64EncodeFileDefinition = ToolDefinition{ + Name: "base64_encode", + Description: `Generates a base64 encoding of a file. + + This is especially useful when asked to describe an image file (you can use + this get a base64 encoded representation of the image file). +`, + InputSchema: Base64EncodeFileInputSchema, + Function: Base64EncodeFile, +} + +type Base64EncodeFileInput struct { + Path string `json:"path" jsonschema_description:"The path to the image"` +} + +var Base64EncodeFileInputSchema = GenerateSchema[EditFileInput]() + +func Base64EncodeFile(input json.RawMessage) (string, error) { + analyzeImageInput := Base64EncodeFileInput{} + err := json.Unmarshal(input, &analyzeImageInput) + if err != nil { + panic(err) + } + + content, err := os.ReadFile(analyzeImageInput.Path) + if err != nil { + return "", err + } + encoded := base64.StdEncoding.EncodeToString([]byte(content)) + + return encoded, nil +}