Skip to content

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

context ls command prettification, store replacements for debugging if GOENV=development, convo summary fixes

Dane Schneidercaa4afab10e0
diff --git a/cli/cmd/ls.go b/cli/cmd/ls.go
index 52ce69cf..fa29087f 100644
--- a/cli/cmd/ls.go
+++ b/cli/cmd/ls.go
@@ -4,7 +4,10 @@ import (
"fmt"
"os"
"plandex/lib"
+ "strconv"
+ "github.com/fatih/color"
+ "github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
@@ -19,33 +22,53 @@ func context(cmd *cobra.Command, args []string) {
context, err := lib.GetAllContext(true)
if err != nil {
- fmt.Fprintln(os.Stderr, "Error listing context:", err)
+ color.New(color.FgRed).Fprintln(os.Stderr, "Error listing context:", err)
os.Exit(1)
}
totalTokens := 0
+ table := tablewriter.NewWriter(os.Stdout)
+ table.SetHeader([]string{"Index", "Type", "ID", "Tokens", "Updated"})
+ table.SetAutoWrapText(false)
for i, part := range context {
totalTokens += part.NumTokens
- if i != 0 {
- fmt.Print("\n")
- }
-
- fmt.Println("Index:", i)
+ var contextType string
+ var id string
if part.FilePath != "" {
- fmt.Printf("File: %s\n", part.FilePath)
+ contextType = "file"
+ id = part.FilePath
+ } else if part.Url != "" {
+ contextType = "url"
+ id = part.Url
+ } else {
+ contextType = "text"
+ id = part.Name
}
- if part.Url != "" {
- fmt.Printf("Url: %s\n", part.Url)
+ row := []string{
+ strconv.Itoa(i),
+ contextType,
+ id,
+ strconv.Itoa(part.NumTokens) + " 🪙",
+ part.UpdatedAt,
+ }
+ if i%2 == 0 {
+ table.Rich(row, []tablewriter.Colors{
+ {tablewriter.FgHiCyanColor, tablewriter.Bold},
+ {tablewriter.FgHiWhiteColor},
+ {tablewriter.FgHiGreenColor, tablewriter.Bold},
+ {tablewriter.FgHiWhiteColor},
+ {tablewriter.FgHiWhiteColor},
+ })
+ } else {
+ table.Append(row)
}
-
- fmt.Printf("Tokens: %d\n", part.NumTokens)
- fmt.Printf("Updated: %s\n", part.UpdatedAt)
}
- fmt.Printf("\nTotal tokens: %d\n", totalTokens)
+ table.Render()
+ fmt.Print(color.New(color.FgHiCyan, color.Bold).Sprintf("\nTotal →") + color.New(color.FgHiWhite, color.Bold).Sprintf(" %d 🪙\n", totalTokens))
}
diff --git a/cli/lib/stream.go b/cli/lib/stream.go
index a5aa63b6..64f3fd91 100644
--- a/cli/lib/stream.go
+++ b/cli/lib/stream.go
@@ -99,10 +99,11 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
content = string(bytes)
}
- // Uncomment below for debugging updates
- // updatesPath := filepath.Join(CurrentPlanRootDir, "updates", chunk.Path)
- // os.MkdirAll(filepath.Dir(updatesPath), os.ModePerm)
- // os.WriteFile(updatesPath+"-"+"replacements"+"-"+StringTs(), []byte(buffer), 0644)
+ if os.Getenv("GOENV") == "development" {
+ updatesPath := filepath.Join(CurrentPlanRootDir, "updates", chunk.Path)
+ os.MkdirAll(filepath.Dir(updatesPath), os.ModePerm)
+ os.WriteFile(updatesPath+"-"+"replacements"+"-"+shared.StringTs(), []byte(buffer), 0644)
+ }
// log.Println("Content before replacements: " + content)
@@ -141,10 +142,11 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
} else if streamedType == "file" {
content = streamed.Content
- // Uncomment below for debugging updates
- // updatesPath := filepath.Join(CurrentPlanRootDir, "updates", chunk.Path)
- // os.MkdirAll(filepath.Dir(updatesPath), os.ModePerm)
- // os.WriteFile(updatesPath+"-"+"file"+"-"+StringTs(), []byte(buffer), 0644)
+ if os.Getenv("GOENV") == "development" {
+ updatesPath := filepath.Join(CurrentPlanRootDir, "updates", chunk.Path)
+ os.MkdirAll(filepath.Dir(updatesPath), os.ModePerm)
+ os.WriteFile(updatesPath+"-"+"file"+"-"+shared.StringTs(), []byte(buffer), 0644)
+ }
}
diff --git a/server/model/proposal/create.go b/server/model/proposal/create.go
index d5511b3e..9f269e48 100644
--- a/server/model/proposal/create.go
+++ b/server/model/proposal/create.go
@@ -3,6 +3,7 @@ package proposal
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"os"
"strings"
@@ -132,8 +133,12 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
tokensUpToTimestamp[convoMessage.Timestamp] = conversationTokens
}
+ fmt.Printf("Conversation tokens: %d\n", conversationTokens)
+
var summary *shared.ConversationSummary
if (totalTokens + conversationTokens) > shared.MaxTokens {
+ fmt.Println("Token limit exceeded. Attempting to reduce via conversation summary.")
+
// token limit exceeded after adding conversation
// get summary for as much as the conversation as necessary to stay under the token limit
for _, s := range req.ConversationSummaries {
@@ -144,21 +149,26 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
return err
}
updatedConversationTokens := (conversationTokens - tokens) + s.Tokens
+ savedTokens := (totalTokens + conversationTokens) - updatedConversationTokens
if updatedConversationTokens <= shared.MaxTokens {
+ fmt.Printf("Summarizing up to %s | saving %d tokens\n", s.LastMessageTimestamp, savedTokens)
summary = &s
break
}
}
+
+ if summary == nil {
+ err := errors.New("couldn't get under token limit with conversation summary")
+ fmt.Printf("Error: %v\n", err)
+ return err
+ }
}
if summary == nil {
- if len(req.Conversation) > 0 {
- for _, convoMessage := range req.Conversation {
- messages = append(messages, convoMessage.Message)
- }
+ for _, convoMessage := range req.Conversation {
+ messages = append(messages, convoMessage.Message)
}
} else {
-
if (totalTokens + summary.Tokens) > shared.MaxTokens {
err := fmt.Errorf("token limit still exceeded after summarizing conversation")
return err
@@ -171,6 +181,13 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
%s`, summary.Summary),
})
+
+ // add messages after the last message in the summary
+ for _, convoMessage := range req.Conversation {
+ if convoMessage.Timestamp > summary.LastMessageTimestamp {
+ messages = append(messages, convoMessage.Message)
+ }
+ }
}
messages = append(messages, openai.ChatCompletionMessage{