WordPress\AiClient\Builders

PromptBuilder::appendPartToMessagesprotectedWP 0.1.0

Appends a MessagePart to the messages array.

If the last message has a user role, the part is added to it. Otherwise, a new UserMessage is created with the part.

Method of the class: PromptBuilder{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->appendPartToMessages( $part ): void;
$part(MessagePart) (required)
The part to append.

Changelog

Since 0.1.0 Introduced.

PromptBuilder::appendPartToMessages() code WP 7.0

protected function appendPartToMessages(MessagePart $part): void
{
    $lastMessage = end($this->messages);
    if ($lastMessage instanceof Message && $lastMessage->getRole()->isUser()) {
        // Replace the last message with a new one containing the appended part
        array_pop($this->messages);
        $this->messages[] = $lastMessage->withPart($part);
        return;
    }
    // Create new UserMessage with the part
    $this->messages[] = new UserMessage([$part]);
}