diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 5a89f7dd..2a57193a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -19,7 +19,7 @@ public interface IBotSharpRepository #region Agent void UpdateAgent(Agent agent, AgentField field); - Agent GetAgent(string agentId); + Agent? GetAgent(string agentId); List GetAgentResponses(string agentId, string prefix, string intent); string GetAgentTemplate(string agentId, string templateName); #endregion diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 3b70849f..1a65bb2c 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -1,7 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Routing; -using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core.Agents.Services; @@ -27,27 +24,11 @@ public partial class AgentService { var profile = _db.GetAgent(id); - var instructionFile = profile?.Instruction; - if (instructionFile != null) + if (profile == null) { - profile.Instruction = instructionFile; - } - else - { - _logger.LogError($"Can't find instruction file from {instructionFile}"); - } - - var samplesFile = profile?.Samples; - if (samplesFile != null) - { - profile.Samples = samplesFile; - } - - var functionsFile = profile?.Functions; - if (functionsFile != null) - { - profile.Functions = functionsFile; - } + _logger.LogError($"Can't find agent {id}"); + return null; + }; return profile; } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index bf92c5a4..f5d19637 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -5,7 +5,6 @@ using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Agents.Models; using MongoDB.Driver; using BotSharp.Abstraction.Routing.Models; - namespace BotSharp.Core.Repository; public class FileRepository : IBotSharpRepository @@ -490,20 +489,29 @@ public class FileRepository : IBotSharpRepository return responses; } - public Agent GetAgent(string agentId) + public Agent? GetAgent(string agentId) { var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); - foreach (var dir in Directory.GetDirectories(agentDir)) + var dir = Directory.GetDirectories(agentDir).FirstOrDefault(x => x.Split(Path.DirectorySeparatorChar).Last() == agentId); + + if (!string.IsNullOrEmpty(dir)) { var json = File.ReadAllText(Path.Combine(dir, "agent.json")); + if (string.IsNullOrEmpty(json)) return null; + var record = JsonSerializer.Deserialize(json, _options); - if (record != null && record.Id == agentId) - { - var instruction = FetchInstruction(dir); - var functions = FetchFunctions(dir); - return record.SetInstruction(instruction).SetFunctions(functions); - } + if (record == null) return null; + + var instruction = FetchInstruction(dir); + var functions = FetchFunctions(dir); + var templates = FetchTemplates(dir); + var responses = FetchResponses(dir); + return record.SetInstruction(instruction) + .SetFunctions(functions) + .SetTemplates(templates) + .SetResponses(responses); } + return null; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index a20deb20..c79bef48 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Routing; using BotSharp.OpenAPI.ViewModels.Agents; namespace BotSharp.OpenAPI.Controllers; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs index c2144870..6a473078 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs @@ -5,6 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Models; public class RoutingRuleMongoElement { public string Field { get; set; } + public string Description { get; set; } public bool Required { get; set; } public Guid? RedirectTo { get; set; } @@ -18,6 +19,7 @@ public class RoutingRuleMongoElement return new RoutingRuleMongoElement { Field = routingRule.Field, + Description = routingRule.Description, Required = routingRule.Required, RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null }; @@ -30,6 +32,7 @@ public class RoutingRuleMongoElement AgentId = agentId, AgentName = agentName, Field = rule.Field, + Description = rule.Description, Required = rule.Required, RedirectTo = rule.RedirectTo?.ToString() }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index ef7afb03..f9cbd047 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -517,7 +517,7 @@ public class MongoRepository : IBotSharpRepository - public Agent GetAgent(string agentId) + public Agent? GetAgent(string agentId) { var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId); return foundAgent;