Compare commits
5 commits
Author | SHA1 | Date | |
---|---|---|---|
|
2e897a8cd1 | ||
|
721ed01d01 | ||
|
b6d1729a79 | ||
|
eb882731e3 | ||
|
c347b598cb |
20
go.mod
Normal file
20
go.mod
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
module agent
|
||||||
|
|
||||||
|
go 1.24.2
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
33
go.sum
Normal file
33
go.sum
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
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=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
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=
|
319
main.go
Normal file
319
main.go
Normal file
|
@ -0,0 +1,319 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/anthropics/anthropic-sdk-go"
|
||||||
|
"github.com/invopop/jsonschema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
client := anthropic.NewClient()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
getUserMessage := func() (string, bool) {
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return scanner.Text(), true
|
||||||
|
}
|
||||||
|
tools := []ToolDefinition{ReadFileDefinition, ListFilesDefinition, EditFileDefinition}
|
||||||
|
|
||||||
|
agent := NewAgent(&client, getUserMessage, tools)
|
||||||
|
err := agent.Run(context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAgent(client *anthropic.Client, getUserMessage func() (string, bool), tools []ToolDefinition) *Agent {
|
||||||
|
return &Agent{
|
||||||
|
client: client,
|
||||||
|
getUserMessage: getUserMessage,
|
||||||
|
tools: tools,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Agent struct {
|
||||||
|
client *anthropic.Client
|
||||||
|
getUserMessage func() (string, bool)
|
||||||
|
tools []ToolDefinition
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) Run(ctx context.Context) error {
|
||||||
|
conversation := []anthropic.MessageParam{}
|
||||||
|
|
||||||
|
fmt.Println("Chat with Claude (use 'ctrl-c' to quit)")
|
||||||
|
|
||||||
|
readUserInput := true
|
||||||
|
for {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
message, err := a.runInference(ctx, conversation)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
anthropicTools = append(anthropicTools, anthropic.ToolUnionParam{
|
||||||
|
OfTool: &anthropic.ToolParam{
|
||||||
|
Name: tool.Name,
|
||||||
|
Description: anthropic.String(tool.Description),
|
||||||
|
InputSchema: tool.InputSchema,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
message, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{
|
||||||
|
Model: anthropic.ModelClaude3_7SonnetLatest,
|
||||||
|
MaxTokens: int64(1024),
|
||||||
|
Messages: conversation,
|
||||||
|
Tools: anthropicTools,
|
||||||
|
})
|
||||||
|
return message, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type ToolDefinition struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ListFilesDefinition = ToolDefinition{
|
||||||
|
Name: "list_files",
|
||||||
|
Description: "List files and directories at a given path. If no path is provided, lists files in the current directory.",
|
||||||
|
InputSchema: ListFilesInputSchema,
|
||||||
|
Function: ListFiles,
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListFilesInput struct {
|
||||||
|
Path string `json:"path,omitempty" jsonschema_description:"Optional relative path to list files from. Defaults to current directory if not provided."`
|
||||||
|
}
|
||||||
|
|
||||||
|
var ListFilesInputSchema = GenerateSchema[ListFilesInput]()
|
||||||
|
|
||||||
|
func ListFiles(input json.RawMessage) (string, error) {
|
||||||
|
listFilesInput := ListFilesInput{}
|
||||||
|
err := json.Unmarshal(input, &listFilesInput)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := "."
|
||||||
|
if listFilesInput.Path != "" {
|
||||||
|
dir = listFilesInput.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
var files []string
|
||||||
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore devenv folder because it blows up the list of files (too many
|
||||||
|
// tokens)
|
||||||
|
if strings.Contains(path, ".devenv") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, err := filepath.Rel(dir, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if relPath != "." {
|
||||||
|
if info.IsDir() {
|
||||||
|
files = append(files, relPath+"/")
|
||||||
|
} else {
|
||||||
|
files = append(files, relPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := json.Marshal(files)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(result), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var EditFileDefinition = ToolDefinition{
|
||||||
|
Name: "edit_file",
|
||||||
|
Description: `Make edits to a text file.
|
||||||
|
|
||||||
|
Replaces 'old_str' with 'new_str' in the given file. 'old_str' and 'new_str' MUST be different from each other.
|
||||||
|
|
||||||
|
If the file specified with path doesn't exist, it will be created.
|
||||||
|
`,
|
||||||
|
InputSchema: EditFileInputSchema,
|
||||||
|
Function: EditFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
type EditFileInput struct {
|
||||||
|
Path string `json:"path" jsonschema_description:"The path to the file"`
|
||||||
|
OldStr string `json:"old_str" jsonschema_description:"Text to search for - must match exactly and must only have one match exactly"`
|
||||||
|
NewStr string `json:"new_str" jsonschema_description:"Text to replace old_str with"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var EditFileInputSchema = GenerateSchema[EditFileInput]()
|
||||||
|
|
||||||
|
func EditFile(input json.RawMessage) (string, error) {
|
||||||
|
editFileInput := EditFileInput{}
|
||||||
|
err := json.Unmarshal(input, &editFileInput)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if editFileInput.Path == "" || editFileInput.OldStr == editFileInput.NewStr {
|
||||||
|
return "", fmt.Errorf("invalid input parameters")
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(editFileInput.Path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) && editFileInput.OldStr == "" {
|
||||||
|
return createNewFile(editFileInput.Path, editFileInput.NewStr)
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldContent := string(content)
|
||||||
|
newContent := strings.Replace(oldContent, editFileInput.OldStr, editFileInput.NewStr, -1)
|
||||||
|
|
||||||
|
if oldContent == newContent && editFileInput.OldStr != "" {
|
||||||
|
return "", fmt.Errorf("old_str not found in file")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(editFileInput.Path, []byte(newContent), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return "OK", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNewFile(filePath, content string) (string, error) {
|
||||||
|
dir := path.Dir(filePath)
|
||||||
|
if dir != "." {
|
||||||
|
err := os.MkdirAll(dir, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create directory: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := os.WriteFile(filePath, []byte(content), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("Successfully created file %s", filePath), nil
|
||||||
|
}
|
Loading…
Reference in a new issue