Skip to content

Commit eebc2a5

Browse files
authored
Merge branch 'trunk' into add/build-script-results-check
2 parents 6b5a327 + 58ecd43 commit eebc2a5

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

src/wp-includes/php-ai-client/src/AiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class AiClient
8484
/**
8585
* @var string The version of the AI Client.
8686
*/
87-
public const VERSION = '1.3.0';
87+
public const VERSION = '1.3.1';
8888
/**
8989
* @var ProviderRegistry|null The default provider registry instance.
9090
*/

src/wp-includes/php-ai-client/src/Common/AbstractDataTransferObject.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ private function convertEmptyArraysToObjects($data, array $schema)
112112
$data[$index] = $this->convertEmptyArraysToObjects($item, $schema['items']);
113113
}
114114
}
115-
// Handle oneOf schemas - just use the first one
116-
if (isset($schema['oneOf']) && is_array($schema['oneOf'])) {
117-
foreach ($schema['oneOf'] as $possibleSchema) {
118-
if (is_array($possibleSchema)) {
119-
return $this->convertEmptyArraysToObjects($data, $possibleSchema);
115+
// Handle oneOf/anyOf schemas - just use the first one
116+
foreach (['oneOf', 'anyOf'] as $keyword) {
117+
if (isset($schema[$keyword]) && is_array($schema[$keyword])) {
118+
foreach ($schema[$keyword] as $possibleSchema) {
119+
if (is_array($possibleSchema)) {
120+
return $this->convertEmptyArraysToObjects($data, $possibleSchema);
121+
}
120122
}
121123
}
122124
}

src/wp-includes/php-ai-client/src/Providers/DTO/ProviderMetadata.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace WordPress\AiClient\Providers\DTO;
55

66
use WordPress\AiClient\Common\AbstractDataTransferObject;
7+
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
78
use WordPress\AiClient\Providers\Enums\ProviderTypeEnum;
89
use WordPress\AiClient\Providers\Http\Enums\RequestAuthenticationMethod;
910
/**
@@ -79,9 +80,17 @@ class ProviderMetadata extends AbstractDataTransferObject
7980
* @param RequestAuthenticationMethod|null $authenticationMethod The authentication method.
8081
* @param string|null $description The provider's description.
8182
* @param string|null $logoPath The full path to the provider's logo image file.
83+
* @throws InvalidArgumentException If the provider ID contains invalid characters.
8284
*/
8385
public function __construct(string $id, string $name, ProviderTypeEnum $type, ?string $credentialsUrl = null, ?RequestAuthenticationMethod $authenticationMethod = null, ?string $description = null, ?string $logoPath = null)
8486
{
87+
if (!preg_match('/^[a-z0-9\-_]+$/', $id)) {
88+
throw new InvalidArgumentException(sprintf(
89+
// phpcs:ignore Generic.Files.LineLength.TooLong
90+
'Invalid provider ID "%s". Only lowercase alphanumeric characters, hyphens, and underscores are allowed.',
91+
$id
92+
));
93+
}
8594
$this->id = $id;
8695
$this->name = $name;
8796
$this->description = $description;

src/wp-includes/php-ai-client/src/Tools/DTO/FunctionCall.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getArgs()
9393
*/
9494
public static function getJsonSchema(): array
9595
{
96-
return ['type' => 'object', 'properties' => [self::KEY_ID => ['type' => 'string', 'description' => 'Unique identifier for this function call.'], self::KEY_NAME => ['type' => 'string', 'description' => 'The name of the function to call.'], self::KEY_ARGS => ['type' => ['string', 'number', 'boolean', 'object', 'array', 'null'], 'description' => 'The arguments to pass to the function.']], 'oneOf' => [['required' => [self::KEY_ID]], ['required' => [self::KEY_NAME]]]];
96+
return ['type' => 'object', 'properties' => [self::KEY_ID => ['type' => 'string', 'description' => 'Unique identifier for this function call.'], self::KEY_NAME => ['type' => 'string', 'description' => 'The name of the function to call.'], self::KEY_ARGS => ['type' => ['string', 'number', 'boolean', 'object', 'array', 'null'], 'description' => 'The arguments to pass to the function.']], 'anyOf' => [['required' => [self::KEY_ID]], ['required' => [self::KEY_NAME]]]];
9797
}
9898
/**
9999
* {@inheritDoc}

src/wp-includes/php-ai-client/src/Tools/DTO/FunctionResponse.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @since 0.1.0
1515
*
16-
* @phpstan-type FunctionResponseArrayShape array{id: string, name: string, response: mixed}
16+
* @phpstan-type FunctionResponseArrayShape array{id?: string, name?: string, response: mixed}
1717
*
1818
* @extends AbstractDataTransferObject<FunctionResponseArrayShape>
1919
*/
@@ -23,13 +23,13 @@ class FunctionResponse extends AbstractDataTransferObject
2323
public const KEY_NAME = 'name';
2424
public const KEY_RESPONSE = 'response';
2525
/**
26-
* @var string The ID of the function call this is responding to.
26+
* @var string|null The ID of the function call this is responding to.
2727
*/
28-
private string $id;
28+
private ?string $id;
2929
/**
30-
* @var string The name of the function that was called.
30+
* @var string|null The name of the function that was called.
3131
*/
32-
private string $name;
32+
private ?string $name;
3333
/**
3434
* @var mixed The response data from the function.
3535
*/
@@ -39,12 +39,16 @@ class FunctionResponse extends AbstractDataTransferObject
3939
*
4040
* @since 0.1.0
4141
*
42-
* @param string $id The ID of the function call this is responding to.
43-
* @param string $name The name of the function that was called.
42+
* @param string|null $id The ID of the function call this is responding to.
43+
* @param string|null $name The name of the function that was called.
4444
* @param mixed $response The response data from the function.
45+
* @throws InvalidArgumentException If neither id nor name is provided.
4546
*/
46-
public function __construct(string $id, string $name, $response)
47+
public function __construct(?string $id, ?string $name, $response)
4748
{
49+
if ($id === null && $name === null) {
50+
throw new InvalidArgumentException('At least one of id or name must be provided.');
51+
}
4852
$this->id = $id;
4953
$this->name = $name;
5054
$this->response = $response;
@@ -89,7 +93,7 @@ public function getResponse()
8993
*/
9094
public static function getJsonSchema(): array
9195
{
92-
return ['type' => 'object', 'properties' => [self::KEY_ID => ['type' => 'string', 'description' => 'The ID of the function call this is responding to.'], self::KEY_NAME => ['type' => 'string', 'description' => 'The name of the function that was called.'], self::KEY_RESPONSE => ['type' => ['string', 'number', 'boolean', 'object', 'array', 'null'], 'description' => 'The response data from the function.']], 'oneOf' => [['required' => [self::KEY_RESPONSE, self::KEY_ID]], ['required' => [self::KEY_RESPONSE, self::KEY_NAME]]]];
96+
return ['type' => 'object', 'properties' => [self::KEY_ID => ['type' => 'string', 'description' => 'The ID of the function call this is responding to.'], self::KEY_NAME => ['type' => 'string', 'description' => 'The name of the function that was called.'], self::KEY_RESPONSE => ['type' => ['string', 'number', 'boolean', 'object', 'array', 'null'], 'description' => 'The response data from the function.']], 'anyOf' => [['required' => [self::KEY_RESPONSE, self::KEY_ID]], ['required' => [self::KEY_RESPONSE, self::KEY_NAME]]]];
9397
}
9498
/**
9599
* {@inheritDoc}
@@ -100,7 +104,15 @@ public static function getJsonSchema(): array
100104
*/
101105
public function toArray(): array
102106
{
103-
return [self::KEY_ID => $this->id, self::KEY_NAME => $this->name, self::KEY_RESPONSE => $this->response];
107+
$data = [];
108+
if ($this->id !== null) {
109+
$data[self::KEY_ID] = $this->id;
110+
}
111+
if ($this->name !== null) {
112+
$data[self::KEY_NAME] = $this->name;
113+
}
114+
$data[self::KEY_RESPONSE] = $this->response;
115+
return $data;
104116
}
105117
/**
106118
* {@inheritDoc}
@@ -110,10 +122,6 @@ public function toArray(): array
110122
public static function fromArray(array $array): self
111123
{
112124
static::validateFromArrayData($array, [self::KEY_RESPONSE]);
113-
// Validate that at least one of id or name is provided
114-
if (!array_key_exists(self::KEY_ID, $array) && !array_key_exists(self::KEY_NAME, $array)) {
115-
throw new InvalidArgumentException('At least one of id or name must be provided.');
116-
}
117125
return new self($array[self::KEY_ID] ?? null, $array[self::KEY_NAME] ?? null, $array[self::KEY_RESPONSE]);
118126
}
119127
}

0 commit comments

Comments
 (0)