Skip to content

Commit 0f25ee6

Browse files
committed
Implement tool events > function call events for AG-UI support
Added two new case branches and three new converter methods: - ToolExecutionStartEvent → FunctionCallContent (the AGUI layer then emits TOOL_CALL_START/ARGS/END to the client) - ToolExecutionCompleteEvent → FunctionResultContent → TOOL_CALL_RESULT - Arguments are converted from JsonElement using AOT-safe JsonElement.EnumerateObject() — no reflection/dynamic code Fixes microsoft#4823
1 parent 628bb1a commit 0f25ee6

2 files changed

Lines changed: 311 additions & 0 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
185185
channel.Writer.TryWrite(this.ConvertToAgentResponseUpdate(usageEvent));
186186
break;
187187

188+
case ToolExecutionStartEvent toolStart:
189+
channel.Writer.TryWrite(this.ConvertToolStartToAgentResponseUpdate(toolStart));
190+
break;
191+
192+
case ToolExecutionCompleteEvent toolComplete:
193+
channel.Writer.TryWrite(this.ConvertToolCompleteToAgentResponseUpdate(toolComplete));
194+
break;
195+
188196
case SessionIdleEvent idleEvent:
189197
channel.Writer.TryWrite(this.ConvertToAgentResponseUpdate(idleEvent));
190198
channel.Writer.TryComplete();
@@ -430,6 +438,75 @@ private AgentResponseUpdate ConvertToAgentResponseUpdate(SessionEvent sessionEve
430438
};
431439
}
432440

