From c897209fda4866c0e209d661657d52e75045222f Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sun, 3 Sep 2023 14:32:12 -0500 Subject: [PATCH] depend on default repository --- .../Repositories/IBotSharpRepository.cs | 1 + .../Repositories/Records/AgentRecord.cs | 2 + .../Agents/Services/AgentService.GetAgents.cs | 9 +++-- .../BotSharpServiceCollectionExtensions.cs | 5 ++- .../Repository/BotSharpDbContext.cs | 5 +++ .../Repository/FileRepository.cs | 37 +++++++++++++++++++ .../BotSharp.Plugin.MongoStorage.csproj | 1 + .../MongoStoragePlugin.cs | 16 +++++--- .../Repository/MongoRepository.cs | 6 +++ 9 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index f8610c03..46a0023c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -25,5 +25,6 @@ public interface IBotSharpRepository void DeleteRoutingItems(); void DeleteRoutingProfiles(); + AgentRecord GetAgent(string agentId); List GetAgentResponses(string agentId); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs index 8c3b0268..224492c9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/AgentRecord.cs @@ -15,6 +15,8 @@ public class AgentRecord : RecordBase public List Responses { get; set; } + public string Samples { get; set; } + [Required] public DateTime CreatedTime { 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 2313da0c..73145edc 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -20,11 +20,12 @@ public partial class AgentService public async Task GetAgent(string id) { var db = _services.GetRequiredService(); - var query = from agent in db.Agent - where agent.Id == id - select agent.ToAgent(); + //var query = from agent in db.Agent + // where agent.Id == id + // select agent.ToAgent(); - var profile = query.FirstOrDefault(); + //var profile = query.FirstOrDefault(); + var profile = db.GetAgent(id)?.ToAgent(); //var dir = GetAgentDataDir(id); var instructionFile = profile?.Instruction; diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 00d6ec67..7b22c5d9 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -68,7 +68,10 @@ public static class BotSharpServiceCollectionExtensions services.AddScoped(); - //services.AddScoped(); + if (myDatabaseSettings.Default == "FileRepository") + { + services.AddScoped(); + } return services; } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 445cf555..5ba6f80d 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -101,4 +101,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository { throw new NotImplementedException(); } + + public AgentRecord GetAgent(string agentId) + { + throw new NotImplementedException(); + } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 182f7c28..be59916b 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -364,4 +364,41 @@ public class FileRepository : IBotSharpRepository return responses; } + + public AgentRecord GetAgent(string agentId) + { + var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); + foreach (var dir in Directory.GetDirectories(agentDir)) + { + var json = File.ReadAllText(Path.Combine(dir, "agent.json")); + 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); + } + } + return null; + } + + private string FetchInstruction(string fileDir) + { + var file = Path.Combine(fileDir, "instruction.liquid"); + if (!File.Exists(file)) return null; + + var instruction = File.ReadAllText(file); + return instruction; + } + + private List FetchFunctions(string fileDir) + { + var file = Path.Combine(fileDir, "functions.json"); + if (!File.Exists(file)) return new List(); + + var functionsJson = File.ReadAllText(file); + var functionDefs = JsonSerializer.Deserialize>(functionsJson, _options); + var functions = functionDefs.Select(x => JsonSerializer.Serialize(x, _options)).ToList(); + return functions; + } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index f4e8050c..d376992e 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs index af6e5d06..f4682f48 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs @@ -6,12 +6,18 @@ public class MongoStoragePlugin : IBotSharpPlugin { public void RegisterDI(IServiceCollection services, IConfiguration config) { - services.AddScoped((IServiceProvider x) => + var sp = services.BuildServiceProvider(); + var dbSettings = sp.GetRequiredService(); + + if (dbSettings.Default == "MongoRepository") { - var dbSettings = x.GetRequiredService(); - return new MongoDbContext(dbSettings.MongoDb); - }); + services.AddScoped((IServiceProvider x) => + { + var dbSettings = x.GetRequiredService(); + return new MongoDbContext(dbSettings.MongoDb); + }); - services.AddScoped(); + services.AddScoped(); + } } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 06590035..9b78d71a 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -438,4 +438,10 @@ public class MongoRepository : IBotSharpRepository return agent.Responses; } + + public AgentRecord GetAgent(string agentId) + { + var foundAgent = Agent.FirstOrDefault(x => x.Id == agentId); + return foundAgent; + } }