2024-04-12 15:00:55 +00:00
|
|
|
using BotSharp.Abstraction.Repositories.Enums;
|
2023-09-18 23:14:24 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2024-05-15 22:30:45 +00:00
|
|
|
using BotSharp.Abstraction.Users.Enums;
|
2024-11-15 02:19:53 +00:00
|
|
|
using BotSharp.Abstraction.Users.Models;
|
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
|
|
|
|
|
{
|
2023-09-15 20:19:44 +00:00
|
|
|
public async Task UpdateAgent(Agent agent, AgentField updateField)
|
2023-06-23 04:43:00 +00:00
|
|
|
{
|
2024-10-31 19:36:26 +00:00
|
|
|
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
|
|
|
|
|
|
2024-05-15 22:30:45 +00:00
|
|
|
var userService = _services.GetRequiredService<IUserService>();
|
2024-11-15 02:19:53 +00:00
|
|
|
var auth = await userService.GetUserAuthorizations(new List<string> { agent.Id });
|
|
|
|
|
var allowEdit = auth.IsAgentActionAllowed(agent.Id, UserAction.Edit);
|
2024-05-15 22:30:45 +00:00
|
|
|
|
2024-11-15 02:19:53 +00:00
|
|
|
if (!allowEdit)
|
2024-10-31 19:36:26 +00:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-28 05:28:14 +00:00
|
|
|
|
2023-10-19 22:27:51 +00:00
|
|
|
var record = _db.GetAgent(agent.Id);
|
2023-09-02 05:10:46 +00:00
|
|
|
if (record == null) return;
|
2023-06-26 23:08:24 +00:00
|
|
|
|
2023-09-15 20:52:22 +00:00
|
|
|
record.Name = agent.Name ?? string.Empty;
|
|
|
|
|
record.Description = agent.Description ?? string.Empty;
|
2023-09-18 23:14:24 +00:00
|
|
|
record.IsPublic = agent.IsPublic;
|
|
|
|
|
record.Disabled = agent.Disabled;
|
2024-01-26 04:32:48 +00:00
|
|
|
record.Type = agent.Type;
|
2023-09-18 23:14:24 +00:00
|
|
|
record.Profiles = agent.Profiles ?? new List<string>();
|
|
|
|
|
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
|
2023-09-15 20:52:22 +00:00
|
|
|
record.Instruction = agent.Instruction ?? string.Empty;
|
2024-08-13 21:36:12 +00:00
|
|
|
record.ChannelInstructions = agent.ChannelInstructions ?? new List<ChannelInstruction>();
|
2023-09-27 20:49:44 +00:00
|
|
|
record.Functions = agent.Functions ?? new List<FunctionDef>();
|
2023-09-15 20:52:22 +00:00
|
|
|
record.Templates = agent.Templates ?? new List<AgentTemplate>();
|
|
|
|
|
record.Responses = agent.Responses ?? new List<AgentResponse>();
|
2023-10-19 22:27:51 +00:00
|
|
|
record.Samples = agent.Samples ?? new List<string>();
|
2024-07-02 18:36:45 +00:00
|
|
|
record.Utilities = agent.Utilities ?? new List<string>();
|
2024-01-16 03:26:18 +00:00
|
|
|
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
|
2023-12-26 03:16:41 +00:00
|
|
|
{
|
|
|
|
|
record.LlmConfig = agent.LlmConfig;
|
|
|
|
|
}
|
2023-09-15 20:52:22 +00:00
|
|
|
|
2023-09-15 20:19:44 +00:00
|
|
|
_db.UpdateAgent(record, updateField);
|
2023-12-27 15:33:03 +00:00
|
|
|
|
|
|
|
|
Utilities.ClearCache();
|
2023-09-02 05:10:46 +00:00
|
|
|
await Task.CompletedTask;
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|
2023-09-13 22:34:19 +00:00
|
|
|
|
2024-04-11 07:00:17 +00:00
|
|
|
public async Task<string> UpdateAgentFromFile(string id)
|
2023-09-13 22:34:19 +00:00
|
|
|
{
|
2024-04-11 15:19:57 +00:00
|
|
|
string updateResult;
|
2024-04-12 15:00:55 +00:00
|
|
|
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
|
|
|
|
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
|
|
|
|
|
|
|
|
|
if (dbSettings.Default == RepositoryEnum.FileRepository)
|
|
|
|
|
{
|
|
|
|
|
updateResult = $"Invalid database repository setting: {dbSettings.Default}";
|
|
|
|
|
_logger.LogWarning(updateResult);
|
|
|
|
|
return updateResult;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 22:27:51 +00:00
|
|
|
var agent = _db.GetAgent(id);
|
2024-04-11 07:00:17 +00:00
|
|
|
if (agent == null)
|
|
|
|
|
{
|
2024-04-11 15:19:57 +00:00
|
|
|
updateResult = $"Cannot find agent ${id}";
|
|
|
|
|
_logger.LogError(updateResult);
|
|
|
|
|
return updateResult;
|
2024-04-11 07:00:17 +00:00
|
|
|
}
|
2023-09-13 22:34:19 +00:00
|
|
|
|
2024-01-16 03:26:18 +00:00
|
|
|
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
|
|
|
|
|
dbSettings.FileRepository,
|
|
|
|
|
agentSettings.DataDir);
|
2023-09-13 22:34:19 +00:00
|
|
|
|
|
|
|
|
var clonedAgent = Agent.Clone(agent);
|
2024-08-13 22:16:55 +00:00
|
|
|
var foundAgent = GetAgentFileById(agent.Id, filePath);
|
2024-04-11 07:00:17 +00:00
|
|
|
if (foundAgent == null)
|
|
|
|
|
{
|
2024-04-11 15:19:57 +00:00
|
|
|
updateResult = $"Cannot find agent {agent.Name} in file directory: {filePath}";
|
|
|
|
|
_logger.LogError(updateResult);
|
|
|
|
|
return updateResult;
|
2024-04-11 07:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
2023-09-13 22:34:19 +00:00
|
|
|
{
|
|
|
|
|
clonedAgent.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-13 22:34:19 +00:00
|
|
|
.SetInstruction(foundAgent.Instruction)
|
2024-08-13 21:36:12 +00:00
|
|
|
.SetChannelInstructions(foundAgent.ChannelInstructions)
|
2023-09-13 22:34:19 +00:00
|
|
|
.SetTemplates(foundAgent.Templates)
|
|
|
|
|
.SetFunctions(foundAgent.Functions)
|
2023-10-19 22:27:51 +00:00
|
|
|
.SetResponses(foundAgent.Responses)
|
2023-12-13 20:26:46 +00:00
|
|
|
.SetSamples(foundAgent.Samples)
|
2024-07-02 18:36:45 +00:00
|
|
|
.SetUtilities(foundAgent.Utilities)
|
2023-12-13 20:26:46 +00:00
|
|
|
.SetLlmConfig(foundAgent.LlmConfig);
|
2023-09-13 22:34:19 +00:00
|
|
|
|
2023-09-15 20:19:44 +00:00
|
|
|
_db.UpdateAgent(clonedAgent, AgentField.All);
|
2023-12-27 15:33:03 +00:00
|
|
|
Utilities.ClearCache();
|
2024-04-11 15:19:57 +00:00
|
|
|
|
|
|
|
|
updateResult = $"Agent {agent.Name} has been migrated!";
|
|
|
|
|
_logger.LogInformation(updateResult);
|
|
|
|
|
return updateResult;
|
2024-04-11 07:00:17 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-04-11 15:19:57 +00:00
|
|
|
updateResult = $"Failed to migrate agent {agent.Name} in file directory {filePath}.\r\nError: {ex.Message}";
|
|
|
|
|
_logger.LogError(updateResult);
|
|
|
|
|
return updateResult;
|
2023-09-13 22:34:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 22:06:03 +00:00
|
|
|
|
|
|
|
|
public async Task<string> PatchAgentTemplate(Agent agent)
|
|
|
|
|
{
|
|
|
|
|
var patchResult = string.Empty;
|
|
|
|
|
if (agent == null || agent.Templates.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
patchResult = $"Null agent instance or empty input templates";
|
|
|
|
|
_logger.LogWarning(patchResult);
|
|
|
|
|
return patchResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var record = _db.GetAgent(agent.Id);
|
|
|
|
|
if (record == null)
|
|
|
|
|
{
|
|
|
|
|
patchResult = $"Cannot find agent {agent.Id}";
|
|
|
|
|
_logger.LogWarning(patchResult);
|
|
|
|
|
return patchResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var successTemplates = new List<string>();
|
|
|
|
|
var failTemplates = new List<string>();
|
|
|
|
|
foreach (var template in agent.Templates)
|
|
|
|
|
{
|
|
|
|
|
if (template == null) continue;
|
|
|
|
|
|
|
|
|
|
var result = _db.PatchAgentTemplate(agent.Id, template);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
successTemplates.Add(template.Name);
|
|
|
|
|
_logger.LogInformation($"Template {template.Name} is updated successfully!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
failTemplates.Add(template.Name);
|
|
|
|
|
_logger.LogWarning($"Template {template.Name} is failed to be updated!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utilities.ClearCache();
|
|
|
|
|
|
|
|
|
|
if (!successTemplates.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
patchResult += $"Success templates:\n{string.Join('\n', successTemplates)}\n\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!failTemplates.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
patchResult += $"Failed templates:\n{string.Join('\n', failTemplates)}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return patchResult;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 22:16:55 +00:00
|
|
|
private Agent? GetAgentFileById(string agentId, string filePath)
|
2023-09-13 22:34:19 +00:00
|
|
|
{
|
2024-04-11 07:00:17 +00:00
|
|
|
if (!Directory.Exists(filePath)) return null;
|
|
|
|
|
|
2023-09-13 22:34:19 +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.Id == agentId)
|
|
|
|
|
{
|
2024-08-13 22:16:55 +00:00
|
|
|
var (defaultInstruction, channelInstructions) = GetInstructionsFromFile(dir);
|
|
|
|
|
var functions = GetFunctionsFromFile(dir);
|
|
|
|
|
var responses = GetResponsesFromFile(dir);
|
|
|
|
|
var templates = GetTemplatesFromFile(dir);
|
|
|
|
|
var samples = GetSamplesFromFile(dir);
|
2024-08-13 21:36:12 +00:00
|
|
|
return agent.SetInstruction(defaultInstruction)
|
|
|
|
|
.SetChannelInstructions(channelInstructions)
|
2023-09-13 22:34:19 +00:00
|
|
|
.SetTemplates(templates)
|
|
|
|
|
.SetFunctions(functions)
|
2023-10-19 22:27:51 +00:00
|
|
|
.SetResponses(responses)
|
|
|
|
|
.SetSamples(samples);
|
2023-09-13 22:34:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-23 04:43:00 +00:00
|
|
|
}
|