Merge pull request #449 from iceljc/features/add-agent-template-update
add agent template update endpoint
This commit is contained in:
commit
2fca75d9d8
|
|
@ -37,6 +37,13 @@ public interface IAgentService
|
|||
|
||||
Task<bool> DeleteAgent(string id);
|
||||
Task UpdateAgent(Agent agent, AgentField updateField);
|
||||
|
||||
/// <summary>
|
||||
/// Path existing templates of agent, cannot create new or delete templates
|
||||
/// </summary>
|
||||
/// <param name="agent"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> PatchAgentTemplate(Agent agent);
|
||||
Task<string> UpdateAgentFromFile(string id);
|
||||
string GetDataDir();
|
||||
string GetAgentDataDir(string agentId);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
using BotSharp.Abstraction.Loggers.Models;
|
||||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Repositories.Filters;
|
||||
using BotSharp.Abstraction.Repositories.Models;
|
||||
using BotSharp.Abstraction.Tasks.Models;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
|
||||
|
|
@ -35,6 +34,7 @@ public interface IBotSharpRepository
|
|||
bool DeleteAgent(string agentId);
|
||||
List<string> GetAgentResponses(string agentId, string prefix, string intent);
|
||||
string GetAgentTemplate(string agentId, string templateName);
|
||||
bool PatchAgentTemplate(string agentId, AgentTemplate template);
|
||||
#endregion
|
||||
|
||||
#region Agent Task
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Repositories.Enums;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using System.IO;
|
||||
|
|
@ -106,6 +103,59 @@ public partial class AgentService
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private Agent? FetchAgentFileById(string agentId, string filePath)
|
||||
{
|
||||
if (!Directory.Exists(filePath)) return null;
|
||||
|
|
|
|||
|
|
@ -87,6 +87,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
public string GetAgentTemplate(string agentId, string templateName)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public bool PatchAgentTemplate(string agentId, AgentTemplate template)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
public List<string> GetAgentResponses(string agentId, string prefix, string intent)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
|
|
|
|||
|
|
@ -401,6 +401,25 @@ namespace BotSharp.Core.Repository
|
|||
return string.Empty;
|
||||
}
|
||||
|
||||
public bool PatchAgentTemplate(string agentId, AgentTemplate template)
|
||||
{
|
||||
if (string.IsNullOrEmpty(agentId) || template == null) return false;
|
||||
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, "templates");
|
||||
if (!Directory.Exists(dir)) return false;
|
||||
|
||||
var foundTemplate = Directory.GetFiles(dir).FirstOrDefault(f =>
|
||||
{
|
||||
var fileName = Path.GetFileNameWithoutExtension(f);
|
||||
var extension = Path.GetExtension(f).Substring(1);
|
||||
return fileName.IsEqualTo(template.Name) && extension.IsEqualTo(_agentSettings.TemplateFormat);
|
||||
});
|
||||
|
||||
if (foundTemplate == null) return false;
|
||||
|
||||
File.WriteAllText(foundTemplate, template.Content);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void BulkInsertAgents(List<Agent> agents)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -110,4 +110,12 @@ public class AgentController : ControllerBase
|
|||
model.Id = agentId;
|
||||
await _agentService.UpdateAgent(model, field);
|
||||
}
|
||||
|
||||
[HttpPatch("/agent/{agentId}/templates")]
|
||||
public async Task<string> PatchAgentTemplates([FromRoute] string agentId, [FromBody] AgentTemplatePatchModel agent)
|
||||
{
|
||||
var model = agent.ToAgent();
|
||||
model.Id = agentId;
|
||||
return await _agentService.PatchAgentTemplate(model);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Agents;
|
||||
|
||||
public class AgentTemplatePatchModel
|
||||
{
|
||||
public List<AgentTemplate>? Templates { get; set; }
|
||||
|
||||
public AgentTemplatePatchModel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Agent ToAgent()
|
||||
{
|
||||
var agent = new Agent()
|
||||
{
|
||||
Templates = Templates ?? new List<AgentTemplate>(),
|
||||
};
|
||||
|
||||
return agent;
|
||||
}
|
||||
}
|
||||
|
|
@ -332,6 +332,23 @@ public partial class MongoRepository
|
|||
return agent.Templates?.FirstOrDefault(x => x.Name == templateName.ToLower())?.Content ?? string.Empty;
|
||||
}
|
||||
|
||||
public bool PatchAgentTemplate(string agentId, AgentTemplate template)
|
||||
{
|
||||
if (string.IsNullOrEmpty(agentId) || template == null) return false;
|
||||
|
||||
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
|
||||
var agent = _dc.Agents.Find(filter).FirstOrDefault();
|
||||
if (agent == null || agent.Templates.IsNullOrEmpty()) return false;
|
||||
|
||||
var foundTemplate = agent.Templates.FirstOrDefault(x => x.Name.IsEqualTo(template.Name));
|
||||
if (foundTemplate == null) return false;
|
||||
|
||||
foundTemplate.Content = template.Content;
|
||||
var update = Builders<AgentDocument>.Update.Set(x => x.Templates, agent.Templates);
|
||||
_dc.Agents.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void BulkInsertAgents(List<Agent> agents)
|
||||
{
|
||||
if (agents.IsNullOrEmpty()) return;
|
||||
|
|
|
|||
Loading…
Reference in a new issue