Skip to content

Commit cdbe06b

Browse files
authored
Merge pull request #199 from sj-distributor/enhance-ai-speech-call-out
Enhance ai speech assistant call out
2 parents 015c1b8 + ea1e42f commit cdbe06b

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/SmartTalk.Api/Controllers/AiSpeechAssistantController.cs

+22
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ public async Task ConnectAiSpeechAssistantAsync(string from, string to)
4646
HttpContext.Response.StatusCode = 400;
4747
}
4848
}
49+
50+
[HttpGet("outbound/connect")]
51+
[HttpGet("outbound/connect/{from}/{to}/{id}")]
52+
public async Task OutboundConnectAiSpeechAssistantAsync(string from, string to, int id)
53+
{
54+
if (HttpContext.WebSockets.IsWebSocketRequest)
55+
{
56+
var command = new ConnectAiSpeechAssistantCommand
57+
{
58+
From = from,
59+
To = to,
60+
AssistantId = id,
61+
Host = HttpContext.Request.Host.Host,
62+
TwilioWebSocket = await HttpContext.WebSockets.AcceptWebSocketAsync()
63+
};
64+
await _mediator.SendAsync(command);
65+
}
66+
else
67+
{
68+
HttpContext.Response.StatusCode = 400;
69+
}
70+
}
4971

5072
[Route("recording/callback"), HttpPost]
5173
public async Task<IActionResult> ReceivePhoneRecordingStatusCallBackAcyn()

src/SmartTalk.Core/Services/AiSpeechAssistant/AiSpeechAssistantDataProvider.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SmartTalk.Core.Services.AiSpeechAssistant;
99
public interface IAiSpeechAssistantDataProvider : IScopedDependency
1010
{
1111
Task<(Domain.AISpeechAssistant.AiSpeechAssistant, AiSpeechAssistantPromptTemplate, AiSpeechAssistantUserProfile)>
12-
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, CancellationToken cancellationToken);
12+
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, int? assistantId = null, CancellationToken cancellationToken = default);
1313

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

@@ -28,7 +28,7 @@ public AiSpeechAssistantDataProvider(IRepository repository)
2828
}
2929

3030
public async Task<(Domain.AISpeechAssistant.AiSpeechAssistant, AiSpeechAssistantPromptTemplate, AiSpeechAssistantUserProfile)>
31-
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, CancellationToken cancellationToken)
31+
GetAiSpeechAssistantInfoByNumbersAsync(string callerNumber, string didNumber, int? assistantId = null, CancellationToken cancellationToken = default)
3232
{
3333
var assistantInfo =
3434
from assistant in _repository.Query<Domain.AISpeechAssistant.AiSpeechAssistant>()
@@ -38,12 +38,13 @@ from promptTemplate in promptGroup.DefaultIfEmpty()
3838
join userProfile in _repository.Query<AiSpeechAssistantUserProfile>().Where(x => x.CallerNumber == callerNumber)
3939
on assistant.Id equals userProfile.AssistantId into userProfileGroup
4040
from userProfile in userProfileGroup.DefaultIfEmpty()
41-
where assistant.DidNumber == didNumber
4241
select new
4342
{
4443
assistant, promptTemplate, userProfile
4544
};
4645

46+
assistantInfo = assistantInfo.Where(x => assistantId.HasValue ? x.assistant.Id == assistantId.Value : x.assistant.DidNumber == didNumber);
47+
4748
var result = await assistantInfo.FirstOrDefaultAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
4849

4950
return (result.assistant, result.promptTemplate, result.userProfile);

src/SmartTalk.Core/Services/AiSpeechAssistant/AiSpeechAssistantService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public async Task<AiSpeechAssistantConnectCloseEvent> ConnectAiSpeechAssistantAs
106106
{
107107
Log.Information($"The call from {command.From} to {command.To} is connected");
108108

109-
var (assistant, knowledgeBase) = await BuildingAiSpeechAssistantKnowledgeBaseAsync(command.From, command.To, cancellationToken).ConfigureAwait(false);
109+
var (assistant, knowledgeBase) = await BuildingAiSpeechAssistantKnowledgeBaseAsync(command.From, command.To, command.AssistantId, cancellationToken).ConfigureAwait(false);
110110

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

@@ -204,10 +204,10 @@ await CallResource.UpdateAsync(
204204
);
205205
}
206206

207-
private async Task<(Domain.AISpeechAssistant.AiSpeechAssistant Assistant, string Prompt)> BuildingAiSpeechAssistantKnowledgeBaseAsync(string from, string to, CancellationToken cancellationToken)
207+
private async Task<(Domain.AISpeechAssistant.AiSpeechAssistant Assistant, string Prompt)> BuildingAiSpeechAssistantKnowledgeBaseAsync(string from, string to, int? assistantId, CancellationToken cancellationToken)
208208
{
209209
var (assistant, promptTemplate, userProfile) = await _aiSpeechAssistantDataProvider
210-
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, cancellationToken).ConfigureAwait(false);
210+
.GetAiSpeechAssistantInfoByNumbersAsync(from, to, assistantId, cancellationToken).ConfigureAwait(false);
211211

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

src/SmartTalk.Messages/Commands/AiSpeechAssistant/ConnectAiSpeechAssistantCommand.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public class ConnectAiSpeechAssistantCommand : ICommand
1111

1212
public string Host { get; set; }
1313

14+
public int? AssistantId { get; set; }
15+
1416
public WebSocket TwilioWebSocket { get; set; }
1517
}

0 commit comments

Comments
 (0)