remove record reference

This commit is contained in:
Jicheng Lu 2023-09-07 17:04:34 -05:00
parent 5b543e6f8b
commit 45ed8d6cfe
35 changed files with 354 additions and 487 deletions

View file

@ -4,7 +4,7 @@ public class Agent
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public string Description { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
@ -35,9 +35,31 @@ public class Agent
public bool IsPublic { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
public override string ToString()
=> $"{Name} {Id}";
public static Agent Clone(Agent agent)
{
return new Agent
{
Id = agent.Id,
Name = agent.Name,
Description = agent.Description,
Instruction = agent.Instruction,
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Knowledges = agent.Knowledges,
IsPublic = agent.IsPublic,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime,
};
}
public Agent SetInstruction(string instruction)
{
Instruction = instruction;
@ -55,4 +77,28 @@ public class Agent
Responses = responses ?? new List<string>(); ;
return this;
}
public Agent SetId(string id)
{
Id = id;
return this;
}
public Agent SetName(string name)
{
Name = name;
return this;
}
public Agent SetDescription(string description)
{
Description = description;
return this;
}
public Agent SetIsPublic(bool isPublic)
{
IsPublic = isPublic;
return this;
}
}

View file

@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Agents.Models;
public class UserAgent
{
public string Id { get; set; } = string.Empty;
public string UserId { get; set; } = string.Empty;
public string AgentId { get; set; } = string.Empty;
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}

View file

@ -7,7 +7,7 @@ public class Conversation
public string UserId { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Dialog { get; set; } = string.Empty;
public ConversationState State { get; set; }
public ConversationState States { get; set; }
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;

View file

@ -1,41 +1,40 @@
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Repositories.Records;
using BotSharp.Abstraction.Routing.Models;
using System.Linq;
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Abstraction.Repositories;
public interface IBotSharpRepository
{
IQueryable<UserRecord> User { get; }
IQueryable<AgentRecord> Agent { get; }
IQueryable<UserAgentRecord> UserAgent { get; }
IQueryable<ConversationRecord> Conversation { get; }
IQueryable<RoutingItemRecord> RoutingItem { get; }
IQueryable<RoutingProfileRecord> RoutingProfile { get; }
IQueryable<User> Users { get; }
IQueryable<Agent> Agents { get; }
IQueryable<UserAgent> UserAgents { get; }
IQueryable<Conversation> Conversations { get; }
IQueryable<RoutingItem> RoutingItems { get; }
IQueryable<RoutingProfile> RoutingProfiles { get; }
int Transaction<TTableInterface>(Action action);
void Add<TTableInterface>(object entity);
UserRecord GetUserByEmail(string email);
void CreateUser(UserRecord user);
void UpdateAgent(AgentRecord agent);
User GetUserByEmail(string email);
void CreateUser(User user);
void UpdateAgent(Agent agent);
List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems);
List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles);
List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems);
List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles);
void DeleteRoutingItems();
void DeleteRoutingProfiles();
AgentRecord GetAgent(string agentId);
Agent GetAgent(string agentId);
List<string> GetAgentResponses(string agentId);
void CreateNewConversation(ConversationRecord conversation);
void CreateNewConversation(Conversation conversation);
string GetConversationDialog(string conversationId);
void UpdateConversationDialog(string conversationId, string dialogs);
List<KeyValueModel> GetConversationState(string conversationId);
void UpdateConversationState(string conversationId, List<KeyValueModel> state);
List<KeyValueModel> GetConversationStates(string conversationId);
void UpdateConversationStates(string conversationId, List<KeyValueModel> states);
ConversationRecord GetConversation(string conversationId);
List<ConversationRecord> GetConversations(string userId);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(string userId);
}

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Utilities;
namespace BotSharp.Abstraction.Repositories.Records;
public class AgentRecord : RecordBase
@ -23,59 +25,4 @@ public class AgentRecord : RecordBase
[Required]
public DateTime UpdatedTime { get; set; }
public static AgentRecord FromAgent(Agent agent)
{
return new AgentRecord
{
Id = agent.Id,
Name = agent.Name,
Description = agent.Description,
Instruction = agent.Instruction,
Functions = agent.Functions,
Responses = agent.Responses,
IsPublic = agent.IsPublic
};
}
public Agent ToAgent()
{
return new Agent
{
Id = Id,
Name = Name,
Description = Description,
Instruction = Instruction,
Functions = Functions,
Responses = Responses,
IsPublic = IsPublic,
CreatedDateTime = CreatedTime,
UpdatedDateTime = UpdatedTime
};
}
public AgentRecord SetId(string id)
{
Id = id;
return this;
}
public AgentRecord SetInstruction(string instruction)
{
Instruction = instruction;
return this;
}
public AgentRecord SetFunctions(List<string> functions)
{
Functions = functions;
return this;
}
public AgentRecord SetResponses(List<string> responses)
{
Responses = responses;
return this;
}
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Repositories.Models;
using System.Text.Json.Serialization;
@ -8,11 +7,11 @@ public class ConversationRecord : RecordBase
{
[Required]
[MaxLength(36)]
public string AgentId { get; set; } = string.Empty;
public Guid AgentId { get; set; } = Guid.Empty;
[Required]
[MaxLength(36)]
public string UserId { get; set; } = string.Empty;
public Guid UserId { get; set; } = Guid.Empty;
[MaxLength(64)]
public string Title { get; set; } = string.Empty;
@ -21,41 +20,11 @@ public class ConversationRecord : RecordBase
public string Dialog { get; set; }
[JsonIgnore]
public List<KeyValueModel> State { get; set; }
public List<KeyValueModel> States { get; set; }
[Required]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
[Required]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static ConversationRecord FromConversation(Conversation conv)
{
return new ConversationRecord
{
AgentId = conv.AgentId,
UserId = conv.UserId,
Id = conv.Id,
Title = conv.Title,
Dialog = conv.Dialog,
State = conv.State?.Select(x => new KeyValueModel(x.Key, x.Value))?.ToList() ?? new List<KeyValueModel>(),
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
};
}
public Conversation ToConversation()
{
return new Conversation
{
Id = Id,
Title = Title,
UserId = UserId,
AgentId = AgentId,
Dialog = Dialog,
State = new ConversationState(State),
CreatedTime = CreatedTime,
UpdatedTime = UpdatedTime
};
}
}

