diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookProvider.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookProvider.cs deleted file mode 100644 index 00393da4..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookProvider.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace BotSharp.Abstraction.Conversations; - -public class ConversationHookProvider -{ - public IEnumerable Hooks { get; } - - private readonly Lazy> _hooksOrderByPriority; - - public IEnumerable HooksOrderByPriority - => _hooksOrderByPriority.Value; - - public ConversationHookProvider(IEnumerable conversationHooks) - { - Hooks = conversationHooks; - _hooksOrderByPriority = new Lazy>(() => - { - return conversationHooks.OrderBy(hook => hook.Priority).ToArray(); - }); - } -} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Hooks/HookProvider.cs b/src/Infrastructure/BotSharp.Abstraction/Hooks/HookProvider.cs new file mode 100644 index 00000000..cedc7c2a --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Hooks/HookProvider.cs @@ -0,0 +1,25 @@ +using BotSharp.Abstraction.Conversations; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Abstraction.Hooks +{ + public static class HookProvider + { + public static List GetHooks(this IServiceProvider services, string agentId) where T : IHookBase + { + var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); + return hooks.ToList(); + } + + public static List GetHooksOrderByPriority(this IServiceProvider services, string agentId) where T: IConversationHook + { + var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); + return hooks.OrderBy(p => p.Priority).ToList(); + } + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs index a0ad18e6..834577e9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs @@ -12,6 +12,6 @@ namespace BotSharp.Abstraction.Hooks /// Agent Id /// string SelfId => string.Empty; - bool IsMatch(string id) => string.IsNullOrEmpty(SelfId) || SelfId == id; + bool IsMatch(string agentId) => string.IsNullOrEmpty(SelfId) || SelfId == agentId; } } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs index cc9fa0c1..97ac91c2 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Options; using BotSharp.Core.Infrastructures; @@ -23,7 +24,6 @@ public class RealtimeHub : IRealtimeHub public async Task ConnectToModel(Func? responseToUser = null, Func? init = null) { - var hookProvider = _services.GetService(); var convService = _services.GetRequiredService(); convService.SetConversationId(_conn.ConversationId, []); var conversation = await convService.GetConversation(_conn.ConversationId); @@ -105,7 +105,8 @@ public class RealtimeHub : IRealtimeHub dialogs.Add(message); storage.Append(_conn.ConversationId, message); - foreach (var hook in hookProvider?.HooksOrderByPriority ?? []) + var hooks = _services.GetHooksOrderByPriority(_conn.CurrentAgentId); + foreach (var hook in hooks) { hook.SetAgent(agent) .SetConversation(conversation); @@ -126,7 +127,8 @@ public class RealtimeHub : IRealtimeHub storage.Append(_conn.ConversationId, message); routing.Context.SetMessageId(_conn.ConversationId, message.MessageId); - foreach (var hook in hookProvider?.HooksOrderByPriority ?? []) + var hooks = _services.GetHooksOrderByPriority(_conn.CurrentAgentId); + foreach (var hook in hooks) { hook.SetAgent(agent) .SetConversation(conversation); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index b2b97f4f..1227d3dc 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Infrastructures.Enums; using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Messaging.Models.RichContent; @@ -29,7 +30,6 @@ public partial class ConversationService var dialogs = conv.GetDialogHistory(); var statistics = _services.GetRequiredService(); - var hookProvider = _services.GetRequiredService(); RoleDialogModel response = message; bool stopCompletion = false; @@ -44,7 +44,8 @@ public partial class ConversationService message.Payload = replyMessage.Payload; } - foreach (var hook in hookProvider.HooksOrderByPriority) + var hooks = _services.GetHooksOrderByPriority(message.CurrentAgentId); + foreach (var hook in hooks) { hook.SetAgent(agent) .SetConversation(conversation); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs index 203fa885..25cf32d1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Infrastructures.Enums; namespace BotSharp.Core.Conversations.Services; @@ -31,9 +32,7 @@ public partial class ConversationService states.CleanStates(excludedStates); } - var hooks = _services - .GetRequiredService() - .HooksOrderByPriority; + var hooks = _services.GetHooksOrderByPriority(routingCtx.GetCurrentAgentId()); // Before executing functions foreach (var hook in hooks) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index dc98d440..6eaeb167 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Conversations.Enums; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Models; namespace BotSharp.Core.Conversations.Services; @@ -116,7 +117,7 @@ public partial class ConversationService : IConversationService db.CreateNewConversation(record); - var hooks = _services.GetServices(); + var hooks = _services.GetHooks(record.AgentId); foreach (var hook in hooks) { diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 853b1561..c41d3110 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -15,6 +15,7 @@ ******************************************************************************/ using BotSharp.Abstraction.Conversations.Enums; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Options; using BotSharp.Abstraction.SideCar; @@ -28,6 +29,7 @@ public class ConversationStateService : IConversationStateService private readonly ILogger _logger; private readonly IServiceProvider _services; private readonly IBotSharpRepository _db; + private readonly IRoutingContext _routingContext; private readonly IConversationSideCar? _sidecar; private string _conversationId; /// @@ -42,10 +44,12 @@ public class ConversationStateService : IConversationStateService public ConversationStateService( IServiceProvider services, IBotSharpRepository db, + IRoutingContext routingContext, ILogger logger) { _services = services; _db = db; + _routingContext = routingContext; _logger = logger; _curStates = new ConversationState(); _historyStates = new ConversationState(); @@ -87,7 +91,6 @@ public class ConversationStateService : IConversationStateService } _logger.LogDebug($"[STATE] {name} = {value}"); - var routingCtx = _services.GetRequiredService(); var isNoChange = ContainsState(name) && preValue == currentValue @@ -98,7 +101,7 @@ public class ConversationStateService : IConversationStateService && prevLeafNode?.Active == curActive && pair?.Readonly == readOnly; - var hooks = _services.GetServices(); + var hooks = _services.GetHooks(_routingContext.GetCurrentAgentId()); if (!ContainsState(name) || preValue != currentValue || prevLeafNode?.ActiveRounds != curActiveRounds) { foreach (var hook in hooks) @@ -106,7 +109,7 @@ public class ConversationStateService : IConversationStateService hook.OnStateChanged(new StateChangeModel { ConversationId = _conversationId, - MessageId = routingCtx.MessageId, + MessageId = _routingContext.MessageId, Name = name, BeforeValue = preValue, BeforeActiveRounds = prevLeafNode?.ActiveRounds, @@ -129,7 +132,7 @@ public class ConversationStateService : IConversationStateService var newValue = new StateValue { Data = currentValue, - MessageId = routingCtx.MessageId, + MessageId = _routingContext.MessageId, Active = curActive, ActiveRounds = curActiveRounds, DataType = valueType, @@ -171,8 +174,7 @@ public class ConversationStateService : IConversationStateService return endNodes; } - var routingCtx = _services.GetRequiredService(); - var curMsgId = routingCtx.MessageId; + var curMsgId = _routingContext.MessageId; var dialogs = _db.GetConversationDialogs(conversationId); var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User) .GroupBy(x => x.MetaData?.MessageId) @@ -225,7 +227,7 @@ public class ConversationStateService : IConversationStateService } _logger.LogInformation($"Loaded conversation states: {conversationId}"); - var hooks = _services.GetServices(); + var hooks = _services.GetHooks(_routingContext.GetCurrentAgentId()); foreach (var hook in hooks) { hook.OnStateLoaded(_curStates).Wait(); @@ -277,7 +279,6 @@ public class ConversationStateService : IConversationStateService { if (!ContainsState(name)) return false; - var routingCtx = _services.GetRequiredService(); var value = _curStates[name]; var leafNode = value?.Values?.LastOrDefault(); if (value == null || !value.Versioning || leafNode == null) return false; @@ -285,7 +286,7 @@ public class ConversationStateService : IConversationStateService _curStates[name].Values.Add(new StateValue { Data = leafNode.Data, - MessageId = routingCtx.MessageId, + MessageId = _routingContext.MessageId, Active = false, ActiveRounds = leafNode.ActiveRounds, DataType = leafNode.DataType, @@ -293,13 +294,13 @@ public class ConversationStateService : IConversationStateService UpdateTime = DateTime.UtcNow }); - var hooks = _services.GetServices(); + var hooks = _services.GetHooks(_routingContext.GetCurrentAgentId()); foreach (var hook in hooks) { hook.OnStateChanged(new StateChangeModel { ConversationId = _conversationId, - MessageId = routingCtx.MessageId, + MessageId = _routingContext.MessageId, Name = name, BeforeValue = leafNode.Data, BeforeActiveRounds = leafNode.ActiveRounds, @@ -316,8 +317,7 @@ public class ConversationStateService : IConversationStateService public void CleanStates(params string[] excludedStates) { - var routingCtx = _services.GetRequiredService(); - var curMsgId = routingCtx.MessageId; + var curMsgId = _routingContext.MessageId; var utcNow = DateTime.UtcNow; foreach (var key in _curStates.Keys) diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs index 95d55596..3615faf5 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs @@ -9,7 +9,7 @@ public static class HookEmitter { var logger = services.GetRequiredService>(); var result = new HookEmittedResult(); - var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); + var hooks = services.GetHooks(agentId); option = option ?? new(); foreach (var hook in hooks) @@ -40,7 +40,7 @@ public static class HookEmitter { var logger = services.GetRequiredService>(); var result = new HookEmittedResult(); - var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); + var hooks = services.GetHooks(agentId); option = option ?? new(); foreach (var hook in hooks) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 3bc069d5..a6422de6 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.MLTasks; @@ -23,7 +24,7 @@ public partial class InstructService } // Trigger before completion hooks - var hooks = _services.GetServices().Where(p => p.IsMatch(agentId)); + var hooks = _services.GetHooks(agentId); foreach (var hook in hooks) { await hook.BeforeCompletion(agent, message); diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs index 719dd805..66cd4484 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Functions; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Core.Routing.Functions; @@ -15,9 +16,7 @@ public class HumanInterventionNeededFn : IFunctionCallback public async Task Execute(RoleDialogModel message) { - var hooks = _services - .GetRequiredService() - .HooksOrderByPriority; + var hooks = _services.GetHooksOrderByPriority(message.CurrentAgentId); foreach (var hook in hooks) { diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs index 126453e8..697e2aa6 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Functions; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Templating; using BotSharp.Core.Routing.Executor; @@ -29,10 +30,6 @@ public partial class RoutingService var clonedMessage = RoleDialogModel.From(message); clonedMessage.FunctionName = name; - var hooks = _services - .GetRequiredService() - .HooksOrderByPriority; - var progressService = _services.GetService(); clonedMessage.Indication = await funcExecutor.GetIndicatorAsync(message); @@ -41,7 +38,8 @@ public partial class RoutingService { await progressService.OnFunctionExecuting(clonedMessage); } - + + var hooks = _services.GetHooksOrderByPriority(clonedMessage.CurrentAgentId); foreach (var hook in hooks) { hook.SetAgent(agent); diff --git a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs index 6c6c2da2..285e8abd 100644 --- a/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs @@ -1,5 +1,6 @@ using Anthropic.SDK.Common; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.MLTasks.Settings; using System.Text.Json.Nodes; using System.Text.Json.Serialization; @@ -29,7 +30,7 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs index 35fd0d2d..92b1a6b5 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -1,5 +1,6 @@ using Azure; using BotSharp.Abstraction.Files.Utilities; +using BotSharp.Abstraction.Hooks; using OpenAI.Chat; using System.ClientModel; @@ -29,7 +30,7 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) @@ -128,7 +129,7 @@ public class ChatCompletionProvider : IChatCompletion Func onMessageReceived, Func onFunctionExecuting) { - var hooks = _services.GetServices().ToList(); + var hooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in hooks) diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs index f3bb7d1f..538bbfaa 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Text/TextCompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.MLTasks.Settings; using System.Net.Http; using System.Net.Mime; @@ -36,7 +37,7 @@ public class TextCompletionProvider : ITextCompletion public async Task GetCompletion(string text, string agentId, string messageId) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agentId); // Before chat completion hook var agent = new Agent() diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs index 7275a273..725655fc 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs @@ -24,7 +24,6 @@ public class ChatHubPlugin : IBotSharpPlugin services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs index 9ef3318e..12b595f2 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SignalR; @@ -26,7 +27,6 @@ public class SignalRHub : Hub { _logger.LogInformation($"SignalR Hub: {_user.FirstName} {_user.LastName} ({Context.User.Identity.Name}) connected in {Context.ConnectionId}"); - var hooks = _services.GetServices(); var convService = _services.GetRequiredService(); _context.HttpContext.Request.Query.TryGetValue("conversationId", out var conversationId); @@ -38,6 +38,7 @@ public class SignalRHub : Hub var conv = await convService.GetConversation(conversationId); if (conv != null) { + var hooks = _services.GetHooks(conv.AgentId); foreach (var hook in hooks) { // Check if user connected with agent is the first time. diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs index 955d0ad2..85304054 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Chat/ChatCompletionProvider.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using OpenAI.Chat; using BotSharp.Abstraction.Files; using BotSharp.Plugin.DeepSeek.Providers; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Plugin.DeepSeekAI.Providers.Chat; @@ -25,7 +26,7 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) @@ -95,7 +96,7 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletionsAsync(Agent agent, List conversations, Func onMessageReceived, Func onFunctionExecuting) { - var hooks = _services.GetServices().ToList(); + var hooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in hooks) diff --git a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs index b9494aff..28ad97f7 100644 --- a/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.DeepSeekAI/Providers/Text/TextCompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using Microsoft.Extensions.Logging; using OpenAI.Chat; @@ -22,7 +23,7 @@ public class TextCompletionProvider : ITextCompletion public async Task GetCompletion(string text, string agentId, string messageId) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agentId); var state = _services.GetRequiredService(); // Before chat completion hook diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs index 4b3a72a8..95dda6c1 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs @@ -2,6 +2,7 @@ using System.Text.Json.Nodes; using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Loggers; using GenerativeAI; using GenerativeAI.Core; @@ -33,7 +34,7 @@ public class GeminiChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) @@ -91,7 +92,7 @@ public class GeminiChatCompletionProvider : IChatCompletion public async Task GetChatCompletionsAsync(Agent agent, List conversations, Func onMessageReceived, Func onFunctionExecuting) { - var hooks = _services.GetServices().ToList(); + var hooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in hooks) diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs index de7e316f..dde9fa57 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/PalmChatCompletionProvider.cs @@ -5,6 +5,7 @@ using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Routing; using LLMSharp.Google.Palm; using LLMSharp.Google.Palm.DiscussService; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Plugin.GoogleAi.Providers.Chat; @@ -29,7 +30,7 @@ public class PalmChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs index fad14730..3f4b8753 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using GenerativeAI; using GenerativeAI.Core; using GenerativeAI.Live; @@ -216,7 +217,7 @@ public class GoogleRealTimeProvider : IRealTimeCompletion } } - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(conn.CurrentAgentId); // After chat completion hook foreach (var hook in contentHooks) { diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs index 7568fddc..abc12c3e 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/GeminiTextCompletionProvider.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Loggers; using GenerativeAI; using GenerativeAI.Core; @@ -32,7 +33,7 @@ public class GeminiTextCompletionProvider : ITextCompletion public async Task GetCompletion(string text, string agentId, string messageId) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agentId); // Before completion hook var agent = new Agent() diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs index ea8ecd5e..b75116d9 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/PalmTextCompletionProvider.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations; +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Loggers; namespace BotSharp.Plugin.GoogleAi.Providers.Text; @@ -27,7 +28,7 @@ public class PalmTextCompletionProvider : ITextCompletion public async Task GetCompletion(string text, string agentId, string messageId) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agentId); // Before completion hook var agent = new Agent() { Id = agentId }; diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs index 9d542b19..95d4b909 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Hooks; using OpenAI.Chat; namespace BotSharp.Plugin.OpenAI.Providers.Chat; @@ -32,7 +33,7 @@ public class ChatCompletionProvider : IChatCompletion public async Task GetChatCompletions(Agent agent, List conversations) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in contentHooks) @@ -105,7 +106,7 @@ public class ChatCompletionProvider : IChatCompletion Func onMessageReceived, Func onFunctionExecuting) { - var hooks = _services.GetServices().ToList(); + var hooks = _services.GetHooks(agent.Id); // Before chat completion hook foreach (var hook in hooks) diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index 23cc5b77..61e7dddc 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Plugin.OpenAI.Models.Realtime; using OpenAI.Chat; @@ -609,7 +610,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion return []; } - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(conn.CurrentAgentId); var prompts = new List(); var inputTokenDetails = data.Usage?.InputTokenDetails; diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs index bf0252ea..fd3cb5a1 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Text/TextCompletionProvider.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using System.Net.Http; using System.Net.Mime; @@ -25,7 +26,7 @@ public class TextCompletionProvider : ITextCompletion public async Task GetCompletion(string text, string agentId, string messageId) { - var contentHooks = _services.GetServices().ToList(); + var contentHooks = _services.GetHooks(agentId); // Before chat completion hook var agent = new Agent() diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs index 0e0682f3..bc647140 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs @@ -3,6 +3,7 @@ using Task = System.Threading.Tasks.Task; using Twilio.Rest.Api.V2010.Account; using BotSharp.Plugin.Twilio.Interfaces; using BotSharp.Plugin.Twilio.Models; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Plugin.Twilio.Hooks; @@ -23,7 +24,7 @@ public class TwilioConversationHook : ConversationHookBase, IConversationHook public override async Task OnFunctionExecuted(RoleDialogModel message) { - var hooks = _services.GetServices(); + var hooks = _services.GetHooks(message.CurrentAgentId); var routing = _services.GetRequiredService(); var conversationId = routing.Context.ConversationId; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs b/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs index 589b2db2..ea4d5c50 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Realtime; using BotSharp.Abstraction.Realtime.Models; @@ -63,7 +64,7 @@ public class TwilioStreamMiddleware // load conversation and state var convService = services.GetRequiredService(); convService.SetConversationId(conversationId, []); - var hooks = services.GetServices(); + var hooks = services.GetHooks(agentId); foreach (var hook in hooks) { await hook.OnStreamingStarted(conn); @@ -229,7 +230,6 @@ public class TwilioStreamMiddleware private async Task HandleUserDtmfReceived(IServiceProvider _services, RealtimeHubConnection conn, IRealTimeCompletion completer, string data) { var routing = _services.GetRequiredService(); - var hookProvider = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); var agent = await agentService.GetAgent(conn.CurrentAgentId); var dialogs = routing.Context.GetDialogs(); @@ -245,7 +245,8 @@ public class TwilioStreamMiddleware var storage = _services.GetRequiredService(); storage.Append(conn.ConversationId, message); - foreach (var hook in hookProvider.HooksOrderByPriority) + var hooks = _services.GetHooksOrderByPriority(conn.CurrentAgentId); + foreach (var hook in hooks) { hook.SetAgent(agent) .SetConversation(conversation);