depend on default repository

This commit is contained in:
Jicheng Lu 2023-09-03 14:32:12 -05:00
parent be5f9f247a
commit c897209fda
9 changed files with 72 additions and 10 deletions

View file

@ -25,5 +25,6 @@ public interface IBotSharpRepository
void DeleteRoutingItems();
void DeleteRoutingProfiles();
AgentRecord GetAgent(string agentId);
List<string> GetAgentResponses(string agentId);
}

View file

@ -15,6 +15,8 @@ public class AgentRecord : RecordBase
public List<string> Responses { get; set; }
public string Samples { get; set; }
[Required]
public DateTime CreatedTime { get; set; }

View file

@ -20,11 +20,12 @@ public partial class AgentService
public async Task<Agent> GetAgent(string id)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
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;

View file

@ -68,7 +68,10 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<Simulator>();
//services.AddScoped<IBotSharpRepository, FileRepository>();
if (myDatabaseSettings.Default == "FileRepository")
{
services.AddScoped<IBotSharpRepository, FileRepository>();
}
return services;
}

View file

@ -101,4 +101,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
public AgentRecord GetAgent(string agentId)
{
throw new NotImplementedException();
}
}

View file

@ -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<AgentRecord>(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<string> FetchFunctions(string fileDir)
{
var file = Path.Combine(fileDir, "functions.json");
if (!File.Exists(file)) return new List<string>();
var functionsJson = File.ReadAllText(file);
var functionDefs = JsonSerializer.Deserialize<List<FunctionDef>>(functionsJson, _options);
var functions = functionDefs.Select(x => JsonSerializer.Serialize(x, _options)).ToList();
return functions;
}
}

View file

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.7.23375.6" />
<PackageReference Include="MongoDB.Driver" Version="2.19.2" />
</ItemGroup>

View file

@ -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<BotSharpDatabaseSettings>();
if (dbSettings.Default == "MongoRepository")
{
var dbSettings = x.GetRequiredService<BotSharpDatabaseSettings>();
return new MongoDbContext(dbSettings.MongoDb);
});
services.AddScoped((IServiceProvider x) =>
{
var dbSettings = x.GetRequiredService<BotSharpDatabaseSettings>();
return new MongoDbContext(dbSettings.MongoDb);
});
services.AddScoped<IBotSharpRepository, MongoRepository>();
services.AddScoped<IBotSharpRepository, MongoRepository>();
}
}
}

View file

@ -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;
}
}