View file

@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction.Repositories.Records;
namespace BotSharp.Abstraction.Repositories.Records
public class RecordBase
{
public class RecordBase
{
public string? Id { get; set; }
}
public Guid Id { get; set; } = Guid.Empty;
}

View file

@ -1,39 +1,14 @@
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Utilities;
namespace BotSharp.Abstraction.Repositories.Records;
public class RoutingItemRecord : RecordBase
{
public string AgentId { get; set; }
public Guid AgentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<string> RequiredFields { get; set; } = new List<string>();
public string? RedirectTo { get; set; }
public bool Disabled { get; set; }
public RoutingItem ToRoutingItem()
{
return new RoutingItem
{
AgentId = AgentId,
Name = Name,
Description = Description,
RequiredFields = RequiredFields,
RedirectTo = RedirectTo,
Disabled = Disabled
};
}
public static RoutingItemRecord FromRoutingItem(RoutingItem item)
{
return new RoutingItemRecord
{
AgentId = item.AgentId,
Name = item.Name,
Description = item.Description,
RequiredFields = item.RequiredFields,
RedirectTo = item.RedirectTo,
Disabled = item.Disabled
};
}
}

View file

@ -6,22 +6,4 @@ public class RoutingProfileRecord : RecordBase
{
public string Name { get; set; }
public List<string> AgentIds { get; set; }
public RoutingProfile ToRoutingProfile()
{
return new RoutingProfile
{
Name = Name,
AgentIds = AgentIds.ToArray(),
};
}
public static RoutingProfileRecord FromRoutingProfile(RoutingProfile profile)
{
return new RoutingProfileRecord
{
Name = profile.Name,
AgentIds = profile.AgentIds.ToList(),
};
}
}

View file

@ -17,26 +17,4 @@ public class UserAgentRecord : RecordBase
[Required]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static UserRecord FromUser(User user)
{
return new UserRecord
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password
};
}
public User ToUser()
{
return new User
{
Id = Id,
CreatedTime = CreatedTime,
UpdatedTime = UpdatedTime
};
}
}

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Abstraction.Repositories.Records;
public class UserRecord : RecordBase
@ -32,31 +30,4 @@ public class UserRecord : RecordBase
[Required]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static UserRecord FromUser(User user)
{
return new UserRecord
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password
};
}
public User ToUser()
{
return new User
{
Id = Id,
FirstName = FirstName,
LastName = LastName,
Email = Email,
Salt = Salt,
Password = Password,
CreatedTime = CreatedTime,
UpdatedTime = UpdatedTime
};
}
}

View file

@ -4,6 +4,8 @@ namespace BotSharp.Abstraction.Routing.Models;
public class RoutingItem
{
public string Id { get; set; }
[JsonPropertyName("agent_id")]
public string AgentId { get; set; }

View file

@ -4,9 +4,11 @@ namespace BotSharp.Abstraction.Routing.Models;
public class RoutingProfile
{
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("agent_ids")]
public string[] AgentIds { get; set; }
public List<string> AgentIds { get; set; }
}

View file

@ -8,6 +8,7 @@ public class User
public string Email { get; set; } = string.Empty;
public string Salt { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string? ExternalId { get; set; }
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Utilities;
public static class GuidExtensitions
{
public static Guid IfNullOrEmptyAsDefault(this string str)
=> string.IsNullOrEmpty(str) ? Guid.Empty : Guid.Parse(str);
}

View file

@ -1,13 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
using BotSharp.Abstraction.Users.Models;
using MongoDB.Bson;
using System.IO;
using Tensorflow;
using static Tensorflow.TensorShapeProto.Types;
namespace BotSharp.Core.Agents.Services;
@ -17,21 +10,21 @@ public partial class AgentService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = (from a in db.Agent
join ua in db.UserAgent on a.Id equals ua.AgentId
join u in db.User on ua.UserId equals u.Id
var agentRecord = (from a in db.Agents
join ua in db.UserAgents on a.Id equals ua.AgentId
join u in db.Users on ua.UserId equals u.Id
where u.ExternalId == _user.Id && a.Name == agent.Name
select a).FirstOrDefault();
if (record != null)
if (agentRecord != null)
{
return record.ToAgent();
return agentRecord;
}
record = AgentRecord.FromAgent(agent);
record.Id = Guid.NewGuid().ToString();
record.CreatedTime = DateTime.UtcNow;
record.UpdatedTime = DateTime.UtcNow;
agentRecord = Agent.Clone(agent);
agentRecord.Id = Guid.NewGuid().ToString();
agentRecord.CreatedTime = DateTime.UtcNow;
agentRecord.UpdatedTime = DateTime.UtcNow;
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
@ -40,29 +33,32 @@ public partial class AgentService
if (foundAgent != null)
{
record.SetId(foundAgent.Id)
.SetInstruction(foundAgent.Instruction)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
agentRecord.SetId(foundAgent.Id)
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetInstruction(foundAgent.Instruction)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
}
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var userAgentRecord = new UserAgentRecord
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var userAgentRecord = new UserAgent
{
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AgentId = foundAgent?.Id ?? record.Id,
AgentId = foundAgent?.Id ?? agentRecord.Id,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IBotSharpTable>(record);
db.Add<IBotSharpTable>(agentRecord);
db.Add<IBotSharpTable>(userAgentRecord);
});
return record.ToAgent();
return agentRecord;
}
private JsonSerializerOptions _options = new JsonSerializerOptions
@ -83,7 +79,9 @@ public partial class AgentService
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
return agent.SetInstruction(instruction).SetFunctions(functions).SetResponses(responses);
return agent.SetInstruction(instruction)
.SetFunctions(functions)
.SetResponses(responses);
}
}

