lol backups and old site first commit

This commit is contained in:
2026-09-06 00:14:06 +02:00
parent 1e8a86873d
commit 56e7e28142
4021 changed files with 1866999 additions and 0 deletions
@@ -0,0 +1,173 @@
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Messages\DTO;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;
/**
* Represents a message in an AI conversation.
*
* Messages are the fundamental unit of communication with AI models,
* containing a role and one or more parts with different content types.
*
* @since 0.1.0
*
* @phpstan-import-type MessagePartArrayShape from MessagePart
*
* @phpstan-type MessageArrayShape array{
* role: string,
* parts: array<MessagePartArrayShape>
* }
*
* @extends AbstractDataTransferObject<MessageArrayShape>
*/
class Message extends AbstractDataTransferObject
{
public const KEY_ROLE = 'role';
public const KEY_PARTS = 'parts';
/**
* @var MessageRoleEnum The role of the message sender.
*/
protected MessageRoleEnum $role;
/**
* @var MessagePart[] The parts that make up this message.
*/
protected array $parts;
/**
* Constructor.
*
* @since 0.1.0
*
* @param MessageRoleEnum $role The role of the message sender.
* @param MessagePart[] $parts The parts that make up this message.
* @throws InvalidArgumentException If parts contain invalid content for the role.
*/
public function __construct(MessageRoleEnum $role, array $parts)
{
$this->role = $role;
$this->parts = $parts;
$this->validateParts();
}
/**
* Gets the role of the message sender.
*
* @since 0.1.0
*
* @return MessageRoleEnum The role.
*/
public function getRole(): MessageRoleEnum
{
return $this->role;
}
/**
* Gets the message parts.
*
* @since 0.1.0
*
* @return MessagePart[] The message parts.
*/
public function getParts(): array
{
return $this->parts;
}
/**
* Returns a new instance with the given part appended.
*
* @since 0.1.0
*
* @param MessagePart $part The part to append.
* @return Message A new instance with the part appended.
* @throws InvalidArgumentException If the part is invalid for the role.
*/
public function withPart(\WordPress\AiClient\Messages\DTO\MessagePart $part): \WordPress\AiClient\Messages\DTO\Message
{
$newParts = $this->parts;
$newParts[] = $part;
return new \WordPress\AiClient\Messages\DTO\Message($this->role, $newParts);
}
/**
* Validates that the message parts are appropriate for the message role.
*
* @since 0.1.0
*
* @return void
* @throws InvalidArgumentException If validation fails.
*/
private function validateParts(): void
{
foreach ($this->parts as $part) {
$type = $part->getType();
if ($this->role->isUser() && $type->isFunctionCall()) {
throw new InvalidArgumentException('User messages cannot contain function calls.');
}
if ($this->role->isModel() && $type->isFunctionResponse()) {
throw new InvalidArgumentException('Model messages cannot contain function responses.');
}
}
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public static function getJsonSchema(): array
{
return ['type' => 'object', 'properties' => [self::KEY_ROLE => ['type' => 'string', 'enum' => MessageRoleEnum::getValues(), 'description' => 'The role of the message sender.'], self::KEY_PARTS => ['type' => 'array', 'items' => \WordPress\AiClient\Messages\DTO\MessagePart::getJsonSchema(), 'minItems' => 1, 'description' => 'The parts that make up this message.']], 'required' => [self::KEY_ROLE, self::KEY_PARTS]];
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*
* @return MessageArrayShape
*/
public function toArray(): array
{
return [self::KEY_ROLE => $this->role->value, self::KEY_PARTS => array_map(function (\WordPress\AiClient\Messages\DTO\MessagePart $part) {
return $part->toArray();
}, $this->parts)];
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*
* @return self The specific message class based on the role.
*/
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);
}
}
/**
* Performs a deep clone of the message.
*
* This method ensures that message part objects are cloned to prevent
* modifications to the cloned message from affecting the original.
*
* @since 0.4.2
*/
public function __clone()
{
$clonedParts = [];
foreach ($this->parts as $part) {
$clonedParts[] = clone $part;
}
$this->parts = $clonedParts;
}
}
@@ -0,0 +1,266 @@
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Messages\DTO;
use WordPress\AiClient\Common\AbstractDataTransferObject;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Files\DTO\File;
use WordPress\AiClient\Messages\Enums\MessagePartChannelEnum;
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
use WordPress\AiClient\Tools\DTO\FunctionCall;
use WordPress\AiClient\Tools\DTO\FunctionResponse;
/**
* Represents a part of a message.
*
* Messages can contain multiple parts of different types, such as text, files,
* function calls, etc. This DTO encapsulates one such part.
*
* @since 0.1.0
*
* @phpstan-import-type FileArrayShape from File
* @phpstan-import-type FunctionCallArrayShape from FunctionCall
* @phpstan-import-type FunctionResponseArrayShape from FunctionResponse
*
* @phpstan-type MessagePartArrayShape array{
* channel: string,
* type: string,
* thoughtSignature?: string,
* text?: string,
* file?: FileArrayShape,
* functionCall?: FunctionCallArrayShape,
* functionResponse?: FunctionResponseArrayShape
* }
*
* @extends AbstractDataTransferObject<MessagePartArrayShape>
*/
class MessagePart extends AbstractDataTransferObject
{
public const KEY_CHANNEL = 'channel';
public const KEY_TYPE = 'type';
public const KEY_THOUGHT_SIGNATURE = 'thoughtSignature';
public const KEY_TEXT = 'text';
public const KEY_FILE = 'file';
public const KEY_FUNCTION_CALL = 'functionCall';
public const KEY_FUNCTION_RESPONSE = 'functionResponse';
/**
* @var MessagePartChannelEnum The channel this message part belongs to.
*/
private MessagePartChannelEnum $channel;
/**
* @var MessagePartTypeEnum The type of this message part.
*/
private MessagePartTypeEnum $type;
/**
* @var string|null Thought signature for extended thinking.
*/
private ?string $thoughtSignature = null;
/**
* @var string|null Text content (when type is TEXT).
*/
private ?string $text = null;
/**
* @var File|null File data (when type is FILE).
*/
private ?File $file = null;
/**
* @var FunctionCall|null Function call request (when type is FUNCTION_CALL).
*/
private ?FunctionCall $functionCall = null;
/**
* @var FunctionResponse|null Function response (when type is FUNCTION_RESPONSE).
*/
private ?FunctionResponse $functionResponse = null;
/**
* Constructor that accepts various content types and infers the message part type.
*
* @since 0.1.0
*
* @param mixed $content The content of this message part.
* @param MessagePartChannelEnum|null $channel The channel this part belongs to. Defaults to CONTENT.
* @param string|null $thoughtSignature Optional thought signature for extended thinking.
* @throws InvalidArgumentException If an unsupported content type is provided.
*/
public function __construct($content, ?MessagePartChannelEnum $channel = null, ?string $thoughtSignature = null)
{
$this->channel = $channel ?? MessagePartChannelEnum::content();
$this->thoughtSignature = $thoughtSignature;
if (is_string($content)) {
$this->type = MessagePartTypeEnum::text();
$this->text = $content;
} elseif ($content instanceof File) {
$this->type = MessagePartTypeEnum::file();
$this->file = $content;
} elseif ($content instanceof FunctionCall) {
$this->type = MessagePartTypeEnum::functionCall();
$this->functionCall = $content;
} elseif ($content instanceof FunctionResponse) {
$this->type = MessagePartTypeEnum::functionResponse();
$this->functionResponse = $content;
} else {
$type = is_object($content) ? get_class($content) : gettype($content);
throw new InvalidArgumentException(sprintf('Unsupported content type %s. Expected string, File, ' . 'FunctionCall, or FunctionResponse.', $type));
}
}
/**
* Gets the channel this message part belongs to.
*
* @since 0.1.0
*
* @return MessagePartChannelEnum The channel.
*/
public function getChannel(): MessagePartChannelEnum
{
return $this->channel;
}
/**
* Gets the type of this message part.
*
* @since 0.1.0
*
* @return MessagePartTypeEnum The type.
*/
public function getType(): MessagePartTypeEnum
{
return $this->type;
}
/**
* Gets the thought signature.
*
* @since 1.3.0
*
* @return string|null The thought signature or null if not set.
*/
public function getThoughtSignature(): ?string
{
return $this->thoughtSignature;
}
/**
* Gets the text content.
*
* @since 0.1.0
*
* @return string|null The text content or null if not a text part.
*/
public function getText(): ?string
{
return $this->text;
}
/**
* Gets the file.
*
* @since 0.1.0
*
* @return File|null The file or null if not a file part.
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* Gets the function call.
*
* @since 0.1.0
*
* @return FunctionCall|null The function call or null if not a function call part.
*/
public function getFunctionCall(): ?FunctionCall
{
return $this->functionCall;
}
/**
* Gets the function response.
*
* @since 0.1.0
*
* @return FunctionResponse|null The function response or null if not a function response part.
*/
public function getFunctionResponse(): ?FunctionResponse
{
return $this->functionResponse;
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public static function getJsonSchema(): array
{
$channelSchema = ['type' => 'string', 'enum' => MessagePartChannelEnum::getValues(), 'description' => 'The channel this message part belongs to.'];
$thoughtSignatureSchema = ['type' => 'string', 'description' => 'Thought signature for extended thinking.'];
return ['oneOf' => [['type' => 'object', 'properties' => [self::KEY_CHANNEL => $channelSchema, self::KEY_TYPE => ['type' => 'string', 'const' => MessagePartTypeEnum::text()->value], self::KEY_TEXT => ['type' => 'string', 'description' => 'Text content.'], self::KEY_THOUGHT_SIGNATURE => $thoughtSignatureSchema], 'required' => [self::KEY_TYPE, self::KEY_TEXT], 'additionalProperties' => \false], ['type' => 'object', 'properties' => [self::KEY_CHANNEL => $channelSchema, self::KEY_TYPE => ['type' => 'string', 'const' => MessagePartTypeEnum::file()->value], self::KEY_FILE => File::getJsonSchema(), self::KEY_THOUGHT_SIGNATURE => $thoughtSignatureSchema], 'required' => [self::KEY_TYPE, self::KEY_FILE], 'additionalProperties' => \false], ['type' => 'object', 'properties' => [self::KEY_CHANNEL => $channelSchema, self::KEY_TYPE => ['type' => 'string', 'const' => MessagePartTypeEnum::functionCall()->value], self::KEY_FUNCTION_CALL => FunctionCall::getJsonSchema(), self::KEY_THOUGHT_SIGNATURE => $thoughtSignatureSchema], 'required' => [self::KEY_TYPE, self::KEY_FUNCTION_CALL], 'additionalProperties' => \false], ['type' => 'object', 'properties' => [self::KEY_CHANNEL => $channelSchema, self::KEY_TYPE => ['type' => 'string', 'const' => MessagePartTypeEnum::functionResponse()->value], self::KEY_FUNCTION_RESPONSE => FunctionResponse::getJsonSchema(), self::KEY_THOUGHT_SIGNATURE => $thoughtSignatureSchema], 'required' => [self::KEY_TYPE, self::KEY_FUNCTION_RESPONSE], 'additionalProperties' => \false]]];
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*
* @return MessagePartArrayShape
*/
public function toArray(): array
{
$data = [self::KEY_CHANNEL => $this->channel->value, self::KEY_TYPE => $this->type->value];
if ($this->text !== null) {
$data[self::KEY_TEXT] = $this->text;
} elseif ($this->file !== null) {
$data[self::KEY_FILE] = $this->file->toArray();
} elseif ($this->functionCall !== null) {
$data[self::KEY_FUNCTION_CALL] = $this->functionCall->toArray();
} elseif ($this->functionResponse !== null) {
$data[self::KEY_FUNCTION_RESPONSE] = $this->functionResponse->toArray();
} else {
throw new RuntimeException('MessagePart requires one of: text, file, functionCall, or functionResponse. ' . 'This should not be a possible condition.');
}
if ($this->thoughtSignature !== null) {
$data[self::KEY_THOUGHT_SIGNATURE] = $this->thoughtSignature;
}
return $data;
}
/**
* {@inheritDoc}
*
* @since 0.1.0
*/
public static function fromArray(array $array): self
{
if (isset($array[self::KEY_CHANNEL])) {
$channel = MessagePartChannelEnum::from($array[self::KEY_CHANNEL]);
} else {
$channel = null;
}
$thoughtSignature = $array[self::KEY_THOUGHT_SIGNATURE] ?? null;
// Check which properties are set to determine how to construct the MessagePart
if (isset($array[self::KEY_TEXT])) {
return new self($array[self::KEY_TEXT], $channel, $thoughtSignature);
} elseif (isset($array[self::KEY_FILE])) {
return new self(File::fromArray($array[self::KEY_FILE]), $channel, $thoughtSignature);
} elseif (isset($array[self::KEY_FUNCTION_CALL])) {
return new self(FunctionCall::fromArray($array[self::KEY_FUNCTION_CALL]), $channel, $thoughtSignature);
} elseif (isset($array[self::KEY_FUNCTION_RESPONSE])) {
return new self(FunctionResponse::fromArray($array[self::KEY_FUNCTION_RESPONSE]), $channel, $thoughtSignature);
} else {
throw new InvalidArgumentException('MessagePart requires one of: text, file, functionCall, or functionResponse.');
}
}
/**
* Performs a deep clone of the message part.
*
* This method ensures that nested objects (file, function call, function response)
* are cloned to prevent modifications to the cloned part from affecting the original.
*
* @since 0.4.2
*/
public function __clone()
{
if ($this->file !== null) {
$this->file = clone $this->file;
}
if ($this->functionCall !== null) {
$this->functionCall = clone $this->functionCall;
}
if ($this->functionResponse !== null) {
$this->functionResponse = clone $this->functionResponse;
}
}
}
@@ -0,0 +1,32 @@
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Messages\DTO;
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;
/**
* Represents a message from the AI model.
*
* This is a convenience class that automatically sets the role to MODEL.
* Model messages contain the AI's responses.
*
* Important: Do not rely on `instanceof ModelMessage` to determine the message role.
* This is merely a helper class for construction. Always use `$message->getRole()`
* to check the role of a message.
*
* @since 0.1.0
*/
class ModelMessage extends \WordPress\AiClient\Messages\DTO\Message
{
/**
* Constructor.
*
* @since 0.1.0
*
* @param MessagePart[] $parts The parts that make up this message.
*/
public function __construct(array $parts)
{
parent::__construct(MessageRoleEnum::model(), $parts);
}
}
@@ -0,0 +1,31 @@
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Messages\DTO;
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;
/**
* Represents a message from a user.
*
* This is a convenience class that automatically sets the role to USER.
*
* Important: Do not rely on `instanceof UserMessage` to determine the message role.
* This is merely a helper class for construction. Always use `$message->getRole()`
* to check the role of a message.
*
* @since 0.1.0
*/
class UserMessage extends \WordPress\AiClient\Messages\DTO\Message
{
/**
* Constructor.
*
* @since 0.1.0
*
* @param MessagePart[] $parts The parts that make up this message.
*/
public function __construct(array $parts)
{
parent::__construct(MessageRoleEnum::user(), $parts);
}
}