From 41f963ad98947a8c23b951e81453acc5e4e64aa5 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Mon, 1 May 2023 23:45:37 -0700 Subject: [PATCH] Make server response processing more resiliant --- server/main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/server/main.go b/server/main.go index 7f9df7a..d9b7937 100644 --- a/server/main.go +++ b/server/main.go @@ -408,18 +408,22 @@ func completion(w http.ResponseWriter, req *http.Request) { with_action := strings.SplitAfter(response, "ACT_") if len(with_action) != 2 { 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 } response = with_action[1] - // trim ending quotation mark - if !strings.HasSuffix(response, "\"") { - log.Printf("Could not find ending quotation in response string `%s`\n", response) + // trim ending quotation mark. There might be text after the ending quotation mark because chatgpt sometimes + // puts addendums in its responses, like `ACT_none "Hey" (I wanted the NPC to say hey here)`. The stuffafter the second + // 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) return } - response = response[:len(response)-1] + response = between_quotes[0] + "\"" + between_quotes[1] + "\"" } if logResponses { log.Println("Println response: `", response + "`")