From 74d93b275885ee45f3cc1d4bd795c6ca0ec391dc Mon Sep 17 00:00:00 2001 From: alejandro-angulo Date: Tue, 3 Jun 2025 22:19:07 -0700 Subject: [PATCH] Add read file tool --- go.mod | 10 ++++++- go.sum | 21 +++++++++++++ main.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 115 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index b51242c..6d7a035 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,19 @@ module agent go 1.24.2 -require github.com/anthropics/anthropic-sdk-go v1.3.0 +require ( + github.com/anthropics/anthropic-sdk-go v1.3.0 + github.com/invopop/jsonschema v0.13.0 +) require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect + github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 3a7142b..6de236b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,20 @@ github.com/anthropics/anthropic-sdk-go v1.3.0 h1:KroW4oDT3KzFT71d3bnu4DxLFAEPvY+d1c6z2CrOz/s= github.com/anthropics/anthropic-sdk-go v1.3.0/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E= +github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -10,3 +25,9 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 0fc7f59..f01db02 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os" "github.com/anthropics/anthropic-sdk-go" + "github.com/invopop/jsonschema" ) func main() { @@ -20,7 +21,7 @@ func main() { } return scanner.Text(), true } - tools := []ToolDefinition{} + tools := []ToolDefinition{ReadFileDefinition} agent := NewAgent(&client, getUserMessage, tools) err := agent.Run(context.TODO()) @@ -48,15 +49,18 @@ func (a *Agent) Run(ctx context.Context) error { fmt.Println("Chat with Claude (use 'ctrl-c' to quit)") + readUserInput := true for { - fmt.Print("\u001b[94mYou\u001b[0m: ") - userInput, ok := a.getUserMessage() - if !ok { - break - } + if readUserInput { + fmt.Print("\u001b[94mYou\u001b[0m: ") + userInput, ok := a.getUserMessage() + if !ok { + break + } - userMessage := anthropic.NewUserMessage(anthropic.NewTextBlock(userInput)) - conversation = append(conversation, userMessage) + userMessage := anthropic.NewUserMessage(anthropic.NewTextBlock(userInput)) + conversation = append(conversation, userMessage) + } message, err := a.runInference(ctx, conversation) if err != nil { @@ -64,17 +68,49 @@ func (a *Agent) Run(ctx context.Context) error { } conversation = append(conversation, message.ToParam()) + toolResults := []anthropic.ContentBlockParamUnion{} for _, content := range message.Content { switch content.Type { case "text": fmt.Printf("\u001b[93mClaude\u001b[0m: %s\n", content.Text) + case "tool_use": + result := a.executeTool(content.ID, content.Name, content.Input) + toolResults = append(toolResults, result) } } + if len(toolResults) == 0 { + readUserInput = true + continue + } + readUserInput = false + conversation = append(conversation, anthropic.NewUserMessage(toolResults...)) } return nil } +func (a *Agent) executeTool(id, name string, input json.RawMessage) anthropic.ContentBlockParamUnion { + var toolDef ToolDefinition + var found bool + for _, tool := range a.tools { + if tool.Name == name { + toolDef = tool + found = true + break + } + } + if !found { + return anthropic.NewToolResultBlock(id, "tool not found", true) + } + + fmt.Printf("\u001b[92mtool\u001b[0m: %s(%s)\n", name, input) + response, err := toolDef.Function(input) + if err != nil { + return anthropic.NewToolResultBlock(id, err.Error(), true) + } + return anthropic.NewToolResultBlock(id, response, false) +} + func (a *Agent) runInference(ctx context.Context, conversation []anthropic.MessageParam) (*anthropic.Message, error) { anthropicTools := []anthropic.ToolUnionParam{} for _, tool := range a.tools { @@ -102,3 +138,44 @@ type ToolDefinition struct { InputSchema anthropic.ToolInputSchemaParam `json:"input_schema"` Function func(input json.RawMessage) (string, error) } + +var ReadFileDefinition = ToolDefinition{ + Name: "read_file", + Description: "Read the contents of a given relative file path. Use this when you want to see what's inside a file. Do not use this with directory names.", + InputSchema: ReadFileInputSchema, + Function: ReadFile, +} + +type ReadFileInput struct { + Path string `json:"path" jsonschema_description:"The relative path of a file in the working directory."` +} + +var ReadFileInputSchema = GenerateSchema[ReadFileInput]() + +func ReadFile(input json.RawMessage) (string, error) { + readFileInput := ReadFileInput{} + err := json.Unmarshal(input, &readFileInput) + if err != nil { + panic(err) + } + + content, err := os.ReadFile(readFileInput.Path) + if err != nil { + return "", err + } + return string(content), nil +} + +func GenerateSchema[T any]() anthropic.ToolInputSchemaParam { + reflector := jsonschema.Reflector{ + AllowAdditionalProperties: false, + DoNotReference: true, + } + var v T + + schema := reflector.Reflect(v) + + return anthropic.ToolInputSchemaParam{ + Properties: schema.Properties, + } +}