diff --git a/Pacos/Services/ChatCommandHandlers/MentionHandler.cs b/Pacos/Services/ChatCommandHandlers/MentionHandler.cs index 6970e31..6f2afa5 100644 --- a/Pacos/Services/ChatCommandHandlers/MentionHandler.cs +++ b/Pacos/Services/ChatCommandHandlers/MentionHandler.cs @@ -106,13 +106,13 @@ private async Task GetChatResponseWithRetryAsync( byte[]? fileBytes = null, string? fileMimeType = null) { - var result = await Policy + return await Policy .Handle(x => x.ErrorCode is 502 or 503 or 504 || x.ErrorMessage?.Contains("try again", StringComparison.OrdinalIgnoreCase) == true) .Or() .OrResult(x => string.IsNullOrWhiteSpace(x.Text) && x.DataContents.Count == 0) .WaitAndRetryAsync(retryCount: 2, retryNumber => TimeSpan.FromMilliseconds(retryNumber * 200)) - .ExecuteAndCaptureAsync(async () => await _chatService.GetResponseAsync( + .ExecuteAsync(async () => await _chatService.GetResponseAsync( chatId, messageId, authorName, @@ -120,14 +120,6 @@ private async Task GetChatResponseWithRetryAsync( fileBytes, fileMimeType )); - - return result switch - { - { Outcome: OutcomeType.Failure, FinalException: not null } => throw result.FinalException, - { Outcome: OutcomeType.Failure, FinalException: null } when string.IsNullOrWhiteSpace(result.Result.Text) => throw new InvalidOperationException("Empty AI response."), - { Outcome: OutcomeType.Failure, FinalException: null } => throw new InvalidOperationException("Unexpected failure without an exception."), - _ => result.Result, - }; } public async Task HandleMentionAsync( @@ -234,6 +226,7 @@ _ when _wordFilter.ContainsBannedWords(fullMessageToLlm) => "ты пидор, к } catch (Exception e) { + _logger.LogError(e, "Failed to get chat response for {Author}", author); replyText = $"{e.GetType().Name}: {e.Message}"; } @@ -241,16 +234,6 @@ _ when _wordFilter.ContainsBannedWords(fullMessageToLlm) => "ты пидор, к _logger.LogInformation("Replying to {Author} with: {ReplyText}", author, replyText); - async Task SendReply(string text, ParseMode parseMode) - { - await botClient.SendMessage( - new ChatId(updateMessage.Chat.Id), - text, - parseMode, - new ReplyParameters { MessageId = updateMessage.MessageId, }, - cancellationToken: cancellationToken); - } - try { await SendReply(markdownReplyText, ParseMode.MarkdownV2); @@ -260,5 +243,17 @@ await botClient.SendMessage( _logger.LogError(e, "Failed to send message with MarkdownV2. Falling back to plain text"); await SendReply(replyText, ParseMode.None); } + + return; + + async Task SendReply(string text, ParseMode parseMode) + { + await botClient.SendMessage( + new ChatId(updateMessage.Chat.Id), + text, + parseMode, + new ReplyParameters { MessageId = updateMessage.MessageId, }, + cancellationToken: cancellationToken); + } } }