This commit is contained in:
Jicheng Lu 2025-01-30 15:58:57 -06:00
parent d34284eddf
commit 169cb95e62
8 changed files with 88 additions and 65 deletions

View file

@ -31,5 +31,5 @@ public enum AgentTaskField
Description,
Enabled,
Content,
DirectAgentId
Status
}

View file

@ -14,13 +14,13 @@
limitations under the License.
******************************************************************************/
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Tasks;
using BotSharp.Abstraction.Tasks.Models;
using BotSharp.Abstraction.Utilities;
using BotSharp.Core.Infrastructures;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;
@ -60,23 +60,22 @@ public class CrontabService : ICrontabService, ITaskFeeder
public async Task<List<AgentTask>> GetTasks()
{
var agentService = _services.GetRequiredService<IAgentService>();
var tasks = new List<AgentTask>();
var agentService = _services.GetRequiredService<IAgentService>();
var cronsources = _services.GetServices<ICrontabSource>();
// Get all agent subscribed to this cron
var agents = await agentService.GetAgents(new AgentFilter
{
Pager = new Pagination
{
Size = 1000
}
});
foreach (var source in cronsources)
{
var cron = source.GetCrontabItem();
// Get all agent subscribed to this cron
var agents = await agentService.GetAgents(new AgentFilter
{
Pager = new Pagination
{
Size = 1000
}
});
var preFilteredAgents = agents.Items.Where(x =>
x.Rules.Exists(r => r.TriggerName == cron.Title)).ToList();
@ -84,7 +83,7 @@ public class CrontabService : ICrontabService, ITaskFeeder
{
Id = Guid.Empty.ToString(),
AgentId = x.Id,
Agent = new BotSharp.Abstraction.Agents.Models.Agent
Agent = new Agent
{
Name = x.Name,
Description = x.Description

View file

@ -50,6 +50,11 @@ public partial class FileRepository
matched = matched && task.Enabled == filter.Enabled;
}
if (!string.IsNullOrEmpty(filter?.Status))
{
matched = matched && task.Status == filter.Status;
}
if (!matched) continue;
totalCount++;

View file

@ -21,11 +21,18 @@ public class AgentTaskService : IAgentTaskService
if (filter.Status == TaskStatus.Scheduled)
{
var taskFeeders = _services.GetServices<ITaskFeeder>();
var items = taskFeeders.SelectMany(x => x.GetTasks().Result);
var items = new List<AgentTask>();
foreach (var feeder in taskFeeders)
{
var tasks = await feeder.GetTasks();
items.AddRange(tasks);
}
return new PagedItems<AgentTask>
{
Items = items,
Items = items.OrderByDescending(x => x.UpdatedDateTime)
.Skip(filter.Pager.Offset).Take(filter.Pager.Size),
Count = items.Count()
};
}

View file

@ -39,11 +39,11 @@ public class AgentTaskController : ControllerBase
public async Task<PagedItems<AgentTaskViewModel>> GetAgentTasks([FromQuery] AgentTaskFilter filter)
{
filter.Status = TaskStatus.Scheduled;
var tasks = await _agentTaskService.GetTasks(filter);
var page = await _agentTaskService.GetTasks(filter);
return new PagedItems<AgentTaskViewModel>
{
Items = tasks.Items.Select(AgentTaskViewModel.From),
Count = tasks.Count
Items = page.Items.Select(AgentTaskViewModel.From),
Count = page.Count
};
}

View file

@ -15,10 +15,13 @@ public class AgentTaskViewModel
[JsonPropertyName("created_datetime")]
public DateTime CreatedDateTime { get; set; }
[JsonPropertyName("updated_datetime")]
public DateTime UpdatedDateTime { get; set; }
[JsonPropertyName("agent_id")]
public string AgentId { get; set; } = null!;
[JsonPropertyName("agent_name")]
public string AgentName { get; set; } = null!;

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Tasks.Models;
namespace BotSharp.Plugin.MongoStorage.Collections;
public class AgentTaskDocument : MongoBase
@ -7,7 +9,37 @@ public class AgentTaskDocument : MongoBase
public string Content { get; set; }
public bool Enabled { get; set; }
public string AgentId { get; set; }
public string? DirectAgentId { get; set; }
public string Status { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
public static AgentTask ToDomainModel(AgentTaskDocument model)
{
return new AgentTask
{
Id = model.Id,
Description = model.Description,
Content = model.Content,
Enabled = model.Enabled,
AgentId = model.AgentId,
Status = model.Status,
CreatedDateTime = model.CreatedTime,
UpdatedDateTime = model.UpdatedTime
};
}
public static AgentTaskDocument ToMongoModel(AgentTask model)
{
return new AgentTaskDocument
{
Id = model.Id,
Description = model.Description,
Content = model.Content,
Enabled = model.Enabled,
AgentId = model.AgentId,
Status = model.Status,
CreatedTime = model.CreatedDateTime,
UpdatedTime = model.UpdatedDateTime
};
}
}

View file

@ -22,6 +22,11 @@ public partial class MongoRepository
filters.Add(builder.Eq(x => x.AgentId, filter.AgentId));
}
if (!string.IsNullOrEmpty(filter.Status))
{
filters.Add(builder.Eq(x => x.Status, filter.Status));
}
if (filter.Enabled.HasValue)
{
filters.Add(builder.Eq(x => x.Enabled, filter.Enabled.Value));
@ -35,17 +40,11 @@ public partial class MongoRepository
var agentIds = taskDocs.Select(x => x.AgentId).Distinct().ToList();
var agents = GetAgents(new AgentFilter { AgentIds = agentIds });
var tasks = taskDocs.Select(x => new AgentTask
var tasks = taskDocs.Select(x =>
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
Enabled = x.Enabled,
AgentId = x.AgentId,
Content = x.Content,
CreatedDateTime = x.CreatedTime,
UpdatedDateTime = x.UpdatedTime,
Agent = agents.FirstOrDefault(a => a.Id == x.AgentId)
var task = AgentTaskDocument.ToDomainModel(x);
task.Agent = agents.FirstOrDefault(a => a.Id == x.AgentId);
return task;
}).ToList();
return new PagedItems<AgentTask>
@ -65,36 +64,15 @@ public partial class MongoRepository
var agentDoc = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == taskDoc.AgentId);
var agent = TransformAgentDocument(agentDoc);
var task = new AgentTask
{
Id = taskDoc.Id,
Name = taskDoc.Name,
Description = taskDoc.Description,
Enabled = taskDoc.Enabled,
AgentId = taskDoc.AgentId,
Content = taskDoc.Content,
CreatedDateTime = taskDoc.CreatedTime,
UpdatedDateTime = taskDoc.UpdatedTime,
Agent = agent
};
var task = AgentTaskDocument.ToDomainModel(taskDoc);
task.Agent = agent;
return task;
}
public void InsertAgentTask(AgentTask task)
{
var taskDoc = new AgentTaskDocument
{
Id = Guid.NewGuid().ToString(),
Name = task.Name,
Description = task.Description,
Enabled = task.Enabled,
AgentId = task.AgentId,
Content = task.Content,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
var taskDoc = AgentTaskDocument.ToMongoModel(task);
taskDoc.Id = Guid.NewGuid().ToString();
_dc.AgentTasks.InsertOne(taskDoc);
}
@ -102,16 +80,11 @@ public partial class MongoRepository
{
if (tasks.IsNullOrEmpty()) return;
var taskDocs = tasks.Select(x => new AgentTaskDocument
var taskDocs = tasks.Select(x =>
{
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid().ToString() : x.Id,
Name = x.Name,
Description = x.Description,
Enabled = x.Enabled,
AgentId = x.AgentId,
Content = x.Content,
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
var task = AgentTaskDocument.ToMongoModel(x);
task.Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString();
return task;
}).ToList();
_dc.AgentTasks.InsertMany(taskDocs);
@ -139,11 +112,15 @@ public partial class MongoRepository
case AgentTaskField.Content:
taskDoc.Content = task.Content;
break;
case AgentTaskField.Status:
taskDoc.Status = task.Status;
break;
case AgentTaskField.All:
taskDoc.Name = task.Name;
taskDoc.Description = task.Description;
taskDoc.Enabled = task.Enabled;
taskDoc.Content = task.Content;
taskDoc.Status = task.Status;
break;
}