Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions BotSharp.Core/AgentStorageInMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotSharp.Core
{
Expand All @@ -20,7 +21,7 @@ public AgentStorageInMemory()
if (agents == null) agents = new Dictionary<string, TAgent>();
}

public TAgent FetchById(string agentId)
public async Task<TAgent> FetchById(string agentId)
{
if (agents.ContainsKey(agentId))
{
Expand All @@ -32,14 +33,14 @@ public TAgent FetchById(string agentId)
}
}

public TAgent FetchByName(string agentName)
public async Task<TAgent> FetchByName(string agentName)
{
var data = agents.FirstOrDefault(x => x.Value.Name == agentName);

return data.Value;
}

public bool Persist(TAgent agent)
public async Task<bool> Persist(TAgent agent)
{
if (String.IsNullOrEmpty(agent.Id))
{
Expand All @@ -54,7 +55,7 @@ public bool Persist(TAgent agent)
return true;
}

public int PurgeAllAgents()
public async Task<int> PurgeAllAgents()
{
int count = agents.Count;

Expand All @@ -63,7 +64,7 @@ public int PurgeAllAgents()
return count;
}

public List<TAgent> Query()
public async Task<List<TAgent>> Query()
{
return agents.Select(x => x.Value).ToList();
}
Expand Down
11 changes: 6 additions & 5 deletions BotSharp.Core/AgentStorageInRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotSharp.Core
{
Expand All @@ -30,7 +31,7 @@ public AgentStorageInRedis()
}
}

public TAgent FetchById(string agentId)
public async Task<TAgent> FetchById(string agentId)
{
var key = agentId;
if (csredis.Exists(key))
Expand All @@ -43,7 +44,7 @@ public TAgent FetchById(string agentId)
}
}

public TAgent FetchByName(string agentName)
public async Task<TAgent> FetchByName(string agentName)
{
var agents = new List<TAgent>();

Expand All @@ -62,7 +63,7 @@ public TAgent FetchByName(string agentName)
return default(TAgent);
}

