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
various fixes--working on outdated context fix
Dane Schneider984bf385eb5b
diff --git a/app/Dockerfile.server b/app/Dockerfile.serverindex 900110a9..9026509e 100644--- a/app/Dockerfile.server+++ b/app/Dockerfile.server@@ -1,4 +1,7 @@-FROM golang:1.21+FROM golang:1.21.3++RUN apt-get update && \+ apt-get install -y git=2.43.0WORKDIR /appdiff --git a/app/cli/api/methods.go b/app/cli/api/methods.goindex 5a564dce..c6c7df5b 100644--- a/app/cli/api/methods.go+++ b/app/cli/api/methods.go@@ -627,7 +627,7 @@ func (a *Api) RejectAllChanges(planId, branch string) *shared.ApiError {}func (a *Api) RejectResult(planId, branch, resultId string) *shared.ApiError {- serverUrl := fmt.Sprintf("%s/plans/%s/%s/results/%s/reject", getApiHost(), planId, resultId)+ serverUrl := fmt.Sprintf("%s/plans/%s/%s/results/%s/reject", getApiHost(), planId, branch, resultId)req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)if err != nil {@@ -654,7 +654,7 @@ func (a *Api) RejectResult(planId, branch, resultId string) *shared.ApiError {}func (a *Api) RejectReplacement(planId, branch, resultId, replacementId string) *shared.ApiError {- serverUrl := fmt.Sprintf("%s/plans/%s/%s/results/%s/replacements/%s/reject", getApiHost(), planId, resultId, replacementId)+ serverUrl := fmt.Sprintf("%s/plans/%s/%s/results/%s/replacements/%s/reject", getApiHost(), planId, branch, resultId, replacementId)req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)if err != nil {@@ -1397,7 +1397,7 @@ func (a *Api) CreateBranch(planId, branch string, req shared.CreateBranchRequest}func (a *Api) DeleteBranch(planId, branch string) *shared.ApiError {- serverUrl := fmt.Sprintf("%s/plans/%s/branches/%s", getApiHost(), planId)+ serverUrl := fmt.Sprintf("%s/plans/%s/branches/%s", getApiHost(), planId, branch)req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)if err != nil {diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.goindex 7f34ebf9..4739d980 100644--- a/app/cli/cmd/continue.go+++ b/app/cli/cmd/continue.go@@ -2,6 +2,7 @@ package cmdimport ("fmt"+ "log""os""plandex/api""plandex/auth"@@ -38,12 +39,14 @@ func next(cmd *cobra.Command, args []string) {lib.MustCheckOutdatedContextWithOutput()if !continueBg {- err := streamtui.StartStreamUI()+ go func() {+ err := streamtui.StartStreamUI()- if err != nil {- fmt.Fprintln(os.Stderr, "Error starting stream UI:", err)- return- }+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error starting stream UI:", err)+ os.Exit(1)+ }+ }()}var fn func() bool@@ -53,6 +56,7 @@ func next(cmd *cobra.Command, args []string) {ConnectStream: !continueBg,}, lib.OnStreamPlan)if apiErr != nil {+ log.Println("Error telling plan:", apiErr)if apiErr.Type == shared.ApiErrorTypeTrialMessagesExceeded {streamtui.Quit()@@ -84,5 +88,16 @@ func next(cmd *cobra.Command, args []string) {return true}- fn()+ shouldContinue := fn()+ if !shouldContinue {+ return+ }++ if tellBg {+ fmt.Println("β Prompt sent")+ } else {+ // Wait for stream UI to quit+ select {}+ }+}diff --git a/app/cli/cmd/convo.go b/app/cli/cmd/convo.goindex b007f8f8..24deb138 100644--- a/app/cli/cmd/convo.go+++ b/app/cli/cmd/convo.go@@ -68,7 +68,7 @@ func convo(cmd *cobra.Command, args []string) {formattedTs = msg.CreatedAt.Local().Format("Yesterday | 3:04pm MST")}- header := fmt.Sprintf("#### %d | %s | %s | %d πͺ | ", i+1,+ header := fmt.Sprintf("#### %d | %s | %s | %d πͺ ", i+1,author, formattedTs, msg.Tokens)convMarkdown = append(convMarkdown, header, msg.Message, "")totalTokens += msg.Tokensdiff --git a/app/cli/cmd/plans.go b/app/cli/cmd/plans.goindex e448e58e..4d98fc3b 100644--- a/app/cli/cmd/plans.go+++ b/app/cli/cmd/plans.go@@ -40,6 +40,13 @@ func plans(cmd *cobra.Command, args []string) {return}+ if len(plans) == 0 {+ fmt.Println("π€·ββοΈ No plans")+ fmt.Println()+ term.PrintCmds("", "new")+ return+ }+var planIds []stringfor _, p := range plans {planIds = append(planIds, p.Id)@@ -65,13 +72,6 @@ func plans(cmd *cobra.Command, args []string) {table.SetAutoWrapText(false)table.SetHeader([]string{"#", "Name", "Updated", "Created", "Branches", "Branch", "Context", "Convo"})- if len(plans) == 0 {- fmt.Println("π€·ββοΈ No plans")- fmt.Println()- term.PrintCmds("", "new")- return- }-for i, p := range plans {var name stringdiff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.goindex c6436b6e..dd299105 100644--- a/app/cli/cmd/tell.go+++ b/app/cli/cmd/tell.go@@ -2,7 +2,6 @@ package cmdimport ("fmt"- "log""os""os/exec""plandex/api"@@ -132,8 +131,6 @@ func tell(cmd *cobra.Command, args []string) {fmt.Fprintln(os.Stderr, "Error starting stream UI:", err)os.Exit(1)}-- os.Exit(0)}()}@@ -181,8 +178,6 @@ func tell(cmd *cobra.Command, args []string) {return}- log.Println("Prompt sent")-if tellBg {fmt.Println("β Prompt sent")} else {diff --git a/app/cli/lib/current.go b/app/cli/lib/current.goindex 2ec8a9b3..c1f55537 100644--- a/app/cli/lib/current.go+++ b/app/cli/lib/current.go@@ -96,17 +96,19 @@ func MustLoadCurrentPlan() {CurrentPlanId = currentPlan.Id- err = loadCurrentBranch()+ if CurrentPlanId != "" {+ err = loadCurrentBranch()- if err != nil {- panic(fmt.Errorf("error loading current branch: %v", err))- }+ if err != nil {+ panic(fmt.Errorf("error loading current branch: %v", err))+ }- if CurrentBranch == "" {- err = WriteCurrentBranch("main")+ if CurrentBranch == "" {+ err = WriteCurrentBranch("main")- if err != nil {- panic(fmt.Errorf("error setting current branch: %v", err))+ if err != nil {+ panic(fmt.Errorf("error setting current branch: %v", err))+ }}}}diff --git a/app/cli/lib/plans.go b/app/cli/lib/plans.goindex 4176f52e..1d50eab3 100644--- a/app/cli/lib/plans.go+++ b/app/cli/lib/plans.go@@ -79,7 +79,15 @@ func WriteCurrentBranch(branch string) error {return fmt.Errorf("error marshalling current plan settings: %v", err)}- path := filepath.Join(fs.HomePlandexDir, CurrentProjectId, CurrentPlanId, "settings.json")+ dir := filepath.Join(fs.HomePlandexDir, CurrentProjectId, CurrentPlanId)++ err = os.MkdirAll(dir, os.ModePerm)++ if err != nil {+ return fmt.Errorf("error creating plan dir: %v", err)+ }++ path := filepath.Join(dir, "settings.json")err = os.WriteFile(path, bytes, 0644)diff --git a/app/cli/stream_tui/run.go b/app/cli/stream_tui/run.goindex 0a6c386d..e80d7942 100644--- a/app/cli/stream_tui/run.go+++ b/app/cli/stream_tui/run.go@@ -10,6 +10,7 @@ import (var ui *tea.Programvar mu sync.Mutex+var wg sync.WaitGroupfunc StartStreamUI() error {initial := initialModel()@@ -18,7 +19,9 @@ func StartStreamUI() error {ui = tea.NewProgram(initial, tea.WithAltScreen())mu.Unlock()+ wg.Add(1)_, err := ui.Run()+ wg.Done()if err != nil {return fmt.Errorf("error running changes UI: %v", err)@@ -35,6 +38,8 @@ func Quit() {mu.Lock()defer mu.Unlock()ui.Quit()++ wg.Wait() // Wait for the UI to fully terminate}func Send(msg tea.Msg) {diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.goindex 9deacbf6..60fd61ad 100644--- a/app/server/db/context_helpers.go+++ b/app/server/db/context_helpers.go@@ -19,6 +19,10 @@ func GetPlanContexts(orgId, planId string, includeBody bool) ([]*Context, error)// get all context filesfiles, err := os.ReadDir(contextDir)if err != nil {+ if os.IsNotExist(err) {+ return contexts, nil+ }+return nil, fmt.Errorf("error reading context dir: %v", err)}@@ -117,6 +121,11 @@ func ContextRemove(contexts []*Context) error {func StoreContext(context *Context) error {contextDir := getPlanContextDir(context.OrgId, context.PlanId)+ err := os.MkdirAll(contextDir, os.ModePerm)+ if err != nil {+ return fmt.Errorf("error creating context dir: %v", err)+ }+ts := time.Now().UTC()if context.Id == "" {context.Id = uuid.New().String()diff --git a/app/server/db/convo_helpers.go b/app/server/db/convo_helpers.goindex 0a889e96..391aa126 100644--- a/app/server/db/convo_helpers.go+++ b/app/server/db/convo_helpers.go@@ -18,6 +18,10 @@ func GetPlanConvo(orgId, planId string) ([]*ConvoMessage, error) {files, err := os.ReadDir(convoDir)if err != nil {+ if os.IsNotExist(err) {+ return convo, nil+ }+return nil, fmt.Errorf("error reading convo dir: %v", err)}@@ -79,6 +83,12 @@ func StoreConvoMessage(message *ConvoMessage, currentUserId, branch string, commreturn "", fmt.Errorf("error marshalling convo message: %v", err)}+ err = os.MkdirAll(convoDir, os.ModePerm)++ if err != nil {+ return "", fmt.Errorf("error creating convo dir: %v", err)+ }+err = os.WriteFile(filepath.Join(convoDir, message.Id+".json"), bytes, os.ModePerm)if err != nil {diff --git a/app/server/db/db.go b/app/server/db/db.goindex 320ec981..8cb5621f 100644--- a/app/server/db/db.go+++ b/app/server/db/db.go@@ -26,6 +26,15 @@ func Connect() error {if err != nil {return err}++ log.Println("connected to database")++ _, err = Conn.Exec("SET TIMEZONE='UTC';")++ if err != nil {+ return fmt.Errorf("error setting timezone: %v", err)+ }+return nil}@@ -50,24 +59,24 @@ 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 := 2024011700- // if err := m.Force(migrateVersion); err != nil {- // return fmt.Errorf("error forcing migration version: %v", err)+ // migrateVersion := 2024012400+ // if err := m.Force(migrateVersion); err != nil {+ // return fmt.Errorf("error forcing migration version: %v", err)// }// }// Uncomment below to run down migrations (RESETS DATABASE!!)- if os.Getenv("GOENV") == "development" {- err = m.Down()- if err != nil {- if err == migrate.ErrNoChange {- log.Println("no migrations to run down")- } else {- return fmt.Errorf("error running down migrations: %v", err)- }- }- log.Println("ran down migrations - database was reset")- }+ // if os.Getenv("GOENV") == "development" {+ // err = m.Down()+ // if err != nil {+ // if err == migrate.ErrNoChange {+ // log.Println("no migrations to run down")+ // } else {+ // return fmt.Errorf("error running down migrations: %v", err)+ // }+ // }+ // log.Println("ran down migrations - database was reset")+ // }err = m.Up()diff --git a/app/server/db/git.go b/app/server/db/git.goindex be740953..1bab6687 100644--- a/app/server/db/git.go+++ b/app/server/db/git.go@@ -120,6 +120,22 @@ func GitDeleteBranch(orgId, planId, branchName string) error {}func gitCheckoutBranch(repoDir, branch string) error {+ // get current branch and only checkout if it's not the same+ // trying to check out the same branch will result in an error+ var out bytes.Buffer+ cmd := exec.Command("git", "-C", repoDir, "branch", "--show-current")+ cmd.Stdout = &out+ err := cmd.Run()+ if err != nil {+ return fmt.Errorf("error getting current git branch for dir: %s, err: %v", repoDir, err)+ }++ currentBranch := strings.TrimSpace(out.String())++ if currentBranch == branch {+ return nil+ }+res, err := exec.Command("git", "-C", repoDir, "checkout", branch).CombinedOutput()if err != nil {return fmt.Errorf("error checking out git branch for dir: %s, err: %v, output: %s", repoDir, err, string(res))diff --git a/app/server/db/models.go b/app/server/db/models.goindex c0e406d0..04deb484 100644--- a/app/server/db/models.go+++ b/app/server/db/models.go@@ -166,6 +166,7 @@ type Branch struct {ArchivedAt *time.Time `db:"archived_at,omitempty"`CreatedAt time.Time `db:"created_at"`UpdatedAt time.Time `db:"updated_at"`+ DeletedAt *time.Time `db:"deleted_at"`}func (branch *Branch) ToApi() *shared.Branch {@@ -254,6 +255,7 @@ type ModelStream struct {OrgId string `db:"org_id"`PlanId string `db:"plan_id"`InternalIp string `db:"internal_ip"`+ Branch string `db:"branch"`CreatedAt time.Time `db:"created_at"`FinishedAt *time.Time `db:"finished_at"`}diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.goindex e7850dff..e35b65b2 100644--- a/app/server/db/plan_helpers.go+++ b/app/server/db/plan_helpers.go@@ -71,11 +71,16 @@ func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {return nil, fmt.Errorf("error initializing plan dir: %v", err)}+ // commit the transaction+ if err := tx.Commit(); err != nil {+ return nil, fmt.Errorf("error committing transaction: %v", err)+ }+return plan, nil}-func ListOwnedPlans(projectId, userId string, archived bool) ([]shared.Plan, error) {- qs := "SELECT id, owner_id, name, shared_with_org_at, archived_at, created_at, updated_at FROM plans WHERE project_id = $1 AND owner_id = $2"+func ListOwnedPlans(projectId, userId string, archived bool) ([]*Plan, error) {+ qs := "SELECT * FROM plans WHERE project_id = $1 AND owner_id = $2"qargs := []interface{}{projectId, userId}if archived {@@ -85,25 +90,14 @@ func ListOwnedPlans(projectId, userId string, archived bool) ([]shared.Plan, err}qs += " ORDER BY updated_at DESC"- res, err := Conn.Query(qs, qargs...)++ var plans []*Plan+ err := Conn.Select(&plans, qs, qargs...)if err != nil {return nil, fmt.Errorf("error listing plans: %v", err)}- defer res.Close()- var plans []shared.Plan- for res.Next() {- var plan shared.Plan-- err := res.Scan(&plan.Id, &plan.OwnerId, &plan.Name, &plan.SharedWithOrgAt, &plan.ArchivedAt, &plan.CreatedAt, &plan.UpdatedAt)-- if err != nil {- return nil, fmt.Errorf("error scanning plan: %v", err)- }- plans = append(plans, plan)- }-return plans, nil}@@ -186,7 +180,7 @@ func SyncPlanTokens(orgId, planId, branch string) error {convoTokens += msg.Tokens}- _, err := Conn.Exec("UPDATE plans SET context_tokens = $1, convo_tokens = $2 WHERE id = $3", contextTokens, convoTokens, planId)+ _, err := Conn.Exec("UPDATE branches SET context_tokens = $1, convo_tokens = $2 WHERE plan_id = $3 AND name = $4", contextTokens, convoTokens, planId, branch)if err != nil {return fmt.Errorf("error updating plan tokens: %v", err)@@ -198,7 +192,7 @@ func SyncPlanTokens(orgId, planId, branch string) error {func GetPlan(planId string) (*Plan, error) {var plan Plan- err := Conn.Get(&plan, "SELECT id, org_id, owner_id, project_id, name, status, error, context_tokens, convo_tokens, shared_with_org_at, total_replies, archived_at, created_at, updated_at FROM plans WHERE id = $1", planId)+ err := Conn.Get(&plan, "SELECT * FROM plans WHERE id = $1", planId)if err != nil {return nil, fmt.Errorf("error getting plan: %v", err)@@ -207,8 +201,8 @@ func GetPlan(planId string) (*Plan, error) {return &plan, nil}-func SetPlanStatus(planId string, status shared.PlanStatus, errStr string) error {- _, err := Conn.Exec("UPDATE plans SET status = $1, error = $2 WHERE id = $3", status, errStr, planId)+func SetPlanStatus(planId, branch string, status shared.PlanStatus, errStr string) error {+ _, err := Conn.Exec("UPDATE branches SET status = $1, error = $2 WHERE plan_id = $3 AND name = $4", status, errStr, planId, branch)if err != nil {return fmt.Errorf("error setting plan status: %v", err)@@ -250,6 +244,12 @@ func IncNumNonDraftPlans(userId string) error {func StoreDescription(description *ConvoMessageDescription) error {descriptionsDir := getPlanDescriptionsDir(description.OrgId, description.PlanId)+ err := os.MkdirAll(descriptionsDir, os.ModePerm)++ if err != nil {+ return fmt.Errorf("error creating convo message descriptions dir: %v", err)+ }+now := time.Now()if description.Id == "" {diff --git a/app/server/db/result_helpers.go b/app/server/db/result_helpers.goindex 5a5dcc14..29164ef4 100644--- a/app/server/db/result_helpers.go+++ b/app/server/db/result_helpers.go@@ -158,15 +158,19 @@ func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanStat}func GetPendingBuildDescriptions(orgId, planId string) ([]*ConvoMessageDescription, error) {+ var descriptions []*ConvoMessageDescriptiondescriptionsDir := getPlanDescriptionsDir(orgId, planId)-files, err := os.ReadDir(descriptionsDir)if err != nil {++ if os.IsNotExist(err) {+ return descriptions, nil+ }+return nil, fmt.Errorf("error reading descriptions dir: %v", err)}- var descriptions []*ConvoMessageDescriptionerrCh := make(chan error, len(files))descCh := make(chan *ConvoMessageDescription, len(files))diff --git a/app/server/go.mod b/app/server/go.modindex 6a29c27d..9cb02fc2 100644--- a/app/server/go.mod+++ b/app/server/go.mod@@ -35,7 +35,6 @@ require (github.com/golang-migrate/migrate/v4 v4.16.2github.com/jmoiron/sqlx v1.3.5github.com/lib/pq v1.10.9- github.com/looplab/fsm v1.0.1 // indirect)replace github.com/plandex/plandex/shared => ../shareddiff --git a/app/server/go.sum b/app/server/go.sumindex 62fc0f88..78c88536 100644--- a/app/server/go.sum+++ b/app/server/go.sum@@ -47,8 +47,6 @@ github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccgithub.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=-github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=-github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=diff --git a/app/server/handlers/plans_crud.go b/app/server/handlers/plans_crud.goindex 1c04d7f8..83db9160 100644--- a/app/server/handlers/plans_crud.go+++ b/app/server/handlers/plans_crud.go@@ -280,7 +280,12 @@ func ListPlansHandler(w http.ResponseWriter, r *http.Request) {return}- bytes, err := json.Marshal(plans)+ var apiPlans []*shared.Plan+ for _, plan := range plans {+ apiPlans = append(apiPlans, plan.ToApi())+ }++ bytes, err := json.Marshal(apiPlans)if err != nil {log.Printf("Error marshalling plan ids: %v\n", err)@@ -393,7 +398,12 @@ func ListArchivedPlansHandler(w http.ResponseWriter, r *http.Request) {return}- jsonBytes, err := json.Marshal(plans)+ var apiPlans []*shared.Plan+ for _, plan := range plans {+ apiPlans = append(apiPlans, plan.ToApi())+ }++ jsonBytes, err := json.Marshal(apiPlans)if err != nil {log.Printf("Error marshalling plans: %v\n", err)http.Error(w, "Error marshalling plans: "+err.Error(), http.StatusInternalServerError)diff --git a/app/server/handlers/plans_exec.go b/app/server/handlers/plans_exec.goindex 5736aa0f..916d2853 100644--- a/app/server/handlers/plans_exec.go+++ b/app/server/handlers/plans_exec.go@@ -2,11 +2,13 @@ package handlersimport ("encoding/json"+ "fmt""io""log""net/http""os""plandex-server/db"+ "plandex-server/host"model "plandex-server/model/plan""plandex-server/types"@@ -93,14 +95,12 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {}if requestBody.ConnectStream {- active := model.Active.Get(planId)- subscriptionId, ch := model.SubscribePlan(planId)-+ active := model.GetActivePlan(planId, branch)+ subscriptionId, ch := model.SubscribePlan(planId, branch)startResponseStream(w, ch, active.Ctx, func() {- model.UnsubscribePlan(planId, subscriptionId)+ model.UnsubscribePlan(planId, branch, subscriptionId)})}-}func BuildPlanHandler(w http.ResponseWriter, r *http.Request) {@@ -140,8 +140,6 @@ func ConnectPlanHandler(w http.ResponseWriter, r *http.Request) {}func StopPlanHandler(w http.ResponseWriter, r *http.Request) {- // TODO: this handler should lookup the IP of the active plan stream and forward the request if it's not the same as this server's IP-log.Println("Received request for StopPlanHandler")auth := authenticate(w, r, true)if auth == nil {@@ -158,6 +156,36 @@ func StopPlanHandler(w http.ResponseWriter, r *http.Request) {return}+ active := model.GetActivePlan(planId, branch)++ if active == nil {+ modelStream, err := db.GetActiveModelStream(planId, branch)++ if err != nil {+ log.Printf("Error getting active model stream: %v\n", err)+ http.Error(w, "Error getting active model stream", http.StatusInternalServerError)+ return+ }++ if modelStream == nil {+ log.Printf("No active model stream for plan %s\n", planId)+ http.Error(w, "No active model stream for plan", http.StatusNotFound)+ return+ }++ if modelStream.InternalIp == host.Ip {+ db.SetModelStreamFinished(modelStream.Id)+ log.Printf("No active plan for plan %s\n", planId)+ http.Error(w, "No active plan for plan", http.StatusNotFound)+ return+ } else {+ log.Printf("Forwarding request to %s\n", modelStream.InternalIp)+ proxyUrl := fmt.Sprintf("http://%s:%s/plans/%s/%s/stop", modelStream.InternalIp, os.Getenv("EXTERNAL_PORT"), planId, branch)+ proxyRequest(w, r, proxyUrl)+ return+ }+ }+unlockFn := lockRepo(w, r, auth, db.LockScopeWrite)if unlockFn == nil {returndiff --git a/app/server/main.go b/app/server/main.goindex 5858c914..1054b956 100644--- a/app/server/main.go+++ b/app/server/main.go@@ -51,7 +51,7 @@ func main() {<-sigTermChanfor {- l := plan.Active.Len()+ l := plan.NumActivePlans()if l == 0 {break}diff --git a/app/server/migrations/2023120500_init.down.sql b/app/server/migrations/2023120500_init.down.sqlindex 2d23a548..2996e412 100644--- a/app/server/migrations/2023120500_init.down.sql+++ b/app/server/migrations/2023120500_init.down.sql@@ -1,3 +1,5 @@+DROP TABLE IF EXISTS branches;+DROP TABLE IF EXISTS convo_summaries;DROP TABLE IF EXISTS plan_builds;diff --git a/app/server/migrations/2023120500_init.up.sql b/app/server/migrations/2023120500_init.up.sqlindex f6dc307c..066cbfdc 100644--- a/app/server/migrations/2023120500_init.up.sql+++ b/app/server/migrations/2023120500_init.up.sql@@ -130,7 +130,7 @@ CREATE TABLE IF NOT EXISTS branches (shared_with_org_at TIMESTAMP,archived_at TIMESTAMP,created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW()+ updated_at TIMESTAMP NOT NULL DEFAULT NOW(),deleted_at TIMESTAMP);CREATE TRIGGER update_branches_modtime BEFORE UPDATE ON branches FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();diff --git a/app/server/model/plan/build.go b/app/server/model/plan/build.goindex 84154166..2d6e50f4 100644--- a/app/server/model/plan/build.go+++ b/app/server/model/plan/build.go@@ -25,10 +25,10 @@ const MaxRetries = 3const MaxReplacementRetries = 1func QueueBuild(currentOrgId, currentUserId, planId, branch string, activeBuild *types.ActiveBuild) {- activePlan := Active.Get(planId)+ activePlan := GetActivePlan(planId, branch)filePath := activeBuild.Path- Active.Update(planId, func(active *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.BuildQueuesByPath[filePath] = append(active.BuildQueuesByPath[filePath], activeBuild)})log.Printf("Queued build for file %s\n", filePath)@@ -43,7 +43,7 @@ func QueueBuild(currentOrgId, currentUserId, planId, branch string, activeBuild}func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *types.ActivePlan, activeBuild *types.ActiveBuild) {- Active.Update(activePlan.Id, func(ap *types.ActivePlan) {+ UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {ap.IsBuildingByPath[activeBuild.Path] = true})@@ -87,7 +87,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *types}()go func() {- err := db.SetPlanStatus(planId, shared.PlanStatusBuilding, "")+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusBuilding, "")if err != nil {errCh <- fmt.Errorf("error setting plan status to building: %v", err)return@@ -113,7 +113,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *typeserr := <-errChif err != nil {log.Printf("Error building plan %s: %v\n", planId, err)- Active.Update(activePlan.Id, func(ap *types.ActivePlan) {+ UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {ap.IsBuildingByPath[activeBuild.Path] = false})activePlan.StreamDoneCh <- &shared.ApiError{@@ -128,7 +128,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *typesonFinishBuild := func() {log.Println("Build finished")- if Active.Get(planId).RepliesFinished {+ if GetActivePlan(planId, branch).RepliesFinished {activePlan.Stream(shared.StreamMessage{Type: shared.StreamMessageFinished,})@@ -179,7 +179,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *typesactiveBuild.Success = true- Active.Update(planId, func(ap *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {ap.BuiltFiles[filePath] = trueap.IsBuildingByPath[filePath] = false@@ -232,7 +232,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *typeslog.Printf("Error setting build error: %v\n", err)}- Active.Update(activePlan.Id, func(ap *types.ActivePlan) {+ UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {ap.IsBuildingByPath[activeBuild.Path] = false})}@@ -455,7 +455,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *typeslog.Printf("File %s: Stream finished with non-function call\n", filePath)log.Println("finish reason: " + choice.FinishReason)- active := Active.Get(planId)+ active := GetActivePlan(planId, branch)if !active.BuiltFiles[filePath] {log.Printf("Stream finished before replacements parsed. File: %s\n", filePath)log.Println("Buffer:")diff --git a/app/server/model/plan/describe.go b/app/server/model/plan/describe.goindex 30665142..b8e3eb49 100644--- a/app/server/model/plan/describe.go+++ b/app/server/model/plan/describe.go@@ -12,7 +12,7 @@ import ("github.com/sashabaranov/go-openai")-func genPlanDescription(planId string, ctx context.Context) (*db.ConvoMessageDescription, error) {+func genPlanDescription(planId, branch string, ctx context.Context) (*db.ConvoMessageDescription, error) {descResp, err := model.Client.CreateChatCompletion(ctx,@@ -26,7 +26,7 @@ func genPlanDescription(planId string, ctx context.Context) (*db.ConvoMessageDes},{Role: openai.ChatMessageRoleAssistant,- Content: Active.Get(planId).CurrentReplyContent,+ Content: GetActivePlan(planId, branch).CurrentReplyContent,},},ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.goindex 9f08a2ef..0e66bcbd 100644--- a/app/server/model/plan/state.go+++ b/app/server/model/plan/state.go@@ -4,43 +4,55 @@ import ("log""plandex-server/db""plandex-server/types"+ "strings""github.com/plandex/plandex/shared")var (- Active types.SafeMap[*types.ActivePlan] = *types.NewSafeMap[*types.ActivePlan]()+ activePlans types.SafeMap[*types.ActivePlan] = *types.NewSafeMap[*types.ActivePlan]())-func CreateActivePlan(planId, prompt string) *types.ActivePlan {- activePlan := types.NewActivePlan(planId, prompt)- Active.Set(planId, activePlan)+func GetActivePlan(planId, branch string) *types.ActivePlan {+ return activePlans.Get(strings.Join([]string{planId, branch}, "|"))+}++func CreateActivePlan(planId, branch, prompt string) *types.ActivePlan {+ activePlan := types.NewActivePlan(planId, branch, prompt)+ key := strings.Join([]string{planId, branch}, "|")++ activePlans.Set(key, activePlan)go func() {for {select {case <-activePlan.Ctx.Done():return- case err := <-activePlan.StreamDoneCh:- if err == nil {+ case apiErr := <-activePlan.StreamDoneCh:+ if apiErr == nil {log.Printf("Plan %s stream completed successfully", planId)- err := db.SetPlanStatus(planId, shared.PlanStatusFinished, "")+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusFinished, "")if err != nil {log.Printf("Error setting plan %s status to ready: %v\n", planId, err)}} else {- log.Printf("Error streaming plan %s: %v\n", planId, err)+ log.Printf("Error streaming plan %s: %v\n", planId, apiErr)- err := db.SetPlanStatus(planId, shared.PlanStatusError, err.Msg)+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusError, apiErr.Msg)if err != nil {log.Printf("Error setting plan %s status to error: %v\n", planId, err)}}+ err := db.SetModelStreamFinished(activePlan.ModelStreamId)+ if err != nil {+ log.Printf("Error setting model stream %s finished: %v\n", activePlan.ModelStreamId, err)+ }+activePlan.CancelFn()- Active.Delete(planId)+ DeleteActivePlan(planId, branch)return}}@@ -49,23 +61,35 @@ func CreateActivePlan(planId, prompt string) *types.ActivePlan {return activePlan}-func SubscribePlan(planId string) (string, chan string) {+func DeleteActivePlan(planId, branch string) {+ activePlans.Delete(strings.Join([]string{planId, branch}, "|"))+}++func UpdateActivePlan(planId, branch string, fn func(*types.ActivePlan)) {+ activePlans.Update(strings.Join([]string{planId, branch}, "|"), fn)+}++func SubscribePlan(planId, branch string) (string, chan string) {var id stringvar ch chan string- Active.Update(planId, func(activePlan *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(activePlan *types.ActivePlan) {id, ch = activePlan.Subscribe()})return id, ch}-func UnsubscribePlan(planId, subscriptionId string) {- active := Active.Get(planId)+func UnsubscribePlan(planId, branch, subscriptionId string) {+ active := GetActivePlan(planId, branch)if active == nil {return}- Active.Update(planId, func(activePlan *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(activePlan *types.ActivePlan) {activePlan.Unsubscribe(subscriptionId)})}++func NumActivePlans() int {+ return activePlans.Len()+}diff --git a/app/server/model/plan/stop.go b/app/server/model/plan/stop.goindex 6dcffcd7..14a5dd72 100644--- a/app/server/model/plan/stop.go+++ b/app/server/model/plan/stop.go@@ -8,7 +8,7 @@ import ()func Stop(planId, branch, currentUserId, currentOrgId string) error {- active := Active.Get(planId)+ active := GetActivePlan(planId, branch)if active == nil {return fmt.Errorf("no active plan with id %s", planId)diff --git a/app/server/model/plan/tell.go b/app/server/model/plan/tell.goindex 8a2536b3..675c9e1e 100644--- a/app/server/model/plan/tell.go+++ b/app/server/model/plan/tell.go@@ -31,14 +31,12 @@ func Tell(plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.Tell// return nil// }- planId := plan.Id-- active := Active.Get(planId)+ active := GetActivePlan(plan.Id, branch)if active != nil {- return fmt.Errorf("plan %s already has an active stream", planId)+ return fmt.Errorf("plan %s branch %s already has an active stream on this host", plan.Id, branch)}- active = CreateActivePlan(planId, req.Prompt)+ active = CreateActivePlan(plan.Id, branch, req.Prompt)go execTellPlan(plan, branch, auth, req, active, 0)@@ -66,7 +64,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha}planId := plan.Id- err := db.SetPlanStatus(planId, shared.PlanStatusReplying, "")+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusReplying, "")if err != nil {log.Printf("Error setting plan %s status to replying: %v\n", planId, err)active.StreamDoneCh <- &shared.ApiError{@@ -226,7 +224,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha}if iteration == 0 {- Active.Update(planId, func(ap *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {ap.Contexts = modelContextap.PromptMessageNum = len(convo) + 1@@ -458,11 +456,13 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shareturn}- err = db.StoreModelStream(&db.ModelStream{+ modelStream := &db.ModelStream{OrgId: currentOrgId,PlanId: planId,InternalIp: host.Ip,- })+ Branch: branch,+ }+ err = db.StoreModelStream(modelStream)if err != nil {log.Printf("Error storing model stream: %v\n", err)@@ -474,6 +474,8 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shareturn}+ active.ModelStreamId = modelStream.Id+storeAssistantReply := func() (*db.ConvoMessage, []string, string, error) {num := len(convo) + 1if iteration == 0 {@@ -488,7 +490,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shaRole: openai.ChatMessageRoleAssistant,Tokens: replyNumTokens,Num: num,- Message: Active.Get(planId).CurrentReplyContent,+ Message: GetActivePlan(planId, branch).CurrentReplyContent,}commitMsg, err := db.StoreConvoMessage(&assistantMsg, auth.User.Id, branch, false)@@ -598,7 +600,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shaType: shared.StreamMessageDescribing,})- err := db.SetPlanStatus(planId, shared.PlanStatusDescribing, "")+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusDescribing, "")if err != nil {onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "", "")return@@ -610,6 +612,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha// summarize in the backgroundgo summarizeConvo(summarizeConvoParams{planId: planId,+ branch: branch,convo: convo,summaries: summaries,promptMessage: promptMessage,@@ -654,7 +657,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shaMadePlan: false,}} else {- description, err = genPlanDescription(planId, active.Ctx)+ description, err = genPlanDescription(planId, branch, active.Ctx)if err != nil {onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)return@@ -711,12 +714,12 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha}if !req.AutoContinue || execStatus.Finished || execStatus.NeedsInput {- if Active.Get(planId).BuildFinished() {+ if GetActivePlan(planId, branch).BuildFinished() {active.Stream(shared.StreamMessage{Type: shared.StreamMessageFinished,})} else {- Active.Update(planId, func(active *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.RepliesFinished = true})}@@ -731,7 +734,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shadelta := choice.Deltacontent := delta.Content- Active.Update(planId, func(active *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.CurrentReplyContent += contentactive.NumTokens++})@@ -751,12 +754,12 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shaif len(files) > len(replyFiles) {latestFile := files[len(files)-1]- Active.Update(planId, func(active *types.ActivePlan) {+ UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {active.Files = files})log.Printf("Queuing build for %s\n", latestFile)- QueueBuild(currentOrgId, currentUserId, branch, planId, &types.ActiveBuild{+ QueueBuild(currentOrgId, currentUserId, planId, branch, &types.ActiveBuild{AssistantMessageId: replyId,ReplyContent: content,Path: latestFile,@@ -772,6 +775,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shatype summarizeConvoParams struct {planId string+ branch stringconvo []*db.ConvoMessagesummaries []*db.ConvoSummarypromptMessage *openai.ChatCompletionMessage@@ -780,6 +784,7 @@ type summarizeConvoParams struct {func summarizeConvo(params summarizeConvoParams) error {planId := params.planId+ branch := params.branchconvo := params.convosummaries := params.summariespromptMessage := params.promptMessage@@ -838,7 +843,7 @@ func summarizeConvo(params summarizeConvoParams) error {if promptMessage != nil {summaryMessages = append(summaryMessages, promptMessage, &openai.ChatCompletionMessage{Role: openai.ChatMessageRoleAssistant,- Content: Active.Get(planId).CurrentReplyContent,+ Content: GetActivePlan(planId, branch).CurrentReplyContent,})}diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.goindex bce99f01..f4e38633 100644--- a/app/server/types/active_plan.go+++ b/app/server/types/active_plan.go@@ -22,6 +22,7 @@ type ActiveBuild struct {type ActivePlan struct {Id string+ Branch stringPrompt stringCtx context.ContextCancelFn context.CancelFunc@@ -36,16 +37,17 @@ type ActivePlan struct {BuildQueuesByPath map[string][]*ActiveBuildRepliesFinished boolStreamDoneCh chan *shared.ApiError-- streamCh chan string- subscriptions map[string]chan string+ ModelStreamId string+ streamCh chan string+ subscriptions map[string]chan string}-func NewActivePlan(planId, prompt string) *ActivePlan {+func NewActivePlan(planId, branch, prompt string) *ActivePlan {ctx, cancel := context.WithCancel(context.Background())active := ActivePlan{Id: planId,+ Branch: branch,Prompt: prompt,Ctx: ctx,CancelFn: cancel,diff --git a/app/shared/models.go b/app/shared/models.goindex a89c3dfd..0a34ea19 100644--- a/app/shared/models.go+++ b/app/shared/models.go@@ -154,6 +154,7 @@ type PlanFileResult struct {ConvoMessageId string `json:"convoMessageId"`PlanBuildId string `json:"planBuildId"`Path string `json:"path"`+ ContextBody string `json:"contextBody"`ContextSha string `json:"contextSha"`Content string `json:"content"`AnyFailed bool `json:"anyFailed"`diff --git a/infra/package-lock.json b/infra/package-lock.jsonindex 3e77746b..eaea7201 100644--- a/infra/package-lock.json+++ b/infra/package-lock.json@@ -8,6 +8,7 @@"name": "aws-infra","version": "1.0.0","dependencies": {+ "@aws-cdk/aws-certificatemanager": "^1.204.0","@aws-cdk/aws-ec2": "^1.203.0","@aws-cdk/aws-ecr": "^1.203.0","@aws-cdk/aws-ecs": "^1.203.0",diff --git a/infra/package.json b/infra/package.jsonindex 2dded510..7b9c5781 100644--- a/infra/package.json+++ b/infra/package.json@@ -3,6 +3,7 @@"version": "1.0.0","private": true,"dependencies": {+ "@aws-cdk/aws-certificatemanager": "^1.204.0","@aws-cdk/aws-ec2": "^1.203.0","@aws-cdk/aws-ecr": "^1.203.0","@aws-cdk/aws-ecs": "^1.203.0",diff --git a/infra/src/main.ts b/infra/src/main.tsindex 03be67f3..77a79865 100644--- a/infra/src/main.ts+++ b/infra/src/main.ts@@ -5,8 +5,9 @@ import * as secretsmanager from "@aws-cdk/aws-secretsmanager";import * as ecs from "@aws-cdk/aws-ecs";import * as ecr from "@aws-cdk/aws-ecr";import * as efs from "@aws-cdk/aws-efs";-import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';+import * as elbv2 from "@aws-cdk/aws-elasticloadbalancingv2";import * as iam from "@aws-cdk/aws-iam";+import * as acm from "@aws-cdk/aws-certificatemanager";import { v4 as uuid } from "uuid";const tag = uuid().split("-")[0];@@ -68,7 +69,10 @@ export class PlandexStack extends cdk.Stack {// Create an ECR repositoryconst ecrRepository = new ecr.Repository(this,- `plandex-ecr-repository-${tag}`+ `plandex-ecr-repository-${tag}`,+ {+ repositoryName: "plandex-ecr-repository",+ });// Create an ECS cluster@@ -144,9 +148,7 @@ export class PlandexStack extends cdk.Stack {"Allow Fargate service to access RDS instance");- import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';--const fargateService = new ecs.FargateService(+ const fargateService = new ecs.FargateService(this,`plandex-fargate-service-${tag}`,{@@ -164,51 +166,38 @@ const fargateService = new ecs.FargateService(});// Create a listener for the ALB- const certificate = acm.Certificate.fromCertificateArn(this, 'Certificate', 'arn:aws:acm:region:account-id:certificate/certificate-id');+ const certificate = acm.Certificate.fromCertificateArn(+ this,+ "Certificate",+ "arn:aws:acm:region:account-id:certificate/certificate-id"+ );- const listener = alb.addListener('plandexListener', {+ const listener = alb.addListener("plandexListener", {port: 443,- certificates: [certificate]+ certificates: [certificate],});// Add a target group for the ECS service- const targetGroup = listener.addTargets('plandexEcsTarget', {+ const targetGroup = listener.addTargets("plandexEcsTarget", {port: 80,targets: [fargateService],});// Adjust the security group for the ALB to allow inbound traffic on port 80- listener.addRedirectResponse('HTTPtoHTTPSRedirect', {- statusCode: 'HTTP_301',- protocol: 'HTTPS',- port: '443',- host: '#{host}',- path: '/#{path}',- query: '#{query}'+ listener.addRedirectResponse("HTTPtoHTTPSRedirect", {+ statusCode: "HTTP_301",+ protocol: "HTTPS",+ port: "443",+ host: "#{host}",+ path: "/#{path}",+ query: "#{query}",});- alb.connections.allowFromAnyIpv4(ec2.Port.tcp(443), 'Allow inbound HTTPS traffic');+ alb.connections.allowFromAnyIpv4(+ ec2.Port.tcp(443),+ "Allow inbound HTTPS traffic"+ );}}- // Create an Application Load Balancer in the VPC- const alb = new elbv2.ApplicationLoadBalancer(this, `plandex-alb-${tag}`, {- vpc,- internetFacing: true,- });-- // Create a listener for the ALB- const listener = alb.addListener('plandexListener', {- port: 80,- });-- // Add a target group for the ECS service- const targetGroup = listener.addTargets('plandexEcsTarget', {- port: 80,- targets: [fargateService],- });-- // Adjust the security group for the ALB to allow inbound traffic on port 80- alb.connections.allowFromAnyIpv4(ec2.Port.tcp(80), 'Allow inbound HTTP traffic');-const app = new cdk.App();new PlandexStack(app, "plandex-stack-" + tag);