Skip to content

Features/refine agent filter #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ List<User> GetUsersByAffiliateId(string affiliateId)
=> throw new NotImplementedException();
void UpdateUserName(string userId, string userName)
=> throw new NotImplementedException();
Dashboard? GetDashboard(string id = null)
Dashboard? GetDashboard(string userId = null)
=> throw new NotImplementedException();
void CreateUser(User user)
=> throw new NotImplementedException();
Expand Down
8 changes: 4 additions & 4 deletions src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public interface IUserService
Task<bool> UpdatePassword(string newPassword, string verificationCode);
Task<DateTime> GetUserTokenExpires();
Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisable);
Task<bool> AddDashboardConversation(string userId, string conversationId);
Task<bool> RemoveDashboardConversation(string userId, string conversationId);
Task UpdateDashboardConversation(string userId, DashboardConversation dashConv);
Task<Dashboard?> GetDashboard(string userId);
Task<bool> AddDashboardConversation(string conversationId);
Task<bool> RemoveDashboardConversation(string conversationId);
Task UpdateDashboardConversation(DashboardConversation dashConv);
Task<Dashboard?> GetDashboard();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<User> GetUsersByAffiliateId(string affiliateId)
return Users.FirstOrDefault(x => x.UserName == userName.ToLower());
}

public Dashboard? GetDashboard(string id = null)
public Dashboard? GetDashboard(string userId = null)
{
return Dashboards.FirstOrDefault();
}
Expand Down
29 changes: 18 additions & 11 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,40 +810,47 @@ public async Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisabl
return true;
}

public async Task<bool> AddDashboardConversation(string userId, string conversationId)
public async Task<bool> AddDashboardConversation(string conversationId)
{
var user = await GetUser(_user.Id);
var db = _services.GetRequiredService<IBotSharpRepository>();
db.AddDashboardConversation(userId, conversationId);

db.AddDashboardConversation(user?.Id, conversationId);
await Task.CompletedTask;
return true;
}

public async Task<bool> RemoveDashboardConversation(string userId, string conversationId)
public async Task<bool> RemoveDashboardConversation(string conversationId)
{
var user = await GetUser(_user.Id);
var db = _services.GetRequiredService<IBotSharpRepository>();
db.RemoveDashboardConversation(userId, conversationId);

db.RemoveDashboardConversation(user?.Id, conversationId);
await Task.CompletedTask;
return true;
}

public async Task UpdateDashboardConversation(string userId, DashboardConversation newDashConv)
public async Task UpdateDashboardConversation(DashboardConversation newDashConv)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var dashConv = db.GetDashboard(userId)?.ConversationList.FirstOrDefault(x => string.Equals(x.ConversationId, newDashConv.ConversationId));

var user = await GetUser(_user.Id);
var dashConv = db.GetDashboard(user?.Id)?
.ConversationList
.FirstOrDefault(x => string.Equals(x.ConversationId, newDashConv.ConversationId));
if (dashConv == null) return;

dashConv.Name = newDashConv.Name ?? dashConv.Name;
dashConv.Instruction = newDashConv.Instruction ?? dashConv.Instruction;
db.UpdateDashboardConversation(userId, dashConv);
db.UpdateDashboardConversation(user?.Id, dashConv);
await Task.CompletedTask;
return;
}

public async Task<Dashboard?> GetDashboard(string userId)
public async Task<Dashboard?> GetDashboard()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var dash = db.GetDashboard();

var user = await GetUser(_user.Id);
var dash = db.GetDashboard(user?.Id);
await Task.CompletedTask;
return dash;
}
Expand Down
24 changes: 20 additions & 4 deletions src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public AgentSettings GetSettings()
return targetAgent;
}

[HttpGet("/agents")]
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter, [FromQuery] bool checkAuth = false)
[HttpPost("/agents")]
public async Task<PagedItems<AgentViewModel>> GetAgents([FromBody] AgentQueryRequest request)
{
var agentSetting = _services.GetRequiredService<AgentSettings>();
var userService = _services.GetRequiredService<IUserService>();

List<AgentViewModel> agents;
var pagedAgents = await _agentService.GetAgents(filter);
var pagedAgents = await _agentService.GetAgents(request.Filter);

if (!checkAuth)
if (!request.CheckAuth)
{
agents = pagedAgents?.Items?.Select(x => AgentViewModel.FromAgent(x))?.ToList() ?? [];
return new PagedItems<AgentViewModel>
Expand Down Expand Up @@ -160,4 +160,20 @@ public IEnumerable<AgentUtility> GetAgentUtilityOptions()
}
return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
}

[HttpGet("/agent/labels")]
public async Task<IEnumerable<string>> GetAgentLabels()
{
var agentService = _services.GetRequiredService<IAgentService>();
var agents = await agentService.GetAgents(new AgentFilter
{
Pager = new Pagination { Size = 1000 }
});

var labels = agents.Items?.SelectMany(x => x.Labels)
.Distinct()
.OrderBy(x => x)
.ToList() ?? [];
return labels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,24 +535,20 @@ public IActionResult DownloadMessageFile([FromRoute] string conversationId, [Fro
}
#endregion

#region miscellaneous
#region Dashboard
[HttpPut("/agent/{agentId}/conversation/{conversationId}/dashboard")]
public async Task<bool> PinConversationToDashboard([FromRoute] string agentId, [FromRoute] string conversationId)
{
var userService = _services.GetRequiredService<IUserService>();

var user = await userService.GetUser(_user.Id);
var pinned = await userService.AddDashboardConversation(user.Id, conversationId);
var pinned = await userService.AddDashboardConversation(conversationId);
return pinned;
}

[HttpDelete("/agent/{agentId}/conversation/{conversationId}/dashboard")]
public async Task<bool> UnpinConversationFromDashboard([FromRoute] string agentId, [FromRoute] string conversationId)
{
var userService = _services.GetRequiredService<IUserService>();

var user = await userService.GetUser(_user.Id);
var unpinned = await userService.RemoveDashboardConversation(user.Id, conversationId);
var unpinned = await userService.RemoveDashboardConversation(conversationId);
return unpinned;
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public DashboardController(IServiceProvider services,
}
#region User Components
[HttpGet("/dashboard/components")]
public async Task<UserDashboardModel> GetComponents(string userId)
public async Task<UserDashboardModel> GetComponents()
{
var userService = _services.GetRequiredService<IUserService>();
var dashboardProfile = await userService.GetDashboard(userId);
var dashboardProfile = await userService.GetDashboard();
if (dashboardProfile == null) return new UserDashboardModel();

var result = new UserDashboardModel
{
ConversationList = dashboardProfile.ConversationList.Select(
Expand All @@ -39,7 +40,7 @@ public async Task<UserDashboardModel> GetComponents(string userId)
}

[HttpPost("/dashboard/component/conversation")]
public async Task UpdateDashboardConversationInstruction(string userId, UserDashboardConversationModel dashConv)
public async Task UpdateDashboardConversationInstruction(UserDashboardConversationModel dashConv)
{
if (string.IsNullOrEmpty(dashConv.Name) && string.IsNullOrEmpty(dashConv.Instruction))
{
Expand All @@ -60,7 +61,7 @@ public async Task UpdateDashboardConversationInstruction(string userId, UserDash
}

var userService = _services.GetRequiredService<IUserService>();
await userService.UpdateDashboardConversation(userId, newDashConv);
await userService.UpdateDashboardConversation(newDashConv);
return;
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.OpenAPI.ViewModels.Agents;

public class AgentQueryRequest
{
public AgentFilter Filter { get; set; } = AgentFilter.Empty();
public bool CheckAuth { get; set; }
}