| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Result; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\MetaObject; |
| 18: | use Nexus\Mcp\Core\Schema\Prompt\PromptMessage; |
| 19: | use Nexus\Mcp\Core\Schema\Result; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | final readonly class GetPromptResult extends Result implements ServerResult |
| 27: | { |
| 28: | |
| 29: | |
| 30: | |
| 31: | public array $messages; |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public ?string $description; |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function __construct(array $messages, ?string $description = null, MetaObject $meta = new MetaObject()) |
| 42: | { |
| 43: | Assert::that($messages)->isList('"result.messages" must be a list, non-list array given.'); |
| 44: | |
| 45: | foreach ($messages as $message) { |
| 46: | Assert::that($message)->isInstanceOf(PromptMessage::class); |
| 47: | } |
| 48: | |
| 49: | Assert::that($description)->nullOr()->isNonEmptyString('"result.description" must be a non-empty string or null.'); |
| 50: | |
| 51: | $this->messages = $messages; |
| 52: | $this->description = $description; |
| 53: | |
| 54: | parent::__construct($meta); |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | #[\Override] |
| 61: | public static function fromArray(array $data): static |
| 62: | { |
| 63: | Assert::that($data)->hasOffset('messages', '"result" missing the required "messages" key.'); |
| 64: | Assert::that($data['messages'])->isArray('"result.messages" must be an array, {type} given.'); |
| 65: | |
| 66: | $messages = []; |
| 67: | |
| 68: | foreach ($data['messages'] as $entry) { |
| 69: | Assert::that($entry) |
| 70: | ->isArray('"result.message" must be an object, {type} given.') |
| 71: | ->isMap('"result.message" must be a string-keyed object.') |
| 72: | ; |
| 73: | $messages[] = PromptMessage::fromArray($entry); |
| 74: | } |
| 75: | |
| 76: | $description = $data['description'] ?? null; |
| 77: | Assert::that($description)->nullOr()->isString('"result.description" must be a string or null, {type} given.'); |
| 78: | |
| 79: | $meta = new MetaObject(); |
| 80: | |
| 81: | if (\array_key_exists('_meta', $data)) { |
| 82: | Assert::that($data['_meta']) |
| 83: | ->isArray('"result._meta" must be an object, {type} given.') |
| 84: | ->isMap('"result._meta" must be a string-keyed object.') |
| 85: | ; |
| 86: | $meta = MetaObject::fromArray($data['_meta']); |
| 87: | } |
| 88: | |
| 89: | return new self($messages, $description, $meta); |
| 90: | } |
| 91: | |
| 92: | #[\Override] |
| 93: | public function toArray(): array |
| 94: | { |
| 95: | $data = [ |
| 96: | ...parent::toArray(), |
| 97: | 'messages' => array_map(static fn(PromptMessage $message): array => $message->toArray(), $this->messages), |
| 98: | ]; |
| 99: | |
| 100: | if (null !== $this->description) { |
| 101: | $data['description'] = $this->description; |
| 102: | } |
| 103: | |
| 104: | return $data; |
| 105: | } |
| 106: | |
| 107: | #[\Override] |
| 108: | public function jsonSerialize(): array |
| 109: | { |
| 110: | return $this->toArray(); |
| 111: | } |
| 112: | } |
| 113: | |