Message::fromArray( $array ): self

In this article

{@inheritDoc}

Return

self The specific message class based on the role.

Source

final public static function fromArray(array $array): self
{
    static::validateFromArrayData($array, [self::KEY_ROLE, self::KEY_PARTS]);
    $role = MessageRoleEnum::from($array[self::KEY_ROLE]);
    $partsData = $array[self::KEY_PARTS];
    $parts = array_map(function (array $partData) {
        return \WordPress\AiClient\Messages\DTO\MessagePart::fromArray($partData);
    }, $partsData);
    // Determine which concrete class to instantiate based on role
    if ($role->isUser()) {
        return new \WordPress\AiClient\Messages\DTO\UserMessage($parts);
    } elseif ($role->isModel()) {
        return new \WordPress\AiClient\Messages\DTO\ModelMessage($parts);
    } else {
        // Only USER and MODEL roles are supported
        throw new InvalidArgumentException('Invalid message role: ' . $role->value);
    }
}

Changelog

VersionDescription
0.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.