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
refactor: improve context handling and mapping
Dane Schneider8a7ed191db9a
diff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.goindex d339ca61..25deb187 100644--- a/app/server/handlers/context_helper.go+++ b/app/server/handlers/context_helper.go@@ -36,10 +36,18 @@ func loadContexts(cachedMapsByPath := params.cachedMapsByPathautoLoaded := params.autoLoaded+ log.Printf("Starting loadContexts with %d contexts, cachedMapsByPath: %v, autoLoaded: %v", len(*loadReq), cachedMapsByPath != nil, autoLoaded)+// check file count and size limitstotalFiles := 0+ mapFilesCount := 0for _, context := range *loadReq {totalFiles+++ if context.ContextType == shared.ContextMapType {+ mapFilesCount+++ log.Printf("Found map file: %s with %d map inputs", context.FilePath, len(context.MapInputs))+ }+if totalFiles > shared.MaxContextCount {log.Printf("Error: Too many contexts to load (found %d, limit is %d)\n", totalFiles, shared.MaxContextCount)http.Error(w, fmt.Sprintf("Too many contexts to load (found %d, limit is %d)", totalFiles, shared.MaxContextCount), http.StatusBadRequest)@@ -54,6 +62,10 @@ func loadContexts(}}+ if mapFilesCount > 0 {+ log.Printf("Processing %d map files out of %d total contexts", mapFilesCount, totalFiles)+ }+var err errorvar settings *shared.PlanSettings@@ -151,6 +163,11 @@ func loadContexts(}()}+ log.Printf("Calling db.LoadContexts with %d contexts, %d cached maps", len(*loadReq), len(cachedMapsByPath))+ for path := range cachedMapsByPath {+ log.Printf("Using cached map for path: %s", path)+ }+res, dbContexts, err := db.LoadContexts(ctx, db.LoadContextsParams{OrgId: auth.OrgId,Plan: plan,@@ -167,6 +184,20 @@ func loadContexts(return nil, nil}+ log.Printf("db.LoadContexts completed successfully, loaded %d contexts", len(dbContexts))++ // Log information about loaded map contexts+ mapContextsCount := 0+ for _, context := range dbContexts {+ if context.ContextType == shared.ContextMapType {+ mapContextsCount+++ log.Printf("Loaded map context: %s, path: %s, tokens: %d", context.Name, context.FilePath, context.NumTokens)+ }+ }+ if mapContextsCount > 0 {+ log.Printf("Successfully loaded %d map contexts out of %d total contexts", mapContextsCount, len(dbContexts))+ }+if res.MaxTokensExceeded {log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", res.TotalTokens, res.MaxTokens)bytes, err := json.Marshal(res)diff --git a/app/server/handlers/file_maps.go b/app/server/handlers/file_maps.goindex f92e017c..8ea24331 100644--- a/app/server/handlers/file_maps.go+++ b/app/server/handlers/file_maps.go@@ -30,7 +30,22 @@ func GetFileMapHandler(w http.ResponseWriter, r *http.Request) {return}- log.Println("GetFileMapHandler checking server resources")+ log.Println("GetFileMapHandler: checking limits")++ if len(req.MapInputs) > shared.MaxContextMapPaths {+ http.Error(w, fmt.Sprintf("Too many files to map: %d (max %d)", len(req.MapInputs), shared.MaxContextMapPaths), http.StatusBadRequest)+ return+ }++ totalSize := 0+ for _, input := range req.MapInputs {+ totalSize += len(input)+ }++ if totalSize > shared.MaxContextMapInputSize {+ http.Error(w, fmt.Sprintf("Too many files to map: %d (max %d)", totalSize, shared.MaxContextMapInputSize), http.StatusBadRequest)+ return+ }maps := make(shared.FileMapBodies)diff --git a/app/server/model/plan/tell_context.go b/app/server/model/plan/tell_context.goindex 871ba45c..35e2092a 100644--- a/app/server/model/plan/tell_context.go+++ b/app/server/model/plan/tell_context.go@@ -225,7 +225,7 @@ func (state *activeTellStreamState) checkAutoLoadContext() checkAutoLoadContextR// pick out all potential file paths within backticksmatches := pathRegex.FindAllStringSubmatch(activePlan.CurrentReplyContent, -1)- toAutoLoad := []string{}+ toAutoLoad := map[string]bool{}toActivate := map[string]bool{}for _, match := range matches {@@ -237,16 +237,21 @@ func (state *activeTellStreamState) checkAutoLoadContext() checkAutoLoadContextRif req.ProjectPaths[trimmed] {toActivate[trimmed] = trueif contextsByPath[trimmed] == nil {- toAutoLoad = append(toAutoLoad, trimmed)+ toAutoLoad[trimmed] = true}}}- log.Printf("Tell plan - checkAutoLoadContext - toAutoLoad: %v\n", toAutoLoad)+ toAutoLoadPaths := []string{}+ for path := range toAutoLoad {+ toAutoLoadPaths = append(toAutoLoadPaths, path)+ }++ log.Printf("Tell plan - checkAutoLoadContext - toAutoLoad: %v\n", toAutoLoadPaths)log.Printf("Tell plan - checkAutoLoadContext - toActivate: %v\n", toActivate)return checkAutoLoadContextResult{- autoLoadPaths: toAutoLoad,+ autoLoadPaths: toAutoLoadPaths,activatePaths: toActivate,}}diff --git a/app/server/model/plan/tell_prompt_message.go b/app/server/model/plan/tell_prompt_message.goindex aa81c048..87a0eb35 100644--- a/app/server/model/plan/tell_prompt_message.go+++ b/app/server/model/plan/tell_prompt_message.go@@ -58,6 +58,7 @@ func (state *activeTellStreamState) resolvePromptMessage(CreatePromptParams: prompts.CreatePromptParams{AutoContext: req.AutoContext,ExecMode: req.ExecEnabled,+ IsGitRepo: req.IsGitRepo,// no need to pass in IsUserDebug or IsApplyDebug here because it's a chat message},Prompt: prompt,@@ -91,6 +92,7 @@ func (state *activeTellStreamState) resolvePromptMessage(CreatePromptParams: prompts.CreatePromptParams{ExecMode: req.ExecEnabled,AutoContext: req.AutoContext,+ IsGitRepo: req.IsGitRepo,// no need to pass in IsUserDebug or IsApplyDebug here because we're continuing},Prompt: content,@@ -119,6 +121,7 @@ func (state *activeTellStreamState) resolvePromptMessage(CreatePromptParams: prompts.CreatePromptParams{ExecMode: req.ExecEnabled,AutoContext: req.AutoContext,+ IsGitRepo: req.IsGitRepo,// no need to pass in IsUserDebug or IsApplyDebug here because we're continuing},Prompt: prompts.UserContinuePrompt,@@ -159,6 +162,7 @@ func (state *activeTellStreamState) resolvePromptMessage(AutoContext: req.AutoContext,IsUserDebug: req.IsUserDebug,IsApplyDebug: req.IsApplyDebug,+ IsGitRepo: req.IsGitRepo,},Prompt: prompt,OsDetails: req.OsDetails,diff --git a/app/server/model/plan/tell_sys_prompt.go b/app/server/model/plan/tell_sys_prompt.goindex bf71d770..b65f531e 100644--- a/app/server/model/plan/tell_sys_prompt.go+++ b/app/server/model/plan/tell_sys_prompt.go@@ -37,6 +37,7 @@ func (state *activeTellStreamState) getTellSysPrompt(params getTellSysPromptParaAutoContext: autoContextEnabled,IsUserDebug: req.IsUserDebug,IsApplyDebug: req.IsApplyDebug,+ IsGitRepo: req.IsGitRepo,}// log.Println("getTellSysPrompt - prompt params:", spew.Sdump(params))diff --git a/app/shared/context.go b/app/shared/context.goindex 43972c5d..a3190bbf 100644--- a/app/shared/context.go+++ b/app/shared/context.go@@ -226,13 +226,22 @@ func SummaryForRemoveContext(contexts []*Context, previousTotalTokens int) strinreturn fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ", len(contexts), suffix, removedTokens, totalTokens)}-func SummaryForUpdateContext(updateRes *ContextUpdateResult) string {- numFiles := updateRes.NumFiles- numTrees := updateRes.NumTrees- numUrls := updateRes.NumUrls- numMaps := updateRes.NumMaps- tokensDiff := updateRes.TokensDiff- totalTokens := updateRes.TotalTokens+type SummaryForUpdateContextParams struct {+ NumFiles int+ NumTrees int+ NumUrls int+ NumMaps int+ TokensDiff int+ TotalTokens int+}++func SummaryForUpdateContext(params SummaryForUpdateContextParams) string {+ numFiles := params.NumFiles+ numTrees := params.NumTrees+ numUrls := params.NumUrls+ numMaps := params.NumMaps+ tokensDiff := params.TokensDiff+ totalTokens := params.TotalTokensmsg := "Updated"var toAdd []string