convertkit
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Client
/
Sampling
/
DTO
/
CreateMessageResult.php
CreateMessageResult.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at vendor/wordpress/php-mcp-schema/src/Client/Sampling/DTO/CreateMessageResult.php
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Client\Sampling\DTO; |
| 6 | |
| 7 | use WP\McpSchema\Client\Lifecycle\Union\ClientResultInterface; |
| 8 | use WP\McpSchema\Common\Protocol\DTO\Result; |
| 9 | use WP\McpSchema\Common\Protocol\Factory\SamplingMessageContentBlockFactory; |
| 10 | use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 11 | |
| 12 | /** |
| 13 | * The client's response to a sampling/createMessage request from the server. |
| 14 | * The client should inform the user before returning the sampled message, to allow them |
| 15 | * to inspect the response (human in the loop) and decide whether to allow the server to see it. |
| 16 | * |
| 17 | * @since 2024-11-05 |
| 18 | * @last-updated 2025-11-25 (modified properties: content, role) |
| 19 | * |
| 20 | * @mcp-domain Client |
| 21 | * @mcp-subdomain Sampling |
| 22 | * @mcp-version 2025-11-25 |
| 23 | */ |
| 24 | class CreateMessageResult extends Result implements ClientResultInterface |
| 25 | { |
| 26 | use ValidatesRequiredFields; |
| 27 | |
| 28 | /** |
| 29 | * Wire keys this class models. Anything else is kept in $additionalProperties. |
| 30 | * |
| 31 | * @var array<int, string> |
| 32 | */ |
| 33 | private const KNOWN_KEYS = ['_meta', 'model', 'stopReason', 'role', 'content']; |
| 34 | |
| 35 | /** |
| 36 | * The name of the model that generated the message. |
| 37 | * |
| 38 | * @since 2024-11-05 |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | protected string $model; |
| 43 | |
| 44 | /** |
| 45 | * The reason why sampling stopped, if known. |
| 46 | * |
| 47 | * Standard values: |
| 48 | * - "endTurn": Natural end of the assistant's turn |
| 49 | * - "stopSequence": A stop sequence was encountered |
| 50 | * - "maxTokens": Maximum token limit was reached |
| 51 | * - "toolUse": The model wants to use one or more tools |
| 52 | * |
| 53 | * This field is an open string to allow for provider-specific stop reasons. |
| 54 | * |
| 55 | * @since 2024-11-05 |
| 56 | * |
| 57 | * @var "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null |
| 58 | */ |
| 59 | protected $stopReason; |
| 60 | |
| 61 | /** |
| 62 | * @since 2024-11-05 |
| 63 | * |
| 64 | * @var 'user'|'assistant' |
| 65 | */ |
| 66 | protected string $role; |
| 67 | |
| 68 | /** |
| 69 | * @since 2024-11-05 |
| 70 | * |
| 71 | * @var array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> |
| 72 | */ |
| 73 | protected array $content; |
| 74 | |
| 75 | /** |
| 76 | * @param string $model @since 2024-11-05 |
| 77 | * @param 'user'|'assistant' $role @since 2024-11-05 |
| 78 | * @param array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content @since 2024-11-05 |
| 79 | * @param array<string, mixed>|null $_meta @since 2024-11-05 |
| 80 | * @param "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason @since 2024-11-05 |
| 81 | * @param array<string, mixed>|null $additionalProperties |
| 82 | */ |
| 83 | public function __construct( |
| 84 | string $model, |
| 85 | string $role, |
| 86 | array $content, |
| 87 | ?array $_meta = null, |
| 88 | $stopReason = null, |
| 89 | ?array $additionalProperties = null |
| 90 | ) { |
| 91 | parent::__construct($_meta, $additionalProperties); |
| 92 | $this->model = $model; |
| 93 | $this->role = $role; |
| 94 | $this->content = $content; |
| 95 | $this->stopReason = $stopReason; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates an instance from an array. |
| 100 | * |
| 101 | * @param array{ |
| 102 | * _meta?: array<string, mixed>|null, |
| 103 | * model: string, |
| 104 | * stopReason?: "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null, |
| 105 | * role: 'user'|'assistant', |
| 106 | * content: array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> |
| 107 | * } $data |
| 108 | * @phpstan-param array<string, mixed> $data |
| 109 | * @return self |
| 110 | */ |
| 111 | public static function fromArray(array $data): self |
| 112 | { |
| 113 | self::assertRequired($data, ['model', 'role', 'content']); |
| 114 | |
| 115 | /** @var 'user'|'assistant' $role */ |
| 116 | $role = self::asString($data['role']); |
| 117 | |
| 118 | /** @var array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content */ |
| 119 | $content = array_map( |
| 120 | static fn($item) => is_array($item) |
| 121 | ? SamplingMessageContentBlockFactory::fromArray($item) |
| 122 | : $item, |
| 123 | self::asArray($data['content']) |
| 124 | ); |
| 125 | |
| 126 | /** @var "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason */ |
| 127 | $stopReason = isset($data['stopReason']) |
| 128 | ? $data['stopReason'] |
| 129 | : null; |
| 130 | |
| 131 | return new self( |
| 132 | self::asString($data['model']), |
| 133 | $role, |
| 134 | $content, |
| 135 | self::asArrayOrNull($data['_meta'] ?? null), |
| 136 | $stopReason, |
| 137 | self::additionalFields($data, self::KNOWN_KEYS) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Converts the instance to an array. |
| 143 | * |
| 144 | * @return array<string, mixed> |
| 145 | */ |
| 146 | public function toArray(): array |
| 147 | { |
| 148 | $result = parent::toArray(); |
| 149 | |
| 150 | $result['model'] = $this->model; |
| 151 | if ($this->stopReason !== null) { |
| 152 | $result['stopReason'] = $this->stopReason; |
| 153 | } |
| 154 | $result['role'] = $this->role; |
| 155 | $result['content'] = array_map(static fn($item) => (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item, $this->content); |
| 156 | |
| 157 | return $result; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @return string |
| 162 | */ |
| 163 | public function getModel(): string |
| 164 | { |
| 165 | return $this->model; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @return "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null |
| 170 | */ |
| 171 | public function getStopReason() |
| 172 | { |
| 173 | return $this->stopReason; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return 'user'|'assistant' |
| 178 | */ |
| 179 | public function getRole(): string |
| 180 | { |
| 181 | return $this->role; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @return array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> |
| 186 | */ |
| 187 | public function getContent(): array |
| 188 | { |
| 189 | return $this->content; |
| 190 | } |
| 191 | } |
| 192 |