Skip to content

Commit 8b4f7d5

Browse files
CopilotSergeyMenshykhcrickman
authored
.NET: [Breaking] Introduce RunCoreAsync/RunCoreStreamingAsync delegation pattern in AIAgent (#2749)
* Initial plan * Refactor AIAgent: Make RunAsync and RunStreamingAsync non-abstract, add RunCoreAsync and RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix infinite recursion in test implementations Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Make RunAsync and RunStreamingAsync non-virtual as requested Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix DelegatingAIAgent subclasses to use RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix XML documentation references in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Restore <see cref> tags with proper qualified signatures in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Rollback unnecessary XML documentation changes in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Remove pragma and update crefs to RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix EntityAgentWrapper to call base.RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * fix compilation issues * fix compilatio issue * fix tests * fix unit tests * fix unit test --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com> Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
1 parent 4b8a545 commit 8b4f7d5

44 files changed

Lines changed: 372 additions & 257 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dotnet/samples/AGUIClientServer/AGUIDojoServer/AgenticUI/AgenticUIAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public AgenticUIAgent(AIAgent innerAgent, JsonSerializerOptions jsonSerializerOp
1919
this._jsonSerializerOptions = jsonSerializerOptions;
2020
}
2121

22-
public override Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
22+
protected override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
2323
{
24-
return this.RunStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
24+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
2525
}
2626

27-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
27+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
2828
IEnumerable<ChatMessage> messages,
2929
AgentThread? thread = null,
3030
AgentRunOptions? options = null,

dotnet/samples/AGUIClientServer/AGUIDojoServer/PredictiveStateUpdates/PredictiveStateUpdatesAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public PredictiveStateUpdatesAgent(AIAgent innerAgent, JsonSerializerOptions jso
2020
this._jsonSerializerOptions = jsonSerializerOptions;
2121
}
2222

23-
public override Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
23+
protected override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
2424
{
25-
return this.RunStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
25+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
2626
}
2727

28-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
28+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
2929
IEnumerable<ChatMessage> messages,
3030
AgentThread? thread = null,
3131
AgentRunOptions? options = null,

dotnet/samples/AGUIClientServer/AGUIDojoServer/SharedState/SharedStateAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public SharedStateAgent(AIAgent innerAgent, JsonSerializerOptions jsonSerializer
1919
this._jsonSerializerOptions = jsonSerializerOptions;
2020
}
2121

22-
public override Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
22+
protected override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
2323
{
24-
return this.RunStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
24+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
2525
}
2626

27-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
27+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
2828
IEnumerable<ChatMessage> messages,
2929
AgentThread? thread = null,
3030
AgentRunOptions? options = null,

dotnet/samples/GettingStarted/AGUI/Step04_HumanInLoop/Client/ServerFunctionApprovalClientAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public ServerFunctionApprovalClientAgent(AIAgent innerAgent, JsonSerializerOptio
2222
this._jsonSerializerOptions = jsonSerializerOptions;
2323
}
2424

25-
public override Task<AgentRunResponse> RunAsync(
25+
protected override Task<AgentRunResponse> RunCoreAsync(
2626
IEnumerable<ChatMessage> messages,
2727
AgentThread? thread = null,
2828
AgentRunOptions? options = null,
2929
CancellationToken cancellationToken = default)
3030
{
31-
return this.RunStreamingAsync(messages, thread, options, cancellationToken)
31+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken)
3232
.ToAgentRunResponseAsync(cancellationToken);
3333
}
3434

35-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
35+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
3636
IEnumerable<ChatMessage> messages,
3737
AgentThread? thread = null,
3838
AgentRunOptions? options = null,

dotnet/samples/GettingStarted/AGUI/Step04_HumanInLoop/Server/ServerFunctionApprovalServerAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public ServerFunctionApprovalAgent(AIAgent innerAgent, JsonSerializerOptions jso
2222
this._jsonSerializerOptions = jsonSerializerOptions;
2323
}
2424

25-
public override Task<AgentRunResponse> RunAsync(
25+
protected override Task<AgentRunResponse> RunCoreAsync(
2626
IEnumerable<ChatMessage> messages,
2727
AgentThread? thread = null,
2828
AgentRunOptions? options = null,
2929
CancellationToken cancellationToken = default)
3030
{
31-
return this.RunStreamingAsync(messages, thread, options, cancellationToken)
31+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken)
3232
.ToAgentRunResponseAsync(cancellationToken);
3333
}
3434

35-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
35+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
3636
IEnumerable<ChatMessage> messages,
3737
AgentThread? thread = null,
3838
AgentRunOptions? options = null,

dotnet/samples/GettingStarted/AGUI/Step05_StateManagement/Client/StatefulAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ public StatefulAgent(AIAgent innerAgent, JsonSerializerOptions jsonSerializerOpt
3535
}
3636

3737
/// <inheritdoc />
38-
public override Task<AgentRunResponse> RunAsync(
38+
protected override Task<AgentRunResponse> RunCoreAsync(
3939
IEnumerable<ChatMessage> messages,
4040
AgentThread? thread = null,
4141
AgentRunOptions? options = null,
4242
CancellationToken cancellationToken = default)
4343
{
44-
return this.RunStreamingAsync(messages, thread, options, cancellationToken)
44+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken)
4545
.ToAgentRunResponseAsync(cancellationToken);
4646
}
4747

4848
/// <inheritdoc />
49-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
49+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
5050
IEnumerable<ChatMessage> messages,
5151
AgentThread? thread = null,
5252
AgentRunOptions? options = null,

dotnet/samples/GettingStarted/AGUI/Step05_StateManagement/Server/SharedStateAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ public SharedStateAgent(AIAgent innerAgent, JsonSerializerOptions jsonSerializer
1717
this._jsonSerializerOptions = jsonSerializerOptions;
1818
}
1919

20-
public override Task<AgentRunResponse> RunAsync(
20+
protected override Task<AgentRunResponse> RunCoreAsync(
2121
IEnumerable<ChatMessage> messages,
2222
AgentThread? thread = null,
2323
AgentRunOptions? options = null,
2424
CancellationToken cancellationToken = default)
2525
{
26-
return this.RunStreamingAsync(messages, thread, options, cancellationToken)
26+
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken)
2727
.ToAgentRunResponseAsync(cancellationToken);
2828
}
2929

30-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(
30+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
3131
IEnumerable<ChatMessage> messages,
3232
AgentThread? thread = null,
3333
AgentRunOptions? options = null,

dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override AgentThread GetNewThread()
3434
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null)
3535
=> new CustomAgentThread(serializedThread, jsonSerializerOptions);
3636

37-
public override async Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
37+
protected override async Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
3838
{
3939
// Create a thread if the user didn't supply one.
4040
thread ??= this.GetNewThread();
@@ -58,7 +58,7 @@ public override async Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> m
5858
};
5959
}
6060

