Skip to content

Commit 22f3a59

Browse files
authored
Merge pull request #958 from MicrosoftDocs/foundry-updates-0004
Fix pre-existing C# code inaccuracies in workflows and integrations docs
2 parents 3dde166 + a61282d commit 22f3a59

5 files changed

Lines changed: 41 additions & 31 deletions

File tree

agent-framework/agents/providers/custom.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ Agents can therefore attach any additional state or behaviors needed to the sess
6868
Two methods are required to be implemented:
6969

7070
```csharp
71-
public override Task<AgentSession> CreateSessionAsync(CancellationToken cancellationToken = default)
72-
=> Task.FromResult<AgentSession>(new CustomAgentSession());
71+
protected override ValueTask<AgentSession> CreateSessionCoreAsync(CancellationToken cancellationToken = default)
72+
=> new(new CustomAgentSession());
7373

74-
public override Task<AgentSession> DeserializeSessionAsync(JsonElement serializedSession, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
75-
=> Task.FromResult<AgentSession>(new CustomAgentSession(serializedSession, jsonSerializerOptions));
74+
protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(JsonElement serializedState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
75+
=> new(new CustomAgentSession(serializedState, jsonSerializerOptions));
7676
```
7777

7878
### Core agent logic
@@ -109,7 +109,7 @@ Messages can be retrieved and passed to the `ChatHistoryProvider` on the session
109109
If you don't do this, the user won't be able to have a multi-turn conversation with the agent and each run will be a fresh interaction.
110110

