From 4ddb4ddf1c74d5bed12f598e6c8322e04042a594 Mon Sep 17 00:00:00 2001 From: Dmitry Rubtsov Date: Mon, 28 Aug 2023 18:12:14 +0600 Subject: [PATCH] add handling for timeouts --- cmd/matrix-gpt/main.go | 8 ++++---- internal/bot/actions.go | 3 ++- internal/bot/handlers.go | 7 ++++++- internal/bot/strings.go | 6 +++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/matrix-gpt/main.go b/cmd/matrix-gpt/main.go index cba831e..57c2aad 100644 --- a/cmd/matrix-gpt/main.go +++ b/cmd/matrix-gpt/main.go @@ -51,7 +51,7 @@ func main() { Name: "history-limit", Usage: "Maximum number of history entries", EnvVars: []string{"HISTORY_LIMIT"}, - Value: 0, + Value: 5, }, &cli.IntFlag{ Name: "history-expire", @@ -69,13 +69,13 @@ func main() { Name: "gpt-timeout", Usage: "Time to wait for a GPT response (in seconds)", EnvVars: []string{"GPT_TIMEOUT"}, - Value: 180, + Value: 35, }, &cli.IntFlag{ Name: "max-attempts", - Usage: "Maximum number of retry attempts for GPT", + Usage: "Maximum number of attempts for GPT requests", EnvVars: []string{"MAX_ATTEMPTS"}, - Value: 3, + Value: 1, }, &cli.StringSliceFlag{ Name: "user-ids", diff --git a/internal/bot/actions.go b/internal/bot/actions.go index 40ae5e0..a5050f8 100644 --- a/internal/bot/actions.go +++ b/internal/bot/actions.go @@ -61,7 +61,7 @@ func (b *Bot) completionResponse(ctx context.Context, u *user, evt *event.Event, // helpResponse responds with help message. func (b *Bot) helpResponse(ctx context.Context, u *user, evt *event.Event, msg string) error { - return b.markdownResponse(evt, false, helpMessage) + return b.markdownResponse(evt, false, helpMsg) } // imageResponse responds to the user message with a DALL-E created image. @@ -123,6 +123,7 @@ func (b *Bot) markdownResponse(evt *event.Event, reply bool, msg string) error { if reply { formattedMsg.SetReply(evt) } + _, err := b.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedMsg) return err } diff --git a/internal/bot/handlers.go b/internal/bot/handlers.go index 7c83212..a1eb5cb 100644 --- a/internal/bot/handlers.go +++ b/internal/bot/handlers.go @@ -2,6 +2,7 @@ package bot import ( "context" + "errors" "time" "github.com/rs/zerolog/log" @@ -115,8 +116,12 @@ func (b *Bot) sendResponse(ctx context.Context, u *user, e *event.Event) (err er func (b *Bot) err(evt *event.Event, err error) { switch t := err.(type) { case *unknownCommandError: - b.markdownResponse(evt, true, errorMessage) + b.markdownResponse(evt, true, unknownCommandMsg) case *openai.APIError: b.markdownResponse(evt, true, t.Message) + default: + if errors.Is(err, context.DeadlineExceeded) { + b.markdownResponse(evt, true, timeoutMsg) + } } } diff --git a/internal/bot/strings.go b/internal/bot/strings.go index cd9e5c5..1cc1947 100644 --- a/internal/bot/strings.go +++ b/internal/bot/strings.go @@ -1,8 +1,7 @@ package bot const ( - errorMessage = "Unknown command. Please use the `!help` command to access the available commands" - helpMessage = `**Commands** + helpMsg = `**Commands** - *!image [text]*: Creates an image based on the provided text. - *!reset [text]*: Resets the user history. If a text is provided after the reset command, it will generate a GPT response based on this text. - *[text]*: If only text is provided, the bot will generate a GPT-based response related to that text. @@ -10,6 +9,7 @@ const ( **Notes** - You can use the first letter of a command as an alias. For example, "!i" for "!image". - If you wish to terminate the current processing, simply delete your message from the chat. -- The bot responds with ❌ reaction if there are any errors. Contact the administrator if you see this. ` + timeoutMsg = "Timeout error. Please try again. If issue persists, contact the administrator." + unknownCommandMsg = "Unknown command. Please use the `!help` command to access the available commands" )