|
13 | 13 | use NeuronAI\Chat\Attachments\Image; |
14 | 14 | use NeuronAI\Chat\Enums\AttachmentContentType; |
15 | 15 | use NeuronAI\Chat\Messages\AssistantMessage; |
| 16 | +use NeuronAI\Chat\Messages\ToolCallMessage; |
16 | 17 | use NeuronAI\Chat\Messages\UserMessage; |
17 | 18 | use NeuronAI\Providers\Gemini\Gemini; |
18 | 19 | use NeuronAI\Tools\PropertyType; |
|
21 | 22 | use PHPUnit\Framework\TestCase; |
22 | 23 |
|
23 | 24 | use function json_decode; |
| 25 | +use function json_encode; |
24 | 26 |
|
25 | 27 | class GeminiTest extends TestCase |
26 | 28 | { |
@@ -385,4 +387,161 @@ public function test_chat_with_attachment_response(): void |
385 | 387 | $this->assertSame('application/pdf', $attachmentList[1]->mediaType); |
386 | 388 | $this->assertSame('321cba', $attachmentList[1]->content); |
387 | 389 | } |
| 390 | + |
| 391 | + public function test_function_call_in_first_part(): void |
| 392 | + { |
| 393 | + $body = json_encode([ |
| 394 | + 'candidates' => [[ |
| 395 | + 'content' => [ |
| 396 | + 'parts' => [ |
| 397 | + ['functionCall' => ['name' => 'my_tool', 'args' => ['prop' => 'hello']]], |
| 398 | + ], |
| 399 | + ], |
| 400 | + 'finishReason' => 'STOP', |
| 401 | + ]], |
| 402 | + 'usageMetadata' => ['promptTokenCount' => 10, 'candidatesTokenCount' => 5], |
| 403 | + ]); |
| 404 | + |
| 405 | + $mockHandler = new MockHandler([new Response(status: 200, body: $body)]); |
| 406 | + $client = new Client(['handler' => HandlerStack::create($mockHandler)]); |
| 407 | + |
| 408 | + $provider = (new Gemini('', 'gemini-2.0-flash')) |
| 409 | + ->setTools([$this->makeSimpleTool('my_tool')]) |
| 410 | + ->setClient($client); |
| 411 | + |
| 412 | + $response = $provider->chat([new UserMessage('test')]); |
| 413 | + |
| 414 | + $this->assertInstanceOf(ToolCallMessage::class, $response); |
| 415 | + $this->assertCount(1, $response->getTools()); |
| 416 | + $this->assertSame('my_tool', $response->getTools()[0]->getName()); |
| 417 | + } |
| 418 | + |
| 419 | + public function test_function_call_after_text_part(): void |
| 420 | + { |
| 421 | + $body = json_encode([ |
| 422 | + 'candidates' => [[ |
| 423 | + 'content' => [ |
| 424 | + 'parts' => [ |
| 425 | + ['text' => 'Let me call the tool.'], |
| 426 | + ['functionCall' => ['name' => 'my_tool', 'args' => ['prop' => 'data']]], |
| 427 | + ], |
| 428 | + ], |
| 429 | + 'finishReason' => 'STOP', |
| 430 | + ]], |
| 431 | + 'usageMetadata' => ['promptTokenCount' => 10, 'candidatesTokenCount' => 5], |
| 432 | + ]); |
| 433 | + |
| 434 | + $mockHandler = new MockHandler([new Response(status: 200, body: $body)]); |
| 435 | + $client = new Client(['handler' => HandlerStack::create($mockHandler)]); |
| 436 | + |
| 437 | + $provider = (new Gemini('', 'gemini-3-pro-preview')) |
| 438 | + ->setTools([$this->makeSimpleTool('my_tool')]) |
| 439 | + ->setClient($client); |
| 440 | + |
| 441 | + $response = $provider->chat([new UserMessage('test')]); |
| 442 | + |
| 443 | + $this->assertInstanceOf(ToolCallMessage::class, $response); |
| 444 | + $this->assertCount(1, $response->getTools()); |
| 445 | + $this->assertSame('my_tool', $response->getTools()[0]->getName()); |
| 446 | + } |
| 447 | + |
| 448 | + public function test_multiple_function_calls_after_text_part(): void |
| 449 | + { |
| 450 | + $body = json_encode([ |
| 451 | + 'candidates' => [[ |
| 452 | + 'content' => [ |
| 453 | + 'parts' => [ |
| 454 | + ['text' => 'I will call both tools.'], |
| 455 | + ['functionCall' => ['name' => 'tool_a', 'args' => ['prop' => '1']]], |
| 456 | + ['functionCall' => ['name' => 'tool_b', 'args' => ['prop' => '2']]], |
| 457 | + ], |
| 458 | + ], |
| 459 | + 'finishReason' => 'STOP', |
| 460 | + ]], |
| 461 | + 'usageMetadata' => ['promptTokenCount' => 10, 'candidatesTokenCount' => 5], |
| 462 | + ]); |
| 463 | + |
| 464 | + $mockHandler = new MockHandler([new Response(status: 200, body: $body)]); |
| 465 | + $client = new Client(['handler' => HandlerStack::create($mockHandler)]); |
| 466 | + |
| 467 | + $provider = (new Gemini('', 'gemini-3-pro-preview')) |
| 468 | + ->setTools([ |
| 469 | + $this->makeSimpleTool('tool_a'), |
| 470 | + $this->makeSimpleTool('tool_b'), |
| 471 | + ]) |
| 472 | + ->setClient($client); |
| 473 | + |
| 474 | + $response = $provider->chat([new UserMessage('test')]); |
| 475 | + |
| 476 | + $this->assertInstanceOf(ToolCallMessage::class, $response); |
| 477 | + $this->assertCount(2, $response->getTools()); |
| 478 | + $this->assertSame('tool_a', $response->getTools()[0]->getName()); |
| 479 | + $this->assertSame('tool_b', $response->getTools()[1]->getName()); |
| 480 | + } |
| 481 | + |
| 482 | + public function test_no_function_call_returns_assistant_message(): void |
| 483 | + { |
| 484 | + $body = json_encode([ |
| 485 | + 'candidates' => [[ |
| 486 | + 'content' => [ |
| 487 | + 'parts' => [ |
| 488 | + ['text' => 'Here is a plain text response.'], |
| 489 | + ], |
| 490 | + ], |
| 491 | + 'finishReason' => 'STOP', |
| 492 | + ]], |
| 493 | + 'usageMetadata' => ['promptTokenCount' => 10, 'candidatesTokenCount' => 5], |
| 494 | + ]); |
| 495 | + |
| 496 | + $mockHandler = new MockHandler([new Response(status: 200, body: $body)]); |
| 497 | + $client = new Client(['handler' => HandlerStack::create($mockHandler)]); |
| 498 | + |
| 499 | + $provider = (new Gemini('', 'gemini-3-pro-preview')) |
| 500 | + ->setTools([$this->makeSimpleTool('my_tool')]) |
| 501 | + ->setClient($client); |
| 502 | + |
| 503 | + $response = $provider->chat([new UserMessage('test')]); |
| 504 | + |
| 505 | + $this->assertInstanceOf(AssistantMessage::class, $response); |
| 506 | + $this->assertNotInstanceOf(ToolCallMessage::class, $response); |
| 507 | + $this->assertSame('Here is a plain text response.', $response->getContent()); |
| 508 | + } |
| 509 | + |
| 510 | + public function test_function_call_with_thought_signature(): void |
| 511 | + { |
| 512 | + $body = json_encode([ |
| 513 | + 'candidates' => [[ |
| 514 | + 'content' => [ |
| 515 | + 'parts' => [ |
| 516 | + ['text' => 'Thinking...'], |
| 517 | + [ |
| 518 | + 'functionCall' => ['name' => 'my_tool', 'args' => ['prop' => 'value']], |
| 519 | + 'thoughtSignature' => 'sig-abc123', |
| 520 | + ], |
| 521 | + ], |
| 522 | + ], |
| 523 | + 'finishReason' => 'STOP', |
| 524 | + ]], |
| 525 | + 'usageMetadata' => ['promptTokenCount' => 10, 'candidatesTokenCount' => 5], |
| 526 | + ]); |
| 527 | + |
| 528 | + $mockHandler = new MockHandler([new Response(status: 200, body: $body)]); |
| 529 | + $client = new Client(['handler' => HandlerStack::create($mockHandler)]); |
| 530 | + |
| 531 | + $provider = (new Gemini('', 'gemini-3-pro-preview')) |
| 532 | + ->setTools([$this->makeSimpleTool('my_tool')]) |
| 533 | + ->setClient($client); |
| 534 | + |
| 535 | + $response = $provider->chat([new UserMessage('test')]); |
| 536 | + |
| 537 | + $this->assertInstanceOf(ToolCallMessage::class, $response); |
| 538 | + $this->assertSame('sig-abc123', $response->getMetadata('thoughtSignature')); |
| 539 | + } |
| 540 | + |
| 541 | + private function makeSimpleTool(string $name): Tool |
| 542 | + { |
| 543 | + return Tool::make($name, "A test tool called {$name}.") |
| 544 | + ->addProperty(new ToolProperty('prop', PropertyType::STRING, 'A test property', true)) |
| 545 | + ->setCallable(static fn (string $prop): string => "result: {$prop}"); |
| 546 | + } |
388 | 547 | } |
0 commit comments