This commit is contained in:
Jicheng Lu 2025-09-30 20:45:32 -05:00
parent bc500a4dfa
commit 0a7dbfe44f
9 changed files with 38 additions and 39 deletions

View file

@ -6,5 +6,6 @@ public interface ICodeInterpretService
{
string Provider { get; }
Task<CodeInterpretResult> RunCode(string codeScript, IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
Task<CodeInterpretResult> RunCode(string codeScript, CodeInterpretOptions? options = null)
=> throw new NotImplementedException();
}

View file

@ -2,4 +2,5 @@ namespace BotSharp.Abstraction.CodeInterpreter.Models;
public class CodeInterpretOptions
{
public List<KeyValue>? Arguments { get; set; }
}

View file

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

View file

@ -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();
}
}

View file

@ -63,9 +63,9 @@ public partial class FileRepository
return string.Empty;
}
public bool UpdateAgentCodeScript(string agentId, AgentCodeScript script)
public bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> 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;
}

View file

@ -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;

View file

@ -46,30 +46,27 @@ public partial class MongoRepository
return found?.Content;
}
public bool UpdateAgentCodeScript(string agentId, AgentCodeScript script)
public bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts)
{
if (string.IsNullOrWhiteSpace(agentId) || script == null)
if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty())
{
return false;
}
var builder = Builders<AgentCodeDocument>.Filter;
var filters = new List<FilterDefinition<AgentCodeDocument>>()
{
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<AgentCodeDocument>(
builder.And(new List<FilterDefinition<AgentCodeDocument>>
{
builder.Eq(y => y.AgentId, agentId),
builder.Eq(y => y.Name, x.Name)
}),
Builders<AgentCodeDocument>.Update.Set(y => y.Content, x.Content)
))
.ToList();
var found = _dc.AgentCodes.Find(filterDef).FirstOrDefault();
if (found == null)
{
return false;
}
var update = Builders<AgentCodeDocument>.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<AgentCodeScript> scripts)

View file

@ -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<IRichMessage>
{
Recipient = new Recipient { Id = convService.ConversationId },

View file

@ -20,8 +20,7 @@ public class PyInterpretService : ICodeInterpretService
public string Provider => "python-interpreter";
public async Task<CodeInterpretResult> RunCode(string codeScript,
IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null)
public async Task<CodeInterpretResult> 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
};
}