From dc32fca6ebe8b03f474ff6adac7b9912b7b8cf2c Mon Sep 17 00:00:00 2001 From: Jakob Ehn Date: Thu, 9 May 2024 13:15:35 +0200 Subject: [PATCH] Add FileIOPlugin for local file system operations --- src/Active.Toolbox.BlazorWeb/Program.cs | 5 ++- .../Plugins/FileIOPlugin.cs | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/Active.Toolbox.Core/Plugins/FileIOPlugin.cs diff --git a/src/Active.Toolbox.BlazorWeb/Program.cs b/src/Active.Toolbox.BlazorWeb/Program.cs index ebfc445..970e0a6 100644 --- a/src/Active.Toolbox.BlazorWeb/Program.cs +++ b/src/Active.Toolbox.BlazorWeb/Program.cs @@ -24,13 +24,14 @@ kernel.Plugins.AddFromType("Math"); kernel.Plugins.AddFromType("Time"); + kernel.Plugins.AddFromType("FileIO"); // Built-in plugins - //#pragma warning disable SKEXP0050 + //#pragma warning disable SKEX //kernel.Plugins.AddFromType("FileIO"); //kernel.Plugins.AddFromType("ConversationSummary"); //kernel.Plugins.AddFromType("Wait"); - //#pragma warning restore SKEXP0050 + //#pragma warning restore SKEX return kernel; }); diff --git a/src/Active.Toolbox.Core/Plugins/FileIOPlugin.cs b/src/Active.Toolbox.Core/Plugins/FileIOPlugin.cs new file mode 100644 index 0000000..32cd530 --- /dev/null +++ b/src/Active.Toolbox.Core/Plugins/FileIOPlugin.cs @@ -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); + } +}