public bool Persist(TAgent agent)
public async Task<bool> Persist(TAgent agent)
{
if (String.IsNullOrEmpty(agent.Id))
{
Expand All @@ -80,7 +81,7 @@ public bool Persist(TAgent agent)
return true;
}

public int PurgeAllAgents()
public async Task<int> PurgeAllAgents()
{
var keys = csredis.Keys($"{prefix}*");

Expand All @@ -89,7 +90,7 @@ public int PurgeAllAgents()
return keys.Count();
}

public List<TAgent> Query()
public async Task<List<TAgent>> Query()
{
var agents = new List<TAgent>();

Expand Down
1 change: 1 addition & 0 deletions BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ If you feel that this project is helpful to you, please Star on the project, we
<PackageReference Include="DotNetToolkit" Version="1.6.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.9.1" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="RestSharp" Version="106.4.2" />
</ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions BotSharp.Core/Engines/BotEngineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ namespace BotSharp.Core.Engines
/// </summary>
public abstract class BotEngineBase
{
protected Database dc;
protected Database Dc;

protected AgentBase agent { get; set; }
protected AgentBase Agent { get; set; }

public BotEngineBase()
{
dc = new DefaultDataContextLoader().GetDefaultDc();
Dc = new DefaultDataContextLoader().GetDefaultDc();
}

public AiResponse TextRequest(AiRequest request)
public async Task<AiResponse> TextRequest(AiRequest request)
{
var preditor = new BotPredictor();
var doc = preditor.Predict(agent, new AiRequest
var doc = preditor.Predict(Agent, new AiRequest
{
AgentDir = request.AgentDir,
Model = request.Model,
Expand Down
4 changes: 2 additions & 2 deletions BotSharp.Core/Engines/BotSharp/BotSharpNLU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class BotSharpNLU : BotEngineBase, IBotEngine
{
public override async Task Train(BotTrainOptions options)
{
var trainer = new BotTrainer(agent.Id, dc);
await trainer.Train(agent, options);
var trainer = new BotTrainer(Agent.Id, Dc);
await trainer.Train(Agent, options);
}
}
}
4 changes: 2 additions & 2 deletions BotSharp.Core/Engines/BotTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace BotSharp.Core.Engines
{
public class BotTrainer
{
private Database dc;
private readonly Database dc;

private string agentId;
private readonly string agentId;

public BotTrainer()
{
Expand Down
11 changes: 11 additions & 0 deletions BotSharp.Core/IAgentStorageFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using System.Threading.Tasks;

namespace BotSharp.Core
{
public interface IAgentStorageFactory
{
Task<IAgentStorage<TAgent>> Get<TAgent>() where TAgent : AgentBase;
}
}
32 changes: 32 additions & 0 deletions BotSharp.Core/Modules/IModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BotSharp.Core.Modules
{
/// <summary>
/// Represents a configurable module containing multiple servies.
/// </summary>
public interface IModule
{
/// <summary>
/// Configurates the module services.
/// </summary>
/// <param name="services">
/// Instance of <see cref="IServiceCollection"/>.
/// </param>
/// <returns>
/// Instance of <see cref="IServiceProvider"/>.
/// </returns>
void ConfigureServices(IServiceCollection services, IConfiguration configuration);

/// <summary>
/// Configures module services.
/// </summary>
/// <param name="app">
/// Instance of <see cref="IApplicationBuilder"/>.
/// </param>
void Configure(IApplicationBuilder app);
}
}
17 changes: 17 additions & 0 deletions BotSharp.Core/Modules/ModuleOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Core.Modules
{
/// <summary>
/// Module configuration .
/// </summary>
public class ModuleOptions
{
/// <summary>
/// Module type.
/// </summary>
public string Type { get; set; }
}
}
17 changes: 17 additions & 0 deletions BotSharp.Core/Modules/ModulesOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Core.Modules
{
/// <summary>
/// Module Host configuration.
/// </summary>
public class ModulesOptions
{
/// <summary>
/// List of module configurations.
/// </summary>
public List<ModuleOptions> Modules { get; set; }
}
}
79 changes: 79 additions & 0 deletions BotSharp.Core/Modules/ModulesStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BotSharp.Core.Modules
{
/// <summary>
/// Startup class for configurable modules
/// </summary>
public class ModulesStartup
{
private readonly IEnumerable<IModule> _modules;
private readonly IConfiguration _configuration;

/// <summary>
/// Create an instance of <see cref="ModulesStartup"/>
/// </summary>
/// <param name="configuration">
/// Application configuration containing modules configuration
/// </param>
public ModulesStartup(IConfiguration configuration)
{
this._configuration = configuration ??
throw new ArgumentNullException(nameof(configuration));
ModulesOptions options = configuration.Get<ModulesOptions>();

this._modules = options.Modules
.Select(s =>
{
Type type = Type.GetType(s.Type);

if (type == null)
{
throw new TypeLoadException(
$"Cannot load type \"{s.Type}\"");
}

IModule module = (IModule)Activator.CreateInstance(type);
return module;
}
);
}

/// <summary>
/// Configurates the services.
/// </summary>
/// <param name="services">
/// Instance of <see cref="IServiceCollection"/>.
/// </param>
/// <returns>
/// Instance of <see cref="IServiceProvider"/>.
/// </returns>
public void ConfigureServices(IServiceCollection services)
{
foreach (IModule module in this._modules)
{
module.ConfigureServices(services, this._configuration);
}
}

/// <summary>
/// Configures module services.
/// </summary>
/// <param name="app">
/// Instance of <see cref="IApplicationBuilder"/>.
/// </param>
public void Configure(IApplicationBuilder app)
{
foreach (IModule module in this._modules)
{
module.Configure(app);
}
}
}
}
17 changes: 17 additions & 0 deletions BotSharp.Core/NLUSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Core
{
public class NLUSetting
{

public string BotEngine { get; set; }

public string AgentStorage { get; set; }
}

}
Loading