Skip to content

Commit 7e90cbc

Browse files
committed
fix: Address PR Comments
* Implicit filter in collection loops * Remove debug / usused code * Fix ProtocolBuilder implicit output registrations * Fix logic error in ExecuteRouteGeneratorTests.ClassWithManualConfigureProtocol_DoesNotGenerate
1 parent c0ed51e commit 7e90cbc

File tree

8 files changed

+43
-66
lines changed

8 files changed

+43
-66
lines changed

dotnet/src/Microsoft.Agents.AI.Workflows.Generators/Generation/SourceBuilder.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56
using Microsoft.Agents.AI.Workflows.Generators.Models;
67

@@ -55,6 +56,7 @@ public static string Generate(ExecutorInfo info)
5556
{
5657
sb.AppendLine($"{indent}partial class {containingType}");
5758
sb.AppendLine($"{indent}{{");
59+
5860
indent += IndentUnit;
5961
}
6062
}
@@ -198,24 +200,18 @@ private static void GenerateConfigureSentTypes(StringBuilder sb, ExecutorInfo in
198200
// but cleaner generated code is easier to read).
199201
var addedTypes = new HashSet<string>();
200202

201-
foreach (var type in info.ClassSendTypes)
203+
foreach (var type in info.ClassSendTypes.Where(type => addedTypes.Add(type)))
202204
{
203-
if (addedTypes.Add(type))
204-
{
205-
sb.AppendLine($".SendsMessage<{type}>()");
206-
sb.Append(indent);
207-
}
205+
sb.AppendLine($".SendsMessage<{type}>()");
206+
sb.Append(indent);
208207
}
209208

210209
foreach (var handler in info.Handlers)
211210
{
212-
foreach (var type in handler.SendTypes)
211+
foreach (var type in handler.SendTypes.Where(type => addedTypes.Add(type)))
213212
{
214-
if (addedTypes.Add(type))
215-
{
216-
sb.AppendLine($".SendsMessage<{type}>()");
217-
sb.Append(indent);
218-
}
213+
sb.AppendLine($".SendsMessage<{type}>()");
214+
sb.Append(indent);
219215
}
220216
}
221217
}
@@ -233,24 +229,18 @@ private static void GenerateConfigureYieldTypes(StringBuilder sb, ExecutorInfo i
233229
// but cleaner generated code is easier to read).
234230
var addedTypes = new HashSet<string>();
235231

236-
foreach (var type in info.ClassYieldTypes)
232+
foreach (var type in info.ClassYieldTypes.Where(type => addedTypes.Add(type)))
237233
{
238-
if (addedTypes.Add(type))
239-
{
240-
sb.AppendLine($".YieldsOutput<{type}>()");
241-
sb.Append(indent);
242-
}
234+
sb.AppendLine($".YieldsOutput<{type}>()");
235+
sb.Append(indent);
243236
}
244237

245238
foreach (var handler in info.Handlers)
246239
{
247-
foreach (var type in handler.YieldTypes)
240+
foreach (var type in handler.YieldTypes.Where(type => addedTypes.Add(type)))
248241
{
249-
if (addedTypes.Add(type))
250-
{
251-
sb.AppendLine($".YieldsOutput<{type}>()");
252-
sb.Append(indent);
253-
}
242+
sb.AppendLine($".YieldsOutput<{type}>()");
243+
sb.Append(indent);
254244
}
255245
}
256246
}

dotnet/src/Microsoft.Agents.AI.Workflows/Execution/MessageRouter.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,14 @@ internal MessageRouter(Dictionary<Type, MessageHandlerF> handlers, HashSet<Type>
8383
}
8484
else if (this._interfaceHandlers.Count > 0)
8585
{
86-
foreach (Type interfaceType in this._interfaceHandlers)
86+
foreach (Type interfaceType in this._interfaceHandlers.Where(it => it.IsAssignableFrom(candidateType)))
8787
{
88-
if (interfaceType.IsAssignableFrom(candidateType))
89-
{
90-
handler = this._typedHandlers[interfaceType];
91-
this._typedHandlers[messageType] = handler;
92-
93-
// TODO: This could cause some consternation with Checkpointing (need to ensure we surface errors well)
94-
this._runtimeTypeMap[new TypeId(messageType)] = interfaceType;
95-
return handler;
96-
}
88+
handler = this._typedHandlers[interfaceType];
89+
this._typedHandlers[messageType] = handler;
90+
91+
// TODO: This could cause some consternation with Checkpointing (need to ensure we surface errors well)
92+
this._runtimeTypeMap[new TypeId(messageType)] = interfaceType;
93+
return handler;
9794
}
9895
}
9996
}

