BotSharp/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

717 lines
28 KiB
C#
Raw Normal View History

2023-09-03 22:43:50 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
2023-09-27 20:49:44 +00:00
using BotSharp.Abstraction.Functions.Models;
2023-09-18 23:14:24 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-09-07 22:04:34 +00:00
using BotSharp.Abstraction.Users.Models;
2023-09-01 22:33:36 +00:00
using BotSharp.Plugin.MongoStorage.Collections;
2023-09-18 23:14:24 +00:00
using BotSharp.Plugin.MongoStorage.Models;
2023-08-26 04:41:01 +00:00
2023-09-01 22:33:36 +00:00
namespace BotSharp.Plugin.MongoStorage.Repository;
2023-08-28 05:28:14 +00:00
public class MongoRepository : IBotSharpRepository
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
private readonly MongoDbContext _dc;
private readonly IServiceProvider _services;
private UpdateOptions _options;
public MongoRepository(MongoDbContext dc, IServiceProvider services)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
_dc = dc;
_services = services;
_options = new UpdateOptions
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
IsUpsert = true,
};
}
2023-08-26 04:41:01 +00:00
2023-09-07 22:04:34 +00:00
private List<Agent> _agents;
public IQueryable<Agent> Agents
2023-08-28 05:28:14 +00:00
{
get
2023-08-26 04:41:01 +00:00
{
2023-09-08 17:17:54 +00:00
if (!_agents.IsNullOrEmpty())
2023-08-26 04:41:01 +00:00
{
return _agents.AsQueryable();
}
2023-08-28 05:28:14 +00:00
var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List<AgentCollection>();
2023-09-07 22:04:34 +00:00
_agents = agentDocs.Select(x => new Agent
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
2023-08-28 05:28:14 +00:00
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?
.ToList() ?? new List<AgentTemplate>(),
Functions = x.Functions?
.Select(f => FunctionDefMongoElement.ToDomainElement(f))?
.ToList() ?? new List<FunctionDef>(),
Responses = x.Responses?
.Select(r => AgentResponseMongoElement.ToDomainElement(r))?
.ToList() ?? new List<AgentResponse>(),
2023-09-04 15:15:11 +00:00
IsPublic = x.IsPublic,
2023-09-18 23:14:24 +00:00
Disabled = x.Disabled,
AllowRouting = x.AllowRouting,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?
2023-09-19 18:20:41 +00:00
.Select(r => RoutingRuleMongoElement.ToDomainElement(x.Id.ToString(), x.Name, r))?
2023-09-18 23:14:24 +00:00
.ToList() ?? new List<RoutingRule>(),
2023-09-08 17:17:54 +00:00
CreatedDateTime = x.CreatedTime,
UpdatedDateTime = x.UpdatedTime
2023-08-28 05:28:14 +00:00
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _agents.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-09-07 22:04:34 +00:00
private List<User> _users;
public IQueryable<User> Users
2023-08-28 05:28:14 +00:00
{
get
2023-08-26 04:41:01 +00:00
{
2023-09-08 17:17:54 +00:00
if (!_users.IsNullOrEmpty())
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _users.AsQueryable();
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List<UserCollection>();
2023-09-07 22:04:34 +00:00
_users = userDocs.Select(x => new User
2023-08-28 05:28:14 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
2023-08-28 05:28:14 +00:00
FirstName = x.FirstName,
LastName = x.LastName,
Email = x.Email,
Password = x.Password,
Salt = x.Salt,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _users.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-09-07 22:04:34 +00:00
private List<UserAgent> _userAgents;
public IQueryable<UserAgent> UserAgents
2023-08-28 05:28:14 +00:00
{
get
2023-08-26 04:41:01 +00:00
{
2023-09-08 17:17:54 +00:00
if (!_userAgents.IsNullOrEmpty())
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _userAgents.AsQueryable();
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List<UserAgentCollection>();
2023-09-07 22:04:34 +00:00
_userAgents = userDocs.Select(x => new UserAgent
2023-08-28 05:28:14 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
UserId = x.UserId.ToString(),
2023-08-28 05:28:14 +00:00
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _userAgents.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-09-07 22:04:34 +00:00
private List<Conversation> _conversations;
public IQueryable<Conversation> Conversations
2023-08-28 05:28:14 +00:00
{
get
2023-08-26 04:41:01 +00:00
{
2023-09-08 17:17:54 +00:00
if (!_conversations.IsNullOrEmpty())
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _conversations.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
2023-09-07 22:04:34 +00:00
_conversations = new List<Conversation>();
2023-08-28 05:28:14 +00:00
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
2023-09-06 04:41:49 +00:00
foreach (var conv in conversationDocs)
2023-08-26 04:41:01 +00:00
{
2023-09-06 04:41:49 +00:00
var convId = conv.Id.ToString();
var dialog = GetConversationDialog(convId);
2023-09-07 22:04:34 +00:00
_conversations.Add(new Conversation
2023-09-06 04:41:49 +00:00
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog,
2023-09-08 17:17:54 +00:00
States = new ConversationState(conv.States),
2023-09-06 04:41:49 +00:00
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});
}
2023-08-28 05:28:14 +00:00
return _conversations.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
2023-09-07 22:04:34 +00:00
if (entity is Conversation conversation)
2023-08-28 05:28:14 +00:00
{
_conversations.Add(conversation);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(Conversation));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is Agent agent)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
_agents.Add(agent);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(Agent));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is User user)
2023-08-28 05:28:14 +00:00
{
_users.Add(user);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(User));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is UserAgent userAgent)
2023-08-28 05:28:14 +00:00
{
_userAgents.Add(userAgent);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(UserAgent));
2023-08-28 05:28:14 +00:00
}
}
public int Transaction<TTableInterface>(Action action)
{
_changedTableNames.Clear();
action();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var table in _changedTableNames)
{
2023-09-07 22:04:34 +00:00
if (table == nameof(Conversation))
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
var conversations = _conversations.Select(x => new ConversationCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
2023-09-28 17:25:37 +00:00
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
2023-08-28 18:57:51 +00:00
Title = x.Title,
2023-09-09 21:44:25 +00:00
States = x.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
2023-08-28 05:28:14 +00:00
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var conversation in conversations)
{
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.Update
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
2023-08-28 18:57:51 +00:00
.Set(x => x.Title, conversation.Title)
2023-09-08 17:17:54 +00:00
.Set(x => x.States, conversation.States)
2023-08-28 05:28:14 +00:00
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(Agent))
2023-08-28 05:28:14 +00:00
{
var agents = _agents.Select(x => new AgentCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
2023-08-28 05:28:14 +00:00
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>(),
2023-09-04 15:15:11 +00:00
IsPublic = x.IsPublic,
2023-09-18 23:14:24 +00:00
AllowRouting = x.AllowRouting,
Disabled = x.Disabled,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?
2023-09-19 18:20:41 +00:00
.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?
.ToList() ?? new List<RoutingRuleMongoElement>(),
2023-09-08 17:17:54 +00:00
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
2023-08-28 05:28:14 +00:00
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var agent in agents)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, agent.Id);
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
2023-09-12 22:08:58 +00:00
.Set(x => x.Templates, agent.Templates)
2023-08-28 05:28:14 +00:00
.Set(x => x.Functions, agent.Functions)
2023-09-02 05:10:46 +00:00
.Set(x => x.Responses, agent.Responses)
2023-09-04 15:15:11 +00:00
.Set(x => x.IsPublic, agent.IsPublic)
2023-09-18 23:14:24 +00:00
.Set(x => x.AllowRouting, agent.AllowRouting)
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules)
2023-08-28 05:28:14 +00:00
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(User))
2023-08-28 05:28:14 +00:00
{
var users = _users.Select(x => new UserCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
2023-08-28 05:28:14 +00:00
FirstName = x.FirstName,
LastName = x.LastName,
Salt = x.Salt,
Password = x.Password,
Email = x.Email,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var user in users)
{
var filter = Builders<UserCollection>.Filter.Eq(x => x.Id, user.Id);
var update = Builders<UserCollection>.Update
.Set(x => x.FirstName, user.FirstName)
.Set(x => x.LastName, user.LastName)
.Set(x => x.Email, user.Email)
.Set(x => x.Salt, user.Salt)
.Set(x => x.Password, user.Password)
.Set(x => x.ExternalId, user.ExternalId)
.Set(x => x.CreatedTime, user.CreatedTime)
.Set(x => x.UpdatedTime, user.UpdatedTime);
_dc.Users.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(UserAgent))
2023-08-28 05:28:14 +00:00
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
2023-09-28 17:25:37 +00:00
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
2023-08-28 05:28:14 +00:00
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var userAgent in userAgents)
{
var filter = Builders<UserAgentCollection>.Filter.Eq(x => x.Id, userAgent.Id);
var update = Builders<UserAgentCollection>.Update
.Set(x => x.AgentId, userAgent.AgentId)
.Set(x => x.UserId, userAgent.UserId)
.Set(x => x.CreatedTime, userAgent.CreatedTime)
.Set(x => x.UpdatedTime, userAgent.UpdatedTime);
_dc.UserAgents.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
}
}
2023-08-28 05:28:14 +00:00
return _changedTableNames.Count;
2023-08-26 04:41:01 +00:00
}
2023-09-02 05:10:46 +00:00
2023-09-15 20:19:44 +00:00
#region Agent
public void UpdateAgent(Agent agent, AgentField field)
2023-09-02 05:10:46 +00:00
{
2023-09-15 20:19:44 +00:00
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
switch (field)
2023-09-02 05:10:46 +00:00
{
2023-09-15 20:19:44 +00:00
case AgentField.Name:
UpdateAgentName(agent.Id, agent.Name);
break;
case AgentField.Description:
UpdateAgentDescription(agent.Id, agent.Description);
break;
case AgentField.IsPublic:
UpdateAgentIsPublic(agent.Id, agent.IsPublic);
break;
2023-09-18 23:14:24 +00:00
case AgentField.Disabled:
UpdateAgentDisabled(agent.Id, agent.Disabled);
break;
case AgentField.AllowRouting:
UpdateAgentAllowRouting(agent.Id, agent.AllowRouting);
break;
case AgentField.Profiles:
UpdateAgentProfiles(agent.Id, agent.Profiles);
break;
case AgentField.RoutingRules:
UpdateAgentRoutingRules(agent.Id, agent.RoutingRules);
break;
2023-09-15 20:19:44 +00:00
case AgentField.Instruction:
UpdateAgentInstruction(agent.Id, agent.Instruction);
break;
case AgentField.Function:
UpdateAgentFunctions(agent.Id, agent.Functions);
break;
case AgentField.Template:
UpdateAgentTemplates(agent.Id, agent.Templates);
break;
case AgentField.Response:
UpdateAgentResponses(agent.Id, agent.Responses);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
break;
default:
break;
}
2023-09-02 05:10:46 +00:00
}
2023-09-15 20:19:44 +00:00
#region Update Agent Fields
private void UpdateAgentName(string agentId, string name)
2023-09-02 05:10:46 +00:00
{
2023-09-15 20:19:44 +00:00
if (string.IsNullOrEmpty(name)) return;
2023-09-02 05:10:46 +00:00
2023-09-15 20:19:44 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, name)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-02 05:10:46 +00:00
2023-09-15 20:19:44 +00:00
_dc.Agents.UpdateOne(filter, update);
2023-09-02 05:10:46 +00:00
}
2023-09-15 20:19:44 +00:00
private void UpdateAgentDescription(string agentId, string description)
2023-09-02 05:10:46 +00:00
{
2023-09-15 20:19:44 +00:00
if (string.IsNullOrEmpty(description)) return;
2023-09-02 05:10:46 +00:00
2023-09-15 20:19:44 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Description, description)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-02 05:10:46 +00:00
2023-09-15 20:19:44 +00:00
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentIsPublic(string agentId, bool isPublic)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.IsPublic, isPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
2023-09-18 23:14:24 +00:00
private void UpdateAgentDisabled(string agentId, bool disabled)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Disabled, disabled)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentAllowRouting(string agentId, bool allowRouting)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.AllowRouting, allowRouting)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentProfiles(string agentId, List<string> profiles)
{
if (profiles.IsNullOrEmpty()) return;
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Profiles, profiles)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
{
if (rules.IsNullOrEmpty()) return;
2023-09-19 18:20:41 +00:00
var ruleElements = rules.Select(x => RoutingRuleMongoElement.ToMongoElement(x)).ToList();
2023-09-18 23:14:24 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.RoutingRules, ruleElements)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
2023-09-15 20:19:44 +00:00
private void UpdateAgentInstruction(string agentId, string instruction)
{
if (string.IsNullOrEmpty(instruction)) return;
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Instruction, instruction)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
2023-09-27 20:49:44 +00:00
private void UpdateAgentFunctions(string agentId, List<FunctionDef> functions)
2023-09-15 20:19:44 +00:00
{
if (functions.IsNullOrEmpty()) return;
2023-09-02 05:10:46 +00:00
var functionsToUpdate = functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList();
2023-09-15 20:19:44 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Functions, functionsToUpdate)
2023-09-15 20:19:44 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentTemplates(string agentId, List<AgentTemplate> templates)
{
if (templates.IsNullOrEmpty()) return;
var templatesToUpdate = templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList();
2023-09-15 20:19:44 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Templates, templatesToUpdate)
2023-09-15 20:19:44 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentResponses(string agentId, List<AgentResponse> responses)
{
if (responses.IsNullOrEmpty()) return;
var responsesToUpdate = responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList();
2023-09-15 20:19:44 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Responses, responsesToUpdate)
2023-09-15 20:19:44 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentAllFields(Agent agent)
{
2023-09-02 05:10:46 +00:00
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agent.Id));
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
2023-09-18 23:14:24 +00:00
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.AllowRouting, agent.AllowRouting)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules.Select(r => RoutingRuleMongoElement.ToMongoElement(r)).ToList())
2023-09-02 05:10:46 +00:00
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Templates, agent.Templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList())
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
2023-09-04 15:15:11 +00:00
.Set(x => x.IsPublic, agent.IsPublic)
2023-09-15 20:19:44 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-02 05:10:46 +00:00
2023-09-13 22:34:19 +00:00
_dc.Agents.UpdateOne(filter, update);
2023-09-02 05:10:46 +00:00
}
2023-09-15 20:19:44 +00:00
#endregion
2023-09-03 04:11:53 +00:00
2023-09-15 20:19:44 +00:00
public Agent GetAgent(string agentId)
2023-09-03 04:11:53 +00:00
{
2023-09-15 20:19:44 +00:00
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
return foundAgent;
2023-09-03 04:11:53 +00:00
}
2023-09-03 04:54:22 +00:00
2023-09-09 20:13:19 +00:00
public List<string> GetAgentResponses(string agentId, string prefix, string intent)
2023-09-03 04:54:22 +00:00
{
var responses = new List<string>();
2023-09-07 22:04:34 +00:00
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
2023-09-03 04:54:22 +00:00
if (agent == null) return responses;
2023-09-09 21:44:25 +00:00
return agent.Responses.Where(x => x.Prefix == prefix && x.Intent == intent).Select(x => x.Content).ToList();
2023-09-03 04:54:22 +00:00
}
2023-09-03 19:32:12 +00:00
2023-09-15 20:19:44 +00:00
public string GetAgentTemplate(string agentId, string templateName)
2023-09-03 19:32:12 +00:00
{
2023-09-15 20:19:44 +00:00
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
if (agent == null) return string.Empty;
return agent.Templates?.FirstOrDefault(x => x.Name == templateName.ToLower())?.Content ?? string.Empty;
2023-09-03 19:32:12 +00:00
}
2023-09-15 20:19:44 +00:00
#endregion
2023-09-03 22:43:50 +00:00
2023-09-15 20:19:44 +00:00
#region Conversation
2023-09-07 22:04:34 +00:00
public void CreateNewConversation(Conversation conversation)
2023-09-03 22:43:50 +00:00
{
if (conversation == null) return;
2023-09-06 04:41:49 +00:00
var conv = new ConversationCollection
2023-09-03 22:43:50 +00:00
{
2023-09-07 22:04:34 +00:00
Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(),
2023-09-03 22:43:50 +00:00
AgentId = Guid.Parse(conversation.AgentId),
2023-09-28 17:25:37 +00:00
UserId = !string.IsNullOrEmpty(conversation.UserId) && Guid.TryParse(conversation.UserId, out var _) ? Guid.Parse(conversation.UserId) : Guid.Empty,
2023-09-03 22:43:50 +00:00
Title = conversation.Title,
2023-09-09 21:44:25 +00:00
States = conversation.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
2023-09-06 04:41:49 +00:00
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow,
};
var dialog = new ConversationDialogCollection
{
Id = Guid.NewGuid(),
ConversationId = conv.Id,
2023-09-07 22:04:34 +00:00
Dialog = string.Empty
2023-09-03 22:43:50 +00:00
};
2023-09-06 04:41:49 +00:00
_dc.Conversations.InsertOne(conv);
_dc.ConversationDialogs.InsertOne(dialog);
2023-09-03 22:43:50 +00:00
}
public string GetConversationDialog(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
2023-09-06 04:41:49 +00:00
var filter = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault();
if (foundDialog == null) return string.Empty;
2023-09-03 22:43:50 +00:00
2023-09-06 04:41:49 +00:00
return foundDialog.Dialog;
2023-09-03 22:43:50 +00:00
}
public void UpdateConversationDialog(string conversationId, string dialogs)
{
if (string.IsNullOrEmpty(conversationId)) return;
2023-09-07 22:04:34 +00:00
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConv = _dc.Conversations.Find(filterConv).FirstOrDefault();
if (foundConv == null) return;
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundDialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
2023-09-06 04:41:49 +00:00
if (foundDialog == null) return;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
var updateDialog = Builders<ConversationDialogCollection>.Update.Set(x => x.Dialog, dialogs);
var updateConv = Builders<ConversationCollection>.Update.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
_dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog);
_dc.Conversations.UpdateOne(filterConv, updateConv);
2023-09-03 22:43:50 +00:00
}
2023-09-09 21:44:25 +00:00
public List<StateKeyValue> GetConversationStates(string conversationId)
2023-09-03 22:43:50 +00:00
{
2023-09-09 21:44:25 +00:00
var states = new List<StateKeyValue>();
2023-09-03 22:43:50 +00:00
if (string.IsNullOrEmpty(conversationId)) return states;
2023-09-07 22:04:34 +00:00
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConversation = _dc.Conversations.Find(filter).FirstOrDefault();
2023-09-09 21:44:25 +00:00
var savedStates = foundConversation?.States ?? new List<StateKeyValue>();
2023-09-03 22:43:50 +00:00
return savedStates;
}
2023-09-09 21:44:25 +00:00
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
2023-09-03 22:43:50 +00:00
{
if (string.IsNullOrEmpty(conversationId)) return;
2023-09-07 22:04:34 +00:00
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConv = _dc.Conversations.Find(filter).FirstOrDefault();
if (foundConv == null) return;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
var update = Builders<ConversationCollection>.Update
.Set(x => x.States, states)
2023-09-03 22:43:50 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-07 22:04:34 +00:00
_dc.Conversations.UpdateOne(filter, update);
2023-09-03 22:43:50 +00:00
}
2023-09-07 22:04:34 +00:00
public Conversation GetConversation(string conversationId)
2023-09-03 22:43:50 +00:00
{
if (string.IsNullOrEmpty(conversationId)) return null;
2023-09-07 22:04:34 +00:00
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
2023-09-06 04:41:49 +00:00
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
2023-09-07 22:04:34 +00:00
var conv = _dc.Conversations.Find(filterConv).FirstOrDefault();
2023-09-06 04:41:49 +00:00
var dialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
if (conv == null) return null;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
return new Conversation
2023-09-15 20:19:44 +00:00
{
2023-09-06 04:41:49 +00:00
Id = conv.Id.ToString(),
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog?.Dialog ?? string.Empty,
2023-09-09 21:44:25 +00:00
States = new ConversationState(conv.States ?? new List<StateKeyValue>()),
2023-09-06 04:41:49 +00:00
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
};
2023-09-03 22:43:50 +00:00
}
2023-09-07 22:04:34 +00:00
public List<Conversation> GetConversations(string userId)
2023-09-03 22:43:50 +00:00
{
2023-09-07 22:04:34 +00:00
var records = new List<Conversation>();
2023-09-28 17:26:18 +00:00
if (string.IsNullOrEmpty(userId) || !Guid.TryParse(userId, out var _)) return records;
2023-09-03 22:43:50 +00:00
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, Guid.Parse(userId));
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
2023-09-06 04:41:49 +00:00
foreach (var conv in conversations)
2023-09-03 22:43:50 +00:00
{
2023-09-06 04:41:49 +00:00
var convId = conv.Id.ToString();
2023-09-07 22:04:34 +00:00
records.Add(new Conversation
2023-09-06 04:41:49 +00:00
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});
}
return records;
2023-09-03 22:43:50 +00:00
}
2023-09-15 20:19:44 +00:00
#endregion
2023-09-12 22:08:58 +00:00
2023-09-15 20:19:44 +00:00
#region User
public User GetUserByEmail(string email)
2023-09-12 22:08:58 +00:00
{
2023-09-15 20:19:44 +00:00
var user = Users.FirstOrDefault(x => x.Email == email);
return user != null ? new User
{
Id = user.Id.ToString(),
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password,
Salt = user.Salt,
ExternalId = user.ExternalId,
CreatedTime = user.CreatedTime,
UpdatedTime = user.UpdatedTime
} : null;
}
2023-09-12 22:08:58 +00:00
2023-09-15 20:19:44 +00:00
public void CreateUser(User user)
{
if (user == null) return;
var userCollection = new UserCollection
{
Id = Guid.NewGuid(),
FirstName = user.FirstName,
LastName = user.LastName,
Salt = user.Salt,
Password = user.Password,
Email = user.Email,
ExternalId = user.ExternalId,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
_dc.Users.InsertOne(userCollection);
}
#endregion
2023-08-26 04:41:01 +00:00
}