Skip to content

Commit 3c7c3c1

Browse files
committed
add ContextStorageFactory
persist context to file
1 parent 0520edd commit 3c7c3c1

File tree

17 files changed

+146
-28
lines changed

17 files changed

+146
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,4 @@ __pycache__/
291291
/docs/_build
292292
*.RestApi.xml
293293
/BotSharp.WebHost/App_Data/AgentStorage
294+
/BotSharp.WebHost/App_Data/SessionStorage

BotSharp.Core/AgentStorage/AgentStorageFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public AgentStorageFactory(IPlatformSettings setting, Func<string, IAgentStorage
1919
this.platformSetting = setting;
2020
}
2121

22-
public async Task<IAgentStorage<TAgent>> Get()
22+
public IAgentStorage<TAgent> Get()
2323
{
2424
IAgentStorage<TAgent> storage = null;
2525
string storageName = this.platformSetting.AgentStorage;

BotSharp.Core/AgentStorage/AgentStorageInFile.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ namespace BotSharp.Core.AgentStorage
1616
public class AgentStorageInFile<TAgent> : IAgentStorage<TAgent>
1717
where TAgent : AgentBase
1818
{
19-
private static CSRedisClient csredis;
20-
private static string prefix = String.Empty;
21-
2219
private static string storageDir;
2320

2421
public AgentStorageInFile()
@@ -27,7 +24,7 @@ public AgentStorageInFile()
2724
var db = config.GetSection("Database:Default").Value;
2825
storageDir = config.GetSection($"Database:ConnectionStrings:{db}").Value;
2926
string contentDir = AppDomain.CurrentDomain.GetData("DataPath").ToString();
30-
storageDir = storageDir.Replace("|DataDirectory|", contentDir + Path.DirectorySeparatorChar);
27+
storageDir = storageDir.Replace("|DataDirectory|", contentDir + Path.DirectorySeparatorChar + "AgentStorage" + Path.DirectorySeparatorChar);
3128

3229
if (!Directory.Exists(storageDir))
3330
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using BotSharp.Platform.Abstraction;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BotSharp.Core.ContextStorage
8+
{
9+
public class ContextStorageFactory<T> : IContextStorageFactory<T>
10+
{
11+
private readonly Func<string, IContextStorage<T>> func;
12+
private readonly IPlatformSettings platformSetting;
13+
14+
public ContextStorageFactory(IPlatformSettings setting, Func<string, IContextStorage<T>> serviceAccessor)
15+
{
16+
this.func = serviceAccessor;
17+
this.platformSetting = setting;
18+
}
19+
20+
public IContextStorage<T> Get()
21+
{
22+
IContextStorage<T> storage = null;
23+
string storageName = this.platformSetting.ContextStorage;
24+
storage = func(storageName);
25+
return storage as IContextStorage<T>;
26+
}
27+
}
28+
}
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
using BotSharp.Platform.Abstraction;
2+
using Microsoft.Extensions.Configuration;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Serialization;
25
using System;
36
using System.Collections.Generic;
7+
using System.IO;
48
using System.Text;
9+
using System.Threading.Tasks;
510

611
namespace BotSharp.Core.ContextStorage
712
{
8-
public class ContextStorageInFile : IContextStorage
13+
public class ContextStorageInFile<T> : IContextStorage<T>
914
{
15+
private static string storageDir;
1016

17+
public ContextStorageInFile()
18+
{
19+
IConfiguration config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
20+
var db = config.GetSection("Database:Default").Value;
21+
storageDir = config.GetSection($"Database:ConnectionStrings:{db}").Value;
22+
string contentDir = AppDomain.CurrentDomain.GetData("DataPath").ToString();
23+
storageDir = storageDir.Replace("|DataDirectory|", contentDir + Path.DirectorySeparatorChar + "SessionStorage" + Path.DirectorySeparatorChar);
24+
25+
if (!Directory.Exists(storageDir))
26+
{
27+
Directory.CreateDirectory(storageDir);
28+
}
29+
}
30+
31+
public async Task<bool> Persist(string sessionId, T[] context)
32+
{
33+
var json = JsonConvert.SerializeObject(context, new JsonSerializerSettings
34+
{
35+
NullValueHandling = NullValueHandling.Ignore,
36+
Formatting = Formatting.Indented,
37+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
38+
Converters = new List<JsonConverter>
39+
{
40+
new Newtonsoft.Json.Converters.StringEnumConverter()
41+
}
42+
});
43+
44+
string dataPath = Path.Combine(storageDir, sessionId + ".json");
45+
46+
File.WriteAllText(dataPath, json);
47+
48+
return true;
49+
}
1150
}
1251
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using BotSharp.Platform.Abstraction;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace BotSharp.Core.ContextStorage
8+
{
9+
public class ContextStorageServiceRegister
10+
{
11+
public static void Register<T>(IServiceCollection services)
12+
{
13+
services.AddSingleton<IContextStorageFactory<T>, ContextStorageFactory<T>>();
14+
15+
services.AddSingleton<ContextStorageInFile<T>>();
16+
17+
services.AddSingleton(factory =>
18+
{
19+
Func<string, IContextStorage<T>> accesor = key =>
20+
{
21+
if (key.Equals("ContextStorageInFile"))
22+
{
23+
return factory.GetService<ContextStorageInFile<T>>();
24+
}
25+
else
26+
{
27+
throw new ArgumentException($"Not Support key : {key}");
28+
}
29+
};
30+
31+
return accesor;
32+
});
33+
}
34+
}
35+
}

BotSharp.Core/PlatformBuilderBase.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ public PlatformBuilderBase(IAgentStorageFactory<TAgent> agentStorageFactory, IPl
3232
{
3333
this.agentStorageFactory = agentStorageFactory;
3434
this.settings = settings;
35+
GetAgentStorage();
3536
}
3637

3738
public async Task<List<TAgent>> GetAllAgents()
3839
{
39-
await GetStorage();
40-
4140
return await Storage.Query();
4241
}
4342

@@ -80,15 +79,11 @@ private AgentImportHeader LoadMeta(string dataDir)
8079

8180
public async Task<TAgent> GetAgentById(string agentId)
8281
{
83-
GetStorage();
84-
8582
return await Storage.FetchById(agentId);
8683
}
8784

8885
public async Task<TAgent> GetAgentByName(string agentName)
8986
{
90-
await GetStorage();
91-
9287
return await Storage.FetchByName(agentName);
9388
}
9489

@@ -195,7 +190,7 @@ public virtual async Task<TResult> TextRequest<TResult>(AiRequest request)
195190

196191
Console.WriteLine($"TextResponse: {aiResponse.Intent}, {request.SessionId}");
197192

198-
return await AssembleResult<TResult>(aiResponse);
193+
return await AssembleResult<TResult>(request, aiResponse);
199194
}
200195

201196
public virtual async Task<TextClassificationResult> FallbackResponse(AiRequest request)
@@ -222,27 +217,25 @@ public virtual async Task<TextClassificationResult> FallbackResponse(AiRequest r
222217
}
223218
}
224219

225-
public virtual async Task<TResult> AssembleResult<TResult>(AiResponse response)
220+
public virtual async Task<TResult> AssembleResult<TResult>(AiRequest request, AiResponse response)
226221
{
227222
throw new NotImplementedException();
228223
}
229224

230225
public virtual async Task<bool> SaveAgent(TAgent agent)
231226
{
232-
await GetStorage();
233-
234227
// default save agent in FileStorage
235228
await Storage.Persist(agent);
236229

237230
return true;
238231
}
239-
240-
protected async Task<IAgentStorage<TAgent>> GetStorage()
232+
protected IAgentStorage<TAgent> GetAgentStorage()
241233
{
242234
if (Storage == null)
243235
{
244-
Storage = await agentStorageFactory.Get();
236+
Storage = agentStorageFactory.Get();
245237
}
238+
246239
return Storage;
247240
}
248241
}

BotSharp.Core/PlatformSettingsBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ public PlatformSettingsBase()
1616
{
1717
BotEngine = "BotSharpNLU";
1818
AgentStorage = "AgentStorageInFile";
19+
ContextStorage = "ContextStorageInFile";
1920
}
2021

2122
public string BotEngine { get; set; }
2223

24+
public string ContextStorage { get; set; }
25+
2326
public string AgentStorage { get; set; }
2427
}
2528

BotSharp.Platform.Abstraction/IAgentStorageFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace BotSharp.Platform.Abstraction
66
{
77
public interface IAgentStorageFactory<TAgent> where TAgent : AgentBase
88
{
9-
Task<IAgentStorage<TAgent>> Get();
9+
IAgentStorage<TAgent> Get();
1010
}
1111
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45

56
namespace BotSharp.Platform.Abstraction
67
{
7-
public interface IContextStorage
8+
public interface IContextStorage<T>
89
{
10+
Task<bool> Persist(string sessionId, T[] context);
911
}
1012
}

0 commit comments

Comments
 (0)