|
1 | 1 | using Microsoft.Extensions.AI; |
2 | 2 | using ModelContextProtocol.Client; |
3 | 3 | using ModelContextProtocol.Protocol; |
4 | | -#if !NET |
5 | | -using System.Runtime.InteropServices; |
6 | | -#endif |
| 4 | +using System.Text; |
7 | 5 | using System.Text.Json; |
8 | 6 | using System.Text.Json.Nodes; |
9 | 7 |
|
@@ -324,7 +322,7 @@ public static AIContent ToAIContent(this ResourceContents content) |
324 | 322 |
|
325 | 323 | AIContent ac = content switch |
326 | 324 | { |
327 | | - BlobResourceContents blobResource => new DataContent(blobResource.Data, blobResource.MimeType ?? "application/octet-stream"), |
| 325 | + BlobResourceContents blobResource => new DataContent(blobResource.DecodedData, blobResource.MimeType ?? "application/octet-stream"), |
328 | 326 | TextResourceContents textResource => new TextContent(textResource.Text), |
329 | 327 | _ => throw new NotSupportedException($"Resource type '{content.GetType().Name}' is not supported.") |
330 | 328 | }; |
@@ -449,20 +447,12 @@ public static ContentBlock ToContentBlock(this AIContent content, JsonSerializer |
449 | 447 |
|
450 | 448 | return contentBlock; |
451 | 449 |
|
452 | | - unsafe byte[] GetUtf8Bytes(ReadOnlySpan<char> utf16) |
| 450 | + static byte[] GetUtf8Bytes(ReadOnlySpan<char> utf16) |
453 | 451 | { |
454 | | - // gets UTF-8 bytes from UTF-16 chars without intermediate string allocations |
455 | | - fixed (char* pChars = utf16) |
456 | | - { |
457 | | - var byteCount = System.Text.Encoding.UTF8.GetByteCount(pChars, utf16.Length); |
458 | | - var bytes = new byte[byteCount]; |
459 | | - |
460 | | - fixed (byte* pBytes = bytes) |
461 | | - { |
462 | | - System.Text.Encoding.UTF8.GetBytes(pChars, utf16.Length, pBytes, byteCount); |
463 | | - } |
464 | | - return bytes; |
465 | | - } |
| 452 | + // Get UTF-8 bytes from UTF-16 chars without intermediate string allocations |
| 453 | + byte[] bytes = new byte[Encoding.UTF8.GetByteCount(utf16)]; |
| 454 | + Encoding.UTF8.GetBytes(utf16, bytes); |
| 455 | + return bytes; |
466 | 456 | } |
467 | 457 | } |
468 | 458 |
|
|
0 commit comments