|
4 | 4 |
|
5 | 5 | namespace PhpLlm\LlmChain\Chain\Toolbox; |
6 | 6 |
|
| 7 | +use PhpLlm\LlmChain\Chain\Toolbox\Event\ToolCallArgumentsResolved; |
7 | 8 | use PhpLlm\LlmChain\Chain\Toolbox\Exception\ToolExecutionException; |
8 | 9 | use PhpLlm\LlmChain\Chain\Toolbox\Exception\ToolNotFoundException; |
9 | 10 | use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\ReflectionToolFactory; |
10 | 11 | use PhpLlm\LlmChain\Platform\Response\ToolCall; |
11 | 12 | use PhpLlm\LlmChain\Platform\Tool\Tool; |
12 | 13 | use Psr\Log\LoggerInterface; |
13 | 14 | use Psr\Log\NullLogger; |
| 15 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
14 | 16 |
|
15 | 17 | /** |
16 | 18 | * @author Christopher Hertel <mail@christopher-hertel.de> |
@@ -38,6 +40,8 @@ public function __construct( |
38 | 40 | private readonly ToolFactoryInterface $toolFactory, |
39 | 41 | iterable $tools, |
40 | 42 | private readonly LoggerInterface $logger = new NullLogger(), |
| 43 | + private readonly ToolCallArgumentResolverInterface $argumentResolver = new ToolCallArgumentResolver(), |
| 44 | + private readonly ?EventDispatcherInterface $eventDispatcher = null, |
41 | 45 | ) { |
42 | 46 | $this->tools = $tools instanceof \Traversable ? iterator_to_array($tools) : $tools; |
43 | 47 | } |
@@ -70,7 +74,11 @@ public function execute(ToolCall $toolCall): mixed |
70 | 74 |
|
71 | 75 | try { |
72 | 76 | $this->logger->debug(\sprintf('Executing tool "%s".', $toolCall->name), $toolCall->arguments); |
73 | | - $result = $tool->{$metadata->reference->method}(...$toolCall->arguments); |
| 77 | + |
| 78 | + $arguments = $this->argumentResolver->resolveArguments($tool, $metadata, $toolCall); |
| 79 | + $this->eventDispatcher?->dispatch(new ToolCallArgumentsResolved($tool, $metadata, $arguments)); |
| 80 | + |
| 81 | + $result = $tool->{$metadata->reference->method}(...$arguments); |
74 | 82 | } catch (\Throwable $e) { |
75 | 83 | $this->logger->warning(\sprintf('Failed to execute tool "%s".', $toolCall->name), ['exception' => $e]); |
76 | 84 | throw ToolExecutionException::executionFailed($toolCall, $e); |
|
0 commit comments