refine agent actions

This commit is contained in:
Jicheng Lu 2024-11-21 10:31:24 -06:00
parent f3faecb2ee
commit 8d207cbe7a
3 changed files with 25 additions and 14 deletions

View file

@ -22,4 +22,26 @@ public static class UserAuthorizationExtension
var actions = found.Actions ?? [];
return actions.Any(x => x == targetAction);
}
/// <summary>
/// Get allowed user actions on the agent. If user is admin, returns null;
/// </summary>
/// <param name="auth"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public static IEnumerable<string>? GetAllowedAgentActions(this UserAuthorization auth, string agentId)
{
if (auth == null || string.IsNullOrEmpty(agentId))
{
return [];
}
if (auth.IsAdmin)
{
return null;
}
var found = auth.AgentActions.FirstOrDefault(x => x.AgentId == agentId);
return found?.Actions ?? [];
}
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Users.Enums;
namespace BotSharp.OpenAPI.Controllers;
@ -60,11 +59,7 @@ public class AgentController : ControllerBase
var userService = _services.GetRequiredService<IUserService>();
var auth = await userService.GetUserAuthorizations(new List<string> { targetAgent.Id });
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);
targetAgent.Actions = auth.GetAllowedAgentActions(targetAgent.Id);
return targetAgent;
}
@ -91,10 +86,7 @@ public class AgentController : ControllerBase
agents = pagedAgents?.Items?.Select(x =>
{
var model = AgentViewModel.FromAgent(x);
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);
model.Actions = auth.GetAllowedAgentActions(x.Id);
return model;
})?.ToList() ?? [];

View file

@ -48,10 +48,7 @@ public class AgentViewModel
public PluginDef Plugin { get; set; }
public bool Editable { get; set; }
public bool Chatable { get; set; }
public bool Trainable { get; set; }
public bool Evaluable { get; set; }
public IEnumerable<string>? Actions { get; set; }
[JsonPropertyName("created_datetime")]
public DateTime CreatedDateTime { get; set; }