2023-08-19 00:15:50 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
|
|
|
|
using BotSharp.Core.Functions;
|
2023-08-28 15:24:43 +00:00
|
|
|
using BotSharp.Core.Plugins.Knowledges.Services;
|
2023-05-27 01:58:31 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2023-06-03 02:07:30 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-07-21 22:17:16 +00:00
|
|
|
|
2023-05-27 01:58:31 +00:00
|
|
|
namespace BotSharp.Core;
|
|
|
|
|
|
|
|
|
|
public static class BotSharpServiceCollectionExtensions
|
|
|
|
|
{
|
2023-06-03 14:57:08 +00:00
|
|
|
public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
2023-06-11 23:46:02 +00:00
|
|
|
services.AddScoped<IUserService, UserService>();
|
2023-06-23 04:43:00 +00:00
|
|
|
|
2023-06-11 23:46:02 +00:00
|
|
|
services.AddScoped<IAgentService, AgentService>();
|
2023-06-23 04:43:00 +00:00
|
|
|
|
2023-07-21 22:17:16 +00:00
|
|
|
var agentSettings = new AgentSettings();
|
|
|
|
|
config.Bind("Agent", agentSettings);
|
|
|
|
|
services.AddSingleton((IServiceProvider x) => agentSettings);
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
var convsationSettings = new ConversationSetting();
|
|
|
|
|
config.Bind("Conversation", convsationSettings);
|
|
|
|
|
services.AddSingleton((IServiceProvider x) => convsationSettings);
|
2023-06-23 04:43:00 +00:00
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
services.AddScoped<IConversationStorage, ConversationStorage>();
|
2023-06-12 13:28:49 +00:00
|
|
|
services.AddScoped<IConversationService, ConversationService>();
|
2023-08-09 21:49:55 +00:00
|
|
|
services.AddScoped<IConversationStateService, ConversationStateService>();
|
2023-06-18 18:15:00 +00:00
|
|
|
|
2023-07-21 21:56:14 +00:00
|
|
|
var databaseSettings = new DatabaseSettings();
|
|
|
|
|
config.Bind("Database", databaseSettings);
|
|
|
|
|
services.AddSingleton((IServiceProvider x) => databaseSettings);
|
|
|
|
|
|
|
|
|
|
var myDatabaseSettings = new MyDatabaseSettings();
|
|
|
|
|
config.Bind("Database", myDatabaseSettings);
|
2023-08-10 04:53:22 +00:00
|
|
|
services.AddSingleton((IServiceProvider x) => myDatabaseSettings);
|
2023-07-21 21:56:14 +00:00
|
|
|
|
2023-08-14 18:16:35 +00:00
|
|
|
RegisterPlugins(services, config);
|
|
|
|
|
|
2023-08-17 04:04:23 +00:00
|
|
|
services.AddScoped<IAgentRouting, AgentRouter>();
|
|
|
|
|
|
2023-08-20 19:02:59 +00:00
|
|
|
services.AddScoped<IFunctionCallback, RouteToAgentFn>();
|
2023-08-19 00:15:50 +00:00
|
|
|
|
2023-08-14 18:16:35 +00:00
|
|
|
return services;
|
|
|
|
|
}
|
2023-07-21 21:56:14 +00:00
|
|
|
|
2023-08-14 18:16:35 +00:00
|
|
|
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped<IBotSharpRepository>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
|
|
|
|
|
return DataContextHelper.GetDbContext<BotSharpDbContext, DbContext4SqlServer>(myDatabaseSettings, sp);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
2023-08-10 04:53:22 +00:00
|
|
|
services.AddScoped<IBotSharpRepository>(sp =>
|
|
|
|
|
{
|
2023-08-14 18:16:35 +00:00
|
|
|
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
|
|
|
|
|
return new FileRepository(myDatabaseSettings, sp);
|
2023-08-10 04:53:22 +00:00
|
|
|
});
|
2023-07-21 21:56:14 +00:00
|
|
|
|
|
|
|
|
return services;
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
if (app == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(app));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 22:16:21 +00:00
|
|
|
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
|
|
|
|
|
|
2023-05-27 01:58:31 +00:00
|
|
|
return app;
|
|
|
|
|
}
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2023-06-10 02:56:04 +00:00
|
|
|
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
2023-06-17 02:42:35 +00:00
|
|
|
var pluginSettings = new PluginLoaderSettings();
|
|
|
|
|
config.Bind("PluginLoader", pluginSettings);
|
2023-06-11 23:46:02 +00:00
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
var loader = new PluginLoader(services, config, pluginSettings);
|
|
|
|
|
loader.Load();
|
2023-06-26 22:16:21 +00:00
|
|
|
|
|
|
|
|
services.AddSingleton(loader);
|
2023-08-28 15:24:43 +00:00
|
|
|
|
|
|
|
|
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
|
2023-06-10 02:56:04 +00:00
|
|
|
}
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|