BotSharp/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

150 lines
5.5 KiB
C#
Raw Normal View History

2023-06-23 04:43:00 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-09-27 20:49:44 +00:00
using BotSharp.Abstraction.Functions.Models;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Utilities;
2023-09-02 05:10:46 +00:00
using System.IO;
2023-06-23 04:43:00 +00:00
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
2023-09-15 20:19:44 +00:00
var agentRecord = (from a in _db.Agents
join ua in _db.UserAgents on a.Id equals ua.AgentId
join u in _db.Users on ua.UserId equals u.Id
2023-09-02 05:10:46 +00:00
where u.ExternalId == _user.Id && a.Name == agent.Name
2023-07-25 02:22:18 +00:00
select a).FirstOrDefault();
2023-09-07 22:04:34 +00:00
if (agentRecord != null)
2023-06-23 04:43:00 +00:00
{
2023-09-07 22:04:34 +00:00
return agentRecord;
2023-06-23 04:43:00 +00:00
}
2023-09-07 22:04:34 +00:00
agentRecord = Agent.Clone(agent);
agentRecord.Id = Guid.NewGuid().ToString();
2023-09-08 17:17:54 +00:00
agentRecord.CreatedDateTime = DateTime.UtcNow;
agentRecord.UpdatedDateTime = DateTime.UtcNow;
2023-06-23 04:43:00 +00:00
2023-09-02 05:10:46 +00:00
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
2023-09-13 22:34:19 +00:00
var foundAgent = FetchAgentFileByName(agent.Name, filePath);
2023-09-02 05:10:46 +00:00
if (foundAgent != null)
{
2023-09-07 22:04:34 +00:00
agentRecord.SetId(foundAgent.Id)
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
2023-09-18 23:14:24 +00:00
.SetDisabled(foundAgent.Disabled)
.SetAllowRouting(foundAgent.AllowRouting)
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)
2023-09-07 22:04:34 +00:00
.SetInstruction(foundAgent.Instruction)
2023-09-12 22:08:58 +00:00
.SetTemplates(foundAgent.Templates)
2023-09-07 22:04:34 +00:00
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
2023-09-02 05:10:46 +00:00
}
2023-10-09 00:05:17 +00:00
var user = _db.Users.FirstOrDefault(x => x.Id == _user.Id || x.ExternalId == _user.Id);
2023-09-07 22:04:34 +00:00
var userAgentRecord = new UserAgent
2023-07-25 02:22:18 +00:00
{
2023-09-02 05:10:46 +00:00
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
2023-09-07 22:04:34 +00:00
AgentId = foundAgent?.Id ?? agentRecord.Id,
2023-07-25 02:22:18 +00:00
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
2023-09-15 20:19:44 +00:00
_db.Transaction<IBotSharpTable>(delegate
2023-06-23 04:43:00 +00:00
{
2023-09-15 20:19:44 +00:00
_db.Add<IBotSharpTable>(agentRecord);
_db.Add<IBotSharpTable>(userAgentRecord);
2023-06-23 04:43:00 +00:00
});
2023-09-07 22:04:34 +00:00
return agentRecord;
2023-06-23 04:43:00 +00:00
}
2023-09-02 05:10:46 +00:00
2023-09-13 22:34:19 +00:00
private Agent FetchAgentFileByName(string agentName, string filePath)
2023-09-02 05:10:46 +00:00
{
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.Name.IsEqualTo(agentName))
2023-09-02 05:10:46 +00:00
{
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
2023-09-12 22:08:58 +00:00
var templates = FetchTemplatesFromFile(dir);
2023-09-07 22:04:34 +00:00
return agent.SetInstruction(instruction)
2023-09-12 22:08:58 +00:00
.SetTemplates(templates)
2023-09-07 22:04:34 +00:00
.SetFunctions(functions)
.SetResponses(responses);
2023-09-02 05:10:46 +00:00
}
}
return null;
}
private string FetchInstructionFromFile(string fileDir)
{
2023-09-15 22:08:59 +00:00
var file = Path.Combine(fileDir, $"instruction.{_agentSettings.TemplateFormat}");
2023-09-02 05:10:46 +00:00
if (!File.Exists(file)) return null;
var instruction = File.ReadAllText(file);
return instruction;
}
2023-09-12 22:08:58 +00:00
private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
{
var templates = new List<AgentTemplate>();
var templateDir = Path.Combine(fileDir, "templates");
if (!Directory.Exists(templateDir)) return templates;
foreach (var file in Directory.GetFiles(templateDir))
2023-09-12 22:08:58 +00:00
{
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
var splits = fileName.ToLower().Split('.');
var name = splits[0];
var extension = splits[1];
if (extension.IsEqualTo(_agentSettings.TemplateFormat))
2023-09-12 22:08:58 +00:00
{
var content = File.ReadAllText(file);
templates.Add(new AgentTemplate(name, content));
}
}
return templates;
}
2023-09-27 20:49:44 +00:00
private List<FunctionDef> FetchFunctionsFromFile(string fileDir)
2023-09-02 05:10:46 +00:00
{
var file = Path.Combine(fileDir, "functions.json");
2023-09-27 20:49:44 +00:00
if (!File.Exists(file)) return new List<FunctionDef>();
2023-09-02 05:10:46 +00:00
var functionsJson = File.ReadAllText(file);
2023-09-27 20:49:44 +00:00
var functions = JsonSerializer.Deserialize<List<FunctionDef>>(functionsJson, _options);
2023-09-02 05:10:46 +00:00
return functions;
}
2023-09-09 21:44:25 +00:00
private List<AgentResponse> FetchResponsesFromFile(string fileDir)
2023-09-02 05:10:46 +00:00
{
2023-09-09 21:44:25 +00:00
var responses = new List<AgentResponse>();
2023-09-02 05:10:46 +00:00
var responseDir = Path.Combine(fileDir, "responses");
if (!Directory.Exists(responseDir)) return responses;
foreach (var file in Directory.GetFiles(responseDir))
{
2023-09-09 21:44:25 +00:00
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
var splits = fileName.Split('.');
var prefix = splits[0];
var intent = splits[1];
var content = File.ReadAllText(file);
responses.Add(new AgentResponse(prefix, intent, content));
2023-09-02 05:10:46 +00:00
}
return responses;
}
2023-06-23 04:43:00 +00:00
}