Skip to content

minor changes #604

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

Merged
merged 1 commit into from
Aug 22, 2024
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 @@ -54,17 +54,24 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
var twilio = _services.GetRequiredService<TwilioService>();
var messageQueue = _services.GetRequiredService<TwilioMessageQueue>();
var sessionManager = _services.GetRequiredService<ITwilioSessionManager>();
var url = $"twilio/voice/{conversationId}/reply/{seqNum}?states={states}";
var messages = await sessionManager.RetrieveStagedCallerMessagesAsync(conversationId, seqNum);
if (!string.IsNullOrWhiteSpace(request.SpeechResult))
{
messages.Add(request.SpeechResult);
await sessionManager.StageCallerMessageAsync(conversationId, seqNum, request.SpeechResult);
}
var messageContent = string.Join("\r\n", messages);
VoiceResponse response;
if (!string.IsNullOrWhiteSpace(messageContent))
if (messages.Count == 0 && seqNum == 0)
{
response = twilio.ReturnInstructions("twilio/welcome.mp3", $"twilio/voice/{conversationId}/receive/{seqNum}?states={states}", true);
}
else
{

if (messages.Count == 0)
{
messages = await sessionManager.RetrieveStagedCallerMessagesAsync(conversationId, seqNum - 1);
}
var messageContent = string.Join("\r\n", messages);
var callerMessage = new CallerMessage()
{
ConversationId = conversationId,
Expand All @@ -81,12 +88,7 @@ public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversat
}
}
await messageQueue.EnqueueAsync(callerMessage);
response = twilio.ReturnInstructions(null, url, true, 1);
}
else
{
var speechPath = seqNum > 0 ? $"twilio/voice/speeches/{conversationId}/{seqNum - 1}.mp3" : "twilio/welcome.mp3";
response = twilio.ReturnInstructions(speechPath, $"twilio/voice/{conversationId}/receive/{seqNum}?states={states}", true);
response = twilio.ReturnInstructions(null, $"twilio/voice/{conversationId}/reply/{seqNum}?states={states}", true, 1);
}
return TwiML(response);
}
Expand All @@ -108,12 +110,21 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
var indication = await sessionManager.GetReplyIndicationAsync(conversationId, seqNum);
if (indication != null)
{
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
var fileService = _services.GetRequiredService<IFileStorageService>();
var data = await textToSpeechService.GenerateSpeechFromTextAsync(indication);
var fileName = $"indication_{seqNum}.mp3";
await fileService.SaveSpeechFileAsync(conversationId, fileName, data);
response = twilio.ReturnInstructions($"twilio/voice/speeches/{conversationId}/{fileName}", $"twilio/voice/{conversationId}/reply/{seqNum}?states={states}", true, 2);
string speechPath;
if (indication.StartsWith('#'))
{
speechPath = $"twilio/{indication.Substring(1)}";
}
else
{
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
var fileService = _services.GetRequiredService<IFileStorageService>();
var data = await textToSpeechService.GenerateSpeechFromTextAsync(indication);
var fileName = $"indication_{seqNum}.mp3";
await fileService.SaveSpeechFileAsync(conversationId, fileName, data);
speechPath = $"twilio/voice/speeches/{conversationId}/{fileName}";
}
response = twilio.ReturnInstructions(speechPath, $"twilio/voice/{conversationId}/reply/{seqNum}?states={states}", true, 2);
}
else
{
Expand All @@ -125,7 +136,7 @@ public async Task<TwiMLResult> ReplyCallerMessage([FromRoute] string conversatio
var textToSpeechService = CompletionProvider.GetTextToSpeech(_services, "openai", "tts-1");
var fileService = _services.GetRequiredService<IFileStorageService>();
var data = await textToSpeechService.GenerateSpeechFromTextAsync(reply.Content);
var fileName = $"reply_{seqNum}.mp3";
var fileName = $"reply_{reply.MessageId ?? seqNum.ToString()}.mp3";
await fileService.SaveSpeechFileAsync(conversationId, fileName, data);
if (reply.ConversationEnd)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class AssistantMessage
{
public bool ConversationEnd { get; set; }
public string Content { get; set; }
public string MessageId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private async Task ProcessUserMessageAsync(CallerMessage message)
reply = new AssistantMessage()
{
ConversationEnd = msg.Instruction.ConversationEnd,
Content = msg.Content
Content = msg.Content,
MessageId = msg.MessageId
};
},
async msg =>
Expand Down