add routing item and routing profile

This commit is contained in:
Jicheng Lu 2023-09-02 17:41:58 -05:00
parent f5819919fc
commit 5531944a9f
9 changed files with 153 additions and 9 deletions

View file

@ -9,6 +9,9 @@ public interface IBotSharpRepository
IQueryable<AgentRecord> Agent { get; }
IQueryable<UserAgentRecord> UserAgent { get; }
IQueryable<ConversationRecord> Conversation { get; }
IQueryable<RoutingItemRecord> RoutingItem { get; }
IQueryable<RoutingProfileRecord> RoutingProfile { get; }
int Transaction<TTableInterface>(Action action);
void Add<TTableInterface>(object entity);

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Abstraction.Repositories.Records;
public class RoutingItemRecord : RecordBase
@ -8,4 +10,16 @@ public class RoutingItemRecord : RecordBase
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
{
Name = Name,
Description = Description,
RequiredFields = RequiredFields,
RedirectTo = RedirectTo,
Disabled = Disabled
};
}
}

View file

@ -10,6 +10,8 @@ public class BotSharpDbContext : Database, IBotSharpRepository
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 void UpdateAgent(AgentRecord agent)
{

View file

@ -1,6 +1,8 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
using BotSharp.Abstraction.Routing.Models;
using SharpCompress.Common;
using System.IO;
using System.Text.Json;
namespace BotSharp.Core.Repository;
@ -118,6 +120,52 @@ public class FileRepository : IBotSharpRepository
}
}
private List<RoutingItemRecord> _routingItems;
public IQueryable<RoutingItemRecord> RoutingItem
{
get
{
if (_routingItems != null)
{
return _routingItems.AsQueryable();
}
_routingItems = new List<RoutingItemRecord>();
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));
}
return _routingItems.AsQueryable();
}
}
private List<RoutingProfileRecord> _routingProfiles;
public IQueryable<RoutingProfileRecord> RoutingProfile
{
get
{
if (_routingProfiles != null)
{
return _routingProfiles.AsQueryable();
}
_routingProfiles = new List<RoutingProfileRecord>();
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));
}
return _routingProfiles.AsQueryable();
}
}
public void Add<TTableInterface>(object entity)
{
_conversations = Conversation.ToList();

View file

@ -33,20 +33,26 @@ public class Router : IAgentRouting
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json");
var records = JsonSerializer.Deserialize<RoutingItem[]>(File.ReadAllText(filePath));
var db = _services.GetRequiredService<IBotSharpRepository>();
//var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "route.json");
//var records = JsonSerializer.Deserialize<RoutingItem[]>(File.ReadAllText(filePath));
var records = db.RoutingItem.Select(x => x.ToRoutingItem()).ToArray();
// check if routing profile is specified
filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
if (File.Exists(filePath))
//filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
var profiles = db.RoutingProfile.ToList();
if (profiles != null && profiles.Any())
{
var state = _services.GetRequiredService<IConversationStateService>();
var name = state.GetState("channel");
var profiles = JsonSerializer.Deserialize<RoutingProfile[]>(File.ReadAllText(filePath));
var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name);
if (spcificedProfile != null)
//var profiles = JsonSerializer.Deserialize<RoutingProfile[]>(File.ReadAllText(filePath));
var specifiedProfile = profiles.FirstOrDefault(x => x.Name == name);
if (specifiedProfile != null)
{
records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray();
records = records.Where(x => specifiedProfile.AgentIds.Contains(x.AgentId)).ToArray();
}
}

View file

@ -0,0 +1,11 @@
namespace BotSharp.Plugin.MongoStorage.Collections;
public class RoutingItemCollection : MongoBase
{
public Guid AgentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<string> RequiredFields { get; set; }
public Guid RedirectTo { get; set; }
public bool Disabled { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.MongoStorage.Collections;
public class RoutingProfileCollection : MongoBase
{
public string Name { get; set; }
public List<Guid> AgentIds { get; set; }
}

View file

@ -36,4 +36,10 @@ public class MongoDbContext
public IMongoCollection<UserAgentCollection> UserAgents
=> Database.GetCollection<UserAgentCollection>("OneBrainUserAgents");
public IMongoCollection<RoutingItemCollection> RoutingItems
=> Database.GetCollection<RoutingItemCollection>("OneBrainRoutingItems");
public IMongoCollection<RoutingProfileCollection> RoutingProfiles
=> Database.GetCollection<RoutingProfileCollection>("OneBrainRoutingProfiles");
}

View file

@ -12,7 +12,6 @@ public class MongoRepository : IBotSharpRepository
{
_dc = dc;
_services = services;
_options = new UpdateOptions
{
IsUpsert = true,
@ -125,6 +124,54 @@ public class MongoRepository : IBotSharpRepository
}
}
private List<RoutingItemRecord> _routingItems;
public IQueryable<RoutingItemRecord> RoutingItem
{
get
{
if (_routingItems != null)
{
return _routingItems.AsQueryable();
}
var routingItemDocs = _dc.RoutingItems?.AsQueryable()?.ToList() ?? new List<RoutingItemCollection>();
_routingItems = routingItemDocs.Select(x => new RoutingItemRecord
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
Description = x.Description,
RequiredFields = x.RequiredFields,
RedirectTo = x.RedirectTo.ToString(),
Disabled = x.Disabled
}).ToList();
return _routingItems.AsQueryable();
}
}
private List<RoutingProfileRecord> _routingProfiles;
public IQueryable<RoutingProfileRecord> RoutingProfile
{
get
{
if (_routingProfiles != null)
{
return _routingProfiles.AsQueryable();
}
var routingProfilDocs = _dc.RoutingProfiles?.AsQueryable()?.ToList() ?? new List<RoutingProfileCollection>();
_routingProfiles = routingProfilDocs.Select(x => new RoutingProfileRecord
{
Id = x.Id.ToString(),
Name = x.Name,
AgentIds = x.AgentIds?.Select(x => x.ToString())?.ToList() ?? new List<string>()
}).ToList();
return _routingProfiles.AsQueryable();
}
}
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{