Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

reply counter improvement

Dane Schneiderf67a201372be
diff --git a/cli/lib/conversation.go b/cli/lib/conversation.go
index 0c68a1ec..9b2b09ba 100644
--- a/cli/lib/conversation.go
+++ b/cli/lib/conversation.go
@@ -176,9 +176,11 @@ func (r *replyTokenCounter) AddChunk(chunk string) {
if r.currentFilePath == "" {
var gotPath string
- if (strings.HasPrefix(prevFullLine, "-") && strings.HasSuffix(prevFullLine, ":")) || strings.HasPrefix(prevFullLine, "-file:") || strings.HasPrefix(prevFullLine, "- file:") {
- p := strings.TrimPrefix(prevFullLine, "-")
+ if (strings.HasPrefix(prevFullLine, "-") && strings.HasSuffix(prevFullLine, ":")) || strings.HasPrefix(prevFullLine, "-file:") || strings.HasPrefix(prevFullLine, "- file:") || (strings.HasPrefix(prevFullLine, "**") && strings.HasSuffix(prevFullLine, "**")) {
+ p := strings.TrimPrefix(prevFullLine, "**")
+ p = strings.TrimPrefix(p, "-")
p = strings.TrimSpace(p)
+ p = strings.TrimSuffix(p, "**")
p = strings.TrimPrefix(p, "file:")
p = strings.TrimSuffix(p, ":")
p = strings.TrimSpace(p)
diff --git a/cli/lib/conversation_test.go b/cli/lib/conversation_test.go
index 721a9764..dab9e18b 100644
--- a/cli/lib/conversation_test.go
+++ b/cli/lib/conversation_test.go
@@ -1,38 +1,57 @@
package lib
import (
+ "fmt"
"os"
"testing"
)
+type TestExample struct {
+ FilePath string
+ NumPaths int
+}
+
+var examples = []TestExample{
+ {
+ FilePath: "conversation_test_examples/1.md",
+ NumPaths: 2,
+ },
+ {
+ FilePath: "conversation_test_examples/2.md",
+ NumPaths: 2,
+ },
+}
+
func TestReplyTokenCounter(t *testing.T) {
- bytes, err := os.ReadFile("conversation_test.md")
- if err != nil {
- t.Error(err)
- }
+ for _, example := range examples {
+ bytes, err := os.ReadFile(example.FilePath)
+ if err != nil {
+ t.Error(err)
+ }
- content := string(bytes)
+ content := string(bytes)
- chunkSize := 10
+ chunkSize := 10
- counter := NewReplyTokenCounter()
+ counter := NewReplyTokenCounter()
- for i := 0; i < len(content); {
- end := i + chunkSize
- if end > len(content) {
- end = len(content)
+ for i := 0; i < len(content); {
+ end := i + chunkSize
+ if end > len(content) {
+ end = len(content)
+ }
+ chunk := content[i:end]
+ counter.AddChunk(chunk)
+ i = end
}
- chunk := content[i:end]
- counter.AddChunk(chunk)
- i = end
- }
- tokensByFilePath := counter.FinishAndRead()
+ tokensByFilePath := counter.FinishAndRead()
- if len(tokensByFilePath) != 2 {
- t.Error("Expected 2 file paths")
- }
+ if len(tokensByFilePath) != example.NumPaths {
+ t.Error(fmt.Sprintf("Expected %d file paths", example.NumPaths))
+ }
+ }
}
// func TestGetTokensPerFilePath(t *testing.T) {
diff --git a/cli/lib/conversation_test.md b/cli/lib/conversation_test_examples/1.md
similarity index 100%
rename from cli/lib/conversation_test.md
rename to cli/lib/conversation_test_examples/1.md
diff --git a/cli/lib/conversation_test_examples/2.md b/cli/lib/conversation_test_examples/2.md
new file mode 100644
index 00000000..c2bf218d
--- /dev/null
+++ b/cli/lib/conversation_test_examples/2.md
@@ -0,0 +1,105 @@
+Sure, here's how you could structure your 'context rm' command and 'context update' command using placeholders for the 'lib' functions that will handle the actual removal and updating of contexts:
+
+**cmd/context_rm.go**
+
+```go
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "fmt"
+ "log"
+
+ "github.com/spf13/cobra"
+)
+
+// contextRmCmd represents the delete command
+var contextRmCmd = &cobra.Command{
+ Use: "rm [context_name]",
+ Aliases: []string{"delete"},
+ Short: "",
+ Long: ``,
+ Run: contextRm,
+}
+
+func contextRm(cmd *cobra.Command, args []string) {
+ all, err := cmd.Flags().GetBool("all")
+
+ if err != nil {
+ log.Fatal("Error parsing all flag: ", err)
+ }
+
+ if all {
+ // Placeholder for lib function
+ err = lib.RemoveAllContexts()
+ if err != nil {
+ log.Fatal("Error removing all contexts: ", err)
+ }
+ } else if len(args) > 0 {
+ for _, contextName := range args {
+ // Placeholder for lib function
+ err = lib.RemoveContext(contextName)
+ if err != nil {
+ fmt.Printf("Error removing context '%s': %v\n", contextName, err)
+ }
+ }
+ } else {
+ fmt.Println("Please provide a context name or use the --all flag to remove all contexts.")
+ }
+}
+
+func init() {
+ contextCmd.AddCommand(contextRmCmd)
+ contextRmCmd.Flags().BoolP("all", "a", false, "remove all contexts")
+}
+```
+
+**cmd/context_update.go**
+
+```go
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "fmt"
+ "log"
+
+ "github.com/spf13/cobra"
+)
+
+// contextUpdateCmd represents the context command
+var contextUpdateCmd = &cobra.Command{
+ Use: "update [file-or-urls...]",
+ Short: "A brief description of your command",
+ Long: `A longer description that spans multiple lines and likely contains examples
+and usage of using your command. For example:
+
+Cobra is a CLI library for Go that empowers applications.
+This application is a tool to generate the needed files
+to quickly create a Cobra application.`,
+ Run: func(cmd *cobra.Command, args []string) {
+ if len(args) == 0 {
+ fmt.Println("Please provide one or more files or URLs to update.")
+ return
+ }
+
+ for _, arg := range args {
+ // Placeholder for lib function
+ err := lib.UpdateContext(arg)
+ if err != nil {
+ fmt.Printf("Error updating context for '%s': %v\n", arg, err)
+ }
+ }
+ },
+}
+
+func init() {
+ contextCmd.AddCommand(contextUpdateCmd)
+}
+```
+
+In your next iteration, you could implement the functions in 'lib' package such as `RemoveAllContexts`, `RemoveContext`, and `UpdateContext`.
diff --git a/cli/lib/proposal.go b/cli/lib/proposal.go
index 0852bed0..9667fb0b 100644
--- a/cli/lib/proposal.go
+++ b/cli/lib/proposal.go
@@ -159,7 +159,7 @@ func Propose(prompt string) error {
}
if desc.MadePlan && (len(desc.Files) > 0 || desc.HasExec) {
- fmt.Println("Writing plan files:")
+ fmt.Println("Writing plan draft:")
for _, filePath := range desc.Files {
fmt.Printf("- %s\n", filePath)
}
@@ -210,7 +210,7 @@ func Propose(prompt string) error {
total := contextTotal + added
fmtArgs = append(fmtArgs, total, contextTotal, added)
- } else {
+ } else if added > 0 {
fmtStr += " / %d estimated"
fmtArgs = append(fmtArgs, added)
}