Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

build improvements using line num references, back to building one file block at a time, and auto-update context on apply

Dane Schneiderf54ad40416a4
diff --git a/app/cli/api/methods.go b/app/cli/api/methods.go
index 79048575..081646c7 100644
--- a/app/cli/api/methods.go
+++ b/app/cli/api/methods.go
@@ -1564,6 +1564,8 @@ func (a *Api) UpdateSettings(planId, branch string, req shared.UpdateSettingsReq
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)}
diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.go
index 25c232ed..5fe1f912 100644
--- a/app/cli/lib/context_update.go
+++ b/app/cli/lib/context_update.go
@@ -128,7 +128,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedRe
contexts, err := api.Client.ListContext(CurrentPlanId, CurrentBranch)
if err != nil {
- return nil, fmt.Errorf("error retrieving context: %w", err)
+ return nil, fmt.Errorf("error retrieving context: %v", err)
}
var errs []error
@@ -267,7 +267,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedRe
} else if doUpdate {
res, err := api.Client.UpdateContext(CurrentPlanId, CurrentBranch, req)
if err != nil {
- return nil, fmt.Errorf("failed to update context: %w", err)
+ return nil, fmt.Errorf("failed to update context: %v", err)
}
msg = res.Msg
}
diff --git a/app/server/db/build_helpers.go b/app/server/db/build_helpers.go
index 25990698..00aa604e 100644
--- a/app/server/db/build_helpers.go
+++ b/app/server/db/build_helpers.go
@@ -3,22 +3,17 @@ package db
import (
"fmt"
"time"
-
- "github.com/lib/pq"
)
func StorePlanBuild(build *PlanBuild) error {
- if build.ConvoMessageIds == nil || len(build.ConvoMessageIds) == 0 {
- return fmt.Errorf("error storing plan build: ConvoMessageIds cannot be nil or empty")
- }
- query := `INSERT INTO plan_builds (org_id, plan_id, convo_message_ids, file_path) VALUES (:org_id, :plan_id, :convo_message_ids, :file_path) RETURNING id, created_at, updated_at`
+ query := `INSERT INTO plan_builds (org_id, plan_id, convo_message_id, file_path) VALUES (:org_id, :plan_id, :convo_message_id, :file_path) RETURNING id, created_at, updated_at`
args := map[string]interface{}{
- "org_id": build.OrgId,
- "plan_id": build.PlanId,
- "convo_message_ids": pq.Array(build.ConvoMessageIds),
- "file_path": build.FilePath,
+ "org_id": build.OrgId,
+ "plan_id": build.PlanId,
+ "convo_message_id": build.ConvoMessageId,
+ "file_path": build.FilePath,
}
row, err := Conn.NamedQuery(query, args)
diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.go
index 18e3a104..a8732e7f 100644
--- a/app/server/db/context_helpers.go
+++ b/app/server/db/context_helpers.go
@@ -1,15 +1,19 @@
package db
import (
+ "crypto/sha256"
+ "encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
+ "sync"
"time"
"github.com/google/uuid"
+ "github.com/plandex/plandex/shared"
)
func GetPlanContexts(orgId, planId string, includeBody bool) ([]*Context, error) {
@@ -163,3 +167,299 @@ func StoreContext(context *Context) error {
return nil
}
+
+type LoadContextsParams struct {
+ Req *shared.LoadContextRequest
+ OrgId string
+ Plan *Plan
+ BranchName string
+ UserId string
+}
+
+func LoadContexts(params LoadContextsParams) (*shared.LoadContextResponse, []*Context, error) {
+ req := params.Req
+ orgId := params.OrgId
+ plan := params.Plan
+ planId := plan.Id
+ branchName := params.BranchName
+ userId := params.UserId
+
+ tokensAdded := 0
+
+ paramsByTempId := make(map[string]*shared.LoadContextParams)
+ numTokensByTempId := make(map[string]int)
+
+ branch, err := GetDbBranch(planId, branchName)
+ if err != nil {
+ return nil, nil, fmt.Errorf("error getting branch: %v", err)
+ }
+ totalTokens := branch.ContextTokens
+
+ settings, err := GetPlanSettings(plan, true)
+ if err != nil {
+ return nil, nil, fmt.Errorf("error getting settings: %v", err)
+ }
+
+ maxTokens := settings.GetPlannerEffectiveMaxTokens()
+
+ for _, context := range *req {
+ tempId := uuid.New().String()
+ numTokens, err := shared.GetNumTokens(context.Body)
+
+ if err != nil {
+ return nil, nil, fmt.Errorf("error getting num tokens: %v", err)
+ }
+
+ paramsByTempId[tempId] = context
+ numTokensByTempId[tempId] = numTokens
+
+ tokensAdded += numTokens
+ totalTokens += numTokens
+ }
+
+ if totalTokens > maxTokens {
+ return &shared.LoadContextResponse{
+ TokensAdded: tokensAdded,
+ TotalTokens: totalTokens,
+ MaxTokens: maxTokens,
+ MaxTokensExceeded: true,
+ }, nil, nil
+ }
+
+ dbContextsCh := make(chan *Context)
+ errCh := make(chan error)
+ for tempId, params := range paramsByTempId {
+
+ go func(tempId string, params *shared.LoadContextParams) {
+ hash := sha256.Sum256([]byte(params.Body))
+ sha := hex.EncodeToString(hash[:])
+
+ context := Context{
+ // Id generated by db layer
+ OrgId: orgId,
+ OwnerId: userId,
+ PlanId: planId,
+ ContextType: params.ContextType,
+ Name: params.Name,
+ Url: params.Url,
+ FilePath: params.FilePath,
+ NumTokens: numTokensByTempId[tempId],
+ Sha: sha,
+ Body: params.Body,
+ }
+
+ err := StoreContext(&context)
+
+ if err != nil {
+ errCh <- err
+ return
+ }
+
+ dbContextsCh <- &context
+
+ }(tempId, params)
+ }
+
+ var dbContexts []*Context
+ var apiContexts []*shared.Context
+
+ for i := 0; i < len(*req); i++ {
+ select {
+ case err := <-errCh:
+ return nil, nil, fmt.Errorf("error storing context: %v", err)
+ case dbContext := <-dbContextsCh:
+ dbContexts = append(dbContexts, dbContext)
+ apiContext := dbContext.ToApi()
+ apiContext.Body = ""
+ apiContexts = append(apiContexts, apiContext)
+ }
+ }
+
+ err = AddPlanContextTokens(planId, branchName, tokensAdded)
+ if err != nil {
+ return nil, nil, fmt.Errorf("error adding plan context tokens: %v", err)
+ }
+
+ commitMsg := shared.SummaryForLoadContext(apiContexts, tokensAdded, totalTokens)
+
+ if len(apiContexts) > 1 {
+ commitMsg += "\n\n" + shared.TableForLoadContext(apiContexts)
+ }
+
+ return &shared.LoadContextResponse{
+ TokensAdded: tokensAdded,
+ TotalTokens: totalTokens,
+ Msg: commitMsg,
+ }, dbContexts, nil
+}
+
+type UpdateContextsParams struct {
+ Req *shared.UpdateContextRequest
+ OrgId string
+ Plan *Plan
+ BranchName string
+ ContextsById map[string]*Context
+}
+
+func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse, error) {
+ req := params.Req
+ orgId := params.OrgId
+ plan := params.Plan
+ planId := plan.Id
+ branchName := params.BranchName
+
+ branch, err := GetDbBranch(planId, branchName)
+ if err != nil {
+ return nil, fmt.Errorf("error getting branch: %v", err)
+ }
+
+ settings, err := GetPlanSettings(plan, true)
+ if err != nil {
+ return nil, fmt.Errorf("error getting settings: %v", err)
+ }
+
+ maxTokens := settings.GetPlannerEffectiveMaxTokens()
+ totalTokens := branch.ContextTokens
+
+ tokensDiff := 0
+ tokenDiffsById := make(map[string]int)
+
+ var contextsById map[string]*Context
+ if params.ContextsById == nil {
+ contextsById = make(map[string]*Context)
+ } else {
+ contextsById = params.ContextsById
+ }
+
+ var updatedContexts []*shared.Context
+
+ numFiles := 0
+ numUrls := 0
+ numTrees := 0
+
+ var mu sync.Mutex
+ errCh := make(chan error)
+
+ for id, params := range *req {
+ go func(id string, params *shared.UpdateContextParams) {
+
+ var context *Context
+ if _, ok := contextsById[id]; ok {
+ context = contextsById[id]
+ } else {
+ var err error
+ context, err = GetContext(orgId, planId, id, true)
+
+ if err != nil {
+ errCh <- fmt.Errorf("error getting context: %v", err)
+ return
+ }
+ }
+
+ mu.Lock()
+ defer mu.Unlock()
+
+ contextsById[id] = context
+ updatedContexts = append(updatedContexts, context.ToApi())
+ updateNumTokens, err := shared.GetNumTokens(params.Body)
+
+ if err != nil {
+ errCh <- fmt.Errorf("error getting num tokens: %v", err)
+ return
+ }
+
+ tokenDiff := updateNumTokens - context.NumTokens
+ tokenDiffsById[id] = tokenDiff
+ tokensDiff += tokenDiff
+ totalTokens += tokenDiff
+
+ context.NumTokens = updateNumTokens
+
+ switch context.ContextType {
+ case shared.ContextFileType:
+ numFiles++
+ case shared.ContextURLType:
+ numUrls++
+ case shared.ContextDirectoryTreeType:
+ numTrees++
+ }
+
+ errCh <- nil
+ }(id, params)
+ }
+
+ updateRes := &shared.ContextUpdateResult{
+ UpdatedContexts: updatedContexts,
+ TokenDiffsById: tokenDiffsById,
+ TokensDiff: tokensDiff,
+ TotalTokens: totalTokens,
+ NumFiles: numFiles,
+ NumUrls: numUrls,
+ NumTrees: numTrees,
+ MaxTokens: maxTokens,
+ }
+
+ if totalTokens > maxTokens {
+ return &shared.UpdateContextResponse{
+ TokensAdded: tokensDiff,
+ TotalTokens: totalTokens,
+ MaxTokens: maxTokens,
+ MaxTokensExceeded: true,
+ }, nil
+ }
+
+ for i := 0; i < len(*req); i++ {
+ err := <-errCh
+ if err != nil {
+ return nil, fmt.Errorf("error getting context: %v", err)
+ }
+ }
+
+ errCh = make(chan error)
+ dbContextsCh := make(chan *Context)
+
+ for id, params := range *req {
+ go func(id string, params *shared.UpdateContextParams) {
+
+ context := contextsById[id]
+
+ hash := sha256.Sum256([]byte(params.Body))
+ sha := hex.EncodeToString(hash[:])
+
+ context.Body = params.Body
+ context.Sha = sha
+
+ err := StoreContext(context)
+
+ if err != nil {
+ errCh <- fmt.Errorf("error storing context: %v", err)
+ return
+ }
+
+ dbContextsCh <- context
+ }(id, params)
+ }
+
+ var dbContexts []*Context
+ for i := 0; i < len(*req); i++ {
+ select {
+ case err := <-errCh:
+ return nil, fmt.Errorf("error storing context: %v", err)
+ case dbContext := <-dbContextsCh:
+ dbContexts = append(dbContexts, dbContext)
+ }
+ }
+
+ err = AddPlanContextTokens(planId, branchName, tokensDiff)
+ if err != nil {
+ return nil, fmt.Errorf("error adding plan context tokens: %v", err)
+ }
+
+ commitMsg := shared.SummaryForUpdateContext(updateRes) + "\n\n" + shared.TableForContextUpdate(updateRes)
+
+ return &shared.LoadContextResponse{
+ TokensAdded: tokensDiff,
+ TotalTokens: totalTokens,
+ Msg: commitMsg,
+ }, nil
+}
diff --git a/app/server/db/data_models.go b/app/server/db/data_models.go
index 4b5b2848..40ca14a4 100644
--- a/app/server/db/data_models.go
+++ b/app/server/db/data_models.go
@@ -211,24 +211,24 @@ func (summary *ConvoSummary) ToApi() *shared.ConvoSummary {
}
type PlanBuild struct {
- Id string `db:"id"`
- OrgId string `db:"org_id"`
- PlanId string `db:"plan_id"`
- ConvoMessageIds []string `db:"convo_message_ids"`
- FilePath string `db:"file_path"`
- Error string `db:"error"`
- CreatedAt time.Time `db:"created_at"`
- UpdatedAt time.Time `db:"updated_at"`
+ Id string `db:"id"`
+ OrgId string `db:"org_id"`
+ PlanId string `db:"plan_id"`
+ ConvoMessageId string `db:"convo_message_id"`
+ FilePath string `db:"file_path"`
+ Error string `db:"error"`
+ CreatedAt time.Time `db:"created_at"`
+ UpdatedAt time.Time `db:"updated_at"`
}
func (build *PlanBuild) ToApi() *shared.PlanBuild {
return &shared.PlanBuild{
- Id: build.Id,
- ConvoMessageIds: build.ConvoMessageIds,
- Error: build.Error,
- FilePath: build.FilePath,
- CreatedAt: build.CreatedAt,
- UpdatedAt: build.UpdatedAt,
+ Id: build.Id,
+ ConvoMessageId: build.ConvoMessageId,
+ Error: build.Error,
+ FilePath: build.FilePath,
+ CreatedAt: build.CreatedAt,
+ UpdatedAt: build.UpdatedAt,
}
}
@@ -385,38 +385,38 @@ func (desc *ConvoMessageDescription) ToApi() *shared.ConvoMessageDescription {
}
type PlanFileResult struct {
- Id string `json:"id"`
- OrgId string `json:"orgId"`
- PlanId string `json:"planId"`
- ConvoMessageIds []string `json:"convoMessageIds"`
- PlanBuildId string `json:"planBuildId"`
- Path string `json:"path"`
- ContextSha string `json:"contextSha"`
- ContextBody string `json:"contextBody"`
- 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"`
+ OrgId string `json:"orgId"`
+ PlanId string `json:"planId"`
+ ConvoMessageId string `json:"convoMessageId"`
+ PlanBuildId string `json:"planBuildId"`
+ Path string `json:"path"`
+ ContextSha string `json:"contextSha"`
+ ContextBody string `json:"contextBody"`
+ 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,
- ConvoMessageIds: res.ConvoMessageIds,
- Path: res.Path,
- ContextSha: res.ContextSha,
- ContextBody: res.ContextBody,
- Content: res.Content,
- AnyFailed: res.AnyFailed,
- AppliedAt: res.AppliedAt,
- RejectedAt: res.RejectedAt,
- Replacements: res.Replacements,
- CreatedAt: res.CreatedAt,
- UpdatedAt: res.UpdatedAt,
+ Id: res.Id,
+ PlanBuildId: res.PlanBuildId,
+ ConvoMessageId: res.ConvoMessageId,
+ Path: res.Path,
+ ContextSha: res.ContextSha,
+ ContextBody: res.ContextBody,
+ 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/result_helpers.go b/app/server/db/result_helpers.go
index 81446b37..490a8345 100644
--- a/app/server/db/result_helpers.go
+++ b/app/server/db/result_helpers.go
@@ -275,13 +275,17 @@ func GetPlanResult(planFileResults []*shared.PlanFileResult) *shared.PlanResult
}
}
-func ApplyPlan(orgId, planId, branch string) error {
+func ApplyPlan(orgId, userId, branchName string, plan *Plan) error {
+ planId := plan.Id
+
resultsDir := getPlanResultsDir(orgId, planId)
errCh := make(chan error)
var results []*PlanFileResult
var pendingBuildDescriptions []*ConvoMessageDescription
+ contextsById := make(map[string]*Context)
+ contextsByPath := make(map[string]*Context)
go func() {
res, err := GetPlanFileResults(orgId, planId)
@@ -303,7 +307,24 @@ func ApplyPlan(orgId, planId, branch string) error {
errCh <- nil
}()
- for i := 0; i < 2; i++ {
+ go func() {
+ res, err := GetPlanContexts(orgId, planId, false)
+ if err != nil {
+ errCh <- fmt.Errorf("error getting contexts: %v", err)
+ return
+ }
+
+ for _, context := range res {
+ contextsById[context.Id] = context
+ if context.FilePath != "" {
+ contextsByPath[context.FilePath] = context
+ }
+ }
+
+ errCh <- nil
+ }()
+
+ for i := 0; i < 3; i++ {
err := <-errCh
if err != nil {
return fmt.Errorf("error applying plan: %v", err)
@@ -319,7 +340,36 @@ func ApplyPlan(orgId, planId, branch string) error {
}
}
- errCh = make(chan error, len(pendingDbResults)+len(pendingBuildDescriptions))
+ pendingNewFilesSet := make(map[string]bool)
+ pendingUpdatedFilesSet := make(map[string]bool)
+ for _, result := range pendingDbResults {
+ if len(result.Replacements) == 0 && result.Content != "" {
+ pendingNewFilesSet[result.Path] = true
+ } else if !pendingNewFilesSet[result.Path] {
+ pendingUpdatedFilesSet[result.Path] = true
+ }
+ }
+
+ var loadContextRes *shared.LoadContextResponse
+ var updateContextRes *shared.UpdateContextResponse
+
+ var currentPlanState *shared.CurrentPlanState
+ if len(pendingNewFilesSet) > 0 || len(pendingUpdatedFilesSet) > 0 {
+ res, err := GetCurrentPlanState(CurrentPlanStateParams{
+ OrgId: orgId,
+ PlanId: plan.Id,
+ PlanFileResults: results,
+ PendingBuildDescriptions: pendingBuildDescriptions,
+ })
+
+ if err != nil {
+ return fmt.Errorf("error getting current plan state: %v", err)
+ }
+
+ currentPlanState = res
+ }
+
+ errCh = make(chan error)
now := time.Now()
for _, result := range pendingDbResults {
@@ -360,16 +410,96 @@ func ApplyPlan(orgId, planId, branch string) error {
}(description)
}
- for i := 0; i < len(pendingDbResults)+len(pendingBuildDescriptions); i++ {
+ if len(pendingNewFilesSet) > 0 {
+ go func() {
+ loadReq := shared.LoadContextRequest{}
+ for path := range pendingNewFilesSet {
+ loadReq = append(loadReq, &shared.LoadContextParams{
+ ContextType: shared.ContextFileType,
+ Name: path,
+ FilePath: path,
+ Body: currentPlanState.CurrentPlanFiles.Files[path],
+ })
+ }
+
+ res, _, err := LoadContexts(
+ LoadContextsParams{
+ OrgId: orgId,
+ UserId: userId,
+ Plan: plan,
+ BranchName: branchName,
+ Req: &loadReq,
+ },
+ )
+
+ if err != nil {
+ errCh <- fmt.Errorf("error loading context: %v", err)
+ return
+ }
+
+ loadContextRes = res
+ errCh <- nil
+ }()
+ }
+
+ if len(pendingUpdatedFilesSet) > 0 {
+ go func() {
+ updateReq := shared.UpdateContextRequest{}
+ for path := range pendingUpdatedFilesSet {
+ context := contextsByPath[path]
+ updateReq[context.Id] = &shared.UpdateContextParams{
+ Body: currentPlanState.CurrentPlanFiles.Files[path],
+ }
+ }
+
+ res, err := UpdateContexts(
+ UpdateContextsParams{
+ OrgId: orgId,
+ Plan: plan,
+ BranchName: branchName,
+ Req: &updateReq,
+ },
+ )
+
+ if err != nil {
+ errCh <- fmt.Errorf("error updating context: %v", err)
+ return
+ }
+
+ updateContextRes = res
+ errCh <- nil
+
+ }()
+
+ }
+
+ numRoutines := len(pendingDbResults) +
+ len(pendingBuildDescriptions)
+ if len(pendingNewFilesSet) > 0 {
+ numRoutines++
+ }
+ if len(pendingUpdatedFilesSet) > 0 {
+ numRoutines++
+ }
+
+ for i := 0; i < numRoutines; i++ {
err := <-errCh
if err != nil {
return fmt.Errorf("error applying plan: %v", err)
}
}
- msg := "Marked pending results as applied."
+ msg := "βœ… Marked pending results as applied"
+
+ if loadContextRes != nil && !loadContextRes.MaxTokensExceeded {
+ msg += "\n\n" + loadContextRes.Msg
+ }
+
+ if updateContextRes != nil && !updateContextRes.MaxTokensExceeded {
+ msg += "\n\n" + updateContextRes.Msg
+ }
- err := GitAddAndCommit(orgId, planId, branch, msg)
+ err := GitAddAndCommit(orgId, plan.Id, branchName, msg)
if err != nil {
return fmt.Errorf("error committing plan: %v", err)
diff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.go
index 98129b54..b74dc7b9 100644
--- a/app/server/handlers/context_helper.go
+++ b/app/server/handlers/context_helper.go
@@ -2,68 +2,42 @@ package handlers
import (
"context"
- "crypto/sha256"
- "encoding/hex"
"encoding/json"
"log"
"net/http"
"plandex-server/db"
"plandex-server/types"
- "github.com/google/uuid"
"github.com/plandex/plandex/shared"
)
func loadContexts(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth, loadReq *shared.LoadContextRequest, plan *db.Plan, branchName string) (*shared.LoadContextResponse, []*db.Context) {
- planId := plan.Id
+ var err error
- branch, err := db.GetDbBranch(planId, branchName)
- if err != nil {
- log.Printf("Error getting branch: %v\n", err)
- http.Error(w, "Error getting branch: "+err.Error(), http.StatusInternalServerError)
+ ctx, cancel := context.WithCancel(context.Background())
+ unlockFn := lockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel)
+ if unlockFn == nil {
return nil, nil
+ } else {
+ defer (*unlockFn)(err)
}
- settings, err := db.GetPlanSettings(plan, true)
+ res, dbContexts, err := db.LoadContexts(db.LoadContextsParams{
+ OrgId: auth.OrgId,
+ Plan: plan,
+ BranchName: branchName,
+ Req: loadReq,
+ UserId: auth.User.Id,
+ })
+
if err != nil {
- log.Printf("Error getting settings: %v\n", err)
- http.Error(w, "Error getting settings: "+err.Error(), http.StatusInternalServerError)
+ log.Printf("Error loading contexts: %v\n", err)
+ http.Error(w, "Error loading contexts: "+err.Error(), http.StatusInternalServerError)
return nil, nil
}
- maxTokens := settings.GetPlannerEffectiveMaxTokens()
- tokensAdded := 0
- totalTokens := branch.ContextTokens
-
- paramsByTempId := make(map[string]*shared.LoadContextParams)
- numTokensByTempId := make(map[string]int)
-
- for _, context := range *loadReq {
- tempId := uuid.New().String()
- numTokens, err := shared.GetNumTokens(context.Body)
-
- if err != nil {
- log.Printf("Error getting num tokens: %v\n", err)
- http.Error(w, "Error getting num tokens: "+err.Error(), http.StatusInternalServerError)
- return nil, nil
- }
-
- paramsByTempId[tempId] = context
- numTokensByTempId[tempId] = numTokens
-
- tokensAdded += numTokens
- totalTokens += numTokens
- }
-
- if totalTokens > maxTokens {
- log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- res := shared.LoadContextResponse{
- TokensAdded: tokensAdded,
- TotalTokens: totalTokens,
- MaxTokens: maxTokens,
- MaxTokensExceeded: true,
- }
-
+ if res.MaxTokensExceeded {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", res.TotalTokens, res.MaxTokens)
bytes, err := json.Marshal(res)
if err != nil {
@@ -76,74 +50,7 @@ func loadContexts(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth
return nil, nil
}
- ctx, cancel := context.WithCancel(context.Background())
- unlockFn := lockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel)
- if unlockFn == nil {
- return nil, nil
- } else {
- defer (*unlockFn)(err)
- }
-
- dbContextsCh := make(chan *db.Context)
- errCh := make(chan error)
- for tempId, params := range paramsByTempId {
-
- go func(tempId string, params *shared.LoadContextParams) {
- hash := sha256.Sum256([]byte(params.Body))
- sha := hex.EncodeToString(hash[:])
-
- context := db.Context{
- // Id generated by db layer
- OrgId: auth.OrgId,
- OwnerId: auth.User.Id,
- PlanId: planId,
- ContextType: params.ContextType,
- Name: params.Name,
- Url: params.Url,
- FilePath: params.FilePath,
- NumTokens: numTokensByTempId[tempId],
- Sha: sha,
- Body: params.Body,
- }
-
- err := db.StoreContext(&context)
-
- if err != nil {
- errCh <- err
- return
- }
-
- dbContextsCh <- &context
-
- }(tempId, params)
- }
-
- var dbContexts []*db.Context
- var apiContexts []*shared.Context
-
- for i := 0; i < len(*loadReq); i++ {
- select {
- case err := <-errCh:
- log.Printf("Error creating context: %v\n", err)
- http.Error(w, "Error creating context: "+err.Error(), http.StatusInternalServerError)
- return nil, nil
- case dbContext := <-dbContextsCh:
- dbContexts = append(dbContexts, dbContext)
- apiContext := dbContext.ToApi()
- apiContext.Body = ""
- apiContexts = append(apiContexts, apiContext)
- }
- }
-
- err = db.AddPlanContextTokens(planId, branchName, tokensAdded)
- if err != nil {
- log.Printf("Error updating plan tokens: %v\n", err)
- http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)
- return nil, nil
- }
-
- commitMsg := shared.SummaryForLoadContext(apiContexts, tokensAdded, totalTokens) + "\n\n" + shared.TableForLoadContext(apiContexts)
- err = db.GitAddAndCommit(auth.OrgId, planId, branchName, commitMsg)
+ err = db.GitAddAndCommit(auth.OrgId, plan.Id, branchName, res.Msg)
if err != nil {
log.Printf("Error committing changes: %v\n", err)
@@ -151,9 +58,5 @@ func loadContexts(w http.ResponseWriter, r *http.Request, auth *types.ServerAuth
return nil, nil
}
- return &shared.LoadContextResponse{
- TokensAdded: tokensAdded,
- TotalTokens: totalTokens,
- Msg: commitMsg,
- }, dbContexts
+ return res, dbContexts
}
diff --git a/app/server/handlers/plans_changes.go b/app/server/handlers/plans_changes.go
index 6acb1101..c056ea1d 100644
--- a/app/server/handlers/plans_changes.go
+++ b/app/server/handlers/plans_changes.go
@@ -75,7 +75,8 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {
branch := vars["branch"]
log.Println("planId: ", planId)
- if authorizePlan(w, planId, auth) == nil {
+ plan := authorizePlan(w, planId, auth)
+ if plan == nil {
return
}
@@ -88,7 +89,7 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {
defer (*unlockFn)(err)
}
- err = db.ApplyPlan(auth.OrgId, planId, branch)
+ err = db.ApplyPlan(auth.OrgId, auth.User.Id, branch, plan)
if err != nil {
log.Printf("Error applying plan: %v\n", err)
diff --git a/app/server/handlers/plans_context.go b/app/server/handlers/plans_context.go
index 1f69f09b..684b854b 100644
--- a/app/server/handlers/plans_context.go
+++ b/app/server/handlers/plans_context.go
@@ -2,15 +2,11 @@ package handlers
import (
"context"
- "crypto/sha256"
- "encoding/hex"
"encoding/json"
- "fmt"
"io"
"log"
"net/http"
"plandex-server/db"
- "sync"
"github.com/gorilla/mux"
"github.com/plandex/plandex/shared"
@@ -101,6 +97,7 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {
}
res, _ := loadContexts(w, r, auth, &requestBody, plan, branchName)
+
if res == nil {
return
}
@@ -136,13 +133,6 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {
return
}
- branch, err := db.GetDbBranch(planId, branchName)
- if err != nil {
- log.Printf("Error getting branch: %v\n", err)
- http.Error(w, "Error getting branch: "+err.Error(), http.StatusInternalServerError)
- return
- }
-
// read the request body
body, err := io.ReadAll(r.Body)
if err != nil {
@@ -159,100 +149,6 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {
return
}
- settings, err := db.GetPlanSettings(plan, true)
- if err != nil {
- log.Printf("Error getting settings: %v\n", err)
- http.Error(w, "Error getting settings: "+err.Error(), http.StatusInternalServerError)
- return
- }
-
- maxTokens := settings.GetPlannerEffectiveMaxTokens()
- tokensDiff := 0
- totalTokens := branch.ContextTokens
- tokensDiffById := make(map[string]int)
- contextsById := make(map[string]*db.Context)
- var updatedContexts []*shared.Context
-
- numFiles := 0
- numUrls := 0
- numTrees := 0
-
- var mu sync.Mutex
- errCh := make(chan error)
-
- for id, params := range requestBody {
- go func(id string, params *shared.UpdateContextParams) {
- context, err := db.GetContext(auth.OrgId, planId, id, true)
-
- if err != nil {
- errCh <- fmt.Errorf("error getting context: %v", err)
- return
- }
-
- // spew.Dump(context)
-
- mu.Lock()
- defer mu.Unlock()
-
- contextsById[id] = context
- updatedContexts = append(updatedContexts, context.ToApi())
- updateNumTokens, err := shared.GetNumTokens(params.Body)
-
- if err != nil {
- errCh <- fmt.Errorf("error getting num tokens: %v", err)
- return
- }
-
- tokenDiff := updateNumTokens - context.NumTokens
- tokensDiffById[id] = tokenDiff
- tokensDiff += tokenDiff
- totalTokens += tokenDiff
-
- context.NumTokens = updateNumTokens
-
- switch context.ContextType {
- case shared.ContextFileType:
- numFiles++
- case shared.ContextURLType:
- numUrls++
- case shared.ContextDirectoryTreeType:
- numTrees++
- }
-
- errCh <- nil
- }(id, params)
- }
-
- for i := 0; i < len(requestBody); i++ {
- err := <-errCh
- if err != nil {
- log.Printf("Error updating context: %v\n", err)
- http.Error(w, "Error updating context: "+err.Error(), http.StatusInternalServerError)
- return
- }
- }
-
- if totalTokens > maxTokens {
- log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)
- res := shared.LoadContextResponse{
- TokensAdded: tokensDiff,
- TotalTokens: totalTokens,
- MaxTokens: maxTokens,
- MaxTokensExceeded: true,
- }
-
- bytes, err := json.Marshal(res)
-
- if err != nil {
- log.Printf("Error marshalling response: %v\n", err)
- http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)
- return
- }
-
- w.Write(bytes)
- return
- }
-
ctx, cancel := context.WithCancel(context.Background())
unlockFn := lockRepo(w, r, auth, db.LockScopeWrite, ctx, cancel)
if unlockFn == nil {
@@ -261,44 +157,33 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {
defer (*unlockFn)(err)
}
- errCh = make(chan error)
-
- for id, params := range requestBody {
- go func(id string, params *shared.UpdateContextParams) {
-
- context := contextsById[id]
+ updateRes, err := db.UpdateContexts(db.UpdateContextsParams{
+ Req: &requestBody,
+ OrgId: auth.OrgId,
+ Plan: plan,
+ })
- hash := sha256.Sum256([]byte(params.Body))
- sha := hex.EncodeToString(hash[:])
-
- context.Body = params.Body
- context.Sha = sha
-
- errCh <- db.StoreContext(context)
- }(id, params)
+ if err != nil {
+ log.Printf("Error error updating contexts: %v\n", err)
+ http.Error(w, "Error error updating contexts: "+err.Error(), http.StatusInternalServerError)
+ return
}
- for i := 0; i < len(requestBody); i++ {
- err := <-errCh
+ if updateRes.MaxTokensExceeded {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", updateRes.TotalTokens, updateRes.MaxTokens)
+ bytes, err := json.Marshal(updateRes)
+
if err != nil {
- log.Printf("Error creating context: %v\n", err)
- http.Error(w, "Error creating context: "+err.Error(), http.StatusInternalServerError)
+ log.Printf("Error marshalling response: %v\n", err)
+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)
return
}
- }
- updateRes := &shared.ContextUpdateResult{
- UpdatedContexts: updatedContexts,
- TokenDiffsById: tokensDiffById,
- TokensDiff: tokensDiff,
- TotalTokens: totalTokens,
- NumFiles: numFiles,
- NumUrls: numUrls,
- NumTrees: numTrees,
+ w.Write(bytes)
+ return
}
- commitMsg := shared.SummaryForUpdateContext(updateRes) + "\n\n" + shared.TableForContextUpdate(updateRes)
- err = db.GitAddAndCommit(auth.OrgId, planId, branchName, commitMsg)
+ err = db.GitAddAndCommit(auth.OrgId, planId, branchName, updateRes.Msg)
if err != nil {
log.Printf("Error committing changes: %v\n", err)
@@ -306,20 +191,7 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {
return
}
- err = db.AddPlanContextTokens(planId, branchName, tokensDiff)
- if err != nil {
- log.Printf("Error updating plan tokens: %v\n", err)
- http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)
- return
- }
-
- res := shared.UpdateContextResponse{
- TokensAdded: tokensDiff,
- TotalTokens: totalTokens,
- Msg: commitMsg,
- }
-
- bytes, err := json.Marshal(res)
+ bytes, err := json.Marshal(updateRes)
if err != nil {
log.Printf("Error marshalling response: %v\n", err)
diff --git a/app/server/handlers/plans_crud.go b/app/server/handlers/plans_crud.go
index ee8f6e04..35b091ef 100644
--- a/app/server/handlers/plans_crud.go
+++ b/app/server/handlers/plans_crud.go
@@ -435,8 +435,6 @@ func GetCurrentBranchByPlanIdHandler(w http.ResponseWriter, r *http.Request) {
query += "(" + strings.Join(orConditions, " OR ") + ") AND archived_at IS NULL AND deleted_at IS NULL"
- log.Println("query: ", query)
-
var branches []db.Branch
err = db.Conn.Select(&branches, query, queryArgs...)
diff --git a/app/server/handlers/settings.go b/app/server/handlers/settings.go
index 741cbbaa..5af56969 100644
--- a/app/server/handlers/settings.go
+++ b/app/server/handlers/settings.go
@@ -10,6 +10,7 @@ import (
"reflect"
"strings"
+ "github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
"github.com/plandex/plandex/shared"
)
@@ -42,7 +43,7 @@ func GetSettingsHandler(w http.ResponseWriter, r *http.Request) {
defer (*unlockFn)(err)
}
- settings, err := db.GetPlanSettings(plan, false)
+ settings, err := db.GetPlanSettings(plan, true)
if err != nil {
log.Println("Error getting settings: ", err)
@@ -108,6 +109,12 @@ func UpdateSettingsHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ log.Println("Original settings:")
+ spew.Dump(originalSettings)
+
+ log.Println("req.Settings:")
+ spew.Dump(req.Settings)
+
err = db.StorePlanSettings(plan, req.Settings)
if err != nil {
@@ -170,6 +177,11 @@ func getUpdateCommitMsg(settings *shared.PlanSettings, originalSettings *shared.
func compareAny(a, b interface{}, path string, changes *[]string) {
// log.Println("Comparing", path)
+ // log.Println("a")
+ // spew.Dump(a)
+ // log.Println("b")
+ // spew.Dump(b)
+
if strings.HasSuffix(path, "updated-at") ||
strings.HasSuffix(path, "open-ai-response-format") {
return
@@ -187,9 +199,9 @@ func compareAny(a, b interface{}, path string, changes *[]string) {
bVal = bVal.Elem()
}
- // log.Println("Comparing", path, aVal.Kind(), bVal.Kind())
- // log.Println("aVal", aVal)
- // log.Println("bVal", bVal)
+ log.Println("Comparing", path, aVal.Kind(), bVal.Kind())
+ log.Println("aVal", aVal)
+ log.Println("bVal", bVal)
switch aVal.Kind() {
case reflect.Struct:
@@ -214,6 +226,8 @@ func compareAny(a, b interface{}, path string, changes *[]string) {
}
}
+ log.Println("field", fieldName, "updatedPath", updatedPath)
+
compareAny(aVal.Field(i).Interface(), bVal.Field(i).Interface(),
updatedPath, changes)
}
diff --git a/app/server/migrations/2024022000_revert_plan_build_convo_ids.down.sql b/app/server/migrations/2024022000_revert_plan_build_convo_ids.down.sql
new file mode 100644
index 00000000..c8d3dcb0
--- /dev/null
+++ b/app/server/migrations/2024022000_revert_plan_build_convo_ids.down.sql
@@ -0,0 +1,5 @@
+ALTER TABLE plan_builds
+ RENAME COLUMN convo_message_id TO convo_message_ids;
+
+ALTER TABLE plan_builds
+ ALTER COLUMN convo_message_ids TYPE UUID[] USING ARRAY[convo_message_ids];
diff --git a/app/server/migrations/2024022000_revert_plan_build_convo_ids.up.sql b/app/server/migrations/2024022000_revert_plan_build_convo_ids.up.sql
new file mode 100644
index 00000000..1dfa59ca
--- /dev/null
+++ b/app/server/migrations/2024022000_revert_plan_build_convo_ids.up.sql
@@ -0,0 +1,5 @@
+ALTER TABLE plan_builds
+ RENAME COLUMN convo_message_ids TO convo_message_id;
+
+ALTER TABLE plan_builds
+ ALTER COLUMN convo_message_id TYPE UUID USING (convo_message_id[1]);
\ No newline at end of file
diff --git a/app/server/model/plan/build_exec.go b/app/server/model/plan/build_exec.go
index aab86349..e27c63a0 100644
--- a/app/server/model/plan/build_exec.go
+++ b/app/server/model/plan/build_exec.go
@@ -32,7 +32,7 @@ func Build(client *openai.Client, plan *db.Plan, branch string, auth *types.Serv
return 0, err
}
- pendingBuildsByPath, err := state.loadBuild()
+ pendingBuildsByPath, err := state.loadPendingBuilds()
if err != nil {
return onErr(err)
}
@@ -53,7 +53,7 @@ func Build(client *openai.Client, plan *db.Plan, branch string, auth *types.Serv
log.Printf("Starting %d builds\n", len(pendingBuildsByPath))
for _, pendingBuilds := range pendingBuildsByPath {
- go state.execPlanBuild(pendingBuilds)
+ go state.queueBuilds(pendingBuilds)
}
return len(pendingBuildsByPath), nil
@@ -64,30 +64,37 @@ func (state *activeBuildStreamState) queueBuilds(activeBuilds []*types.ActiveBui
branch := state.branch
activePlan := GetActivePlan(planId, branch)
- filePath := activeBuilds[0].Path
- // log.Printf("Queue:")
- // spew.Dump(activePlan.BuildQueuesByPath[filePath])
+ queueBuild := func(activeBuild *types.ActiveBuild) {
+ filePath := activeBuild.Path
- UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {
- active.BuildQueuesByPath[filePath] = append(active.BuildQueuesByPath[filePath], activeBuilds...)
- })
- log.Printf("Queued %d build(s) for file %s\n", len(activeBuilds), filePath)
+ // log.Printf("Queue:")
+ // spew.Dump(activePlan.BuildQueuesByPath[filePath])
- if activePlan.IsBuildingByPath[filePath] {
- log.Printf("Already building file %s\n", filePath)
- return
- } else {
- log.Printf("Not building file %s, will execute now\n", filePath)
- go state.execPlanBuild(activeBuilds)
+ UpdateActivePlan(planId, branch, func(active *types.ActivePlan) {
+ active.BuildQueuesByPath[filePath] = append(active.BuildQueuesByPath[filePath], activeBuilds...)
+ })
+ log.Printf("Queued %d build(s) for file %s\n", len(activeBuilds), filePath)
+
+ if activePlan.IsBuildingByPath[filePath] {
+ log.Printf("Already building file %s\n", filePath)
+ return
+ } else {
+ log.Printf("Not building file %s, will execute now\n", filePath)
+ go state.execPlanBuild(activeBuild)
+ }
+ }
+
+ for _, activeBuild := range activeBuilds {
+ queueBuild(activeBuild)
}
}
-func (buildState *activeBuildStreamState) execPlanBuild(activeBuilds []*types.ActiveBuild) {
- log.Printf("execPlanBuild for %d active builds\n", len(activeBuilds))
+func (buildState *activeBuildStreamState) execPlanBuild(activeBuild *types.ActiveBuild) {
+ log.Println("execPlanBuild")
- if len(activeBuilds) == 0 {
- log.Println("No active builds")
+ if activeBuild == nil {
+ log.Println("No active build")
return
}
@@ -95,7 +102,7 @@ func (buildState *activeBuildStreamState) execPlanBuild(activeBuilds []*types.Ac
branch := buildState.branch
activePlan := GetActivePlan(planId, branch)
- filePath := activeBuilds[0].Path
+ filePath := activeBuild.Path
// stream initial status to client
buildInfo := &shared.BuildInfo{
@@ -111,9 +118,9 @@ func (buildState *activeBuildStreamState) execPlanBuild(activeBuilds []*types.Ac
fileState := &activeBuildStreamFileState{
activeBuildStreamState: buildState,
filePath: filePath,
- activeBuilds: activeBuilds,
+ activeBuild: activeBuild,
}
- err := fileState.loadBuildFile(activeBuilds)
+ err := fileState.loadBuildFile(activeBuild)
if err != nil {
log.Printf("Error loading build file: %v\n", err)
return
@@ -124,7 +131,7 @@ func (buildState *activeBuildStreamState) execPlanBuild(activeBuilds []*types.Ac
func (fileState *activeBuildStreamFileState) buildFile() {
filePath := fileState.filePath
- activeBuilds := fileState.activeBuilds
+ activeBuild := fileState.activeBuild
planId := fileState.plan.Id
branch := fileState.branch
currentPlan := fileState.currentPlanState
@@ -194,12 +201,12 @@ func (fileState *activeBuildStreamFileState) buildFile() {
// new file
planRes := &db.PlanFileResult{
- OrgId: currentOrgId,
- PlanId: planId,
- PlanBuildId: build.Id,
- ConvoMessageIds: build.ConvoMessageIds,
- Path: filePath,
- Content: activeBuilds[0].FileContent,
+ OrgId: currentOrgId,
+ PlanId: planId,
+ PlanBuildId: build.Id,
+ ConvoMessageId: build.ConvoMessageId,
+ Path: filePath,
+ Content: activeBuild.FileContent,
}
fileState.onFinishBuildFile(planRes)
return
@@ -208,36 +215,13 @@ func (fileState *activeBuildStreamFileState) buildFile() {
log.Println("Getting file from model: " + filePath)
// log.Println("File context:", fileContext)
- replacePrompt := prompts.GetReplacePrompt(filePath)
- currentStatePrompt := prompts.GetBuildCurrentStatePrompt(filePath, currentState)
- sysPrompt := prompts.GetBuildSysPrompt(filePath, currentStatePrompt)
-
- var mergedReply string
- for _, activeBuild := range activeBuilds {
- mergedReply += "\n\n" + activeBuild.ReplyContent
- }
-
- log.Println("Num active builds: " + fmt.Sprintf("%d", len(activeBuilds)))
- // log.Println("Merged reply:")
- // log.Println(mergedReply)
+ sysPrompt := prompts.GetBuildSysPrompt(filePath, currentState, activeBuild.FileDescription, activeBuild.FileContent)
fileMessages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: sysPrompt,
},
- {
- Role: openai.ChatMessageRoleUser,
- Content: activePlan.Prompt,
- },
- {
- Role: openai.ChatMessageRoleAssistant,
- Content: mergedReply,
- },
- {
- Role: openai.ChatMessageRoleUser,
- Content: replacePrompt,
- },
}
log.Println("Calling model for file: " + filePath)
@@ -251,13 +235,13 @@ func (fileState *activeBuildStreamFileState) buildFile() {
Tools: []openai.Tool{
{
Type: "function",
- Function: prompts.ReplaceFn,
+ Function: prompts.ListReplacementsFn,
},
},
ToolChoice: openai.ToolChoice{
Type: "function",
Function: openai.ToolFunction{
- Name: prompts.ReplaceFn.Name,
+ Name: prompts.ListReplacementsFn.Name,
},
},
Messages: fileMessages,
diff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.go
index cf9de4e7..f9e8b550 100644
--- a/app/server/model/plan/build_finish.go
+++ b/app/server/model/plan/build_finish.go
@@ -17,7 +17,7 @@ func (state *activeBuildStreamFileState) onFinishBuild() {
branch := state.branch
currentOrgId := state.currentOrgId
currentUserId := state.currentUserId
- convoMessageIds := state.convoMessageIds
+ convoMessageId := state.convoMessageId
build := state.build
activePlan := GetActivePlan(planId, branch)
@@ -26,12 +26,9 @@ func (state *activeBuildStreamFileState) onFinishBuild() {
stillStreaming := false
var doneCh chan bool
ap := GetActivePlan(planId, branch)
- for _, convoMessageId := range convoMessageIds {
- if ap.CurrentStreamingReplyId == convoMessageId {
- stillStreaming = true
- doneCh = ap.CurrentReplyDoneCh
- break
- }
+ if ap.CurrentStreamingReplyId == convoMessageId {
+ stillStreaming = true
+ doneCh = ap.CurrentReplyDoneCh
}
if stillStreaming {
log.Println("Reply is still streaming, waiting for it to finish before finishing build")
@@ -174,7 +171,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanF
currentOrgId := fileState.currentOrgId
currentUserId := fileState.currentUserId
build := fileState.build
- activeBuilds := fileState.activeBuilds
+ activeBuild := fileState.activeBuild
activePlan := GetActivePlan(planId, branch)
filePath := fileState.filePath
@@ -238,9 +235,7 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanF
return
}
- for _, build := range activeBuilds {
- build.Success = true
- }
+ activeBuild.Success = true
UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {
ap.BuiltFiles[filePath] = true
@@ -265,20 +260,18 @@ func (fileState *activeBuildStreamFileState) onFinishBuildFile(planRes *db.PlanF
log.Printf("File %s finished, but not all builds finished\n", filePath)
} else {
log.Printf("Processing next build for file %s\n", filePath)
- var nextBuilds []*types.ActiveBuild
- for _, build := range activePlan.BuildQueuesByPath[filePath] {
- if !build.BuildFinished() && len(nextBuilds) < 5 {
- nextBuilds = append(nextBuilds, build)
+ queue := activePlan.BuildQueuesByPath[filePath]
+ var nextBuild *types.ActiveBuild
+ for _, build := range queue {
+ if !build.BuildFinished() {
+ nextBuild = build
+ break
}
}
- // log.Println("Next builds:")
- // spew.Dump(nextBuilds)
- log.Printf("%d builds left for file %s\n", len(nextBuilds), filePath)
-
- if len(nextBuilds) > 0 {
+ if nextBuild != nil {
log.Println("Calling execPlanBuild for next build in queue")
- go fileState.execPlanBuild(nextBuilds)
+ go fileState.execPlanBuild(nextBuild)
}
return
}
@@ -291,16 +284,14 @@ func (fileState *activeBuildStreamFileState) onBuildFileError(err error) {
branch := fileState.branch
filePath := fileState.filePath
build := fileState.build
- activeBuilds := fileState.activeBuilds
+ activeBuild := fileState.activeBuild
activePlan := GetActivePlan(planId, branch)
log.Printf("Error for file %s: %v\n", filePath, err)
- for _, build := range activeBuilds {
- build.Success = false
- build.Error = err
- }
+ activeBuild.Success = false
+ activeBuild.Error = err
activePlan.StreamDoneCh <- &shared.ApiError{
Type: shared.ApiErrorTypeOther,
diff --git a/app/server/model/plan/build_load.go b/app/server/model/plan/build_load.go
index c85c6997..a312c979 100644
--- a/app/server/model/plan/build_load.go
+++ b/app/server/model/plan/build_load.go
@@ -10,7 +10,7 @@ import (
"github.com/plandex/plandex/shared"
)
-func (state *activeBuildStreamState) loadBuild() (map[string][]*types.ActiveBuild, error) {
+func (state *activeBuildStreamState) loadPendingBuilds() (map[string][]*types.ActiveBuild, error) {
client := state.client
plan := state.plan
branch := state.branch
@@ -118,7 +118,7 @@ func (state *activeBuildStreamState) loadBuild() (map[string][]*types.ActiveBuil
return pendingBuildsByPath, nil
}
-func (state *activeBuildStreamFileState) loadBuildFile(activeBuilds []*types.ActiveBuild) error {
+func (state *activeBuildStreamFileState) loadBuildFile(activeBuild *types.ActiveBuild) error {
currentOrgId := state.currentOrgId
currentUserId := state.currentUserId
@@ -128,15 +128,7 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuilds []*types.Act
activePlan := GetActivePlan(planId, branch)
- var convoMessageIds []string
- added := map[string]bool{}
-
- for _, activeBuild := range activeBuilds {
- if !added[activeBuild.ReplyId] {
- convoMessageIds = append(convoMessageIds, activeBuild.ReplyId)
- added[activeBuild.ReplyId] = true
- }
- }
+ convoMessageId := activeBuild.ReplyId
if !activePlan.IsBuildingByPath[filePath] {
UpdateActivePlan(activePlan.Id, activePlan.Branch, func(ap *types.ActivePlan) {
@@ -145,10 +137,10 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuilds []*types.Act
}
build := &db.PlanBuild{
- OrgId: currentOrgId,
- PlanId: planId,
- ConvoMessageIds: convoMessageIds,
- FilePath: filePath,
+ OrgId: currentOrgId,
+ PlanId: planId,
+ ConvoMessageId: convoMessageId,
+ FilePath: filePath,
}
err := db.StorePlanBuild(build)
@@ -227,7 +219,7 @@ func (state *activeBuildStreamFileState) loadBuildFile(activeBuilds []*types.Act
}
state.filePath = filePath
- state.convoMessageIds = convoMessageIds
+ state.convoMessageId = convoMessageId
state.build = build
state.currentPlanState = currentPlan
diff --git a/app/server/model/plan/build_result.go b/app/server/model/plan/build_result.go
index bf10e36d..fd176626 100644
--- a/app/server/model/plan/build_result.go
+++ b/app/server/model/plan/build_result.go
@@ -2,6 +2,7 @@ package plan
import (
"plandex-server/db"
+ "plandex-server/types"
"sort"
"strings"
@@ -10,14 +11,15 @@ import (
)
type planResultParams struct {
- orgId string
- planId string
- planBuildId string
- convoMessageIds []string
- filePath string
- currentState string
- context *db.Context
- replacements []*shared.Replacement
+ orgId string
+ planId string
+ planBuildId string
+ convoMessageId string
+ filePath string
+ currentState string
+ context *db.Context
+ fileContent string
+ streamedReplacements []*types.StreamedReplacement
}
func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {
@@ -27,8 +29,38 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {
filePath := params.filePath
currentState := params.currentState
contextPart := params.context
- replacements := params.replacements
+ streamedReplacements := params.streamedReplacements
updated := params.currentState
+ fileContent := params.fileContent
+
+ currentStateLines := strings.Split(currentState, "\n")
+ fileContentLines := strings.Split(fileContent, "\n")
+
+ var replacements []*shared.Replacement
+ for _, streamedReplacement := range streamedReplacements {
+ var old string
+ var new string
+
+ if streamedReplacement.Old.StartLine == streamedReplacement.Old.EndLine {
+ old = currentStateLines[streamedReplacement.Old.StartLine-1]
+ } else {
+ old = strings.Join(currentStateLines[streamedReplacement.Old.StartLine-1:streamedReplacement.Old.EndLine], "\n")
+ }
+
+ if streamedReplacement.New.StartLine == streamedReplacement.New.EndLine {
+ new = fileContentLines[streamedReplacement.New.StartLine-1]
+ } else {
+ new = strings.Join(fileContentLines[streamedReplacement.New.StartLine-1:streamedReplacement.New.EndLine], "\n")
+ }
+
+ replacement := &shared.Replacement{
+ Old: old,
+ New: new,
+ Summary: streamedReplacement.ShortSummary,
+ }
+
+ replacements = append(replacements, replacement)
+ }
sort.Slice(replacements, func(i, j int) bool {
iIdx := strings.Index(updated, replacements[i].Old)
@@ -51,15 +83,15 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {
}
return &db.PlanFileResult{
- OrgId: orgId,
- PlanId: planId,
- PlanBuildId: planBuildId,
- ConvoMessageIds: params.convoMessageIds,
- Content: "",
- Path: filePath,
- Replacements: replacements,
- ContextSha: contextSha,
- ContextBody: contextBody,
- AnyFailed: !allSucceeded,
+ OrgId: orgId,
+ PlanId: planId,
+ PlanBuildId: planBuildId,
+ ConvoMessageId: params.convoMessageId,
+ Content: "",
+ Path: filePath,
+ Replacements: replacements,
+ ContextSha: contextSha,
+ ContextBody: contextBody,
+ AnyFailed: !allSucceeded,
}, allSucceeded
}
diff --git a/app/server/model/plan/build_stream.go b/app/server/model/plan/build_stream.go
index a7d2260a..8f2d545b 100644
--- a/app/server/model/plan/build_stream.go
+++ b/app/server/model/plan/build_stream.go
@@ -29,10 +29,10 @@ type activeBuildStreamState struct {
type activeBuildStreamFileState struct {
*activeBuildStreamState
filePath string
- convoMessageIds []string
+ convoMessageId string
build *db.PlanBuild
currentPlanState *shared.CurrentPlanState
- activeBuilds []*types.ActiveBuild
+ activeBuild *types.ActiveBuild
buffer string
contextPart *db.Context
currentState string
@@ -46,6 +46,7 @@ func (fileState *activeBuildStreamFileState) listenStream(stream *openai.ChatCom
branch := fileState.branch
currentState := fileState.currentState
contextPart := fileState.contextPart
+ activeBuild := fileState.activeBuild
activePlan := GetActivePlan(planId, branch)
@@ -120,18 +121,20 @@ func (fileState *activeBuildStreamFileState) listenStream(stream *openai.ChatCom
var streamed types.StreamedReplacements
err = json.Unmarshal([]byte(fileState.buffer), &streamed)
if err == nil && len(streamed.Replacements) > 0 {
- log.Printf("File %s: Parsed replacements\n", filePath)
+ log.Printf("File %s: Parsed streamed replacements\n", filePath)
+ spew.Dump(streamed)
planFileResult, allSucceeded := getPlanResult(
planResultParams{
- orgId: currentOrgId,
- planId: planId,
- planBuildId: build.Id,
- convoMessageIds: build.ConvoMessageIds,
- filePath: filePath,
- currentState: currentState,
- context: contextPart,
- replacements: streamed.Replacements,
+ orgId: currentOrgId,
+ planId: planId,
+ planBuildId: build.Id,
+ convoMessageId: build.ConvoMessageId,
+ filePath: filePath,
+ currentState: currentState,
+ context: contextPart,
+ fileContent: activeBuild.FileContent,
+ streamedReplacements: streamed.Replacements,
},
)
diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.go
index 3515b1c2..15cf0e9a 100644
--- a/app/server/model/plan/tell_stream.go
+++ b/app/server/model/plan/tell_stream.go
@@ -344,11 +344,12 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionSt
// log.Printf("content: %s\n", content)
replyParser.AddChunk(content, true)
- res := replyParser.Read()
- files := res.Files
- fileContents := res.FileContents
- state.replyNumTokens = res.TotalTokens
- currentFile := res.CurrentFilePath
+ parserRes := replyParser.Read()
+ files := parserRes.Files
+ fileContents := parserRes.FileContents
+ state.replyNumTokens = parserRes.TotalTokens
+ currentFile := parserRes.CurrentFilePath
+ fileDescriptions := parserRes.FileDescriptions
// log.Printf("currentFile: %s\n", currentFile)
// log.Println("files:")
@@ -445,10 +446,11 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionSt
}
buildState.queueBuilds([]*types.ActiveBuild{{
- ReplyId: replyId,
- ReplyContent: active.CurrentReplyContent,
- FileContent: fileContents[i],
- Path: file,
+ ReplyId: replyId,
+ Idx: i,
+ FileDescription: fileDescriptions[i],
+ FileContent: fileContents[i],
+ Path: file,
}})
}
replyFiles = append(replyFiles, file)
diff --git a/app/server/model/prompts/build.go b/app/server/model/prompts/build.go
index 3129db75..a1886313 100644
--- a/app/server/model/prompts/build.go
+++ b/app/server/model/prompts/build.go
@@ -1,235 +1,148 @@
package prompts
import (
- "encoding/json"
"fmt"
+ "strings"
- "github.com/plandex/plandex/shared"
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)
-func GetBuildSysPrompt(filePath, currentStatePrompt string) string {
- return fmt.Sprintf(`
-[YOUR INSTRUCTIONS]
-
-You are an AI replacer. You apply changes from a plan to a given code file using the 'writeReplacements' function. You call 'writeReplacements' with a valid JSON array of replacements.
-
-Each replacement is an object with 3 properties: 'old', 'new', and 'summary'. 'old' is the *exact* old text to replace. 'new' is the new text to replace it with. 'summary' is a brief 1 line summary of the change.
-
-Use your judgment on how to logically apply the changes from the plan in a series of replacements. Use both the code and the plan description to determine the correct order of replacements. BE COMPLETELY SURE that replacements are inserted logically at the correct locations in the file and do not break any syntax or logic rules of the programming language.
-
-Lean toward using fewer replacements. If you can apply all the changes in a single replacement, and that replacement isn't very long, do that. If you need to use multiple replacements, that's fine too, but try to use as few as possible.
-
-DO NOT INCLUDE any sections that are just comments, placeholders, references to the original file, or TODOs that are not yet implemented. Only include actual changes that move the plan forward and are ready to be applied to the file.
-
-These replacements will be applied automatically by a program to the file exactly as written, so the 'old' text must be an EXACT SUBSTRING of the **current state of the file**. The current state of the file will be provided below, labelled with '**Current state of file for %s:**'. It does *not* include any of the suggested changes from the latest response. The 'old' text MUST be an exact substring of the current state of the file, not the suggested changes from the latest response.
-
-Pay special attention to any special characters in the strings, extra spaces, or anything else that might cause 'old' not to be an exact substring.
-
-The 'old' text should be unique and unambiguous in the current file. It must not overlap with any other 'old' text in the list of replacements. Make the 'old' text as short as it can be while still being unique and unambiguous. If the 'old' text can be a single line, it should be. If it must be multiple lines, it should be as few lines as possible.
-
-The 'new' text must include the full text of the replacement without any placeholders or references to the original file. DO NOT INCLUDE text like "// ... existing code", "// ... rest of the file", or other equivalents with different wording/formatting/syntax in the 'new' text. Only include the actual code that should be inserted.
-
-For the 'new' text, apart from the exceptions I mentioned in the previous paragraph, do not make any additional modifications on top of the changes from the plan unless absolutely necessary to apply the change correctly. Don't add additional comments. Don't escape characters (like emoji or other characters) unless they are also escaped in the plan. Keep as closely as possible to the changes as suggested in the plan, down to specific characters and formatting.
-
-You MUST call only 'writeReplacements'--don't call any other function or produce any other output.
-
-You MUST only generate replacements for the *current file*--don't generate replacements for any other file. If other files are present in the plan, ignore them. Only generate replacements for the current file.
-
-Replacement examples below. Note: >>> and <<< indicate the start and end of an example response.
-
-1.)
-If the current file is 'main.go' and the state is:
-`+"```"+`
-package main
-
-import "fmt"
-
-func main() {
- fmt.Println("Hello, world!")
+func GetBuildSysPrompt(filePath, currentState, desc, changes string) string {
+ return listReplacementsPrompt + "\n\n" + getBuildCurrentStatePrompt(filePath, currentState) + "\n\n" + getBuildPrompt(desc, changes)
}
-`+"```"+`
-
-And the previous response was:
->>>
-You can change the main.go file to print the current time instead of "Hello, world!".:
+func getBuildPrompt(desc, changes string) string {
+ s := ""
-- main.go:
-`+"```"+`
-func main() {
- fmt.Println(time.Now())
-}
-`+"```"+`
-
-You'll also need to import the time package:
-
-- main.go:
-`+"```"+`
-import (
- "fmt"
- "time"
-)
-`+"```"+`
-<<<
-
-Then you would call the 'writeReplacements' function like this:
-
-writeReplacements({
- replacements: [
- {
- old: "import \"fmt\"",
- new: "import (\n\t\"fmt\"\n\t\"time\"\n)",
- summary: "Import time package"
- },
- {
- old: "fmt.Println(\"Hello, world!\")",
- new: "fmt.Println(time.Now())",
- summary: "Print current time"
- }
+ if desc != "" {
+ s += "Description of the proposed changes from AI-generated plan:\n```\n" + desc + "\n```\n\n"
}
-})
-
-2.)
-If the current file is 'helpers.go' and the state is:
-`+"```"+`
-package helpers
-
-func Add(a, b int) int {
- return a + b
-}
-`+"```"+`
-
-And the previous response was:
->>>
-Add another function to the helpers.go file that subtracts two numbers:
-
-- helpers.go:
-`+"```"+`
-func Subtract(a, b int) int {
- return a - b
-}
-`+"```"+`
-<<<
-
-Then you would call the 'writeReplacements' function like this:
-
-writeReplacements({
- replacements: [
- {
- old: "\n}",
- new: "\n}\n\nfunc Subtract(a, b int) int {\n\treturn a - b\n}",
- summary: "Add Subtract function"
- }
- ]
-})
-
-3.)
-If the current file is 'main.go' and the state is:
-`+"```"+`
-package main
-
-import "fmt"
-
-func main() {
- fmt.Println("Hello, world!")
-}
-`+"```"+`
-
-And the previous response was:
-
->>>
-You can change the main.go file to print "I love you!" in addition to "Hello, world!".:
-
-- main.go:
-`+"```"+`
-func main() {
- fmt.Println("Hello, world!")
- fmt.Println("I love you!")
-}
-`+"```"+`
-<<<
+ withLineNums := ""
+ for i, line := range strings.Split(changes, "\n") {
+ withLineNums += fmt.Sprintf("%d: %s\n", i+1, line)
+ }
-Then you would call the 'writeReplacements' function like this:
+ s += "Proposed changes:\n```\n" + withLineNums + "\n```"
-writeReplacements({
- replacements: [
- {
- old: "fmt.Println(\"Hello, world!\")",
- new: "fmt.Println(\"Hello, world!\")\n\tfmt.Println(\"I love you!\")",
- summary: "Also print \"I love you!\""
- }
- ]
-})
+ s += "\n\n" + "Now call the 'listReplacements' function with a valid JSON array of replacements according to your instructions. You must always call 'listReplacements' with one or more valid replacements. Don't call any other function."
-[END INSTRUCTIONS]
-%s
-`, filePath, currentStatePrompt)
+ return s
}
-func GetBuildCurrentStatePrompt(filePath, currentState string) string {
+func getBuildCurrentStatePrompt(filePath, currentState string) string {
if currentState == "" {
return ""
}
- return fmt.Sprintf("**The current file is %s. Current state of the file:**\n```\n%s\n```", filePath, currentState) + "\n\n"
+
+ withLineNums := ""
+ for i, line := range strings.Split(currentState, "\n") {
+ withLineNums += fmt.Sprintf("%d: %s\n", i+1, line)
+ }
+
+ return fmt.Sprintf("**The current file is %s. Original state of the file:**\n```\n%s\n```", filePath, withLineNums) + "\n\n"
}
-var ReplaceFn = openai.FunctionDefinition{
- Name: "writeReplacements",
+var listReplacementsPrompt = `
+ You are an AI that analyzes a code file and an AI-generated plan to update the code file and produces a list of replacements.
+
+ [YOUR INSTRUCTIONS]
+ Call the 'listReplacements' function with a valid JSON array of replacements. Each replacement is an object with properties: 'shortSummary', 'changeSections', 'old', and 'new'.
+
+ The 'shortSummary' property is a brief summary of the change.
+
+ 'shortSummary' examples:
+ - 'Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar.'
+ - 'Change the value of someVar to 10.'
+ - 'Update the Org model to include StripeCustomerId and StripeSubscriptionId fields.'
+
+ The 'changeSections' property is a description of what section of the original file will be replaced, and what section of code from the proposed changes will replace it. Refer to sections of code using line numbers, 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 with which line(s) of code from the proposed changes.
+
+ 'changeSections' example:
+ ---
+ The section to be replaced begins on line 10 of the original file with 'for i := 0; i < 10; i++ {...' and ends on line 15 of the original file with '}'
+
+ The new code begins on line 17 of the proposed changes with 'for i := 0; i < 10; i++ {...' and ends on line 25 of the proposed changes with '}'
+ ---
+
+ If only a single line needs to be replaced, you can reference a single line from the original file and the proposed changes rather than a range of lines like this:
+ ---
+ The section to be replaced is on line 10 of the original file with 'someVar = 5'
+
+ The new code is on line 12 of the proposed changes with 'someVar = 10'
+ ---
+
+ The 'old' property is an object with two properties: 'startLine' and 'endLine'.
+
+ 'startLine' is the 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. For a single line replacement, 'startLine' and 'endLine' will be the same.
+
+ The 'new' property is also an object with two properties: 'startLine' and 'endLine'.
+
+ 'startLine' is the line number where the new section begins in the code block included with the proposed changes. 'endLine' is the line number where the new section ends in the code block included with the proposed changes. For a single line replacement, 'startLine' and 'endLine' will be the same.
+
+ Example function call with all keys:
+ ---
+ listReplacements([{
+ shortSummary: "Insert function ExecQuery after GetResults function.",
+ changeSections: "The section to be replaced is on line 5 of the original file with '\\n'\n\nThe new code starts on line 3 of the proposed changes with 'func ExecQuery()...' and ends on line 10 of the proposed changes with '}'",
+ old: {
+ startLine: 5,
+ endLine: 5,
+ },
+ new: {
+ startLine: 3,
+ endLine: 10,
+ }
+ }])
+ ---
+ [END YOUR INSTRUCTIONS]
+`
+var ListReplacementsFn = openai.FunctionDefinition{
+ Name: "listReplacements",
Parameters: &jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"replacements": {
- Type: jsonschema.Array,
- Description: "A list of replacements to apply to the file",
+ Type: jsonschema.Array,
Items: &jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
+ "shortSummary": {
+ Type: jsonschema.String,
+ },
+ "changeSections": {
+ Type: jsonschema.String,
+ },
"old": {
- Type: jsonschema.String,
- Description: "The old text to replace. Must be an exact substring of the current state of the file.",
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "startLine": {
+ Type: jsonschema.Integer,
+ },
+ "endLine": {
+ Type: jsonschema.Integer,
+ },
+ },
+ Required: []string{"startLine", "endLine"},
},
"new": {
- Type: jsonschema.String,
- Description: "The new text to replace it with from the changes suggested in the previous response.",
- },
- "summary": {
- Type: jsonschema.String,
- Description: "A brief 1 line summary of the change.",
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "startLine": {
+ Type: jsonschema.Integer,
+ },
+ "endLine": {
+ Type: jsonschema.Integer,
+ },
+ },
+ Required: []string{"startLine", "endLine"},
},
},
- Required: []string{"old", "new", "summary"},
+ Required: []string{"shortSummary", "changeSections", "old", "new"},
},
},
},
Required: []string{"replacements"},
},
}
-
-func GetReplacePrompt(filePath string) string {
- return fmt.Sprintf(`
- Based on your instructions, apply the changes from the plan to %s. Call the 'writeReplacements' function with a JSON array of replacements to apply to the file from your previous response. You *must always call the 'writeReplacements' function*. Don't call any other function.`, filePath)
-}
-
-func GetCorrectReplacementPrompt(replacements []*shared.Replacement, currentState string) (string, error) {
- msg := "There were errors with the replacements you suggested."
- for i, replacement := range replacements {
-
- if replacement.Failed {
- bytes, err := json.Marshal(replacement)
- if err != nil {
- return "", fmt.Errorf("failed to marshal replacement: %w", err)
- }
-
- msg += fmt.Sprintf("\n- The replacement at index %d was invalid. The replacement you suggested was:\n\n```%s```\n\n", i, string(bytes))
-
- msg += fmt.Sprintf("\n- The string `%s` (which you set for the 'old' key of this replacement) was not found in the current state of the file.", replacement.Old)
- }
-
- }
- msg += "\n\nPlease review these errors and try again to call the 'writeReplacements' function with corrected replacements. Pay special attention to any special characters in the strings, extra spaces, or anything else that might cause the strings to not match exactly. You MUST call 'writeReplacements' with an updated list of replacements. Don't call any other function, produce any other output, or call 'writeReplacements' with the same list of replacements as before--it must be called with an updated list of replacements to fix the errors."
-
- return msg, nil
-}
diff --git a/app/server/model/prompts/describe.go b/app/server/model/prompts/describe.go
index 88d9da61..c650f102 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. You *must* call the 'describePlan' function with a valid JSON object that includes the 'commitMsg' key. Don't produce any other output. Don't call any other function."
+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."
var DescribePlanFn = openai.FunctionDefinition{
Name: "describePlan",
@@ -13,8 +13,7 @@ var DescribePlanFn = openai.FunctionDefinition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"commitMsg": {
- Type: jsonschema.String,
- Description: "A good, succinct commit message for the changes proposed.",
+ Type: jsonschema.String,
},
},
Required: []string{"commitMsg"},
diff --git a/app/server/model/prompts/exec_status.go b/app/server/model/prompts/exec_status.go
index c19a6167..d70d4d8f 100644
--- a/app/server/model/prompts/exec_status.go
+++ b/app/server/model/prompts/exec_status.go
@@ -31,8 +31,7 @@ var ShouldAutoContinueFn = openai.FunctionDefinition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"shouldContinue": {
- Type: jsonschema.Boolean,
- Description: "Whether the plan should automatically continue.",
+ Type: jsonschema.Boolean,
},
},
Required: []string{"shouldContinue"},
diff --git a/app/server/model/prompts/name.go b/app/server/model/prompts/name.go
index 0a3ac091..851f3ad3 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. Only call the 'namePlan' function in your response. Don't call any other function."
+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."
var PlanNameFn = openai.FunctionDefinition{
Name: "namePlan",
@@ -17,8 +17,7 @@ var PlanNameFn = openai.FunctionDefinition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"planName": {
- Type: jsonschema.String,
- Description: "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.",
+ Type: jsonschema.String,
},
},
Required: []string{"planName"},
diff --git a/app/server/model/prompts/summary.go b/app/server/model/prompts/summary.go
index 263dcc86..3fe36d54 100644
--- a/app/server/model/prompts/summary.go
+++ b/app/server/model/prompts/summary.go
@@ -5,7 +5,7 @@ import (
"github.com/sashabaranov/go-openai/jsonschema"
)
-const SysShortSummary = "You are an AI summarizer that summarizes text, including programs, documentation, websites, and more. Most text will be related to software development."
+const SysShortSummary = "You are an AI summarizer that summarizes text, including programs, documentation, websites, and more. Most text will be related to software development. You produce a brief summary of the text. A few sentences at most."
var ShortSummaryFn = openai.FunctionDefinition{
Name: "summarize",
@@ -13,8 +13,7 @@ var ShortSummaryFn = openai.FunctionDefinition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"summary": {
- Type: jsonschema.String,
- Description: "A brief summary of the text. A few sentences at most",
+ Type: jsonschema.String,
},
},
Required: []string{"summary"},
diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.go
index 88ea75ff..e741ed11 100644
--- a/app/server/types/active_plan.go
+++ b/app/server/types/active_plan.go
@@ -14,13 +14,14 @@ import (
)
type ActiveBuild struct {
- ReplyId string
- ReplyContent string
- FileContent string
- Path string
- Buffer string
- Success bool
- Error error
+ ReplyId string
+ FileDescription string
+ FileContent string
+ Path string
+ Idx int
+ Buffer string
+ Success bool
+ Error error
}
type ActivePlan struct {
@@ -160,7 +161,6 @@ func (b *ActiveBuild) BuildFinished() bool {
func (ap *ActivePlan) PendingBuildsByPath(orgId, userId string, convoMessagesArg []*db.ConvoMessage) (map[string][]*ActiveBuild, error) {
var planDescs []*db.ConvoMessageDescription
- var currentPlan *shared.CurrentPlanState
var convoMessagesById map[string]*db.ConvoMessage
errCh := make(chan error)
@@ -173,16 +173,6 @@ func (ap *ActivePlan) PendingBuildsByPath(orgId, userId string, convoMessagesArg
return
}
- currentPlan, err = db.GetCurrentPlanState(db.CurrentPlanStateParams{
- OrgId: orgId,
- PlanId: ap.Id,
- PendingBuildDescriptions: planDescs,
- })
- if err != nil {
- errCh <- fmt.Errorf("error getting current plan state: %v", err)
- return
- }
-
errCh <- nil
}()
@@ -233,16 +223,23 @@ func (ap *ActivePlan) PendingBuildsByPath(orgId, userId string, convoMessagesArg
return nil, fmt.Errorf("no convo message for ID: %s", desc.ConvoMessageId)
}
- for _, file := range desc.Files {
+ convoMessage := convoMessagesById[desc.ConvoMessageId]
+
+ replyParser := NewReplyParser()
+ replyParser.AddChunk(convoMessage.Message, false)
+ parserRes := replyParser.FinishAndRead()
+
+ for i, file := range desc.Files {
if activeBuildsByPath[file] == nil {
activeBuildsByPath[file] = []*ActiveBuild{}
}
activeBuildsByPath[file] = append(activeBuildsByPath[file], &ActiveBuild{
- ReplyId: desc.ConvoMessageId,
- FileContent: currentPlan.CurrentPlanFiles.Files[file],
- Path: file,
- ReplyContent: convoMessagesById[desc.ConvoMessageId].Message,
+ ReplyId: desc.ConvoMessageId,
+ Idx: i,
+ FileContent: parserRes.FileContents[i],
+ Path: file,
+ FileDescription: parserRes.FileDescriptions[i],
})
}
}
diff --git a/app/server/types/model.go b/app/server/types/model.go
index c2f1255b..0b2f5bf7 100644
--- a/app/server/types/model.go
+++ b/app/server/types/model.go
@@ -1,11 +1,21 @@
package types
-import "github.com/plandex/plandex/shared"
-
type StreamedFile struct {
Content string `json:"content"`
}
+type Section struct {
+ StartLine int `json:"startLine"`
+ EndLine int `json:"endLine"`
+}
+
+type StreamedReplacement struct {
+ ShortSummary string `json:"shortSummary"`
+ ChangeSections string `json:"changeSections"`
+ Old Section `json:"old"`
+ New Section `json:"new"`
+}
+
type StreamedReplacements struct {
- Replacements []*shared.Replacement `json:"replacements"`
+ Replacements []*StreamedReplacement `json:"replacements"`
}
diff --git a/app/server/types/reply.go b/app/server/types/reply.go
index b7d26ca3..c538e8c1 100644
--- a/app/server/types/reply.go
+++ b/app/server/types/reply.go
@@ -2,38 +2,42 @@ package types
import (
"strings"
-
- "github.com/davecgh/go-spew/spew"
)
type parserRes struct {
- CurrentFilePath string
- Files []string
- FileContents []string
- NumTokensByFile map[string]int
- TotalTokens int
+ CurrentFilePath string
+ Files []string
+ FileContents []string
+ FileDescriptions []string
+ NumTokensByFile map[string]int
+ TotalTokens int
}
type ReplyParser struct {
- lines []string
- currentFileLines []string
- lineIndex int
- maybeFilePath string
- currentFilePath string
- currentFileIdx int
- files []string
- fileContents []string
- numTokens int
- numTokensByFile map[string]int
+ lines []string
+ currentFileLines []string
+ lineIndex int
+ maybeFilePath string
+ currentFilePath string
+ currentFileIdx int
+ files []string
+ fileContents []string
+ fileDescriptions []string
+ currentDescriptionLines []string
+ currentDescriptionLineIdx int
+ numTokens int
+ numTokensByFile map[string]int
}
func NewReplyParser() *ReplyParser {
info := &ReplyParser{
- lines: []string{""},
- currentFileLines: []string{},
- files: []string{},
- fileContents: []string{},
- numTokensByFile: make(map[string]int),
+ lines: []string{""},
+ currentFileLines: []string{},
+ files: []string{},
+ fileContents: []string{},
+ currentDescriptionLines: []string{""},
+ fileDescriptions: []string{},
+ numTokensByFile: make(map[string]int),
}
return info
@@ -61,6 +65,12 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
r.lines = append(r.lines, "")
hasNewLine = true
r.lineIndex++
+
+ if r.currentFilePath == "" {
+ r.currentDescriptionLines = append(r.currentDescriptionLines, "")
+ r.currentDescriptionLineIdx++
+ }
+
} else {
chunkLines := strings.Split(chunk, "\n")
@@ -68,13 +78,28 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
currentLine := r.lines[r.lineIndex]
currentLine += chunkLines[0]
-
// log.Println("Current line:", strconv.Quote(currentLine))
r.lines[r.lineIndex] = currentLine
+ if r.currentFilePath == "" {
+ // log.Println("Current file path is empty--adding to current description...")
+ // log.Println("Current description lines:", r.currentDescriptionLines)
+ // log.Printf("Current description line index: %d\n", r.currentDescriptionLineIdx)
+
+ currentDescLine := r.currentDescriptionLines[r.currentDescriptionLineIdx]
+ currentDescLine += chunkLines[0]
+ r.currentDescriptionLines[r.currentDescriptionLineIdx] = currentDescLine
+ }
+
if len(chunkLines) > 1 {
r.lines = append(r.lines, chunkLines[1])
r.lineIndex++
+
+ if r.currentFilePath == "" {
+ r.currentDescriptionLines = append(r.currentDescriptionLines, chunkLines[1])
+ r.currentDescriptionLineIdx++
+ }
+
hasNewLine = true
if len(chunkLines) > 2 {
@@ -108,6 +133,11 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
r.fileContents = append(r.fileContents, "")
r.maybeFilePath = ""
r.currentFileLines = []string{}
+
+ r.fileDescriptions = append(r.fileDescriptions, strings.Join(r.currentDescriptionLines[0:len(r.currentDescriptionLines)-2], "\n"))
+ r.currentDescriptionLines = []string{""}
+ r.currentDescriptionLineIdx = 0
+
// log.Println("Confirmed file path:", r.currentFilePath) // Logging the confirmed file path
return
@@ -142,7 +172,7 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
r.files = append(r.files, r.currentFilePath)
r.currentFilePath = ""
- spew.Dump(r.files)
+ // spew.Dump(r.files)
} else {
// log.Println("Adding tokens to current file...") // Logging token addition
@@ -155,11 +185,12 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
func (r *ReplyParser) Read() parserRes {
return parserRes{
- CurrentFilePath: r.currentFilePath,
- Files: r.files,
- FileContents: r.fileContents,
- NumTokensByFile: r.numTokensByFile,
- TotalTokens: r.numTokens,
+ CurrentFilePath: r.currentFilePath,
+ Files: r.files,
+ FileContents: r.fileContents,
+ NumTokensByFile: r.numTokensByFile,
+ TotalTokens: r.numTokens,
+ FileDescriptions: r.fileDescriptions,
}
}
diff --git a/app/shared/context.go b/app/shared/context.go
index dae31018..f41b0a53 100644
--- a/app/shared/context.go
+++ b/app/shared/context.go
@@ -17,6 +17,7 @@ type ContextUpdateResult struct {
NumFiles int
NumUrls int
NumTrees int
+ MaxTokens int
}
func (c *Context) TypeAndIcon() (string, string) {
diff --git a/app/shared/data_models.go b/app/shared/data_models.go
index faa2d6a4..5358c681 100644
--- a/app/shared/data_models.go
+++ b/app/shared/data_models.go
@@ -139,12 +139,12 @@ type ConvoMessageDescription struct {
}
type PlanBuild struct {
- Id string `json:"id"`
- ConvoMessageIds []string `json:"convoMessageIds"`
- FilePath string `json:"filePath"`
- Error string `json:"error"`
- CreatedAt time.Time `json:"createdAt"`
- UpdatedAt time.Time `json:"updatedAt"`
+ Id string `json:"id"`
+ ConvoMessageId string `json:"convoMessageId"`
+ FilePath string `json:"filePath"`
+ Error string `json:"error"`
+ CreatedAt time.Time `json:"createdAt"`
+ UpdatedAt time.Time `json:"updatedAt"`
}
type Replacement struct {
@@ -157,19 +157,19 @@ type Replacement struct {
}
type PlanFileResult struct {
- Id string `json:"id"`
- ConvoMessageIds []string `json:"convoMessageIds"`
- PlanBuildId string `json:"planBuildId"`
- Path string `json:"path"`
- ContextBody string `json:"contextBody"`
- ContextSha string `json:"contextSha"`
- 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"`
+ 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"`
+ 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 {
diff --git a/app/shared/plan_result.go b/app/shared/plan_result.go
index a2da0e79..211531a6 100644
--- a/app/shared/plan_result.go
+++ b/app/shared/plan_result.go
@@ -186,6 +186,8 @@ func (planState *CurrentPlanState) GetFilesBeforeReplacement(
func (state *CurrentPlanState) PendingChangesSummary() string {
var msgs []string
+ msgs = append(msgs, "πŸ—οΈ Build pending changes")
+
descByConvoMessageId := make(map[string]*ConvoMessageDescription)
for _, desc := range state.PendingBuildDescriptions {
if desc.ConvoMessageId == "" {
@@ -205,10 +207,8 @@ func (state *CurrentPlanState) PendingChangesSummary() string {
for _, result := range state.PlanResult.Results {
convoIds := map[string]bool{}
- for _, convoMessageId := range result.ConvoMessageIds {
- if descByConvoMessageId[convoMessageId] != nil {
- convoIds[convoMessageId] = true
- }
+ if descByConvoMessageId[result.ConvoMessageId] != nil {
+ convoIds[result.ConvoMessageId] = true
}
var uniqueConvoIds []string
for convoId := range convoIds {
@@ -257,10 +257,10 @@ func (state *CurrentPlanState) PendingChangesSummary() string {
var descMsgs []string
if len(ch.descs) == 0 {
- descMsgs = append(descMsgs, "πŸ—οΈ Changes")
+ descMsgs = append(descMsgs, " ✏️ Changes")
} else {
for _, desc := range ch.descs {
- descMsgs = append(descMsgs, fmt.Sprintf("πŸ—οΈ %s", desc.CommitMsg))
+ descMsgs = append(descMsgs, fmt.Sprintf(" ✏️ %s", desc.CommitMsg))
}
}
@@ -306,27 +306,16 @@ func (state *CurrentPlanState) PendingChangesSummary() string {
})
if len(pendingNewFiles) > 0 {
- newMsg := ""
for _, path := range pendingNewFiles {
- newMsg += fmt.Sprintf(" β€’ new file β†’ %s\n", path)
+ msgs = append(msgs, fmt.Sprintf(" β€’ new file β†’ %s", path))
}
- msgs = append(msgs, newMsg)
}
if len(pendingReplacementPaths) > 0 {
- // updatesMsg := " ✏️ Edits:\n"
- updatesMsg := ""
-
for _, path := range pendingReplacementPaths {
- updatesMsg += fmt.Sprintf(" β€’ edit β†’ %s\n", path)
-
- replacements := pendingReplacementsByPath[path]
-
- for _, replacement := range replacements {
- updatesMsg += fmt.Sprintf(" βœ… %s\n", replacement.Summary)
- }
+ msgs = append(msgs, fmt.Sprintf(" β€’ edit β†’ %s", path))
}
- msgs = append(msgs, updatesMsg)
+
}
}