2024-02-05 20:53:18 +00:00
|
|
|
using BotSharp.Abstraction.Tasks.Models;
|
2023-09-02 05:10:46 +00:00
|
|
|
using System.IO;
|
2024-02-05 20:53:18 +00:00
|
|
|
using System.Text.RegularExpressions;
|
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-10-19 22:27:51 +00:00
|
|
|
var agentRecord = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Name.IsEqualTo(agent.Name));
|
2023-07-25 02:22:18 +00:00
|
|
|
|
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)
|
2024-01-26 04:32:48 +00:00
|
|
|
.SetAgentType(foundAgent.Type)
|
2023-09-18 23:14:24 +00:00
|
|
|
.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)
|
2023-12-13 20:26:46 +00:00
|
|
|
.SetResponses(foundAgent.Responses)
|
|
|
|
|
.SetLlmConfig(foundAgent.LlmConfig);
|
2023-09-02 05:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-14 01:25:25 +00:00
|
|
|
var user = _db.GetUserById(_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-11-14 05:37:04 +00:00
|
|
|
Editable = false,
|
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-12-27 15:33:03 +00:00
|
|
|
Utilities.ClearCache();
|
|
|
|
|
|
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);
|
2023-10-16 04:12:17 +00:00
|
|
|
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-10-19 22:27:51 +00:00
|
|
|
var samples = FetchSamplesFromFile(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)
|
2023-10-19 22:27:51 +00:00
|
|
|
.SetResponses(responses)
|
|
|
|
|
.SetSamples(samples);
|
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>();
|
2023-10-16 04:12:17 +00:00
|
|
|
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();
|
2024-01-29 21:02:27 +00:00
|
|
|
var splitIdx = fileName.LastIndexOf(".");
|
|
|
|
|
var name = fileName.Substring(0, splitIdx);
|
|
|
|
|
var extension = fileName.Substring(splitIdx + 1);
|
2023-10-16 04:12:17 +00:00
|
|
|
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-10-19 22:27:51 +00:00
|
|
|
|
|
|
|
|
private List<string> FetchSamplesFromFile(string fileDir)
|
|
|
|
|
{
|
|
|
|
|
var file = Path.Combine(fileDir, "samples.txt");
|
|
|
|
|
if (!File.Exists(file)) return new List<string>();
|
|
|
|
|
|
|
|
|
|
var samples = File.ReadAllLines(file);
|
|
|
|
|
return samples?.ToList() ?? new List<string>();
|
|
|
|
|
}
|
2024-02-05 20:53:18 +00:00
|
|
|
|
|
|
|
|
private List<AgentTask> FetchTasksFromFile(string fileDir)
|
|
|
|
|
{
|
|
|
|
|
var tasks = new List<AgentTask>();
|
|
|
|
|
var taskDir = Path.Combine(fileDir, "tasks");
|
|
|
|
|
if (!Directory.Exists(taskDir)) return tasks;
|
|
|
|
|
|
|
|
|
|
var agentId = fileDir.Split(Path.DirectorySeparatorChar).Last();
|
|
|
|
|
foreach (var file in Directory.GetFiles(taskDir))
|
|
|
|
|
{
|
|
|
|
|
var parsedTask = ParseAgentTask(file);
|
|
|
|
|
if (parsedTask == null) continue;
|
|
|
|
|
|
|
|
|
|
var task = new AgentTask
|
|
|
|
|
{
|
|
|
|
|
Id = parsedTask.Id,
|
|
|
|
|
Name = parsedTask.Name,
|
|
|
|
|
Description = parsedTask.Description,
|
|
|
|
|
Enabled = parsedTask.Enabled,
|
|
|
|
|
DirectAgentId = parsedTask.DirectAgentId,
|
|
|
|
|
Content = parsedTask.Content,
|
2024-02-05 20:55:24 +00:00
|
|
|
AgentId = agentId,
|
|
|
|
|
CreatedDateTime = parsedTask.CreatedDateTime,
|
|
|
|
|
UpdatedDateTime = parsedTask.UpdatedDateTime
|
2024-02-05 20:53:18 +00:00
|
|
|
};
|
|
|
|
|
tasks.Add(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AgentTask? ParseAgentTask(string taskFile)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(taskFile)) return null;
|
|
|
|
|
|
|
|
|
|
var prefix = @"#metadata";
|
|
|
|
|
var suffix = @"/metadata";
|
|
|
|
|
var fileName = taskFile.Split(Path.DirectorySeparatorChar).Last();
|
|
|
|
|
var id = fileName.Split('.').First();
|
|
|
|
|
var data = File.ReadAllText(taskFile);
|
|
|
|
|
var pattern = $@"{prefix}.+{suffix}";
|
|
|
|
|
var metaData = Regex.Match(data, pattern, RegexOptions.Singleline);
|
|
|
|
|
|
|
|
|
|
if (!metaData.Success) return null;
|
|
|
|
|
|
|
|
|
|
var task = metaData.Value.JsonContent<AgentTask>();
|
|
|
|
|
if (task == null) return null;
|
|
|
|
|
|
|
|
|
|
task.Id = id;
|
|
|
|
|
pattern = $@"{suffix}.+";
|
|
|
|
|
var content = Regex.Match(data, pattern, RegexOptions.Singleline).Value;
|
|
|
|
|
task.Content = content.Substring(suffix.Length).Trim();
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserAgent BuildUserAgent(string agentId, string userId, bool editable = false)
|
|
|
|
|
{
|
|
|
|
|
return new UserAgent
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid().ToString(),
|
|
|
|
|
UserId = userId,
|
|
|
|
|
AgentId = agentId,
|
|
|
|
|
Editable = editable,
|
|
|
|
|
CreatedTime = DateTime.UtcNow,
|
|
|
|
|
UpdatedTime = DateTime.UtcNow
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|