PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / wordpress / php-mcp-schema / src / Client / Sampling / DTO / CreateMessageResult.php

CreateMessageResult.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.2, at vendor/wordpress/php-mcp-schema/src/Client/Sampling/DTO/CreateMessageResult.php

182 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * The name of the model that generated the message.
30 *
31 * @since 2024-11-05
32 *
33 * @var string
34 */
35 protected string $model;
36
37 /**
38 * The reason why sampling stopped, if known.
39 *
40 * Standard values:
41 * - "endTurn": Natural end of the assistant's turn
42 * - "stopSequence": A stop sequence was encountered
43 * - "maxTokens": Maximum token limit was reached
44 * - "toolUse": The model wants to use one or more tools
45 *
46 * This field is an open string to allow for provider-specific stop reasons.
47 *
48 * @since 2024-11-05
49 *
50 * @var "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null
51 */
52 protected $stopReason;
53
54 /**
55 * @since 2024-11-05
56 *
57 * @var 'user'|'assistant'
58 */
59 protected string $role;
60
61 /**
62 * @since 2024-11-05
63 *
64 * @var array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface>
65 */
66 protected array $content;
67
68 /**
69 * @param string $model @since 2024-11-05
70 * @param 'user'|'assistant' $role @since 2024-11-05
71 * @param array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content @since 2024-11-05
72 * @param array<string, mixed>|null $_meta @since 2024-11-05
73 * @param "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason @since 2024-11-05
74 */
75 public function __construct(
76 string $model,
77 string $role,
78 array $content,
79 ?array $_meta = null,
80 $stopReason = null
81 ) {
82 parent::__construct($_meta);
83 $this->model = $model;
84 $this->role = $role;
85 $this->content = $content;
86 $this->stopReason = $stopReason;
87 }
88
89 /**
90 * Creates an instance from an array.
91 *
92 * @param array{
93 * _meta?: array<string, mixed>|null,
94 * model: string,
95 * stopReason?: "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null,
96 * role: 'user'|'assistant',
97 * content: array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface>
98 * } $data
99 * @phpstan-param array<string, mixed> $data
100 * @return self
101 */
102 public static function fromArray(array $data): self
103 {
104 self::assertRequired($data, ['model', 'role', 'content']);
105
106 /** @var 'user'|'assistant' $role */
107 $role = self::asString($data['role']);
108
109 /** @var array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content */
110 $content = array_map(
111 static fn($item) => is_array($item)
112 ? SamplingMessageContentBlockFactory::fromArray($item)
113 : $item,
114 self::asArray($data['content'])
115 );
116
117 /** @var "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason */
118 $stopReason = isset($data['stopReason'])
119 ? $data['stopReason']
120 : null;
121
122 return new self(
123 self::asString($data['model']),
124 $role,
125 $content,
126 self::asArrayOrNull($data['_meta'] ?? null),
127 $stopReason
128 );
129 }
130
131 /**
132 * Converts the instance to an array.
133 *
134 * @return array<string, mixed>
135 */
136 public function toArray(): array
137 {
138 $result = parent::toArray();
139
140 $result['model'] = $this->model;
141 if ($this->stopReason !== null) {
142 $result['stopReason'] = $this->stopReason;
143 }
144 $result['role'] = $this->role;
145 $result['content'] = array_map(static fn($item) => (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item, $this->content);
146
147 return $result;
148 }
149
150 /**
151 * @return string
152 */
153 public function getModel(): string
154 {
155 return $this->model;
156 }
157
158 /**
159 * @return "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null
160 */
161 public function getStopReason()
162 {
163 return $this->stopReason;
164 }
165
166 /**
167 * @return 'user'|'assistant'
168 */
169 public function getRole(): string
170 {
171 return $this->role;
172 }
173
174 /**
175 * @return array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface>
176 */
177 public function getContent(): array
178 {
179 return $this->content;
180 }
181 }
182