Skip to content

Commit 18299bc

Browse files
stephentoubCopilot
andauthored
Fix handoff function names (#178)
* Fix handoff function names Handoffs are including the agent name in the function name. But the agent name can include characters that are invalid for a function name, which results in errors. Replace them. * Update dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 94e00bd commit 18299bc

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Text.RegularExpressions;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.Extensions.AI;
@@ -14,7 +15,7 @@ namespace Microsoft.Agents.Orchestration;
1415
/// <summary>
1516
/// An actor used with the <see cref="HandoffOrchestration{TInput,TOutput}"/>.
1617
/// </summary>
17-
internal sealed class HandoffActor : AgentActor
18+
internal sealed partial class HandoffActor : AgentActor
1819
{
1920
private readonly ChatClientAgent _chatAgent;
2021
private readonly HandoffLookup _handoffs;
@@ -155,7 +156,7 @@ private IEnumerable<AIFunction> CreateHandoffFunctions()
155156
AIFunction handoffFunction =
156157
AIFunctionFactory.Create(
157158
() => this.Handoff(handoff.Key),
158-
name: $"transfer_to_{handoff.Key}",
159+
name: $"transfer_to_{InvalidNameCharsRegex().Replace(handoff.Key, "_")}",
159160
description: handoff.Value.Description);
160161

161162
yield return handoffFunction;
@@ -181,4 +182,13 @@ private async ValueTask EndAsync(string summary, CancellationToken cancellationT
181182
FunctionInvokingChatClient.CurrentContext.Terminate = true;
182183
}
183184
}
185+
186+
/// <summary>Regex that flags any character other than ASCII digits or letters or the underscore.</summary>
187+
#if NET
188+
[GeneratedRegex("[^0-9A-Za-z_]+")]
189+
private static partial Regex InvalidNameCharsRegex();
190+
#else
191+
private static Regex InvalidNameCharsRegex() => s_invalidNameCharsRegex;
192+
private static readonly Regex s_invalidNameCharsRegex = new("[^0-9A-Za-z_]+", RegexOptions.Compiled);
193+
#endif
184194
}

0 commit comments

Comments
 (0)