From 2bf15f50784f13ea8d362123ea90f9841051115c Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 22 May 2025 13:58:42 -0500 Subject: [PATCH] refine routing mode --- .../BotSharp.Abstraction/Agents/Enums/AgentField.cs | 2 +- .../BotSharp.Abstraction/Agents/Models/Agent.cs | 4 +++- .../Enums/AgentMode.cs => Routing/Enums/RoutingMode.cs} | 4 ++-- .../Services/ConversationService.SendMessage.cs | 5 +++-- .../Repository/FileRepository/FileRepository.Agent.cs | 6 +++--- src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs | 5 +++-- .../ViewModels/Agents/Request/AgentCreationModel.cs | 7 ++++++- .../ViewModels/Agents/Request/AgentUpdateModel.cs | 6 +++++- .../ViewModels/Agents/View/AgentViewModel.cs | 4 +++- .../Collections/AgentDocument.cs | 2 +- .../Repository/MongoRepository.Agent.cs | 6 +++--- 11 files changed, 33 insertions(+), 18 deletions(-) rename src/Infrastructure/BotSharp.Abstraction/{Agents/Enums/AgentMode.cs => Routing/Enums/RoutingMode.cs} (54%) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 0cab0dfb..0617a9de 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -8,7 +8,7 @@ public enum AgentField IsPublic, Disabled, Type, - Mode, + RoutingMode, InheritAgentId, Profile, Label, diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 4562f16a..aa872566 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Routing.Enums; using BotSharp.Abstraction.Tasks.Models; namespace BotSharp.Abstraction.Agents.Models; @@ -17,7 +18,8 @@ public class Agent /// /// Routing Mode: lazy or eager /// - public string Mode { get; set; } = AgentMode.Eager; + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Mode { get; set; } public DateTime CreatedDateTime { get; set; } public DateTime UpdatedDateTime { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentMode.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Enums/RoutingMode.cs similarity index 54% rename from src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentMode.cs rename to src/Infrastructure/BotSharp.Abstraction/Routing/Enums/RoutingMode.cs index 4e2cd60c..8f946d01 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentMode.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Enums/RoutingMode.cs @@ -1,6 +1,6 @@ -namespace BotSharp.Abstraction.Agents.Enums; +namespace BotSharp.Abstraction.Routing.Enums; -public class AgentMode +public class RoutingMode { public const string Eager = "eager"; public const string Lazy = "lazy"; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 42e0de2c..a28ccbc7 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -2,6 +2,7 @@ using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Infrastructures.Enums; using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Messaging.Models.RichContent; +using BotSharp.Abstraction.Routing.Enums; using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core.Conversations.Services; @@ -83,10 +84,10 @@ public partial class ConversationService { // Check the routing mode var states = _services.GetRequiredService(); - var routingMode = states.GetState(StateConst.ROUTING_MODE, AgentMode.Eager); + var routingMode = states.GetState(StateConst.ROUTING_MODE, RoutingMode.Eager); routing.Context.Push(agent.Id, reason: "request started", updateLazyRouting: false); - if (routingMode == AgentMode.Lazy) + if (routingMode == RoutingMode.Lazy) { message.CurrentAgentId = states.GetState(StateConst.LAZY_ROUTING_AGENT_ID, message.CurrentAgentId); routing.Context.Push(message.CurrentAgentId, reason: "lazy routing", updateLazyRouting: false); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index 5494f972..f7e70ca7 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -27,8 +27,8 @@ namespace BotSharp.Core.Repository case AgentField.Type: UpdateAgentType(agent.Id, agent.Type); break; - case AgentField.Mode: - UpdateAgentMode(agent.Id, agent.Mode); + case AgentField.RoutingMode: + UpdateAgentRoutingMode(agent.Id, agent.Mode); break; case AgentField.InheritAgentId: UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); @@ -145,7 +145,7 @@ namespace BotSharp.Core.Repository File.WriteAllText(agentFile, json); } - private void UpdateAgentMode(string agentId, string mode) + private void UpdateAgentRoutingMode(string agentId, string? mode) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index 5a5a02da..929289aa 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Routing.Enums; using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core.Routing; @@ -290,8 +291,8 @@ public class RoutingContext : IRoutingContext // Set next handling agent for lazy routing mode var states = _services.GetRequiredService(); - var routingMode = states.GetState(StateConst.ROUTING_MODE, AgentMode.Eager); - if (routingMode == AgentMode.Lazy) + var routingMode = states.GetState(StateConst.ROUTING_MODE, RoutingMode.Eager); + if (routingMode == RoutingMode.Lazy) { var agentId = GetCurrentAgentId(); if (agentId != BuiltInAgentId.Fallback) diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentCreationModel.cs index 2fd2c429..33553e48 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentCreationModel.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Routing.Enums; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -8,7 +9,11 @@ public class AgentCreationModel public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } = AgentType.Task; - public string Mode { get; set; } = AgentMode.Eager; + + /// + /// Agent routing mode + /// + public string? Mode { get; set; } /// /// LLM default system instructions diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentUpdateModel.cs index da0c8b3a..3b69055f 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/Request/AgentUpdateModel.cs @@ -9,7 +9,11 @@ public class AgentUpdateModel public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; public string Type { get; set; } = AgentType.Task; - public string Mode { get; set; } = AgentMode.Eager; + + /// + /// Agent routing mode + /// + public string? Mode { get; set; } /// /// Instruction diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/View/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/View/AgentViewModel.cs index 4fede545..728a73e6 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/View/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/View/AgentViewModel.cs @@ -12,7 +12,9 @@ public class AgentViewModel public string Name { get; set; } public string Description { get; set; } public string Type { get; set; } = AgentType.Task; - public string Mode { get; set; } = null!; + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Mode { get; set; } public string Instruction { get; set; } [JsonPropertyName("channel_instructions")] diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs index 7ebded25..767a054a 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs @@ -5,7 +5,7 @@ public class AgentDocument : MongoBase public string Name { get; set; } = default!; public string Description { get; set; } = default!; public string Type { get; set; } = default!; - public string Mode { get; set; } = default!; + public string? Mode { get; set; } public string? InheritAgentId { get; set; } public string? IconUrl { get; set; } public string Instruction { get; set; } = default!; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 6eda94ed..5e0e741a 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -28,8 +28,8 @@ public partial class MongoRepository case AgentField.Type: UpdateAgentType(agent.Id, agent.Type); break; - case AgentField.Mode: - UpdateAgentMode(agent.Id, agent.Mode); + case AgentField.RoutingMode: + UpdateAgentRoutingMode(agent.Id, agent.Mode); break; case AgentField.InheritAgentId: UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); @@ -139,7 +139,7 @@ public partial class MongoRepository _dc.Agents.UpdateOne(filter, update); } - private void UpdateAgentMode(string agentId, string mode) + private void UpdateAgentRoutingMode(string agentId, string? mode) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update