Skip to content

Commit

Permalink
PluginController
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Oct 11, 2023
1 parent 129bd50 commit 7fbff8c
Show file tree
Hide file tree
Showing 24 changed files with 188 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,4 @@ __pycache__/
/docs/_build
*.bin
/src/WebStarter/data/conversations
XMLs
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Plugins;

public class PluginLoaderSettings
public class PluginSettings
{
public string[] Assemblies { get; set; } = new string[0];
}
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Routing.Hooks;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Core.Plugins;

namespace BotSharp.Core;

Expand Down Expand Up @@ -106,8 +107,9 @@ public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)

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 =>
Expand Down
72 changes: 0 additions & 72 deletions src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs

This file was deleted.

122 changes: 122 additions & 0 deletions src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public AgentController(IAgentService agentService, IServiceProvider services)
_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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 3 additions & 0 deletions src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace BotSharp.Platform.AzureAi;

/// <summary>
/// Azure OpenAI Service
/// </summary>
public class AzureOpenAiPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace BotSharp.Plugin.MongoStorage;

/// <summary>
/// MongoDB as the repository
/// </summary>
public class MongoStoragePlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 7fbff8c

Please sign in to comment.