From 16bea297fca120e6e29f781524e96796e1d0ddaa Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 3 Feb 2025 15:12:36 -0600 Subject: [PATCH] add routing dialogs --- .../Conversations/Models/ConversationContext.cs | 1 + .../Routing/IRoutingContext.cs | 4 ++++ .../Services/BotSharpConversationSideCar.cs | 3 +++ .../BotSharp.Core/Routing/RoutingContext.cs | 16 ++++++++++++++++ .../Routing/RoutingService.InstructLoop.cs | 1 + .../Routing/RoutingService.InvokeAgent.cs | 4 ++++ .../BotSharp.Core/Routing/RoutingService.cs | 1 - .../Functions/ReadImageFn.cs | 5 ++++- 8 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationContext.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationContext.cs index 0b0b0846..2fc4dee6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationContext.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationContext.cs @@ -4,6 +4,7 @@ public class ConversationContext { public ConversationState State { get; set; } public List Dialogs { get; set; } = new(); + public List RoutingDialogs { get; set; } = new(); public List Breakpoints { get; set; } = new(); public int RecursiveCounter { get; set; } public Stack RoutingStack { get; set; } = new(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs index 1dd9f50a..da7d5c01 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs @@ -29,4 +29,8 @@ public interface IRoutingContext Stack GetAgentStack(); void SetAgentStack(Stack stack); void ResetAgentStack(); + + void SetDialogs(List dialogs); + List GetDialogs(); + void ResetDialogs(); } diff --git a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs index 59087311..34a98017 100644 --- a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs +++ b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs @@ -124,6 +124,7 @@ public class BotSharpConversationSideCar : IConversationSideCar { State = state.GetCurrentState(), Dialogs = dialogs ?? [], + RoutingDialogs = routing.Context.GetDialogs(), Breakpoints = [], RecursiveCounter = routing.Context.GetRecursiveCounter(), RoutingStack = routing.Context.GetAgentStack() @@ -134,6 +135,7 @@ public class BotSharpConversationSideCar : IConversationSideCar state.ResetCurrentState(); routing.Context.ResetRecursiveCounter(); routing.Context.ResetAgentStack(); + routing.Context.ResetDialogs(); Utilities.ClearCache(); } @@ -148,6 +150,7 @@ public class BotSharpConversationSideCar : IConversationSideCar state.SetCurrentState(node.State); routing.Context.SetRecursiveCounter(node.RecursiveCounter); routing.Context.SetAgentStack(node.RoutingStack); + routing.Context.SetDialogs(node.RoutingDialogs); Utilities.ClearCache(); enabled = false; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index dc6cb0bb..cf604c55 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -10,6 +10,7 @@ public class RoutingContext : IRoutingContext private string _conversationId; private string _messageId; private int _currentRecursionDepth = 0; + private List _dialogs = []; public RoutingContext(IServiceProvider services, RoutingSettings setting) { @@ -259,4 +260,19 @@ public class RoutingContext : IRoutingContext { _stack.Clear(); } + + public void SetDialogs(List dialogs) + { + _dialogs = dialogs ?? []; + } + + public List GetDialogs() + { + return _dialogs ?? []; + } + + public void ResetDialogs() + { + _dialogs = []; + } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs index b7dc6cc3..07b2f6fa 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs @@ -41,6 +41,7 @@ public partial class RoutingService } dialogs.Add(message); + Context.SetDialogs(dialogs); storage.Append(convService.ConversationId, message); // Get first instruction diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs index fbe8c055..a4d4b37a 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs @@ -59,6 +59,7 @@ public partial class RoutingService message = RoleDialogModel.From(message, role: AgentRole.Assistant, content: response.Content); message.CurrentAgentId = agent.Id; dialogs.Add(message); + Context.SetDialogs(dialogs); } return true; @@ -86,6 +87,7 @@ public partial class RoutingService dialogs.Add(RoleDialogModel.From(message, role: AgentRole.Assistant, content: responseTemplate)); + Context.SetDialogs(dialogs); } else { @@ -95,6 +97,7 @@ public partial class RoutingService content: message.Content); dialogs.Add(msg); + Context.SetDialogs(dialogs); // Send to Next LLM var agentId = routing.Context.GetCurrentAgentId(); @@ -106,6 +109,7 @@ public partial class RoutingService dialogs.Add(RoleDialogModel.From(message, role: AgentRole.Assistant, content: message.Content)); + Context.SetDialogs(dialogs); } return true; diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs index 075307b5..8a0ab666 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Routing.Settings; -using BotSharp.Core.Infrastructures; namespace BotSharp.Core.Routing; diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs index 9babe8c9..32769bee 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Routing; + namespace BotSharp.Plugin.FileHandler.Functions; public class ReadImageFn : IFunctionCallback @@ -20,6 +22,7 @@ public class ReadImageFn : IFunctionCallback { var args = JsonSerializer.Deserialize(message.FunctionArgs); var conv = _services.GetRequiredService(); + var routingCtx = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); Agent? fromAgent = null; @@ -28,7 +31,7 @@ public class ReadImageFn : IFunctionCallback fromAgent = await agentService.LoadAgent(message.CurrentAgentId); } - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = routingCtx.GetDialogs(); var dialogs = AssembleFiles(conv.ConversationId, args?.ImageUrls, wholeDialogs); var agent = new Agent {