Skip to content

Commit a64e7f9

Browse files
committed
Added basic StructuredContent
1 parent 628ee49 commit a64e7f9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Schema/Content/TextContent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* type: 'text',
2424
* text: string,
2525
* annotations?: AnnotationsData,
26+
* isError: bool
2627
* }
2728
*
2829
* @author Kyrian Obikwelu <[email protected]>
@@ -34,10 +35,12 @@ class TextContent extends Content
3435
*
3536
* @param mixed $text The value to convert to text
3637
* @param ?Annotations $annotations Optional annotations describing the content
38+
* @param bool $isError Optional, mark response as error
3739
*/
3840
public function __construct(
3941
public mixed $text,
4042
public readonly ?Annotations $annotations = null,
43+
public readonly bool $isError = false,
4144
) {
4245
$this->text = (\is_array($text) || \is_object($text))
4346
? json_encode($text, \JSON_PRETTY_PRINT) : (string) $text;

src/Schema/Result/CallToolResult.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static function fromArray(array $data): self
8787
{
8888
$contents = [];
8989
$structuredContent = null;
90+
$isError = false;
9091

9192
foreach ($data as $item) {
9293
if (!$item instanceof Content) {
@@ -100,9 +101,13 @@ public static function fromArray(array $data): self
100101
'text', 'audio', 'image', 'resource' => $item,
101102
default => throw new InvalidArgumentException(\sprintf('Invalid content type in CallToolResult data: "%s".', $item['type'] ?? null)),
102103
};
104+
105+
if ('text' === $item->type && $item instanceof TextContent) {
106+
$isError = $item->isError;
107+
}
103108
}
104109

105-
return new self($contents, $structuredContent);
110+
return new self($contents, $structuredContent, $isError);
106111
}
107112

108113
/**
@@ -114,8 +119,12 @@ public static function fromArray(array $data): self
114119
*/
115120
public function jsonSerialize(): array
116121
{
122+
$result['content'] = [];
123+
foreach ($this->content as $item) {
124+
$result['content'][] = $item->jsonSerialize();
125+
}
126+
117127
$result = [
118-
'content' => $this->content,
119128
'isError' => $this->isError,
120129
];
121130

0 commit comments

Comments
 (0)