Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
13aaba0
chore: simplifies type
JasonTheAdams Jul 26, 2025
0b52ffd
feat: adds json serialization to DTOs
JasonTheAdams Jul 28, 2025
0c0407c
refactor: moves enum trait to traits directory
JasonTheAdams Jul 28, 2025
03f66ff
test: adds serialization tests
JasonTheAdams Jul 28, 2025
a350b57
feat: improves fromJson validation and documenting
JasonTheAdams Jul 28, 2025
f6a1605
refactor: clean up typing with generics
JasonTheAdams Jul 28, 2025
cf5b304
refactor: further cleaning up of types
JasonTheAdams Jul 28, 2025
404699a
refactor: improves Message::toJson types
JasonTheAdams Jul 28, 2025
18ffdb4
refactor: changes to array terminology over json
JasonTheAdams Jul 28, 2025
cb4406e
refactor: adds abstract DTO with json serialization
JasonTheAdams Jul 28, 2025
f17f662
refactor: simplifies File::fromArray
JasonTheAdams Jul 28, 2025
1f77a32
refactor: simplifies Message::fromArray
JasonTheAdams Jul 28, 2025
3bc7064
refactor: simplifies MessagePart::fromArray
JasonTheAdams Jul 28, 2025
a9629c4
refactor: simplifies GenerativeAiResult
JasonTheAdams Jul 28, 2025
0f8f66d
fix: corrects incorrect types
JasonTheAdams Jul 28, 2025
18afbb5
refactor: simplifies GenerativeAiResult::fromArray
JasonTheAdams Jul 28, 2025
a66a56b
fix: corrects TokenUsage types
JasonTheAdams Jul 28, 2025
5bef6fb
refactor: simplifies FunctionCall
JasonTheAdams Jul 28, 2025
eac5146
refactor: simplifies Tool::fromArray
JasonTheAdams Jul 28, 2025
2811597
refactor: simplifies WebSearch
JasonTheAdams Jul 28, 2025
3e36f0f
refactor: simplifies Fiel and MessagePart toArray
JasonTheAdams Jul 29, 2025
b6890c8
chore: corrects too specific Message return type
JasonTheAdams Jul 29, 2025
cb985cc
fix: adds missing imports
JasonTheAdams Jul 29, 2025
25f0491
feat: adds fromArray key validation
JasonTheAdams Jul 29, 2025
87d9c67
fix: corrects linting issue
JasonTheAdams Jul 29, 2025
30ee59c
refactor: adds schema constants
JasonTheAdams Jul 29, 2025
59514c3
test: adds missing import
JasonTheAdams Jul 29, 2025
59e586c
refactor: removes need for final DTOs
JasonTheAdams Jul 29, 2025
c4c6452
fix: checks for key existence, allowing null values
JasonTheAdams Jul 29, 2025
342c07a
refacor: removes non-required key validation
JasonTheAdams Jul 29, 2025
8ded94d
feat: validates that successful oepration has result
JasonTheAdams Jul 29, 2025
6b48bef
refactor: removes potentially problematic schema condition
JasonTheAdams Jul 29, 2025
3eb5279
fix: corrects either id or name to be required
JasonTheAdams Jul 29, 2025
7d53157
fix: removes unnecessary Tool type validation
JasonTheAdams Jul 29, 2025
75a8034
chore: marks mimeType required
JasonTheAdams Jul 30, 2025
19b4692
chore: fixes since tags
JasonTheAdams Jul 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Common/Contracts/WithJsonSerialization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Common\Contracts;

use JsonSerializable;

/**
* Interface for objects that support JSON serialization and deserialization.
*
* @since 1.0.0
*/
interface WithJsonSerialization extends JsonSerializable
{
/**
* Creates an instance from JSON data.
*
* @since 1.0.0
*
* @param array<string, mixed> $json The JSON data.
* @return self The created instance.
*/
public static function fromJson(array $json);
}
44 changes: 43 additions & 1 deletion src/Files/DTO/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Files\DTO;

