-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
.Net: Expose any custom attributes on the underlying function method #8367
Draft
anthonypuppo
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
anthonypuppo:kernel-function-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ internal sealed partial class KernelFunctionFromMethod : KernelFunction | |
/// <param name="target">The target object for the <paramref name="method"/> if it represents an instance method. This should be null if and only if <paramref name="method"/> is a static method.</param> | ||
/// <param name="functionName">The name to use for the function. If null, it will default to one derived from the method represented by <paramref name="method"/>.</param> | ||
/// <param name="description">The description to use for the function. If null, it will default to one derived from the method represented by <paramref name="method"/>, if possible (e.g. via a <see cref="DescriptionAttribute"/> on the method).</param> | ||
/// <param name="attributes">Optional function attributes. If null, it will default to those derived from the method represented by <paramref name="method"/>.</param> | ||
/// <param name="parameters">Optional parameter descriptions. If null, it will default to one derived from the method represented by <paramref name="method"/>.</param> | ||
/// <param name="returnParameter">Optional return parameter description. If null, it will default to one derived from the method represented by <paramref name="method"/>.</param> | ||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param> | ||
|
@@ -46,6 +47,7 @@ public static KernelFunction Create( | |
object? target = null, | ||
string? functionName = null, | ||
string? description = null, | ||
IEnumerable<Attribute>? attributes = null, | ||
IEnumerable<KernelParameterMetadata>? parameters = null, | ||
KernelReturnParameterMetadata? returnParameter = null, | ||
ILoggerFactory? loggerFactory = null) | ||
|
@@ -57,6 +59,7 @@ public static KernelFunction Create( | |
{ | ||
FunctionName = functionName, | ||
Description = description, | ||
Attributes = attributes, | ||
Parameters = parameters, | ||
ReturnParameter = returnParameter, | ||
LoggerFactory = loggerFactory | ||
|
@@ -87,6 +90,7 @@ public static KernelFunction Create( | |
methodDetails.Function, | ||
methodDetails.Name, | ||
options?.Description ?? methodDetails.Description, | ||
options?.Attributes?.ToList() ?? methodDetails.Attributes, | ||
options?.Parameters?.ToList() ?? methodDetails.Parameters, | ||
options?.ReturnParameter ?? methodDetails.ReturnParameter, | ||
options?.AdditionalMetadata); | ||
|
@@ -160,6 +164,7 @@ public override KernelFunction Clone(string pluginName) | |
this.Name, | ||
pluginName, | ||
this.Description, | ||
this.Metadata.Attributes, | ||
this.Metadata.Parameters, | ||
this.Metadata.ReturnParameter, | ||
this.Metadata.AdditionalProperties); | ||
|
@@ -175,16 +180,17 @@ private delegate ValueTask<FunctionResult> ImplementationFunc( | |
private static readonly object[] s_cancellationTokenNoneArray = [CancellationToken.None]; | ||
private readonly ImplementationFunc _function; | ||
|
||
private record struct MethodDetails(string Name, string Description, ImplementationFunc Function, List<KernelParameterMetadata> Parameters, KernelReturnParameterMetadata ReturnParameter); | ||
private record struct MethodDetails(string Name, string Description, ImplementationFunc Function, List<Attribute> Attributes, List<KernelParameterMetadata> Parameters, KernelReturnParameterMetadata ReturnParameter); | ||
|
||
private KernelFunctionFromMethod( | ||
ImplementationFunc implementationFunc, | ||
string functionName, | ||
string description, | ||
IReadOnlyList<Attribute> attributes, | ||
IReadOnlyList<KernelParameterMetadata> parameters, | ||
KernelReturnParameterMetadata returnParameter, | ||
ReadOnlyDictionary<string, object?>? additionalMetadata = null) : | ||
this(implementationFunc, functionName, null, description, parameters, returnParameter, additionalMetadata) | ||
this(implementationFunc, functionName, null, description, attributes, parameters, returnParameter, additionalMetadata) | ||
{ | ||
} | ||
|
||
|
@@ -193,10 +199,11 @@ private KernelFunctionFromMethod( | |
string functionName, | ||
string? pluginName, | ||
string description, | ||
IReadOnlyList<Attribute> attributes, | ||
IReadOnlyList<KernelParameterMetadata> parameters, | ||
KernelReturnParameterMetadata returnParameter, | ||
ReadOnlyDictionary<string, object?>? additionalMetadata = null) : | ||
base(functionName, pluginName, description, parameters, returnParameter, additionalMetadata: additionalMetadata) | ||
base(functionName, pluginName, description, attributes, parameters, returnParameter, additionalMetadata: additionalMetadata) | ||
{ | ||
Verify.ValidFunctionName(functionName); | ||
|
||
|
@@ -281,6 +288,7 @@ ValueTask<FunctionResult> Function(Kernel kernel, KernelFunction function, Kerne | |
Function = Function, | ||
Name = functionName!, | ||
Description = method.GetCustomAttribute<DescriptionAttribute>(inherit: true)?.Description ?? "", | ||
Attributes = [.. Attribute.GetCustomAttributes(method, inherit: true)], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only extracts attributes off the method. Possibly fine for a first pass but there's potentially value in also grabbing attributes off the plugin itself, parameters, etc. |
||
Parameters = argParameterViews, | ||
ReturnParameter = new KernelReturnParameterMetadata() | ||
{ | ||
|
@@ -470,7 +478,7 @@ private static bool TryToDeserializeValue(object value, Type targetType, out obj | |
JsonDocument document => document.Deserialize(targetType), | ||
JsonNode node => node.Deserialize(targetType), | ||
JsonElement element => element.Deserialize(targetType), | ||
// The JSON can be represented by other data types from various libraries. For example, JObject, JToken, and JValue from the Newtonsoft.Json library. | ||
// The JSON can be represented by other data types from various libraries. For example, JObject, JToken, and JValue from the Newtonsoft.Json library. | ||
// Since we don't take dependencies on these libraries and don't have access to the types here, | ||
// the only way to deserialize those types is to convert them to a string first by calling the 'ToString' method. | ||
// Attempting to use the 'JsonSerializer.Serialize' method, instead of calling the 'ToString' directly on those types, can lead to unpredictable outcomes. | ||
|
@@ -739,7 +747,7 @@ private static void ThrowForInvalidSignatureIf([DoesNotReturnIf(true)] bool cond | |
{ | ||
if (input?.GetType() is Type type && converter.CanConvertFrom(type)) | ||
{ | ||
// This line performs string to type conversion | ||
// This line performs string to type conversion | ||
return converter.ConvertFrom(context: null, culture, input); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not make more sense to just expose the raw
MethodInfo
instance, rather than just the attributes?