Skip to content

Commit 7fbff8c

Browse files
committed
PluginController
1 parent 129bd50 commit 7fbff8c

File tree

24 files changed

+188
-76
lines changed

24 files changed

+188
-76
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,4 @@ __pycache__/
287287
/docs/_build
288288
*.bin
289289
/src/WebStarter/data/conversations
290+
XMLs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BotSharp.Abstraction.Plugins.Models;
2+
3+
public class PluginDef
4+
{
5+
public string Name { get; set; }
6+
public string Description { get; set; }
7+
public string Assembly { get; set; }
8+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BotSharp.Abstraction.Plugins;
22

3-
public class PluginLoaderSettings
3+
public class PluginSettings
44
{
55
public string[] Assemblies { get; set; } = new string[0];
66
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</ItemGroup>
5353

5454
<ItemGroup>
55-
<PackageReference Include="Aspects.Cache" Version="2.0.3" />
55+
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
5656
<PackageReference Include="Colorful.Console" Version="1.2.15" />
5757
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
5858
<PackageReference Include="Fluid.Core" Version="2.5.0" />

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using BotSharp.Abstraction.Routing;
1212
using BotSharp.Core.Routing.Hooks;
1313
using BotSharp.Abstraction.Routing.Models;
14+
using BotSharp.Core.Plugins;
1415

1516
namespace BotSharp.Core;
1617

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

107108
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
108109
{
109-
var pluginSettings = new PluginLoaderSettings();
110+
var pluginSettings = new PluginSettings();
110111
config.Bind("PluginLoader", pluginSettings);
112+
services.AddSingleton(pluginSettings);
111113

112114
var loader = new PluginLoader(services, config, pluginSettings);
113115
loader.Load(assembly =>

src/Infrastructure/BotSharp.Core/Infrastructures/PluginLoader.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using BotSharp.Abstraction.Plugins.Models;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.Configuration;
4+
using System.Drawing;
5+
using System.IO;
6+
using System.Reflection;
7+
using System.Xml;
8+
9+
namespace BotSharp.Core.Plugins;
10+
11+
public class PluginLoader
12+
{
13+
private readonly IServiceCollection _services;
14+
private readonly IConfiguration _config;
15+
private readonly PluginSettings _settings;
16+
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>();
17+
private static List<PluginDef> _plugins = new List<PluginDef>();
18+
private static string _executingDir;
19+
20+
public PluginLoader(IServiceCollection services,
21+
IConfiguration config,
22+
PluginSettings settings)
23+
{
24+
_services = services;
25+
_config = config;
26+
_settings = settings;
27+
}
28+
29+
public void Load(Action<Assembly> loaded)
30+
{
31+
_executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName;
32+
33+
_settings.Assemblies.ToList().ForEach(assemblyName =>
34+
{
35+
var assemblyPath = Path.Combine(_executingDir, assemblyName + ".dll");
36+
if (File.Exists(assemblyPath))
37+
{
38+
var assembly = Assembly.Load(assemblyName);
39+
40+
var modules = assembly.GetTypes()
41+
.Where(x => x.GetInterface(nameof(IBotSharpPlugin)) != null)
42+
.Select(x => Activator.CreateInstance(x) as IBotSharpPlugin)
43+
.ToList();
44+
45+
foreach (var module in modules)
46+
{
47+
module.RegisterDI(_services, _config);
48+
string classSummary = GetSummaryComment(module.GetType());
49+
_modules.Add(module);
50+
_plugins.Add(new PluginDef
51+
{
52+
Name = module.GetType().Name,
53+
Description = classSummary,
54+
Assembly = assemblyName
55+
});
56+
Console.Write($"Loaded plugin ");
57+
Console.Write(module.GetType().Name, Color.Green);
58+
Console.WriteLine($" from {assemblyName}.");
59+
if (!string.IsNullOrEmpty(classSummary))
60+
{
61+
Console.WriteLine(classSummary);
62+
}
63+
}
64+
65+
loaded(assembly);
66+
}
67+
else
68+
{
69+
Console.WriteLine($"Can't find assemble {assemblyPath}.");
70+
}
71+
});
72+
}
73+
74+
public List<PluginDef> GetPlugins()
75+
{
76+
return _plugins;
77+
}
78+
79+
public string GetSummaryComment(Type member)
80+
{
81+
string summary = string.Empty;
82+
XmlDocument xmlDoc = new XmlDocument();
83+
84+
// Load the XML documentation file
85+
var xmlFile = Path.Combine(_executingDir, $"{member.Module.Assembly.FullName.Split(',')[0]}.xml");
86+
if (!File.Exists(xmlFile))
87+
{
88+
return "";
89+
}
90+
xmlDoc.Load(xmlFile); // Replace with your actual XML documentation file name
91+
92+
// Construct the XPath query to find the summary comment
93+
string memberName = $"T:{member.FullName}";
94+
string xpath = $"/doc/members/member[@name='{memberName}']/summary";
95+
96+
// Find the summary comment using the XPath query
97+
XmlNode summaryNode = xmlDoc.SelectSingleNode(xpath);
98+
99+
if (summaryNode != null)
100+
{
101+
summary = summaryNode.InnerXml.Trim();
102+
}
103+
104+
return summary;
105+
}
106+
107+
public void Configure(IApplicationBuilder app)
108+
{
109+
if (_modules.Count == 0)
110+
{
111+
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow);
112+
}
113+
114+
_modules.ForEach(module =>
115+
{
116+
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null)
117+
{
118+
(module as IBotSharpAppPlugin).Configure(app);
119+
}
120+
});
121+
}
122+
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public AgentController(IAgentService agentService, IServiceProvider services)
1818
_services = services;
1919
}
2020

21+
[HttpGet("/agent/{id}")]
22+
public async Task<AgentViewModel> GetAgent([FromRoute] string id)
23+
{
24+
var agent = await _agentService.GetAgent(id);
25+
return AgentViewModel.FromAgent(agent);
26+
}
27+
2128
[HttpGet("/agents")]
2229
public async Task<List<AgentViewModel>> GetAgents()
2330
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BotSharp.Abstraction.Plugins.Models;
2+
using BotSharp.Core.Plugins;
3+
4+
namespace BotSharp.OpenAPI.Controllers;
5+
6+
[Authorize]
7+
[ApiController]
8+
public class PluginController : ControllerBase
9+
{
10+
private readonly IServiceProvider _services;
11+
private readonly PluginSettings _settings;
12+
13+
public PluginController(IServiceProvider services, PluginSettings settings)
14+
{
15+
_services = services;
16+
_settings = settings;
17+
}
18+
19+
[HttpGet("/plugins")]
20+
public List<PluginDef> GetPlugins()
21+
{
22+
var loader = _services.GetRequiredService<PluginLoader>();
23+
return loader.GetPlugins();
24+
}
25+
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace BotSharp.Platform.AzureAi;
1111

12+
/// <summary>
13+
/// Azure OpenAI Service
14+
/// </summary>
1215
public class AzureOpenAiPlugin : IBotSharpPlugin
1316
{
1417
public void RegisterDI(IServiceCollection services, IConfiguration config)

src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.GoogleAI/BotSharp.Plugin.GoogleAI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.HuggingFace/BotSharp.Plugin.HuggingFace.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.LLamaSharp/BotSharp.Plugin.LLamaSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.MetaAI/BotSharp.Plugin.MetaAI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.MetaMessenger/BotSharp.Plugin.MetaMessenger.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<LangVersion>$(LangVersion)</LangVersion>
66
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
77
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
8+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
<LangVersion>$(LangVersion)</LangVersion>
66
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
77
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
8+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
89
</PropertyGroup>
910

1011
<ItemGroup>
11-
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
12+
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
1213
</ItemGroup>
1314

1415
<ItemGroup>

src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace BotSharp.Plugin.MongoStorage;
44

5+
/// <summary>
6+
/// MongoDB as the repository
7+
/// </summary>
58
public class MongoStoragePlugin : IBotSharpPlugin
69
{
710
public void RegisterDI(IServiceCollection services, IConfiguration config)

src/Plugins/BotSharp.Plugin.PaddleSharp/BotSharp.Plugin.PaddleSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<LangVersion>$(LangVersion)</LangVersion>
77
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
88
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>

0 commit comments

Comments
 (0)