diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index a2f2a17f..e7b054d7 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Agents.Options; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Repositories.Filters; @@ -65,4 +66,13 @@ public interface IAgentService Task> GetUserAgents(string userId); PluginDef GetPlugin(string agentId); + + Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) + => Task.FromResult(new List()); + + Task GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src) + => Task.FromResult(string.Empty); + + Task UpdateAgentCodeScripts(string agentId, List codeScripts, AgentCodeScriptUpdateOptions? options = null) + => Task.FromResult(false); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Options/AgentCodeScriptUpdateOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Options/AgentCodeScriptUpdateOptions.cs new file mode 100644 index 00000000..4b5e9e95 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Options/AgentCodeScriptUpdateOptions.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Repositories.Models; + +namespace BotSharp.Abstraction.Agents.Options; + +public class AgentCodeScriptUpdateOptions : AgentCodeScriptDbUpdateOptions +{ + public bool DeleteIfNotIncluded { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index f2ebe6a8..63ca3da3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Repositories.Filters; +using BotSharp.Abstraction.Repositories.Models; using BotSharp.Abstraction.Roles.Models; using BotSharp.Abstraction.Shared; using BotSharp.Abstraction.Statistics.Enums; @@ -111,7 +112,7 @@ public interface IBotSharpRepository : IHaveServiceProvider => throw new NotImplementedException(); string? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src) => throw new NotImplementedException(); - bool UpdateAgentCodeScripts(string agentId, List scripts) + bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) => throw new NotImplementedException(); bool BulkInsertAgentCodeScripts(string agentId, List scripts) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/AgentCodeScriptDbUpdateOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/AgentCodeScriptDbUpdateOptions.cs new file mode 100644 index 00000000..9217445b --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/AgentCodeScriptDbUpdateOptions.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Abstraction.Repositories.Models; + +public class AgentCodeScriptDbUpdateOptions +{ + public bool IsUpsert { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CodeScripts.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CodeScripts.cs new file mode 100644 index 00000000..5c7c8bc0 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CodeScripts.cs @@ -0,0 +1,46 @@ +using BotSharp.Abstraction.Agents.Options; + +namespace BotSharp.Core.Agents.Services; + +public partial class AgentService +{ + public async Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) + { + var db = _services.GetRequiredService(); + var scripts = db.GetAgentCodeScripts(agentId, filter); + return await Task.FromResult(scripts); + } + + public async Task GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src) + { + var db = _services.GetRequiredService(); + var script = db.GetAgentCodeScript(agentId, scriptName, scriptType); + return await Task.FromResult(script); + } + + public async Task UpdateAgentCodeScripts(string agentId, List codeScripts, AgentCodeScriptUpdateOptions? options = null) + { + if (string.IsNullOrWhiteSpace(agentId) || codeScripts.IsNullOrEmpty()) + { + return false; + } + + var db = _services.GetRequiredService(); + + var toDeleteScripts = new List(); + if (options?.DeleteIfNotIncluded == true) + { + var curDbScripts = await GetAgentCodeScripts(agentId); + var codePaths = codeScripts.Select(x => x.CodePath).ToList(); + toDeleteScripts = curDbScripts.Where(x => !codePaths.Contains(x.CodePath)).ToList(); + } + + var updateResult = db.UpdateAgentCodeScripts(agentId, codeScripts, options); + if (!toDeleteScripts.IsNullOrEmpty()) + { + db.DeleteAgentCodeScripts(agentId, toDeleteScripts); + } + + return updateResult; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index daa55803..be162e6d 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -150,9 +150,8 @@ public partial class InstructService return response; } - + var agentService = _services.GetRequiredService(); var state = _services.GetRequiredService(); - var db = _services.GetRequiredService(); var hooks = _services.GetHooks(agent.Id); var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter"; @@ -187,7 +186,7 @@ public partial class InstructService } // Get code script - var codeScript = db.GetAgentCodeScript(agent.Id, scriptName, scriptType: AgentCodeScriptType.Src); + var codeScript = await agentService.GetAgentCodeScript(agent.Id, scriptName, scriptType: AgentCodeScriptType.Src); if (string.IsNullOrWhiteSpace(codeScript)) { #if DEBUG diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs index 764287dd..c332d147 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Repositories.Models; using System.IO; namespace BotSharp.Core.Repository; @@ -73,7 +74,7 @@ public partial class FileRepository return string.Empty; } - public bool UpdateAgentCodeScripts(string agentId, List scripts) + public bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) { if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { @@ -95,7 +96,10 @@ public partial class FileRepository } var file = Path.Combine(dir, script.Name); - File.WriteAllText(file, script.Content); + if (options?.IsUpsert == true || File.Exists(file)) + { + File.WriteAllText(file, script.Content); + } } return true; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs index 3d551b7d..8e8a8154 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories.Filters; +using BotSharp.Abstraction.Repositories.Models; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -55,7 +56,7 @@ public partial class MongoRepository return found?.Content; } - public bool UpdateAgentCodeScripts(string agentId, List scripts) + public bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) { if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { @@ -73,7 +74,7 @@ public partial class MongoRepository }), Builders.Update.Set(y => y.Content, x.Content) .Set(x => x.UpdatedTime, DateTime.UtcNow) - )) + ) { IsUpsert = options?.IsUpsert ?? false }) .ToList(); var result = _dc.AgentCodeScripts.BulkWrite(ops, new BulkWriteOptions { IsOrdered = false });