// Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; using System.Text.Json; using Microsoft.Agents.AI; namespace Microsoft.Extensions.AI; /// /// Extension methods for AdditionalPropertiesDictionary. /// internal static class AdditionalPropertiesDictionaryExtensions { /// /// Converts an to a dictionary of values suitable for A2A metadata. /// /// /// This method can be replaced by the one from A2A SDK once it is available. /// /// The additional properties dictionary to convert, or null. /// A dictionary of JSON elements representing the metadata, or null if the input is null or empty. internal static Dictionary? ToA2AMetadata(this AdditionalPropertiesDictionary? additionalProperties) { if (additionalProperties is not { Count: > 0 }) { return null; } var metadata = new Dictionary(); foreach (var kvp in additionalProperties) { if (kvp.Value is JsonElement) { metadata[kvp.Key] = (JsonElement)kvp.Value!; continue; } metadata[kvp.Key] = JsonSerializer.SerializeToElement(kvp.Value, A2AJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))); } return metadata; } }