From 04a2d4fe5a8fa7e04abcbfea091e4c62b420cf45 Mon Sep 17 00:00:00 2001 From: Andraz Bajt Date: Fri, 14 Apr 2023 16:01:28 +0200 Subject: [PATCH] Gracefully handle missing session --- main.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 4b23b84..ba35840 100644 --- a/main.go +++ b/main.go @@ -100,23 +100,26 @@ func getClient() *openai.Client { func getCompletionRequest(p params, model string) openai.ChatCompletionRequest { if p.continueSession { - return loadLastCompletion() - } else { - return newCompletionRequest(p, model) + req := loadLastCompletion() + if req != nil { + return *req + } + fmt.Println("WARN: failed to load previous session, starting a new one") } + return newCompletionRequest(p, model) } -func loadLastCompletion() openai.ChatCompletionRequest { +func loadLastCompletion() *openai.ChatCompletionRequest { var req openai.ChatCompletionRequest session, err := os.ReadFile(sessionFile) if err != nil { - panic(err) + return nil } err = json.Unmarshal(session, &req) if err != nil { - panic(err) + return nil } - return req + return &req } func saveCompletion(req openai.ChatCompletionRequest) error {