mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: [BREAKING] Rename session state json param (#3681)
* Rename session state json param to ensure consistency * Fix merge failures and PR comments.
This commit is contained in:
@@ -79,8 +79,8 @@ public sealed class A2AAgent : AIAgent
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new A2AAgentSession(serializedSession, jsonSerializerOptions));
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new A2AAgentSession(serializedState, jsonSerializerOptions));
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override async Task<AgentResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -143,18 +143,18 @@ public abstract class AIAgent
|
||||
/// <summary>
|
||||
/// Deserializes an agent session from its JSON serialized representation.
|
||||
/// </summary>
|
||||
/// <param name="serializedSession">A <see cref="JsonElement"/> containing the serialized session state.</param>
|
||||
/// <param name="serializedState">A <see cref="JsonElement"/> containing the serialized session state.</param>
|
||||
/// <param name="jsonSerializerOptions">Optional settings to customize the deserialization process.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A value task that represents the asynchronous operation. The task result contains a restored <see cref="AgentSession"/> instance with the state from <paramref name="serializedSession"/>.</returns>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedSession"/> is not in the expected format.</exception>
|
||||
/// <returns>A value task that represents the asynchronous operation. The task result contains a restored <see cref="AgentSession"/> instance with the state from <paramref name="serializedState"/>.</returns>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedState"/> is not in the expected format.</exception>
|
||||
/// <exception cref="JsonException">The serialized data is invalid or cannot be deserialized.</exception>
|
||||
/// <remarks>
|
||||
/// This method enables restoration of conversation sessions from previously saved state,
|
||||
/// allowing conversations to resume across application restarts or be migrated between
|
||||
/// different agent instances.
|
||||
/// </remarks>
|
||||
public abstract ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default);
|
||||
public abstract ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Run the agent with no message assuming that all required instructions are already provided to the agent or on the session.
|
||||
|
||||
@@ -81,8 +81,8 @@ public abstract class DelegatingAIAgent : AIAgent
|
||||
=> this.InnerAgent.SerializeSession(session, jsonSerializerOptions);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> this.InnerAgent.DeserializeSessionAsync(serializedSession, jsonSerializerOptions, cancellationToken);
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> this.InnerAgent.DeserializeSessionAsync(serializedState, jsonSerializerOptions, cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task<AgentResponse> RunCoreAsync(
|
||||
|
||||
@@ -58,29 +58,29 @@ public abstract class InMemoryAgentSession : AgentSession
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="InMemoryAgentSession"/> class from previously serialized state.
|
||||
/// </summary>
|
||||
/// <param name="serializedSessionState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="serializedState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="jsonSerializerOptions">Optional settings for customizing the JSON deserialization process.</param>
|
||||
/// <param name="chatHistoryProviderFactory">
|
||||
/// Optional factory function to create the <see cref="InMemoryChatHistoryProvider"/> from its serialized state.
|
||||
/// If not provided, a default factory will be used that creates a basic <see cref="InMemoryChatHistoryProvider"/>.
|
||||
/// </param>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedSessionState"/> is not a JSON object.</exception>
|
||||
/// <exception cref="JsonException">The <paramref name="serializedSessionState"/> is invalid or cannot be deserialized to the expected type.</exception>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedState"/> is not a JSON object.</exception>
|
||||
/// <exception cref="JsonException">The <paramref name="serializedState"/> is invalid or cannot be deserialized to the expected type.</exception>
|
||||
/// <remarks>
|
||||
/// This constructor enables restoration of in-memory threads from previously saved state, allowing
|
||||
/// conversations to be resumed across application restarts or migrated between different instances.
|
||||
/// </remarks>
|
||||
protected InMemoryAgentSession(
|
||||
JsonElement serializedSessionState,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null,
|
||||
Func<JsonElement, JsonSerializerOptions?, InMemoryChatHistoryProvider>? chatHistoryProviderFactory = null)
|
||||
{
|
||||
if (serializedSessionState.ValueKind != JsonValueKind.Object)
|
||||
if (serializedState.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedSessionState));
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedState));
|
||||
}
|
||||
|
||||
var state = serializedSessionState.Deserialize(
|
||||
var state = serializedState.Deserialize(
|
||||
AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(InMemoryAgentSessionState))) as InMemoryAgentSessionState;
|
||||
|
||||
this.ChatHistoryProvider =
|
||||
|
||||
@@ -42,24 +42,24 @@ public abstract class ServiceIdAgentSession : AgentSession
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ServiceIdAgentSession"/> class from previously serialized state.
|
||||
/// </summary>
|
||||
/// <param name="serializedSessionState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="serializedState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="jsonSerializerOptions">Optional settings for customizing the JSON deserialization process.</param>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedSessionState"/> is not a JSON object.</exception>
|
||||
/// <exception cref="JsonException">The <paramref name="serializedSessionState"/> is invalid or cannot be deserialized to the expected type.</exception>
|
||||
/// <exception cref="ArgumentException">The <paramref name="serializedState"/> is not a JSON object.</exception>
|
||||
/// <exception cref="JsonException">The <paramref name="serializedState"/> is invalid or cannot be deserialized to the expected type.</exception>
|
||||
/// <remarks>
|
||||
/// This constructor enables restoration of a service-backed session from serialized state, typically used
|
||||
/// when deserializing session information that was previously saved or transmitted across application boundaries.
|
||||
/// </remarks>
|
||||
protected ServiceIdAgentSession(
|
||||
JsonElement serializedSessionState,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null)
|
||||
{
|
||||
if (serializedSessionState.ValueKind != JsonValueKind.Object)
|
||||
if (serializedState.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedSessionState));
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedState));
|
||||
}
|
||||
|
||||
var state = serializedSessionState.Deserialize(
|
||||
var state = serializedState.Deserialize(
|
||||
AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ServiceIdAgentSessionState))) as ServiceIdAgentSessionState;
|
||||
|
||||
if (state?.ServiceSessionId is string serviceSessionId)
|
||||
|
||||
@@ -67,8 +67,8 @@ public class CopilotStudioAgent : AIAgent
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new CopilotStudioAgentSession(serializedSession, jsonSerializerOptions));
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new CopilotStudioAgentSession(serializedState, jsonSerializerOptions));
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override async Task<AgentResponse> RunCoreAsync(
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
- Removed AgentThreadMetadata and used AgentSessionId directly instead ([#3067](https://github.com/microsoft/agent-framework/pull/3067));
|
||||
- Renamed AgentThread to AgentSession ([#3430](https://github.com/microsoft/agent-framework/pull/3430))
|
||||
- Moved AgentSession.Serialize to AIAgent.SerializeSession ([#3650](https://github.com/microsoft/agent-framework/pull/3650))
|
||||
- Renamed serializedSession parameter to serializedState on DeserializeSessionAsync for consistency ([#3681](https://github.com/microsoft/agent-framework/pull/3681))
|
||||
|
||||
## v1.0.0-preview.251204.1
|
||||
|
||||
|
||||
@@ -64,15 +64,15 @@ public sealed class DurableAIAgent : AIAgent
|
||||
/// <summary>
|
||||
/// Deserializes an agent session from JSON.
|
||||
/// </summary>
|
||||
/// <param name="serializedSession">The serialized session data.</param>
|
||||
/// <param name="serializedState">The serialized session data.</param>
|
||||
/// <param name="jsonSerializerOptions">Optional JSON serializer options.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A value task that represents the asynchronous operation. The task result contains the deserialized agent session.</returns>
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(
|
||||
JsonElement serializedSession,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ValueTask.FromResult<AgentSession>(DurableAgentSession.Deserialize(serializedSession, jsonSerializerOptions));
|
||||
return ValueTask.FromResult<AgentSession>(DurableAgentSession.Deserialize(serializedState, jsonSerializerOptions));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -27,10 +27,10 @@ internal class DurableAIAgentProxy(string name, IDurableAgentClient agentClient)
|
||||
}
|
||||
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(
|
||||
JsonElement serializedSession,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ValueTask.FromResult<AgentSession>(DurableAgentSession.Deserialize(serializedSession, jsonSerializerOptions));
|
||||
return ValueTask.FromResult<AgentSession>(DurableAgentSession.Deserialize(serializedState, jsonSerializerOptions));
|
||||
}
|
||||
|
||||
public override ValueTask<AgentSession> CreateSessionAsync(CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -112,10 +112,10 @@ public sealed class GitHubCopilotAgent : AIAgent, IAsyncDisposable
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(
|
||||
JsonElement serializedSession,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
=> new(new GitHubCopilotAgentSession(serializedSession, jsonSerializerOptions));
|
||||
=> new(new GitHubCopilotAgentSession(serializedState, jsonSerializerOptions));
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override Task<AgentResponse> RunCoreAsync(
|
||||
|
||||
@@ -36,9 +36,9 @@ internal class PurviewAgent : AIAgent, IDisposable
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return this._innerAgent.DeserializeSessionAsync(serializedSession, jsonSerializerOptions, cancellationToken);
|
||||
return this._innerAgent.DeserializeSessionAsync(serializedState, jsonSerializerOptions, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -80,8 +80,8 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
return workflowSession.Serialize(jsonSerializerOptions);
|
||||
}
|
||||
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new WorkflowSession(this._workflow, serializedSession, this._executionEnvironment, this._checkpointManager, this._includeExceptionDetails, this._includeWorkflowOutputsInResponse, jsonSerializerOptions));
|
||||
public override ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
=> new(new WorkflowSession(this._workflow, serializedState, this._executionEnvironment, this._checkpointManager, this._includeExceptionDetails, this._includeWorkflowOutputsInResponse, jsonSerializerOptions));
|
||||
|
||||
private async ValueTask<WorkflowSession> UpdateSessionAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -399,7 +399,7 @@ public sealed partial class ChatClientAgent : AIAgent
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override async ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
public override async ValueTask<AgentSession> DeserializeSessionAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Func<JsonElement, JsonSerializerOptions?, CancellationToken, ValueTask<ChatHistoryProvider>>? chatHistoryProviderFactory = this._agentOptions?.ChatHistoryProviderFactory is null ?
|
||||
null :
|
||||
@@ -410,7 +410,7 @@ public sealed partial class ChatClientAgent : AIAgent
|
||||
(jse, jso, ct) => this._agentOptions.AIContextProviderFactory.Invoke(new() { SerializedState = jse, JsonSerializerOptions = jso }, ct);
|
||||
|
||||
return await ChatClientAgentSession.DeserializeAsync(
|
||||
serializedSession,
|
||||
serializedState,
|
||||
jsonSerializerOptions,
|
||||
chatHistoryProviderFactory,
|
||||
aiContextProviderFactory,
|
||||
|
||||
@@ -115,7 +115,7 @@ public sealed class ChatClientAgentSession : AgentSession
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="ChatClientAgentSession"/> class from previously serialized state.
|
||||
/// </summary>
|
||||
/// <param name="serializedSessionState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="serializedState">A <see cref="JsonElement"/> representing the serialized state of the session.</param>
|
||||
/// <param name="jsonSerializerOptions">Optional settings for customizing the JSON deserialization process.</param>
|
||||
/// <param name="chatHistoryProviderFactory">
|
||||
/// An optional factory function to create a custom <see cref="AI.ChatHistoryProvider"/> from its serialized state.
|
||||
@@ -128,18 +128,18 @@ public sealed class ChatClientAgentSession : AgentSession
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
|
||||
/// <returns>A task representing the asynchronous operation. The task result contains the deserialized <see cref="ChatClientAgentSession"/>.</returns>
|
||||
internal static async Task<ChatClientAgentSession> DeserializeAsync(
|
||||
JsonElement serializedSessionState,
|
||||
JsonElement serializedState,
|
||||
JsonSerializerOptions? jsonSerializerOptions = null,
|
||||
Func<JsonElement, JsonSerializerOptions?, CancellationToken, ValueTask<ChatHistoryProvider>>? chatHistoryProviderFactory = null,
|
||||
Func<JsonElement, JsonSerializerOptions?, CancellationToken, ValueTask<AIContextProvider>>? aiContextProviderFactory = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (serializedSessionState.ValueKind != JsonValueKind.Object)
|
||||
if (serializedState.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedSessionState));
|
||||
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedState));
|
||||
}
|
||||
|
||||
var state = serializedSessionState.Deserialize(
|
||||
var state = serializedState.Deserialize(
|
||||
AgentJsonUtilities.DefaultOptions.GetTypeInfo(typeof(SessionState))) as SessionState;
|
||||
|
||||
var session = new ChatClientAgentSession();
|
||||
|
||||
Reference in New Issue
Block a user