From cd2c1c03da91a5f2a76b2999d0d0f15b8647e0b7 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh Date: Mon, 24 Nov 2025 17:06:41 +0000 Subject: [PATCH 1/2] change type of props representing continuation token --- .../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..c2e4a68a28 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 @@ 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 @@ 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 = serializedToken?.Deserialize(AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken))) as 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 AgentRunOptions(AgentRunOptions options) /// 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 IList Messages /// 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 @@ response.RawRepresentation as ChatResponse ?? RawRepresentation = response, ResponseId = response.ResponseId, Usage = response.Usage, - ContinuationToken = response.ContinuationToken as ResponseContinuationToken, + ContinuationToken = response.ContinuationToken, }; } @@ -75,7 +75,7 @@ responseUpdate.RawRepresentation as ChatResponseUpdate ?? 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 IList Contents /// 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 @@ await thread.AIContextProvider.InvokedAsync(new(inputMessages, aiContextProvider { 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 void CloningConstructorCopiesProperties() // 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 @@ public static async ValueTask RunAsync(TextWriter writer, IWorkflowExecutionEnvi 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 @@ public static async ValueTask RunAsync(TextWriter writer, IWorkflowExecutionEnvi 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 @@ public static async ValueTask RunAsync(TextWriter writer, IWorkflowExecutionEnvi foreach (string input in inputs) { AgentRunResponse response; - object? continuationToken = null; + ResponseContinuationToken? continuationToken = null; do { response = await hostAgent.RunAsync(input, thread, new AgentRunOptions { ContinuationToken = continuationToken }); From ad201c85aa1f13576db545e899641bce4663edb0 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh Date: Mon, 24 Nov 2025 23:10:35 +0000 Subject: [PATCH 2/2] use explict cast instead of the as operator --- .../Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs index c2e4a68a28..83a97d76c5 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs @@ -64,7 +64,7 @@ void RestoreAgentState(AIAgent agent, out AgentThread thread, out ResponseContin JsonElement? serializedToken = stateStore["continuationToken"]; thread = agent.DeserializeThread(serializedThread); - continuationToken = serializedToken?.Deserialize(AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken))) as ResponseContinuationToken; + continuationToken = (ResponseContinuationToken?)serializedToken?.Deserialize(AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ResponseContinuationToken))); } [Description("Researches relevant space facts and scientific information for writing a science fiction novel")]