From 117bb568c5cfb5af6c5f6d6aa2e2f10f486e41ee Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Thu, 20 Aug 2026 09:17:04 +0800 Subject: [PATCH] optimize ClearMessage --- .../Conversations/Models/RoleDialogModel.cs | 9 +++++++++ .../Hooks/RateLimitConversationHook.cs | 13 +++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index 810add7fa..31eda506a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -216,4 +216,13 @@ public static RoleDialogModel From(RoleDialogModel source, MetaData = source.MetaData != null ? new(source.MetaData) : null }; } + + public void ClearMessage() + { + this.Payload = null; + this.Content = string.Empty; + this.RichContent = null; + this.SecondaryContent = null; + this.SecondaryRichContent = null; + } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs index d1d95d31f..20c048fc8 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/RateLimitConversationHook.cs @@ -34,9 +34,9 @@ public override async Task OnMessageReceived(RoleDialogModel message) if (charCount > rateLimit.MaxInputLengthPerRequest) { await storage.Append(convId, message); + message.ClearMessage(); message.Content = $"The number of characters in your message exceeds the system maximum of {rateLimit.MaxInputLengthPerRequest}"; message.StopCompletion = true; - ClearMessage(message); return; } @@ -55,9 +55,9 @@ public override async Task OnMessageReceived(RoleDialogModel message) if (seconds < rateLimit.MinTimeSecondsBetweenMessages) { await storage.Append(convId, message); + message.ClearMessage(); message.Content = "Your message sending frequency exceeds the frequency specified by the system. Please try again later."; message.StopCompletion = true; - ClearMessage(message); return; } } @@ -76,18 +76,11 @@ public override async Task OnMessageReceived(RoleDialogModel message) if (results.Count > rateLimit.MaxConversationPerDay) { await storage.Append(convId, message); + message.ClearMessage(); message.Content = $"The number of conversations you have exceeds the system maximum of {rateLimit.MaxConversationPerDay}"; message.StopCompletion = true; - ClearMessage(message); return; } } } - - private void ClearMessage(RoleDialogModel message) - { - message.SecondaryContent = null; - message.RichContent = null; - message.SecondaryRichContent = null; - } }