Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 75e1263

Browse files
authored
Add and use ToolNotFoundException (#162)
1 parent c6ac0e7 commit 75e1263

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Chain/ToolBox/ToolBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Chain\ToolBox;
66

7-
use PhpLlm\LlmChain\Exception\RuntimeException;
7+
use PhpLlm\LlmChain\Exception\ToolNotFoundException;
88
use PhpLlm\LlmChain\Model\Response\ToolCall;
99

1010
final class ToolBox implements ToolBoxInterface
@@ -55,6 +55,6 @@ public function execute(ToolCall $toolCall): string
5555
}
5656
}
5757

58-
throw new RuntimeException('Tool not found');
58+
throw ToolNotFoundException::forToolCall($toolCall);
5959
}
6060
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Exception;
6+
7+
use PhpLlm\LlmChain\Model\Response\ToolCall;
8+
9+
final class ToolNotFoundException extends RuntimeException
10+
{
11+
public static function forToolCall(ToolCall $toolCall): self
12+
{
13+
return new self(sprintf('Tool not found for call: %s', $toolCall->name));
14+
}
15+
}

tests/Chain/ToolBox/ToolBoxTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpLlm\LlmChain\Chain\ToolBox\ParameterAnalyzer;
1010
use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\Chain\ToolBox\ToolBox;
12+
use PhpLlm\LlmChain\Exception\ToolNotFoundException;
1213
use PhpLlm\LlmChain\Model\Response\ToolCall;
1314
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoParams;
1415
use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolOptionalParam;
@@ -101,6 +102,15 @@ public function toolsMap(): void
101102
self::assertSame(json_encode($expected), json_encode($actual));
102103
}
103104

105+
#[Test]
106+
public function executeWithUnknownTool(): void
107+
{
108+
self::expectException(ToolNotFoundException::class);
109+
self::expectExceptionMessage('Tool not found for call: foo_bar_baz');
110+
111+
$this->toolBox->execute(new ToolCall('call_1234', 'foo_bar_baz'));
112+
}
113+
104114
#[Test]
105115
public function execute(): void
106116
{

0 commit comments

Comments
 (0)