Merge branch 'master' of https://github.com/SciSharp/BotSharp into features/call-dummy-func
This commit is contained in:
commit
eb078d121b
|
|
@ -1,7 +1,7 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<MSExtensionsVersion>8.0.0</MSExtensionsVersion>
|
||||
<AspNetCoreVersion>2.3.0</AspNetCoreVersion>
|
||||
<AspNetCoreVersion>2.3.0</AspNetCoreVersion>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.1" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
|
||||
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.0.0" />
|
||||
|
|
@ -51,7 +51,7 @@
|
|||
<PackageVersion Include="PdfPig" Version="0.1.8" />
|
||||
<PackageVersion Include="TensorFlow.Keras" Version="0.15.0" />
|
||||
<PackageVersion Include="LangChain.Providers.Google.VertexAI" Version="0.15.3-dev.58" />
|
||||
<PackageVersion Include="LLamaSharp" Version="0.20.0" />
|
||||
<PackageVersion Include="LLamaSharp" Version="0.21.0" />
|
||||
<PackageVersion Include="FaissMask" Version="0.2.0" />
|
||||
<PackageVersion Include="FastText.NetWrapper" Version="1.3.0" />
|
||||
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25161.3" />
|
||||
|
|
@ -108,7 +108,9 @@
|
|||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />
|
||||
<PackageVersion Include="BotSharp.Logger" Version="$(BotSharpVersion)" />
|
||||
<PackageVersion Include="BotSharp.Core.Realtime" Version="$(BotSharpVersion)" />
|
||||
<PackageVersion Include="BotSharp.OpenAPI" Version="$(BotSharpVersion)" />
|
||||
<PackageVersion Include="BotSharp.Plugin.Dashboard" Version="$(BotSharpVersion)" />
|
||||
<PackageVersion Include="BotSharp.Plugin.AzureOpenAI" Version="$(BotSharpVersion)" />
|
||||
|
|
@ -127,7 +129,6 @@
|
|||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.5" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.27" />
|
||||
<PackageVersion Include="AspNet.Security.OAuth.GitHub" Version="6.0.15" />
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ public class RealtimeHub : IRealtimeHub
|
|||
}
|
||||
|
||||
routing.Context.SetDialogs(dialogs);
|
||||
routing.Context.SetMessageId(_conn.ConversationId, dialogs.Last().MessageId);
|
||||
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
|
||||
|
|
@ -188,6 +189,7 @@ public class RealtimeHub : IRealtimeHub
|
|||
// append input audio transcript to conversation
|
||||
dialogs.Add(message);
|
||||
storage.Append(_conn.ConversationId, message);
|
||||
routing.Context.SetMessageId(_conn.ConversationId, message.MessageId);
|
||||
|
||||
foreach (var hook in hookProvider.HooksOrderByPriority)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -46,18 +46,22 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="build\**" />
|
||||
<Compile Remove="packages\**" />
|
||||
<Compile Remove="Planning\**" />
|
||||
<Compile Remove="Translation\Models\**" />
|
||||
<EmbeddedResource Remove="build\**" />
|
||||
<EmbeddedResource Remove="packages\**" />
|
||||
<EmbeddedResource Remove="Planning\**" />
|
||||
<EmbeddedResource Remove="Translation\Models\**" />
|
||||
<None Remove="build\**" />
|
||||
<None Remove="packages\**" />
|
||||
<None Remove="Planning\**" />
|
||||
<None Remove="Translation\Models\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Content.targets" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.hf.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.naive.liquid" />
|
||||
|
|
@ -220,4 +224,11 @@
|
|||
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="build\BotSharp.Core.targets" PackagePath="build\BotSharp.Core.targets" />
|
||||
<Content Include="data/*.*">
|
||||
<Pack>true</Pack>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
10
src/Infrastructure/BotSharp.Core/build/BotSharp.Core.targets
Normal file
10
src/Infrastructure/BotSharp.Core/build/BotSharp.Core.targets
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<!-- Define the files to be copied -->
|
||||
<ContentFiles Include="$(MSBuildThisFileDirectory)..\contentFiles\any\**\*.*" />
|
||||
</ItemGroup>
|
||||
<Target Name="CopyContentFiles" AfterTargets="Build">
|
||||
<!-- Copy the content files to the output directory -->
|
||||
<Copy SourceFiles="@(ContentFiles)" DestinationFolder="$(TargetDir)%(RecursiveDir)" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
|
@ -10,6 +10,9 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-transfer_phone_call.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-hangup_phone_call.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ public class TwilioStreamController : TwilioController
|
|||
throw new ArgumentNullException(nameof(VoiceRequest.CallSid));
|
||||
}
|
||||
|
||||
var twilio = _services.GetRequiredService<TwilioService>();
|
||||
VoiceResponse response = default!;
|
||||
|
||||
if (request.AnsweredBy == "machine_start" &&
|
||||
|
|
@ -42,7 +43,8 @@ public class TwilioStreamController : TwilioController
|
|||
request.InitAudioFile != null)
|
||||
{
|
||||
response = new VoiceResponse();
|
||||
response.Play(new Uri(request.InitAudioFile));
|
||||
var url = twilio.GetSpeechPath(request.ConversationId, request.InitAudioFile);
|
||||
response.Play(new Uri(url));
|
||||
return TwiML(response);
|
||||
}
|
||||
|
||||
|
|
@ -67,10 +69,8 @@ public class TwilioStreamController : TwilioController
|
|||
});
|
||||
|
||||
request.ConversationId = await InitConversation(request);
|
||||
|
||||
var twilio = _services.GetRequiredService<TwilioService>();
|
||||
|
||||
response = twilio.ReturnBidirectionalMediaStreamsInstructions(request.ConversationId, instruction);
|
||||
instruction.ConversationId = request.ConversationId;
|
||||
response = twilio.ReturnBidirectionalMediaStreamsInstructions(instruction);
|
||||
|
||||
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -438,6 +438,26 @@ public class TwilioVoiceController : TwilioController
|
|||
return TwiML(response);
|
||||
}
|
||||
|
||||
[ValidateRequest]
|
||||
[HttpPost("twilio/voice/transfer-call")]
|
||||
public async Task<TwiMLResult> TransferCall(ConversationalVoiceRequest request)
|
||||
{
|
||||
var instruction = new ConversationalVoiceResponse
|
||||
{
|
||||
ConversationId = request.ConversationId,
|
||||
TransferTo = request.TransferTo
|
||||
};
|
||||
|
||||
if (request.InitAudioFile != null)
|
||||
{
|
||||
instruction.SpeechPaths.Add(request.InitAudioFile);
|
||||
}
|
||||
|
||||
var twilio = _services.GetRequiredService<TwilioService>();
|
||||
var response = twilio.TransferCall(instruction);
|
||||
return TwiML(response);
|
||||
}
|
||||
|
||||
[ValidateRequest]
|
||||
[HttpPost("twilio/voice/status")]
|
||||
public async Task<ActionResult> PhoneCallStatus(ConversationalVoiceRequest request)
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ public class ConversationalVoiceRequest : VoiceRequest
|
|||
[FromForm]
|
||||
public string? CallbackSource { get; set; }
|
||||
|
||||
[FromQuery(Name = "transfer-to")]
|
||||
public string? TransferTo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// machine_start
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -13,4 +13,9 @@ public class ConversationalVoiceResponse
|
|||
public int Timeout { get; set; } = 3;
|
||||
|
||||
public string Hints { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Phone Number to transfer to
|
||||
/// </summary>
|
||||
public string? TransferTo { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,6 +108,7 @@ public class OutboundPhoneCallFn : IFunctionCallback
|
|||
statusCallback: new Uri(statusUrl),
|
||||
// https://www.twilio.com/docs/voice/answering-machine-detection
|
||||
machineDetection: _twilioSetting.MachineDetection,
|
||||
machineDetectionSilenceTimeout: _twilioSetting.MachineDetectionSilenceTimeout,
|
||||
record: _twilioSetting.RecordingEnabled,
|
||||
recordingStatusCallback: $"{_twilioSetting.CallbackHost}/twilio/record/status?conversation-id={newConversationId}");
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
using BotSharp.Abstraction.Files;
|
||||
using BotSharp.Abstraction.Options;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
using Twilio.Rest.Api.V2010.Account;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
|
||||
|
||||
public class TransferPhoneCallFn : IFunctionCallback
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger _logger;
|
||||
private readonly BotSharpOptions _options;
|
||||
private readonly TwilioSetting _twilioSetting;
|
||||
|
||||
public string Name => "util-twilio-transfer_phone_call";
|
||||
public string Indication => "Transferring the active line";
|
||||
|
||||
public TransferPhoneCallFn(
|
||||
IServiceProvider services,
|
||||
ILogger<TransferPhoneCallFn> logger,
|
||||
BotSharpOptions options,
|
||||
TwilioSetting twilioSetting)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_options = options;
|
||||
_twilioSetting = twilioSetting;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<ForwardPhoneCallArgs>(message.FunctionArgs, _options.JsonSerializerOptions);
|
||||
|
||||
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
var routing = _services.GetRequiredService<IRoutingService>();
|
||||
var conversationId = routing.Context.ConversationId;
|
||||
var processUrl = $"{_twilioSetting.CallbackHost}/twilio/voice/transfer-call?conversation-id={conversationId}&transfer-to={args.PhoneNumber}";
|
||||
|
||||
// Generate initial assistant audio
|
||||
string initAudioFile = null;
|
||||
if (!string.IsNullOrEmpty(args.TransitionMessage))
|
||||
{
|
||||
var completion = CompletionProvider.GetAudioCompletion(_services, "openai", "tts-1");
|
||||
var data = await completion.GenerateAudioFromTextAsync(args.TransitionMessage);
|
||||
initAudioFile = "transfer.mp3";
|
||||
fileStorage.SaveSpeechFile(conversationId, initAudioFile, data);
|
||||
|
||||
processUrl += $"&init-audio-file={initAudioFile}";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(initAudioFile))
|
||||
{
|
||||
processUrl += $"&init-audio-file={initAudioFile}";
|
||||
}
|
||||
|
||||
// Forward call
|
||||
var sid = states.GetState("twilio_call_sid");
|
||||
|
||||
if (string.IsNullOrEmpty(sid))
|
||||
{
|
||||
_logger.LogError("Twilio call sid is empty.");
|
||||
message.Content = "There is an error when transferring the phone call.";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
var call = CallResource.Update(
|
||||
pathSid: sid,
|
||||
url: new Uri(processUrl));
|
||||
|
||||
message.Content = args.TransitionMessage;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,8 +7,9 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
|
|||
{
|
||||
private static string PREFIX = "util-twilio-";
|
||||
private static string OUTBOUND_PHONE_CALL_FN = $"{PREFIX}outbound_phone_call";
|
||||
private static string TRANSFER_PHONE_CALL_FN = $"{PREFIX}transfer_phone_call";
|
||||
private static string HANGUP_PHONE_CALL_FN = $"{PREFIX}hangup_phone_call";
|
||||
public static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
|
||||
private static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
|
||||
|
||||
public void AddUtilities(List<AgentUtility> utilities)
|
||||
{
|
||||
|
|
@ -18,6 +19,7 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
|
|||
Functions =
|
||||
[
|
||||
new($"{OUTBOUND_PHONE_CALL_FN}"),
|
||||
new($"{TRANSFER_PHONE_CALL_FN}"),
|
||||
new($"{HANGUP_PHONE_CALL_FN}"),
|
||||
new($"{TEXT_MESSAGE_FN}")
|
||||
],
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
|
||||
public class ForwardPhoneCallArgs
|
||||
{
|
||||
[JsonPropertyName("phone_number")]
|
||||
public string PhoneNumber { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("transition_message")]
|
||||
public string TransitionMessage { get; set; } = null!;
|
||||
}
|
||||
|
|
@ -160,6 +160,23 @@ public class TwilioService
|
|||
return response;
|
||||
}
|
||||
|
||||
public VoiceResponse TransferCall(ConversationalVoiceResponse conversationalVoiceResponse)
|
||||
{
|
||||
var response = new VoiceResponse();
|
||||
var conversationId = conversationalVoiceResponse.ConversationId;
|
||||
if (conversationalVoiceResponse.SpeechPaths != null && conversationalVoiceResponse.SpeechPaths.Any())
|
||||
{
|
||||
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
|
||||
{
|
||||
var uri = GetSpeechPath(conversationId, speechPath);
|
||||
response.Play(new Uri(uri));
|
||||
}
|
||||
}
|
||||
response.Dial(conversationalVoiceResponse.TransferTo, answerOnBridge: true);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public VoiceResponse HoldOn(int interval, string message = null)
|
||||
{
|
||||
var twilioSetting = _services.GetRequiredService<TwilioSetting>();
|
||||
|
|
@ -190,26 +207,17 @@ public class TwilioService
|
|||
/// </summary>
|
||||
/// <param name="conversationalVoiceResponse"></param>
|
||||
/// <returns></returns>
|
||||
public VoiceResponse ReturnBidirectionalMediaStreamsInstructions(string conversationId, ConversationalVoiceResponse conversationalVoiceResponse)
|
||||
public VoiceResponse ReturnBidirectionalMediaStreamsInstructions(ConversationalVoiceResponse conversationalVoiceResponse)
|
||||
{
|
||||
var response = new VoiceResponse();
|
||||
var conversationId = conversationalVoiceResponse.ConversationId;
|
||||
|
||||
if (conversationalVoiceResponse.SpeechPaths != null && conversationalVoiceResponse.SpeechPaths.Any())
|
||||
{
|
||||
foreach (var speechPath in conversationalVoiceResponse.SpeechPaths)
|
||||
{
|
||||
if (speechPath.StartsWith("twilio/"))
|
||||
{
|
||||
response.Play(new Uri($"{_settings.CallbackHost}/{speechPath}"));
|
||||
}
|
||||
else if (speechPath.StartsWith(_settings.CallbackHost))
|
||||
{
|
||||
response.Play(new Uri(speechPath));
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Play(new Uri($"{_settings.CallbackHost}/twilio/voice/speeches/{conversationalVoiceResponse.ConversationId}/{speechPath}"));
|
||||
}
|
||||
var uri = GetSpeechPath(conversationId, speechPath);
|
||||
response.Play(new Uri(uri));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -220,4 +228,20 @@ public class TwilioService
|
|||
|
||||
return response;
|
||||
}
|
||||
|
||||
public string GetSpeechPath(string conversationId, string speechPath)
|
||||
{
|
||||
if (speechPath.StartsWith("twilio/"))
|
||||
{
|
||||
return $"{_settings.CallbackHost}/{speechPath}";
|
||||
}
|
||||
else if (speechPath.StartsWith(_settings.CallbackHost))
|
||||
{
|
||||
return speechPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{_settings.CallbackHost}/twilio/voice/speeches/{conversationId}/{speechPath}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ public class TwilioSetting
|
|||
public int MaxGatherAttempts { get; set; } = 4;
|
||||
|
||||
public string? MachineDetection { get; set; }
|
||||
public int MachineDetectionSilenceTimeout { get; set; } = 2500;
|
||||
|
||||
public bool RecordingEnabled { get; set; } = false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "util-twilio-transfer_phone_call",
|
||||
"description": "When user wants to transfer the phone call",
|
||||
"visibility_expression": "{% if states.channel == 'phone' %}visible{% endif %}",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"transition_message": {
|
||||
"type": "string",
|
||||
"description": "Transition message when forwarding."
|
||||
},
|
||||
"phone_number": {
|
||||
"type": "string",
|
||||
"description": "Phone number transfer to."
|
||||
}
|
||||
},
|
||||
"required": [ "transition_message", "phone_number" ]
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
<ItemGroup Condition="$(SolutionName)==PizzaBot">
|
||||
<PackageReference Include="BotSharp.Logger" />
|
||||
<PackageReference Include="BotSharp.OpenAPI" />
|
||||
<PackageReference Include="BotSharp.Core.Realtime" />
|
||||
<PackageReference Include="BotSharp.Plugin.Dashboard" />
|
||||
<PackageReference Include="BotSharp.Plugin.AzureOpenAI" />
|
||||
<PackageReference Include="BotSharp.Plugin.GoogleAI" />
|
||||
|
|
|
|||
|
|
@ -10,7 +10,11 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="$(SolutionName)==PizzaBot">
|
||||
<PackageReference Include="BotSharp.Core" Version="$(BotSharpVersion)" />
|
||||
<PackageReference Include="BotSharp.Core" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="$(SolutionName)==BotSharp">
|
||||
<ProjectReference Include="..\..\src\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
@ -97,8 +101,4 @@
|
|||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="$(SolutionName)==BotSharp">
|
||||
<ProjectReference Include="..\..\src\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Reference in a new issue