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)
|
|
|
|
|
{
|
2024-10-31 19:36:26 +00:00
|
|
|
var userAgents = _db.GetUserAgents(_user.Id);
|
|
|
|
|
var found = userAgents?.FirstOrDefault(x => x.Agent != null && x.Agent.Name.IsEqualTo(agent.Name));
|
|
|
|
|
if (found != null)
|
2023-06-23 04:43:00 +00:00
|
|
|
{
|
2024-10-31 19:36:26 +00:00
|
|
|
return found.Agent;
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-31 19:36:26 +00:00
|
|
|
var agentRecord = Agent.Clone(agent);
|
2023-09-07 22:04:34 +00:00
|
|
|
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>();
|
|
|
|
|
|
2023-11-14 01:25:25 +00:00
|
|
|
var user = _db.GetUserById(_user.Id);
|
2024-10-31 19:36:26 +00:00
|
|
|
_db.BulkInsertAgents(new List<Agent> { agentRecord });
|
2023-06-23 04:43:00 +00:00
|
|
|
|
2023-12-27 15:33:03 +00:00
|
|
|
Utilities.ClearCache();
|
2024-05-21 20:55:12 +00:00
|
|
|
return await Task.FromResult(agentRecord);
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|
2023-09-02 05:10:46 +00:00
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private (string, List<ChannelInstruction>) GetInstructionsFromFile(string fileDir)
|
2023-09-02 05:10:46 +00:00
|
|
|
{
|
2024-08-13 21:36:12 +00:00
|
|
|
var defaultInstruction = string.Empty;
|
|
|
|
|
var channelInstructions = new List<ChannelInstruction>();
|
|
|
|
|
|
|
|
|
|
var instructionDir = Path.Combine(fileDir, "instructions");
|
|
|
|
|
if (!Directory.Exists(instructionDir))
|
2023-09-02 05:10:46 +00:00
|
|
|
{
|
2024-08-13 21:36:12 +00:00
|
|
|
return (defaultInstruction, channelInstructions);
|
2023-09-02 05:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 21:36:12 +00:00
|
|
|
foreach (var file in Directory.GetFiles(instructionDir))
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(file).Substring(1);
|
|
|
|
|
if (!extension.IsEqualTo(_agentSettings.TemplateFormat))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-02 05:10:46 +00:00
|
|
|
|
2024-08-13 21:36:12 +00:00
|
|
|
var segments = Path.GetFileName(file).Split(".", StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (segments.IsNullOrEmpty() || !segments[0].IsEqualTo("instruction"))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-02 05:10:46 +00:00
|
|
|
|
2024-08-13 21:36:12 +00:00
|
|
|
if (segments.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
defaultInstruction = File.ReadAllText(file);
|
|
|
|
|
}
|
|
|
|
|
else if (segments.Length == 3)
|
|
|
|
|
{
|
|
|
|
|
var item = new ChannelInstruction
|
|
|
|
|
{
|
|
|
|
|
Channel = segments[1],
|
|
|
|
|
Instruction = File.ReadAllText(file)
|
|
|
|
|
};
|
|
|
|
|
channelInstructions.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (defaultInstruction, channelInstructions);
|
2023-09-02 05:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private List<AgentTemplate> GetTemplatesFromFile(string fileDir)
|
2023-09-12 22:08:58 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
{
|
2024-06-26 18:30:27 +00:00
|
|
|
var extension = Path.GetExtension(file).Substring(1);
|
2023-10-16 04:12:17 +00:00
|
|
|
if (extension.IsEqualTo(_agentSettings.TemplateFormat))
|
2023-09-12 22:08:58 +00:00
|
|
|
{
|
2024-08-13 21:36:12 +00:00
|
|
|
var name = Path.GetFileNameWithoutExtension(file);
|
2023-09-12 22:08:58 +00:00
|
|
|
var content = File.ReadAllText(file);
|
|
|
|
|
templates.Add(new AgentTemplate(name, content));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return templates;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private List<FunctionDef> GetFunctionsFromFile(string fileDir)
|
2023-09-02 05:10:46 +00:00
|
|
|
{
|
2024-06-26 18:13:52 +00:00
|
|
|
var functions = new List<FunctionDef>();
|
|
|
|
|
var functionDir = Path.Combine(fileDir, "functions");
|
2023-09-02 05:10:46 +00:00
|
|
|
|
2024-06-26 18:13:52 +00:00
|
|
|
if (!Directory.Exists(functionDir)) return functions;
|
|
|
|
|
|
|
|
|
|
foreach (var file in Directory.GetFiles(functionDir))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(file).Substring(1);
|
|
|
|
|
if (extension != "json") continue;
|
|
|
|
|
|
|
|
|
|
var json = File.ReadAllText(file);
|
|
|
|
|
var function = JsonSerializer.Deserialize<FunctionDef>(json, _options);
|
|
|
|
|
functions.Add(function);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-09-02 05:10:46 +00:00
|
|
|
return functions;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private List<AgentResponse> GetResponsesFromFile(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
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private List<string> GetSamplesFromFile(string fileDir)
|
2023-10-19 22:27:51 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private List<AgentTask> GetTasksFromFile(string fileDir)
|
2024-02-05 20:53:18 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|