Skip to content

Commit ae684aa

Browse files
committed
Cleanup
1 parent b112cc5 commit ae684aa

File tree

9 files changed

+40
-223
lines changed

9 files changed

+40
-223
lines changed

dotnet/src/Microsoft.Agents.AI.AGUI/AGUIChatClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public AGUIChatClient(
4444
{
4545
}
4646

47-
private static IChatClient CreateInnerClient(
47+
private static FunctionInvokingChatClient CreateInnerClient(
4848
HttpClient httpClient,
4949
string endpoint,
5050
JsonSerializerOptions jsonSerializerOptions,

dotnet/src/Microsoft.Agents.AI.AGUI/Microsoft.Agents.AI.AGUI.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
<PropertyGroup>
1313
<InjectSharedThrow>true</InjectSharedThrow>
14-
<!-- CA1859: We intentionally use IChatClient for flexibility in middleware composition -->
15-
<NoWarn>$(NoWarn);CA1859</NoWarn>
1614
</PropertyGroup>
1715

1816
<PropertyGroup>

dotnet/src/Microsoft.Agents.AI.AGUI/Shared/ToolCallResultEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public ToolCallResultEvent()
1515
this.Type = AGUIEventTypes.ToolCallResult;
1616
}
1717

18-
[JsonPropertyName("message_id")]
18+
[JsonPropertyName("messageId")]
1919
public string? MessageId { get; set; }
2020

21-
[JsonPropertyName("tool_call_id")]
21+
[JsonPropertyName("toolCallId")]
2222
public string ToolCallId { get; set; } = string.Empty;
2323

2424
[JsonPropertyName("content")]

dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/AGUIEndpointRouteBuilderExtensions.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,40 @@ public static IEndpointConventionBuilder MapAGUI(
4343
return Results.BadRequest();
4444
}
4545

46-
var messages = input.Messages.AsChatMessages(AGUIJsonSerializerContext.Default.Options);
47-
logger.LogInformation("[MapAGUI] Received request - ThreadId: {ThreadId}, RunId: {RunId}, MessageCount: {MessageCount}",
48-
input.ThreadId, input.RunId, messages.Count());
46+
var jsonSerializerOptions = context.RequestServices.GetService<JsonSerializerOptions>() ??
47+
AGUIJsonSerializerContext.Default.Options;
4948

50-
for (int i = 0; i < messages.Count(); i++)
51-
{
52-
var msg = messages.ElementAt(i);
53-
logger.LogDebug("[MapAGUI] Message[{Index}]: Role={Role}, ContentCount={ContentCount}",
54-
i, msg.Role.Value, msg.Contents.Count);
49+
var messages = input.Messages.AsChatMessages(jsonSerializerOptions);
50+
var agent = aiAgent;
5551

56-
foreach (var content in msg.Contents)
52+
ChatClientAgentRunOptions? runOptions = null;
53+
if (input.Tools?.Any() == true)
54+
{
55+
runOptions = new ChatClientAgentRunOptions
5756
{
58-
if (content is FunctionCallContent fcc)
59-
{
60-
logger.LogDebug("[MapAGUI] - FunctionCallContent: Name={Name}, CallId={CallId}",
61-
fcc.Name, fcc.CallId);
62-
}
63-
else if (content is FunctionResultContent frc)
57+
ChatOptions = new ChatOptions
6458
{
65-
logger.LogDebug("[MapAGUI] - FunctionResultContent: CallId={CallId}, Result={Result}",
66-
frc.CallId, frc.Result);
59+
Tools = input.Tools.AsAITools().ToList()
6760
}
68-
else
69-
{
70-
logger.LogDebug("[MapAGUI] - {ContentType}", content.GetType().Name);
71-
}
72-
}
61+
};
7362
}
7463

75-
var agent = aiAgent;
76-
7764
logger.LogInformation("[MapAGUI] Starting agent.RunStreamingAsync for ThreadId: {ThreadId}, RunId: {RunId}",
7865
input.ThreadId, input.RunId);
7966

8067
var events = agent.RunStreamingAsync(
8168
messages,
69+
options: runOptions,
8270
cancellationToken: cancellationToken)
8371
.AsChatResponseUpdatesAsync()
8472
.AsAGUIEventStreamAsync(
8573
input.ThreadId,
8674
input.RunId,
75+
jsonSerializerOptions,
8776
cancellationToken);
8877

8978
var sseLogger = context.RequestServices.GetRequiredService<ILogger<AGUIServerSentEventsResult>>();
90-
await new AGUIServerSentEventsResult(events, sseLogger).ExecuteAsync(context).ConfigureAwait(false);
79+
return new AGUIServerSentEventsResult(events, sseLogger);
9180
});
9281
}
9382
}

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ public async Task ClientReceivesStreamedAssistantMessageAsync()
2929
{
3030
// Arrange
3131
await this.SetupTestServerAsync();
32-
<<<<<<< HEAD
33-
AGUIAgent agent = new("assistant", "Sample assistant", this._client!, "", null, []);
34-
=======
3532
var chatClient = new AGUIChatClient(this._client!, "", null);
3633
AIAgent agent = chatClient.CreateAIAgent(instructions: null, name: "assistant", description: "Sample assistant", tools: []);
37-
>>>>>>> 60969b4e (Cleanup)
3834
AgentThread thread = agent.GetNewThread();
3935
ChatMessage userMessage = new(ChatRole.User, "hello");
4036

@@ -473,5 +469,5 @@ public FakeInMemoryAgentThread(JsonElement serializedThread, JsonSerializerOptio
473469
}
474470
}
475471

