Overhaul of Resources (#125)
* Supporting resource templates with read resource callback Fixes #123 * Aligning the resource data structures with the 2024-11-05 schema - Introducing separate classes for Text and Blob ResourceContents - Custom JSON converter to allow those types to be returned, making it easier to follow the type system against the spec - Updated tests * Apply suggestions from code review Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: Peder Holdgaard Pedersen <127606677+PederHP@users.noreply.github.com> * Putting files back where they belong * Adding an internal constructor to prevent overloads * Addressing feedback * Apply suggestions from code review Co-authored-by: Stephen Toub <stoub@microsoft.com> * Update src/ModelContextProtocol/Protocol/Types/ResourceContents.cs * Addressing feedback --------- Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: Peder Holdgaard Pedersen <127606677+PederHP@users.noreply.github.com>
This commit is contained in:
parent
2ab9db0878
commit
3c0679e92b
1 changed files with 290 additions and 49 deletions
|
|
@ -39,11 +39,7 @@ public static class AIContentExtensions
|
|||
}
|
||||
else if (content is { Type: "resource" } && content.Resource is { } resourceContents)
|
||||
{
|
||||
ac = resourceContents.Blob is not null && resourceContents.MimeType is not null ?
|
||||
new DataContent(Convert.FromBase64String(resourceContents.Blob), resourceContents.MimeType) :
|
||||
new TextContent(resourceContents.Text);
|
||||
|
||||
(ac.AdditionalProperties ??= [])["uri"] = resourceContents.Uri;
|
||||
ac = resourceContents.ToAIContent();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -62,9 +58,12 @@ public static class AIContentExtensions
|
|||
{
|
||||
Throw.IfNull(content);
|
||||
|
||||
AIContent ac = content.Blob is not null && content.MimeType is not null ?
|
||||
new DataContent(Convert.FromBase64String(content.Blob), content.MimeType) :
|
||||
new TextContent(content.Text);
|
||||
AIContent ac = content switch
|
||||
{
|
||||
BlobResourceContents blobResource => new DataContent(Convert.FromBase64String(blobResource.Blob), blobResource.MimeType ?? "application/octet-stream"),
|
||||
TextResourceContents textResource => new TextContent(textResource.Text),
|
||||
_ => throw new NotSupportedException($"Resource type '{content.GetType().Name}' is not supported.")
|
||||
};
|
||||
|
||||
(ac.AdditionalProperties ??= [])["uri"] = content.Uri;
|
||||
ac.RawRepresentation = content;
|
||||
|
|
@ -79,7 +78,7 @@ public static class AIContentExtensions
|
|||
{
|
||||
Throw.IfNull(contents);
|
||||
|
||||
return contents.Select(ToAIContent).ToList();
|
||||
return [.. contents.Select(ToAIContent)];
|
||||
}
|
||||
|
||||
/// <summary>Creates a list of <see cref="AIContent"/> from a sequence of <see cref="ResourceContents"/>.</summary>
|
||||
|
|
@ -89,7 +88,7 @@ public static class AIContentExtensions
|
|||
{
|
||||
Throw.IfNull(contents);
|
||||
|
||||
return contents.Select(ToAIContent).ToList();
|
||||
return [.. contents.Select(ToAIContent)];
|
||||
}
|
||||
|
||||
/// <summary>Extracts the data from a <see cref="DataContent"/> as a Base64 string.</summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue