diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs index ac939a7d..d820aa6b 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; } = "CrontabWatcher:locker"; public DebugSetting Debug { get; set; } = new(); } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs index dc5f8cd2..d5073520 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 = _cronSettings.LockName; } 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); }); 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 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,