441+
internal AgentResponseUpdate ConvertToolStartToAgentResponseUpdate(ToolExecutionStartEvent toolStart)
442+
{
443+
IDictionary<string, object?>? arguments = null;
444+
if (toolStart.Data?.Arguments is JsonElement jsonArgs)
445+
{
446+
arguments = ConvertJsonElementToArguments(jsonArgs);
447+
}
448+
449+
string toolName = toolStart.Data?.McpToolName ?? toolStart.Data?.ToolName ?? string.Empty;
450+
string callId = toolStart.Data?.ToolCallId ?? string.Empty;
451+
452+
FunctionCallContent functionCallContent = new(callId, toolName, arguments)
453+
{
454+
RawRepresentation = toolStart
455+
};
456+
457+
return new AgentResponseUpdate(ChatRole.Assistant, [functionCallContent])
458+
{
459+
AgentId = this.Id,
460+
MessageId = callId,
461+
CreatedAt = toolStart.Timestamp
462+
};
463+
}
464+
465+
internal AgentResponseUpdate ConvertToolCompleteToAgentResponseUpdate(ToolExecutionCompleteEvent toolComplete)
466+
{
467+
string callId = toolComplete.Data?.ToolCallId ?? string.Empty;
468+
object? result = toolComplete.Data?.Result?.Content
469+
?? toolComplete.Data?.Error?.Message;
470+
471+
FunctionResultContent functionResultContent = new(callId, result)
472+
{
473+
RawRepresentation = toolComplete
474+
};
475+
476+
return new AgentResponseUpdate(ChatRole.Tool, [functionResultContent])
477+
{
478+
AgentId = this.Id,
479+
MessageId = callId,
480+
CreatedAt = toolComplete.Timestamp
481+
};
482+
}
483+
484+
private static Dictionary<string, object?>? ConvertJsonElementToArguments(JsonElement element)
485+
{
486+
if (element.ValueKind != JsonValueKind.Object)
487+
{
488+
return null;
489+
}
490+
491+
Dictionary<string, object?> arguments = [];
492+
foreach (JsonProperty property in element.EnumerateObject())
493+
{
494+
arguments[property.Name] = property.Value.ValueKind switch
495+
{
496+
JsonValueKind.String => property.Value.GetString(),
497+
JsonValueKind.True => true,
498+
JsonValueKind.False => false,
499+
JsonValueKind.Null => null,
500+
JsonValueKind.Number => property.Value.TryGetInt64(out long l)
501+
? (object?)l
502+
: property.Value.GetDouble(),
503+
_ => property.Value.GetRawText()
504+
};
505+
}
506+
507+
return arguments;
508+
}
509+
433510
private static SessionConfig? GetSessionConfig(IList<AITool>? tools, string? instructions)
434511
{
435512
List<AIFunction>? mappedTools = tools is { Count: > 0 } ? tools.OfType<AIFunction>().ToList() : null;

‎dotnet/tests/Microsoft.Agents.AI.GitHub.Copilot.UnitTests/GitHubCopilotAgentTests.cs‎

Lines changed: 234 additions & 0 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.Json;
56
using System.Threading.Tasks;
67
using GitHub.Copilot.SDK;
78
using Microsoft.Extensions.AI;
@@ -243,4 +244,237 @@ public void ConvertToAgentResponseUpdate_AssistantMessageEvent_DoesNotEmitTextCo
243244
Assert.Empty(result.Text);
244245
Assert.DoesNotContain(result.Contents, c => c is TextContent);
245246
}
247+
248+
[Fact]
249+
public void ConvertToolStartToAgentResponseUpdate_WithMcpToolName_ReturnsFunctionCallContent()
250+
{
251+
// Arrange
252+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
253+
const string AgentId = "agent-id";
254+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: AgentId, tools: null);
255+
var timestamp = DateTimeOffset.UtcNow;
256+
var toolStart = new ToolExecutionStartEvent
257+
{
258+
Data = new ToolExecutionStartData
259+
{
260+
ToolCallId = "call-123",
261+
ToolName = "fallback_tool",
262+
McpToolName = "mcp_tool",
263+
Arguments = JsonSerializer.SerializeToElement(new { param1 = "value1", count = 42 })
264+
},
265+
Timestamp = timestamp
266+
};
267+
268+
// Act
269+
AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart);
270+
271+
// Assert
272+
Assert.Equal(ChatRole.Assistant, result.Role);
273+
Assert.Equal(AgentId, result.AgentId);
274+
Assert.Equal("call-123", result.MessageId);
275+
Assert.Equal(timestamp, result.CreatedAt);
276+
FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents));
277+
Assert.Equal("call-123", content.CallId);
278+
Assert.Equal("mcp_tool", content.Name);
279+
Assert.NotNull(content.Arguments);
280+
Assert.Equal("value1", content.Arguments["param1"]);
281+
Assert.Equal(42L, content.Arguments["count"]);
282+
Assert.Same(toolStart, content.RawRepresentation);
283+
}
284+
285+
[Fact]
286+
public void ConvertToolStartToAgentResponseUpdate_WithToolNameFallback_UsesToolName()
287+
{
288+
// Arrange
289+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
290+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
291+
var toolStart = new ToolExecutionStartEvent
292+
{
293+
Data = new ToolExecutionStartData
294+
{
295+
ToolCallId = "call-456",
296+
ToolName = "local_tool",
297+
}
298+
};
299+
300+
// Act
301+
AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart);
302+
303+
// Assert
304+
FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents));
305+
Assert.Equal("local_tool", content.Name);
306+
Assert.Null(content.Arguments);
307+
}
308+
309+
[Fact]
310+
public void ConvertToolStartToAgentResponseUpdate_WithNonObjectJsonArguments_ReturnsNullArguments()
311+
{
312+
// Arrange
313+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
314+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
315+
var toolStart = new ToolExecutionStartEvent
316+
{
317+
Data = new ToolExecutionStartData
318+
{
319+
ToolCallId = "call-789",
320+
ToolName = "some_tool",
321+
Arguments = JsonSerializer.SerializeToElement("just a string")
322+
}
323+
};
324+
325+
// Act
326+
AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart);
327+
328+
// Assert
329+
FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents));
330+
Assert.Null(content.Arguments);
331+
}
332+
333+
[Fact]
334+
public void ConvertToolStartToAgentResponseUpdate_WithAllJsonValueKinds_ConvertsCorrectly()
335+
{
336+
// Arrange
337+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
338+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
339+
var toolStart = new ToolExecutionStartEvent
340+
{
341+
Data = new ToolExecutionStartData
342+
{
343+
ToolCallId = "call-kinds",
344+
ToolName = "multi_type_tool",
345+
Arguments = JsonSerializer.SerializeToElement(new
346+
{
347+
strVal = "hello",
348+
boolTrue = true,
349+
boolFalse = false,
350+
nullVal = (string?)null,
351+
intVal = 100,
352+
floatVal = 3.14,
353+
objVal = new { nested = "value" }
354+
})
355+
}
356+
};
357+
358+
// Act
359+
AgentResponseUpdate result = agent.ConvertToolStartToAgentResponseUpdate(toolStart);
360+
361+
// Assert
362+
FunctionCallContent content = Assert.IsType<FunctionCallContent>(Assert.Single(result.Contents));
363+
Assert.NotNull(content.Arguments);
364+
Assert.Equal("hello", content.Arguments["strVal"]);
365+
Assert.Equal(true, content.Arguments["boolTrue"]);
366+
Assert.Equal(false, content.Arguments["boolFalse"]);
367+
Assert.Null(content.Arguments["nullVal"]);
368+
Assert.Equal(100L, content.Arguments["intVal"]);
369+
Assert.Equal(3.14, (double)content.Arguments["floatVal"]!, 2);
370+
// Non-primitive values fall back to raw JSON text
371+
Assert.IsType<string>(content.Arguments["objVal"]);
372+
}
373+
374+
[Fact]
375+
public void ConvertToolCompleteToAgentResponseUpdate_WithResult_ReturnsFunctionResultContent()
376+
{
377+
// Arrange
378+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
379+
const string AgentId = "agent-id";
380+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: AgentId, tools: null);
381+
var timestamp = DateTimeOffset.UtcNow;
382+
var toolComplete = new ToolExecutionCompleteEvent
383+
{
384+
Data = new ToolExecutionCompleteData
385+
{
386+
ToolCallId = "call-123",
387+
Success = true,
388+
Result = new ToolExecutionCompleteDataResult { Content = "tool output" }
389+
},
390+
Timestamp = timestamp
391+
};
392+
393+
// Act
394+
AgentResponseUpdate result = agent.ConvertToolCompleteToAgentResponseUpdate(toolComplete);
395+
396+
// Assert
397+
Assert.Equal(ChatRole.Tool, result.Role);
398+
Assert.Equal(AgentId, result.AgentId);
399+
Assert.Equal("call-123", result.MessageId);
400+
Assert.Equal(timestamp, result.CreatedAt);
401+
FunctionResultContent content = Assert.IsType<FunctionResultContent>(Assert.Single(result.Contents));
402+
Assert.Equal("call-123", content.CallId);
403+
Assert.Equal("tool output", content.Result);
404+
Assert.Same(toolComplete, content.RawRepresentation);
405+
}
406+
407+
[Fact]
408+
public void ConvertToolCompleteToAgentResponseUpdate_WithError_ReturnsErrorMessage()
409+
{
410+
// Arrange
411+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
412+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
413+
var toolComplete = new ToolExecutionCompleteEvent
414+
{
415+
Data = new ToolExecutionCompleteData
416+
{
417+
ToolCallId = "call-err",
418+
Success = false,
419+
Error = new ToolExecutionCompleteDataError { Message = "Something went wrong" }
420+
}
421+
};
422+
423+
// Act
424+
AgentResponseUpdate result = agent.ConvertToolCompleteToAgentResponseUpdate(toolComplete);
425+
426+
// Assert
427+
FunctionResultContent content = Assert.IsType<FunctionResultContent>(Assert.Single(result.Contents));
428+
Assert.Equal("call-err", content.CallId);
429+
Assert.Equal("Something went wrong", content.Result);
430+
}
431+
432+
[Fact]
433+
public void ConvertToolCompleteToAgentResponseUpdate_ResultTakesPrecedenceOverError()
434+
{
435+
// Arrange
436+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
437+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
438+
var toolComplete = new ToolExecutionCompleteEvent
439+
{
440+
Data = new ToolExecutionCompleteData
441+
{
442+
ToolCallId = "call-both",
443+
Success = true,
444+
Result = new ToolExecutionCompleteDataResult { Content = "actual result" },
445+
Error = new ToolExecutionCompleteDataError { Message = "should be ignored" }
446+
}
447+
};
448+
449+
// Act
450+
AgentResponseUpdate result = agent.ConvertToolCompleteToAgentResponseUpdate(toolComplete);
451+
452+
// Assert
453+
FunctionResultContent content = Assert.IsType<FunctionResultContent>(Assert.Single(result.Contents));
454+
Assert.Equal("actual result", content.Result);
455+
}
456+
457+
[Fact]
458+
public void ConvertToolCompleteToAgentResponseUpdate_WithNoResultOrError_ReturnsNullResult()
459+
{
460+
// Arrange
461+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
462+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
463+
var toolComplete = new ToolExecutionCompleteEvent
464+
{
465+
Data = new ToolExecutionCompleteData
466+
{
467+
ToolCallId = "call-empty",
468+
Success = true
469+
}
470+
};
471+
472+
// Act
473+
AgentResponseUpdate result = agent.ConvertToolCompleteToAgentResponseUpdate(toolComplete);
474+
475+
// Assert
476+
FunctionResultContent content = Assert.IsType<FunctionResultContent>(Assert.Single(result.Contents));
477+
Assert.Equal("call-empty", content.CallId);
478+
Assert.Null(content.Result);
479+
}
246480
}

0 commit comments

Comments
 (0)