Introduce schema component
This commit is contained in:
parent
ee0dffe369
commit
6025f890f9
1 changed files with 5419 additions and 0 deletions
88
src/Schema/Content/PromptMessage.php
Normal file
88
src/Schema/Content/PromptMessage.php
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Schema\Content;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
|
||||
/**
|
||||
* Describes a message returned as part of a prompt.
|
||||
*
|
||||
* @phpstan-import-type TextContentData from TextContent
|
||||
* @phpstan-import-type ImageContentData from ImageContent
|
||||
* @phpstan-import-type AudioContentData from AudioContent
|
||||
* @phpstan-import-type EmbeddedResourceData from EmbeddedResource
|
||||
*
|
||||
* @phpstan-type PromptMessageData array{
|
||||
* role: string,
|
||||
* content: TextContentData|ImageContentData|AudioContentData|EmbeddedResourceData,
|
||||
* }
|
||||
*
|
||||
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
|
||||
*/
|
||||
class PromptMessage extends Content
|
||||
{
|
||||
/**
|
||||
* Create a new PromptMessage instance.
|
||||
*
|
||||
* @param Role $role The role of the message
|
||||
* @param TextContent|ImageContent|AudioContent|EmbeddedResource $content The content of the message
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly Role $role,
|
||||
public readonly TextContent|ImageContent|AudioContent|EmbeddedResource $content,
|
||||
) {
|
||||
parent::__construct('prompt');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PromptMessageData $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
if (!isset($data['role']) || !\is_string($data['role'])) {
|
||||
throw new InvalidArgumentException('Missing or invalid "role" in PromptMessage data.');
|
||||
}
|
||||
if (!isset($data['content']) || !\is_array($data['content'])) {
|
||||
throw new InvalidArgumentException('Missing or invalid "content" in PromptMessage data.');
|
||||
}
|
||||
|
||||
$contentData = $data['content'];
|
||||
$contentType = $contentData['type'] ?? null;
|
||||
|
||||
$content = match ($contentType) {
|
||||
'text' => TextContent::fromArray($contentData),
|
||||
'image' => ImageContent::fromArray($contentData),
|
||||
'audio' => AudioContent::fromArray($contentData),
|
||||
'resource' => EmbeddedResource::fromArray($contentData),
|
||||
default => throw new InvalidArgumentException(\sprintf('Invalid content type "%s" for PromptMessage.', $contentType)),
|
||||
};
|
||||
|
||||
return new self(Role::from($data['role']), $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the message to an array.
|
||||
*
|
||||
* @return array{
|
||||
* role: string,
|
||||
* content: TextContent|ImageContent|AudioContent|EmbeddedResource
|
||||
* }
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'role' => $this->role->value,
|
||||
'content' => $this->content,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue