wppaste
WordPress

PromptBuilder::parseMessage( mixed $input, WordPress\AiClient\Messages\Enums\MessageRoleEnum $defaultRole ): WordPress\AiClient\Messages\DTO\Message

Since
0.1.0
Source
wp-includes/php-ai-client/src/Builders/PromptBuilder.php:1311
Parses various input types into a Message with the given role.

Compatibility

WordPress
since 0.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 0.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$inputmixed
The input to parse.
$defaultRoleWordPress\AiClient\Messages\Enums\MessageRoleEnum
The role for the message if not specified by input.

Return value

WordPress\AiClient\Messages\DTO\Message
The parsed message.

Performance profile

How much work a call to PromptBuilder::parseMessage() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–41

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 116.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost PromptBuilder::parseMessage() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–21none
!($input instanceof) && !is_string($input) && is_array($input) && ::Message()19::Message(), ::Message()
!($input instanceof) && !is_string($input) && is_array($input) && !::Message() && !::MessagePart()26–37::Message(), ::MessagePart(), array_is_list()
!($input instanceof) && !is_string($input) && is_array($input) && !::Message() && ::MessagePart()28::Message(), ::MessagePart(), ::MessagePart()
!($input instanceof) && !is_string($input) && is_array($input) && !::Message() && !::MessagePart() && array_is_list() && !empty($input) && !$input && !is_string($item) && !($item instanceof) && is_array($item)41::Message(), ::MessagePart(), array_is_list(), ::MessagePart()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1166–4115
8.51166–4115
8.41166–41152 fewer instructions than PHP 8.3
8.31186–4115
8.21186–4115
8.11186–4115
7.41186–4115

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 8

  • array_is_list()Polyfill for `array_is_list()` function added in PHP 8.1.
  • \WordPress\AiClient\Messages\DTO\Message::__construct()
  • \WordPress\AiClient\Common\Exception\InvalidArgumentException::__construct()
  • \WordPress\AiClient\Messages\DTO\MessagePart::__construct()
  • \WordPress\AiClient\Messages\DTO\Message::isArrayShape()
  • \WordPress\AiClient\Messages\DTO\Message::fromArray()
  • \WordPress\AiClient\Messages\DTO\MessagePart::isArrayShape()
  • \WordPress\AiClient\Messages\DTO\MessagePart::fromArray()

Source code

    private function parseMessage($input, MessageRoleEnum $defaultRole): Message    {        // Handle Message input directly        if ($input instanceof Message) {            return $input;        }        // Handle single MessagePart        if ($input instanceof MessagePart) {            return new Message($defaultRole, [$input]);        }        // Handle string input        if (is_string($input)) {            if (trim($input) === '') {                throw new InvalidArgumentException('Cannot create a message from an empty string.');            }            return new Message($defaultRole, [new MessagePart($input)]);        }        // Handle array input        if (!is_array($input)) {            throw new InvalidArgumentException('Input must be a string, MessagePart, MessagePartArrayShape, ' . 'a list of string|MessagePart|MessagePartArrayShape, or a Message instance.');        }        // Handle MessageArrayShape input        if (Message::isArrayShape($input)) {            return Message::fromArray($input);        }        // Check if it's a MessagePartArrayShape        if (MessagePart::isArrayShape($input)) {            return new Message($defaultRole, [MessagePart::fromArray($input)]);        }        // It should be a list of string|MessagePart|MessagePartArrayShape        if (!array_is_list($input)) {            throw new InvalidArgumentException('Array input must be a list array.');        }        // Empty array check        if (empty($input)) {            throw new InvalidArgumentException('Cannot create a message from an empty array.');        }        $parts = [];        foreach ($input as $item) {            if (is_string($item)) {                $parts[] = new MessagePart($item);            } elseif ($item instanceof MessagePart) {                $parts[] = $item;            } elseif (is_array($item) && MessagePart::isArrayShape($item)) {                $parts[] = MessagePart::fromArray($item);            } else {                throw new InvalidArgumentException('Array items must be strings, MessagePart instances, or MessagePartArrayShape.');            }        }        return new Message($defaultRole, $parts);    }

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/php-ai-client/src/Builders/PromptBuilder.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.