Skip to content

Commit 18220be

Browse files
CopilotCopilot
andcommitted
fix(#5897): Address review feedback for ParseArguments robustness
- Handle non-generic IDictionary variants (Hashtable, etc.) that don't match IDictionary<string, object?> due to generic invariance - Return null for empty/whitespace string arguments instead of wrapping them in a spurious { value = "" } dictionary, aligning with ParseFunctionArgumentsObject convention elsewhere in the repo - Add test coverage for Dictionary, Hashtable, and JsonElement argument types - Add edge-case test for Success=true with null Result Co-authored-by: Copilot <[email protected]>
1 parent bf011c9 commit 18220be

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,26 @@ internal AgentResponseUpdate ConvertToAgentResponseUpdate(ToolExecutionCompleteE
424424
return dict;
425425
}
426426

427+
// Catch dictionary variants that don't match IDictionary<string, object?> due to
428+
// generic invariance (e.g., IDictionary<string, object>, custom dictionary types).
429+
if (arguments is System.Collections.IDictionary nonGenericDict)
430+
{
431+
var result = new Dictionary<string, object?>();
432+
foreach (System.Collections.DictionaryEntry entry in nonGenericDict)
433+
{
434+
result[entry.Key?.ToString() ?? string.Empty] = entry.Value;
435+
}
436+
437+
return result;
438+
}
439+
427440
if (arguments is string jsonString)
428441
{
442+
if (string.IsNullOrWhiteSpace(jsonString))
443+
{
444+
return null;
445+
}
446+
429447
try
430448
{
431449
return JsonSerializer.Deserialize<Dictionary<string, object?>>(jsonString);

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

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Text.Json;
36
using GitHub.Copilot.SDK;
47
using Microsoft.Extensions.AI;
58

@@ -262,4 +265,151 @@ public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithMultipleArg
262265
Assert.Equal("10", functionCall.Arguments!["limit"]?.ToString());
263266
Assert.Equal("active=true", functionCall.Arguments!["filter"]?.ToString());
264267
}
268+
269+
[Fact]
270+
public void ConvertToAgentResponseUpdate_ToolExecutionCompleteEvent_WithSuccessButNullResult_ProducesNullResult()
271+
{
272+
// Arrange
273+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
274+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
275+
276+
var completeEvent = new ToolExecutionCompleteEvent
277+
{
278+
Data = new ToolExecutionCompleteData
279+
{
280+
ToolCallId = "call-null-result",
281+
Success = true,
282+
Result = null
283+
}
284+
};
285+
286+
// Act
287+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(completeEvent);
288+
289+
// Assert
290+
var content = Assert.Single(result.Contents);
291+
var functionResult = Assert.IsType<FunctionResultContent>(content);
292+
Assert.Equal("call-null-result", functionResult.CallId);
293+
Assert.Null(functionResult.Result);
294+
}
295+
296+
[Fact]
297+
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithDictionaryArguments_ParsesDirectly()
298+
{
299+
// Arrange
300+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
301+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
302+
303+
var args = new Dictionary<string, object?> { ["path"] = "/tmp/file.txt", ["encoding"] = "utf-8" };
304+
var startEvent = new ToolExecutionStartEvent
305+
{
306+
Data = new ToolExecutionStartData
307+
{
308+
ToolCallId = "call-dict",
309+
ToolName = "readFile",
310+
Arguments = args
311+
}
312+
};
313+
314+
// Act
315+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent);
316+
317+
// Assert
318+
var content = Assert.Single(result.Contents);
319+
var functionCall = Assert.IsType<FunctionCallContent>(content);
320+
Assert.Equal("call-dict", functionCall.CallId);
321+
Assert.NotNull(functionCall.Arguments);
322+
Assert.Equal("/tmp/file.txt", functionCall.Arguments!["path"]?.ToString());
323+
Assert.Equal("utf-8", functionCall.Arguments!["encoding"]?.ToString());
324+
}
325+
326+
[Fact]
327+
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithNonGenericDictionary_ParsesViaEnumeration()
328+
{
329+
// Arrange
330+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
331+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
332+
333+
var args = new Hashtable { ["key1"] = "value1", ["key2"] = 42 };
334+
var startEvent = new ToolExecutionStartEvent
335+
{
336+
Data = new ToolExecutionStartData
337+
{
338+
ToolCallId = "call-hashtable",
339+
ToolName = "processTool",
340+
Arguments = args
341+
}
342+
};
343+
344+
// Act
345+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent);
346+
347+
// Assert
348+
var content = Assert.Single(result.Contents);
349+
var functionCall = Assert.IsType<FunctionCallContent>(content);
350+
Assert.Equal("call-hashtable", functionCall.CallId);
351+
Assert.NotNull(functionCall.Arguments);
352+
Assert.Equal("value1", functionCall.Arguments!["key1"]?.ToString());
353+
Assert.Equal("42", functionCall.Arguments!["key2"]?.ToString());
354+
}
355+
356+
[Fact]
357+
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithJsonElement_ParsesArguments()
358+
{
359+
// Arrange
360+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
361+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
362+
363+
var jsonDoc = JsonDocument.Parse("{\"host\":\"localhost\",\"port\":8080}");
364+
var startEvent = new ToolExecutionStartEvent
365+
{
366+
Data = new ToolExecutionStartData
367+
{
368+
ToolCallId = "call-json-element",
369+
ToolName = "connect",
370+
Arguments = jsonDoc.RootElement
371+
}
372+
};
373+
374+
// Act
375+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent);
376+
377+
// Assert
378+
var content = Assert.Single(result.Contents);
379+
var functionCall = Assert.IsType<FunctionCallContent>(content);
380+
Assert.Equal("call-json-element", functionCall.CallId);
381+
Assert.NotNull(functionCall.Arguments);
382+
Assert.Equal("localhost", functionCall.Arguments!["host"]?.ToString());
383+
Assert.Equal("8080", functionCall.Arguments!["port"]?.ToString());
384+
}
385+
386+
[Theory]
387+
[InlineData("")]
388+
[InlineData(" ")]
389+
[InlineData("\t")]
390+
public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithEmptyOrWhitespaceArguments_ProducesNullArguments(string emptyArgs)
391+
{
392+
// Arrange
393+
var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false });
394+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null);
395+
396+
var startEvent = new ToolExecutionStartEvent
397+
{
398+
Data = new ToolExecutionStartData
399+
{
400+
ToolCallId = "call-empty",
401+
ToolName = "noArgsTool",
402+
Arguments = emptyArgs
403+
}
404+
};
405+
406+
// Act
407+
AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent);
408+
409+
// Assert
410+
var content = Assert.Single(result.Contents);
411+
var functionCall = Assert.IsType<FunctionCallContent>(content);
412+
Assert.Equal("call-empty", functionCall.CallId);
413+
Assert.Null(functionCall.Arguments);
414+
}
265415
}

0 commit comments

Comments
 (0)