Skip to content

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

moving cli/server/shared to app, working on long tasks and more robust builds008fb78aws

Dane Schneider7736d01ca7cb
diff --git a/app/server/db/build_helpers.go b/app/server/db/build_helpers.go
index 06ef3a0d..3387a646 100644
--- a/app/server/db/build_helpers.go
+++ b/app/server/db/build_helpers.go
@@ -6,7 +6,7 @@ import (
)
func StorePlanBuild(build *PlanBuild) error {
- query := `INSERT INTO plan_builds (org_id, plan_id, convo_message_id) VALUES (:org_id, :plan_id, :convo_message_id) 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`
row, err := Conn.NamedQuery(query, build)
@@ -32,7 +32,7 @@ func StorePlanBuild(build *PlanBuild) error {
}
func SetBuildError(build *PlanBuild) error {
- _, err := Conn.Exec("UPDATE plan_builds SET error = $1, error_path = $2 WHERE id = $3", build.Error, build.ErrorPath, build.Id)
+ _, err := Conn.Exec("UPDATE plan_builds SET error = $1 WHERE id = $2", build.Error, build.Id)
if err != nil {
return fmt.Errorf("error setting build error: %v", err)
diff --git a/app/server/db/convo_helpers.go b/app/server/db/convo_helpers.go
index 9c35fa40..cb47adcf 100644
--- a/app/server/db/convo_helpers.go
+++ b/app/server/db/convo_helpers.go
@@ -65,10 +65,12 @@ func GetPlanConvo(orgId, planId string) ([]*ConvoMessage, error) {
func StoreConvoMessage(message *ConvoMessage, commit bool) (string, error) {
convoDir := getPlanConversationDir(message.OrgId, message.PlanId)
- id := uuid.New().String()
ts := time.Now().UTC()
- message.Id = id
+ if message.Id == "" {
+ message.Id = uuid.New().String()
+ }
+
message.CreatedAt = ts
bytes, err := json.Marshal(message)
diff --git a/app/server/db/db.go b/app/server/db/db.go
index 8e711f32..46f284df 100644
--- a/app/server/db/db.go
+++ b/app/server/db/db.go
@@ -48,23 +48,24 @@ func MigrationsUp() error {
return fmt.Errorf("error creating migration instance: %v", err)
}
- // Uncomment below to reset migration state to a specific version after a failure
- // if err := m.Force(2024011700); err != nil {
+ // Uncomment below (and update migration version) to reset migration state to a specific version after a failure
+ // migrateVersion := 2024011700
+ // if err := m.Force(migrateVersion); err != nil {
// return fmt.Errorf("error forcing migration version: %v", err)
// }
- // Uncomment below to run down migrations in development (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")
- // }
+ // Uncomment below to run down migrations in development (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")
+ }
err = m.Up()
diff --git a/app/server/db/models.go b/app/server/db/models.go
index a470ea0f..d0ca2c68 100644
--- a/app/server/db/models.go
+++ b/app/server/db/models.go
@@ -185,8 +185,8 @@ type PlanBuild struct {
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"`
- ErrorPath string `db:"error_path"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
@@ -196,7 +196,7 @@ func (build *PlanBuild) ToApi() *shared.PlanBuild {
Id: build.Id,
ConvoMessageId: build.ConvoMessageId,
Error: build.Error,
- ErrorPath: build.ErrorPath,
+ FilePath: build.FilePath,
CreatedAt: build.CreatedAt,
UpdatedAt: build.UpdatedAt,
}
diff --git a/app/server/migrations/2023120500_init.up.sql b/app/server/migrations/2023120500_init.up.sql
index b89aa949..31f730a5 100644
--- a/app/server/migrations/2023120500_init.up.sql
+++ b/app/server/migrations/2023120500_init.up.sql
@@ -149,8 +149,8 @@ CREATE TABLE IF NOT EXISTS plan_builds (
org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
plan_id UUID NOT NULL REFERENCES plans(id) ON DELETE CASCADE,
convo_message_id UUID NOT NULL,
+ file_path VARCHAR(255) NOT NULL,
error TEXT,
- error_path TEXT,
finished_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
diff --git a/app/server/model/plan/build.go b/app/server/model/plan/build.go
index 262d5d19..34d6984f 100644
--- a/app/server/model/plan/build.go
+++ b/app/server/model/plan/build.go
@@ -23,46 +23,66 @@ import (
const MaxRetries = 3
const MaxReplacementRetries = 1
-func Build(currentOrgId, planId string) *types.ActivePlan {
- log.Printf("Building plan: %s\n", planId)
-
- // goEnv := os.Getenv("GOENV")
- // if goEnv == "test" {
- // streamFilesLoremIpsum(onStream)
- // return nil
- // }
+// func BuildPlan(currentOrgId, planId string) *types.ActivePlan {
+// log.Printf("Building plan: %s\n", planId)
+
+// // goEnv := os.Getenv("GOENV")
+// // if goEnv == "test" {
+// // streamFilesLoremIpsum(onStream)
+// // return nil
+// // }
+
+// active := Active.Get(planId)
+
+// if active == nil {
+// log.Printf("Active plan not found for plan %s\n", planId)
+// // TODO: to allow for rebuilds or selective builds, construct the active plan here from the db
+// } else {
+// for _, filePath := range active.Files {
+// go execPlanBuild(currentOrgId, filePath, active)
+// }
+// }
+
+// return active
+// }
+
+func QueueBuild(currentOrgId, planId string, activeBuild *types.ActiveBuild) {
+ activePlan := Active.Get(planId)
+ filePath := activeBuild.Path
+
+ if !activePlan.PathFinished(filePath) {
+ log.Printf("Already building file %s\n", filePath)
+ return
+ } else {
- active := Active.Get(planId)
+ Active.Update(planId, func(active *types.ActivePlan) {
+ active.BuildQueuesByPath[filePath] = append(active.BuildQueuesByPath[filePath])
+ })
- if active == nil {
- log.Printf("Active plan not found for plan %s\n", planId)
- // TODO: to allow for rebuilds or selective builds, construct the active plan here from the db
- } else {
- go execPlanBuild(currentOrgId, planId, active)
+ execPlanBuild(currentOrgId, activePlan, activeBuild)
}
-
- return active
}
-func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
- for _, filePath := range active.Files {
- tokenCount := &shared.PlanTokenCount{
- Path: filePath,
- NumTokens: 0,
- Finished: false,
- }
- json, err := json.Marshal(tokenCount)
- if err != nil {
- log.Printf("Error marshalling plan token count: %v\n", err)
- active.StreamDoneCh <- err
- return
- }
- active.StreamCh <- string(json)
+func execPlanBuild(currentOrgId string, activePlan *types.ActivePlan, activeBuild *types.ActiveBuild) {
+ planId := activePlan.Id
+ filePath := activeBuild.Path
+
+ tokenCount := &shared.PlanTokenCount{
+ Path: filePath,
+ NumTokens: 0,
+ Finished: false,
+ }
+ tokenCountJson, err := json.Marshal(tokenCount)
+ if err != nil {
+ log.Printf("Error marshalling plan token count: %v\n", err)
+ activePlan.StreamDoneCh <- err
+ return
}
+ activePlan.StreamCh <- string(tokenCountJson)
- replyInfo := types.NewReplyInfo()
- replyInfo.AddToken(active.Content, true)
- _, fileContents, numTokensByFile, _ := replyInfo.FinishAndRead()
+ replyInfo := types.NewReplyParser()
+ replyInfo.AddToken(activePlan.CurrentReplyContent, true)
+ _, fileContents, numTokensByFile, _ := replyInfo.Read()
errCh := make(chan error)
@@ -73,7 +93,8 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
build = &db.PlanBuild{
OrgId: currentOrgId,
PlanId: planId,
- ConvoMessageId: active.AssistantMessageId,
+ ConvoMessageId: activeBuild.AssistantMessageId,
+ FilePath: filePath,
}
err := db.StorePlanBuild(build)
@@ -98,7 +119,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
res, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{
OrgId: currentOrgId,
PlanId: planId,
- Contexts: active.Contexts,
+ Contexts: activePlan.Contexts,
})
if err != nil {
errCh <- fmt.Errorf("error getting current plan state: %v", err)
@@ -112,15 +133,15 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
err := <-errCh
if err != nil {
log.Printf("Error building plan %s: %v\n", planId, err)
- active.StreamDoneCh <- err
+ activePlan.StreamDoneCh <- err
return
}
}
onFinishBuild := func() {
log.Println("Build finished.")
- active.StreamCh <- shared.STREAM_FINISHED
- active.StreamDoneCh <- nil
+ activePlan.StreamCh <- shared.STREAM_FINISHED
+ activePlan.StreamDoneCh <- nil
}
onFinishBuildFile := func(filePath string, planRes *db.PlanFileResult) {
@@ -132,7 +153,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
err := db.StorePlanResult(planRes)
if err != nil {
log.Printf("Error storing plan result: %v\n", err)
- active.StreamDoneCh <- err
+ activePlan.StreamDoneCh <- err
return
}
@@ -154,14 +175,13 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
onBuildFileError := func(filePath string, err error) {
log.Printf("Error for file %s: %v\n", filePath, err)
- active.StreamDoneCh <- err
+ activePlan.StreamDoneCh <- err
if err != nil {
log.Printf("Error storing plan error result: %v\n", err)
}
build.Error = err.Error()
- build.ErrorPath = filePath
err = db.SetBuildError(build)
if err != nil {
@@ -174,7 +194,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
log.Printf("Building file %s, numRetry: %d\n", filePath, numRetry)
// get relevant file context (if any)
- contextPart := active.ContextsByPath[filePath]
+ contextPart := activePlan.ContextsByPath[filePath]
var currentState string
currentPlanFile, fileInCurrentPlan := currentPlan.CurrentPlanFiles.Files[filePath]
@@ -204,7 +224,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))
return
}
- active.StreamCh <- string(planTokenCountJson)
+ activePlan.StreamCh <- string(planTokenCountJson)
// new file
planRes := &db.PlanFileResult{
@@ -232,10 +252,10 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
Content: sysPrompt,
}, {
Role: openai.ChatMessageRoleUser,
- Content: active.Prompt,
+ Content: activePlan.Prompt,
}, {
Role: openai.ChatMessageRoleAssistant,
- Content: active.Content,
+ Content: activePlan.CurrentReplyContent,
},
{
Role: openai.ChatMessageRoleUser,
@@ -283,7 +303,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
// ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},
}
- stream, err := model.Client.CreateChatCompletionStream(active.Ctx, modelReq)
+ stream, err := model.Client.CreateChatCompletionStream(activePlan.Ctx, modelReq)
if err != nil {
log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)
@@ -331,7 +351,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
for {
select {
- case <-active.Ctx.Done():
+ case <-activePlan.Ctx.Done():
// The main context was canceled (not the timer)
return
case <-timer.C:
@@ -394,7 +414,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
if !active.BuiltFiles[filePath] {
log.Printf("Stream finished before replacements parsed. File: %s\n", filePath)
log.Println("Buffer:")
- log.Println(active.BuildBuffers[filePath])
+ log.Println(activeBuild.Buffer)
handleErrorRetry(
fmt.Errorf("stream finished before replacements parsed. File: %s", filePath),
@@ -429,16 +449,12 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))
return
}
- active.StreamCh <- string(planTokenCountJson)
+ activePlan.StreamCh <- string(planTokenCountJson)
- var buffer string
- Active.Update(planId, func(ap *types.ActivePlan) {
- ap.BuildBuffers[filePath] += content
- buffer = ap.BuildBuffers[filePath]
- })
+ activeBuild.Buffer += content
var streamed types.StreamedReplacements
- err = json.Unmarshal([]byte(buffer), &streamed)
+ err = json.Unmarshal([]byte(activeBuild.Buffer), &streamed)
if err == nil && len(streamed.Replacements) > 0 {
log.Printf("File %s: Parsed replacements\n", filePath)
@@ -488,7 +504,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))
return
}
- active.StreamCh <- string(planTokenCountJson)
+ activePlan.StreamCh <- string(planTokenCountJson)
onFinishBuildFile(filePath, planFileResult)
return
@@ -498,7 +514,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {
}()
}
- for _, filePath := range active.Files {
+ for _, filePath := range activePlan.Files {
go buildFile(filePath, 0, 0, nil)
}
}
diff --git a/app/server/model/plan/describe.go b/app/server/model/plan/describe.go
index 18df8619..30665142 100644
--- a/app/server/model/plan/describe.go
+++ b/app/server/model/plan/describe.go
@@ -26,7 +26,7 @@ func genPlanDescription(planId string, ctx context.Context) (*db.ConvoMessageDes
},
{
Role: openai.ChatMessageRoleAssistant,
- Content: Active.Get(planId).Content,
+ Content: Active.Get(planId).CurrentReplyContent,
},
},
ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},
diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.go
index 0d939b71..114fec97 100644
--- a/app/server/model/plan/exec_status.go
+++ b/app/server/model/plan/exec_status.go
@@ -6,33 +6,31 @@ import (
"fmt"
"plandex-server/model"
"plandex-server/model/prompts"
+ "plandex-server/types"
"github.com/sashabaranov/go-openai"
)
-type PlanExecStatus struct {
- NeedsInput bool `json:"needs_input"`
- Finished bool `json:"finished"`
-}
-
-func ExecStatus(conversation []openai.ChatCompletionMessage, ctx context.Context) (*PlanExecStatus, error) {
- var res PlanExecStatus
+func ExecStatus(message string, ctx context.Context) (*types.PlanExecStatus, error) {
+ var res types.PlanExecStatus
errCh := make(chan error, 2)
go func() {
- needsInput, err := ExecStatusNeedsInput(conversation, ctx)
+ needsInput, err := ExecStatusNeedsInput(message, ctx)
if err != nil {
errCh <- err
+ return
}
res.NeedsInput = needsInput
errCh <- nil
}()
go func() {
- finished, err := ExecStatusIsFinished(conversation, ctx)
+ finished, err := ExecStatusIsFinished(message, ctx)
if err != nil {
errCh <- err
+ return
}
res.Finished = finished
errCh <- nil
@@ -48,11 +46,11 @@ func ExecStatus(conversation []openai.ChatCompletionMessage, ctx context.Context
return &res, nil
}
-func ExecStatusIsFinished(conversation []openai.ChatCompletionMessage, ctx context.Context) (bool, error) {
+func ExecStatusIsFinished(message string, ctx context.Context) (bool, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
- Content: prompts.GetExecStatusIsFinishedPrompt(conversation),
+ Content: prompts.GetExecStatusIsFinishedPrompt(message),
},
}
@@ -72,7 +70,7 @@ func ExecStatusIsFinished(conversation []openai.ChatCompletionMessage, ctx conte
}
var strRes string
- var res PlanExecStatus
+ var res types.PlanExecStatus
for _, choice := range resp.Choices {
if choice.FinishReason == "function_call" &&
@@ -99,16 +97,14 @@ func ExecStatusIsFinished(conversation []openai.ChatCompletionMessage, ctx conte
return res.Finished, nil
}
-func ExecStatusNeedsInput(conversation []openai.ChatCompletionMessage, ctx context.Context) (bool, error) {
+func ExecStatusNeedsInput(message string, ctx context.Context) (bool, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
- Content: prompts.GetExecStatusNeedsInputPrompt(&conversation[len(conversation)-1]),
+ Content: prompts.GetExecStatusNeedsInputPrompt(message),
},
}
- messages = append(messages, conversation...)
-
resp, err := model.Client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
@@ -125,7 +121,7 @@ func ExecStatusNeedsInput(conversation []openai.ChatCompletionMessage, ctx conte
}
var strRes string
- var res PlanExecStatus
+ var res types.PlanExecStatus
for _, choice := range resp.Choices {
if choice.FinishReason == "function_call" &&
diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.go
index 80191bbf..1861d0b4 100644
--- a/app/server/model/plan/state.go
+++ b/app/server/model/plan/state.go
@@ -13,7 +13,7 @@ var (
)
func CreateActivePlan(planId, prompt string) *types.ActivePlan {
- activePlan := types.NewActivePlan(prompt)
+ activePlan := types.NewActivePlan(planId, prompt)
Active.Set(planId, activePlan)
go func() {
diff --git a/app/server/model/plan/stop.go b/app/server/model/plan/stop.go
index 550fdafc..80fffd6e 100644
--- a/app/server/model/plan/stop.go
+++ b/app/server/model/plan/stop.go
@@ -15,7 +15,7 @@ func Stop(planId, currentUserId, currentOrgId string) error {
return fmt.Errorf("no active plan with id %s", planId)
}
- content := active.Content
+ content := active.CurrentReplyContent
active.CancelFn()
diff --git a/app/server/model/plan/tell.go b/app/server/model/plan/tell.go
index cdfe1840..bb5abd32 100644
--- a/app/server/model/plan/tell.go
+++ b/app/server/model/plan/tell.go
@@ -14,6 +14,7 @@ import (
"plandex-server/types"
"github.com/davecgh/go-spew/spew"
+ "github.com/google/uuid"
"github.com/plandex/plandex/shared"
"github.com/sashabaranov/go-openai"
)
@@ -321,7 +322,10 @@ func execTellPlan(plan *db.Plan, auth *types.ServerAuth, req *shared.TellPlanReq
// log.Printf("%s: %s\n", message.Role, message.Content)
// }
- replyInfo := types.NewReplyInfo()
+ replyId := uuid.New().String()
+ replyParser := types.NewReplyParser()
+ replyFiles := []string{}
+ replyNumTokens := 0
modelReq := openai.ChatCompletionRequest{
Model: model.PlannerModel,
@@ -347,8 +351,6 @@ func execTellPlan(plan *db.Plan, auth *types.ServerAuth, req *shared.TellPlanReq
}
storeAssistantReply := func() (*db.ConvoMessage, []string, string, error) {
- files, _, _, replyNumTokens := replyInfo.FinishAndRead()
-
assistantMsg := db.ConvoMessage{
OrgId: currentOrgId,
PlanId: planId,
@@ -356,21 +358,17 @@ func execTellPlan(plan *db.Plan, auth *types.ServerAuth, req *shared.TellPlanReq
Role: openai.ChatMessageRoleAssistant,
Tokens: replyNumTokens,
Num: len(convo) + 2,
- Message: Active.Get(planId).Content,
+ Message: Active.Get(planId).CurrentReplyContent,
}
commitMsg, err := db.StoreConvoMessage(&assistantMsg, false)
if err != nil {
log.Printf("Error storing assistant message: %v\n", err)
- return nil, files, "", err
+ return nil, replyFiles, "", err
}
- Active.Update(planId, func(active *types.ActivePlan) {
- active.AssistantMessageId = assistantMsg.Id
- })
-
- return &assistantMsg, files, commitMsg, err
+ return &assistantMsg, replyFiles, commitMsg, err
}
onError := func(streamErr error, storeDesc bool, convoMessageId, commitMsg string) {
@@ -489,38 +487,61 @@ func execTellPlan(plan *db.Plan, auth *types.ServerAuth, req *shared.TellPlanReq
}
var description *db.ConvoMessageDescription
-
- if len(files) == 0 {
- description = &db.ConvoMessageDescription{
- OrgId: currentOrgId,
- PlanId: planId,
- ConvoMessageId: assistantMsg.Id,
- SummarizedToMessageId: summarizedToMessageId,
- MadePlan: false,
+ var execStatus *types.PlanExecStatus
+
+ errCh := make(chan error, 2)
+
+ go func() {
+ if len(files) == 0 {
+ description = &db.ConvoMessageDescription{
+ OrgId: currentOrgId,
+ PlanId: planId,
+ ConvoMessageId: assistantMsg.Id,
+ SummarizedToMessageId: summarizedToMessageId,
+ MadePlan: false,
+ }
+ } else {
+ description, err = genPlanDescription(planId, active.Ctx)
+ if err != nil {
+ onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)
+ return
+ }
+
+ description.OrgId = currentOrgId
+ description.ConvoMessageId = assistantMsg.Id
+ description.SummarizedToMessageId = summarizedToMessageId
+ description.MadePlan = true
+ description.Files = files
}
- } else {
- Active.Update(planId, func(ap *types.ActivePlan) {
- ap.Files = files
- })
- description, err = genPlanDescription(planId, active.Ctx)
+ err = db.StoreDescription(description)
+
if err != nil {
- onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)
+ onError(fmt.Errorf("failed to store description: %v", err), false, assistantMsg.Id, convoCommitMsg)
+ errCh <- err
return
}
- description.OrgId = currentOrgId
- description.ConvoMessageId = assistantMsg.Id
- description.SummarizedToMessageId = summarizedToMessageId
- description.MadePlan = true
- description.Files = files
- }
+ errCh <- nil
+ }()
- err = db.StoreDescription(description)
+ go func() {
- if err != nil {
- onError(fmt.Errorf("failed to store description: %v", err), false, assistantMsg.Id, convoCommitMsg)
- return
+ execStatus, err = ExecStatus(assistantMsg.Message, active.Ctx)
+ if err != nil {
+ onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)
+ errCh <- err
+ return
+ }
+
+ errCh <- nil
+ }()
+
+ for i := 0; i < 2; i++ {
+ err := <-errCh
+ if err != nil {
+ return
+ }
}
err = db.GitAddAndCommit(currentOrgId, planId, convoCommitMsg)
@@ -529,26 +550,48 @@ func execTellPlan(plan *db.Plan, auth *types.ServerAuth, req *shared.TellPlanReq
return
}
- if len(files) == 0 {
+ if !req.AutoContinue || execStatus.Finished || execStatus.NeedsInput {
active.StreamCh <- shared.STREAM_FINISHED
active.StreamDoneCh <- nil
+
+ return
} else {
- active.StreamCh <- shared.STREAM_BUILD_PHASE
- Build(currentOrgId, planId)
+
+ // continue plan
+
}
- return
+
}
delta := choice.Delta
content := delta.Content
Active.Update(planId, func(active *types.ActivePlan) {
- active.Content += content
+ active.CurrentReplyContent += content
active.NumTokens++
})
// log.Printf("%s", content)
active.StreamCh <- content
- replyInfo.AddToken(content, true)
+ replyParser.AddToken(content, true)
+
+ files, _, _, numTokens := replyParser.Read()
+ replyNumTokens = numTokens
+ if len(files) > len(replyFiles) {
+ latestFile := files[len(files)-1]
+
+ Active.Update(planId, func(active *types.ActivePlan) {
+ active.Files = files
+ })
+
+ log.Printf("Queuing build for %s\n", latestFile)
+ QueueBuild(currentOrgId, planId, &types.ActiveBuild{
+ AssistantMessageId: replyId,
+ ReplyContent: content,
+ Path: latestFile,
+ })
+
+ replyFiles = files
+ }
}
}
@@ -622,7 +665,7 @@ func summarizeConvo(params summarizeConvoParams) error {
summaryMessages = append(summaryMessages, promptMessage, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
- Content: Active.Get(planId).Content,
+ Content: Active.Get(planId).CurrentReplyContent,
})
summary, err := model.PlanSummary(model.PlanSummaryParams{
diff --git a/app/server/model/prompts/exec_status.go b/app/server/model/prompts/exec_status.go
index eb9e36fd..06f9db5b 100644
--- a/app/server/model/prompts/exec_status.go
+++ b/app/server/model/prompts/exec_status.go
@@ -11,14 +11,8 @@ The plan is finished if all the plan's tasks and subtasks have been completed. W
Return a JSON object with the 'finished' key set to true or false. Only call the 'planIsFinished' function in your response. Don't call any other function.`
-func GetExecStatusIsFinishedPrompt(conversation []openai.ChatCompletionMessage) string {
- s := ""
- for _, m := range conversation {
- s += m.Role + ":\n"
- s += m.Content + "\n"
- }
-
- return SysExecStatusIsFinished + "\n\nConversation:\n" + s
+func GetExecStatusIsFinishedPrompt(message string) string {
+ return SysExecStatusIsFinished + "\n\nLatest message from coding AI:\n" + message
}
var PlanIsFinishedFn = openai.FunctionDefinition{
@@ -39,10 +33,10 @@ const SysExecStatusNeedsInput = `You are an AI assistant that determines the exe
When the coding AI needs more input, it will say something like "I need more information or context to make a plan for this task."
-If the coding AI says or implies that additional information would be helpful or useful, but that information isn't *required* to continue the plan, then the plan *does not* need more input. It only needs more input if the AI says or implies that more information is necessary and required to continue. Return a JSON object with the 'needs_input' key set to true or false. Only call the 'planNeedsInput' function in your response. Don't call any other function.`
+If the coding AI says or implies that additional information would be helpful or useful, but that information isn't *required* to continue the plan, then the plan *does not* need more input. It only needs more input if the AI says or implies that more information is necessary and required to continue. Return a JSON object with the 'needsInput' key set to true or false. Only call the 'planNeedsInput' function in your response. Don't call any other function.`
-func GetExecStatusNeedsInputPrompt(message *openai.ChatCompletionMessage) string {
- return SysExecStatusNeedsInput + "\nLatest message from coding AI:\n" + message.Content
+func GetExecStatusNeedsInputPrompt(message string) string {
+ return SysExecStatusNeedsInput + "\nLatest message from coding AI:\n" + message
}
var PlanNeedsInputFn = openai.FunctionDefinition{
@@ -50,11 +44,11 @@ var PlanNeedsInputFn = openai.FunctionDefinition{
Parameters: &jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
- "needs_input": {
+ "needsInput": {
Type: jsonschema.Boolean,
Description: "Whether the plan needs more input. If ambiguous or unclear, assume the plan does not need more input.",
},
},
- Required: []string{"needs_input"},
+ Required: []string{"needsInput"},
},
}
diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.go
index b635064f..2d13db6a 100644
--- a/app/server/types/active_plan.go
+++ b/app/server/types/active_plan.go
@@ -9,40 +9,47 @@ import (
type ActiveBuild struct {
AssistantMessageId string
- Files []string
- BuildBuffers map[string]string
- BuiltFiles map[string]bool
+ ReplyContent string
+ Path string
+ Buffer string
+ Success bool
Error error
ErrorReason string
}
type ActivePlan struct {
- Prompt string
- StreamCh chan string
- StreamDoneCh chan error
- Ctx context.Context
- CancelFn context.CancelFunc
- Contexts []*db.Context
- ContextsByPath map[string]*db.Context
- Content string
- NumTokens int
- PromptMessageNum int
- BuildQueuesByPath map[string]*[]ActiveBuild
- subscriptions map[string]chan string
+ Id string
+ Prompt string
+ StreamCh chan string
+ StreamDoneCh chan error
+ Ctx context.Context
+ CancelFn context.CancelFunc
+ Contexts []*db.Context
+ ContextsByPath map[string]*db.Context
+ Files []string
+ BuiltFiles map[string]bool
+ CurrentReplyContent string
+ NumTokens int
+ PromptMessageNum int
+ BuildQueuesByPath map[string][]*ActiveBuild
+ subscriptions map[string]chan string
}
-func NewActivePlan(prompt string) *ActivePlan {
+func NewActivePlan(planId, prompt string) *ActivePlan {
ctx, cancel := context.WithCancel(context.Background())
active := ActivePlan{
+ Id: planId,
Prompt: prompt,
StreamCh: make(chan string),
StreamDoneCh: make(chan error),
Ctx: ctx,
CancelFn: cancel,
- BuildQueuesByPath: map[string]*[]ActiveBuild{},
+ BuildQueuesByPath: map[string][]*ActiveBuild{},
Contexts: []*db.Context{},
ContextsByPath: map[string]*db.Context{},
+ Files: []string{},
+ BuiltFiles: map[string]bool{},
subscriptions: map[string]chan string{},
}
@@ -63,7 +70,25 @@ func NewActivePlan(prompt string) *ActivePlan {
}
func (b *ActiveBuild) BuildFinished() bool {
- return len(b.Files) == len(b.BuiltFiles)
+ return b.Success || b.Error != nil
+}
+
+func (ap *ActivePlan) BuildFinished() bool {
+ for path := range ap.BuildQueuesByPath {
+ if !ap.PathFinished(path) {
+ return false
+ }
+ }
+ return true
+}
+
+func (ap *ActivePlan) PathFinished(path string) bool {
+ for _, build := range ap.BuildQueuesByPath[path] {
+ if !build.BuildFinished() {
+ return false
+ }
+ }
+ return true
}
func (ap *ActivePlan) Subscribe() (string, chan string) {
diff --git a/app/server/types/reply.go b/app/server/types/reply.go
index f04b5b7e..ea557c9f 100644
--- a/app/server/types/reply.go
+++ b/app/server/types/reply.go
@@ -4,7 +4,7 @@ import (
"strings"
)
-type replyInfo struct {
+type replyParser struct {
lines []string
currentFileLines []string
lineIndex int
@@ -16,8 +16,8 @@ type replyInfo struct {
numTokensByFile map[string]int
}
-func NewReplyInfo() *replyInfo {
- info := &replyInfo{
+func NewReplyParser() *replyParser {
+ info := &replyParser{
lines: []string{""},
currentFileLines: []string{},
files: make(map[string]bool),
@@ -28,7 +28,7 @@ func NewReplyInfo() *replyInfo {
return info
}
-func (r *replyInfo) AddToken(token string, addToTotal bool) {
+func (r *replyParser) AddToken(token string, addToTotal bool) {
// fmt.Println("Adding chunk:", strconv.Quote(chunk)) // Logging the chunk that's being processed
hasNewLine := false
@@ -138,10 +138,10 @@ func (r *replyInfo) AddToken(token string, addToTotal bool) {
}
}
-func (r *replyInfo) FinishAndRead() ([]string, map[string]string, map[string]int, int) {
+func (r *replyParser) Read() (files []string, fileContents map[string]string, numTokensByFile map[string]int, totalTokens int) {
r.AddToken("\n", false)
- files := make([]string, 0, len(r.files))
+ files = make([]string, 0, len(r.files))
for file := range r.files {
files = append(files, file)
}
diff --git a/app/server/types/reply_test.go b/app/server/types/reply_test.go
index 4d51aea7..e1e07fc0 100644
--- a/app/server/types/reply_test.go
+++ b/app/server/types/reply_test.go
@@ -71,7 +71,7 @@ func TestReplyTokenCounter(t *testing.T) {
tokenSize := 5
- counter := NewReplyInfo()
+ counter := NewReplyParser()
totalTokens := 0
for i := 0; i < len(content); {
@@ -85,7 +85,7 @@ func TestReplyTokenCounter(t *testing.T) {
i = end
}
- files, fileContents, tokensByFilePath, totalCounted := counter.FinishAndRead()
+ files, fileContents, tokensByFilePath, totalCounted := counter.Read()
fmt.Printf("Total tokens counted: %d\n", totalCounted)
fmt.Printf("%d files: %v\n", len(files), files)
diff --git a/app/shared/models.go b/app/shared/models.go
index 2f1b34a5..bc1a911b 100644
--- a/app/shared/models.go
+++ b/app/shared/models.go
@@ -122,8 +122,8 @@ type ConvoMessageDescription struct {
type PlanBuild struct {
Id string `json:"id"`
ConvoMessageId string `json:"convoMessageId"`
+ FilePath string `json:"filePath"`
Error string `json:"error"`
- ErrorPath string `json:"errorPath"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
diff --git a/app/shared/req_res.go b/app/shared/req_res.go
index 81298e3f..c454e240 100644
--- a/app/shared/req_res.go
+++ b/app/shared/req_res.go
@@ -88,6 +88,7 @@ type CreatePlanResponse struct {
type TellPlanRequest struct {
Prompt string `json:"prompt"`
ConnectStream bool `json:"connectStream"`
+ AutoContinue bool `json:"autoContinue"`
}
type LoadContextParams struct {
@@ -138,9 +139,3 @@ type LogResponse struct {
Shas []string `json:"shas"`
Body string `json:"body"`
}
-
-type PlanTokenCount struct {
- Path string `json:"path"`
- NumTokens int `json:"numTokens"`
- Finished bool `json:"finished"`
-}
diff --git a/app/shared/stream.go b/app/shared/stream.go
index d2fe3888..087551d8 100644
--- a/app/shared/stream.go
+++ b/app/shared/stream.go
@@ -5,19 +5,18 @@ import "github.com/looplab/fsm"
const STREAM_MESSAGE_SEPARATOR = "@@PX@@"
const STREAM_FINISHED = "@@PXEND@@"
const STREAM_DESCRIPTION_PHASE = "@@PXDESC@@"
-const STREAM_BUILD_PHASE = "@@PXBUILD@@"
+const STREAM_RESUME = "@@PXRESUME@@"
const STREAM_ABORTED = "@@PXABORT@@"
const EVENT_DESCRIBE = "describe"
-const EVENT_BUILD = "build"
const EVENT_FINISH = "finish"
+const EVENT_RESUME = "resume"
const EVENT_ABORT = "abort"
const EVENT_CANCEL = "cancel"
const EVENT_ERROR = "error"
const STATE_REPLYING = "replying"
const STATE_DESCRIBING = "describing"
-const STATE_BUILDING = "building"
const STATE_FINISHED = "finished"
const STATE_ABORTED = "aborted"
const STATE_CANCELED = "canceled"
@@ -29,15 +28,25 @@ func NewPlanStreamState() *fsm.FSM {
fsm.Events{
// Define state transitions
{Name: EVENT_DESCRIBE, Src: []string{STATE_REPLYING}, Dst: STATE_DESCRIBING},
- {Name: EVENT_BUILD, Src: []string{STATE_DESCRIBING}, Dst: STATE_BUILDING},
- {Name: EVENT_FINISH, Src: []string{STATE_DESCRIBING, STATE_BUILDING}, Dst: STATE_FINISHED},
- {Name: EVENT_ABORT, Src: []string{STATE_REPLYING, STATE_DESCRIBING, STATE_BUILDING},
- Dst: STATE_ABORTED},
+
+ {Name: EVENT_RESUME, Src: []string{STATE_DESCRIBING}, Dst: STATE_REPLYING},
+
+ {Name: EVENT_FINISH, Src: []string{STATE_DESCRIBING}, Dst: STATE_FINISHED},
+
+ {Name: EVENT_ABORT, Src: []string{STATE_REPLYING, STATE_DESCRIBING}, Dst: STATE_ABORTED},
+
{Name: EVENT_CANCEL, Src: []string{STATE_ABORTED}, Dst: STATE_CANCELED},
- {Name: EVENT_ERROR, Src: []string{STATE_REPLYING, STATE_DESCRIBING, STATE_BUILDING}},
+
+ {Name: EVENT_ERROR, Src: []string{STATE_REPLYING, STATE_DESCRIBING}},
},
fsm.Callbacks{},
)
return sm
}
+
+type PlanTokenCount struct {
+ Path string `json:"path"`
+ NumTokens int `json:"numTokens"`
+ Finished bool `json:"finished"`
+}