*/ protected array $content; /** * @param string $model @since 2024-11-05 * @param 'user'|'assistant' $role @since 2024-11-05 * @param array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content @since 2024-11-05 * @param array|null $_meta @since 2024-11-05 * @param "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason @since 2024-11-05 */ public function __construct( string $model, string $role, array $content, ?array $_meta = null, $stopReason = null ) { parent::__construct($_meta); $this->model = $model; $this->role = $role; $this->content = $content; $this->stopReason = $stopReason; } /** * Creates an instance from an array. * * @param array{ * _meta?: array|null, * model: string, * stopReason?: "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null, * role: 'user'|'assistant', * content: array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> * } $data * @phpstan-param array $data * @return self */ public static function fromArray(array $data): self { self::assertRequired($data, ['model', 'role', 'content']); /** @var 'user'|'assistant' $role */ $role = self::asString($data['role']); /** @var array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> $content */ $content = array_map( static fn($item) => is_array($item) ? SamplingMessageContentBlockFactory::fromArray($item) : $item, self::asArray($data['content']) ); /** @var "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null $stopReason */ $stopReason = isset($data['stopReason']) ? $data['stopReason'] : null; return new self( self::asString($data['model']), $role, $content, self::asArrayOrNull($data['_meta'] ?? null), $stopReason ); } /** * Converts the instance to an array. * * @return array */ public function toArray(): array { $result = parent::toArray(); $result['model'] = $this->model; if ($this->stopReason !== null) { $result['stopReason'] = $this->stopReason; } $result['role'] = $this->role; $result['content'] = array_map(static fn($item) => (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item, $this->content); return $result; } /** * @return string */ public function getModel(): string { return $this->model; } /** * @return "endTurn"|"stopSequence"|"maxTokens"|"toolUse"|string|null */ public function getStopReason() { return $this->stopReason; } /** * @return 'user'|'assistant' */ public function getRole(): string { return $this->role; } /** * @return array<\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface|\WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface> */ public function getContent(): array { return $this->content; } }