Skip to content

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

Improve model handling with better error recovery

Dane Schneidere11b868cfba1
diff --git a/app/server/model/client_stream.go b/app/server/model/client_stream.go
index 174b4e24..082d8a2a 100644
--- a/app/server/model/client_stream.go
+++ b/app/server/model/client_stream.go
@@ -116,44 +116,50 @@ func processChatCompletionStream(
return accumulator.Result(false, nil), nil
}
- if len(response.Choices) == 0 {
- log.Println("No choices in response")
- err := fmt.Errorf("no choices in response")
- return accumulator.Result(false, err), err
- }
+ emptyChoices := false
+ var content string
- if len(response.Choices) > 1 {
- err = fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response.")
- return accumulator.Result(true, err), err
+ if len(response.Choices) == 0 {
+ // Previously we'd return an error if there were no choices, but some models do this and then keep streaming, so we'll just log it and continue
+ log.Println("processChatCompletionStream - no choices in response")
+ // err := fmt.Errorf("no choices in response")
+ // return accumulator.Result(false, err), err
+ emptyChoices = true
}
- choice := response.Choices[0]
-
- if choice.FinishReason != "" {
- if choice.FinishReason == "error" {
- err = fmt.Errorf("model stopped with error status | The model is not responding.")
- return accumulator.Result(true, err), err
- } else {
- // Reset the timer for the usage chunk
- if !timer.Stop() {
- <-timer.C
+ // We'll be more accepting of multiple choices and just take the first one
+ // if len(response.Choices) > 1 {
+ // err = fmt.Errorf("stream finished with more than one choice | The model failed to generate a valid response.")
+ // return accumulator.Result(true, err), err
+ // }
+
+ if !emptyChoices {
+ choice := response.Choices[0]
+
+ if choice.FinishReason != "" {
+ if choice.FinishReason == "error" {
+ err = fmt.Errorf("model stopped with error status | The model is not responding.")
+ return accumulator.Result(true, err), err
+ } else {
+ // Reset the timer for the usage chunk
+ if !timer.Stop() {
+ <-timer.C
+ }
+ timer.Reset(OPENAI_USAGE_CHUNK_TIMEOUT)
+ streamFinished = true
+ continue
}
- timer.Reset(OPENAI_USAGE_CHUNK_TIMEOUT)
- streamFinished = true
- continue
}
- }
- var content string
-
- if req.Tools != nil {
- if choice.Delta.ToolCalls != nil {
- toolCall := choice.Delta.ToolCalls[0]
- content = toolCall.Function.Arguments
- }
- } else {
- if choice.Delta.Content != "" {
- content = choice.Delta.Content
+ if req.Tools != nil {
+ if choice.Delta.ToolCalls != nil {
+ toolCall := choice.Delta.ToolCalls[0]
+ content = toolCall.Function.Arguments
+ }
+ } else {
+ if choice.Delta.Content != "" {
+ content = choice.Delta.Content
+ }
}
}
diff --git a/app/server/model/plan/build_finish.go b/app/server/model/plan/build_finish.go
index a5898910..8b630361 100644
--- a/app/server/model/plan/build_finish.go
+++ b/app/server/model/plan/build_finish.go
@@ -1,6 +1,7 @@
package plan
import (
+ "context"
"fmt"
"log"
"net/http"
@@ -140,10 +141,13 @@ func (state *activeBuildStreamFileState) onFinishBuild() {
if err != nil {
log.Printf("Error finishing build: %v\n", err)
- ap.StreamDoneCh <- &shared.ApiError{
- Type: shared.ApiErrorTypeOther,
- Status: http.StatusInternalServerError,
- Msg: "Error finishing build: " + err.Error(),
+
+ if err.Error() != context.Canceled.Error() {
+ ap.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error finishing build: " + err.Error(),
+ }
}
return
}
diff --git a/app/server/model/plan/state.go b/app/server/model/plan/state.go
index aa5db029..ff2799f4 100644
--- a/app/server/model/plan/state.go
+++ b/app/server/model/plan/state.go
@@ -4,6 +4,7 @@ import (
"context"
"log"
"plandex-server/db"
+ "plandex-server/shutdown"
"plandex-server/types"
"strings"
"time"
@@ -51,6 +52,11 @@ func CreateActivePlan(orgId, userId, planId, branch, prompt string, buildOnly, a
log.Printf("Error setting plan %s status to ready: %v\n", planId, err)
}
+ // cancel *after* the DeleteActivePlan call
+ // allows queued operations to complete
+ DeleteActivePlan(orgId, userId, planId, branch)
+ activePlan.CancelFn()
+ return
} else {
log.Printf("Error streaming plan %s: %v\n", planId, apiErr)
@@ -71,12 +77,14 @@ func CreateActivePlan(orgId, userId, planId, branch, prompt string, buildOnly, a
log.Println("Waiting 100ms after streaming error before canceling active plan")
time.Sleep(100 * time.Millisecond)
+
+ // cancel *before* the DeleteActivePlan call below
+ // short circuits any active operations
log.Println("Cancelling active plan")
+ activePlan.CancelFn()
+ DeleteActivePlan(orgId, userId, planId, branch)
+ return
}
-
- activePlan.CancelFn()
- DeleteActivePlan(orgId, userId, planId, branch)
- return
}
}
}()
@@ -93,7 +101,7 @@ func DeleteActivePlan(orgId, userId, planId, branch string) {
return
}
- ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
+ ctx, cancelFn := context.WithTimeout(shutdown.ShutdownCtx, 10*time.Second)
defer cancelFn()
log.Printf("Clearing uncommitted changes for plan %s - %s - %s\n", planId, branch, orgId)