// Copyright (c) Microsoft. All rights reserved. using System.Text.Json.Nodes; using Microsoft.Agents.AI.DurableTask; using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata; namespace Microsoft.Agents.AI.Hosting.AzureFunctions; /// /// Provides factory methods for creating common instances /// used by function metadata transformers. /// internal static class FunctionMetadataFactory { /// /// Creates function metadata for an entity trigger function. /// /// The base name used to derive the entity function name. /// A configured for an entity trigger. internal static DefaultFunctionMetadata CreateEntityTrigger(string name) { return new DefaultFunctionMetadata() { Name = AgentSessionId.ToEntityName(name), Language = "dotnet-isolated", RawBindings = [ """{"name":"encodedEntityRequest","type":"entityTrigger","direction":"In"}""", """{"name":"client","type":"durableClient","direction":"In"}""" ], EntryPoint = BuiltInFunctions.RunAgentEntityFunctionEntryPoint, ScriptFile = BuiltInFunctions.ScriptFile, }; } /// /// Creates function metadata for an HTTP trigger function. /// /// The base name used to derive the HTTP function name. /// The HTTP route for the trigger. /// The entry point method for the HTTP trigger. /// The allowed HTTP methods as a JSON array fragment (e.g., "\"get\""). Defaults to POST. /// A configured for an HTTP trigger. internal static DefaultFunctionMetadata CreateHttpTrigger(string name, string route, string entryPoint, string methods = "\"post\"") { return new DefaultFunctionMetadata() { Name = $"{BuiltInFunctions.HttpPrefix}{name}", Language = "dotnet-isolated", RawBindings = [ $"{{\"name\":\"req\",\"type\":\"httpTrigger\",\"direction\":\"In\",\"authLevel\":\"function\",\"methods\": [{methods}],\"route\":\"{route}\"}}", "{\"name\":\"$return\",\"type\":\"http\",\"direction\":\"Out\"}", "{\"name\":\"client\",\"type\":\"durableClient\",\"direction\":\"In\"}" ], EntryPoint = entryPoint, ScriptFile = BuiltInFunctions.ScriptFile, }; } /// /// Creates function metadata for an activity trigger function. /// /// The name of the activity function. /// A configured for an activity trigger. internal static DefaultFunctionMetadata CreateActivityTrigger(string functionName) { return new DefaultFunctionMetadata() { Name = functionName, Language = "dotnet-isolated", RawBindings = [ """{"name":"input","type":"activityTrigger","direction":"In","dataType":"String"}""", """{"name":"durableTaskClient","type":"durableClient","direction":"In"}""" ], EntryPoint = BuiltInFunctions.InvokeWorkflowActivityFunctionEntryPoint, ScriptFile = BuiltInFunctions.ScriptFile, }; } /// /// Creates function metadata for an orchestration trigger function. /// /// The name of the orchestration function. /// The entry point method for the orchestration trigger. /// A configured for an orchestration trigger. internal static DefaultFunctionMetadata CreateOrchestrationTrigger(string functionName, string entryPoint) { return new DefaultFunctionMetadata() { Name = functionName, Language = "dotnet-isolated", RawBindings = [ """{"name":"context","type":"orchestrationTrigger","direction":"In"}""" ], EntryPoint = entryPoint, ScriptFile = BuiltInFunctions.ScriptFile, }; } /// /// Creates function metadata for an MCP tool trigger function that starts a workflow. /// /// The name of the workflow to expose as an MCP tool. /// An optional description for the MCP tool. If null, a default description is generated. /// A configured for an MCP tool trigger. internal static DefaultFunctionMetadata CreateWorkflowMcpToolTrigger( string workflowName, string? description) { var functionName = $"{BuiltInFunctions.McpToolPrefix}{workflowName}"; var toolDescription = description ?? $"Run the {workflowName} workflow"; var toolProperties = new JsonArray(new JsonObject { ["propertyName"] = "input", ["propertyType"] = "string", ["description"] = "The input to the workflow.", ["isRequired"] = true, ["isArray"] = false, }); var triggerBinding = new JsonObject { ["name"] = "context", ["type"] = "mcpToolTrigger", ["direction"] = "In", ["toolName"] = workflowName, ["description"] = toolDescription, ["toolProperties"] = toolProperties.ToJsonString(), }; var inputBinding = new JsonObject { ["name"] = "input", ["type"] = "mcpToolProperty", ["direction"] = "In", ["propertyName"] = "input", ["description"] = "The input to the workflow", ["isRequired"] = true, ["dataType"] = "String", ["propertyType"] = "string", }; var clientBinding = new JsonObject { ["name"] = "client", ["type"] = "durableClient", ["direction"] = "In", }; return new DefaultFunctionMetadata { Name = functionName, Language = "dotnet-isolated", RawBindings = [triggerBinding.ToJsonString(), inputBinding.ToJsonString(), clientBinding.ToJsonString()], EntryPoint = BuiltInFunctions.RunWorkflowMcpToolFunctionEntryPoint, ScriptFile = BuiltInFunctions.ScriptFile, }; } }