61-
public override async IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
61+
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
6262
{
6363
// Create a thread if the user didn't supply one.
6464
thread ??= this.GetNewThread();

dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/OpenAIChatClientAgent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public virtual IAsyncEnumerable<StreamingChatCompletionUpdate> RunStreamingAsync
8787
}
8888

8989
/// <inheritdoc/>
90-
public sealed override Task<AgentRunResponse> RunAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
91-
base.RunAsync(messages, thread, options, cancellationToken);
90+
protected sealed override Task<AgentRunResponse> RunCoreAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
91+
base.RunCoreAsync(messages, thread, options, cancellationToken);
9292

9393
/// <inheritdoc/>
94-
public override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
95-
base.RunStreamingAsync(messages, thread, options, cancellationToken);
94+
protected override IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
95+
base.RunCoreStreamingAsync(messages, thread, options, cancellationToken);
9696
}

dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/OpenAIResponseClientAgent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public virtual async IAsyncEnumerable<StreamingResponseUpdate> RunStreamingAsync
105105
}
106106

107107
/// <inheritdoc/>
108-
public sealed override Task<AgentRunResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
109-
base.RunAsync(messages, thread, options, cancellationToken);
108+
protected sealed override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
109+
base.RunCoreAsync(messages, thread, options, cancellationToken);
110110

111111
/// <inheritdoc/>
112-
public sealed override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
113-
base.RunStreamingAsync(messages, thread, options, cancellationToken);
112+
protected sealed override IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) =>
113+
base.RunCoreStreamingAsync(messages, thread, options, cancellationToken);
114114
}

0 commit comments

Comments
 (0)