refine update agent code scripts
This commit is contained in:
parent
835a3c71ef
commit
3e71f24d1a
|
|
@ -1,3 +1,4 @@
|
||||||
|
using BotSharp.Abstraction.Agents.Options;
|
||||||
using BotSharp.Abstraction.Functions.Models;
|
using BotSharp.Abstraction.Functions.Models;
|
||||||
using BotSharp.Abstraction.Plugins.Models;
|
using BotSharp.Abstraction.Plugins.Models;
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
using BotSharp.Abstraction.Repositories.Filters;
|
||||||
|
|
@ -65,4 +66,13 @@ public interface IAgentService
|
||||||
Task<List<UserAgent>> GetUserAgents(string userId);
|
Task<List<UserAgent>> GetUserAgents(string userId);
|
||||||
|
|
||||||
PluginDef GetPlugin(string agentId);
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
|
|
||||||
|
namespace BotSharp.Abstraction.Agents.Options;
|
||||||
|
|
||||||
|
public class AgentCodeScriptUpdateOptions : AgentCodeScriptDbUpdateOptions
|
||||||
|
{
|
||||||
|
public bool DeleteIfNotIncluded { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using BotSharp.Abstraction.Loggers.Models;
|
using BotSharp.Abstraction.Loggers.Models;
|
||||||
using BotSharp.Abstraction.Plugins.Models;
|
using BotSharp.Abstraction.Plugins.Models;
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
using BotSharp.Abstraction.Repositories.Filters;
|
||||||
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
using BotSharp.Abstraction.Roles.Models;
|
using BotSharp.Abstraction.Roles.Models;
|
||||||
using BotSharp.Abstraction.Shared;
|
using BotSharp.Abstraction.Shared;
|
||||||
using BotSharp.Abstraction.Statistics.Enums;
|
using BotSharp.Abstraction.Statistics.Enums;
|
||||||
|
|
@ -111,7 +112,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
||||||
=> throw new NotImplementedException();
|
=> throw new NotImplementedException();
|
||||||
string? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
|
string? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
|
||||||
=> throw new NotImplementedException();
|
=> throw new NotImplementedException();
|
||||||
bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts)
|
bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
|
||||||
=> throw new NotImplementedException();
|
=> throw new NotImplementedException();
|
||||||
bool BulkInsertAgentCodeScripts(string agentId, List<AgentCodeScript> scripts)
|
bool BulkInsertAgentCodeScripts(string agentId, List<AgentCodeScript> scripts)
|
||||||
=> throw new NotImplementedException();
|
=> throw new NotImplementedException();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace BotSharp.Abstraction.Repositories.Models;
|
||||||
|
|
||||||
|
public class AgentCodeScriptDbUpdateOptions
|
||||||
|
{
|
||||||
|
public bool IsUpsert { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -150,9 +150,8 @@ public partial class InstructService
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
var state = _services.GetRequiredService<IConversationStateService>();
|
var state = _services.GetRequiredService<IConversationStateService>();
|
||||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
||||||
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
|
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
|
||||||
|
|
||||||
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
|
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
|
||||||
|
|
@ -187,7 +186,7 @@ public partial class InstructService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get code script
|
// 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 (string.IsNullOrWhiteSpace(codeScript))
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace BotSharp.Core.Repository;
|
namespace BotSharp.Core.Repository;
|
||||||
|
|
@ -73,7 +74,7 @@ public partial class FileRepository
|
||||||
return string.Empty;
|
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())
|
if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
|
|
@ -95,7 +96,10 @@ public partial class FileRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
var file = Path.Combine(dir, script.Name);
|
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;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
using BotSharp.Abstraction.Repositories.Filters;
|
||||||
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.MongoStorage.Repository;
|
namespace BotSharp.Plugin.MongoStorage.Repository;
|
||||||
|
|
||||||
|
|
@ -55,7 +56,7 @@ public partial class MongoRepository
|
||||||
return found?.Content;
|
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())
|
if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
|
|
@ -73,7 +74,7 @@ public partial class MongoRepository
|
||||||
}),
|
}),
|
||||||
Builders<AgentCodeScriptDocument>.Update.Set(y => y.Content, x.Content)
|
Builders<AgentCodeScriptDocument>.Update.Set(y => y.Content, x.Content)
|
||||||
.Set(x => x.UpdatedTime, DateTime.UtcNow)
|
.Set(x => x.UpdatedTime, DateTime.UtcNow)
|
||||||
))
|
) { IsUpsert = options?.IsUpsert ?? false })
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var result = _dc.AgentCodeScripts.BulkWrite(ops, new BulkWriteOptions { IsOrdered = false });
|
var result = _dc.AgentCodeScripts.BulkWrite(ops, new BulkWriteOptions { IsOrdered = false });
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue