Skip to content

Commit 5127c65

Browse files
authored
Merge pull request #1093 from iceljc/master
Refine realtime and twilio
2 parents 1821772 + 81bb38b commit 5127c65

File tree

9 files changed

+34
-34
lines changed

9 files changed

+34
-34
lines changed

src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static MethodInfo GetMethod(string name)
7979
object? res = null;
8080
var isHandled = false;
8181

82-
var enabled = instance != null && instance.IsEnabled() && method != null;
82+
var enabled = instance != null && instance.IsEnabled && method != null;
8383
if (!enabled)
8484
{
8585
return (isHandled, value);
@@ -112,7 +112,7 @@ private static MethodInfo GetMethod(string name)
112112
object? value = null;
113113
var isHandled = false;
114114

115-
var enabled = instance != null && instance.IsEnabled() && method != null;
115+
var enabled = instance != null && instance.IsEnabled && method != null;
116116
if (!enabled)
117117
{
118118
return (isHandled, value);

src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace BotSharp.Abstraction.SideCar;
55
public interface IConversationSideCar
66
{
77
string Provider { get; }
8+
bool IsEnabled { get; }
89

9-
bool IsEnabled();
1010
void AppendConversationDialogs(string conversationId, List<DialogElement> messages);
1111
List<DialogElement> GetConversationDialogs(string conversationId);
1212
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);

src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
using BotSharp.Abstraction.SideCar.Models;
1817
using BotSharp.Core.Infrastructures;
1918

2019
namespace BotSharp.Core.SideCar.Services;
@@ -31,6 +30,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
3130
private string _conversationId = string.Empty;
3231

3332
public string Provider => "botsharp";
33+
public bool IsEnabled => _enabled;
3434

3535
public BotSharpConversationSideCar(
3636
IServiceProvider services,
@@ -40,11 +40,6 @@ public BotSharpConversationSideCar(
4040
_logger = logger;
4141
}
4242

43-
public bool IsEnabled()
44-
{
45-
return _enabled;
46-
}
47-
4843
public void AppendConversationDialogs(string conversationId, List<DialogElement> messages)
4944
{
5045
if (!IsValid(conversationId))
@@ -99,17 +94,22 @@ public void UpdateConversationStates(string conversationId, List<StateKeyValue>
9994
top.State = new ConversationState(states);
10095
}
10196

102-
public async Task<RoleDialogModel> SendMessage(string agentId, string text,
97+
public async Task<RoleDialogModel> SendMessage(
98+
string agentId,
99+
string text,
103100
PostbackMessageModel? postback = null,
104101
List<MessageState>? states = null,
105102
List<DialogElement>? dialogs = null,
106103
SideCarOptions? options = null)
107104
{
108105
_sideCarOptions = options;
106+
_logger.LogInformation($"Entering side car conversation...");
109107

110108
BeforeExecute(dialogs);
111109
var response = await InnerExecute(agentId, text, postback, states);
112110
AfterExecute();
111+
112+
_logger.LogInformation($"Existing side car conversation...");
113113
return response;
114114
}
115115

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
159159
Reset();
160160

161161
var endNodes = new Dictionary<string, string>();
162-
if (_sidecar?.IsEnabled() == true)
162+
if (_sidecar?.IsEnabled == true)
163163
{
164164
return endNodes;
165165
}
@@ -234,7 +234,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
234234

235235
public void Save()
236236
{
237-
if (_conversationId == null || _sidecar?.IsEnabled() == true)
237+
if (_conversationId == null || _sidecar?.IsEnabled == true)
238238
{
239239
return;
240240
}

src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st
6262
var hub = services.GetRequiredService<IRealtimeHub>();
6363
var conn = hub.SetHubConnection(conversationId);
6464
conn.CurrentAgentId = agentId;
65+
InitEvents(conn);
6566

6667
// load conversation and state
6768
var convService = services.GetRequiredService<IConversationService>();
@@ -128,6 +129,11 @@ await hub.ConnectToModel(async data =>
128129
break;
129130
}
130131

132+
return (response.Event, data);
133+
}
134+
135+
private void InitEvents(RealtimeHubConnection conn)
136+
{
131137
conn.OnModelMessageReceived = message =>
132138
JsonSerializer.Serialize(new
133139
{
@@ -147,7 +153,5 @@ await hub.ConnectToModel(async data =>
147153
{
148154
@event = "clear"
149155
});
150-
151-
return (response.Event, data);
152156
}
153157
}

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public override async Task OnMessageDeleted(string conversationId, string messag
179179
private bool AllowSendingMessage()
180180
{
181181
var sidecar = _services.GetService<IConversationSideCar>();
182-
return sidecar == null || !sidecar.IsEnabled();
182+
return sidecar == null || !sidecar.IsEnabled;
183183
}
184184

185185
private async Task InitClientConversation(string conversationId, ConversationDto conversation)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,13 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool isInit
329329
var words = new List<string>();
330330
HookEmitter.Emit<IRealtimeHook>(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), agent.Id);
331331

332-
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x =>
332+
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x => new FunctionDef
333333
{
334-
var fn = new FunctionDef
335-
{
336-
Name = x.Name ?? string.Empty,
337-
Description = x.Description ?? string.Empty,
338-
Parameters = x.Parameters != null
334+
Name = x.Name ?? string.Empty,
335+
Description = x.Description ?? string.Empty,
336+
Parameters = x.Parameters != null
339337
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
340338
: null
341-
};
342-
return fn;
343339
}).ToArray();
344340

345341
await HookEmitter.Emit<IContentGeneratingHook>(_services,

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,11 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool isInit
326326
var (prompt, messages, options) = PrepareOptions(agent, []);
327327

328328
var instruction = messages.FirstOrDefault()?.Content.FirstOrDefault()?.Text ?? agent?.Description ?? string.Empty;
329-
var functions = options.Tools.Select(x =>
329+
var functions = options.Tools.Select(x => new FunctionDef
330330
{
331-
var fn = new FunctionDef
332-
{
333-
Name = x.FunctionName,
334-
Description = x.FunctionDescription
335-
};
336-
fn.Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters);
337-
return fn;
331+
Name = x.FunctionName,
332+
Description = x.FunctionDescription,
333+
Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters)
338334
}).ToArray();
339335

340336
var realtimeModelSettings = _services.GetRequiredService<RealtimeModelSettings>();

src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,29 @@ public async Task<TwiMLResult> InitiateStreamConversation(ConversationalVoiceReq
5353
instruction.SpeechPaths.Add(request.InitAudioFile);
5454
}
5555

56+
// Before creating session
5657
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
5758
{
5859
await hook.OnSessionCreating(request, instruction);
5960
}, request.AgentId);
6061

62+
6163
var (agent, conversationId) = await InitConversation(request);
6264
request.ConversationId = conversationId.Id;
6365
instruction.AgentId = request.AgentId;
6466
instruction.ConversationId = request.ConversationId;
6567

68+
69+
// After creating session
6670
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
6771
{
6872
await hook.OnSessionCreated(request);
6973
}, request.AgentId);
7074

75+
7176
if (twilio.MachineDetected(request))
7277
{
7378
response = new VoiceResponse();
74-
7579
await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
7680
async hook => await hook.OnVoicemailStarting(request), request.AgentId);
7781

@@ -119,7 +123,7 @@ await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
119123
await Task.Delay(1500);
120124
await twilio.StartRecording(request.CallSid, request.AgentId, request.ConversationId);
121125
});
122-
}
126+
}
123127

124128
return TwiML(response);
125129
}
@@ -204,7 +208,7 @@ protected Dictionary<string, string> ParseStates(List<string> states)
204208

205209
storage.Append(conversation.Id, new RoleDialogModel(AgentRole.User, request.Intent)
206210
{
207-
CurrentAgentId = conversation.Id,
211+
CurrentAgentId = agent.Id,
208212
CreatedAt = DateTime.UtcNow
209213
});
210214
}

0 commit comments

Comments
 (0)