Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static MethodInfo GetMethod(string name)
object? res = null;
var isHandled = false;

var enabled = instance != null && instance.IsEnabled() && method != null;
var enabled = instance != null && instance.IsEnabled && method != null;
if (!enabled)
{
return (isHandled, value);
Expand Down Expand Up @@ -112,7 +112,7 @@ private static MethodInfo GetMethod(string name)
object? value = null;
var isHandled = false;

var enabled = instance != null && instance.IsEnabled() && method != null;
var enabled = instance != null && instance.IsEnabled && method != null;
if (!enabled)
{
return (isHandled, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace BotSharp.Abstraction.SideCar;
public interface IConversationSideCar
{
string Provider { get; }
bool IsEnabled { get; }

bool IsEnabled();
void AppendConversationDialogs(string conversationId, List<DialogElement> messages);
List<DialogElement> GetConversationDialogs(string conversationId);
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ You may obtain a copy of the License at
limitations under the License.
******************************************************************************/

using BotSharp.Abstraction.SideCar.Models;
using BotSharp.Core.Infrastructures;

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

public string Provider => "botsharp";
public bool IsEnabled => _enabled;

public BotSharpConversationSideCar(
IServiceProvider services,
Expand All @@ -40,11 +40,6 @@ public BotSharpConversationSideCar(
_logger = logger;
}

public bool IsEnabled()
{
return _enabled;
}

public void AppendConversationDialogs(string conversationId, List<DialogElement> messages)
{
if (!IsValid(conversationId))
Expand Down Expand Up @@ -99,17 +94,22 @@ public void UpdateConversationStates(string conversationId, List<StateKeyValue>
top.State = new ConversationState(states);
}

public async Task<RoleDialogModel> SendMessage(string agentId, string text,
public async Task<RoleDialogModel> SendMessage(
string agentId,
string text,
PostbackMessageModel? postback = null,
List<MessageState>? states = null,
List<DialogElement>? dialogs = null,
SideCarOptions? options = null)
{
_sideCarOptions = options;
_logger.LogInformation($"Entering side car conversation...");

BeforeExecute(dialogs);
var response = await InnerExecute(agentId, text, postback, states);
AfterExecute();

_logger.LogInformation($"Existing side car conversation...");
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
Reset();

var endNodes = new Dictionary<string, string>();
if (_sidecar?.IsEnabled() == true)
if (_sidecar?.IsEnabled == true)
{
return endNodes;
}
Expand Down Expand Up @@ -234,7 +234,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =

public void Save()
{
if (_conversationId == null || _sidecar?.IsEnabled() == true)
if (_conversationId == null || _sidecar?.IsEnabled == true)
{
return;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st
var hub = services.GetRequiredService<IRealtimeHub>();
var conn = hub.SetHubConnection(conversationId);
conn.CurrentAgentId = agentId;
InitEvents(conn);

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

return (response.Event, data);
}

private void InitEvents(RealtimeHubConnection conn)
{
conn.OnModelMessageReceived = message =>
JsonSerializer.Serialize(new
{
Expand All @@ -147,7 +153,5 @@ await hub.ConnectToModel(async data =>
{
@event = "clear"
});

return (response.Event, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public override async Task OnMessageDeleted(string conversationId, string messag
private bool AllowSendingMessage()
{
var sidecar = _services.GetService<IConversationSideCar>();
return sidecar == null || !sidecar.IsEnabled();
return sidecar == null || !sidecar.IsEnabled;
}

private async Task InitClientConversation(string conversationId, ConversationDto conversation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,13 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool isInit
var words = new List<string>();
HookEmitter.Emit<IRealtimeHook>(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), agent.Id);

var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x =>
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x => new FunctionDef
{
var fn = new FunctionDef
{
Name = x.Name ?? string.Empty,
Description = x.Description ?? string.Empty,
Parameters = x.Parameters != null
Name = x.Name ?? string.Empty,
Description = x.Description ?? string.Empty,
Parameters = x.Parameters != null
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
: null
};
return fn;
}).ToArray();

await HookEmitter.Emit<IContentGeneratingHook>(_services,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,11 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool isInit
var (prompt, messages, options) = PrepareOptions(agent, []);

var instruction = messages.FirstOrDefault()?.Content.FirstOrDefault()?.Text ?? agent?.Description ?? string.Empty;
var functions = options.Tools.Select(x =>
var functions = options.Tools.Select(x => new FunctionDef
{
var fn = new FunctionDef
{
Name = x.FunctionName,
Description = x.FunctionDescription
};
fn.Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters);
return fn;
Name = x.FunctionName,
Description = x.FunctionDescription,
Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters)
}).ToArray();

var realtimeModelSettings = _services.GetRequiredService<RealtimeModelSettings>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,29 @@ public async Task<TwiMLResult> InitiateStreamConversation(ConversationalVoiceReq
instruction.SpeechPaths.Add(request.InitAudioFile);
}

// Before creating session
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
{
await hook.OnSessionCreating(request, instruction);
}, request.AgentId);


var (agent, conversationId) = await InitConversation(request);
request.ConversationId = conversationId.Id;
instruction.AgentId = request.AgentId;
instruction.ConversationId = request.ConversationId;


// After creating session
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
{
await hook.OnSessionCreated(request);
}, request.AgentId);


if (twilio.MachineDetected(request))
{
response = new VoiceResponse();

await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
async hook => await hook.OnVoicemailStarting(request), request.AgentId);

Expand Down Expand Up @@ -119,7 +123,7 @@ await HookEmitter.Emit<ITwilioCallStatusHook>(_services,
await Task.Delay(1500);
await twilio.StartRecording(request.CallSid, request.AgentId, request.ConversationId);
});
}
}

return TwiML(response);
}
Expand Down Expand Up @@ -204,7 +208,7 @@ protected Dictionary<string, string> ParseStates(List<string> states)

storage.Append(conversation.Id, new RoleDialogModel(AgentRole.User, request.Intent)
{
CurrentAgentId = conversation.Id,
CurrentAgentId = agent.Id,
CreatedAt = DateTime.UtcNow
});
}
Expand Down
Loading