2025-09-29 06:20:25 +00:00
|
|
|
using BotSharp.Abstraction.Settings;
|
2024-08-12 19:22:47 +00:00
|
|
|
using BotSharp.Plugin.PythonInterpreter.Hooks;
|
2024-08-23 15:16:37 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Python.Runtime;
|
|
|
|
|
using System.IO;
|
2024-08-12 19:22:47 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.PythonInterpreter;
|
|
|
|
|
|
2025-09-29 06:35:08 +00:00
|
|
|
public class PythonInterpreterPlugin : IBotSharpAppPlugin
|
2024-08-12 19:22:47 +00:00
|
|
|
{
|
|
|
|
|
public string Id => "23174e08-e866-4173-824a-cf1d97afa8d0";
|
|
|
|
|
public string Name => "Python Interpreter";
|
|
|
|
|
public string Description => "Python Interpreter enables AI to write and execute Python code within a secure, sandboxed environment.";
|
|
|
|
|
public string? IconUrl => "https://static.vecteezy.com/system/resources/previews/012/697/295/non_2x/3d-python-programming-language-logo-free-png.png";
|
|
|
|
|
|
|
|
|
|
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
|
|
|
|
{
|
2025-09-29 06:20:25 +00:00
|
|
|
services.AddSingleton(provider =>
|
|
|
|
|
{
|
|
|
|
|
var settingService = provider.GetRequiredService<ISettingService>();
|
2025-09-29 06:35:08 +00:00
|
|
|
return settingService.Bind<PythonInterpreterSettings>("PythonInterpreter");
|
2025-09-29 06:20:25 +00:00
|
|
|
});
|
|
|
|
|
|
2025-09-29 06:35:08 +00:00
|
|
|
services.AddScoped<IAgentUtilityHook, PythonInterpreterUtilityHook>();
|
2024-08-12 19:22:47 +00:00
|
|
|
}
|
2024-08-23 15:16:37 +00:00
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app)
|
|
|
|
|
{
|
2025-09-29 06:35:08 +00:00
|
|
|
var settings = app.ApplicationServices.GetRequiredService<PythonInterpreterSettings>();
|
2024-08-23 15:16:37 +00:00
|
|
|
|
|
|
|
|
// For Python interpreter plugin
|
2025-09-29 06:35:08 +00:00
|
|
|
if (File.Exists(settings.DllLocation))
|
2024-08-23 15:16:37 +00:00
|
|
|
{
|
2025-09-29 06:35:08 +00:00
|
|
|
Runtime.PythonDLL = settings.DllLocation;
|
2024-08-23 15:16:37 +00:00
|
|
|
PythonEngine.Initialize();
|
|
|
|
|
PythonEngine.BeginAllowThreads();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 06:35:08 +00:00
|
|
|
Serilog.Log.Error("Python DLL found at {PythonDLL}", settings.DllLocation);
|
2024-08-23 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shut down the Python engine
|
|
|
|
|
// PythonEngine.Shutdown();
|
|
|
|
|
}
|
2024-08-12 19:22:47 +00:00
|
|
|
}
|