Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
- batch stream messages to fix UI flickering issue - prompt tweaks for placeholder comments
Dane Schneiderc127bf2620a9
diff --git a/app/cli/stream_tui/update.go b/app/cli/stream_tui/update.goindex cd0f1eb8..4ea658b8 100644--- a/app/cli/stream_tui/update.go+++ b/app/cli/stream_tui/update.go@@ -42,7 +42,7 @@ func (m streamUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {m.windowResized(msg.Width, msg.Height)case shared.StreamMessage:- return m.streamUpdate(&msg)+ return m.streamUpdate(&msg, false)case delayFileRestartMsg:m.finishedByPath[msg.path] = false@@ -231,8 +231,7 @@ func (m *streamUIModel) scrollEnd() {}}-func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage) (tea.Model, tea.Cmd) {-+func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage, deferUIUpdate bool) (tea.Model, tea.Cmd) {checkMissingFileFn := func() {if msg.MissingFilePath != "" {m.promptingMissingFile = true@@ -259,8 +258,26 @@ func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage) (tea.Model, tea.}// log.Println("streamUI received message:", msg.Type)+switch msg.Type {+ case shared.StreamMessageMulti:+ cmds := []tea.Cmd{}+ for _, subMsg := range msg.StreamMessages {+ teaModel, cmd := m.streamUpdate(&subMsg, true)++ m = teaModel.(*streamUIModel)++ if cmd != nil {+ cmds = append(cmds, cmd)+ }+ }++ m.updateReplyDisplay()+ m.updateViewportDimensions()++ return m, tea.Batch(cmds...)+case shared.StreamMessageConnectActive:if msg.InitPrompt != "" {@@ -270,7 +287,7 @@ func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage) (tea.Model, tea.m.buildOnly = true}if len(msg.InitReplies) > 0 {- m.reply = strings.Join(msg.InitReplies, "\n\nπ ")+ m.reply = strings.Join(msg.InitReplies, "\n\nπ\n")}m.updateReplyDisplay()@@ -289,14 +306,17 @@ func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage) (tea.Model, tea.if m.promptedMissingFile {m.promptedMissingFile = false} else {- m.reply += "\n\nπ "+ m.reply += "\n\nπ\n"}}// log.Println("reply chunk:", msg.ReplyChunk)m.reply += msg.ReplyChunk- m.updateReplyDisplay()++ if !deferUIUpdate {+ m.updateReplyDisplay()+ }case shared.StreamMessageBuildInfo:if m.starting {@@ -321,7 +341,9 @@ func (m *streamUIModel) streamUpdate(msg *shared.StreamMessage) (tea.Model, tea.m.tokensByPath[msg.BuildInfo.Path] += msg.BuildInfo.NumTokens}- m.updateViewportDimensions()+ if !deferUIUpdate {+ m.updateViewportDimensions()+ }cmds := []tea.Cmd{m.buildSpinner.Tick}if m.processing && !m.finished {diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.goindex 9572d4f4..4cd1234c 100644--- a/app/server/model/plan/tell_stream.go+++ b/app/server/model/plan/tell_stream.go@@ -16,6 +16,8 @@ import ()const MaxAutoContinueIterations = 100+const MaxSendRate = 30 * time.Millisecond+const MaxTellStreamRetries = 4func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStream) {defer stream.Close()@@ -80,8 +82,6 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStreturn}- state.onError(fmt.Errorf("stream error: %v", err), true, "", "")- return}if len(response.Choices) == 0 {@@ -98,10 +98,13 @@ func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStif choice.FinishReason != "" {log.Println("Model stream finished")+ active.FlushStreamBuffer()+ time.Sleep(100 * time.Millisecond)active.Stream(shared.StreamMessage{Type: shared.StreamMessageDescribing,})+ active.FlushStreamBuffer()err := db.SetPlanStatus(planId, branch, shared.PlanStatusDescribing, "")if err != nil {@@ -570,7 +573,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, conreturn}- storeDescAndReply := func() {+ storeDescAndReply := func() error {ctx, cancelFn := context.WithCancel(context.Background())repoLockId, err := db.LockRepo(@@ -587,6 +590,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, conif err != nil {log.Printf("Error locking repo for plan %s: %v\n", planId, err)+ return err} else {defer func() {@@ -599,6 +603,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, conerr := db.GitClearUncommittedChanges(state.currentOrgId, planId)if err != nil {log.Printf("Error clearing uncommitted changes for plan %s: %v\n", planId, err)+ return err}}@@ -613,6 +618,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, constoredMessage = true} else {log.Printf("Error storing assistant message after stream error: %v\n", err)+ return err}}@@ -630,6 +636,7 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, constoredDesc = true} else {log.Printf("Error storing description after stream error: %v\n", err)+ return err}}@@ -637,8 +644,11 @@ func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, conerr := db.GitAddAndCommit(currentOrgId, planId, branch, commitMsg)if err != nil {log.Printf("Error committing after stream error: %v\n", err)+ return err}}++ return nil}storeDescAndReply()diff --git a/app/server/model/prompts/build.go b/app/server/model/prompts/build.goindex 53045a35..31400af4 100644--- a/app/server/model/prompts/build.go+++ b/app/server/model/prompts/build.go@@ -152,7 +152,7 @@ The 'listChangesWithLineNums' function MUST be called *valid JSON*. Double quote`const commentsPrompt = `-The 'comments' key is an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.+The 'comments' key is an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." or "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.In 'comments', you must list EVERY comment included in the proposed updates. Only list *code comments* that are valid comments for the programming language being used. Do not list logging statements or any other non-comment text that is not a valid code comment. If there are no code comments in the proposed updates, 'comments' must be an empty array.@@ -395,7 +395,7 @@ Based on supplied diffs, you must list EVERY code section that was removed or ov'hasDuplicationErrors': A boolean that indicates whether any code was *incorrectly* duplicated in the updated file, based on the reasoning provided in 'duplicationErrorsReasoning'. If code was *incorrectly* duplicated, set 'hasDuplicationErrors' to true. If code was *correctly* duplicated, consistent with the intention of the plan, set 'hasDuplicationErrors' to false.-'comments': an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.+'comments': an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.In 'comments', you must list EVERY comment included in the *updated file*. Only list *code comments* that are valid comments for the programming language being used. Do not list logging statements or any other non-comment text that is not a valid code comment. If there are no code comments in the *updated file*, 'comments' must be an empty array.diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.goindex 66fb228e..6a30f769 100644--- a/app/server/model/prompts/create.go+++ b/app/server/model/prompts/create.go@@ -85,13 +85,13 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextAn exception to the above instructions on comments are if a file block is empty because you removed everything in it. In that case, leave a brief one-line comment starting with 'Plandex: removed' that says what was removed so that the file block isn't empty.- In code blocks, include the *minimum amount of code* necessary to describe the suggested changes. Include only lines that are changing and lines that make it clear where the change should be applied. You can use comments like "// rest of the function..." or "// rest of the file..." to help make it clear where changes should be applied. You *must not* include large sections of the original file unless it helps make the suggested changes clear.+ In code blocks, include the *minimum amount of code* necessary to describe the suggested changes. Include only lines that are changing and lines that make it clear where the change should be applied. You can use the comment "// ... existing code ..." (adapted for the programming language) instead of including large sections from the original file in context in order to make it clear where changes should be applied. You *must not* include large sections of the original file unless it is absolutely necessary to make the suggested changes clear. Instead show only the code that is changing and the immediately surrounding code that is necessary to understand the changes. Use the comment "// ... existing code ..." (adapted for the programming language) to replace sections of code from the original file and denote where the existing code should be placed.Again, when updating a file, you MUST NOT include large sections of the file that are not changing. Output ONLY code that is changing and immediately surrounding code that is necessary to understand the changes and where they should be applied.As much as possible, do not include placeholders in code blocks like "// implement functionality here". Unless you absolutely cannot implement the full code block, do not include a placeholder denoted with comments. Do your best to implement the functionality rather than inserting a placeholder. You **MUST NOT** include placeholders just to shorten the code block. If the task is too large to implement in a single code block, you should break the task down into smaller steps and **FULLY** implement each step.- If you are outputting some code for illustraive or explanatory purpose and not because you are updating that code, you MUST NOT use a labelled file block. Instead output the label with NO PRECEDING DASH and NO COLON postfix. Use a conversational sentence like 'This code in src/main.rs.' to label the code. This is the only exception to the rule that all code blocks must be labelled with a file path. Labelled code blocks are ONLY for code that is being created or modified in the plan.+ If you are outputting some code for illustrative or explanatory purpose and not because you are updating that code, you MUST NOT use a labelled file block. Instead output the label with NO PRECEDING DASH and NO COLON postfix. Use a conversational sentence like 'This code in src/main.rs.' to label the code. This is the only exception to the rule that all code blocks must be labelled with a file path. Labelled code blocks are ONLY for code that is being created or modified in the plan.As much as possible, the code you suggest should be robust, complete, and ready for production.@@ -119,7 +119,7 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextNext, describe the subtask and what your approach will be, then implement it with code blocks. Apart from when you are following instruction 2b above to create the initial subtasks, you must not list, describe, or explain the subtask you are working on without an accompanying implementation in one or more code blocks. Describing what needs to be done to complete a subtask *DOES NOT* count as completing the subtask. It must be fully implemented with code blocks.- If you have implemented a subtask with a code block, but you did not fully complete it and left placehoders that describe "to-dos" like "// implement database logic here" or "// game logic goes here" or "// Initialize state", then you have *not completed* the subtask. You MUST *IMMEDIATELY* continue working on the subtask and replace the placeholders with a *FULL IMPLEMENTION* in code, even if doing so requires multiple code blocks and responses. You MUST NOT leave placeholders in the code blocks.+ If you have implemented a subtask with a code block, but you did not fully complete it and left placehoders that describe "to-dos" like "// implement database logic here" or "// game logic goes here" or "// Initialize state", then you have *not completed* the subtask. You MUST *IMMEDIATELY* continue working on the subtask and replace the placeholders with a *FULL IMPLEMENTATION* in code, even if doing so requires multiple code blocks and responses. You MUST NOT leave placeholders in the code blocks.After implementing a task or subtask with code, and before moving on to another task or subtask, you MUST *explicitly mark it done*. You can do this by explicitly stating "[subtask] has been completed". For example, "**Adding the update function** has been completed." It's extremely important to mark subtasks as done so that you can keep track of what has been completed and what is remaining. Never move on to a new subtask. Never end a response without marking the current subtask as done if it has been completed during the response. You MUST NOT omit marking a subtask as done when it has been completed.diff --git a/app/server/types/active_plan.go b/app/server/types/active_plan.goindex 27feaa2b..1d97d957 100644--- a/app/server/types/active_plan.go+++ b/app/server/types/active_plan.go@@ -13,6 +13,8 @@ import ("github.com/plandex/plandex/shared")+const MaxStreamRate = 50 * time.Millisecond+// const MaxConcurrentBuildStreams = 3 // otherwise we get EOF errors from openaitype ActiveBuild struct {@@ -77,9 +79,14 @@ type ActivePlan struct {AllowOverwritePaths map[string]boolSkippedPaths map[string]boolStoredReplyIds []string- streamCh chan string- subscriptions map[string]*subscription- subscriptionMu sync.Mutex++ subscriptions map[string]*subscription+ subscriptionMu sync.Mutex++ streamCh chan string+ streamMu sync.Mutex+ lastStreamMessageSent time.Time+ streamMessageBuffer []shared.StreamMessage}func NewActivePlan(orgId, userId, planId, branch, prompt string, buildOnly bool) *ActivePlan {@@ -145,7 +152,53 @@ func NewActivePlan(orgId, userId, planId, branch, prompt string, buildOnly bool)return &active}+func (ap *ActivePlan) FlushStreamBuffer() {+ // log.Println("ActivePlan: flush stream buffer")++ ap.streamMu.Lock()+ if len(ap.streamMessageBuffer) == 0 {+ // log.Println("ActivePlan: stream buffer empty")+ ap.streamMu.Unlock()+ return+ }++ // log.Printf("ActivePlan: flushing %d messages from stream buffer\n", len(ap.streamMessageBuffer))++ // construct a multi message from the buffer+ multiMsg := shared.StreamMessage{+ Type: shared.StreamMessageMulti,+ StreamMessages: ap.streamMessageBuffer,+ }+ ap.streamMessageBuffer = []shared.StreamMessage{}++ ap.streamMu.Unlock()++ ap.Stream(multiMsg)+}+func (ap *ActivePlan) Stream(msg shared.StreamMessage) {+ // log.Printf("ActivePlan: received Stream message: %v\n", msg)++ ap.streamMu.Lock()+ defer ap.streamMu.Unlock()++ if msg.Type != shared.StreamMessageFinished {+ if time.Since(ap.lastStreamMessageSent) < MaxStreamRate {+ // log.Println("ActivePlan: stream rate limiting -- buffering message")+ ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)+ return+ } else if len(ap.streamMessageBuffer) > 0 {+ // log.Println("ActivePlan: stream buffer not empty -- flushing buffer before sending message")+ ap.streamMessageBuffer = append(ap.streamMessageBuffer, msg)++ // unlock before recursive call+ ap.streamMu.Unlock()+ ap.FlushStreamBuffer()+ ap.streamMu.Lock() // re-lock after recursive call+ return+ }+ }+msgJson, err := json.Marshal(msg)if err != nil {ap.StreamDoneCh <- &shared.ApiError{@@ -158,10 +211,34 @@ func (ap *ActivePlan) Stream(msg shared.StreamMessage) {// log.Printf("ActivePlan: sending stream message: %s\n", string(msgJson))+ if msg.Type == shared.StreamMessageFinished {+ // send full buffer if we got a finished message+ if len(ap.streamMessageBuffer) > 0 {+ // log.Println("ActivePlan: finished message -- sending stream buffer first")++ // unlock before recursive call+ ap.streamMu.Unlock()+ ap.FlushStreamBuffer()+ ap.streamMu.Lock() // re-lock after recursive call++ // sleep a little before the final message+ time.Sleep(50 * time.Millisecond)+ }+ }++ // log.Println("ActivePlan: sending stream message")+ap.streamCh <- string(msgJson)+ // log.Println("ActivePlan: sent stream message")++ now := time.Now()+ if now.After(ap.lastStreamMessageSent) {+ ap.lastStreamMessageSent = now+ }+if msg.Type == shared.StreamMessageFinished {- // Wait briefly allow last stream message to be sent+ // Wait briefly to allow last stream message to be senttime.Sleep(100 * time.Millisecond)ap.StreamDoneCh <- nil}diff --git a/app/shared/stream.go b/app/shared/stream.goindex 5a3dc2e1..b512064d 100644--- a/app/shared/stream.go+++ b/app/shared/stream.go@@ -21,6 +21,8 @@ const (StreamMessageAborted StreamMessageType = "aborted"StreamMessageFinished StreamMessageType = "finished"StreamMessageError StreamMessageType = "error"++ StreamMessageMulti StreamMessageType = "multi")type StreamMessage struct {@@ -37,4 +39,6 @@ type StreamMessage struct {InitPrompt string `json:"initPrompt,omitempty"`InitReplies []string `json:"initReplies,omitempty"`InitBuildOnly bool `json:"initBuildOnly,omitempty"`++ StreamMessages []StreamMessage `json:"streamMessages,omitempty"`}