Skip to content
Draft
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
40 changes: 40 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class Response
use Conditionable;
use Macroable;

/**
* @var array<string, mixed>
*/
protected array $meta = [];

/**
* @var array<string, mixed>
*/
protected array $structured_content = [];

protected function __construct(
protected Content $content,
protected Role $role = Role::USER,
Expand All @@ -27,6 +37,36 @@ protected function __construct(
//
}

/**
* @param array<string, mixed>|null $meta
* @return ($meta is null ? array<string, mixed> : self)
*/
public function meta(?array $meta = null): array|self
{
if (is_null($meta)) {
return $this->meta;
}

$this->meta = array_merge($this->meta, $meta);

return $this;
}

/**
* @param array<string, mixed>|null $structuredContent
* @return ($structuredContent is null ? array<string, mixed> : self)
*/
public function structuredContent(?array $structuredContent = null): array|self
{
if (is_null($structuredContent)) {
return $this->structured_content;
}

$this->structured_content = array_merge($this->structured_content, $structuredContent);

return $this;
}

/**
* @param array<string, mixed> $params
*/
Expand Down
13 changes: 10 additions & 3 deletions src/Server/Methods/CallTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ public function handle(JsonRpcRequest $request, ServerContext $context): Generat
}

/**
* @return callable(Collection<int, Response>): array{content: array<int, array<string, mixed>>, isError: bool}
* @return callable(Collection<int, Response>):array{
* _meta?: array<string, mixed>,
* content?: array<int, array<string, mixed>>,
* isError?: bool,
* structuredContent?: array<string, mixed>,
* }
*/
protected function serializable(Tool $tool): callable
{
return fn (Collection $responses): array => [
return fn (Collection $responses): array => array_filter([
'content' => $responses->map(fn (Response $response): array => $response->content()->toTool($tool))->all(),
'isError' => $responses->contains(fn (Response $response): bool => $response->isError()),
];
'structuredContent' => $responses->flatMap(fn (Response $response): array => $response->structuredContent())->all(),
'_meta' => $responses->flatMap(fn (Response $response): array => $response->meta())->all(),
], filled(...));
}
}
28 changes: 28 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,31 @@
$content = $response->content();
expect((string) $content)->toBe(json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
});

it('handles adding meta to a text response', function (): void {
$response = Response::text('Hello world')
->meta(['key1' => 'value1', 'key2' => 2]);

expect($response->content())->toBeInstanceOf(Text::class);
expect($response->isNotification())->toBeFalse();
expect($response->isError())->toBeFalse();
expect($response->role())->toBe(Role::USER);
expect($response->meta())->toEqual(['key1' => 'value1', 'key2' => 2]);
});

it('handles adding structured content to a text response', function (): void {
$response = Response::text('Hello world')
->structuredContent([
'section1' => ['item1', 'item2'],
'section2' => ['item3', 'item4'],
]);

expect($response->content())->toBeInstanceOf(Text::class);
expect($response->isNotification())->toBeFalse();
expect($response->isError())->toBeFalse();
expect($response->role())->toBe(Role::USER);
expect($response->structuredContent())->toEqual([
'section1' => ['item1', 'item2'],
'section2' => ['item3', 'item4'],
]);
});