Skip to content

Conversation

@SergeyMenshykh
Copy link
Member

This PR updates AF model to use model for background responses (long-running operations) from MEAI and updates ChatClientAgent to support it.

Background responses use a continuation token returned by the RunAsync and RunStreamingAsync methods in the AgentRunResponse and AgentRunResponseUpdate classes, respectively, when background responses are enabled and supported by the AI agent.

The continuation token contains all necessary details to enable polling for a background response using the non-streaming RunAsync method. It also allows resuming a streamed background response with the RunStreamingAsync method if the stream is interrupted. In both cases, the continuation token obtained from the initial agent response or the streamed update received before the interruption should be supplied as input for follow-up calls.

When a background response has completed, failed, or cannot proceed further (for example, when user input is required), the continuation token returned by either method will be null, signaling to consumers that processing is complete and there is nothing to poll or resume. The result returned by either method can then be used for further processing.

Non-streaming API:

AgentRunOptions options = new()
{
    AllowBackgroundResponses = true
};

// Get initial response with continuation token
AgentRunResponse response = await agent.RunAsync("What's the biggest animal?", options: options);

// Continue to poll until the final response is received
while (response.ContinuationToken is not null)
{
    options.ContinuationToken = response.ContinuationToken;
    response = await agent.RunAsync([], options: options);
}

Console.WriteLine(response.Text);

Streaming API:

AgentRunOptions options = new()
{
    AllowBackgroundResponses = true
};

AgentRunResponseUpdate? lastReceivedUpdate = null;

await foreach (var update in agent.RunStreamingAsync("What is the capital of France?", options: options))
{
    Console.Write(update.Text);
  
    lastReceivedUpdate = update;
  
   // Simulate an interruption
    break;
}

// Resume from interruption point captured by the continuation token
options.ContinuationToken = lastReceivedUpdate?.ContinuationToken;
await foreach (var update in agent.RunStreamingAsync([], options: options))
{
    Console.Write(update.Text);
}

@SergeyMenshykh SergeyMenshykh self-assigned this Oct 15, 2025
@Copilot Copilot AI review requested due to automatic review settings October 15, 2025 19:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds background (long‑running) response support with continuation tokens across agent and chat client layers.

  • Introduces AllowBackgroundResponses and ContinuationToken plumbing in AgentRunOptions, AgentRunResponse, AgentRunResponseUpdate, and ChatClientAgent merge logic.
  • Adds comprehensive unit tests plus a new sample (Step17) and ADR documenting long‑running operations.
  • Extends serialization to include ContinuationToken and updates solution / docs.

Reviewed Changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
ChatClientAgentTests.cs Adds tests validating propagation and precedence of background response options and continuation tokens.
AgentRunResponseUpdateTests.cs Adds tests for new ContinuationToken property and JSON round‑trip.
AgentRunResponseTests.cs Adds ContinuationToken handling tests and serialization updates.
AgentRunOptionsTests.cs Tests cloning and JSON serialization including new properties.
ChatClientAgent.cs Merges chat options now applying background response properties, introduces helper.
AgentRunResponseUpdate.cs Adds ContinuationToken property with documentation remark.
AgentRunResponseExtensions.cs Propagates ContinuationToken when converting chat responses to agent responses.
AgentRunResponse.cs Adds ContinuationToken property and remarks.
AgentRunOptions.cs Adds AllowBackgroundResponses and ContinuationToken with extensive remarks.
Agents README / Sample Step17 New sample demonstrating polling and streaming resumption using continuation tokens.
Solution (.slnx) Registers new sample and ADR document.
ADR 0009 Design record for long‑running operations (background responses).

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Oct 22, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 22, 2025
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Oct 22, 2025
@SergeyMenshykh SergeyMenshykh removed this pull request from the merge queue due to a manual request Oct 22, 2025
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Oct 22, 2025
Merged via the queue into microsoft:main with commit 1bf520a Oct 22, 2025
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants