Merge pull request #175 from iceljc/features/add-refresh-agents

add bulk insert agents and user agents
This commit is contained in:
Haiping 2023-10-17 15:41:22 -05:00 committed by GitHub
commit a69cb49044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 171 additions and 14 deletions

View file

@ -6,6 +6,7 @@ namespace BotSharp.Abstraction.Agents;
public interface IAgentService
{
Task<Agent> CreateAgent(Agent agent);
Task RefreshAgents();
Task<List<Agent>> GetAgents();
/// <summary>

View file

@ -13,13 +13,16 @@ public interface IBotSharpRepository
void Add<TTableInterface>(object entity);
#region User
User GetUserByEmail(string email);
User? GetUserByEmail(string email);
void CreateUser(User user);
#endregion
#region Agent
void UpdateAgent(Agent agent, AgentField field);
Agent? GetAgent(string agentId);
void BulkInsertAgents(List<Agent> agents);
void BulkInsertUserAgents(List<UserAgent> userAgents);
bool DeleteAgents();
List<string> GetAgentResponses(string agentId, string prefix, string intent);
string GetAgentTemplate(string agentId, string templateName);
#endregion

View file

@ -2,8 +2,8 @@ namespace BotSharp.Abstraction.Utilities;
public static class ListExtenstions
{
public static bool IsNullOrEmpty<T>(this IEnumerable<T> strList)
public static bool IsNullOrEmpty<T>(this IEnumerable<T> list)
{
return strList == null || !strList.Any();
return list == null || !list.Any();
}
}

View file

@ -0,0 +1,51 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using System.IO;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task RefreshAgents()
{
var isDeleted = _db.DeleteAgents();
if (!isDeleted) return;
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentDir = Path.Combine(dbSettings.FileRepository, _agentSettings.DataDir);
var user = _db.Users.FirstOrDefault(x => x.Id == _user.Id || x.ExternalId == _user.Id);
var agents = new List<Agent>();
var userAgents = new List<UserAgent>();
foreach (var dir in Directory.GetDirectories(agentDir))
{
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent == null) continue;
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
var userAgent = new UserAgent
{
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AgentId = agent.Id,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
agents.Add(agent);
userAgents.Add(userAgent);
}
_db.BulkInsertAgents(agents);
_db.BulkInsertUserAgents(userAgents);
}
}

View file

@ -86,6 +86,21 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
public void BulkInsertAgents(List<Agent> agents)
{
throw new NotImplementedException();
}
public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
throw new NotImplementedException();
}
public bool DeleteAgents()
{
throw new NotImplementedException();
}
#endregion
@ -128,7 +143,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
#region User
public User GetUserByEmail(string email)
public User? GetUserByEmail(string email)
{
throw new NotImplementedException();
}

View file

@ -31,7 +31,7 @@ public class FileRepository : IBotSharpRepository
};
}
private List<User> _users;
private List<User> _users = new List<User>();
public IQueryable<User> Users
{
get
@ -52,7 +52,7 @@ public class FileRepository : IBotSharpRepository
}
}
private List<Agent> _agents;
private List<Agent> _agents = new List<Agent>();
public IQueryable<Agent> Agents
{
get
@ -81,7 +81,7 @@ public class FileRepository : IBotSharpRepository
}
}
private List<UserAgent> _userAgents;
private List<UserAgent> _userAgents = new List<UserAgent>();
public IQueryable<UserAgent> UserAgents
{
get
@ -106,7 +106,7 @@ public class FileRepository : IBotSharpRepository
}
}
private List<Conversation> _conversations;
private List<Conversation> _conversations = new List<Conversation>();
public IQueryable<Conversation> Conversations
{
get
@ -534,6 +534,19 @@ public class FileRepository : IBotSharpRepository
return string.Empty;
}
public void BulkInsertAgents(List<Agent> agents)
{
}
public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
}
public bool DeleteAgents()
{
return false;
}
#endregion
#region Conversation
@ -684,7 +697,7 @@ public class FileRepository : IBotSharpRepository
#endregion
#region User
public User GetUserByEmail(string email)
public User? GetUserByEmail(string email)
{
return Users.FirstOrDefault(x => x.Email == email);
}

View file

@ -38,6 +38,12 @@ public class AgentController : ControllerBase, IApiAdapter
return AgentViewModel.FromAgent(createdAgent);
}
[HttpPost("/refresh-agents")]
public async Task RefreshAgents()
{
await _agentService.RefreshAgents();
}
[HttpPut("/agent/file/{agentId}")]
public async Task UpdateAgentFromFile([FromRoute] string agentId)
{

View file

@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>$(LangVersion)</LangVersion>
<Nullable>enable</Nullable>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>

View file

@ -3,6 +3,7 @@ using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Utilities;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
@ -24,7 +25,7 @@ public class MongoRepository : IBotSharpRepository
};
}
private List<Agent> _agents;
private List<Agent> _agents = new List<Agent>();
public IQueryable<Agent> Agents
{
get
@ -65,7 +66,7 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<User> _users;
private List<User> _users = new List<User>();
public IQueryable<User> Users
{
get
@ -93,7 +94,7 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<UserAgent> _userAgents;
private List<UserAgent> _userAgents = new List<UserAgent>();
public IQueryable<UserAgent> UserAgents
{
get
@ -117,7 +118,7 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<Conversation> _conversations;
private List<Conversation> _conversations = new List<Conversation>();
public IQueryable<Conversation> Conversations
{
get
@ -539,6 +540,72 @@ public class MongoRepository : IBotSharpRepository
return agent.Templates?.FirstOrDefault(x => x.Name == templateName.ToLower())?.Content ?? string.Empty;
}
public void BulkInsertAgents(List<Agent> agents)
{
if (agents.IsNullOrEmpty()) return;
var agentDocs = agents.Select(x => new AgentCollection
{
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?
.ToList() ?? new List<AgentTemplateMongoElement>(),
Functions = x.Functions?
.Select(f => FunctionDefMongoElement.ToMongoElement(f))?
.ToList() ?? new List<FunctionDefMongoElement>(),
Responses = x.Responses?
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
.ToList() ?? new List<AgentResponseMongoElement>(),
IsPublic = x.IsPublic,
AllowRouting = x.AllowRouting,
Disabled = x.Disabled,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?
.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?
.ToList() ?? new List<RoutingRuleMongoElement>(),
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();
_dc.Agents.InsertMany(agentDocs);
}
public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
if (userAgents.IsNullOrEmpty()) return;
var userAgentDocs = userAgents.Select(x => new UserAgentCollection
{
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
_dc.UserAgents.InsertMany(userAgentDocs);
}
public bool DeleteAgents()
{
try
{
var userAgentFilter = Builders<UserAgentCollection>.Filter.Empty;
var agentfilter = Builders<AgentCollection>.Filter.Empty;
_dc.UserAgents.DeleteMany(userAgentFilter);
_dc.Agents.DeleteMany(agentfilter);
return true;
}
catch
{
return false;
}
}
#endregion
#region Conversation
@ -676,7 +743,7 @@ public class MongoRepository : IBotSharpRepository
#endregion
#region User
public User GetUserByEmail(string email)
public User? GetUserByEmail(string email)
{
var user = Users.FirstOrDefault(x => x.Email == email);
return user != null ? new User