diff --git a/.envrc b/.envrc index 3550a30..3082345 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,5 @@ +#!/etc/profiles/per-user/jmug/bin/zsh + use flake + +export ANTHROPIC_API_KEY=$(cat /home/jmug/.keys/anthropic) diff --git a/fizzbuzz.sh b/fizzbuzz.sh new file mode 100755 index 0000000..4487895 --- /dev/null +++ b/fizzbuzz.sh @@ -0,0 +1,19 @@ +#!/etc/profiles/per-user/jmug/bin/zsh + +# FizzBuzz implementation +# Prints numbers from 1 to 15 +# For multiples of 3, print "Fizz" instead of the number +# For multiples of 5, print "Buzz" instead of the number +# For multiples of both 3 and 5, print "FizzBuzz" + +for i in {1..15}; do + if (( i % 3 == 0 && i % 5 == 0 )); then + echo "FizzBuzz" + elif (( i % 3 == 0 )); then + echo "Fizz" + elif (( i % 5 == 0 )); then + echo "Buzz" + else + echo $i + fi +done \ No newline at end of file diff --git a/go.mod b/go.mod index 85eae4e..2cd3f4f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,20 @@ module code.jmug.me/jmug/amp-blog-agent go 1.24.5 + +require ( + github.com/anthropics/anthropic-sdk-go v1.6.2 + 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 new file mode 100644 index 0000000..e9c518d --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +github.com/anthropics/anthropic-sdk-go v1.6.2 h1:oORA212y0/zAxe7OPvdgIbflnn/x5PGk5uwjF60GqXM= +github.com/anthropics/anthropic-sdk-go v1.6.2/go.mod h1:3qSNQ5NrAmjC8A2ykuruSQttfqfdEYNZY5o8c0XSHB8= +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.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..e5a7743 --- /dev/null +++ b/main.go @@ -0,0 +1,324 @@ +package main + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "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) 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) +} + +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) +} + +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 { + // TODO: A bit weird to panic here instead of return the error. + 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 + } + + 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 "", errors.New("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 "", errors.New("old_str not found in the current 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 +} diff --git a/secret-file.txt b/secret-file.txt new file mode 100644 index 0000000..9769e05 --- /dev/null +++ b/secret-file.txt @@ -0,0 +1 @@ +what animal is the most disagreeable because it always says neigh?