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 planning stages for better separation of context and task phases
Dane Schneiderf35ce1906ef8
diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex f971b607..1ea3f43a 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -13,6 +13,7 @@ import (shared "plandex-shared"+ "github.com/davecgh/go-spew/spew""github.com/google/uuid""github.com/sashabaranov/go-openai")@@ -28,6 +29,7 @@ func Tell(clients map[string]model.ClientInfo, plan *db.Plan, branch string, autreq.Prompt,false,req.AutoContext,+ req.SessionId,)if err != nil {@@ -84,26 +86,6 @@ func execTellPlan(params execTellPlanParams) {return}- // Load existing subtasks to log their state- // subtasks, err := db.GetPlanSubtasks(currentOrgId, plan.Id)- // if err != nil {- // log.Printf("[TellExec] Error loading subtasks: %v", err)- // } else {- // var unfinished []string- // var finished []string- // for _, task := range subtasks {- // if task.IsFinished {- // finished = append(finished, task.Title)- // } else {- // unfinished = append(unfinished, task.Title)- // }- // }- // log.Printf("[TellExec] Current subtask state - Total: %d, Finished: %d, Unfinished: %d", len(subtasks), len(finished), len(unfinished))- // log.Printf("[TellExec] Finished tasks: %v", finished)- // log.Printf("[TellExec] Unfinished tasks: %v", unfinished)- // log.Printf("[TellExec] Unfinished subtask reasoning: %s", unfinishedSubtaskReasoning)- // }-if missingFileResponse == "" {log.Println("Executing WillExecPlanHook")_, apiErr := hooks.ExecHook(hooks.WillExecPlan, hooks.HookParams{@@ -155,42 +137,98 @@ func execTellPlan(params execTellPlanParams) {}log.Println("execTellPlan - Tell plan loaded")- activatedPaths := state.resolveCurrentStage()+ activatePaths, activatePathsOrdered := state.resolveCurrentStage()++ var tentativeModelConfig shared.ModelRoleConfig+ var tentativeMaxTokens int+ if state.currentStage.TellStage == shared.TellStagePlanning {+ if state.currentStage.PlanningPhase == shared.PlanningPhaseContext {+ log.Println("Tell plan - isContextStage - setting modelConfig to context loader")+ tentativeModelConfig = state.settings.ModelPack.GetArchitect()+ tentativeMaxTokens = state.settings.GetArchitectEffectiveMaxTokens()+ } else {+ plannerConfig := state.settings.ModelPack.Planner+ tentativeModelConfig = plannerConfig.ModelRoleConfig+ tentativeMaxTokens = state.settings.GetPlannerEffectiveMaxTokens()+ }+ } else if state.currentStage.TellStage == shared.TellStageImplementation {+ tentativeModelConfig = state.settings.ModelPack.GetCoder()+ tentativeMaxTokens = tentativeModelConfig.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens+ } else {+ log.Printf("Tell plan - execTellPlan - unknown tell stage: %s\n", state.currentStage.TellStage)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Unknown tell stage",+ }+ return+ }+ state.tenativeModelConfig = &tentativeModelConfig++ ok, tokensWithoutContext := state.dryRunCalculateTokensWithoutContext(tentativeMaxTokens, unfinishedSubtaskReasoning)+ if !ok {+ return+ }- var baseContextMsg *types.ExtendedChatMessagePart- var autoContextMsg *types.ExtendedChatMessagePart- var smartContextMsg *types.ExtendedChatMessagePart+ var planStageSharedMsgs []*types.ExtendedChatMessagePart+ var planningPhaseOnlyMsgs []*types.ExtendedChatMessagePart+ var implementationMsgs []*types.ExtendedChatMessagePartif state.currentStage.TellStage == shared.TellStageImplementation {- smartContextMsg = state.formatModelContext(formatModelContextParams{+ implementationMsgs = state.formatModelContext(formatModelContextParams{includeMaps: false,smartContextEnabled: req.SmartContext,- execEnabled: req.ExecEnabled,+ includeApplyScript: req.ExecEnabled,})} else if state.currentStage.TellStage == shared.TellStagePlanning {- baseContextMsg = state.formatModelContext(formatModelContextParams{+ // add the shared context between planning and context phases first so it can be cached+ // this is just for the map and any manually loaded contexts - auto contexts will be added later+ planStageSharedMsgs = state.formatModelContext(formatModelContextParams{includeMaps: true,smartContextEnabled: req.SmartContext,- execEnabled: req.ExecEnabled,+ includeApplyScript: req.ExecEnabled,baseOnly: true,cacheControl: true,})- if state.currentStage.PlanningPhase == shared.PlanningPhasePlanning {+ if state.currentStage.PlanningPhase == shared.PlanningPhaseTasks {if req.AutoContext {- autoContextMsg = state.formatModelContext(formatModelContextParams{- includeMaps: false,- smartContextEnabled: req.SmartContext,- execEnabled: req.ExecEnabled,- activeOnly: true,- activatedPaths: activatedPaths,+ msg := types.ExtendedChatMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: []types.ExtendedChatMessagePart{},+ }+ for _, part := range planStageSharedMsgs {+ msg.Content = append(msg.Content, *part)+ }+ sharedMsgsTokens := model.GetMessagesTokenEstimate(msg)++ tokensRemaining := tentativeMaxTokens - (sharedMsgsTokens + tokensWithoutContext)++ if tokensRemaining < 0 {+ log.Println("tokensRemaining is negative")+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Max tokens exceeded before adding context",+ }+ return+ }++ planningPhaseOnlyMsgs = state.formatModelContext(formatModelContextParams{+ includeMaps: false,+ smartContextEnabled: req.SmartContext,+ includeApplyScript: false, // already included in planStageSharedMsgs+ activeOnly: true,+ activatePaths: activatePaths,+ activatePathsOrdered: activatePathsOrdered,+ maxTokens: int(float64(tokensRemaining) * 0.95), // leave a little extra room})} else {- // if auto context is disabled, just dump in everything, both basic and auto, that may have accumulated- autoContextMsg = state.formatModelContext(formatModelContextParams{+ // if auto context is disabled, just dump in any remaining auto contexts, since all basic contexts have already been added in planStageSharedMsgs+ planningPhaseOnlyMsgs = state.formatModelContext(formatModelContextParams{includeMaps: false,smartContextEnabled: req.SmartContext,- execEnabled: req.ExecEnabled,+ includeApplyScript: false, // already included in planStageSharedMsgsautoOnly: true,})}@@ -198,11 +236,10 @@ func execTellPlan(params execTellPlanParams) {}getTellSysPromptParams := getTellSysPromptParams{- autoContextEnabled: state.currentStage.TellStage == shared.TellStagePlanning && state.currentStage.PlanningPhase == shared.PlanningPhaseContext,- smartContextEnabled: req.SmartContext,- basicContextMsg: baseContextMsg,- autoContextMsg: autoContextMsg,- smartContextMsg: smartContextMsg,+ planStageSharedMsgs: planStageSharedMsgs,+ planningPhaseOnlyMsgs: planningPhaseOnlyMsgs,+ implementationMsgs: implementationMsgs,+ contextTokenLimit: tentativeMaxTokens,}// log.Println("getTellSysPromptParams:\n", spew.Sdump(getTellSysPromptParams))@@ -227,28 +264,22 @@ func execTellPlan(params execTellPlanParams) {},}- // Add a separate message for image contexts- imageContextTokens, ok := state.addImageContext()- if !ok {- return- }-promptMessage, ok := state.resolvePromptMessage(unfinishedSubtaskReasoning)if !ok {return}+ // log.Println("messages:\n\n", spew.Sdump(state.messages))+// log.Println("promptMessage:", spew.Sdump(promptMessage))state.tokensBeforeConvo =model.GetMessagesTokenEstimate(state.messages...) +model.GetMessagesTokenEstimate(*promptMessage) +state.latestSummaryTokens +- imageContextTokens +model.TokensPerRequest// print out breakdown of token usage- log.Printf("Image context tokens: %d\n", imageContextTokens)log.Printf("Latest summary tokens: %d\n", state.latestSummaryTokens)log.Printf("Total tokens before convo: %d\n", state.tokensBeforeConvo)@@ -268,6 +299,19 @@ func execTellPlan(params execTellPlanParams) {return}+ // add the prompt message to the end of the messages slice+ if promptMessage != nil {+ state.messages = append(state.messages, *promptMessage)+ } else {+ log.Println("promptMessage is nil")+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Prompt message isn't set",+ }+ return+ }+state.replyId = uuid.New().String()state.replyParser = types.NewReplyParser()@@ -282,17 +326,17 @@ func execTellPlan(params execTellPlanParams) {// log.Printf("%s: %s\n", message.Role, message.Content)// }- requestTokens := model.GetMessagesTokenEstimate(state.messages...) + imageContextTokens + model.TokensPerRequest+ requestTokens := model.GetMessagesTokenEstimate(state.messages...) + model.TokensPerRequeststate.totalRequestTokens = requestTokensstop := []string{"<PlandexFinish/>"}- var modelConfig shared.ModelRoleConfig+ modelConfig := tentativeModelConfigif state.currentStage.TellStage == shared.TellStagePlanning {- plannerConfig := state.settings.ModelPack.Planner.GetRoleForTokens(requestTokens)- modelConfig = plannerConfig.ModelRoleConfigif state.currentStage.PlanningPhase == shared.PlanningPhaseContext {log.Println("Tell plan - isContextStage - setting modelConfig to context loader")modelConfig = state.settings.ModelPack.GetArchitect().GetRoleForInputTokens(requestTokens)+ } else if state.currentStage.PlanningPhase == shared.PlanningPhaseTasks {+ modelConfig = state.settings.ModelPack.Planner.GetRoleForInputTokens(requestTokens)}} else if state.currentStage.TellStage == shared.TellStageImplementation {modelConfig = state.settings.ModelPack.GetCoder().GetRoleForInputTokens(requestTokens)@@ -309,7 +353,26 @@ func execTellPlan(params execTellPlanParams) {}}- log.Println("totalRequestTokens:", requestTokens)+ // if the model doesn't support images, remove any image parts from the messages+ if !modelConfig.BaseModelConfig.HasImageSupport {+ log.Println("Tell exec - model doesn't support images. Removing image parts from messages. File name will still be included.")++ for i := range state.messages {+ filteredContent := []types.ExtendedChatMessagePart{}+ for _, part := range state.messages[i].Content {+ if part.Type != openai.ChatMessagePartTypeImageURL {+ filteredContent = append(filteredContent, part)+ }+ }+ state.messages[i].Content = filteredContent+ }+ }++ log.Println("tell exec - will send model request with:", spew.Sdump(map[string]interface{}{+ "provider": modelConfig.BaseModelConfig.Provider,+ "model": modelConfig.BaseModelConfig.ModelName,+ "tokens": requestTokens,+ }))_, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{Auth: auth,@@ -380,3 +443,85 @@ func execTellPlan(params execTellPlanParams) {go state.listenStream(stream)}++func (state *activeTellStreamState) dryRunCalculateTokensWithoutContext(tentativeMaxTokens int, unfinishedSubtaskReasoning string) (bool, int) {+ clone := &activeTellStreamState{+ modelStreamId: state.modelStreamId,+ execTellPlanParams: state.execTellPlanParams,+ clients: state.clients,+ req: state.req,+ auth: state.auth,+ currentOrgId: state.currentOrgId,+ currentUserId: state.currentUserId,+ plan: state.plan,+ branch: state.branch,+ iteration: state.iteration,+ missingFileResponse: state.missingFileResponse,+ settings: state.settings,+ currentStage: state.currentStage,+ subtasks: state.subtasks,+ currentSubtask: state.currentSubtask,+ convo: state.convo,+ summaries: state.summaries,+ latestSummaryTokens: state.latestSummaryTokens,+ userPrompt: state.userPrompt,+ promptMessage: state.promptMessage,+ hasContextMap: state.hasContextMap,+ contextMapEmpty: state.contextMapEmpty,+ hasAssistantReply: state.hasAssistantReply,+ modelContext: state.modelContext,+ activePlan: state.activePlan,+ tenativeModelConfig: state.tenativeModelConfig,+ }++ sysParts, err := clone.getTellSysPrompt(getTellSysPromptParams{+ contextTokenLimit: tentativeMaxTokens,+ dryRunWithoutContext: true,+ })++ if err != nil {+ log.Printf("error getting tell sys prompt for dry run token calculation: %v", err)+ state.activePlan.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting tell sys prompt for dry run token calculation",+ }+ return false, 0+ }++ clone.messages = []types.ExtendedChatMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: sysParts,+ },+ }++ promptMessage, ok := clone.resolvePromptMessage(unfinishedSubtaskReasoning)+ if !ok {+ return false, 0+ }++ clone.tokensBeforeConvo =+ model.GetMessagesTokenEstimate(clone.messages...) ++ model.GetMessagesTokenEstimate(*promptMessage) ++ clone.latestSummaryTokens ++ model.TokensPerRequest++ if clone.tokensBeforeConvo > clone.settings.GetPlannerEffectiveMaxTokens() {+ log.Println("tokensBeforeConvo exceeds max tokens during dry run")+ state.activePlan.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Max tokens exceeded before adding conversation",+ }+ return false, 0+ }++ if !clone.addConversationMessages() {+ return false, 0+ }++ clone.messages = append(clone.messages, *promptMessage)++ return true, model.GetMessagesTokenEstimate(clone.messages...) + model.TokensPerRequest+}diff --git a/app/server/model/plan/tell_stage.go b/app/server/model/plan/tell_stage.goindex 2ac3b0dc..57af035e 100644--- a/app/server/model/plan/tell_stage.go+++ b/app/server/model/plan/tell_stage.go@@ -8,7 +8,18 @@ import ("github.com/sashabaranov/go-openai")-func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[string]bool) {+func (state *activeTellStreamState) lastSuccessfulConvoMessage() *db.ConvoMessage {+ for i := len(state.convo) - 1; i >= 0; i-- {+ msg := state.convo[i]+ if msg.Stopped || msg.Flags.HasError {+ continue+ }+ return msg+ }+ return nil+}++func (state *activeTellStreamState) resolveCurrentStage() (activatePaths map[string]bool, activatePathsOrdered []string) {req := state.reqiteration := state.iterationhasContextMap := state.hasContextMap@@ -17,15 +28,10 @@ func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[stlog.Printf("[resolveCurrentStage] Initial state: hasContextMap: %v, convo len: %d", hasContextMap, len(convo))- var lastConvoMsg *db.ConvoMessage- if len(convo) > 0 {- lastConvoMsg = convo[len(convo)-1]- log.Printf("[resolveCurrentStage] Last convo message - Role: %s, Flags: %+v", lastConvoMsg.Role, lastConvoMsg.Flags)- } else {- log.Println("[resolveCurrentStage] No previous conversation messages")- }+ lastConvoMsg := state.lastSuccessfulConvoMessage()- activatedPaths = map[string]bool{}+ activatePaths = map[string]bool{}+ activatePathsOrdered = []string{}isContinueFromAssistantMsg := false@@ -33,15 +39,15 @@ func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[stisContinueFromAssistantMsg = iteration == 0 && req.IsUserContinue && lastConvoMsg.Role == openai.ChatMessageRoleAssistantlog.Printf("[resolveCurrentStage] isContinueFromAssistantMsg: %v (IsUserContinue: %v, LastMsgRole: %s)",isContinueFromAssistantMsg, req.IsUserContinue, lastConvoMsg.Role)+ } else {+ log.Println("[resolveCurrentStage] No previous successful conversation message found")}isUserPrompt := false- // isApplyDebug := falseif !isContinueFromAssistantMsg {isUserPrompt = lastConvoMsg == nil || lastConvoMsg.Role == openai.ChatMessageRoleUserlog.Printf("[resolveCurrentStage] isUserPrompt: %v", isUserPrompt)- // isApplyDebug = isUserPrompt && ((lastConvoMsg != nil && lastConvoMsg.Flags.IsApplyDebug) || (req.IsApplyDebug))}var tellStage shared.TellStage@@ -70,8 +76,9 @@ func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[stlog.Printf("[resolveCurrentStage] Last convo message flags: %+v", flags)if flags.CurrentStage.TellStage == shared.TellStagePlanning && flags.CurrentStage.PlanningPhase == shared.PlanningPhaseContext {wasContextStage = true- activatedPaths = lastConvoMsg.ActivatedPaths- log.Printf("[resolveCurrentStage] Was context stage, copied activatedPaths: %v", activatedPaths)+ activatePaths = lastConvoMsg.ActivatedPaths+ activatePathsOrdered = lastConvoMsg.ActivatedPathsOrdered+ log.Printf("[resolveCurrentStage] Was context stage, copied activatePaths: %v", activatePaths)}}@@ -81,8 +88,8 @@ func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[stlog.Printf("[resolveCurrentStage] Set planningPhase to Context - AutoContext: %v, hasContextMap: %v, contextMapEmpty: %v, wasContextStage: %v",req.AutoContext, hasContextMap, contextMapEmpty, wasContextStage)} else {- planningPhase = shared.PlanningPhasePlanning- log.Printf("[resolveCurrentStage] Set planningPhase to Planning - AutoContext: %v, hasContextMap: %v, contextMapEmpty: %v, wasContextStage: %v",+ planningPhase = shared.PlanningPhaseTasks+ log.Printf("[resolveCurrentStage] Set planningPhase to Tasks - AutoContext: %v, hasContextMap: %v, contextMapEmpty: %v, wasContextStage: %v",req.AutoContext, hasContextMap, contextMapEmpty, wasContextStage)}}@@ -93,5 +100,5 @@ func (state *activeTellStreamState) resolveCurrentStage() (activatedPaths map[st}log.Printf("[resolveCurrentStage] Final state - TellStage: %s, PlanningPhase: %s", tellStage, planningPhase)- return activatedPaths+ return activatePaths, activatePathsOrdered}diff --git a/app/shared/data_models.go b/app/shared/data_models.goindex 131c28bf..0ff33116 100644--- a/app/shared/data_models.go+++ b/app/shared/data_models.go@@ -125,8 +125,8 @@ const (type PlanningPhase stringconst (- PlanningPhaseContext PlanningPhase = "context"- PlanningPhasePlanning PlanningPhase = "planning"+ PlanningPhaseContext PlanningPhase = "context"+ PlanningPhaseTasks PlanningPhase = "tasks")type CurrentStage struct {@@ -147,6 +147,7 @@ type ConvoMessageFlags struct {HasUnfinishedSubtasks bool `json:"hasUnfinishedSubtasks"`IsApplyDebug bool `json:"isApplyDebug"`IsUserDebug bool `json:"isUserDebug"`+ HasError bool `json:"hasError"`}type Subtask struct {@@ -376,5 +377,19 @@ type CreditsTransaction struct {DebitCacheDiscount *decimal.Decimal `json:"debitCacheDiscount,omitempty"`+ DebitSessionId *string `json:"debitSessionId,omitempty"`+CreatedAt time.Time `json:"createdAt"`}++func (t *CreditsTransaction) ModelString() string {+ s := ""+ if t.DebitModelProvider != nil && *t.DebitModelProvider != ModelProviderOpenAI {+ s += string(*t.DebitModelProvider) + "/"+ }+ if t.DebitModelName != nil {+ s += *t.DebitModelName+ }++ return s+}