Skip to content

Commit

Permalink
fix for potential panic when updating model settings. might possibly …
Browse files Browse the repository at this point in the history
…be the cause of or somehow related to #121 but hard to be sure.
  • Loading branch information
danenania committed May 16, 2024
1 parent 3f1314f commit 6d13da3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
53 changes: 21 additions & 32 deletions app/server/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"plandex-server/db"
"reflect"
"strings"

"github.com/gorilla/mux"
"github.com/plandex/plandex/shared"
Expand Down Expand Up @@ -303,33 +302,29 @@ 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
}
aVal, bVal := reflect.ValueOf(a), reflect.ValueOf(b)

if reflect.DeepEqual(a, b) {
// Check if either value is invalid and return to prevent panic
if !aVal.IsValid() || !bVal.IsValid() {
return
}

aVal, bVal := reflect.ValueOf(a), reflect.ValueOf(b)
if aVal.Kind() == reflect.Ptr {
// If both values are pointers, get the elements they point to
if aVal.Kind() == reflect.Ptr && bVal.Kind() == reflect.Ptr {
aVal = aVal.Elem()
}
if bVal.Kind() == reflect.Ptr {
bVal = bVal.Elem()
}

// log.Println("Comparing", path, aVal.Kind(), bVal.Kind())
// log.Println("aVal", aVal)
// log.Println("bVal", bVal)
// Check again for validity after dereferencing pointers
if !aVal.IsValid() || !bVal.IsValid() {
return
}

if reflect.DeepEqual(a, b) {
return // No difference found
}

// Continue with the comparison
switch aVal.Kind() {
case reflect.Struct:
for i := 0; i < aVal.NumField(); i++ {
Expand All @@ -348,33 +343,27 @@ func compareAny(a, b interface{}, path string, changes *[]string) {
if dasherizedName == "model-overrides" {
dasherizedName = "overrides"
}

updatedPath = dasherizedName
}
}

// log.Println("field", fieldName, "updatedPath", updatedPath)

compareAny(aVal.Field(i).Interface(), bVal.Field(i).Interface(),
updatedPath, changes)
compareAny(aVal.Field(i).Interface(), bVal.Field(i).Interface(), updatedPath, changes)
}
default:
var a string
var b string

var aStr, bStr string
if aVal.IsValid() {
a = fmt.Sprintf("%v", aVal.Interface())
aStr = fmt.Sprintf("%v", aVal.Interface())
} else {
a = "no override"
aStr = "no override"
}

if bVal.IsValid() {
b = fmt.Sprintf("%v", bVal.Interface())
bStr = fmt.Sprintf("%v", bVal.Interface())
} else {
b = "no override"
bStr = "no override"
}

change := fmt.Sprintf("%s | %v → %v", path, a, b)
change := fmt.Sprintf("%s | %v → %v", path, aStr, bStr)
*changes = append(*changes, change)
}
}
3 changes: 2 additions & 1 deletion app/shared/plan_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func (ps PlanSettings) GetRequiredEnvVars() map[string]bool {
envVars[ms.Namer.BaseModelConfig.ApiKeyEnvVar] = true
envVars[ms.CommitMsg.BaseModelConfig.ApiKeyEnvVar] = true
envVars[ms.ExecStatus.BaseModelConfig.ApiKeyEnvVar] = true
envVars[ms.Verifier.BaseModelConfig.ApiKeyEnvVar] = true
envVars[ms.GetVerifier().BaseModelConfig.ApiKeyEnvVar] = true
envVars[ms.GetAutoFix().BaseModelConfig.ApiKeyEnvVar] = true

// for backward compatibility with <= 0.8.4 server versions
if len(envVars) == 0 {
Expand Down

0 comments on commit 6d13da3

Please sign in to comment.