View file

@ -9,18 +9,18 @@ public partial class AgentService
public async Task<List<Agent>> GetAgents()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var query = from a in db.Agent
join ua in db.UserAgent on a.Id equals ua.AgentId
join u in db.User on ua.UserId equals u.Id
var query = from a in db.Agents
join ua in db.UserAgents on a.Id equals ua.AgentId
join u in db.Users on ua.UserId equals u.Id
where ua.UserId == _user.Id || u.ExternalId == _user.Id || a.IsPublic
select a.ToAgent();
select a;
return query.ToList();
}
public async Task<Agent> GetAgent(string id)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var profile = db.GetAgent(id)?.ToAgent();
var profile = db.GetAgent(id);
var instructionFile = profile?.Instruction;
if (instructionFile != null)

View file

@ -10,9 +10,9 @@ public partial class AgentService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = (from a in db.Agent
join ua in db.UserAgent on a.Id equals ua.AgentId
join u in db.User on ua.UserId equals u.Id
var record = (from a in db.Agents
join ua in db.UserAgents on a.Id equals ua.AgentId
join u in db.Users on ua.UserId equals u.Id
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
a.Id == agent.Id
select a).FirstOrDefault();

View file

@ -40,15 +40,15 @@ public partial class ConversationService : IConversationService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var conversation = db.GetConversation(id);
return conversation?.ToConversation();
return conversation;
}
public async Task<List<Conversation>> GetConversations()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var conversations = db.GetConversations(user?.Id);
return conversations.Select(x => x.ToConversation()).OrderByDescending(x => x.CreatedTime).ToList();
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
}
public async Task<Conversation> NewConversation(Conversation sess)
@ -56,16 +56,16 @@ public partial class ConversationService : IConversationService
var db = _services.GetRequiredService<IBotSharpRepository>();
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var foundUserId = user?.Id ?? _user.Id;
var record = ConversationRecord.FromConversation(sess);
var record = sess;
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId);
record.Title = "New Conversation";
db.CreateNewConversation(record);
return record.ToConversation();
return record;
}
public Task CleanHistory(string agentId)

View file

@ -53,7 +53,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
return _states;
}
_savedStates = _db.GetConversationState(_conversationId);
_savedStates = _db.GetConversationStates(_conversationId);
if (!_savedStates.IsNullOrEmpty())
{
@ -88,7 +88,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
states.Add(new KeyValueModel(dic.Key, dic.Value));
}
_db.UpdateConversationState(_conversationId, states);
_db.UpdateConversationStates(_conversationId, states);
_logger.LogInformation($"Saved state {_conversationId}");
}

View file

@ -43,7 +43,7 @@ public class ConversationStorage : IConversationStorage
}
else
{
var agent = db.Agent.First(x => x.Id == agentId);
var agent = db.Agents.First(x => x.Id == agentId);
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agent.Name}|");
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();

View file

