You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.NET: [BREAKING] Add ability to mark the source of Agent request messages and use that for filtering (microsoft#3540)
* Add ability to mark the source of Agent request messages and use that for filtering
* Add support for source, in addition to source type, and add unit tests for automatic stamping
* Address PR comments.
* Add merge fixes
* Address PR comments
Copy file name to clipboardExpand all lines: dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs
+99-17Lines changed: 99 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
usingSystem;
4
4
usingSystem.Collections.Generic;
5
+
usingSystem.Linq;
5
6
usingSystem.Text.Json;
6
7
usingSystem.Threading;
7
8
usingSystem.Threading.Tasks;
@@ -31,6 +32,25 @@ namespace Microsoft.Agents.AI;
31
32
/// </remarks>
32
33
publicabstractclassAIContextProvider
33
34
{
35
+
privatereadonlystring_sourceName;
36
+
37
+
/// <summary>
38
+
/// Initializes a new instance of the <see cref="AIContextProvider"/> class.
39
+
/// </summary>
40
+
protectedAIContextProvider()
41
+
{
42
+
this._sourceName=this.GetType().FullName!;
43
+
}
44
+
45
+
/// <summary>
46
+
/// Initializes a new instance of the <see cref="AIContextProvider"/> class with the specified source name.
47
+
/// </summary>
48
+
/// <param name="sourceName">The source name to stamp on <see cref="ChatMessage.AdditionalProperties"/> for each messages produced by the <see cref="AIContextProvider"/>.</param>
49
+
protectedAIContextProvider(stringsourceName)
50
+
{
51
+
this._sourceName=sourceName;
52
+
}
53
+
34
54
/// <summary>
35
55
/// Called at the start of agent invocation to provide additional context.
36
56
/// </summary>
@@ -48,7 +68,81 @@ public abstract class AIContextProvider
/// Called at the start of agent invocation to provide additional context.
106
+
/// </summary>
107
+
/// <param name="context">Contains the request context including the caller provided messages that will be used by the agent for this invocation.</param>
108
+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
109
+
/// <returns>A task that represents the asynchronous operation. The task result contains the <see cref="AIContext"/> with additional context to be used by the agent during this invocation.</returns>
110
+
/// <remarks>
111
+
/// <para>
112
+
/// Implementers can load any additional context required at this time, such as:
113
+
/// <list type="bullet">
114
+
/// <item><description>Retrieving relevant information from knowledge bases</description></item>
115
+
/// <item><description>Adding system instructions or prompts</description></item>
116
+
/// <item><description>Providing function tools for the current invocation</description></item>
117
+
/// <item><description>Injecting contextual messages from conversation history</description></item>
/// Called at the end of the agent invocation to process the invocation results.
125
+
/// </summary>
126
+
/// <param name="context">Contains the invocation context including request messages, response messages, and any exception that occurred.</param>
127
+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
128
+
/// <returns>A task that represents the asynchronous operation.</returns>
129
+
/// <remarks>
130
+
/// <para>
131
+
/// Implementers can use the request and response messages in the provided <paramref name="context"/> to:
132
+
/// <list type="bullet">
133
+
/// <item><description>Update internal state based on conversation outcomes</description></item>
134
+
/// <item><description>Extract and store memories or preferences from user messages</description></item>
135
+
/// <item><description>Log or audit conversation details</description></item>
136
+
/// <item><description>Perform cleanup or finalization tasks</description></item>
137
+
/// </list>
138
+
/// </para>
139
+
/// <para>
140
+
/// This method is called regardless of whether the invocation succeeded or failed.
141
+
/// To check if the invocation was successful, inspect the <see cref="InvokedContext.InvokeException"/> property.
0 commit comments