// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows.Declarative.Events;
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Workflows.Declarative;
///
/// Defines contract used by declarative workflow actions to invoke and manipulate agents and conversations.
///
///
/// The shape of this provider contract is very much opinionated around patterns that exist in the Open AI Responses API.
/// In addition to direct usage of the Responses API, Foundry V2 agents are supported as they are fundamentally based on
/// the Open AI Responses API. Using other or patterns that are not
/// based on the Response API is currently not supported.
///
public abstract class ResponseAgentProvider
{
///
/// Gets or sets a collection of additional tools an agent is able to automatically invoke.
/// If an agent is configured with a function tool that is not available, a is executed
/// that provides an that describes the function calls requested. The caller may
/// then respond with a corrsponding that includes the results of the function calls.
///
///
/// These will not impact the requests sent to the model by the .
///
public IEnumerable? Functions { get; init; }
///
/// Gets or sets a value indicating whether to allow concurrent invocation of functions.
///
///
/// if multiple function calls can execute in parallel.
/// if function calls are processed serially.
/// The default value is .
///
///
/// An individual response from the inner client might contain multiple function call requests.
/// By default, such function calls are processed serially. Set to
/// to enable concurrent invocation such that multiple function calls can execute in parallel.
///
public bool AllowConcurrentInvocation { get; init; }
///
/// Gets or sets a flag to indicate whether a single response is allowed to include multiple tool calls.
/// If , the is asked to return a maximum of one tool call per request.
/// If , there is no limit.
/// If , the provider may select its own default.
///
///
///
/// When used with function calling middleware, this does not affect the ability to perform multiple function calls in sequence.
/// It only affects the number of function calls within a single iteration of the function calling loop.
///
///
/// The underlying provider is not guaranteed to support or honor this flag. For example it may choose to ignore it and return multiple tool calls regardless.
///
///
public bool AllowMultipleToolCalls { get; init; }
///
/// Asynchronously creates a new conversation and returns its unique identifier.
///
/// The to monitor for cancellation requests. The default is .
/// The conversation identifier
public abstract Task CreateConversationAsync(CancellationToken cancellationToken = default);
///
/// Creates a new message in the specified conversation.
///
/// The identifier of the target conversation.
/// The message being added.
/// The to monitor for cancellation requests. The default is .
public abstract Task CreateMessageAsync(string conversationId, ChatMessage conversationMessage, CancellationToken cancellationToken = default);
///
/// Retrieves a specific message from a conversation.
///
/// The identifier of the target conversation.
/// The identifier of the target message.
/// The to monitor for cancellation requests. The default is .
/// The requested message
public abstract Task GetMessageAsync(string conversationId, string messageId, CancellationToken cancellationToken = default);
///
/// Asynchronously retrieves an AI agent by its unique identifier.
///
/// The unique identifier of the AI agent to retrieve. Cannot be null or empty.
/// An optional agent version.
/// Optional identifier of the target conversation.
/// The messages to include in the invocation.
/// Optional input arguments for agents that provide support.
/// A token that propagates notification when operation should be canceled.
/// Asynchronous set of .
public abstract IAsyncEnumerable InvokeAgentAsync(
string agentId,
string? agentVersion,
string? conversationId,
IEnumerable? messages,
IDictionary? inputArguments,
CancellationToken cancellationToken = default);
///
/// Retrieves a set of messages from a conversation.
///
/// The identifier of the target conversation.
/// A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.
/// A cursor for use in pagination. after is an object ID that defines your place in the list.
/// A cursor for use in pagination. before is an object ID that defines your place in the list.
/// Provide records in descending order when true.
/// The to monitor for cancellation requests. The default is .
/// The requested messages
public abstract IAsyncEnumerable GetMessagesAsync(
string conversationId,
int? limit = null,
string? after = null,
string? before = null,
bool newestFirst = false,
CancellationToken cancellationToken = default);
///
/// Utility method to convert a dictionary of input arguments to a JsonNode.
///
/// The dictionary of input arguments.
/// A JsonNode representing the input arguments.
protected static JsonNode ConvertDictionaryToJson(IDictionary inputArguments)
{
return inputArguments.ToFormula().ToJson();
}
}