2024-05-15 20:54:08 +00:00
|
|
|
using BotSharp.Abstraction.Users.Enums;
|
2024-01-23 05:04:06 +00:00
|
|
|
|
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;
|
2024-05-15 20:54:08 +00:00
|
|
|
private readonly IUserIdentity _user;
|
2023-09-21 16:23:43 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
2024-05-15 20:54:08 +00:00
|
|
|
public AgentController(IAgentService agentService, IUserIdentity user, IServiceProvider services)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
|
|
|
|
_agentService = agentService;
|
2024-05-15 20:54:08 +00:00
|
|
|
_user = user;
|
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}")]
|
2024-05-21 20:55:12 +00:00
|
|
|
public async Task<AgentViewModel?> GetAgent([FromRoute] string id)
|
2023-10-11 04:11:06 +00:00
|
|
|
{
|
2024-08-13 21:36:12 +00:00
|
|
|
var pagedAgents = await _agentService.GetAgents(new AgentFilter
|
2024-01-28 01:02:33 +00:00
|
|
|
{
|
|
|
|
|
AgentIds = new List<string> { id }
|
2024-08-13 21:36:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var foundAgent = pagedAgents.Items.FirstOrDefault();
|
|
|
|
|
if (foundAgent == null) return null;
|
2024-03-22 06:30:57 +00:00
|
|
|
|
2024-08-13 21:36:12 +00:00
|
|
|
await _agentService.InheritAgent(foundAgent);
|
|
|
|
|
var targetAgent = AgentViewModel.FromAgent(foundAgent);
|
|
|
|
|
var agentSetting = _services.GetRequiredService<AgentSettings>();
|
|
|
|
|
targetAgent.IsHost = targetAgent.Id == agentSetting.HostAgentId;
|
2024-05-21 20:55:12 +00:00
|
|
|
|
2024-03-22 06:30:57 +00:00
|
|
|
var redirectAgentIds = targetAgent.RoutingRules
|
|
|
|
|
.Where(x => !string.IsNullOrEmpty(x.RedirectTo))
|
2024-08-13 22:37:47 +00:00
|
|
|
.Select(x => x.RedirectTo)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-03-22 06:30:57 +00:00
|
|
|
var redirectAgents = await _agentService.GetAgents(new AgentFilter
|
|
|
|
|
{
|
|
|
|
|
AgentIds = redirectAgentIds
|
|
|
|
|
});
|
|
|
|
|
foreach (var rule in targetAgent.RoutingRules)
|
|
|
|
|
{
|
|
|
|
|
var found = redirectAgents.Items.FirstOrDefault(x => x.Id == rule.RedirectTo);
|
|
|
|
|
if (found == null) continue;
|
|
|
|
|
|
|
|
|
|
rule.RedirectToAgentName = found.Name;
|
|
|
|
|
}
|
2024-05-15 20:54:08 +00:00
|
|
|
|
|
|
|
|
var userService = _services.GetRequiredService<IUserService>();
|
2024-11-15 02:19:53 +00:00
|
|
|
var auth = await userService.GetUserAuthorizations(new List<string> { targetAgent.Id });
|
2024-05-15 20:54:08 +00:00
|
|
|
|
2024-11-15 02:19:53 +00:00
|
|
|
targetAgent.Editable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Edit);
|
|
|
|
|
targetAgent.Chatable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Chat);
|
|
|
|
|
targetAgent.Trainable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Train);
|
|
|
|
|
targetAgent.Evaluable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Evaluate);
|
2024-03-22 06:30:57 +00:00
|
|
|
return targetAgent;
|
2023-10-11 04:11:06 +00:00
|
|
|
}
|
2024-07-08 22:37:22 +00:00
|
|
|
|
2024-01-23 17:32:28 +00:00
|
|
|
[HttpGet("/agents")]
|
2024-11-01 18:17:25 +00:00
|
|
|
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter, [FromQuery] bool checkAuth = false)
|
2023-09-15 20:19:44 +00:00
|
|
|
{
|
2024-01-28 01:02:33 +00:00
|
|
|
var agentSetting = _services.GetRequiredService<AgentSettings>();
|
2024-10-31 19:36:26 +00:00
|
|
|
var userService = _services.GetRequiredService<IUserService>();
|
|
|
|
|
|
2024-11-01 18:17:25 +00:00
|
|
|
List<AgentViewModel> agents;
|
2024-01-23 05:04:06 +00:00
|
|
|
var pagedAgents = await _agentService.GetAgents(filter);
|
2024-11-01 18:17:25 +00:00
|
|
|
|
|
|
|
|
if (!checkAuth)
|
|
|
|
|
{
|
|
|
|
|
agents = pagedAgents?.Items?.Select(x => AgentViewModel.FromAgent(x))?.ToList() ?? [];
|
|
|
|
|
return new PagedItems<AgentViewModel>
|
|
|
|
|
{
|
|
|
|
|
Items = agents,
|
|
|
|
|
Count = pagedAgents?.Count ?? 0
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 02:19:53 +00:00
|
|
|
var auth = await userService.GetUserAuthorizations(pagedAgents.Items.Select(x => x.Id));
|
2024-11-01 18:17:25 +00:00
|
|
|
agents = pagedAgents?.Items?.Select(x =>
|
2024-10-31 19:36:26 +00:00
|
|
|
{
|
|
|
|
|
var model = AgentViewModel.FromAgent(x);
|
2024-11-15 02:19:53 +00:00
|
|
|
model.Editable = auth.IsAgentActionAllowed(x.Id, UserAction.Edit);
|
|
|
|
|
model.Chatable = auth.IsAgentActionAllowed(x.Id, UserAction.Chat);
|
|
|
|
|
model.Trainable = auth.IsAgentActionAllowed(x.Id, UserAction.Train);
|
|
|
|
|
model.Evaluable = auth.IsAgentActionAllowed(x.Id, UserAction.Evaluate);
|
2024-10-31 19:36:26 +00:00
|
|
|
return model;
|
|
|
|
|
})?.ToList() ?? [];
|
2024-01-28 01:02:33 +00:00
|
|
|
|
2024-01-23 05:04:06 +00:00
|
|
|
return new PagedItems<AgentViewModel>
|
|
|
|
|
{
|
2024-01-28 01:02:33 +00:00
|
|
|
Items = agents,
|
2024-08-13 21:36:12 +00:00
|
|
|
Count = pagedAgents?.Count ?? 0
|
2024-01-23 05:04:06 +00:00
|
|
|
};
|
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")]
|
2024-04-11 07:00:17 +00:00
|
|
|
public async Task<string> RefreshAgents()
|
2023-10-17 20:29:40 +00:00
|
|
|
{
|
2024-04-11 07:00:17 +00:00
|
|
|
return await _agentService.RefreshAgents();
|
2023-10-17 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 20:19:44 +00:00
|
|
|
[HttpPut("/agent/file/{agentId}")]
|
2024-04-11 07:00:17 +00:00
|
|
|
public async Task<string> UpdateAgentFromFile([FromRoute] string agentId)
|
2023-09-15 20:19:44 +00:00
|
|
|
{
|
2024-04-11 07:00:17 +00:00
|
|
|
return await _agentService.UpdateAgentFromFile(agentId);
|
2023-09-15 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2024-05-09 22:06:03 +00:00
|
|
|
|
|
|
|
|
[HttpPatch("/agent/{agentId}/templates")]
|
|
|
|
|
public async Task<string> PatchAgentTemplates([FromRoute] string agentId, [FromBody] AgentTemplatePatchModel agent)
|
|
|
|
|
{
|
|
|
|
|
var model = agent.ToAgent();
|
|
|
|
|
model.Id = agentId;
|
|
|
|
|
return await _agentService.PatchAgentTemplate(model);
|
|
|
|
|
}
|
2024-05-21 20:55:12 +00:00
|
|
|
|
|
|
|
|
[HttpDelete("/agent/{agentId}")]
|
|
|
|
|
public async Task<bool> DeleteAgent([FromRoute] string agentId)
|
|
|
|
|
{
|
|
|
|
|
return await _agentService.DeleteAgent(agentId);
|
|
|
|
|
}
|
2024-06-25 04:34:30 +00:00
|
|
|
|
2024-07-02 18:36:45 +00:00
|
|
|
[HttpGet("/agent/utilities")]
|
|
|
|
|
public IEnumerable<string> GetAgentUtilities()
|
2024-06-25 04:34:30 +00:00
|
|
|
{
|
2024-07-02 18:36:45 +00:00
|
|
|
return _agentService.GetAgentUtilities();
|
2024-06-25 04:34:30 +00:00
|
|
|
}
|
2023-08-19 00:15:02 +00:00
|
|
|
}
|