sync agent structure changes

This commit is contained in:
Jicheng Lu 2023-09-18 18:14:24 -05:00
parent 3df0565a63
commit 4a219798d9
10 changed files with 203 additions and 3 deletions

View file

@ -6,6 +6,10 @@ public enum AgentField
Name,
Description,
IsPublic,
Disabled,
AllowRouting,
Profiles,
RoutingRules,
Instruction,
Function,
Template,

View file

@ -6,7 +6,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; } = string.Empty;
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
@ -81,6 +81,10 @@ public class Agent
Samples = agent.Samples,
Knowledges = agent.Knowledges,
IsPublic = agent.IsPublic,
Disabled = agent.Disabled,
AllowRouting = agent.AllowRouting,
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime,
};
@ -94,7 +98,7 @@ public class Agent
public Agent SetTemplates(List<AgentTemplate> templates)
{
Templates = templates;
Templates = templates ?? new List<AgentTemplate>();
return this;
}
@ -133,4 +137,28 @@ public class Agent
IsPublic = isPublic;
return this;
}
public Agent SetDisabled(bool disabled)
{
Disabled = disabled;
return this;
}
public Agent SetAllowRouting(bool allowRouting)
{
AllowRouting = allowRouting;
return this;
}
public Agent SetProfiles(List<string> profiles)
{
Profiles = profiles ?? new List<string>();
return this;
}
public Agent SetRoutingRules(List<RoutingRule> rules)
{
RoutingRules = rules ?? new List<RoutingRule>();
return this;
}
}

View file

@ -18,4 +18,9 @@ public class RoutingRule
{
return $"{AgentName} {Field}";
}
public RoutingRule()
{
}
}

View file

@ -35,6 +35,10 @@ public partial class AgentService
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetDisabled(foundAgent.Disabled)
.SetAllowRouting(foundAgent.AllowRouting)
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using System.IO;
namespace BotSharp.Core.Agents.Services;
@ -15,6 +16,11 @@ public partial class AgentService
record.Name = agent.Name ?? string.Empty;
record.Description = agent.Description ?? string.Empty;
record.IsPublic = agent.IsPublic;
record.Disabled = agent.Disabled;
record.AllowRouting = agent.AllowRouting;
record.Profiles = agent.Profiles ?? new List<string>();
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
record.Instruction = agent.Instruction ?? string.Empty;
record.Functions = agent.Functions ?? new List<string>();
record.Templates = agent.Templates ?? new List<AgentTemplate>();
@ -53,6 +59,10 @@ public partial class AgentService
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetDisabled(foundAgent.Disabled)
.SetAllowRouting(foundAgent.AllowRouting)
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -11,6 +12,10 @@ public class AgentCreationModel
public List<string> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public bool IsPublic { get; set; }
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }
public List<RoutingRule> RoutingRules { get; set; }
public Agent ToAgent()
{
@ -22,7 +27,11 @@ public class AgentCreationModel
Templates = Templates,
Functions = Functions,
Responses = Responses,
IsPublic = IsPublic
IsPublic = IsPublic,
AllowRouting = AllowRouting,
Disabled = Disabled,
Profiles = Profiles,
RoutingRules = RoutingRules
};
}
}

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -32,12 +33,30 @@ public class AgentUpdateModel
/// </summary>
public List<AgentResponse>? Responses { get; set; }
public bool IsPublic { get; set; }
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
/// <summary>
/// Profile by channel
/// </summary>
public List<string> Profiles { get; set; }
public List<RoutingRule> RoutingRules { get; set; }
public Agent ToAgent()
{
var agent = new Agent()
{
Name = Name ?? string.Empty,
Description = Description ?? string.Empty,
IsPublic = IsPublic,
Disabled = Disabled,
AllowRouting = AllowRouting,
Profiles = Profiles ?? new List<string>(),
RoutingRules = RoutingRules ?? new List<RoutingRule>(),
Instruction = Instruction ?? string.Empty,
Templates = Templates ?? new List<AgentTemplate>(),
Functions = Functions ?? new List<string>(),

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Plugin.MongoStorage.Models;
namespace BotSharp.Plugin.MongoStorage.Collections;
@ -11,6 +12,10 @@ public class AgentCollection : MongoBase
public List<string> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public bool IsPublic { get; set; }
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }
public List<RoutintRuleElement> RoutingRules { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }

View file

@ -0,0 +1,37 @@
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Plugin.MongoStorage.Models;
public class RoutingRuleElement
{
public string Field { get; set; }
public bool Required { get; set; }
public Guid? RedirectTo { get; set; }
public RoutingRuleElement()
{
}
public static RoutingRuleElement ToMongoElement(RoutingRule routingRule)
{
return new RoutingRuleElement
{
Field = routingRule.Field,
Required = routingRule.Required,
RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null
};
}
public static RoutingRule ToDomainElement(string agentId, string agentName, RoutingRuleElement rule)
{
return new RoutingRule
{
AgentId = agentId,
AgentName = agentName,
Field = rule.Field,
Required = rule.Required,
RedirectTo = rule.RedirectTo?.ToString()
};
}
}

View file

@ -1,7 +1,9 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -42,6 +44,12 @@ public class MongoRepository : IBotSharpRepository
Functions = x.Functions,
Responses = x.Responses,
IsPublic = x.IsPublic,
Disabled = x.Disabled,
AllowRouting = x.AllowRouting,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?
.Select(r => RoutingRuleElement.ToDomainElement(x.Id.ToString(), x.Name, r))?
.ToList() ?? new List<RoutingRule>(),
CreatedDateTime = x.CreatedTime,
UpdatedDateTime = x.UpdatedTime
}).ToList();
@ -206,6 +214,12 @@ public class MongoRepository : IBotSharpRepository
Functions = x.Functions,
Responses = x.Responses,
IsPublic = x.IsPublic,
AllowRouting = x.AllowRouting,
Disabled = x.Disabled,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?
.Select(r => RoutingRuleElement.ToMongoElement(r))?
.ToList() ?? new List<RoutingRuleElement>(),
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();
@ -221,6 +235,10 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Responses, agent.Responses)
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.AllowRouting, agent.AllowRouting)
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules)
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
@ -299,6 +317,18 @@ public class MongoRepository : IBotSharpRepository
case AgentField.IsPublic:
UpdateAgentIsPublic(agent.Id, agent.IsPublic);
break;
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;
case AgentField.Instruction:
UpdateAgentInstruction(agent.Id, agent.Instruction);
break;
@ -354,6 +384,51 @@ public class MongoRepository : IBotSharpRepository
_dc.Agents.UpdateOne(filter, update);
}
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;
var ruleElements = rules.Select(x => RoutingRuleElement.ToMongoElement(x)).ToList();
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);
}
private void UpdateAgentInstruction(string agentId, string instruction)
{
if (string.IsNullOrEmpty(instruction)) return;
@ -408,6 +483,10 @@ public class MongoRepository : IBotSharpRepository
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.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(x => RoutingRuleElement.ToMongoElement(x)).ToList())
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Templates, agent.Templates)
.Set(x => x.Functions, agent.Functions)