PluginController
This commit is contained in:
parent
129bd50bf0
commit
7fbff8c777
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -287,3 +287,4 @@ __pycache__/
|
|||
/docs/_build
|
||||
*.bin
|
||||
/src/WebStarter/data/conversations
|
||||
XMLs
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
namespace BotSharp.Abstraction.Plugins.Models;
|
||||
|
||||
public class PluginDef
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Assembly { get; set; }
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Plugins;
|
||||
|
||||
public class PluginLoaderSettings
|
||||
public class PluginSettings
|
||||
{
|
||||
public string[] Assemblies { get; set; } = new string[0];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspects.Cache" Version="2.0.3" />
|
||||
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
|
||||
<PackageReference Include="Colorful.Console" Version="1.2.15" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
|
||||
<PackageReference Include="Fluid.Core" Version="2.5.0" />
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ using BotSharp.Abstraction.Instructs;
|
|||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Core.Routing.Hooks;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Core.Plugins;
|
||||
|
||||
namespace BotSharp.Core;
|
||||
|
||||
|
|
@ -106,8 +107,9 @@ public static class BotSharpServiceCollectionExtensions
|
|||
|
||||
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var pluginSettings = new PluginLoaderSettings();
|
||||
var pluginSettings = new PluginSettings();
|
||||
config.Bind("PluginLoader", pluginSettings);
|
||||
services.AddSingleton(pluginSettings);
|
||||
|
||||
var loader = new PluginLoader(services, config, pluginSettings);
|
||||
loader.Load(assembly =>
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BotSharp.Core.Infrastructures;
|
||||
|
||||
public class PluginLoader
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly PluginLoaderSettings _settings;
|
||||
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>();
|
||||
|
||||
public PluginLoader(IServiceCollection services,
|
||||
IConfiguration config,
|
||||
PluginLoaderSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_config = config;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public void Load(Action<Assembly> loaded)
|
||||
{
|
||||
var executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName;
|
||||
|
||||
_settings.Assemblies.ToList().ForEach(assemblyName =>
|
||||
{
|
||||
var assemblyPath = Path.Combine(executingDir, assemblyName + ".dll");
|
||||
if (File.Exists(assemblyPath))
|
||||
{
|
||||
var assembly = Assembly.Load(assemblyName);
|
||||
|
||||
var modules = assembly.GetTypes()
|
||||
.Where(x => x.GetInterface(nameof(IBotSharpPlugin)) != null)
|
||||
.Select(x => Activator.CreateInstance(x) as IBotSharpPlugin)
|
||||
.ToList();
|
||||
|
||||
foreach (var module in modules)
|
||||
{
|
||||
module.RegisterDI(_services, _config);
|
||||
Console.WriteLine($"Loaded plugin {module.GetType().Name} from {assemblyName}.", Color.Green);
|
||||
}
|
||||
|
||||
loaded(assembly);
|
||||
_modules.AddRange(modules);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Can't find assemble {assemblyPath}.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
if (_modules.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow);
|
||||
}
|
||||
|
||||
_modules.ForEach(module =>
|
||||
{
|
||||
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null)
|
||||
{
|
||||
(module as IBotSharpAppPlugin).Configure(app);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
122
src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Normal file
122
src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Xml;
|
||||
|
||||
namespace BotSharp.Core.Plugins;
|
||||
|
||||
public class PluginLoader
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly PluginSettings _settings;
|
||||
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>();
|
||||
private static List<PluginDef> _plugins = new List<PluginDef>();
|
||||
private static string _executingDir;
|
||||
|
||||
public PluginLoader(IServiceCollection services,
|
||||
IConfiguration config,
|
||||
PluginSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_config = config;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public void Load(Action<Assembly> loaded)
|
||||
{
|
||||
_executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName;
|
||||
|
||||
_settings.Assemblies.ToList().ForEach(assemblyName =>
|
||||
{
|
||||
var assemblyPath = Path.Combine(_executingDir, assemblyName + ".dll");
|
||||
if (File.Exists(assemblyPath))
|
||||
{
|
||||
var assembly = Assembly.Load(assemblyName);
|
||||
|
||||
var modules = assembly.GetTypes()
|
||||
.Where(x => x.GetInterface(nameof(IBotSharpPlugin)) != null)
|
||||
.Select(x => Activator.CreateInstance(x) as IBotSharpPlugin)
|
||||
.ToList();
|
||||
|
||||
foreach (var module in modules)
|
||||
{
|
||||
module.RegisterDI(_services, _config);
|
||||
string classSummary = GetSummaryComment(module.GetType());
|
||||
_modules.Add(module);
|
||||
_plugins.Add(new PluginDef
|
||||
{
|
||||
Name = module.GetType().Name,
|
||||
Description = classSummary,
|
||||
Assembly = assemblyName
|
||||
});
|
||||
Console.Write($"Loaded plugin ");
|
||||
Console.Write(module.GetType().Name, Color.Green);
|
||||
Console.WriteLine($" from {assemblyName}.");
|
||||
if (!string.IsNullOrEmpty(classSummary))
|
||||
{
|
||||
Console.WriteLine(classSummary);
|
||||
}
|
||||
}
|
||||
|
||||
loaded(assembly);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Can't find assemble {assemblyPath}.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<PluginDef> GetPlugins()
|
||||
{
|
||||
return _plugins;
|
||||
}
|
||||
|
||||
public string GetSummaryComment(Type member)
|
||||
{
|
||||
string summary = string.Empty;
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
|
||||
// Load the XML documentation file
|
||||
var xmlFile = Path.Combine(_executingDir, $"{member.Module.Assembly.FullName.Split(',')[0]}.xml");
|
||||
if (!File.Exists(xmlFile))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
xmlDoc.Load(xmlFile); // Replace with your actual XML documentation file name
|
||||
|
||||
// Construct the XPath query to find the summary comment
|
||||
string memberName = $"T:{member.FullName}";
|
||||
string xpath = $"/doc/members/member[@name='{memberName}']/summary";
|
||||
|
||||
// Find the summary comment using the XPath query
|
||||
XmlNode summaryNode = xmlDoc.SelectSingleNode(xpath);
|
||||
|
||||
if (summaryNode != null)
|
||||
{
|
||||
summary = summaryNode.InnerXml.Trim();
|
||||
}
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
if (_modules.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow);
|
||||
}
|
||||
|
||||
_modules.ForEach(module =>
|
||||
{
|
||||
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null)
|
||||
{
|
||||
(module as IBotSharpAppPlugin).Configure(app);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,13 @@ public class AgentController : ControllerBase, IApiAdapter
|
|||
_services = services;
|
||||
}
|
||||
|
||||
[HttpGet("/agent/{id}")]
|
||||
public async Task<AgentViewModel> GetAgent([FromRoute] string id)
|
||||
{
|
||||
var agent = await _agentService.GetAgent(id);
|
||||
return AgentViewModel.FromAgent(agent);
|
||||
}
|
||||
|
||||
[HttpGet("/agents")]
|
||||
public async Task<List<AgentViewModel>> GetAgents()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Core.Plugins;
|
||||
|
||||
namespace BotSharp.OpenAPI.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class PluginController : ControllerBase
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly PluginSettings _settings;
|
||||
|
||||
public PluginController(IServiceProvider services, PluginSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
[HttpGet("/plugins")]
|
||||
public List<PluginDef> GetPlugins()
|
||||
{
|
||||
var loader = _services.GetRequiredService<PluginLoader>();
|
||||
return loader.GetPlugins();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,9 @@ using System;
|
|||
|
||||
namespace BotSharp.Platform.AzureAi;
|
||||
|
||||
/// <summary>
|
||||
/// Azure OpenAI Service
|
||||
/// </summary>
|
||||
public class AzureOpenAiPlugin : IBotSharpPlugin
|
||||
{
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -5,10 +5,11 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ using BotSharp.Plugin.MongoStorage.Repository;
|
|||
|
||||
namespace BotSharp.Plugin.MongoStorage;
|
||||
|
||||
/// <summary>
|
||||
/// MongoDB as the repository
|
||||
/// </summary>
|
||||
public class MongoStoragePlugin : IBotSharpPlugin
|
||||
{
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
|
|
|||
Loading…
Reference in a new issue