Skip to content

Hang-up if waiting AI Response Timeout #927

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 3 commits into from
Mar 10, 2025
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 @@ -32,13 +32,12 @@ public interface IConversationService
/// Send message to LLM
/// </summary>
/// <param name="agentId"></param>
/// <param name="lastDalog"></param>
/// <param name="lastDialog"></param>
/// <param name="replyMessage"></param>
/// <param name="onResponseReceived">Received the response from AI Agent</param>
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
/// <returns></returns>
Task<bool> SendMessage(string agentId,
RoleDialogModel lastDalog,
RoleDialogModel lastDialog,
PostbackMessageModel? replyMessage,
Func<RoleDialogModel, Task> onResponseReceived);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<OutputPath>$(SolutionDir)packages</OutputPath>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task<TwiMLResult> ReceiveCallerMessage(ConversationalVoiceRequest r
await messageQueue.EnqueueAsync(callerMessage);

response = new VoiceResponse();
response.Redirect(new Uri($"{_settings.CallbackHost}/twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}"), HttpMethod.Post);
response.Redirect(new Uri($"{_settings.CallbackHost}/twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}&AIResponseWaitTime=0"), HttpMethod.Post);

await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
{
Expand Down Expand Up @@ -217,8 +217,22 @@ public async Task<TwiMLResult> ReplyCallerMessage(ConversationalVoiceRequest req

var reply = await sessionManager.GetAssistantReplyAsync(request.ConversationId, request.SeqNum);
VoiceResponse response;

if (request.AIResponseWaitTime > 5)
{
// Wait AI Response Timeout
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
{
request.AIResponseErrorMessage = $"AI response timeout: AIResponseWaitTime greater than {request.AIResponseWaitTime}, please check internal error log!";
await hook.OnAgentHangUp(request);
}, new HookEmitOption
{
OnlyOnce = true
});

if (reply == null)
response = twilio.HangUp($"twilio/error.mp3");
}
else if (reply == null)
{
var indication = await sessionManager.GetReplyIndicationAsync(request.ConversationId, request.SeqNum);
if (indication != null)
Expand Down Expand Up @@ -262,7 +276,7 @@ public async Task<TwiMLResult> ReplyCallerMessage(ConversationalVoiceRequest req
var instruction = new ConversationalVoiceResponse
{
SpeechPaths = speechPaths,
CallbackPath = $"twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}",
CallbackPath = $"twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}&AIResponseWaitTime={++request.AIResponseWaitTime}",
ActionOnEmptyResult = true
};

Expand Down Expand Up @@ -301,7 +315,7 @@ await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
var instruction = new ConversationalVoiceResponse
{
SpeechPaths = instructions,
CallbackPath = $"twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}",
CallbackPath = $"twilio/voice/{request.ConversationId}/reply/{request.SeqNum}?{GenerateStatesParameter(request.States)}&AIResponseWaitTime={++request.AIResponseWaitTime}",
ActionOnEmptyResult = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ namespace BotSharp.Plugin.Twilio.Models;
public class ConversationalVoiceRequest : VoiceRequest
{
[FromQuery(Name = "agent-id")]
public string AgentId { get; set; }
public string AgentId { get; set; } = string.Empty;

[FromRoute]
public string ConversationId { get; set; }
public string ConversationId { get; set; } = string.Empty;

[FromRoute]
public int SeqNum { get; set; }

public int Attempts { get; set; } = 1;
public int AIResponseWaitTime { get; set; } = 0;
public string? AIResponseErrorMessage { get; set; } = string.Empty;

public string Intent { get; set; }
public string Intent { get; set; } = string.Empty;

public List<string> States { get; set; } = [];
}
Loading
Loading