2024-01-23 05:04:06 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
namespace BotSharp.OpenAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
2023-12-27 15:53:54 +00:00
|
|
|
public class AgentController : ControllerBase
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
|
|
|
|
private readonly IAgentService _agentService;
|
2023-09-21 16:23:43 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public AgentController(IAgentService agentService, IServiceProvider services)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
|
|
|
|
_agentService = agentService;
|
2023-09-21 16:23:43 +00:00
|
|
|
_services = services;
|
2023-08-19 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-30 03:54:41 +00:00
|
|
|
[HttpGet("/agent/settings")]
|
|
|
|
|
public AgentSettings GetSettings()
|
|
|
|
|
{
|
|
|
|
|
var settings = _services.GetRequiredService<AgentSettings>();
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 04:11:06 +00:00
|
|
|
[HttpGet("/agent/{id}")]
|
|
|
|
|
public async Task<AgentViewModel> GetAgent([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
var agent = await _agentService.GetAgent(id);
|
|
|
|
|
return AgentViewModel.FromAgent(agent);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 17:32:28 +00:00
|
|
|
[HttpGet("/agents")]
|
|
|
|
|
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter)
|
2023-09-15 20:19:44 +00:00
|
|
|
{
|
2024-01-23 05:04:06 +00:00
|
|
|
var pagedAgents = await _agentService.GetAgents(filter);
|
2024-01-27 01:14:14 +00:00
|
|
|
var items = new List<Agent>();
|
|
|
|
|
foreach (var agent in pagedAgents.Items)
|
|
|
|
|
{
|
|
|
|
|
var renderedAgent = await _agentService.LoadAgent(agent.Id);
|
|
|
|
|
items.Add(renderedAgent);
|
|
|
|
|
}
|
2024-01-23 05:04:06 +00:00
|
|
|
return new PagedItems<AgentViewModel>
|
|
|
|
|
{
|
2024-01-27 01:14:14 +00:00
|
|
|
Items = items.Select(x => AgentViewModel.FromAgent(x)).ToList(),
|
2024-01-23 05:04:06 +00:00
|
|
|
Count = pagedAgents.Count
|
|
|
|
|
};
|
2023-09-15 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
[HttpPost("/agent")]
|
|
|
|
|
public async Task<AgentViewModel> CreateAgent(AgentCreationModel agent)
|
|
|
|
|
{
|
|
|
|
|
var createdAgent = await _agentService.CreateAgent(agent.ToAgent());
|
|
|
|
|
return AgentViewModel.FromAgent(createdAgent);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 20:29:40 +00:00
|
|
|
[HttpPost("/refresh-agents")]
|
|
|
|
|
public async Task RefreshAgents()
|
|
|
|
|
{
|
|
|
|
|
await _agentService.RefreshAgents();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 20:19:44 +00:00
|
|
|
[HttpPut("/agent/file/{agentId}")]
|
|
|
|
|
public async Task UpdateAgentFromFile([FromRoute] string agentId)
|
|
|
|
|
{
|
|
|
|
|
await _agentService.UpdateAgentFromFile(agentId);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 03:16:41 +00:00
|
|
|
[HttpPut("/agent/{agentId}")]
|
2023-09-15 20:19:44 +00:00
|
|
|
public async Task UpdateAgent([FromRoute] string agentId, [FromBody] AgentUpdateModel agent)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
|
|
|
|
var model = agent.ToAgent();
|
|
|
|
|
model.Id = agentId;
|
2023-09-15 20:19:44 +00:00
|
|
|
await _agentService.UpdateAgent(model, AgentField.All);
|
2023-08-19 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-26 03:16:41 +00:00
|
|
|
[HttpPatch("/agent/{agentId}/{field}")]
|
|
|
|
|
public async Task PatchAgentByField([FromRoute] string agentId, AgentField field, [FromBody] AgentUpdateModel agent)
|
2023-12-13 20:26:46 +00:00
|
|
|
{
|
|
|
|
|
var model = agent.ToAgent();
|
|
|
|
|
model.Id = agentId;
|
2023-12-26 03:16:41 +00:00
|
|
|
await _agentService.UpdateAgent(model, field);
|
2023-12-13 20:26:46 +00:00
|
|
|
}
|
2023-08-19 00:15:02 +00:00
|
|
|
}
|