add AgentStorageInFile
This commit is contained in:
parent
225b945d65
commit
086d297f52
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -290,3 +290,4 @@ __pycache__/
|
|||
/Data
|
||||
/docs/_build
|
||||
*.RestApi.xml
|
||||
/BotSharp.WebHost/App_Data/AgentStorage
|
||||
|
|
|
|||
115
BotSharp.Core/AgentStorage/AgentStorageInFile.cs
Normal file
115
BotSharp.Core/AgentStorage/AgentStorageInFile.cs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
using BotSharp.Platform.Abstraction;
|
||||
using BotSharp.Platform.Models;
|
||||
using CSRedis;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Core.AgentStorage
|
||||
{
|
||||
public class AgentStorageInFile<TAgent> : IAgentStorage<TAgent>
|
||||
where TAgent : AgentBase
|
||||
{
|
||||
private static CSRedisClient csredis;
|
||||
private static string prefix = String.Empty;
|
||||
|
||||
private static string storageDir;
|
||||
|
||||
public AgentStorageInFile()
|
||||
{
|
||||
IConfiguration config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
|
||||
var db = config.GetSection("Database:Default").Value;
|
||||
storageDir = config.GetSection($"Database:ConnectionStrings:{db}").Value;
|
||||
string contentDir = AppDomain.CurrentDomain.GetData("DataPath").ToString();
|
||||
storageDir = storageDir.Replace("|DataDirectory|", contentDir + Path.DirectorySeparatorChar);
|
||||
|
||||
if (!Directory.Exists(storageDir))
|
||||
{
|
||||
Directory.CreateDirectory(storageDir);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TAgent> FetchById(string agentId)
|
||||
{
|
||||
string dataPath = Path.Combine(storageDir, agentId + ".json");
|
||||
if (File.Exists(dataPath))
|
||||
{
|
||||
string json = File.ReadAllText(dataPath);
|
||||
return JsonConvert.DeserializeObject<TAgent>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(TAgent);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TAgent> FetchByName(string agentName)
|
||||
{
|
||||
var files = Directory.GetFiles(storageDir);
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
var file = files[i];
|
||||
string json = File.ReadAllText(file);
|
||||
var agent = JsonConvert.DeserializeObject<TAgent>(json);
|
||||
if (agent.Name.ToLower() == agentName.ToLower())
|
||||
{
|
||||
return agent;
|
||||
}
|
||||
}
|
||||
|
||||
return default(TAgent);
|
||||
}
|
||||
|
||||
public async Task<bool> Persist(TAgent agent)
|
||||
{
|
||||
if (String.IsNullOrEmpty(agent.Id))
|
||||
{
|
||||
agent.Id = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
var json = JsonConvert.SerializeObject(agent, new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
});
|
||||
|
||||
string dataPath = Path.Combine(storageDir, agent.Id + ".json");
|
||||
|
||||
File.WriteAllText(dataPath, json);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<int> PurgeAllAgents()
|
||||
{
|
||||
var files = Directory.GetFiles(storageDir);
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
File.Delete(files[i]);
|
||||
}
|
||||
|
||||
return files.Length;
|
||||
}
|
||||
|
||||
public async Task<List<TAgent>> Query()
|
||||
{
|
||||
var agents = new List<TAgent>();
|
||||
|
||||
var files = Directory.GetFiles(storageDir);
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
var file = files[i];
|
||||
string json = File.ReadAllText(file);
|
||||
var agent = JsonConvert.DeserializeObject<TAgent>(json);
|
||||
agents.Add(agent);
|
||||
}
|
||||
|
||||
return agents;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ namespace BotSharp.Core.AgentStorage
|
|||
|
||||
services.AddSingleton<AgentStorageInMemory<TAgent>>();
|
||||
services.AddSingleton<AgentStorageInRedis<TAgent>>();
|
||||
services.AddSingleton<AgentStorageInFile<TAgent>>();
|
||||
|
||||
services.AddSingleton(factory =>
|
||||
{
|
||||
|
|
@ -30,6 +31,10 @@ namespace BotSharp.Core.AgentStorage
|
|||
{
|
||||
return factory.GetService<AgentStorageInMemory<TAgent>>();
|
||||
}
|
||||
else if (key.Equals("AgentStorageInFile"))
|
||||
{
|
||||
return factory.GetService<AgentStorageInFile<TAgent>>();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Not Support key : {key}");
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ If you feel that this project is helpful to you, please Star on the project, we
|
|||
<RepositoryType>MIT</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl>
|
||||
<PackageTags>NLU, Chatbot, Bot, AI Bot, Artificial Intelligence</PackageTags>
|
||||
<Version>1.7.1</Version>
|
||||
<Version>1.7.2</Version>
|
||||
<PackageReleaseNotes>Monthly Update.
|
||||
Integrated with Tencent Wechat.
|
||||
If you feel that this project is helpful to you, please Star on the project, we will be very grateful.</PackageReleaseNotes>
|
||||
<Copyright>Since 2018 Haiping Chen</Copyright>
|
||||
<PackageProjectUrl>https://github.com/Oceania2018/BotSharp</PackageProjectUrl>
|
||||
<AssemblyVersion>1.7.1.0</AssemblyVersion>
|
||||
<AssemblyVersion>1.7.2.0</AssemblyVersion>
|
||||
<PackageIconUrl>https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png</PackageIconUrl>
|
||||
<PackageLicenseUrl>https://github.com/Oceania2018/BotSharp/blob/master/LICENSE</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,11 @@ namespace BotSharp.Core.Modules
|
|||
throw new ArgumentNullException(nameof(configuration));
|
||||
ModulesOptions options = configuration.Get<ModulesOptions>();
|
||||
|
||||
if (options.Modules.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"Platform emulator not found.", Color.Red);
|
||||
}
|
||||
|
||||
this._modules = options.Modules
|
||||
.Select(s =>
|
||||
{
|
||||
|
|
@ -48,6 +53,7 @@ namespace BotSharp.Core.Modules
|
|||
else
|
||||
{
|
||||
IModule module = (IModule)Activator.CreateInstance(type);
|
||||
Console.WriteLine($"Loaded module \"{s.Type}\"", Color.Green);
|
||||
return module;
|
||||
}
|
||||
}
|
||||
|
|
@ -82,6 +88,7 @@ namespace BotSharp.Core.Modules
|
|||
{
|
||||
foreach (IModule module in this._modules)
|
||||
{
|
||||
if (module == null) continue;
|
||||
module.Configure(app, env);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ namespace BotSharp.Core
|
|||
|
||||
public async Task<TAgent> LoadAgentFromFile<TImporter>(string dataDir) where TImporter : IAgentImporter<TAgent>, new()
|
||||
{
|
||||
Console.WriteLine($"Loading agent from folder {dataDir}");
|
||||
|
||||
var meta = LoadMeta(dataDir);
|
||||
var importer = new TImporter
|
||||
{
|
||||
|
|
@ -54,6 +56,8 @@ namespace BotSharp.Core
|
|||
// Load system buildin entities
|
||||
await importer.LoadBuildinEntities(agent);
|
||||
|
||||
Console.WriteLine($"Loaded agent: {agent.Name} {agent.Id}");
|
||||
|
||||
return agent;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ namespace BotSharp.Core
|
|||
public PlatformSettingsBase()
|
||||
{
|
||||
BotEngine = "BotSharpNLU";
|
||||
AgentStorage = "AgentStorageInMemory";
|
||||
AgentStorage = "AgentStorageInFile";
|
||||
}
|
||||
|
||||
public string BotEngine { get; set; }
|
||||
|
|
|
|||
|
|
@ -30,11 +30,13 @@
|
|||
|
||||
<ItemGroup>
|
||||
<Compile Remove="App_Data\AgentArchive\**" />
|
||||
<Compile Remove="App_Data\AgentStorage\**" />
|
||||
<Compile Remove="App_Data\Corpus\**" />
|
||||
<Compile Remove="App_Data\Projects\**" />
|
||||
<Compile Remove="PublishOutput\**" />
|
||||
<Compile Remove="publish\**" />
|
||||
<Content Remove="App_Data\AgentArchive\**" />
|
||||
<Content Remove="App_Data\AgentStorage\**" />
|
||||
<Content Remove="App_Data\Corpus\**" />
|
||||
<Content Remove="App_Data\DbInitializer\**" />
|
||||
<Content Remove="App_Data\Projects\**" />
|
||||
|
|
@ -42,11 +44,13 @@
|
|||
<Content Remove="PublishOutput\**" />
|
||||
<Content Remove="publish\**" />
|
||||
<EmbeddedResource Remove="App_Data\AgentArchive\**" />
|
||||
<EmbeddedResource Remove="App_Data\AgentStorage\**" />
|
||||
<EmbeddedResource Remove="App_Data\Corpus\**" />
|
||||
<EmbeddedResource Remove="App_Data\Projects\**" />
|
||||
<EmbeddedResource Remove="PublishOutput\**" />
|
||||
<EmbeddedResource Remove="publish\**" />
|
||||
<None Remove="App_Data\AgentArchive\**" />
|
||||
<None Remove="App_Data\AgentStorage\**" />
|
||||
<None Remove="App_Data\Corpus\**" />
|
||||
<None Remove="App_Data\DbInitializer\**" />
|
||||
<None Remove="App_Data\Projects\**" />
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
// if you want to override platform setting, please set corresponding value, otherwise you don't need this section.
|
||||
"dialogflowAi": {
|
||||
"botEngine": "BotSharpNLU",
|
||||
"agentStorage": "AgentStorageInRedis"
|
||||
}
|
||||
"dialogflowAi": {
|
||||
"botEngine": "BotSharpNLU",
|
||||
"agentStorage": "AgentStorageInFile"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,19 +4,11 @@
|
|||
|
||||
"platformModuleName": "DialogflowAi",
|
||||
|
||||
"machineLearning": {
|
||||
"dataDir": "D:\\Projects\\BotSharp\\Data"
|
||||
},
|
||||
|
||||
"modules": [
|
||||
{
|
||||
/*{
|
||||
"Name": "DialogflowAi",
|
||||
"Type": "BotSharp.Platform.Dialogflow"
|
||||
},
|
||||
{
|
||||
"Name": "WeixinChannel",
|
||||
"Type": "BotSharp.Channel.Weixin"
|
||||
}/*,
|
||||
{
|
||||
"Name": "RasaAi",
|
||||
"Type": "BotSharp.Platform.Rasa",
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
{
|
||||
"Database": {
|
||||
"Default": "Redis",
|
||||
"Default": "File",
|
||||
"ConnectionStrings": {
|
||||
"InMemory": "DataSource=:memory:",
|
||||
"Redis": "127.0.0.1:6379,defaultDatabase=BotSharp,poolsize=50,ssl=false,writeBuffer=10240,prefix=agent_",
|
||||
"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"
|
||||
"SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
|
||||
"File": "|DataDirectory|AgentStorage"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,7 +40,7 @@ namespace BotSharp.WebHost
|
|||
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||
});
|
||||
|
||||
PlatformModuleAssembyLoader.LoadAssemblies(Configuration, assembly => mvcBuilder.AddApplicationPart(assembly));
|
||||
//PlatformModuleAssembyLoader.LoadAssemblies(Configuration, assembly => mvcBuilder.AddApplicationPart(assembly));
|
||||
|
||||
this.modulesStartup.ConfigureServices(services);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue