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 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
21 changes: 21 additions & 0 deletions src/SmartTalk.Api/Controllers/AiSpeechAssistantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ public async Task ConnectAiSpeechAssistantAsync(string from, string to)
HttpContext.Response.StatusCode = 400;
}
}

[HttpGet("outbound/connect/{from}/{to}/{id}")]
public async Task OutboundConnectAiSpeechAssistantAsync(string from, string to, int id)
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
var command = new ConnectAiSpeechAssistantCommand
{
From = from,
To = to,
AssistantId = id,
Host = HttpContext.Request.Host.Host,
TwilioWebSocket = await HttpContext.WebSockets.AcceptWebSocketAsync()
};
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
Expand Up @@ -9,7 +9,7 @@ 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, int? assistantId = null, CancellationToken cancellationToken = default);

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

Expand All @@ -28,7 +28,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, int? assistantId = null, CancellationToken cancellationToken = default)
{
var assistantInfo =
from assistant in _repository.Query<Domain.AISpeechAssistant.AiSpeechAssistant>()
Expand All @@ -38,12 +38,13 @@ 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
select new
{
assistant, promptTemplate, userProfile
};

assistantInfo = assistantInfo.Where(x => assistantId.HasValue ? x.assistant.Id == assistantId.Value : x.assistant.DidNumber == didNumber);

var result = await assistantInfo.FirstOrDefaultAsync(cancellationToken: cancellationToken).ConfigureAwait(false);

return (result.assistant, result.promptTemplate, result.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.AssistantId, 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, int? assistantId, CancellationToken cancellationToken)
{
var (assistant, promptTemplate, userProfile) = await _aiSpeechAssistantDataProvider
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, cancellationToken).ConfigureAwait(false);
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, assistantId, 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
Expand Up @@ -11,5 +11,7 @@ public class ConnectAiSpeechAssistantCommand : ICommand

public string Host { get; set; }

public int? AssistantId { get; set; }

public WebSocket TwilioWebSocket { get; set; }
}