From 81bb38b9b3137c36d1fa3235177a1059de200b30 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 9 Jul 2025 11:14:47 -0500 Subject: [PATCH] refine side car --- .../SideCar/Attributes/SideCarAttribute.cs | 4 ++-- .../SideCar/IConversationSideCar.cs | 2 +- .../Services/BotSharpConversationSideCar.cs | 14 +++++++------- .../Services/ConversationStateService.cs | 4 ++-- .../Hooks/ChatHubConversationHook.cs | 2 +- .../Controllers/TwilioInboundController.cs | 9 +++++---- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs index 833cb013..4426d71f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs @@ -79,7 +79,7 @@ public class SideCarAttribute : AsyncMoAttribute object? res = null; var isHandled = false; - var enabled = instance != null && instance.IsEnabled() && method != null; + var enabled = instance != null && instance.IsEnabled && method != null; if (!enabled) { return (isHandled, value); @@ -112,7 +112,7 @@ public class SideCarAttribute : AsyncMoAttribute object? value = null; var isHandled = false; - var enabled = instance != null && instance.IsEnabled() && method != null; + var enabled = instance != null && instance.IsEnabled && method != null; if (!enabled) { return (isHandled, value); diff --git a/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs b/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs index 3c9bcfa4..81b0cf9c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs +++ b/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs @@ -5,8 +5,8 @@ namespace BotSharp.Abstraction.SideCar; public interface IConversationSideCar { string Provider { get; } + bool IsEnabled { get; } - bool IsEnabled(); void AppendConversationDialogs(string conversationId, List messages); List GetConversationDialogs(string conversationId); void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint); diff --git a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs index e4996b06..d636fa71 100644 --- a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs +++ b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs @@ -14,7 +14,6 @@ limitations under the License. ******************************************************************************/ -using BotSharp.Abstraction.SideCar.Models; using BotSharp.Core.Infrastructures; namespace BotSharp.Core.SideCar.Services; @@ -31,6 +30,7 @@ public class BotSharpConversationSideCar : IConversationSideCar private string _conversationId = string.Empty; public string Provider => "botsharp"; + public bool IsEnabled => _enabled; public BotSharpConversationSideCar( IServiceProvider services, @@ -40,11 +40,6 @@ public class BotSharpConversationSideCar : IConversationSideCar _logger = logger; } - public bool IsEnabled() - { - return _enabled; - } - public void AppendConversationDialogs(string conversationId, List messages) { if (!IsValid(conversationId)) @@ -99,17 +94,22 @@ public class BotSharpConversationSideCar : IConversationSideCar top.State = new ConversationState(states); } - public async Task SendMessage(string agentId, string text, + public async Task SendMessage( + string agentId, + string text, PostbackMessageModel? postback = null, List? states = null, List? dialogs = null, SideCarOptions? options = null) { _sideCarOptions = options; + _logger.LogInformation($"Entering side car conversation..."); BeforeExecute(dialogs); var response = await InnerExecute(agentId, text, postback, states); AfterExecute(); + + _logger.LogInformation($"Existing side car conversation..."); return response; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index ff61fde2..4c8178d8 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -159,7 +159,7 @@ public class ConversationStateService : IConversationStateService Reset(); var endNodes = new Dictionary(); - if (_sidecar?.IsEnabled() == true) + if (_sidecar?.IsEnabled == true) { return endNodes; } @@ -234,7 +234,7 @@ public class ConversationStateService : IConversationStateService public void Save() { - if (_conversationId == null || _sidecar?.IsEnabled() == true) + if (_conversationId == null || _sidecar?.IsEnabled == true) { return; } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index a41abe87..3c7a675d 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -179,7 +179,7 @@ public class ChatHubConversationHook : ConversationHookBase private bool AllowSendingMessage() { var sidecar = _services.GetService(); - return sidecar == null || !sidecar.IsEnabled(); + return sidecar == null || !sidecar.IsEnabled; } private async Task InitClientConversation(string conversationId, ConversationDto conversation) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs index 31d3db52..de58dca6 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs @@ -53,28 +53,29 @@ public class TwilioInboundController : TwilioController instruction.SpeechPaths.Add(request.InitAudioFile); } + // Before creating session await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreating(request, instruction); }, request.AgentId); + var (agent, conversationId) = await InitConversation(request); request.ConversationId = conversationId.Id; instruction.AgentId = request.AgentId; instruction.ConversationId = request.ConversationId; + + // After creating session await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreated(request); }, request.AgentId); - var conv = _services.GetRequiredService(); - conv.SaveStates(); if (twilio.MachineDetected(request)) { response = new VoiceResponse(); - await HookEmitter.Emit(_services, async hook => await hook.OnVoicemailStarting(request), request.AgentId); @@ -122,7 +123,7 @@ public class TwilioInboundController : TwilioController await Task.Delay(1500); await twilio.StartRecording(request.CallSid, request.AgentId, request.ConversationId); }); - } + } return TwiML(response); }