From a89c15d6e64f5d7719aa5d353e95143a21ec382b Mon Sep 17 00:00:00 2001
From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Date: Tue, 25 Nov 2025 09:59:43 +0000
Subject: [PATCH] .NET: Change type of props representing continuation token
(#2430)
* change type of props representing continuation token
* use explict cast instead of the as operator
---
.../Program.cs | 8 ++++----
.../Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs | 2 +-
.../Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs | 2 +-
.../AgentRunResponseExtensions.cs | 4 ++--
.../AgentRunResponseUpdate.cs | 2 +-
.../src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs | 2 +-
.../AgentRunOptionsTests.cs | 2 +-
.../Sample/10_Sequential_HostAsAgent.cs | 2 +-
.../Sample/11_Concurrent_HostAsAgent.cs | 2 +-
.../Sample/12_HandOff_HostAsAgent.cs | 2 +-
10 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs
index f2a3bdf5c0..83a97d76c5 100644
--- a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs
+++ b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs
@@ -44,7 +44,7 @@ while (response.ContinuationToken is not null)
await Task.Delay(TimeSpan.FromSeconds(10));
- RestoreAgentState(agent, out thread, out object? continuationToken);
+ RestoreAgentState(agent, out thread, out ResponseContinuationToken? continuationToken);
options.ContinuationToken = continuationToken;
response = await agent.RunAsync(thread, options);
@@ -52,19 +52,19 @@ while (response.ContinuationToken is not null)
Console.WriteLine(response.Text);
-void PersistAgentState(AgentThread thread, object? continuationToken)
+void PersistAgentState(AgentThread thread, ResponseContinuationToken? continuationToken)
{
stateStore["thread"] = thread.Serialize();
stateStore["continuationToken"] = JsonSerializer.SerializeToElement(continuationToken, AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken)));
}
-void RestoreAgentState(AIAgent agent, out AgentThread thread, out object? continuationToken)
+void RestoreAgentState(AIAgent agent, out AgentThread thread, out ResponseContinuationToken? continuationToken)
{
JsonElement serializedThread = stateStore["thread"] ?? throw new InvalidOperationException("No serialized thread found in state store.");
JsonElement? serializedToken = stateStore["continuationToken"];
thread = agent.DeserializeThread(serializedThread);
- continuationToken = serializedToken?.Deserialize(AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken)));
+ continuationToken = (ResponseContinuationToken?)serializedToken?.Deserialize(AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken)));
}
[Description("Researches relevant space facts and scientific information for writing a science fiction novel")]
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs
index 7262979207..9cd6d51680 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs
@@ -49,7 +49,7 @@ public class AgentRunOptions
/// can be polled for completion by obtaining the token from the property
/// and passing it via this property on subsequent calls to .
///
- public object? ContinuationToken { get; set; }
+ public ResponseContinuationToken? ContinuationToken { get; set; }
///
/// Gets or sets a value indicating whether the background responses are allowed.
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs
index faa1ce1743..001cfd9469 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs
@@ -175,7 +175,7 @@ public class AgentRunResponse
/// to poll for completion.
///
///
- public object? ContinuationToken { get; set; }
+ public ResponseContinuationToken? ContinuationToken { get; set; }
///
/// Gets or sets the timestamp indicating when this response was created.
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseExtensions.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseExtensions.cs
index 4ff07cac56..cb3ad7ec74 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseExtensions.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseExtensions.cs
@@ -42,7 +42,7 @@ public static class AgentRunResponseExtensions
RawRepresentation = response,
ResponseId = response.ResponseId,
Usage = response.Usage,
- ContinuationToken = response.ContinuationToken as ResponseContinuationToken,
+ ContinuationToken = response.ContinuationToken,
};
}
@@ -75,7 +75,7 @@ public static class AgentRunResponseExtensions
RawRepresentation = responseUpdate,
ResponseId = responseUpdate.ResponseId,
Role = responseUpdate.Role,
- ContinuationToken = responseUpdate.ContinuationToken as ResponseContinuationToken,
+ ContinuationToken = responseUpdate.ContinuationToken,
};
}
diff --git a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUpdate.cs b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUpdate.cs
index 0e5277d453..ccf3deae54 100644
--- a/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUpdate.cs
+++ b/dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUpdate.cs
@@ -159,7 +159,7 @@ public class AgentRunResponseUpdate
/// to resume streaming from the point of interruption.
///
///
- public object? ContinuationToken { get; set; }
+ public ResponseContinuationToken? ContinuationToken { get; set; }
///
public override string ToString() => this.Text;
diff --git a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
index bbe1b28352..d04d9bb9fb 100644
--- a/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
+++ b/dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
@@ -551,7 +551,7 @@ public sealed partial class ChatClientAgent : AIAgent
{
chatOptions ??= new ChatOptions();
chatOptions.AllowBackgroundResponses = agentRunOptions.AllowBackgroundResponses;
- chatOptions.ContinuationToken = agentRunOptions.ContinuationToken as ResponseContinuationToken;
+ chatOptions.ContinuationToken = agentRunOptions.ContinuationToken;
}
return chatOptions;
diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunOptionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunOptionsTests.cs
index 32560949fb..7460ea4623 100644
--- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunOptionsTests.cs
+++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunOptionsTests.cs
@@ -17,7 +17,7 @@ public class AgentRunOptionsTests
// Arrange
var options = new AgentRunOptions
{
- ContinuationToken = new object(),
+ ContinuationToken = ResponseContinuationToken.FromBytes(new byte[] { 1, 2, 3 }),
AllowBackgroundResponses = true,
AdditionalProperties = new AdditionalPropertiesDictionary
{
diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/10_Sequential_HostAsAgent.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/10_Sequential_HostAsAgent.cs
index 4670f8b931..fc23d44155 100644
--- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/10_Sequential_HostAsAgent.cs
+++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/10_Sequential_HostAsAgent.cs
@@ -25,7 +25,7 @@ internal static class Step10EntryPoint
foreach (string input in inputs)
{
AgentRunResponse response;
- object? continuationToken = null;
+ ResponseContinuationToken? continuationToken = null;
do
{
response = await hostAgent.RunAsync(input, thread, new AgentRunOptions { ContinuationToken = continuationToken });
diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/11_Concurrent_HostAsAgent.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/11_Concurrent_HostAsAgent.cs
index ca2ba46405..d47b90223c 100644
--- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/11_Concurrent_HostAsAgent.cs
+++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/11_Concurrent_HostAsAgent.cs
@@ -37,7 +37,7 @@ internal static class Step11EntryPoint
foreach (string input in inputs)
{
AgentRunResponse response;
- object? continuationToken = null;
+ ResponseContinuationToken? continuationToken = null;
do
{
response = await hostAgent.RunAsync(input, thread, new AgentRunOptions { ContinuationToken = continuationToken });
diff --git a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/12_HandOff_HostAsAgent.cs b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/12_HandOff_HostAsAgent.cs
index 8eb553b868..824a75d5d0 100644
--- a/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/12_HandOff_HostAsAgent.cs
+++ b/dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/12_HandOff_HostAsAgent.cs
@@ -73,7 +73,7 @@ internal static class Step12EntryPoint
foreach (string input in inputs)
{
AgentRunResponse response;
- object? continuationToken = null;
+ ResponseContinuationToken? continuationToken = null;
do
{
response = await hostAgent.RunAsync(input, thread, new AgentRunOptions { ContinuationToken = continuationToken });