Skip to content

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

context update command and prompt for updates when outdated

Dane Schneider55086b0285eb
diff --git a/cli/cmd/clear.go b/cli/cmd/clear.go
index ed9d9450..5733d3b2 100644
--- a/cli/cmd/clear.go
+++ b/cli/cmd/clear.go
@@ -31,7 +31,7 @@ func clearAllContext(cmd *cobra.Command, args []string) {
toRemovePaths = append(toRemovePaths, path)
}
- err = lib.ContextRm(toRemovePaths)
+ err = lib.ContextRemoveFiles(toRemovePaths)
if err != nil {
fmt.Fprintln(os.Stderr, "Error removing context:", err)
return
@@ -44,6 +44,7 @@ func clearAllContext(cmd *cobra.Command, args []string) {
return
}
planState.ContextTokens = 0
+ planState.ContextUpdatableTokens = 0
err = lib.SetPlanState(planState, shared.StringTs())
diff --git a/cli/cmd/load.go b/cli/cmd/load.go
index c70b50f5..7383e627 100644
--- a/cli/cmd/load.go
+++ b/cli/cmd/load.go
@@ -10,11 +10,8 @@ import (
var (
recursive bool
- // maxDepth int
namesOnly bool
- // truncate bool
- // maxTokens int
- note string
+ note string
)
var contextLoadCmd = &cobra.Command{
@@ -29,23 +26,14 @@ func init() {
contextLoadCmd.Flags().StringVarP(&note, "note", "n", "", "Add a note to the context")
contextLoadCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "Search directories recursively")
contextLoadCmd.Flags().BoolVar(&namesOnly, "tree", false, "Load directory tree with file names only")
- // contextLoadCmd.Flags().IntVarP(&maxDepth, "depth", "d", -1, "Maximum depth for recursive directory search (-1 means no limit)")
- // contextLoadCmd.Flags().BoolVar(&truncate, "truncate", false, "Truncate contents if tokens exceed maximum")
- // contextLoadCmd.Flags().IntVar(&maxTokens, "max", -1, "Maximum limit for number of tokens")
-
- // can be called via plandex load or plandex context load
RootCmd.AddCommand(contextLoadCmd)
- contextCmd.AddCommand(contextLoadCmd)
}
func contextLoad(cmd *cobra.Command, args []string) {
- lib.LoadContextOrDie(args, &types.LoadContextParams{
- Note: note,
- // MaxTokens: maxTokens,
+ lib.MustLoadContext(args, &types.LoadContextParams{
+ Note: note,
Recursive: recursive,
- // MaxDepth: maxDepth,
NamesOnly: namesOnly,
- // Truncate: truncate,
})
fmt.Println()
diff --git a/cli/cmd/ls.go b/cli/cmd/ls.go
index 52cce62f..a321c562 100644
--- a/cli/cmd/ls.go
+++ b/cli/cmd/ls.go
@@ -44,7 +44,6 @@ func context(cmd *cobra.Command, args []string) {
for i, part := range context {
totalTokens += part.NumTokens
- // var contextType string
var icon string
var t string
id := part.Name
diff --git a/cli/cmd/rm.go b/cli/cmd/rm.go
index 467979ce..c5eee19e 100644
--- a/cli/cmd/rm.go
+++ b/cli/cmd/rm.go
@@ -53,7 +53,7 @@ func contextRm(cmd *cobra.Command, args []string) {
}
}
- err = lib.ContextRm(toRemovePaths)
+ err = lib.ContextRemoveFiles(toRemovePaths)
if err != nil {
fmt.Fprintln(os.Stderr, "Error removing context:", err)
return
@@ -68,11 +68,17 @@ func contextRm(cmd *cobra.Command, args []string) {
removedTokens := 0
totalTokens := planState.ContextTokens
+ totalUpdatableTokens := planState.ContextUpdatableTokens
for _, part := range toRemoveParts {
removedTokens += part.NumTokens
totalTokens -= part.NumTokens
+
+ if part.Type == shared.ContextFileType || part.Type == shared.ContextDirectoryTreeType || part.Type == shared.ContextURLType {
+ totalUpdatableTokens -= part.NumTokens
+ }
}
planState.ContextTokens = totalTokens
+ planState.ContextUpdatableTokens = totalUpdatableTokens
err = lib.SetPlanState(planState, shared.StringTs())
diff --git a/cli/cmd/update.go b/cli/cmd/update.go
index 39cf3fae..dfdf30f4 100644
--- a/cli/cmd/update.go
+++ b/cli/cmd/update.go
@@ -1,10 +1,11 @@
package cmd
import (
+ "plandex/lib"
+
"github.com/spf13/cobra"
)
-// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update ",
Aliases: []string{"u"},
@@ -14,12 +15,12 @@ var updateCmd = &cobra.Command{
}
func init() {
- // Add update command
RootCmd.AddCommand(updateCmd)
}
func update(cmd *cobra.Command, args []string) {
- // Argument parsing and update logic will be implemented
-
+ updateRes := lib.MustUpdateContextWithOuput()
+ table := lib.TableForContextUpdateRes(updateRes)
+ table.Render()
}
diff --git a/cli/go.mod b/cli/go.mod
index ec7b10fe..0304447f 100644
--- a/cli/go.mod
+++ b/cli/go.mod
@@ -46,6 +46,7 @@ require (
github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000
github.com/sashabaranov/go-openai v1.15.1
github.com/spf13/pflag v1.0.5 // indirect
+ golang.org/x/text v0.14.0
)
replace github.com/plandex/plandex/shared => ../shared
diff --git a/cli/go.sum b/cli/go.sum
index 7e644e90..dd9cd4d5 100644
--- a/cli/go.sum
+++ b/cli/go.sum
@@ -941,6 +941,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
+golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
+golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
diff --git a/cli/lib/context_fs.go b/cli/lib/context_fs.go
index 6107e974..67c0df31 100644
--- a/cli/lib/context_fs.go
+++ b/cli/lib/context_fs.go
@@ -16,7 +16,7 @@ func CreateContextFileName(name, sha string) string {
return fmt.Sprintf("%s.%s", name, shaSubstring)
}
-func ContextRm(paths []string) error {
+func ContextRemoveFiles(paths []string) error {
// remove files
errCh := make(chan error, len(paths)*2)
for _, path := range paths {
diff --git a/cli/lib/context_load.go b/cli/lib/context_load.go
index 939d1e0b..f40d3001 100644
--- a/cli/lib/context_load.go
+++ b/cli/lib/context_load.go
@@ -19,7 +19,7 @@ import (
"github.com/plandex/plandex/shared"
)
-func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int, int) {
+func MustLoadContext(resources []string, params *types.LoadContextParams) (int, int) {
timeStart := time.Now()
s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)
@@ -27,11 +27,6 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
s.Start()
maxTokens := shared.MaxContextTokens
- // if params.MaxTokens == -1 {
- // maxTokens = shared.MaxTokens
- // } else {
- // maxTokens = min(params.MaxTokens, shared.MaxTokens)
- // }
planState, err := GetPlanState()
if err != nil {
@@ -42,6 +37,7 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
tokensAdded := 0
totalTokens := planState.ContextTokens
+ totalUpdatableTokens := planState.ContextUpdatableTokens
var totalTokensMutex sync.Mutex
var contextParts []*shared.ModelContextPart
@@ -66,27 +62,9 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
tokensAdded += numTokens
if totalTokens > maxTokens {
- // if params.Truncate {
- // s.Stop()
- // ClearCurrentLine()
- // fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating input text.\n", totalTokens, maxTokens)
- // s.Start()
- // numTokens = maxTokens - (totalTokens - numTokens)
-
- // // If the number of tokens is less than or equal to 0, then we can stop processing files
- // if numTokens <= 0 {
- // return
- // }
-
- // body = body[:numTokens]
- // totalTokens = maxTokens
-
- // } else {
s.Stop()
ClearCurrentLine()
log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- // }
-
}
}()
@@ -155,25 +133,9 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
totalTokens += numTokens
tokensAdded += numTokens
if totalTokens > maxTokens {
- // if params.Truncate {
- // s.Stop()
- // ClearCurrentLine()
- // fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating piped data.\n", totalTokens, maxTokens)
- // s.Start()
- // numTokens = maxTokens - (totalTokens - numTokens)
-
- // if numTokens <= 0 {
- // return
- // }
-
- // body = body[:numTokens]
- // totalTokens = maxTokens
-
- // } else {
s.Stop()
ClearCurrentLine()
log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- // }
}
}()
@@ -248,30 +210,12 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
func() {
defer totalTokensMutex.Unlock()
totalTokens += numTokens
+ totalUpdatableTokens += numTokens
tokensAdded += numTokens
if totalTokens > maxTokens {
-
- // if params.Truncate {
- // s.Stop()
- // ClearCurrentLine()
- // fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating directory tree.\n", totalTokens, maxTokens)
- // s.Start()
- // numTokens = maxTokens - (totalTokens - numTokens)
-
- // // If the number of tokens is less than or equal to 0, then we can stop processing files
- // if numTokens <= 0 {
- // return
- // }
-
- // body = body[:numTokens]
- // totalTokens = maxTokens
-
- // } else {
s.Stop()
ClearCurrentLine()
log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- // }
-
}
}()
@@ -303,7 +247,6 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
contextPartsMutex.Unlock()
}(inputFilePath)
-
}
} else {
@@ -337,29 +280,11 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
defer totalTokensMutex.Unlock()
totalTokens += numTokens
tokensAdded += numTokens
+ totalUpdatableTokens += numTokens
if totalTokens > maxTokens {
-
- // if params.Truncate {
- // s.Stop()
- // ClearCurrentLine()
- // fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating the file %s.\n", totalTokens, maxTokens, path)
- // numTokens = maxTokens - (totalTokens - numTokens)
- // s.Start()
-
- // // If the number of tokens is less than or equal to 0, then we can stop processing files
- // if numTokens <= 0 {
- // return
- // }
-
- // body = body[:numTokens]
- // totalTokens = maxTokens
-
- // } else {
s.Stop()
ClearCurrentLine()
log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- // }
-
}
}()
@@ -411,27 +336,11 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
defer totalTokensMutex.Unlock()
totalTokens += numTokens
tokensAdded += numTokens
+ totalUpdatableTokens += numTokens
if totalTokens > maxTokens {
- // if params.Truncate {
- // s.Stop()
- // ClearCurrentLine()
- // fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating content from URL %s.\n", totalTokens, maxTokens, url)
- // numTokens = maxTokens - (totalTokens - numTokens)
- // s.Start()
-
- // // If the number of tokens is less than or equal to 0, then we can stop processing content
- // if numTokens <= 0 {
- // return
- // }
-
- // body = body[:numTokens]
- // totalTokens = maxTokens
-
- // } else {
s.Stop()
ClearCurrentLine()
log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- // }
}
}()
@@ -471,6 +380,7 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
go func() {
planState.ContextTokens = totalTokens
+ planState.ContextUpdatableTokens = totalUpdatableTokens
errCh <- SetPlanState(planState, shared.StringTs())
}()
@@ -532,8 +442,6 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
log.Fatalf("Failed to get total tokens: %v", err)
}
- // msg += fmt.Sprintf("\n\nTotal tokens in context: %d\n", totalTokens)
-
err = GitCommitContextUpdate(msg)
if err != nil {
s.Stop()
@@ -542,7 +450,6 @@ func LoadContextOrDie(resources []string, params *types.LoadContextParams) (int,
}
elapsed := time.Since(timeStart)
-
if elapsed < 700*time.Millisecond {
time.Sleep(700*time.Millisecond - elapsed)
}
diff --git a/cli/lib/context_update.go b/cli/lib/context_update.go
index e1baa96b..5a73a144 100644
--- a/cli/lib/context_update.go
+++ b/cli/lib/context_update.go
@@ -4,41 +4,215 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
+ "math"
"os"
"plandex/types"
+ "strconv"
"strings"
"sync"
+ "time"
+ "github.com/briandowns/spinner"
+ "github.com/olekukonko/tablewriter"
"github.com/plandex/plandex/shared"
)
-func UpdateContext() ([]string, int, bool, error) {
- return checkOutdatedAndMaybeUpdateContext(true)
+type contextUpdate struct {
+ Path string
+ Part *shared.ModelContextPart
}
-func CheckOutdatedContext() ([]string, int, bool, error) {
- return checkOutdatedAndMaybeUpdateContext(false)
+type updateRes struct {
+ UpdatedParts []*shared.ModelContextPart
+ TokenDiffsByName map[string]int
+ TokensDiff int
+ TotalTokens int
+ MaxExceeded bool
+ NumFiles int
+ NumUrls int
+ NumTrees int
}
-type contextUpdate struct {
- Path string
- Part *shared.ModelContextPart
+func MustUpdateContextWithOuput() *updateRes {
+ timeStart := time.Now()
+
+ s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)
+ s.Prefix = "πŸ”„ Updating context... "
+ s.Start()
+
+ stopFn := func() {
+ elapsed := time.Since(timeStart)
+ if elapsed < 700*time.Millisecond {
+ time.Sleep(700*time.Millisecond - elapsed)
+ }
+ s.Stop()
+ ClearCurrentLine()
+ }
+
+ updateRes, err := UpdateContext()
+
+ if err != nil {
+ stopFn()
+ fmt.Fprintln(os.Stderr, "Error updating context:", err)
+ os.Exit(1)
+ }
+
+ maxExceeded := updateRes.MaxExceeded
+ updatedParts := updateRes.UpdatedParts
+ tokensDiff := updateRes.TokensDiff
+ totalTokens := updateRes.TotalTokens
+ numFiles := updateRes.NumFiles
+ numTrees := updateRes.NumTrees
+ numUrls := updateRes.NumUrls
+
+ if maxExceeded {
+ stopFn()
+ overage := totalTokens - shared.MaxContextTokens
+ fmt.Printf("🚨 Update would add %d πŸͺ™ and exceed token limit (%d) by %d πŸͺ™", tokensDiff, shared.MaxContextTokens, overage)
+ os.Exit(1)
+ }
+
+ if len(updatedParts) == 0 {
+ stopFn()
+ fmt.Println("βœ… Context is already up to date")
+ os.Exit(1)
+ }
+
+ msg := "Updated"
+
+ var toAdd []string
+ if numFiles > 0 {
+ postfix := "s"
+ if numFiles == 1 {
+ postfix = ""
+ }
+ toAdd = append(toAdd, fmt.Sprintf("%d file%s", numFiles, postfix))
+ }
+ if numTrees > 0 {
+ postfix := "s"
+ if numTrees == 1 {
+ postfix = ""
+ }
+ toAdd = append(toAdd, fmt.Sprintf("%d tree%s", numTrees, postfix))
+ }
+ if numUrls > 0 {
+ postfix := "s"
+ if numUrls == 1 {
+ postfix = ""
+ }
+ toAdd = append(toAdd, fmt.Sprintf("%d url%s", numUrls, postfix))
+ }
+
+ if len(toAdd) <= 2 {
+ msg += " " + strings.Join(toAdd, " and ")
+ } else {
+ for i, add := range toAdd {
+ if i == len(toAdd)-1 {
+ msg += ", and " + add
+ } else {
+ msg += ", " + add
+ }
+ }
+ }
+
+ action := "added"
+ if tokensDiff < 0 {
+ action = "removed"
+ }
+ absTokenDiff := int(math.Abs(float64(tokensDiff)))
+ msg += fmt.Sprintf(" | %s β†’ %d πŸͺ™ | total β†’ %d πŸͺ™", action, absTokenDiff, totalTokens)
+
+ err = GitCommitContextUpdate(msg)
+
+ stopFn()
+
+ if err != nil {
+ fmt.Println("Error committing context update:", err)
+ os.Exit(1)
+ }
+
+ fmt.Println("βœ… " + msg)
+
+ return updateRes
}
-func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, error) {
+func TableForContextUpdateRes(updateRes *updateRes) *tablewriter.Table {
+ updatedParts := updateRes.UpdatedParts
+ tokenDiffsByName := updateRes.TokenDiffsByName
+
+ table := tablewriter.NewWriter(os.Stdout)
+ table.SetHeader([]string{"Name", "Type", "πŸͺ™"})
+ table.SetAutoWrapText(false)
+
+ for _, part := range updatedParts {
+
+ var icon string
+ var t string
+ id := part.Name
+ switch part.Type {
+ case shared.ContextFileType:
+ icon = "πŸ“„"
+ t = "file"
+ id = part.FilePath
+ case shared.ContextURLType:
+ icon = "🌎"
+ t = "url"
+ case shared.ContextDirectoryTreeType:
+ icon = "πŸ—‚ "
+ t = "tree"
+ id = part.FilePath
+ }
+
+ diff := tokenDiffsByName[part.Name]
+
+ diffStr := "+" + strconv.Itoa(diff)
+ tableColor := []int{tablewriter.FgHiGreenColor, tablewriter.Bold}
+
+ if diff < 0 {
+ diffStr = strconv.Itoa(diff)
+ tableColor = []int{tablewriter.FgHiRedColor, tablewriter.Bold}
+ }
+
+ row := []string{
+ " " + icon + " " + id,
+ t,
+ diffStr,
+ }
+
+ table.Rich(row, []tablewriter.Colors{
+ {tablewriter.FgHiWhiteColor},
+ {tablewriter.FgHiWhiteColor},
+ tableColor,
+ })
+ }
+
+ return table
+}
+
+func UpdateContext() (*updateRes, error) {
+ return checkOutdatedAndMaybeUpdateContext(true)
+}
+
+func CheckOutdatedContext() (*updateRes, error) {
+ return checkOutdatedAndMaybeUpdateContext(false)
+}
+
+func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*updateRes, error) {
maxTokens := shared.MaxContextTokens
planState, err := GetPlanState()
if err != nil {
- return nil, 0, false, fmt.Errorf("error retrieving plan state: %w", err)
+ return nil, fmt.Errorf("error retrieving plan state: %w", err)
}
+ tokenDiffsByName := make(map[string]int)
tokensDiff := 0
totalTokens := planState.ContextTokens
+ totalUpdatableTokens := planState.ContextUpdatableTokens
var mu sync.Mutex
context, err := GetAllContext(true)
if err != nil {
- return nil, 0, false, fmt.Errorf("error retrieving context: %w", err)
+ return nil, fmt.Errorf("error retrieving context: %w", err)
}
ts := shared.StringTs()
@@ -72,6 +246,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, err
mu.Lock()
tokensDiff += numTokens - part.NumTokens
+ tokenDiffsByName[part.Name] = numTokens - part.NumTokens
mu.Unlock()
updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{
@@ -110,6 +285,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, err
numTokens := shared.GetNumTokens(body)
mu.Lock()
tokensDiff += numTokens - part.NumTokens
+ tokenDiffsByName[part.Name] = numTokens - part.NumTokens
mu.Unlock()
updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{
@@ -144,6 +320,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, err
numTokens := shared.GetNumTokens(body)
mu.Lock()
tokensDiff += numTokens - part.NumTokens
+ tokenDiffsByName[part.Name] = numTokens - part.NumTokens
mu.Unlock()
updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{
@@ -169,10 +346,10 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, err
updatedPaths := []string{}
updatedParts := []*shared.ModelContextPart{}
- for i := 0; i < len(context); i++ {
+ for range context {
select {
case err := <-errCh:
- return nil, 0, false, err
+ return nil, err
case update := <-updateCh:
if update != nil {
updatedPaths = append(updatedPaths, update.Path)
@@ -182,29 +359,61 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) ([]string, int, bool, err
}
totalTokens += tokensDiff
+ totalUpdatableTokens += tokensDiff
if doUpdate {
if totalTokens > maxTokens {
- return nil, 0, true, fmt.Errorf("context update would exceed the token limit of %d", maxTokens)
+ return &updateRes{
+ UpdatedParts: nil,
+ TokensDiff: tokensDiff,
+ TotalTokens: totalTokens,
+ MaxExceeded: true,
+ }, fmt.Errorf("context update would exceed the token limit of %d", maxTokens)
}
- err = ContextRm(updatedPaths)
+ err = ContextRemoveFiles(updatedPaths)
if err != nil {
- return nil, 0, false, fmt.Errorf("error removing updated context paths: %w", err)
+ return nil, fmt.Errorf("error removing updated context paths: %w", err)
}
err = writeContextParts(updatedParts)
if err != nil {
- return nil, 0, false, fmt.Errorf("error writing updated context parts: %w", err)
+ return nil, fmt.Errorf("error writing updated context parts: %w", err)
}
planState.ContextTokens = totalTokens
+ planState.ContextUpdatableTokens = totalUpdatableTokens
err = SetPlanState(planState, ts)
if err != nil {
- return nil, 0, false, fmt.Errorf("error writing plan state: %w", err)
+ return nil, fmt.Errorf("error writing plan state: %w", err)
+ }
+ }
+
+ var numFiles int
+ var numUrls int
+ var numTrees int
+ for _, part := range updatedParts {
+ if part.Type == shared.ContextFileType {
+ numFiles++
+ }
+ if part.Type == shared.ContextURLType {
+ numUrls++
+ }
+ if part.Type == shared.ContextDirectoryTreeType {
+ numTrees++
}
}
- return updatedPaths, tokensDiff, totalTokens > maxTokens, nil
+ return &updateRes{
+ UpdatedParts: updatedParts,
+ TokenDiffsByName: tokenDiffsByName,
+ TokensDiff: tokensDiff,
+ TotalTokens: totalTokens,
+ MaxExceeded: totalTokens > maxTokens,
+ NumFiles: numFiles,
+ NumUrls: numUrls,
+ NumTrees: numTrees,
+ }, nil
+
}
diff --git a/cli/lib/proposal.go b/cli/lib/proposal.go
index f01f95e9..0fbfcd30 100644
--- a/cli/lib/proposal.go
+++ b/cli/lib/proposal.go
@@ -7,6 +7,8 @@ import (
"os"
"plandex/format"
"plandex/types"
+ "strconv"
+ "strings"
"time"
"github.com/fatih/color"
@@ -23,9 +25,25 @@ type key struct {
func Propose(prompt string) error {
var err error
- start := time.Now()
+ planState, err := GetPlanState()
+ if err != nil {
+ return fmt.Errorf("failed to get plan state: %s", err)
+ }
s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)
+ if planState.ContextUpdatableTokens > 0 {
+ shouldContinue, err := checkOutdatedContext(s)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to check outdated context: %v", err)
+ return err
+ }
+
+ if !shouldContinue {
+ return nil
+ }
+ }
+
+ start := time.Now()
s.Prefix = "πŸ’¬ Sending prompt... "
s.Start()
@@ -64,11 +82,6 @@ func Propose(prompt string) error {
var parentProposalId string
var rootId string
- planState, err := GetPlanState()
-
- if err != nil {
- return fmt.Errorf("failed to get plan state: %s", err)
- }
parentProposalId = planState.ProposalId
rootId = planState.RootId
@@ -390,3 +403,109 @@ func Abort(proposalId string) error {
err := Api.Abort(proposalId)
return err
}
+
+func checkOutdatedContext(s *spinner.Spinner) (bool, error) {
+ start := time.Now()
+ s.Prefix = "πŸ”¬ Checking context... "
+ s.Start()
+
+ stopSpinner := func() {
+ s.Stop()
+ ClearCurrentLine()
+ }
+
+ outdatedRes, err := CheckOutdatedContext()
+ if err != nil {
+ stopSpinner()
+ return false, fmt.Errorf("failed to check outdated context: %s", err)
+ }
+
+ elapsed := time.Since(start)
+ if elapsed < 700*time.Millisecond {
+ time.Sleep(700*time.Millisecond - elapsed)
+ }
+
+ stopSpinner()
+
+ if len(outdatedRes.UpdatedParts) == 0 {
+ fmt.Println("βœ… Context is up to date")
+ return true, nil
+ }
+ types := []string{}
+ if outdatedRes.NumFiles > 0 {
+ lbl := "file"
+ if outdatedRes.NumFiles > 1 {
+ lbl = "files"
+ }
+ lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl
+ types = append(types, lbl)
+ }
+ if outdatedRes.NumUrls > 0 {
+ lbl := "url"
+ if outdatedRes.NumUrls > 1 {
+ lbl = "urls"
+ }
+ lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl
+ types = append(types, lbl)
+ }
+ if outdatedRes.NumTrees > 0 {
+ lbl := "directory tree"
+ if outdatedRes.NumTrees > 1 {
+ lbl = "directory trees"
+ }
+ lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl
+ types = append(types, lbl)
+ }
+
+ var msg string
+ if len(types) <= 2 {
+ msg += strings.Join(types, " and ")
+ } else {
+ for i, add := range types {
+ if i == len(types)-1 {
+ msg += ", and " + add
+ } else {
+ msg += ", " + add
+ }
+ }
+ }
+
+ color.New(color.FgHiCyan, color.Bold).Printf("%s in context have been modified πŸ‘‡\n\n", msg)
+
+ table := TableForContextUpdateRes(outdatedRes)
+ table.Render()
+
+ fmt.Println()
+
+ var input func() (bool, error)
+ input = func() (bool, error) {
+ color.New(color.FgHiGreen, color.Bold).Println("Update context now? (y)es | (n)o | (c)ancel")
+ color.New(color.FgHiGreen, color.Bold).Print("> ")
+
+ char, err := getUserInput()
+ if err != nil {
+ return false, fmt.Errorf("failed to get user input: %s", err)
+ }
+
+ fmt.Println(string(char))
+ if char == 'y' || char == 'Y' {
+ MustUpdateContextWithOuput()
+ return true, nil
+ } else if char == 'n' || char == 'N' {
+ return true, nil
+ } else if char == 'c' || char == 'C' {
+ return false, nil
+ } else {
+ fmt.Println()
+ color.New(color.FgHiRed, color.Bold).Print("Invalid input.\nEnter 'y' for yes, 'n' for no, or 'c' to cancel.\n\n")
+ return input()
+ }
+ }
+
+ shouldContinue, err := input()
+ if err != nil {
+ return false, fmt.Errorf("failed to get user input: %s", err)
+ }
+
+ return shouldContinue, nil
+}
diff --git a/cli/types/types.go b/cli/types/types.go
index ae778029..7893c5d9 100644
--- a/cli/types/types.go
+++ b/cli/types/types.go
@@ -33,15 +33,16 @@ type AppendConversationParams struct {
}
type PlanState struct {
- Name string `json:"name"`
- ProposalId string `json:"proposalId"`
- RootId string `json:"rootId"`
- CreatedAt string `json:"createdAt"`
- UpdatedAt string `json:"updatedAt"`
- Description *shared.PlanDescription `json:"description"`
- ContextTokens int `json:"contextTokens"`
- ConvoTokens int `json:"convoTokens"`
- ConvoSummarizedTokens int `json:"convoSummarizedTokens"`
+ Name string `json:"name"`
+ ProposalId string `json:"proposalId"`
+ RootId string `json:"rootId"`
+ CreatedAt string `json:"createdAt"`
+ UpdatedAt string `json:"updatedAt"`
+ Description *shared.PlanDescription `json:"description"`
+ ContextTokens int `json:"contextTokens"`
+ ContextUpdatableTokens int `json:"contextUpdatableTokens"`
+ ConvoTokens int `json:"convoTokens"`
+ ConvoSummarizedTokens int `json:"convoSummarizedTokens"`
}
type PlanSettings struct {
@@ -52,7 +53,4 @@ type LoadContextParams struct {
Note string
Recursive bool
NamesOnly bool
- // Truncate bool
- // MaxDepth int
- // MaxTokens int
}
diff --git a/server/model/proposal/build.go b/server/model/proposal/build.go
index da0256aa..94b3d503 100644
--- a/server/model/proposal/build.go
+++ b/server/model/proposal/build.go
@@ -60,7 +60,7 @@ func buildPlan(proposalId string, fileContents map[string]string, numTokensByFil
var fileContext *shared.ModelContextPart
for _, part := range proposal.Request.ModelContext {
if part.FilePath == filePath {
- fileContext = &part
+ fileContext = part
break
}
}