Skip to content

.Net: Issue 11562 (add plugin description) #11601

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,27 @@ public static async Task<KernelPlugin> ImportPluginFromApiManifestAsync(
string filePath,
ApiManifestPluginParameters? pluginParameters = null,
CancellationToken cancellationToken = default)
=> await kernel.ImportPluginFromApiManifestAsync(pluginName, filePath, null, pluginParameters, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Imports a plugin from an API manifest asynchronously.
/// </summary>
/// <param name="kernel">The kernel instance.</param>
/// <param name="pluginName">The name of the plugin.</param>
/// <param name="filePath">The file path of the API manifest.</param>
/// <param name="description">The description of the plugin.</param>
/// <param name="pluginParameters">Optional parameters for the plugin setup.</param>
/// <param name="cancellationToken">Optional cancellation token.</param>
/// <returns>The imported plugin.</returns>
public static async Task<KernelPlugin> ImportPluginFromApiManifestAsync(
this Kernel kernel,
string pluginName,
string filePath,
string? description,
ApiManifestPluginParameters? pluginParameters = null,
CancellationToken cancellationToken = default)
{
KernelPlugin plugin = await kernel.CreatePluginFromApiManifestAsync(pluginName, filePath, pluginParameters, cancellationToken).ConfigureAwait(false);
KernelPlugin plugin = await kernel.CreatePluginFromApiManifestAsync(pluginName, filePath, description, pluginParameters, cancellationToken).ConfigureAwait(false);
kernel.Plugins.Add(plugin);
return plugin;
}
Expand All @@ -60,6 +79,25 @@ public static async Task<KernelPlugin> CreatePluginFromApiManifestAsync(
string filePath,
ApiManifestPluginParameters? pluginParameters = null,
CancellationToken cancellationToken = default)
=> await kernel.CreatePluginFromApiManifestAsync(pluginName, filePath, null, pluginParameters, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Creates a kernel plugin from an API manifest file asynchronously.
/// </summary>
/// <param name="kernel">The kernel instance.</param>
/// <param name="pluginName">The name of the plugin.</param>
/// <param name="filePath">The file path of the API manifest.</param>
/// <param name="description">The description of the plugin.</param>
/// <param name="pluginParameters">Optional parameters for the plugin setup.</param>
/// <param name="cancellationToken">Optional cancellation token.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the created kernel plugin.</returns>
public static async Task<KernelPlugin> CreatePluginFromApiManifestAsync(
this Kernel kernel,
string pluginName,
string filePath,
string? description,
ApiManifestPluginParameters? pluginParameters = null,
CancellationToken cancellationToken = default)
{
Verify.NotNull(kernel);
Verify.ValidPluginName(pluginName, kernel.Plugins);
Expand Down Expand Up @@ -187,6 +225,6 @@ await DocumentLoader.LoadDocumentFromUriAsStreamAsync(new Uri(apiDescriptionUrl)
}
}

return KernelPluginFactory.CreateFromFunctions(pluginName, null, functions);
return KernelPluginFactory.CreateFromFunctions(pluginName, description, functions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<EmbeddedResource Include="OpenApi\TestPlugins\multipart-form-data.json" />
</ItemGroup>
<ItemGroup>
<None Update="OpenApi\TestPlugins\example-apimanifest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="OpenApi\TestPlugins\messages-apiplugin.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft. All rights reserved.

using System.IO;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Xunit;

namespace SemanticKernel.Functions.UnitTests.OpenApi.Extensions;
public sealed class ApiManifestKernelExtensionsTests
{
[Fact]
public async Task ItCanCreatePluginFromApiManifestAsync()
{
// Act
var kernel = new Kernel();
var testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "OpenApi", "TestPlugins");
var manifestFilePath = Path.Combine(testPluginsDir, "example-apimanifest.json");

// Arrange
var plugin = await kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath);

// Assert
Assert.NotNull(plugin);
Assert.Equal(3, plugin.FunctionCount);
}

[Fact]
public async Task ItCanCreatePluginFromApiManifestWithDescriptionParameterAsync()
{
// Act
var kernel = new Kernel();
var testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "OpenApi", "TestPlugins");
var manifestFilePath = Path.Combine(testPluginsDir, "example-apimanifest.json");
var description = "My plugin description";

// Arrange
var plugin = await kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description);

// Assert
Assert.NotNull(plugin);
Assert.Equal(description, plugin.Description);
}

[Fact]
public async Task ItCanCreatePluginFromApiManifestWithEmptyDescriptionParameterAsync()
{
// Act
var kernel = new Kernel();
var testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "OpenApi", "TestPlugins");
var manifestFilePath = Path.Combine(testPluginsDir, "example-apimanifest.json");

// Arrange
var plugin = await kernel.CreatePluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description: null);

// Assert
Assert.NotNull(plugin);
Assert.Empty(plugin.Description);
}

[Fact]
public async Task ItCanImportPluginFromApiManifestAsync()
{
// Act
var kernel = new Kernel();
var testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "OpenApi", "TestPlugins");
var manifestFilePath = Path.Combine(testPluginsDir, "example-apimanifest.json");

// Arrange
var plugin = await kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath);

// Assert
Assert.NotNull(plugin);
Assert.Equal(3, plugin.FunctionCount);
Assert.Single(kernel.Plugins);
}

[Fact]
public async Task ItCanImportPluginFromApiManifestWithDescriptionParameterAsync()
{
// Act
var kernel = new Kernel();
var testPluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "OpenApi", "TestPlugins");
var manifestFilePath = Path.Combine(testPluginsDir, "example-apimanifest.json");
var description = "My plugin description";

// Arrange
var plugin = await kernel.ImportPluginFromApiManifestAsync("ApiManifestPlugin", manifestFilePath, description);

// Assert
Assert.NotNull(plugin);
Assert.Equal(description, plugin.Description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"applicationName": "My Application",
"publisher": {
"name": "Alice",
"contactEmail": "[email protected]"
},
"apiDependencies": {
"moostodon": {
"apiDescriptionUrl": "https://raw.githubusercontent.com/APIPatterns/Moostodon/main/spec/tsp-output/%40typespec/openapi3/openapi.yaml",
"auth": {
"clientIdentifier": "some-uuid-here",
"access": [
"resourceA.ReadWrite",
"resourceB.ReadWrite",
"resourceB.Read"
]
},
"requests": [
{
"method": "GET",
"uriTemplate": "/api/v1/accounts/search"
},
{
"method": "GET",
"uriTemplate": "/api/v1/accounts/{id}"
}
]
},
"MicrosoftGraph": {
"apiDescriptionUrl": "https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/openApiDocs/v1.0/DirectoryObjects.yml",
"auth": {
"clientIdentifier": "some-uuid-here",
"access": [
"resourceA.ReadWrite",
"resourceB.Read"
]
},
"requests": [
{
"method": "GET",
"uriTemplate": "/directoryObjects/{directoryObject-id}"
}
]
}
}
}
Loading