@ -1,33 +1,26 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Repositories.Records;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace BotSharp.Core.Repository;
public class BotSharpDbContext : Database, IBotSharpRepository
{
public IQueryable<UserRecord> User => Table<UserRecord>();
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();
public IQueryable<UserAgentRecord> UserAgent => Table<UserAgentRecord>();
public IQueryable<ConversationRecord> Conversation => Table<ConversationRecord>();
public IQueryable<RoutingItemRecord> RoutingItem => throw new NotImplementedException();
public IQueryable<RoutingProfileRecord> RoutingProfile => throw new NotImplementedException();
public IQueryable<User> Users => throw new NotImplementedException();
public void UpdateAgent(AgentRecord agent)
{
throw new NotImplementedException();
}
public IQueryable<Agent> Agents => throw new NotImplementedException();
public void CreateUser(UserRecord user)
{
throw new NotImplementedException();
}
public IQueryable<UserAgent> UserAgents => throw new NotImplementedException();
public IQueryable<Conversation> Conversations => throw new NotImplementedException();
public IQueryable<RoutingItem> RoutingItems => throw new NotImplementedException();
public IQueryable<RoutingProfile> RoutingProfiles => throw new NotImplementedException();
public UserRecord GetUserByEmail(string email)
{
throw new NotImplementedException();
}
public int Transaction<TTableInterface>(Action action)
{
@ -78,6 +71,28 @@ public class BotSharpDbContext : Database, IBotSharpRepository
}
}
public void CreateNewConversation(Conversation conversation)
{
throw new NotImplementedException();
}
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
{
throw new NotImplementedException();
}
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
{
throw new NotImplementedException();
}
public void CreateUser(User user)
{
throw new NotImplementedException();
}
public void DeleteRoutingItems()
{
throw new NotImplementedException();
@ -88,12 +103,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
{
throw new NotImplementedException();
}
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
public Agent GetAgent(string agentId)
{
throw new NotImplementedException();
}
@ -103,12 +113,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public AgentRecord GetAgent(string agentId)
{
throw new NotImplementedException();
}
public void CreateNewConversation(ConversationRecord conversation)
public Conversation GetConversation(string conversationId)
{
throw new NotImplementedException();
}
@ -118,27 +123,32 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public List<Conversation> GetConversations(string userId)
{
throw new NotImplementedException();
}
public List<KeyValueModel> GetConversationStates(string conversationId)
{
throw new NotImplementedException();
}
public User GetUserByEmail(string email)
{
throw new NotImplementedException();
}
public void UpdateAgent(Agent agent)
{
throw new NotImplementedException();
}
public void UpdateConversationDialog(string conversationId, string dialogs)
{
throw new NotImplementedException();
}
public List<KeyValueModel> GetConversationState(string conversationId)
{
throw new NotImplementedException();
}
public void UpdateConversationState(string conversationId, List<KeyValueModel> state)
{
throw new NotImplementedException();
}
public ConversationRecord GetConversation(string conversationId)
{
throw new NotImplementedException();
}
public List<ConversationRecord> GetConversations(string userId)
public void UpdateConversationStates(string conversationId, List<KeyValueModel> states)
{
throw new NotImplementedException();
}

View file

@ -9,6 +9,9 @@ using System.IO;
using static Tensorflow.TensorShapeProto.Types;
using Tensorflow;
using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Repository;
@ -39,8 +42,8 @@ public class FileRepository : IBotSharpRepository
};
}
private List<UserRecord> _users;
public IQueryable<UserRecord> User
private List<User> _users;
public IQueryable<User> Users
{
get
{
@ -50,18 +53,18 @@ public class FileRepository : IBotSharpRepository
}
var dir = Path.Combine(_dbSettings.FileRepository, "users");
_users = new List<UserRecord>();
_users = new List<User>();
foreach (var d in Directory.GetDirectories(dir))
{
var json = File.ReadAllText(Path.Combine(d, "user.json"));
_users.Add(JsonSerializer.Deserialize<UserRecord>(json, _options));
_users.Add(JsonSerializer.Deserialize<User>(json, _options));
}
return _users.AsQueryable();
}
}
private List<AgentRecord> _agents;
public IQueryable<AgentRecord> Agent
private List<Agent> _agents;
public IQueryable<Agent> Agents
{
get
{
@ -72,18 +75,18 @@ public class FileRepository : IBotSharpRepository
//var agentSettings = _services.GetService<AgentSettings>();
var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
_agents = new List<AgentRecord>();
_agents = new List<Agent>();
foreach (var d in Directory.GetDirectories(dir))
{
var json = File.ReadAllText(Path.Combine(d, "agent.json"));
_agents.Add(JsonSerializer.Deserialize<AgentRecord>(json, _options));
_agents.Add(JsonSerializer.Deserialize<Agent>(json, _options));
}
return _agents.AsQueryable();
}
}
private List<UserAgentRecord> _userAgents;
public IQueryable<UserAgentRecord> UserAgent
private List<UserAgent> _userAgents;
public IQueryable<UserAgent> UserAgents
{
get
{
@ -93,22 +96,22 @@ public class FileRepository : IBotSharpRepository
}
var dir = Path.Combine(_dbSettings.FileRepository, "users");
_userAgents = new List<UserAgentRecord>();
_userAgents = new List<UserAgent>();
foreach (var d in Directory.GetDirectories(dir))
{
var file = Path.Combine(d, "agents.json");
if (Directory.Exists(d) && File.Exists(file))
{
var json = File.ReadAllText(file);
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgentRecord>>(json, _options));
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgent>>(json, _options));
}
}
return _userAgents.AsQueryable();
}
}
private List<ConversationRecord> _conversations;
public IQueryable<ConversationRecord> Conversation
private List<Conversation> _conversations;
public IQueryable<Conversation> Conversations
{
get
{
@ -119,22 +122,22 @@ public class FileRepository : IBotSharpRepository
//var convSettings = _services.GetService<ConversationSetting>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir);
_conversations = new List<ConversationRecord>();
_conversations = new List<Conversation>();
foreach (var d in Directory.GetDirectories(dir))
{
var path = Path.Combine(d, "conversation.json");
if (File.Exists(path))
{
var json = File.ReadAllText(path);
_conversations.Add(JsonSerializer.Deserialize<ConversationRecord>(json, _options));
_conversations.Add(JsonSerializer.Deserialize<Conversation>(json, _options));
}
}
return _conversations.AsQueryable();
}
}
private List<RoutingItemRecord> _routingItems;
public IQueryable<RoutingItemRecord> RoutingItem
private List<RoutingItem> _routingItems;
public IQueryable<RoutingItem> RoutingItems
{
get
{
@ -143,21 +146,21 @@ public class FileRepository : IBotSharpRepository
return _routingItems.AsQueryable();
}
_routingItems = new List<RoutingItemRecord>();
_routingItems = new List<RoutingItem>();
//var agentSettings = _services.GetRequiredService<AgentSettings>();
//var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "route.json");
if (File.Exists(filePath))
{
_routingItems = JsonSerializer.Deserialize<List<RoutingItemRecord>>(File.ReadAllText(filePath));
_routingItems = JsonSerializer.Deserialize<List<RoutingItem>>(File.ReadAllText(filePath));
}
return _routingItems.AsQueryable();
}
}
private List<RoutingProfileRecord> _routingProfiles;
public IQueryable<RoutingProfileRecord> RoutingProfile
private List<RoutingProfile> _routingProfiles;
public IQueryable<RoutingProfile> RoutingProfiles
{
get
{
@ -166,13 +169,13 @@ public class FileRepository : IBotSharpRepository
return _routingProfiles.AsQueryable();
}
_routingProfiles = new List<RoutingProfileRecord>();
_routingProfiles = new List<RoutingProfile>();
//var agentSettings = _services.GetRequiredService<AgentSettings>();
//var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "routing-profile.json");
if (File.Exists(filePath))
{
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfileRecord>>(File.ReadAllText(filePath));
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath));
}
return _routingProfiles.AsQueryable();
@ -181,26 +184,25 @@ public class FileRepository : IBotSharpRepository
public void Add<TTableInterface>(object entity)
{
_conversations = Conversation.ToList();
if (entity is ConversationRecord conversation)
if (entity is Conversation conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(ConversationRecord));
_changedTableNames.Add(nameof(Conversation));
}
else if (entity is AgentRecord agent)
else if (entity is Agent agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(AgentRecord));
_changedTableNames.Add(nameof(Agent));
}
else if (entity is UserRecord user)
else if (entity is User user)
{
_users.Add(user);
_changedTableNames.Add(nameof(UserRecord));
_changedTableNames.Add(nameof(User));
}
else if (entity is UserAgentRecord userAgent)
else if (entity is UserAgent userAgent)
{
_userAgents.Add(userAgent);
_changedTableNames.Add(nameof(UserAgentRecord));
_changedTableNames.Add(nameof(UserAgent));
}
}
@ -213,7 +215,7 @@ public class FileRepository : IBotSharpRepository
// Persist to disk
foreach (var table in _changedTableNames)
{
if (table == nameof(ConversationRecord))
if (table == nameof(Conversation))
{
//var convSettings = _services.GetService<ConversationSetting>();
@ -230,7 +232,7 @@ public class FileRepository : IBotSharpRepository
File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options));
}
}
else if (table == nameof(AgentRecord))
else if (table == nameof(Agent))
{
//var agentSettings = _services.GetService<AgentSettings>();
@ -247,7 +249,7 @@ public class FileRepository : IBotSharpRepository
File.WriteAllText(path, JsonSerializer.Serialize(agent, _options));
}
}
else if (table == nameof(UserRecord))
else if (table == nameof(User))
{
foreach (var user in _users)
{
@ -262,7 +264,7 @@ public class FileRepository : IBotSharpRepository
File.WriteAllText(path, JsonSerializer.Serialize(user, _options));
}
}
else if (table == nameof(UserAgentRecord))
else if (table == nameof(UserAgent))
{
_userAgents.GroupBy(x => x.UserId)
.Select(x => x.Key).ToList()
@ -282,12 +284,12 @@ public class FileRepository : IBotSharpRepository
return _changedTableNames.Count;
}
public UserRecord GetUserByEmail(string email)
public User GetUserByEmail(string email)
{
return User.FirstOrDefault(x => x.Email == email);
return Users.FirstOrDefault(x => x.Email == email);
}
public void CreateUser(UserRecord user)
public void CreateUser(User user)
{
var userId = Guid.NewGuid().ToString();
var dir = Path.Combine(_dbSettings.FileRepository, "users", userId);
@ -299,7 +301,7 @@ public class FileRepository : IBotSharpRepository
File.WriteAllText(path, JsonSerializer.Serialize(user, _options));
}
public void UpdateAgent(AgentRecord agent)
public void UpdateAgent(Agent agent)
{
if (agent == null) return;
@ -348,12 +350,12 @@ public class FileRepository : IBotSharpRepository
throw new NotImplementedException();
}
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
{
throw new NotImplementedException();
}
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
{
throw new NotImplementedException();
}
@ -372,13 +374,13 @@ public class FileRepository : IBotSharpRepository
return responses;
}
public AgentRecord GetAgent(string agentId)
public Agent GetAgent(string agentId)
{
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
foreach (var dir in Directory.GetDirectories(agentDir))
{
var json = File.ReadAllText(Path.Combine(dir, "agent.json"));
var record = JsonSerializer.Deserialize<AgentRecord>(json, _options);
var record = JsonSerializer.Deserialize<Agent>(json, _options);
if (record != null && record.Id == agentId)
{
var instruction = FetchInstruction(dir);
@ -409,7 +411,7 @@ public class FileRepository : IBotSharpRepository
return functions;
}
public void CreateNewConversation(ConversationRecord conversation)
public void CreateNewConversation(Conversation conversation)
{
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir, conversation.Id);
if (!Directory.Exists(dir))
@ -466,7 +468,7 @@ public class FileRepository : IBotSharpRepository
return;
}
public List<KeyValueModel> GetConversationState(string conversationId)
public List<KeyValueModel> GetConversationStates(string conversationId)
{
var curStates = new List<KeyValueModel>();
var convDir = FindConversationDirectory(conversationId);
@ -497,7 +499,7 @@ public class FileRepository : IBotSharpRepository
if (!File.Exists(path)) continue;
var json = File.ReadAllText(path);
var conv = JsonSerializer.Deserialize<ConversationRecord>(json, _options);
var conv = JsonSerializer.Deserialize<Conversation>(json, _options);
if (conv != null && conv.Id == conversationId)
{
return d;
@ -507,7 +509,7 @@ public class FileRepository : IBotSharpRepository
return null;
}
public void UpdateConversationState(string conversationId, List<KeyValueModel> state)
public void UpdateConversationStates(string conversationId, List<KeyValueModel> states)
{
var localStates = new List<string>();
var convDir = FindConversationDirectory(conversationId);
@ -516,7 +518,7 @@ public class FileRepository : IBotSharpRepository
var stateDir = Path.Combine(convDir, "state.dict");
if (File.Exists(stateDir))
{
foreach (var data in state)
foreach (var data in states)
{
localStates.Add($"{data.Key}={data.Value}");
}
@ -525,13 +527,13 @@ public class FileRepository : IBotSharpRepository
}
}
public ConversationRecord GetConversation(string conversationId)
public Conversation GetConversation(string conversationId)
{
var convDir = FindConversationDirectory(conversationId);
if (!string.IsNullOrEmpty(convDir))
{
var convFile = Path.Combine(convDir, "conversation.json");
var record = JsonSerializer.Deserialize<ConversationRecord>(convFile);
var record = JsonSerializer.Deserialize<Conversation>(convFile);
var dialogFile = Path.Combine(convDir, "dialogs.txt");
if (record != null && File.Exists(dialogFile))
@ -543,7 +545,7 @@ public class FileRepository : IBotSharpRepository
if (record != null && File.Exists(stateFile))
{
var states = File.ReadLines(stateFile);
record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList();
//record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do
}
return record;
@ -552,9 +554,9 @@ public class FileRepository : IBotSharpRepository
return null;
}
public List<ConversationRecord> GetConversations(string userId)
public List<Conversation> GetConversations(string userId)
{
var records = new List<ConversationRecord>();
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSetting.DataDir);
foreach (var d in Directory.GetDirectories(dir))
@ -563,7 +565,7 @@ public class FileRepository : IBotSharpRepository
if (!File.Exists(path)) continue;
var json = File.ReadAllText(path);
var record = JsonSerializer.Deserialize<ConversationRecord>(json, _options);
var record = JsonSerializer.Deserialize<Conversation>(json, _options);
if (record != null && record.UserId == userId)
{
var dialogFile = Path.Combine(d, "dialogs.txt");
@ -576,7 +578,7 @@ public class FileRepository : IBotSharpRepository
if (File.Exists(stateFile))
{
var states = File.ReadLines(stateFile);
record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList();
//record.State = states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList(); // to do
}
records.Add(record);

View file

@ -33,8 +33,8 @@ public class Router : IAgentRouting
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var records = db.RoutingItem.Select(x => x.ToRoutingItem()).ToArray();
var profiles = db.RoutingProfile.ToList();
var records = db.RoutingItems.ToArray();
var profiles = db.RoutingProfiles.ToList();
if (!profiles.IsNullOrEmpty())
{

View file

@ -16,17 +16,17 @@ public class RoutingService : IRoutingService
public async Task<List<RoutingItem>> CreateRoutingItems(List<RoutingItem> routingItems)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var items = routingItems?.Select(x => RoutingItemRecord.FromRoutingItem(x))?.ToList() ?? new List<RoutingItemRecord>();
var items = routingItems?.ToList() ?? new List<RoutingItem>();
var savedItems = db.CreateRoutingItems(items);
return await Task.FromResult(savedItems.Select(x => x.ToRoutingItem()).ToList());
return await Task.FromResult(savedItems.ToList());
}
public async Task<List<RoutingProfile>> CreateRoutingProfiles(List<RoutingProfile> routingProfiles)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var profiles = routingProfiles?.Select(x => RoutingProfileRecord.FromRoutingProfile(x))?.ToList() ?? new List<RoutingProfileRecord>();
var profiles = routingProfiles?.ToList() ?? new List<RoutingProfile>();
var savedProfiles = db.CreateRoutingProfiles(profiles);
return await Task.FromResult(savedProfiles.Select(x => x.ToRoutingProfile()).ToList());
return await Task.FromResult(savedProfiles.ToList());
}
public async Task DeleteRoutingItems()

View file

@ -25,17 +25,17 @@ public class UserService : IUserService
var record = db.GetUserByEmail(user.Email);
if (record != null)
{
return record.ToUser();
return record;
}
record = UserRecord.FromUser(user);
record = user;
record.Email = user.Email.ToLower();
record.Salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashText(user.Password, record.Salt);
record.ExternalId = _user.Id;
db.CreateUser(record);
return record.ToUser();
return record;
}
public async Task<Token> GetToken(string authorization)
@ -44,7 +44,7 @@ public class UserService : IUserService
var (userEmail, password) = base64.SplitAsTuple(":");
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.User.FirstOrDefault(x => x.Email == userEmail);
var record = db.Users.FirstOrDefault(x => x.Email == userEmail);
if (record == null)
{
return default;
@ -66,7 +66,7 @@ public class UserService : IUserService
};
}
private string GenerateJwtToken(UserRecord user)
private string GenerateJwtToken(User user)
{
var config = _services.GetRequiredService<IConfiguration>();
var issuer = config["Jwt:Issuer"];
@ -97,7 +97,7 @@ public class UserService : IUserService
public async Task<User> GetMyProfile()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = (from u in db.User
var user = (from u in db.Users
where u.ExternalId == _user.Id
select new User
{

View file

@ -2,7 +2,6 @@ using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.OpenAPI.ViewModels.Routing;
using NetTopologySuite.Index.HPRtree;
namespace BotSharp.OpenAPI.Controllers;

View file

@ -4,6 +4,7 @@ namespace BotSharp.OpenAPI.ViewModels.Routing;
public class RoutingItemViewModel
{
public string Id { get; set; }
public string AgentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
@ -15,6 +16,7 @@ public class RoutingItemViewModel
{
return new RoutingItemViewModel
{
Id = routingItem.Id,
AgentId = routingItem.AgentId,
Name = routingItem.Name,
Description = routingItem.Description,

View file

@ -5,7 +5,7 @@ namespace BotSharp.OpenAPI.ViewModels.Routing;
public class RoutingProfileCreationModel
{
public string Name { get; set; }
public string[] AgentIds { get; set; }
public List<string> AgentIds { get; set; }
public RoutingProfile ToRoutingProfile()
{

View file

@ -4,13 +4,15 @@ namespace BotSharp.OpenAPI.ViewModels.Routing;
public class RoutingProfileViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string[] AgentIds { get; set; }
public List<string> AgentIds { get; set; }
public static RoutingProfileViewModel FromRoutingProfile(RoutingProfile profile)
{
return new RoutingProfileViewModel
{
Id = profile.Id,
Name = profile.Name,
AgentIds = profile.AgentIds,
};

View file

@ -7,7 +7,7 @@ public class ConversationCollection : MongoBase
public Guid AgentId { get; set; }
public Guid UserId { get; set; }
public string Title { get; set; }
public List<KeyValueModel> States { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -4,6 +4,4 @@ public class ConversationDialogCollection : MongoBase
{
public Guid ConversationId { get; set; }
public string Dialog { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -1,11 +0,0 @@
using BotSharp.Abstraction.Repositories.Models;
namespace BotSharp.Plugin.MongoStorage.Collections;
public class ConversationStatesCollection : MongoBase
{
public Guid ConversationId { get; set; }
public List<KeyValueModel> State { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -34,9 +34,6 @@ public class MongoDbContext
public IMongoCollection<ConversationDialogCollection> ConversationDialogs
=> Database.GetCollection<ConversationDialogCollection>("OneBrainConversationDialogs");
public IMongoCollection<ConversationStatesCollection> ConversationStates
=> Database.GetCollection<ConversationStatesCollection>("OneBrainConversationStates");
public IMongoCollection<UserCollection> Users
=> Database.GetCollection<UserCollection>("OneBrainUsers");

View file

@ -1,6 +1,8 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Plugin.MongoStorage.Collections;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -21,8 +23,8 @@ public class MongoRepository : IBotSharpRepository
};
}
private List<AgentRecord> _agents;
public IQueryable<AgentRecord> Agent
private List<Agent> _agents;
public IQueryable<Agent> Agents
{
get
{
@ -32,7 +34,7 @@ public class MongoRepository : IBotSharpRepository
}
var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List<AgentCollection>();
_agents = agentDocs.Select(x => new AgentRecord
_agents = agentDocs.Select(x => new Agent
{
Id = x.Id.ToString(),
Name = x.Name,
@ -49,8 +51,8 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<UserRecord> _users;
public IQueryable<UserRecord> User
private List<User> _users;
public IQueryable<User> Users
{
get
{
@ -60,7 +62,7 @@ public class MongoRepository : IBotSharpRepository
}
var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List<UserCollection>();
_users = userDocs.Select(x => new UserRecord
_users = userDocs.Select(x => new User
{
Id = x.Id.ToString(),
FirstName = x.FirstName,
@ -77,8 +79,8 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<UserAgentRecord> _userAgents;
public IQueryable<UserAgentRecord> UserAgent
private List<UserAgent> _userAgents;
public IQueryable<UserAgent> UserAgents
{
get
{
@ -88,7 +90,7 @@ public class MongoRepository : IBotSharpRepository
}
var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List<UserAgentCollection>();
_userAgents = userDocs.Select(x => new UserAgentRecord
_userAgents = userDocs.Select(x => new UserAgent
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
@ -101,8 +103,8 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<ConversationRecord> _conversations;
public IQueryable<ConversationRecord> Conversation
private List<Conversation> _conversations;
public IQueryable<Conversation> Conversations
{
get
{
@ -111,22 +113,22 @@ public class MongoRepository : IBotSharpRepository
return _conversations.AsQueryable();
}
_conversations = new List<ConversationRecord>();
_conversations = new List<Conversation>();
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
foreach (var conv in conversationDocs)
{
var convId = conv.Id.ToString();
var dialog = GetConversationDialog(convId);
var states = GetConversationState(convId);
_conversations.Add(new ConversationRecord
var states = GetConversationStates(convId);
_conversations.Add(new Conversation
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog,
State = states,
//State = states, to do
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});
@ -136,8 +138,8 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<RoutingItemRecord> _routingItems;
public IQueryable<RoutingItemRecord> RoutingItem
private List<RoutingItem> _routingItems;
public IQueryable<RoutingItem> RoutingItems
{
get
{
@ -147,7 +149,7 @@ public class MongoRepository : IBotSharpRepository
}
var routingItemDocs = _dc.RoutingItems?.AsQueryable()?.ToList() ?? new List<RoutingItemCollection>();
_routingItems = routingItemDocs.Select(x => new RoutingItemRecord
_routingItems = routingItemDocs.Select(x => new RoutingItem
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
@ -161,8 +163,8 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<RoutingProfileRecord> _routingProfiles;
public IQueryable<RoutingProfileRecord> RoutingProfile
private List<RoutingProfile> _routingProfiles;
public IQueryable<RoutingProfile> RoutingProfiles
{
get
{
@ -172,7 +174,7 @@ public class MongoRepository : IBotSharpRepository
}
var routingProfilDocs = _dc.RoutingProfiles?.AsQueryable()?.ToList() ?? new List<RoutingProfileCollection>();
_routingProfiles = routingProfilDocs.Select(x => new RoutingProfileRecord
_routingProfiles = routingProfilDocs.Select(x => new RoutingProfile
{
Id = x.Id.ToString(),
Name = x.Name,
@ -187,25 +189,25 @@ public class MongoRepository : IBotSharpRepository
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
if (entity is ConversationRecord conversation)
if (entity is Conversation conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(ConversationRecord));
_changedTableNames.Add(nameof(Conversation));
}
else if (entity is AgentRecord agent)
else if (entity is Agent agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(AgentRecord));
_changedTableNames.Add(nameof(Agent));
}
else if (entity is UserRecord user)
else if (entity is User user)
{
_users.Add(user);
_changedTableNames.Add(nameof(UserRecord));
_changedTableNames.Add(nameof(User));
}
else if (entity is UserAgentRecord userAgent)
else if (entity is UserAgent userAgent)
{
_userAgents.Add(userAgent);
_changedTableNames.Add(nameof(UserAgentRecord));
_changedTableNames.Add(nameof(UserAgent));
}
}
@ -216,7 +218,7 @@ public class MongoRepository : IBotSharpRepository
foreach (var table in _changedTableNames)
{
if (table == nameof(ConversationRecord))
if (table == nameof(Conversation))
{
var conversations = _conversations.Select(x => new ConversationCollection
{
@ -240,7 +242,7 @@ public class MongoRepository : IBotSharpRepository
_dc.Conversations.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(AgentRecord))
else if (table == nameof(Agent))
{
var agents = _agents.Select(x => new AgentCollection
{
@ -270,7 +272,7 @@ public class MongoRepository : IBotSharpRepository
_dc.Agents.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(UserRecord))
else if (table == nameof(User))
{
var users = _users.Select(x => new UserCollection
{
@ -300,7 +302,7 @@ public class MongoRepository : IBotSharpRepository
_dc.Users.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(UserAgentRecord))
else if (table == nameof(UserAgent))
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
{
@ -327,10 +329,10 @@ public class MongoRepository : IBotSharpRepository
return _changedTableNames.Count;
}
public UserRecord GetUserByEmail(string email)
public User GetUserByEmail(string email)
{
var user = User.FirstOrDefault(x => x.Email == email);
return user != null ? new UserRecord
var user = Users.FirstOrDefault(x => x.Email == email);
return user != null ? new User
{
Id = user.Id.ToString(),
FirstName = user.FirstName,
@ -344,7 +346,7 @@ public class MongoRepository : IBotSharpRepository
} : null;
}
public void CreateUser(UserRecord user)
public void CreateUser(User user)
{
if (user == null) return;
@ -364,7 +366,7 @@ public class MongoRepository : IBotSharpRepository
_dc.Users.InsertOne(userCollection);
}
public void UpdateAgent(AgentRecord agent)
public void UpdateAgent(Agent agent)
{
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
@ -404,7 +406,7 @@ public class MongoRepository : IBotSharpRepository
_dc.RoutingProfiles.DeleteMany(Builders<RoutingProfileCollection>.Filter.Empty);
}
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
{
var collections = routingItems?.Select(x => new RoutingItemCollection
{
@ -418,7 +420,7 @@ public class MongoRepository : IBotSharpRepository
})?.ToList() ?? new List<RoutingItemCollection>();
_dc.RoutingItems.InsertMany(collections);
return collections.Select(x => new RoutingItemRecord
return collections.Select(x => new RoutingItem
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
@ -430,7 +432,7 @@ public class MongoRepository : IBotSharpRepository
}).ToList();
}
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
{
var collections = profiles?.Select(x => new RoutingProfileCollection
{
@ -440,7 +442,7 @@ public class MongoRepository : IBotSharpRepository
})?.ToList() ?? new List<RoutingProfileCollection>();
_dc.RoutingProfiles.InsertMany(collections);
return collections.Select(x => new RoutingProfileRecord
return collections.Select(x => new RoutingProfile
{
Id = x.Id.ToString(),
Name = x.Name,
@ -451,25 +453,25 @@ public class MongoRepository : IBotSharpRepository
public List<string> GetAgentResponses(string agentId)
{
var responses = new List<string>();
var agent = Agent.FirstOrDefault(x => x.Id == agentId);
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
if (agent == null) return responses;
return agent.Responses;
}
public AgentRecord GetAgent(string agentId)
public Agent GetAgent(string agentId)
{
var foundAgent = Agent.FirstOrDefault(x => x.Id == agentId);
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
return foundAgent;
}
public void CreateNewConversation(ConversationRecord conversation)
public void CreateNewConversation(Conversation conversation)
{
if (conversation == null) return;
var conv = new ConversationCollection
{
Id = Guid.Parse(conversation.Id),
Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(),
AgentId = Guid.Parse(conversation.AgentId),
UserId = Guid.Parse(conversation.UserId),
Title = conversation.Title,
@ -481,23 +483,11 @@ public class MongoRepository : IBotSharpRepository
{
Id = Guid.NewGuid(),
ConversationId = conv.Id,
Dialog = string.Empty,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow,
};
var states = new ConversationStatesCollection
{
Id = Guid.NewGuid(),
ConversationId = conv.Id,
State = new List<KeyValueModel>(),
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow,
Dialog = string.Empty
};
_dc.Conversations.InsertOne(conv);
_dc.ConversationDialogs.InsertOne(dialog);
_dc.ConversationStates.InsertOne(states);
}
public string GetConversationDialog(string conversationId)
@ -515,75 +505,75 @@ public class MongoRepository : IBotSharpRepository
{
if (string.IsNullOrEmpty(conversationId)) return;
var filter = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault();
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();
if (foundDialog == null) return;
var update = Builders<ConversationDialogCollection>.Update
.Set(x => x.Dialog, dialogs)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
var updateDialog = Builders<ConversationDialogCollection>.Update.Set(x => x.Dialog, dialogs);
var updateConv = Builders<ConversationCollection>.Update.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.ConversationDialogs.UpdateOne(filter, update);
_dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog);
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
public List<KeyValueModel> GetConversationState(string conversationId)
public List<KeyValueModel> GetConversationStates(string conversationId)
{
var states = new List<KeyValueModel>();
if (string.IsNullOrEmpty(conversationId)) return states;
var filter = Builders<ConversationStatesCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundStates = _dc.ConversationStates.Find(filter).FirstOrDefault();
if (foundStates == null) return states;
var savedStates = foundStates.State ?? new List<KeyValueModel>();
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConversation = _dc.Conversations.Find(filter).FirstOrDefault();
var savedStates = foundConversation?.States ?? new List<KeyValueModel>();
return savedStates;
}
public void UpdateConversationState(string conversationId, List<KeyValueModel> state)
public void UpdateConversationStates(string conversationId, List<KeyValueModel> states)
{
if (string.IsNullOrEmpty(conversationId)) return;
var filter = Builders<ConversationStatesCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundStates = _dc.ConversationStates.Find(filter).FirstOrDefault();
if (foundStates == null) return;
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConv = _dc.Conversations.Find(filter).FirstOrDefault();
if (foundConv == null) return;
var update = Builders<ConversationStatesCollection>.Update
.Set(x => x.State, state)
var update = Builders<ConversationCollection>.Update
.Set(x => x.States, states)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.ConversationStates.UpdateOne(filter, update);
_dc.Conversations.UpdateOne(filter, update);
}
public ConversationRecord GetConversation(string conversationId)
public Conversation GetConversation(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return null;
var filterById = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var filterStates = Builders<ConversationStatesCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var conv = _dc.Conversations.Find(filterById).FirstOrDefault();
var conv = _dc.Conversations.Find(filterConv).FirstOrDefault();
var dialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
var states = _dc.ConversationStates.Find(filterStates).FirstOrDefault();
if (conv == null) return null;
return new ConversationRecord
return new Conversation
{
Id = conv.Id.ToString(),
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog?.Dialog ?? string.Empty,
State = states?.State ?? new List<KeyValueModel>(),
States = new ConversationState(conv.States ?? new List<KeyValueModel>()),
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
};
}
public List<ConversationRecord> GetConversations(string userId)
public List<Conversation> GetConversations(string userId)
{
var records = new List<ConversationRecord>();
var records = new List<Conversation>();
if (string.IsNullOrEmpty(userId)) return records;
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, Guid.Parse(userId));
@ -592,16 +582,12 @@ public class MongoRepository : IBotSharpRepository
foreach (var conv in conversations)
{
var convId = conv.Id.ToString();
var dialog = GetConversationDialog(convId);
var states = GetConversationState(convId);
records.Add(new ConversationRecord
records.Add(new Conversation
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog,
State = states,
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});