111111
```csharp
112-
public override async Task<AgentResponse> RunAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
112+
protected override async Task<AgentResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
113113
{
114114
session ??= await this.CreateSessionAsync(cancellationToken);
115115

@@ -134,7 +134,7 @@ If you don't do this, the user won't be able to have a multi-turn conversation w
134134
};
135135
}
136136

137-
public override async IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
137+
protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(IEnumerable<ChatMessage> messages, AgentSession? session = null, AgentRunOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
138138
{
139139
session ??= await this.CreateSessionAsync(cancellationToken);
140140

agent-framework/integrations/ag-ui/state-management.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ internal sealed class SharedStateAgent : DelegatingAIAgent
113113
this._jsonSerializerOptions = jsonSerializerOptions;
114114
}
115115

116-
public override Task<AgentResponse> RunAsync(
116+
protected override Task<AgentResponse> RunCoreAsync(
117117
IEnumerable<ChatMessage> messages,
118118
AgentSession? session = null,
119119
AgentRunOptions? options = null,
@@ -123,7 +123,7 @@ internal sealed class SharedStateAgent : DelegatingAIAgent
123123
.ToAgentResponseAsync(cancellationToken);
124124
}
125125

126-
public override async IAsyncEnumerable<AgentResponseUpdate> RunStreamingAsync(
126+
protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
127127
IEnumerable<ChatMessage> messages,
128128
AgentSession? session = null,
129129
AgentRunOptions? options = null,
@@ -203,22 +203,25 @@ internal sealed class SharedStateAgent : DelegatingAIAgent
203203
var response = allUpdates.ToAgentResponse();
204204

205205
// Try to deserialize the structured state response
206-
if (response.TryDeserialize(this._jsonSerializerOptions, out JsonElement stateSnapshot))
206+
JsonElement stateSnapshot;
207+
try
207208
{
208-
// Serialize and emit as STATE_SNAPSHOT via DataContent
209-
byte[] stateBytes = JsonSerializer.SerializeToUtf8Bytes(
210-
stateSnapshot,
211-
this._jsonSerializerOptions.GetTypeInfo(typeof(JsonElement)));
212-
yield return new AgentResponseUpdate
213-
{
214-
Contents = [new DataContent(stateBytes, "application/json")]
215-
};
209+
stateSnapshot = JsonSerializer.Deserialize<JsonElement>(response.Text, this._jsonSerializerOptions);
216210
}
217-
else
211+
catch (JsonException)
218212
{
219213
yield break;
220214
}
221215

216+
// Serialize and emit as STATE_SNAPSHOT via DataContent
217+
byte[] stateBytes = JsonSerializer.SerializeToUtf8Bytes(
218+
stateSnapshot,
219+
this._jsonSerializerOptions.GetTypeInfo(typeof(JsonElement)));
220+
yield return new AgentResponseUpdate
221+
{
222+
Contents = [new DataContent(stateBytes, "application/json")]
223+
};
224+
222225
// Second run: Generate user-friendly summary
223226
var secondRunMessages = messages.Concat(response.Messages).Append(
224227
new ChatMessage(

agent-framework/workflows/edges.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Collect messages from multiple sources into a single target:
6868
::: zone pivot="programming-language-csharp"
6969

7070
```csharp
71-
builder.AddFanInBarrierEdge(sources: [worker1, worker2, worker3], target: aggregatorExecutor);
71+
builder.AddFanInBarrierEdge(sources: [ worker1, worker2, worker3 ], target: aggregatorExecutor);
7272
```
7373

7474
::: zone-end
@@ -210,10 +210,11 @@ using Microsoft.Extensions.AI;
210210
/// </summary>
211211
/// <returns>A ChatClientAgent configured for spam detection</returns>
212212
private static ChatClientAgent GetSpamDetectionAgent(IChatClient chatClient) =>
213-
new(chatClient, new ChatClientAgentOptions(instructions: "You are a spam detection assistant that identifies spam emails.")
213+
new(chatClient, new ChatClientAgentOptions
214214
{
215215
ChatOptions = new()
216216
{
217+
Instructions = "You are a spam detection assistant that identifies spam emails.",
217218
ResponseFormat = ChatResponseFormat.ForJsonSchema(AIJsonUtilities.CreateJsonSchema(typeof(DetectionResult)))
218219
}
219220
});
@@ -223,10 +224,11 @@ private static ChatClientAgent GetSpamDetectionAgent(IChatClient chatClient) =>
223224
/// </summary>
224225
/// <returns>A ChatClientAgent configured for email assistance</returns>
225226
private static ChatClientAgent GetEmailAssistantAgent(IChatClient chatClient) =>
226-
new(chatClient, new ChatClientAgentOptions(instructions: "You are an email assistant that helps users draft professional responses to emails.")
227+
new(chatClient, new ChatClientAgentOptions
227228
{
228229
ChatOptions = new()
229230
{
231+
Instructions = "You are an email assistant that helps users draft professional responses to emails.",
230232
ResponseFormat = ChatResponseFormat.ForJsonSchema(AIJsonUtilities.CreateJsonSchema(typeof(EmailResponse)))
231233
}
232234
});
@@ -378,7 +380,7 @@ public static class Program
378380

379381
// Execute the workflow with sample spam email
380382
string emailContent = "Congratulations! You've won $1,000,000! Click here to claim your prize now!";
381-
StreamingRun run = await InProcessExecution.StreamAsync(workflow, new ChatMessage(ChatRole.User, emailContent));
383+
StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, new ChatMessage(ChatRole.User, emailContent));
382384
await run.TrySendMessageAsync(new TurnToken(emitEvents: true));
383385

384386
await foreach (WorkflowEvent evt in run.WatchStreamAsync().ConfigureAwait(false))
@@ -815,10 +817,11 @@ Update the spam detection agent to be less confident and return three-way classi
815817
/// </summary>
816818
/// <returns>A ChatClientAgent configured for three-way spam detection</returns>
817819
private static ChatClientAgent GetSpamDetectionAgent(IChatClient chatClient) =>
818-
new(chatClient, new ChatClientAgentOptions(instructions: "You are a spam detection assistant that identifies spam emails. Be less confident in your assessments.")
820+
new(chatClient, new ChatClientAgentOptions
819821
{
820822
ChatOptions = new()
821823
{
824+
Instructions = "You are a spam detection assistant that identifies spam emails. Be less confident in your assessments.",
822825
ResponseFormat = ChatResponseFormat.ForJsonSchema<DetectionResult>()
823826
}
824827
});
@@ -828,10 +831,11 @@ private static ChatClientAgent GetSpamDetectionAgent(IChatClient chatClient) =>
828831
/// </summary>
829832
/// <returns>A ChatClientAgent configured for email assistance</returns>
830833
private static ChatClientAgent GetEmailAssistantAgent(IChatClient chatClient) =>
831-
new(chatClient, new ChatClientAgentOptions(instructions: "You are an email assistant that helps users draft responses to emails with professionalism.")
834+
new(chatClient, new ChatClientAgentOptions
832835
{
833836
ChatOptions = new()
834837
{
838+
Instructions = "You are an email assistant that helps users draft responses to emails with professionalism.",
835839
ResponseFormat = ChatResponseFormat.ForJsonSchema<EmailResponse>()
836840
}
837841
});
@@ -1015,7 +1019,7 @@ public static class Program
10151019
string email = Resources.Read("ambiguous_email.txt");
10161020

10171021
// Execute the workflow
1018-
StreamingRun run = await InProcessExecution.StreamAsync(workflow, new ChatMessage(ChatRole.User, email));
1022+
StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, new ChatMessage(ChatRole.User, email));
10191023
await run.TrySendMessageAsync(new TurnToken(emitEvents: true));
10201024
await foreach (WorkflowEvent evt in run.WatchStreamAsync().ConfigureAwait(false))
10211025
{
@@ -1677,10 +1681,11 @@ Create agents for analysis, assistance, and summarization:
16771681
/// </summary>
16781682
/// <returns>A ChatClientAgent configured for comprehensive email analysis</returns>
16791683
private static ChatClientAgent GetEmailAnalysisAgent(IChatClient chatClient) =>
1680-
new(chatClient, new ChatClientAgentOptions(instructions: "You are a spam detection assistant that identifies spam emails.")
1684+
new(chatClient, new ChatClientAgentOptions
16811685
{
16821686
ChatOptions = new()
16831687
{
1688+
Instructions = "You are a spam detection assistant that identifies spam emails.",
16841689
ResponseFormat = ChatResponseFormat.ForJsonSchema<AnalysisResult>()
16851690
}
16861691
});
@@ -1690,10 +1695,11 @@ private static ChatClientAgent GetEmailAnalysisAgent(IChatClient chatClient) =>
16901695
/// </summary>
16911696
/// <returns>A ChatClientAgent configured for email assistance</returns>
16921697
private static ChatClientAgent GetEmailAssistantAgent(IChatClient chatClient) =>
1693-
new(chatClient, new ChatClientAgentOptions(instructions: "You are an email assistant that helps users draft responses to emails with professionalism.")
1698+
new(chatClient, new ChatClientAgentOptions
16941699
{
16951700
ChatOptions = new()
16961701
{
1702+
Instructions = "You are an email assistant that helps users draft responses to emails with professionalism.",
16971703
ResponseFormat = ChatResponseFormat.ForJsonSchema<EmailResponse>()
16981704
}
16991705
});
@@ -1703,10 +1709,11 @@ private static ChatClientAgent GetEmailAssistantAgent(IChatClient chatClient) =>
17031709
/// </summary>
17041710
/// <returns>A ChatClientAgent configured for email summarization</returns>
17051711
private static ChatClientAgent GetEmailSummaryAgent(IChatClient chatClient) =>
1706-
new(chatClient, new ChatClientAgentOptions(instructions: "You are an assistant that helps users summarize emails.")
1712+
new(chatClient, new ChatClientAgentOptions
17071713
{
17081714
ChatOptions = new()
17091715
{
1716+
Instructions = "You are an assistant that helps users summarize emails.",
17101717
ResponseFormat = ChatResponseFormat.ForJsonSchema<EmailSummary>()
17111718
}
17121719
});
@@ -1773,7 +1780,7 @@ public static class Program
17731780
string email = Resources.Read("email.txt");
17741781

17751782
// Execute the workflow with custom event handling
1776-
StreamingRun run = await InProcessExecution.StreamAsync(workflow, new ChatMessage(ChatRole.User, email));
1783+
StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, new ChatMessage(ChatRole.User, email));
17771784
await run.TrySendMessageAsync(new TurnToken(emitEvents: true));
17781785
await foreach (WorkflowEvent evt in run.WatchStreamAsync().ConfigureAwait(false))
17791786
{

agent-framework/workflows/orchestrations/concurrent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ await foreach (WorkflowEvent evt in run.WatchStreamAsync())
121121
Console.WriteLine("===== Final Aggregated Results =====");
122122
foreach (var message in result)
123123
{
124-
Console.WriteLine($"{message.Role}: {message.Content}");
124+
Console.WriteLine($"{message.Role}: {message.Text}");
125125
}
126126
```
127127

agent-framework/workflows/orchestrations/sequential.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ await foreach (WorkflowEvent evt in run.WatchStreamAsync())
135135
Console.WriteLine();
136136
foreach (var message in result)
137137
{
138-
Console.WriteLine($"{message.Role}: {message.Content}");
138+
Console.WriteLine($"{message.Role}: {message.Text}");
139139
}
140140
```
141141

0 commit comments

Comments
 (0)