autogen/cli/SandboxSkill.cs
Kosta Petan 1fcac0179d Chaining the whole team (#1)
* do it all :)

* save implementation as it comes

* add sandbox skill

* generate files via the sandbox skill in output/src

---------

Co-authored-by: Kosta Petan <kosta.petan@microsoft.com>
2023-06-08 17:27:11 +02:00

44 lines
1.9 KiB
C#

using DotNet.Testcontainers.Builders;
using Microsoft.SemanticKernel.SkillDefinition;
public class SandboxSkill
{
[SKFunction("Run a script in Alpine sandbox")]
[SKFunctionInput(Description = "The script to be executed")]
[SKFunctionName("RunInAlpine")]
public async Task<string> RunInAlpineAsync(string input)
{
return await RunInContainer(input, "alpine");
}
[SKFunction("Run a script in dotnet alpine sandbox")]
[SKFunctionInput(Description = "The script to be executed")]
[SKFunctionName("RunInDotnetAlpine")]
public async Task<string> RunInDotnetAlpineAsync(string input)
{
return await RunInContainer(input, "mcr.microsoft.com/dotnet/sdk:7.0");
}
private async Task<string> RunInContainer(string input, string image)
{
var tempScriptFile = $"{Guid.NewGuid().ToString()}.sh";
var tempScriptPath = $"./output/{tempScriptFile}";
await File.WriteAllTextAsync(tempScriptPath, input);
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(),"output", "src"));
var dotnetContainer = new ContainerBuilder()
.WithName(Guid.NewGuid().ToString("D"))
.WithImage(image)
.WithBindMount(Path.Combine(Directory.GetCurrentDirectory(),"output", "src"), "/src")
.WithBindMount(Path.Combine(Directory.GetCurrentDirectory(), tempScriptPath), $"/src/{tempScriptFile}")
.WithWorkingDirectory("/src")
.WithCommand("sh", tempScriptFile)
.Build();
await dotnetContainer.StartAsync()
.ConfigureAwait(false);
// Cleanup
File.Delete(tempScriptPath);
File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "output", "src", tempScriptFile));
return "";
}
}