use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
use WordPress\AiClient\Files\Enums\FileTypeEnum;
use WordPress\AiClient\Files\ValueObjects\MimeType;

Expand All @@ -16,7 +17,7 @@
*
* @since n.e.x.t
*/
class File implements WithJsonSchemaInterface
class File implements WithJsonSchemaInterface, WithJsonSerialization
{
/**
* @var MimeType The MIME type of the file.
Expand Down Expand Up @@ -377,4 +378,45 @@
],
];
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$data = [
'fileType' => $this->fileType->value,
'mimeType' => $this->getMimeType(),
];

if ($this->fileType->isRemote()) {
$data['url'] = $this->url;
} else {
$data['base64Data'] = $this->base64Data;
}

return $data;
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): File
{
$fileType = FileTypeEnum::from((string) $json['fileType']);

Check failure on line 412 in src/Files/DTO/File.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.

if ($fileType->isRemote()) {
return new self((string) $json['url'], (string) $json['mimeType']);

Check failure on line 415 in src/Files/DTO/File.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.

Check failure on line 415 in src/Files/DTO/File.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.
} else {
// Create data URI from base64 data and mime type
$dataUri = sprintf('data:%s;base64,%s', (string) $json['mimeType'], (string) $json['base64Data']);

Check failure on line 418 in src/Files/DTO/File.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.

Check failure on line 418 in src/Files/DTO/File.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.
return new self($dataUri);
}
}
}
37 changes: 36 additions & 1 deletion src/Messages/DTO/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Messages\DTO;

use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;

/**
Expand All @@ -15,7 +16,7 @@
*
* @since n.e.x.t
*/
class Message implements WithJsonSchemaInterface
class Message implements WithJsonSchemaInterface, WithJsonSerialization
{
/**
* @var MessageRoleEnum The role of the message sender.
Expand Down Expand Up @@ -90,4 +91,38 @@
'required' => ['role', 'parts'],
];
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'role' => $this->role->value,
'parts' => array_map(function (MessagePart $part) {
return $part->jsonSerialize();
}, $this->parts),
];
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): Message
{
$role = MessageRoleEnum::from((string) $json['role']);

Check failure on line 119 in src/Messages/DTO/Message.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.
/** @var array<array<string, mixed>> $partsData */
$partsData = $json['parts'];
$parts = array_map(function (array $partData) {
return MessagePart::fromJson($partData);
}, $partsData);

return new self($role, $parts);
}
}
55 changes: 54 additions & 1 deletion src/Messages/DTO/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Messages\DTO;

use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
use WordPress\AiClient\Files\DTO\File;
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
use WordPress\AiClient\Tools\DTO\FunctionCall;
Expand All @@ -18,7 +19,7 @@
*
* @since n.e.x.t
*/
class MessagePart implements WithJsonSchemaInterface
class MessagePart implements WithJsonSchemaInterface, WithJsonSerialization
{
/**
* @var MessagePartTypeEnum The type of this message part.
Expand Down Expand Up @@ -202,4 +203,56 @@
],
];
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$data = ['type' => $this->type->value];

if ($this->type->isText() && $this->text !== null) {
$data['text'] = $this->text;
} elseif ($this->type->isFile() && $this->file !== null) {
$data['file'] = $this->file->jsonSerialize();
} elseif ($this->type->isFunctionCall() && $this->functionCall !== null) {
$data['functionCall'] = $this->functionCall->jsonSerialize();
} elseif ($this->type->isFunctionResponse() && $this->functionResponse !== null) {
$data['functionResponse'] = $this->functionResponse->jsonSerialize();
}

return $data;
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): MessagePart
{
$type = MessagePartTypeEnum::from((string) $json['type']);

Check failure on line 238 in src/Messages/DTO/MessagePart.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.

if ($type->isText()) {
return new self((string) $json['text']);

Check failure on line 241 in src/Messages/DTO/MessagePart.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.
} elseif ($type->isFile()) {
/** @var array<string, mixed> $fileData */
$fileData = $json['file'];
return new self(File::fromJson($fileData));
} elseif ($type->isFunctionCall()) {
/** @var array<string, mixed> $functionCallData */
$functionCallData = $json['functionCall'];
return new self(FunctionCall::fromJson($functionCallData));
} elseif ($type->isFunctionResponse()) {
/** @var array<string, mixed> $functionResponseData */
$functionResponseData = $json['functionResponse'];
return new self(FunctionResponse::fromJson($functionResponseData));
}

