diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs deleted file mode 100644 index bdc10f5c9..000000000 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BotSharp.Abstraction.CodeInterpreter.Models; - -namespace BotSharp.Abstraction.CodeInterpreter; - -public interface ICodeInterpretService -{ - string Provider { get; } - - Task RunCode(string codeScript, CodeInterpretOptions? options = null) - => throw new NotImplementedException(); -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Coding/ICodeProcessor.cs b/src/Infrastructure/BotSharp.Abstraction/Coding/ICodeProcessor.cs new file mode 100644 index 000000000..1fe083f01 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Coding/ICodeProcessor.cs @@ -0,0 +1,12 @@ +using BotSharp.Abstraction.Coding.Options; +using BotSharp.Abstraction.Coding.Responses; + +namespace BotSharp.Abstraction.Coding; + +public interface ICodeProcessor +{ + string Provider { get; } + + Task RunAsync(string codeScript, CodeInterpretOptions? options = null) + => throw new NotImplementedException(); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Coding/Options/CodeInterpretOptions.cs similarity index 82% rename from src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs rename to src/Infrastructure/BotSharp.Abstraction/Coding/Options/CodeInterpretOptions.cs index 0e8953bea..b563b03de 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Coding/Options/CodeInterpretOptions.cs @@ -1,6 +1,6 @@ using System.Threading; -namespace BotSharp.Abstraction.CodeInterpreter.Models; +namespace BotSharp.Abstraction.Coding.Options; public class CodeInterpretOptions { diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretResult.cs b/src/Infrastructure/BotSharp.Abstraction/Coding/Responses/CodeInterpretResponse.cs similarity index 61% rename from src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretResult.cs rename to src/Infrastructure/BotSharp.Abstraction/Coding/Responses/CodeInterpretResponse.cs index 2958cca81..a07c7a1fe 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretResult.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Coding/Responses/CodeInterpretResponse.cs @@ -1,6 +1,6 @@ -namespace BotSharp.Abstraction.CodeInterpreter.Models; +namespace BotSharp.Abstraction.Coding.Responses; -public class CodeInterpretResult +public class CodeInterpretResponse { public string Result { get; set; } = string.Empty; public bool Success { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Options/FileLlmProcessOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Options/FileHandleOptions.cs similarity index 96% rename from src/Infrastructure/BotSharp.Abstraction/Files/Options/FileLlmProcessOptions.cs rename to src/Infrastructure/BotSharp.Abstraction/Files/Options/FileHandleOptions.cs index eafae5040..31a2ab46c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/Options/FileLlmProcessOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Options/FileHandleOptions.cs @@ -1,6 +1,6 @@ namespace BotSharp.Abstraction.Files.Options; -public class FileLlmProcessOptions +public class FileHandleOptions { /// /// Llm provider diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileLlmProcessor.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileProcessor.cs similarity index 53% rename from src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileLlmProcessor.cs rename to src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileProcessor.cs index ebc057927..19ab437c9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileLlmProcessor.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Proccessors/IFileProcessor.cs @@ -3,10 +3,10 @@ namespace BotSharp.Abstraction.Files.Proccessors; -public interface IFileLlmProcessor +public interface IFileProcessor { public string Provider { get; } - Task GetFileLlmInferenceAsync(Agent agent, string text, IEnumerable files, FileLlmProcessOptions? options = null) + Task HandleFilesAsync(Agent agent, string text, IEnumerable files, FileHandleOptions? options = null) => throw new NotImplementedException(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileLlmInferenceResponse.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileHandleResponse.cs similarity index 83% rename from src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileLlmInferenceResponse.cs rename to src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileHandleResponse.cs index fa11b1f9b..6957ed0fc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileLlmInferenceResponse.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Responses/FileHandleResponse.cs @@ -1,6 +1,6 @@ namespace BotSharp.Abstraction.Files.Responses; -public class FileLlmInferenceResponse +public class FileHandleResponse { public string Result { get; set; } = string.Empty; public bool Success { get; set; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/CodeInstructOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/CodeInstructOptions.cs index e40816099..89684cfdf 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/CodeInstructOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/CodeInstructOptions.cs @@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Instructs.Options; public class CodeInstructOptions { + public string? Processor { get; set; } public string? CodeScriptName { get; set; } - public string? CodeInterpretProvider { get; set; } public List? Arguments { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/FileInstructOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/FileInstructOptions.cs index a4f3a8589..bb575f49d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/FileInstructOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/Options/FileInstructOptions.cs @@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Instructs.Options; public class FileInstructOptions { - public string? FileLlmProcessorProvider { get; set; } + public string? Processor { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs b/src/Infrastructure/BotSharp.Core/Coding/CodeScriptExecutor.cs similarity index 76% rename from src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs rename to src/Infrastructure/BotSharp.Core/Coding/CodeScriptExecutor.cs index 0fea0fa1d..bc38416dc 100644 --- a/src/Infrastructure/BotSharp.Core/CodeInterpreter/CodeScriptExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/Coding/CodeScriptExecutor.cs @@ -1,6 +1,4 @@ -using BotSharp.Abstraction.CodeInterpreter.Models; - -namespace BotSharp.Core.CodeInterpreter; +namespace BotSharp.Core.Coding; public class CodeScriptExecutor { @@ -13,7 +11,7 @@ public CodeScriptExecutor( _logger = logger; } - public async Task Execute(Func> func, CancellationToken cancellationToken = default) + public async Task ExecuteAsync(Func> func, CancellationToken cancellationToken = default) { await _semLock.WaitAsync(cancellationToken); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs index 798fe307e..e1c44a9a1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Google.Settings; using BotSharp.Abstraction.Instructs; -using BotSharp.Abstraction.MessageHub; using BotSharp.Abstraction.MessageHub.Observers; using BotSharp.Abstraction.MessageHub.Services; using BotSharp.Abstraction.Messaging; @@ -8,7 +7,7 @@ using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Settings; using BotSharp.Abstraction.Templating; -using BotSharp.Core.CodeInterpreter; +using BotSharp.Core.Coding; using BotSharp.Core.Instructs; using BotSharp.Core.MessageHub; using BotSharp.Core.MessageHub.Observers; @@ -18,7 +17,6 @@ using BotSharp.Core.Templating; using BotSharp.Core.Translation; using BotSharp.Core.WebSearch.Hooks; -using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; namespace BotSharp.Core.Conversations; diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 267f46335..c22eca182 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.CodeInterpreter; +using BotSharp.Abstraction.Coding; using BotSharp.Abstraction.Files.Options; using BotSharp.Abstraction.Files.Proccessors; using BotSharp.Abstraction.Instructs; @@ -47,7 +47,7 @@ public async Task Execute( // Run code template - var codeResponse = await GetCodeResponse(agent, message, templateName, codeOptions); + var codeResponse = await RunCode(agent, message, templateName, codeOptions); if (codeResponse != null) { return codeResponse; @@ -105,16 +105,16 @@ public async Task Execute( prompt = message.Content; } - IFileLlmProcessor? fileProcessor = null; + IFileProcessor? fileProcessor = null; if (!files.IsNullOrEmpty() && fileOptions != null) { - fileProcessor = _services.GetServices() - .FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions.FileLlmProcessorProvider)); + fileProcessor = _services.GetServices() + .FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions.Processor)); } if (fileProcessor != null) { - var inference = await fileProcessor.GetFileLlmInferenceAsync(agent, prompt, files, new FileLlmProcessOptions + var fileResponse = await fileProcessor.HandleFilesAsync(agent, prompt, files, new FileHandleOptions { Provider = provider, Model = model, @@ -124,7 +124,7 @@ public async Task Execute( InvokeFrom = $"{nameof(InstructService)}.{nameof(Execute)}", Data = state.GetStates().ToDictionary(x => x.Key, x => (object)x.Value) }); - result = inference.Result.IfNullOrEmptyAs(string.Empty); + result = fileResponse.Result.IfNullOrEmptyAs(string.Empty); } else { @@ -160,7 +160,7 @@ await hook.OnResponseGenerated(new InstructResponseModel /// /// /// - private async Task GetCodeResponse( + private async Task RunCode( Agent agent, RoleDialogModel message, string templateName, @@ -177,14 +177,14 @@ await hook.OnResponseGenerated(new InstructResponseModel var state = _services.GetRequiredService(); var hooks = _services.GetHooks(agent.Id); - var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter"; - var codeInterpreter = _services.GetServices() + var codeProvider = codeOptions?.Processor ?? "botsharp-py-interpreter"; + var codeProcessor = _services.GetServices() .FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider)); - if (codeInterpreter == null) + if (codeProcessor == null) { #if DEBUG - _logger.LogWarning($"No code interpreter found. (Agent: {agent.Id}, Code interpreter: {codeProvider})"); + _logger.LogWarning($"No code processor found. (Agent: {agent.Id}, Code processor: {codeProvider})"); #endif return response; } @@ -248,7 +248,7 @@ await hook.OnResponseGenerated(new InstructResponseModel } // Run code script - var result = await codeInterpreter.RunCode(context.CodeScript, options: new() + var codeResponse = await codeProcessor.RunAsync(context.CodeScript, options: new() { ScriptName = scriptName, Arguments = context.Arguments @@ -258,7 +258,7 @@ await hook.OnResponseGenerated(new InstructResponseModel { MessageId = message.MessageId, Template = scriptName, - Text = result?.Result ?? result?.ErrorMsg + Text = codeResponse?.Result ?? codeResponse?.ErrorMsg }; if (context?.Arguments != null) @@ -273,7 +273,7 @@ await hook.OnResponseGenerated(new InstructResponseModel await hook.OnResponseGenerated(new InstructResponseModel { AgentId = agent.Id, - Provider = codeInterpreter.Provider, + Provider = codeProcessor.Provider, Model = string.Empty, TemplateName = scriptName, UserMessage = message.Content, diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs index bee046e51..a56a9e447 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs @@ -22,7 +22,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) services.AddSingleton(x => settings); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); } public void Configure(IApplicationBuilder app) diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs similarity index 79% rename from src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs rename to src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs index 46ed2e47a..76bd6fe60 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs @@ -1,4 +1,3 @@ -using BotSharp.Core.CodeInterpreter; using Microsoft.Extensions.Logging; using Python.Runtime; using System.Threading; @@ -6,15 +5,15 @@ namespace BotSharp.Plugin.PythonInterpreter.Services; -public class PyInterpretService : ICodeInterpretService +public class PyCodeInterpreter : ICodeProcessor { private readonly IServiceProvider _services; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly CodeScriptExecutor _executor; - public PyInterpretService( + public PyCodeInterpreter( IServiceProvider services, - ILogger logger, + ILogger logger, CodeScriptExecutor executor) { _services = services; @@ -24,11 +23,11 @@ public PyInterpretService( public string Provider => "botsharp-py-interpreter"; - public async Task RunCode(string codeScript, CodeInterpretOptions? options = null) + public async Task RunAsync(string codeScript, CodeInterpretOptions? options = null) { if (options?.UseMutex == true) { - return await _executor.Execute(async () => + return await _executor.ExecuteAsync(async () => { return InnerRunCode(codeScript, options); }, cancellationToken: options?.CancellationToken ?? CancellationToken.None); @@ -36,7 +35,7 @@ public async Task RunCode(string codeScript, CodeInterpretO return InnerRunCode(codeScript, options); } - private CodeInterpretResult InnerRunCode(string codeScript, CodeInterpretOptions? options = null) + private CodeInterpretResponse InnerRunCode(string codeScript, CodeInterpretOptions? options = null) { try { @@ -44,10 +43,10 @@ private CodeInterpretResult InnerRunCode(string codeScript, CodeInterpretOptions } catch (Exception ex) { - var errorMsg = $"Error when executing inner python code in {nameof(PyInterpretService)}: {Provider}."; + var errorMsg = $"Error when executing inner python code in {nameof(PyCodeInterpreter)}: {Provider}."; _logger.LogError(ex, errorMsg); - return new CodeInterpretResult + return new CodeInterpretResponse { Success = false, ErrorMsg = errorMsg @@ -55,7 +54,7 @@ private CodeInterpretResult InnerRunCode(string codeScript, CodeInterpretOptions } } - private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? options = null) + private CodeInterpretResponse CoreRun(string codeScript, CodeInterpretOptions? options = null) { using (Py.GIL()) { @@ -100,7 +99,7 @@ private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? opt // Get result var result = stringIO.getvalue()?.ToString() as string; - return new CodeInterpretResult + return new CodeInterpretResponse { Result = result?.TrimEnd('\r', '\n'), Success = true @@ -108,10 +107,10 @@ private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? opt } catch (Exception ex) { - var errorMsg = $"Error when executing core python code in {nameof(PyInterpretService)}: {Provider}. {ex.Message}"; + var errorMsg = $"Error when executing core python code in {nameof(PyCodeInterpreter)}: {Provider}. {ex.Message}"; _logger.LogError(ex, errorMsg); - return new CodeInterpretResult + return new CodeInterpretResponse { Success = false, ErrorMsg = errorMsg diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs index 11a023fe7..bd2c943b7 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs @@ -20,9 +20,10 @@ global using BotSharp.Abstraction.Messaging.Models.RichContent; global using BotSharp.Abstraction.Messaging.Models.RichContent.Template; global using BotSharp.Abstraction.Routing; -global using BotSharp.Abstraction.CodeInterpreter.Models; -global using BotSharp.Abstraction.CodeInterpreter; - +global using BotSharp.Abstraction.Coding; +global using BotSharp.Abstraction.Coding.Options; +global using BotSharp.Abstraction.Coding.Responses; +global using BotSharp.Core.Coding; global using BotSharp.Core.Infrastructures; global using BotSharp.Plugin.PythonInterpreter.Enums;