Skip to content

Add FileIOPlugin for local file system operations #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Active.Toolbox.BlazorWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

kernel.Plugins.AddFromType<Active.Toolbox.Core.Plugins.MathPlugin>("Math");
kernel.Plugins.AddFromType<Active.Toolbox.Core.Plugins.TimePlugin>("Time");
kernel.Plugins.AddFromType<Active.Toolbox.Core.Plugins.FileIOPlugin>("FileIO");

// Built-in plugins
//#pragma warning disable SKEXP0050
//#pragma warning disable SKEX
//kernel.Plugins.AddFromType<FileIOPlugin>("FileIO");
//kernel.Plugins.AddFromType<ConversationSummaryPlugin>("ConversationSummary");
//kernel.Plugins.AddFromType<Microsoft.SemanticKernel.Plugins.Core.WaitPlugin>("Wait");
//#pragma warning restore SKEXP0050
//#pragma warning restore SKEX

return kernel;
});
Expand Down
40 changes: 40 additions & 0 deletions src/Active.Toolbox.Core/Plugins/FileIOPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.ComponentModel;
using System.IO;
using Microsoft.SemanticKernel;

namespace Active.Toolbox.Core.Plugins;

public class FileIOPlugin
{
[KernelFunction("FileIO_ListFiles"), Description("List files in a specified directory")]
public static string[] ListFiles(
[Description("The path of the directory")] string directoryPath)
{
if (!Directory.Exists(directoryPath))
{
throw new DirectoryNotFoundException($"Directory not found: {directoryPath}");
}

return Directory.GetFiles(directoryPath);
}

[KernelFunction("FileIO_ReadFile"), Description("Read the contents of a specified file")]
public static string ReadFile(
[Description("The path of the file")] string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"File not found: {filePath}");
}

return File.ReadAllText(filePath);
}

[KernelFunction("FileIO_WriteFile"), Description("Write content to a specified file")]
public static void WriteFile(
[Description("The path of the file")] string filePath,
[Description("The content to write")] string content)
{
File.WriteAllText(filePath, content);
}
}