From d76fef38d03a41ac7870c6f4e24880c5abceb797 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 14 Apr 2025 10:45:28 -0500 Subject: [PATCH] add hook --- .../Realtime/Models/RealtimeHubConnection.cs | 1 + .../BotSharp.Core.Realtime/Services/RealtimeHub.cs | 3 +++ .../Providers/Realtime/RealTimeCompletionProvider.cs | 2 +- tests/BotSharp.Test.RealtimeVoice/Program.cs | 10 ++++++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/RealtimeHubConnection.cs b/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/RealtimeHubConnection.cs index b3132df7..0f2af400 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/RealtimeHubConnection.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/RealtimeHubConnection.cs @@ -13,6 +13,7 @@ public class RealtimeHubConnection public Func OnModelMessageReceived { get; set; } = null!; public Func OnModelAudioResponseDone { get; set; } = null!; public Func OnModelUserInterrupted { get; set; } = null!; + public Func OnUserSpeechDetected { get; set; } = () => string.Empty; public void ResetResponseState() { diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs index 4eae3a64..0c36eee3 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs @@ -150,6 +150,9 @@ public class RealtimeHub : IRealtimeHub var data = _conn.OnModelUserInterrupted(); await (responseToUser?.Invoke(data) ?? Task.CompletedTask); } + + var res = _conn.OnUserSpeechDetected(); + await (responseToUser?.Invoke(res) ?? Task.CompletedTask); }); } diff --git a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs index 6b36f805..959ed457 100644 --- a/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs @@ -263,7 +263,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion MaxResponseOutputTokens = realtimeModelSettings.MaxResponseOutputTokens, TurnDetection = new RealtimeSessionTurnDetection { - InterruptResponse = false/*, + InterruptResponse = realtimeModelSettings.InterruptResponse/*, Threshold = realtimeModelSettings.TurnDetection.Threshold, PrefixPadding = realtimeModelSettings.TurnDetection.PrefixPadding, SilenceDuration = realtimeModelSettings.TurnDetection.SilenceDuration*/ diff --git a/tests/BotSharp.Test.RealtimeVoice/Program.cs b/tests/BotSharp.Test.RealtimeVoice/Program.cs index 8498b297..b86d6aae 100644 --- a/tests/BotSharp.Test.RealtimeVoice/Program.cs +++ b/tests/BotSharp.Test.RealtimeVoice/Program.cs @@ -49,14 +49,20 @@ conn.OnModelAudioResponseDone = () => conn.OnModelUserInterrupted = () => JsonSerializer.Serialize(new { - @event = "clear" + @event = "interrupted" + }); + +conn.OnUserSpeechDetected = () => + JsonSerializer.Serialize(new + { + @event = "speech-detected" }); await hub.ConnectToModel(async data => { var response = JsonSerializer.Deserialize(data); - if (response.Event == "clear") + if (response.Event == "speech-detected") { channel.ClearBuffer(); }