Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ai speech assistant call out #199

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions src/SmartTalk.Api/Controllers/AiSpeechAssistantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using SmartTalk.Messages.Commands.AiSpeechAssistant;
using SmartTalk.Messages.Enums.AiSpeechAssistant;

namespace SmartTalk.Api.Controllers;

Expand Down Expand Up @@ -46,6 +47,27 @@ public async Task ConnectAiSpeechAssistantAsync(string from, string to)
HttpContext.Response.StatusCode = 400;
}
}

[HttpGet("outbound/connect/{from}/{to}")]
public async Task OutboundConnectAiSpeechAssistantAsync(string from, string to)
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
var command = new ConnectAiSpeechAssistantCommand
{
From = from,
To = to,
Host = HttpContext.Request.Host.Host,
TwilioWebSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(),
CallType = AiSpeechAssistantCallType.Outbound
};
await _mediator.SendAsync(command);
}
else
{
HttpContext.Response.StatusCode = 400;
}
}

[Route("recording/callback"), HttpPost]
public async Task<IActionResult> ReceivePhoneRecordingStatusCallBackAcyn()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `ai_speech_assistant_prompt_template` add column `call_type` int not null default 0;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SmartTalk.Messages.Enums.AiSpeechAssistant;

namespace SmartTalk.Core.Domain.AIAssistant;

Expand All @@ -17,6 +18,9 @@ public class AiSpeechAssistantPromptTemplate : IEntity, IHasCreatedFields
[Column("template")]
public string Template { get; set; }

[Column("call_type")]
public AiSpeechAssistantCallType CallType { get; set; }

[Column("created_date")]
public DateTimeOffset CreatedDate { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using Microsoft.EntityFrameworkCore;
using SmartTalk.Core.Domain.AIAssistant;
using SmartTalk.Core.Domain.AISpeechAssistant;
using SmartTalk.Messages.Enums.AiSpeechAssistant;

namespace SmartTalk.Core.Services.AiSpeechAssistant;

public interface IAiSpeechAssistantDataProvider : IScopedDependency
{
Task<(Domain.AISpeechAssistant.AiSpeechAssistant, AiSpeechAssistantPromptTemplate, AiSpeechAssistantUserProfile)>
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, CancellationToken cancellationToken);
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, AiSpeechAssistantCallType callType, CancellationToken cancellationToken);

Task<Domain.AISpeechAssistant.AiSpeechAssistant> GetAiSpeechAssistantByNumbersAsync(string didNumber, CancellationToken cancellationToken);

Expand All @@ -28,7 +29,7 @@ public AiSpeechAssistantDataProvider(IRepository repository)
}

public async Task<(Domain.AISpeechAssistant.AiSpeechAssistant, AiSpeechAssistantPromptTemplate, AiSpeechAssistantUserProfile)>
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, CancellationToken cancellationToken)
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, AiSpeechAssistantCallType callType, CancellationToken cancellationToken)
{
var assistantInfo =
from assistant in _repository.Query<Domain.AISpeechAssistant.AiSpeechAssistant>()
Expand All @@ -38,7 +39,7 @@ from promptTemplate in promptGroup.DefaultIfEmpty()
join userProfile in _repository.Query<AiSpeechAssistantUserProfile>().Where(x => x.CallerNumber == callerNumber)
on assistant.Id equals userProfile.AssistantId into userProfileGroup
from userProfile in userProfileGroup.DefaultIfEmpty()
where assistant.DidNumber == didNumber
where assistant.DidNumber == didNumber && promptTemplate.CallType == callType
select new
{
assistant, promptTemplate, userProfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<AiSpeechAssistantConnectCloseEvent> ConnectAiSpeechAssistantAs
{
Log.Information($"The call from {command.From} to {command.To} is connected");

var (assistant, knowledgeBase) = await BuildingAiSpeechAssistantKnowledgeBaseAsync(command.From, command.To, cancellationToken).ConfigureAwait(false);
var (assistant, knowledgeBase) = await BuildingAiSpeechAssistantKnowledgeBaseAsync(command.From, command.To, command.CallType, cancellationToken).ConfigureAwait(false);

if (string.IsNullOrEmpty(knowledgeBase)) return new AiSpeechAssistantConnectCloseEvent();

Expand Down Expand Up @@ -169,10 +169,10 @@ await CallResource.UpdateAsync(
);
}

private async Task<(Domain.AISpeechAssistant.AiSpeechAssistant Assistant, string Prompt)> BuildingAiSpeechAssistantKnowledgeBaseAsync(string from, string to, CancellationToken cancellationToken)
private async Task<(Domain.AISpeechAssistant.AiSpeechAssistant Assistant, string Prompt)> BuildingAiSpeechAssistantKnowledgeBaseAsync(string from, string to, AiSpeechAssistantCallType callType, CancellationToken cancellationToken)
{
var (assistant, promptTemplate, userProfile) = await _aiSpeechAssistantDataProvider
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, cancellationToken).ConfigureAwait(false);
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, callType, cancellationToken).ConfigureAwait(false);

Log.Information("Matching Ai speech assistant: {@Assistant}、{@PromptTemplate}、{@UserProfile}", assistant, promptTemplate, userProfile);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.WebSockets;
using Mediator.Net.Contracts;
using SmartTalk.Messages.Enums.AiSpeechAssistant;

namespace SmartTalk.Messages.Commands.AiSpeechAssistant;

Expand All @@ -12,4 +13,6 @@ public class ConnectAiSpeechAssistantCommand : ICommand
public string Host { get; set; }

public WebSocket TwilioWebSocket { get; set; }

public AiSpeechAssistantCallType CallType { get; set; } = AiSpeechAssistantCallType.Inbound;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SmartTalk.Messages.Enums.AiSpeechAssistant;

public enum AiSpeechAssistantCallType
{
Inbound,
Outbound
}