From 5fc98cc327fe56ea8d3c2af2e85e22b4dddc7b77 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 30 Sep 2018 10:11:36 -0500 Subject: [PATCH] articulate integration in progress. --- .../IPlatformBuilder.cs | 7 -- BotSharp.Platform.Models/EntityBase.cs | 6 ++ Platform.Articulate/ArticulateAi.cs | 40 ++++++++--- .../Controllers/DomainController.cs | 3 +- .../Controllers/EntityController.cs | 7 +- .../Controllers/IntentController.cs | 30 ++------- .../Controllers/ParseControllercs.cs | 19 ++++++ .../Controllers/ScenarioController.cs | 67 +++++++++++++++++++ Platform.Articulate/Models/DomainModel.cs | 7 +- Platform.Articulate/Models/EntityModel.cs | 2 - Platform.Articulate/Models/IntentModel.cs | 5 +- Platform.Articulate/Models/ScenarioModel.cs | 22 ++++++ .../ViewModels/IntentScenarioViewModel.cs | 8 +-- 13 files changed, 162 insertions(+), 61 deletions(-) create mode 100644 Platform.Articulate/Controllers/ParseControllercs.cs create mode 100644 Platform.Articulate/Controllers/ScenarioController.cs create mode 100644 Platform.Articulate/Models/ScenarioModel.cs diff --git a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs index b00e91ce..60d5c6d3 100644 --- a/BotSharp.Platform.Abstraction/IPlatformBuilder.cs +++ b/BotSharp.Platform.Abstraction/IPlatformBuilder.cs @@ -25,13 +25,6 @@ namespace BotSharp.Platform.Abstraction /// StandardAgent StandardizeAgent(TAgent agent); - /// - /// Recover standard agent to specific agent format - /// - /// - /// - TAgent RecoverAgent(StandardAgent agent); - /// /// /// diff --git a/BotSharp.Platform.Models/EntityBase.cs b/BotSharp.Platform.Models/EntityBase.cs index 42971aa5..32e23288 100644 --- a/BotSharp.Platform.Models/EntityBase.cs +++ b/BotSharp.Platform.Models/EntityBase.cs @@ -1,10 +1,16 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace BotSharp.Platform.Models { public abstract class EntityBase { + /// + /// Guid + /// + [StringLength(36)] + public String Id { get; set; } } } diff --git a/Platform.Articulate/ArticulateAi.cs b/Platform.Articulate/ArticulateAi.cs index dafd8eb3..c902ccdd 100644 --- a/Platform.Articulate/ArticulateAi.cs +++ b/Platform.Articulate/ArticulateAi.cs @@ -4,6 +4,7 @@ using BotSharp.Platform.Models; using Platform.Articulate.Models; using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace Platform.Articulate @@ -20,19 +21,40 @@ namespace Platform.Articulate { public DialogRequestOptions RequestOptions { get; set; } - public TAgent RecoverAgent(StandardAgent agent) + public Tuple GetAgentByDomainId(String domainId) { - if (agent == null) return default(TAgent); + var results = GetAllAgents(); - var agent1 = new AgentModel + foreach (TAgent agent in results) { - Id = agent.Id, - Name = agent.Name, - Description = agent.Description, - Language = agent.Language - }; + var domain = (agent as AgentModel).Domains.FirstOrDefault(x => x.Id == domainId); - return (TAgent)(agent1 as Object); + if (domain != null) + { + return new Tuple(agent, domain); + } + } + + return null; + } + + public Tuple GetAgentByIntentId(String intentId) + { + var results = GetAllAgents(); + + foreach (TAgent agent in results) + { + foreach (DomainModel domain in (agent as AgentModel).Domains) + { + var intent = domain.Intents.FirstOrDefault(x => x.Id == intentId); + if (intent != null) + { + return new Tuple(agent, domain, intent); + } + } + } + + return null; } public StandardAgent StandardizeAgent(TAgent specificAgent) diff --git a/Platform.Articulate/Controllers/DomainController.cs b/Platform.Articulate/Controllers/DomainController.cs index 97823996..a17f7d80 100644 --- a/Platform.Articulate/Controllers/DomainController.cs +++ b/Platform.Articulate/Controllers/DomainController.cs @@ -61,9 +61,8 @@ namespace Platform.Articulate.Controllers public DomainPageViewModel GetAgentDomains([FromRoute] string agentId, [FromQuery] int start, [FromQuery] int limit) { var agent = builder.GetAgentById(agentId); - - return new DomainPageViewModel { /*Domains = agent.d, Total = domains.Count*/ }; + return new DomainPageViewModel { Domains = agent.Domains, Total = agent.Domains.Count }; } } #endif diff --git a/Platform.Articulate/Controllers/EntityController.cs b/Platform.Articulate/Controllers/EntityController.cs index 42199ff3..e3dac061 100644 --- a/Platform.Articulate/Controllers/EntityController.cs +++ b/Platform.Articulate/Controllers/EntityController.cs @@ -40,9 +40,8 @@ namespace Platform.Articulate.Controllers [HttpGet("/agent/{agentId}/entity")] public EntityPageViewModel GetAgentEntities([FromRoute] string agentId, [FromQuery] int start, [FromQuery] int limit) { - var entities = new List(); - - return new EntityPageViewModel { Entities = entities, Total = entities.Count }; + var agent = builder.GetAgentById(agentId); + return new EntityPageViewModel { Entities = agent.Entities.Select(x => x as EntityModel).ToList(), Total = agent.Entities.Count }; } [HttpPost] @@ -57,7 +56,7 @@ namespace Platform.Articulate.Controllers } var agent = builder.GetAgentByName(entity.Agent); - + entity.Id = Guid.NewGuid().ToString(); agent.Entities.Add(entity); builder.SaveAgent(agent); diff --git a/Platform.Articulate/Controllers/IntentController.cs b/Platform.Articulate/Controllers/IntentController.cs index 6b7b23e6..d667441a 100644 --- a/Platform.Articulate/Controllers/IntentController.cs +++ b/Platform.Articulate/Controllers/IntentController.cs @@ -26,15 +26,9 @@ namespace Platform.Articulate.Controllers [HttpGet("{intentId}")] public IntentModel GetIntent([FromRoute] string intentId) { - string dataDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Articulate"); + var agent = builder.GetAgentByIntentId(intentId); - string dataPath = Directory.GetFiles(dataDir).FirstOrDefault(x => x.EndsWith($"-intent-{intentId}.json")); - - string json = System.IO.File.ReadAllText(dataPath); - - var intent = JsonConvert.DeserializeObject(json); - - return intent; + return agent.Item3; } [HttpPost] @@ -56,20 +50,6 @@ namespace Platform.Articulate.Controllers return intent; } - [HttpGet("{intentId}/scenario")] - public IntentScenarioViewModel GetIntentScenario([FromRoute] string intentId) - { - string dataDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Articulate"); - - string dataPath = Directory.GetFiles(dataDir).FirstOrDefault(x => x.Contains($"-intent-{intentId}-scenario-")); - - string json = System.IO.File.ReadAllText(dataPath); - - var scenario = JsonConvert.DeserializeObject(json); - - return scenario; - } - [HttpGet("{intentId}/webhook")] public void GetIntentWebhook([FromRoute] string intentId) { @@ -137,10 +117,8 @@ namespace Platform.Articulate.Controllers [HttpGet("/domain/{domainId}/intent")] public IntentPageViewModel GetReferencedIntentsByDomain([FromRoute] string domainId, [FromQuery] int start, [FromQuery] int limit) { - var intents = new List(); - - var results = builder.GetAllAgents(); - //var agents = results.Select(x => builder.RecoverAgent(x) as AgentModel).Where(x => x.).ToList(); + var agent = builder.GetAgentByDomainId(domainId); + var intents = agent.Item2.Intents.Select(x => x as IntentModel).ToList(); return new IntentPageViewModel { Intents = intents, Total = intents.Count }; } diff --git a/Platform.Articulate/Controllers/ParseControllercs.cs b/Platform.Articulate/Controllers/ParseControllercs.cs new file mode 100644 index 00000000..2aa7db0b --- /dev/null +++ b/Platform.Articulate/Controllers/ParseControllercs.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Platform.Articulate.Controllers +{ +#if ARTICULATE + [Route("[controller]")] + public class ParseControllercs : ControllerBase + { + [HttpGet("/agent/{agentId}/parse")] + public void ParseText([FromRoute] string agentId, [FromQuery] string text) + { + + } + } +#endif +} diff --git a/Platform.Articulate/Controllers/ScenarioController.cs b/Platform.Articulate/Controllers/ScenarioController.cs new file mode 100644 index 00000000..1c8d8711 --- /dev/null +++ b/Platform.Articulate/Controllers/ScenarioController.cs @@ -0,0 +1,67 @@ +using BotSharp.Core; +using DotNetToolkit; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Platform.Articulate.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace Platform.Articulate.Controllers +{ +#if ARTICULATE + [Route("[controller]")] + public class ScenarioController : ControllerBase + { + private ArticulateAi, AgentModel> builder; + + public ScenarioController() + { + builder = new ArticulateAi, AgentModel>(); + } + + [HttpGet("/intent/{intentId}/scenario")] + public IntentScenarioViewModel GetIntentScenario([FromRoute] string intentId) + { + var agent = builder.GetAgentByIntentId(intentId); + + var view = agent.Item3.Scenario.ToObject(); + + view.Agent = agent.Item1.AgentName; + view.Domain = agent.Item2.DomainName; + view.Intent = agent.Item3.IntentName; + + return view; + } + + [HttpPost("/intent/{intentId}/scenario")] + public IntentScenarioViewModel PostIntentScenario() + { + IntentScenarioViewModel scenario = null; + + using (var reader = new StreamReader(Request.Body)) + { + string body = reader.ReadToEnd(); + scenario = JsonConvert.DeserializeObject(body); + } + + scenario.Id = Guid.NewGuid().ToString(); + + var agent = builder.GetAgentByName(scenario.Agent); + + var model = scenario.ToObject(); + + agent.Domains.First(x => x.DomainName == scenario.Domain) + .Intents + .First(x => x.IntentName == scenario.Intent) + .Scenario = model; + + builder.SaveAgent(agent); + + return scenario; + } + } +#endif +} diff --git a/Platform.Articulate/Models/DomainModel.cs b/Platform.Articulate/Models/DomainModel.cs index 6f792468..cfb83dc5 100644 --- a/Platform.Articulate/Models/DomainModel.cs +++ b/Platform.Articulate/Models/DomainModel.cs @@ -7,6 +7,11 @@ namespace Platform.Articulate.Models { public class DomainModel { + public DomainModel() + { + Intents = new List(); + } + public string Id { get; set; } public string Agent { get; set; } @@ -21,6 +26,6 @@ namespace Platform.Articulate.Models public string Status { get; set; } - public List Intents { get; set; } + public List Intents { get; set; } } } diff --git a/Platform.Articulate/Models/EntityModel.cs b/Platform.Articulate/Models/EntityModel.cs index f0c5552d..fb063cc4 100644 --- a/Platform.Articulate/Models/EntityModel.cs +++ b/Platform.Articulate/Models/EntityModel.cs @@ -8,8 +8,6 @@ namespace Platform.Articulate.Models { public class EntityModel : EntityBase { - public int Id { get; set; } - public string Regex { get; set; } public string Agent { get; set; } diff --git a/Platform.Articulate/Models/IntentModel.cs b/Platform.Articulate/Models/IntentModel.cs index e02d7739..b1a2cc3f 100644 --- a/Platform.Articulate/Models/IntentModel.cs +++ b/Platform.Articulate/Models/IntentModel.cs @@ -8,8 +8,7 @@ namespace Platform.Articulate.Models { public class IntentModel : IntentBase { - [JsonProperty("IntentName")] - public new string Name { get; set; } + public string IntentName { get; set; } public string Agent { get; set; } @@ -21,6 +20,6 @@ namespace Platform.Articulate.Models public List Examples { get; set; } - public IntentResponseBase Response { get; set; } + public ScenarioModel Scenario { get; set; } } } diff --git a/Platform.Articulate/Models/ScenarioModel.cs b/Platform.Articulate/Models/ScenarioModel.cs new file mode 100644 index 00000000..4f92e704 --- /dev/null +++ b/Platform.Articulate/Models/ScenarioModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace Platform.Articulate.Models +{ + public class ScenarioModel + { + /// + /// Guid + /// + [StringLength(36)] + public String Id { get; set; } + + public string ScenarioName { get; set; } + + public List IntentResponses { get; set; } + + public List Slots { get; set; } + } +} diff --git a/Platform.Articulate/ViewModels/IntentScenarioViewModel.cs b/Platform.Articulate/ViewModels/IntentScenarioViewModel.cs index 684856f9..b9dcf8a7 100644 --- a/Platform.Articulate/ViewModels/IntentScenarioViewModel.cs +++ b/Platform.Articulate/ViewModels/IntentScenarioViewModel.cs @@ -4,18 +4,12 @@ using System.Text; namespace Platform.Articulate.Models { - public class IntentScenarioViewModel + public class IntentScenarioViewModel : ScenarioModel { public string Domain { get; set; } public string Agent { get; set; } public string Intent { get; set; } - - public string ScenarioName { get; set; } - - public List IntentResponses { get; set; } - - public List Slots { get; set; } } }