Skip to content

Commit d64392b

Browse files
authored
Merge pull request #1121 from hchen2020/master
Update IRealtimeHook
2 parents 87124c4 + df0423d commit d64392b

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/Infrastructure/BotSharp.Abstraction/Realtime/IRealtimeHook.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ namespace BotSharp.Abstraction.Realtime;
66

77
public interface IRealtimeHook : IHookBase
88
{
9-
Task OnModelReady(Agent agent, IRealTimeCompletion completer);
10-
string[] OnModelTranscriptPrompt(Agent agent);
11-
Task OnTranscribeCompleted(RoleDialogModel message, TranscriptionData data);
12-
Task<bool> ShouldReconnect(RealtimeHubConnection conn) => Task.FromResult(false);
9+
Task OnModelReady(Agent agent, IRealTimeCompletion completer)
10+
=> Task.CompletedTask;
11+
12+
string[] OnModelTranscriptPrompt(Agent agent)
13+
=> [];
14+
15+
Task OnTranscribeCompleted(RoleDialogModel message, TranscriptionData data)
16+
=> Task.CompletedTask;
17+
18+
Task<bool> ShouldReconnect(RealtimeHubConnection conn, RoleDialogModel message)
19+
=> Task.FromResult(false);
1320
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
100100
}
101101

102102
await routing.InvokeFunction(message.FunctionName, message, options: new() { From = InvokeSource.Llm });
103+
104+
var hooks = _services.GetHooks<IRealtimeHook>(_conn.CurrentAgentId);
105+
foreach (var hook in hooks)
106+
{
107+
if (await hook.ShouldReconnect(_conn, message))
108+
{
109+
await _completer.Reconnect(_conn);
110+
_logger.LogWarning("Reconnecting to model due to function call: {FunctionName}", message.FunctionName);
111+
break;
112+
}
113+
}
103114
}
104115
else
105116
{
@@ -117,19 +128,6 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
117128
}
118129
}
119130
}
120-
121-
var isReconnect = false;
122-
var realtimeHooks = _services.GetHooks<IRealtimeHook>(_conn.CurrentAgentId);
123-
foreach (var hook in realtimeHooks)
124-
{
125-
isReconnect = await hook.ShouldReconnect(_conn);
126-
if (isReconnect) break;
127-
}
128-
129-
if (isReconnect)
130-
{
131-
await _completer.Reconnect(_conn);
132-
}
133131
},
134132
onConversationItemCreated: async response =>
135133
{

0 commit comments

Comments
 (0)