diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index 76019e47c..5f3b1b876 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -10,7 +10,7 @@ public interface IAgentService { Task CreateAgent(Agent agent); Task RefreshAgents(); - Task> GetAgents(AgentFilter filter); + Task> GetAgents(AgentFilter filter); /// /// Load agent configurations and trigger hooks @@ -29,7 +29,7 @@ public interface IAgentService /// /// Original agent information Task GetAgent(string id); - + Task DeleteAgent(string id); Task UpdateAgent(Agent agent, AgentField updateField); Task UpdateAgentFromFile(string id); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs index 1b4e232bb..860a7dc90 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs @@ -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; } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index aba053a02..486c756b7 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -9,7 +9,7 @@ public partial class AgentService #if !DEBUG [MemoryCache(10 * 60)] #endif - public async Task> GetAgents(AgentFilter filter) + public async Task> GetAgents(AgentFilter filter) { var agents = _db.GetAgents(filter); @@ -22,8 +22,12 @@ public async Task> GetAgents(AgentFilter filter) } agents = agents.Where(x => x.Installed).ToList(); - - return agents; + var pager = filter?.Pager ?? new Pagination(); + return new PagedItems + { + Items = agents.Skip(pager.Offset).Take(pager.Size), + Count = agents.Count() + }; } #if !DEBUG diff --git a/src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs b/src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs index 9ad951212..6db3a40ab 100644 --- a/src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Planning/NaivePlanner.cs @@ -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 diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index d9e276e52..76122be68 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -204,10 +204,10 @@ public PagedItems GetConversations(ConversationFilter filter) { var records = new List(); 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; @@ -217,20 +217,20 @@ public PagedItems 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 { - Items = records.OrderByDescending(x => x.CreatedTime), - Count = totalDirs.Count(), + Items = records.OrderByDescending(x => x.CreatedTime).Skip(pager.Offset).Take(pager.Size), + Count = records.Count(), }; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 9259cb542..eccf8316c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Agents.Models; + namespace BotSharp.OpenAPI.Controllers; [Authorize] @@ -27,11 +29,15 @@ public async Task GetAgent([FromRoute] string id) return AgentViewModel.FromAgent(agent); } - [HttpGet("/agents")] - public async Task> GetAgents([FromQuery] AgentFilter filter) + [HttpPost("/agents")] + public async Task> GetAgents([FromBody] AgentFilter filter) { - var agents = await _agentService.GetAgents(filter); - return agents.Select(x => AgentViewModel.FromAgent(x)).ToList(); + var pagedAgents = await _agentService.GetAgents(filter); + return new PagedItems + { + Items = pagedAgents.Items.Select(x => AgentViewModel.FromAgent(x)).ToList(), + Count = pagedAgents.Count + }; } [HttpPost("/agent")] diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 84ccce6dc..18fe98d63 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -217,7 +217,8 @@ public PagedItems GetConversations(ConversationFilter filter) var filterDef = builder.And(filters); var sortDefinition = Builders.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)