diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs index 5255c18f..23e78a97 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs @@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models; public class CodeInterpretOptions { - public List? Arguments { get; set; } + public IEnumerable? Arguments { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 44ab141e..aae327b9 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -8,6 +8,8 @@ namespace BotSharp.Core.Instructs; public partial class InstructService { + private const string DEFAULT_CODE_INTERPRETER = "python-interpreter"; + public async Task Execute( string agentId, RoleDialogModel message, @@ -22,7 +24,7 @@ public partial class InstructService var response = new InstructResult { MessageId = message.MessageId, - Template = codeOptions?.CodeScriptName ?? templateName + Template = templateName }; @@ -40,41 +42,13 @@ public partial class InstructService } // Run code template - if (!string.IsNullOrWhiteSpace(codeOptions?.CodeScriptName)) + var (text, isCodeComplete) = await GetCodeResponse(agentId, templateName, codeOptions); + if (isCodeComplete) { - var codeInterpreter = _services.GetServices() - .FirstOrDefault(x => x.Provider.IsEqualTo(codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("python-interpreter"))); - - if (codeInterpreter == null) - { - var error = $"No code interpreter found. (Agent: {agentId}, Code interpreter: {codeOptions.CodeInterpretProvider})"; - _logger.LogError(error); - response.Text = error; - } - else - { - var db = _services.GetRequiredService(); - var state = _services.GetRequiredService(); - - var codeScript = db.GetAgentCodeScript(agentId, codeOptions.CodeScriptName); - if (string.IsNullOrWhiteSpace(codeScript)) - { - var error = $"Empty code script. (Agent: {agentId}, Code script: {codeOptions.CodeScriptName})"; - _logger.LogError(error); - response.Text = error; - } - else - { - var result = await codeInterpreter.RunCode(codeScript, options: new() - { - Arguments = codeOptions?.Arguments - }); - response.Text = result?.Result?.ToString(); - } - } + response.Text = text; return response; } - + // Trigger before completion hooks var hooks = _services.GetHooks(agentId); @@ -93,6 +67,7 @@ public partial class InstructService } } + var provider = string.Empty; var model = string.Empty; @@ -159,4 +134,65 @@ public partial class InstructService return response; } + + private async Task<(string?, bool)> GetCodeResponse(string agentId, string templateName, CodeInstructOptions? codeOptions) + { + var state = _services.GetRequiredService(); + var db = _services.GetRequiredService(); + + var isComplete = false; + var response = string.Empty; + + var codeProvider = codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs(DEFAULT_CODE_INTERPRETER); + var codeInterpreter = _services.GetServices() + .FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider)); + + if (codeInterpreter == null) + { + _logger.LogWarning($"No code interpreter found. (Agent: {agentId}, Code interpreter: {codeProvider})"); + return (response, isComplete); + } + + // Get code script name + var scriptName = string.Empty; + if (!string.IsNullOrEmpty(codeOptions?.CodeScriptName)) + { + scriptName = codeOptions.CodeScriptName; + } + else if (!string.IsNullOrEmpty(templateName)) + { + scriptName = $"{templateName}.py"; + } + + if (string.IsNullOrEmpty(scriptName)) + { + _logger.LogWarning($"Empty code script name. (Agent: {agentId}, {scriptName})"); + return (response, isComplete); + } + + // Get code script + var codeScript = db.GetAgentCodeScript(agentId, scriptName); + if (string.IsNullOrWhiteSpace(codeScript)) + { + _logger.LogWarning($"Empty code script. (Agent: {agentId}, {scriptName})"); + return (response, isComplete); + } + + // Get code arguments + var arguments = codeOptions?.Arguments ?? []; + if (arguments.IsNullOrEmpty()) + { + arguments = state.GetStates().Select(x => new KeyValue(x.Key, x.Value)).ToList(); + } + + // Run code script + var result = await codeInterpreter.RunCode(codeScript, options: new() + { + Arguments = arguments + }); + + response = result?.Result?.ToString(); + isComplete = true; + return (response, isComplete); + } }