throw new \InvalidArgumentException(sprintf('Unknown message part type: %s', $json['type']));

Check failure on line 256 in src/Messages/DTO/MessagePart.php

View workflow job for this annotation

GitHub Actions / PHP

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
}
}
16 changes: 16 additions & 0 deletions src/Messages/DTO/ModelMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public function __construct(array $parts)
{
parent::__construct(MessageRoleEnum::model(), $parts);
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): ModelMessage
{
/** @var array<array<string, mixed>> $partsData */
$partsData = $json['parts'];
$parts = array_map(function (array $partData) {
return MessagePart::fromJson($partData);
}, $partsData);

return new self($parts);
}
}
16 changes: 16 additions & 0 deletions src/Messages/DTO/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public function __construct(array $parts)
{
parent::__construct(MessageRoleEnum::system(), $parts);
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): SystemMessage
{
/** @var array<array<string, mixed>> $partsData */
$partsData = $json['parts'];
$parts = array_map(function (array $partData) {
return MessagePart::fromJson($partData);
}, $partsData);

return new self($parts);
}
}
16 changes: 16 additions & 0 deletions src/Messages/DTO/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ public function __construct(array $parts)
{
parent::__construct(MessageRoleEnum::user(), $parts);
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): UserMessage
{
/** @var array<array<string, mixed>> $partsData */
$partsData = $json['parts'];
$parts = array_map(function (array $partData) {
return MessagePart::fromJson($partData);
}, $partsData);

return new self($parts);
}
}
3 changes: 2 additions & 1 deletion src/Operations/Contracts/OperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Operations\Contracts;

use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
use WordPress\AiClient\Operations\Enums\OperationStateEnum;

/**
Expand All @@ -15,7 +16,7 @@
*
* @since n.e.x.t
*/
interface OperationInterface extends WithJsonSchemaInterface
interface OperationInterface extends WithJsonSchemaInterface, WithJsonSerialization
{
/**
* Gets the operation ID.
Expand Down
39 changes: 39 additions & 0 deletions src/Operations/DTO/GenerativeAiOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,43 @@
],
];
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$data = [
'id' => $this->id,
'state' => $this->state->value,
];

if ($this->result !== null) {
$data['result'] = $this->result->jsonSerialize();
}

return $data;
}

/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
public static function fromJson(array $json): GenerativeAiOperation
{
$state = OperationStateEnum::from((string) $json['state']);

Check failure on line 164 in src/Operations/DTO/GenerativeAiOperation.php

View workflow job for this annotation

GitHub Actions / PHP

Cannot cast mixed to string.
$result = null;
if (isset($json['result'])) {
/** @var array<string, mixed> $resultData */
$resultData = $json['result'];
$result = GenerativeAiResult::fromJson($resultData);
}

return new self((string) $json['id'], $state, $result);
}
}
3 changes: 2 additions & 1 deletion src/Results/Contracts/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WordPress\AiClient\Results\Contracts;

use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
use WordPress\AiClient\Results\DTO\TokenUsage;

/**
Expand All @@ -15,7 +16,7 @@
*
* @since n.e.x.t
*/
interface ResultInterface extends WithJsonSchemaInterface
interface ResultInterface extends WithJsonSchemaInterface, WithJsonSerialization
{
/**
* Gets the result ID.
Expand Down
Loading
Loading