From df0423d917966370c2de3ffbd1edbd37b62d8ff7 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Thu, 7 Aug 2025 15:38:16 -0500 Subject: [PATCH] Update IRealtimeHook --- .../Realtime/IRealtimeHook.cs | 15 ++++++++---- .../Services/RealtimeHub.cs | 24 +++++++++---------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs b/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs index 51d7383a7..b4fed5143 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs @@ -6,8 +6,15 @@ namespace BotSharp.Abstraction.Realtime; public interface IRealtimeHook : IHookBase { - Task OnModelReady(Agent agent, IRealTimeCompletion completer); - string[] OnModelTranscriptPrompt(Agent agent); - Task OnTranscribeCompleted(RoleDialogModel message, TranscriptionData data); - Task ShouldReconnect(RealtimeHubConnection conn) => Task.FromResult(false); + Task OnModelReady(Agent agent, IRealTimeCompletion completer) + => Task.CompletedTask; + + string[] OnModelTranscriptPrompt(Agent agent) + => []; + + Task OnTranscribeCompleted(RoleDialogModel message, TranscriptionData data) + => Task.CompletedTask; + + Task ShouldReconnect(RealtimeHubConnection conn, RoleDialogModel message) + => Task.FromResult(false); } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs index 0ff601e1e..8c057b950 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs @@ -100,6 +100,17 @@ await HookEmitter.Emit(_services, async hook => await hook.OnRouti } await routing.InvokeFunction(message.FunctionName, message, options: new() { From = InvokeSource.Llm }); + + var hooks = _services.GetHooks(_conn.CurrentAgentId); + foreach (var hook in hooks) + { + if (await hook.ShouldReconnect(_conn, message)) + { + await _completer.Reconnect(_conn); + _logger.LogWarning("Reconnecting to model due to function call: {FunctionName}", message.FunctionName); + break; + } + } } else { @@ -117,19 +128,6 @@ await HookEmitter.Emit(_services, async hook => await hook.OnRouti } } } - - var isReconnect = false; - var realtimeHooks = _services.GetHooks(_conn.CurrentAgentId); - foreach (var hook in realtimeHooks) - { - isReconnect = await hook.ShouldReconnect(_conn); - if (isReconnect) break; - } - - if (isReconnect) - { - await _completer.Reconnect(_conn); - } }, onConversationItemCreated: async response => {