dotnet/src/Microsoft.Agents.AI.Workflows/Executor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public MessageTypeTranslator(ISet<Type> types)
9696
}
9797
}
9898

99-
public TypeId? GetDeclaredType(Type messageType, bool allowPolymorphism = true)
99+
public TypeId? GetDeclaredType(Type messageType)
100100
{
101101
// If the user declares a base type, the user is expected to set up any serialization to be able to deal with
102102
// the polymorphism transparently to the framework, or be expecting to deal with the appropriate truncation.
@@ -112,10 +112,6 @@ public MessageTypeTranslator(ISet<Type> types)
112112

113113
return declaredTypeId;
114114
}
115-
if (!allowPolymorphism)
116-
{
117-
break;
118-
}
119115
}
120116

121117
return null;

dotnet/src/Microsoft.Agents.AI.Workflows/InProc/InProcessRunner.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ await executor.ExecuteAsync(
236236
}
237237
}
238238

239-
if (envelope.MessageType != messageType)
240-
{
241-
Console.Error.WriteLine($"Translated type of delivery: {envelope.MessageType} to {messageType}");
242-
}
243-
244239
return (value, messageType);
245240
}
246241
}

dotnet/src/Microsoft.Agents.AI.Workflows/InProc/InProcessRunnerContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public async ValueTask SendMessageAsync(string sourceId, object message, string?
215215

216216
Debug.Assert(this._executors.ContainsKey(sourceId));
217217
Executor source = await this.EnsureExecutorAsync(sourceId, tracer: null, cancellationToken).ConfigureAwait(false);
218-
TypeId? declaredType = source.Protocol.SendTypeTranslator.GetDeclaredType(message.GetType(), allowPolymorphism: true);
218+
TypeId? declaredType = source.Protocol.SendTypeTranslator.GetDeclaredType(message.GetType());
219219
if (declaredType is null)
220220
{
221221
throw new InvalidOperationException($"Executor '{sourceId}' cannot send messages of type '{message.GetType().FullName}'.");

dotnet/src/Microsoft.Agents.AI.Workflows/ProtocolBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ internal ExecutorProtocol Build(ExecutorOptions options)
136136
}
137137

138138
HashSet<Type> yieldTypes = new(this._yieldTypes);
139-
if (options.AutoSendMessageHandlerResultObject)
139+
if (options.AutoYieldOutputHandlerResultObject)
140140
{
141141
yieldTypes.UnionWith(router.DefaultOutputTypes);
142142
}

dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.ComponentModel;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Text.Json;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -75,25 +76,23 @@ public override async ValueTask<HandoffState> HandleAsync(HandoffState message,
7576
{
7677
await AddUpdateAsync(update, cancellationToken).ConfigureAwait(false);
7778

78-
foreach (var c in update.Contents)
79+
foreach (var fcc in update.Contents.OfType<FunctionCallContent>()
80+
.Where(fcc => this._handoffFunctionNames.Contains(fcc.Name)))
7981
{
80-
if (c is FunctionCallContent fcc && this._handoffFunctionNames.Contains(fcc.Name))
81-
{
82-
requestedHandoff = fcc.Name;
83-
await AddUpdateAsync(
84-
new AgentResponseUpdate
85-
{
86-
AgentId = this._agent.Id,
87-
AuthorName = this._agent.Name ?? this._agent.Id,
88-
Contents = [new FunctionResultContent(fcc.CallId, "Transferred.")],
89-
CreatedAt = DateTimeOffset.UtcNow,
90-
MessageId = Guid.NewGuid().ToString("N"),
91-
Role = ChatRole.Tool,
92-
},
93-
cancellationToken
94-
)
95-
.ConfigureAwait(false);
96-
}
82+
requestedHandoff = fcc.Name;
83+
await AddUpdateAsync(
84+
new AgentResponseUpdate
85+
{
86+
AgentId = this._agent.Id,
87+
AuthorName = this._agent.Name ?? this._agent.Id,
88+
Contents = [new FunctionResultContent(fcc.CallId, "Transferred.")],
89+
CreatedAt = DateTimeOffset.UtcNow,
90+
MessageId = Guid.NewGuid().ToString("N"),
91+
Role = ChatRole.Tool,
92+
},
93+
cancellationToken
94+
)
95+
.ConfigureAwait(false);
9796
}
9897
}
9998

dotnet/tests/Microsoft.Agents.AI.Workflows.Generators.UnitTests/ExecutorRouteGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,9 @@ public partial class TestExecutor : Executor
848848
{
849849
public TestExecutor() : base("test") { }
850850
851-
protected override RouteBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder)
851+
protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder)
852852
{
853-
return routeBuilder;
853+
return protocolBuilder;
854854
}
855855
856856
[MessageHandler]

0 commit comments

Comments
 (0)