Skip to content

Commit 89fc99f

Browse files
author
Haiping Chen
committed
prevent initial response interruption
1 parent 9c50824 commit 89fc99f

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/Infrastructure/BotSharp.Abstraction/MLTasks/IRealTimeCompletion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Task Connect(RealtimeHubConnection conn,
2424
Task Disconnect();
2525

2626
Task<RealtimeSession> CreateSession(Agent agent, List<RoleDialogModel> conversations);
27-
Task UpdateSession(RealtimeHubConnection conn);
27+
Task UpdateSession(RealtimeHubConnection conn, bool turnDetection = true);
2828
Task InsertConversationItem(RoleDialogModel message);
2929
Task RemoveConversationItem(string itemId);
3030
Task TriggerModelInference(string? instructions = null);

src/Infrastructure/BotSharp.Core/Realtime/RealtimeHub.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ private async Task ConnectToModel(IRealTimeCompletion completer, WebSocket userW
9999
await completer.Connect(conn,
100100
onModelReady: async () =>
101101
{
102-
// Control initial session
103-
await completer.UpdateSession(conn);
102+
// Control initial session, prevent initial response interruption
103+
await completer.UpdateSession(conn, turnDetection: false);
104104

105105
// Add dialog history
106106
foreach (var item in dialogs)
@@ -110,12 +110,16 @@ await completer.Connect(conn,
110110

111111
if (dialogs.LastOrDefault()?.Role == AgentRole.Assistant)
112112
{
113-
// await completer.TriggerModelInference($"Rephase your last response:\r\n{dialogs.LastOrDefault()?.Content}");
113+
await completer.TriggerModelInference($"Rephase your last response:\r\n{dialogs.LastOrDefault()?.Content}");
114114
}
115115
else
116116
{
117117
await completer.TriggerModelInference("Reply based on the conversation context.");
118118
}
119+
120+
// Start turn detection
121+
await Task.Delay(1000 * 8);
122+
await completer.UpdateSession(conn, turnDetection: true);
119123
},
120124
onModelAudioDeltaReceived: async audioDeltaData =>
121125
{

src/Plugins/BotSharp.Plugin.OpenAI/Models/Realtime/RealtimeSessionBody.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class RealtimeSessionBody
4747
public FunctionDef[] Tools { get; set; } = [];
4848

4949
[JsonPropertyName("turn_detection")]
50-
public RealtimeSessionTurnDetection TurnDetection { get; set; } = new();
50+
public RealtimeSessionTurnDetection? TurnDetection { get; set; } = new();
5151
}
5252

5353
public class RealtimeSessionTurnDetection

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ public async Task Connect(RealtimeHubConnection conn,
5959

6060
if (_webSocket.State == WebSocketState.Open)
6161
{
62-
onModelReady();
63-
6462
// Receive a message
6563
_ = ReceiveMessage(conn,
64+
onModelReady,
6665
onModelAudioDeltaReceived,
6766
onModelAudioResponseDone,
6867
onAudioTranscriptDone,
@@ -122,7 +121,8 @@ await SendEventToModel(new
122121
});
123122
}
124123

125-
private async Task ReceiveMessage(RealtimeHubConnection conn,
124+
private async Task ReceiveMessage(RealtimeHubConnection conn,
125+
Action onModelReady,
126126
Action<string> onModelAudioDeltaReceived,
127127
Action onModelAudioResponseDone,
128128
Action<string> onAudioTranscriptDone,
@@ -156,6 +156,7 @@ private async Task ReceiveMessage(RealtimeHubConnection conn,
156156
else if (response.Type == "session.created")
157157
{
158158
_logger.LogInformation($"{response.Type}: {receivedText}");
159+
onModelReady();
159160
}
160161
else if (response.Type == "session.updated")
161162
{
@@ -295,7 +296,7 @@ public async Task<RealtimeSession> CreateSession(Agent agent, List<RoleDialogMod
295296
return session;
296297
}
297298

298-
public async Task UpdateSession(RealtimeHubConnection conn)
299+
public async Task UpdateSession(RealtimeHubConnection conn, bool turnDetection = true)
299300
{
300301
var convService = _services.GetRequiredService<IConversationService>();
301302
var conv = await convService.GetConversation(conn.ConversationId);
@@ -346,6 +347,11 @@ public async Task UpdateSession(RealtimeHubConnection conn)
346347
}
347348
};
348349

350+
if (!turnDetection)
351+
{
352+
sessionUpdate.session.TurnDetection = null;
353+
}
354+
349355
await HookEmitter.Emit<IContentGeneratingHook>(_services, async hook =>
350356
{
351357
await hook.OnSessionUpdated(agent, instruction, functions);

src/Plugins/BotSharp.Plugin.Twilio/Services/Stream/TwilioStreamMiddleware.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using BotSharp.Abstraction.Realtime;
22
using BotSharp.Abstraction.Realtime.Models;
3-
using BotSharp.Core.Infrastructures;
43
using BotSharp.Plugin.Twilio.Interfaces;
54
using BotSharp.Plugin.Twilio.Models.Stream;
65
using Microsoft.AspNetCore.Http;
76
using System.Net.WebSockets;
8-
using System.Text.Json;
9-
using System.Collections.Concurrent;
10-
using System.Text;
117
using Task = System.Threading.Tasks.Task;
128

139
namespace BotSharp.Plugin.Twilio.Services.Stream;

0 commit comments

Comments
 (0)