diff --git a/docs/architecture/hooks.md b/docs/architecture/hooks.md index 27e18bbb..ac655a5e 100644 --- a/docs/architecture/hooks.md +++ b/docs/architecture/hooks.md @@ -21,6 +21,9 @@ Task BeforeCompletion(); Task OnFunctionExecuting(RoleDialogModel message); Task OnFunctionExecuted(RoleDialogModel message); Task AfterCompletion(RoleDialogModel message); + +// LLM detected the user's intention to end the conversation +Task ConversationEnding(RoleDialogModel conversation); ``` More information about conversation hook please go to [Conversation Hook](../conversation/hook.md). diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs index 3634255a..47808628 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs @@ -61,4 +61,9 @@ public abstract class ConversationHookBase : IConversationHook _dialogs = dialogs; return Task.CompletedTask; } + + public virtual Task ConversationEnding(RoleDialogModel message) + { + return Task.CompletedTask; + } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs index 8e55dab7..c754ff16 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs @@ -24,4 +24,6 @@ public interface IConversationHook Task OnFunctionExecuting(RoleDialogModel message); Task OnFunctionExecuted(RoleDialogModel message); Task AfterCompletion(RoleDialogModel message); + + Task ConversationEnding(RoleDialogModel conversation); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.CallFunctions.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.CallFunctions.cs index 5c3f941a..94cb3ddf 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.CallFunctions.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.CallFunctions.cs @@ -6,10 +6,6 @@ public partial class ConversationService { public async Task CallFunctions(RoleDialogModel msg) { - var hooks = _services.GetServices() - .OrderBy(x => x.Priority) - .ToList(); - // Invoke functions var functions = _services.GetServices() .Where(x => x.Name == msg.FunctionName) @@ -22,6 +18,10 @@ public partial class ConversationService return; } + var hooks = _services.GetServices() + .OrderBy(x => x.Priority) + .ToList(); + foreach (var fn in functions) { // Before executing functions diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs index bbbc6ec8..bf3ad621 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs @@ -25,6 +25,16 @@ public class ConversationEndRoutingHandler : RoutingHandlerBase, IRoutingHandler FunctionName = inst.Function, ExecutionData = inst }; + + var hooks = _services.GetServices() + .OrderBy(x => x.Priority) + .ToList(); + + foreach (var hook in hooks) + { + await hook.OnFunctionExecuting(result); + } + return result; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index fe4206fa..a20deb20 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -29,12 +29,7 @@ public class AgentController : ControllerBase, IApiAdapter public async Task> GetAgents() { var agents = await _agentService.GetAgents(); - - // Add the router as agent - var routing = _services.GetRequiredService(); - agents.Insert(0, routing.Load().Router); - - return agents.Select(x => AgentViewModel.FromAgent(x)).ToList(); + return agents.Select(x => AgentViewModel.FromAgent(x)).ToList(); } [HttpPost("/agent")]