add agent delete

This commit is contained in:
Jicheng Lu 2024-05-21 15:55:12 -05:00
parent 003de30538
commit effd44eb7c
4 changed files with 57 additions and 25 deletions

View file

@ -22,32 +22,13 @@ public partial class AgentService
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
var foundAgent = FetchAgentFileByName(agent.Name, filePath);
if (foundAgent != null)
{
agentRecord.SetId(foundAgent.Id)
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetDisabled(foundAgent.Disabled)
.SetAgentType(foundAgent.Type)
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
.SetLlmConfig(foundAgent.LlmConfig);
}
var user = _db.GetUserById(_user.Id);
var userAgentRecord = new UserAgent
{
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AgentId = foundAgent?.Id ?? agentRecord.Id,
AgentId = agentRecord.Id,
Editable = false,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
@ -61,7 +42,7 @@ public partial class AgentService
Utilities.ClearCache();
return agentRecord;
return await Task.FromResult(agentRecord);
}
private Agent FetchAgentFileByName(string agentName, string filePath)

View file

@ -1,9 +1,20 @@
using BotSharp.Abstraction.Users.Enums;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task<bool> DeleteAgent(string id)
{
throw new NotImplementedException();
var user = _db.GetUserById(_user.Id);
var agent = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Id.IsEqualTo(id));
if (user?.Role != UserRole.Admin && agent == null)
{
return false;
}
var deleted = _db.DeleteAgent(id);
return await Task.FromResult(deleted);
}
}

View file

@ -436,7 +436,40 @@ namespace BotSharp.Core.Repository
public bool DeleteAgent(string agentId)
{
return false;
if (string.IsNullOrEmpty(agentId)) return false;
try
{
var agentDir = GetAgentDataDir(agentId);
if (string.IsNullOrEmpty(agentDir)) return false;
// Delete agent user relationships
var usersDir = Path.Combine(_dbSettings.FileRepository, "users");
if (Directory.Exists(usersDir))
{
foreach (var userDir in Directory.GetDirectories(usersDir))
{
var userAgentFile = Directory.GetFiles(userDir).FirstOrDefault(x => Path.GetFileName(x) == USER_AGENT_FILE);
if (string.IsNullOrEmpty(userAgentFile)) continue;
var text = File.ReadAllText(userAgentFile);
var userAgents = JsonSerializer.Deserialize<List<UserAgent>>(text, _options);
if (userAgents.IsNullOrEmpty()) continue;
userAgents = userAgents.Where(x => x.AgentId != agentId).ToList();
File.WriteAllText(userAgentFile, JsonSerializer.Serialize(userAgents, _options));
}
}
// Delete agent folder
Directory.Delete(agentDir, true);
return true;
}
catch
{
return false;
}
}
}
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Users.Enums;
namespace BotSharp.OpenAPI.Controllers;
@ -27,7 +26,7 @@ public class AgentController : ControllerBase
}
[HttpGet("/agent/{id}")]
public async Task<AgentViewModel> GetAgent([FromRoute] string id)
public async Task<AgentViewModel?> GetAgent([FromRoute] string id)
{
var agents = await GetAgents(new AgentFilter
{
@ -35,6 +34,8 @@ public class AgentController : ControllerBase
});
var targetAgent = agents.Items.FirstOrDefault();
if (targetAgent == null) return null;
var redirectAgentIds = targetAgent.RoutingRules
.Where(x => !string.IsNullOrEmpty(x.RedirectTo))
.Select(x => x.RedirectTo).ToList();
@ -133,4 +134,10 @@ public class AgentController : ControllerBase
model.Id = agentId;
return await _agentService.PatchAgentTemplate(model);
}
[HttpDelete("/agent/{agentId}")]
public async Task<bool> DeleteAgent([FromRoute] string agentId)
{
return await _agentService.DeleteAgent(agentId);
}
}