// Copyright (c) Microsoft. All rights reserved. using System; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; using Microsoft.Shared.DiagnosticIds; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI; /// /// A skill script backed by a delegate. /// [Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)] internal sealed class AgentInlineSkillScript : AgentSkillScript { private readonly AIFunction _function; /// /// Initializes a new instance of the class from a delegate. /// The delegate's parameters and return type are automatically marshaled via . /// /// The script name. /// A method to execute when the script is invoked. Parameters are automatically deserialized from JSON. /// An optional description of the script. public AgentInlineSkillScript(string name, Delegate method, string? description = null) : base(Throw.IfNullOrWhitespace(name), description) { Throw.IfNull(method); this._function = AIFunctionFactory.Create(method, name: this.Name); } /// /// Gets the JSON schema describing the parameters accepted by this script, or if not available. /// public override JsonElement? ParametersSchema => this._function.JsonSchema; /// public override async Task RunAsync(AgentSkill skill, AIFunctionArguments arguments, CancellationToken cancellationToken = default) { return await this._function.InvokeAsync(arguments, cancellationToken).ConfigureAwait(false); } }