From bedbcd999d78c45dc4d3947f682469a2892a13bc Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 16 Mar 2019 23:50:33 -0500 Subject: [PATCH] add agent creation api for ownthink --- .../BotSharp.Channel.FacebookMessenger.csproj | 4 +- .../BotSharp.Channel.Weixin.csproj | 3 - .../Controllers/WeixinAsyncController.cs | 2 +- .../Models/CustomMessageHandler.cs | 2 +- ...wModel.cs => AgentCreationRequestModel.cs} | 4 +- .../Agents/AgentCreationResponseModel.cs | 15 +++ .../BotSharp.Platform.OwnThink.csproj | 4 +- .../Controllers/AgentController.cs | 50 ++++++- .../Controllers/QueryController.cs | 55 ++++++++ .../Models/AIResponseFulfillment.cs | 13 ++ .../Models/AIResponseMetadata.cs | 12 ++ .../Models/AIResponseResult.cs | 127 ++++++++++++++++++ .../Models/AgentModel.cs | 2 + .../AgentCreationRequestViewModel.cs | 14 ++ .../AgentCreationResponseViewModel.cs | 12 ++ BotSharp.WebHost/Settings/app.json | 8 +- 16 files changed, 310 insertions(+), 17 deletions(-) rename BotSharp.Core/Models/Agents/{AgentCreationViewModel.cs => AgentCreationRequestModel.cs} (67%) create mode 100644 BotSharp.Core/Models/Agents/AgentCreationResponseModel.cs create mode 100644 BotSharp.Platform.OwnThink/Controllers/QueryController.cs create mode 100644 BotSharp.Platform.OwnThink/Models/AIResponseFulfillment.cs create mode 100644 BotSharp.Platform.OwnThink/Models/AIResponseMetadata.cs create mode 100644 BotSharp.Platform.OwnThink/Models/AIResponseResult.cs create mode 100644 BotSharp.Platform.OwnThink/ViewModels/AgentCreationRequestViewModel.cs create mode 100644 BotSharp.Platform.OwnThink/ViewModels/AgentCreationResponseViewModel.cs diff --git a/BotSharp.Channel.FacebookMessenger/BotSharp.Channel.FacebookMessenger.csproj b/BotSharp.Channel.FacebookMessenger/BotSharp.Channel.FacebookMessenger.csproj index f67b33ee..b68aaa1e 100644 --- a/BotSharp.Channel.FacebookMessenger/BotSharp.Channel.FacebookMessenger.csproj +++ b/BotSharp.Channel.FacebookMessenger/BotSharp.Channel.FacebookMessenger.csproj @@ -10,13 +10,11 @@ + - - - diff --git a/BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj b/BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj index e74df3f9..4a11168b 100644 --- a/BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj +++ b/BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj @@ -24,10 +24,7 @@ - - - diff --git a/BotSharp.Channel.Weixin/Controllers/WeixinAsyncController.cs b/BotSharp.Channel.Weixin/Controllers/WeixinAsyncController.cs index 65e8a44b..6bf03cf7 100644 --- a/BotSharp.Channel.Weixin/Controllers/WeixinAsyncController.cs +++ b/BotSharp.Channel.Weixin/Controllers/WeixinAsyncController.cs @@ -1,6 +1,6 @@ using BotSharp.Channel.Weixin.Models; using BotSharp.Platform.Abstractions; -using BotSharp.Platform.Dialogflow.Models; +using BotSharp.Platform.OwnThink.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Senparc.CO2NET.HttpUtility; diff --git a/BotSharp.Channel.Weixin/Models/CustomMessageHandler.cs b/BotSharp.Channel.Weixin/Models/CustomMessageHandler.cs index e67f7dd1..b8a6a648 100644 --- a/BotSharp.Channel.Weixin/Models/CustomMessageHandler.cs +++ b/BotSharp.Channel.Weixin/Models/CustomMessageHandler.cs @@ -31,8 +31,8 @@ using Senparc.Weixin; using Senparc.Weixin.MP; using BotSharp.Platform.Models.AiRequest; using BotSharp.Platform.Abstractions; -using BotSharp.Platform.Dialogflow.Models; using Microsoft.Extensions.Configuration; +using BotSharp.Platform.OwnThink.Models; #if NET45 using System.Web; diff --git a/BotSharp.Core/Models/Agents/AgentCreationViewModel.cs b/BotSharp.Core/Models/Agents/AgentCreationRequestModel.cs similarity index 67% rename from BotSharp.Core/Models/Agents/AgentCreationViewModel.cs rename to BotSharp.Core/Models/Agents/AgentCreationRequestModel.cs index 85b3c93e..ec2a5a1c 100644 --- a/BotSharp.Core/Models/Agents/AgentCreationViewModel.cs +++ b/BotSharp.Core/Models/Agents/AgentCreationRequestModel.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace BotSharp.Platform.Models.Agents { - public class AgentCreationViewModel + public class AgentCreationRequestModel { + [Required] public string Name { get; set; } public string Description { get; set; } diff --git a/BotSharp.Core/Models/Agents/AgentCreationResponseModel.cs b/BotSharp.Core/Models/Agents/AgentCreationResponseModel.cs new file mode 100644 index 00000000..7d346117 --- /dev/null +++ b/BotSharp.Core/Models/Agents/AgentCreationResponseModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.Models.Agents +{ + public class AgentCreationResponseModel + { + public string AgentId { get; set; } + + public string Name { get; set; } + + public string ClientAccessToken { get; set; } + } +} diff --git a/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj b/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj index 9b2b8c48..1855c819 100644 --- a/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj +++ b/BotSharp.Platform.OwnThink/BotSharp.Platform.OwnThink.csproj @@ -1,11 +1,11 @@ - + netcoreapp2.2 - + diff --git a/BotSharp.Platform.OwnThink/Controllers/AgentController.cs b/BotSharp.Platform.OwnThink/Controllers/AgentController.cs index 842b96d7..7a33cc95 100644 --- a/BotSharp.Platform.OwnThink/Controllers/AgentController.cs +++ b/BotSharp.Platform.OwnThink/Controllers/AgentController.cs @@ -1,10 +1,56 @@ -using System; +using BotSharp.Platform.Models.Agents; +using BotSharp.Platform.OwnThink.Models; +using BotSharp.Platform.OwnThink.ViewModels; +using Microsoft.AspNetCore.Mvc; +using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace BotSharp.Platform.OwnThink.Controllers { - public class AgentController + [Route("v1/[controller]")] + public class AgentController : ControllerBase { + private OwnThinkAi builder; + + /// + /// Initialize dialog controller and get a platform instance + /// + /// + public AgentController(OwnThinkAi platform) + { + builder = platform; + } + + /// + /// Create agent + /// + /// + /// + [HttpPost] + [ProducesResponseType(typeof(AgentCreationResponseViewModel), 200)] + public async Task Create(AgentCreationRequestViewModel requestViewModel) + { + var agent = new AgentModel() + { + Id = Guid.NewGuid().ToString(), + Name = requestViewModel.Name, + Description = requestViewModel.Name, + AppId = requestViewModel.AppId, + ClientAccessToken = Guid.NewGuid().ToString("N"), + DeveloperAccessToken = Guid.NewGuid().ToString("N") + }; + + await builder.SaveAgent(agent); + + return Ok(new AgentCreationResponseViewModel + { + AgentId = agent.Id, + AppId = agent.AppId, + Name = agent.Name, + ClientAccessToken = agent.ClientAccessToken + }); + } } } diff --git a/BotSharp.Platform.OwnThink/Controllers/QueryController.cs b/BotSharp.Platform.OwnThink/Controllers/QueryController.cs new file mode 100644 index 00000000..da220707 --- /dev/null +++ b/BotSharp.Platform.OwnThink/Controllers/QueryController.cs @@ -0,0 +1,55 @@ +using BotSharp.Platform.Models.AiRequest; +using BotSharp.Platform.OwnThink.Models; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Text; + +namespace BotSharp.Platform.OwnThink.Controllers +{ + [Route("v1/[controller]")] + public class QueryController : ControllerBase + { + private OwnThinkAi builder; + + public QueryController(OwnThinkAi platform) + { + builder = platform; + } + + /*[HttpGet, HttpPost] + public async Task> Query(QueryModel request) + { + String clientAccessToken = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; + + // find a model according to clientAccessToken + + var agents = await builder.GetAllAgents(); + var agent = agents.FirstOrDefault(x => x.ClientAccessToken == clientAccessToken); + + if (agent == null) + { + return BadRequest("The agent not found."); + } + + var aIResponse = await builder.TextRequest(new AiRequest + { + Text = request.Query, + AgentId = agent.Id, + SessionId = request.SessionId + }); + + return new AIResponse + { + Result = aIResponse, + Id = Guid.NewGuid().ToString(), + Lang = request.Lang, + SessionId = request.SessionId, + Status = new AIResponseStatus(), + Timestamp = DateTime.UtcNow + }; + }*/ + } +} diff --git a/BotSharp.Platform.OwnThink/Models/AIResponseFulfillment.cs b/BotSharp.Platform.OwnThink/Models/AIResponseFulfillment.cs new file mode 100644 index 00000000..77498336 --- /dev/null +++ b/BotSharp.Platform.OwnThink/Models/AIResponseFulfillment.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.OwnThink.Models +{ + public class AIResponseFulfillment + { + public string Speech { get; set; } + + public List Messages { get; set; } + } +} diff --git a/BotSharp.Platform.OwnThink/Models/AIResponseMetadata.cs b/BotSharp.Platform.OwnThink/Models/AIResponseMetadata.cs new file mode 100644 index 00000000..f24a48f2 --- /dev/null +++ b/BotSharp.Platform.OwnThink/Models/AIResponseMetadata.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.OwnThink.Models +{ + public class AIResponseMetadata + { + public string IntentId { get; set; } + public string IntentName { get; set; } + } +} diff --git a/BotSharp.Platform.OwnThink/Models/AIResponseResult.cs b/BotSharp.Platform.OwnThink/Models/AIResponseResult.cs new file mode 100644 index 00000000..c1f4c4f3 --- /dev/null +++ b/BotSharp.Platform.OwnThink/Models/AIResponseResult.cs @@ -0,0 +1,127 @@ +using BotSharp.Platform.Models.AiResponse; +using BotSharp.Platform.Models.Contexts; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace BotSharp.Platform.OwnThink.Models +{ + public class AIResponseResult : AiResponse + { + public Boolean ActionIncomplete { get; set; } + + public String Action { get; set; } + + public Dictionary Parameters { get; set; } + + public AIContext[] Contexts { get; set; } + + public AIResponseMetadata Metadata { get; set; } + + public AIResponseFulfillment Fulfillment { get; set; } + + [JsonIgnore] + public bool HasParameters + { + get + { + return Parameters != null && Parameters.Count > 0; + } + } + + public string GetStringParameter(string name, string defaultValue = "") + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException("name"); + } + + if (Parameters.ContainsKey(name)) + { + return Parameters[name].ToString(); + } + + return defaultValue; + } + + public int GetIntParameter(string name, int defaultValue = 0) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException("name"); + } + + if (Parameters.ContainsKey(name)) + { + var parameterValue = Parameters[name].ToString(); + int result; + if (int.TryParse(parameterValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out result)) + { + return result; + } + + float floatResult; + if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out floatResult)) + { + result = Convert.ToInt32(floatResult); + return result; + } + } + + return defaultValue; + } + + public float GetFloatParameter(string name, float defaultValue = 0) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException("name"); + } + + if (Parameters.ContainsKey(name)) + { + var parameterValue = Parameters[name].ToString(); + float result; + if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out result)) + { + return result; + } + } + + return defaultValue; + } + + public JObject GetJsonParameter(string name, JObject defaultValue = null) + { + if (string.IsNullOrEmpty("name")) + { + throw new ArgumentNullException(nameof(name)); + } + + if (Parameters.ContainsKey(name)) + { + var parameter = Parameters[name].ToString(); + if (parameter != null) + { + return JObject.FromObject(parameter); + } + } + + return defaultValue; + } + + public AIContext GetContext(string name) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentException("Name must be not empty", nameof(name)); + } + + return Contexts?.FirstOrDefault(c => string.Equals(c.Name, name, StringComparison.CurrentCultureIgnoreCase)); + } + } +} diff --git a/BotSharp.Platform.OwnThink/Models/AgentModel.cs b/BotSharp.Platform.OwnThink/Models/AgentModel.cs index c02d915b..16c7b2e3 100644 --- a/BotSharp.Platform.OwnThink/Models/AgentModel.cs +++ b/BotSharp.Platform.OwnThink/Models/AgentModel.cs @@ -11,6 +11,8 @@ namespace BotSharp.Platform.OwnThink.Models { public class AgentModel : AgentBase { + public string AppId { get; set; } + public AgentModel() { diff --git a/BotSharp.Platform.OwnThink/ViewModels/AgentCreationRequestViewModel.cs b/BotSharp.Platform.OwnThink/ViewModels/AgentCreationRequestViewModel.cs new file mode 100644 index 00000000..6cb55e65 --- /dev/null +++ b/BotSharp.Platform.OwnThink/ViewModels/AgentCreationRequestViewModel.cs @@ -0,0 +1,14 @@ +using BotSharp.Platform.Models.Agents; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace BotSharp.Platform.OwnThink.ViewModels +{ + public class AgentCreationRequestViewModel : AgentCreationRequestModel + { + [Required] + public string AppId { get; set; } + } +} diff --git a/BotSharp.Platform.OwnThink/ViewModels/AgentCreationResponseViewModel.cs b/BotSharp.Platform.OwnThink/ViewModels/AgentCreationResponseViewModel.cs new file mode 100644 index 00000000..21e6afdc --- /dev/null +++ b/BotSharp.Platform.OwnThink/ViewModels/AgentCreationResponseViewModel.cs @@ -0,0 +1,12 @@ +using BotSharp.Platform.Models.Agents; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.OwnThink.ViewModels +{ + public class AgentCreationResponseViewModel : AgentCreationResponseModel + { + public string AppId { get; set; } + } +} diff --git a/BotSharp.WebHost/Settings/app.json b/BotSharp.WebHost/Settings/app.json index f4d5943b..bd3d6639 100644 --- a/BotSharp.WebHost/Settings/app.json +++ b/BotSharp.WebHost/Settings/app.json @@ -14,14 +14,14 @@ "Name": "RasaAi", "Type": "BotSharp.Platform.Rasa" }, - { - "Name": "OwnThink", - "Type": "BotSharp.Platform.OwnThink" - }, { "Name": "Articulate", "Type": "BotSharp.Platform.Articulate" }, + { + "Name": "OwnThink", + "Type": "BotSharp.Platform.OwnThink" + }, { "Name": "WeixinChannel", "Type": "BotSharp.Channel.Weixin"