diff --git a/src/Infrastructure/BotSharp.Abstraction/Coding/Models/CodeExecutionResponseModel.cs b/src/Infrastructure/BotSharp.Abstraction/Coding/Models/CodeExecutionResponseModel.cs index df85fb02b..7045f1ac9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Coding/Models/CodeExecutionResponseModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Coding/Models/CodeExecutionResponseModel.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Coding.Responses; + namespace BotSharp.Abstraction.Coding.Models; public class CodeExecutionResponseModel @@ -6,5 +8,5 @@ public class CodeExecutionResponseModel public AgentCodeScript CodeScript { get; set; } public IDictionary? Arguments { get; set; } public string Text { get; set; } = default!; - public string ExecutionResult { get; set; } = default!; + public CodeInterpretResponse? ExecutionResult { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs index 442f64fea..2d52ef6d2 100644 --- a/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs +++ b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs @@ -153,7 +153,7 @@ private async Task TriggerCodeScript(Agent agent, string triggerName, Rule CodeProcessor = processor.Provider, CodeScript = codeScript, Arguments = arguments.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty), - ExecutionResult = response?.ToString() ?? string.Empty + ExecutionResult = response }; foreach (var hook in hooks) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index c2dca0d9e..a0b7dc8cd 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -49,7 +49,7 @@ public async Task Execute( // Run code template var codeResponse = await RunCode(agent, message, templateName, codeOptions); - if (codeResponse != null) + if (!string.IsNullOrWhiteSpace(codeResponse?.Text)) { return codeResponse; } @@ -169,11 +169,11 @@ await hook.OnResponseGenerated(new InstructResponseModel string templateName, CodeInstructOptions? codeOptions) { - InstructResult? response = null; + InstructResult? instructResult = null; if (agent == null) { - return response; + return instructResult; } var agentService = _services.GetRequiredService(); @@ -192,7 +192,7 @@ await hook.OnResponseGenerated(new InstructResponseModel #if DEBUG _logger.LogWarning($"No code processor found. (Agent: {agent.Id}, Code processor: {codeProvider})"); #endif - return response; + return instructResult; } // Get code script name @@ -211,7 +211,7 @@ await hook.OnResponseGenerated(new InstructResponseModel #if DEBUG _logger.LogWarning($"Empty code script name. (Agent: {agent.Id}, {scriptName})"); #endif - return response; + return instructResult; } // Get code script @@ -222,7 +222,7 @@ await hook.OnResponseGenerated(new InstructResponseModel #if DEBUG _logger.LogWarning($"Empty code script. (Agent: {agent.Id}, {scriptName})"); #endif - return response; + return instructResult; } // Get code arguments @@ -266,21 +266,18 @@ await hook.OnResponseGenerated(new InstructResponseModel UseProcess = useProcess }, cancellationToken: cts.Token); - if (codeResponse?.Success == true) + instructResult = new InstructResult { - response = new InstructResult - { - MessageId = message.MessageId, - Template = context.CodeScript?.Name, - Text = codeResponse.Result - }; - } + MessageId = message.MessageId, + Template = context.CodeScript?.Name, + Text = codeResponse?.Result ?? string.Empty + }; - var codeExeResponse = new CodeExecutionResponseModel + var codeExecution = new CodeExecutionResponseModel { CodeProcessor = codeProcessor.Provider, CodeScript = context.CodeScript, - ExecutionResult = codeResponse?.ToString() ?? string.Empty, + ExecutionResult = codeResponse, Text = message.Content, Arguments = context.Arguments?.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty) }; @@ -288,11 +285,11 @@ await hook.OnResponseGenerated(new InstructResponseModel // After code execution foreach (var hook in hooks) { - await hook.AfterCompletion(agent, response ?? new()); - await hook.AfterCodeExecution(agent, codeExeResponse); + await hook.AfterCompletion(agent, instructResult); + await hook.AfterCodeExecution(agent, codeExecution); } - return response; + return instructResult; } private async Task GetTextCompletion( diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs index 1b2b43748..8ba2dfdfa 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs @@ -82,7 +82,7 @@ public override async Task AfterCodeExecution(Agent agent, CodeExecutionResponse TemplateName = response.CodeScript?.Name, UserMessage = response.Text, SystemInstruction = $"Code script name: {response.CodeScript}, Version: {codeScriptVersion.ToString("o")}", - CompletionText = response.ExecutionResult, + CompletionText = response.ExecutionResult?.ToString() ?? string.Empty, States = response.Arguments?.ToDictionary() ?? [], UserId = user?.Id }