|
2 | 2 |
|
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.Text.Json; |
5 | 6 | using System.Threading.Tasks; |
6 | 7 | using GitHub.Copilot.SDK; |
7 | 8 | using Microsoft.Extensions.AI; |
@@ -243,4 +244,237 @@ public void ConvertToAgentResponseUpdate_AssistantMessageEvent_DoesNotEmitTextCo |
243 | 244 | Assert.Empty(result.Text); |
244 | 245 | Assert.DoesNotContain(result.Contents, c => c is TextContent); |
245 | 246 | } |
| 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 | + } |
246 | 480 | } |
0 commit comments