From 517a6001f0032769e6ed32aa9050cf3d7bad3c3a Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Fri, 17 Oct 2025 03:36:03 +0300 Subject: [PATCH] fix issues --- ChatBot/Services/HistoryCompressionService.cs | 24 ++++++++++++------- .../Telegram/Commands/CommandRegistry.cs | 14 +++++------ .../Telegram/Commands/TelegramCommandBase.cs | 2 +- .../Services/TelegramMessageHandler.cs | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/ChatBot/Services/HistoryCompressionService.cs b/ChatBot/Services/HistoryCompressionService.cs index ac50c91..1aee33c 100644 --- a/ChatBot/Services/HistoryCompressionService.cs +++ b/ChatBot/Services/HistoryCompressionService.cs @@ -246,7 +246,10 @@ namespace ChatBot.Services if (content.Length < _aiSettings.MinMessageLengthForSummarization) { return content.Length > _aiSettings.MaxSummarizedMessageLength - ? content.Substring(0, _aiSettings.MaxSummarizedMessageLength) + "..." + ? string.Concat( + content.AsSpan(0, _aiSettings.MaxSummarizedMessageLength), + "..." + ) : content; } @@ -267,10 +270,13 @@ namespace ChatBot.Services var summary = await GenerateSummaryAsync(summaryMessages, cancellationToken); return string.IsNullOrEmpty(summary) - ? content.Substring( - 0, - Math.Min(content.Length, _aiSettings.MaxSummarizedMessageLength) - ) + "..." + ? string.Concat( + content.AsSpan( + 0, + Math.Min(content.Length, _aiSettings.MaxSummarizedMessageLength) + ), + "..." + ) : summary; } catch (Exception ex) @@ -345,7 +351,7 @@ namespace ChatBot.Services var result = response.ToString().Trim(); return result.Length > _aiSettings.MaxSummarizedMessageLength - ? result.Substring(0, _aiSettings.MaxSummarizedMessageLength) + "..." + ? string.Concat(result.AsSpan(0, _aiSettings.MaxSummarizedMessageLength), "...") : result; } @@ -419,8 +425,10 @@ namespace ChatBot.Services return new ChatMessage { Role = message.Role, - Content = - message.Content.Substring(0, _aiSettings.MaxSummarizedMessageLength) + "...", + Content = string.Concat( + message.Content.AsSpan(0, _aiSettings.MaxSummarizedMessageLength), + "..." + ), }; } } diff --git a/ChatBot/Services/Telegram/Commands/CommandRegistry.cs b/ChatBot/Services/Telegram/Commands/CommandRegistry.cs index e1dbd8c..2a054a6 100644 --- a/ChatBot/Services/Telegram/Commands/CommandRegistry.cs +++ b/ChatBot/Services/Telegram/Commands/CommandRegistry.cs @@ -8,7 +8,9 @@ namespace ChatBot.Services.Telegram.Commands /// public class CommandRegistry { - private readonly Dictionary _commands = new(); + private readonly Dictionary _commands = new( + StringComparer.OrdinalIgnoreCase + ); private readonly ILogger _logger; public CommandRegistry( @@ -32,12 +34,9 @@ namespace ChatBot.Services.Telegram.Commands /// private void RegisterCommand(ITelegramCommand command) { - if (command == null) - { - throw new ArgumentNullException(nameof(command)); - } + ArgumentNullException.ThrowIfNull(command); - var commandName = command.CommandName.ToLower(); + var commandName = command.CommandName; if (_commands.ContainsKey(commandName)) { _logger.LogWarning("Command '{CommandName}' is already registered", commandName); @@ -53,8 +52,7 @@ namespace ChatBot.Services.Telegram.Commands /// public ITelegramCommand? GetCommand(string commandName) { - var key = commandName.ToLower(); - return _commands.TryGetValue(key, out var command) ? command : null; + return _commands.TryGetValue(commandName, out var command) ? command : null; } /// diff --git a/ChatBot/Services/Telegram/Commands/TelegramCommandBase.cs b/ChatBot/Services/Telegram/Commands/TelegramCommandBase.cs index 8264add..f806b25 100644 --- a/ChatBot/Services/Telegram/Commands/TelegramCommandBase.cs +++ b/ChatBot/Services/Telegram/Commands/TelegramCommandBase.cs @@ -47,7 +47,7 @@ namespace ChatBot.Services.Telegram.Commands command = command.Split('@')[0]; } - return command == CommandName.ToLower(); + return string.Equals(command, CommandName, StringComparison.OrdinalIgnoreCase); } /// diff --git a/ChatBot/Services/Telegram/Services/TelegramMessageHandler.cs b/ChatBot/Services/Telegram/Services/TelegramMessageHandler.cs index 2dacc7d..a2f6a7e 100644 --- a/ChatBot/Services/Telegram/Services/TelegramMessageHandler.cs +++ b/ChatBot/Services/Telegram/Services/TelegramMessageHandler.cs @@ -65,7 +65,7 @@ namespace ChatBot.Services.Telegram.Services messageText, chatId, userName, - message.Chat.Type.ToString().ToLower(), + message.Chat.Type.ToString(), message.Chat.Title ?? "", replyInfo, cancellationToken