diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs index ae6a4eaf4..19e29642c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs @@ -1,13 +1,10 @@ using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Abstraction.Agents; -public interface IAgentHook +public interface IAgentHook : IHookBase { - /// - /// Agent Id - /// - string SelfId { get; } Agent Agent { get; } void SetAgent(Agent agent); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs index b722d3fa0..6d967391c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs @@ -1,6 +1,8 @@ +using BotSharp.Abstraction.Hooks; + namespace BotSharp.Abstraction.Conversations; -public interface IConversationHook +public interface IConversationHook : IHookBase { int Priority { get; } Agent Agent { get; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/ICrontabHook.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/ICrontabHook.cs index 6ad7e06ab..b92fd8f53 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Crontab/ICrontabHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/ICrontabHook.cs @@ -1,6 +1,8 @@ +using BotSharp.Abstraction.Hooks; + namespace BotSharp.Abstraction.Crontab; -public interface ICrontabHook +public interface ICrontabHook : IHookBase { string[]? Triggers => null; diff --git a/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs new file mode 100644 index 000000000..a0ad18e62 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Abstraction.Hooks +{ + public interface IHookBase + { + /// + /// Agent Id + /// + string SelfId => string.Empty; + bool IsMatch(string id) => string.IsNullOrEmpty(SelfId) || SelfId == id; + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs index c9fa1cd72..23b8bf60b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs @@ -1,10 +1,10 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Instructs.Models; namespace BotSharp.Abstraction.Instructs; -public interface IInstructHook +public interface IInstructHook : IHookBase { - string SelfId { get; } Task BeforeCompletion(Agent agent, RoleDialogModel message); Task AfterCompletion(Agent agent, InstructResult result); Task OnResponseGenerated(InstructResponseModel response); diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/IContentGeneratingHook.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/IContentGeneratingHook.cs index 03b69f5f2..79f71d701 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/IContentGeneratingHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/IContentGeneratingHook.cs @@ -1,11 +1,12 @@ using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Abstraction.Loggers; /// /// Model content generating hook, it can be used for logging, metrics and tracing. /// -public interface IContentGeneratingHook +public interface IContentGeneratingHook : IHookBase { /// /// Before content generating. diff --git a/src/Infrastructure/BotSharp.Abstraction/Planning/IPlanningHook.cs b/src/Infrastructure/BotSharp.Abstraction/Planning/IPlanningHook.cs index 4dcff7cb2..b65c1a7e3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Planning/IPlanningHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Planning/IPlanningHook.cs @@ -1,6 +1,8 @@ +using BotSharp.Abstraction.Hooks; + namespace BotSharp.Abstraction.Planning; -public interface IPlanningHook +public interface IPlanningHook : IHookBase { Task GetSummaryAdditionalRequirements(string planner, RoleDialogModel message) => Task.FromResult(string.Empty); diff --git a/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs b/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs index 95608ff0d..bd131b93a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs @@ -1,8 +1,9 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.MLTasks; namespace BotSharp.Abstraction.Realtime; -public interface IRealtimeHook +public interface IRealtimeHook : IHookBase { Task OnModelReady(Agent agent, IRealTimeCompletion completer); string[] OnModelTranscriptPrompt(Agent agent); diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs index c60bf4f1d..b893c8c15 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs @@ -1,8 +1,9 @@ using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Hooks; namespace BotSharp.Abstraction.Routing; -public interface IRoutingHook +public interface IRoutingHook : IHookBase { /// /// Routing instruction is received from Router diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs index 7ff105c94..255faa71c 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs @@ -125,6 +125,6 @@ await HookEmitter.Emit(_services, async hook => await hook.OnCronTriggered(item); await hook.OnTaskExecuted(item); } - }); + }, item.AgentId); } } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs index 607297582..cc9fa0c1a 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs @@ -49,7 +49,8 @@ await _completer.Connect( // Not TriggerModelInference, waiting for user utter. var instruction = await _completer.UpdateSession(_conn, isInit: true); var data = _conn.OnModelReady(); - await HookEmitter.Emit(_services, async hook => await hook.OnModelReady(agent, _completer)); + await HookEmitter.Emit(_services, async hook => await hook.OnModelReady(agent, _completer), + agent.Id); await (init?.Invoke(data) ?? Task.CompletedTask); }, onModelAudioDeltaReceived: async (audioDeltaData, itemId) => @@ -92,7 +93,8 @@ await _completer.Connect( if (message.FunctionName == "route_to_agent") { var instruction = JsonSerializer.Deserialize(message.FunctionArgs, BotSharpOptions.defaultJsonOptions); - await HookEmitter.Emit(_services, async hook => await hook.OnRoutingInstructionReceived(instruction, message)); + await HookEmitter.Emit(_services, async hook => await hook.OnRoutingInstructionReceived(instruction, message), + agent.Id); } await routing.InvokeFunction(message.FunctionName, message); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 636bc811c..37e3d4450 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Infrastructures; using BotSharp.Abstraction.Routing.Models; using System.Collections.Concurrent; @@ -10,29 +11,12 @@ public partial class AgentService // [SharpCache(10, perInstanceCache: true)] public async Task LoadAgent(string id, bool loadUtility = true) { - if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString()) - { - return null; - } + if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString()) return null; - var hooks = _services.GetServices(); - - // Before agent is loaded. - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id) - { - continue; - } - - hook.OnAgentLoading(ref id); - } + HookEmitter.Emit(_services, hook => hook.OnAgentLoading(ref id), id); var agent = await GetAgent(id); - if (agent == null) - { - return null; - } + if (agent == null) return null; await InheritAgent(agent); OverrideInstructionByChannel(agent); @@ -43,13 +27,7 @@ public async Task LoadAgent(string id, bool loadUtility = true) PopulateState(agent.TemplateDict); // After agent is loaded - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id) - { - continue; - } - + HookEmitter.Emit(_services, hook => { hook.SetAgent(agent); if (!string.IsNullOrEmpty(agent.Instruction)) @@ -72,13 +50,14 @@ public async Task LoadAgent(string id, bool loadUtility = true) hook.OnAgentUtilityLoaded(agent); } - if(!agent.McpTools.IsNullOrEmpty()) + if (!agent.McpTools.IsNullOrEmpty()) { hook.OnAgentMcpToolLoaded(agent); } - + hook.OnAgentLoaded(agent); - } + + }, id); _logger.LogInformation($"Loaded agent {agent}."); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs index ef36f4d79..f862482ca 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs @@ -132,9 +132,8 @@ public string RenderedTemplate(Agent agent, string templateName) agent.TemplateDict[TemplateRenderConstant.RENDER_AGENT] = agent; var content = render.Render(template, agent.TemplateDict); - HookEmitter.Emit(_services, async hook => - await hook.OnRenderingTemplate(agent, templateName, content) - ).Wait(); + HookEmitter.Emit(_services, async hook => await hook.OnRenderingTemplate(agent, templateName, content), + agent.Id).Wait(); return content; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 5ec1ae761..b2b97f4fe 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -158,16 +158,14 @@ response.RichContent is RichContent template && // Emit conversation ending hook if (response.Instruction.ConversationEnd) { - await HookEmitter.Emit(_services, async hook => - await hook.OnConversationEnding(response) - ); + await HookEmitter.Emit(_services, async hook => await hook.OnConversationEnding(response), + response.CurrentAgentId); response.FunctionName = "conversation_end"; } } - await HookEmitter.Emit(_services, async hook => - await hook.OnResponseGenerated(response) - ); + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(response), + response.CurrentAgentId); await onResponseReceived(response); diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs index defd14592..dd2e02d40 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Image.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Instructs; using System.IO; +using BotSharp.Abstraction.Infrastructures; namespace BotSharp.Core.Files.Services; @@ -24,14 +25,7 @@ public async Task ReadImages(string text, IEnumerable } }); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -41,8 +35,7 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = text, SystemInstruction = instruction, CompletionText = message.Content - }); - } + }), innerAgentId); return message.Content; } @@ -59,14 +52,7 @@ public async Task GenerateImage(string text, InstructOptions? o Instruction = instruction }, new RoleDialogModel(AgentRole.User, instruction ?? text)); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -76,8 +62,7 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = text, SystemInstruction = instruction, CompletionText = message.Content - }); - } + }), innerAgentId); return message; } @@ -104,14 +89,7 @@ public async Task VaryImage(InstructFileModel image, InstructOp stream.Close(); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -119,8 +97,7 @@ await hook.OnResponseGenerated(new InstructResponseModel Model = completion.Model, UserMessage = string.Empty, CompletionText = message.Content - }); - } + }), innerAgentId); return message; } @@ -149,14 +126,7 @@ public async Task EditImage(string text, InstructFileModel imag stream.Close(); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -166,8 +136,7 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = text, SystemInstruction = instruction, CompletionText = message.Content - }); - } + }), innerAgentId); return message; } @@ -205,14 +174,7 @@ public async Task EditImage(string text, InstructFileModel imag imageStream.Close(); maskStream.Close(); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -222,8 +184,7 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = text, SystemInstruction = instruction, CompletionText = message.Content - }); - } + }), innerAgentId); return message; } diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs index 2336363ad..cd703cc7b 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Files.Converters; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Instructs; +using BotSharp.Abstraction.Infrastructures; namespace BotSharp.Core.Files.Services; @@ -42,14 +43,7 @@ public async Task ReadPdf(string text, List files, In } }); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != innerAgentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = innerAgentId, @@ -59,8 +53,7 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = text, SystemInstruction = instruction, CompletionText = message.Content - }); - } + }), innerAgentId); return message.Content; } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs index a4baf81cc..95d55596f 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs @@ -1,14 +1,15 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Infrastructures; namespace BotSharp.Core.Infrastructures; public static class HookEmitter { - public static HookEmittedResult Emit(IServiceProvider services, Action action, HookEmitOption? option = null) + public static HookEmittedResult Emit(IServiceProvider services, Action action, string agentId, HookEmitOption? option = null) where T : IHookBase { var logger = services.GetRequiredService>(); var result = new HookEmittedResult(); - var hooks = services.GetServices(); + var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); option = option ?? new(); foreach (var hook in hooks) @@ -35,11 +36,11 @@ public static HookEmittedResult Emit(IServiceProvider services, Action act return result; } - public static async Task Emit(IServiceProvider services, Func action, HookEmitOption? option = null) + public static async Task Emit(IServiceProvider services, Func action, string agentId, HookEmitOption? option = null) where T : IHookBase { var logger = services.GetRequiredService>(); var result = new HookEmittedResult(); - var hooks = services.GetServices(); + var hooks = services.GetServices().Where(p => p.IsMatch(agentId)); option = option ?? new(); foreach (var hook in hooks) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs b/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs index 346218dd4..7d8bcb468 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Functions/ExecuteTemplateFn.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Functions; +using BotSharp.Abstraction.Infrastructures; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; @@ -60,14 +61,7 @@ private async Task GetAiResponse(Agent agent, string templateName) new(AgentRole.User, text) }); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agent.Id) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = agent.Id, @@ -76,8 +70,7 @@ await hook.OnResponseGenerated(new InstructResponseModel Model = completion.Model, UserMessage = text, CompletionText = response.Content - }); - } + }), agent.Id); return response.Content; } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 71a7488b8..3bc069d55 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -23,14 +23,9 @@ public async Task Execute(string agentId, RoleDialogModel messag } // Trigger before completion hooks - var hooks = _services.GetServices(); + var hooks = _services.GetServices().Where(p => p.IsMatch(agentId)); foreach (var hook in hooks) { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) - { - continue; - } - await hook.BeforeCompletion(agent, message); // Interrupted by hook @@ -99,11 +94,6 @@ public async Task Execute(string agentId, RoleDialogModel messag foreach (var hook in hooks) { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) - { - continue; - } - await hook.AfterCompletion(agent, response); await hook.OnResponseGenerated(new InstructResponseModel { diff --git a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/InstructExecutor.cs b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/InstructExecutor.cs index e911437d9..c93e133ce 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/InstructExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/InstructExecutor.cs @@ -25,9 +25,8 @@ public async Task Execute(IRoutingService routing, { inst.OriginalAgent = goalAgent; // Emit hook - await HookEmitter.Emit(_services, async hook => - await hook.OnRoutingInstructionRevised(inst, message) - ); + await HookEmitter.Emit(_services, async hook => await hook.OnRoutingInstructionRevised(inst, message), + message.CurrentAgentId); } message.FunctionArgs = JsonSerializer.Serialize(inst); diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index 1c4ca518a..b422a4235 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -99,9 +99,8 @@ public void Push(string agentId, string? reason = null, bool updateLazyRouting = var preAgentId = _stack.Count == 0 ? agentId : _stack.Peek(); _stack.Push(agentId); - HookEmitter.Emit(_services, async hook => - await hook.OnAgentEnqueued(agentId, preAgentId, reason: reason) - ).Wait(); + HookEmitter.Emit(_services, async hook => await hook.OnAgentEnqueued(agentId, preAgentId, reason: reason), + agentId).Wait(); UpdateLazyRoutingAgent(updateLazyRouting); } @@ -120,9 +119,8 @@ public void Pop(string? reason = null, bool updateLazyRouting = true) var agentId = _stack.Pop(); var currentAgentId = GetCurrentAgentId(); - HookEmitter.Emit(_services, async hook => - await hook.OnAgentDequeued(agentId, currentAgentId, reason: reason) - ).Wait(); + HookEmitter.Emit(_services, async hook => await hook.OnAgentDequeued(agentId, currentAgentId, reason: reason), + agentId).Wait(); if (string.IsNullOrEmpty(currentAgentId)) { @@ -203,9 +201,8 @@ public void Replace(string agentId, string? reason = null, bool updateLazyRoutin _stack.Pop(); _stack.Push(agentId); - HookEmitter.Emit(_services, async hook => - await hook.OnAgentReplaced(fromAgent, toAgent, reason: reason) - ).Wait(); + HookEmitter.Emit(_services, async hook => await hook.OnAgentReplaced(fromAgent, toAgent, reason: reason), + agentId).Wait(); } UpdateLazyRoutingAgent(updateLazyRouting); @@ -220,9 +217,8 @@ public void Empty(string? reason = null) var agentId = GetCurrentAgentId(); _stack.Clear(); - HookEmitter.Emit(_services, async hook => - await hook.OnAgentQueueEmptied(agentId, reason: reason) - ).Wait(); + HookEmitter.Emit(_services, async hook => await hook.OnAgentQueueEmptied(agentId, reason: reason), + agentId).Wait(); } public void SetMessageId(string conversationId, string messageId) diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs index 364e71987..128debadd 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs @@ -51,9 +51,8 @@ public async Task InstructLoop(Agent agent, RoleDialogModel mes int loopCount = 1; while (true) { - await HookEmitter.Emit(_services, async hook => - await hook.OnRoutingInstructionReceived(inst, message) - ); + await HookEmitter.Emit(_services, async hook => await hook.OnRoutingInstructionReceived(inst, message), + agent.Id); // Save states states.SaveStateByArgs(inst.Arguments); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 5e180f22b..897c34e73 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -345,7 +345,8 @@ public async Task SendNotification([FromRoute] string convers }; await HookEmitter.Emit(_services, async hook => - await hook.OnNotificationGenerated(inputMsg) + await hook.OnNotificationGenerated(inputMsg), + routing.Context.GetCurrentAgentId() ); return response; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index ca3ec45a8..430a45633 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Files.Utilities; +using BotSharp.Abstraction.Infrastructures; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Core.Infrastructures; @@ -58,14 +59,7 @@ public async Task TextCompletion([FromBody] IncomingInstructRequest inpu var textCompletion = CompletionProvider.GetTextCompletion(_services); var response = await textCompletion.GetCompletion(input.Text, agentId, Guid.NewGuid().ToString()); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = agentId, @@ -74,8 +68,8 @@ await hook.OnResponseGenerated(new InstructResponseModel TemplateName = input.Template, UserMessage = input.Text, CompletionText = response - }); - } + }), agentId); + return response; } @@ -103,14 +97,7 @@ public async Task ChatCompletion([FromBody] IncomingInstructRequest inpu } }); - var hooks = _services.GetServices(); - foreach (var hook in hooks) - { - if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != agentId) - { - continue; - } - + await HookEmitter.Emit(_services, async hook => await hook.OnResponseGenerated(new InstructResponseModel { AgentId = agentId, @@ -120,8 +107,8 @@ await hook.OnResponseGenerated(new InstructResponseModel UserMessage = input.Text, SystemInstruction = message.RenderedInstruction, CompletionText = message.Content - }); - } + }), agentId); + return message.Content; } #endregion diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs index 98896ac3c..fad14730c 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -255,7 +255,7 @@ public async Task UpdateSession(RealtimeHubConnection conn, bool isInit config.ResponseModalities = new List([Modality.AUDIO]); var words = new List(); - HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent))); + HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), agent.Id); var realtimeModelSettings = _services.GetRequiredService(); @@ -278,7 +278,7 @@ public async Task UpdateSession(RealtimeHubConnection conn, bool isInit }).ToArray(); await HookEmitter.Emit(_services, - async hook => { await hook.OnSessionUpdated(agent, prompt, functions, isInit); }); + async hook => { await hook.OnSessionUpdated(agent, prompt, functions, isInit); }, agent.Id); if (_settings.Gemini.UseGoogleSearch) { diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index b32c0e1c1..23cc5b773 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -319,7 +319,7 @@ public async Task UpdateSession(RealtimeHubConnection conn, bool isInit if (realtimeModelSettings.InputAudioTranscribe) { var words = new List(); - HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent))); + HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), agent.Id); sessionUpdate.session.InputAudioTranscription = new InputAudioTranscription { @@ -332,7 +332,7 @@ public async Task UpdateSession(RealtimeHubConnection conn, bool isInit await HookEmitter.Emit(_services, async hook => { await hook.OnSessionUpdated(agent, instruction, functions, isInit); - }); + }, agent.Id); await SendEventToModel(sessionUpdate); await Task.Delay(300); diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs index 4965a67ce..11fd2cf72 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs @@ -90,7 +90,7 @@ await HookEmitter.Emit(_services, async x => { var requirement = await x.GetSummaryAdditionalRequirements(nameof(SqlGenerationPlanner), message); additionalRequirements.Add(requirement); - }); + }, message.CurrentAgentId); var globalKnowledges = new List(); foreach (var hook in knowledgeHooks) diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlReviewFn.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlReviewFn.cs index 6996fb6de..4255dd7f9 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlReviewFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlReviewFn.cs @@ -30,7 +30,8 @@ public async Task Execute(RoleDialogModel message) if (args != null && !args.IsSqlTemplate && args.ContainsSqlStatements) { await HookEmitter.Emit(_services, async hook => - await hook.OnSourceCodeGenerated(nameof(SqlGenerationPlanner), message, "sql") + await hook.OnSourceCodeGenerated(nameof(SqlGenerationPlanner), message, "sql"), + message.CurrentAgentId ); } return true; diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs index 465f1df20..4212d0c28 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs @@ -68,7 +68,8 @@ public async Task Execute(RoleDialogModel message) message.Content = summary.Content; await HookEmitter.Emit(_services, async hook => - await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message) + await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message), + message.CurrentAgentId ); return true; @@ -88,7 +89,7 @@ await HookEmitter.Emit(_services, async x => { var requirement = await x.GetSummaryAdditionalRequirements(nameof(TwoStageTaskPlanner), message); additionalRequirements.Add(requirement); - }); + }, message.CurrentAgentId); var globalKnowledges = new List(); foreach (var hook in knowledgeHooks) diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverPlanningHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverPlanningHook.cs index 168929ead..99a7744fa 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverPlanningHook.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverPlanningHook.cs @@ -30,7 +30,7 @@ public async Task OnSourceCodeGenerated(string planner, RoleDialogModel msg, str await HookEmitter.Emit(_services, async (hook) => { await hook.SqlGenerated(msg); - }); + }, msg.CurrentAgentId); var settings = _services.GetRequiredService(); if (!settings.ExecuteSqlSelectAutonomous) diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Interfaces/ISqlDriverHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Interfaces/ISqlDriverHook.cs index b4871e668..63cff60b3 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Interfaces/ISqlDriverHook.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Interfaces/ISqlDriverHook.cs @@ -1,6 +1,8 @@ +using BotSharp.Abstraction.Hooks; + namespace BotSharp.Plugin.SqlDriver.Interfaces; -public interface ISqlDriverHook +public interface ISqlDriverHook : IHookBase { // Get database type string GetDatabaseType(RoleDialogModel message); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs index 1db293f20..511abb455 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs @@ -56,7 +56,7 @@ public async Task InitiateStreamConversation(ConversationalVoiceReq await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreating(request, instruction); - }); + }, request.AgentId); var (agent, conversationId) = await InitConversation(request); request.ConversationId = conversationId.Id; @@ -67,12 +67,8 @@ await HookEmitter.Emit(_services, async hook => { response = new VoiceResponse(); - var emitOptions = new HookEmitOption - { - ShouldExecute = hook => hook.IsMatch(request) - }; await HookEmitter.Emit(_services, - async hook => await hook.OnVoicemailStarting(request), emitOptions); + async hook => await hook.OnVoicemailStarting(request), request.AgentId); var url = twilio.GetSpeechPath(request.ConversationId, "voicemail.mp3"); response.Play(new Uri(url)); @@ -123,7 +119,7 @@ await HookEmitter.Emit(_services, await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreated(request); - }); + }, request.AgentId); return TwiML(response); } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioOutboundController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioOutboundController.cs index 4cb13a619..5543af38a 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioOutboundController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioOutboundController.cs @@ -33,13 +33,8 @@ public async Task InitiateOutboundCall(ConversationalVoiceRequest r if (twilio.MachineDetected(request)) { response = new VoiceResponse(); - - var emitOptions = new HookEmitOption - { - ShouldExecute = hook => hook.IsMatch(request) - }; await HookEmitter.Emit(_services, - async hook => await hook.OnVoicemailStarting(request), emitOptions); + async hook => await hook.OnVoicemailStarting(request), request.AgentId); var url = twilio.GetSpeechPath(request.ConversationId, "voicemail.mp3"); response.Play(new Uri(url)); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs index 8bc26a8e9..3d13dc739 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs @@ -37,11 +37,7 @@ public async Task PhoneRecordingStatus(ConversationalVoiceRequest convService.SaveStates(); // recording completed - var emitOptions = new HookEmitOption - { - ShouldExecute = hook => hook.IsMatch(request) - }; - await HookEmitter.Emit(_services, x => x.OnRecordingCompleted(request), emitOptions); + await HookEmitter.Emit(_services, x => x.OnRecordingCompleted(request), request.AgentId); } else { diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs index 1fa020c00..778aa29dd 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs @@ -49,7 +49,8 @@ public async Task PhoneRecordingTranscribe(ConversationalVoiceRequ // transcription completed transcript.Language = request.LanguageCode; - await HookEmitter.Emit(_services, async x => await x.OnTranscribeCompleted(message, transcript)); + await HookEmitter.Emit(_services, async x => await x.OnTranscribeCompleted(message, transcript), + request.AgentId); } } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs index 048d75f38..786f116cd 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs @@ -65,7 +65,7 @@ public async Task InitiateConversation(ConversationalVoiceRequest r await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreating(request, instruction); - }); + }, request.AgentId); var twilio = _services.GetRequiredService(); if (string.IsNullOrWhiteSpace(request.Intent)) @@ -98,7 +98,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnSessionCreated(request); - }); + }, request.AgentId); return TwiML(response); } @@ -151,7 +151,7 @@ public async Task ReceiveCallerMessage(ConversationalVoiceRequest r await HookEmitter.Emit(_services, async hook => { await hook.OnReceivedUserMessage(request); - }); + }, request.AgentId); } else { @@ -161,7 +161,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnAgentHangUp(request); - }); + }, request.AgentId); response = twilio.HangUp(string.Empty); } @@ -185,7 +185,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnWaitingUserResponse(request, instruction); - }); + }, request.AgentId); response = twilio.ReturnInstructions(instruction); } @@ -223,7 +223,7 @@ await HookEmitter.Emit(_services, async hook => { request.AIResponseErrorMessage = $"AI response timeout: AIResponseWaitTime greater than {request.AIResponseWaitTime}, please check internal error log!"; await hook.OnAgentHangUp(request); - }); + }, request.AgentId); response = twilio.HangUp($"twilio/error.mp3"); } @@ -238,7 +238,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnAgentTransferring(request, _settings); - }); + }, request.AgentId); response = twilio.DialCsrAgent($"twilio/voice/speeches/{request.ConversationId}/{reply.SpeechFileName}"); } @@ -249,7 +249,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnAgentHangUp(request); - }); + }, request.AgentId); } else { @@ -274,7 +274,7 @@ await HookEmitter.Emit(_services, async hook => await HookEmitter.Emit(_services, async hook => { await hook.OnAgentResponsing(request, instruction); - }); + }, request.AgentId); response = twilio.ReturnInstructions(instruction); } @@ -343,41 +343,35 @@ public async Task PhoneCallStatus(ConversationalVoiceRequest reque { var twilio = _services.GetRequiredService(); - // Define the options with the predicate - var emitOptions = new HookEmitOption - { - ShouldExecute = hook => hook.IsMatch(request) - }; - switch (request.CallStatus) { case "completed": if (twilio.MachineDetected(request)) { // voicemail - await HookEmitter.Emit(_services, hook => hook.OnVoicemailLeft(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnVoicemailLeft(request), request.AgentId); } else { // phone call completed - await HookEmitter.Emit(_services, hook => hook.OnUserDisconnected(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnUserDisconnected(request), request.AgentId); } break; case "busy": - await HookEmitter.Emit(_services, hook => hook.OnCallBusyStatus(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnCallBusyStatus(request), request.AgentId); break; case "no-answer": - await HookEmitter.Emit(_services, hook => hook.OnCallNoAnswerStatus(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnCallNoAnswerStatus(request), request.AgentId); break; case "canceled": - await HookEmitter.Emit(_services, hook => hook.OnCallCanceledStatus(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnCallCanceledStatus(request), request.AgentId); break; case "failed": - await HookEmitter.Emit(_services, hook => hook.OnCallFailedStatus(request), emitOptions); + await HookEmitter.Emit(_services, hook => hook.OnCallFailedStatus(request), request.AgentId); break; default: _logger.LogError($"Unknown call status: {request.CallStatus}, {request.CallSid}"); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioCallStatusHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioCallStatusHook.cs index ae4615952..e7ce09325 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioCallStatusHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioCallStatusHook.cs @@ -1,11 +1,11 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Plugin.Twilio.Models; using Task = System.Threading.Tasks.Task; namespace BotSharp.Plugin.Twilio.Interfaces; -public interface ITwilioCallStatusHook +public interface ITwilioCallStatusHook : IHookBase { - bool IsMatch(ConversationalVoiceRequest request) => true; Task OnVoicemailLeft(ConversationalVoiceRequest request) => Task.CompletedTask; Task OnUserDisconnected(ConversationalVoiceRequest request) => Task.CompletedTask; Task OnRecordingCompleted(ConversationalVoiceRequest request) => Task.CompletedTask; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioSessionHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioSessionHook.cs index 405254754..00837b6a4 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioSessionHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Interfaces/ITwilioSessionHook.cs @@ -1,10 +1,11 @@ +using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Realtime.Models; using BotSharp.Plugin.Twilio.Models; using Task = System.Threading.Tasks.Task; namespace BotSharp.Plugin.Twilio.Interfaces; -public interface ITwilioSessionHook +public interface ITwilioSessionHook : IHookBase { /// /// Before session creating diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs index 0ca10949a..84f88b5b3 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs @@ -157,7 +157,8 @@ private static string GetHints(string agentId, AssistantMessage reply, IServiceP var agentService = sp.GetRequiredService(); var agent = agentService.GetAgent(agentId).Result; var extraWords = new List(); - HookEmitter.Emit(sp, hook => extraWords.AddRange(hook.OnModelTranscriptPrompt(agent))); + HookEmitter.Emit(sp, hook => extraWords.AddRange(hook.OnModelTranscriptPrompt(agent)), + agentId); var phrases = reply.Content.Split(',', StringSplitOptions.RemoveEmptyEntries); int capcity = 100; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs index 7bc95382b..e0618aaf6 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs @@ -235,7 +235,8 @@ public VoiceResponse ReturnBidirectionalMediaStreamsInstructions(ConversationalV if (_settings.TranscribeEnabled) { var words = new List(); - HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent))); + HookEmitter.Emit(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), + agent.Id); var hints = string.Join(", ", words); var start = new Start(); start.Transcription( @@ -323,10 +324,8 @@ public async Task WaitingForAiResponse(ConversationalVoiceRequest ActionOnEmptyResult = true }; - await HookEmitter.Emit(_services, async hook => - { - await hook.OnWaitingAgentResponse(request, instruction); - }); + await HookEmitter.Emit(_services, async hook => await hook.OnWaitingAgentResponse(request, instruction), + request.AgentId); response = ReturnInstructions(instruction); }