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
mega-commit for 0.9.0 release, including lots of work that should have been split up into smaller commits.
Dane Schneider3a6f38eb8ab2
diff --git a/README.md b/README.mdindex 155d8ef4..b2cf6def 100644--- a/README.md+++ b/README.md@@ -72,7 +72,7 @@📑 Manage context efficiently in the terminal. Easily add files or entire directories to context, and keep them updated automatically as you work so that models always have the latest state of your project.-🧠 Plandex relies on the OpenAI API and requires an `OPENAI_API_KEY` environment variable. Support for open source models, Google Gemini, and Anthropic Claude is coming soon. Use the `OPENAI_ENDPOINT` environment variable for a custom OpenAI endpoint.+🧠 By default, Plandex relies on the OpenAI API and requires an `OPENAI_API_KEY` environment variable. You can also use it with a wide range of other models, including Anthropic Claude, Google Gemini, Mixtral, and many more via OpenRouter.ai, Together.ai, or any other OpenAI-compatible provider.✅ Plandex supports Mac, Linux, FreeBSD, and Windows. It runs from a single binary with no dependencies.@@ -115,15 +115,21 @@ Windows is supported via <a href="https://learn.microsoft.com/en-us/windows/wsl/## Get started 🚀-If you don't have an OpenAI account, first [sign up here.](https://platform.openai.com/signup)+Plandex uses OpenAI by default. If you don't have an OpenAI account, first [sign up here.](https://platform.openai.com/signup)Then [generate an API key here.](https://platform.openai.com/account/api-keys)```bashcd your-project+export OPENAI_API_KEY=...-export OPENAI_ENDPOINT=... # optional e.g. https://<your-proxy>/v1+export OPENAI_API_BASE=... # optional e.g. https://<your-proxy>/v1export OPENAI_ORG_ID=... # optional - set the OrgID if you have multiple OpenAI orgs++# optional - set api keys for any other providers you're using+export OPENROUTER_API_KEY=...+export TOGETHER_API_KEY...+plandex new```@@ -190,9 +196,9 @@ Plandex Cloud follows best practices for network and data security. And whether## Roadmap 🗺️-🧠 Support for open source models, Google Gemini, and Anthropic Claude in addition to OpenAI<br>+🧠 Support for open source models, Google Gemini, and Anthropic Claude in addition to OpenAI ✅ - released April 2024<br>🤝 Plan sharing and team collaboration<br>-🖼️ Support for GPT4-Vision and other multi-modal models—add images and screenshots to context<br>+🖼️ Support for multi-modal models—add images and screenshots to context<br>🖥️ VSCode and JetBrains extensions<br>📦 Community plugins and modules<br>🔌 Github integration<br>@@ -210,6 +216,12 @@ Speaking of feedback, feel free to give yours, ask questions, report a bug, or j- [Discussions](https://github.com/plandex-ai/plandex/discussions)- [Issues](https://github.com/plandex-ai/plandex/issues)+## Twitter / X / whatever it's called these days 🐦++Follow [@PlandexAI](https://twitter.com/PlandexAI) for updates and AI coding content.++Follow [@Danenania](https://twitter.com/Danenania), the creator of Plandex, for more wide-ranging dev stuff.+## Contributors 👥⭐️ Please star, fork, explore, and contribute to Plandex. There's a lot of work to do and so much that can be improved.diff --git a/app/cli/api/methods.go b/app/cli/api/methods.goindex f62781d4..785d8e10 100644--- a/app/cli/api/methods.go+++ b/app/cli/api/methods.go@@ -16,8 +16,6 @@ import (func (a *Api) StartTrial() (*shared.StartTrialResponse, *shared.ApiError) {serverUrl := cloudApiHost + "/accounts/start_trial"- log.Println("Sending request to", serverUrl)-resp, err := unauthenticatedClient.Post(serverUrl, "application/json", nil)if err != nil {return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}@@ -653,9 +651,49 @@ func (a *Api) GetCurrentPlanState(planId, branch string) (*shared.CurrentPlanStareturn &state, nil}-func (a *Api) ApplyPlan(planId, branch string) *shared.ApiError {+func (a *Api) ApplyPlan(planId, branch string, req shared.ApplyPlanRequest) (string, *shared.ApiError) {serverUrl := fmt.Sprintf("%s/plans/%s/%s/apply", getApiHost(), planId, branch)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return "", &shared.ApiError{Msg: fmt.Sprintf("error marshalling request: %v", err)}+ }++ request, err := http.NewRequest(http.MethodPatch, serverUrl, bytes.NewBuffer(reqBytes))+ if err != nil {+ return "", &shared.ApiError{Msg: fmt.Sprintf("error creating request: %v", err)}+ }+ request.Header.Set("Content-Type", "application/json")++ resp, err := authenticatedFastClient.Do(request)+ if err != nil {+ return "", &shared.ApiError{Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ apiErr := handleApiError(resp, errorBody)++ didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ if didRefresh {+ return a.ApplyPlan(planId, branch, req)+ }+ return "", apiErr+ }++ // Reading the body on success+ responseData, err := io.ReadAll(resp.Body)+ if err != nil {+ return "", &shared.ApiError{Msg: fmt.Sprintf("error reading response body: %v", err)}+ }++ return string(responseData), nil+}++func (a *Api) ArchivePlan(planId string) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/plans/%s/archive", getApiHost(), planId)+req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)if err != nil {return &shared.ApiError{Msg: fmt.Sprintf("error creating request: %v", err)}@@ -673,7 +711,7 @@ func (a *Api) ApplyPlan(planId, branch string) *shared.ApiError {didRefresh, apiErr := refreshTokenIfNeeded(apiErr)if didRefresh {- return a.ApplyPlan(planId, branch)+ return a.ArchivePlan(planId)}return apiErr}@@ -681,8 +719,8 @@ func (a *Api) ApplyPlan(planId, branch string) *shared.ApiError {return nil}-func (a *Api) ArchivePlan(planId string) *shared.ApiError {- serverUrl := fmt.Sprintf("%s/plans/%s/archive", getApiHost(), planId)+func (a *Api) UnarchivePlan(planId string) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/plans/%s/unarchive", getApiHost(), planId)req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)if err != nil {@@ -709,6 +747,45 @@ func (a *Api) ArchivePlan(planId string) *shared.ApiError {return nil}+func (a *Api) RenamePlan(planId string, name string) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/plans/%s/rename", getApiHost(), planId)++ reqBytes, err := json.Marshal(shared.RenamePlanRequest{Name: name})+ if err != nil {+ return &shared.ApiError{Msg: fmt.Sprintf("error marshalling request: %v", err)}+ }++ request, err := http.NewRequest(http.MethodPatch, serverUrl, bytes.NewBuffer(reqBytes))++ if err != nil {+ return &shared.ApiError{Msg: fmt.Sprintf("error creating request: %v", err)}+ }++ request.Header.Set("Content-Type", "application/json")++ resp, err := authenticatedFastClient.Do(request)++ if err != nil {+ return &shared.ApiError{Msg: fmt.Sprintf("error sending request: %v", err)}+ }++ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)++ didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ if didRefresh {+ return a.RenamePlan(planId, name)+ }+ return apiErr+ }++ return nil+}+func (a *Api) RejectAllChanges(planId, branch string) *shared.ApiError {serverUrl := fmt.Sprintf("%s/plans/%s/%s/reject_all", getApiHost(), planId, branch)@@ -940,6 +1017,62 @@ func (a *Api) ListConvo(planId, branch string) ([]*shared.ConvoMessage, *shared.return convos, nil}+func (a *Api) GetPlanStatus(planId, branch string) (string, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/plans/%s/%s/status", getApiHost(), planId, branch)++ resp, err := authenticatedFastClient.Get(serverUrl)+ if err != nil {+ return "", &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.GetPlanStatus(planId, branch)+ }+ return "", apiErr+ }++ body, err := io.ReadAll(resp.Body)++ if err != nil {+ return "", &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error reading response body: %v", err)}+ }++ return string(body), nil+}++func (a *Api) GetPlanDiffs(planId, branch string) (string, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/plans/%s/%s/diffs", getApiHost(), planId, branch)++ resp, err := authenticatedFastClient.Get(serverUrl)+ if err != nil {+ return "", &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.GetPlanDiffs(planId, branch)+ }+ return "", apiErr+ }++ body, err := io.ReadAll(resp.Body)++ if err != nil {+ return "", &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error reading response body: %v", err)}+ }++ return string(body), nil+}+func (a *Api) ListLogs(planId, branch string) (*shared.LogResponse, *shared.ApiError) {serverUrl := fmt.Sprintf("%s/plans/%s/%s/logs", getApiHost(), planId, branch)@@ -1602,59 +1735,243 @@ func (a *Api) UpdateSettings(planId, branch string, req shared.UpdateSettingsReqreturn &updateRes, nil}-func (a *Api) CreateCustomModel(model shared.CustomModel) *shared.ApiError {- serverUrl := a.baseUrl + "/custom_models"++func (a *Api) GetOrgDefaultSettings() (*shared.PlanSettings, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/default_settings", getApiHost())++ resp, err := authenticatedFastClient.Get(serverUrl)++ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %s", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.GetOrgDefaultSettings()+ }+ return nil, apiErr+ }++ var settings shared.PlanSettings+ err = json.NewDecoder(resp.Body).Decode(&settings)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %s", err)}+ }++ return &settings, nil+}++func (a *Api) UpdateOrgDefaultSettings(req shared.UpdateSettingsRequest) (*shared.UpdateSettingsResponse, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/default_settings", getApiHost())++ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error marshalling request: %s", err)}+ }++ // log.Println("UpdateSettings", string(reqBytes))++ request, err := http.NewRequest(http.MethodPut, serverUrl, bytes.NewBuffer(reqBytes))+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error creating request: %s", err)}+ }+ request.Header.Set("Content-Type", "application/json")++ resp, err := authenticatedFastClient.Do(request)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %s", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.UpdateOrgDefaultSettings(req)+ }+ return nil, apiErr+ }++ var updateRes shared.UpdateSettingsResponse+ err = json.NewDecoder(resp.Body).Decode(&updateRes)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %s", err)}+ }++ return &updateRes, nil++}++func (a *Api) CreateCustomModel(model *shared.AvailableModel) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/custom_models", getApiHost())body, err := json.Marshal(model)if err != nil {return &shared.ApiError{Msg: "Failed to marshal model"}}- resp, err := a.client.Post(serverUrl, "application/json", bytes.NewBuffer(body))+ resp, err := authenticatedFastClient.Post(serverUrl, "application/json", bytes.NewBuffer(body))if err != nil {- return &shared.ApiError{Msg: "Failed to create custom model"}+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}}defer resp.Body.Close()- if resp.StatusCode != 200 {- return &shared.ApiError{Msg: "Failed to create custom model"}+ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.CreateCustomModel(model)+ }+ return apiErr}return nil}-func (a *Api) ListCustomModels() ([]shared.CustomModel, *shared.ApiError) {- serverUrl := a.baseUrl + "/custom_models"- resp, err := a.client.Get(serverUrl)+func (a *Api) ListCustomModels() ([]*shared.AvailableModel, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/custom_models", getApiHost())+ resp, err := authenticatedFastClient.Get(serverUrl)if err != nil {- return nil, &shared.ApiError{Msg: "Failed to list custom models"}+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}}defer resp.Body.Close()- var models []shared.CustomModel- if err := json.NewDecoder(resp.Body).Decode(&models); err != nil {- return nil, &shared.ApiError{Msg: "Failed to decode custom models"}+ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.ListCustomModels()+ }+ return nil, apiErr+ }++ var models []*shared.AvailableModel+ err = json.NewDecoder(resp.Body).Decode(&models)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %v", err)}}return models, nil}-func (a *Api) DeleteCustomModel(modelId string) *shared.ApiError {- serverUrl := a.baseUrl + "/custom_models/" + modelId- req, err := http.NewRequest("DELETE", serverUrl, nil)+func (a *Api) DeleteAvailableModel(modelId string) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/custom_models/%s", getApiHost(), modelId)+ req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)if err != nil {- return &shared.ApiError{Msg: "Failed to create request"}+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error creating request: %v", err)}}- resp, err := a.client.Do(req)+ resp, err := authenticatedFastClient.Do(req)if err != nil {- return &shared.ApiError{Msg: "Failed to delete custom model"}+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}}defer resp.Body.Close()- if resp.StatusCode != 200 {- return &shared.ApiError{Msg: "Failed to delete custom model"}+ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.DeleteAvailableModel(modelId)+ }+ return apiErr}return nil}+func (a *Api) CreateModelPack(set *shared.ModelPack) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/model_sets", getApiHost())+ body, err := json.Marshal(set)+ if err != nil {+ return &shared.ApiError{Msg: "Failed to marshal model pack"}+ }++ resp, err := authenticatedFastClient.Post(serverUrl, "application/json", bytes.NewBuffer(body))+ if err != nil {+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.CreateModelPack(set)+ }+ return apiErr+ }++ return nil++}++func (a *Api) ListModelPacks() ([]*shared.ModelPack, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/model_sets", getApiHost())++ resp, err := authenticatedFastClient.Get(serverUrl)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.ListModelPacks()+ }+ return nil, apiErr+ }++ var sets []*shared.ModelPack+ err = json.NewDecoder(resp.Body).Decode(&sets)+ if err != nil {+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %v", err)}+ }++ return sets, nil++}++func (a *Api) DeleteModelPack(setId string) *shared.ApiError {+ serverUrl := fmt.Sprintf("%s/model_sets/%s", getApiHost(), setId)++ req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)+ if err != nil {+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error creating request: %v", err)}+ }++ resp, err := authenticatedFastClient.Do(req)+ if err != nil {+ return &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)++ apiErr := handleApiError(resp, errorBody)+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)+ if tokenRefreshed {+ return a.DeleteModelPack(setId)+ }+ return apiErr+ }++ return nil+}diff --git a/app/cli/changes_tui/replace.go b/app/cli/changes_tui/replace.goindex 38cbab01..f62036c6 100644--- a/app/cli/changes_tui/replace.go+++ b/app/cli/changes_tui/replace.go@@ -6,6 +6,7 @@ import ("github.com/fatih/color""github.com/muesli/reflow/wrap"+ "github.com/plandex/plandex/shared")const replacementPrependLines = 20@@ -32,14 +33,22 @@ func (m changesUIModel) getReplacementOldDisplay() oldReplacementRes {// log.Printf("oldContent: %v", oldContent)// log.Printf("originalFile: %v", originalFile)- fileIdx := strings.Index(originalFile, oldContent)- if fileIdx == -1 {- panic("old content not found in full file") // should never happen+ if m.selectionInfo.currentRes.ReplaceWithLineNums {+ originalFile = shared.AddLineNums(originalFile)}// Convert originalFile to a slice of runes to properly handle multi-byte charactersoriginalFileRunes := []rune(originalFile)+ // Convert oldContent to a slice of runes+ oldContentRunes := []rune(oldContent)++ // Find the index of oldContentRunes in originalFileRunes+ fileIdx := shared.IndexRunes(originalFileRunes, oldContentRunes)+ if fileIdx == -1 {+ term.OutputErrorAndExit("Could not find replacement in original file")+ }+toPrepend := ""numLinesPrepended := 0for i := fileIdx - 1; i >= 0; i-- {@@ -61,6 +70,8 @@ func (m changesUIModel) getReplacementOldDisplay() oldReplacementRes {toPrepend = "…\n" + toPrepend}+ toPrepend = shared.RemoveLineNums(toPrepend)+toAppend := ""numLinesAppended := 0// Convert originalFile to a slice of runes to properly handle multi-byte characters@@ -85,9 +96,11 @@ func (m changesUIModel) getReplacementOldDisplay() oldReplacementRes {if !appendedToEnd {toAppend += "\n…"}+ toAppend = shared.RemoveLineNums(toAppend)wrapWidth := m.changeOldViewport.Width - 6toPrepend = wrap.String(toPrepend, wrapWidth)+ oldContent = shared.RemoveLineNums(oldContent)oldContent = wrap.String(oldContent, wrapWidth)// log.Println("toAppend", toAppend)diff --git a/app/cli/cmd/archive.go b/app/cli/cmd/archive.goindex 71ee8da1..fcdd5f73 100644--- a/app/cli/cmd/archive.go+++ b/app/cli/cmd/archive.go@@ -2,7 +2,6 @@ package cmdimport ("fmt"- "os""plandex/api""plandex/auth""plandex/lib"@@ -11,12 +10,13 @@ import ("strings""github.com/fatih/color"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")var archiveCmd = &cobra.Command{Use: "archive [name-or-index]",- Aliases: []string{"ar"},+ Aliases: []string{"arc"},Short: "Archive a plan",Args: cobra.MaximumNArgs(1),Run: archive,@@ -37,7 +37,7 @@ func archive(cmd *cobra.Command, args []string) {var plan *shared.Plan- term.StartSpinner("Loading plans")+ term.StartSpinner("")plans, apiErr := api.Client.ListPlans([]string{lib.CurrentProjectId})term.StopSpinner()@@ -90,5 +90,9 @@ func archive(cmd *cobra.Command, args []string) {term.OutputErrorAndExit("Error archiving plan: %v", err)}- fmt.Printf("✅ Plan %s archived successfully\n", color.New(color.Bold).Sprint(plan.Name))+ fmt.Printf("✅ Plan %s archived\n", color.New(color.Bold, term.ColorHiYellow).Sprint(plan.Name))++ fmt.Println()++ term.PrintCmds("", "plans --archived", "unarchive")}diff --git a/app/cli/cmd/build.go b/app/cli/cmd/build.goindex 1ed94d8d..01fd78bb 100644--- a/app/cli/cmd/build.go+++ b/app/cli/cmd/build.go@@ -2,7 +2,6 @@ package cmdimport ("fmt"- "os""plandex/auth""plandex/lib""plandex/plan_exec"@@ -29,10 +28,6 @@ func init() {}func build(cmd *cobra.Command, args []string) {- if os.Getenv("OPENAI_API_KEY") == "" {- term.OutputNoApiKeyMsgAndExit()- }-auth.MustResolveAuthWithOrg()lib.MustResolveProject()@@ -41,9 +36,12 @@ func build(cmd *cobra.Command, args []string) {return}+ apiKeys := lib.MustVerifyApiKeys()+didBuild, err := plan_exec.Build(plan_exec.ExecParams{CurrentPlanId: lib.CurrentPlanId,CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool) {return lib.MustCheckOutdatedContext(false, maybeContexts)},diff --git a/app/cli/cmd/changes.go b/app/cli/cmd/changes.goindex 07da2662..53e7f60e 100644--- a/app/cli/cmd/changes.go+++ b/app/cli/cmd/changes.go@@ -75,9 +75,12 @@ func changes(cmd *cobra.Command, args []string) {if !viewIncomplete {fmt.Println("This plan has unbuilt changes. Building now.")+ apiKeys := lib.MustVerifyApiKeys()+didBuild, err := plan_exec.Build(plan_exec.ExecParams{CurrentPlanId: lib.CurrentPlanId,CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool) {return lib.MustCheckOutdatedContext(true, maybeContexts)},diff --git a/app/cli/cmd/connect.go b/app/cli/cmd/connect.goindex 2f14af1f..495fd041 100644--- a/app/cli/cmd/connect.go+++ b/app/cli/cmd/connect.go@@ -58,7 +58,7 @@ func connect(cmd *cobra.Command, args []string) {}fmt.Println()- term.PrintCmds("", "changes", "apply", "log")+ term.PrintCmds("", "changes", "diff", "apply", "log")os.Exit(0)}()diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.goindex efd5b5d2..dad0b8cf 100644--- a/app/cli/cmd/continue.go+++ b/app/cli/cmd/continue.go@@ -2,11 +2,9 @@ package cmdimport ("fmt"- "os""plandex/auth""plandex/lib""plandex/plan_exec"- "plandex/term""github.com/plandex/plandex/shared""github.com/spf13/cobra"@@ -28,10 +26,6 @@ func init() {}func doContinue(cmd *cobra.Command, args []string) {- if os.Getenv("OPENAI_API_KEY") == "" {- term.OutputNoApiKeyMsgAndExit()- }-auth.MustResolveAuthWithOrg()lib.MustResolveProject()@@ -40,9 +34,12 @@ func doContinue(cmd *cobra.Command, args []string) {return}+ apiKeys := lib.MustVerifyApiKeys()+plan_exec.TellPlan(plan_exec.ExecParams{CurrentPlanId: lib.CurrentPlanId,CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool) {return lib.MustCheckOutdatedContext(false, maybeContexts)},diff --git a/app/cli/cmd/delete_plan.go b/app/cli/cmd/delete_plan.goindex 734c81c5..06111bfe 100644--- a/app/cli/cmd/delete_plan.go+++ b/app/cli/cmd/delete_plan.go@@ -72,7 +72,7 @@ func del(cmd *cobra.Command, args []string) {opts[i] = plan.Name}- selected, err := term.SelectFromList("Select a plan", opts)+ selected, err := term.SelectFromList("Select a plan:", opts)if err != nil {term.OutputErrorAndExit("Error selecting plan: %v", err)diff --git a/app/cli/cmd/diffs.go b/app/cli/cmd/diffs.gonew file mode 100644index 00000000..c07acb12--- /dev/null+++ b/app/cli/cmd/diffs.go@@ -0,0 +1,41 @@+package cmd++import (+ "fmt"+ "plandex/api"+ "plandex/auth"+ "plandex/lib"+ "plandex/term"++ "github.com/spf13/cobra"+)++var diffsCmd = &cobra.Command{+ Use: "diff",+ Aliases: []string{"diffs"},+ Short: "Show diffs for the pending changes in git diff format",+ Run: diffs,+}++func init() {+ RootCmd.AddCommand(diffsCmd)+}++func diffs(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()+ lib.MaybeResolveProject()++ if lib.CurrentPlanId == "" {+ fmt.Println("🤷♂️ No current plan")+ return+ }++ diffs, err := api.Client.GetPlanDiffs(lib.CurrentPlanId, lib.CurrentBranch)++ if err != nil {+ term.OutputErrorAndExit("Error getting plan diffs: %v", err)+ return+ }++ term.PageOutput(diffs)+}diff --git a/app/cli/cmd/ls.go b/app/cli/cmd/ls.goindex fff09c8a..3c44a99a 100644--- a/app/cli/cmd/ls.go+++ b/app/cli/cmd/ls.go@@ -51,9 +51,14 @@ func listContext(cmd *cobra.Command, args []string) {t, icon := lib.GetContextLabelAndIcon(context.ContextType)+ name := context.Name+ if len(name) > 40 {+ name = name[:20] + "⋯" + name[len(name)-20:]+ }+row := []string{strconv.Itoa(i + 1),- " " + icon + " " + context.Name,+ " " + icon + " " + name,t,strconv.Itoa(context.NumTokens), //+ " 🪙",format.Time(context.CreatedAt),diff --git a/app/cli/cmd/model_packs.go b/app/cli/cmd/model_packs.goindex 1246238c..4d15bf1b 100644--- a/app/cli/cmd/model_packs.go+++ b/app/cli/cmd/model_packs.go@@ -2,72 +2,76 @@ package cmdimport ("fmt"+ "os""plandex/api"+ "plandex/auth""plandex/term""strconv""github.com/fatih/color"+ "github.com/olekukonko/tablewriter""github.com/plandex/plandex/shared""github.com/spf13/cobra")-var modelSetsCmd = &cobra.Command{- Use: "model-packs",- Short: "Manage model packs",-}+var customModelPacksOnly bool-var listModelSetsCmd = &cobra.Command{- Use: "list",+var modelPacksCmd = &cobra.Command{+ Use: "model-packs",Short: "List all model packs",- Run: listModelSets,+ Run: listModelPacks,}-var createModelSetCmd = &cobra.Command{+var createModelPackCmd = &cobra.Command{Use: "create",Short: "Create a model pack",- Run: createModelSet,+ Run: createModelPack,}-func init() {- RootCmd.AddCommand(modelSetsCmd)- modelSetsCmd.AddCommand(listModelSetsCmd)- modelSetsCmd.AddCommand(createModelSetCmd)- modelSetsCmd.AddCommand(deleteModelSetCmd)+var deleteModelPackCmd = &cobra.Command{+ Use: "delete [name-or-index]",+ Aliases: []string{"rm"},+ Short: "Delete a model pack by name or index",+ Args: cobra.MaximumNArgs(1),+ Run: deleteModelPack,}-var deleteModelSetCmd = &cobra.Command{- Use: "delete [name-or-index]",- Short: "Delete a model set by name or index",- Args: cobra.MaximumNArgs(1),- Run: deleteModelSet,+func init() {+ RootCmd.AddCommand(modelPacksCmd)+ modelPacksCmd.AddCommand(createModelPackCmd)+ modelPacksCmd.AddCommand(deleteModelPackCmd)++ modelPacksCmd.Flags().BoolVarP(&customModelPacksOnly, "custom", "c", false, "Only show custom model packs")}-func deleteModelSet(cmd *cobra.Command, args []string) {+func deleteModelPack(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()+term.StartSpinner("")- modelSets, err := api.Client.ListModelSets()+ modelPacks, err := api.Client.ListModelPacks()term.StopSpinner()if err != nil {- term.OutputErrorAndExit("Error fetching model sets: %v", err)+ term.OutputErrorAndExit("Error fetching model packs: %v", err)return}- if len(modelSets) == 0 {- fmt.Println("No model sets available to delete.")+ if len(modelPacks) == 0 {+ fmt.Println("🤷♂️ No custom model packs")return}- var setToDelete *shared.ModelSet+ var setToDelete *shared.ModelPackif len(args) == 1 {input := args[0]// Try to parse input as indexindex, err := strconv.Atoi(input)- if err == nil && index > 0 && index <= len(modelSets) {- setToDelete = modelSets[index-1]+ if err == nil && index > 0 && index <= len(modelPacks) {+ setToDelete = modelPacks[index-1]} else {// Search by name- for _, s := range modelSets {+ for _, s := range modelPacks {if s.Name == input {setToDelete = sbreak@@ -77,100 +81,159 @@ func deleteModelSet(cmd *cobra.Command, args []string) {}if setToDelete == nil {- fmt.Println("Select a model set to delete:")- for i, set := range modelSets {- fmt.Printf("%d: %s\n", i+1, set.Name)+ opts := make([]string, len(modelPacks))+ for i, mp := range modelPacks {+ opts[i] = mp.Name+ }++ selected, err := term.SelectFromList("Select a custom model pack:", opts)++ if err != nil {+ term.OutputErrorAndExit("Error selecting model pack: %v", err)}+var selectedIndex int- fmt.Scanln(&selectedIndex)- if selectedIndex < 1 || selectedIndex > len(modelSets) {- fmt.Println("Invalid selection.")- return+ for i, opt := range opts {+ if opt == selected {+ selectedIndex = i+ break+ }}- setToDelete = modelSets[selectedIndex-1]++ setToDelete = modelPacks[selectedIndex]}term.StartSpinner("")- err = api.Client.DeleteModelSet(setToDelete.Id)+ err = api.Client.DeleteModelPack(setToDelete.Id)term.StopSpinner()if err != nil {- term.OutputErrorAndExit("Error deleting model set: %v", err)+ term.OutputErrorAndExit("Error deleting model pack: %v", err)return}- fmt.Printf("Model set '%s' deleted successfully.\n", setToDelete.Name)+ fmt.Printf("✅ Deleted model pack %s\n", color.New(color.Bold, term.ColorHiCyan).Sprint(setToDelete.Name))++ fmt.Println()++ term.PrintCmds("", "model-packs", "model-packs --custom", "model-packs create")}-func listModelSets(cmd *cobra.Command, args []string) {- term.StartSpinner("Fetching model sets...")- builtInModelSets := shared.BuiltInModelSets // Assuming this is already defined in the shared package- customModelSets, err := api.Client.ListModelSets()+func listModelPacks(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()++ term.StartSpinner("")+ builtInModelPacks := shared.BuiltInModelPacks+ customModelPacks, err := api.Client.ListModelPacks()term.StopSpinner()if err != nil {- term.OutputErrorAndExit("Error fetching model sets: %v", err)+ term.OutputErrorAndExit("Error fetching model packs: %v", err)return}- fmt.Println("Available Model Sets:")- table := tablewriter.NewWriter(os.Stdout)- table.SetHeader([]string{"Name", "Description", "Type"})+ if !customModelPacksOnly {+ color.New(color.Bold, term.ColorHiCyan).Println("🏠 Built-in Model Packs")+ table := tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(true)+ table.SetRowLine(true)+ table.SetHeader([]string{"Name", "Description"})+ for _, set := range builtInModelPacks {+ table.Append([]string{set.Name, set.Description})+ }+ table.Render()+ fmt.Println()+ }++ if len(customModelPacks) > 0 {+ color.New(color.Bold, term.ColorHiCyan).Println("🛠️ Custom Model Packs")+ table := tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(true)+ table.SetRowLine(true)+ table.SetHeader([]string{"#", "Name", "Description"})+ for i, set := range customModelPacks {+ table.Append([]string{fmt.Sprintf("%d", i+1), set.Name, set.Description})+ }+ table.Render()- for _, set := range builtInModelSets {- table.Append([]string{set.Name, set.Description, "Built-in"})+ fmt.Println()+ } else if customModelPacksOnly {+ fmt.Println("🤷♂️ No custom model packs")+ fmt.Println()}- for _, set := range customModelSets {- table.Append([]string{set.Name, set.Description, "Custom"})+ if customModelPacksOnly && len(customModelPacks) > 0 {+ term.PrintCmds("", "model-packs create", "model-packs delete")+ } else if len(customModelPacks) > 0 {+ term.PrintCmds("", "model-packs --custom", "model-packs create", "model-packs delete")+ } else {+ term.PrintCmds("", "model-packs create")}- table.Render()}-func createModelSet(cmd *cobra.Command, args []string) {- set := &shared.ModelSet{}+func createModelPack(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()- name, err := term.GetUserStringInput("Enter model set name:")+ mp := &shared.ModelPack{}++ name, err := term.GetUserStringInput("Enter model pack name:")if err != nil {- term.OutputErrorAndExit("Error reading model set name: %v", err)+ term.OutputErrorAndExit("Error reading model pack name: %v", err)return}- set.Name = name+ mp.Name = namedescription, err := term.GetUserStringInput("Enter description:")if err != nil {term.OutputErrorAndExit("Error reading description: %v", err)return}- set.Description = description+ mp.Description = description++ term.StartSpinner("")+ customModels, apiErr := api.Client.ListCustomModels()+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error fetching models: %v", apiErr)+ }// Selecting models for each role- set.Planner = getPlannerRoleConfig()- set.PlanSummary = getModelRoleConfig(shared.ModelRolePlanSummary)- set.Builder = getModelRoleConfig(shared.ModelRoleBuilder)- set.Namer = getModelRoleConfig(shared.ModelRoleName)- set.CommitMsg = getModelRoleConfig(shared.ModelRoleCommitMsg)- set.ExecStatus = getModelRoleConfig(shared.ModelRoleExecStatus)+ mp.Planner = getPlannerRoleConfig(customModels)+ mp.PlanSummary = getModelRoleConfig(customModels, shared.ModelRolePlanSummary)+ mp.Builder = getModelRoleConfig(customModels, shared.ModelRoleBuilder)+ mp.Namer = getModelRoleConfig(customModels, shared.ModelRoleName)+ mp.CommitMsg = getModelRoleConfig(customModels, shared.ModelRoleCommitMsg)+ mp.ExecStatus = getModelRoleConfig(customModels, shared.ModelRoleExecStatus)term.StartSpinner("")- apiErr := api.Client.CreateModelSet(set)+ apiErr = api.Client.CreateModelPack(mp)term.StopSpinner()if apiErr != nil {- term.OutputErrorAndExit("Error creating model set: %v", apiErr.Msg)+ term.OutputErrorAndExit("Error creating model pack: %v", apiErr.Msg)return}- fmt.Println("✅ Created model pack", color.New(color.Bold, term.ColorHiCyan).Sprint())+ fmt.Println("✅ Created model pack", color.New(color.Bold, term.ColorHiCyan).Sprint(mp.Name))++ fmt.Println()++ term.PrintCmds("", "model-packs", "model-packs --custom", "model-packs delete")+}++func getModelRoleConfig(customModels []*shared.AvailableModel, modelRole shared.ModelRole) shared.ModelRoleConfig {+ _, modelConfig := getModelWithRoleConfig(customModels, modelRole)+ return modelConfig}-func getModelRoleConfig(modelRole shared.ModelRole) shared.ModelRoleConfig {+func getModelWithRoleConfig(customModels []*shared.AvailableModel, modelRole shared.ModelRole) (*shared.AvailableModel, shared.ModelRoleConfig) {role := string(modelRole)- baseModelConfig := selectModelForRole(modelRole)+ model := getModelForRole(customModels, modelRole)- temperatureStr, err := term.GetUserStringInput("Enter the temperature for " + role + ":")+ temperatureStr, err := term.GetUserStringInputWithDefault("Temperature for "+role+":", fmt.Sprintf("%.1f", shared.DefaultConfigByRole[modelRole].Temperature))if err != nil {term.OutputErrorAndExit("Error reading temperature: %v", err)}@@ -179,7 +242,7 @@ func getModelRoleConfig(modelRole shared.ModelRole) shared.ModelRoleConfig {term.OutputErrorAndExit("Invalid number for temperature: %v", err)}- topPStr, err := term.GetUserStringInput("Enter the top P for " + role + ":")+ topPStr, err := term.GetUserStringInputWithDefault("Top P for "+role+":", fmt.Sprintf("%.1f", shared.DefaultConfigByRole[modelRole].TopP))if err != nil {term.OutputErrorAndExit("Error reading top P: %v", err)}@@ -188,70 +251,149 @@ func getModelRoleConfig(modelRole shared.ModelRole) shared.ModelRoleConfig {term.OutputErrorAndExit("Invalid number for top P: %v", err)}- return shared.ModelRoleConfig{- BaseModelConfig: baseModelConfig,+ return model, shared.ModelRoleConfig{+ Role: modelRole,+ BaseModelConfig: model.BaseModelConfig,Temperature: float32(temperature),TopP: float32(topP),}}-func getPlannerRoleConfig() shared.PlannerRoleConfig {- modelConfig := getModelRoleConfig(shared.ModelRolePlanner)-- maxConvoTokensStr, err := term.GetUserStringInput("Max Convo Tokens:")- if err != nil {- term.OutputErrorAndExit("Error reading max-convo-tokens: %v", err)- }- maxConvoTokens, err := strconv.Atoi(maxConvoTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for max-convo-tokens: %v", err)- }-- reservedOutputTokensStr, err := term.GetUserStringInput("Reserved Output Tokens:")- if err != nil {- term.OutputErrorAndExit("Error reading reserved-output-tokens: %v", err)- }- reservedOutputTokens, err := strconv.Atoi(reservedOutputTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for reserved-output-tokens: %v", err)- }+func getPlannerRoleConfig(customModels []*shared.AvailableModel) shared.PlannerRoleConfig {+ model, modelConfig := getModelWithRoleConfig(customModels, shared.ModelRolePlanner)return shared.PlannerRoleConfig{ModelRoleConfig: modelConfig,PlannerModelConfig: shared.PlannerModelConfig{- MaxConvoTokens: maxConvoTokens,- ReservedOutputTokens: reservedOutputTokens,+ MaxConvoTokens: model.DefaultMaxConvoTokens,+ ReservedOutputTokens: model.DefaultReservedOutputTokens,},}}-func selectModelForRole(role shared.ModelRole) shared.BaseModelConfig {- term.StartSpinner("")- customModels, apiErr := api.Client.ListCustomModels()- term.StopSpinner()+func getModelForRole(customModels []*shared.AvailableModel, role shared.ModelRole) *shared.AvailableModel {+ color.New(color.Bold).Printf("Select a model for the %s role 👇\n", role)+ return selectModelForRole(customModels, role, false)+}- if apiErr != nil {- term.OutputErrorAndExit("Error fetching models: %v", apiErr)- }+func selectModelForRole(customModels []*shared.AvailableModel, role shared.ModelRole, includeProviderGoBack bool) *shared.AvailableModel {+ var providers []string+ addedProviders := map[string]bool{}++ builtInModels := shared.FilterCompatibleModels(shared.AvailableModels, role)++ for _, m := range builtInModels {+ var p string+ if m.Provider == shared.ModelProviderCustom {+ p = *m.CustomProvider+ } else {+ p = string(m.Provider)+ }- opts := make([]string, len(customModels))- for i, model := range customModels {- opts[i] = fmt.Sprintf("%s (%s)", model.ModelName, model.Provider)+ if !addedProviders[p] {+ providers = append(providers, p)+ addedProviders[p] = true+ }}- selected, err := term.SelectFromList(fmt.Sprintf("Select a model for the %s role:", role), opts)- if err != nil {- term.OutputErrorAndExit("Error selecting model: %v", err)+ customModels = shared.FilterCompatibleModels(customModels, role)++ for _, m := range customModels {+ var p string+ if m.Provider == shared.ModelProviderCustom {+ p = *m.CustomProvider+ } else {+ p = string(m.Provider)+ }++ if !addedProviders[p] {+ providers = append(providers, p)+ addedProviders[p] = true+ }}- var idx int- for i, opt := range opts {- if opt == selected {- idx = i+ for {+ var opts []string+ opts = append(opts, providers...)+ if includeProviderGoBack {+ opts = append(opts, goBack)+ }+ provider, err := term.SelectFromList("Select a provider:", opts)+ if err != nil {+ if err.Error() == "interrupt" {+ return nil+ }++ term.OutputErrorAndExit("Error selecting provider: %v", err)+ return nil+ }++ if provider == goBack {break}++ var selectableModels []*shared.AvailableModel+ opts = []string{}++ for _, m := range shared.AvailableModels {+ var p string+ if m.Provider == shared.ModelProviderCustom {+ p = *m.CustomProvider+ } else {+ p = string(m.Provider)+ }++ if p == provider {+ label := fmt.Sprintf("%s → %s | max %d 🪙", m.Provider, m.ModelName, m.MaxTokens)+ opts = append(opts, label)+ selectableModels = append(selectableModels, m)+ }+ }++ for _, m := range customModels {+ var p string+ if m.Provider == shared.ModelProviderCustom {+ p = *m.CustomProvider+ } else {+ p = string(m.Provider)+ }++ if p == provider {+ label := fmt.Sprintf("%s → %s | max %d 🪙", p, m.ModelName, m.MaxTokens)+ opts = append(opts, label)+ selectableModels = append(selectableModels, m)+ }+ }++ opts = append(opts, goBack)++ selection, err := term.SelectFromList("Select a model:", opts)++ if err != nil {+ if err.Error() == "interrupt" {+ return nil+ }++ term.OutputErrorAndExit("Error selecting model: %v", err)+ return nil+ }++ if selection == goBack {+ continue+ }++ var idx int+ for i := range opts {+ if opts[i] == selection {+ idx = i+ break+ }+ }++ return selectableModels[idx]+}- return customModels[idx].BaseModelConfig-}+ return nil+}diff --git a/app/cli/cmd/model_sets.go b/app/cli/cmd/model_sets.godeleted file mode 100644index 28808fdc..00000000--- a/app/cli/cmd/model_sets.go+++ /dev/null@@ -1,276 +0,0 @@-package cmd--import (- "fmt"- "plandex/api"- "plandex/term"- "strconv"-- "github.com/fatih/color"- "github.com/plandex/plandex/shared"- "github.com/spf13/cobra"-)--var modelSetsCmd = &cobra.Command{- Use: "model-packs",- Short: "Manage model packs",-}--var listModelSetsCmd = &cobra.Command{- Use: "list",- Short: "List all model packs",- Run: listModelSets,-}--var createModelSetCmd = &cobra.Command{- Use: "create",- Short: "Create a model pack",- Run: createModelSet,-}--func init() {- RootCmd.AddCommand(modelSetsCmd)- modelSetsCmd.AddCommand(listModelSetsCmd)- modelSetsCmd.AddCommand(createModelSetCmd)- modelSetsCmd.AddCommand(deleteModelSetCmd)-}--var deleteModelSetCmd = &cobra.Command{- Use: "delete [name-or-index]",- Short: "Delete a model set by name or index",- Args: cobra.MaximumNArgs(1),- Run: deleteModelSet,-}--func deleteModelSet(cmd *cobra.Command, args []string) {- term.StartSpinner("")- modelSets, err := api.Client.ListModelSets()- term.StopSpinner()-- if err != nil {- term.OutputErrorAndExit("Error fetching model sets: %v", err)- return- }-- if len(modelSets) == 0 {- fmt.Println("No model sets available to delete.")- return- }-- var setToDelete *shared.ModelSet-- if len(args) == 1 {- input := args[0]- // Try to parse input as index- index, err := strconv.Atoi(input)- if err == nil && index > 0 && index <= len(modelSets) {- setToDelete = modelSets[index-1]- } else {- // Search by name- for _, s := range modelSets {- if s.Name == input {- setToDelete = s- break- }- }- }- }-- if setToDelete == nil {- fmt.Println("Select a model set to delete:")- for i, set := range modelSets {- fmt.Printf("%d: %s\n", i+1, set.Name)- }- var selectedIndex int- fmt.Scanln(&selectedIndex)- if selectedIndex < 1 || selectedIndex > len(modelSets) {- fmt.Println("Invalid selection.")- return- }- setToDelete = modelSets[selectedIndex-1]- }-- term.StartSpinner("")- err = api.Client.DeleteModelSet(setToDelete.Id)- term.StopSpinner()-- if err != nil {- term.OutputErrorAndExit("Error deleting model set: %v", err)- return- }-- fmt.Printf("Model set '%s' deleted successfully.\n", setToDelete.Name)-}--func listModelSets(cmd *cobra.Command, args []string) {- term.StartSpinner("Fetching model sets...")- modelSets, err := api.Client.ListModelSets()- term.StopSpinner()-- if err != nil {- term.OutputErrorAndExit("Error fetching model sets: %v", err)- return- }-- fmt.Println("Available Model Sets:")- for _, set := range modelSets {- fmt.Printf("- %s: %s\n", set.Name, set.Description)- }-}--func createModelSet(cmd *cobra.Command, args []string) {- set := &shared.ModelSet{}-- name, err := term.GetUserStringInput("Enter model set name:")- if err != nil {- term.OutputErrorAndExit("Error reading model set name: %v", err)- return- }- set.Name = name-- description, err := term.GetUserStringInput("Enter description:")- if err != nil {- term.OutputErrorAndExit("Error reading description: %v", err)- return- }- set.Description = description-- // Selecting models for each role- set.Planner = getPlannerRoleConfig()- set.PlanSummary = getModelRoleConfig(shared.ModelRolePlanSummary)- set.Builder = getModelRoleConfig(shared.ModelRoleBuilder)- set.Namer = getModelRoleConfig(shared.ModelRoleName)- set.CommitMsg = getModelRoleConfig(shared.ModelRoleCommitMsg)- set.ExecStatus = getModelRoleConfig(shared.ModelRoleExecStatus)-- term.StartSpinner("")- apiErr := api.Client.CreateModelSet(set)- term.StopSpinner()-- if apiErr != nil {- term.OutputErrorAndExit("Error creating model set: %v", apiErr.Msg)- return- }-- fmt.Println("✅ Created model pack", color.New(color.Bold, term.ColorHiCyan).Sprint())-}--func getModelRoleConfig(role string) shared.ModelRoleConfig {- fmt.Printf("Configuring %s\n", role)-- modelName, err := term.GetUserStringInput("Enter the model name for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading model name: %v", err)- }-- provider, err := term.GetUserStringInput("Enter the provider for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading provider: %v", err)- }-- baseUrl, err := term.GetUserStringInput("Enter the base URL for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading base URL: %v", err)- }-- maxTokensStr, err := term.GetUserStringInput("Enter the maximum tokens for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading maximum tokens: %v", err)- }- maxTokens, err := strconv.Atoi(maxTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for maximum tokens: %v", err)- }-- temperatureStr, err := term.GetUserStringInput("Enter the temperature for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading temperature: %v", err)- }- temperature, err := strconv.ParseFloat(temperatureStr, 32)- if err != nil {- term.OutputErrorAndExit("Invalid number for temperature: %v", err)- }-- topPStr, err := term.GetUserStringInput("Enter the top P for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading top P: %v", err)- }- topP, err := strconv.ParseFloat(topPStr, 32)- if err != nil {- term.OutputErrorAndExit("Invalid number for top P: %v", err)- }-- return shared.ModelRoleConfig{- BaseModelConfig: shared.BaseModelConfig{- Provider: provider,- BaseUrl: baseUrl,- ModelName: modelName,- MaxTokens: maxTokens,- ApiKeyEnvVar: "API_KEY_ENV_VAR", // This should be set appropriately- },- Temperature: float32(temperature),- TopP: float32(topP),- }-}--func getPlannerRoleConfig(role string) shared.PlannerRoleConfig {- modelConfig := getModelRoleConfig(role)-- maxConvoTokensStr, err := term.GetUserStringInput("Enter the maximum conversation tokens for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading maximum conversation tokens: %v", err)- }- maxConvoTokens, err := strconv.Atoi(maxConvoTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for maximum conversation tokens: %v", err)- }-- reservedOutputTokensStr, err := term.GetUserStringInput("Enter the reserved output tokens for " + role + ":")- if err != nil {- term.OutputErrorAndExit("Error reading reserved output tokens: %v", err)- }- reservedOutputTokens, err := strconv.Atoi(reservedOutputTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for reserved output tokens: %v", err)- }-- return shared.PlannerRoleConfig{- ModelRoleConfig: modelConfig,- PlannerModelConfig: shared.PlannerModelConfig{- MaxConvoTokens: maxConvoTokens,- ReservedOutputTokens: reservedOutputTokens,- },- }-}--func selectModelForRole(role shared.ModelRole) *shared.BaseModelConfig {- term.StartSpinner(fmt.Sprintf("Fetching models for role: %s...", role))- customModel, apiErr := api.Client.ListCustomModels()- term.StopSpinner()-- if apiErr != nil {- term.OutputErrorAndExit("Error fetching models: %v", apiErr)- }-- modelNames := make([]string, len(customModel))- for i, model := range customModel {- modelNames[i] = fmt.Sprintf("%s (%s)", model.ModelName, model.Provider)- }-- selected, err := term.SelectFromList(fmt.Sprintf("Select a model for the %s role:", role), modelNames)- if err != nil {- term.OutputErrorAndExit("Error selecting model: %v", err)- }-- var idx int- for i, name := range modelNames {- if name == selected {- idx = i- break- }- }-- return customModel[idx].BaseModelConfig-}--diff --git a/app/cli/cmd/models.go b/app/cli/cmd/models.goindex 089afe6e..813a6a84 100644--- a/app/cli/cmd/models.go+++ b/app/cli/cmd/models.go@@ -15,192 +15,207 @@ import ("github.com/spf13/cobra")-var availableModelsOnly bool+var customModelsOnly boolfunc init() {RootCmd.AddCommand(modelsCmd)modelsCmd.AddCommand(listAvailableModelsCmd)- modelsCmd.AddCommand(createAvailableModelCmd)- modelsCmd.AddCommand(deleteAvailableModelCmd)+ modelsCmd.AddCommand(createCustomModelCmd)+ modelsCmd.AddCommand(deleteCustomModelCmd)+ modelsCmd.AddCommand(defaultModelsCmd)- listAvailableModelsCmd.Flags().BoolVarP(&availableModelsOnly, "custom", "c", false, "List custom models only")+ listAvailableModelsCmd.Flags().BoolVarP(&customModelsOnly, "custom", "c", false, "List custom models only")}var modelsCmd = &cobra.Command{Use: "models",- Short: "Show model settings",+ Short: "Show plan model settings",Run: models,}+var defaultModelsCmd = &cobra.Command{+ Use: "default",+ Short: "Show default model settings for new plans",+ Run: defaultModels,+}+var listAvailableModelsCmd = &cobra.Command{- Use: "available",- Short: "List all available models",- Run: listAvailableModels,+ Use: "available",+ Aliases: []string{"avail"},+ Short: "List all available models",+ Run: listAvailableModels,}var createCustomModelCmd = &cobra.Command{- Use: "create",- Short: "Create a custom model",- Run: createCustomModel,+ Use: "add",+ Aliases: []string{"create"},+ Short: "Add a custom model",+ Run: createCustomModel,+}++var deleteCustomModelCmd = &cobra.Command{+ Use: "rm",+ Aliases: []string{"remove", "delete"},+ Short: "Remove a custom model",+ Args: cobra.MaximumNArgs(1),+ Run: deleteCustomModel,}func createCustomModel(cmd *cobra.Command, args []string) {- model := &shared.AvailableModel{}+ auth.MustResolveAuthWithOrg()- modelName, err := term.GetUserStringInput("Enter model name:")- if err != nil {- term.OutputErrorAndExit("Error reading model name: %v", err)- return+ model := &shared.AvailableModel{+ BaseModelConfig: shared.BaseModelConfig{+ ModelCompatibility: shared.ModelCompatibility{+ IsOpenAICompatible: true,+ },+ },}- model.ModelName = modelName- provider, err := term.GetUserStringInput("Enter provider:")- if err != nil {- term.OutputErrorAndExit("Error reading provider: %v", err)- return- }- model.Provider = shared.ModelProvider(provider)+ opts := shared.AllModelProviders+ provider, err := term.SelectFromList("Select provider:", opts)- baseUrl, err := term.GetUserStringInput("Enter base URL:")if err != nil {- term.OutputErrorAndExit("Error reading base URL: %v", err)+ term.OutputErrorAndExit("Error selecting provider: %v", err)return}- model.BaseUrl = baseUrl+ model.Provider = shared.ModelProvider(provider)- maxTokensStr, err := term.GetUserStringInput("Enter max tokens:")- if err != nil {- term.OutputErrorAndExit("Error reading max tokens: %v", err)- return- }- maxTokens, err := strconv.Atoi(maxTokensStr)- if err != nil {- term.OutputErrorAndExit("Invalid number for max tokens: %v", err)- return+ if model.Provider == shared.ModelProviderCustom {+ customProvider, err := term.GetUserStringInput("Custom provider:")+ if err != nil {+ term.OutputErrorAndExit("Error reading custom provider: %v", err)+ return+ }+ model.CustomProvider = &customProvider}- model.MaxTokens = maxTokens- apiKeyEnvVar, err := term.GetUserStringInput("Enter API key environment variable:")+ fmt.Println("For model name, be sure to enter the exact, case-sensitive name of the model as it appears in the provider's API docs. Ex: 'gpt-4-turbo', 'meta-llama/Llama-3-70b-chat-hf'")+ modelName, err := term.GetUserStringInput("Model name:")if err != nil {- term.OutputErrorAndExit("Error reading API key environment variable: %v", err)+ term.OutputErrorAndExit("Error reading model name: %v", err)return}- model.ApiKeyEnvVar = apiKeyEnvVar+ model.ModelName = modelName- description, err := term.GetUserStringInput("Enter description (optional):")+ fmt.Println("Add a human friendly description if you want to.")+ description, err := term.GetUserStringInput("Description (optional):")if err != nil {term.OutputErrorAndExit("Error reading description: %v", err)return}model.Description = description- // Compatibility flags using ConfirmYesNo- model.ModelCompatibility.IsOpenAICompatible, err = term.ConfirmYesNo("Is this model OpenAI compatible?")- if err != nil {- term.OutputErrorAndExit("Error confirming OpenAI compatibility: %v", err)- return- }- model.ModelCompatibility.HasJsonResponseMode, err = term.ConfirmYesNo("Does this model support JSON mode?")- if err != nil {- term.OutputErrorAndExit("Error confirming JSON mode support: %v", err)- return+ if model.Provider == shared.ModelProviderCustom {+ baseUrl, err := term.GetUserStringInput("Base URL:")+ if err != nil {+ term.OutputErrorAndExit("Error reading base URL: %v", err)+ return+ }+ model.BaseUrl = baseUrl+ } else {+ model.BaseUrl = shared.BaseUrlByProvider[model.Provider]}- model.ModelCompatibility.HasStreaming, err = term.ConfirmYesNo("Does this model support streaming?")++ apiKeyDefault := shared.ApiKeyByProvider[model.Provider]+ apiKeyEnvVar, err := term.GetUserStringInputWithDefault("API key environment variable:", apiKeyDefault)if err != nil {- term.OutputErrorAndExit("Error confirming streaming support: %v", err)+ term.OutputErrorAndExit("Error reading API key environment variable: %v", err)return}- model.ModelCompatibility.HasFunctionCalling, err = term.ConfirmYesNo("Does this model support function calling?")+ model.ApiKeyEnvVar = apiKeyEnvVar++ fmt.Println("Max Tokens is the total maximum context size of the model.")++ maxTokensStr, err := term.GetUserStringInput("Max Tokens:")if err != nil {- term.OutputErrorAndExit("Error confirming function calling support: %v", err)+ term.OutputErrorAndExit("Error reading max tokens: %v", err)return}- model.ModelCompatibility.HasStreamingFunctionCalls, err = term.ConfirmYesNo("Does this model support streaming function calls?")+ maxTokens, err := strconv.Atoi(maxTokensStr)if err != nil {- term.OutputErrorAndExit("Error confirming streaming function calls support: %v", err)+ term.OutputErrorAndExit("Invalid number for max tokens: %v", err)return}+ model.MaxTokens = maxTokens- term.StartSpinner("Creating model...")- err = api.Client.CreateCustomModel(model)- term.StopSpinner()-+ fmt.Println("'Default Max Convo Tokens' is the default maximum size a conversation can reach in the 'planner' role before it is shortened by summarization. For models with 8k context, ~2500 is recommended. For 128k context, ~10000 is recommended.")+ maxConvoTokensStr, err := term.GetUserStringInput("Default Max Convo Tokens:")if err != nil {- term.OutputErrorAndExit("Error creating model: %v", err)+ term.OutputErrorAndExit("Error reading max convo tokens: %v", err)return}-- fmt.Println("Model created successfully.")-}--func createAvailableModel(cmd *cobra.Command, args []string) {- model := &shared.AvailableModel{}-- modelName, err := term.GetUserStringInput("Enter model name:")+ maxConvoTokens, err := strconv.Atoi(maxConvoTokensStr)if err != nil {- term.OutputErrorAndExit("Error reading model name: %v", err)+ term.OutputErrorAndExit("Invalid number for max convo tokens: %v", err)return}- model.ModelName = modelName+ model.DefaultMaxConvoTokens = maxConvoTokens- provider, err := term.GetUserStringInput("Enter provider:")+ fmt.Println("'Default Reserved Output Tokens' is the default number of tokens reserved for model output in the 'planner' role. This ensures the model has enough tokens to generate a response. For models with 8k context, ~1000 is recommended. For 128k context, ~4000 is recommended.")+ reservedOutputTokensStr, err := term.GetUserStringInput("Default Reserved Output Tokens:")if err != nil {- term.OutputErrorAndExit("Error reading provider: %v", err)+ term.OutputErrorAndExit("Error reading reserved output tokens: %v", err)return}- model.Provider = shared.ModelProvider(provider)-- baseUrl, err := term.GetUserStringInput("Enter base URL:")+ reservedOutputTokens, err := strconv.Atoi(reservedOutputTokensStr)if err != nil {- term.OutputErrorAndExit("Error reading base URL: %v", err)+ term.OutputErrorAndExit("Invalid number for reserved output tokens: %v", err)return}- model.BaseUrl = baseUrl+ model.DefaultReservedOutputTokens = reservedOutputTokens- maxTokensStr, err := term.GetUserStringInput("Enter max tokens:")+ model.ModelCompatibility.HasStreaming, err = term.ConfirmYesNo("Is streaming supported?")if err != nil {- term.OutputErrorAndExit("Error reading max tokens: %v", err)+ term.OutputErrorAndExit("Error confirming streaming support: %v", err)return}- maxTokens, err := strconv.Atoi(maxTokensStr)+ model.ModelCompatibility.HasJsonResponseMode, err = term.ConfirmYesNo("Is JSON mode supported?")if err != nil {- term.OutputErrorAndExit("Invalid number for max tokens: %v", err)+ term.OutputErrorAndExit("Error confirming JSON mode support: %v", err)return}- model.MaxTokens = maxTokens-- apiKeyEnvVar, err := term.GetUserStringInput("Enter API key environment variable:")+ model.ModelCompatibility.HasFunctionCalling, err = term.ConfirmYesNo("Is function calling supported?")if err != nil {- term.OutputErrorAndExit("Error reading API key environment variable: %v", err)+ term.OutputErrorAndExit("Error confirming function calling support: %v", err)return}- model.ApiKeyEnvVar = apiKeyEnvVar-- description, err := term.GetUserStringInput("Enter description (optional):")+ model.ModelCompatibility.HasStreamingFunctionCalls, err = term.ConfirmYesNo("Are streaming function calls supported?")if err != nil {- term.OutputErrorAndExit("Error reading description: %v", err)+ term.OutputErrorAndExit("Error confirming streaming function calls support: %v", err)return}- model.Description = descriptionterm.StartSpinner("")- apiErr := api.Client.CreateAvailableModel(model)+ apiErr := api.Client.CreateCustomModel(model)term.StopSpinner()if apiErr != nil {- term.OutputErrorAndExit("Error creating model: %v", apiErr.Msg)+ term.OutputErrorAndExit("Error adding model: %v", apiErr.Msg)return}- fmt.Println("✅ Created custom model", color.New(color.Bold, term.ColorHiCyan).Sprint(string(model.Provider)+" → "+model.ModelName))+ fmt.Println("✅ Added custom model", color.New(color.Bold, term.ColorHiCyan).Sprint(string(model.Provider)+" → "+model.ModelName))}func models(cmd *cobra.Command, args []string) {-auth.MustResolveAuthWithOrg()lib.MustResolveProject()+ if lib.CurrentPlanId == "" {+ term.OutputErrorAndExit("No current plan")+ }+term.StartSpinner("")++ plan, err := api.Client.GetPlan(lib.CurrentPlanId)++ if err != nil {+ term.StopSpinner()+ term.OutputErrorAndExit("Error getting plan: %v", err)+ return+ }+settings, err := api.Client.GetSettings(lib.CurrentPlanId, lib.CurrentBranch)term.StopSpinner()@@ -209,95 +224,49 @@ func models(cmd *cobra.Command, args []string) {return}- modelSet := settings.ModelSet- if modelSet == nil {- modelSet = shared.DefaultModelSet- }+ title := fmt.Sprintf("%s Model Settings", color.New(color.Bold, term.ColorHiGreen).Sprint(plan.Name))- color.New(color.Bold, term.ColorHiCyan).Println("🎛️ Current Model Set")table := tablewriter.NewWriter(os.Stdout)- table.SetAutoFormatHeaders(false)- table.SetAutoWrapText(true)- table.SetColWidth(64)- table.SetHeader([]string{modelSet.Name})- table.Append([]string{modelSet.Description})+ table.SetAutoWrapText(false)+ table.Append([]string{title})table.Render()fmt.Println()- color.New(color.Bold, term.ColorHiCyan).Println("🤖 Models")- table = tablewriter.NewWriter(os.Stdout)- table.SetAutoWrapText(false)- table.SetHeader([]string{"Role", "Provider", "Model", "Temperature", "Top P"})+ renderSettings(settings)- addModelRow := func(role string, config shared.ModelRoleConfig) {- table.Append([]string{- role,- string(config.BaseModelConfig.Provider),- config.BaseModelConfig.ModelName,- fmt.Sprintf("%.1f", config.Temperature),- fmt.Sprintf("%.1f", config.TopP),- })- }+ term.PrintCmds("", "set-model", "models available", "models default")+}- addModelRow(string(shared.ModelRolePlanner), modelSet.Planner.ModelRoleConfig)- addModelRow(string(shared.ModelRolePlanSummary), modelSet.PlanSummary)- addModelRow(string(shared.ModelRoleBuilder), modelSet.Builder)- addModelRow(string(shared.ModelRoleName), modelSet.Namer)- addModelRow(string(shared.ModelRoleCommitMsg), modelSet.CommitMsg)- addModelRow(string(shared.ModelRoleExecStatus), modelSet.ExecStatus)- table.Render()+func defaultModels(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()- fmt.Println()+ term.StartSpinner("")+ settings, err := api.Client.GetOrgDefaultSettings()+ term.StopSpinner()- color.New(color.Bold, term.ColorHiCyan).Println("🧠 Planner Defaults")- table = tablewriter.NewWriter(os.Stdout)- table.SetAutoWrapText(false)- table.SetHeader([]string{"Max Tokens", "Max Convo Tokens", "Reserved Output Tokens"})- table.Append([]string{- fmt.Sprintf("%d", modelSet.Planner.BaseModelConfig.MaxTokens),- fmt.Sprintf("%d", modelSet.Planner.MaxConvoTokens),- fmt.Sprintf("%d", modelSet.Planner.ReservedOutputTokens),- })- table.Render()- fmt.Println()+ if err != nil {+ term.OutputErrorAndExit("Error getting default model settings: %v", err)+ return+ }- color.New(color.Bold, term.ColorHiCyan).Println("⚙️ Planner Overrides")- table = tablewriter.NewWriter(os.Stdout)+ title := fmt.Sprintf("%s Model Settings", color.New(color.Bold, term.ColorHiGreen).Sprint("Org-Wide Default"))+ table := tablewriter.NewWriter(os.Stdout)table.SetAutoWrapText(false)- table.SetHeader([]string{"Name", "Value"})- if settings.ModelOverrides.MaxTokens == nil {- table.Append([]string{"Max Tokens", "no override"})- } else {- table.Append([]string{"Max Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.MaxTokens)})- }- if settings.ModelOverrides.MaxConvoTokens == nil {- table.Append([]string{"Max Convo Tokens", "no override"})- } else {- table.Append([]string{"Max Convo Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.MaxConvoTokens)})- }- if settings.ModelOverrides.ReservedOutputTokens == nil {- table.Append([]string{"Reserved Output Tokens", "no override"})- } else {- table.Append([]string{"Reserved Output Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.ReservedOutputTokens)})- }+ table.Append([]string{title})table.Render()fmt.Println()- term.PrintCmds("", "set-model", "models available")+ renderSettings(settings)++ term.PrintCmds("", "set-model default", "models available", "models")}func listAvailableModels(cmd *cobra.Command, args []string) {auth.MustResolveAuthWithOrg()- lib.MustResolveProject()-- if lib.CurrentPlanId == "" {- fmt.Println("🤷♂️ No current plan")- return- }term.StartSpinner("")- availableModels, err := api.Client.ListAvailableModels()+ customModels, err := api.Client.ListCustomModels()term.StopSpinner()@@ -306,43 +275,57 @@ func listAvailableModels(cmd *cobra.Command, args []string) {return}- if !availableModelsOnly {+ if !customModelsOnly {color.New(color.Bold, term.ColorHiCyan).Println("🏠 Built-in Models")builtIn := shared.AvailableModelstable := tablewriter.NewWriter(os.Stdout)table.SetAutoWrapText(false)- table.SetHeader([]string{"Provider", "Name", "🪙", "🔑", "Url"})+ table.SetHeader([]string{"Provider", "Name", "🪙", "🔑"})for _, model := range builtIn {- table.Append([]string{string(model.Provider), model.ModelName, strconv.Itoa(model.MaxTokens), model.ApiKeyEnvVar, model.BaseUrl})+ table.Append([]string{string(model.Provider), model.ModelName, strconv.Itoa(model.MaxTokens), model.ApiKeyEnvVar})}table.Render()fmt.Println()}- if len(availableModels) > 0 {- color.New(color.Bold, term.ColorHiCyan).Println("🛠️ Custom Models")+ if len(customModels) > 0 {+ color.New(color.Bold, term.ColorHiCyan).Println("🛠️ Custom Models")table := tablewriter.NewWriter(os.Stdout)table.SetAutoWrapText(false)- table.SetHeader([]string{"#", "Provider", "Name", "🪙", "🔑", "Url"})- for i, model := range availableModels {- table.Append([]string{fmt.Sprintf("%d", i+1), model.ModelName, string(model.Provider), strconv.Itoa(model.MaxTokens), model.ApiKeyEnvVar, model.BaseUrl})+ table.SetHeader([]string{"#", "Provider", "Name", "🪙", "🔑"})+ for i, model := range customModels {+ var provider string+ p := model.Provider+ if p == shared.ModelProviderCustom {+ provider = *model.CustomProvider+ } else {+ provider = string(p)+ }++ table.Append([]string{fmt.Sprintf("%d", i+1), provider, model.ModelName, strconv.Itoa(model.MaxTokens), model.ApiKeyEnvVar})}table.Render()- } else if availableModelsOnly {+ } else if customModelsOnly {fmt.Println("🤷♂️ No custom models")- fmt.Println()}+ fmt.Println()- if availableModelsOnly {- term.PrintCmds("", "models", "set-model")+ if customModelsOnly {+ if len(customModels) > 0 {+ term.PrintCmds("", "models", "set-model", "models create", "models delete")+ } else {+ term.PrintCmds("", "models create")+ }} else {- term.PrintCmds("", "models available --custom", "models", "set-model")+ term.PrintCmds("", "models available --custom", "models", "set-model", "models create", "models delete")}}-func deleteAvailableModel(cmd *cobra.Command, args []string) {+func deleteCustomModel(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()+term.StartSpinner("")- models, apiErr := api.Client.ListAvailableModels()+ models, apiErr := api.Client.ListCustomModels()term.StopSpinner()if apiErr != nil {@@ -352,6 +335,8 @@ func deleteAvailableModel(cmd *cobra.Command, args []string) {if len(models) == 0 {fmt.Println("🤷♂️ No custom models")+ fmt.Println()+ term.PrintCmds("", "models create")return}@@ -375,17 +360,34 @@ func deleteAvailableModel(cmd *cobra.Command, args []string) {}if modelToDelete == nil {- fmt.Println("Select a model to delete:")+ opts := make([]string, len(models))for i, model := range models {- fmt.Printf("%d: %s\n", i+1, model.ModelName)+ var provider string+ p := model.Provider+ if p == shared.ModelProviderCustom {+ provider = *model.CustomProvider+ } else {+ provider = string(p)+ }+ opts[i] = provider + " → " + model.ModelName}- var selectedIndex int- fmt.Scanln(&selectedIndex)- if selectedIndex < 1 || selectedIndex > len(models) {- fmt.Println("Invalid selection.")++ selected, err := term.SelectFromList("Select model to delete:", opts)++ if err != nil {+ term.OutputErrorAndExit("Error selecting model: %v", err)return}- modelToDelete = models[selectedIndex-1]++ var selectedIndex int+ for i, opt := range opts {+ if opt == selected {+ selectedIndex = i+ break+ }+ }++ modelToDelete = models[selectedIndex]}term.StartSpinner("")@@ -398,6 +400,80 @@ func deleteAvailableModel(cmd *cobra.Command, args []string) {}fmt.Printf("✅ Deleted custom model %s\n", color.New(color.Bold, term.ColorHiCyan).Sprint(string(modelToDelete.Provider)+" → "+modelToDelete.ModelName))++ fmt.Println()+ term.PrintCmds("", "models available", "models create")}+func renderSettings(settings *shared.PlanSettings) {+ modelPack := settings.ModelPack+ color.New(color.Bold, term.ColorHiCyan).Println("🎛️ Current Model Set")+ table := tablewriter.NewWriter(os.Stdout)+ table.SetAutoFormatHeaders(false)+ table.SetAutoWrapText(true)+ table.SetColWidth(64)+ table.SetHeader([]string{modelPack.Name})+ table.Append([]string{modelPack.Description})+ table.Render()+ fmt.Println()++ color.New(color.Bold, term.ColorHiCyan).Println("🤖 Models")+ table = tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(false)+ table.SetHeader([]string{"Role", "Provider", "Model", "Temperature", "Top P"})++ addModelRow := func(role string, config shared.ModelRoleConfig) {+ table.Append([]string{+ role,+ string(config.BaseModelConfig.Provider),+ config.BaseModelConfig.ModelName,+ fmt.Sprintf("%.1f", config.Temperature),+ fmt.Sprintf("%.1f", config.TopP),+ })+ }++ addModelRow(string(shared.ModelRolePlanner), modelPack.Planner.ModelRoleConfig)+ addModelRow(string(shared.ModelRolePlanSummary), modelPack.PlanSummary)+ addModelRow(string(shared.ModelRoleBuilder), modelPack.Builder)+ addModelRow(string(shared.ModelRoleName), modelPack.Namer)+ addModelRow(string(shared.ModelRoleCommitMsg), modelPack.CommitMsg)+ addModelRow(string(shared.ModelRoleExecStatus), modelPack.ExecStatus)+ table.Render()++ fmt.Println()++ color.New(color.Bold, term.ColorHiCyan).Println("🧠 Planner Defaults")+ table = tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(false)+ table.SetHeader([]string{"Max Tokens", "Max Convo Tokens", "Reserved Output Tokens"})+ table.Append([]string{+ fmt.Sprintf("%d", modelPack.Planner.BaseModelConfig.MaxTokens),+ fmt.Sprintf("%d", modelPack.Planner.MaxConvoTokens),+ fmt.Sprintf("%d", modelPack.Planner.ReservedOutputTokens),+ })+ table.Render()+ fmt.Println()++ color.New(color.Bold, term.ColorHiCyan).Println("⚙️ Planner Overrides")+ table = tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(false)+ table.SetHeader([]string{"Name", "Value"})+ if settings.ModelOverrides.MaxTokens == nil {+ table.Append([]string{"Max Tokens", "no override"})+ } else {+ table.Append([]string{"Max Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.MaxTokens)})+ }+ if settings.ModelOverrides.MaxConvoTokens == nil {+ table.Append([]string{"Max Convo Tokens", "no override"})+ } else {+ table.Append([]string{"Max Convo Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.MaxConvoTokens)})+ }+ if settings.ModelOverrides.ReservedOutputTokens == nil {+ table.Append([]string{"Reserved Output Tokens", "no override"})+ } else {+ table.Append([]string{"Reserved Output Tokens", fmt.Sprintf("%d", *settings.ModelOverrides.ReservedOutputTokens)})+ }+ table.Render()+ fmt.Println()+}diff --git a/app/cli/cmd/plans.go b/app/cli/cmd/plans.goindex 8c726844..95564b88 100644--- a/app/cli/cmd/plans.go+++ b/app/cli/cmd/plans.go@@ -25,15 +25,18 @@ import ("github.com/xlab/treeprint")+var archivedOnly bool+func init() {RootCmd.AddCommand(plansCmd)+ plansCmd.Flags().BoolVarP(&archivedOnly, "archived", "a", false, "List archived plans")}// plansCmd represents the list commandvar plansCmd = &cobra.Command{Use: "plans",Aliases: []string{"pl"},- Short: "List all available plans",+ Short: "List plans",Run: plans,}@@ -41,6 +44,14 @@ func plans(cmd *cobra.Command, args []string) {auth.MustResolveAuthWithOrg()lib.MaybeResolveProject()+ if archivedOnly {+ listArchived()+ } else {+ listActive()+ }+}++func listActive() {errCh := make(chan error)var parentProjectIdsWithPaths [][2]string@@ -312,8 +323,66 @@ func plans(cmd *cobra.Command, args []string) {fmt.Println()if len(currentProjectPlanIds) > 0 {- term.PrintCmds("", "new", "cd", "delete-plan")+ term.PrintCmds("", "new", "cd", "delete-plan", "plans --archived", "archive")} else {- term.PrintCmds("", "new")+ term.PrintCmds("", "new", "plans --archived")+ }+}++func listArchived() {+ var projectIds []string++ if lib.CurrentProjectId != "" {+ projectIds = append(projectIds, lib.CurrentProjectId)+ }++ term.StartSpinner("")+ plans, apiErr := api.Client.ListArchivedPlans(projectIds)+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error getting plans: %v", apiErr)}++ if len(plans) == 0 {+ fmt.Println("🤷♂️ No archived plans")+ fmt.Println()+ term.PrintCmds("", "archive")+ return+ }++ table := tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(false)+ table.SetHeader([]string{"#", "Name", "Updated"})++ for i, p := range plans {+ num := strconv.Itoa(i + 1)+ if p.Id == lib.CurrentPlanId {+ num = color.New(color.Bold, term.ColorHiGreen).Sprint(num)+ }++ row := []string{+ num,+ p.Name,+ format.Time(p.UpdatedAt),+ }++ var style []tablewriter.Colors+ if p.Name == lib.CurrentPlanId {+ style = []tablewriter.Colors{+ {tablewriter.FgHiGreenColor, tablewriter.Bold},+ }+ } else {+ style = []tablewriter.Colors{+ {tablewriter.Bold},+ }+ }++ table.Rich(row, style)++ }+ table.Render()++ fmt.Println()+ term.PrintCmds("", "unarchive")}diff --git a/app/cli/cmd/rename.go b/app/cli/cmd/rename.goindex 34f78c15..dad6b62e 100644--- a/app/cli/cmd/rename.go+++ b/app/cli/cmd/rename.go@@ -2,21 +2,20 @@ package cmdimport ("fmt"- "os""plandex/api""plandex/auth""plandex/lib""plandex/term"+ "github.com/fatih/color""github.com/spf13/cobra")var renameCmd = &cobra.Command{- Use: "rename [new-name]",- Aliases: []string{"rn"},- Short: "Rename the current plan",- Args: cobra.MaximumNArgs(1),- Run: rename,+ Use: "rename [new-name]",+ Short: "Rename the current plan",+ Args: cobra.MaximumNArgs(1),+ Run: rename,}func init() {@@ -27,23 +26,28 @@ func rename(cmd *cobra.Command, args []string) {auth.MustResolveAuthWithOrg()lib.MustResolveProject()+ if lib.CurrentPlanId == "" {+ fmt.Println("🤷♂️ No current plan")+ return+ }+var newName stringif len(args) > 0 {newName = args[0]} else {var err error- newName, err = term.Prompt("Enter new name for the current plan:")+ newName, err = term.GetUserStringInput("New name:")if err != nil {term.OutputErrorAndExit("Error reading new name: %v", err)}}if newName == "" {- fmt.Println("No new name provided.")+ fmt.Println("🤷♂️ No new name provided")return}- term.StartSpinner("Renaming plan")+ term.StartSpinner("")err := api.Client.RenamePlan(lib.CurrentPlanId, newName)term.StopSpinner()@@ -51,5 +55,5 @@ func rename(cmd *cobra.Command, args []string) {term.OutputErrorAndExit("Error renaming plan: %v", err)}- fmt.Printf("✅ Plan renamed to %s\n", newName)+ fmt.Printf("✅ Plan renamed to %s\n", color.New(color.Bold, term.ColorHiGreen).Sprint(newName))}diff --git a/app/cli/cmd/rewind.go b/app/cli/cmd/rewind.goindex d9bd22be..2d101b31 100644--- a/app/cli/cmd/rewind.go+++ b/app/cli/cmd/rewind.go@@ -103,7 +103,7 @@ func rewind(cmd *cobra.Command, args []string) {fmt.Println(msg)fmt.Println()- term.PrintCmds("", "log")+ term.PrintCmds("", "log", "continue")// fmt.Println(rwRes.LatestCommit)}diff --git a/app/cli/cmd/set_model.go b/app/cli/cmd/set_model.goindex 201ffb79..e698c1a3 100644--- a/app/cli/cmd/set_model.go+++ b/app/cli/cmd/set_model.go@@ -3,7 +3,6 @@ package cmdimport ("encoding/json""fmt"- "log""plandex/api""plandex/auth""plandex/lib"@@ -16,17 +15,31 @@ import ("github.com/spf13/cobra")+const goBack = "← Go back"++var provider string+func init() {RootCmd.AddCommand(modelsSetCmd)++ modelsSetCmd.AddCommand(defaultModelSetCmd)+}var modelsSetCmd = &cobra.Command{- Use: "set-model [role-or-setting] [property-or-value] [value]",- Short: "Update model settings",+ Use: "set-model [model-set-or-role-or-setting] [property-or-value] [value]",+ Short: "Update current plan model settings",Run: modelsSet,Args: cobra.MaximumNArgs(3),}+var defaultModelSetCmd = &cobra.Command{+ Use: "default [model-set-or-role-or-setting] [property-or-value] [value]",+ Short: "Update org-wide default model settings",+ Run: defaultModelsSet,+ Args: cobra.MaximumNArgs(3),+}+func modelsSet(cmd *cobra.Command, args []string) {auth.MustResolveAuthWithOrg()lib.MustResolveProject()@@ -40,51 +53,124 @@ func modelsSet(cmd *cobra.Command, args []string) {return}+ settings := updateModelSettings(args, originalSettings)++ if settings == nil {+ return+ }++ term.StartSpinner("")+ res, apiErr := api.Client.UpdateSettings(+ lib.CurrentPlanId,+ lib.CurrentBranch,+ shared.UpdateSettingsRequest{+ Settings: settings,+ })+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error updating settings: %v", apiErr)+ return+ }++ fmt.Println(res.Msg)+ fmt.Println()+ term.PrintCmds("", "models", "set-model default", "log")+}++func defaultModelsSet(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()++ term.StartSpinner("")+ originalSettings, apiErr := api.Client.GetOrgDefaultSettings()+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error getting current settings: %v", apiErr)+ return+ }++ settings := updateModelSettings(args, originalSettings)++ if settings == nil {+ return+ }++ term.StartSpinner("")+ res, apiErr := api.Client.UpdateOrgDefaultSettings(+ shared.UpdateSettingsRequest{+ Settings: settings,+ })+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error updating settings: %v", apiErr)+ return+ }++ fmt.Println(res.Msg)+ fmt.Println()+ term.PrintCmds("", "models", "set-model default", "log")+}++func updateModelSettings(args []string, originalSettings *shared.PlanSettings) *shared.PlanSettings {// Marshal and unmarshal to make a deep copy of the settingsjsonBytes, err := json.Marshal(originalSettings)if err != nil {term.OutputErrorAndExit("Error marshalling settings: %v", err)- return+ return nil}var settings *shared.PlanSettingserr = json.Unmarshal(jsonBytes, &settings)if err != nil {term.OutputErrorAndExit("Error unmarshalling settings: %v", err)- return+ return nil}- var roleOrSetting, propertyCompact, value string+ var modelSetOrRoleOrSetting, propertyCompact, value string+ var modelPack *shared.ModelPackvar role shared.ModelRolevar settingCompact stringvar settingDasherized string- var selectedModel *shared.BaseModelConfig+ var selectedModel *shared.AvailableModelvar temperature *float64var topP *float64if len(args) > 0 {- roleOrSetting = args[0]- for _, r := range shared.AllModelRoles {- if strings.EqualFold(string(r), roleOrSetting) {- role = r+ modelSetOrRoleOrSetting = args[0]++ for _, ms := range shared.BuiltInModelPacks {+ if strings.EqualFold(ms.Name, modelSetOrRoleOrSetting) {+ modelPack = msbreak}}- if role == "" {- for _, s := range shared.ModelOverridePropsDasherized {- compact := shared.Compact(s)- if strings.EqualFold(compact, shared.Compact(roleOrSetting)) {- settingCompact = compact- settingDasherized = s++ if modelPack == nil {+ for _, r := range shared.AllModelRoles {+ if strings.EqualFold(string(r), modelSetOrRoleOrSetting) {+ role = rbreak}}+ if role == "" {+ for _, s := range shared.ModelOverridePropsDasherized {+ compact := shared.Compact(s)+ if strings.EqualFold(compact, shared.Compact(modelSetOrRoleOrSetting)) {+ settingCompact = compact+ settingDasherized = s+ break+ }+ }+ }}}- if role == "" && settingCompact == "" {- // Prompt user to select between updating a top-level setting or a modelset role- opts := []string{}+ if modelPack == nil && role == "" && settingCompact == "" {+ // Prompt user to select between updating a model-set, a top-level setting or a model role+ opts := []string{"🎛️ choose a model pack to change all roles at once"}+for _, role := range shared.AllModelRoles {label := fmt.Sprintf("🤖 role | %s → %s", role, shared.ModelRoleDescriptions[role])opts = append(opts, label)@@ -94,14 +180,14 @@ func modelsSet(cmd *cobra.Command, args []string) {opts = append(opts, label)}- selection, err := term.SelectFromList("Select a role or override to update:", opts)+ selection, err := term.SelectFromList("Choose a new model pack, or select a role or override to update:", opts)if err != nil {if err.Error() == "interrupt" {- return+ return nil}term.OutputErrorAndExit("Error selecting setting or role: %v", err)- return+ return nil}idx := 0@@ -112,266 +198,315 @@ func modelsSet(cmd *cobra.Command, args []string) {}}- if idx < len(shared.AllModelRoles) {- role = shared.AllModelRoles[idx]- } else {- settingDasherized = shared.ModelOverridePropsDasherized[idx-len(shared.AllModelRoles)]- settingCompact = shared.Compact(settingDasherized)+ if idx == 0 {+ var opts []string+ for _, ms := range shared.BuiltInModelPacks {+ opts = append(opts, "Built-in | "+ms.Name)+ }- log.Printf("Selected setting: %s", settingDasherized)- log.Printf("Selected setting compact: %s", settingCompact)- }- }+ term.StartSpinner("")+ customModelPacks, apiErr := api.Client.ListModelPacks()+ term.StopSpinner()- if len(args) > 1 {- if role != "" {- propertyCompact = shared.Compact(args[1])- } else {- value = args[1]- }- }+ if apiErr != nil {+ term.OutputErrorAndExit("Error getting custom model packs: %v", apiErr)+ return nil+ }- if len(args) > 2 {- value = args[2]- }+ for _, ms := range customModelPacks {+ opts = append(opts, "Custom | "+ms.Name)+ }++ opts = append(opts, goBack)- if settingCompact != "" {- if value == "" {- var err error- value, err = term.GetUserStringInput(fmt.Sprintf("Set %s (leave blank for no value)", settingDasherized))+ selection, err := term.SelectFromList("Select a model pack:", opts)if err != nil {if err.Error() == "interrupt" {- return+ return nil}- term.OutputErrorAndExit("Error getting value: %v", err)- return+ term.OutputErrorAndExit("Error selecting model pack: %v", err)+ return nil}- }- switch settingCompact {- case "maxconvotokens":- if value == "" {- settings.ModelOverrides.MaxConvoTokens = nil- } else {- n, err := strconv.Atoi(value)- if err != nil {- fmt.Println("Invalid value for max-convo-tokens:", value)- return- }- settings.ModelOverrides.MaxConvoTokens = &n+ if selection == goBack {+ return updateModelSettings([]string{}, originalSettings)}- case "maxtokens":- if value == "" {- settings.ModelOverrides.MaxTokens = nil- } else {- n, err := strconv.Atoi(value)- if err != nil {- fmt.Println("Invalid value for max-tokens:", value)- return++ var idx int+ for i, opt := range opts {+ if opt == selection {+ idx = i+ break}- settings.ModelOverrides.MaxTokens = &n}- case "reservedoutputtokens":- if value == "" {- settings.ModelOverrides.ReservedOutputTokens = nil++ if idx < len(shared.BuiltInModelPacks) {+ modelPack = shared.BuiltInModelPacks[idx]} else {- n, err := strconv.Atoi(value)- if err != nil {- fmt.Println("Invalid value for reserved-output-tokens:", value)- return- }- settings.ModelOverrides.ReservedOutputTokens = &n+ modelPack = customModelPacks[idx-len(shared.BuiltInModelPacks)]}++ } else if idx < len(shared.AllModelRoles)+1 {+ role = shared.AllModelRoles[idx-1]+ } else {+ settingDasherized = shared.ModelOverridePropsDasherized[idx-(len(shared.AllModelRoles)+1)]+ settingCompact = shared.Compact(settingDasherized)}}- if role != "" {- if !(propertyCompact == "temperature" || propertyCompact == "topp") {- for _, m := range shared.AvailableModels {- if propertyCompact == shared.Compact(m.ModelName) {- selectedModel = &m- break- }+ if modelPack == nil {+ if len(args) > 1 {+ if role != "" {+ propertyCompact = strings.ToLower(shared.Compact(args[1]))+ } else {+ value = args[1]}}- if selectedModel == nil && propertyCompact == "" {- opts := []string{- "Select a model",- "Set temperature",- "Set top-p",- }+ if len(args) > 2 {+ value = args[2]+ }- selection, err := term.SelectFromList("Select a property to update:", opts)- if err != nil {- if err.Error() == "interrupt" {- return+ if settingCompact != "" {+ if value == "" {+ var err error+ value, err = term.GetUserStringInput(fmt.Sprintf("Set %s (leave blank for no value)", settingDasherized))+ if err != nil {+ if err.Error() == "interrupt" {+ return nil+ }++ term.OutputErrorAndExit("Error getting value: %v", err)+ return nil}+ }- term.OutputErrorAndExit("Error selecting property: %v", err)- return+ switch settingCompact {+ case "maxconvotokens":+ if value == "" {+ settings.ModelOverrides.MaxConvoTokens = nil+ } else {+ n, err := strconv.Atoi(value)+ if err != nil {+ fmt.Println("Invalid value for max-convo-tokens:", value)+ return nil+ }+ settings.ModelOverrides.MaxConvoTokens = &n+ }+ case "maxtokens":+ if value == "" {+ settings.ModelOverrides.MaxTokens = nil+ } else {+ n, err := strconv.Atoi(value)+ if err != nil {+ fmt.Println("Invalid value for max-tokens:", value)+ return nil+ }+ settings.ModelOverrides.MaxTokens = &n+ }+ case "reservedoutputtokens":+ if value == "" {+ settings.ModelOverrides.ReservedOutputTokens = nil+ } else {+ n, err := strconv.Atoi(value)+ if err != nil {+ fmt.Println("Invalid value for reserved-output-tokens:", value)+ return nil+ }+ settings.ModelOverrides.ReservedOutputTokens = &n+ }}+ }- if selection == "Select a model" {+ if role != "" {+ if !(propertyCompact == "temperature" || propertyCompact == "topp") {+ term.StartSpinner("")+ customModels, apiErr := api.Client.ListCustomModels()+ term.StopSpinner()- var opts []string- for _, m := range shared.AvailableModels {- label := fmt.Sprintf("%s → %s | max %d 🪙", m.Provider, m.ModelName, m.MaxTokens)- opts = append(opts, label)+ if apiErr != nil {+ term.OutputErrorAndExit("Error fetching models: %v", apiErr)}- selection, err := term.SelectFromList("Select a model:", opts)+ customModels = shared.FilterCompatibleModels(customModels, role)+ builtInModels := shared.FilterCompatibleModels(shared.AvailableModels, role)- if err != nil {- if err.Error() == "interrupt" {- return- }+ allModels := append(customModels, builtInModels...)- term.OutputErrorAndExit("Error selecting model: %v", err)- return- }+ for _, m := range allModels {+ var p string+ if m.Provider == shared.ModelProviderCustom {+ p = *m.CustomProvider+ } else {+ p = string(m.Provider)+ }+ p = strings.ToLower(p)- for i := range opts {- if opts[i] == selection {- selectedModel = &shared.AvailableModels[i]+ if propertyCompact == fmt.Sprintf("%s/%s", p, shared.Compact(m.ModelName)) {+ selectedModel = mbreak}}-- } else if selection == "Set temperature" {- propertyCompact = "temperature"- } else if selection == "Set top-p" {- propertyCompact = "topp"}- }- if selectedModel == nil {- if propertyCompact != "" {- if value == "" {- msg := "Set"- if propertyCompact == "temperature" {- msg += "temperature (-2.0 to 2.0)"- } else if propertyCompact == "topp" {- msg += "top-p (0.0 to 1.0)"+ if selectedModel == nil && propertyCompact == "" {+ Outer:+ for {+ opts := []string{+ "Select a model",+ "Set temperature",+ "Set top-p",}- var err error- value, err = term.GetUserStringInput(msg)++ opts = append(opts, goBack)++ selection, err := term.SelectFromList("Select a property to update:", opts)if err != nil {if err.Error() == "interrupt" {- return+ return nil}- term.OutputErrorAndExit("Error getting value: %v", err)- return+ term.OutputErrorAndExit("Error selecting property: %v", err)+ return nil}- }- switch propertyCompact {- case "temperature":- f, err := strconv.ParseFloat(value, 32)- if err != nil || f < -2.0 || f > 2.0 {- fmt.Println("Invalid value for temperature:", value)- return+ if selection == goBack {+ return updateModelSettings([]string{}, originalSettings)}- temperature = &f- case "topp":- f, err := strconv.ParseFloat(value, 32)- if err != nil || f < 0.0 || f > 1.0 {- fmt.Println("Invalid value for top-p:", value)- return++ if selection == "Select a model" {+ term.StartSpinner("")+ customModels, apiErr := api.Client.ListCustomModels()+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error fetching models: %v", apiErr)+ }++ selectedModel = selectModelForRole(customModels, role, true)+ } else if selection == "Set temperature" {+ propertyCompact = "temperature"+ break Outer+ } else if selection == "Set top-p" {+ propertyCompact = "topp"+ break Outer}- topP = &f}}- }- if settings.ModelSet == nil {- settings.ModelSet = &shared.DefaultModelSet- }+ if selectedModel == nil {+ if propertyCompact != "" {+ if value == "" {+ msg := "Set"+ if propertyCompact == "temperature" {+ msg += "temperature (-2.0 to 2.0)"+ } else if propertyCompact == "topp" {+ msg += "top-p (0.0 to 1.0)"+ }+ var err error+ value, err = term.GetUserStringInput(msg)+ if err != nil {+ if err.Error() == "interrupt" {+ return nil+ }++ term.OutputErrorAndExit("Error getting value: %v", err)+ return nil+ }+ }- switch role {- case shared.ModelRolePlanner:- if selectedModel != nil {- settings.ModelSet.Planner.BaseModelConfig = *selectedModel- settings.ModelSet.Planner.PlannerModelConfig = shared.PlannerModelConfigByName[selectedModel.ModelName]- } else if temperature != nil {- settings.ModelSet.Planner.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.Planner.TopP = float32(*topP)+ switch propertyCompact {+ case "temperature":+ f, err := strconv.ParseFloat(value, 32)+ if err != nil || f < -2.0 || f > 2.0 {+ fmt.Println("Invalid value for temperature:", value)+ return nil+ }+ temperature = &f+ case "topp":+ f, err := strconv.ParseFloat(value, 32)+ if err != nil || f < 0.0 || f > 1.0 {+ fmt.Println("Invalid value for top-p:", value)+ return nil+ }+ topP = &f+ }+ }}- case shared.ModelRolePlanSummary:- if selectedModel != nil {- settings.ModelSet.PlanSummary.BaseModelConfig = *selectedModel- } else if temperature != nil {- settings.ModelSet.PlanSummary.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.PlanSummary.TopP = float32(*topP)+ if settings.ModelPack == nil {+ settings.ModelPack = shared.DefaultModelPack}- case shared.ModelRoleBuilder:- if selectedModel != nil {- settings.ModelSet.Builder.BaseModelConfig = *selectedModel- settings.ModelSet.Builder.TaskModelConfig = shared.TaskModelConfigByName[selectedModel.ModelName]- } else if temperature != nil {- settings.ModelSet.Builder.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.Builder.TopP = float32(*topP)- }+ switch role {+ case shared.ModelRolePlanner:+ if selectedModel != nil {+ settings.ModelPack.Planner.BaseModelConfig = selectedModel.BaseModelConfig+ settings.ModelPack.Planner.PlannerModelConfig = shared.PlannerModelConfig{+ MaxConvoTokens: selectedModel.DefaultMaxConvoTokens,+ ReservedOutputTokens: selectedModel.DefaultReservedOutputTokens,+ }+ } else if temperature != nil {+ settings.ModelPack.Planner.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.Planner.TopP = float32(*topP)+ }- case shared.ModelRoleName:- if selectedModel != nil {- settings.ModelSet.Namer.BaseModelConfig = *selectedModel- settings.ModelSet.Namer.TaskModelConfig = shared.TaskModelConfigByName[selectedModel.ModelName]- } else if temperature != nil {- settings.ModelSet.Namer.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.Namer.TopP = float32(*topP)- }+ case shared.ModelRolePlanSummary:+ if selectedModel != nil {+ settings.ModelPack.PlanSummary.BaseModelConfig = selectedModel.BaseModelConfig+ } else if temperature != nil {+ settings.ModelPack.PlanSummary.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.PlanSummary.TopP = float32(*topP)+ }- case shared.ModelRoleCommitMsg:- if selectedModel != nil {- settings.ModelSet.CommitMsg.BaseModelConfig = *selectedModel- settings.ModelSet.CommitMsg.TaskModelConfig = shared.TaskModelConfigByName[selectedModel.ModelName]- } else if temperature != nil {- settings.ModelSet.CommitMsg.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.CommitMsg.TopP = float32(*topP)- }+ case shared.ModelRoleBuilder:+ if selectedModel != nil {+ settings.ModelPack.Builder.BaseModelConfig = selectedModel.BaseModelConfig+ } else if temperature != nil {+ settings.ModelPack.Builder.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.Builder.TopP = float32(*topP)+ }++ case shared.ModelRoleName:+ if selectedModel != nil {+ settings.ModelPack.Namer.BaseModelConfig = selectedModel.BaseModelConfig+ } else if temperature != nil {+ settings.ModelPack.Namer.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.Namer.TopP = float32(*topP)+ }++ case shared.ModelRoleCommitMsg:+ if selectedModel != nil {+ settings.ModelPack.CommitMsg.BaseModelConfig = selectedModel.BaseModelConfig+ } else if temperature != nil {+ settings.ModelPack.CommitMsg.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.CommitMsg.TopP = float32(*topP)+ }- case shared.ModelRoleExecStatus:- if selectedModel != nil {- settings.ModelSet.ExecStatus.BaseModelConfig = *selectedModel- settings.ModelSet.ExecStatus.TaskModelConfig = shared.TaskModelConfigByName[selectedModel.ModelName]- } else if temperature != nil {- settings.ModelSet.ExecStatus.Temperature = float32(*temperature)- } else if topP != nil {- settings.ModelSet.ExecStatus.TopP = float32(*topP)+ case shared.ModelRoleExecStatus:+ if selectedModel != nil {+ settings.ModelPack.ExecStatus.BaseModelConfig = selectedModel.BaseModelConfig+ } else if temperature != nil {+ settings.ModelPack.ExecStatus.Temperature = float32(*temperature)+ } else if topP != nil {+ settings.ModelPack.ExecStatus.TopP = float32(*topP)+ }}}+ } else {+ settings.ModelPack = modelPack}if reflect.DeepEqual(originalSettings, settings) {fmt.Println("🤷♂️ No model settings were updated")- return- }-- term.StartSpinner("")- res, apiErr := api.Client.UpdateSettings(- lib.CurrentPlanId,- lib.CurrentBranch,- shared.UpdateSettingsRequest{- Settings: settings,- })- term.StopSpinner()-- if apiErr != nil {- term.OutputErrorAndExit("Error updating settings: %v", apiErr)- return+ return nil+ } else {+ return settings}-- fmt.Println(res.Msg)- fmt.Println()- term.PrintCmds("", "models", "log", "rewind")}diff --git a/app/cli/cmd/status.go b/app/cli/cmd/status.godeleted file mode 100644index 1d619dd0..00000000--- a/app/cli/cmd/status.go+++ /dev/null@@ -1 +0,0 @@-package cmddiff --git a/app/cli/cmd/summary.go b/app/cli/cmd/summary.gonew file mode 100644index 00000000..73249d54--- /dev/null+++ b/app/cli/cmd/summary.go@@ -0,0 +1,46 @@+package cmd++import (+ "fmt"+ "plandex/api"+ "plandex/auth"+ "plandex/lib"+ "plandex/term"++ "github.com/spf13/cobra"+)++var statusCmd = &cobra.Command{+ Use: "summary",+ Short: "Show the latest summary of the current plan",+ Run: status,+}++func init() {+ RootCmd.AddCommand(statusCmd)+}++func status(cmd *cobra.Command, args []string) {+ auth.MustResolveAuthWithOrg()+ lib.MustResolveProject()++ term.StartSpinner("")+ status, apiErr := api.Client.GetPlanStatus(lib.CurrentPlanId, lib.CurrentBranch)+ term.StopSpinner()++ if apiErr != nil {+ term.OutputErrorAndExit("Error loading conversation: %v", apiErr.Msg)+ }++ if status == "" {+ fmt.Println("🤷♂️ No summary available")+ }++ md, err := term.GetMarkdown(status)++ if err != nil {+ term.OutputErrorAndExit("Error formatting markdown: %v", err)+ }++ fmt.Println(md)+}diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.goindex a227257a..2c34eb34 100644--- a/app/cli/cmd/tell.go+++ b/app/cli/cmd/tell.go@@ -43,9 +43,6 @@ func init() {}func doTell(cmd *cobra.Command, args []string) {- if os.Getenv("OPENAI_API_KEY") == "" {- term.OutputNoApiKeyMsgAndExit()- }auth.MustResolveAuthWithOrg()lib.MustResolveProject()@@ -55,6 +52,8 @@ func doTell(cmd *cobra.Command, args []string) {return}+ apiKeys := lib.MustVerifyApiKeys()+var prompt stringif len(args) > 0 {@@ -77,6 +76,7 @@ func doTell(cmd *cobra.Command, args []string) {plan_exec.TellPlan(plan_exec.ExecParams{CurrentPlanId: lib.CurrentPlanId,CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool) {return lib.MustCheckOutdatedContext(false, maybeContexts)},diff --git a/app/cli/cmd/unarchive.go b/app/cli/cmd/unarchive.goindex baae1622..d98069b3 100644--- a/app/cli/cmd/unarchive.go+++ b/app/cli/cmd/unarchive.go@@ -2,7 +2,6 @@ package cmdimport ("fmt"- "os""plandex/api""plandex/auth""plandex/lib"@@ -11,12 +10,13 @@ import ("strings""github.com/fatih/color"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")var unarchiveCmd = &cobra.Command{Use: "unarchive [name-or-index]",- Aliases: []string{"uar"},+ Aliases: []string{"unarc"},Short: "Unarchive a plan",Args: cobra.MaximumNArgs(1),Run: unarchive,@@ -37,7 +37,7 @@ func unarchive(cmd *cobra.Command, args []string) {var plan *shared.Plan- term.StartSpinner("Loading archived plans")+ term.StartSpinner("")plans, apiErr := api.Client.ListArchivedPlans([]string{lib.CurrentProjectId})term.StopSpinner()@@ -46,7 +46,7 @@ func unarchive(cmd *cobra.Command, args []string) {}if len(plans) == 0 {- fmt.Println("🤷♂️ No archived plans available to unarchive")+ fmt.Println("🤷♂️ No archived plans")return}@@ -56,7 +56,7 @@ func unarchive(cmd *cobra.Command, args []string) {opts[i] = p.Name}- selected, err := term.SelectFromList("Select a plan to unarchive", opts)+ selected, err := term.SelectFromList("Select a plan:", opts)if err != nil {term.OutputErrorAndExit("Error selecting plan: %v", err)}@@ -90,5 +90,8 @@ func unarchive(cmd *cobra.Command, args []string) {term.OutputErrorAndExit("Error unarchiving plan: %v", err)}- fmt.Printf("✅ Plan %s unarchived successfully\n", color.New(color.Bold).Sprint(plan.Name))+ fmt.Printf("✅ Plan %s unarchived\n", color.New(color.Bold, term.ColorHiGreen).Sprint(plan.Name))++ fmt.Println()+ term.PrintCmds("", "plans", "current")}diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.goindex 4a59d314..97099086 100644--- a/app/cli/lib/apply.go+++ b/app/cli/lib/apply.go@@ -8,6 +8,8 @@ import ("plandex/fs""plandex/term""strings"++ "github.com/plandex/plandex/shared")func MustApplyPlan(planId, branch string, autoConfirm bool) {@@ -108,7 +110,20 @@ func MustApplyPlan(planId, branch string, autoConfirm bool) {term.OutputSimpleError(errMsg, unformattedErrMsg)}- apiErr = api.Client.ApplyPlan(planId, branch)+ apiKeys := MustVerifyApiKeysSilent()++ var commitSummary string++ openAIBase := os.Getenv("OPENAI_API_BASE")+ if openAIBase == "" {+ openAIBase = os.Getenv("OPENAI_ENDPOINT")+ }++ commitSummary, apiErr = api.Client.ApplyPlan(planId, branch, shared.ApplyPlanRequest{+ ApiKeys: apiKeys,+ OpenAIBase: openAIBase,+ OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),+ })if apiErr != nil {onErr("failed to set pending results applied: %s", apiErr.Msg)@@ -191,7 +206,7 @@ func MustApplyPlan(planId, branch string, autoConfirm bool) {if confirmed {// Commit the changes- msg := currentPlanState.PendingChangesSummaryForApply()+ msg := currentPlanState.PendingChangesSummaryForApply(commitSummary)// log.Println("Committing changes with message:")// log.Println(msg)diff --git a/app/cli/lib/model_settings.go b/app/cli/lib/model_settings.gonew file mode 100644index 00000000..e5a8530f--- /dev/null+++ b/app/cli/lib/model_settings.go@@ -0,0 +1,60 @@+package lib++import (+ "fmt"+ "os"+ "plandex/api"+ "plandex/term"++ "github.com/fatih/color"+)++func MustVerifyApiKeys() map[string]string {+ return mustVerifyApiKeys(false)+}++func MustVerifyApiKeysSilent() map[string]string {+ return mustVerifyApiKeys(true)+}++func mustVerifyApiKeys(silent bool) map[string]string {+ if !silent {+ term.StartSpinner("")+ }+ planSettings, apiErr := api.Client.GetSettings(CurrentPlanId, CurrentBranch)+ if !silent {+ term.StopSpinner()+ }++ if apiErr != nil {+ term.OutputErrorAndExit("Error getting current settings: %v", apiErr)+ }++ requiredEnvVars := planSettings.GetRequiredEnvVars()++ apiKeys := make(map[string]string)++ if len(requiredEnvVars) == 1 && requiredEnvVars["OPENAI_API_KEY"] {+ if os.Getenv("OPENAI_API_KEY") == "" {+ term.OutputNoOpenAIApiKeyMsgAndExit()+ }+ apiKeys["OPENAI_API_KEY"] = os.Getenv("OPENAI_API_KEY")+ return apiKeys+ }++ missingAny := false+ for envVar := range requiredEnvVars {+ if os.Getenv(envVar) == "" {+ fmt.Fprintln(os.Stderr, color.New(color.Bold, term.ColorHiRed).Sprintf("🚨 %s environment variable is not set.\n", envVar))+ missingAny = true+ } else {+ apiKeys[envVar] = os.Getenv(envVar)+ }+ }++ if missingAny {+ os.Exit(1)+ }++ return apiKeys+}diff --git a/app/cli/main.go b/app/cli/main.goindex 31fa6864..d90d056d 100644--- a/app/cli/main.go+++ b/app/cli/main.go@@ -19,9 +19,11 @@ func init() {// inter-package dependency injections to avoid circular importsauth.SetApiClient(api.Client)lib.SetBuildPlanInlineFn(func(maybeContexts []*shared.Context) (bool, error) {+ apiKeys := lib.MustVerifyApiKeys()return plan_exec.Build(plan_exec.ExecParams{CurrentPlanId: lib.CurrentPlanId,CurrentBranch: lib.CurrentBranch,+ ApiKeys: apiKeys,CheckOutdatedContext: func(maybeContexts []*shared.Context) (bool, bool) {return lib.MustCheckOutdatedContext(true, maybeContexts)},diff --git a/app/cli/plan_exec/build.go b/app/cli/plan_exec/build.goindex 36f0b018..19a56d9d 100644--- a/app/cli/plan_exec/build.go+++ b/app/cli/plan_exec/build.go@@ -36,12 +36,29 @@ func Build(params ExecParams, buildBg bool) (bool, error) {return false, fmt.Errorf("error getting project paths: %v", err)}+ var legacyApiKey, openAIBase, openAIOrgId string++ if params.ApiKeys["OPENAI_API_KEY"] != "" {+ legacyApiKey = params.ApiKeys["OPENAI_API_KEY"]+ openAIBase = os.Getenv("OPENAI_API_BASE")+ if openAIBase == "" {+ openAIBase = os.Getenv("OPENAI_ENDPOINT")+ }+ openAIOrgId = os.Getenv("OPENAI_ORG_ID")+ }++ // log.Println("Building plan...")+ // log.Println("API keys:", params.ApiKeys)+ // log.Println("Legacy API key:", legacyApiKey)+apiErr = api.Client.BuildPlan(params.CurrentPlanId, params.CurrentBranch, shared.BuildPlanRequest{ConnectStream: !buildBg,ProjectPaths: paths.ActivePaths,- ApiKey: os.Getenv("OPENAI_API_KEY"),- Endpoint: os.Getenv("OPENAI_ENDPOINT"),- OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),+ ApiKey: legacyApiKey, // deprecated+ Endpoint: openAIBase, // deprecated+ ApiKeys: params.ApiKeys,+ OpenAIBase: openAIBase,+ OpenAIOrgId: openAIOrgId,}, stream.OnStreamPlan)term.StopSpinner()diff --git a/app/cli/plan_exec/params.go b/app/cli/plan_exec/params.goindex b9236736..f4a8dd5d 100644--- a/app/cli/plan_exec/params.go+++ b/app/cli/plan_exec/params.go@@ -5,5 +5,6 @@ import "github.com/plandex/plandex/shared"type ExecParams struct {CurrentPlanId stringCurrentBranch string+ ApiKeys map[string]stringCheckOutdatedContext func(maybeContexts []*shared.Context) (bool, bool)}diff --git a/app/cli/plan_exec/tell.go b/app/cli/plan_exec/tell.goindex 25b43b69..9f51a997 100644--- a/app/cli/plan_exec/tell.go+++ b/app/cli/plan_exec/tell.go@@ -63,6 +63,18 @@ func TellPlan(term.StartSpinner("💬 Sending prompt...")}+ var legacyApiKey, openAIBase, openAIOrgId string++ if params.ApiKeys["OPENAI_API_KEY"] != "" {+ openAIBase = os.Getenv("OPENAI_API_BASE")+ if openAIBase == "" {+ openAIBase = os.Getenv("OPENAI_ENDPOINT")+ }++ legacyApiKey = params.ApiKeys["OPENAI_API_KEY"]+ openAIOrgId = params.ApiKeys["OPENAI_ORG_ID"]+ }+apiErr := api.Client.TellPlan(params.CurrentPlanId, params.CurrentBranch, shared.TellPlanRequest{Prompt: prompt,ConnectStream: !tellBg,@@ -70,9 +82,11 @@ func TellPlan(ProjectPaths: paths.ActivePaths,BuildMode: buildMode,IsUserContinue: isUserContinue,- ApiKey: os.Getenv("OPENAI_API_KEY"),- Endpoint: os.Getenv("OPENAI_ENDPOINT"),- OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),+ ApiKey: legacyApiKey, // deprecated+ Endpoint: openAIBase, // deprecated+ ApiKeys: params.ApiKeys,+ OpenAIBase: openAIBase,+ OpenAIOrgId: openAIOrgId,}, stream.OnStreamPlan)term.StopSpinner()@@ -117,9 +131,9 @@ func TellPlan(fmt.Println()if tellStop {- term.PrintCmds("", "continue", "changes", "apply", "log", "rewind")+ term.PrintCmds("", "continue", "changes", "diff", "apply", "log", "rewind")} else {- term.PrintCmds("", "changes", "apply", "log", "rewind")+ term.PrintCmds("", "changes", "diff", "apply", "log", "rewind")}os.Exit(0)}()diff --git a/app/cli/term/errors.go b/app/cli/term/errors.goindex f0b0826e..f6920785 100644--- a/app/cli/term/errors.go+++ b/app/cli/term/errors.go@@ -9,7 +9,7 @@ import ("github.com/plandex/plandex/shared")-func OutputNoApiKeyMsgAndExit() {+func OutputNoOpenAIApiKeyMsgAndExit() {fmt.Fprintln(os.Stderr, color.New(color.Bold, ColorHiRed).Sprintln("\n🚨 OPENAI_API_KEY environment variable is not set.")+color.New().Sprintln("\nSet it with:\n\nexport OPENAI_API_KEY=your-api-key\n\nThen try again.\n\n👉 If you don't have an OpenAI account, sign up here → https://platform.openai.com/signup\n\n🔑 Generate an api key here → https://platform.openai.com/api-keys"))os.Exit(1)}diff --git a/app/cli/term/help.go b/app/cli/term/help.goindex 4cb7699d..795621a8 100644--- a/app/cli/term/help.go+++ b/app/cli/term/help.go@@ -11,38 +11,53 @@ import (var CmdDesc = map[string][2]string{"new": {"", "start a new plan"},+ "rename": {"", "rename the current plan"},"current": {"cu", "show current plan"},"cd": {"", "set current plan by name or index"},"load": {"l", "load files, dirs, urls, notes or piped data into context"},"tell": {"t", "describe a task, ask a question, or chat"},- "changes": {"ch", "review plan changes"},- // "diffs": {"d", "show diffs between plan and project files"},+ "changes": {"ch", "review plan changes in a TUI"},+ "diff": {"", "review plan changes in 'git diff' format"},+ "summary": {"", "show the latest summary of the current plan"},// "preview": {"pv", "preview the plan in a branch"},- "apply": {"ap", "apply plan changes to project files"},- "continue": {"c", "continue the plan"},+ "apply": {"ap", "apply plan changes to project files"},+ "archive": {"arc", "archive a plan"},+ "unarchive": {"unarc", "unarchive a plan"},+ "continue": {"c", "continue the plan"},// "status": {"s", "show status of the plan"},- "rewind": {"rw", "rewind to a previous state"},- "ls": {"", "list everything in context"},- "rm": {"", "remove context by name, index, or glob"},- "clear": {"", "remove all context"},- "delete-plan": {"dp", "delete plan by name or index"},- "delete-branch": {"db", "delete a branch by name or index"},- "plans": {"pl", "list plans"},- "update": {"u", "update outdated context"},- "log": {"", "show log of plan updates"},- "convo": {"", "show plan conversation"},- "branches": {"br", "list plan branches"},- "checkout": {"co", "checkout or create a branch"},- "build": {"b", "build any pending changes"},- "models": {"", "show model settings"},- "set-model": {"", "update model settings"},- "ps": {"", "list active and recently finished plan streams"},- "stop": {"", "stop an active plan stream"},- "connect": {"conn", "connect to an active plan stream"},- "sign-in": {"", "sign in, accept an invite, or create an account"},- "invite": {"", "invite a user to join your org"},- "revoke": {"", "revoke an invite or remove a user from your org"},- "users": {"", "list users and pending invites in your org"},+ "rewind": {"rw", "rewind to a previous state"},+ "ls": {"", "list everything in context"},+ "rm": {"", "remove context by name, index, or glob"},+ "clear": {"", "remove all context"},+ "delete-plan": {"dp", "delete plan by name or index"},+ "delete-branch": {"db", "delete a branch by name or index"},+ "plans": {"pl", "list plans"},+ "plans --archived": {"", "list archived plans"},+ "update": {"u", "update outdated context"},+ "log": {"", "show log of plan updates"},+ "convo": {"", "show plan conversation"},+ "branches": {"br", "list plan branches"},+ "checkout": {"co", "checkout or create a branch"},+ "build": {"b", "build any pending changes"},+ "models": {"", "show current plan model settings"},+ "models default": {"", "show org-wide default model settings for new plans"},+ "models available": {"", "show all available models"},+ "models available --custom": {"", "show available custom models only"},+ "models delete": {"", "delete a custom model"},+ "models add": {"", "add a custom model"},+ "model-packs": {"", "show all available model packs"},+ "model-packs create": {"", "create a new custom model pack"},+ "model-packs delete": {"", "delete a custom model pack"},+ "model-packs --custom": {"", "show custom model packs only"},+ "set-model": {"", "update current plan model settings"},+ "set-model default": {"", "update org-wide default model settings for new plans"},+ "ps": {"", "list active and recently finished plan streams"},+ "stop": {"", "stop an active plan stream"},+ "connect": {"conn", "connect to an active plan stream"},+ "sign-in": {"", "sign in, accept an invite, or create an account"},+ "invite": {"", "invite a user to join your org"},+ "revoke": {"", "revoke an invite or remove a user from your org"},+ "users": {"", "list users and pending invites in your org"},}func PrintCmds(prefix string, cmds ...string) {@@ -108,15 +123,15 @@ func PrintCustomHelp() {fmt.Fprintf(builder, " Create a new plan in your project's root directory with %s\n\n", color.New(color.Bold, color.BgCyan, color.FgHiWhite).Sprint(" plandex new "))color.New(color.Bold, color.BgMagenta, color.FgHiWhite).Fprintln(builder, " Key Commands ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiMagenta}, "new", "load", "tell", "changes", "apply")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiMagenta}, "new", "load", "tell", "changes", "diffs", "apply")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Plans ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "new", "plans", "cd", "current", "delete-plan")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "new", "plans", "cd", "current", "delete-plan", "rename", "archive", "plans --archived", "unarchive")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Changes ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "changes", "apply")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "changes", "diff", "apply")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Context ")@@ -140,7 +155,7 @@ func PrintCustomHelp() {fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " AI Models ")- printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "models", "set-model")+ printCmds(builder, " ", []color.Attribute{color.Bold, ColorHiCyan}, "models", "models default", "models available", "set-model", "set-model default", "models available --custom", "models add", "models delete", "model-packs", "model-packs --custom", "model-packs create", "model-packs delete")fmt.Fprintln(builder)color.New(color.Bold, color.BgCyan, color.FgHiWhite).Fprintln(builder, " Accounts ")diff --git a/app/cli/term/prompt.go b/app/cli/term/prompt.goindex 1d2bbe01..e2551638 100644--- a/app/cli/term/prompt.go+++ b/app/cli/term/prompt.go@@ -11,7 +11,11 @@ import ()func GetUserStringInput(msg string) (string, error) {- res, err := prompt.New().Ask(msg).Input("")+ return GetUserStringInputWithDefault(msg, "")+}++func GetUserStringInputWithDefault(msg, def string) (string, error) {+ res, err := prompt.New().Ask(msg).Input(def)if err != nil && err.Error() == "user quit prompt" {os.Exit(0)diff --git a/app/cli/types/api.go b/app/cli/types/api.goindex 79453ea6..d00c542d 100644--- a/app/cli/types/api.go+++ b/app/cli/types/api.go@@ -60,11 +60,14 @@ type ApiClient interface {StopPlan(planId, branch string) *shared.ApiErrorArchivePlan(planId string) *shared.ApiError+ UnarchivePlan(planId string) *shared.ApiError+ RenamePlan(planId string, name string) *shared.ApiErrorGetCurrentPlanState(planId, branch string) (*shared.CurrentPlanState, *shared.ApiError)- ApplyPlan(planId, branch string) *shared.ApiError+ ApplyPlan(planId, branch string, req shared.ApplyPlanRequest) (string, *shared.ApiError)RejectAllChanges(planId, branch string) *shared.ApiErrorRejectFile(planId, branch, filePath string) *shared.ApiError+ GetPlanDiffs(planId, branch string) (string, *shared.ApiError)LoadContext(planId, branch string, req shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError)UpdateContext(planId, branch string, req shared.UpdateContextRequest) (*shared.UpdateContextResponse, *shared.ApiError)@@ -72,6 +75,7 @@ type ApiClient interface {ListContext(planId, branch string) ([]*shared.Context, *shared.ApiError)ListConvo(planId, branch string) ([]*shared.ConvoMessage, *shared.ApiError)+ GetPlanStatus(planId, branch string) (string, *shared.ApiError)ListLogs(planId, branch string) (*shared.LogResponse, *shared.ApiError)RewindPlan(planId, branch string, req shared.RewindPlanRequest) (*shared.RewindPlanResponse, *shared.ApiError)@@ -81,14 +85,15 @@ type ApiClient interface {GetSettings(planId, branch string) (*shared.PlanSettings, *shared.ApiError)UpdateSettings(planId, branch string, req shared.UpdateSettingsRequest) (*shared.UpdateSettingsResponse, *shared.ApiError)- // Custom models management- CreateCustomModel(model shared.CustomModel) *shared.ApiError- ListCustomModels() ([]shared.CustomModel, *shared.ApiError)- DeleteCustomModel(modelId string) *shared.ApiError-- // Model sets management- CreateModelSet(set shared.ModelSet) *shared.ApiError- ListModelSets() ([]shared.ModelSet, *shared.ApiError)- DeleteModelSet(setId string) *shared.ApiError-}+ GetOrgDefaultSettings() (*shared.PlanSettings, *shared.ApiError)+ UpdateOrgDefaultSettings(req shared.UpdateSettingsRequest) (*shared.UpdateSettingsResponse, *shared.ApiError)++ CreateCustomModel(model *shared.AvailableModel) *shared.ApiError+ ListCustomModels() ([]*shared.AvailableModel, *shared.ApiError)+ DeleteAvailableModel(modelId string) *shared.ApiError++ CreateModelPack(set *shared.ModelPack) *shared.ApiError+ ListModelPacks() ([]*shared.ModelPack, *shared.ApiError)+ DeleteModelPack(setId string) *shared.ApiError+}diff --git a/app/server/db/ai_model_helpers.go b/app/server/db/ai_model_helpers.gonew file mode 100644index 00000000..3a49c63e--- /dev/null+++ b/app/server/db/ai_model_helpers.go@@ -0,0 +1 @@+package dbdiff --git a/app/server/db/auth_helpers.go b/app/server/db/auth_helpers.goindex 1f788d4d..c9019671 100644--- a/app/server/db/auth_helpers.go+++ b/app/server/db/auth_helpers.go@@ -8,12 +8,13 @@ import ("time""github.com/google/uuid"+ "github.com/jmoiron/sqlx""github.com/pkg/errors")const tokenExpirationDays = 90 // (trial tokens don't expire)-func CreateAuthToken(userId string, isTrial bool, tx *sql.Tx) (token, id string, err error) {+func CreateAuthToken(userId string, isTrial bool, tx *sqlx.Tx) (token, id string, err error) {uid := uuid.New()bytes := uid[:]hashBytes := sha256.Sum256(bytes)diff --git a/app/server/db/branch_helpers.go b/app/server/db/branch_helpers.goindex e366deb5..d53b3cc2 100644--- a/app/server/db/branch_helpers.go+++ b/app/server/db/branch_helpers.go@@ -5,11 +5,12 @@ import ("fmt""log"+ "github.com/jmoiron/sqlx""github.com/lib/pq""github.com/plandex/plandex/shared")-func CreateBranch(plan *Plan, parentBranch *Branch, name string, tx *sql.Tx) (*Branch, error) {+func CreateBranch(plan *Plan, parentBranch *Branch, name string, tx *sqlx.Tx) (*Branch, error) {query := `INSERT INTO branches (org_id, owner_id, plan_id, parent_branch_id, name, status, context_tokens, convo_tokens)VALUES ($1, $2, $3, $4, $5, $6, $7, $8)@@ -160,7 +161,11 @@ func ListBranchesForPlans(orgId string, planIds []string) ([]*Branch, error) {}func DeleteBranch(orgId, planId, branch string) error {- tx, err := Conn.Begin()+ tx, err := Conn.Beginx()++ if err != nil {+ return fmt.Errorf("error starting transaction: %v", err)+ }// Ensure that rollback is attempted in case of failuredefer func() {@@ -173,10 +178,6 @@ func DeleteBranch(orgId, planId, branch string) error {}}()- if err != nil {- return fmt.Errorf("error starting transaction: %v", err)- }-_, err = tx.Exec("DELETE FROM branches WHERE plan_id = $1 AND name = $2", planId, branch)if err != nil {diff --git a/app/server/db/build_helpers.go b/app/server/db/build_helpers.goindex 00aa604e..b99df0ae 100644--- a/app/server/db/build_helpers.go+++ b/app/server/db/build_helpers.go@@ -22,12 +22,6 @@ func StorePlanBuild(build *PlanBuild) error {}defer row.Close()- if err != nil {- return fmt.Errorf("error storing plan build: %v", err)- }-- defer row.Close()-if row.Next() {var createdAt, updatedAt time.Timevar id stringdiff --git a/app/server/db/data_models.go b/app/server/db/data_models.goindex 6691a0a3..410b9f9a 100644--- a/app/server/db/data_models.go+++ b/app/server/db/data_models.go@@ -289,6 +289,88 @@ type repoLock struct {CreatedAt time.Time `db:"created_at"`}+type ModelPack struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ Name string `db:"name"`+ Description string `db:"description"`+ Planner shared.PlannerRoleConfig `db:"planner"`+ PlanSummary shared.ModelRoleConfig `db:"plan_summary"`+ Builder shared.ModelRoleConfig `db:"builder"`+ Namer shared.ModelRoleConfig `db:"namer"`+ CommitMsg shared.ModelRoleConfig `db:"commit_msg"`+ ExecStatus shared.ModelRoleConfig `db:"exec_status"`+ CreatedAt time.Time `db:"created_at"`+}++func (modelPack *ModelPack) ToApi() *shared.ModelPack {+ return &shared.ModelPack{+ Id: modelPack.Id,+ Name: modelPack.Name,+ Description: modelPack.Description,+ Planner: modelPack.Planner,+ PlanSummary: modelPack.PlanSummary,+ Builder: modelPack.Builder,+ Namer: modelPack.Namer,+ CommitMsg: modelPack.CommitMsg,+ ExecStatus: modelPack.ExecStatus,+ }+}++type AvailableModel struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ Provider shared.ModelProvider `db:"provider"`+ CustomProvider *string `db:"custom_provider"`+ BaseUrl string `db:"base_url"`+ ModelName string `db:"model_name"`+ Description string `db:"description"`+ MaxTokens int `db:"max_tokens"`+ ApiKeyEnvVar string `db:"api_key_env_var"`+ IsOpenAICompatible bool `db:"is_openai_compatible"`+ HasJsonResponseMode bool `db:"has_json_mode"`+ HasStreaming bool `db:"has_streaming"`+ HasFunctionCalling bool `db:"has_function_calling"`+ HasStreamingFunctionCalls bool `db:"has_streaming_function_calls"`+ DefaultMaxConvoTokens int `db:"default_max_convo_tokens"`+ DefaultReservedOutputTokens int `db:"default_reserved_output_tokens"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++func (model *AvailableModel) ToApi() *shared.AvailableModel {+ return &shared.AvailableModel{+ Id: model.Id,+ BaseModelConfig: shared.BaseModelConfig{+ Provider: model.Provider,+ CustomProvider: model.CustomProvider,+ BaseUrl: model.BaseUrl,+ ModelName: model.ModelName,+ MaxTokens: model.MaxTokens,+ ApiKeyEnvVar: model.ApiKeyEnvVar,+ ModelCompatibility: shared.ModelCompatibility{+ IsOpenAICompatible: model.IsOpenAICompatible,+ HasJsonResponseMode: model.HasJsonResponseMode,+ HasStreaming: model.HasStreaming,+ HasFunctionCalling: model.HasFunctionCalling,+ HasStreamingFunctionCalls: model.HasStreamingFunctionCalls,+ }},+ Description: model.Description,+ DefaultMaxConvoTokens: model.DefaultMaxConvoTokens,+ DefaultReservedOutputTokens: model.DefaultReservedOutputTokens,+ CreatedAt: model.CreatedAt,+ UpdatedAt: model.UpdatedAt,+ }+}++type DefaultPlanSettings struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ PlanSettings shared.PlanSettings `db:"plan_settings"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}+// Models below are stored in files, not in the database.// This allows us to store them in a git repo and use git to manage history.@@ -386,34 +468,38 @@ func (desc *ConvoMessageDescription) ToApi() *shared.ConvoMessageDescription {}type PlanFileResult struct {- Id string `json:"id"`- OrgId string `json:"orgId"`- PlanId string `json:"planId"`- ConvoMessageId string `json:"convoMessageId"`- PlanBuildId string `json:"planBuildId"`- Path string `json:"path"`- Content string `json:"content,omitempty"`- Replacements []*shared.Replacement `json:"replacements"`- AnyFailed bool `json:"anyFailed"`- Error string `json:"error"`- AppliedAt *time.Time `json:"appliedAt,omitempty"`- RejectedAt *time.Time `json:"rejectedAt,omitempty"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ TypeVersion int `json:"typeVersion"`+ ReplaceWithLineNums bool `json:"replaceWithLineNums"`+ OrgId string `json:"orgId"`+ PlanId string `json:"planId"`+ ConvoMessageId string `json:"convoMessageId"`+ PlanBuildId string `json:"planBuildId"`+ Path string `json:"path"`+ Content string `json:"content,omitempty"`+ Replacements []*shared.Replacement `json:"replacements"`+ AnyFailed bool `json:"anyFailed"`+ Error string `json:"error"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}func (res *PlanFileResult) ToApi() *shared.PlanFileResult {return &shared.PlanFileResult{- Id: res.Id,- PlanBuildId: res.PlanBuildId,- ConvoMessageId: res.ConvoMessageId,- Path: res.Path,- Content: res.Content,- AnyFailed: res.AnyFailed,- AppliedAt: res.AppliedAt,- RejectedAt: res.RejectedAt,- Replacements: res.Replacements,- CreatedAt: res.CreatedAt,- UpdatedAt: res.UpdatedAt,+ Id: res.Id,+ TypeVersion: res.TypeVersion,+ ReplaceWithLineNums: res.ReplaceWithLineNums,+ PlanBuildId: res.PlanBuildId,+ ConvoMessageId: res.ConvoMessageId,+ Path: res.Path,+ Content: res.Content,+ AnyFailed: res.AnyFailed,+ AppliedAt: res.AppliedAt,+ RejectedAt: res.RejectedAt,+ Replacements: res.Replacements,+ CreatedAt: res.CreatedAt,+ UpdatedAt: res.UpdatedAt,}}diff --git a/app/server/db/db.go b/app/server/db/db.goindex f77469e1..e5ec51d0 100644--- a/app/server/db/db.go+++ b/app/server/db/db.go@@ -73,7 +73,7 @@ func MigrationsUp() error {// Uncomment below (and update migration version) to reset migration state to a specific version after a failure// if os.Getenv("GOENV") == "development" {- // migrateVersion := 2024040400+ // migrateVersion := 2024041500// if err := m.Force(migrateVersion); err != nil {// return fmt.Errorf("error forcing migration version: %v", err)// }diff --git a/app/server/db/diff_helpers.go b/app/server/db/diff_helpers.gonew file mode 100644index 00000000..36d59961--- /dev/null+++ b/app/server/db/diff_helpers.go@@ -0,0 +1,128 @@+package db++import (+ "fmt"+ "os"+ "os/exec"+ "path/filepath"++ "github.com/plandex/plandex/shared"+)++func GetPlanDiffs(orgId, planId string) (string, error) {+ planState, err := GetCurrentPlanState(CurrentPlanStateParams{+ OrgId: orgId,+ PlanId: planId,+ })++ if err != nil {+ return "", fmt.Errorf("error getting current plan state: %v", err)+ }++ // create temp directory+ tempDirPath, err := os.MkdirTemp(getOrgDir(orgId), "tmp-diffs-*")++ if err != nil {+ return "", fmt.Errorf("error creating temp dir: %v", err)+ }++ defer func() {+ go os.RemoveAll(tempDirPath)+ }()++ // init a git repo in the temp dir+ err = initGitRepo(tempDirPath)++ if err != nil {+ return "", fmt.Errorf("error initializing git repo: %v", err)+ }++ files := planState.CurrentPlanFiles.Files++ // write the original files to the temp dir+ errCh := make(chan error, len(planState.ContextsByPath))+ hasAnyOriginal := false++ for path, context := range planState.ContextsByPath {+ go func(path string, context *shared.Context) {+ _, hasPath := files[path]+ if hasPath {+ hasAnyOriginal = true+ // ensure file directory exists+ err = os.MkdirAll(filepath.Dir(filepath.Join(tempDirPath, path)), 0755)+ if err != nil {+ errCh <- fmt.Errorf("error creating directory: %v", err)+ return+ }++ err = os.WriteFile(filepath.Join(tempDirPath, path), []byte(context.Body), 0644)+ if err != nil {+ errCh <- fmt.Errorf("error writing file: %v", err)+ return+ }+ }+ errCh <- nil+ }(path, context)+ }++ for range planState.ContextsByPath {+ err = <-errCh+ if err != nil {+ return "", fmt.Errorf("error writing original files to temp dir: %v", err)+ }+ }++ if hasAnyOriginal {+ // add and commit the files in the temp dir+ err := gitAdd(tempDirPath, ".")+ if err != nil {+ return "", fmt.Errorf("error adding files to git repository for dir: %s, err: %v", tempDirPath, err)+ }++ err = gitCommit(tempDirPath, "original files")+ if err != nil {+ return "", fmt.Errorf("error committing files to git repository for dir: %s, err: %v", tempDirPath, err)+ }+ }++ // write the current files to the temp dir+ errCh = make(chan error, len(files))++ for path, file := range files {+ go func(path, file string) {+ // ensure file directory exists+ err = os.MkdirAll(filepath.Dir(filepath.Join(tempDirPath, path)), 0755)+ if err != nil {+ errCh <- fmt.Errorf("error creating directory: %v", err)+ return+ }++ err = os.WriteFile(filepath.Join(tempDirPath, path), []byte(file), 0644)+ if err != nil {+ errCh <- fmt.Errorf("error writing file: %v", err)+ return+ }+ errCh <- nil+ }(path, file)+ }++ for range files {+ err = <-errCh+ if err != nil {+ return "", fmt.Errorf("error writing current files to temp dir: %v", err)+ }+ }++ err = gitAdd(tempDirPath, ".")+ if err != nil {+ return "", fmt.Errorf("error adding files to git repository for dir: %s, err: %v", tempDirPath, err)+ }++ res, err := exec.Command("git", "-C", tempDirPath, "diff", "--cached", "--color=always").CombinedOutput()++ if err != nil {+ return "", fmt.Errorf("error getting diffs: %v", err)+ }++ return string(res), nil+}diff --git a/app/server/db/fs.go b/app/server/db/fs.goindex 10a1ea8d..b66a7235 100644--- a/app/server/db/fs.go+++ b/app/server/db/fs.go@@ -64,8 +64,12 @@ func DeletePlanDir(orgId, planId string) error {return nil}+func getOrgDir(orgId string) string {+ return filepath.Join(BaseDir, "orgs", orgId)+}+func getPlanDir(orgId, planId string) string {- return filepath.Join(BaseDir, "orgs", orgId, "plans", planId)+ return filepath.Join(getOrgDir(orgId), "plans", planId)}func getPlanContextDir(orgId, planId string) string {diff --git a/app/server/db/git.go b/app/server/db/git.goindex 7b8c29a7..a04b336e 100644--- a/app/server/db/git.go+++ b/app/server/db/git.go@@ -24,7 +24,10 @@ func init() {func InitGitRepo(orgId, planId string) error {dir := getPlanDir(orgId, planId)+ return initGitRepo(dir)+}+func initGitRepo(dir string) error {// Set the default branch name to 'main' for the new repositoryres, err := exec.Command("git", "-C", dir, "init", "-b", "main").CombinedOutput()if err != nil {@@ -330,6 +333,9 @@ func gitRemoveIndexLockFileIfExists(repoDir string) error {lockFilePath := filepath.Join(repoDir, ".git", "index.lock")_, err := os.Stat(lockFilePath)+ // log.Println("lockFilePath:", lockFilePath)+ // log.Println("exists:", err == nil)+if err == nil {if err := os.Remove(lockFilePath); err != nil {return fmt.Errorf("error removing lock file: %v", err)diff --git a/app/server/db/invite_helpers.go b/app/server/db/invite_helpers.goindex e3e1d491..c1361548 100644--- a/app/server/db/invite_helpers.go+++ b/app/server/db/invite_helpers.go@@ -5,9 +5,11 @@ import ("fmt""log""strings"++ "github.com/jmoiron/sqlx")-func CreateInvite(invite *Invite, tx *sql.Tx) error {+func CreateInvite(invite *Invite, tx *sqlx.Tx) error {err := tx.QueryRow("INSERT INTO invites (org_id, email, name, inviter_id, org_role_id) VALUES ($1, $2, $3, $4, $5) RETURNING id", invite.OrgId, invite.Email, invite.Name, invite.InviterId, invite.OrgRoleId).Scan(&invite.Id)if err != nil {@@ -95,7 +97,7 @@ func GetPendingInvitesForEmail(email string) ([]*Invite, error) {return invites, nil}-func DeleteInvite(id string, tx *sql.Tx) error {+func DeleteInvite(id string, tx *sqlx.Tx) error {query := "DELETE FROM invites WHERE id = $1"var err error@@ -114,7 +116,7 @@ func DeleteInvite(id string, tx *sql.Tx) error {func AcceptInvite(invite *Invite, inviteeId string) error {// start a transaction- tx, err := Conn.Begin()+ tx, err := Conn.Beginx()if err != nil {return fmt.Errorf("error starting transaction: %v", err)}diff --git a/app/server/db/locks.go b/app/server/db/locks.goindex 975a0513..8e9d39ee 100644--- a/app/server/db/locks.go+++ b/app/server/db/locks.go@@ -5,6 +5,7 @@ import ("database/sql""fmt""log"+ "math/rand""time""github.com/lib/pq"@@ -45,7 +46,7 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {ctx := params.CtxcancelFn := params.CancelFn- tx, err := Conn.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})+ tx, err := Conn.BeginTxx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})if err != nil {return "", fmt.Errorf("error starting transaction: %v", err)}@@ -69,6 +70,11 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {fn := func() error {rows, err := tx.Query(query, queryArgs...)if err != nil {+ if pqErr, ok := err.(*pq.Error); ok && (pqErr.Code == "40001" || pqErr.Code == "40P01") {+ // return concurrency errors directly for retries+ return err+ }+return fmt.Errorf("error getting repo locks: %v", err)}@@ -101,7 +107,26 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {return nil}- if err := fn(); err != nil {+ err = fn()+ if err != nil {+ if pqErr, ok := err.(*pq.Error); ok && (pqErr.Code == "40001" || pqErr.Code == "40P01") {+ if numRetry > 10 {+ err = fmt.Errorf("plan is currently being updated by another user")+ return "", err+ }++ log.Printf("Serialization or deadlock error, retrying transaction: %v\n", err)++ wait := time.Duration(numRetry) * 300 * time.Millisecond * time.Duration(rand.Intn(500)*int(time.Millisecond))++ select {+ case <-ctx.Done():+ return "", fmt.Errorf("context finished during retry transaction")+ case <-time.After(wait):+ return lockRepo(params, numRetry+1)+ }+ }+return "", err}diff --git a/app/server/db/models.go b/app/server/db/models.goindex 8ebce669..4921138b 100644--- a/app/server/db/models.go+++ b/app/server/db/models.go@@ -1,76 +1,80 @@package dbimport (- "context"- "database/sql"- "github.com/plandex/plandex/shared"- "log"+ "fmt")-// CreateCustomModel inserts a new custom model into the database.-func CreateCustomModel(ctx context.Context, db *sql.DB, model shared.CustomModel) error {- query := `INSERT INTO custom_models (org_id, provider, custom_provider, base_url, model_name, description, max_tokens, api_key_env_var, is_openai_compatible, has_json_mode, has_streaming, has_function_calling, has_streaming_function_calls, default_max_convo_tokens, default_reserved_output_tokens) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)`- _, err := db.ExecContext(ctx, query, model.OrgId, model.Provider, model.CustomProvider, model.BaseUrl, model.ModelName, model.Description, model.MaxTokens, model.ApiKeyEnvVar, model.IsOpenAICompatible, model.HasJsonResponseMode, model.HasStreaming, model.HasFunctionCalling, model.HasStreamingFunctionCalls, model.DefaultMaxConvoTokens, model.DefaultReservedOutputTokens)- return err+func CreateCustomModel(model *AvailableModel) error {+ query := `INSERT INTO custom_models (org_id, provider, custom_provider, base_url, model_name, description, max_tokens, api_key_env_var, is_openai_compatible, has_json_mode, has_streaming, has_function_calling, has_streaming_function_calls, default_max_convo_tokens, default_reserved_output_tokens) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)+ RETURNING id, created_at, updated_at`++ err := Conn.QueryRow(query, model.OrgId, model.Provider, model.CustomProvider, model.BaseUrl, model.ModelName, model.Description, model.MaxTokens, model.ApiKeyEnvVar, model.IsOpenAICompatible, model.HasJsonResponseMode, model.HasStreaming, model.HasFunctionCalling, model.HasStreamingFunctionCalls, model.DefaultMaxConvoTokens, model.DefaultReservedOutputTokens).Scan(&model.Id, &model.CreatedAt, &model.UpdatedAt)++ if err != nil {+ return fmt.Errorf("error inserting new custom model: %v", err)+ }++ return nil}-// ListCustomModels retrieves all custom models from the database.-func ListCustomModels(ctx context.Context, db *sql.DB, orgId string) ([]shared.CustomModel, error) {+func ListCustomModels(orgId string) ([]*AvailableModel, error) {+ var models []*AvailableModel+query := `SELECT * FROM custom_models WHERE org_id = $1`- rows, err := db.QueryContext(ctx, query, orgId)++ err := Conn.Select(&models, query, orgId)+if err != nil {- return nil, err- }- defer rows.Close()-- var models []shared.CustomModel- for rows.Next() {- var model shared.CustomModel- if err := rows.Scan(&model.Id, &model.OrgId, &model.Provider, &model.CustomProvider, &model.BaseUrl, &model.ModelName, &model.Description, &model.MaxTokens, &model.ApiKeyEnvVar, &model.IsOpenAICompatible, &model.HasJsonResponseMode, &model.HasStreaming, &model.HasFunctionCalling, &model.HasStreamingFunctionCalls, &model.DefaultMaxConvoTokens, &model.DefaultReservedOutputTokens, &model.CreatedAt, &model.UpdatedAt); err != nil {- return nil, err- }- models = append(models, model)+ return nil, fmt.Errorf("error fetching custom models: %v", err)}+return models, nil}-// DeleteCustomModel removes a custom model from the database.-func DeleteCustomModel(ctx context.Context, db *sql.DB, modelId string) error {+func DeleteAvailableModel(modelId string) error {query := `DELETE FROM custom_models WHERE id = $1`- _, err := db.ExecContext(ctx, query, modelId)- return err+ _, err := Conn.Exec(query, modelId)++ if err != nil {+ return fmt.Errorf("error deleting custom model: %v", err)+ }++ return nil}-// CreateModelSet inserts a new model set into the database.-func CreateModelSet(ctx context.Context, db *sql.DB, set shared.ModelSet) error {- query := `INSERT INTO model_sets (org_id, name, description, planner, plan_summary, builder, namer, commit_msg, exec_status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`- _, err := db.ExecContext(ctx, query, set.OrgId, set.Name, set.Description, set.Planner, set.PlanSummary, set.Builder, set.Namer, set.CommitMsg, set.ExecStatus)- return err+func CreateModelPack(ms *ModelPack) error {+ query := `INSERT INTO model_sets (org_id, name, description, planner, plan_summary, builder, namer, commit_msg, exec_status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)+ RETURNING id, created_at`++ err := Conn.QueryRow(query, ms.OrgId, ms.Name, ms.Description, ms.Planner, ms.PlanSummary, ms.Builder, ms.Namer, ms.CommitMsg, ms.ExecStatus).Scan(&ms.Id, &ms.CreatedAt)++ if err != nil {+ return fmt.Errorf("error inserting new model pack: %v", err)+ }++ return nil}-// ListModelSets retrieves all model sets from the database.-func ListModelSets(ctx context.Context, db *sql.DB, orgId string) ([]shared.ModelSet, error) {+func ListModelPacks(orgId string) ([]*ModelPack, error) {+ var modelPacks []*ModelPack+query := `SELECT * FROM model_sets WHERE org_id = $1`- rows, err := db.QueryContext(ctx, query, orgId)+ err := Conn.Select(&modelPacks, query, orgId)+if err != nil {- return nil, err+ return nil, fmt.Errorf("error fetching model packs: %v", err)}- defer rows.Close()-- var sets []shared.ModelSet- for rows.Next() {- var set shared.ModelSet- if err := rows.Scan(&set.Id, &set.OrgId, &set.Name, &set.Description, &set.Planner, &set.PlanSummary, &set.Builder, &set.Namer, &set.CommitMsg, &set.ExecStatus, &set.CreatedAt); err != nil {- return nil, err- }- sets = append(sets, set)- }- return sets, nil++ return modelPacks, nil}-// DeleteModelSet removes a model set from the database.-func DeleteModelSet(ctx context.Context, db *sql.DB, setId string) error {+func DeleteModelPack(setId string) error {query := `DELETE FROM model_sets WHERE id = $1`- _, err := db.ExecContext(ctx, query, setId)- return err+ _, err := Conn.Exec(query, setId)++ if err != nil {+ return fmt.Errorf("error deleting model pack: %v", err)+ }++ return nil}diff --git a/app/server/db/org_helpers.go b/app/server/db/org_helpers.goindex 46d31fe4..51380282 100644--- a/app/server/db/org_helpers.go+++ b/app/server/db/org_helpers.go@@ -6,6 +6,7 @@ import ("log""strings"+ "github.com/jmoiron/sqlx""github.com/lib/pq""github.com/plandex/plandex/shared")@@ -89,7 +90,7 @@ func ValidateOrgMembership(userId string, orgId string) (bool, error) {return count > 0, nil}-func CreateOrg(req *shared.CreateOrgRequest, userId string, domain *string, tx *sql.Tx) (*Org, error) {+func CreateOrg(req *shared.CreateOrgRequest, userId string, domain *string, tx *sqlx.Tx) (*Org, error) {org := &Org{Name: req.Name,Domain: domain,@@ -139,7 +140,7 @@ func GetOrgForDomain(domain string) (*Org, error) {return &org, nil}-func AddOrgDomainUsers(orgId, domain string, tx *sql.Tx) error {+func AddOrgDomainUsers(orgId, domain string, tx *sqlx.Tx) error {usersForDomain, err := GetUsersForDomain(domain)if err != nil {@@ -174,7 +175,7 @@ func AddOrgDomainUsers(orgId, domain string, tx *sql.Tx) error {return nil}-func DeleteOrgUser(orgId, userId string, tx *sql.Tx) error {+func DeleteOrgUser(orgId, userId string, tx *sqlx.Tx) error {log.Printf("Deleting org user, org: %s | user: %s\n", orgId, userId)_, err := tx.Exec("DELETE FROM orgs_users WHERE org_id = $1 AND user_id = $2", orgId, userId)@@ -186,7 +187,7 @@ func DeleteOrgUser(orgId, userId string, tx *sql.Tx) error {return nil}-func CreateOrgUser(orgId, userId, orgRoleId string, tx *sql.Tx) error {+func CreateOrgUser(orgId, userId, orgRoleId string, tx *sqlx.Tx) error {query := "INSERT INTO orgs_users (org_id, user_id, org_role_id) VALUES ($1, $2, $3)"var err errorif tx == nil {diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.goindex 48e68e2f..12e0863f 100644--- a/app/server/db/plan_helpers.go+++ b/app/server/db/plan_helpers.go@@ -1,7 +1,6 @@package dbimport (- "database/sql""encoding/json""fmt""log"@@ -10,6 +9,7 @@ import ("time""github.com/google/uuid"+ "github.com/jmoiron/sqlx""github.com/lib/pq""github.com/plandex/plandex/shared""github.com/sashabaranov/go-openai"@@ -17,7 +17,7 @@ import (func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {// start a transaction- tx, err := Conn.Begin()+ tx, err := Conn.Beginx()if err != nil {return nil, fmt.Errorf("error starting transaction: %v", err)}@@ -216,8 +216,13 @@ func SetPlanStatus(planId, branch string, status shared.PlanStatus, errStr strinreturn nil}-func RenamePlan(planId string, name string, tx *sql.Tx) error {- _, err := tx.Exec("UPDATE plans SET name = $1 WHERE id = $2", name, planId)+func RenamePlan(planId string, name string, tx *sqlx.Tx) error {+ var err error+ if tx == nil {+ _, err = Conn.Exec("UPDATE plans SET name = $1 WHERE id = $2", name, planId)+ } else {+ _, err = tx.Exec("UPDATE plans SET name = $1 WHERE id = $2", name, planId)+ }if err != nil {return fmt.Errorf("error renaming plan: %v", err)@@ -226,7 +231,7 @@ func RenamePlan(planId string, name string, tx *sql.Tx) error {return nil}-func IncActiveBranches(planId string, inc int, tx *sql.Tx) error {+func IncActiveBranches(planId string, inc int, tx *sqlx.Tx) error {_, err := tx.Exec("UPDATE plans SET active_branches = active_branches + $1 WHERE id = $2", inc, planId)if err != nil {@@ -236,7 +241,7 @@ func IncActiveBranches(planId string, inc int, tx *sql.Tx) error {return nil}-func IncNumNonDraftPlans(userId string, tx *sql.Tx) error {+func IncNumNonDraftPlans(userId string, tx *sqlx.Tx) error {_, err := tx.Exec("UPDATE users SET num_non_draft_plans = num_non_draft_plans + 1 WHERE id = $1", userId)if err != nil {diff --git a/app/server/db/project_helpers.go b/app/server/db/project_helpers.goindex 34f11016..53e31ece 100644--- a/app/server/db/project_helpers.go+++ b/app/server/db/project_helpers.go@@ -1,8 +1,9 @@package dbimport (- "database/sql""fmt"++ "github.com/jmoiron/sqlx")func ProjectExists(orgId, projectId string) (bool, error) {@@ -16,7 +17,7 @@ func ProjectExists(orgId, projectId string) (bool, error) {return count > 0, nil}-func CreateProject(orgId, name string, tx *sql.Tx) (string, error) {+func CreateProject(orgId, name string, tx *sqlx.Tx) (string, error) {var projectId stringerr := tx.QueryRow("INSERT INTO projects (org_id, name) VALUES ($1, $2) RETURNING id", orgId, name).Scan(&projectId)diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex 992bd97c..ebca5957 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -322,7 +322,7 @@ func GetPlanResult(planFileResults []*shared.PlanFileResult) *shared.PlanResult}}-func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {+func ApplyPlan(orgId, userId, branchName string, plan *Plan) (*shared.CurrentPlanState, error) {planId := plan.IdresultsDir := getPlanResultsDir(orgId, planId)@@ -374,7 +374,7 @@ func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {for i := 0; i < 3; i++ {err := <-errChif err != nil {- return err+ return nil, err}}@@ -410,7 +410,7 @@ func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {})if err != nil {- return fmt.Errorf("error getting current plan state: %v", err)+ return nil, fmt.Errorf("error getting current plan state: %v", err)}currentPlanState = res@@ -534,7 +534,7 @@ func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {for i := 0; i < numRoutines; i++ {err := <-errChif err != nil {- return fmt.Errorf("error applying plan: %v", err)+ return nil, fmt.Errorf("error applying plan: %v", err)}}@@ -551,10 +551,10 @@ func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {err := GitAddAndCommit(orgId, plan.Id, branchName, msg)if err != nil {- return fmt.Errorf("error committing plan: %v", err)+ return nil, fmt.Errorf("error committing plan: %v", err)}- return nil+ return currentPlanState, nil}func RejectAllResults(orgId, planId string) error {diff --git a/app/server/db/settings_helpers.go b/app/server/db/settings_helpers.goindex ccf2272a..8165921c 100644--- a/app/server/db/settings_helpers.go+++ b/app/server/db/settings_helpers.go@@ -1,16 +1,18 @@package dbimport (+ "database/sql""encoding/json""fmt""os""path/filepath""time"+ "github.com/jmoiron/sqlx""github.com/plandex/plandex/shared")-func GetPlanSettings(plan *Plan, fillDefaultModelSet bool) (*shared.PlanSettings, error) {+func GetPlanSettings(plan *Plan, fillDefaultModelPack bool) (*shared.PlanSettings, error) {planDir := getPlanDir(plan.OrgId, plan.Id)settingsPath := filepath.Join(planDir, "settings.json")@@ -19,12 +21,23 @@ func GetPlanSettings(plan *Plan, fillDefaultModelSet bool) (*shared.PlanSettingsbytes, err := os.ReadFile(settingsPath)if os.IsNotExist(err) || len(bytes) == 0 {+ // see if org has default settings+ defaultSettings, err := GetOrgDefaultSettings(plan.OrgId, fillDefaultModelPack)++ if err != nil {+ return nil, fmt.Errorf("error getting org default settings: %v", err)+ }++ if defaultSettings != nil {+ return defaultSettings, nil+ }+// if it doesn't exist, return default settings objectsettings = &shared.PlanSettings{UpdatedAt: plan.CreatedAt,}- if settings.ModelSet == nil && fillDefaultModelSet {- settings.ModelSet = &shared.DefaultModelSet+ if settings.ModelPack == nil && fillDefaultModelPack {+ settings.ModelPack = shared.DefaultModelPack}return settings, nil} else if err != nil {@@ -37,8 +50,8 @@ func GetPlanSettings(plan *Plan, fillDefaultModelSet bool) (*shared.PlanSettingsreturn nil, fmt.Errorf("error unmarshalling settings: %v", err)}- if settings.ModelSet == nil && fillDefaultModelSet {- settings.ModelSet = &shared.DefaultModelSet+ if settings.ModelPack == nil && fillDefaultModelPack {+ settings.ModelPack = shared.DefaultModelPack}return settings, nil@@ -70,3 +83,68 @@ func StorePlanSettings(plan *Plan, settings *shared.PlanSettings) error {return nil}++func GetOrgDefaultSettings(orgId string, fillDefaultModelPack bool) (*shared.PlanSettings, error) {+ query := "SELECT * FROM default_plan_settings WHERE org_id = $1"++ var defaults DefaultPlanSettings++ err := Conn.Get(&defaults, query, orgId)++ if err != nil {+ if err == sql.ErrNoRows {+ // if it doesn't exist, return default settings object+ settings := &shared.PlanSettings{+ UpdatedAt: time.Time{},+ }+ if settings.ModelPack == nil && fillDefaultModelPack {+ settings.ModelPack = shared.DefaultModelPack+ }+ return settings, nil+ }+ return nil, fmt.Errorf("error getting default plan settings: %v", err)+ }++ return &defaults.PlanSettings, nil+}++func GetOrgDefaultSettingsForUpdate(orgId string, tx *sqlx.Tx, fillDefaultModelPack bool) (*shared.PlanSettings, error) {+ query := "SELECT * FROM default_plan_settings WHERE org_id = $1 FOR UPDATE"++ var defaults DefaultPlanSettings++ err := tx.Get(&defaults, query, orgId)++ if err != nil {+ if err == sql.ErrNoRows {+ // if it doesn't exist, return default settings object+ settings := &shared.PlanSettings{+ UpdatedAt: time.Time{},+ }+ if settings.ModelPack == nil && fillDefaultModelPack {+ settings.ModelPack = shared.DefaultModelPack+ }+ return settings, nil+ }+ return nil, fmt.Errorf("error getting default plan settings: %v", err)+ }++ return &defaults.PlanSettings, nil+}++func StoreOrgDefaultSettings(orgId string, settings *shared.PlanSettings, tx *sqlx.Tx) error {+ settings.UpdatedAt = time.Now()++ query := `INSERT INTO default_plan_settings (org_id, plan_settings)+ VALUES ($1, $2)+ ON CONFLICT (org_id) DO UPDATE SET plan_settings = excluded.plan_settings+ `++ _, err := tx.Exec(query, orgId, settings)++ if err != nil {+ return fmt.Errorf("error storing default plan settings: %v", err)+ }++ return nil+}diff --git a/app/server/db/user_helpers.go b/app/server/db/user_helpers.goindex de2c7130..4840e6dc 100644--- a/app/server/db/user_helpers.go+++ b/app/server/db/user_helpers.go@@ -4,6 +4,7 @@ import ("database/sql""fmt"+ "github.com/jmoiron/sqlx""github.com/lib/pq")@@ -97,7 +98,7 @@ func ListUsers(orgId string) ([]*User, error) {return users, nil}-func CreateUser(user *User, tx *sql.Tx) error {+func CreateUser(user *User, tx *sqlx.Tx) error {return tx.QueryRow("INSERT INTO users (name, email, domain, is_trial) VALUES ($1, $2, $3, $4) RETURNING id", user.Name, user.Email, user.Domain, user.IsTrial).Scan(&user.Id)}diff --git a/app/server/go.mod b/app/server/go.modindex 4b195d38..058b617c 100644--- a/app/server/go.mod+++ b/app/server/go.mod@@ -38,6 +38,7 @@ require (github.com/golang-migrate/migrate/v4 v4.17.0github.com/jmoiron/sqlx v1.3.5github.com/lib/pq v1.10.9+ golang.org/x/sync v0.7.0)replace github.com/plandex/plandex/shared => ../shareddiff --git a/app/server/go.sum b/app/server/go.sumindex 9c0a02e5..047431e0 100644--- a/app/server/go.sum+++ b/app/server/go.sum@@ -98,6 +98,8 @@ golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=+golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=+golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=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.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=diff --git a/app/server/handlers/accounts.go b/app/server/handlers/accounts.goindex 06ab3d39..3189d928 100644--- a/app/server/handlers/accounts.go+++ b/app/server/handlers/accounts.go@@ -16,7 +16,7 @@ func StartTrialHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for StartTrialHandler")// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)@@ -155,7 +155,7 @@ func CreateAccountHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)@@ -341,7 +341,7 @@ func ConvertTrialHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/branches.go b/app/server/handlers/branches.goindex 3996b5b8..d8d416c1 100644--- a/app/server/handlers/branches.go+++ b/app/server/handlers/branches.go@@ -117,7 +117,7 @@ func CreateBranchHandler(w http.ResponseWriter, r *http.Request) {}()}- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/client_helper.go b/app/server/handlers/client_helper.gonew file mode 100644index 00000000..6256d910--- /dev/null+++ b/app/server/handlers/client_helper.go@@ -0,0 +1,75 @@+package handlers++import (+ "log"+ "net/http"+ "plandex-server/db"+ "plandex-server/model"++ "github.com/sashabaranov/go-openai"+)++type initClientsParams struct {+ w http.ResponseWriter+ apiKey string+ apiKeys map[string]string+ endpoint string+ openAIBase string+ openAIOrgId string+ plan *db.Plan+}++func initClients(params initClientsParams) map[string]*openai.Client {+ w := params.w+ apiKey := params.apiKey+ apiKeys := params.apiKeys+ openAIOrgId := params.openAIOrgId+ plan := params.plan++ endpoint := params.openAIBase+ if endpoint == "" {+ endpoint = params.endpoint+ }+ if apiKeys == nil {+ apiKeys = map[string]string{"OPENAI_API_KEY": apiKey}+ }++ planSettings, err := db.GetPlanSettings(plan, true)+ if err != nil {+ log.Printf("Error getting plan settings: %v\n", err)+ http.Error(w, "Error getting plan settings", http.StatusInternalServerError)+ return nil+ }++ endpointsByApiKeyEnvVar := map[string]string{}+ for envVar := range apiKeys {+ if planSettings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar == envVar {+ endpointsByApiKeyEnvVar[envVar] = planSettings.ModelPack.Planner.BaseModelConfig.BaseUrl+ continue+ }++ if planSettings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar == envVar {+ endpointsByApiKeyEnvVar[envVar] = planSettings.ModelPack.Planner.BaseModelConfig.BaseUrl+ continue+ }++ if planSettings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar == envVar {+ endpointsByApiKeyEnvVar[envVar] = planSettings.ModelPack.Planner.BaseModelConfig.BaseUrl+ continue+ }++ if planSettings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar == envVar {+ endpointsByApiKeyEnvVar[envVar] = planSettings.ModelPack.Planner.BaseModelConfig.BaseUrl+ continue+ }++ if planSettings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar == envVar {+ endpointsByApiKeyEnvVar[envVar] = planSettings.ModelPack.Planner.BaseModelConfig.BaseUrl+ continue+ }+ }++ clients := model.InitClients(apiKeys, endpointsByApiKeyEnvVar, endpoint, openAIOrgId)++ return clients+}diff --git a/app/server/handlers/invites.go b/app/server/handlers/invites.goindex c9f6498c..e19cb840 100644--- a/app/server/handlers/invites.go+++ b/app/server/handlers/invites.go@@ -112,7 +112,7 @@ func InviteUserHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/models.go b/app/server/handlers/models.goindex 99cd72eb..a3523acc 100644--- a/app/server/handlers/models.go+++ b/app/server/handlers/models.go@@ -1,87 +1,180 @@package handlers+import ("encoding/json"+ "log""net/http""plandex-server/db"+"github.com/gorilla/mux""github.com/plandex/plandex/shared")-// CreateCustomModelHandler handles the creation of a new custom model.func CreateCustomModelHandler(w http.ResponseWriter, r *http.Request) {- var model shared.CustomModel+ log.Println("Received request for CreateCustomModelHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ var model shared.AvailableModelif err := json.NewDecoder(r.Body).Decode(&model); err != nil {+ log.Printf("Error decoding request body: %v\n", err)http.Error(w, "Invalid request body", http.StatusBadRequest)return}+ dbModel := &db.AvailableModel{+ Id: model.Id,+ OrgId: auth.OrgId,+ Provider: model.Provider,+ CustomProvider: model.CustomProvider,+ BaseUrl: model.BaseUrl,+ ModelName: model.ModelName,+ MaxTokens: model.MaxTokens,+ ApiKeyEnvVar: model.ApiKeyEnvVar,+ IsOpenAICompatible: model.IsOpenAICompatible,+ HasJsonResponseMode: model.HasJsonResponseMode,+ HasStreaming: model.HasStreaming,+ HasFunctionCalling: model.HasFunctionCalling,+ HasStreamingFunctionCalls: model.HasStreamingFunctionCalls,+ DefaultMaxConvoTokens: model.DefaultMaxConvoTokens,+ DefaultReservedOutputTokens: model.DefaultReservedOutputTokens,+ }- if err := db.CreateCustomModel(r.Context(), model); err != nil {- http.Error(w, "Failed to create custom model", http.StatusInternalServerError)+ if err := db.CreateCustomModel(dbModel); err != nil {+ log.Printf("Error creating custom model: %v\n", err)+ http.Error(w, "Failed to create custom model: "+err.Error(), http.StatusInternalServerError)return}w.WriteHeader(http.StatusCreated)++ log.Println("Successfully created custom model")}-// ListCustomModelsHandler handles listing all custom models.func ListCustomModelsHandler(w http.ResponseWriter, r *http.Request) {- orgId := r.URL.Query().Get("orgId")- models, err := db.ListCustomModels(r.Context(), orgId)+ log.Println("Received request for ListCustomModelsHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ models, err := db.ListCustomModels(auth.OrgId)if err != nil {- http.Error(w, "Failed to fetch custom models", http.StatusInternalServerError)+ log.Printf("Error fetching custom models: %v\n", err)+ http.Error(w, "Failed to fetch custom models: "+err.Error(), http.StatusInternalServerError)return}json.NewEncoder(w).Encode(models)++ log.Println("Successfully fetched custom models")}-// DeleteCustomModelHandler handles the deletion of a custom model.-func DeleteCustomModelHandler(w http.ResponseWriter, r *http.Request) {+func DeleteAvailableModelHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for DeleteAvailableModelHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }+modelId := mux.Vars(r)["modelId"]- if err := db.DeleteCustomModel(r.Context(), modelId); err != nil {- http.Error(w, "Failed to delete custom model", http.StatusInternalServerError)+ if err := db.DeleteAvailableModel(modelId); err != nil {+ log.Printf("Error deleting custom model: %v\n", err)+ http.Error(w, "Failed to delete custom model: "+err.Error(), http.StatusInternalServerError)return}w.WriteHeader(http.StatusOK)++ log.Println("Successfully deleted custom model")}-// CreateModelSetHandler handles the creation of a new model set.-func CreateModelSetHandler(w http.ResponseWriter, r *http.Request) {- var set shared.ModelSet- if err := json.NewDecoder(r.Body).Decode(&set); err != nil {+func CreateModelPackHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for CreateModelPackHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ var ms shared.ModelPack+ if err := json.NewDecoder(r.Body).Decode(&ms); err != nil {http.Error(w, "Invalid request body", http.StatusBadRequest)return}- if err := db.CreateModelSet(r.Context(), set); err != nil {- http.Error(w, "Failed to create model set", http.StatusInternalServerError)+ dbMs := &db.ModelPack{+ OrgId: auth.OrgId,+ Name: ms.Name,+ Description: ms.Description,+ Planner: ms.Planner,+ PlanSummary: ms.PlanSummary,+ Builder: ms.Builder,+ Namer: ms.Namer,+ CommitMsg: ms.CommitMsg,+ ExecStatus: ms.ExecStatus,+ }++ if err := db.CreateModelPack(dbMs); err != nil {+ log.Printf("Error creating model pack: %v\n", err)+ http.Error(w, "Failed to create model pack: "+err.Error(), http.StatusInternalServerError)return}w.WriteHeader(http.StatusCreated)++ log.Println("Successfully created model pack")}-// ListModelSetsHandler handles listing all model sets.-func ListModelSetsHandler(w http.ResponseWriter, r *http.Request) {- orgId := r.URL.Query().Get("orgId")- sets, err := db.ListModelSets(r.Context(), orgId)+func ListModelPacksHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListModelPacksHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ sets, err := db.ListModelPacks(auth.OrgId)if err != nil {- http.Error(w, "Failed to fetch model sets", http.StatusInternalServerError)+ log.Printf("Error fetching model packs: %v\n", err)+ http.Error(w, "Failed to fetch model packs: "+err.Error(), http.StatusInternalServerError)return}- json.NewEncoder(w).Encode(sets)+ var apiPacks []*shared.ModelPack++ for _, mp := range sets {+ apiPacks = append(apiPacks, mp.ToApi())+ }++ json.NewEncoder(w).Encode(apiPacks)++ log.Println("Successfully fetched model packs")}-// DeleteModelSetHandler handles the deletion of a model set.-func DeleteModelSetHandler(w http.ResponseWriter, r *http.Request) {+func DeleteModelPackHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for DeleteModelPackHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }+setId := mux.Vars(r)["setId"]- if err := db.DeleteModelSet(r.Context(), setId); err != nil {- http.Error(w, "Failed to delete model set", http.StatusInternalServerError)++ log.Printf("Deleting model pack with id: %s\n", setId)++ if err := db.DeleteModelPack(setId); err != nil {+ log.Printf("Error deleting model pack: %v\n", err)+ http.Error(w, "Failed to delete model pack: "+err.Error(), http.StatusInternalServerError)return}w.WriteHeader(http.StatusOK)-}+ log.Println("Successfully deleted model pack")+}diff --git a/app/server/handlers/orgs.go b/app/server/handlers/orgs.goindex 314df992..edcfa377 100644--- a/app/server/handlers/orgs.go+++ b/app/server/handlers/orgs.go@@ -79,7 +79,7 @@ func CreateOrgHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/plans_changes.go b/app/server/handlers/plans_changes.goindex 0e4bc24f..a86ed819 100644--- a/app/server/handlers/plans_changes.go+++ b/app/server/handlers/plans_changes.go@@ -4,9 +4,11 @@ import ("context""encoding/json""fmt"+ "io""log""net/http""plandex-server/db"+ modelPlan "plandex-server/model/plan""time""github.com/gorilla/mux"@@ -85,6 +87,23 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {}var err error++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.ApplyPlanRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }+ctx, cancel := context.WithCancel(context.Background())unlockFn := lockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel, true)if unlockFn == nil {@@ -95,7 +114,14 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {}()}- err = db.ApplyPlan(auth.OrgId, auth.User.Id, branch, plan)+ settings, err := db.GetPlanSettings(plan, true)+ if err != nil {+ log.Printf("Error getting plan settings: %v\n", err)+ http.Error(w, "Error getting plan settings: "+err.Error(), http.StatusInternalServerError)+ return+ }++ currentPlan, err := db.ApplyPlan(auth.OrgId, auth.User.Id, branch, plan)if err != nil {log.Printf("Error applying plan: %v\n", err)@@ -103,6 +129,29 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {return}+ clients := initClients(+ initClientsParams{+ w: w,+ apiKeys: requestBody.ApiKeys,+ openAIBase: requestBody.OpenAIBase,+ openAIOrgId: requestBody.OpenAIOrgId,+ plan: plan,+ },+ )++ envVar := settings.ModelPack.CommitMsg.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ s, err := modelPlan.GenCommitMsgForPendingResults(client, settings.ModelPack.CommitMsg, currentPlan, r.Context())++ if err != nil {+ log.Printf("Error generating commit message: %v\n", err)+ http.Error(w, "Error generating commit message: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write([]byte(s))+log.Println("Successfully applied plan", planId)}@@ -255,3 +304,93 @@ func ArchivePlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Successfully archived plan", planId)}++func UnarchivePlanHandler(w http.ResponseWriter, r *http.Request) {+ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ log.Println("Received request for UnarchivePlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]+ log.Println("planId: ", planId)++ plan := authorizePlanArchive(w, planId, auth)++ if plan == nil {+ return+ }++ if plan.ArchivedAt == nil {+ log.Println("Plan isn't archived")+ http.Error(w, "Plan isn't archived", http.StatusBadRequest)+ return+ }++ res, err := db.Conn.Exec("UPDATE plans SET archived_at = NULL WHERE id = $1", planId)++ if err != nil {+ log.Printf("Error archiving plan: %v\n", err)+ http.Error(w, "Error archiving plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ rowsAffected, err := res.RowsAffected()+ if err != nil {+ log.Printf("Error getting rows affected: %v\n", err)+ http.Error(w, "Error getting rows affected: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if rowsAffected == 0 {+ log.Println("Plan not found")+ http.Error(w, "Not found", http.StatusNotFound)+ return+ }++ log.Println("Successfully unarchived plan", planId)+}++func GetPlanDiffsHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for GetPlanDiffs")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ vars := mux.Vars(r)+ planId := vars["planId"]+ branch := vars["branch"]++ log.Println("planId: ", planId, "branch: ", branch)++ if authorizePlan(w, planId, auth) == nil {+ return+ }++ var err error+ ctx, cancel := context.WithCancel(context.Background())+ unlockFn := lockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)+ if unlockFn == nil {+ return+ } else {+ defer func() {+ (*unlockFn)(err)+ }()+ }++ diffs, err := db.GetPlanDiffs(auth.OrgId, planId)++ if err != nil {+ log.Printf("Error getting plan diffs: %v\n", err)+ http.Error(w, "Error getting plan diffs: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write([]byte(diffs))++ log.Println("Successfully retrieved plan diffs")+}diff --git a/app/server/handlers/plans_convo.go b/app/server/handlers/plans_convo.goindex 0f985a7c..6d4f3a45 100644--- a/app/server/handlers/plans_convo.go+++ b/app/server/handlers/plans_convo.go@@ -37,7 +37,7 @@ func ListConvoHandler(w http.ResponseWriter, r *http.Request) {}()}- convoMessage, err := db.GetPlanConvo(auth.OrgId, planId)+ convoMessages, err := db.GetPlanConvo(auth.OrgId, planId)if err != nil {log.Println("Error getting plan convo: ", err)@@ -45,7 +45,7 @@ func ListConvoHandler(w http.ResponseWriter, r *http.Request) {return}- bytes, err := json.Marshal(convoMessage)+ bytes, err := json.Marshal(convoMessages)if err != nil {log.Println("Error marshalling plan convo: ", err)@@ -57,3 +57,73 @@ func ListConvoHandler(w http.ResponseWriter, r *http.Request) {w.Write(bytes)}++func GetPlanStatusHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received a request for GetPlanStatusHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ vars := mux.Vars(r)+ planId := vars["planId"]+ branch := vars["branch"]++ log.Println("planId: ", planId, "branch: ", branch)++ plan := authorizePlan(w, planId, auth)+ if plan == nil {+ return+ }++ var err error+ ctx, cancel := context.WithCancel(context.Background())+ unlockFn := lockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)+ if unlockFn == nil {+ return+ } else {+ defer func() {+ (*unlockFn)(err)+ }()+ }++ convoMessages, err := db.GetPlanConvo(auth.OrgId, planId)++ if err != nil {+ log.Println("Error getting plan convo: ", err)+ http.Error(w, "Error getting plan convo: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if len(convoMessages) == 0 {+ log.Println("No messages found for plan")+ return+ }++ convoMessageIds := make([]string, len(convoMessages))+ for i, convoMessage := range convoMessages {+ convoMessageIds[i] = convoMessage.Id+ }++ summmaries, err := db.GetPlanSummaries(planId, convoMessageIds)++ if err != nil {+ log.Println("Error getting plan summaries: ", err)+ http.Error(w, "Error getting plan summaries: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if len(summmaries) == 0 {+ log.Println("No summaries found for plan")+ return+ }++ latestSummary := summmaries[len(summmaries)-1]++ bytes := []byte(latestSummary.Summary)++ w.Write(bytes)++ log.Println("Successfully processed request for GetPlanStatusHandler")+}diff --git a/app/server/handlers/plans_crud.go b/app/server/handlers/plans_crud.goindex b2762997..bdfadad8 100644--- a/app/server/handlers/plans_crud.go+++ b/app/server/handlers/plans_crud.go@@ -172,6 +172,55 @@ func GetPlanHandler(w http.ResponseWriter, r *http.Request) {w.Write(bytes)}+func RenamePlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RenamePlanHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ plan := authorizePlan(w, planId, auth)++ if plan == nil {+ return+ }++ var requestBody shared.RenamePlanRequest+ if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ if plan.OwnerId != auth.User.Id {+ log.Println("Only the plan owner can rename a plan")+ http.Error(w, "Only the plan owner can rename a plan", http.StatusForbidden)+ return+ }++ if requestBody.Name == "" {+ log.Println("Name cannot be empty")+ http.Error(w, "Name cannot be empty", http.StatusBadRequest)+ return+ }++ err := db.RenamePlan(planId, requestBody.Name, nil)++ if err != nil {+ log.Printf("Error renaming plan: %v\n", err)+ http.Error(w, "Error renaming plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully renamed plan")+}+func DeletePlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for DeletePlanHandler")@@ -328,7 +377,7 @@ func ListArchivedPlansHandler(w http.ResponseWriter, r *http.Request) {}}- plans, err := db.ListOwnedPlans(projectIds, "", true)+ plans, err := db.ListOwnedPlans(projectIds, auth.User.Id, true)if err != nil {log.Printf("Error listing plans: %v\n", err)diff --git a/app/server/handlers/plans_exec.go b/app/server/handlers/plans_exec.goindex 73d21e43..395a5437 100644--- a/app/server/handlers/plans_exec.go+++ b/app/server/handlers/plans_exec.go@@ -9,11 +9,11 @@ import ("os""plandex-server/db""plandex-server/host"- "plandex-server/model"modelPlan "plandex-server/model/plan""plandex-server/types""time"+ "github.com/davecgh/go-spew/spew""github.com/gorilla/mux""github.com/plandex/plandex/shared")@@ -57,7 +57,7 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {return}- if requestBody.ApiKey == "" {+ if requestBody.ApiKey == "" && len(requestBody.ApiKeys) == 0 {log.Println("API key is required")http.Error(w, "API key is required", http.StatusBadRequest)return@@ -87,8 +87,18 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {}}- client := model.NewClient(requestBody.ApiKey, requestBody.Endpoint, requestBody.OpenAIOrgId)- err = modelPlan.Tell(client, plan, branch, auth, &requestBody)+ clients := initClients(+ initClientsParams{+ w: w,+ apiKey: requestBody.ApiKey,+ apiKeys: requestBody.ApiKeys,+ endpoint: requestBody.Endpoint,+ openAIBase: requestBody.OpenAIBase,+ openAIOrgId: requestBody.OpenAIOrgId,+ plan: plan,+ },+ )+ err = modelPlan.Tell(clients, plan, branch, auth, &requestBody)if err != nil {log.Printf("Error telling plan: %v\n", err)@@ -138,14 +148,26 @@ func BuildPlanHandler(w http.ResponseWriter, r *http.Request) {return}- if requestBody.ApiKey == "" {+ if requestBody.ApiKey == "" && len(requestBody.ApiKeys) == 0 {+ spew.Dump(requestBody)+log.Println("API key is required")http.Error(w, "API key is required", http.StatusBadRequest)return}- client := model.NewClient(requestBody.ApiKey, requestBody.Endpoint, requestBody.OpenAIOrgId)- numBuilds, err := modelPlan.Build(client, plan, branch, auth)+ clients := initClients(+ initClientsParams{+ w: w,+ apiKey: requestBody.ApiKey,+ apiKeys: requestBody.ApiKeys,+ endpoint: requestBody.Endpoint,+ openAIBase: requestBody.OpenAIBase,+ openAIOrgId: requestBody.OpenAIOrgId,+ plan: plan,+ },+ )+ numBuilds, err := modelPlan.Build(clients, plan, branch, auth)if err != nil {log.Printf("Error building plan: %v\n", err)diff --git a/app/server/handlers/projects.go b/app/server/handlers/projects.goindex 3a8188b1..3a21c9cc 100644--- a/app/server/handlers/projects.go+++ b/app/server/handlers/projects.go@@ -42,7 +42,7 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/sessions.go b/app/server/handlers/sessions.goindex 20c2233a..3d9dc3f9 100644--- a/app/server/handlers/sessions.go+++ b/app/server/handlers/sessions.go@@ -144,7 +144,7 @@ func SignInHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/settings.go b/app/server/handlers/settings.goindex 8c739690..1de3bed3 100644--- a/app/server/handlers/settings.go+++ b/app/server/handlers/settings.go@@ -126,7 +126,7 @@ func UpdateSettingsHandler(w http.ResponseWriter, r *http.Request) {return}- commitMsg := getUpdateCommitMsg(req.Settings, originalSettings)+ commitMsg := getUpdateCommitMsg(req.Settings, originalSettings, false)err = db.GitAddAndCommit(auth.OrgId, planId, branch, commitMsg)@@ -153,7 +153,127 @@ func UpdateSettingsHandler(w http.ResponseWriter, r *http.Request) {}-func getUpdateCommitMsg(settings *shared.PlanSettings, originalSettings *shared.PlanSettings) string {+func GetDefaultSettingsHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for GetDefaultSettingsHandler")++ auth := authenticate(w, r, true)+ if auth == nil {+ return+ }++ settings, err := db.GetOrgDefaultSettings(auth.OrgId, true)++ if err != nil {+ log.Println("Error getting default settings: ", err)+ http.Error(w, "Error getting default settings", http.StatusInternalServerError)+ return+ }++ bytes, err := json.Marshal(settings)++ if err != nil {+ log.Println("Error marshalling default settings: ", err)+ http.Error(w, "Error marshalling default settings", http.StatusInternalServerError)+ return+ }++ w.Write(bytes)++ log.Println("GetDefaultSettingsHandler processed successfully")+}++func UpdateDefaultSettingsHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for UpdateDefaultSettingsHandler")++ auth := authenticate(w, r, true)++ if auth == nil {+ return+ }++ var req shared.UpdateSettingsRequest+ err := json.NewDecoder(r.Body).Decode(&req)++ if err != nil {+ log.Println("Error decoding request body: ", err)+ http.Error(w, "Error decoding request body", http.StatusInternalServerError)+ return+ }++ tx, err := db.Conn.Beginx()++ if err != nil {+ log.Println("Error starting transaction: ", err)+ http.Error(w, "Error starting transaction", http.StatusInternalServerError)+ return+ }+ // Ensure that rollback is attempted in case of failure+ defer func() {+ if err != nil {+ if rbErr := tx.Rollback(); rbErr != nil {+ log.Printf("transaction rollback error: %v\n", rbErr)+ } else {+ log.Println("transaction rolled back")+ }+ }+ }()++ originalSettings, err := db.GetOrgDefaultSettingsForUpdate(auth.OrgId, tx, true)++ if err != nil {+ log.Println("Error getting default settings: ", err)+ http.Error(w, "Error getting default settings", http.StatusInternalServerError)+ return+ }++ // log.Println("Original settings:")+ // spew.Dump(originalSettings)++ // log.Println("req.Settings:")+ // spew.Dump(req.Settings)++ if !req.Settings.UpdatedAt.Equal(originalSettings.UpdatedAt) {+ err = fmt.Errorf("default settings have been updated since you last fetched them")+ log.Println("Error updating default settings: ", err)+ http.Error(w, "Error updating default settings: "+err.Error(), http.StatusBadRequest)+ return+ }++ err = db.StoreOrgDefaultSettings(auth.OrgId, req.Settings, tx)++ if err != nil {+ log.Println("Error storing default settings: ", err)+ http.Error(w, "Error storing default settings: "+err.Error(), http.StatusInternalServerError)+ return+ }++ err = tx.Commit()++ if err != nil {+ log.Println("Error committing transaction: ", err)+ http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)+ return+ }++ commitMsg := getUpdateCommitMsg(req.Settings, originalSettings, true)++ res := shared.UpdateSettingsResponse{+ Msg: commitMsg,+ }+ bytes, err := json.Marshal(res)++ if err != nil {+ log.Println("Error marshalling response: ", err)+ http.Error(w, "Error marshalling response", http.StatusInternalServerError)+ return+ }++ w.Write(bytes)++ log.Println("UpdateDefaultSettingsHandler processed successfully")+}++func getUpdateCommitMsg(settings *shared.PlanSettings, originalSettings *shared.PlanSettings, isOrgDefault bool) string {// log.Println("Comparing settings")// log.Println("Original:")// spew.Dump(originalSettings)@@ -168,8 +288,12 @@ func getUpdateCommitMsg(settings *shared.PlanSettings, originalSettings *shared.}// log.Println("Changes to settings:", strings.Join(changes, "\n"))-- s := "⚙️ Updated model settings:"+ var s string+ if isOrgDefault {+ s = "⚙️ Updated org-wide default settings:"+ } else {+ s = "⚙️ Updated model settings:"+ }for _, change := range changes {s += "\n" + " • " + changediff --git a/app/server/handlers/stream_helper.go b/app/server/handlers/stream_helper.goindex f5c8c13a..689dc719 100644--- a/app/server/handlers/stream_helper.go+++ b/app/server/handlers/stream_helper.go@@ -159,10 +159,12 @@ func initConnectActive(auth *types.ServerAuth, planId, branch string, w http.Resreturn fmt.Errorf("error sending connect message: %v", err)}+ buildQueuesByPath := modelPlan.GetActivePlan(planId, branch).BuildQueuesByPath+// if we're connecting to an active stream and there are active builds, send initial build info- if len(active.BuildQueuesByPath) > 0 {+ if len(buildQueuesByPath) > 0 {- for path, queue := range active.BuildQueuesByPath {+ for path, queue := range buildQueuesByPath {buildInfo := shared.BuildInfo{Path: path}for _, build := range queue {@@ -170,7 +172,7 @@ func initConnectActive(auth *types.ServerAuth, planId, branch string, w http.ResbuildInfo.NumTokens = 0buildInfo.Finished = true} else {- tokens := build.BufferTokens+ tokens := build.WithLineNumsBufferTokensbuildInfo.Finished = falsebuildInfo.NumTokens += tokensdiff --git a/app/server/handlers/users.go b/app/server/handlers/users.goindex ba7433f0..879f10e9 100644--- a/app/server/handlers/users.go+++ b/app/server/handlers/users.go@@ -148,7 +148,7 @@ func DeleteOrgUserHandler(w http.ResponseWriter, r *http.Request) {}// start a transaction- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/main.go b/app/server/main.goindex cbd6caf4..7e5e9b36 100644--- a/app/server/main.go+++ b/app/server/main.go@@ -16,6 +16,7 @@ import ()func main() {+err := host.LoadIp()if err != nil {log.Fatal("Error loading IP: ", err)diff --git a/app/server/migrations/2024041500_model_sets_models.down.sql b/app/server/migrations/2024041500_model_sets_models.down.sqlnew file mode 100644index 00000000..212c0991--- /dev/null+++ b/app/server/migrations/2024041500_model_sets_models.down.sql@@ -0,0 +1,2 @@+DROP TABLE IF EXISTS model_sets;+DROP TABLE IF EXISTS custom_models;\ No newline at end of filediff --git a/app/server/migrations/2024041500_model_sets_models.up.sql b/app/server/migrations/2024041500_model_sets_models.up.sqlnew file mode 100644index 00000000..f9731f08--- /dev/null+++ b/app/server/migrations/2024041500_model_sets_models.up.sql@@ -0,0 +1,47 @@+CREATE TABLE IF NOT EXISTS model_sets (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,++ name VARCHAR(255) NOT NULL,+ description TEXT,++ planner JSON,+ plan_summary JSON,+ builder JSON,+ namer JSON,+ commit_msg JSON,+ exec_status JSON,++ created_at TIMESTAMP NOT NULL DEFAULT NOW()+);++CREATE INDEX model_sets_org_idx ON model_sets(org_id);++CREATE TABLE IF NOT EXISTS custom_models (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,++ provider VARCHAR(255) NOT NULL,+ custom_provider VARCHAR(255),+ base_url VARCHAR(255) NOT NULL,+ model_name VARCHAR(255) NOT NULL,+ description TEXT,+ max_tokens INTEGER NOT NULL,+ api_key_env_var VARCHAR(255),++ is_openai_compatible BOOLEAN NOT NULL,+ has_json_mode BOOLEAN NOT NULL,+ has_streaming BOOLEAN NOT NULL,+ has_function_calling BOOLEAN NOT NULL,+ has_streaming_function_calls BOOLEAN NOT NULL,++ default_max_convo_tokens INTEGER NOT NULL,+ default_reserved_output_tokens INTEGER NOT NULL,++ updated_at TIMESTAMP NOT NULL DEFAULT NOW(),+ created_at TIMESTAMP NOT NULL DEFAULT NOW()+);++CREATE TRIGGER update_custom_models_modtime BEFORE UPDATE ON custom_models FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++CREATE INDEX custom_models_org_idx ON custom_models(org_id);diff --git a/app/server/migrations/2024042600_default_plan_settings.down.sql b/app/server/migrations/2024042600_default_plan_settings.down.sqlnew file mode 100644index 00000000..5955fffc--- /dev/null+++ b/app/server/migrations/2024042600_default_plan_settings.down.sql@@ -0,0 +1 @@+DROP TABLE IF EXISTS default_plan_settings;\ No newline at end of filediff --git a/app/server/migrations/2024042600_default_plan_settings.up.sql b/app/server/migrations/2024042600_default_plan_settings.up.sqlnew file mode 100644index 00000000..d5743398--- /dev/null+++ b/app/server/migrations/2024042600_default_plan_settings.up.sql@@ -0,0 +1,12 @@+CREATE TABLE IF NOT EXISTS default_plan_settings (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,++ plan_settings JSON,++ updated_at TIMESTAMP NOT NULL DEFAULT NOW(),+ created_at TIMESTAMP NOT NULL DEFAULT NOW()+);+CREATE TRIGGER update_default_plan_settings_modtime BEFORE UPDATE ON default_plan_settings FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++CREATE UNIQUE INDEX default_plan_settings_org_idx ON default_plan_settings(org_id);diff --git a/app/server/model/client.go b/app/server/model/client.goindex f73918f5..ed145575 100644--- a/app/server/model/client.go+++ b/app/server/model/client.go@@ -11,7 +11,23 @@ import (const OPENAI_STREAM_CHUNK_TIMEOUT = time.Duration(30) * time.Second-func NewClient(apiKey, endpoint, orgId string) *openai.Client {+func InitClients(apiKeys map[string]string, endpointsByApiKeyEnvVar map[string]string, openAIEndpoint, orgId string) map[string]*openai.Client {+ clients := make(map[string]*openai.Client)+ for key, apiKey := range apiKeys {+ var clientEndpoint string+ var clientOrgId string+ if key == "OPENAI_API_KEY" {+ clientEndpoint = openAIEndpoint+ clientOrgId = orgId+ } else {+ clientEndpoint = endpointsByApiKeyEnvVar[key]+ }+ clients[key] = newClient(apiKey, clientEndpoint, clientOrgId)+ }+ return clients+}++func newClient(apiKey, endpoint, orgId string) *openai.Client {config := openai.DefaultConfig(apiKey)if endpoint != "" {config.BaseURL = endpointdiff --git a/app/server/model/name.go b/app/server/model/name.goindex 400c5ba6..3ea97666 100644--- a/app/server/model/name.go+++ b/app/server/model/name.go@@ -10,7 +10,7 @@ import ("github.com/sashabaranov/go-openai")-func GenPlanName(client *openai.Client, config shared.TaskRoleConfig, planContent string) (string, error) {+func GenPlanName(client *openai.Client, config shared.ModelRoleConfig, planContent string) (string, error) {messages := []openai.ChatCompletionMessage{{Role: openai.ChatMessageRoleSystem,@@ -23,6 +23,11 @@ func GenPlanName(client *openai.Client, config shared.TaskRoleConfig, planContenContent: prompts.GetPlanNamePrompt(planContent),})+ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }+resp, err := CreateChatCompletionWithRetries(client,context.Background(),@@ -43,7 +48,7 @@ func GenPlanName(client *openai.Client, config shared.TaskRoleConfig, planContenTemperature: config.Temperature,TopP: config.TopP,Messages: messages,- ResponseFormat: config.OpenAIResponseFormat,+ ResponseFormat: responseFormat,},)diff --git a/app/server/model/plan/activate.go b/app/server/model/plan/activate.goindex d450ed24..2c23bcd9 100644--- a/app/server/model/plan/activate.go+++ b/app/server/model/plan/activate.go@@ -11,7 +11,7 @@ import ("github.com/sashabaranov/go-openai")-func activatePlan(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, prompt string, buildOnly bool) (*types.ActivePlan, error) {+func activatePlan(clients map[string]*openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, prompt string, buildOnly bool) (*types.ActivePlan, error) {active := GetActivePlan(plan.Id, branch)if active != nil {log.Printf("Tell: Active plan found for plan ID %s on branch %s\n", plan.Id, branch) // Log if an active plan is found@@ -29,7 +29,7 @@ func activatePlan(client *openai.Client, plan *db.Plan, branch string, auth *typreturn nil, fmt.Errorf("plan %s branch %s already has an active stream on host %s", plan.Id, branch, modelStream.InternalIp)}- active = CreateActivePlan(plan.Id, branch, prompt, buildOnly)+ active = CreateActivePlan(auth.OrgId, auth.User.Id, plan.Id, branch, prompt, buildOnly)modelStream = &db.ModelStream{OrgId: auth.OrgId,diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.goindex ec29f241..cd6d4aab 100644--- a/app/server/model/plan/build_exec.go+++ b/app/server/model/plan/build_exec.go@@ -13,7 +13,7 @@ import ()func Build(- client *openai.Client,+ clients map[string]*openai.Client,plan *db.Plan,branch string,auth *types.ServerAuth,@@ -22,7 +22,7 @@ func Build(log.Println("Build: Starting Build operation")state := activeBuildStreamState{- client: client,+ clients: clients,auth: auth,currentOrgId: auth.OrgId,currentUserId: auth.User.Id,@@ -80,19 +80,25 @@ func (state *activeBuildStreamState) queueBuilds(activeBuilds []*types.ActiveBui// log.Printf("Queue:")// spew.Dump(activePlan.BuildQueuesByPath[filePath])- var activePlan *types.ActivePlan+ var isBuilding boolUpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.BuildQueuesByPath[filePath] = append(active.BuildQueuesByPath[filePath], activeBuilds...)- activePlan = active+ isBuilding = active.IsBuildingByPath[filePath]})log.Printf("Queued %d build(s) for file %s\n", len(activeBuilds), filePath)- if activePlan.IsBuildingByPath[filePath] {+ if isBuilding {log.Printf("Already building file %s\n", filePath)return} else {- log.Printf("Not building file %s, will execute now\n", filePath)+ log.Printf("Not building file %s\n", filePath)++ active := GetActivePlan(planId, branch)+ if active == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.IsBuildingByPath[filePath] = true@@ -119,6 +125,10 @@ func (buildState *activeBuildStreamState) execPlanBuild(activeBuild *types.Activbranch := buildState.branchactivePlan := GetActivePlan(planId, branch)+ if activePlan == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }filePath := activeBuild.Pathif !activePlan.IsBuildingByPath[filePath] {@@ -160,12 +170,15 @@ func (fileState *activeBuildStreamFileState) buildFile() {branch := fileState.branchcurrentPlan := fileState.currentPlanStatecurrentOrgId := fileState.currentOrgId- client := fileState.client- config := fileState.settings.ModelSet.Builderbuild := fileState.buildactivePlan := GetActivePlan(planId, branch)+ if activePlan == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }+log.Printf("Building file %s\n", filePath)log.Println("activePlan.ContextsByPath files:")@@ -237,12 +250,31 @@ func (fileState *activeBuildStreamFileState) buildFile() {activeBuild.CurrentFileTokens = currentNumTokens}- log.Println("Getting file from model: " + filePath)+ fileState.buildFileLineNums()+}++func (fileState *activeBuildStreamFileState) buildFileLineNums() {+ filePath := fileState.filePath+ activeBuild := fileState.activeBuild+ clients := fileState.clients+ planId := fileState.plan.Id+ branch := fileState.branch+ config := fileState.settings.ModelPack.Builder+ currentState := fileState.currentState++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }++ log.Println("buildFileLineNums - getting file from model: " + filePath)// log.Println("File context:", fileContext)// log.Println("currentState:", currentState)- sysPrompt := prompts.GetBuildSysPrompt(filePath, currentState, activeBuild.FileDescription, activeBuild.FileContent)+ sysPrompt := prompts.GetBuildLineNumbersSysPrompt(filePath, currentState, activeBuild.FileDescription, activeBuild.FileContent)fileMessages := []openai.ChatCompletionMessage{{@@ -251,12 +283,20 @@ func (fileState *activeBuildStreamFileState) buildFile() {},}- log.Println("Calling model for file: " + filePath)+ log.Println("buildFileLineNums - calling model for file: " + filePath)// for _, msg := range fileMessages {// log.Printf("%s: %s\n", msg.Role, msg.Content)// }+ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }++ // log.Println("responseFormat:", responseFormat)+ // log.Println("Model:", config.BaseModelConfig.ModelName)+modelReq := openai.ChatCompletionRequest{Model: config.BaseModelConfig.ModelName,Tools: []openai.Tool{@@ -274,9 +314,12 @@ func (fileState *activeBuildStreamFileState) buildFile() {Messages: fileMessages,Temperature: config.Temperature,TopP: config.TopP,- ResponseFormat: config.OpenAIResponseFormat,+ ResponseFormat: responseFormat,}+ envVar := config.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]+stream, err := model.CreateChatCompletionStreamWithRetries(client, activePlan.Ctx, modelReq)if err != nil {log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)@@ -284,6 +327,79 @@ func (fileState *activeBuildStreamFileState) buildFile() {return}- go fileState.listenStream(stream)-+ go fileState.listenStreamChangesWithLineNums(stream)}++// func (fileState *activeBuildStreamFileState) buildFileFullChanges() {+// filePath := fileState.filePath+// activeBuild := fileState.activeBuild+// clients := fileState.clients+// planId := fileState.plan.Id+// branch := fileState.branch+// config := fileState.settings.ModelPack.Builder+// currentState := fileState.currentState++// activePlan := GetActivePlan(planId, branch)++// if activePlan == nil {+// log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+// return+// }++// log.Println("buildFileFullChanges - getting file from model: " + filePath)+// // log.Println("File context:", fileContext)++// // log.Println("currentState:", currentState)++// sysPrompt := prompts.GetBuildFullChangesSysPrompt(filePath, currentState, activeBuild.FileDescription, activeBuild.FileContent)++// fileMessages := []openai.ChatCompletionMessage{+// {+// Role: openai.ChatMessageRoleSystem,+// Content: sysPrompt,+// },+// }++// log.Println("buildFileFullChanges - calling model for file: " + filePath)++// // for _, msg := range fileMessages {+// // log.Printf("%s: %s\n", msg.Role, msg.Content)+// // }++// var responseFormat *openai.ChatCompletionResponseFormat+// if config.BaseModelConfig.HasJsonResponseMode {+// responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+// }++// modelReq := openai.ChatCompletionRequest{+// Model: config.BaseModelConfig.ModelName,+// Tools: []openai.Tool{+// {+// Type: "function",+// Function: &prompts.ListReplacementsFullFn,+// },+// },+// ToolChoice: openai.ToolChoice{+// Type: "function",+// Function: openai.ToolFunction{+// Name: prompts.ListReplacementsFullFn.Name,+// },+// },+// Messages: fileMessages,+// Temperature: config.Temperature,+// TopP: config.TopP,+// ResponseFormat: responseFormat,+// }++// envVar := config.BaseModelConfig.ApiKeyEnvVar+// client := clients[envVar]++// stream, err := model.CreateChatCompletionStreamWithRetries(client, activePlan.Ctx, modelReq)+// if err != nil {+// log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)+// fileState.onBuildFileError(fmt.Errorf("error creating plan file stream for path '%s': %v", filePath, err))+// return+// }++// go fileState.listenStreamFullChanges(stream)+// }diff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.goindex 0c187b4a..493a7f07 100644--- a/app/server/model/plan/build_finish.go+++ b/app/server/model/plan/build_finish.go@@ -286,6 +286,11 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanFlog.Printf("File %s finished, but not all builds finished\n", filePath)} else {log.Printf("Processing next build for file %s\n", filePath)+ activePlan := GetActivePlan(planId, branch)+ if activePlan == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }queue := activePlan.BuildQueuesByPath[filePath]var nextBuild *types.ActiveBuildfor _, build := range queue {@@ -311,9 +316,15 @@ func (fileState *activeBuildStreamFileState) onBuildFileError(err error) {filePath := fileState.filePathbuild := fileState.buildactiveBuild := fileState.activeBuild+ currentOrgId := fileState.currentOrgIdactivePlan := GetActivePlan(planId, branch)+ if activePlan == nil {+ log.Println("onBuildFileError - Active plan not found")+ return+ }+log.Printf("Error for file %s: %v\n", filePath, err)activeBuild.Success = false@@ -337,4 +348,11 @@ func (fileState *activeBuildStreamFileState) onBuildFileError(err error) {if err != nil {log.Printf("Error setting build error: %v\n", err)}++ // rollback repo in case there are uncommitted builds+ err = db.GitClearUncommittedChanges(currentOrgId, planId)++ if err != nil {+ log.Printf("Error clearing uncommitted changes: %v\n", err)+ }}diff --git a/app/server/model/plan/build_fix.go b/app/server/model/plan/build_fix.gonew file mode 100644index 00000000..0d3b515d--- /dev/null+++ b/app/server/model/plan/build_fix.go@@ -0,0 +1,93 @@+package plan++import (+ "fmt"+ "log"+ "plandex-server/model"+ "plandex-server/model/prompts"++ "github.com/sashabaranov/go-openai"+)++func (fileState *activeBuildStreamFileState) fixFileLineNums() {+ filePath := fileState.filePath+ activeBuild := fileState.activeBuild+ clients := fileState.clients+ planId := fileState.plan.Id+ branch := fileState.branch+ config := fileState.settings.ModelPack.Builder+ incorrectlyUpdated := fileState.updated+ reasoning := fileState.incorrectlyUpdatedReasoning++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("fixFileLineNums - Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }++ // hash := sha256.Sum256([]byte(incorrectlyUpdated))+ // sha := hex.EncodeToString(hash[:])++ // log.Printf("%s - fixFileLineNums - Incorrectly updated content hash: %s\n", filePath, sha)++ log.Println("fixFileLineNums - getting file from model: " + filePath)+ // log.Println("File context:", fileContext)++ // log.Println("currentState:", currentState)++ sysPrompt := prompts.GetBuildFixesLineNumbersSysPrompt(activeBuild.FileDescription, activeBuild.FileContent, incorrectlyUpdated, reasoning)++ fileMessages := []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: sysPrompt,+ },+ }++ log.Println("fixFileLineNums - calling model for file: " + filePath)++ // for _, msg := range fileMessages {+ // log.Printf("%s: %s\n", msg.Role, msg.Content)+ // }++ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }++ // log.Println("responseFormat:", responseFormat)+ // log.Println("Model:", config.BaseModelConfig.ModelName)++ modelReq := openai.ChatCompletionRequest{+ Model: config.BaseModelConfig.ModelName,+ Tools: []openai.Tool{+ {+ Type: "function",+ Function: &prompts.ListReplacementsFn,+ },+ },+ ToolChoice: openai.ToolChoice{+ Type: "function",+ Function: openai.ToolFunction{+ Name: prompts.ListReplacementsFn.Name,+ },+ },+ Messages: fileMessages,+ Temperature: config.Temperature,+ TopP: config.TopP,+ ResponseFormat: responseFormat,+ }++ envVar := config.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ stream, err := model.CreateChatCompletionStreamWithRetries(client, activePlan.Ctx, modelReq)+ if err != nil {+ log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)+ fileState.onBuildFileError(fmt.Errorf("error creating plan file stream for path '%s': %v", filePath, err))+ return+ }++ go fileState.listenStreamFixChanges(stream)+}diff --git a/app/server/model/plan/build_fix_stream.go b/app/server/model/plan/build_fix_stream.gonew file mode 100644index 00000000..f41a8e5a--- /dev/null+++ b/app/server/model/plan/build_fix_stream.go@@ -0,0 +1,261 @@+package plan++import (+ "context"+ "encoding/json"+ "fmt"+ "log"+ "math"+ "math/rand"+ "plandex-server/model"+ "plandex-server/types"+ "strings"+ "time"++ "github.com/davecgh/go-spew/spew"+ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func (fileState *activeBuildStreamFileState) listenStreamFixChanges(stream *openai.ChatCompletionStream) {+ filePath := fileState.filePath+ build := fileState.build+ currentOrgId := fileState.currentOrgId+ planId := fileState.plan.Id+ branch := fileState.branch+ activeBuild := fileState.activeBuild++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("listenStreamFixChanges - Active plan not found for plan ID %s on branch %s\n", planId, branch)+ return+ }++ defer stream.Close()++ // Create a timer that will trigger if no chunk is received within the specified duration+ timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ defer timer.Stop()++ for {+ select {+ case <-activePlan.Ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fileState.fixRetryOrError(fmt.Errorf("listenStreamFixChanges - stream timeout due to inactivity for file '%s'", filePath))+ return+ default:+ response, err := stream.Recv()++ if err == nil {+ // Successfully received a chunk, reset the timer+ if !timer.Stop() {+ <-timer.C+ }+ timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ } else {+ log.Printf("listenStreamFixChanges - File %s: Error receiving stream chunk: %v\n", filePath, err)++ if err == context.Canceled {+ log.Printf("listenStreamFixChanges - File %s: Stream canceled\n", filePath)+ return+ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ if len(response.Choices) == 0 {+ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ var content string+ delta := response.Choices[0].Delta++ if len(delta.ToolCalls) > 0 {+ content = delta.ToolCalls[0].Function.Arguments++ trimmed := strings.TrimSpace(content)+ if trimmed == "{%invalidjson%}" || trimmed == "``(no output)``````" {+ log.Println("listenStreamFixChanges - File", filePath+":", "%invalidjson%} token in streamed chunk")+ fileState.fixRetryOrError(fmt.Errorf("listenStreamFixChanges - invalid JSON in streamed chunk for file '%s'", filePath))++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)++ return+ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 1,+ Finished: false,+ }++ // log.Printf("listenStreamFixChanges - %s: %s", filePath, content)+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })++ fileState.activeBuild.FixBuffer += content+ fileState.activeBuild.FixBufferTokens++++ // After a reasonable threshhold, if buffer has significantly more tokens than original file + proposed changes, something is wrong+ cutoff := int(math.Max(float64(fileState.activeBuild.CurrentFileTokens+fileState.activeBuild.FileContentTokens), 500) * 2)+ if fileState.activeBuild.FixBufferTokens > 500 && fileState.activeBuild.FixBufferTokens > cutoff {+ log.Printf("File %s: Stream buffer tokens too high\n", filePath)+ log.Printf("Current file tokens: %d\n", fileState.activeBuild.CurrentFileTokens)+ log.Printf("File content tokens: %d\n", fileState.activeBuild.FileContentTokens)+ log.Printf("Cutoff: %d\n", cutoff)+ log.Printf("Buffer tokens: %d\n", fileState.activeBuild.FixBufferTokens)+ log.Println("Buffer:")+ log.Println(fileState.activeBuild.FixBuffer)++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }+ }++ var streamed types.StreamedChangesWithLineNums+ err = json.Unmarshal([]byte(fileState.activeBuild.FixBuffer), &streamed)++ if err == nil {+ log.Printf("listenStreamFixChanges - File %s: Parsed streamed replacements\n", filePath)+ // spew.Dump(streamed)++ fileState.streamedChangesWithLineNums = streamed.Changes++ var overlapStrategy OverlapStrategy = OverlapStrategyError+ if fileState.fixFileNumRetry > 1 {+ overlapStrategy = OverlapStrategySkip+ }++ planFileResult, _, allSucceeded, err := getPlanResult(+ planResultParams{+ orgId: currentOrgId,+ planId: planId,+ planBuildId: build.Id,+ convoMessageId: build.ConvoMessageId,+ filePath: filePath,+ currentState: fileState.updated,+ fileContent: activeBuild.FileContent,+ streamedChangesWithLineNums: streamed.Changes,+ overlapStrategy: overlapStrategy,+ },+ )++ if err != nil {+ log.Println("listenStreamFixChanges - Error getting plan result:", err)+ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ if !allSucceeded {+ log.Println("listenStreamFixChanges - Failed replacements:")+ for _, replacement := range planFileResult.Replacements {+ if replacement.Failed {+ spew.Dump(replacement)+ }+ }++ // no retry here as this should never happen+ fileState.onBuildFileError(fmt.Errorf("listenStreamFixChanges - replacements failed for file '%s'", filePath))+ return++ }++ // merge replacements+ var replacements []*shared.Replacement+ replacements = append(replacements, fileState.initialPlanFileResult.Replacements...)+ replacements = append(replacements, planFileResult.Replacements...)++ planFileResult.Replacements = replacements++ // log.Println("listenStreamFixChanges - Plan file result:")+ // spew.Dump(planFileResult)++ fileState.onFinishBuildFile(planFileResult)+ return+ } else if len(delta.ToolCalls) == 0 {+ log.Println("listenStreamFixChanges - Stream chunk missing function call.")++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }+ }+ }+}++func (fileState *activeBuildStreamFileState) fixRetryOrError(err error) {+ if fileState.fixFileNumRetry < MaxBuildStreamErrorRetries {+ fileState.fixFileNumRetry+++ fileState.activeBuild.FixBuffer = ""+ fileState.activeBuild.FixBufferTokens = 0+ log.Printf("Retrying fix file '%s' due to error: %v\n", fileState.filePath, err)++ // Exponential backoff+ time.Sleep(time.Duration(fileState.fixFileNumRetry*fileState.fixFileNumRetry)*time.Second + time.Duration(rand.Intn(1001))*time.Millisecond)++ fileState.fixFileLineNums()+ } else {+ fileState.onBuildFileError(err)+ }+}diff --git a/app/server/model/plan/build_load.go b/app/server/model/plan/build_load.goindex f799efef..af10a842 100644--- a/app/server/model/plan/build_load.go+++ b/app/server/model/plan/build_load.go@@ -11,12 +11,12 @@ import ()func (state *activeBuildStreamState) loadPendingBuilds() (map[string][]*types.ActiveBuild, error) {- client := state.client+ clients := state.clientsplan := state.planbranch := state.branchauth := state.auth- active, err := activatePlan(client, plan, branch, auth, "", true)+ active, err := activatePlan(clients, plan, branch, auth, "", true)if err != nil {log.Printf("Error activating plan: %v\n", err)diff --git a/app/server/model/plan/build_result.go b/app/server/model/plan/build_result.goindex ac8977c7..e045b9e6 100644--- a/app/server/model/plan/build_result.go+++ b/app/server/model/plan/build_result.go@@ -1,6 +1,7 @@package planimport (+ "fmt""log""plandex-server/db""sort"@@ -10,26 +11,36 @@ import ("github.com/plandex/plandex/shared")+type OverlapStrategy int++const (+ OverlapStrategySkip OverlapStrategy = iota+ OverlapStrategyError+)+type planResultParams struct {- orgId string- planId string- planBuildId string- convoMessageId string- filePath string- currentState string- fileContent string- streamedChanges []*shared.StreamedChange+ orgId string+ planId string+ planBuildId string+ convoMessageId string+ filePath string+ currentState string+ fileContent string+ overlapStrategy OverlapStrategy+ streamedChangesWithLineNums []*shared.StreamedChangeWithLineNums}-func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {+func getPlanResult(params planResultParams) (*db.PlanFileResult, string, bool, error) {orgId := params.orgIdplanId := params.planIdplanBuildId := params.planBuildIdfilePath := params.filePathcurrentState := params.currentState- streamedChanges := params.streamedChanges+ streamedChangesWithLineNums := params.streamedChangesWithLineNums// fileContent := params.fileContent+ currentState = shared.AddLineNums(currentState)+currentStateLines := strings.Split(currentState, "\n")// fileContentLines := strings.Split(fileContent, "\n")@@ -43,7 +54,14 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {// log.Print("\n\n")var replacements []*shared.Replacement- for _, streamedChange := range streamedChanges {++ var highestEndLine int = 0++ for _, streamedChange := range streamedChangesWithLineNums {+ if !streamedChange.HasChange {+ continue+ }+var old stringnew := streamedChange.New@@ -51,29 +69,49 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {// log.Printf("getPlanResult - streamedChange.Old.StartLine: %d\n", streamedChange.Old.StartLine)// log.Printf("getPlanResult - streamedChange.Old.EndLine: %d\n", streamedChange.Old.EndLine)- startLine := streamedChange.Old.StartLine- endLine := streamedChange.Old.EndLine+ startLine, endLine, err := streamedChange.GetLines()- if startLine == 0 && streamedChange.Old.MaybeStartLine != 0 {- startLine = streamedChange.Old.MaybeStartLine+ if err != nil {+ log.Println("getPlanResult - Error getting lines from streamedChange:", err)+ return nil, "", false, fmt.Errorf("error getting lines from streamedChange: %v", err)}- if endLine == 0 && streamedChange.Old.MaybeEndLine != 0 {- endLine = streamedChange.Old.MaybeEndLine- }-- if startLine < 1 {- startLine = 1- }if startLine > len(currentStateLines) {- startLine = len(currentStateLines)+ log.Printf("Start line is greater than currentStateLines length: %d > %d\n", startLine, len(currentStateLines))+ return nil, "", false, fmt.Errorf("start line is greater than currentStateLines length: %d > %d", startLine, len(currentStateLines))}if endLine < 1 {- endLine = 1+ log.Printf("End line is less than 1: %d\n", endLine)+ return nil, "", false, fmt.Errorf("end line is less than 1: %d", endLine)}if endLine > len(currentStateLines) {- endLine = len(currentStateLines)+ log.Printf("End line is greater than currentStateLines length: %d > %d\n", endLine, len(currentStateLines))+ return nil, "", false, fmt.Errorf("end line is greater than currentStateLines length: %d > %d", endLine, len(currentStateLines))+ }++ if startLine < highestEndLine {+ log.Printf("Start line is less than highestEndLine: %d < %d\n", startLine, highestEndLine)++ if params.overlapStrategy == OverlapStrategyError {+ return nil, "", false, fmt.Errorf("start line is less than highestEndLine: %d < %d", startLine,+ highestEndLine)+ } else {+ continue+ }+ }++ if endLine < highestEndLine {+ if params.overlapStrategy == OverlapStrategyError {+ log.Printf("End line is less than highestEndLine: %d < %d\n", endLine, highestEndLine)+ return nil, "", false, fmt.Errorf("end line is less than highestEndLine: %d < %d", endLine, highestEndLine)+ } else {+ continue+ }+ }++ if endLine > highestEndLine {+ highestEndLine = endLine}if startLine == endLine {@@ -105,7 +143,15 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {// log.Println("Replacements:")// spew.Dump(replacements)- _, allSucceeded := shared.ApplyReplacements(currentState, replacements, true)+ updated, allSucceeded := shared.ApplyReplacements(currentState, replacements, true)++ updated = shared.RemoveLineNums(updated)++ // log sha256 hash of updated content+ // hash := sha256.Sum256([]byte(updated))+ // sha := hex.EncodeToString(hash[:])++ // log.Printf("apply result - %s - updated content hash: %s\n", filePath, sha)for _, replacement := range replacements {id := uuid.New().String()@@ -113,13 +159,15 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {}return &db.PlanFileResult{- OrgId: orgId,- PlanId: planId,- PlanBuildId: planBuildId,- ConvoMessageId: params.convoMessageId,- Content: "",- Path: filePath,- Replacements: replacements,- AnyFailed: !allSucceeded,- }, allSucceeded+ TypeVersion: 1,+ ReplaceWithLineNums: true,+ OrgId: orgId,+ PlanId: planId,+ PlanBuildId: planBuildId,+ ConvoMessageId: params.convoMessageId,+ Content: "",+ Path: filePath,+ Replacements: replacements,+ AnyFailed: !allSucceeded,+ }, updated, allSucceeded, nil}diff --git a/app/server/model/plan/build_state.go b/app/server/model/plan/build_state.gonew file mode 100644index 00000000..ff40f62d--- /dev/null+++ b/app/server/model/plan/build_state.go@@ -0,0 +1,40 @@+package plan++import (+ "plandex-server/db"+ "plandex-server/types"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++const MaxBuildStreamErrorRetries = 3 // uses naive exponential backoff so be careful about setting this too high++type activeBuildStreamState struct {+ clients map[string]*openai.Client+ auth *types.ServerAuth+ currentOrgId string+ currentUserId string+ plan *db.Plan+ branch string+ settings *shared.PlanSettings+ modelContext []*db.Context+}++type activeBuildStreamFileState struct {+ *activeBuildStreamState+ filePath string+ convoMessageId string+ build *db.PlanBuild+ currentPlanState *shared.CurrentPlanState+ activeBuild *types.ActiveBuild+ currentState string+ lineNumsNumRetry int+ verifyFileNumRetry int+ fixFileNumRetry int+ // fullChangesRetry int+ streamedChangesWithLineNums []*shared.StreamedChangeWithLineNums+ updated string+ initialPlanFileResult *db.PlanFileResult+ incorrectlyUpdatedReasoning string+}diff --git a/app/server/model/plan/build_stream.go b/app/server/model/plan/build_stream.godeleted file mode 100644index 3ed21371..00000000--- a/app/server/model/plan/build_stream.go+++ /dev/null@@ -1,222 +0,0 @@-package plan--import (- "context"- "encoding/json"- "fmt"- "log"- "math"- "plandex-server/db"- "plandex-server/model"- "plandex-server/types"- "strings"- "time"-- "github.com/davecgh/go-spew/spew"- "github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"-)--const MaxBuildStreamErrorRetries = 3 // uses naive exponential backoff so be careful about setting this too high--type activeBuildStreamState struct {- client *openai.Client- auth *types.ServerAuth- currentOrgId string- currentUserId string- plan *db.Plan- branch string- settings *shared.PlanSettings- modelContext []*db.Context-}--type activeBuildStreamFileState struct {- *activeBuildStreamState- filePath string- convoMessageId string- build *db.PlanBuild- currentPlanState *shared.CurrentPlanState- activeBuild *types.ActiveBuild- currentState string- numRetry int-}--func (fileState *activeBuildStreamFileState) listenStream(stream *openai.ChatCompletionStream) {- filePath := fileState.filePath- build := fileState.build- currentOrgId := fileState.currentOrgId- planId := fileState.plan.Id- branch := fileState.branch- currentState := fileState.currentState- activeBuild := fileState.activeBuild-- activePlan := GetActivePlan(planId, branch)-- if activePlan == nil {- log.Printf("listenStream - Active plan not found for plan ID %s on branch %s\n", planId, branch)- return- }-- defer stream.Close()-- // Create a timer that will trigger if no chunk is received within the specified duration- timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)- defer timer.Stop()-- for {- select {- case <-activePlan.Ctx.Done():- // The main context was canceled (not the timer)- return- case <-timer.C:- // Timer triggered because no new chunk was received in time- fileState.retryOrError(fmt.Errorf("stream timeout due to inactivity for file '%s'", filePath))- return- default:- response, err := stream.Recv()-- if err == nil {- // Successfully received a chunk, reset the timer- if !timer.Stop() {- <-timer.C- }- timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)- } else {- log.Printf("File %s: Error receiving stream chunk: %v\n", filePath, err)-- if err == context.Canceled {- log.Printf("File %s: Stream canceled\n", filePath)- log.Println("current buffer:")- log.Println(fileState.activeBuild.Buffer)- return- }-- fileState.retryOrError(fmt.Errorf("stream error for file '%s': %v", filePath, err))- return- }-- if len(response.Choices) == 0 {- fileState.retryOrError(fmt.Errorf("stream error: no choices"))- return- }-- choice := response.Choices[0]- var content string- delta := response.Choices[0].Delta-- if len(delta.ToolCalls) > 0 {- content = delta.ToolCalls[0].Function.Arguments-- trimmed := strings.TrimSpace(content)- if trimmed == "{%invalidjson%}" || trimmed == "``(no output)``````" {- log.Println("File", filePath+":", "%invalidjson%} token in streamed chunk")- fileState.retryOrError(fmt.Errorf("invalid JSON in streamed chunk for file '%s'", filePath))-- return- }-- buildInfo := &shared.BuildInfo{- Path: filePath,- NumTokens: 1,- Finished: false,- }-- // log.Printf("%s: %s", filePath, content)- activePlan.Stream(shared.StreamMessage{- Type: shared.StreamMessageBuildInfo,- BuildInfo: buildInfo,- })-- fileState.activeBuild.Buffer += content- fileState.activeBuild.BufferTokens++-- // After a reasonable threshhold, if buffer has significantly more tokens than original file + proposed changes, something is wrong- cutoff := int(math.Max(float64(fileState.activeBuild.CurrentFileTokens+fileState.activeBuild.FileContentTokens), 500) * 1.5)- if fileState.activeBuild.BufferTokens > 500 && fileState.activeBuild.BufferTokens > cutoff {- log.Printf("File %s: Stream buffer tokens too high\n", filePath)- log.Printf("Current file tokens: %d\n", fileState.activeBuild.CurrentFileTokens)- log.Printf("File content tokens: %d\n", fileState.activeBuild.FileContentTokens)- log.Printf("Cutoff: %d\n", cutoff)- log.Printf("Buffer tokens: %d\n", fileState.activeBuild.BufferTokens)- log.Println("Buffer:")- log.Println(fileState.activeBuild.Buffer)-- fileState.retryOrError(fmt.Errorf("stream buffer tokens too high for file '%s'", filePath))- return- }- }-- var streamed types.StreamedChanges- err = json.Unmarshal([]byte(fileState.activeBuild.Buffer), &streamed)-- if err == nil {- log.Printf("File %s: Parsed streamed replacements\n", filePath)- // spew.Dump(streamed)-- planFileResult, allSucceeded := getPlanResult(- planResultParams{- orgId: currentOrgId,- planId: planId,- planBuildId: build.Id,- convoMessageId: build.ConvoMessageId,- filePath: filePath,- currentState: currentState,- fileContent: activeBuild.FileContent,- streamedChanges: streamed.Changes,- },- )-- if !allSucceeded {- log.Println("Failed replacements:")- for _, replacement := range planFileResult.Replacements {- if replacement.Failed {- spew.Dump(replacement)- }- }-- // no retry here as this should never happen- fileState.onBuildFileError(fmt.Errorf("replacements failed for file '%s'", filePath))- return-- }-- buildInfo := &shared.BuildInfo{- Path: filePath,- NumTokens: 0,- Finished: true,- }- activePlan.Stream(shared.StreamMessage{- Type: shared.StreamMessageBuildInfo,- BuildInfo: buildInfo,- })-- fileState.onFinishBuildFile(planFileResult)- return- } else if len(delta.ToolCalls) == 0 {- log.Println("Stream chunk missing function call. Response:")- log.Println(spew.Sdump(response))- log.Println(spew.Sdump(fileState))-- fileState.retryOrError(fmt.Errorf("stream chunk missing function call. Reason: %s, File: %s", choice.FinishReason, filePath))- return- }- }- }--}--func (fileState *activeBuildStreamFileState) retryOrError(err error) {- if fileState.numRetry < MaxBuildStreamErrorRetries {- fileState.numRetry++- fileState.activeBuild.Buffer = ""- fileState.activeBuild.BufferTokens = 0- log.Printf("Retrying build file '%s' due to error: %v\n", fileState.filePath, err)-- // Exponential backoff- time.Sleep(time.Duration(fileState.numRetry*fileState.numRetry) * time.Second)-- fileState.buildFile()- } else {- fileState.onBuildFileError(err)- }-}diff --git a/app/server/model/plan/build_stream_line_nums.go b/app/server/model/plan/build_stream_line_nums.gonew file mode 100644index 00000000..19b1c4b4--- /dev/null+++ b/app/server/model/plan/build_stream_line_nums.go@@ -0,0 +1,246 @@+package plan++import (+ "context"+ "encoding/json"+ "fmt"+ "log"+ "math"+ "math/rand"+ "plandex-server/model"+ "plandex-server/types"+ "strings"+ "time"++ "github.com/davecgh/go-spew/spew"+ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func (fileState *activeBuildStreamFileState) listenStreamChangesWithLineNums(stream *openai.ChatCompletionStream) {+ filePath := fileState.filePath+ build := fileState.build+ currentOrgId := fileState.currentOrgId+ planId := fileState.plan.Id+ branch := fileState.branch+ currentState := fileState.currentState+ activeBuild := fileState.activeBuild++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("listenStream - Active plan not found for plan ID %s on branch %s\n", planId, branch)+ return+ }++ defer stream.Close()++ // Create a timer that will trigger if no chunk is received within the specified duration+ timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ defer timer.Stop()++ for {+ select {+ case <-activePlan.Ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - stream timeout due to inactivity for file '%s'", filePath))+ return+ default:+ response, err := stream.Recv()++ if err == nil {+ // Successfully received a chunk, reset the timer+ if !timer.Stop() {+ <-timer.C+ }+ timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ } else {+ log.Printf("listenStream - File %s: Error receiving stream chunk: %v\n", filePath, err)++ if err == context.Canceled {+ log.Printf("listenStream - File %s: Stream canceled\n", filePath)+ // log.Println("current buffer:")+ // log.Println(fileState.activeBuild.WithLineNumsBuffer)+ return+ }++ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - stream error for file '%s': %v", filePath, err))+ return+ }++ if len(response.Choices) == 0 {+ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - stream error: no choices"))+ return+ }++ choice := response.Choices[0]+ var content string+ delta := response.Choices[0].Delta++ if len(delta.ToolCalls) > 0 {+ content = delta.ToolCalls[0].Function.Arguments++ trimmed := strings.TrimSpace(content)+ if trimmed == "{%invalidjson%}" || trimmed == "``(no output)``````" {+ log.Println("File", filePath+":", "%invalidjson%} token in streamed chunk")+ fileState.lineNumsRetryOrError(fmt.Errorf("invalid JSON in streamed chunk for file '%s'", filePath))++ return+ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 1,+ Finished: false,+ }++ // log.Printf("%s: %s", filePath, content)+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })++ fileState.activeBuild.WithLineNumsBuffer += content+ fileState.activeBuild.WithLineNumsBufferTokens++++ // After a reasonable threshhold, if buffer has significantly more tokens than original file + proposed changes, something is wrong+ cutoff := int(math.Max(float64(fileState.activeBuild.CurrentFileTokens+fileState.activeBuild.FileContentTokens), 500) * 2)+ if fileState.activeBuild.WithLineNumsBufferTokens > 500 && fileState.activeBuild.WithLineNumsBufferTokens > cutoff {+ log.Printf("File %s: Stream buffer tokens too high\n", filePath)+ log.Printf("Current file tokens: %d\n", fileState.activeBuild.CurrentFileTokens)+ log.Printf("File content tokens: %d\n", fileState.activeBuild.FileContentTokens)+ log.Printf("Cutoff: %d\n", cutoff)+ log.Printf("Buffer tokens: %d\n", fileState.activeBuild.WithLineNumsBufferTokens)+ log.Println("Buffer:")+ log.Println(fileState.activeBuild.WithLineNumsBuffer)++ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - stream buffer tokens too high for file '%s'", filePath))+ return+ }+ }++ var streamed types.StreamedChangesWithLineNums+ err = json.Unmarshal([]byte(fileState.activeBuild.WithLineNumsBuffer), &streamed)++ if err == nil {+ log.Printf("listenStream - File %s: Parsed streamed replacements\n", filePath)+ // spew.Dump(streamed)++ fileState.streamedChangesWithLineNums = streamed.Changes++ var overlapStrategy OverlapStrategy = OverlapStrategyError+ if fileState.lineNumsNumRetry > 1 {+ overlapStrategy = OverlapStrategySkip+ }++ planFileResult, _, allSucceeded, err := getPlanResult(+ planResultParams{+ orgId: currentOrgId,+ planId: planId,+ planBuildId: build.Id,+ convoMessageId: build.ConvoMessageId,+ filePath: filePath,+ currentState: currentState,+ fileContent: activeBuild.FileContent,+ streamedChangesWithLineNums: streamed.Changes,+ overlapStrategy: overlapStrategy,+ },+ )++ if err != nil {+ log.Println("listenStream - Error getting plan result:", err)+ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - error getting plan result for file '%s': %v", filePath, err))+ return+ }++ if !allSucceeded {+ log.Println("listenStream - Failed replacements:")+ for _, replacement := range planFileResult.Replacements {+ if replacement.Failed {+ spew.Dump(replacement)+ }+ }++ // no retry here as this should never happen+ fileState.onBuildFileError(fmt.Errorf("listenStream - replacements failed for file '%s'", filePath))+ return++ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(planFileResult)++ // The verification loop below works, but is too heavy on tokens and also seems to hit OpenAI rate limits on complex tasks+ // Going back to a simple build for now+ // Semaphores may help to throttle calls, but had trouble getting them to work++ // hash := sha256.Sum256([]byte(updatedFile))+ // sha := hex.EncodeToString(hash[:])++ // log.Printf("listenStream - %s - updated content hash: %s\n", filePath, sha)++ // fileState.updated = updatedFile+ // fileState.initialPlanFileResult = planFileResult++ // select {+ // case <-activePlan.Ctx.Done():+ // log.Println("listenStream - Context canceled. Exiting.")+ // return+ // case <-time.After(time.Duration(rand.Intn(1001)) * time.Millisecond):+ // break+ // }+ // fileState.verifyFileBuild()++ return+ } else if len(delta.ToolCalls) == 0 {+ log.Println("listenStream - Stream chunk missing function call.")+ // log.Println(spew.Sdump(response))+ // log.Println(spew.Sdump(fileState))++ // log.Println(fileState.activeBuild.WithLineNumsBuffer)++ fileState.lineNumsRetryOrError(fmt.Errorf("listenStream - stream chunk missing function call. Reason: %s, File: %s", choice.FinishReason, filePath))+ return+ }+ }+ }+}++func (fileState *activeBuildStreamFileState) lineNumsRetryOrError(err error) {+ if fileState.lineNumsNumRetry < MaxBuildStreamErrorRetries {+ fileState.lineNumsNumRetry+++ fileState.activeBuild.WithLineNumsBuffer = ""+ fileState.activeBuild.WithLineNumsBufferTokens = 0+ log.Printf("Retrying line nums build file '%s' due to error: %v\n", fileState.filePath, err)++ activePlan := GetActivePlan(fileState.plan.Id, fileState.branch)++ if activePlan == nil {+ log.Println("lineNumsRetryOrError - Active plan not found")+ return+ }++ select {+ case <-activePlan.Ctx.Done():+ log.Println("lineNumsRetryOrError - Context canceled. Exiting.")+ return+ case <-time.After(time.Duration(fileState.lineNumsNumRetry*fileState.lineNumsNumRetry)*time.Second + time.Duration(rand.Intn(1001))*time.Millisecond):+ break+ }++ fileState.buildFileLineNums()+ } else {+ fileState.onBuildFileError(err)+ }+}diff --git a/app/server/model/plan/build_verify.go b/app/server/model/plan/build_verify.gonew file mode 100644index 00000000..fe81dd40--- /dev/null+++ b/app/server/model/plan/build_verify.go@@ -0,0 +1,87 @@+package plan++import (+ "fmt"+ "log"+ "plandex-server/model"+ "plandex-server/model/prompts"++ "github.com/sashabaranov/go-openai"+)++func (fileState *activeBuildStreamFileState) verifyFileBuild() {+ filePath := fileState.filePath+ activeBuild := fileState.activeBuild+ planId := fileState.plan.Id+ branch := fileState.branch+ clients := fileState.clients+ config := fileState.settings.ModelPack.Builder+ currentState := fileState.currentState++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return+ }++ log.Printf("verifyFileBuild - Verifying file %s\n", filePath)++ log.Println("verifyFileBuild - Getting file from model: " + filePath)+ // log.Println("File context:", fileContext)++ // log.Println("currentState:", currentState)++ sysPrompt := prompts.GetVerifyPrompt(currentState, activeBuild.FileDescription, activeBuild.FileContent)++ fileMessages := []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: sysPrompt,+ },+ }++ log.Println("verifyFileBuild - Calling verify model for file: " + filePath)++ // for _, msg := range fileMessages {+ // log.Printf("%s: %s\n", msg.Role, msg.Content)+ // }++ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }++ modelReq := openai.ChatCompletionRequest{+ Model: config.BaseModelConfig.ModelName,+ Tools: []openai.Tool{+ {+ Type: "function",+ Function: &prompts.VerifyOutputFn,+ },+ },+ ToolChoice: openai.ToolChoice{+ Type: "function",+ Function: openai.ToolFunction{+ Name: prompts.VerifyOutputFn.Name,+ },+ },+ Messages: fileMessages,+ Temperature: config.Temperature,+ TopP: config.TopP,+ ResponseFormat: responseFormat,+ }++ envVar := config.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ stream, err := model.CreateChatCompletionStreamWithRetries(client, activePlan.Ctx, modelReq)+ if err != nil {+ log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)+ fileState.onBuildFileError(fmt.Errorf("error creating plan file stream for path '%s': %v", filePath, err))+ return+ }++ go fileState.listenStreamVerifyOutput(stream)++}diff --git a/app/server/model/plan/build_verify_stream.go b/app/server/model/plan/build_verify_stream.gonew file mode 100644index 00000000..16c96d5e--- /dev/null+++ b/app/server/model/plan/build_verify_stream.go@@ -0,0 +1,201 @@+package plan++import (+ "context"+ "encoding/json"+ "fmt"+ "log"+ "math/rand"+ "plandex-server/model"+ "plandex-server/types"+ "strings"+ "time"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func (fileState *activeBuildStreamFileState) listenStreamVerifyOutput(stream *openai.ChatCompletionStream) {++ filePath := fileState.filePath+ planId := fileState.plan.Id+ branch := fileState.branch++ activePlan := GetActivePlan(planId, branch)++ if activePlan == nil {+ log.Printf("listenStreamVerifyOutput - Active plan not found for plan ID %s on branch %s\n", planId, branch)+ return+ }++ defer stream.Close()++ // Create a timer that will trigger if no chunk is received within the specified duration+ timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ defer timer.Stop()++ for {+ select {+ case <-activePlan.Ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fileState.verifyRetryOrError(fmt.Errorf("listenStreamVerifyOutput - stream timeout due to inactivity for file '%s'", filePath))+ return+ default:+ response, err := stream.Recv()++ if err == nil {+ // Successfully received a chunk, reset the timer+ if !timer.Stop() {+ <-timer.C+ }+ timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ } else {+ log.Printf("listenStreamVerifyOutput - File %s: Error receiving stream chunk: %v\n", filePath, err)++ if err == context.Canceled {+ log.Printf("listenStreamVerifyOutput - File %s: Stream canceled\n", filePath)+ return+ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ if len(response.Choices) == 0 {+ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ choice := response.Choices[0]+ var content string+ delta := response.Choices[0].Delta++ if len(delta.ToolCalls) > 0 {+ content = delta.ToolCalls[0].Function.Arguments++ trimmed := strings.TrimSpace(content)+ if trimmed == "{%invalidjson%}" || trimmed == "``(no output)``````" {+ log.Println("File", filePath+":", "%invalidjson%} token in streamed chunk")+ fileState.verifyRetryOrError(fmt.Errorf("invalid JSON in streamed chunk for file '%s'", filePath))++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ return+ }++ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 1,+ Finished: false,+ }++ // log.Printf("%s: %s", filePath, content)+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })++ fileState.activeBuild.VerifyBuffer += content+ fileState.activeBuild.VerifyBufferTokens+++ }++ var streamed types.StreamedVerifyResult+ err = json.Unmarshal([]byte(fileState.activeBuild.VerifyBuffer), &streamed)++ if err == nil {+ log.Printf("listenStreamVerifyOutput - File %s: Parsed streamed verify result\n", filePath)+ // spew.Dump(streamed)++ if streamed.IsCorrect {+ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ } else {+ log.Printf("listenStreamVerifyOutput - File %s: Streamed verify result is incorrect: %s\n", filePath, streamed.Reasoning)++ fileState.incorrectlyUpdatedReasoning = streamed.Reasoning++ select {+ case <-activePlan.Ctx.Done():+ log.Println("listenStreamVerifyOutput - Context canceled. Exiting.")+ return+ case <-time.After(time.Duration(rand.Intn(1001)) * time.Millisecond):+ break+ }+ fileState.fixFileLineNums()+ }+ } else if len(delta.ToolCalls) == 0 {+ log.Println("listenStreamVerifyOutput - Stream chunk missing function call. Buffer:")+ // log.Println(spew.Sdump(response))++ if fileState.verifyFileNumRetry > 0 {+ buildInfo := &shared.BuildInfo{+ Path: filePath,+ NumTokens: 0,+ Finished: true,+ }+ activePlan.Stream(shared.StreamMessage{+ Type: shared.StreamMessageBuildInfo,+ BuildInfo: buildInfo,+ })+ fileState.onFinishBuildFile(fileState.initialPlanFileResult)+ } else {+ fileState.verifyRetryOrError(fmt.Errorf("listenStreamVerifyOutput - stream chunk missing function call. Reason: %s, File: %s", choice.FinishReason, filePath))+ }+ }+ }+ }++}++func (fileState *activeBuildStreamFileState) verifyRetryOrError(err error) {+ if fileState.verifyFileNumRetry < MaxBuildStreamErrorRetries {+ fileState.verifyFileNumRetry+++ fileState.activeBuild.VerifyBuffer = ""+ fileState.activeBuild.VerifyBufferTokens = 0+ log.Printf("Retrying verify file '%s' due to error: %v\n", fileState.filePath, err)++ // Exponential backoff+ time.Sleep(time.Duration(fileState.verifyFileNumRetry*fileState.verifyFileNumRetry)*time.Second + time.Duration(rand.Intn(1001))*time.Millisecond)++ fileState.buildFile()+ } else {+ fileState.onBuildFileError(err)+ }+}diff --git a/app/server/model/plan/commit_msg.go b/app/server/model/plan/commit_msg.goindex f5fac5d8..5e666e84 100644--- a/app/server/model/plan/commit_msg.go+++ b/app/server/model/plan/commit_msg.go@@ -12,12 +12,17 @@ import ("github.com/sashabaranov/go-openai")-func genPlanDescription(client *openai.Client, config shared.TaskRoleConfig, planId, branch string, ctx context.Context) (*db.ConvoMessageDescription, error) {+func genPlanDescription(client *openai.Client, config shared.ModelRoleConfig, planId, branch string, ctx context.Context) (*db.ConvoMessageDescription, error) {activePlan := GetActivePlan(planId, branch)if activePlan == nil {return nil, fmt.Errorf("active plan not found")}+ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }+descResp, err := model.CreateChatCompletionWithRetries(client,ctx,@@ -47,7 +52,7 @@ func genPlanDescription(client *openai.Client, config shared.TaskRoleConfig, pla},Temperature: config.Temperature,TopP: config.TopP,- ResponseFormat: config.OpenAIResponseFormat,+ ResponseFormat: responseFormat,},)@@ -86,3 +91,55 @@ func genPlanDescription(client *openai.Client, config shared.TaskRoleConfig, plaCommitMsg: desc.CommitMsg,}, nil}++func GenCommitMsgForPendingResults(client *openai.Client, config shared.ModelRoleConfig, current *shared.CurrentPlanState, ctx context.Context) (string, error) {+ s := ""++ num := 0+ for _, desc := range current.ConvoMessageDescriptions {+ if desc.MadePlan && desc.DidBuild && len(desc.BuildPathsInvalidated) == 0 && desc.AppliedAt == nil {+ s += desc.CommitMsg + "\n"+ num+++ }+ }++ if num <= 1 {+ return s, nil+ }++ messages := []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: prompts.SysPendingResults,+ },+ {+ Role: openai.ChatMessageRoleUser,+ Content: "Pending changes:\n\n" + s,+ },+ }++ resp, err := model.CreateChatCompletionWithRetries(+ client,+ ctx,+ openai.ChatCompletionRequest{+ Model: config.BaseModelConfig.ModelName,+ Messages: messages,+ Temperature: config.Temperature,+ TopP: config.TopP,+ },+ )++ if err != nil {+ fmt.Println("PlanSummary err:", err)++ return "", err+ }++ if len(resp.Choices) == 0 {+ return "", fmt.Errorf("no response from GPT")+ }++ content := resp.Choices[0].Message.Content++ return content, nil+}diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.goindex d7250813..1a2e3e43 100644--- a/app/server/model/plan/exec_status.go+++ b/app/server/model/plan/exec_status.go@@ -13,7 +13,7 @@ import ("github.com/sashabaranov/go-openai")-func ExecStatusShouldContinue(client *openai.Client, config shared.TaskRoleConfig, prompt, message string, ctx context.Context) (bool, error) {+func ExecStatusShouldContinue(client *openai.Client, config shared.ModelRoleConfig, prompt, message string, ctx context.Context) (bool, error) {log.Println("Checking if plan should continue based on exec status")// First try to determine if the plan should continue based on the last paragraph without calling the model@@ -46,6 +46,11 @@ func ExecStatusShouldContinue(client *openai.Client, config shared.TaskRoleConfilog.Println("Calling model to check if plan should continue")+ var responseFormat *openai.ChatCompletionResponseFormat+ if config.BaseModelConfig.HasJsonResponseMode {+ responseFormat = &openai.ChatCompletionResponseFormat{Type: "json_object"}+ }+resp, err := model.CreateChatCompletionWithRetries(client,ctx,@@ -64,7 +69,7 @@ func ExecStatusShouldContinue(client *openai.Client, config shared.TaskRoleConfi},},Messages: messages,- ResponseFormat: config.OpenAIResponseFormat,+ ResponseFormat: responseFormat,Temperature: config.Temperature,TopP: config.TopP,},diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.goindex c11fcb8a..7ffba784 100644--- a/app/server/model/plan/state.go+++ b/app/server/model/plan/state.go@@ -1,6 +1,7 @@package planimport (+ "context""log""plandex-server/db""plandex-server/types"@@ -18,8 +19,8 @@ func GetActivePlan(planId, branch string) *types.ActivePlan {return activePlans.Get(strings.Join([]string{planId, branch}, "|"))}-func CreateActivePlan(planId, branch, prompt string, buildOnly bool) *types.ActivePlan {- activePlan := types.NewActivePlan(planId, branch, prompt, buildOnly)+func CreateActivePlan(orgId, userId, planId, branch, prompt string, buildOnly bool) *types.ActivePlan {+ activePlan := types.NewActivePlan(orgId, userId, planId, branch, prompt, buildOnly)key := strings.Join([]string{planId, branch}, "|")activePlans.Set(key, activePlan)@@ -35,7 +36,7 @@ func CreateActivePlan(planId, branch, prompt string, buildOnly bool) *types.Actilog.Printf("Error setting plan %s status to stopped: %v\n", planId, err)}- DeleteActivePlan(planId, branch)+ DeleteActivePlan(orgId, userId, planId, branch)returncase apiErr := <-activePlan.StreamDoneCh:@@ -71,7 +72,7 @@ func CreateActivePlan(planId, branch, prompt string, buildOnly bool) *types.Acti}activePlan.CancelFn()- DeleteActivePlan(planId, branch)+ DeleteActivePlan(orgId, userId, planId, branch)return}}@@ -80,7 +81,38 @@ func CreateActivePlan(planId, branch, prompt string, buildOnly bool) *types.Actireturn activePlan}-func DeleteActivePlan(planId, branch string) {+func DeleteActivePlan(orgId, userId, planId, branch string) {+ ctx, cancelFn := context.WithCancel(context.Background())++ repoLockId, err := db.LockRepo(+ db.LockRepoParams{+ UserId: userId,+ OrgId: orgId,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: ctx,+ CancelFn: cancelFn,+ },+ )++ if err != nil {+ log.Printf("Error locking repo for plan %s: %v\n", planId, err)+ } else {++ defer func() {+ err := db.UnlockRepo(repoLockId)+ if err != nil {+ log.Printf("Error unlocking repo for plan %s: %v\n", planId, err)+ }+ }()++ err := db.GitClearUncommittedChanges(orgId, planId)+ if err != nil {+ log.Printf("Error clearing uncommitted changes for plan %s: %v\n", planId, err)+ }+ }+activePlans.Delete(strings.Join([]string{planId, branch}, "|"))}diff --git a/app/server/model/plan/stop.go b/app/server/model/plan/stop.goindex b627496c..4f511bc1 100644--- a/app/server/model/plan/stop.go+++ b/app/server/model/plan/stop.go@@ -17,13 +17,6 @@ func Stop(planId, branch, currentUserId, currentOrgId string) error {active.SummaryCancelFn()active.CancelFn()- // rollback repo in case there are uncommitted builds- err := db.GitClearUncommittedChanges(currentOrgId, planId)-- if err != nil {- return fmt.Errorf("error clearing uncommitted changes: %v", err)- }-if !active.BuildOnly && !active.RepliesFinished {num := active.MessageNum + 1diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.goindex b56188af..1cf9a2b2 100644--- a/app/server/model/plan/tell_exec.go+++ b/app/server/model/plan/tell_exec.go@@ -17,10 +17,10 @@ import ("github.com/sashabaranov/go-openai")-func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {+func Tell(clients map[string]*openai.Client, 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(client, plan, branch, auth, req.Prompt, false)+ _, err := activatePlan(clients, plan, branch, auth, req.Prompt, false)if err != nil {log.Printf("Error activating plan: %v\n", err)@@ -28,7 +28,7 @@ func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.Serve}go execTellPlan(- client,+ clients,plan,branch,auth,@@ -43,7 +43,7 @@ func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.Serve}func execTellPlan(- client *openai.Client,+ clients map[string]*openai.Client,plan *db.Plan,branch string,auth *types.ServerAuth,@@ -96,7 +96,7 @@ func execTellPlan(}state := &activeTellStreamState{- client: client,+ clients: clients,req: req,auth: auth,currentOrgId: currentOrgId,@@ -196,12 +196,13 @@ func execTellPlan(promptTokens = prompts.PromptWrapperTokens + numPromptTokens}- state.tokensBeforeConvo = prompts.CreateSysMsgNumTokens + modelContextTokens + promptTokens+ state.tokensBeforeConvo = prompts.CreateSysMsgNumTokens + modelContextTokens + state.latestSummaryTokens + promptTokens// print out breakdown of token usagelog.Printf("System message tokens: %d\n", prompts.CreateSysMsgNumTokens)log.Printf("Context tokens: %d\n", modelContextTokens)log.Printf("Prompt tokens: %d\n", promptTokens)+ log.Printf("Latest summary tokens: %d\n", state.latestSummaryTokens)log.Printf("Total tokens before convo: %d\n", state.tokensBeforeConvo)if state.tokensBeforeConvo > state.settings.GetPlannerEffectiveMaxTokens() {@@ -216,7 +217,7 @@ func execTellPlan(return}- if !state.summarizeMessagesIfNeeded() {+ if !state.summarizeEarlyMessagesIfNeeded() {return}@@ -246,6 +247,8 @@ func execTellPlan(Role: openai.ChatMessageRoleUser,Content: prompts.GetWrappedPrompt(lastMessage.Content),}++ state.userPrompt = lastMessage.Content} else {// otherwise we'll use the continue promptpromptMessage = &openai.ChatCompletionMessage{@@ -263,6 +266,8 @@ func execTellPlan(prompt = prompts.AutoContinuePrompt}+ state.userPrompt = prompt+promptMessage = &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,Content: prompts.GetWrappedPrompt(prompt),@@ -336,19 +341,22 @@ func execTellPlan(}}- // log.Println("\n\nMessages:")- // for _, message := range messages {+ log.Printf("\n\nMessages: %d\n", len(state.messages))+ // for _, message := range state.messages {// log.Printf("%s: %s\n", message.Role, message.Content)// }modelReq := openai.ChatCompletionRequest{- Model: state.settings.ModelSet.Planner.BaseModelConfig.ModelName,+ Model: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,Messages: state.messages,Stream: true,- Temperature: state.settings.ModelSet.Planner.Temperature,- TopP: state.settings.ModelSet.Planner.TopP,+ Temperature: state.settings.ModelPack.Planner.Temperature,+ TopP: state.settings.ModelPack.Planner.TopP,}+ envVar := state.settings.ModelPack.Planner.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]+stream, err := model.CreateChatCompletionStreamWithRetries(client, active.ModelStreamCtx, modelReq)if err != nil {log.Printf("Error starting reply stream: %v\n", err)@@ -384,7 +392,7 @@ func execTellPlan(// spew.Dump(pendingBuildsByPath)xbuildState := &activeBuildStreamState{- client: client,+ clients: clients,auth: auth,currentOrgId: currentOrgId,currentUserId: currentUserId,diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.goindex 7ac469f1..4dcd69ad 100644--- a/app/server/model/plan/tell_load.go+++ b/app/server/model/plan/tell_load.go@@ -13,7 +13,7 @@ import ()func (state *activeTellStreamState) loadTellPlan() error {- client := state.client+ clients := state.clientsreq := state.reqauth := state.authplan := state.plan@@ -61,6 +61,7 @@ func (state *activeTellStreamState) loadTellPlan() error {var convo []*db.ConvoMessagevar summaries []*db.ConvoSummaryvar settings *shared.PlanSettings+ var latestSummaryTokens int// get name for plan and rename it's a draftgo func() {@@ -73,7 +74,10 @@ func (state *activeTellStreamState) loadTellPlan() error {settings = resif plan.Name == "draft" {- name, err := model.GenPlanName(client, settings.ModelSet.Namer, req.Prompt)+ envVar := settings.ModelPack.Namer.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ name, err := model.GenPlanName(client, settings.ModelPack.Namer, req.Prompt)if err != nil {log.Printf("Error generating plan name: %v\n", err)@@ -81,7 +85,7 @@ func (state *activeTellStreamState) loadTellPlan() error {return}- tx, err := db.Conn.Begin()+ tx, err := db.Conn.Beginx()if err != nil {log.Printf("Error starting transaction: %v\n", err)errCh <- fmt.Errorf("error starting transaction: %v", err)@@ -214,6 +218,16 @@ func (state *activeTellStreamState) loadTellPlan() error {log.Printf("got %d plan summaries", len(summaries))+ if len(summaries) > 0 {+ var err error+ latestSummaryTokens, err = shared.GetNumTokens(summaries[len(summaries)-1].Summary)+ if err != nil {+ log.Printf("Error getting latest summary tokens: %v\n", err)+ innerErrCh <- fmt.Errorf("error getting latest summary tokens: %v", err)+ return+ }+ }+innerErrCh <- nil}()@@ -277,6 +291,7 @@ func (state *activeTellStreamState) loadTellPlan() error {state.modelContext = modelContextstate.convo = convostate.summaries = summaries+ state.latestSummaryTokens = latestSummaryTokensstate.settings = settingsreturn nildiff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.goindex dc2cf4a4..7e349372 100644--- a/app/server/model/plan/tell_stream.go+++ b/app/server/model/plan/tell_stream.go@@ -17,7 +17,7 @@ import (const MaxAutoContinueIterations = 50type activeTellStreamState struct {- client *openai.Client+ clients map[string]*openai.Clientreq *shared.TellPlanRequestauth *types.ServerAuthcurrentOrgId string@@ -31,6 +31,8 @@ type activeTellStreamState struct {missingFileResponse shared.RespondMissingFileChoicesummaries []*db.ConvoSummarysummarizedToMessageId string+ latestSummaryTokens int+ userPrompt stringpromptMessage *openai.ChatCompletionMessagereplyParser *types.ReplyParserreplyNumTokens int@@ -42,7 +44,7 @@ type activeTellStreamState struct {func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStream) {defer stream.Close()- client := state.client+ clients := state.clientsauth := state.authreq := state.reqplan := state.plan@@ -133,17 +135,20 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionSt}// log.Println("summarize convo:", spew.Sdump(convo))- if len(convo) > 0 {- // summarize in the background- go summarizeConvo(client, settings.ModelSet.PlanSummary, summarizeConvoParams{- planId: planId,- branch: branch,- convo: convo,- summaries: summaries,- promptMessage: promptMessage,- currentOrgId: currentOrgId,- }, active.SummaryCtx)- }+ log.Println("summarize convo")+ envVar := settings.ModelPack.PlanSummary.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ // summarize in the background+ go summarizeConvo(client, settings.ModelPack.PlanSummary, summarizeConvoParams{+ planId: planId,+ branch: branch,+ convo: convo,+ summaries: summaries,+ userPrompt: state.userPrompt,+ currentOrgId: currentOrgId,+ currentReply: active.CurrentReplyContent,+ }, active.SummaryCtx)log.Println("Locking repo to store assistant reply and description")@@ -221,7 +226,11 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionSt}} else {log.Println("Generating plan description")- description, err = genPlanDescription(client, settings.ModelSet.CommitMsg, planId, branch, active.Ctx)++ envVar := settings.ModelPack.CommitMsg.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ description, err = genPlanDescription(client, settings.ModelPack.CommitMsg, planId, branch, active.Ctx)if err != nil {state.onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)return@@ -259,7 +268,10 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStprompt = promptMessage.Content}- shouldContinue, err = ExecStatusShouldContinue(client, settings.ModelSet.ExecStatus, prompt, assistantMsg.Message, active.Ctx)+ envVar := state.settings.ModelPack.ExecStatus.BaseModelConfig.ApiKeyEnvVar+ client := clients[envVar]++ shouldContinue, err = ExecStatusShouldContinue(client, settings.ModelPack.ExecStatus, prompt, assistantMsg.Message, active.Ctx)if err != nil {state.onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)errCh <- err@@ -309,7 +321,7 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStif req.AutoContinue && shouldContinue && iteration < MaxAutoContinueIterations {log.Println("Auto continue plan")// continue plan- execTellPlan(client, plan, branch, auth, req, iteration+1, "", false)+ execTellPlan(clients, plan, branch, auth, req, iteration+1, "", false)} else {var buildFinished boolUpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {@@ -454,7 +466,7 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionSt// continue planexecTellPlan(- client,+ clients,plan,branch,auth,@@ -486,7 +498,7 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStif req.BuildMode == shared.BuildModeAuto {log.Printf("Queuing build for %s\n", file)buildState := &activeBuildStreamState{- client: client,+ clients: clients,auth: auth,currentOrgId: currentOrgId,currentUserId: currentUserId,diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.goindex 95d3d521..744a9e26 100644--- a/app/server/model/plan/tell_summary.go+++ b/app/server/model/plan/tell_summary.go@@ -8,6 +8,7 @@ import ("net/http""plandex-server/db""plandex-server/model"+ "plandex-server/model/prompts""time""github.com/davecgh/go-spew/spew"@@ -15,7 +16,7 @@ import ("github.com/sashabaranov/go-openai")-func (state *activeTellStreamState) summarizeMessagesIfNeeded() bool {+func (state *activeTellStreamState) summarizeEarlyMessagesIfNeeded() bool {convo := state.convosummaries := state.summariestokensBeforeConvo := state.tokensBeforeConvo@@ -112,12 +113,25 @@ func (state *activeTellStreamState) summarizeMessagesIfNeeded() bool {}}+ var latestSummary *db.ConvoSummary+ if len(summaries) > 0 {+ latestSummary = summaries[len(summaries)-1]+ }+if summary == nil {for _, convoMessage := range convo {state.messages = append(state.messages, openai.ChatCompletionMessage{Role: convoMessage.Role,Content: convoMessage.Message,})++ // add the latest summary as a conversation message if this is the last message summarized, in order to reinforce the current state of the plan to the model+ if latestSummary != nil && convoMessage.Id == latestSummary.LatestConvoMessageId {+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: latestSummary.Summary,+ })+ }}} else {if (tokensBeforeConvo + summary.Tokens) > state.settings.GetPlannerEffectiveMaxTokens() {@@ -141,6 +155,14 @@ func (state *activeTellStreamState) summarizeMessagesIfNeeded() bool {Role: convoMessage.Role,Content: convoMessage.Message,})++ // add the latest summary as a conversation message if this is the last message summarized, in order to reinforce the current state of the plan to the model+ if latestSummary != nil && convoMessage.Id == latestSummary.LatestConvoMessageId {+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: latestSummary.Summary,+ })+ }}}}@@ -148,13 +170,44 @@ func (state *activeTellStreamState) summarizeMessagesIfNeeded() bool {return true}+// func (state *activeTellStreamState) insertLatestSummary(){+// if len(state.summaries) == 0 {+// return+// }++// latestSummary := state.summaries[len(state.summaries)-1]+// var insertAtIndex int++// for i, convoMessage := range state.convo {+// if convoMessage.Id == latestSummary.LatestConvoMessageId {+// insertAtIndex = i+// break+// }+// }++// if insertAtIndex < 2 {+// return+// }++// state.convo = append(state.convo[:insertAtIndex], append([]*db.ConvoMessage{+// {+// Id: latestSummary.Id, // use summary id as message id+// Role: openai.ChatMessageRoleAssistant,+// Message: latestSummary.Summary,+// CreatedAt: latestSummary.CreatedAt,+// },+// }, state.convo[insertAtIndex:]...)...)++// }+type summarizeConvoParams struct {- planId string- branch string- convo []*db.ConvoMessage- summaries []*db.ConvoSummary- promptMessage *openai.ChatCompletionMessage- currentOrgId string+ planId string+ branch string+ convo []*db.ConvoMessage+ summaries []*db.ConvoSummary+ userPrompt string+ currentReply string+ currentOrgId string}func summarizeConvo(client *openai.Client, config shared.ModelRoleConfig, params summarizeConvoParams, ctx context.Context) error {@@ -164,8 +217,9 @@ func summarizeConvo(client *openai.Client, config shared.ModelRoleConfig, paramsbranch := params.branchconvo := params.convosummaries := params.summaries- promptMessage := params.promptMessage+ userPrompt := params.userPromptcurrentOrgId := params.currentOrgId+ currentReply := params.currentReplylog.Println("Generating plan summary for planId:", planId)@@ -199,6 +253,7 @@ func summarizeConvo(client *openai.Client, config shared.ModelRoleConfig, params})latestMessageId = convoMessage.IdlatestMessageSummarizedAt = convoMessage.CreatedAt+ numMessagesSummarized++}} else {summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{@@ -206,30 +261,66 @@ func summarizeConvo(client *openai.Client, config shared.ModelRoleConfig, paramsContent: latestSummary.Summary,})+ var found bool+ for _, convoMessage := range convo {+ if convoMessage.Id == latestSummary.LatestConvoMessageId {+ found = true+ continue+ }+ if found {+ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ numMessagesSummarized+++ }+ }+latestConvoMessage := convo[len(convo)-1]latestMessageId = latestConvoMessage.IdlatestMessageSummarizedAt = latestConvoMessage.CreatedAt+ }++ if userPrompt != "" {+ active := GetActivePlan(planId, branch)+ if active == nil {+ log.Printf("Active plan not found for plan ID %s and branch %s\n", planId, branch)+ return fmt.Errorf("active plan not found for plan ID %s and branch %s", planId, branch)+ }++ if userPrompt != prompts.UserContinuePrompt && userPrompt != prompts.AutoContinuePrompt {+ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: userPrompt,+ })+ }++ log.Println("Current reply content:")+ log.Println(active.CurrentReplyContent)summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{- Role: latestConvoMessage.Role,- Content: latestConvoMessage.Message,+ Role: openai.ChatMessageRoleAssistant,+ Content: active.CurrentReplyContent,})}- if promptMessage != nil {- summaryMessages = append(summaryMessages, promptMessage, &openai.ChatCompletionMessage{+ if currentReply != "" {+ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleAssistant,- Content: GetActivePlan(planId, branch).CurrentReplyContent,+ Content: currentReply,})}log.Printf("Calling model for plan summary. Summarizing %d messages\n", len(summaryMessages))+ log.Println("Summary messages:")+ spew.Dump(summaryMessages)+summary, err := model.PlanSummary(client, config, model.PlanSummaryParams{Conversation: summaryMessages,LatestConvoMessageId: latestMessageId,LatestConvoMessageCreatedAt: latestMessageSummarizedAt,- NumMessages: numMessagesSummarized + 1,+ NumMessages: numMessagesSummarized,OrgId: currentOrgId,PlanId: planId,}, ctx)diff --git a/app/server/model/prompts/build.go b/app/server/model/prompts/build.goindex bac4a92f..85206cf0 100644--- a/app/server/model/prompts/build.go+++ b/app/server/model/prompts/build.go@@ -2,154 +2,222 @@ package promptsimport ("fmt"- "strings"+ "github.com/plandex/plandex/shared""github.com/sashabaranov/go-openai""github.com/sashabaranov/go-openai/jsonschema")-func GetBuildSysPrompt(filePath, currentState, desc, changes string) string {- currentStateWithLineNums := ""- var lastLineNum int- for i, line := range strings.Split(currentState, "\n") {- currentStateWithLineNums += fmt.Sprintf("%d: %s\n", i+1, line)- lastLineNum = i + 1- }+func GetBuildLineNumbersSysPrompt(filePath, currentState, desc, changes string) string {+ // hash := sha256.Sum256([]byte(currentState))+ // sha := hex.EncodeToString(hash[:])++ // log.Println("GetBuildLineNumbersSysPrompt currentState sha:", sha)++ currentStateWithLineNums := shared.AddLineNums(currentState)++ return getListChangesLineNumsPrompt() + "\n\n" + getBuildCurrentStatePrompt(filePath, currentStateWithLineNums) + "\n\n" + getBuildPromptWithLineNums(desc, changes)+}++func GetBuildFixesLineNumbersSysPrompt(desc, changes, updated, reasoning string) string {+ // hash := sha256.Sum256([]byte(updated))+ // sha := hex.EncodeToString(hash[:])+ // log.Println("GetBuildFixesLineNumbersSysPrompt currentState sha:", sha)- return getListChangesPrompt(lastLineNum) + "\n\n" + getBuildCurrentStatePrompt(filePath, currentStateWithLineNums) + "\n\n" + getBuildPrompt(desc, changes)+ updatedWithLineNums := shared.AddLineNums(updated)++ return getFixChangesLineNumsPrompt() + "\n\n" + getBuildPromptForFixesWithLineNums(desc, changes, updatedWithLineNums, reasoning)}-func getBuildPrompt(desc, changes string) string {+func getBuildPromptWithLineNums(desc, changes string) string {s := ""if desc != "" {s += "Description of the proposed updates from AI-generated plan:\n```\n" + desc + "\n```\n\n"}- withLineNums := ""- for i, line := range strings.Split(changes, "\n") {- withLineNums += fmt.Sprintf("%d: %s\n", i+1, line)+ s += "Proposed updates:\n```\n" + changes + "\n```"++ s += "\n\n" + "Now call the 'listChangesWithLineNums' function with a valid JSON array of changes according to your instructions. You must always call 'listChangesWithLineNums' with one or more valid changes. Don't call any other function."++ return s+}++func getBuildPromptForFixesWithLineNums(desc, changes, updated, reasoning string) string {+ s := ""++ if desc != "" {+ s += "Description of the proposed updates from AI-generated plan:\n```\n" + desc + "\n```\n\n"}- s += "Proposed updates:\n```\n" + withLineNums + "\n```"+ s += "Proposed updates:\n```\n" + changes + "\n```"++ s += fmt.Sprintf("**The incorrect updated file is:**\n```\n%s\n```\n\n**The problems with the file are:**\n\n%s", updated, reasoning)- s += "\n\n" + "Now call the 'listChanges' function with a valid JSON array of changes according to your instructions. You must always call 'listChanges' with one or more valid changes. Don't call any other function."+ s += "\n\n" + "Now call the 'listChangesWithLineNums' function with a valid JSON array of changes according to your instructions. You must always call 'listChangesWithLineNums' with one or more valid changes. Don't call any other function."return s}-func getBuildCurrentStatePrompt(filePath, withLineNums string) string {- if withLineNums == "" {+func getBuildCurrentStatePrompt(filePath, currentState string) string {+ if currentState == "" {return ""}- return fmt.Sprintf("**The current file is %s. Original state of the file:**\n```\n%s\n```", filePath, withLineNums) + "\n\n"+ return fmt.Sprintf("**The current file is %s. Original state of the file:**\n```\n%s\n```", filePath, currentState) + "\n\n"}-func getListChangesPrompt(lastLineNum int) string {- return fmt.Sprintf(`- You are an AI that analyzes a code file and an AI-generated plan to update the code file and produces a list of changes.-- [YOUR INSTRUCTIONS]- Call the 'listChanges' function with a valid JSON object that includes the 'changes' keys.+const replacementIntro = `+You are an AI that analyzes a code file and an AI-generated plan to update the code file and produces a list of changes.+`- 'changes': An array of NON-OVERLAPPING changes. Each change is an object with properties: 'summary', 'section', 'old', and 'new'.-- The 'summary' property is a brief summary of the change. At the end of the summary, consider if this change will overlap with any ensuing changes. If it will, include those changes in *this* change instead. Continue the summary and includes those ensuing changes that would otherwise overlap. Changes that remove code are especially likely to overlap with ensuing changes.-- 'summary' examples:- - 'Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar.'- - 'Update the Org model to include StripeCustomerId and StripeSubscriptionId fields.'- - 'Add function ExecQuery to execute a query.'-- 'summary' that is larger to avoid overlap:- - 'Insert function ExecQuery after GetResults function in loop body. Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar. Add function ExecQuery to execute a query.'-- The 'section' property is a description of what section of code from the original file will be replaced.-- Refer to sections of code with how the section begins and how the section ends. It must be extremely clear which line(s) of code from the original file will be replaced. Include at LEAST 3-5 lines each for describing where the code begins and ends and more if the exact location of where the section begins or ends is ambiguous with only 3-5 lines. Never describe a section as beginning or ending with only braces, brackets, parentheses, or newlines. Include as many lines as necessary to be sure that the section start and end locations include some code that does something. Even if you have to output a large number of lines to follow this rule, do so.+const lineNumsFunctionCallPrompt = `+Call the 'listChangesWithLineNums' function with a valid JSON object that includes the 'changes' keys.++'changes': An array of NON-OVERLAPPING changes. Each change is an object with properties: 'summary', 'hasChange', 'old', 'startLineIncludedReasoning', 'startLineIncluded', 'endLineIncludedReasoning', 'endLineIncluded', and 'new'.++Note: all line numbers that are used below are prefixed with 'pdx-', like this 'pdx-5: for i := 0; i < 10; i++ {'. This is to help you identify the line numbers in the file. You *must* include the 'pdx-' prefix in the line numbers in the 'old' property.+`++const summaryChangePrompt = `+The 'summary' property is a brief summary of the change. At the end of the summary, consider if this change will overlap with any ensuing changes. If it will, include those changes in *this* change instead. Continue the summary and includes those ensuing changes that would otherwise overlap. Changes that remove code are especially likely to overlap with ensuing changes.++'summary' examples:+ - 'Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar.'+ - 'Update the Org model to include StripeCustomerId and StripeSubscriptionId fields.'+ - 'Add function ExecQuery to execute a query.'- Sections of code begin and end with *entire* lines. Never begin or end a section in the middle of a line.+'summary' that is larger to avoid overlap:+ - 'Insert function ExecQuery after GetResults function in loop body. Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar. Add function ExecQuery to execute a query.'- Abbreviate long lines of code with '...'. Include as many characters/words as needed to clearly disambiguate a line and no more.+The 'hasChange' property is a boolean that indicates whether there is anything to change. If there is nothing to change, set 'hasChange' to false. If there is something to change, set 'hasChange' to true.+`- 'section' examples:- ---- Begins: 'for i := 0; i < 10; i++ {...'- Ends: ' }\n }\n}'- ---+const lineNumsOldPrompt = `+The 'old' property is an object with 2 properties: 'startLineString' and 'endLineString'.++ 'startLineString' is the **entire, exact line** where the section to be replaced begins in the original file, including the line number. Unless it's the first change, 'startLineString' ABSOLUTELY MUST begin with a line number that is HIGHER than both the 'endLineString' of the previous change and the 'startLineString' of the previous change.++ If the previous change's 'endLineString' starts with 'pdx-75: ', then the current change's 'startLineString' MUST start with 'pdx-76: ' or higher. It MUST NOT be 'pdx-75: ' or lower. If the previous change's 'startLineString' starts with 'pdx-88: ' and the previous change's 'endLineString' is an empty string, then the current change's 'startLineString' MUST start with 'pdx-89: ' or higher. If the previous change's 'startLineString' starts with 'pdx-100: ' and the previous change's 'endLineString' starts with 'pdx-105: ', then the current change's 'startLineString' MUST start with 'pdx-106: ' or higher.++ 'endLineString' is the **entire, exact line** where the section to be replaced ends in the original file. Pay careful attention to spaces and indentation. 'startLineString' and 'endLineString' must be *entire lines* and *not partial lines*. Even if a line is very long, you must include the entire line, including the line number and all text on the line.++ **For a single line replacement, 'endLineString' MUST be an empty string.**- The 'old' property is an object with 5 properties: 'maybeStartLine', 'maybeEndLine', 'err', 'startLine' and 'endLine'.+ 'endLineString' MUST ALWAYS come *after* 'startLineString' in the original file. It must start with a line number that is HIGHER than the 'startLineString' line number. If 'startLineString' starts with 'pdx-22: ', then 'endLineString' MUST either be an empty string (for a single line replacement) or start with 'pdx-23: ' or higher (for a multi-line replacement).- 'maybeStartLine' is the line number where the section to be replaced begins in the original file. 'maybeEndLine' is the line number where the section to be replaced ends in the original file. For a single line replacement, 'maybeStartLine' and 'maybeEndLine' will be the same.+ If 'hasChange' is false, both 'startLineString' and 'endLineString' must be empty strings. If 'hasChange' is true, 'startLineString' and 'endLineString' must be valid strings.+`- 'maybeEndLine' MUST ABSOLUTELY ALWAYS be greater than or equal to 'maybeStartLine'.-- Line numbers in 'maybeStartLine' and 'maybeEndLine' are 1-indexed. 1 is the minimum line number. The maximum line number is %d, the number of lines in the original file. 'maybeStartLine' and 'maybeEndLine' MUST be valid line numbers in the original file, greater than or equal to 1 and less than or equal to %d.-- You MUST refer to 1-indexed line numbers exactly as they are labeled in the original file. If the 'maybeStartLine' or 'maybeEndLine' is the first line of the file, 'maybeStartLine' or 'maybeEndLine' will be 1.-- If the 'maybeStartLine' or 'maybeEndLine' is the last line of the file, 'maybeStartLine' or 'maybeEndLine' will be %d.-- Both 'maybeStartLine' and 'maybeEndLine' must NEVER be 0, -1, or any other number less than 1. They must NEVER be a number higher than %d.+const changeLineInclusionAndNewPrompt = `+The 'startLineIncludedReasoning' property is a string that very briefly explains whether 'startLineString' should be included in the 'new' property. For example, if the 'startLineString' is the closing bracket of a function and you are adding another function after it, you *MUST* include the 'startLineString' in the 'new' property, or the previous function will lose its closing bracket when the change is applied. Similarly, if the 'startLineString' is a function definition and you are updating the body of the function, you *MUST* also include 'startLineString' so that they function definition is not removed. The only time 'startLineString' should not be included in 'new' is if it is a line that should be removed or replaced. Generalize the above to all types of code blocks, changes, and syntax to ensure the 'new' property will not remove or overwrite code that should not be removed or overwritten. That also includes newlines, line breaks, and indentation.- 'err' is a string. If 'maybeEndLine' is greater than or equal to 'maybeStartLine', 'err' must be an empty string. Otherwise, 'err' must be a string that describes how the section from the original file can be better described in order to make the line numbers unambiguous and correct.+'startLineIncluded' is a boolean that indicates whether 'startLineString' should be included in the 'new' property. If 'startLineIncluded' is true, 'startLineString' MUST be included in the 'new' property. If 'startLineIncluded' is false, 'startLineString' MUST not be included in the 'new' property.- 'startLine' is the line number where the section to be replaced begins in the original file. *'startLine' MUST be an integer greater than 0.* If 'err' is not set or is an empty string, 'startLine' must be equal to 'maybeStartLine'. If 'err' is set and is *not* an empty string, 'startLine' should be the *corrected* line number where the section to be replaced begins in the original file.-- 'endLine' is the line number where the section to be replaced ends in the original file. *'endLine' MUST be an integer greater than 0.* If 'err' is not set or is an empty string, 'endLine' must be equal to 'maybeEndLine'. If 'err' is set and is *not* an empty string, 'endLine' should be the *corrected* line number where the section to be replaced ends in the original file.+The 'endLineIncludedReasoning' property is a string that very briefly explains whether 'endLineString' should be included in the 'new' property. For example, if the 'endLineString' is the opening bracket of a function and you are adding another function before it, you *MUST* include the 'endLineString' in the 'new' property, or the subsequent function will lose its opening bracket when the change is applied. Similarly, if the 'endLineString' is the closing bracket of a function and you are updating the body of the function, you *MUST* also include 'endLineString' so that the closing bracket not removed. The only time 'endLineString' should not be included in 'new' is if it is a line that should be removed or replaced. Generalize the above to all types of code blocks, changes, and syntax to ensure the 'new' property will not remove or overwrite code that should not be removed or overwritten. That also includes newlines, line breaks, and indentation.- The 'new' property is a string that represents the new code that will replace the old code. The new code must be valid and consistent with the intention of the plan. If the proposed update is to remove code, the 'new' property should be an empty string.+'endLineIncluded' is a boolean that indicates whether 'endLineString' should be included in the 'new' property. If 'endLineIncluded' is true, 'endLineString' MUST be included in the 'new' property. If 'endLineIncluded' is false, 'endLineString' MUST not be included in the 'new' property.++The 'new' property is a string that represents the new code that will replace the old code. The new code must be valid and consistent with the intention of the plan. If the proposed update is to remove code, the 'new' property should be an empty string. Be precise about newlines, line breaks, and indentation. 'new' must include only full lines of code and *no partial lines*. Do NOT include line numbers in the 'new' property.++If the proposed update includes references to the original code in comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or **any other reference to the original code,** you *MUST* ensure that the comment making the reference is *NOT* included in the 'new' property. Instead, include the **exact code** from the original file that the comment is referencing. Do not be overly strict in identifying references. If there is a comment that seems like it could plausibly be a reference and there is code in the original file that could plausibly be the code being referenced, then treat that as a reference and handle it accordingly by including the code from the original file in the 'new' property instead of the comment. YOU MUST NOT MISS ANY REFERENCES.++If the 'startLineIncluded' property is true, the 'startLineString' MUST be the first line of 'new'. If the 'startLineIncluded' property is false, the 'startLineString' MUST NOT be included in 'new'. If the 'endLineIncluded' property is true, the 'endLineString' MUST be the last line of 'new'. If the 'endLineIncluded' property is false, the 'endLineString' MUST NOT be included in 'new'.++If the 'hasChange' property is false, the 'new' property must be an empty string. If the 'hasChange' property is true, the 'new' property must be a valid string.+`++const lineNumsRulesPrompt = `+You ABSOLUTELY MUST NOT generate overlapping changes. Group smaller changes together into larger changes where necessary to avoid overlap. Only generate multiple changes when you are ABSOLUTELY CERTAIN that they do not overlap--otherwise group them together into a single change. If changes are close to each other (within several lines), group them together into a single change. Lean towards grouping changes together and making fewer, larger changes rather than many small changes, unless the changes are completely independent of each other and not close to each other in the file. It's ok to have just one change if that's all that's needed.++Changes must be ordered in the array according to the order they appear in the file. The 'startLineString' of each 'old' property must come after the 'endLineString' of the previous 'old' property. Changes MUST NOT overlap. If a change is dependent on another change or intersects with it, group those changes together into a single change.+`++const changeRulesPrompt = `+Apply changes intelligently in order to avoid syntax errors, breaking code, or removing code from the original file that should not be removed. Consider the reason behind the update and make sure the result is consistent with the intention of the plan.++You ABSOLUTELY MUST NOT overwrite or delete code from the original file unless the plan *clearly intends* for the code to be overwritten or removed. Do NOT replace a full section of code with only new code unless that is the clear intention of the plan. Instead, merge the original code and the proposed changes together intelligently according to the intention of the plan.++Pay *EXTREMELY close attention* to opening and closing brackets, parentheses, and braces. Never leave them unbalanced when the changes are applied. Also pay *EXTREMELY close attention* to newlines and indentation. Make sure that the indentation of the new code is consistent with the indentation of the original code, and syntactically correct.+`++const lineNumsJsonPrompt = `+The 'listChangesWithLineNums' function MUST be called *valid JSON*. Double quotes within json properties of the 'listChangesWithLineNums' function call parameters JSON object *must be properly escaped* with a backslash.+`++func getListChangesLineNumsPrompt() string {++ return replacementIntro + `++ [YOUR INSTRUCTIONS]+ ` + lineNumsFunctionCallPrompt + `++ ` + summaryChangePrompt + `++ ` + lineNumsOldPrompt + `- If the proposed update includes references to the original code in comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or **any other reference to the original code,** you *MUST* ensure that the comment making the reference is *NOT* included in the 'new' property. Instead, include the **exact code** from the original file that the comment is referencing. Do not be overly strict in identifying references. If there is a comment that seems like it could plausibly be a reference and there is code in the original file that could plausibly be the code being referenced, then treat that as a reference and handle it accordingly by including the code from the original file in the 'new' property instead of the comment. YOU MUST NOT MISS ANY REFERENCES.+ ` + changeLineInclusionAndNewPrompt + `Example function call with all keys:---- listChanges([{- summary: "Insert function ExecQuery after GetResults function in loop body.",- section' "Begins: 'for i := 0; i < 10; i++ {...'\nEnds: ' }\n }\n}'",- old: {- maybeStartLine: 5,- maybeEndLine: 10,- err: "",- startLine: 5,- endLine: 10,+ listChangesWithLineNums([{+ summary: "Fix syntax error in loop body.",+ old: {+ startLineString: "pdx-5: for i := 0; i < 10; i++ { ",+ endLineString: "pdx-7: }",},- new: " execQuery()\n }\n }\n}",+ new: "for i := 0; i < 10; i++ {\n execQuery()\n }\n }\n}",}])---- Example function calls with errors:- ---- listChanges([{- summary: "Insert function ExecQuery after GetResults function in loop body.",- section' "Begins: 'for i := 0; i < 10; i++ {...'\nEnds: ' }\n }\n}'",- old: {- maybeStartLine: 5,- maybeEndLine: 4,- err: "maybeStartLine is greater than maybeEndLine. The 'begins' section is ambiguous. Here's a better 'begins' section: '// Loop up to 10\nfor i := 0; i < 10; i++ {\n // Loop body'",- startLine: 5,- endLine: 10,- },- new: " execQuery()\n }\n }\n}",- }])+ ` + lineNumsRulesPrompt + `++ ` + changeRulesPrompt + `++ ` + lineNumsJsonPrompt + `++ [END YOUR INSTRUCTIONS]+`+}- You ABSOLUTELY MUST NOT generate overlapping changes. The start line of each change MUST be **greater than** the end line of the previous change. Group smaller changes together into larger changes where necessary to avoid overlap. Only generate multiple changes when you are ABSOLUTELY CERTAIN that they do not overlap--otherwise group them together into a single change.+func getFixChangesLineNumsPrompt() string {- Apply changes intelligently in order to avoid syntax errors, breaking code, or removing code from the original file that should not be removed. Consider the reason behind the update and make sure the result is consistent with the intention of the plan.+ return `+ You are an AI that analyzes an incorrect updated file, the changes that should have been applied to the file, a description of the problems with the file, and a plan to fix them, and then produces a list of changes to apply to the *incorrect updated file* that will fix *ALL* the problems.- You ABSOLUTELY MUST NOT ovewrite or delete code from the original file unless the plan *clearly intends* for the code to be overwritten or removed. Do NOT replace a full section of code with only new code unless that is the clear intention of the plan. Instead, merge the original code and the proposed changes together intelligently according to the intention of the plan.+ [YOUR INSTRUCTIONS]+ ` + lineNumsFunctionCallPrompt + `- Pay *EXTREMELY close attention* to opening and closing brackets, parentheses, and braces. Never leave them unbalanced when the changes are applied.+ ` + summaryChangePrompt + `- The 'listChanges' function MUST be called *valid JSON*. Double quotes within json properties of the 'listChanges' function call parameters JSON object *must be properly escaped* with a backslash.+ ` + lineNumsOldPrompt + `++ ` + changeLineInclusionAndNewPrompt + `++ Example function call with all keys:+ ---+ listChangesWithLineNums([{+ summary: "Fix syntax error in loop body.",+ old: {+ startLineString: "pdx-5: for i := 0; i < 10; i++ { ",+ endLineString: "pdx-7: }",+ },+ new: "for i := 0; i < 10; i++ {\n execQuery()\n }\n }\n}",+ }])+ ---++ ` + lineNumsRulesPrompt + `++ ` + changeRulesPrompt + `++ ` + lineNumsJsonPrompt + `[END YOUR INSTRUCTIONS]-`, lastLineNum, lastLineNum, lastLineNum, lastLineNum)+`}var ListReplacementsFn = openai.FunctionDefinition{- Name: "listChanges",+ Name: "listChangesWithLineNums",Parameters: &jsonschema.Definition{Type: jsonschema.Object,Properties: map[string]jsonschema.Definition{@@ -161,38 +229,176 @@ var ListReplacementsFn = openai.FunctionDefinition{"summary": {Type: jsonschema.String,},- "section": {- Type: jsonschema.String,+ "hasChange": {+ Type: jsonschema.Boolean,},"old": {Type: jsonschema.Object,Properties: map[string]jsonschema.Definition{- "maybeStartLine": {- Type: jsonschema.Integer,- },- "maybeEndLine": {- Type: jsonschema.Integer,- },- "err": {+ "startLineString": {Type: jsonschema.String,},- "startLine": {- Type: jsonschema.Integer,- },- "endLine": {- Type: jsonschema.Integer,+ "endLineString": {+ Type: jsonschema.String,},},- Required: []string{"maybeStartLine", "maybeEndLine", "startLine", "endLine"},+ Required: []string{"startLineString", "endLineString"},+ },+ "startLineIncludedReasoning": {+ Type: jsonschema.String,+ },+ "startLineIncluded": {+ Type: jsonschema.Boolean,+ },+ "endLineIncludedReasoning": {+ Type: jsonschema.String,+ },+ "endLineIncluded": {+ Type: jsonschema.Boolean,},"new": {Type: jsonschema.String,},},- Required: []string{"summary", "section", "old", "new"},+ Required: []string{+ "summary",+ "hasChange",+ "old",+ "startLineIncludedReasoning",+ "startLineIncluded",+ "endLineIncludedReasoning",+ "endLineIncluded",+ "new",+ },},},},Required: []string{"changes"},},}++func GetVerifyPrompt(currentState, desc, changes string) string {+ return `+Based on an original file, an AI-generated plan, and an updated file, determine whether the proposed changes were applied correctly to the updated file. Is the syntax in the updated file correct? Were the changes applied correctly or was some code from the original file removed or overwritten that should not have been? Does the updated file as a whole make sense and was it updated consistently with the intention of the plan? Were all comments in the proposed changes that referenced the original code correctly handled in the updated file by including the exact code from the original file that the comment was referencing?++Call the 'verifyOutput' function with a valid JSON object that include the 'reasoning' and 'isCorrect' keys.++'reasoning': Succinctly explain whether the proposed changes were or were not applied correctly and whether the syntax is valid. If the changes were not applied correctly or the syntax isn't valid, briefly explain what went wrong and what needs to be done to fix the errors. If the syntax isn't valid only because the syntax wasn't valid in the original file, explain that the syntax, though incorrect, is consistent with the original file and that the changes were applied correctly.++'isCorrect': A boolean that indicates whether the proposed changes were applied correctly. If the proposed changes were applied correctly, set 'isCorrect' to true. If the proposed changes were not applied correctly, set 'isCorrect' to false.++--++**Original file:**++` + currentState + `++--++**Description of the proposed updates from AI-generated plan:**++` + desc + `++--++**Proposed updates:**++` + changes + `++--++Now call the 'verifyOutput' function with a valid JSON object that includes the 'reasoning', and 'isCorrect' keys. You must always call 'verifyOutput' with a valid JSON object. Don't call any other function.`+}++var VerifyOutputFn = openai.FunctionDefinition{+ Name: "verifyOutput",+ Parameters: &jsonschema.Definition{+ Type: jsonschema.Object,+ Properties: map[string]jsonschema.Definition{+ "reasoning": {+ Type: jsonschema.String,+ },+ "isCorrect": {+ Type: jsonschema.Boolean,+ },+ },+ },+}++// Saving the prompts below to maybe take a stab at full replacements in the future -- not robust enough for now++// func GetBuildFullChangesSysPrompt(filePath, currentState, desc, changes string) string {++// return fullChangesPrompt + "\n\n" + getBuildCurrentStatePrompt(filePath, currentState) + "\n\n" + getBuildPromptWithFullChanges(desc, changes)+// }++// func getBuildPromptWithFullChanges(desc, changes string) string {+// s := ""++// if desc != "" {+// s += "Description of the proposed updates from AI-generated plan:\n```\n" + desc + "\n```\n\n"+// }++// s += "Proposed updates:\n```\n" + changes + "\n```"++// s += "\n\n" + "Now call the 'listChangesFull' function with a valid JSON array of changes according to your instructions. You must always call 'listChangesFull' with one or more valid changes. Don't call any other function."++// return s+// }++// var ListReplacementsFullFn = openai.FunctionDefinition{+// Name: "listChangesFull",+// Parameters: &jsonschema.Definition{+// Type: jsonschema.Object,+// Properties: map[string]jsonschema.Definition{+// "changes": {+// Type: jsonschema.Array,+// Items: &jsonschema.Definition{+// Type: jsonschema.Object,+// Properties: map[string]jsonschema.Definition{+// "summary": {+// Type: jsonschema.String,+// },+// "old": {+// Type: jsonschema.String,+// },+// "new": {+// Type: jsonschema.String,+// },+// },+// Required: []string{"summary", "old", "new"},+// },+// },+// },+// Required: []string{"changes"},+// },+// }++// const fullChangesPrompt = replacementIntro + `++// [YOUR INSTRUCTIONS]+// Call the 'listChangesFull' function with a valid JSON object that includes the 'changes' keys.++// 'changes': An array of NON-OVERLAPPING changes. Each change is an object with properties: 'summary', 'old', and 'new'.++// ` + summaryPrompt + `++// The 'old' property is the *exact* old text to replace. 'old' must be an EXACT SUBSTRING of the original file. 'old' *must only include entire lines and *no partial lines*. Pay *extremely* close attention to quotes, indentation, newlines, spaces and special characters. All double quotes inside 'old' MUST ALWAYS be escaped with a backslash. Apart from escaped double quotes, the 'old' property must be an exact match of the original code that will be replaced. 'old' must be large enough to be *unique and unambiguous* within the file. If the 'old' property occurs in multiple places in the file, you must include enough context to ensure that the 'old' property is unambiguous. Expand 'old' as necessary. 'old' must be an *exact* match of the original code that will be replaced.++// ` + changeNewPrompt + `++// Example function call with all keys:+// ---+// listChangesFull([{+// summary: "Insert function ExecQuery after GetResults function in loop body.",+// old: " GetResults()\n }\n }\n }",+// new: " GetResults()\n execQuery()\n }\n }\n}",+// }])+// ---++// ` + changeRulesPrompt + `++// The 'listChangesFull' function MUST be called *valid JSON*. Double quotes within json properties of the 'listChangesFull' function call parameters JSON object *must be properly escaped* with a backslash.++// [END YOUR INSTRUCTIONS]+// `diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.goindex 47f2c17d..e7d6caae 100644--- a/app/server/model/prompts/create.go+++ b/app/server/model/prompts/create.go@@ -32,8 +32,12 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextb. If not:- Explicitly say "Let's break up this task."- Divide the task into smaller subtasks and list them in a numbered list. Stop there.- - If you are already working on a subtask and the subtask is still too large to be implemented in a single response, it should be further broken down into smaller subtasks. In that case, explicitly say "Let's further break up this subtask", further divide the subtask into even smaller steps, and list them in a numbered list. Stop there.- - Be thorough and exhaustive in your list of subtasks. Ensure you've accounted for *every subtask* that must be done to fully complete the user's task to a high standard.+ - If you are already working on a subtask and the subtask is still too large to be implemented in a single response, it should be further broken down into smaller subtasks. In that case, explicitly say "Let's further break up this subtask", further divide the subtask into even smaller steps, and list them in a numbered list. Stop there. Do NOT do this repetitively for the same subtask. Only break down a given subtask into smaller steps once.+ - Be thorough and exhaustive in your list of subtasks. Ensure you've accounted for *every subtask* that must be done to fully complete the user's task. Ensure that you list *every* file that needs to be created or updated. Be specific and detailed in your list of subtasks.+ - Only include subtasks that you can complete by creating or updating files. If a subtask requires executing code or commands, mention it to the user, but do not include it as a subtask in the plan. Do not include subtasks like "Testing and integration" or "Deployment" that require executing code or commands. Only include subtasks that you can complete by creating or updating files.+ - Only break the task up into subtasks that you can do yourself. If a subtask requires executing code or commands, or other tasks that go beyond coding like testing or verifying, deploying, user testing, and son, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that can be completed directly with code by creating or updating files.+ - Do NOT include tests or documentation in the subtasks unless the user has specifically asked for them. Do not include extra code or features beyond what the user has asked for. Focus on the user's request and implement only what is necessary to fulfill it.+ - Do NOT ask the user to confirm after you've made subtasks. After breaking up the task into subtasks, proceed to implement the first subtask.## Code blocks and files@@ -43,12 +47,14 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextEvery file you reference in a plan should either exist in the context directly or be a new file that will be created in the same base directory as a file in the context. For example, if there is a file in context at path 'lib/term.go', you can create a new file at path 'lib/utils_test.go' but *not* at path 'src/lib/term.go'. You can create new directories and sub-directories as needed, but they must be in the same base directory as a file in context. You must *never* create files with absolute paths like '/etc/config.txt'. All files must be created in the same base directory as a file in context, and paths must be relative to that base directory. You must *never* ask the user to create new files or directories--you must do that yourself.- **You must not include anything except valid code in labelled file blocks for code files.** You must not include explanatory text or bullet points in file blocks for code files. Only code. Explanatory text should come either before the file path or after the code block. The only exception is if the plan specifically requires a file to be generated in a non-code format, like a markdown file. In that case, you can include the non-code content in the file block. But if a file has an extension indicating that it is a code file, you must only include code in the file block for that file.+ **You must not include anything except valid code in labelled file blocks for code files.** You must not include explanatory text or bullet points in file blocks for code files. Only code. Explanatory text should come either before the file path or after the code block. The only exception is if the plan specifically requires a file to be generated in a non-code format, like a markdown file. In that case, you can include the non-code content in the file block. But if a file has an extension indicating that it is a code file, you must only include code in the file block for that file.Files MUST NOT be labelled with a comment like "// File to create: src/main.rs" or "// File to update: src/main.rs".File block labels MUST ONLY include a *single* file path. You must NEVER include multiple files in a single file block. If you need to include code for multiple files, you must use multiple file blocks.+ You MUST NOT include ANY PREFIX prior to the file path in a file block label. Include ONLY the EXACT file path like '- src/main.rs:' with no other text. You MUST NOT include the file path again in the code block itself. The file path must be included *only* in the file block label. There must be a SINGLE label for each file block, and the label must be placed immediately before the opening triple backticks of the code block. There must be NO other lines between the file path and the opening triple backticks.+You MUST NEVER use a file block that only contains comments describing an update or describing the file. If you are updating a file, you must include the code that updates the file in the file block. If you are creating a new file, you must include the code that creates the file in the file block. If it's helpful to explain how a file will be updated or created, you can include that explanation either before the file path or after the code block, but you must not include it in the file block itself.You MUST NOT use the labelled file block format followed by triple backticks for **any purpose** other than creating or updating a file in the plan. You must not use it for explanatory purposes, for listing files, or for any other purpose. If you need to label a section or a list of files, use a markdown section header instead like this: '## Files to update'.@@ -77,13 +83,37 @@ const SysCreate = Identity + ` A plan is a set of files with an attached context**You MUST NEVER give up and say the task is too large or complex for you to do.** Do your best to break the task down into smaller steps and then implement those steps. If a task is very large, the smaller steps can later be broken down into even smaller steps and so on. You can use as many responses as needed to complete a large task. Also don't shorten the task or only implement it partially even if the task is very large. Do your best to break up the task and then implement each step fully, breaking each step into further smaller steps as needed.- **You MUST NOT create only the basic structure of the plan and then stop, or leave any gaps or placeholders.** You must *fully* implement every task and subtask, create or update every necessary file, and provide *all* necessary code, leaving no gaps or placeholders. You must be thorough and exhaustive in your implementation of the plan, and use as many responses as needed to complete the task to a high standard.+ **You MUST NOT create only the basic structure of the plan and then stop, or leave any gaps or placeholders.** You must *fully* implement every task and subtask, create or update every necessary file, and provide *all* necessary code, leaving no gaps or placeholders. You must be thorough and exhaustive in your implementation of the plan, and use as many responses as needed to complete the task to a high standard. In the list of subtasks, be sure you are including *every* task needed to complete the plan. Make sure that EVERY file that needs to be created or updated to complete the task is included in the plan. Do NOT leave out any files that need to be created or updated.++ ## Focus on what the user has asked for and don't add extra code or features++ Don't include extra code, features, or tasks beyond what the user has asked for. Focus on the user's request and implement only what is necessary to fulfill it. You ABSOLUTELY MUST NOT write tests or documentation unless the user has specifically asked for them.++ That said, you MUST thoroughly implement EVERYTHING the user has asked you to do, no matter how many responses it requires.++ ## Working on subtasks++ When starting on a subtask, first EXPLICITLY SAY which subtask you are working on. You MUST NOT work on a subtask without explicitly stating which subtask you are working on. Say only the name of the subtask. Refer to it by the exact name you used when breaking up the initial task into subtasks.++ You should not describe or implement *any* functionality without first explicitly saying which task or subtask you are working on. This is a crucial part of the response and must not be omitted.++ Next, describe the subtask and what your approach will be, then implement it with code blocks. Apart from when you are following instruction 2b above to create the initial subtasks, you must not list, describe, or explain the subtask you are working on without an accompanying implementation in one or more code blocks. Describing what needs to be done to complete a subtask *DOES NOT* count as completing the subtask. It must be fully implemented with code blocks.++ After implementing a task or subtask with code, and before moving on to another task or subtask, you MUST *explicitly mark it done*. You can do this by explicitly stating "[subtask] has been completed". For example, "**Adding the update function** has been completed." It's extremely important to mark subtasks as done so that you can keep track of what has been completed and what is remaining. Never move on to a new subtask. Never end a response without marking the current subtask as done if it has been completed during the response. You MUST NOT omit marking a subtask as done when it has been completed.++ After marking the current subtask as done, move on to the next task or subtask if any are remaining. Otherwise, if no subtasks are remaining output "All tasks have been completed." and stop there.++ If all subtasks are completed, then consider the plan complete. DO NOT repeat or re-implement a subtask that has already been done. Only work on subtasks that are NOT done--that have either not been started or have only been partially compeleted.++ You MUST ALWAYS work on subtasks IN ORDER. You must not skip a subtask or work on subtasks out of order. You must work on subtasks in the order they were listed when breaking up the task into subtasks. You must never go backwards and work on an earlier subtask than the current one. After finishing a subtask, you must always either work on the next subtask in the list of subtasks you created when breaking up the task or, if there are no remaining subtasks, end the plan with "All tasks have been completed.".++ ## Things you can't do- ## Working on subtasks+ You are able to create and update files, but you are not able to execute code or commands. You also aren't able to test code you or the user has written (though you can write tests that the user can run if you've been asked to).- If you working on a subtask, first describe the subtask and what your approach will be, then implement it with code blocks. Apart from when you are following instruction 2b above to create the initial subtasks, you must not list, describe, or explain the subtask you are working on without an accompanying implementation in one or more code blocks. Describing what needs to be done to complete a subtask *DOES NOT* count as completing the subtask. It must be fully implemented with code blocks.+ When breaking up a task into subtasks, only include subtasks that you can do yourself. If a subtask requires executing code or commands, you can mention it to the user, but you should not include it as a subtask in the plan. Only include subtasks that you can complete by creating or updating files.- If you are working on a subtask and it is too large to be implemented in a single response, it should be further broken down into smaller steps. Each smaller step should then be treated like a subtask. The smaller step should be described, your approach should be described, and then you should fully implement the step with one or more code blocks.+ If a task or subtask requires executing code or commands, mention that the user should do so, and then consider that task or subtask complete, and move on to the next task or subtask. For tasks that you ARE able to complete because they only require creating or updating files, complete them thoroughly yourself and don't ask the user to do any part of them.## Use open source libraries when appropriate@@ -101,19 +131,22 @@ const SysCreate = Identity + ` A plan is a set of files with an attached context## Ending a response- At the end of each response, you can suggest additional iterations to make the plan better. You can also ask the user to load more files into context or give you more information if it would help you make a better plan.+ Before ending a response, first mark the current subtask as done as described above if it has been completed during the response.At the *very* end of your response, in a final, separate paragraph, you *must* decide whether the plan is completed and if not, whether it should be automatically continued.- - If all the subtasks in a plan have been thoroughly completed to a high standard, you must explicitly say "All tasks have been completed."+ - If all the subtasks in a plan have been completed, you *MUST* explicitly say "All tasks have been completed."+ - If any summaries of the plan have been included in the conversation that list all the subtasks and mark each one 'Done' or 'Not done', consider only the *latest* summary. If the latest summary shows that all subtasks are marked 'Done', OR you have *just completed* all the remaining 'Not done' subtasks in the responses following the summary, then the plan is done and you must explicitly say "All tasks have been completed."Otherwise:- If there is a clear next subtask that definitely needs to be done to finish the plan (and has not already been completed), output a sentence starting with "Next, " and then give a brief description of the next subtask.- If there is no clear next subtask, or the user needs to take some action before you can continue, explicitly say "The plan cannot be continued." Then finish with a brief description of what the user needs to do for the plan to proceed.- You must not output any other text after this final paragraph. It *must* be the last thing in your response, and it *must* begin with one of the options above ("All tasks have been completed.", "Next, ", or "The plan cannot be continued.").++ - If last paragraph in the response does not begin with one of the options listed above, you MUST continue the response and include a paragraph that DOES begin with one of the options listed above.- - You should consider the plan complete if all the subtasks are completed to a decent standard. Even if there are additional steps that *could* be taken, if you have completed all the subtasks, you should consider the plan complete.+ - You should consider the plan complete if all the subtasks are completed to a decent standard. Even if there are additional steps that *could* be taken, if you have completed all the subtasks, you should consider the plan complete, output "All tasks have been completed.", and stop there.- - Don't consider the user verifying or testing the code as a next step. If all that's left is for the user to verify or test the code, consider the plan complete.+ - Don't consider the user verifying, testing, or deploying the code as a next step. If all that's left is for the user to verify, test, deploy, or run the code, consider the plan complete and output "All tasks have been completed." and then stop there.## EXTREMELY IMPORTANT Rules for responses@@ -125,21 +158,28 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextEvery response must start by considering the previous responses and latest context and then attempt to move the plan forward.- If any paragraph begins with "Next,", it *must never* be followed by a paragraph containing "All tasks have been completed." or "The plan cannot be continued." *Only* if the task described in the "Next," paragraph has *BEEN COMPLETED* can you ever follow it with "All tasks have been completed.".+ You MUST NEVER duplicate, restate, or summarize the most recent response or *any* previous response. Start from where the previous response left off and continue seamlessly from there. Continue smoothly from the end of the last response as if you were replying to the user with one long, continuous response. If the previous response ended with a paragraph that began with "Next,", proceed to implement ONLY THAT TASK OR SUBTASK in your response.++ If any paragraph begins with "Next,", it *must never* be followed by a paragraph containing "All tasks have been completed." or "The plan cannot be continued." *Only* if the task described in the "Next," paragraph has *BEEN COMPLETED* can you ever follow it with "All tasks have been completed.".++ If the previous response ended with a paragraph that began with "Next," and you are continuing the plan, you must *not* begin your response with "Next,". Instead, continue seamlessly from where the previous response left off. If you are not able to complete the task described in the "Next," paragraph, you must explicitly say either "All tasks have been completed" if that was the last remianing task, or describe what the user needs to do for the plan to proceed and then output "The plan cannot be continued." and stop there.Never ask a user to do something manually if you can possibly do it yourself with a code block. Never ask the user to do or anything that isn't strictly necessary for completing the plan to a decent standard.- Don't implement a task or subtask that has already been completed in a previous response or is already included in the current state of a file. Don't end a response with "Next," and then describe a task or subtask that has already been completed in a previous response or is already included in the current state of a file. You can revisit a task or subtask if it has not been completed and needs more work, but you must not repeat any part of one of your previous responses.+ Don't implement a task or subtask that has already been completed in a previous response, is marked "Done" in a plan summary, or is already included in the current state of a file. Don't end a response with "Next," and then describe a task or subtask that has already been completed in a previous response or is already included in the current state of a file. You can revisit a task or subtask if it has not been completed and needs more work, but you must not repeat any part of one of your previous responses.++ DO NOT include "fluffy" additional subtasks when breaking a task up. Only include subtasks and steps that are strictly in the realm of coding and doable ONLY through creating and updating files. Remember, you are listing these subtasks and steps so that you can execute them later. Only list things that YOU can do yourself with NO HELP from the user. Your goal is to *fully complete* the *exact task* the user has given you in as few tokens and responses as you can. This means only including *necessary* steps that *you can complete yourself*.## Continuing the plan+ NEVER repeat any part of your previous response. Always continue seamlessly from where your previous response left off.+If the last paragraph of your previous response in the conversation began with "Next," and you are continuing the plan:- - Continue from where your previous response left off.- - **Do not repeat any part of your previous response**- - **Do not begin your response with "Next,"**+ - **ABSOLUTELY DO NOT not repeat any part of your previous response**+ - **ABSOLUTELY DO NOT begin your response with "Next,"**- Continue seamlessly from where your previous response left off.- - Always continue with the task described in paragraph p unless the user has given you new instructions or context that make it clear that you should do something else.- - **Never begin your response with "The plan cannot be continued." or "All tasks have been completed."**+ - Always continue with the task described in the last paragraph of the previous response unless the user has given you new instructions or context that make it clear that you should do something else.+ - If the previous response broke down the plan into subtasks, *DO NOT DO SO AGAIN*. Instead, continue with the first subtask that was described in the previous response.## Consider the latest context@@ -170,7 +210,13 @@ Do not implement a task partially and then give up even if it's very large or coIf a high quality, well-respected open source library is available that can simplify a task or subtask, use it.-If you're making a plan, end every response with either "All tasks have been completed.", "Next, " (plus a brief description of the next step), or "The plan cannot be continued." according to your instructions for ending a response.`+If you're making a plan, end every response with either "All tasks have been completed.", "Next, " (plus a brief description of the next step), or "The plan cannot be continued." according to your instructions for ending a response. Do NOT repeat any part of your previous response. Always continue seamlessly from where your previous response left off.++Always name the subtask you are working on before starting it, and mark it as done before moving on to the next subtask.++If you break up a task into subtasks, only include subtasks that can be implemented directly in code by creating or updating files. Do not include subtasks that require executing code or commands. Do not include subtasks that require user testing, deployment, or other tasks that go beyond coding.++Do NOT include tests or documentation in the subtasks unless the user has specifically asked for them. Do not include extra code or features beyond what the user has asked for. Focus on the user's request and implement only what is necessary to fulfill it.`func GetWrappedPrompt(prompt string) string {return fmt.Sprintf(promptWrapperFormatStr, prompt)@@ -180,6 +226,16 @@ var PromptWrapperTokens, _ = shared.GetNumTokens(fmt.Sprintf(promptWrapperFormatconst UserContinuePrompt = "Continue the plan."-const AutoContinuePrompt = "Continue the plan from where you left off in the previous response. Don't repeat any part of your previous response. Don't begin your response with 'Next,'. Continue seamlessly from where your previous response left off. Never begin your response with 'The plan cannot be continued.' or 'All tasks have been completed.'."+const AutoContinuePrompt = `Continue the plan from where you left off in the previous response. Don't repeat any part of your previous response. Don't begin your response with 'Next,'.++Continue seamlessly from where your previous response left off. Never begin your response with 'The plan cannot be continued.' or 'All tasks have been completed.'.++Always name the subtask you are working on before starting it, and mark it as done before moving on to the next subtask.++If you break up a task into subtasks, only include subtasks that can be implemented directly in code by creating or updating files. Do not include subtasks that require executing code or commands. Do not include subtasks that require user testing, deployment, or other tasks that go beyond coding.++Do NOT include tests or documentation in the subtasks unless the user has specifically asked for them. Do not include extra code or features beyond what the user has asked for. Focus on the user's request and implement only what is necessary to fulfill it.`const SkippedPathsPrompt = "\n\nSome files have been skipped by the user and *must not* be generated. The user will handle any updates to these files themselves. Skip any parts of the plan that require generating these files. You *must not* generate a file block for any of these files.\nSkipped files:\n"++// - If the plan is in progress, this is not your *first* response in the plan, the user's task or tasks have already been broken down into subtasks if necessary, and the plan is *not yet complete* and should be continued, you MUST ALWAYS start the response with "Now I'll" and then proceed to describe and implement the next step in the plan.diff --git a/app/server/model/prompts/describe.go b/app/server/model/prompts/describe.goindex c650f102..daef04b9 100644--- a/app/server/model/prompts/describe.go+++ b/app/server/model/prompts/describe.go@@ -5,7 +5,7 @@ import ("github.com/sashabaranov/go-openai/jsonschema")-const SysDescribe = "You are an AI parser. You turn an AI's plan for a programming task into a structured description. Call the 'describePlan' function with a valid JSON object that includes the 'commitMsg' key. 'commitMsg' should be a good, succinct commit message for the changes proposed."+const SysDescribe = "You are an AI parser. You turn an AI's plan for a programming task into a structured description. You MUST call the 'describePlan' function with a valid JSON object that includes the 'commitMsg' key. 'commitMsg' should be a good, succinct commit message for the changes proposed. You must ALWAYS call the 'describePlan' function. Never call any other function."var DescribePlanFn = openai.FunctionDefinition{Name: "describePlan",@@ -19,3 +19,5 @@ var DescribePlanFn = openai.FunctionDefinition{Required: []string{"commitMsg"},},}++const SysPendingResults = "You are an AI commit message summarizer. You take a list of descriptions of pending changes and turn them into a succinct one-line summary of all the pending changes that makes for a good commit message title. Output ONLY this one-line title and nothing else."diff --git a/app/server/model/prompts/name.go b/app/server/model/prompts/name.goindex 851f3ad3..337874a2 100644--- a/app/server/model/prompts/name.go+++ b/app/server/model/prompts/name.go@@ -9,7 +9,7 @@ type PlanNameRes struct {PlanName string `json:"PlanName"`}-const SysPlanName = "You are an AI namer that creates a name for the plan. Most plans will be related to software development. Call the 'namePlan' function with a valid JSON object that includes the 'planName' key. 'planName' is a *short* lowercase file name for the plan content. Use dashes as word separators. No spaces, numbers, or special characters. **2-3 words max**. 1-2 words if you can. Shorten and abbreviate where possible."+const SysPlanName = "You are an AI namer that creates a name for the plan. Most plans will be related to software development. Call the 'namePlan' function with a valid JSON object that includes the 'planName' key. 'planName' is a *short* lowercase file name for the plan content. Use dashes as word separators. No spaces, numbers, or special characters. **2-3 words max**. 1-2 words if you can. Shorten and abbreviate where possible. You must ALWAYS call the 'namePlan' function. Don't call any other function."var PlanNameFn = openai.FunctionDefinition{Name: "namePlan",diff --git a/app/server/model/prompts/summary.go b/app/server/model/prompts/summary.goindex 3fe36d54..690dec15 100644--- a/app/server/model/prompts/summary.go+++ b/app/server/model/prompts/summary.go@@ -21,30 +21,41 @@ var ShortSummaryFn = openai.FunctionDefinition{}func GetShortSummaryPrompt(text string) string {- return `- Please summarize the text below using the 'summarize' function. Call 'summarize' with valid JSON that includes the 'summary' key. Only call the 'summarize' function in your reponse. Don't call any other function.-- Text:-- ` + text+ return `Please summarize the text below using the 'summarize' function. Call 'summarize' with valid JSON that includes the 'summary' key. You must ALWAYS call the 'summarize' function in your reponse. Don't call any other function.++ Text:++ ` + text}const PlanSummary = `-Based on the conversation so far, make a summary of the current state of the plan.+You are an AI summarizer that summarizes the conversation so far. The conversation so far is a plan to complete one or more programming tasks for a user. This conversation may begin with an existing summary of the plan.++If the plan is just starting, there will be no existing summary, so you should just summarize the conversation between the user and yourself prior to this message. If the plan has already been started, you should summarize the existing plan based on the existing summary, then update the summary based on the latest messages.++Based on the existing summary and the conversation so far, make a summary of the current state of the plan.++- Begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages.++- Next, if the plan has been broken down into subtasks, include ALL those subtasks *verbatim* in the summary as a numbered list, then mark whether each subtask has been implemented in code during the conversation. You must include ALL subtasks, even if they have not been mentioned in the latest messages. If this conversation began with an existing summary, include ALL the subtasks from the existing summary.++- Mark whether each subtask has been implemented in code during the conversation. A subtask has been implemented if *ANY* code blocks related to it have been included in the conversation. Do not be overly strict. Mark a subtask as implemented if it has been dealt with at all during the conversation. If a subtask has been implemented, mark it as implemented in code in the summary.++- If a subtask has been further broken down into more subtasks, you should include those subtasks in the summary as a numbered list nested under the parent subtask. Just as with subtasks, you should include the name and a brief description of each sub-subtask and mark whether each sub-subtask has been implemented in code during the conversation.-- It should begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages. If the user has sent messages like 'continue to the next step of the plan' that don't contain any new information relevant to the plan, you should omit them.+- Any subtask or sub-subtask that has been marked as implemented in code in the existing summary *MUST* be marked as implemented in code in this new summary, even if they are not mentioned in the latest messages. It is EXTREMELY IMPORTANT that any subtask or sub-subtask that has been marked as implemented in the existing summary keeps the EXACT SAME status in this new summary.-- Next, if the plan includes a statement from the assistant to the effect of "I will break this large task into subtasks" and the plan has been broken down into subtasks, include those subtasks in the summary as a numbered list. Condense these as much as possible while still being clear and retaining the meaning of each subtask. At the end of the list, state which subtask is currently being worked on (unless they are all finished, in which case state that they are all finished).+- Each task, subtask, and sub-subtask should only appear once in the list.-- Last, summarize the latest version of the plan and any changes you have suggested. If the some of the older changes have been overridden by newer changes, you should only include the newest changes and omit the older ones.+- A task, subtask, or sub-subtask can be removed from the list ONLY if it has not been implemented *and* the user has specifically asked for it not to be done, or it has been explicitly mentioned in the latest messages that it is no longer needed or relevant to the plan. If a subtask has been marked as implemented in code in the existing summary, it must not be removed from the list even if it is no longer relevant to the plan.-- If your responses include code blocks labelled with file paths, include the latest state of your modifications to each file's code and label them with file paths in the same format as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.+- Do NOT change the order of tasks, subtasks, or sub-subtasks in the list. If a task, subtask, or sub-subtask is listed as a subtask of another task, it should remain in that position in the list. If a subtask has sub-subtasks, those sub-subtasks should remain nested under that subtask.-- If code blocks can be converted to a textual summary without losing meaning or precision, summarize them. Otherwise, leave them as code.+- Last, summarize what has been done in the latest messages, and any next steps that have been described at the end of the latest message. Do NOT list the subtasks multiple times. Only list the subtasks once.-- If part of the plan has already been summarized, update the summary based on the latest messages while continuing the follow the above guidelines.+- Do not include code in the summary. Explain in words what has been done and what needs to be done.-- The summary should condense the conversation to save tokens, but still contain enough information about the state of the plan to be actionable and apply the suggested changes.+- Treat the summary as *append-only*. Keep as much information as possible from the existing summary and add the new information from the latest messages. The summary is meant to be a record of the entire plan as it evolves over time.Output only the summary of the current state of the plan and nothing else.`diff --git a/app/server/model/summarize.go b/app/server/model/summarize.goindex 4d387d41..7bd8e1dd 100644--- a/app/server/model/summarize.go+++ b/app/server/model/summarize.go@@ -3,6 +3,7 @@ package modelimport ("context""fmt"+ "log""plandex-server/db""plandex-server/model/prompts""time"@@ -37,7 +38,7 @@ func PlanSummary(client *openai.Client, config shared.ModelRoleConfig, params PlContent: prompts.PlanSummary,})- // fmt.Println("summarizing messages:")+ fmt.Println("summarizing messages:")// spew.Dump(messages)resp, err := CreateChatCompletionWithRetries(@@ -63,10 +64,13 @@ func PlanSummary(client *openai.Client, config shared.ModelRoleConfig, params Plcontent := resp.Choices[0].Message.Content+ log.Println("Plan summary content:")+ log.Println(content)+return &db.ConvoSummary{OrgId: params.OrgId,PlanId: params.PlanId,- Summary: content,+ Summary: "## Summary of the plan so far:\n\n" + content,Tokens: resp.Usage.CompletionTokens,LatestConvoMessageId: params.LatestConvoMessageId,LatestConvoMessageCreatedAt: params.LatestConvoMessageCreatedAt,diff --git a/app/server/routes.go b/app/server/routes.goindex 5b64ccac..bea928c2 100644--- a/app/server/routes.go+++ b/app/server/routes.go@@ -77,9 +77,13 @@ func routes() *mux.Router {r.HandleFunc("/plans/{planId}/{branch}/current_plan", handlers.CurrentPlanHandler).Methods("GET")r.HandleFunc("/plans/{planId}/{branch}/apply", handlers.ApplyPlanHandler).Methods("PATCH")- r.HandleFunc("/plans/{planId}/{branch}/archive", handlers.ArchivePlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/archive", handlers.ArchivePlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/unarchive", handlers.UnarchivePlanHandler).Methods("PATCH")++ r.HandleFunc("/plans/{planId}/rename", handlers.RenamePlanHandler).Methods("PATCH")r.HandleFunc("/plans/{planId}/{branch}/reject_all", handlers.RejectAllChangesHandler).Methods("PATCH")r.HandleFunc("/plans/{planId}/{branch}/reject_file", handlers.RejectFileHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/{branch}/diffs", handlers.GetPlanDiffsHandler).Methods("GET")r.HandleFunc("/plans/{planId}/{branch}/context", handlers.ListContextHandler).Methods("GET")r.HandleFunc("/plans/{planId}/{branch}/context", handlers.LoadContextHandler).Methods("POST")@@ -97,6 +101,19 @@ func routes() *mux.Router {r.HandleFunc("/plans/{planId}/{branch}/settings", handlers.GetSettingsHandler).Methods("GET")r.HandleFunc("/plans/{planId}/{branch}/settings", handlers.UpdateSettingsHandler).Methods("PUT")+ r.HandleFunc("/plans/{planId}/{branch}/status", handlers.GetPlanStatusHandler).Methods("GET")++ r.HandleFunc("/custom_models", handlers.ListCustomModelsHandler).Methods("GET")+ r.HandleFunc("/custom_models", handlers.CreateCustomModelHandler).Methods("POST")+ r.HandleFunc("/custom_models/{modelId}", handlers.DeleteAvailableModelHandler).Methods("DELETE")++ r.HandleFunc("/model_sets", handlers.ListModelPacksHandler).Methods("GET")+ r.HandleFunc("/model_sets", handlers.CreateModelPackHandler).Methods("POST")+ r.HandleFunc("/model_sets/{setId}", handlers.DeleteModelPackHandler).Methods("DELETE")++ r.HandleFunc("/default_settings", handlers.GetDefaultSettingsHandler).Methods("GET")+ r.HandleFunc("/default_settings", handlers.UpdateDefaultSettingsHandler).Methods("PUT")+return r}diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.goindex 4b8b0f97..3e89c0f3 100644--- a/app/server/types/active_plan.go+++ b/app/server/types/active_plan.go@@ -13,18 +13,26 @@ import ("github.com/plandex/plandex/shared")+// const MaxConcurrentBuildStreams = 3 // otherwise we get EOF errors from openai+type ActiveBuild struct {- ReplyId string- FileDescription string- FileContent string- FileContentTokens int- CurrentFileTokens int- Path string- Idx int- Buffer string- BufferTokens int- Success bool- Error error+ ReplyId string+ FileDescription string+ FileContent string+ FileContentTokens int+ CurrentFileTokens int+ Path string+ Idx int+ WithLineNumsBuffer string+ WithLineNumsBufferTokens int+ VerifyBuffer string+ VerifyBufferTokens int+ // FullChangeBuffer string+ // FullChangeBufferTokens int+ FixBuffer string+ FixBufferTokens int+ Success bool+ Error error}type subscription struct {@@ -38,6 +46,8 @@ type subscription struct {type ActivePlan struct {Id string+ UserId string+ OrgId stringCurrentStreamingReplyId stringCurrentReplyDoneCh chan boolBranch string@@ -71,7 +81,7 @@ type ActivePlan struct {subscriptionMu sync.Mutex}-func NewActivePlan(planId, branch, prompt string, buildOnly bool) *ActivePlan {+func NewActivePlan(orgId, userId, planId, branch, prompt string, buildOnly bool) *ActivePlan {ctx, cancel := context.WithCancel(context.Background())// child context for model stream so we can cancel it separately if neededmodelStreamCtx, cancelModelStream := context.WithCancel(ctx)@@ -81,6 +91,8 @@ func NewActivePlan(planId, branch, prompt string, buildOnly bool) *ActivePlan {active := ActivePlan{Id: planId,+ OrgId: orgId,+ UserId: userId,BuildOnly: buildOnly,Branch: branch,Prompt: prompt,diff --git a/app/server/types/model.go b/app/server/types/model.goindex 7a1771ca..5716db57 100644--- a/app/server/types/model.go+++ b/app/server/types/model.go@@ -8,7 +8,15 @@ type StreamedFile struct {Content string `json:"content"`}-type StreamedChanges struct {- References string `json:"references"`- Changes []*shared.StreamedChange `json:"changes"`+type StreamedChangesWithLineNums struct {+ Changes []*shared.StreamedChangeWithLineNums `json:"changes"`+}++// type StreamedChangesFull struct {+// Changes []*shared.StreamedChangeFull `json:"changes"`+// }++type StreamedVerifyResult struct {+ Reasoning string `json:"reasoning"`+ IsCorrect bool `json:"isCorrect"`}diff --git a/app/server/types/reply.go b/app/server/types/reply.goindex f32163eb..9c7aee90 100644--- a/app/server/types/reply.go+++ b/app/server/types/reply.go@@ -159,7 +159,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {// log.Println("Current file path is empty--checking for possible file path...")var gotPath string- if lineHasFilePath(prevFullLineTrimmed) {+ if lineMaybeHasFilePath(prevFullLineTrimmed) {gotPath = extractFilePath(prevFullLineTrimmed)} else {// log.Println("No possible file path detected.", strconv.Quote(prevFullLineTrimmed))@@ -215,7 +215,7 @@ func (r *ReplyParser) GetReplyBeforeCurrentPath() string {var idx intfor i := len(r.lines) - 1; i >= 0; i-- {line := r.lines[i]- if lineHasFilePath(line) && r.currentFilePath == extractFilePath(line) {+ if lineMaybeHasFilePath(line) && r.currentFilePath == extractFilePath(line) {idx = ibreak}@@ -224,8 +224,8 @@ func (r *ReplyParser) GetReplyBeforeCurrentPath() string {return strings.Join(r.lines[:idx], "\n")}-func lineHasFilePath(line string) bool {- return (strings.HasPrefix(line, "-")) || strings.HasPrefix(line, "-file:") || strings.HasPrefix(line, "- file:") || (strings.HasPrefix(line, "**") && strings.HasSuffix(line, "**"))+func lineMaybeHasFilePath(line string) bool {+ return (strings.HasPrefix(line, "-")) || strings.HasPrefix(line, "-file:") || strings.HasPrefix(line, "- file:") || (strings.HasPrefix(line, "**") && strings.HasSuffix(line, "**")) || (strings.HasPrefix(line, "#") && strings.HasSuffix(line, ":"))}func extractFilePath(line string) string {diff --git a/app/shared/ai_models.go b/app/shared/ai_models.goindex a845a871..3ffa1493 100644--- a/app/shared/ai_models.go+++ b/app/shared/ai_models.go@@ -4,243 +4,555 @@ import ("github.com/sashabaranov/go-openai")-var AvailableModels = []BaseModelConfig{+const OpenAIEnvVar = "OPENAI_API_KEY"+const OpenAIV1BaseUrl = "https://api.openai.com/v1"++var fullCompatibility = ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: true,+}++var AvailableModels = []*AvailableModel{+ {+ Description: "OpenAI's latest gpt-4-turbo model, first released on 2024-04-09",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4Turbo,+ MaxTokens: 128000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ }},++ {+ Description: "OpenAI's gpt-4-turbo, pinned to version released on 2024-04-09",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4Turbo20240409,+ MaxTokens: 128000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ }},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4Turbo,- MaxTokens: 128000,+ Description: "OpenAI's latest gpt-4-turbo-preview model, first released on 2024-11-06",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4TurboPreview,+ MaxTokens: 128000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ }},+ {+ Description: "OpenAI's gpt-4-turbo-preview, pinned to version released on 2024-01-25",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4Turbo0125,+ MaxTokens: 128000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ }},+ {+ Description: "OpenAI's gpt-4-turbo-preview, pinned to version released on 2023-11-06",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4Turbo1106,+ MaxTokens: 128000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ }},+ {+ Description: "OpenAI's gpt-4 model",+ DefaultMaxConvoTokens: 2500,+ DefaultReservedOutputTokens: 1000,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT4,+ MaxTokens: 8000,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: false,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: true,+ },+ BaseUrl: OpenAIV1BaseUrl,+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4Turbo20240409,- MaxTokens: 128000,+ Description: "OpenAI's latest gpt-3.5-turbo model",+ DefaultMaxConvoTokens: 5000,+ DefaultReservedOutputTokens: 2000,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT3Dot5Turbo,+ MaxTokens: 16385,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4TurboPreview,- MaxTokens: 128000,+ Description: "OpenAI's gpt-3.5-turbo, pinned to version released on 2024-01-25",+ DefaultMaxConvoTokens: 5000,+ DefaultReservedOutputTokens: 2000,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT3Dot5Turbo0125,+ MaxTokens: 16385,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4Turbo0125,- MaxTokens: 128000,+ Description: "OpenAI's gpt-3.5-turbo, pinned to version released on 2023-11-06",+ DefaultMaxConvoTokens: 5000,+ DefaultReservedOutputTokens: 2000,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenAI,+ ModelName: openai.GPT3Dot5Turbo1106,+ MaxTokens: 16385,+ ApiKeyEnvVar: OpenAIEnvVar,+ ModelCompatibility: fullCompatibility,+ BaseUrl: OpenAIV1BaseUrl,+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4Turbo1106,- MaxTokens: 128000,+ Description: "Anthropic Claude Opus via OpenRouter",+ DefaultMaxConvoTokens: 15000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenRouter,+ ModelName: "anthropic/claude-3-opus",+ MaxTokens: 200000,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT4,- MaxTokens: 8000,+ Description: "Anthropic Claude Opus via OpenRouter",+ DefaultMaxConvoTokens: 15000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenRouter,+ ModelName: "anthropic/claude-3-opus",+ MaxTokens: 200000,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT3Dot5Turbo,- MaxTokens: 16385,+ Description: "Anthropic Claude Sonnet via OpenRouter",+ DefaultMaxConvoTokens: 15000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenRouter,+ ModelName: "anthropic/claude-3-sonnet",+ MaxTokens: 200000,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT3Dot5Turbo0125,- MaxTokens: 16385,+ Description: "Anthropic Claude Haiku via OpenRouter",+ DefaultMaxConvoTokens: 15000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenRouter,+ ModelName: "anthropic/claude-3-haiku",+ MaxTokens: 200000,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],+ },},{- Provider: ModelProviderOpenAI,- ModelName: openai.GPT3Dot5Turbo1106,- MaxTokens: 16385,+ Description: "Mixtral-8x22B via Together.ai",+ DefaultMaxConvoTokens: 10000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderTogether,+ ModelName: "mistralai/Mixtral-8x22B-Instruct-v0.1",+ MaxTokens: 65536,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderTogether],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: false,+ HasStreaming: true,+ HasFunctionCalling: false,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderTogether],+ },},-}--var PlannerModelConfigByName = map[string]PlannerModelConfig{- openai.GPT4Turbo: {- MaxConvoTokens: 10000,- ReservedOutputTokens: 4096,+ {+ Description: "Mixtral-8x7B via Together.ai",+ DefaultMaxConvoTokens: 5000,+ DefaultReservedOutputTokens: 4096,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderTogether,+ ModelName: "mistralai/Mixtral-8x7B-Instruct-v0.1",+ MaxTokens: 32768,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderTogether],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderTogether],+ },},- openai.GPT4TurboPreview: {- MaxConvoTokens: 10000,- ReservedOutputTokens: 4096,+ {+ Description: "Google Gemini Pro 1.5 preview via OpenRouter",+ DefaultMaxConvoTokens: 100000,+ DefaultReservedOutputTokens: 22937,+ BaseModelConfig: BaseModelConfig{+ Provider: ModelProviderOpenRouter,+ ModelName: "google/gemini-pro-1.5",+ MaxTokens: 2800000000,+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],+ ModelCompatibility: ModelCompatibility{+ IsOpenAICompatible: true,+ HasJsonResponseMode: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: false,+ },+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],+ },},- openai.GPT4Turbo0125: {- MaxConvoTokens: 10000,- ReservedOutputTokens: 4096,+}++var AvailableModelsByName = map[string]*AvailableModel{}++var Gpt4TurboPreviewLatestModelPack ModelPack+var Gpt4TurboLatestModelPack ModelPack+var OpenRouterClaudeOpusGPT4TurboModelPack ModelPack+var TogetherMixtral8x22BModelPack ModelPack++var BuiltInModelPacks = []*ModelPack{+ &Gpt4TurboLatestModelPack,+ &Gpt4TurboPreviewLatestModelPack,+ &OpenRouterClaudeOpusGPT4TurboModelPack,+ &TogetherMixtral8x22BModelPack,+}++var DefaultModelPack *ModelPack = &Gpt4TurboLatestModelPack++func getPlannerModelConfig(name string) PlannerModelConfig {+ return PlannerModelConfig{+ MaxConvoTokens: AvailableModelsByName[name].DefaultMaxConvoTokens,+ ReservedOutputTokens: AvailableModelsByName[name].DefaultReservedOutputTokens,+ }+}++var DefaultConfigByRole = map[ModelRole]ModelRoleConfig{+ ModelRolePlanner: {+ Temperature: 0.3,+ TopP: 0.3,},- openai.GPT4Turbo1106: {- MaxConvoTokens: 10000,- ReservedOutputTokens: 4096,+ ModelRolePlanSummary: {+ Temperature: 0.2,+ TopP: 0.2,},- openai.GPT4: {- MaxConvoTokens: 2500,- ReservedOutputTokens: 1000,+ ModelRoleBuilder: {+ Temperature: 0.1,+ TopP: 0.1,},- openai.GPT3Dot5Turbo: {- MaxConvoTokens: 5000,- ReservedOutputTokens: 2000,+ ModelRoleName: {+ Temperature: 0.8,+ TopP: 0.5,},- openai.GPT3Dot5Turbo0125: {- MaxConvoTokens: 5000,- ReservedOutputTokens: 2000,+ ModelRoleCommitMsg: {+ Temperature: 0.8,+ TopP: 0.5,},- openai.GPT3Dot5Turbo1106: {- MaxConvoTokens: 5000,- ReservedOutputTokens: 2000,+ ModelRoleExecStatus: {+ Temperature: 0.1,+ TopP: 0.1,},}-var TaskModelConfigByName = map[string]TaskModelConfig{- openai.GPT4Turbo: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},- },- openai.GPT4TurboPreview: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+var RequiredCompatibilityByRole = map[ModelRole]ModelCompatibility{+ ModelRolePlanner: {+ IsOpenAICompatible: true,+ HasStreaming: true,},- openai.GPT4Turbo0125: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+ ModelRolePlanSummary: {+ IsOpenAICompatible: true,+ HasStreaming: true,},- openai.GPT4Turbo1106: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+ ModelRoleBuilder: {+ IsOpenAICompatible: true,+ HasStreaming: true,+ HasFunctionCalling: true,+ HasStreamingFunctionCalls: true,},- openai.GPT4: {- OpenAIResponseFormat: nil,+ ModelRoleName: {+ IsOpenAICompatible: true,},- openai.GPT3Dot5Turbo: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+ ModelRoleCommitMsg: {+ IsOpenAICompatible: true,+ HasFunctionCalling: true,},- openai.GPT3Dot5Turbo0125: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+ ModelRoleExecStatus: {+ IsOpenAICompatible: true,+ HasFunctionCalling: true,},- openai.GPT3Dot5Turbo1106: {- OpenAIResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},- },-}--var AvailableModelsByName = map[string]BaseModelConfig{}--var Gpt4TurboPreviewLatestModelSet ModelSet-var Gpt4TurboLatestModelSet ModelSet--var BuiltInModelSets = []*ModelSet{- &Gpt4TurboLatestModelSet,- &Gpt4TurboPreviewLatestModelSet,}-var DefaultModelSet *ModelSet = &Gpt4TurboLatestModelSet-func init() {for _, model := range AvailableModels {AvailableModelsByName[model.ModelName] = model}- Gpt4TurboLatestModelSet = ModelSet{+ Gpt4TurboLatestModelPack = ModelPack{Name: "gpt-4-turbo-latest",- Description: "Uses latest version of OpenAI gpt-4-turbo model first released on 2024-04-09 for heavy lifting, latest version of gpt-3.5-turbo for lighter tasks.",+ Description: "Uses latest version of OpenAI gpt-4-turbo model (first released on 2024-04-09) for heavy lifting, latest version of gpt-3.5-turbo for lighter tasks.",Planner: PlannerRoleConfig{ModelRoleConfig: ModelRoleConfig{Role: ModelRolePlanner,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.4,- TopP: 0.3,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanner].TopP,},- PlannerModelConfig: PlannerModelConfigByName[openai.GPT4Turbo],+ PlannerModelConfig: getPlannerModelConfig(openai.GPT4Turbo),},PlanSummary: ModelRoleConfig{Role: ModelRolePlanSummary,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.2,- TopP: 0.2,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,},- Builder: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleBuilder,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.2,- TopP: 0.2,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT4Turbo],+ Builder: ModelRoleConfig{+ Role: ModelRoleBuilder,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,+ TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,},- Namer: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleName,- BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo],- Temperature: 0.8,- TopP: 0.5,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT3Dot5Turbo],+ Namer: ModelRoleConfig{+ Role: ModelRoleName,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleName].Temperature,+ TopP: DefaultConfigByRole[ModelRoleName].TopP,},- CommitMsg: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleCommitMsg,- BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo],- Temperature: 0.8,- TopP: 0.5,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT3Dot5Turbo],+ CommitMsg: ModelRoleConfig{+ Role: ModelRoleCommitMsg,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleCommitMsg].Temperature,+ TopP: DefaultConfigByRole[ModelRoleCommitMsg].TopP,},- ExecStatus: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleExecStatus,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.1,- TopP: 0.1,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT4Turbo],+ ExecStatus: ModelRoleConfig{+ Role: ModelRoleExecStatus,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleExecStatus].Temperature,+ TopP: DefaultConfigByRole[ModelRoleExecStatus].TopP,},}- Gpt4TurboPreviewLatestModelSet = ModelSet{+ Gpt4TurboPreviewLatestModelPack = ModelPack{Name: "gpt-4-turbo-preview-latest",- Description: "Uses latest version of OpenAI's gpt-4-turbo-preview model first released on 2024-11-06 for heavy lifting, latest version of gpt-3.5-turbo for lighter tasks.",+ Description: "Uses latest version of OpenAI's gpt-4-turbo-preview model (first released on 2024-11-06) for heavy lifting, latest version of gpt-3.5-turbo for lighter tasks.",Planner: PlannerRoleConfig{ModelRoleConfig: ModelRoleConfig{Role: ModelRolePlanner,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.4,- TopP: 0.3,+ BaseModelConfig: AvailableModelsByName[openai.GPT4TurboPreview].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanner].TopP,},- PlannerModelConfig: PlannerModelConfigByName[openai.GPT4Turbo],+ PlannerModelConfig: getPlannerModelConfig(openai.GPT4TurboPreview),},PlanSummary: ModelRoleConfig{Role: ModelRolePlanSummary,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.2,- TopP: 0.2,+ BaseModelConfig: AvailableModelsByName[openai.GPT4TurboPreview].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,},- Builder: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleBuilder,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.2,- TopP: 0.2,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT4Turbo],+ Builder: ModelRoleConfig{+ Role: ModelRoleBuilder,+ BaseModelConfig: AvailableModelsByName[openai.GPT4TurboPreview].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,+ TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,},- Namer: TaskRoleConfig{- ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleName,- BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo],- Temperature: 0.8,- TopP: 0.5,- },- TaskModelConfig: TaskModelConfigByName[openai.GPT3Dot5Turbo],+ Namer: ModelRoleConfig{+ Role: ModelRoleName,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleName].Temperature,+ TopP: DefaultConfigByRole[ModelRoleName].TopP,+ },+ CommitMsg: ModelRoleConfig{+ Role: ModelRoleCommitMsg,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleCommitMsg].Temperature,+ TopP: DefaultConfigByRole[ModelRoleCommitMsg].TopP,+ },+ ExecStatus: ModelRoleConfig{+ Role: ModelRoleExecStatus,+ BaseModelConfig: AvailableModelsByName[openai.GPT4TurboPreview].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleExecStatus].Temperature,+ TopP: DefaultConfigByRole[ModelRoleExecStatus].TopP,},- CommitMsg: TaskRoleConfig{+ }++ OpenRouterClaudeOpusGPT4TurboModelPack = ModelPack{+ Name: "claude-opus/gpt-4-turbo",+ Description: "Uses Anthropic's Claude Opus model (via OpenRouter) for planning, Claude Sonnet for summarization, gpt-4-turbo for builds and auto-continue, and gpt-3.5-turbo for lighter tasks.",+ Planner: PlannerRoleConfig{ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleCommitMsg,- BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo],- Temperature: 0.8,- TopP: 0.5,+ Role: ModelRolePlanner,+ BaseModelConfig: AvailableModelsByName["anthropic/claude-3-opus"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanner].TopP,},- TaskModelConfig: TaskModelConfigByName[openai.GPT3Dot5Turbo],+ PlannerModelConfig: getPlannerModelConfig("anthropic/claude-3-opus"),},- ExecStatus: TaskRoleConfig{+ PlanSummary: ModelRoleConfig{+ Role: ModelRolePlanSummary,+ BaseModelConfig: AvailableModelsByName["anthropic/claude-3-sonnet"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,+ },+ Builder: ModelRoleConfig{+ Role: ModelRoleBuilder,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,+ TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,+ },+ Namer: ModelRoleConfig{+ Role: ModelRoleName,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleName].Temperature,+ TopP: DefaultConfigByRole[ModelRoleName].TopP,+ },+ CommitMsg: ModelRoleConfig{+ Role: ModelRoleCommitMsg,+ BaseModelConfig: AvailableModelsByName[openai.GPT3Dot5Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleName].Temperature,+ TopP: DefaultConfigByRole[ModelRoleName].TopP,+ },+ ExecStatus: ModelRoleConfig{+ Role: ModelRoleExecStatus,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,+ TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,+ },+ }++ TogetherMixtral8x22BModelPack = ModelPack{+ Name: "Mixtral-8x22b/Mixtral-8x7b/gpt-4-turbo",+ Description: "Uses Together.ai's Mixtral-8x22B for planning and summarization, gpt-4-turbo for builds and auto-continue, and Mixtral-8x7B for lighter tasks.",+ Planner: PlannerRoleConfig{ModelRoleConfig: ModelRoleConfig{- Role: ModelRoleExecStatus,- BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo],- Temperature: 0.1,- TopP: 0.1,+ Role: ModelRolePlanner,+ BaseModelConfig: AvailableModelsByName["mistralai/Mixtral-8x22B-Instruct-v0.1"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanner].TopP,},- TaskModelConfig: TaskModelConfigByName[openai.GPT4Turbo],+ PlannerModelConfig: getPlannerModelConfig("mistralai/Mixtral-8x22B-Instruct-v0.1"),+ },+ PlanSummary: ModelRoleConfig{+ Role: ModelRolePlanSummary,+ BaseModelConfig: AvailableModelsByName["mistralai/Mixtral-8x22B-Instruct-v0.1"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,+ TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,+ },+ Builder: ModelRoleConfig{+ Role: ModelRoleBuilder,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,+ TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,+ },+ Namer: ModelRoleConfig{+ Role: ModelRoleName,+ BaseModelConfig: AvailableModelsByName["mistralai/Mixtral-8x7B-Instruct-v0.1"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleName].Temperature,+ TopP: DefaultConfigByRole[ModelRoleName].TopP,+ },+ CommitMsg: ModelRoleConfig{+ Role: ModelRoleCommitMsg,+ BaseModelConfig: AvailableModelsByName["mistralai/Mixtral-8x7B-Instruct-v0.1"].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleCommitMsg].Temperature,+ TopP: DefaultConfigByRole[ModelRoleCommitMsg].TopP,+ },+ ExecStatus: ModelRoleConfig{+ Role: ModelRoleExecStatus,+ BaseModelConfig: AvailableModelsByName[openai.GPT4Turbo].BaseModelConfig,+ Temperature: DefaultConfigByRole[ModelRoleExecStatus].Temperature,+ TopP: DefaultConfigByRole[ModelRoleExecStatus].TopP,},}+}++func FilterCompatibleModels(models []*AvailableModel, role ModelRole) []*AvailableModel {+ required := RequiredCompatibilityByRole[role]+ var compatibleModels []*AvailableModel++ for _, model := range models {+ if required.IsOpenAICompatible && !model.ModelCompatibility.IsOpenAICompatible {+ continue+ }+ if required.HasJsonResponseMode && !model.ModelCompatibility.HasJsonResponseMode {+ continue+ }+ if required.HasStreaming && !model.ModelCompatibility.HasStreaming {+ continue+ }+ if required.HasFunctionCalling && !model.ModelCompatibility.HasFunctionCalling {+ continue+ }+ if required.HasStreamingFunctionCalls && !model.ModelCompatibility.HasStreamingFunctionCalls {+ continue+ }++ compatibleModels = append(compatibleModels, model)+ }+ return compatibleModels}diff --git a/app/shared/data_models.go b/app/shared/data_models.goindex 4518bc29..2ff79c4d 100644--- a/app/shared/data_models.go+++ b/app/shared/data_models.go@@ -1,9 +1,10 @@package sharedimport (+ "database/sql/driver"+ "encoding/json"+ "fmt""time"-- "github.com/sashabaranov/go-openai")type Org struct {@@ -142,26 +143,28 @@ type PlanBuild struct {}type Replacement struct {- Id string `json:"id"`- Old string `json:"old"`- New string `json:"new"`- Failed bool `json:"failed"`- RejectedAt *time.Time `json:"rejectedAt,omitempty"`- StreamedChange *StreamedChange `json:"streamedChange"`+ Id string `json:"id"`+ Old string `json:"old"`+ New string `json:"new"`+ Failed bool `json:"failed"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+ StreamedChange *StreamedChangeWithLineNums `json:"streamedChange"`}type PlanFileResult struct {- Id string `json:"id"`- ConvoMessageId string `json:"convoMessageId"`- PlanBuildId string `json:"planBuildId"`- Path string `json:"path"`- Content string `json:"content"`- AnyFailed bool `json:"anyFailed"`- AppliedAt *time.Time `json:"appliedAt,omitempty"`- RejectedAt *time.Time `json:"rejectedAt,omitempty"`- Replacements []*Replacement `json:"replacements"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ TypeVersion int `json:"typeVersion"`+ ReplaceWithLineNums bool `json:"replaceWithLineNums"`+ ConvoMessageId string `json:"convoMessageId"`+ PlanBuildId string `json:"planBuildId"`+ Path string `json:"path"`+ Content string `json:"content"`+ AnyFailed bool `json:"anyFailed"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+ Replacements []*Replacement `json:"replacements"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}type CurrentPlanFiles struct {@@ -192,11 +195,32 @@ type OrgRole struct {Description string `json:"description"`}+type ModelCompatibility struct {+ IsOpenAICompatible bool `json:"isOpenAICompatible"`+ HasJsonResponseMode bool `json:"hasJsonMode"`+ HasStreaming bool `json:"hasStreaming"`+ HasFunctionCalling bool `json:"hasFunctionCalling"`+ HasStreamingFunctionCalls bool `json:"hasStreamingFunctionCalls"`+}+type BaseModelConfig struct {- Provider ModelProvider `json:"provider"`- BaseUrl string `json:"baseUrl"`- ModelName string `json:"modelName"`- MaxTokens int `json:"maxTokens"`+ Provider ModelProvider `json:"provider"`+ CustomProvider *string `json:"customProvider,omitempty"`+ BaseUrl string `json:"baseUrl"`+ ModelName string `json:"modelName"`+ MaxTokens int `json:"maxTokens"`+ ApiKeyEnvVar string `json:"apiKeyEnvVar"`+ ModelCompatibility+}++type AvailableModel struct {+ Id string `json:"id"`+ BaseModelConfig+ Description string `json:"description"`+ DefaultMaxConvoTokens int `json:"defaultMaxConvoTokens"`+ DefaultReservedOutputTokens int `json:"defaultReservedOutputTokens"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}type PlannerModelConfig struct {@@ -204,10 +228,6 @@ type PlannerModelConfig struct {ReservedOutputTokens int `json:"maxOutputTokens"`}-type TaskModelConfig struct {- OpenAIResponseFormat *openai.ChatCompletionResponseFormat `json:"openAIResponseFormat"`-}-type ModelRoleConfig struct {Role ModelRole `json:"role"`BaseModelConfig BaseModelConfig `json:"baseModelConfig"`@@ -215,25 +235,57 @@ type ModelRoleConfig struct {TopP float32 `json:"topP"`}+func (m *ModelRoleConfig) Scan(src interface{}) error {+ if src == nil {+ return nil+ }+ switch s := src.(type) {+ case []byte:+ return json.Unmarshal(s, m)+ case string:+ return json.Unmarshal([]byte(s), m)+ default:+ return fmt.Errorf("unsupported data type: %T", src)+ }+}++func (m ModelRoleConfig) Value() (driver.Value, error) {+ return json.Marshal(m)+}+type PlannerRoleConfig struct {ModelRoleConfigPlannerModelConfig}-type TaskRoleConfig struct {- ModelRoleConfig- TaskModelConfig+func (p *PlannerRoleConfig) Scan(src interface{}) error {+ if src == nil {+ return nil+ }+ switch s := src.(type) {+ case []byte:+ return json.Unmarshal(s, p)+ case string:+ return json.Unmarshal([]byte(s), p)+ default:+ return fmt.Errorf("unsupported data type: %T", src)+ }+}++func (p PlannerRoleConfig) Value() (driver.Value, error) {+ return json.Marshal(p)}-type ModelSet struct {+type ModelPack struct {+ Id string `json:"id"`Name string `json:"name"`Description string `json:"description"`Planner PlannerRoleConfig `json:"planner"`PlanSummary ModelRoleConfig `json:"planSummary"`- Builder TaskRoleConfig `json:"builder"`- Namer TaskRoleConfig `json:"namer"`- CommitMsg TaskRoleConfig `json:"commitMsg"`- ExecStatus TaskRoleConfig `json:"execStatus"`+ Builder ModelRoleConfig `json:"builder"`+ Namer ModelRoleConfig `json:"namer"`+ CommitMsg ModelRoleConfig `json:"commitMsg"`+ ExecStatus ModelRoleConfig `json:"execStatus"`}type ModelOverrides struct {@@ -244,6 +296,24 @@ type ModelOverrides struct {type PlanSettings struct {ModelOverrides ModelOverrides `json:"modelOverrides"`- ModelSet *ModelSet `json:"modelSet"`+ ModelPack *ModelPack `json:"modelPack"`UpdatedAt time.Time `json:"updatedAt"`}++func (p *PlanSettings) Scan(src interface{}) error {+ if src == nil {+ return nil+ }+ switch s := src.(type) {+ case []byte:+ return json.Unmarshal(s, p)+ case string:+ return json.Unmarshal([]byte(s), p)+ default:+ return fmt.Errorf("unsupported data type: %T", src)+ }+}++func (p PlanSettings) Value() (driver.Value, error) {+ return json.Marshal(p)+}diff --git a/app/shared/plan_result.go b/app/shared/plan_result.goindex 04df5327..84491d58 100644--- a/app/shared/plan_result.go+++ b/app/shared/plan_result.go@@ -91,8 +91,15 @@ func (p PlanFileResultsByPath) ConflictedPaths(filesByPath map[string]string) macontinue}+ maybeWithLineNums := updated+ if res.ReplaceWithLineNums {+ maybeWithLineNums = AddLineNums(updated)+ }+var succeeded bool- updated, succeeded = ApplyReplacements(updated, res.Replacements, false)+ updated, succeeded = ApplyReplacements(maybeWithLineNums, res.Replacements, false)++ updated = RemoveLineNums(updated)// log.Println("updated:", updated)// log.Println("succeeded:", succeeded)diff --git a/app/shared/plan_result_pending_summary.go b/app/shared/plan_result_pending_summary.goindex 8d6c2180..46ecab01 100644--- a/app/shared/plan_result_pending_summary.go+++ b/app/shared/plan_result_pending_summary.go@@ -8,14 +8,14 @@ import ()func (state *CurrentPlanState) PendingChangesSummaryForBuild() string {- return state.pendingChangesSummary(false)+ return state.pendingChangesSummary(false, "")}-func (state *CurrentPlanState) PendingChangesSummaryForApply() string {- return state.pendingChangesSummary(true)+func (state *CurrentPlanState) PendingChangesSummaryForApply(commitSummary string) string {+ return state.pendingChangesSummary(true, commitSummary)}-func (state *CurrentPlanState) pendingChangesSummary(forApply bool) string {+func (state *CurrentPlanState) pendingChangesSummary(forApply bool, commitSummary string) string {var msgs []stringdescByConvoMessageId := make(map[string]*ConvoMessageDescription)@@ -93,7 +93,7 @@ func (state *CurrentPlanState) pendingChangesSummary(forApply bool) string {rebuildPathsSet := make(map[string]bool)if forApply {- msgs = append(msgs, "🤖 Plandex → apply pending changes")+ msgs = append(msgs, "🤖 Plandex → "+commitSummary)} else {for _, ch := range sortedChangesets {allRebuild := true@@ -127,11 +127,8 @@ func (state *CurrentPlanState) pendingChangesSummary(forApply bool) string {for _, ch := range sortedChangesets {var descMsgs []string- if len(ch.descs) == 0 {- // log.Println("Warning: no descriptions for changeset")- // spew.Dump(ch)+ if len(ch.descs) <= 1 {continue- // descMsgs = append(descMsgs, " ✏️ Changes")}for _, desc := range ch.descs {diff --git a/app/shared/plan_result_replacements.go b/app/shared/plan_result_replacements.goindex c20dd011..519cbf30 100644--- a/app/shared/plan_result_replacements.go+++ b/app/shared/plan_result_replacements.go@@ -21,10 +21,15 @@ func ApplyReplacements(content string, replacements []*Replacement, setFailed bopre := updated[:lastInsertedIdx]sub := updated[lastInsertedIdx:]- originalIdx := strings.Index(updated, replacement.Old)+ originalIdx := strings.Index(sub, replacement.Old)// log.Println("originalIdx:", originalIdx)+ // only for use with full replacements, which we aren't using now+ // if originalIdx == -1 {+ // originalIdx = getUniqueFuzzyIndex(updated, replacement.Old)+ // }+if originalIdx == -1 {if setFailed {replacement.Failed = true@@ -38,6 +43,7 @@ func ApplyReplacements(content string, replacements []*Replacement, setFailed bolog.Println(updated)return updated, i+} else {// log.Printf("originalIdx: %d, len(replacement.Old): %d\n", originalIdx, len(replacement.Old))// log.Println("Old: ", replacement.Old)@@ -61,31 +67,34 @@ func ApplyReplacements(content string, replacements []*Replacement, setFailed boreturn updated, -1}- for {- res, failedAtIndex := apply(replacements)+ res, failedAtIndex := apply(replacements)- if failedAtIndex == 0 {- return res, false- } else if failedAtIndex > 0 {- // check if there's overlap between the failed replacement and the previous replacement- // if there is, remove the previous one and try again- failed := replacements[failedAtIndex]- prev := replacements[failedAtIndex-1]+ return res, failedAtIndex == -1- hasOverlap := failed.StreamedChange.Old.StartLine <= prev.StreamedChange.Old.EndLine+ // for {- if hasOverlap {- replacements = append(replacements[:failedAtIndex-1], replacements[failedAtIndex:]...)+ // if failedAtIndex == 0 {+ // return res, false+ // } else if failedAtIndex > 0 {+ // // check if there's overlap between the failed replacement and the previous replacement+ // // if there is, remove the previous one and try again+ // failed := replacements[failedAtIndex]+ // prev := replacements[failedAtIndex-1]- continue- } else {- return res, false- }+ // hasOverlap := failed.StreamedChange.Old.StartLine <= prev.StreamedChange.Old.EndLine- } else {- return res, true- }- }+ // if hasOverlap {+ // replacements = append(replacements[:failedAtIndex-1], replacements[failedAtIndex:]...)++ // continue+ // } else {+ // return res, false+ // }++ // } else {+ // return res, true+ // }+ // }}@@ -160,9 +169,23 @@ func (planState *CurrentPlanState) GetFilesBeforeReplacement(// }var allSucceeded bool- updated, allSucceeded = ApplyReplacements(updated, replacements, false)++ maybeWithLineNums := updated+ if planRes.ReplaceWithLineNums {+ maybeWithLineNums = AddLineNums(updated)+ }++ updated, allSucceeded = ApplyReplacements(maybeWithLineNums, replacements, false)++ updated = RemoveLineNums(updated)if !allSucceeded {+ log.Println("updated:")+ log.Println(updated)++ log.Println("replacements:")+ log.Println(spew.Sdump(replacements))+return nil, fmt.Errorf("plan replacement failed - %s", path)}@@ -184,3 +207,37 @@ func (planState *CurrentPlanState) GetFilesBeforeReplacement(return &CurrentPlanFiles{Files: files, UpdatedAtByPath: updatedAtByPath}, nil}++// func getUniqueFuzzyIndex(doc, s string) int {+// words := strings.Fields(s)+// if len(words) < 2 {+// return -1+// }++// searchStart := regexp.QuoteMeta(words[0])+// searchEnd := regexp.QuoteMeta(words[len(words)-1])++// // Loop to expand search pattern gradually from both ends towards the center+// for i := 0; i < len(words)/2; i++ { // Ensure we do not go out of bounds+// // Construct the regex pattern+// regexPattern := fmt.Sprintf("%s.*%s", searchStart, searchEnd)+// re, err := regexp.Compile(regexPattern)+// if err != nil {+// return -1 // Handle regex compilation error+// }++// // Find all matches of the pattern in the document+// matches := re.FindAllStringIndex(doc, -1)+// if len(matches) == 1 {+// return matches[0][0] // Return the start index of the unique match+// }++// // Update search strings if possible+// if i+1 < len(words)/2 {+// searchStart += " " + regexp.QuoteMeta(words[i+1])+// searchEnd = regexp.QuoteMeta(words[len(words)-i-2]) + " " + searchEnd+// }+// }++// return -1+// }diff --git a/app/shared/plan_settings.go b/app/shared/plan_settings.goindex c682c395..d723d185 100644--- a/app/shared/plan_settings.go+++ b/app/shared/plan_settings.go@@ -2,7 +2,26 @@ package sharedtype ModelProvider string-const ModelProviderOpenAI ModelProvider = "openai"+const (+ ModelProviderOpenAI ModelProvider = "openai"+ ModelProviderTogether ModelProvider = "together"+ ModelProviderOpenRouter ModelProvider = "openrouter"+ ModelProviderCustom ModelProvider = "custom"+)++var AllModelProviders = []string{string(ModelProviderOpenAI), string(ModelProviderTogether), string(ModelProviderCustom)}++var BaseUrlByProvider = map[ModelProvider]string{+ ModelProviderOpenAI: OpenAIV1BaseUrl,+ ModelProviderTogether: "https://api.together.xyz/v1",+ ModelProviderOpenRouter: "https://openrouter.ai/api/v1",+}++var ApiKeyByProvider = map[ModelProvider]string{+ ModelProviderOpenAI: OpenAIEnvVar,+ ModelProviderTogether: "TOGETHER_API_KEY",+ ModelProviderOpenRouter: "OPENROUTER_API_KEY",+}type ModelRole string@@ -34,10 +53,10 @@ var ModelOverridePropsDasherized = []string{"max-convo-tokens", "max-tokens", "rfunc (ps PlanSettings) GetPlannerMaxTokens() int {if ps.ModelOverrides.MaxTokens == nil {- if ps.ModelSet == nil {- return DefaultModelSet.Planner.BaseModelConfig.MaxTokens+ if ps.ModelPack == nil {+ return DefaultModelPack.Planner.BaseModelConfig.MaxTokens} else {- return ps.ModelSet.Planner.BaseModelConfig.MaxTokens+ return ps.ModelPack.Planner.BaseModelConfig.MaxTokens}} else {return *ps.ModelOverrides.MaxTokens@@ -46,10 +65,10 @@ func (ps PlanSettings) GetPlannerMaxTokens() int {func (ps PlanSettings) GetPlannerMaxConvoTokens() int {if ps.ModelOverrides.MaxConvoTokens == nil {- if ps.ModelSet == nil {- return DefaultModelSet.Planner.PlannerModelConfig.MaxConvoTokens+ if ps.ModelPack == nil {+ return DefaultModelPack.Planner.PlannerModelConfig.MaxConvoTokens} else {- return ps.ModelSet.Planner.PlannerModelConfig.MaxConvoTokens+ return ps.ModelPack.Planner.PlannerModelConfig.MaxConvoTokens}} else {return *ps.ModelOverrides.MaxConvoTokens@@ -58,10 +77,10 @@ func (ps PlanSettings) GetPlannerMaxConvoTokens() int {func (ps PlanSettings) GetPlannerReservedOutputTokens() int {if ps.ModelOverrides.ReservedOutputTokens == nil {- if ps.ModelSet == nil {- return DefaultModelSet.Planner.PlannerModelConfig.ReservedOutputTokens+ if ps.ModelPack == nil {+ return DefaultModelPack.Planner.PlannerModelConfig.ReservedOutputTokens} else {- return ps.ModelSet.Planner.PlannerModelConfig.ReservedOutputTokens+ return ps.ModelPack.Planner.PlannerModelConfig.ReservedOutputTokens}} else {return *ps.ModelOverrides.ReservedOutputTokens@@ -71,3 +90,26 @@ func (ps PlanSettings) GetPlannerReservedOutputTokens() int {func (ps PlanSettings) GetPlannerEffectiveMaxTokens() int {return ps.GetPlannerMaxTokens() - ps.GetPlannerReservedOutputTokens()}++func (ps PlanSettings) GetRequiredEnvVars() map[string]bool {+ envVars := map[string]bool{}++ ms := ps.ModelPack+ if ms == nil {+ ms = DefaultModelPack+ }++ envVars[ms.Planner.BaseModelConfig.ApiKeyEnvVar] = true+ envVars[ms.Builder.BaseModelConfig.ApiKeyEnvVar] = true+ envVars[ms.PlanSummary.BaseModelConfig.ApiKeyEnvVar] = true+ envVars[ms.Namer.BaseModelConfig.ApiKeyEnvVar] = true+ envVars[ms.CommitMsg.BaseModelConfig.ApiKeyEnvVar] = true+ envVars[ms.ExecStatus.BaseModelConfig.ApiKeyEnvVar] = true++ // for backward compatibility with <= 0.8.4 server versions+ if len(envVars) == 0 {+ envVars["OPENAI_API_KEY"] = true+ }++ return envVars+}diff --git a/app/shared/req_res.go b/app/shared/req_res.goindex a6e4c389..3f3a6a75 100644--- a/app/shared/req_res.go+++ b/app/shared/req_res.go@@ -107,23 +107,27 @@ const ()type TellPlanRequest struct {- Prompt string `json:"prompt"`- BuildMode BuildMode `json:"buildMode"`- ConnectStream bool `json:"connectStream"`- AutoContinue bool `json:"autoContinue"`- IsUserContinue bool `json:"isUserContinue"`- ApiKey string `json:"apiKey"`- Endpoint string `json:"endpoint"`- OpenAIOrgId string `json:"openAIOrgId"`- ProjectPaths map[string]bool `json:"projectPaths"`+ Prompt string `json:"prompt"`+ BuildMode BuildMode `json:"buildMode"`+ ConnectStream bool `json:"connectStream"`+ AutoContinue bool `json:"autoContinue"`+ IsUserContinue bool `json:"isUserContinue"`+ ApiKey string `json:"apiKey"` // deprecated+ Endpoint string `json:"endpoint"` // deprecated+ ApiKeys map[string]string `json:"apiKeys"`+ OpenAIBase string `json:"openAIBase"`+ OpenAIOrgId string `json:"openAIOrgId"`+ ProjectPaths map[string]bool `json:"projectPaths"`}type BuildPlanRequest struct {- ConnectStream bool `json:"connectStream"`- ApiKey string `json:"apiKey"`- Endpoint string `json:"endpoint"`- OpenAIOrgId string `json:"openAIOrgId"`- ProjectPaths map[string]bool `json:"projectPaths"`+ ConnectStream bool `json:"connectStream"`+ ApiKey string `json:"apiKey"` // deprecated+ Endpoint string `json:"endpoint"` // deprecated+ ApiKeys map[string]string `json:"apiKeys"`+ OpenAIBase string `json:"openAIBase"`+ OpenAIOrgId string `json:"openAIOrgId"`+ ProjectPaths map[string]bool `json:"projectPaths"`}const NoBuildsErr string = "No builds"@@ -213,3 +217,13 @@ type ListUsersResponse struct {Users []*User `json:"users"`OrgUsersByUserId map[string]*OrgUser `json:"orgUsersByUserId"`}++type ApplyPlanRequest struct {+ ApiKeys map[string]string `json:"apiKeys"`+ OpenAIBase string `json:"openAIBase"`+ OpenAIOrgId string `json:"openAIOrgId"`+}++type RenamePlanRequest struct {+ Name string `json:"name"`+}diff --git a/app/shared/streamed_change.go b/app/shared/streamed_change.goindex 71a799e1..fc9bfae4 100644--- a/app/shared/streamed_change.go+++ b/app/shared/streamed_change.go@@ -1,26 +1,103 @@package shared+import (+ "fmt"+ "log"+ "strconv"+ "strings"+)+type StreamedChangeSection struct {- MaybeStartLine int `json:"maybeStartLine"`- MaybeEndLine int `json:"maybeEndLine"`- Err string `json:"err"`+ StartLine int `json:"startLine"`+ EndLine int `json:"endLine"`+ StartLineString string `json:"startLineString"`+ EndLineString string `json:"endLineString"`+}++type StreamedChangeWithLineNums struct {+ Summary string `json:"summary"`+ HasChange bool `json:"hasChange"`+ Old StreamedChangeSection `json:"old"`+ StartLineIncludedReasoning string `json:"startLineIncludedReasoning"`+ StartLineIncluded bool `json:"startLineIncluded"`+ EndLineIncludedReasoning string `json:"endLineIncludedReasoning"`+ EndLineIncluded bool `json:"endLineIncluded"`+ New string `json:"new"`+}- StartLine int `json:"startLine"`- EndLine int `json:"endLine"`+// type StreamedChangeFull struct {+// Summary string `json:"summary"`+// Old string `json:"old"`+// New string `json:"new"`+// }++type StreamedVerifyFunction struct {+ Reasoning string `json:"reasoning"`+ IsCorrect bool `json:"isCorrect"`}-// type StreamedChangeType int+func (streamedChange StreamedChangeWithLineNums) GetLines() (int, int, error) {+ var startLine, endLine int+ var err error++ if streamedChange.Old.StartLineString == "" {+ startLine = streamedChange.Old.StartLine+ } else {+ startLine, err = extractLineNumber(streamedChange.Old.StartLineString)++ if err != nil {+ log.Printf("Error extracting start line number: %v\n", err)+ return 0, 0, fmt.Errorf("error extracting start line number: %v", err)+ }+ }++ if streamedChange.Old.EndLineString == "" {+ if streamedChange.Old.EndLine > 0 {+ endLine = streamedChange.Old.EndLine+ } else {+ endLine = startLine+ }+ } else {+ endLine, err = extractLineNumber(streamedChange.Old.EndLineString)++ if err != nil {+ log.Printf("Error extracting end line number: %v\n", err)+ return 0, 0, fmt.Errorf("error extracting end line number: %v", err)+ }+ }++ if startLine > endLine {+ log.Printf("Start line is greater than end line: %d > %d\n", startLine, endLine)+ return 0, 0, fmt.Errorf("start line is greater than end line: %d > %d", startLine, endLine)+ }++ if startLine < 1 {+ log.Printf("Start line is less than 1: %d\n", startLine)+ return 0, 0, fmt.Errorf("start line is less than 1: %d", startLine)+ }++ return startLine, endLine, nil+}++func extractLineNumber(line string) (int, error) {+ // Split the line at the first space to isolate the line number+ parts := strings.SplitN(line, " ", 2)+ if len(parts) < 2 {+ return 0, fmt.Errorf("invalid line format")+ }++ // Remove the colon from the line number part+ lineNumberStr := strings.TrimSuffix(parts[0], ":")+ lineNumberStr = strings.TrimPrefix(lineNumberStr, "pdx-")+ if lineNumberStr == "" {+ return 0, fmt.Errorf("no line number found")+ }-// const (-// StreamedChangeTypeSimple StreamedChangeType = 1-// StreamedChangeTypeComplex StreamedChangeType = 2-// )+ // Convert the line number part to an integer+ lineNumber, err := strconv.Atoi(lineNumberStr)+ if err != nil {+ return 0, fmt.Errorf("invalid line number: %v", err)+ }-type StreamedChange struct {- Summary string `json:"summary"`- Section string `json:"section"`- // ChangeType StreamedChangeType `json:"changeType"`- Old StreamedChangeSection `json:"old"`- // New StreamedChangeSection `json:"new"`- New string `json:"new"`+ return lineNumber, nil}diff --git a/app/shared/utils.go b/app/shared/utils.goindex 63c90f8d..b50c9a8c 100644--- a/app/shared/utils.go+++ b/app/shared/utils.go@@ -2,6 +2,7 @@ package sharedimport ("crypto/rand"+ "fmt""regexp""strings""time"@@ -63,3 +64,42 @@ func Capitalize(s string) string {}return strings.ToUpper(s[:1]) + s[1:]}++func AddLineNums(s string) string {+ var res string+ for i, line := range strings.Split(s, "\n") {+ res += fmt.Sprintf("pdx-%d: %s\n", i+1, line)+ }+ return res+}++func RemoveLineNums(s string) string {+ return regexp.MustCompile(`(?m)^pdx-\d+: `).ReplaceAllString(s, "")+}++// indexRunes searches for the slice of runes `needle` in the slice of runes `haystack`+// and returns the index of the first rune of `needle` in `haystack`, or -1 if `needle` is not present.+func IndexRunes(haystack []rune, needle []rune) int {+ if len(needle) == 0 {+ return 0+ }+ if len(haystack) == 0 {+ return -1+ }++ // Search for the needle+ for i := 0; i <= len(haystack)-len(needle); i++ {+ found := true+ for j := 0; j < len(needle); j++ {+ if haystack[i+j] != needle[j] {+ found = false+ break+ }+ }+ if found {+ return i+ }+ }++ return -1+}diff --git a/guides/USAGE.md b/guides/USAGE.mdindex 19a7d839..3149bfcd 100644--- a/guides/USAGE.md+++ b/guides/USAGE.md@@ -1,20 +1,28 @@# Using Plandex 🛠️-## OpenAI API key 🔑+## Provider API key(s) 🔑++By default, Plandex uses the OpenAI API. While you can now use Plandex with many different providers and models, for some functionality Plandex has requirements that only OpenAI models, for the time being, can satisfy. That means you'll need an OpenAI account to get started even if you plan to use other model providers.If you don't have an OpenAI account, first [sign up here.](https://platform.openai.com/signup)Then [generate an API key here.](https://platform.openai.com/account/api-keys)-Support for more model providers is coming soon.+You'll also need API keys for any other providers you plan on using, like OpenRouter.ai (Anthropic, Gemini, and open source models), Together.ai (open source models), Replicate, Ollama, and more.## New plan 🪄```bashcd your-project+export OPENAI_API_KEY=...-export OPENAI_ENDPOINT=... # optional e.g. https://<your-proxy>/v1+export OPENAI_API_BASE=... # optional e.g. https://<your-proxy>/v1export OPENAI_ORG_ID=... # optional - set the OrgID if you have multiple OpenAI orgs++# optional - set api keys for any other providers you're using+export OPENROUTER_API_KEY=...+export TOGETHER_API_KEY...+plandex new```@@ -142,17 +150,30 @@ plandex update # update files in context## Plans 🌟-When you have multiple plans, you can list them with the `plans` command, switch between them with the `cd` command, see the current plan with the `current` command, and delete plans with the `delete-plan` command. Archiving of plans will be added in the future for plans that you want to keep around but aren't currently working on.+When you have multiple plans, you can list them with the `plans` command, switch between them with the `cd` command, see the current plan with the `current` command, and delete plans with the `delete-plan` command.++You can archive plans you want to keep around but aren't currently working on with the `archive` command. You can see archived plans in the current directory with `plans --archived`. You can unarchive a plan with the `unarchive` command.```plandex plans # list all plans+plandex cd # select from a list of plansplandex cd some-other-plan # cd to a plan by nameplandex cd 2 # cd to a plan by number in the `plandex plans` list+plandex current # show the current plan+plandex delete-plan # select from a list of plans to deleteplandex delete-plan some-plan # delete a plan by nameplandex delete-plan 4 # delete a plan by number in the `plandex plans` list++plandex archive # select from a list of plans to archive+plandex archive some-plan # archive a plan by name+plandex archive 2 # archive a plan by number in the `plandex plans` list++plandex unarchive # select from a list of archived plans to unarchive+plandex unarchive some-plan # unarchive a plan by name+plandex unarchive 2 # unarchive a plan by number in the `plandex plans --archived` list```## Conversation history 💬@@ -167,14 +188,21 @@ plandex convo # show the full conversation historyEvery time the AI model replies, Plandex will summarize the conversation so far in the background and store the summary in case it's needed later. When the conversation size in tokens exceeds the model's limit, Plandex will automatically replace some number of older messages with the corresponding summary. It will summarize as many messages as necessary to keep the conversation size under the limit.+You can see the latest summary with the `summary` command.++```bash+plandex summary # show the latest conversation summary+```+## Model settings 🧠You can see the current AI models and model settings with the `models` command and change them with the `set-model` command.```bashplandex models # show the current AI models and model settings+plandex models available # show all available modelsplandex set-model # select from a list of models and settings-plandex set-model planner gpt-4 # set the main planner model to gpt-4+plandex set-model planner openrouter/anthropic/claude-opus-3 # set the main planner model to Claude Opus 3 from OpenRouter.aiplandex set-model builder temperature 0.1 # set the builder model's temperature to 0.1plandex set-model max-tokens 4000 # set the planner model overall token limit to 4000plandex set-model max-convo-tokens 20000 # set how large the conversation can grow before Plandex starts using summaries@@ -182,6 +210,41 @@ plandex set-model max-convo-tokens 20000 # set how large the conversation can gModel changes are versioned and can be rewound or applied to a branch just like any other change.+### Model defaults++`set-model` udpates model settings for the current plan. If you want to change the default model settings for all new plans, use `set-model default`.++```bash+plandex models default # show the default model settings+plandex set-model default # select from a list of models and settings+plandex set-model default planner openai/gpt-4 # set the default planner model to OpenAI gpt-4+```++### Custom models++Use `models add` to add a custom model and use any provider that is compatible with OpenAI, including OpenRouter.ai, Together.ai, Ollama, Replicate, and more.++```bash+plandex models add # add a custom model+plandex models available --custom # show all available custom models+plandex models delete # delete a custom model+```++### Model packs++Instead of changing models for each role one by one, a model pack lets you switch out all roles at once. You can create your own model packs with `model-packs create`, list built-in and custom model packs with `model-packs`, and remove custom model packs with `model-packs delete`.++```bash+plandex set-model # select from a list of model packs for the current plan+plandex set-model default # select from a list of model packs to set as the default for all new plans+plandex set-model gpt-4-turbo-latest # set the current plan's model pack by name+plandex set-model default gpt-4-turbo-latest # set the default model pack for all new plans++plandex model-packs # list built-in and custom model packs+plandex model-packs create # create a new custom model pack+plandex model-packs --custom # list only custom model packs+```+## .plandex directory ⚙️When you run `plandex new` for the first time in any directory, Plandex will create a `.plandex` directory there for light project-level config.diff --git a/prompts/model-sets-custom-models-crud.txt b/prompts/model-sets-custom-models-crud.txtnew file mode 100644index 00000000..ad131a63--- /dev/null+++ b/prompts/model-sets-custom-models-crud.txt@@ -0,0 +1,21 @@+I want to add functionality on both the CLI (cobra commands, api calls) and server (routes, handlers, db access functions) that allow a user to create, list, and delete custom models, and also create, list, or delete model sets for their org. I also want to add commands that list all the available models and model sets, including both built-in and user-created models and model sets.++Use the existing functionality for models and model sets as a guide and follow the same architecture, coding style, and ux.++Here are the commands/subcommands I want to add:++`plandex models available` - list all available models, both built-in and custom, in a nicely formatted table (use tablewriter like the other commands)++`plandex models create` - use terminal prompts in a similar way to the 'set-model' command to prompt the user for all necessary values to create a custom model, then call the api function to store it on the server++`plandex models delete` - prompt the user to choose from a list of custom models to delete++`plandex model-sets` - list all available model sets, both built-in and user-created++`plandex model-sets create` - use terminal prompts in a similar way to the 'set-model' command to prompt the user for all necessary values to create a model set, then call the api function to store it on the server++Add all the required CLI and server code to make this work. Put the server-side handlers in 'app/server/handlers/models.go'. Create a new file in 'app/server/db/' for the db access functions.++On the client-side, update the Api interface and implementation to add the new api calls.++diff --git a/releases/cli/versions/0.9.0.md b/releases/cli/versions/0.9.0.mdindex bb94b166..78b1f4f0 100644--- a/releases/cli/versions/0.9.0.md+++ b/releases/cli/versions/0.9.0.md@@ -1 +1,52 @@-- Better message in `plandex log` when a single piece of context is loaded or updated\ No newline at end of file+## Custom models! 🧠+### Use Plandex with models from OpenRouter, Together.ai, and more+- Use `plandex models add` to add a custom model and use any provider that is compatible with OpenAI, including OpenRouter.ai, Together.ai, Ollama, Replicate, and more.+- Anthropic Claude models are available via OpenRouter.ai. Google Gemini 1.5 preview is also available on OpenRouter.ai but was flakey in initial testing. Tons of open source models are available on both OpenRouter.ai and Together.ai, among other providers.+- Some built-in models and model packs (see 'Model packs' below) have been included as a quick way to try out a few of the more powerful model options. Just use `plandex set-model` to try these.+- You can use a custom model you've added with `plandex set-model`, or add it to a model pack (see 'Model packs' below) with `plandex model-packs create`. Delete custom models you've added with `plandex models delete`.+- The roles a custom model can be used for depends on its OpenAI compatibility. You can set a model's compatibility when adding it with `plandex models add`. That determines which roles it can be used for.+- Each model provider has an `ApiKeyEnvVar` associated with it, like `OPENROUTER_API_KEY`, `TOGETHER_API_KEY`, etc. You will need to have the appropriate environment variables set with a valid api key for each provider that you're using.+- Because all of Plandex's prompts have been tested against OpenAI models, support for new models should be considered **experimental**.+- If you find prompting patterns that are effective for certain models, please share them on Discord (https://discord.gg/plandex-ai) or GitHub (https://github.com/plandex-ai/plandex/discussions) and they may be included in future releases.++## Major file update improvements 📄+- Much better accuracy for updates to existing files.+- Plandex is less likely to screw up braces, parentheses, and other code structures.+- Plandex is less likely to mistakenly remove code that it shouldn't.++## Major improvements to long plans with many steps 🛤️+- Plandex's 'working memory' has been upgraded. It is now much better at working through very long plans without skipping tasks, repeating tasks it's already done, or otherwise losing track of what it's doing.++## 'plandex diff' command ⚖️+- New `plandex diff` command shows pending plan changes in `git diff` format.++## Plans can be archived 🗄️+- If you aren't using a plan anymore, but you don't want to delete it, you can now archive it.+- Use `plandex archive` (or `plandex arc` for short) to archive a plan.+- Use `plandex plans --archived` (or `plandex plans -a`) to see archived plans in the current directory.+- Use `plandex unarchive` (or `plandex unarc`) to restore an archived plan.++## Model packs 🎛️+- Instead of changing models for each role one by one, a model packs let you switch out all roles at once.+- Use `plandex model-packs create` to create your own model packs.+- Use `plandex model-packs` to list built-in and custom model packs.+- Use `plandex set-model` to load a model pack.+- Use `plandex model-packs delete` to remove a custom model pack.++## Model defaults ⚙️+- Instead of only changing models on a per-plan basis, you can set model defaults that will apply to all new plans you start.+- Use `plandex models default` to see default model settings and `plandex set-model default` to update them.++## More commands 💻+- `plandex summary` to see a summary of the current state of the plan.+- `plandex rename` to rename the current plan++## Quality of life improvements 🧘♀️+- Descriptive top-line for `plandex apply` commit messages instead of just "applied pending changes".+- Better message in `plandex log` when a single piece of context is loaded or updated.+- Abbreviate really long file paths in `plandex ls`.+- Changed OPENAI_ENDPOINT env var to OPENAI_API_BASE, which is more standardized. OPENAI_ENDPOINT is still quietly supported.++## Bug fixes 🐞+- Fix for potential crash in `plandex changes` TUI.+- Fixes for some rare potential deadlocks and conflicts when building a file or stopping astream.diff --git a/releases/server/versions/0.9.0.md b/releases/server/versions/0.9.0.mdindex bb94b166..627bba46 100644--- a/releases/server/versions/0.9.0.md+++ b/releases/server/versions/0.9.0.md@@ -1 +1,12 @@-- Better message in `plandex log` when a single piece of context is loaded or updated\ No newline at end of file+- Support for custom models, model packs, and default models (see CLI 0.9.0 release notes for details).+- Better accuracy for updates to existing files.+- Plandex is less likely to screw up braces, parentheses, and other code structures.+- Plandex is less likely to mistakenly remove code that it shouldn't.+- Plandex is now much better at working through very long plans without skipping tasks, repeating tasks it's already done, or otherwise losing track of what it's doing.+- Server-side support for `plandex diff` command to show pending plan changes in `git diff` format.+- Server-side support for archiving and unarchiving plans.+- Server-side support for `plandex summary` command.+- Server-side support for `plandex rename` command.+- Descriptive top-line for `plandex apply` commit messages instead of just "applied pending changes".+- Better message in `plandex log` when a single piece of context is loaded or updated.+- Fixes for some rare potential deadlocks and conflicts when building a file or stopping astream.\ No newline at end of file