Skip to content
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

add agent pagination #261

Merged
merged 1 commit into from
Jan 23, 2024
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 @@ -10,7 +10,7 @@ public interface IAgentService
{
Task<Agent> CreateAgent(Agent agent);
Task RefreshAgents();
Task<List<Agent>> GetAgents(AgentFilter filter);
Task<PagedItems<Agent>> GetAgents(AgentFilter filter);

/// <summary>
/// Load agent configurations and trigger hooks
Expand All @@ -29,7 +29,7 @@ public interface IAgentService
/// <param name="id"></param>
/// <returns>Original agent information</returns>
Task<Agent> GetAgent(string id);

Task<bool> DeleteAgent(string id);
Task UpdateAgent(Agent agent, AgentField updateField);
Task UpdateAgentFromFile(string id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Repositories.Filters;

public class AgentFilter
{
public Pagination Pager { get; set; } = new Pagination();
public string? AgentName { get; set; }
public bool? Disabled { get; set; }
public bool? Installed { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class AgentService
#if !DEBUG
[MemoryCache(10 * 60)]
#endif
public async Task<List<Agent>> GetAgents(AgentFilter filter)
public async Task<PagedItems<Agent>> GetAgents(AgentFilter filter)
{
var agents = _db.GetAgents(filter);

Expand All @@ -22,8 +22,12 @@ public async Task<List<Agent>> GetAgents(AgentFilter filter)
}

agents = agents.Where(x => x.Installed).ToList();

return agents;
var pager = filter?.Pager ?? new Pagination();
return new PagedItems<Agent>
{
Items = agents.Skip(pager.Offset).Take(pager.Size),
Count = agents.Count()
};
}

#if !DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void FixMalformedResponse(FunctionCallFromLlm args)
var agents = agentService.GetAgents(new AgentFilter
{
AllowRouting = true
}).Result;
}).Result.Items.ToList();
var malformed = false;

// Sometimes it populate malformed Function in Agent name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
var pager = filter?.Pager ?? new Pagination();
var totalDirs = Directory.GetDirectories(dir);
var dirs = totalDirs.Skip(filter.Pager.Offset).Take(filter.Pager.Size).ToList();

foreach (var d in dirs)
foreach (var d in totalDirs)
{
var path = Path.Combine(d, CONVERSATION_FILE);
if (!File.Exists(path)) continue;
Expand All @@ -217,20 +217,20 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
if (record == null) continue;

var matched = true;
if (filter.Id != null) matched = matched && record.Id == filter.Id;
if (filter.AgentId != null) matched = matched && record.AgentId == filter.AgentId;
if (filter.Status != null) matched = matched && record.Status == filter.Status;
if (filter.Channel != null) matched = matched && record.Channel == filter.Channel;
if (filter.UserId != null) matched = matched && record.UserId == filter.UserId;
if (filter?.Id != null) matched = matched && record.Id == filter.Id;
if (filter?.AgentId != null) matched = matched && record.AgentId == filter.AgentId;
if (filter?.Status != null) matched = matched && record.Status == filter.Status;
if (filter?.Channel != null) matched = matched && record.Channel == filter.Channel;
if (filter?.UserId != null) matched = matched && record.UserId == filter.UserId;

if (!matched) continue;
records.Add(record);
}

return new PagedItems<Conversation>
{
Items = records.OrderByDescending(x => x.CreatedTime),
Count = totalDirs.Count(),
Items = records.OrderByDescending(x => x.CreatedTime).Skip(pager.Offset).Take(pager.Size),
Count = records.Count(),
};
}

Expand Down
14 changes: 10 additions & 4 deletions src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Agents.Models;

namespace BotSharp.OpenAPI.Controllers;

[Authorize]
Expand Down Expand Up @@ -27,11 +29,15 @@ public async Task<AgentViewModel> GetAgent([FromRoute] string id)
return AgentViewModel.FromAgent(agent);
}

[HttpGet("/agents")]
public async Task<List<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter)
[HttpPost("/agents")]
public async Task<PagedItems<AgentViewModel>> GetAgents([FromBody] AgentFilter filter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change to Post?

{
var agents = await _agentService.GetAgents(filter);
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
var pagedAgents = await _agentService.GetAgents(filter);
return new PagedItems<AgentViewModel>
{
Items = pagedAgents.Items.Select(x => AgentViewModel.FromAgent(x)).ToList(),
Count = pagedAgents.Count
};
}

[HttpPost("/agent")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)

var filterDef = builder.And(filters);
var sortDefinition = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(filter.Pager.Offset).Limit(filter.Pager.Size).ToList();
var pager = filter?.Pager ?? new Pagination();
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(pager.Offset).Limit(pager.Size).ToList();
var count = _dc.Conversations.CountDocuments(filterDef);

foreach (var conv in conversationDocs)
Expand Down