From d511d84226fd220a27e1f55d18a635c9603d80b1 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 29 Apr 2018 22:09:32 -0500 Subject: [PATCH] milestone 0.1 --- .gitignore | 1 + BotSharp.Core/Agents/Agent.cs | 4 + BotSharp.Core/Engines/RasaAi.cs | 3 + BotSharp.Core/Engines/RequestExtension.cs | 7 + BotSharp.Core/Intents/Intent.cs | 2 +- BotSharp.Core/Loader/DbInitializer.cs | 28 --- BotSharp.Core/Loader/IHookDbInitializer.cs | 27 -- BotSharp.Core/Loader/IInitializationLoader.cs | 13 - BotSharp.Core/Loader/InitializationLoader.cs | 34 --- BotSharp.RestApi/IntentController.cs | 26 -- BotSharp.UnitTest/AgentTest.cs | 6 +- BotSharp.UnitTest/BotSharp.UnitTest.csproj | 230 ++++++++++++++++++ BotSharp.UnitTest/IntentTest.cs | 3 - BotSharp.UnitTest/Settings/settings.app.json | 15 ++ BotSharp.UnitTest/Settings/settings.auth.json | 11 + BotSharp.UnitTest/Settings/settings.aws.json | 10 + BotSharp.UnitTest/Settings/settings.bot.json | 5 + BotSharp.UnitTest/Settings/settings.db.json | 10 + .../Settings/settings.swagger.json | 12 + BotSharp.UnitTest/TestEssential.cs | 6 +- .../BotSharp.WebStarter.csproj | 10 +- BotSharp.sln | 16 +- 22 files changed, 324 insertions(+), 155 deletions(-) delete mode 100644 BotSharp.Core/Loader/DbInitializer.cs delete mode 100644 BotSharp.Core/Loader/IHookDbInitializer.cs delete mode 100644 BotSharp.Core/Loader/IInitializationLoader.cs delete mode 100644 BotSharp.Core/Loader/InitializationLoader.cs delete mode 100644 BotSharp.RestApi/IntentController.cs create mode 100644 BotSharp.UnitTest/Settings/settings.app.json create mode 100644 BotSharp.UnitTest/Settings/settings.auth.json create mode 100644 BotSharp.UnitTest/Settings/settings.aws.json create mode 100644 BotSharp.UnitTest/Settings/settings.bot.json create mode 100644 BotSharp.UnitTest/Settings/settings.db.json create mode 100644 BotSharp.UnitTest/Settings/settings.swagger.json diff --git a/.gitignore b/.gitignore index 6ab26e09..07eaa50d 100644 --- a/.gitignore +++ b/.gitignore @@ -290,3 +290,4 @@ __pycache__/ /Bot.WebStarter/App_Data/bot-rasa.db /Bot.WebStarter/App_Data/DbInitializer/Agents/Dialogflow/VirtualAssistant /BotSharp.WebStarter/App_Data/DbInitializer/Agents/Dialogflow/VirtualAssistant +/BotSharp.UnitTest/App_Data/DbInitializer/Agents diff --git a/BotSharp.Core/Agents/Agent.cs b/BotSharp.Core/Agents/Agent.cs index fe699be8..c24dc327 100644 --- a/BotSharp.Core/Agents/Agent.cs +++ b/BotSharp.Core/Agents/Agent.cs @@ -16,20 +16,24 @@ namespace BotSharp.Core.Agents [MaxLength(64)] public String Name { get; set; } + [MaxLength(256)] public String Description { get; set; } public Boolean Published { get; set; } + [MaxLength(3)] public String Language { get; set; } /// /// Only access text/ audio rquest /// + [StringLength(32)] public String ClientAccessToken { get; set; } /// /// Developer can access more APIs /// + [StringLength(32)] public String DeveloperAccessToken { get; set; } [ForeignKey("AgentId")] diff --git a/BotSharp.Core/Engines/RasaAi.cs b/BotSharp.Core/Engines/RasaAi.cs index d55361f1..ffd85318 100644 --- a/BotSharp.Core/Engines/RasaAi.cs +++ b/BotSharp.Core/Engines/RasaAi.cs @@ -14,6 +14,9 @@ using System.Text; namespace BotSharp.Core.Engines { + /// + /// Rasa nlu 0.11.x + /// public class RasaAi { public Database dc { get; set; } diff --git a/BotSharp.Core/Engines/RequestExtension.cs b/BotSharp.Core/Engines/RequestExtension.cs index 2b25f9f2..5c09db90 100644 --- a/BotSharp.Core/Engines/RequestExtension.cs +++ b/BotSharp.Core/Engines/RequestExtension.cs @@ -56,6 +56,13 @@ namespace BotSharp.Core.Engines var result = CallRasa(rasa.agent.Id, request.Query.First(), rasa.agent.Id); + if (result.Data.IntentRanking == null) + { + result.Data.IntentRanking = new List + { + result.Data.Intent + }; + } result.Data.IntentRanking = result.Data.IntentRanking.Where(x => intents.Select(i => i.Name).Contains(x.Name)).ToList(); result.Data.Intent = result.Data.IntentRanking.First(); response = result.Data; diff --git a/BotSharp.Core/Intents/Intent.cs b/BotSharp.Core/Intents/Intent.cs index 75a218dc..5d20ded0 100644 --- a/BotSharp.Core/Intents/Intent.cs +++ b/BotSharp.Core/Intents/Intent.cs @@ -17,7 +17,7 @@ namespace BotSharp.Core.Intents [StringLength(36)] public String AgentId { get; set; } - [MaxLength(32)] + [MaxLength(64)] public String Name { get; set; } [MaxLength(256)] diff --git a/BotSharp.Core/Loader/DbInitializer.cs b/BotSharp.Core/Loader/DbInitializer.cs deleted file mode 100644 index 642cf06f..00000000 --- a/BotSharp.Core/Loader/DbInitializer.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Linq; -using EntityFrameworkCore.BootKit; -using DotNetToolkit; - -namespace BotSharp.Core.Loader -{ - public class DbInitializer : IInitializationLoader - { - public int Priority => 1; - - public void Initialize() - { - var dc = new DefaultDataContextLoader().GetDefaultDc(); - - var instances = TypeHelper.GetInstanceWithInterface(Database.Assemblies).OrderBy(x => x.Priority).ToList(); - - for (int idx = 0; idx < instances.Count; idx++) - { - DateTime start = DateTime.UtcNow; - Console.WriteLine($"{instances[idx].ToString()} P:{instances[idx].Priority} started at {DateTime.UtcNow}"); - int effected = dc.DbTran(() => instances[idx].Load(Database.Configuration, dc)); - Console.WriteLine($"{instances[idx].ToString()} effected [{effected}] records in {(DateTime.UtcNow - start).TotalMilliseconds} ms"); - } - } - } -} diff --git a/BotSharp.Core/Loader/IHookDbInitializer.cs b/BotSharp.Core/Loader/IHookDbInitializer.cs deleted file mode 100644 index 30b854cd..00000000 --- a/BotSharp.Core/Loader/IHookDbInitializer.cs +++ /dev/null @@ -1,27 +0,0 @@ -using EntityFrameworkCore.BootKit; -using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.Core.Loader -{ - /// - /// Initialize data for modules - /// - public interface IHookDbInitializer - { - /// - /// value smaller is higher priority - /// - int Priority { get; } - - /// - /// Load data - /// - /// - /// - /// - void Load(IConfiguration config, Database dc); - } -} diff --git a/BotSharp.Core/Loader/IInitializationLoader.cs b/BotSharp.Core/Loader/IInitializationLoader.cs deleted file mode 100644 index 9128bc87..00000000 --- a/BotSharp.Core/Loader/IInitializationLoader.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Text; - -namespace BotSharp.Core.Loader -{ - /// - /// Implement a customzied loader - /// - public interface IInitializationLoader - { - int Priority { get; } - void Initialize(); - } -} diff --git a/BotSharp.Core/Loader/InitializationLoader.cs b/BotSharp.Core/Loader/InitializationLoader.cs deleted file mode 100644 index 393ede7d..00000000 --- a/BotSharp.Core/Loader/InitializationLoader.cs +++ /dev/null @@ -1,34 +0,0 @@ -using DotNetToolkit; -using EntityFrameworkCore.BootKit; -using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.Core.Loader -{ - public class InitializationLoader - { - public void Load() - { - DateTime startAll = DateTime.UtcNow; - Console.WriteLine(); - Console.WriteLine($"*** *** *** *** InitializationLoader running *** *** *** ***"); - Console.WriteLine(); - - var coreLoaders = TypeHelper.GetInstanceWithInterface(Database.Assemblies); - - coreLoaders.ForEach(loader => - { - DateTime start = DateTime.UtcNow; - Console.WriteLine($"{loader.ToString()} P:{loader.Priority}..."); - loader.Initialize(); - Console.WriteLine($"{loader.ToString()} completed in {(DateTime.UtcNow - start).TotalSeconds} s."); - Console.WriteLine(); - }); - - Console.WriteLine($"*** *** *** *** InitializationLoader completed in {(DateTime.UtcNow - startAll).TotalSeconds} s. *** *** *** ***"); - Console.WriteLine(); - } - } -} diff --git a/BotSharp.RestApi/IntentController.cs b/BotSharp.RestApi/IntentController.cs deleted file mode 100644 index a8aae9c1..00000000 --- a/BotSharp.RestApi/IntentController.cs +++ /dev/null @@ -1,26 +0,0 @@ -using BotSharp.Core.Agents; -using BotSharp.Core.Intents; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.Core.RestApi -{ - public class IntentController : EssentialController - { - /// - /// User intent, a intent is connected to a dialog flow. - /// - /// - /// - /// - [HttpPost("{agentId}")] - public string CreateIntent([FromRoute] String agentId, [FromBody] Intent intent) - { - var agent = dc.Agent(agentId); - - return intent.Id; - } - } -} diff --git a/BotSharp.UnitTest/AgentTest.cs b/BotSharp.UnitTest/AgentTest.cs index 52097c1b..5ead8885 100644 --- a/BotSharp.UnitTest/AgentTest.cs +++ b/BotSharp.UnitTest/AgentTest.cs @@ -14,7 +14,7 @@ namespace BotSharp.UnitTest public class AgentTest : TestEssential { [TestMethod] - public void CreateAgentTes() + public void CreateAgentTest() { var agent = new Agent { @@ -63,7 +63,9 @@ namespace BotSharp.UnitTest var rasa = new RasaAi(dc, config); rasa.agent = rasa.LoadAgent(); - rasa.Train(dc); + string msg = rasa.Train(dc); + + Assert.IsTrue(!String.IsNullOrEmpty(msg)); } } } diff --git a/BotSharp.UnitTest/BotSharp.UnitTest.csproj b/BotSharp.UnitTest/BotSharp.UnitTest.csproj index 57562107..7f6c8ca5 100644 --- a/BotSharp.UnitTest/BotSharp.UnitTest.csproj +++ b/BotSharp.UnitTest/BotSharp.UnitTest.csproj @@ -6,6 +6,232 @@ false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + @@ -22,4 +248,8 @@ + + + + diff --git a/BotSharp.UnitTest/IntentTest.cs b/BotSharp.UnitTest/IntentTest.cs index 75d9864b..67009a6d 100644 --- a/BotSharp.UnitTest/IntentTest.cs +++ b/BotSharp.UnitTest/IntentTest.cs @@ -17,9 +17,6 @@ namespace BotSharp.UnitTest config.SessionId = Guid.NewGuid().ToString(); var rasa = new RasaAi(dc, config); - - var response = rasa.TextRequest(new AIRequest { Query = new String[] { "Hello" } }); - Assert.IsTrue(response.Result.Metadata.IntentName == "greeting"); } } } diff --git a/BotSharp.UnitTest/Settings/settings.app.json b/BotSharp.UnitTest/Settings/settings.app.json new file mode 100644 index 00000000..26bb0ac7 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.app.json @@ -0,0 +1,15 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Warning" + } + }, + "Console": { + "LogLevel": { + "Default": "Warning" + } + } + } +} diff --git a/BotSharp.UnitTest/Settings/settings.auth.json b/BotSharp.UnitTest/Settings/settings.auth.json new file mode 100644 index 00000000..c8d08264 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.auth.json @@ -0,0 +1,11 @@ +{ + "TokenAuthentication": { + "SecretKey": "QEMYOjgkNEq/krV1Ouzz8w==", + "Subject": "OpenBotKit", + "Issuer": "Haiping Chen", + "Audience": "Haiping Chen", + "TokenPath": "/token", + "CookieName": "token", + "LoginPath": "/login" + } +} \ No newline at end of file diff --git a/BotSharp.UnitTest/Settings/settings.aws.json b/BotSharp.UnitTest/Settings/settings.aws.json new file mode 100644 index 00000000..88d79907 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.aws.json @@ -0,0 +1,10 @@ +{ + "AWS": { + "AWSRegionEndPoint": "us-east-1", + "AWSSecretKey": "", + "AWSAccessKey": "", + "AWSEncoding": "utf-8", + "SESVerifiedEmail": "", + "AWSBucketPrefix": "" + } +} \ No newline at end of file diff --git a/BotSharp.UnitTest/Settings/settings.bot.json b/BotSharp.UnitTest/Settings/settings.bot.json new file mode 100644 index 00000000..96cf8614 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.bot.json @@ -0,0 +1,5 @@ +{ + "Rasa": { + "Host": "http://rasa.local:5000" + } +} diff --git a/BotSharp.UnitTest/Settings/settings.db.json b/BotSharp.UnitTest/Settings/settings.db.json new file mode 100644 index 00000000..ebd2d785 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.db.json @@ -0,0 +1,10 @@ +{ + "Database": { + "Default": "SqlServer", + "ConnectionStrings": { + "InMemory": "DataSource=:memory:", + "Sqlite": "Data Source=|DataDirectory|BotSharp.db;", + "SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" + } + } +} \ No newline at end of file diff --git a/BotSharp.UnitTest/Settings/settings.swagger.json b/BotSharp.UnitTest/Settings/settings.swagger.json new file mode 100644 index 00000000..a1a35359 --- /dev/null +++ b/BotSharp.UnitTest/Settings/settings.swagger.json @@ -0,0 +1,12 @@ +{ + "Swagger": { + "Version": "v1", + "Title": "Open Chatbot Kit", + "Description": "OpenBotKit API", + "TermsOfService": "MIT", + "Contact": { + "Name": "Haiping Chen", + "Email": "haiping008@gmail.com" + } + } +} diff --git a/BotSharp.UnitTest/TestEssential.cs b/BotSharp.UnitTest/TestEssential.cs index ba69ddde..eff7a16e 100644 --- a/BotSharp.UnitTest/TestEssential.cs +++ b/BotSharp.UnitTest/TestEssential.cs @@ -10,17 +10,17 @@ namespace BotSharp.UnitTest { public abstract class TestEssential { - public static String BOT_ID = "2b6a288e-d891-40c6-96ce-6a0cf324545c"; + public static String BOT_ID = "fd9f1b29-fed8-4c68-8fda-69ab463da126"; public static String BOT_CLIENT_TOKEN = "23a53c46d6244840bbb10c89c171d299"; public static String BOT_DEVELOPER_TOKEN = "d86103f446d049ff8d5f506e8dfe5f3f"; - public static String BOT_NAME = "VirtualAssistant"; + public static String BOT_NAME = "Voicebot"; protected Database dc { get; set; } protected string contentRoot; public TestEssential() { - contentRoot = $"{Directory.GetCurrentDirectory()}\\..\\..\\..\\..\\Bot.WebStarter"; + contentRoot = $"{Directory.GetCurrentDirectory()}\\..\\..\\..\\"; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); var settings = Directory.GetFiles(contentRoot + "/Settings/", "*.json"); diff --git a/BotSharp.WebStarter/BotSharp.WebStarter.csproj b/BotSharp.WebStarter/BotSharp.WebStarter.csproj index a7506ff4..9018544b 100644 --- a/BotSharp.WebStarter/BotSharp.WebStarter.csproj +++ b/BotSharp.WebStarter/BotSharp.WebStarter.csproj @@ -14,17 +14,13 @@ - - - + + + - - - - diff --git a/BotSharp.sln b/BotSharp.sln index 0f013937..aa03dd53 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -7,9 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.UnitTest", "BotSha EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Core", "BotSharp.Core\BotSharp.Core.csproj", "{95780673-2A1A-4953-962F-C46CBFDD07FF}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.RestApi", "BotSharp.RestApi\BotSharp.RestApi.csproj", "{A1DDFCE9-10A1-45AF-8F1C-DE0A9145BA48}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.WebStarter", "BotSharp.WebStarter\BotSharp.WebStarter.csproj", "{C1B91383-D548-4906-BEB8-FED7E320731B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit", "..\DotNetToolkit\DotNetToolkit\DotNetToolkit.csproj", "{AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,14 +23,10 @@ Global {95780673-2A1A-4953-962F-C46CBFDD07FF}.Debug|Any CPU.Build.0 = Debug|Any CPU {95780673-2A1A-4953-962F-C46CBFDD07FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {95780673-2A1A-4953-962F-C46CBFDD07FF}.Release|Any CPU.Build.0 = Release|Any CPU - {A1DDFCE9-10A1-45AF-8F1C-DE0A9145BA48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A1DDFCE9-10A1-45AF-8F1C-DE0A9145BA48}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A1DDFCE9-10A1-45AF-8F1C-DE0A9145BA48}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A1DDFCE9-10A1-45AF-8F1C-DE0A9145BA48}.Release|Any CPU.Build.0 = Release|Any CPU - {C1B91383-D548-4906-BEB8-FED7E320731B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C1B91383-D548-4906-BEB8-FED7E320731B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C1B91383-D548-4906-BEB8-FED7E320731B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C1B91383-D548-4906-BEB8-FED7E320731B}.Release|Any CPU.Build.0 = Release|Any CPU + {AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AF20AEA3-8922-4CDC-B0E3-4DD085B9954C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE