Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
Fix to prevent loading of duplicate files, directory trees, or urls into context, fixes #57
Dane Schneiderff27a3fec954
diff --git a/app/cli/cmd/ls.go b/app/cli/cmd/ls.goindex 3803a360..fff09c8a 100644--- a/app/cli/cmd/ls.go+++ b/app/cli/cmd/ls.go@@ -49,7 +49,7 @@ func listContext(cmd *cobra.Command, args []string) {for i, context := range contexts {totalTokens += context.NumTokens- t, icon := lib.GetContextTypeAndIcon(context)+ t, icon := lib.GetContextLabelAndIcon(context.ContextType)row := []string{strconv.Itoa(i + 1),diff --git a/app/cli/lib/context_display.go b/app/cli/lib/context_display.goindex d27ab238..6093e719 100644--- a/app/cli/lib/context_display.go+++ b/app/cli/lib/context_display.go@@ -2,26 +2,26 @@ package libimport "github.com/plandex/plandex/shared"-func GetContextTypeAndIcon(context *shared.Context) (string, string) {+func GetContextLabelAndIcon(contextType shared.ContextType) (string, string) {var icon string- var t string- switch context.ContextType {+ var lbl string+ switch contextType {case shared.ContextFileType:icon = "π"- t = "file"+ lbl = "file"case shared.ContextURLType:icon = "π"- t = "url"+ lbl = "url"case shared.ContextDirectoryTreeType:icon = "π "- t = "tree"+ lbl = "tree"case shared.ContextNoteType:icon = "βοΈ "- t = "note"+ lbl = "note"case shared.ContextPipedDataType:icon = "βοΈ "- t = "piped"+ lbl = "piped"}- return t, icon+ return lbl, icon}diff --git a/app/cli/lib/context_load.go b/app/cli/lib/context_load.goindex 7d3ae06f..fd50f93a 100644--- a/app/cli/lib/context_load.go+++ b/app/cli/lib/context_load.go@@ -11,6 +11,7 @@ import ("plandex/types""plandex/url""strings"+ "sync""github.com/fatih/color""github.com/plandex/plandex/shared"@@ -65,11 +66,30 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {}}- contextCh := make(chan *shared.LoadContextParams)- errCh := make(chan error)+ var contextMu sync.Mutex+ errCh := make(chan error)ignoredPaths := make(map[string]string)+ numRoutines := 0++ // filter out already loaded contexts+ alreadyLoadedByComposite := make(map[string]string)+ existingContexts, apiErr := api.Client.ListContext(CurrentPlanId, CurrentBranch)+ if apiErr != nil {+ onErr(fmt.Errorf("failed to list contexts: %v", apiErr.Msg))+ }++ existsByComposite := make(map[string]bool)+ for _, context := range existingContexts {+ switch context.ContextType {+ case shared.ContextFileType, shared.ContextDirectoryTreeType:+ existsByComposite[strings.Join([]string{string(context.ContextType), context.FilePath}, "|")] = true+ case shared.ContextURLType:+ existsByComposite[strings.Join([]string{string(context.ContextType), context.Url}, "|")] = true+ }+ }+if len(inputFilePaths) > 0 {baseDir := fs.GetBaseDirForFilePaths(inputFilePaths)@@ -111,7 +131,13 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {if params.NamesOnly {for _, inputFilePath := range inputFilePaths {+ composite := strings.Join([]string{string(shared.ContextDirectoryTreeType), inputFilePath}, "|")+ if existsByComposite[composite] {+ alreadyLoadedByComposite[composite] = inputFilePath+ continue+ }+ numRoutines++go func(inputFilePath string) {flattenedPaths, err := ParseInputPaths([]string{inputFilePath}, params)if err != nil {@@ -143,13 +169,17 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {name = "parent"}- contextCh <- &shared.LoadContextParams{+ contextMu.Lock()+ defer contextMu.Unlock()+ loadContextReq = append(loadContextReq, &shared.LoadContextParams{ContextType: shared.ContextDirectoryTreeType,Name: name,Body: body,FilePath: inputFilePath,ForceSkipIgnore: params.ForceSkipIgnore,- }+ })++ errCh <- nil}(inputFilePath)}@@ -176,7 +206,14 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {inputFilePaths = flattenedPathsfor _, path := range flattenedPaths {+ composite := strings.Join([]string{string(shared.ContextFileType), path}, "|")+ if existsByComposite[composite] {+ alreadyLoadedByComposite[composite] = path+ continue+ }++ numRoutines++go func(path string) {fileContent, err := os.ReadFile(path)if err != nil {@@ -185,12 +222,17 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {}body := string(fileContent)- contextCh <- &shared.LoadContextParams{+ contextMu.Lock()+ defer contextMu.Unlock()++ loadContextReq = append(loadContextReq, &shared.LoadContextParams{ContextType: shared.ContextFileType,Name: path,Body: body,FilePath: path,- }+ })++ errCh <- nil}(path)}}@@ -198,6 +240,13 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {if len(inputUrls) > 0 {for _, u := range inputUrls {+ composite := strings.Join([]string{string(shared.ContextURLType), u}, "|")+ if existsByComposite[composite] {+ alreadyLoadedByComposite[composite] = u+ continue+ }++ numRoutines++go func(u string) {body, err := url.FetchURLContent(u)if err != nil {@@ -211,36 +260,25 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {name = name[:20] + "β―" + name[len(name)-20:]}- contextCh <- &shared.LoadContextParams{+ contextMu.Lock()+ defer contextMu.Unlock()++ loadContextReq = append(loadContextReq, &shared.LoadContextParams{ContextType: shared.ContextURLType,Name: name,Body: body,Url: u,- }+ })++ errCh <- nil}(u)}}- contexts, _ := api.Client.ListContext(CurrentPlanId, CurrentBranch)-- for i := 0; i < len(inputFilePaths)+len(inputUrls); i++ {- select {- case err := <-errCh:+ for i := 0; i < numRoutines; i++ {+ err := <-errCh+ if err != nil {onErr(err)- case context := <-contextCh:- exists := false- for _, c := range contexts {- if c.FilePath == context.FilePath && c.Url == context.Url {- exists = true- break- }- }-- if exists {- fmt.Printf("\nβ οΈ Context '%s' has already been loaded\n", context.Name)- } else {- loadContextReq = append(loadContextReq, context)- }}}@@ -260,6 +298,9 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {if len(loadContextReq) == 0 {term.StopSpinner()fmt.Println("π€·ββοΈ No context loaded")+ if len(alreadyLoadedByComposite) > 0 {+ printAlreadyLoadedMsg(alreadyLoadedByComposite)+ }if len(ignoredPaths) > 0 {printIgnoredMsg()}@@ -292,11 +333,32 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {fmt.Println("β " + res.Msg)+ if len(alreadyLoadedByComposite) > 0 {+ printAlreadyLoadedMsg(alreadyLoadedByComposite)+ }+if len(ignoredPaths) > 0 {printIgnoredMsg()}}+func printAlreadyLoadedMsg(alreadyLoadedByComposite map[string]string) {+ fmt.Println()+ pronoun := "they're"+ if len(alreadyLoadedByComposite) == 1 {+ pronoun = "it's"+ }+ fmt.Printf("π ββοΈ Skipped because %s already in context:\n", pronoun)+ for composite, name := range alreadyLoadedByComposite {+ parts := strings.Split(composite, "|")+ contextType := parts[0]++ _, icon := GetContextLabelAndIcon(shared.ContextType(contextType))++ fmt.Printf(" β’ %s %s\n", icon, name)+ }+}+func printIgnoredMsg() {fmt.Println()fmt.Println("βΉοΈ " + color.New(color.FgWhite).Sprint("Due to .gitignore or .plandexignore, some paths weren't loaded.\nUse --force / -f to load ignored paths."))diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.goindex 3efdd0b7..df71e339 100644--- a/app/cli/lib/context_update.go+++ b/app/cli/lib/context_update.go@@ -465,7 +465,7 @@ func tableForContextOutdated(updatedContexts []*shared.Context, tokenDiffsById mtable.SetAutoWrapText(false)for _, context := range updatedContexts {- t, icon := GetContextTypeAndIcon(context)+ t, icon := GetContextLabelAndIcon(context.ContextType)diff := tokenDiffsById[context.Id]diffStr := "+" + strconv.Itoa(diff)diff --git a/releases/cli/versions/0.8.3.md b/releases/cli/versions/0.8.3.mdindex 6f001300..289c1bb6 100644--- a/releases/cli/versions/0.8.3.md+++ b/releases/cli/versions/0.8.3.md@@ -2,4 +2,5 @@- Make `gpt-4-turbo` model the new default model for the planner, builder, and auto-continue roles -- in testing it seems to be better at reasoning and significantly less lazy than the previous default for these roles, `gpt-4-turbo-preview` -- any plan that has not previously had its model settings modified will now use `gpt-4-turbo` by default (those that have been modified will need to be updated manually) -- remember that you can always use `plandex set-model` to change models for your plans- Fix for `set-model` command argument parsing (https://github.com/plandex-ai/plandex/issues/75)- Fix for panic during plan stream when a file name's length exceeds the terminal width (https://github.com/plandex-ai/plandex/issues/84)-- Fix for handling files that are loaded into context and later deleted from the file system (https://github.com/plandex-ai/plandex/issues/47)\ No newline at end of file+- Fix for handling files that are loaded into context and later deleted from the file system (https://github.com/plandex-ai/plandex/issues/47)+- Fix to prevent loading of duplicate files, directory trees, or urls into context (https://github.com/plandex-ai/plandex/issues/57)\ No newline at end of file