From 0a7dbfe44f28c3f79b5e00dd521a11027ef36bf8 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 30 Sep 2025 20:45:32 -0500 Subject: [PATCH] refine --- .../CodeInterpreter/ICodeInterpretService.cs | 3 +- .../Models/CodeInterpretOptions.cs | 1 + .../Repositories/IBotSharpRepository.cs | 2 +- .../Services/InstructService.Execute.cs | 5 ++- .../FileRepository.AgentCode.cs | 19 ++++++------ .../Repository/MongoRepository.Agent.cs | 1 - .../Repository/MongoRepository.AgentCode.cs | 31 +++++++++---------- .../Functions/PyProgrammerFn.cs | 4 +-- .../Services/PyInterpretService.cs | 11 +++---- 9 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs index 49b6b91d..bdc10f5c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/ICodeInterpretService.cs @@ -6,5 +6,6 @@ public interface ICodeInterpretService { string Provider { get; } - Task RunCode(string codeScript, IEnumerable? arguments = null, CodeInterpretOptions? options = null); + Task RunCode(string codeScript, CodeInterpretOptions? options = null) + => throw new NotImplementedException(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs index ded31cbe..5255c18f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/CodeInterpreter/Models/CodeInterpretOptions.cs @@ -2,4 +2,5 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models; public class CodeInterpretOptions { + public List? Arguments { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 3d23352a..85f75e52 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -111,7 +111,7 @@ public interface IBotSharpRepository : IHaveServiceProvider => throw new NotImplementedException(); string? GetAgentCodeScript(string agentId, string scriptName) => throw new NotImplementedException(); - bool UpdateAgentCodeScript(string agentId, AgentCodeScript script) + bool UpdateAgentCodeScripts(string agentId, List scripts) => throw new NotImplementedException(); bool BulkInsertAgentCodeScripts(string agentId, List scripts) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index 3916beb1..44ab141e 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -65,7 +65,10 @@ public partial class InstructService } else { - var result = await codeInterpreter.RunCode(codeScript, codeOptions.Arguments); + var result = await codeInterpreter.RunCode(codeScript, options: new() + { + Arguments = codeOptions?.Arguments + }); response.Text = result?.Result?.ToString(); } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCode.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCode.cs index 69934174..b596b4ed 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCode.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCode.cs @@ -63,9 +63,9 @@ public partial class FileRepository return string.Empty; } - public bool UpdateAgentCodeScript(string agentId, AgentCodeScript script) + public bool UpdateAgentCodeScripts(string agentId, List scripts) { - if (string.IsNullOrWhiteSpace(agentId) || script == null) + if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { return false; } @@ -76,18 +76,17 @@ public partial class FileRepository return false; } - var found = Directory.GetFiles(dir).FirstOrDefault(f => - { - var fileName = Path.GetFileName(f); - return fileName.IsEqualTo(script.Name); - }); + var dict = scripts.DistinctBy(x => x.Name).ToDictionary(x => x.Name, x => x); + var files = Directory.GetFiles(dir).Where(x => dict.Keys.Contains(Path.GetFileName(x))).ToList(); - if (found == null) + foreach (var file in files) { - return false; + if (dict.TryGetValue(Path.GetFileName(file), out var script)) + { + File.WriteAllText(file, script.Content); + } } - File.WriteAllText(found, script.Content); return true; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 407832d9..3d085c70 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -2,7 +2,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Routing.Models; -using MongoDB.Driver; namespace BotSharp.Plugin.MongoStorage.Repository; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCode.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCode.cs index e07f3c8c..580497f7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCode.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCode.cs @@ -46,30 +46,27 @@ public partial class MongoRepository return found?.Content; } - public bool UpdateAgentCodeScript(string agentId, AgentCodeScript script) + public bool UpdateAgentCodeScripts(string agentId, List scripts) { - if (string.IsNullOrWhiteSpace(agentId) || script == null) + if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { return false; } var builder = Builders.Filter; - var filters = new List>() - { - builder.Eq(x => x.AgentId, agentId), - builder.Eq(x => x.Name, script.Name) - }; - var filterDef = builder.And(filters); + var ops = scripts.Where(x => !string.IsNullOrWhiteSpace(x.Name)) + .Select(x => new UpdateOneModel( + builder.And(new List> + { + builder.Eq(y => y.AgentId, agentId), + builder.Eq(y => y.Name, x.Name) + }), + Builders.Update.Set(y => y.Content, x.Content) + )) + .ToList(); - var found = _dc.AgentCodes.Find(filterDef).FirstOrDefault(); - if (found == null) - { - return false; - } - - var update = Builders.Update.Set(x => x.Content, script.Content); - _dc.AgentCodes.UpdateOne(filterDef, update); - return true; + var result = _dc.AgentCodes.BulkWrite(ops, new BulkWriteOptions { IsOrdered = false }); + return result.ModifiedCount > 0; } public bool BulkInsertAgentCodeScripts(string agentId, List scripts) diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs index 477778a0..6bfc9043 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs @@ -88,8 +88,8 @@ public class PyProgrammerFn : IFunctionCallback PythonEngine.Exec(ret.PythonCode, globals); // Get result - var result = stringIO.getvalue().ToString(); - message.Content = result; + var result = stringIO.getvalue()?.ToString() as string; + message.Content = result?.TrimEnd('\r', '\n') ?? string.Empty; message.RichContent = new RichContent { Recipient = new Recipient { Id = convService.ConversationId }, diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs index 82739f2a..a03638eb 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyInterpretService.cs @@ -20,8 +20,7 @@ public class PyInterpretService : ICodeInterpretService public string Provider => "python-interpreter"; - public async Task RunCode(string codeScript, - IEnumerable? arguments = null, CodeInterpretOptions? options = null) + public async Task RunCode(string codeScript, CodeInterpretOptions? options = null) { try { @@ -44,12 +43,12 @@ public class PyInterpretService : ICodeInterpretService } // Set arguments - if (!arguments.IsNullOrEmpty()) + if (options?.Arguments?.Any() == true) { var list = new PyList(); list.Append(new PyString("code.py")); - foreach (var arg in arguments) + foreach (var arg in options.Arguments) { if (!string.IsNullOrWhiteSpace(arg.Key) && !string.IsNullOrWhiteSpace(arg.Value)) { @@ -64,7 +63,7 @@ public class PyInterpretService : ICodeInterpretService PythonEngine.Exec(codeScript, globals); // Get result - var result = stringIO.getvalue().ToString(); + var result = stringIO.getvalue()?.ToString() as string; // Restore the original stdout/stderr sys.stdout = sys.__stdout__; @@ -72,7 +71,7 @@ public class PyInterpretService : ICodeInterpretService return new CodeInterpretResult { - Result = result, + Result = result?.TrimEnd('\r', '\n'), Success = true }; }