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-06-29 23:14:57 +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 UpdateAgent(Agent agent)
|
|
|
|
|
{
|
2023-08-10 04:53:22 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-06-26 23:08:24 +00:00
|
|
|
|
2023-09-07 22:04:34 +00:00
|
|
|
var record = (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 (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
|
|
|
|
|
a.Id == agent.Id
|
|
|
|
|
select a).FirstOrDefault();
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
if (record == null) return;
|
2023-06-26 23:08:24 +00:00
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
record.Name = agent.Name;
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
if (!string.IsNullOrEmpty(agent.Description))
|
|
|
|
|
record.Description = agent.Description;
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
if (!string.IsNullOrEmpty(agent.Instruction))
|
|
|
|
|
record.Instruction = agent.Instruction;
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-13 22:34:19 +00:00
|
|
|
if (!agent.Templates.IsNullOrEmpty())
|
|
|
|
|
record.Templates = agent.Templates;
|
|
|
|
|
|
2023-09-06 04:41:49 +00:00
|
|
|
if (!agent.Functions.IsNullOrEmpty())
|
2023-09-02 05:10:46 +00:00
|
|
|
record.Functions = agent.Functions;
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-06 04:41:49 +00:00
|
|
|
if (!agent.Responses.IsNullOrEmpty())
|
2023-09-02 05:10:46 +00:00
|
|
|
record.Responses = agent.Responses;
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-09-02 05:10:46 +00:00
|
|
|
db.UpdateAgent(record);
|
|
|
|
|
await Task.CompletedTask;
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|
2023-09-13 22:34:19 +00:00
|
|
|
|
|
|
|
|
public async Task UpdateAgentFromFile(string id)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
|
|
|
|
|
|
|
|
|
|
if (agent == null) return;
|
|
|
|
|
|
|
|
|
|
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
|
|
|
|
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
|
|
|
|
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
|
|
|
|
|
|
|
|
|
|
var clonedAgent = Agent.Clone(agent);
|
|
|
|
|
var foundAgent = FetchAgentFileById(agent.Id, filePath);
|
|
|
|
|
if (foundAgent != null)
|
|
|
|
|
{
|
|
|
|
|
clonedAgent.SetId(foundAgent.Id)
|
|
|
|
|
.SetName(foundAgent.Name)
|
|
|
|
|
.SetDescription(foundAgent.Description)
|
|
|
|
|
.SetIsPublic(foundAgent.IsPublic)
|
|
|
|
|
.SetInstruction(foundAgent.Instruction)
|
|
|
|
|
.SetTemplates(foundAgent.Templates)
|
|
|
|
|
.SetFunctions(foundAgent.Functions)
|
|
|
|
|
.SetResponses(foundAgent.Responses);
|
|
|
|
|
|
|
|
|
|
db.UpdateAgent(clonedAgent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Agent FetchAgentFileById(string agentId, string filePath)
|
|
|
|
|
{
|
|
|
|
|
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.Id == agentId)
|
|
|
|
|
{
|
|
|
|
|
var functions = FetchFunctionsFromFile(dir);
|
|
|
|
|
var instruction = FetchInstructionFromFile(dir);
|
|
|
|
|
var responses = FetchResponsesFromFile(dir);
|
|
|
|
|
var templates = FetchTemplatesFromFile(dir);
|
|
|
|
|
return agent.SetInstruction(instruction)
|
|
|
|
|
.SetTemplates(templates)
|
|
|
|
|
.SetFunctions(functions)
|
|
|
|
|
.SetResponses(responses);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|