Skip to content

.Net: Remove obsoleted code for agent abstractions #12391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Agents;
/// <summary>
/// Demonstrate service selection for <see cref="ChatCompletionAgent"/> through setting service-id
/// on <see cref="Agent.Arguments"/> and also providing override <see cref="KernelArguments"/>
/// when calling <see cref="ChatCompletionAgent.InvokeAsync(ChatHistory, KernelArguments?, Kernel?, CancellationToken)"/>
/// when calling <see cref="ChatCompletionAgent.InvokeAsync(ICollection{ChatMessageContent}, AgentThread?, AgentInvokeOptions?, CancellationToken)"/>
/// </summary>
public class ChatCompletion_ServiceSelection(ITestOutputHelper output) : BaseAgentsTest(output)
{
Expand Down
166 changes: 14 additions & 152 deletions dotnet/src/Agents/AzureAI/AzureAIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,54 +96,6 @@ public AzureAIAgent(
/// </summary>
public PersistentAgentsClient Client { get; }

/// <summary>
/// Adds a message to the specified thread.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="message">A non-system message to append to the conversation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <remarks>
/// Only supports messages with <see href="https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-additional_messages">role = User or agent</see>.
/// </remarks>
[Obsolete("Pass messages directly to Invoke instead. This method will be removed after May 1st 2025.")]
public Task AddChatMessageAsync(string threadId, ChatMessageContent message, CancellationToken cancellationToken = default)
{
return AgentThreadActions.CreateMessageAsync(this.Client, threadId, message, cancellationToken);
}

/// <summary>
/// Gets messages for a specified thread.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An asynchronous enumeration of messages.</returns>
[Obsolete("Use the AzureAIAgentThread to retrieve messages instead. This method will be removed after May 1st 2025.")]
public IAsyncEnumerable<ChatMessageContent> GetThreadMessagesAsync(string threadId, CancellationToken cancellationToken = default)
{
return AgentThreadActions.GetMessagesAsync(this.Client, threadId, null, cancellationToken);
}

/// <summary>
/// Invokes the assistant on the specified thread.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An asynchronous enumeration of response messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
[Obsolete("Use InvokeAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
string threadId,
KernelArguments? arguments = null,
Kernel? kernel = null,
CancellationToken cancellationToken = default)
{
return this.InvokeAsync(threadId, options: null, arguments, kernel, cancellationToken);
}

/// <inheritdoc/>
public override IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
ICollection<ChatMessageContent> messages,
Expand Down Expand Up @@ -236,44 +188,6 @@ async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync()
}
}

/// <summary>
/// Invokes the assistant on the specified thread.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="options">Optional invocation options.</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An asynchronous enumeration of response messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
[Obsolete("Use InvokeAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
string threadId,
AzureAIInvocationOptions? options,
KernelArguments? arguments = null,
Kernel? kernel = null,
CancellationToken cancellationToken = default)
{
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
() => InternalInvokeAsync(),
cancellationToken);

async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync()
{
kernel ??= this.Kernel;
await foreach ((bool isVisible, ChatMessageContent message) in AgentThreadActions.InvokeAsync(this, this.Client, threadId, options, this.Logger, kernel, arguments, cancellationToken).ConfigureAwait(false))
{
if (isVisible)
{
yield return message;
}
}
}
}

/// <inheritdoc/>
public override IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
ICollection<ChatMessageContent> messages,
Expand Down Expand Up @@ -328,17 +242,22 @@ public async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> In
new AzureAIAgentInvokeOptions() { AdditionalInstructions = mergedAdditionalInstructions } :
new AzureAIAgentInvokeOptions(options) { AdditionalInstructions = mergedAdditionalInstructions };

#pragma warning disable CS0618 // Type or member is obsolete
// Invoke the Agent with the thread that we already added our message to.
// Invoke the Agent with the thread that we already added our message to, and with
// a chat history to receive complete messages.
var newMessagesReceiver = new ChatHistory();
var invokeResults = this.InvokeStreamingAsync(
azureAIAgentThread.Id!,
extensionsContextOptions.ToAzureAIInvocationOptions(),
options?.KernelArguments,
kernel,
newMessagesReceiver,
var invokeResults = ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
() => AgentThreadActions.InvokeStreamingAsync(
this,
this.Client,
azureAIAgentThread.Id!,
newMessagesReceiver,
extensionsContextOptions.ToAzureAIInvocationOptions(),
this.Logger,
kernel,
options?.KernelArguments,
cancellationToken),
cancellationToken);
#pragma warning restore CS0618 // Type or member is obsolete

// Return the chunks to the caller.
await foreach (var result in invokeResults.ConfigureAwait(false))
Expand All @@ -358,63 +277,6 @@ public async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> In
}
}

/// <summary>
/// Invokes the assistant on the specified thread with streaming response.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="messages">Optional receiver of the completed messages that are generated.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An asynchronous enumeration of messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
[Obsolete("Use InvokeStreamingAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
string threadId,
KernelArguments? arguments = null,
Kernel? kernel = null,
ChatHistory? messages = null,
CancellationToken cancellationToken = default)
{
return this.InvokeStreamingAsync(threadId, options: null, arguments, kernel, messages, cancellationToken);
}

/// <summary>
/// Invokes the assistant on the specified thread with streaming response.
/// </summary>
/// <param name="threadId">The thread identifier.</param>
/// <param name="options">Optional invocation options.</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="messages">Optional receiver of the completed messages that are generated.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An asynchronous enumeration of messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
[Obsolete("Use InvokeStreamingAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
string threadId,
AzureAIInvocationOptions? options,
KernelArguments? arguments = null,
Kernel? kernel = null,
ChatHistory? messages = null,
CancellationToken cancellationToken = default)
{
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
() => InternalInvokeStreamingAsync(),
cancellationToken);

IAsyncEnumerable<StreamingChatMessageContent> InternalInvokeStreamingAsync()
{
kernel ??= this.Kernel;
return AgentThreadActions.InvokeStreamingAsync(this, this.Client, threadId, messages, options, this.Logger, kernel, arguments, cancellationToken);
}
}

/// <inheritdoc/>
protected override IEnumerable<string> GetChannelKeys()
{
Expand Down
Loading
Loading