-
Notifications
You must be signed in to change notification settings - Fork 1.3k
.NET: Implement tool events > function call events for AG-UI support #4824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||||
|
|
||||||||||||
| using System; | ||||||||||||
| using System.Collections.Generic; | ||||||||||||
| using System.Text.Json; | ||||||||||||
| using System.Threading.Tasks; | ||||||||||||
| using GitHub.Copilot.SDK; | ||||||||||||
| using Microsoft.Extensions.AI; | ||||||||||||
|
|
@@ -243,4 +244,237 @@ public void ConvertToAgentResponseUpdate_AssistantMessageEvent_DoesNotEmitTextCo | |||||||||||
| Assert.Empty(result.Text); | ||||||||||||
| Assert.DoesNotContain(result.Contents, c => c is TextContent); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Fact] | ||||||||||||
| public void ConvertToolStartToAgentResponseUpdate_WithMcpToolName_ReturnsFunctionCallContent() | ||||||||||||
| { | ||||||||||||
| // Arrange | ||||||||||||
| CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false }); | ||||||||||||
| const string AgentId = "agent-id"; | ||||||||||||
| var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: AgentId, tools: null); | ||||||||||||
| var timestamp = DateTimeOffset.UtcNow; | ||||||||||||
| var toolStart = new ToolExecutionStartEvent | ||||||||||||
| { | ||||||||||||
| Data = new ToolExecutionStartData | ||||||||||||
| { | ||||||||||||
| ToolCallId = "call-123", | ||||||||||||
| ToolName = "fallback_tool", | ||||||||||||
| McpToolName = "mcp_tool", | ||||||||||||
| Arguments = JsonSerializer.SerializeToElement(new { param1 = "value1", count = 42 }) | ||||||||||||
| }, | ||||||||||||
| Timestamp = timestamp | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| Assert.Equal(ChatRole.Assistant, result.Role); | ||||||||||||
| Assert.Equal(AgentId, result.AgentId); | ||||||||||||
| Assert.Equal("call-123", result.MessageId); | ||||||||||||
| Assert.Equal(timestamp, result.CreatedAt); | ||||||||||||
| FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents)); | ||||||||||||
| Assert.Equal("call-123", content.CallId); | ||||||||||||
| Assert.Equal("mcp_tool", content.Name); | ||||||||||||
| Assert.NotNull(content.Arguments); | ||||||||||||
| Assert.Equal("value1", content.Arguments["param1"]); | ||||||||||||
| Assert.Equal(42L, content.Arguments["count"]); | ||||||||||||
| Assert.Same(toolStart, content.RawRepresentation); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Fact] | ||||||||||||
| public void ConvertToolStartToAgentResponseUpdate_WithToolNameFallback_UsesToolName() | ||||||||||||
| { | ||||||||||||
| // Arrange | ||||||||||||
| CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false }); | ||||||||||||
| var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); | ||||||||||||
| var toolStart = new ToolExecutionStartEvent | ||||||||||||
| { | ||||||||||||
| Data = new ToolExecutionStartData | ||||||||||||
| { | ||||||||||||
| ToolCallId = "call-456", | ||||||||||||
| ToolName = "local_tool", | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents)); | ||||||||||||
| Assert.Equal("local_tool", content.Name); | ||||||||||||
| Assert.Null(content.Arguments); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Fact] | ||||||||||||
| public void ConvertToolStartToAgentResponseUpdate_WithNonObjectJsonArguments_ReturnsNullArguments() | ||||||||||||
| { | ||||||||||||
| // Arrange | ||||||||||||
| CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false }); | ||||||||||||
| var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); | ||||||||||||
| var toolStart = new ToolExecutionStartEvent | ||||||||||||
| { | ||||||||||||
| Data = new ToolExecutionStartData | ||||||||||||
| { | ||||||||||||
| ToolCallId = "call-789", | ||||||||||||
| ToolName = "some_tool", | ||||||||||||
| Arguments = JsonSerializer.SerializeToElement("just a string") | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents)); | ||||||||||||
| Assert.Null(content.Arguments); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Fact] | ||||||||||||
| public void ConvertToolStartToAgentResponseUpdate_WithAllJsonValueKinds_ConvertsCorrectly() | ||||||||||||
| { | ||||||||||||
| // Arrange | ||||||||||||
| CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false }); | ||||||||||||
| var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); | ||||||||||||
| var toolStart = new ToolExecutionStartEvent | ||||||||||||
| { | ||||||||||||
| Data = new ToolExecutionStartData | ||||||||||||
| { | ||||||||||||
| ToolCallId = "call-kinds", | ||||||||||||
| ToolName = "multi_type_tool", | ||||||||||||
| Arguments = JsonSerializer.SerializeToElement(new | ||||||||||||
| { | ||||||||||||
| strVal = "hello", | ||||||||||||
| boolTrue = true, | ||||||||||||
| boolFalse = false, | ||||||||||||
| nullVal = (string?)null, | ||||||||||||
| intVal = 100, | ||||||||||||
| floatVal = 3.14, | ||||||||||||
| objVal = new { nested = "value" } | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents)); | ||||||||||||
| Assert.NotNull(content.Arguments); | ||||||||||||
| Assert.Equal("hello", content.Arguments["strVal"]); | ||||||||||||
| Assert.Equal(true, content.Arguments["boolTrue"]); | ||||||||||||
| Assert.Equal(false, content.Arguments["boolFalse"]); | ||||||||||||
| Assert.Null(content.Arguments["nullVal"]); | ||||||||||||
| Assert.Equal(100L, content.Arguments["intVal"]); | ||||||||||||
| Assert.Equal(3.14, (double)content.Arguments["floatVal"]!, 2); | ||||||||||||
| // Non-primitive values fall back to raw JSON text | ||||||||||||
| Assert.IsType<string>(content.Arguments["objVal"]); | ||||||||||||
|
Comment on lines
+370
to
+371
|
||||||||||||
| // Non-primitive values fall back to raw JSON text | |
| Assert.IsType<string>(content.Arguments["objVal"]); | |
| // Non-primitive values are preserved as structured JSON | |
| var objValElement = Assert.IsType<JsonElement>(content.Arguments["objVal"]); | |
| Assert.Equal("value", objValElement.GetProperty("nested").GetString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConvertJsonElementToArgumentscurrently converts non-primitive argument values (e.g., JsonValueKind.Object/Array) usingGetRawText(), which turns them into JSON strings. When these arguments are later serialized (e.g., into AG-UI ToolCallArgs events), nested objects/arrays will be double-encoded instead of remaining structured JSON. Prefer preserving structured values by returningJsonElement(e.g.,property.Value.Clone()) for Object/Array (and consider how to handle Undefined), so downstream serialization emits proper JSON objects/arrays.