PromptBuilder::appendPartToMessages( WordPressAiClientMessagesDTOMessagePart $part ): void

Appends a MessagePart to the messages array.

Description

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

Parameters

$partWordPressAiClientMessagesDTOMessagePartrequired
The part to append.

Return

void

Source

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]);
}

Changelog

VersionDescription
0.1.0Introduced.

User Contributed Notes

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