|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | + |
| 8 | +#pragma warning disable SA1111 // Closing parenthesis should be on line of last parameter |
| 9 | +#pragma warning disable SA1112 // Closing parenthesis should be on line of opening parenthesis |
| 10 | +#pragma warning disable SA1114 // Parameter list should follow declaration |
| 11 | +#pragma warning disable CA1710 // Identifiers should have correct suffix |
| 12 | + |
| 13 | +namespace Microsoft.Extensions.AI; |
| 14 | + |
| 15 | +/// <summary>Represents arguments to be used with <see cref="AIFunction.InvokeAsync"/>.</summary> |
| 16 | +/// <remarks> |
| 17 | +/// <see cref="AIFunctionArguments"/> is a dictionary of name/value pairs that are used |
| 18 | +/// as inputs to an <see cref="AIFunction"/>. However, an instance carries additional non-nominal |
| 19 | +/// information, such as an optional <see cref="IServiceProvider"/> that can be used by |
| 20 | +/// an <see cref="AIFunction"/> if it needs to resolve any services from a dependency injection |
| 21 | +/// container. |
| 22 | +/// </remarks> |
| 23 | +public sealed class AIFunctionArguments : IDictionary<string, object?>, IReadOnlyDictionary<string, object?> |
| 24 | +{ |
| 25 | + /// <summary>The nominal arguments.</summary> |
| 26 | + private readonly Dictionary<string, object?> _arguments; |
| 27 | + |
| 28 | + /// <summary>Initializes a new instance of the <see cref="AIFunctionArguments"/> class.</summary> |
| 29 | + public AIFunctionArguments() |
| 30 | + { |
| 31 | + _arguments = []; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="AIFunctionArguments"/> class containing |
| 36 | + /// the specified <paramref name="arguments"/>. |
| 37 | + /// </summary> |
| 38 | + /// <param name="arguments">The arguments represented by this instance.</param> |
| 39 | + /// <remarks> |
| 40 | + /// The <paramref name="arguments"/> reference will be stored if the instance is |
| 41 | + /// already a <see cref="Dictionary{TKey, TValue}"/>, in which case all dictionary |
| 42 | + /// operations on this instance will be routed directly to that instance. If <paramref name="arguments"/> |
| 43 | + /// is not a dictionary, a shallow clone of its data will be used to populate this |
| 44 | + /// instance. A <see langword="null"/> <paramref name="arguments"/> is treated as an |
| 45 | + /// empty dictionary. |
| 46 | + /// </remarks> |
| 47 | + public AIFunctionArguments(IDictionary<string, object?>? arguments) |
| 48 | + { |
| 49 | + _arguments = |
| 50 | + arguments is null ? [] : |
| 51 | + arguments as Dictionary<string, object?> ?? |
| 52 | + new Dictionary<string, object?>(arguments); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary>Gets or sets services optionally associated with these arguments.</summary> |
| 56 | + public IServiceProvider? Services { get; set; } |
| 57 | + |
| 58 | + /// <inheritdoc /> |
| 59 | + public object? this[string key] |
| 60 | + { |
| 61 | + get => _arguments[key]; |
| 62 | + set => _arguments[key] = value; |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc /> |
| 66 | + public ICollection<string> Keys => _arguments.Keys; |
| 67 | + |
| 68 | + /// <inheritdoc /> |
| 69 | + public ICollection<object?> Values => _arguments.Values; |
| 70 | + |
| 71 | + /// <inheritdoc /> |
| 72 | + public int Count => _arguments.Count; |
| 73 | + |
| 74 | + /// <inheritdoc /> |
| 75 | + bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => false; |
| 76 | + |
| 77 | + /// <inheritdoc /> |
| 78 | + IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys => Keys; |
| 79 | + |
| 80 | + /// <inheritdoc /> |
| 81 | + IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values => Values; |
| 82 | + |
| 83 | + /// <inheritdoc /> |
| 84 | + public void Add(string key, object? value) => _arguments.Add(key, value); |
| 85 | + |
| 86 | + /// <inheritdoc /> |
| 87 | + void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) => |
| 88 | + ((ICollection<KeyValuePair<string, object?>>)_arguments).Add(item); |
| 89 | + |
| 90 | + /// <inheritdoc /> |
| 91 | + public void Clear() => _arguments.Clear(); |
| 92 | + |
| 93 | + /// <inheritdoc /> |
| 94 | + bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => |
| 95 | + ((ICollection<KeyValuePair<string, object?>>)_arguments).Contains(item); |
| 96 | + |
| 97 | + /// <inheritdoc /> |
| 98 | + public bool ContainsKey(string key) => _arguments.ContainsKey(key); |
| 99 | + |
| 100 | + /// <inheritdoc /> |
| 101 | + public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => |
| 102 | + ((ICollection<KeyValuePair<string, object?>>)_arguments).CopyTo(array, arrayIndex); |
| 103 | + |
| 104 | + /// <inheritdoc /> |
| 105 | + public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _arguments.GetEnumerator(); |
| 106 | + |
| 107 | + /// <inheritdoc /> |
| 108 | + public bool Remove(string key) => _arguments.Remove(key); |
| 109 | + |
| 110 | + /// <inheritdoc /> |
| 111 | + bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) => |
| 112 | + ((ICollection<KeyValuePair<string, object?>>)_arguments).Remove(item); |
| 113 | + |
| 114 | + /// <inheritdoc /> |
| 115 | + public bool TryGetValue(string key, out object? value) => _arguments.TryGetValue(key, out value); |
| 116 | + |
| 117 | + /// <inheritdoc /> |
| 118 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 119 | +} |
0 commit comments