Skip to content

Commit

Permalink
Merge pull request #103 from teknologi-umum/vision
Browse files Browse the repository at this point in the history
Fix command priority
  • Loading branch information
ronnygunawan authored Jan 30, 2024
2 parents cf42dc8 + 6c77e1a commit 27ca944
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 66 deletions.
6 changes: 5 additions & 1 deletion BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BotNet.Commands.AI.OpenAI;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;
using BotNet.Services.MarkdownV2;
using BotNet.Services.OpenAI;
Expand All @@ -17,11 +18,13 @@ public sealed class AskCommandHandler(
ITelegramBotClient telegramBotClient,
OpenAIClient openAIClient,
ITelegramMessageCache telegramMessageCache,
CommandPriorityCategorizer commandPriorityCategorizer,
ILogger<AskCommandHandler> logger
) : ICommandHandler<AskCommand> {
private readonly ITelegramBotClient _telegramBotClient = telegramBotClient;
private readonly OpenAIClient _openAIClient = openAIClient;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;
private readonly ILogger<AskCommandHandler> _logger = logger;

public async Task Handle(AskCommand askCommand, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -92,7 +95,8 @@ select ChatMessage.FromText(
message: AIResponseMessage.FromMessage(
message: responseMessage,
replyToMessage: askCommand.Command,
callSign: "AI"
callSign: "AI",
commandPriorityCategorizer: _commandPriorityCategorizer
)
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BotNet.Commands;
using BotNet.Commands.AI.OpenAI;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.CommandPrioritization;
using BotNet.Services.OpenAI.Skills;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
Expand All @@ -12,11 +13,13 @@ public sealed class OpenAIImageGenerationPromptHandler(
ITelegramBotClient telegramBotClient,
ImageGenerationBot imageGenerationBot,
ITelegramMessageCache telegramMessageCache,
CommandPriorityCategorizer commandPriorityCategorizer,
ILogger<OpenAIImageGenerationPromptHandler> logger
) : ICommandHandler<OpenAIImageGenerationPrompt> {
private readonly ITelegramBotClient _telegramBotClient = telegramBotClient;
private readonly ImageGenerationBot _imageGenerationBot = imageGenerationBot;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;
private readonly ILogger<OpenAIImageGenerationPromptHandler> _logger = logger;

public Task Handle(OpenAIImageGenerationPrompt command, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -62,7 +65,7 @@ await _telegramBotClient.DeleteMessageAsync(

// Track thread
_telegramMessageCache.Add(
NormalMessage.FromMessage(responseMessage)
NormalMessage.FromMessage(responseMessage, _commandPriorityCategorizer)
);
} catch (OperationCanceledException) {
// Terminate gracefully
Expand Down
6 changes: 5 additions & 1 deletion BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BotNet.Commands.AI.Stability;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;
using BotNet.Services.MarkdownV2;
using BotNet.Services.OpenAI;
Expand All @@ -21,6 +22,7 @@ public sealed class OpenAIImagePromptHandler(
ICommandQueue commandQueue,
ITelegramMessageCache telegramMessageCache,
OpenAIClient openAIClient,
CommandPriorityCategorizer commandPriorityCategorizer,
ILogger<OpenAIImageGenerationPromptHandler> logger
) : ICommandHandler<OpenAIImagePrompt> {
internal static readonly RateLimiter VISION_RATE_LIMITER = RateLimiter.PerUserPerChat(1, TimeSpan.FromMinutes(15));
Expand All @@ -29,6 +31,7 @@ ILogger<OpenAIImageGenerationPromptHandler> logger
private readonly ICommandQueue _commandQueue = commandQueue;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly OpenAIClient _openAIClient = openAIClient;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;
private readonly ILogger<OpenAIImageGenerationPromptHandler> _logger = logger;

public Task Handle(OpenAIImagePrompt imagePrompt, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -185,7 +188,8 @@ await _telegramBotClient.EditMessageTextAsync(
message: AIResponseMessage.FromMessage(
message: responseMessage,
replyToMessage: imagePrompt.Command,
callSign: imagePrompt.CallSign
callSign: imagePrompt.CallSign,
commandPriorityCategorizer: _commandPriorityCategorizer
)
);
});
Expand Down
6 changes: 5 additions & 1 deletion BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BotNet.Commands.AI.Stability;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;
using BotNet.Services.MarkdownV2;
using BotNet.Services.OpenAI;
Expand All @@ -20,6 +21,7 @@ public sealed class OpenAITextPromptHandler(
ICommandQueue commandQueue,
ITelegramMessageCache telegramMessageCache,
OpenAIClient openAIClient,
CommandPriorityCategorizer commandPriorityCategorizer,
ILogger<OpenAITextPromptHandler> logger
) : ICommandHandler<OpenAITextPrompt> {
internal static readonly RateLimiter CHAT_RATE_LIMITER = RateLimiter.PerUserPerChat(5, TimeSpan.FromMinutes(15));
Expand All @@ -28,6 +30,7 @@ ILogger<OpenAITextPromptHandler> logger
private readonly ICommandQueue _commandQueue = commandQueue;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly OpenAIClient _openAIClient = openAIClient;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;
private readonly ILogger<OpenAITextPromptHandler> _logger = logger;

public Task Handle(OpenAITextPrompt textPrompt, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -156,7 +159,8 @@ await _telegramBotClient.EditMessageTextAsync(
message: AIResponseMessage.FromMessage(
message: responseMessage,
replyToMessage: textPrompt.Command,
callSign: textPrompt.CallSign
callSign: textPrompt.CallSign,
commandPriorityCategorizer: _commandPriorityCategorizer
)
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BotNet.Commands;
using BotNet.Commands.AI.Stability;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.CommandPrioritization;
using BotNet.Services.Stability.Models;
using BotNet.Services.Stability.Skills;
using Telegram.Bot;
Expand All @@ -11,11 +12,13 @@ namespace BotNet.CommandHandlers.AI.Stability {
public sealed class StabilityTextToImagePromptHandler(
ITelegramBotClient telegramBotClient,
ImageGenerationBot imageGenerationBot,
ITelegramMessageCache telegramMessageCache
ITelegramMessageCache telegramMessageCache,
CommandPriorityCategorizer commandPriorityCategorizer
) : ICommandHandler<StabilityTextToImagePrompt> {
private readonly ITelegramBotClient _telegramBotClient = telegramBotClient;
private readonly ImageGenerationBot _imageGenerationBot = imageGenerationBot;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;

public Task Handle(StabilityTextToImagePrompt command, CancellationToken cancellationToken) {
// Fire and forget
Expand Down Expand Up @@ -69,7 +72,7 @@ await _telegramBotClient.DeleteMessageAsync(

// Track thread
_telegramMessageCache.Add(
NormalMessage.FromMessage(responseMessage)
NormalMessage.FromMessage(responseMessage, _commandPriorityCategorizer)
);
} catch (OperationCanceledException) {
// Terminate gracefully
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotNet.Commands;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.CommandPrioritization;
using BotNet.Services.BotProfile;
using BotNet.Services.SocialLink;
using RG.Ninja;
Expand All @@ -11,12 +12,14 @@ public sealed class MessageUpdateHandler(
ITelegramBotClient telegramBotClient,
ICommandQueue commandQueue,
ITelegramMessageCache telegramMessageCache,
BotProfileAccessor botProfileAccessor
BotProfileAccessor botProfileAccessor,
CommandPriorityCategorizer commandPriorityCategorizer
) : ICommandHandler<MessageUpdate> {
private readonly ITelegramBotClient _telegramBotClient = telegramBotClient;
private readonly ICommandQueue _commandQueue = commandQueue;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly BotProfileAccessor _botProfileAccessor = botProfileAccessor;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;

public async Task Handle(MessageUpdate update, CancellationToken cancellationToken) {
// Handle slash commands
Expand All @@ -27,6 +30,7 @@ public async Task Handle(MessageUpdate update, CancellationToken cancellationTok
if (SlashCommand.TryCreate(
message: update.Message,
botUsername: (await _botProfileAccessor.GetBotProfileAsync(cancellationToken)).Username!,
commandPriorityCategorizer: _commandPriorityCategorizer,
out SlashCommand? slashCommand
)) {
await _commandQueue.DispatchAsync(
Expand Down Expand Up @@ -108,6 +112,7 @@ await _telegramBotClient.SendTextMessageAsync(
// Handle AI calls
if (AICallCommand.TryCreate(
message: update.Message!,
commandPriorityCategorizer: _commandPriorityCategorizer,
out AICallCommand? aiCallCommand
)) {
// Cache both message and reply to message
Expand Down Expand Up @@ -140,6 +145,7 @@ await _commandQueue.DispatchAsync(
messageId: new(replyToMessageId),
chatId: new(chatId)
),
commandPriorityCategorizer: _commandPriorityCategorizer,
out AIFollowUpMessage? aiFollowUpMessage
)) {
return;
Expand Down
8 changes: 5 additions & 3 deletions BotNet.Commands/BotUpdate/Message/AICallCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;

namespace BotNet.Commands.BotUpdate.Message {
Expand Down Expand Up @@ -36,17 +37,18 @@ string callSign

public static bool TryCreate(
Telegram.Bot.Types.Message message,
CommandPriorityCategorizer commandPriorityCategorizer,
[NotNullWhen(true)] out AICallCommand? aiCallCommand
) {
// Chat must be private or group
if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) {
if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) {
aiCallCommand = null;
return false;
}

// Sender must be a user
if (message.From is not { } from
|| !HumanSender.TryCreate(from, out HumanSender? sender)) {
|| !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) {
aiCallCommand = null;
return false;
}
Expand All @@ -72,7 +74,7 @@ public static bool TryCreate(
?? message.ReplyToMessage?.Sticker?.FileId,
replyToMessage: message.ReplyToMessage is null
? null
: NormalMessage.FromMessage(message.ReplyToMessage),
: NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer),
callSign: callSign
);
return true;
Expand Down
6 changes: 4 additions & 2 deletions BotNet.Commands/BotUpdate/Message/AIFollowUpMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;

namespace BotNet.Commands.BotUpdate.Message {
Expand All @@ -26,17 +27,18 @@ AIResponseMessage replyToMessage
public static bool TryCreate(
Telegram.Bot.Types.Message message,
IEnumerable<MessageBase> thread,
CommandPriorityCategorizer commandPriorityCategorizer,
[NotNullWhen(true)] out AIFollowUpMessage? aiFollowUpMessage
) {
// Chat must be private or group
if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) {
if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) {
aiFollowUpMessage = null;
return false;
}

// Sender must be a user
if (message.From is not { } from
|| !HumanSender.TryCreate(from, out HumanSender? sender)) {
|| !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) {
aiFollowUpMessage = null;
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions BotNet.Commands/BotUpdate/Message/AIResponseMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;

namespace BotNet.Commands.BotUpdate.Message {
Expand Down Expand Up @@ -27,10 +28,11 @@ string callSign
public static AIResponseMessage FromMessage(
Telegram.Bot.Types.Message message,
HumanMessageBase replyToMessage,
string callSign
string callSign,
CommandPriorityCategorizer commandPriorityCategorizer
) {
// Chat must be private or group
if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) {
if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) {
throw new ArgumentException("Chat must be private or group.", nameof(message));
}

Expand Down
9 changes: 5 additions & 4 deletions BotNet.Commands/BotUpdate/Message/NormalMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;

namespace BotNet.Commands.BotUpdate.Message {
Expand All @@ -19,9 +20,9 @@ private NormalMessage(
replyToMessage: replyToMessage
) { }

public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) {
public static NormalMessage FromMessage(Telegram.Bot.Types.Message message, CommandPriorityCategorizer commandPriorityCategorizer) {
// Chat must be private or group
if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) {
if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) {
throw new ArgumentException("Chat must be private or group.", nameof(message));
}

Expand All @@ -33,7 +34,7 @@ public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) {
return new(
messageId: new(message.MessageId),
chat: chat,
sender: HumanSender.TryCreate(from, out HumanSender? humanSender)
sender: HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? humanSender)
? humanSender
: BotSender.TryCreate(from, out BotSender? botSender)
? botSender
Expand All @@ -42,7 +43,7 @@ public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) {
imageFileId: message.Photo?.LastOrDefault()?.FileId ?? message.Sticker?.FileId,
replyToMessage: message.ReplyToMessage is null
? null
: NormalMessage.FromMessage(message.ReplyToMessage)
: NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer)
);
}
}
Expand Down
8 changes: 5 additions & 3 deletions BotNet.Commands/BotUpdate/Message/SlashCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
using BotNet.Commands.SenderAggregate;
using Telegram.Bot.Types.Enums;

Expand Down Expand Up @@ -33,6 +34,7 @@ string command
public static bool TryCreate(
Telegram.Bot.Types.Message message,
string botUsername,
CommandPriorityCategorizer commandPriorityCategorizer,
[NotNullWhen(true)] out SlashCommand? slashCommand
) {
// Message must start with a slash command
Expand All @@ -46,14 +48,14 @@ public static bool TryCreate(
}

// Chat must be private or group
if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) {
if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) {
slashCommand = null;
return false;
}

// Sender must be a user
if (message.From is not { } from
|| !HumanSender.TryCreate(from, out HumanSender? sender)) {
|| !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) {
slashCommand = null;
return false;
}
Expand Down Expand Up @@ -88,7 +90,7 @@ public static bool TryCreate(
imageFileId: message.Photo?.LastOrDefault()?.FileId,
replyToMessage: message.ReplyToMessage is null
? null
: NormalMessage.FromMessage(message.ReplyToMessage),
: NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer),
command: commandText
);
return true;
Expand Down
6 changes: 5 additions & 1 deletion BotNet.Commands/ChatAggregate/Chat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using BotNet.Commands.CommandPrioritization;
using Telegram.Bot.Types.Enums;

namespace BotNet.Commands.ChatAggregate {
Expand All @@ -16,11 +17,14 @@ protected ChatBase(

public static bool TryCreate(
Telegram.Bot.Types.Chat telegramChat,
CommandPriorityCategorizer priorityCategorizer,
[NotNullWhen(true)] out ChatBase? chat
) {
chat = telegramChat switch {
Telegram.Bot.Types.Chat { Type: ChatType.Private } => PrivateChat.FromTelegramChat(telegramChat),
Telegram.Bot.Types.Chat { Type: ChatType.Group or ChatType.Supergroup } => GroupChat.FromTelegramChat(telegramChat),
Telegram.Bot.Types.Chat { Type: ChatType.Group or ChatType.Supergroup } => priorityCategorizer.IsHomeGroup(telegramChat.Id)
? HomeGroupChat.FromTelegramChat(telegramChat)
: GroupChat.FromTelegramChat(telegramChat),
_ => null
};
return chat is not null;
Expand Down
Loading

0 comments on commit 27ca944

Please sign in to comment.