476-
public object? GetService(Type serviceType, object? serviceKey = null) => null;
472+
public override object? GetService(Type serviceType, object? serviceKey = null) => null;
477473
}

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/ToolCallingTests.cs

Lines changed: 20 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -538,58 +538,24 @@ private async Task SetupTestServerWithAzureOpenAIAsync(string endpoint, string d
538538

539539
this._app = builder.Build();
540540

541-
this._app.MapAGUIAgent("/agent", (IEnumerable<ChatMessage> messages, IEnumerable<AITool> tools) =>
542-
{
543-
var clientTools = tools?.ToList() ?? [];
544-
var messagesList = messages?.ToList() ?? [];
545-
var serverToolsList = serverTools?.ToList() ?? [];
546-
547-
this._output.WriteLine($"[Server Factory] Client tools count: {clientTools.Count}");
548-
this._output.WriteLine($"[Server Factory] Server tools count: {serverToolsList.Count}");
549-
this._output.WriteLine($"[Server Factory] Messages count: {messagesList.Count}");
550-
for (int i = 0; i < messagesList.Count; i++)
551-
{
552-
var msg = messagesList[i];
553-
this._output.WriteLine($" Message[{i}]: Role={msg.Role}, ContentCount={msg.Contents.Count}");
554-
foreach (var content in msg.Contents)
555-
{
556-
if (content is FunctionCallContent fcc)
557-
{
558-
this._output.WriteLine($" - FunctionCallContent: {fcc.Name} (CallId: {fcc.CallId})");
559-
}
560-
else if (content is FunctionResultContent frc)
561-
{
562-
this._output.WriteLine($" - FunctionResultContent: CallId={frc.CallId}, Result={frc.Result}");
563-
}
564-
else if (content is TextContent tc)
565-
{
566-
this._output.WriteLine($" - TextContent: {tc.Text}");
567-
}
568-
else
569-
{
570-
this._output.WriteLine($" - {content.GetType().Name}");
571-
}
572-
}
573-
}
574-
575-
var azureOpenAIClient = new AzureOpenAIClient(
576-
new Uri(endpoint),
577-
new DefaultAzureCredential());
578-
579-
var chatClient = azureOpenAIClient
580-
.GetChatClient(deploymentName)
581-
.AsIChatClient();
582-
583-
// DO NOT pass client tools to Azure OpenAI
584-
// Client tools should only be executed on the client side
585-
// Pass only server-side tools to Azure OpenAI
586-
return chatClient.CreateAIAgent(
587-
instructions: null,
588-
name: "azure-openai-agent",
589-
description: "An agent using Azure OpenAI",
590-
tools: serverToolsList);
591-
});
541+
var azureOpenAIClient = new AzureOpenAIClient(
542+
new Uri(endpoint),
543+
new DefaultAzureCredential());
544+
545+
var chatClient = azureOpenAIClient
546+
.GetChatClient(deploymentName)
547+
.AsIChatClient();
548+
549+
// DO NOT pass client tools to Azure OpenAI
550+
// Client tools should only be executed on the client side
551+
// Pass only server-side tools to Azure OpenAI
552+
var agent = chatClient.CreateAIAgent(
553+
instructions: null,
554+
name: "azure-openai-agent",
555+
description: "An agent using Azure OpenAI",
556+
tools: serverTools);
592557

558+
this._app.MapAGUI("/agent", agent);
593559
await this._app.StartAsync();
594560

595561
TestServer testServer = this._app.Services.GetRequiredService<IServer>() as TestServer
@@ -605,39 +571,9 @@ private async Task SetupTestServerAsync(IList<AITool>? serverTools = null, bool
605571
builder.WebHost.UseTestServer();
606572

607573
this._app = builder.Build();
608-
609-
this._app.MapAGUIAgent("/agent", (IEnumerable<ChatMessage> messages, IEnumerable<AITool> tools) =>
610-
{
611-
// tools = client tools from AG-UI request (sent as declarations, not executable)
612-
// serverTools = actual executable server tools
613-
var clientTools = tools?.ToList() ?? [];
614-
var serverToolsList = serverTools?.ToList() ?? [];
615-
616-
this._output.WriteLine($"[Server Factory] Client tools count: {clientTools.Count}");
617-
this._output.WriteLine($"[Server Factory] Server tools count: {serverToolsList.Count}");
618-
619-
foreach (var tool in serverToolsList)
620-
{
621-
var name = tool is AIFunctionDeclaration decl ? decl.Name : "Unknown";
622-
this._output.WriteLine($"[Server Factory] Server tool: {tool?.GetType().Name} - {name}");
623-
}
624-
625-
foreach (var tool in clientTools)
626-
{
627-
var name = tool is AIFunctionDeclaration decl ? decl.Name : "Unknown";
628-
this._output.WriteLine($"[Server Factory] Client tool: {tool?.GetType().Name} - {name}");
629-
}
630-
631-
// FunctionInvokingChatClient will terminate if it sees ANY non-invocable AIFunctionDeclaration
632-
// So we must ONLY pass executable server tools to CreateAIAgent
633-
// The FakeChatClient needs to know about ALL tools (server + client) to generate proper function calls
634-
var allTools = serverToolsList.Concat(clientTools).ToList();
635-
636-
var fakeChatClient = new FakeToolCallingChatClient(triggerParallelCalls, this._output, allTools);
637-
// Pass ONLY executable server tools - client tool declarations would cause FunctionInvokingChatClient to terminate
638-
this._output.WriteLine($"[Server Factory] Passing {serverToolsList.Count} server tools to CreateAIAgent");
639-
return fakeChatClient.CreateAIAgent(instructions: null, name: "fake-agent", description: "A fake agent for tool testing", tools: serverToolsList);
640-
});
574+
var fakeChatClient = new FakeToolCallingChatClient(triggerParallelCalls, this._output);
575+
AIAgent baseAgent = fakeChatClient.CreateAIAgent(instructions: null, name: "base-agent", description: "A base agent for tool testing", tools: serverTools ?? []);
576+
this._app.MapAGUI("/agent", baseAgent);
641577

642578
await this._app.StartAsync();
643579

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/test-output.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/test-output2.txt

Lines changed: 0 additions & 80 deletions
This file was deleted.

dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private RequestDelegate CreateRequestDelegate(
217217
return;
218218
}
219219

220-
IEnumerable<ChatMessage> messages = input.Messages.AsChatMessages();
220+
IEnumerable<ChatMessage> messages = input.Messages.AsChatMessages(AGUIJsonSerializerContext.Default.Options);
221221
IEnumerable<KeyValuePair<string, string>> contextValues = input.Context;
222222
JsonElement forwardedProps = input.ForwardedProperties;
223223
AIAgent agent = factory(messages, [], contextValues, forwardedProps);
@@ -229,6 +229,7 @@ private RequestDelegate CreateRequestDelegate(
229229
.AsAGUIEventStreamAsync(
230230
input.ThreadId,
231231
input.RunId,
232+
AGUIJsonSerializerContext.Default.Options,
232233
cancellationToken);
233234

234235
ILogger<AGUIServerSentEventsResult> logger = NullLogger<AGUIServerSentEventsResult>.Instance;

0 commit comments

Comments
 (0)