|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Tests\Chain\ToolBox; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolExecutionException; |
| 8 | +use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolNotFoundException; |
| 9 | +use PhpLlm\LlmChain\Chain\ToolBox\FaultTolerantToolBox; |
| 10 | +use PhpLlm\LlmChain\Chain\ToolBox\Metadata; |
| 11 | +use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface; |
| 12 | +use PhpLlm\LlmChain\Model\Response\ToolCall; |
| 13 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams; |
| 14 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolRequiredParams; |
| 15 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 16 | +use PHPUnit\Framework\Attributes\Test; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +#[CoversClass(FaultTolerantToolBox::class)] |
| 20 | +final class FaultTolerantToolBoxTest extends TestCase |
| 21 | +{ |
| 22 | + #[Test] |
| 23 | + public function faultyToolExecution(): void |
| 24 | + { |
| 25 | + $faultyToolBox = $this->createFaultyToolBox( |
| 26 | + fn (ToolCall $toolCall) => ToolExecutionException::executionFailed($toolCall, new \Exception('error')) |
| 27 | + ); |
| 28 | + |
| 29 | + $faultTolerantToolBox = new FaultTolerantToolBox($faultyToolBox); |
| 30 | + $expected = 'An error occurred while executing tool "tool_foo".'; |
| 31 | + |
| 32 | + $toolCall = new ToolCall('987654321', 'tool_foo'); |
| 33 | + $actual = $faultTolerantToolBox->execute($toolCall); |
| 34 | + |
| 35 | + self::assertSame($expected, $actual); |
| 36 | + } |
| 37 | + |
| 38 | + #[Test] |
| 39 | + public function faultyToolCall(): void |
| 40 | + { |
| 41 | + $faultyToolBox = $this->createFaultyToolBox( |
| 42 | + fn (ToolCall $toolCall) => ToolNotFoundException::notFoundForToolCall($toolCall) |
| 43 | + ); |
| 44 | + |
| 45 | + $faultTolerantToolBox = new FaultTolerantToolBox($faultyToolBox); |
| 46 | + $expected = 'Tool "tool_xyz" was not found, please use one of these: tool_no_params, tool_required_params'; |
| 47 | + |
| 48 | + $toolCall = new ToolCall('123456789', 'tool_xyz'); |
| 49 | + $actual = $faultTolerantToolBox->execute($toolCall); |
| 50 | + |
| 51 | + self::assertSame($expected, $actual); |
| 52 | + } |
| 53 | + |
| 54 | + private function createFaultyToolBox(\Closure $exceptionFactory): ToolBoxInterface |
| 55 | + { |
| 56 | + return new class($exceptionFactory) implements ToolBoxInterface { |
| 57 | + public function __construct(private readonly \Closure $exceptionFactory) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return Metadata[] |
| 63 | + */ |
| 64 | + public function getMap(): array |
| 65 | + { |
| 66 | + return [ |
| 67 | + new Metadata(ToolNoParams::class, 'tool_no_params', 'A tool without parameters', '__invoke', null), |
| 68 | + new Metadata(ToolRequiredParams::class, 'tool_required_params', 'A tool with required parameters', 'bar', null), |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + public function execute(ToolCall $toolCall): mixed |
| 73 | + { |
| 74 | + throw ($this->exceptionFactory)($toolCall); |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
0 commit comments