Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions Pacos/Services/ChatCommandHandlers/MentionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,20 @@ private async Task<ChatResponseInfo> GetChatResponseWithRetryAsync(
byte[]? fileBytes = null,
string? fileMimeType = null)
{
var result = await Policy
return await Policy
.Handle<ApiException>(x => x.ErrorCode is 502 or 503 or 504
|| x.ErrorMessage?.Contains("try again", StringComparison.OrdinalIgnoreCase) == true)
.Or<HttpRequestException>()
.OrResult<ChatResponseInfo>(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,
messageText,
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(
Expand Down Expand Up @@ -234,23 +226,14 @@ _ when _wordFilter.ContainsBannedWords(fullMessageToLlm) => "ты пидор, к
}
catch (Exception e)
{
_logger.LogError(e, "Failed to get chat response for {Author}", author);
replyText = $"{e.GetType().Name}: {e.Message}";
}

var markdownReplyText = _markdownConversionService.ConvertToTelegramMarkdown(replyText);

_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);
Expand All @@ -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);
}
}
}