forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceIdAgentSession.cs
More file actions
104 lines (93 loc) · 5.04 KB
/
ServiceIdAgentSession.cs
File metadata and controls
104 lines (93 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Diagnostics;
using System.Text.Json;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI;
/// <summary>
/// Provides a base class for agent sessions that store conversation state remotely in a service and maintain only an identifier reference locally.
/// </summary>
/// <remarks>
/// This class is designed for scenarios where conversation state is managed by an external service (such as a cloud-based AI service)
/// rather than being stored locally. The session maintains only the service identifier needed to reference the remote conversation state.
/// </remarks>
[DebuggerDisplay("ServiceSessionId = {ServiceSessionId}")]
public abstract class ServiceIdAgentSession : AgentSession
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceIdAgentSession"/> class without a service session identifier.
/// </summary>
/// <remarks>
/// When using this constructor, the <see cref="ServiceSessionId"/> will be <see langword="null"/> initially
/// and should be set by derived classes when the remote conversation is created.
/// </remarks>
protected ServiceIdAgentSession()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceIdAgentSession"/> class with the specified service session identifier.
/// </summary>
/// <param name="serviceSessionId">The unique identifier that references the conversation state stored in the remote service.</param>
/// <exception cref="ArgumentNullException"><paramref name="serviceSessionId"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="serviceSessionId"/> is empty or contains only whitespace.</exception>
protected ServiceIdAgentSession(string serviceSessionId)
{
this.ServiceSessionId = Throw.IfNullOrEmpty(serviceSessionId);
}
/// <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="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>
/// <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,
JsonSerializerOptions? jsonSerializerOptions = null)
{
if (serializedSessionState.ValueKind != JsonValueKind.Object)
{
throw new ArgumentException("The serialized session state must be a JSON object.", nameof(serializedSessionState));
}
var state = serializedSessionState.Deserialize(
AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ServiceIdAgentSessionState))) as ServiceIdAgentSessionState;
if (state?.ServiceSessionId is string serviceSessionId)
{
this.ServiceSessionId = serviceSessionId;
}
}
/// <summary>
/// Gets or sets the unique identifier that references the conversation state stored in the remote service.
/// </summary>
/// <value>
/// A string identifier that uniquely identifies the conversation within the remote service,
/// or <see langword="null"/> if no remote conversation has been established yet.
/// </value>
/// <remarks>
/// This identifier is used by derived classes to reference the remote conversation state when making
/// API calls to the backing service. The exact format and meaning of this identifier depends on the
/// specific service implementation.
/// </remarks>
protected string? ServiceSessionId { get; set; }
/// <summary>
/// Serializes the current object's state to a <see cref="JsonElement"/> using the specified serialization options.
/// </summary>
/// <param name="jsonSerializerOptions">The JSON serialization options to use.</param>
/// <returns>A <see cref="JsonElement"/> representation of the object's state.</returns>
protected internal virtual JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null)
{
var state = new ServiceIdAgentSessionState
{
ServiceSessionId = this.ServiceSessionId,
};
return JsonSerializer.SerializeToElement(state, AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ServiceIdAgentSessionState)));
}
internal sealed class ServiceIdAgentSessionState
{
public string? ServiceSessionId { get; set; }
}
}