refine update agent code scripts

This commit is contained in:
Jicheng Lu 2025-10-10 15:19:30 -05:00
parent 835a3c71ef
commit 3e71f24d1a
8 changed files with 83 additions and 8 deletions

View file

@ -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<List<UserAgent>> GetUserAgents(string userId);
PluginDef GetPlugin(string agentId);
Task<List<AgentCodeScript>> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null)
=> Task.FromResult(new List<AgentCodeScript>());
Task<string?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
=> Task.FromResult(string.Empty);
Task<bool> UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> codeScripts, AgentCodeScriptUpdateOptions? options = null)
=> Task.FromResult(false);
}

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Repositories.Models;
namespace BotSharp.Abstraction.Agents.Options;
public class AgentCodeScriptUpdateOptions : AgentCodeScriptDbUpdateOptions
{
public bool DeleteIfNotIncluded { get; set; }
}

View file

@ -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<AgentCodeScript> scripts)
bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
=> throw new NotImplementedException();
bool BulkInsertAgentCodeScripts(string agentId, List<AgentCodeScript> scripts)
=> throw new NotImplementedException();

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Repositories.Models;
public class AgentCodeScriptDbUpdateOptions
{
public bool IsUpsert { get; set; }
}

View file

@ -0,0 +1,46 @@
using BotSharp.Abstraction.Agents.Options;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task<List<AgentCodeScript>> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var scripts = db.GetAgentCodeScripts(agentId, filter);
return await Task.FromResult(scripts);
}
public async Task<string?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var script = db.GetAgentCodeScript(agentId, scriptName, scriptType);
return await Task.FromResult(script);
}
public async Task<bool> UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> codeScripts, AgentCodeScriptUpdateOptions? options = null)
{
if (string.IsNullOrWhiteSpace(agentId) || codeScripts.IsNullOrEmpty())
{
return false;
}
var db = _services.GetRequiredService<IBotSharpRepository>();
var toDeleteScripts = new List<AgentCodeScript>();
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;
}
}

View file

@ -150,9 +150,8 @@ public partial class InstructService
return response;
}
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
var db = _services.GetRequiredService<IBotSharpRepository>();
var hooks = _services.GetHooks<IInstructHook>(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

View file

@ -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<AgentCodeScript> scripts)
public bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
{
if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty())
{
@ -95,8 +96,11 @@ public partial class FileRepository
}
var file = Path.Combine(dir, script.Name);
if (options?.IsUpsert == true || File.Exists(file))
{
File.WriteAllText(file, script.Content);
}
}
return true;
}

View file

@ -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<AgentCodeScript> scripts)
public bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
{
if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty())
{
@ -73,7 +74,7 @@ public partial class MongoRepository
}),
Builders<AgentCodeScriptDocument>.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 });