// Copyright (c) Microsoft. All rights reserved. using System.Diagnostics; using System.Text.Json; using System.Text.Json.Serialization; namespace Microsoft.Agents.AI.DurableTask; /// /// An agent thread implementation for durable agents. /// [DebuggerDisplay("{SessionId}")] public sealed class DurableAgentThread : AgentThread { [JsonConstructor] internal DurableAgentThread(AgentSessionId sessionId) { this.SessionId = sessionId; } /// /// Gets the agent session ID. /// [JsonInclude] [JsonPropertyName("sessionId")] internal AgentSessionId SessionId { get; } /// public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null) { return JsonSerializer.SerializeToElement( this, DurableAgentJsonUtilities.DefaultOptions.GetTypeInfo(typeof(DurableAgentThread))); } /// /// Deserializes a DurableAgentThread from JSON. /// /// The serialized thread data. /// Optional JSON serializer options. /// The deserialized DurableAgentThread. internal static DurableAgentThread Deserialize(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null) { if (!serializedThread.TryGetProperty("sessionId", out JsonElement sessionIdElement) || sessionIdElement.ValueKind != JsonValueKind.String) { throw new JsonException("Invalid or missing sessionId property."); } string sessionIdString = sessionIdElement.GetString() ?? throw new JsonException("sessionId property is null."); AgentSessionId sessionId = AgentSessionId.Parse(sessionIdString); return new DurableAgentThread(sessionId); } /// public override object? GetService(Type serviceType, object? serviceKey = null) { if (serviceType == typeof(AgentSessionId)) { return this.SessionId; } return base.GetService(serviceType, serviceKey); } /// public override string ToString() { return this.SessionId.ToString(); } }