diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs index 46f05ebd..2c4e0fc9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs @@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models; public class CodeInterpretOptions { + public string? ScriptName { get; set; } public IEnumerable? Arguments { get; set; } - public bool LockFree { get; set; } public CancellationToken? CancellationToken { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs b/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs index 5b544f11..0fea0fa1 100644 --- a/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs @@ -13,7 +13,7 @@ public class CodeScriptExecutor _logger = logger; } - public async Task Execute(Func> func, CancellationToken cancellationToken = default) + public async Task Execute(Func> func, CancellationToken cancellationToken = default) { await _semLock.WaitAsync(cancellationToken); @@ -24,11 +24,7 @@ public class CodeScriptExecutor catch (Exception ex) { _logger.LogError(ex, $"Error in {nameof(CodeScriptExecutor)}."); - return new CodeInterpretResult - { - Success = false, - ErrorMsg = ex.Message - }; + return default(T); } finally { diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 11ff7518..daa55803 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -155,7 +155,7 @@ public partial class InstructService var db = _services.GetRequiredService(); var hooks = _services.GetHooks(agent.Id); - var codeProvider = codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("botsharp-py-interpreter"); + var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter"; var codeInterpreter = _services.GetServices() .FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider)); @@ -228,6 +228,7 @@ public partial class InstructService // Run code script var result = await codeInterpreter.RunCode(context.CodeScript, options: new() { + ScriptName = scriptName, Arguments = context.Arguments }); diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs index 168d83f8..303ae41a 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs @@ -29,6 +29,7 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin { var sp = app.ApplicationServices; var settings = sp.GetRequiredService(); + var lifetime = app.ApplicationServices.GetRequiredService(); var logger = sp.GetRequiredService>(); var pyLoc = settings.InstallLocation; @@ -38,12 +39,19 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin { Runtime.PythonDLL = pyLoc; PythonEngine.Initialize(); +#if DEBUG _pyState = PythonEngine.BeginAllowThreads(); +#endif - var lifetime = app.ApplicationServices.GetRequiredService(); lifetime.ApplicationStopping.Register(() => { - PythonEngine.EndAllowThreads(_pyState); - PythonEngine.Shutdown(); + try + { +#if DEBUG + PythonEngine.EndAllowThreads(_pyState); +#endif + PythonEngine.Shutdown(); + } + catch { } }); } else diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs index 34a1f03e..f5688027 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs @@ -1,7 +1,6 @@ using BotSharp.Core.CodeInterpreter; using Microsoft.Extensions.Logging; using Python.Runtime; -using System.Threading; using System.Threading.Tasks; namespace BotSharp.Plugin.PythonInterpreter.Services; @@ -26,15 +25,6 @@ public class PyInterpretService : ICodeInterpretService public async Task RunCode(string codeScript, CodeInterpretOptions? options = null) { - if (options?.LockFree != true) - { - return await _executor.Execute(async () => - { - return InnerRunCode(codeScript, options); - }, cancellationToken: options?.CancellationToken ?? CancellationToken.None); - - } - return InnerRunCode(codeScript, options); } @@ -83,7 +73,7 @@ public class PyInterpretService : ICodeInterpretService var list = new PyList(); if (options?.Arguments?.Any() == true) { - list.Append(new PyString("code.py")); + list.Append(new PyString(options?.ScriptName.IfNullOrEmptyAs("script.py"))); foreach (var arg in options.Arguments) {