Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Chain/ToolBox/ToolResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

final readonly class ToolResultConverter
{
public function convert(mixed $result): string
/**
* @param \JsonSerializable|\Stringable|array<int|string, mixed>|float|string|null $result
*/
public function convert(\JsonSerializable|\Stringable|array|float|string|null $result): ?string
{
if (null === $result) {
return null;
}

if ($result instanceof \JsonSerializable || is_array($result)) {
return json_encode($result, flags: JSON_THROW_ON_ERROR);
}

if (is_integer($result) || is_float($result) || $result instanceof \Stringable) {
if (is_float($result) || $result instanceof \Stringable) {
return (string) $result;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Chain/ToolBox/ToolResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ToolResultConverterTest extends TestCase
{
#[Test]
#[DataProvider('provideResults')]
public function testConvert(mixed $result, string $expected): void
public function testConvert(mixed $result, ?string $expected): void
{
$converter = new ToolResultConverter();

Expand All @@ -24,6 +24,8 @@ public function testConvert(mixed $result, string $expected): void

public static function provideResults(): \Generator
{
yield 'null' => [null, null];

yield 'integer' => [42, '42'];

yield 'float' => [42.42, '42.42'];
Expand Down