Merge branch 'master' of https://github.com/SciSharp/BotSharp into features/call-dummy-func
This commit is contained in:
commit
8fe89a60e0
|
|
@ -16,4 +16,5 @@ public class StateConst
|
||||||
|
|
||||||
public const string SUB_CONVERSATION_ID = "sub_conversation_id";
|
public const string SUB_CONVERSATION_ID = "sub_conversation_id";
|
||||||
public const string ORIGIN_CONVERSATION_ID = "origin_conversation_id";
|
public const string ORIGIN_CONVERSATION_ID = "origin_conversation_id";
|
||||||
|
public const string WEB_DRIVER_TASK_ID = "web_driver_task_id";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,12 @@
|
||||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-hangup_phone_call.json" />
|
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-hangup_phone_call.json" />
|
||||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-text_message.json" />
|
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-text_message.json" />
|
||||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json" />
|
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json" />
|
||||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-twilio-hangup_phone_call.fn.liquid" />
|
|
||||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-twilio-outbound_phone_call.fn.liquid" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-leave_voicemail.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-transfer_phone_call.json">
|
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-transfer_phone_call.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
|
@ -31,12 +32,6 @@
|
||||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json">
|
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-twilio-outbound_phone_call.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-twilio-hangup_phone_call.fn.liquid">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-twilio-outbound_phone_call.fn.liquid">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
@ -51,8 +46,4 @@
|
||||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -38,16 +38,6 @@ public class TwilioStreamController : TwilioController
|
||||||
var twilio = _services.GetRequiredService<TwilioService>();
|
var twilio = _services.GetRequiredService<TwilioService>();
|
||||||
VoiceResponse response = default!;
|
VoiceResponse response = default!;
|
||||||
|
|
||||||
if (request.AnsweredBy == "machine_start" &&
|
|
||||||
request.Direction == "outbound-api" &&
|
|
||||||
request.InitAudioFile != null)
|
|
||||||
{
|
|
||||||
response = new VoiceResponse();
|
|
||||||
var url = twilio.GetSpeechPath(request.ConversationId, request.InitAudioFile);
|
|
||||||
response.Play(new Uri(url));
|
|
||||||
return TwiML(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
var instruction = new ConversationalVoiceResponse
|
var instruction = new ConversationalVoiceResponse
|
||||||
{
|
{
|
||||||
ConversationId = request.ConversationId,
|
ConversationId = request.ConversationId,
|
||||||
|
|
@ -70,7 +60,24 @@ public class TwilioStreamController : TwilioController
|
||||||
|
|
||||||
request.ConversationId = await InitConversation(request);
|
request.ConversationId = await InitConversation(request);
|
||||||
instruction.ConversationId = request.ConversationId;
|
instruction.ConversationId = request.ConversationId;
|
||||||
response = twilio.ReturnBidirectionalMediaStreamsInstructions(instruction);
|
|
||||||
|
if (request.AnsweredBy == "machine_start" &&
|
||||||
|
request.Direction == "outbound-api")
|
||||||
|
{
|
||||||
|
response = new VoiceResponse();
|
||||||
|
|
||||||
|
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, async hook =>
|
||||||
|
{
|
||||||
|
await hook.OnVoicemailStarting(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
var url = twilio.GetSpeechPath(request.ConversationId, "voicemail.mp3");
|
||||||
|
response.Play(new Uri(url));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response = twilio.ReturnBidirectionalMediaStreamsInstructions(instruction);
|
||||||
|
}
|
||||||
|
|
||||||
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
|
await HookEmitter.Emit<ITwilioSessionHook>(_services, async hook =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -475,8 +475,7 @@ public class TwilioVoiceController : TwilioController
|
||||||
if (request.CallStatus == "completed")
|
if (request.CallStatus == "completed")
|
||||||
{
|
{
|
||||||
if (request.AnsweredBy == "machine_start" &&
|
if (request.AnsweredBy == "machine_start" &&
|
||||||
request.Direction == "outbound-api" &&
|
request.Direction == "outbound-api")
|
||||||
request.InitAudioFile != null)
|
|
||||||
{
|
{
|
||||||
// voicemail
|
// voicemail
|
||||||
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, async hook =>
|
await HookEmitter.Emit<ITwilioCallStatusHook>(_services, async hook =>
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,5 @@ public interface ITwilioCallStatusHook
|
||||||
Task OnVoicemailLeft(ConversationalVoiceRequest request);
|
Task OnVoicemailLeft(ConversationalVoiceRequest request);
|
||||||
Task OnUserDisconnected(ConversationalVoiceRequest request);
|
Task OnUserDisconnected(ConversationalVoiceRequest request);
|
||||||
Task OnRecordingCompleted(ConversationalVoiceRequest request);
|
Task OnRecordingCompleted(ConversationalVoiceRequest request);
|
||||||
|
Task OnVoicemailStarting(ConversationalVoiceRequest request);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
using BotSharp.Abstraction.Files;
|
||||||
|
using BotSharp.Abstraction.Routing;
|
||||||
|
using BotSharp.Core.Infrastructures;
|
||||||
|
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions;
|
||||||
|
|
||||||
|
public class LeaveVoicemailFn : IFunctionCallback
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _services;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly TwilioSetting _setting;
|
||||||
|
|
||||||
|
public string Name => "util-twilio-leave_voicemail";
|
||||||
|
public string Indication => "leaving a voicemail";
|
||||||
|
|
||||||
|
public LeaveVoicemailFn(
|
||||||
|
IServiceProvider services,
|
||||||
|
ILogger<LeaveVoicemailFn> logger,
|
||||||
|
TwilioSetting setting)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
_logger = logger;
|
||||||
|
_setting = setting;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
|
{
|
||||||
|
var args = JsonSerializer.Deserialize<LeaveVoicemailArgs>(message.FunctionArgs);
|
||||||
|
|
||||||
|
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
||||||
|
var routing = _services.GetRequiredService<IRoutingService>();
|
||||||
|
var conversationId = routing.Context.ConversationId;
|
||||||
|
var states = _services.GetRequiredService<IConversationStateService>();
|
||||||
|
var callSid = states.GetState("twilio_call_sid");
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(callSid))
|
||||||
|
{
|
||||||
|
message.Content = "The call has not been initiated.";
|
||||||
|
_logger.LogError(message.Content);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate voice message audio
|
||||||
|
string initAudioFile = null;
|
||||||
|
if (!string.IsNullOrEmpty(args.VoicemailMessage))
|
||||||
|
{
|
||||||
|
var completion = CompletionProvider.GetAudioCompletion(_services, "openai", "tts-1");
|
||||||
|
var data = await completion.GenerateAudioFromTextAsync(args.VoicemailMessage);
|
||||||
|
initAudioFile = "voicemail.mp3";
|
||||||
|
fileStorage.SaveSpeechFile(conversationId, initAudioFile, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
message.Content = args.VoicemailMessage;
|
||||||
|
message.StopCompletion = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
|
||||||
private static string TRANSFER_PHONE_CALL_FN = $"{PREFIX}transfer_phone_call";
|
private static string TRANSFER_PHONE_CALL_FN = $"{PREFIX}transfer_phone_call";
|
||||||
private static string HANGUP_PHONE_CALL_FN = $"{PREFIX}hangup_phone_call";
|
private static string HANGUP_PHONE_CALL_FN = $"{PREFIX}hangup_phone_call";
|
||||||
private static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
|
private static string TEXT_MESSAGE_FN = $"{PREFIX}text_message";
|
||||||
|
private static string LEAVE_VOICEMAIL_FN = $"{PREFIX}leave_voicemail";
|
||||||
|
|
||||||
public void AddUtilities(List<AgentUtility> utilities)
|
public void AddUtilities(List<AgentUtility> utilities)
|
||||||
{
|
{
|
||||||
|
|
@ -21,12 +22,11 @@ public class OutboundPhoneCallHandlerUtilityHook : IAgentUtilityHook
|
||||||
new($"{OUTBOUND_PHONE_CALL_FN}"),
|
new($"{OUTBOUND_PHONE_CALL_FN}"),
|
||||||
new($"{TRANSFER_PHONE_CALL_FN}"),
|
new($"{TRANSFER_PHONE_CALL_FN}"),
|
||||||
new($"{HANGUP_PHONE_CALL_FN}"),
|
new($"{HANGUP_PHONE_CALL_FN}"),
|
||||||
new($"{TEXT_MESSAGE_FN}")
|
new($"{TEXT_MESSAGE_FN}"),
|
||||||
|
new($"{LEAVE_VOICEMAIL_FN}")
|
||||||
],
|
],
|
||||||
Templates =
|
Templates =
|
||||||
[
|
[
|
||||||
new($"{OUTBOUND_PHONE_CALL_FN}.fn"),
|
|
||||||
new($"{HANGUP_PHONE_CALL_FN}.fn")
|
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||||
|
|
||||||
|
public class LeaveVoicemailArgs
|
||||||
|
{
|
||||||
|
[JsonPropertyName("phone_number")]
|
||||||
|
public string PhoneNumber { get; set; } = null!;
|
||||||
|
|
||||||
|
[JsonPropertyName("voicemail_message")]
|
||||||
|
public string VoicemailMessage { get; set; } = null!;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"name": "util-twilio-leave_voicemail",
|
||||||
|
"description": "If the user wants you to leave a voicemail.",
|
||||||
|
"visibility_expression": "{% if states.channel == 'phone' %}visible{% endif %}",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"voicemail_message": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "User voicemail with details."
|
||||||
|
},
|
||||||
|
"phone_number": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Phone number to callback."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [ "voicemail_message", "phone_number" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
{% if channel == 'phone' %}
|
|
||||||
** If user wants to end the phone call or conversation, ask user if there is anything else to help. If not, end the phone call.
|
|
||||||
{% endif %}
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
** Please call util-twilio-outbound_phone_call if user wants to make an outbound call.
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class ChangeCheckboxFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class ChangeListValueFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class CheckRadioButtonFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class ClickButtonFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class ClickElementFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ public class ExtractDataFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ public class GoToPageFn : IFunctionCallback
|
||||||
var result = await _browser.GoToPage(new MessageInfo
|
var result = await _browser.GoToPage(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, new PageActionArgs
|
}, new PageActionArgs
|
||||||
{
|
{
|
||||||
|
|
@ -45,7 +45,7 @@ public class GoToPageFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using BotSharp.Plugin.WebDriver.Services;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.WebDriver.Functions;
|
namespace BotSharp.Plugin.WebDriver.Functions;
|
||||||
|
|
||||||
public class HttpRequestFn : IFunctionCallback
|
public class HttpRequestFn : IFunctionCallback
|
||||||
|
|
@ -20,12 +22,13 @@ public class HttpRequestFn : IFunctionCallback
|
||||||
var args = JsonSerializer.Deserialize<HttpRequestParams>(message.FunctionArgs);
|
var args = JsonSerializer.Deserialize<HttpRequestParams>(message.FunctionArgs);
|
||||||
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||||
var result = await _browser.SendHttpRequest(new MessageInfo
|
var result = await _browser.SendHttpRequest(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = agent.Id,
|
AgentId = agent.Id,
|
||||||
MessageId = message.MessageId,
|
MessageId = message.MessageId,
|
||||||
ContextId = convService.ConversationId
|
ContextId = webDriverService.GetMessageContext(message)
|
||||||
}, args);
|
}, args);
|
||||||
|
|
||||||
message.Content = result.IsSuccess ?
|
message.Content = result.IsSuccess ?
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ public class InputUserPasswordFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ public class InputUserTextFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ public class OpenBrowserFn : IFunctionCallback
|
||||||
var msgInfo = new MessageInfo
|
var msgInfo = new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
};
|
};
|
||||||
var result = await _browser.LaunchBrowser(msgInfo, new BrowserActionArgs
|
var result = await _browser.LaunchBrowser(msgInfo, new BrowserActionArgs
|
||||||
|
|
@ -58,7 +58,7 @@ public class OpenBrowserFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ public class ScreenshotFn : IFunctionCallback
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
message.Content = "Took screenshot completed. You can take another screenshot if needed.";
|
message.Content = "Took screenshot completed. You can take another screenshot if needed.";
|
||||||
|
|
|
||||||
|
|
@ -20,12 +20,13 @@ public class ScrollPageFn : IFunctionCallback
|
||||||
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
||||||
|
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||||
|
|
||||||
message.Data = await _browser.ScrollPage(new MessageInfo
|
message.Data = await _browser.ScrollPage(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = agent.Id,
|
AgentId = agent.Id,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, new PageActionArgs
|
}, new PageActionArgs
|
||||||
{
|
{
|
||||||
|
|
@ -35,13 +36,12 @@ public class ScrollPageFn : IFunctionCallback
|
||||||
|
|
||||||
message.Content = "Scrolled. You can scroll more if needed.";
|
message.Content = "Scrolled. You can scroll more if needed.";
|
||||||
|
|
||||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
|
||||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||||
|
|
||||||
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
ContextId = convService.ConversationId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
MessageId = message.MessageId
|
MessageId = message.MessageId
|
||||||
}, path);
|
}, path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
using BotSharp.Abstraction.Infrastructures.Enums;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.WebDriver.Services
|
||||||
|
{
|
||||||
|
public partial class WebDriverService
|
||||||
|
{
|
||||||
|
public string GetMessageContext(RoleDialogModel message)
|
||||||
|
{
|
||||||
|
var states = _services.GetService<IConversationStateService>();
|
||||||
|
var convService = _services.GetRequiredService<IConversationService>();
|
||||||
|
var webDriverTaskId = states.GetState(StateConst.WEB_DRIVER_TASK_ID, "");
|
||||||
|
var contextId = message.CurrentAgentId;
|
||||||
|
if (!string.IsNullOrWhiteSpace(webDriverTaskId))
|
||||||
|
{
|
||||||
|
contextId = webDriverTaskId;
|
||||||
|
}
|
||||||
|
return contextId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -35,17 +35,17 @@ public class UtilWebActionOnElementFn : IFunctionCallback
|
||||||
var conv = _services.GetRequiredService<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
|
|
||||||
var browser = _services.GetRequiredService<IWebBrowser>();
|
var browser = _services.GetRequiredService<IWebBrowser>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var msg = new MessageInfo
|
var msg = new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
MessageId = message.MessageId,
|
MessageId = message.MessageId,
|
||||||
ContextId = message.CurrentAgentId,
|
ContextId = webDriverService.GetMessageContext(message),
|
||||||
};
|
};
|
||||||
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
|
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
|
||||||
|
|
||||||
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}";
|
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}";
|
||||||
|
|
||||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
|
||||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||||
|
|
||||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||||
|
|
|
||||||
|
|
@ -18,20 +18,19 @@ public class UtilWebCloseBrowserFn : IFunctionCallback
|
||||||
public async Task<bool> Execute(RoleDialogModel message)
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
var conv = _services.GetRequiredService<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var browser = _services.GetRequiredService<IWebBrowser>();
|
var browser = _services.GetRequiredService<IWebBrowser>();
|
||||||
var msg = new MessageInfo
|
var msg = new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
MessageId = message.MessageId,
|
MessageId = message.MessageId,
|
||||||
ContextId = message.CurrentAgentId,
|
ContextId = webDriverService.GetMessageContext(message)
|
||||||
};
|
};
|
||||||
|
|
||||||
await browser.CloseBrowser(message.CurrentAgentId);
|
await browser.CloseBrowser(message.CurrentAgentId);
|
||||||
|
|
||||||
message.Content = $"Browser closed.";
|
message.Content = $"Browser closed.";
|
||||||
|
|
||||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
|
||||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||||
|
|
||||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,13 @@ public class UtilWebGoToPageFn : IFunctionCallback
|
||||||
args.WaitTime = _webDriver.DefaultWaitTime;
|
args.WaitTime = _webDriver.DefaultWaitTime;
|
||||||
|
|
||||||
var conv = _services.GetRequiredService<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var browser = _services.GetRequiredService<IWebBrowser>();
|
var browser = _services.GetRequiredService<IWebBrowser>();
|
||||||
var msg = new MessageInfo
|
var msg = new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
MessageId = message.MessageId,
|
MessageId = message.MessageId,
|
||||||
ContextId = message.CurrentAgentId,
|
ContextId = webDriverService.GetMessageContext(message)
|
||||||
};
|
};
|
||||||
if (!args.KeepBrowserOpen)
|
if (!args.KeepBrowserOpen)
|
||||||
{
|
{
|
||||||
|
|
@ -50,7 +50,6 @@ public class UtilWebGoToPageFn : IFunctionCallback
|
||||||
|
|
||||||
message.Content = $"Open web page successfully.";
|
message.Content = $"Open web page successfully.";
|
||||||
|
|
||||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
|
||||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||||
|
|
||||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||||
|
|
|
||||||
|
|
@ -22,17 +22,17 @@ public class UtilWebLocateElementFn : IFunctionCallback
|
||||||
locatorArgs.Highlight = true;
|
locatorArgs.Highlight = true;
|
||||||
|
|
||||||
var browser = _services.GetRequiredService<IWebBrowser>();
|
var browser = _services.GetRequiredService<IWebBrowser>();
|
||||||
|
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
||||||
var msg = new MessageInfo
|
var msg = new MessageInfo
|
||||||
{
|
{
|
||||||
AgentId = message.CurrentAgentId,
|
AgentId = message.CurrentAgentId,
|
||||||
MessageId = message.MessageId,
|
MessageId = message.MessageId,
|
||||||
ContextId = message.CurrentAgentId,
|
ContextId = webDriverService.GetMessageContext(message)
|
||||||
};
|
};
|
||||||
var result = await browser.LocateElement(msg, locatorArgs);
|
var result = await browser.LocateElement(msg, locatorArgs);
|
||||||
|
|
||||||
message.Content = $"Locating element {(result.IsSuccess ? "success" : "failed")}";
|
message.Content = $"Locating element {(result.IsSuccess ? "success" : "failed")}";
|
||||||
|
|
||||||
var webDriverService = _services.GetRequiredService<WebDriverService>();
|
|
||||||
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
|
||||||
|
|
||||||
message.Data = await browser.ScreenshotAsync(msg, path);
|
message.Data = await browser.ScreenshotAsync(msg, path);
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,7 @@ public class WebDriverPlugin : IBotSharpPlugin
|
||||||
{
|
{
|
||||||
var settings = new WebBrowsingSettings();
|
var settings = new WebBrowsingSettings();
|
||||||
config.Bind("WebBrowsing", settings);
|
config.Bind("WebBrowsing", settings);
|
||||||
|
services.AddSingleton(x => settings);
|
||||||
services.AddScoped(provider =>
|
|
||||||
{
|
|
||||||
var settingService = provider.GetRequiredService<ISettingService>();
|
|
||||||
return settings;
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddScoped<PlaywrightWebDriver>();
|
services.AddScoped<PlaywrightWebDriver>();
|
||||||
services.AddSingleton<PlaywrightInstance>();
|
services.AddSingleton<PlaywrightInstance>();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue