From d3035c07a275ab905c337002d58c2aa10fc1f581 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 8 Sep 2025 23:07:51 -0500 Subject: [PATCH 1/5] add message wrapper to chat response --- .../Controllers/ConversationController.cs | 4 ++ .../Response/ChatResponseModel.cs | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 13fb5199..7f12eec2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -371,9 +371,11 @@ public class ConversationController : ControllerBase { response.Text = !string.IsNullOrEmpty(msg.SecondaryContent) ? msg.SecondaryContent : msg.Content; response.Function = msg.FunctionName; + response.MessageLabel = msg.MessageLabel; response.RichContent = msg.SecondaryRichContent ?? msg.RichContent; response.Instruction = msg.Instruction; response.Data = msg.Data; + response.AdditionalMessageWrapper = ChatResponseWrapper.From(msg.AdditionalMessageWrapper, conversationId, inputMsg.MessageId); }); var state = _services.GetRequiredService(); @@ -426,11 +428,13 @@ public class ConversationController : ControllerBase async msg => { response.Text = !string.IsNullOrEmpty(msg.SecondaryContent) ? msg.SecondaryContent : msg.Content; + response.MessageLabel = msg.MessageLabel; response.Function = msg.FunctionName; response.RichContent = msg.SecondaryRichContent ?? msg.RichContent; response.Instruction = msg.Instruction; response.Data = msg.Data; response.States = state.GetStates(); + response.AdditionalMessageWrapper = ChatResponseWrapper.From(msg.AdditionalMessageWrapper, conversationId, inputMsg.MessageId); await OnChunkReceived(Response, response); }); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/Response/ChatResponseModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/Response/ChatResponseModel.cs index 4d149200..37db3084 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/Response/ChatResponseModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/Response/ChatResponseModel.cs @@ -1,7 +1,46 @@ using BotSharp.Abstraction.Conversations.Dtos; +using System.Text.Json.Serialization; namespace BotSharp.OpenAPI.ViewModels.Conversations; public class ChatResponseModel : ChatResponseDto { + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("additional_message_wrapper")] + public ChatResponseWrapper? AdditionalMessageWrapper { get; set; } } + +public class ChatResponseWrapper +{ + [JsonPropertyName("sending_interval")] + public int SendingInterval { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("messages")] + public List? Messages { get; set; } + + public static ChatResponseWrapper? From(ChatMessageWrapper? wrapper, string conversationId, string? messageId = null) + { + if (wrapper == null) + { + return null; + } + + return new ChatResponseWrapper + { + SendingInterval = wrapper.SendingInterval, + Messages = wrapper?.Messages?.Select(x => new ChatResponseModel + { + ConversationId = conversationId, + MessageId = messageId ?? x.MessageId, + Text = !string.IsNullOrEmpty(x.SecondaryContent) ? x.SecondaryContent : x.Content, + MessageLabel = x.MessageLabel, + Function = x.FunctionName, + RichContent = x.SecondaryRichContent ?? x.RichContent, + Instruction = x.Instruction, + Data = x.Data, + IsAppend = true + })?.ToList() + }; + } +} \ No newline at end of file From 8250336498e3379106925d5d8d1ab75a2927f3d3 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 9 Sep 2025 01:58:46 -0500 Subject: [PATCH 2/5] fix missing label --- .../BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index e0034001..9aa7e5b6 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -153,6 +153,7 @@ public class ChatHubConversationHook : ConversationHookBase { ConversationId = conv.ConversationId, MessageId = item.MessageId, + MessageLabel = item.MessageLabel, Text = !string.IsNullOrEmpty(item.SecondaryContent) ? item.SecondaryContent : item.Content, Function = item.FunctionName, RichContent = item.SecondaryRichContent ?? item.RichContent, From e4c53a32b5cf5a825777abca7fae72ee9c6d30f2 Mon Sep 17 00:00:00 2001 From: vguruparan Date: Tue, 9 Sep 2025 11:19:29 -0500 Subject: [PATCH 3/5] Update distributed lock for cronservice --- .../Crontab/Settings/CrontabSettings.cs | 1 + .../BotSharp.Core.Crontab/Services/CrontabWatcher.cs | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs index 4bca1240..53e90959 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs @@ -4,6 +4,7 @@ public class CrontabSettings { public CrontabBaseSetting EventSubscriber { get; set; } = new(); public CrontabBaseSetting Watcher { get; set; } = new(); + public string LockName { get; set; } } public class CrontabBaseSetting diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs index 7bb621f4..9497ddd4 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs @@ -10,11 +10,15 @@ public class CrontabWatcher : BackgroundService { private readonly ILogger _logger; private readonly IServiceProvider _services; + private readonly CrontabSettings _cronSettings; + private string DIST_KEY; - public CrontabWatcher(IServiceProvider services, ILogger logger) + public CrontabWatcher(IServiceProvider services, ILogger logger, CrontabSettings cronSettings) { _logger = logger; _services = services; + _cronSettings = cronSettings; + DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName ?? "default"}"; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -29,7 +33,7 @@ public class CrontabWatcher : BackgroundService { var delay = Task.Delay(1000, stoppingToken); - await locker.LockAsync("CrontabWatcher:locker", async () => + await locker.LockAsync(DIST_KEY, async () => { await RunCronChecker(scope.ServiceProvider); }); From 4a1439574dc7cd59bfdbc3ffeac0c070292d3cef Mon Sep 17 00:00:00 2001 From: vguruparan Date: Tue, 9 Sep 2025 11:45:04 -0500 Subject: [PATCH 4/5] Update lockname to default if not provided. --- .../BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs | 2 +- .../BotSharp.Core.Crontab/Services/CrontabWatcher.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs index 53e90959..31326687 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs @@ -4,7 +4,7 @@ public class CrontabSettings { public CrontabBaseSetting EventSubscriber { get; set; } = new(); public CrontabBaseSetting Watcher { get; set; } = new(); - public string LockName { get; set; } + public string LockName { get; set; } = "default"; } public class CrontabBaseSetting diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs index 9497ddd4..dce21fa6 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs @@ -18,7 +18,7 @@ public class CrontabWatcher : BackgroundService _logger = logger; _services = services; _cronSettings = cronSettings; - DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName ?? "default"}"; + DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName}"; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) From 393430e9819bebbff37e976b89aa95418826f2e2 Mon Sep 17 00:00:00 2001 From: vguruparan Date: Tue, 9 Sep 2025 13:13:53 -0500 Subject: [PATCH 5/5] Update crontab naming --- .../BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs | 2 +- .../BotSharp.Core.Crontab/Services/CrontabWatcher.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs index 31326687..ac74ac38 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs @@ -4,7 +4,7 @@ public class CrontabSettings { public CrontabBaseSetting EventSubscriber { get; set; } = new(); public CrontabBaseSetting Watcher { get; set; } = new(); - public string LockName { get; set; } = "default"; + public string LockName { get; set; } = "CrontabWatcher:locker"; } public class CrontabBaseSetting diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs index dce21fa6..f75f09ad 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs @@ -18,7 +18,7 @@ public class CrontabWatcher : BackgroundService _logger = logger; _services = services; _cronSettings = cronSettings; - DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName}"; + DIST_KEY = _cronSettings.LockName; } protected override async Task ExecuteAsync(CancellationToken stoppingToken)