| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Prompt; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\JsonRpc\ContentBlockDispatcher; |
| 18: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 19: | use Nexus\Mcp\Core\Schema\ContentBlock\AudioContent; |
| 20: | use Nexus\Mcp\Core\Schema\ContentBlock\EmbeddedResource; |
| 21: | use Nexus\Mcp\Core\Schema\ContentBlock\ImageContent; |
| 22: | use Nexus\Mcp\Core\Schema\ContentBlock\ResourceLink; |
| 23: | use Nexus\Mcp\Core\Schema\ContentBlock\TextContent; |
| 24: | use Nexus\Mcp\Core\Schema\Enum\Role; |
| 25: | use Nexus\Mcp\Core\Validation\EnumValueValidator; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | final readonly class PromptMessage implements Arrayable |
| 40: | { |
| 41: | public function __construct(public Role $role, public AudioContent|EmbeddedResource|ImageContent|ResourceLink|TextContent $content) |
| 42: | { |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | #[\Override] |
| 49: | public static function fromArray(array $data): static |
| 50: | { |
| 51: | Assert::that($data)->hasOffset('role', 'prompt message missing the required "role" key.'); |
| 52: | $role = EnumValueValidator::parse(Role::class, $data['role'], 'prompt message "role"'); |
| 53: | |
| 54: | Assert::that($data)->hasOffset('content', 'prompt message missing the required "content" key.'); |
| 55: | Assert::that($data['content']) |
| 56: | ->isArray('prompt message "content" must be an object, {type} given.') |
| 57: | ->isMap('prompt message "content" must be a string-keyed object.') |
| 58: | ; |
| 59: | |
| 60: | return new self($role, ContentBlockDispatcher::fromArray($data['content'], 'prompt message "content"')); |
| 61: | } |
| 62: | |
| 63: | #[\Override] |
| 64: | public function toArray(): array |
| 65: | { |
| 66: | return [ |
| 67: | 'content' => $this->content->toArray(), |
| 68: | 'role' => $this->role->value, |
| 69: | ]; |
| 70: | } |
| 71: | |
| 72: | #[\Override] |
| 73: | public function jsonSerialize(): array |
| 74: | { |
| 75: | return $this->toArray(); |
| 76: | } |
| 77: | } |
| 78: | |