Merge pull request #142 from iceljc/features/refine-mongo-migration
Features/refine mongo migration
This commit is contained in:
commit
d911b8de3f
|
|
@ -18,6 +18,7 @@ public interface IAgentService
|
|||
Task<Agent> GetAgent(string id);
|
||||
Task<bool> DeleteAgent(string id);
|
||||
Task UpdateAgent(Agent agent);
|
||||
Task UpdateAgentFromFile(string id);
|
||||
string GetDataDir();
|
||||
string GetAgentDataDir(string agentId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public partial class AgentService
|
|||
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
|
||||
var foundAgent = FetchAgentInfoFromFile(agent.Name, filePath);
|
||||
var foundAgent = FetchAgentFileByName(agent.Name, filePath);
|
||||
|
||||
if (foundAgent != null)
|
||||
{
|
||||
|
|
@ -62,14 +62,7 @@ public partial class AgentService
|
|||
return agentRecord;
|
||||
}
|
||||
|
||||
private JsonSerializerOptions _options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
private Agent FetchAgentInfoFromFile(string agentName, string filePath)
|
||||
private Agent FetchAgentFileByName(string agentName, string filePath)
|
||||
{
|
||||
foreach (var dir in Directory.GetDirectories(filePath))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ public partial class AgentService
|
|||
if (!string.IsNullOrEmpty(agent.Instruction))
|
||||
record.Instruction = agent.Instruction;
|
||||
|
||||
if (!agent.Templates.IsNullOrEmpty())
|
||||
record.Templates = agent.Templates;
|
||||
|
||||
if (!agent.Functions.IsNullOrEmpty())
|
||||
record.Functions = agent.Functions;
|
||||
|
||||
|
|
@ -36,4 +39,57 @@ public partial class AgentService
|
|||
db.UpdateAgent(record);
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task UpdateAgentFromFile(string id)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
if (agent == null) return;
|
||||
|
||||
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
|
||||
|
||||
var clonedAgent = Agent.Clone(agent);
|
||||
var foundAgent = FetchAgentFileById(agent.Id, filePath);
|
||||
if (foundAgent != null)
|
||||
{
|
||||
clonedAgent.SetId(foundAgent.Id)
|
||||
.SetName(foundAgent.Name)
|
||||
.SetDescription(foundAgent.Description)
|
||||
.SetIsPublic(foundAgent.IsPublic)
|
||||
.SetInstruction(foundAgent.Instruction)
|
||||
.SetTemplates(foundAgent.Templates)
|
||||
.SetFunctions(foundAgent.Functions)
|
||||
.SetResponses(foundAgent.Responses);
|
||||
|
||||
db.UpdateAgent(clonedAgent);
|
||||
}
|
||||
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Agent FetchAgentFileById(string agentId, string filePath)
|
||||
{
|
||||
foreach (var dir in Directory.GetDirectories(filePath))
|
||||
{
|
||||
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
|
||||
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
|
||||
if (agent != null && agent.Id == agentId)
|
||||
{
|
||||
var functions = FetchFunctionsFromFile(dir);
|
||||
var instruction = FetchInstructionFromFile(dir);
|
||||
var responses = FetchResponsesFromFile(dir);
|
||||
var templates = FetchTemplatesFromFile(dir);
|
||||
return agent.SetInstruction(instruction)
|
||||
.SetTemplates(templates)
|
||||
.SetFunctions(functions)
|
||||
.SetResponses(responses);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ public partial class AgentService : IAgentService
|
|||
private readonly ILogger _logger;
|
||||
private readonly IUserIdentity _user;
|
||||
private readonly AgentSettings _settings;
|
||||
private readonly JsonSerializerOptions _options;
|
||||
|
||||
public AgentService(IServiceProvider services,
|
||||
ILogger<AgentService> logger,
|
||||
|
|
@ -20,6 +21,12 @@ public partial class AgentService : IAgentService
|
|||
_logger = logger;
|
||||
_user = user;
|
||||
_settings = settings;
|
||||
_options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
}
|
||||
|
||||
public string GetDataDir()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Routing.Services;
|
||||
|
||||
public partial class RoutingService
|
||||
{
|
||||
public async Task<List<RoutingItem>> CreateRoutingItems(List<RoutingItem> routingItems)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var items = FetchRoutingItemsFromFile();
|
||||
|
||||
if (items.IsNullOrEmpty())
|
||||
{
|
||||
items = routingItems?.ToList() ?? new List<RoutingItem>();
|
||||
}
|
||||
|
||||
var savedItems = db.CreateRoutingItems(items);
|
||||
return await Task.FromResult(savedItems.ToList());
|
||||
}
|
||||
|
||||
public async Task<List<RoutingProfile>> CreateRoutingProfiles(List<RoutingProfile> routingProfiles)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var profiles = FetchRoutingProfilesFromFile();
|
||||
|
||||
if (profiles.IsNullOrEmpty())
|
||||
{
|
||||
profiles = routingProfiles?.ToList() ?? new List<RoutingProfile>();
|
||||
}
|
||||
|
||||
var savedProfiles = db.CreateRoutingProfiles(profiles);
|
||||
return await Task.FromResult(savedProfiles.ToList());
|
||||
}
|
||||
|
||||
private List<RoutingItem> FetchRoutingItemsFromFile()
|
||||
{
|
||||
var routingItems = new List<RoutingItem>();
|
||||
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
routingItems = JsonSerializer.Deserialize<List<RoutingItem>>(File.ReadAllText(filePath), _options);
|
||||
}
|
||||
|
||||
return routingItems ?? new List<RoutingItem>();
|
||||
}
|
||||
|
||||
private List<RoutingProfile> FetchRoutingProfilesFromFile()
|
||||
{
|
||||
var routingProfiles = new List<RoutingProfile>();
|
||||
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath), _options);
|
||||
}
|
||||
|
||||
return routingProfiles ?? new List<RoutingProfile>();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Core.Routing.Services;
|
||||
|
||||
public partial class RoutingService
|
||||
{
|
||||
public async Task DeleteRoutingItems()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingItems();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DeleteRoutingProfiles()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingProfiles();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,45 +1,23 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Repositories.Records;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Routing.Services;
|
||||
|
||||
public class RoutingService : IRoutingService
|
||||
public partial class RoutingService : IRoutingService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly JsonSerializerOptions _options;
|
||||
|
||||
public RoutingService(IServiceProvider service)
|
||||
{
|
||||
_services = service;
|
||||
}
|
||||
public async Task<List<RoutingItem>> CreateRoutingItems(List<RoutingItem> routingItems)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var items = routingItems?.ToList() ?? new List<RoutingItem>();
|
||||
var savedItems = db.CreateRoutingItems(items);
|
||||
return await Task.FromResult(savedItems.ToList());
|
||||
}
|
||||
|
||||
public async Task<List<RoutingProfile>> CreateRoutingProfiles(List<RoutingProfile> routingProfiles)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var profiles = routingProfiles?.ToList() ?? new List<RoutingProfile>();
|
||||
var savedProfiles = db.CreateRoutingProfiles(profiles);
|
||||
return await Task.FromResult(savedProfiles.ToList());
|
||||
}
|
||||
|
||||
public async Task DeleteRoutingItems()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingItems();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DeleteRoutingProfiles()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingProfiles();
|
||||
await Task.CompletedTask;
|
||||
_options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,12 @@ public class AgentController : ControllerBase, IApiAdapter
|
|||
await _agentService.UpdateAgent(model);
|
||||
}
|
||||
|
||||
[HttpPut("/agent/file/{agentId}")]
|
||||
public async Task UpdateAgentFromFile([FromRoute] string agentId)
|
||||
{
|
||||
await _agentService.UpdateAgentFromFile(agentId);
|
||||
}
|
||||
|
||||
[HttpGet("/agents")]
|
||||
public async Task<List<AgentViewModel>> GetAgents()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -379,6 +379,7 @@ public class MongoRepository : IBotSharpRepository
|
|||
Name = agent.Name,
|
||||
Description = agent.Description,
|
||||
Instruction = agent.Instruction,
|
||||
Templates = agent.Templates,
|
||||
Functions = agent.Functions,
|
||||
Responses = agent.Responses,
|
||||
IsPublic = agent.IsPublic,
|
||||
|
|
@ -391,12 +392,13 @@ public class MongoRepository : IBotSharpRepository
|
|||
.Set(x => x.Name, agent.Name)
|
||||
.Set(x => x.Description, agent.Description)
|
||||
.Set(x => x.Instruction, agent.Instruction)
|
||||
.Set(x => x.Templates, agent.Templates)
|
||||
.Set(x => x.Functions, agent.Functions)
|
||||
.Set(x => x.Responses, agent.Responses)
|
||||
.Set(x => x.IsPublic, agent.IsPublic)
|
||||
.Set(x => x.UpdatedTime, agent.UpdatedDateTime);
|
||||
|
||||
_dc.Agents.UpdateOne(filter, update, _options);
|
||||
_dc.Agents.UpdateOne(filter, update);
|
||||
}
|
||||
|
||||
public void DeleteRoutingItems()
|
||||
|
|
|
|||
Loading…
Reference in a new issue