Use ReadOnlyMemory<byte> for binary data to eliminate UTF-16 transcoding (#1070)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com> Co-authored-by: Eric StJohn <ericstj@microsoft.com>
This commit is contained in:
parent
a3ce9e9606
commit
80b1ad21d7
1 changed files with 400 additions and 103 deletions
|
|
@ -1,9 +1,7 @@
|
|||
using Microsoft.Extensions.AI;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol;
|
||||
#if !NET
|
||||
using System.Runtime.InteropServices;
|
||||
#endif
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
|
|
@ -281,9 +279,9 @@ public static class AIContentExtensions
|
|||
{
|
||||
TextContentBlock textContent => new TextContent(textContent.Text),
|
||||
|
||||
ImageContentBlock imageContent => new DataContent(Convert.FromBase64String(imageContent.Data), imageContent.MimeType),
|
||||
ImageContentBlock imageContent => new DataContent(imageContent.DecodedData, imageContent.MimeType),
|
||||
|
||||
AudioContentBlock audioContent => new DataContent(Convert.FromBase64String(audioContent.Data), audioContent.MimeType),
|
||||
AudioContentBlock audioContent => new DataContent(audioContent.DecodedData, audioContent.MimeType),
|
||||
|
||||
EmbeddedResourceBlock resourceContent => resourceContent.Resource.ToAIContent(),
|
||||
|
||||
|
|
@ -324,7 +322,7 @@ public static class AIContentExtensions
|
|||
|
||||
AIContent ac = content switch
|
||||
{
|
||||
BlobResourceContents blobResource => new DataContent(Convert.FromBase64String(blobResource.Blob), blobResource.MimeType ?? "application/octet-stream"),
|
||||
BlobResourceContents blobResource => new DataContent(blobResource.DecodedData, blobResource.MimeType ?? "application/octet-stream"),
|
||||
TextResourceContents textResource => new TextContent(textResource.Text),
|
||||
_ => throw new NotSupportedException($"Resource type '{content.GetType().Name}' is not supported.")
|
||||
};
|
||||
|
|
@ -401,13 +399,13 @@ public static class AIContentExtensions
|
|||
|
||||
DataContent dataContent when dataContent.HasTopLevelMediaType("image") => new ImageContentBlock
|
||||
{
|
||||
Data = dataContent.Base64Data.ToString(),
|
||||
Data = EncodingUtilities.GetUtf8Bytes(dataContent.Base64Data.Span),
|
||||
MimeType = dataContent.MediaType,
|
||||
},
|
||||
|
||||
DataContent dataContent when dataContent.HasTopLevelMediaType("audio") => new AudioContentBlock
|
||||
{
|
||||
Data = dataContent.Base64Data.ToString(),
|
||||
Data = EncodingUtilities.GetUtf8Bytes(dataContent.Base64Data.Span),
|
||||
MimeType = dataContent.MediaType,
|
||||
},
|
||||
|
||||
|
|
@ -415,7 +413,7 @@ public static class AIContentExtensions
|
|||
{
|
||||
Resource = new BlobResourceContents
|
||||
{
|
||||
Blob = dataContent.Base64Data.ToString(),
|
||||
Blob = EncodingUtilities.GetUtf8Bytes(dataContent.Base64Data.Span),
|
||||
MimeType = dataContent.MediaType,
|
||||
Uri = string.Empty,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue