From 18299bc7014b19fa1abf013bc2bf68c8161e9e10 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 11 Jul 2025 21:35:10 -0400 Subject: [PATCH] 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> --- .../Handoff/HandoffActor.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs b/dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs index da7fca1f42..4bca4984d2 100644 --- a/dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs +++ b/dotnet/src/Microsoft.Agents.Orchestration/Handoff/HandoffActor.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; @@ -14,7 +15,7 @@ namespace Microsoft.Agents.Orchestration; /// /// An actor used with the . /// -internal sealed class HandoffActor : AgentActor +internal sealed partial class HandoffActor : AgentActor { private readonly ChatClientAgent _chatAgent; private readonly HandoffLookup _handoffs; @@ -155,7 +156,7 @@ internal sealed class HandoffActor : AgentActor AIFunction handoffFunction = AIFunctionFactory.Create( () => this.Handoff(handoff.Key), - name: $"transfer_to_{handoff.Key}", + name: $"transfer_to_{InvalidNameCharsRegex().Replace(handoff.Key, "_")}", description: handoff.Value.Description); yield return handoffFunction; @@ -181,4 +182,13 @@ internal sealed class HandoffActor : AgentActor FunctionInvokingChatClient.CurrentContext.Terminate = true; } } + + /// Regex that flags any character other than ASCII digits or letters or the underscore. +#if NET + [GeneratedRegex("[^0-9A-Za-z_]+")] + private static partial Regex InvalidNameCharsRegex(); +#else + private static Regex InvalidNameCharsRegex() => s_invalidNameCharsRegex; + private static readonly Regex s_invalidNameCharsRegex = new("[^0-9A-Za-z_]+", RegexOptions.Compiled); +#endif }