diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 709958db..77d37d30 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -39,8 +39,10 @@ public interface IBotSharpRepository PagedItems GetAgentTasks(AgentTaskFilter filter); AgentTask? GetAgentTask(string agentId, string taskId); void InsertAgentTask(AgentTask task); + void BulkInsertAgentTasks(List tasks); void UpdateAgentTask(AgentTask task, AgentTaskField field); bool DeleteAgentTask(string agentId, string taskId); + bool DeleteAgentTasks(); #endregion #region Conversation diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 01ca0c03..2f877234 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -1,7 +1,10 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Tasks.Models; +using BotSharp.Abstraction.Users.Models; using System.IO; +using System.Text.RegularExpressions; namespace BotSharp.Core.Agents.Services; @@ -156,4 +159,69 @@ public partial class AgentService var samples = File.ReadAllLines(file); return samples?.ToList() ?? new List(); } + + private List FetchTasksFromFile(string fileDir) + { + var tasks = new List(); + var taskDir = Path.Combine(fileDir, "tasks"); + if (!Directory.Exists(taskDir)) return tasks; + + var agentId = fileDir.Split(Path.DirectorySeparatorChar).Last(); + foreach (var file in Directory.GetFiles(taskDir)) + { + var parsedTask = ParseAgentTask(file); + if (parsedTask == null) continue; + + var task = new AgentTask + { + Id = parsedTask.Id, + Name = parsedTask.Name, + Description = parsedTask.Description, + Enabled = parsedTask.Enabled, + DirectAgentId = parsedTask.DirectAgentId, + Content = parsedTask.Content, + AgentId = agentId + }; + tasks.Add(task); + } + + return tasks; + } + + private AgentTask? ParseAgentTask(string taskFile) + { + if (string.IsNullOrWhiteSpace(taskFile)) return null; + + var prefix = @"#metadata"; + var suffix = @"/metadata"; + var fileName = taskFile.Split(Path.DirectorySeparatorChar).Last(); + var id = fileName.Split('.').First(); + var data = File.ReadAllText(taskFile); + var pattern = $@"{prefix}.+{suffix}"; + var metaData = Regex.Match(data, pattern, RegexOptions.Singleline); + + if (!metaData.Success) return null; + + var task = metaData.Value.JsonContent(); + if (task == null) return null; + + task.Id = id; + pattern = $@"{suffix}.+"; + var content = Regex.Match(data, pattern, RegexOptions.Singleline).Value; + task.Content = content.Substring(suffix.Length).Trim(); + return task; + } + + private UserAgent BuildUserAgent(string agentId, string userId, bool editable = false) + { + return new UserAgent + { + Id = Guid.NewGuid().ToString(), + UserId = userId, + AgentId = agentId, + Editable = editable, + CreatedTime = DateTime.UtcNow, + UpdatedTime = DateTime.UtcNow + }; + } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index 43757571..2831a669 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -1,6 +1,8 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Tasks.Models; using Microsoft.Extensions.Caching.Memory; +using System.Collections.Generic; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -9,8 +11,9 @@ public partial class AgentService { public async Task RefreshAgents() { - var isDeleted = _db.DeleteAgents(); - if (!isDeleted) return; + var isAgentDeleted = _db.DeleteAgents(); + var isTaskDeleted = _db.DeleteAgentTasks(); + if (!isAgentDeleted) return; var dbSettings = _services.GetRequiredService(); var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @@ -20,6 +23,7 @@ public partial class AgentService var user = _db.GetUserById(_user.Id); var agents = new List(); var userAgents = new List(); + var agentTasks = new List(); foreach (var dir in Directory.GetDirectories(agentDir)) { @@ -37,23 +41,18 @@ public partial class AgentService .SetFunctions(functions) .SetResponses(responses) .SetSamples(samples); - - var userAgent = new UserAgent - { - Id = Guid.NewGuid().ToString(), - UserId = user.Id, - AgentId = agent.Id, - Editable = false, - CreatedTime = DateTime.UtcNow, - UpdatedTime = DateTime.UtcNow - }; - agents.Add(agent); + + var userAgent = BuildUserAgent(agent.Id, user.Id); userAgents.Add(userAgent); + + var tasks = FetchTasksFromFile(dir); + agentTasks.AddRange(tasks); } _db.BulkInsertAgents(agents); _db.BulkInsertUserAgents(userAgents); + _db.BulkInsertAgentTasks(agentTasks); Utilities.ClearCache(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index de71e1d1..4223aedc 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -136,6 +136,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } + public void BulkInsertAgentTasks(List tasks) + { + throw new NotImplementedException(); + } + public void UpdateAgentTask(AgentTask task, AgentTaskField field) { throw new NotImplementedException(); @@ -145,6 +150,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository { throw new NotImplementedException(); } + + public bool DeleteAgentTasks() + { + throw new NotImplementedException(); + } #endregion #region Conversation diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs index aa1d798e..73a8e3ed 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs @@ -130,6 +130,11 @@ public partial class FileRepository File.WriteAllText(taskFile, fileContent); } + public void BulkInsertAgentTasks(List tasks) + { + + } + public void UpdateAgentTask(AgentTask task, AgentTaskField field) { if (task == null || string.IsNullOrEmpty(task.Id)) return; @@ -202,6 +207,11 @@ public partial class FileRepository return true; } + public bool DeleteAgentTasks() + { + return false; + } + private string? FindTaskFileById(string taskDir, string taskId) { if (!Directory.Exists(taskDir) || string.IsNullOrEmpty(taskId)) return null; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs index 01e64e3e..4b696665 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs @@ -97,6 +97,26 @@ public partial class MongoRepository _dc.AgentTasks.InsertOne(taskDoc); } + public void BulkInsertAgentTasks(List tasks) + { + if (tasks.IsNullOrEmpty()) return; + + var taskDocs = tasks.Select(x => new AgentTaskDocument + { + Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid().ToString() : x.Id, + Name = x.Name, + Description = x.Description, + Enabled = x.Enabled, + AgentId = x.AgentId, + DirectAgentId = x.DirectAgentId, + Content = x.Content, + CreatedTime = DateTime.UtcNow, + UpdatedTime = DateTime.UtcNow + }).ToList(); + + _dc.AgentTasks.InsertMany(taskDocs); + } + public void UpdateAgentTask(AgentTask task, AgentTaskField field) { if (task == null || string.IsNullOrEmpty(task.Id)) return; @@ -143,5 +163,18 @@ public partial class MongoRepository var taskDeleted = _dc.AgentTasks.DeleteOne(filter); return taskDeleted.DeletedCount > 0; } + + public bool DeleteAgentTasks() + { + try + { + _dc.AgentTasks.DeleteMany(Builders.Filter.Empty); + return true; + } + catch + { + return false; + } + } #endregion }