From aa93512a325ffbbca2600b24e83d0d6c67e9ee1a Mon Sep 17 00:00:00 2001 From: Ryan Sweet Date: Thu, 13 Mar 2025 08:40:50 -0700 Subject: [PATCH 1/3] add public methods for Load(Stream...) and LoadAsync(Stream,...), includes tests --- .../promptycs/Prompty.Core.Tests/LoadTests.cs | 28 +++++++++++++++ runtime/promptycs/Prompty.Core/Prompty.cs | 36 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs index e5fdd14e..03d46f5f 100644 --- a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs +++ b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs @@ -36,6 +36,34 @@ public void LoadRawWithConfig(string path) Assert.Equal("FAKE_TYPE", prompty.Model?.Configuration.Type); } + /// + /// Test the Loading from a Stream + /// + [Fact] + public void LoadStream() + { + var path = "prompty/basic.prompty"; + using var stream = File.OpenRead(path); + var prompty = Prompty.Load(stream); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + + /// + /// Test the Loading from a Stream Async + /// + [Fact] + public async Task LoadStreamAsync() + { + var path = "prompty/basic.prompty"; + using var stream = File.OpenRead(path); + var prompty = await Prompty.LoadAsync(stream); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + [Fact] public void BasicSampleParameters() { diff --git a/runtime/promptycs/Prompty.Core/Prompty.cs b/runtime/promptycs/Prompty.Core/Prompty.cs index eccd0912..4c1e2b91 100644 --- a/runtime/promptycs/Prompty.Core/Prompty.cs +++ b/runtime/promptycs/Prompty.Core/Prompty.cs @@ -181,6 +181,42 @@ public static async Task LoadAsync(string path, string configuration = return prompty; } + /// + /// Load a prompty file from a Stream + /// + /// Stream to read the prompty file from. + /// Id of the configuration to use. + public static Prompty Load(Stream stream, string configuration = "default") + { + using var reader = new StreamReader(stream); + string text = reader.ReadToEnd(); + + var global_config = GlobalConfig.Load(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; + global_config = Normalizer.Normalize(global_config, stream.ToString()); + + var frontmatter = LoadRaw(text, global_config, stream.ToString()); + var prompty = Convert(frontmatter, stream.ToString()); + return prompty; + } + + /// + /// Load a prompty file from a Stream Asynchronously + /// + /// Stream to read the prompty file from. + /// Id of the configuration to use. + public static async Task LoadAsync(Stream stream, string configuration = "default") + { + using var reader = new StreamReader(stream); + string text = await reader.ReadToEndAsync(); + + var global_config = await GlobalConfig.LoadAsync(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; + global_config = Normalizer.Normalize(global_config, stream.ToString()); + + var frontmatter = LoadRaw(text, global_config, stream.ToString()); + var prompty = Convert(frontmatter, stream.ToString()); + return prompty; + } + /// /// Load a prompty file using the provided text content. /// From cfa4007328abb6b3dcc628431b0f18a9d271221d Mon Sep 17 00:00:00 2001 From: Ryan Sweet Date: Thu, 13 Mar 2025 11:54:55 -0700 Subject: [PATCH 2/3] add requested tests --- .../promptycs/Prompty.Core.Tests/LoadTests.cs | 43 +++++++++++++++++++ .../Prompty.Core.Tests.csproj | 1 + 2 files changed, 44 insertions(+) diff --git a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs index 03d46f5f..e296a7a2 100644 --- a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs +++ b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs @@ -1,3 +1,4 @@ +using System.Reflection; using System.Transactions; namespace Prompty.Core.Tests; @@ -50,6 +51,48 @@ public void LoadStream() Assert.NotNull(prompty.Content); } + /// + /// Test the Loading from an embedded resource + /// + [Fact] + public void LoadEmbeddedResource() + { + // Get the fully qualified name of the embedded resource + var assembly = Assembly.GetExecutingAssembly(); + var resourceNames = assembly.GetManifestResourceNames(); + var resourceName = resourceNames.FirstOrDefault(r => r.EndsWith("basic.prompty")); + + Assert.NotNull(resourceName); // Ensure we found the resource + + using var stream = assembly.GetManifestResourceStream(resourceName); + Assert.NotNull(stream); + var prompty = Prompty.Load(stream!); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + + /// + /// Test the Loading from an embedded resource with config + /// + [Fact] + public void LoadEmbeddedResourceWithConfig() + { + // Get the fully qualified name of the embedded resource + var assembly = Assembly.GetExecutingAssembly(); + var resourceNames = assembly.GetManifestResourceNames(); + var resourceName = resourceNames.FirstOrDefault(r => r.EndsWith("basic.prompty")); + + Assert.NotNull(resourceName); // Ensure we found the resource + + using var stream = assembly.GetManifestResourceStream(resourceName); + Assert.NotNull(stream); + var prompty = Prompty.Load(stream!, "fake"); + + Assert.Equal("FAKE_TYPE", prompty.Model?.Configuration.Type); + } + + /// /// Test the Loading from a Stream Async /// diff --git a/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj b/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj index bb5a5b8b..e6a0505d 100644 --- a/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj +++ b/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj @@ -69,6 +69,7 @@ Always + Always From c78ccdf7dd0ce439a15225514b70702a8c4a2098 Mon Sep 17 00:00:00 2001 From: Ryan Sweet Date: Sun, 16 Mar 2025 09:54:55 -0700 Subject: [PATCH 3/3] fix warnings --- runtime/promptycs/Prompty.Core/Prompty.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtime/promptycs/Prompty.Core/Prompty.cs b/runtime/promptycs/Prompty.Core/Prompty.cs index 4c1e2b91..7170f375 100644 --- a/runtime/promptycs/Prompty.Core/Prompty.cs +++ b/runtime/promptycs/Prompty.Core/Prompty.cs @@ -192,7 +192,8 @@ public static Prompty Load(Stream stream, string configuration = "default") string text = reader.ReadToEnd(); var global_config = GlobalConfig.Load(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; - global_config = Normalizer.Normalize(global_config, stream.ToString()); + var streamPath = stream.ToString() ?? string.Empty; + global_config = Normalizer.Normalize(global_config, streamPath); var frontmatter = LoadRaw(text, global_config, stream.ToString()); var prompty = Convert(frontmatter, stream.ToString()); @@ -210,7 +211,8 @@ public static async Task LoadAsync(Stream stream, string configuration string text = await reader.ReadToEndAsync(); var global_config = await GlobalConfig.LoadAsync(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; - global_config = Normalizer.Normalize(global_config, stream.ToString()); + var streamPath = stream.ToString() ?? string.Empty; + global_config = Normalizer.Normalize(global_config, streamPath); var frontmatter = LoadRaw(text, global_config, stream.ToString()); var prompty = Convert(frontmatter, stream.ToString());