Make server response processing more resiliant

main
Cameron Murphy Reikes 2 years ago
parent b87efc8c2b
commit 41f963ad98

@ -408,18 +408,22 @@ func completion(w http.ResponseWriter, req *http.Request) {
with_action := strings.SplitAfter(response, "ACT_") with_action := strings.SplitAfter(response, "ACT_")
if len(with_action) != 2 { if len(with_action) != 2 {
log.Printf("Could not find action in response string `%s`\n", response) log.Printf("Could not find action in response string `%s`\n", response)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError) // game should send a new retry request after this
return return
} }
response = with_action[1] response = with_action[1]
// trim ending quotation mark // trim ending quotation mark. There might be text after the ending quotation mark because chatgpt sometimes
if !strings.HasSuffix(response, "\"") { // puts addendums in its responses, like `ACT_none "Hey" (I wanted the NPC to say hey here)`. The stuffafter the second
log.Printf("Could not find ending quotation in response string `%s`\n", response) // quotation mark needs to be ignored
between_quotes := strings.Split(response, "\"")
// [action] " [stuff] " [anything extra]
if len(between_quotes) < 2 {
log.Printf("Could not find enough quotes in response string `%s`\n", response)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
response = response[:len(response)-1] response = between_quotes[0] + "\"" + between_quotes[1] + "\""
} }
if logResponses { if logResponses {
log.Println("Println response: `", response + "`") log.Println("Println response: `", response + "`")

Loading…
Cancel
Save