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
separating maps from context in autocontext mode, immediately using whole file build with gpt4o speculative decoding (prediction field) using a custom request. mark convo messages that loaded context, added tasks, or wrote code, other minor fixes
Dane Schneider9a6d42215634
diff --git a/app/cli/cmd/convo.go b/app/cli/cmd/convo.goindex da018728..9aca3228 100644--- a/app/cli/cmd/convo.go+++ b/app/cli/cmd/convo.go@@ -11,6 +11,7 @@ import ("time""github.com/fatih/color"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -103,6 +104,15 @@ func convo(cmd *cobra.Command, args []string) {author = msg.Role}+ var replyType string+ if msg.ReplyType == shared.ReplyTypeLoadedContext {+ replyType = "π₯ Loaded Context"+ } else if msg.ReplyType == shared.ReplyTypeMadePlan {+ replyType = "π Made Plan"+ } else if msg.ReplyType == shared.ReplyTypeImplementation {+ replyType = "π¨βπ» Wrote Code"+ }+// format as above but start with day of weekformattedTs := msg.CreatedAt.Local().Format("Mon Jan 2, 2006 | 3:04pm MST")@@ -116,8 +126,14 @@ func convo(cmd *cobra.Command, args []string) {formattedTs = msg.CreatedAt.Local().Format("Yesterday | 3:04pm MST")}- header := fmt.Sprintf("#### %d | %s | %s | %d πͺ ", i+1,- author, formattedTs, msg.Tokens)+ var header string+ if replyType != "" {+ header = fmt.Sprintf("#### %d | %s | %s | %s | %d πͺ ", i+1,+ author, replyType, formattedTs, msg.Tokens)+ } else {+ header = fmt.Sprintf("#### %d | %s | %s | %d πͺ ", i+1,+ author, formattedTs, msg.Tokens)+ }txt := msg.Messageif !convoRaw {diff --git a/app/cli/cmd/ls.go b/app/cli/cmd/ls.goindex ae7e0d8d..e95946e4 100644--- a/app/cli/cmd/ls.go+++ b/app/cli/cmd/ls.go@@ -12,6 +12,7 @@ import ("github.com/fatih/color""github.com/olekukonko/tablewriter"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -28,13 +29,20 @@ func listContext(cmd *cobra.Command, args []string) {term.StartSpinner("")contexts, err := api.Client.ListContext(lib.CurrentPlanId, lib.CurrentBranch)- term.StopSpinner()if err != nil {term.OutputErrorAndExit("Error listing context: %v", err)}+ planConfig, err := api.Client.GetPlanConfig(lib.CurrentPlanId)+ if err != nil {+ term.OutputErrorAndExit("Error getting plan config: %v", err)+ }+ term.StopSpinner()+totalTokens := 0+ totalPlannerTokens := 0+ totalMapTokens := 0table := tablewriter.NewWriter(os.Stdout)table.SetHeader([]string{"#", "Name", "Type", "πͺ", "Added", "Updated"})table.SetAutoWrapText(false)@@ -49,6 +57,12 @@ func listContext(cmd *cobra.Command, args []string) {for i, context := range contexts {totalTokens += context.NumTokens+ if context.ContextType == shared.ContextMapType {+ totalMapTokens += context.NumTokens+ } else {+ totalPlannerTokens += context.NumTokens+ }+t, icon := context.TypeAndIcon()name := context.Name@@ -77,8 +91,16 @@ func listContext(cmd *cobra.Command, args []string) {tokensTbl := tablewriter.NewWriter(os.Stdout)tokensTbl.SetAutoWrapText(false)- tokensTbl.Append([]string{color.New(term.ColorHiCyan, color.Bold).Sprintf("Total tokens β") + color.New(color.Bold).Sprintf(" %d πͺ", totalTokens)})+ if planConfig.AutoLoadContext {+ tokensTbl.Append([]string{+ color.New(term.ColorHiCyan, color.Bold).Sprintf("Map tokens β") + color.New(color.Bold).Sprintf(" %d πͺ", totalMapTokens),+ color.New(term.ColorHiCyan, color.Bold).Sprintf("Context tokens β") + color.New(color.Bold).Sprintf(" %d πͺ", totalPlannerTokens),+ })+ } else {++ tokensTbl.Append([]string{color.New(term.ColorHiCyan, color.Bold).Sprintf("Total tokens β") + color.New(color.Bold).Sprintf(" %d πͺ", totalTokens)})+ }tokensTbl.Render()fmt.Println()diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.goindex 5ea352f3..0f6f5ada 100644--- a/app/server/db/context_helpers.go+++ b/app/server/db/context_helpers.go@@ -455,13 +455,22 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextResponreturn nil, nil, fmt.Errorf("error getting branch: %v", err)}totalTokens := branch.ContextTokens+ totalPlannerTokens := totalTokens+ totalMapTokens := 0settings, err := GetPlanSettings(plan, true)if err != nil {return nil, nil, fmt.Errorf("error getting settings: %v", err)}- maxTokens := settings.GetPlannerEffectiveMaxTokens()+ planConfig, err := GetPlanConfig(planId)+ if err != nil {+ return nil, nil, fmt.Errorf("error getting plan config: %v", err)+ }++ plannerMaxTokens := settings.GetPlannerEffectiveMaxTokens()+ contextLoaderMaxTokens := settings.GetContextLoaderEffectiveMaxTokens()+mapContextsByFilePath := make(map[string]Context)existingContexts, err := GetPlanContexts(orgId, planId, false, false)@@ -472,6 +481,11 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextResponfor _, context := range existingContexts {composite := strings.Join([]string{context.Name, string(context.ContextType)}, "|")existingContextsByName[composite] = true++ if planConfig.AutoLoadContext && context.ContextType == shared.ContextMapType {+ totalMapTokens += context.NumTokens+ totalPlannerTokens -= context.NumTokens+ }}var filteredReq []*shared.LoadContextParams@@ -492,7 +506,10 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextResponvar numTokens intvar err error+ var isMap bool+if context.ContextType == shared.ContextMapType && (len(context.MapInputs) > 0 || params.CachedMapsByPath != nil) {+ isMap = truevar mappedFiles shared.FileMapBodiesif params.CachedMapsByPath != nil && params.CachedMapsByPath[context.FilePath] != nil {log.Println("Using cached map for", context.FilePath)@@ -559,18 +576,35 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextResponparamsByTempId[tempId] = contextnumTokensByTempId[tempId] = numTokens-- tokensAdded += numTokenstotalTokens += numTokens++ // maps don't count toward the token limit if auto-loading+ if planConfig.AutoLoadContext && isMap {+ tokensAdded += numTokens+ totalMapTokens += numTokens+ } else {+ tokensAdded += numTokens+ totalPlannerTokens += numTokens+ }}// showElapsed("Loaded reqs")+ if planConfig.AutoLoadContext {+ if totalMapTokens > contextLoaderMaxTokens {+ return &shared.LoadContextResponse{+ TokensAdded: tokensAdded,+ TotalTokens: totalMapTokens,+ MaxTokens: contextLoaderMaxTokens,+ MaxTokensExceeded: true,+ }, nil, nil+ }+ }- if totalTokens > maxTokens {+ if totalPlannerTokens > plannerMaxTokens {return &shared.LoadContextResponse{TokensAdded: tokensAdded,- TotalTokens: totalTokens,- MaxTokens: maxTokens,+ TotalTokens: totalPlannerTokens,+ MaxTokens: plannerMaxTokens,MaxTokensExceeded: true,}, nil, nil}@@ -678,13 +712,36 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,return nil, fmt.Errorf("branch not found")}+ totalTokens := branch.ContextTokens+ totalPlannerTokens := totalTokens+ totalMapTokens := 0+settings, err := GetPlanSettings(plan, true)if err != nil {return nil, fmt.Errorf("error getting settings: %v", err)}- maxTokens := settings.GetPlannerEffectiveMaxTokens()- totalTokens := branch.ContextTokens+ planConfig, err := GetPlanConfig(planId)+ if err != nil {+ return nil, fmt.Errorf("error getting plan config: %v", err)+ }++ plannerMaxTokens := settings.GetPlannerEffectiveMaxTokens()+ contextLoaderMaxTokens := settings.GetContextLoaderEffectiveMaxTokens()++ if planConfig.AutoLoadContext {+ existingContexts, err := GetPlanContexts(orgId, planId, false, false)+ if err != nil {+ return nil, fmt.Errorf("error getting existing contexts: %v", err)+ }++ for _, context := range existingContexts {+ if context.ContextType == shared.ContextMapType {+ totalMapTokens += context.NumTokens+ totalPlannerTokens -= context.NumTokens+ }+ }+ }aggregateTokensDiff := 0tokenDiffsById := make(map[string]int)@@ -776,6 +833,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,tokenDiffsById[id] = tokenDiffaggregateTokensDiff += tokenDifftotalTokens += tokenDiff+ totalPlannerTokens += tokenDiffcontext.NumTokens = updateNumTokens}@@ -801,11 +859,11 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,}}- if totalTokens > maxTokens {+ if totalPlannerTokens > plannerMaxTokens {return &shared.UpdateContextResponse{TokensAdded: aggregateTokensDiff,TotalTokens: totalTokens,- MaxTokens: maxTokens,+ MaxTokens: plannerMaxTokens,MaxTokensExceeded: true,}, nil}@@ -870,6 +928,11 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,tokenDiffsById[id] = tokenDiffaggregateTokensDiff += tokenDifftotalTokens += tokenDiff+ if planConfig.AutoLoadContext {+ totalMapTokens += tokenDiff+ } else {+ totalPlannerTokens += tokenDiff+ }mu.Unlock()context.NumTokens = newNumTokens@@ -903,6 +966,17 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,}}+ if planConfig.AutoLoadContext {+ if totalMapTokens > contextLoaderMaxTokens {+ return &shared.UpdateContextResponse{+ TokensAdded: aggregateTokensDiff,+ TotalTokens: totalTokens,+ MaxTokens: contextLoaderMaxTokens,+ MaxTokensExceeded: true,+ }, nil+ }+ }+updateRes := &shared.ContextUpdateResult{UpdatedContexts: updatedContexts,TokenDiffsById: tokenDiffsById,@@ -912,7 +986,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,NumUrls: numUrls,NumTrees: numTrees,NumMaps: numMaps,- MaxTokens: maxTokens,+ MaxTokens: plannerMaxTokens,}err = AddPlanContextTokens(planId, branchName, aggregateTokensDiff)diff --git a/app/server/db/convo_helpers.go b/app/server/db/convo_helpers.goindex 780096a0..23b8fa8a 100644--- a/app/server/db/convo_helpers.go+++ b/app/server/db/convo_helpers.go@@ -10,6 +10,7 @@ import ("github.com/fatih/color""github.com/google/uuid"+ "github.com/plandex/plandex/shared""github.com/sashabaranov/go-openai")@@ -132,7 +133,29 @@ func StoreConvoMessage(message *ConvoMessage, currentUserId, branch string, comm}}- msg := fmt.Sprintf("Message #%d | %s | %d πͺ", message.Num, desc, message.Tokens)+ var replyType string+ if message.ReplyType == shared.ReplyTypeLoadedContext {+ replyType = "π₯ Loaded Context"+ } else if message.ReplyType == shared.ReplyTypeMadePlan {+ replyType = "π Made Plan"+ } else if message.ReplyType == shared.ReplyTypeImplementation {+ replyType = "π¨βπ» Wrote Code"+ }++ var msg string+ if replyType != "" {+ msg = fmt.Sprintf("Message #%d | %s | %s | %d πͺ", message.Num, desc, replyType, message.Tokens)+ } else {+ msg = fmt.Sprintf("Message #%d | %s | %d πͺ", message.Num, desc, message.Tokens)+ }++ // Cleaner without the cut off message - maybe need a separate command to show both the log and full messages?+ // cutoff := 140+ // if len(message.Message) > cutoff {+ // msg += "\n\n" + message.Message[:cutoff] + "..."+ // } else {+ // msg += "\n\n" + message.Message+ // }if commit {err = GitAddAndCommit(message.OrgId, message.PlanId, branch, msg)diff --git a/app/server/db/data_models.go b/app/server/db/data_models.goindex 31ccb0e0..78165f1b 100644--- a/app/server/db/data_models.go+++ b/app/server/db/data_models.go@@ -447,16 +447,17 @@ func (context *Context) ToApi() *shared.Context {}type ConvoMessage struct {- Id string `json:"id"`- OrgId string `json:"orgId"`- PlanId string `json:"planId"`- UserId string `json:"userId"`- Role string `json:"role"`- Tokens int `json:"tokens"`- Num int `json:"num"`- Message string `json:"message"`- Stopped bool `json:"stopped"`- CreatedAt time.Time `json:"createdAt"`+ Id string `json:"id"`+ OrgId string `json:"orgId"`+ PlanId string `json:"planId"`+ UserId string `json:"userId"`+ Role string `json:"role"`+ Tokens int `json:"tokens"`+ Num int `json:"num"`+ Message string `json:"message"`+ Stopped bool `json:"stopped"`+ ReplyType shared.ReplyType `json:"replyType"`+ CreatedAt time.Time `json:"createdAt"`}func (msg *ConvoMessage) ToApi() *shared.ConvoMessage {@@ -468,6 +469,7 @@ func (msg *ConvoMessage) ToApi() *shared.ConvoMessage {Num: msg.Num,Message: msg.Message,Stopped: msg.Stopped,+ ReplyType: msg.ReplyType,CreatedAt: msg.CreatedAt,}}diff --git a/app/server/go.mod b/app/server/go.modindex ff0c4433..271ced47 100644--- a/app/server/go.mod+++ b/app/server/go.mod@@ -8,7 +8,7 @@ require (github.com/gorilla/mux v1.8.1github.com/pkg/errors v0.9.1github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000- github.com/sashabaranov/go-openai v1.36.0+ github.com/sashabaranov/go-openai v1.36.1)require (@@ -18,7 +18,7 @@ require (github.com/hashicorp/errwrap v1.1.0 // indirectgithub.com/hashicorp/go-multierror v1.1.1 // indirectgithub.com/jmespath/go-jmespath v0.4.0 // indirect- github.com/mattn/go-colorable v0.1.13 // indirect+ github.com/mattn/go-colorable v0.1.14 // indirectgithub.com/mattn/go-isatty v0.0.20 // indirectgithub.com/mattn/go-runewidth v0.0.16 // indirectgithub.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect@@ -29,23 +29,23 @@ require (github.com/shopspring/decimal v1.4.0 // indirectgithub.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirectgo.uber.org/atomic v1.11.0 // indirect- golang.org/x/image v0.22.0 // indirect- golang.org/x/sys v0.27.0 // indirect+ golang.org/x/image v0.23.0 // indirect+ golang.org/x/sys v0.29.0 // indirectgopkg.in/yaml.v3 v3.0.1 // indirect)require (github.com/atotto/clipboard v0.1.4- github.com/aws/aws-sdk-go v1.55.5+ github.com/aws/aws-sdk-go v1.55.6github.com/fatih/color v1.18.0github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4- github.com/golang-migrate/migrate/v4 v4.18.1+ github.com/golang-migrate/migrate/v4 v4.18.2github.com/jmoiron/sqlx v1.4.0github.com/lib/pq v1.10.9github.com/rs/cors v1.11.1github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82github.com/stretchr/testify v1.10.0- golang.org/x/net v0.31.0+ golang.org/x/net v0.34.0)replace github.com/plandex/plandex/shared => ../shareddiff --git a/app/server/go.sum b/app/server/go.sumindex e63e013d..e13cc313 100644--- a/app/server/go.sum+++ b/app/server/go.sum@@ -8,11 +8,14 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1zgithub.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=+github.com/aws/aws-sdk-go v1.55.6 h1:cSg4pvZ3m8dgYcgqB97MrcdjUmZ1BeMYKUxMMB89IPk=+github.com/aws/aws-sdk-go v1.55.6/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/dhui/dktest v0.4.3 h1:wquqUxAFdcUgabAVLvSCOKOlag5cIZuaOjYIBOWdsR0=github.com/dhui/dktest v0.4.3/go.mod h1:zNK8IwktWzQRm6I/l2Wjp7MakiyaFWv4G1hjmodmMTs=+github.com/dhui/dktest v0.4.4 h1:+I4s6JRE1yGuqflzwqG+aIaMdgXIorCf5P98JnaAWa8=github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=@@ -37,12 +40,15 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvgithub.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE=github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=+github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=github.com/golang-migrate/migrate/v4 v4.18.1 h1:JML/k+t4tpHCpQTCAD62Nu43NUFzHY4CV3uAuvHGC+Y=github.com/golang-migrate/migrate/v4 v4.18.1/go.mod h1:HAX6m3sQgcdO81tdjn5exv20+3Kb13cmGli1hrD6hks=+github.com/golang-migrate/migrate/v4 v4.18.2 h1:2VSCMz7x7mjyTXx3m2zPokOY82LTRgxK1yQYKo6wWQ8=+github.com/golang-migrate/migrate/v4 v4.18.2/go.mod h1:2CM6tJvn2kqPXwnXO/d3rAQYiyoIm180VsO8PRX6Rpk=github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=@@ -62,6 +68,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=+github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=+github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=@@ -97,6 +105,8 @@ github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=+github.com/sashabaranov/go-openai v1.36.1 h1:EVfRXwIlW2rUzpx6vR+aeIKCK/xylSrVYAx1TMTSX3g=+github.com/sashabaranov/go-openai v1.36.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=@@ -118,12 +128,18 @@ go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4=+golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=+golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=+golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=+golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=+golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=+golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=diff --git a/app/server/handlers/client_helper.go b/app/server/handlers/client_helper.goindex e0f57f13..fe4448d4 100644--- a/app/server/handlers/client_helper.go+++ b/app/server/handlers/client_helper.go@@ -7,8 +7,6 @@ import ("plandex-server/hooks""plandex-server/model""plandex-server/types"-- "github.com/sashabaranov/go-openai")type initClientsParams struct {@@ -22,7 +20,7 @@ type initClientsParams struct {plan *db.Plan}-func initClients(params initClientsParams) map[string]*openai.Client {+func initClients(params initClientsParams) map[string]model.ClientInfo {w := params.wapiKey := params.apiKeyapiKeys := params.apiKeysdiff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.goindex 3cdf1618..9fc0512b 100644--- a/app/server/handlers/context_helper.go+++ b/app/server/handlers/context_helper.go@@ -11,7 +11,6 @@ import ("plandex-server/types""github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai")func loadContexts(@@ -44,7 +43,7 @@ func loadContexts(var err errorvar settings *shared.PlanSettings- var clients map[string]*openai.Client+ var clients map[string]model.ClientInfofor _, context := range *loadReq {if context.ContextType == shared.ContextPipedDataType || context.ContextType == shared.ContextNoteType || context.ContextType == shared.ContextImageType {diff --git a/app/server/model/client.go b/app/server/model/client.goindex d2d99670..f2455af9 100644--- a/app/server/model/client.go+++ b/app/server/model/client.go@@ -1,9 +1,14 @@package modelimport (+ "bytes""context"+ "encoding/json""fmt"+ "io""log"+ "net/http"+ "reflect""regexp""strings""time"@@ -12,11 +17,26 @@ import ("github.com/sashabaranov/go-openai")-const OPENAI_STREAM_CHUNK_TIMEOUT = time.Duration(30) * time.Second-const OPENAI_USAGE_CHUNK_TIMEOUT = time.Duration(5) * time.Second+const (+ OPENAI_STREAM_CHUNK_TIMEOUT = time.Duration(30) * time.Second+ OPENAI_USAGE_CHUNK_TIMEOUT = time.Duration(5) * time.Second+ OPENAI_MAX_RETRIES = 3+ OPENAI_MAX_WAIT_DURATION = 60 * time.Second+ OPENAI_ABORT_WAIT_DURATION = 120 * time.Second+ OPENAI_BACKOFF_MULTIPLIER = 3.0+)++var httpClient = &http.Client{}-func InitClients(apiKeys map[string]string, endpointsByApiKeyEnvVar map[string]string, openAIEndpoint, orgId string) map[string]*openai.Client {- clients := make(map[string]*openai.Client)+type ClientInfo struct {+ Client *openai.Client+ ApiKey string+ OrgId string+ Endpoint string+}++func InitClients(apiKeys map[string]string, endpointsByApiKeyEnvVar map[string]string, openAIEndpoint, orgId string) map[string]ClientInfo {+ clients := make(map[string]ClientInfo)for key, apiKey := range apiKeys {var clientEndpoint stringvar clientOrgId string@@ -31,7 +51,7 @@ func InitClients(apiKeys map[string]string, endpointsByApiKeyEnvVar map[string]sreturn clients}-func newClient(apiKey, endpoint, orgId string) *openai.Client {+func newClient(apiKey, endpoint, orgId string) ClientInfo {config := openai.DefaultConfig(apiKey)if endpoint != "" {config.BaseURL = endpoint@@ -40,18 +60,33 @@ func newClient(apiKey, endpoint, orgId string) *openai.Client {config.OrgID = orgId}- return openai.NewClientWithConfig(config)+ return ClientInfo{+ Client: openai.NewClientWithConfig(config),+ ApiKey: apiKey,+ OrgId: orgId,+ Endpoint: endpoint,+ }+}++type OpenAIPrediction struct {+ Type string `json:"type"`+ Content string `json:"content"`+}++type ExtendedChatCompletionRequest struct {+ *openai.ChatCompletionRequest+ Prediction OpenAIPrediction `json:"prediction,omitempty"`}func CreateChatCompletionStreamWithRetries(- clients map[string]*openai.Client,+ clients map[string]ClientInfo,modelConfig *shared.ModelRoleConfig,ctx context.Context,req openai.ChatCompletionRequest,) (*openai.ChatCompletionStream, error) {- client := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]+ client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]- if client == nil {+ if !ok {return nil, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)}@@ -61,124 +96,98 @@ func CreateChatCompletionStreamWithRetries(log.Println("Message role:", msg.Role)}- return createChatCompletionStream(client, ctx, req, 0)+ return withRetries(ctx, func() (*openai.ChatCompletionStream, error) {+ return client.Client.CreateChatCompletionStream(ctx, req)+ })}-func createChatCompletionStream(- client *openai.Client,+func CreateChatCompletionWithRetries[T openai.ChatCompletionRequest | ExtendedChatCompletionRequest](+ clients map[string]ClientInfo,+ modelConfig *shared.ModelRoleConfig,ctx context.Context,- req openai.ChatCompletionRequest,- numRetry int,-) (*openai.ChatCompletionStream, error) {- if ctx.Err() != nil {- return nil, ctx.Err()- }-- log.Println("Creating chat completion stream")-- stream, err := client.CreateChatCompletionStream(ctx, req)+ req T,+) (openai.ChatCompletionResponse, error) {+ client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]- if err != nil {- log.Printf("Error creating chat completion stream: %v, retry: %d\n", err, numRetry)+ if !ok {+ return openai.ChatCompletionResponse{}, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)+ }+ var baseReq *openai.ChatCompletionRequest+ var fn func() (openai.ChatCompletionResponse, error)- if isNonRetriableErr(err) {- return nil, err+ if normalReq, ok := any(req).(openai.ChatCompletionRequest); ok {+ baseReq = &normalReq+ fn = func() (openai.ChatCompletionResponse, error) {+ return client.Client.CreateChatCompletion(ctx, normalReq)}-- // for retriable errors, retry with exponential backoff- if numRetry < 5 {- // check if the error message contains a retry duration- if duration := parseRetryAfter(err.Error()); duration != nil {- // wait for the duration times 3 to give some buffer- waitDuration := time.Duration(float64(*duration) * 3)-- // ensure wait duration is 60 seconds or less - for really long retries just error out- if waitDuration > 120*time.Second {- return nil, err- } else if waitDuration > 60*time.Second {- waitDuration = 60 * time.Second- }- time.Sleep(waitDuration)- return createChatCompletionStream(client, ctx, req, numRetry+1)- }-- waitBackoff(numRetry)- return createChatCompletionStream(client, ctx, req, numRetry+1)+ } else if extendedReq, ok := any(req).(ExtendedChatCompletionRequest); ok {+ baseReq = extendedReq.ChatCompletionRequest+ fn = func() (openai.ChatCompletionResponse, error) {+ return createChatCompletionExtended(clients, modelConfig, ctx, extendedReq)}-- log.Println("Max retries reached - no retry")- return nil, err+ } else {+ log.Println("Invalid request type")+ log.Println("Request type:", reflect.TypeOf(req))+ return openai.ChatCompletionResponse{}, fmt.Errorf("invalid request type")}- return stream, nil+ resolveReq(baseReq, modelConfig)++ return withRetries(ctx, fn)}-func CreateChatCompletionWithRetries(- clients map[string]*openai.Client,+func createChatCompletionExtended(+ clients map[string]ClientInfo,modelConfig *shared.ModelRoleConfig,ctx context.Context,- req openai.ChatCompletionRequest,+ extendedReq ExtendedChatCompletionRequest,) (openai.ChatCompletionResponse, error) {- client := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]+ client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]- if client == nil {+ if !ok {return openai.ChatCompletionResponse{}, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)}- resolveReq(&req, modelConfig)-- return createChatCompletion(client, ctx, req, 0)-}--func createChatCompletion(- client *openai.Client,- ctx context.Context,- req openai.ChatCompletionRequest,- numRetry int,-) (openai.ChatCompletionResponse, error) {-- if ctx.Err() != nil {- return openai.ChatCompletionResponse{}, ctx.Err()+ req, err := http.NewRequestWithContext(ctx, "POST", modelConfig.BaseModelConfig.BaseUrl+"/chat/completions", nil)+ if err != nil {+ return openai.ChatCompletionResponse{}, err}- resp, err := client.CreateChatCompletion(ctx, req)+ // Add headers+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("Authorization", "Bearer "+client.ApiKey)+ if client.OrgId != "" {+ req.Header.Set("OpenAI-Organization", client.OrgId)+ }+ // Add body+ jsonBody, err := json.Marshal(extendedReq)if err != nil {- log.Printf("Error creating chat completion: %v, retry: %d\n", err, numRetry)-- if isNonRetriableErr(err) {- return openai.ChatCompletionResponse{}, err- }-- // for retriable errors, retry with exponential backoff- if numRetry < 5 {- // check if the error message contains a retry duration- if duration := parseRetryAfter(err.Error()); duration != nil {- log.Printf("Retry duration found: %v\n", *duration)-- // wait for the duration times 3 to give some buffer- waitDuration := time.Duration(float64(*duration) * 3)-- // ensure wait duration is 60 seconds or less - for really long retries just error out- if waitDuration > 120*time.Second {- return openai.ChatCompletionResponse{}, err- } else if waitDuration > 60*time.Second {- waitDuration = 60 * time.Second- }+ return openai.ChatCompletionResponse{}, err+ }+ req.Body = io.NopCloser(bytes.NewReader(jsonBody))- time.Sleep(waitDuration)- return createChatCompletion(client, ctx, req, numRetry+1)- }+ // Make request+ resp, err := httpClient.Do(req)+ if err != nil {+ return openai.ChatCompletionResponse{}, err+ }+ defer resp.Body.Close()- waitBackoff(numRetry)- return createChatCompletion(client, ctx, req, numRetry+1)- }+ // log the response+ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return openai.ChatCompletionResponse{}, err+ }+ log.Println("Response body:", string(body))- log.Println("Max retries reached - no retry")+ var response openai.ChatCompletionResponse+ err = json.Unmarshal(body, &response)+ if err != nil {return openai.ChatCompletionResponse{}, err}- return resp, nil+ return response, nil}func isNonRetriableErr(err error) bool {@@ -258,3 +267,51 @@ func resolveReq(req *openai.ChatCompletionRequest, modelConfig *shared.ModelRolelog.Println("Resolved request")}++func withRetries[T any](+ ctx context.Context,+ operation func() (T, error),+) (T, error) {+ var result T+ var numRetry int++ for {+ if ctx.Err() != nil {+ return result, ctx.Err()+ }++ resp, err := operation()+ if err == nil {+ return resp, nil+ }++ log.Printf("Error in operation: %v, retry: %d\n", err, numRetry)++ if isNonRetriableErr(err) {+ return result, err+ }++ if numRetry >= OPENAI_MAX_RETRIES {+ log.Println("Max retries reached - no retry")+ return result, err+ }++ // Handle retry timing+ if duration := parseRetryAfter(err.Error()); duration != nil {+ log.Printf("Retry duration found: %v\n", *duration)+ waitDuration := time.Duration(float64(*duration) * OPENAI_BACKOFF_MULTIPLIER)++ if waitDuration > OPENAI_ABORT_WAIT_DURATION {+ return result, err+ } else if waitDuration > OPENAI_MAX_WAIT_DURATION {+ waitDuration = OPENAI_MAX_WAIT_DURATION+ }++ time.Sleep(waitDuration)+ } else {+ waitBackoff(numRetry)+ }++ numRetry+++ }+}diff --git a/app/server/model/name.go b/app/server/model/name.goindex dfd12bee..19248d19 100644--- a/app/server/model/name.go+++ b/app/server/model/name.go@@ -18,7 +18,7 @@ func GenPlanName(auth *types.ServerAuth,plan *db.Plan,settings *shared.PlanSettings,- clients map[string]*openai.Client,+ clients map[string]ClientInfo,planContent string,ctx context.Context,) (string, error) {@@ -141,7 +141,7 @@ func GenPipedDataName(auth *types.ServerAuth,plan *db.Plan,settings *shared.PlanSettings,- clients map[string]*openai.Client,+ clients map[string]ClientInfo,pipedContent string,) (string, error) {config := settings.ModelPack.Namer@@ -272,7 +272,7 @@ func GenNoteName(auth *types.ServerAuth,plan *db.Plan,settings *shared.PlanSettings,- clients map[string]*openai.Client,+ clients map[string]ClientInfo,note string,) (string, error) {config := settings.ModelPack.Namerdiff --git a/app/server/model/plan/activate.go b/app/server/model/plan/activate.goindex 92cb6624..909e9f6a 100644--- a/app/server/model/plan/activate.go+++ b/app/server/model/plan/activate.go@@ -5,15 +5,15 @@ import ("log""plandex-server/db""plandex-server/host"+ "plandex-server/model""plandex-server/types""time""github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai")func activatePlan(- clients map[string]*openai.Client,+ clients map[string]model.ClientInfo,plan *db.Plan,branch string,auth *types.ServerAuth,diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.goindex 7c299c84..4d4ffd30 100644--- a/app/server/model/plan/build_exec.go+++ b/app/server/model/plan/build_exec.go@@ -4,15 +4,15 @@ import ("fmt""log""plandex-server/db"+ "plandex-server/model""plandex-server/types""time""github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai")func Build(- clients map[string]*openai.Client,+ clients map[string]model.ClientInfo,plan *db.Plan,branch string,auth *types.ServerAuth,diff --git a/app/server/model/plan/build_state.go b/app/server/model/plan/build_state.goindex f51210dc..1f0d295b 100644--- a/app/server/model/plan/build_state.go+++ b/app/server/model/plan/build_state.go@@ -2,10 +2,10 @@ package planimport ("plandex-server/db"+ "plandex-server/model""plandex-server/types""github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"sitter "github.com/smacker/go-tree-sitter")@@ -13,7 +13,7 @@ const MaxBuildErrorRetries = 3 // uses semi-exponential backoff so be careful witype activeBuildStreamState struct {tellState *activeTellStreamState- clients map[string]*openai.Client+ clients map[string]model.ClientInfoauth *types.ServerAuthcurrentOrgId stringcurrentUserId stringdiff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.goindex f5763747..9acc2742 100644--- a/app/server/model/plan/build_structured_edits.go+++ b/app/server/model/plan/build_structured_edits.go@@ -65,8 +65,10 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {fileState.language,activePlan.Ctx,)- // log.Println("buildStructuredEdits - applyRes.NewFile:")- // log.Println(applyRes.NewFile)+ log.Println("buildStructuredEdits - applyRes.NewFile:")+ log.Println(applyRes.NewFile)+ log.Println("buildStructuredEdits - applyRes.NeedsVerifyReasons:")+ log.Println(applyRes.NeedsVerifyReasons)updatedFile := applyRes.NewFile@@ -91,7 +93,7 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {log.Printf("buildStructuredEdits - %s - initial isValid: %t\n", filePath, isValid)- var buildStage BuildStage = BuildStageInitial+ var buildStage BuildStage = BuildStageValidateAndCorrectfor !isValid && int(buildStage) <= int(BuildStageValidateAndCorrect) {buildStage = BuildStage(int(buildStage) + 1)diff --git a/app/server/model/plan/build_whole_file.go b/app/server/model/plan/build_whole_file.goindex 9d5de69e..ba09795b 100644--- a/app/server/model/plan/build_whole_file.go+++ b/app/server/model/plan/build_whole_file.go@@ -61,14 +61,38 @@ func (fileState *activeBuildStreamFileState) buildWholeFileFallback(proposedContlog.Println("buildWholeFile - calling model for applied changes validation")- modelReq := openai.ChatCompletionRequest{+ var resp openai.ChatCompletionResponse+ var err error++ modelReq := &openai.ChatCompletionRequest{Model: config.BaseModelConfig.ModelName,Messages: messages,Temperature: config.Temperature,TopP: config.TopP,}- resp, err := model.CreateChatCompletionWithRetries(clients, &config, activePlan.Ctx, modelReq)+ log.Println("buildWholeFile - config.BaseModelConfig.PredictedOutputEnabled:", config.BaseModelConfig.PredictedOutputEnabled)++ if config.BaseModelConfig.PredictedOutputEnabled {+ extendedReq := &model.ExtendedChatCompletionRequest{+ ChatCompletionRequest: modelReq,+ Prediction: model.OpenAIPrediction{+ Type: "content",+ Content: `+## Comments++No comments++<PlandexWholeFile>+` + originalFile + `+</PlandexWholeFile>+`,+ },+ }+ resp, err = model.CreateChatCompletionWithRetries(clients, &config, activePlan.Ctx, *extendedReq)+ } else {+ resp, err = model.CreateChatCompletionWithRetries(clients, &config, activePlan.Ctx, *modelReq)+ }if err != nil {log.Printf("buildWholeFile - error calling model: %v\n", err)diff --git a/app/server/model/plan/commit_msg.go b/app/server/model/plan/commit_msg.goindex 3e238043..73d27b03 100644--- a/app/server/model/plan/commit_msg.go+++ b/app/server/model/plan/commit_msg.go@@ -160,7 +160,7 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri}, nil}-func GenCommitMsgForPendingResults(auth *types.ServerAuth, plan *db.Plan, clients map[string]*openai.Client, settings *shared.PlanSettings, current *shared.CurrentPlanState, ctx context.Context) (string, error) {+func GenCommitMsgForPendingResults(auth *types.ServerAuth, plan *db.Plan, clients map[string]model.ClientInfo, settings *shared.PlanSettings, current *shared.CurrentPlanState, ctx context.Context) (string, error) {config := settings.ModelPack.CommitMsgs := ""diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex 391128e0..62bb41c5 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -18,7 +18,7 @@ import ("github.com/sashabaranov/go-openai")-func Tell(clients map[string]*openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {+func Tell(clients map[string]model.ClientInfo, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {log.Printf("Tell: Called with plan ID %s on branch %s\n", plan.Id, branch)_, err := activatePlan(@@ -51,7 +51,7 @@ func Tell(clients map[string]*openai.Client, plan *db.Plan, branch string, auth}type execTellPlanParams struct {- clients map[string]*openai.Client+ clients map[string]model.ClientInfoplan *db.Planbranch stringauth *types.ServerAuthdiff --git a/app/server/model/plan/tell_state.go b/app/server/model/plan/tell_state.goindex d859d84d..f27a8d73 100644--- a/app/server/model/plan/tell_state.go+++ b/app/server/model/plan/tell_state.go@@ -2,6 +2,7 @@ package planimport ("plandex-server/db"+ "plandex-server/model""plandex-server/types""github.com/plandex/plandex/shared"@@ -10,7 +11,7 @@ import (type activeTellStreamState struct {activePlan *types.ActivePlan- clients map[string]*openai.Client+ clients map[string]model.ClientInforeq *shared.TellPlanRequestauth *types.ServerAuthcurrentOrgId stringdiff --git a/app/server/model/plan/tell_stream_error.go b/app/server/model/plan/tell_stream_error.goindex b13451de..d45e06ba 100644--- a/app/server/model/plan/tell_stream_error.go+++ b/app/server/model/plan/tell_stream_error.go@@ -63,7 +63,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, constoredDesc := falseif convoMessageId == "" {- assistantMsg, msg, err := state.storeAssistantReply()+ assistantMsg, msg, err := state.storeAssistantReply("")if err == nil {convoMessageId = assistantMsg.IdcommitMsg = msgdiff --git a/app/server/model/plan/tell_stream_finish.go b/app/server/model/plan/tell_stream_finish.goindex 3a8033ba..b60377ca 100644--- a/app/server/model/plan/tell_stream_finish.go+++ b/app/server/model/plan/tell_stream_finish.go@@ -435,7 +435,20 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParams}}()- assistantMsg, convoCommitMsg, err := state.storeAssistantReply() // updates state.convo+ var replyType shared.ReplyType+ if len(replyOperations) > 0 {+ replyType = shared.ReplyTypeImplementation+ } else if hasNewSubtasks {+ replyType = shared.ReplyTypeMadePlan+ } else if state.isContextStage {+ replyType = shared.ReplyTypeLoadedContext+ } else if followUpNeedsContextStage {+ replyType = shared.ReplyTypeContextAssessment+ } else {+ replyType = shared.ReplyTypeChat+ }++ assistantMsg, convoCommitMsg, err := state.storeAssistantReply(replyType) // updates state.convoif err != nil {state.onError(fmt.Errorf("failed to store assistant message: %v", err), true, "", "")@@ -547,7 +560,7 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParams}}-func (state *activeTellStreamState) storeAssistantReply() (*db.ConvoMessage, string, error) {+func (state *activeTellStreamState) storeAssistantReply(replyType shared.ReplyType) (*db.ConvoMessage, string, error) {currentOrgId := state.currentOrgIdcurrentUserId := state.currentUserIdplanId := state.plan.Id@@ -564,14 +577,15 @@ func (state *activeTellStreamState) storeAssistantReply() (*db.ConvoMessage, stractivePlan := state.activePlanassistantMsg := db.ConvoMessage{- Id: replyId,- OrgId: currentOrgId,- PlanId: planId,- UserId: currentUserId,- Role: openai.ChatMessageRoleAssistant,- Tokens: replyNumTokens,- Num: num,- Message: activePlan.CurrentReplyContent,+ Id: replyId,+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleAssistant,+ Tokens: replyNumTokens,+ Num: num,+ Message: activePlan.CurrentReplyContent,+ ReplyType: replyType,}commitMsg, err := db.StoreConvoMessage(&assistantMsg, auth.User.Id, branch, false)diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.goindex 6f0cff4c..dc2a3d55 100644--- a/app/server/model/plan/tell_summary.go+++ b/app/server/model/plan/tell_summary.go@@ -197,7 +197,7 @@ type summarizeConvoParams struct {modelPackName string}-func summarizeConvo(clients map[string]*openai.Client, config shared.ModelRoleConfig, params summarizeConvoParams, ctx context.Context) error {+func summarizeConvo(clients map[string]model.ClientInfo, config shared.ModelRoleConfig, params summarizeConvoParams, ctx context.Context) error {plan := params.planplanId := plan.Idlog.Printf("summarizeConvo: Called for plan ID %s on branch %s\n", planId, params.branch)diff --git a/app/server/model/prompts/context_load.go b/app/server/model/prompts/context_load.goindex 1987d1a3..7918090c 100644--- a/app/server/model/prompts/context_load.go+++ b/app/server/model/prompts/context_load.go@@ -5,13 +5,13 @@ const AutoContextPreamble = `You are operating in 'auto-context mode'. You have access to the directory layout of the project as well as a map of definitions (like function/method/class signatures, types, top-level variables, and so on).-In response to the user's latest prompt, do the following:+In response to the user's latest prompt, do the following IN ORDER:- Decide whether you've been given enough information to load necessary context and make a plan (if you've been given a task) or give a helpful response to the user (if you're responding in chat form). In general, do your best with whatever information you've been provided. Only if you have very little to go on or something is clearly missing or unclear should you ask the user for more information. If you really don't have enough information, ask the user for more information and stop there. 'Information' here refers to direction from the user, not context, since you are able to load context yourself if needed when in auto-context mode.- - Reply with a brief, high level overview of how you will approach implementing the task (if you've been given a task) or responding to the user (if you're responding in chat form). Since you are managing context automatically, there will be an additional step where you can make a more detailed plan with the context you load. Do not state that you are creating a final or comprehensive planβthat is not the purpose of this response. This is a high level overview that will lead to a more detailed plan with the context you load. Do not call this overview a 'plan'βthe purpose is only to help you exmaine the codebase to determine what context to load. You will then make a plan in the next step.+ - Reply with a brief, high level overview of how you will approach implementing the task (if you've been given a task) or responding to the user (if you're responding in chat form). Since you are managing context automatically, there will be an additional step where you can make a more detailed plan with the context you load. Do not state that you are creating a final or comprehensive planβthat is not the purpose of this response. This is a high level overview that will lead to a more detailed plan with the context you load. Do not call this overview a 'plan'βthe purpose is only to help you examine the codebase to determine what context to load. You will then make a plan in the next step.- - If you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below.+ - If you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below. NEVER say "No context needs to be loaded." *after* you've already output the '### Context Categories' and '### Files' sections.- In a section titled '### Context Categories', list one or more categories of context that are relevant to the user's task, question, or message. For example, if the user is asking you to implement an API endpoint, you might list 'API endpoints', 'database operations', 'frontend code', 'utilities', and so on. Make sure any and all relevant categories are included, but don't include more categories than necessaryβif only a single category is relevant, then only list that one. Do not include file paths, symbols, or explanationsβonly the categories.diff --git a/app/server/model/summarize.go b/app/server/model/summarize.goindex 93197065..9ca09e6c 100644--- a/app/server/model/summarize.go+++ b/app/server/model/summarize.go@@ -28,7 +28,7 @@ type PlanSummaryParams struct {NumMessages int}-func PlanSummary(clients map[string]*openai.Client, config shared.ModelRoleConfig, params PlanSummaryParams, ctx context.Context) (*db.ConvoSummary, error) {+func PlanSummary(clients map[string]ClientInfo, config shared.ModelRoleConfig, params PlanSummaryParams, ctx context.Context) (*db.ConvoSummary, error) {messages := []openai.ChatCompletionMessage{{Role: openai.ChatMessageRoleSystem,diff --git a/app/server/syntax/validate.go b/app/server/syntax/validate.goindex cfa2e4fa..8da1b020 100644--- a/app/server/syntax/validate.go+++ b/app/server/syntax/validate.go@@ -35,6 +35,9 @@ func ValidateFile(ctx context.Context, path string, file string) (*ValidationRes}func ValidateWithParsers(ctx context.Context, lang shared.TreeSitterLanguage, parser *tree_sitter.Parser, fallbackLang shared.TreeSitterLanguage, fallbackParser *tree_sitter.Parser, file string) (*ValidationRes, error) {+ if file == "" {+ return &ValidationRes{Lang: lang, Parser: parser, Valid: true}, nil+ }// Set a timeout duration for the parsing operationsctx, cancel := context.WithTimeout(ctx, parserTimeout)@@ -90,6 +93,10 @@ func ValidateWithParsers(ctx context.Context, lang shared.TreeSitterLanguage, pa}func insertErrorMarkers(source string, node *tree_sitter.Node) []string {+ if source == "" {+ return []string{}+ }+var markers []stringvar uniqueMarkers = map[string]bool{}diff --git a/app/shared/ai_models.go b/app/shared/ai_models.goindex 8ba46af2..46d20045 100644--- a/app/shared/ai_models.go+++ b/app/shared/ai_models.go@@ -24,6 +24,7 @@ var AvailableModels = []*AvailableModel{ModelCompatibility: fullCompatibility,BaseUrl: OpenAIV1BaseUrl,PreferredModelOutputFormat: ModelOutputFormatToolCallJson,+ PredictedOutputEnabled: true,},},{@@ -38,6 +39,7 @@ var AvailableModels = []*AvailableModel{ModelCompatibility: fullCompatibility,BaseUrl: OpenAIV1BaseUrl,PreferredModelOutputFormat: ModelOutputFormatToolCallJson,+ PredictedOutputEnabled: true,},},{diff --git a/app/shared/data_models.go b/app/shared/data_models.goindex d8268b35..9fd7f4d6 100644--- a/app/shared/data_models.go+++ b/app/shared/data_models.go@@ -117,6 +117,16 @@ type Context struct {UpdatedAt time.Time `json:"updatedAt"`}+type ReplyType string++const (+ ReplyTypeContextAssessment ReplyType = "context-assessment"+ ReplyTypeMadePlan ReplyType = "made-plan"+ ReplyTypeLoadedContext ReplyType = "loaded-context"+ ReplyTypeChat ReplyType = "chat"+ ReplyTypeImplementation ReplyType = "implementation"+)+type ConvoMessage struct {Id string `json:"id"`UserId string `json:"userId"`@@ -125,6 +135,7 @@ type ConvoMessage struct {Num int `json:"num"`Message string `json:"message"`Stopped bool `json:"stopped"`+ ReplyType ReplyType `json:"replyType"`CreatedAt time.Time `json:"createdAt"`}@@ -280,8 +291,9 @@ type BaseModelConfig struct {ApiKeyEnvVar string `json:"apiKeyEnvVar"`PreferredModelOutputFormat ModelOutputFormat `json:"preferredModelOutputFormat"`- SystemPromptDisabled bool `json:"systemPromptDisabled"`- RoleParamsDisabled bool `json:"roleParamsDisabled"`+ SystemPromptDisabled bool `json:"systemPromptDisabled"`+ RoleParamsDisabled bool `json:"roleParamsDisabled"`+ PredictedOutputEnabled bool `json:"predictedOutputEnabled"`ModelCompatibility}diff --git a/app/shared/plan_model_settings.go b/app/shared/plan_model_settings.goindex 3b3993c6..e4afbbfe 100644--- a/app/shared/plan_model_settings.go+++ b/app/shared/plan_model_settings.go@@ -74,6 +74,18 @@ func (ps PlanSettings) GetPlannerMaxTokens() int {}}+func (ps PlanSettings) GetContextLoaderMaxTokens() int {+ if ps.ModelOverrides.MaxTokens == nil {+ if ps.ModelPack == nil {+ return DefaultModelPack.GetContextLoader().BaseModelConfig.MaxTokens+ } else {+ return ps.ModelPack.GetContextLoader().BaseModelConfig.MaxTokens+ }+ } else {+ return *ps.ModelOverrides.MaxTokens+ }+}+func (ps PlanSettings) GetPlannerMaxConvoTokens() int {if ps.ModelOverrides.MaxConvoTokens == nil {if ps.ModelPack == nil {@@ -90,6 +102,11 @@ func (ps PlanSettings) GetPlannerEffectiveMaxTokens() int {return ps.GetPlannerMaxTokens() - ps.ModelPack.Planner.GetReservedOutputTokens()}+func (ps PlanSettings) GetContextLoaderEffectiveMaxTokens() int {+ config := ps.ModelPack.GetContextLoader()+ return ps.GetContextLoaderMaxTokens() - config.GetReservedOutputTokens()+}+func (ps PlanSettings) GetRequiredEnvVars() map[string]bool {envVars := map[string]bool{}