-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAgentResponseExtensions.cs
More file actions
217 lines (197 loc) · 10.3 KB
/
AgentResponseExtensions.cs
File metadata and controls
217 lines (197 loc) · 10.3 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI;
/// <summary>
/// Provides extension methods for working with <see cref="AgentResponse"/> and <see cref="AgentResponseUpdate"/> instances.
/// </summary>
public static class AgentResponseExtensions
{
/// <summary>
/// Creates a <see cref="ChatResponse"/> from an <see cref="AgentResponse"/> instance.
/// </summary>
/// <param name="response">The <see cref="AgentResponse"/> to convert.</param>
/// <returns>A <see cref="ChatResponse"/> built from the specified <paramref name="response"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception>
/// <remarks>
/// If the <paramref name="response"/>'s <see cref="AgentResponse.RawRepresentation"/> is already a
/// <see cref="ChatResponse"/> instance, that instance is returned directly.
/// Otherwise, a new <see cref="ChatResponse"/> is created and populated with the data from the <paramref name="response"/>.
/// The resulting instance is a shallow copy; any reference-type members (e.g. <see cref="AgentResponse.Messages"/>)
/// will be shared between the two instances.
/// </remarks>
public static ChatResponse AsChatResponse(this AgentResponse response)
{
Throw.IfNull(response);
return
response.RawRepresentation as ChatResponse ??
new()
{
AdditionalProperties = response.AdditionalProperties,
CreatedAt = response.CreatedAt,
Messages = response.Messages,
RawRepresentation = response,
ResponseId = response.ResponseId,
Usage = response.Usage,
ContinuationToken = response.ContinuationToken,
};
}
/// <summary>
/// Creates a <see cref="ChatResponseUpdate"/> from an <see cref="AgentResponseUpdate"/> instance.
/// </summary>
/// <param name="responseUpdate">The <see cref="AgentResponseUpdate"/> to convert.</param>
/// <returns>A <see cref="ChatResponseUpdate"/> built from the specified <paramref name="responseUpdate"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="responseUpdate"/> is <see langword="null"/>.</exception>
/// <remarks>
/// If the <paramref name="responseUpdate"/>'s <see cref="AgentResponseUpdate.RawRepresentation"/> is already a
/// <see cref="ChatResponseUpdate"/> instance, that instance is returned directly.
/// Otherwise, a new <see cref="ChatResponseUpdate"/> is created and populated with the data from the <paramref name="responseUpdate"/>.
/// The resulting instance is a shallow copy; any reference-type members (e.g. <see cref="AgentResponseUpdate.Contents"/>)
/// will be shared between the two instances.
/// </remarks>
public static ChatResponseUpdate AsChatResponseUpdate(this AgentResponseUpdate responseUpdate)
{
Throw.IfNull(responseUpdate);
if (responseUpdate.RawRepresentation is ChatResponseUpdate raw)
{
// Recover MessageId from the wrapper if the raw representation doesn't have one.
// This ensures consistency when the wrapper has information the raw object lost.
raw.MessageId ??= responseUpdate.MessageId;
return raw;
}
return new()
{
AdditionalProperties = responseUpdate.AdditionalProperties,
AuthorName = responseUpdate.AuthorName,
Contents = responseUpdate.Contents,
CreatedAt = responseUpdate.CreatedAt,
MessageId = responseUpdate.MessageId,
RawRepresentation = responseUpdate,
ResponseId = responseUpdate.ResponseId,
Role = responseUpdate.Role,
ContinuationToken = responseUpdate.ContinuationToken,
};
}
/// <summary>
/// Creates an asynchronous enumerable of <see cref="ChatResponseUpdate"/> instances from an asynchronous
/// enumerable of <see cref="AgentResponseUpdate"/> instances.
/// </summary>
/// <param name="responseUpdates">The sequence of <see cref="AgentResponseUpdate"/> instances to convert.</param>
/// <returns>An asynchronous enumerable of <see cref="ChatResponseUpdate"/> instances built from <paramref name="responseUpdates"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="responseUpdates"/> is <see langword="null"/>.</exception>
/// <remarks>
/// Each <see cref="AgentResponseUpdate"/> is converted to a <see cref="ChatResponseUpdate"/> using
/// <see cref="AsChatResponseUpdate"/>.
/// </remarks>
public static async IAsyncEnumerable<ChatResponseUpdate> AsChatResponseUpdatesAsync(
this IAsyncEnumerable<AgentResponseUpdate> responseUpdates)
{
Throw.IfNull(responseUpdates);
await foreach (var responseUpdate in responseUpdates.ConfigureAwait(false))
{
yield return responseUpdate.AsChatResponseUpdate();
}
}
/// <summary>
/// Combines a sequence of <see cref="AgentResponseUpdate"/> instances into a single <see cref="AgentResponse"/>.
/// </summary>
/// <param name="updates">The sequence of updates to be combined into a single response.</param>
/// <returns>A single <see cref="AgentResponse"/> that represents the combined state of all the updates.</returns>
/// <exception cref="ArgumentNullException"><paramref name="updates"/> is <see langword="null"/>.</exception>
/// <remarks>
/// As part of combining <paramref name="updates"/> into a single <see cref="AgentResponse"/>, the method will attempt to reconstruct
/// <see cref="ChatMessage"/> instances. This includes using <see cref="AgentResponseUpdate.MessageId"/> to determine
/// message boundaries, as well as coalescing contiguous <see cref="AIContent"/> items where applicable, e.g. multiple
/// <see cref="TextContent"/> instances in a row may be combined into a single <see cref="TextContent"/>.
/// </remarks>
public static AgentResponse ToAgentResponse(
this IEnumerable<AgentResponseUpdate> updates)
{
_ = Throw.IfNull(updates);
AgentResponseDetails additionalDetails = new();
ChatResponse chatResponse =
AsChatResponseUpdatesWithAdditionalDetails(updates, additionalDetails)
.ToChatResponse();
return new AgentResponse(chatResponse)
{
AgentId = additionalDetails.AgentId,
};
}
/// <summary>
/// Asynchronously combines a sequence of <see cref="AgentResponseUpdate"/> instances into a single <see cref="AgentResponse"/>.
/// </summary>
/// <param name="updates">The asynchronous sequence of updates to be combined into a single response.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a single <see cref="AgentResponse"/> that represents the combined state of all the updates.</returns>
/// <exception cref="ArgumentNullException"><paramref name="updates"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>
/// This is the asynchronous version of <see cref="ToAgentResponse(IEnumerable{AgentResponseUpdate})"/>.
/// It performs the same combining logic but operates on an asynchronous enumerable of updates.
/// </para>
/// <para>
/// As part of combining <paramref name="updates"/> into a single <see cref="AgentResponse"/>, the method will attempt to reconstruct
/// <see cref="ChatMessage"/> instances. This includes using <see cref="AgentResponseUpdate.MessageId"/> to determine
/// message boundaries, as well as coalescing contiguous <see cref="AIContent"/> items where applicable, e.g. multiple
/// <see cref="TextContent"/> instances in a row may be combined into a single <see cref="TextContent"/>.
/// </para>
/// </remarks>
public static Task<AgentResponse> ToAgentResponseAsync(
this IAsyncEnumerable<AgentResponseUpdate> updates,
CancellationToken cancellationToken = default)
{
_ = Throw.IfNull(updates);
return ToAgentResponseAsync(updates, cancellationToken);
static async Task<AgentResponse> ToAgentResponseAsync(
IAsyncEnumerable<AgentResponseUpdate> updates,
CancellationToken cancellationToken)
{
AgentResponseDetails additionalDetails = new();
ChatResponse chatResponse = await
AsChatResponseUpdatesWithAdditionalDetailsAsync(updates, additionalDetails, cancellationToken)
.ToChatResponseAsync(cancellationToken)
.ConfigureAwait(false);
return new AgentResponse(chatResponse)
{
AgentId = additionalDetails.AgentId,
};
}
}
private static IEnumerable<ChatResponseUpdate> AsChatResponseUpdatesWithAdditionalDetails(
IEnumerable<AgentResponseUpdate> updates,
AgentResponseDetails additionalDetails)
{
foreach (var update in updates)
{
UpdateAdditionalDetails(update, additionalDetails);
yield return update.AsChatResponseUpdate();
}
}
private static async IAsyncEnumerable<ChatResponseUpdate> AsChatResponseUpdatesWithAdditionalDetailsAsync(
IAsyncEnumerable<AgentResponseUpdate> updates,
AgentResponseDetails additionalDetails,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
await foreach (var update in updates.WithCancellation(cancellationToken).ConfigureAwait(false))
{
UpdateAdditionalDetails(update, additionalDetails);
yield return update.AsChatResponseUpdate();
}
}
private static void UpdateAdditionalDetails(AgentResponseUpdate update, AgentResponseDetails details)
{
if (update.AgentId is { Length: > 0 })
{
details.AgentId = update.AgentId;
}
}
private sealed class AgentResponseDetails
{
public string? AgentId { get; set; }
}
}