2023-06-23 04:43:00 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-08-26 04:41:01 +00:00
|
|
|
using BotSharp.Abstraction.Repositories;
|
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-08-10 04:53:22 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-07-25 02:22:18 +00:00
|
|
|
|
2023-09-07 22:04:34 +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)
|
|
|
|
|
.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-09-07 22:04:34 +00:00
|
|
|
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
|
|
|
|
|
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-07-21 21:56:14 +00:00
|
|
|
db.Transaction<IBotSharpTable>(delegate
|
2023-06-23 04:43:00 +00:00
|
|
|
{
|
2023-09-07 22:04:34 +00:00
|
|
|
db.Add<IBotSharpTable>(agentRecord);
|
2023-07-25 02:22:18 +00:00
|
|
|
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 == agentName)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var file = Path.Combine(fileDir, "instruction.liquid");
|
|
|
|
|
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>();
|
|
|
|
|
foreach (var file in Directory.GetFiles(fileDir))
|
|
|
|
|
{
|
|
|
|
|
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
|
|
|
|
var splits = fileName.ToLower().Split('.');
|
|
|
|
|
var name = splits[0];
|
|
|
|
|
var extension = splits[1];
|
|
|
|
|
if (name != "instruction" && extension == "liquid")
|
|
|
|
|
{
|
|
|
|
|
var content = File.ReadAllText(file);
|
|
|
|
|
templates.Add(new AgentTemplate(name, content));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return templates;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
private List<string> FetchFunctionsFromFile(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<Abstraction.Functions.Models.FunctionDef>>(functionsJson, _options);
|
|
|
|
|
var functions = functionDefs.Select(x => JsonSerializer.Serialize(x, _options)).ToList();
|
|
|
|
|
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
|
|
|
}
|