HttpRequestParams
This commit is contained in:
parent
f063756096
commit
cc6244a301
|
|
@ -23,6 +23,6 @@ public interface IWebBrowser
|
|||
Task<string> ExtractData(BrowserActionParams actionParams);
|
||||
Task<T> EvaluateScript<T>(string conversationId, string script);
|
||||
Task CloseBrowser(string conversationId);
|
||||
Task<BrowserActionResult> SendHttpRequest(BrowserActionParams actionParams);
|
||||
Task<BrowserActionResult> SendHttpRequest(HttpRequestParams actionParams);
|
||||
Task<string> GetAttributeValue(MessageInfo message, ElementLocatingArgs location, BrowserActionResult result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,10 +40,4 @@ public class BrowsingContextIn
|
|||
|
||||
[JsonPropertyName("direction")]
|
||||
public string? Direction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Http request payload
|
||||
/// </summary>
|
||||
[JsonPropertyName("payload")]
|
||||
public string? Payload { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace BotSharp.Abstraction.Browsing.Models;
|
||||
|
||||
public class HttpRequestParams
|
||||
{
|
||||
[JsonPropertyName("url")]
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("method")]
|
||||
public HttpMethod Method { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Http request payload
|
||||
/// </summary>
|
||||
[JsonPropertyName("payload")]
|
||||
public string? Payload { get; set; }
|
||||
|
||||
public HttpRequestParams(string url, HttpMethod method, string? payload = null)
|
||||
{
|
||||
Method = HttpMethod.Get;
|
||||
Url = url;
|
||||
Payload = payload;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +1,34 @@
|
|||
using System.Net.Http;
|
||||
|
||||
namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver;
|
||||
|
||||
public partial class PlaywrightWebDriver
|
||||
{
|
||||
public async Task<BrowserActionResult> SendHttpRequest(BrowserActionParams actionParams)
|
||||
public async Task<BrowserActionResult> SendHttpRequest(HttpRequestParams args)
|
||||
{
|
||||
var result = new BrowserActionResult();
|
||||
|
||||
var body = args.Method == HttpMethod.Post ?
|
||||
$"body: '{args.Payload}'" : string.Empty;
|
||||
|
||||
// Send AJAX request
|
||||
string script = $@"
|
||||
(async () => {{
|
||||
const response = await fetch('{actionParams.Context.Url}', {{
|
||||
method: 'POST',
|
||||
const response = await fetch('{args.Url}', {{
|
||||
method: '{args.Method}',
|
||||
headers: {{
|
||||
'Content-Type': 'application/json'
|
||||
}},
|
||||
body: '{actionParams.Context.Payload}'
|
||||
{body}
|
||||
}});
|
||||
return await response.json();
|
||||
}})();
|
||||
";
|
||||
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
try
|
||||
{
|
||||
var response = await EvaluateScript<object>(actionParams.ConversationId, script);
|
||||
var response = await EvaluateScript<object>(conv.ConversationId, script);
|
||||
result.IsSuccess = true;
|
||||
result.Body = JsonSerializer.Serialize(response);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,11 +17,11 @@ public class HttpRequestFn : IFunctionCallback
|
|||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var convService = _services.GetRequiredService<IConversationService>();
|
||||
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
||||
var args = JsonSerializer.Deserialize<HttpRequestParams>(message.FunctionArgs);
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||
var result = await _browser.SendHttpRequest(new BrowserActionParams(agent, args, convService.ConversationId, message.MessageId));
|
||||
var result = await _browser.SendHttpRequest(args);
|
||||
|
||||
message.Content = result.IsSuccess ?
|
||||
result.Body :
|
||||
|
|
|
|||
Loading…
Reference in a new issue