Initial commit: AuroraX
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Tools\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
/**
|
||||
* Represents a function call request from an AI model.
|
||||
*
|
||||
* This DTO encapsulates information about a function that the AI model
|
||||
* wants to invoke, including the function name and its arguments.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type FunctionCallArrayShape array{id?: string, name?: string, args?: mixed}
|
||||
*
|
||||
* @extends AbstractDataTransferObject<FunctionCallArrayShape>
|
||||
*/
|
||||
class FunctionCall extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_ID = 'id';
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_ARGS = 'args';
|
||||
/**
|
||||
* @var string|null Unique identifier for this function call.
|
||||
*/
|
||||
private ?string $id;
|
||||
/**
|
||||
* @var string|null The name of the function to call.
|
||||
*/
|
||||
private ?string $name;
|
||||
/**
|
||||
* @var mixed The arguments to pass to the function.
|
||||
*/
|
||||
private $args;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string|null $id Unique identifier for this function call.
|
||||
* @param string|null $name The name of the function to call.
|
||||
* @param mixed $args The arguments to pass to the function.
|
||||
* @throws InvalidArgumentException If neither id nor name is provided.
|
||||
*/
|
||||
public function __construct(?string $id = null, ?string $name = null, $args = null)
|
||||
{
|
||||
if ($id === null && $name === null) {
|
||||
throw new InvalidArgumentException('At least one of id or name must be provided.');
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->args = $args;
|
||||
}
|
||||
/**
|
||||
* Gets the function call ID.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The function call ID.
|
||||
*/
|
||||
public function getId(): ?string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
/**
|
||||
* Gets the function name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The function name.
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Gets the function arguments.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return mixed The function arguments.
|
||||
*/
|
||||
public function getArgs()
|
||||
{
|
||||
return $this->args;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
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]]]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FunctionCallArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [];
|
||||
if ($this->id !== null) {
|
||||
$data[self::KEY_ID] = $this->id;
|
||||
}
|
||||
if ($this->name !== null) {
|
||||
$data[self::KEY_NAME] = $this->name;
|
||||
}
|
||||
if ($this->args !== null) {
|
||||
$data[self::KEY_ARGS] = $this->args;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self($array[self::KEY_ID] ?? null, $array[self::KEY_NAME] ?? null, $array[self::KEY_ARGS] ?? null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Tools\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
/**
|
||||
* Represents a function declaration for AI models.
|
||||
*
|
||||
* This DTO describes a function that can be called by the AI model,
|
||||
* including its name, description, and parameter schema.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type FunctionDeclarationArrayShape array{
|
||||
* name: string,
|
||||
* description: string,
|
||||
* parameters?: array<string, mixed>
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<FunctionDeclarationArrayShape>
|
||||
*/
|
||||
class FunctionDeclaration extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_DESCRIPTION = 'description';
|
||||
public const KEY_PARAMETERS = 'parameters';
|
||||
/**
|
||||
* @var string The name of the function.
|
||||
*/
|
||||
private string $name;
|
||||
/**
|
||||
* @var string A description of what the function does.
|
||||
*/
|
||||
private string $description;
|
||||
/**
|
||||
* @var array<string, mixed>|null The JSON schema for the function parameters.
|
||||
*/
|
||||
private ?array $parameters;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $name The name of the function.
|
||||
* @param string $description A description of what the function does.
|
||||
* @param array<string, mixed>|null $parameters The JSON schema for the function parameters.
|
||||
*/
|
||||
public function __construct(string $name, string $description, ?array $parameters = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->description = $description;
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
/**
|
||||
* Gets the function name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The function name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Gets the function description.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The function description.
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
/**
|
||||
* Gets the function parameters schema.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return array<string, mixed>|null The parameters schema.
|
||||
*/
|
||||
public function getParameters(): ?array
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_NAME => ['type' => 'string', 'description' => 'The name of the function.'], self::KEY_DESCRIPTION => ['type' => 'string', 'description' => 'A description of what the function does.'], self::KEY_PARAMETERS => ['type' => 'object', 'description' => 'The JSON schema for the function parameters.', 'additionalProperties' => \true]], 'required' => [self::KEY_NAME, self::KEY_DESCRIPTION]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FunctionDeclarationArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [self::KEY_NAME => $this->name, self::KEY_DESCRIPTION => $this->description];
|
||||
if ($this->parameters !== null) {
|
||||
$data[self::KEY_PARAMETERS] = $this->parameters;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_NAME, self::KEY_DESCRIPTION]);
|
||||
return new self($array[self::KEY_NAME], $array[self::KEY_DESCRIPTION], $array[self::KEY_PARAMETERS] ?? null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Tools\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
/**
|
||||
* Represents a response to a function call.
|
||||
*
|
||||
* This DTO encapsulates the result of executing a function that was
|
||||
* requested by the AI model through a FunctionCall.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type FunctionResponseArrayShape array{id?: string, name?: string, response: mixed}
|
||||
*
|
||||
* @extends AbstractDataTransferObject<FunctionResponseArrayShape>
|
||||
*/
|
||||
class FunctionResponse extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_ID = 'id';
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_RESPONSE = 'response';
|
||||
/**
|
||||
* @var string|null The ID of the function call this is responding to.
|
||||
*/
|
||||
private ?string $id;
|
||||
/**
|
||||
* @var string|null The name of the function that was called.
|
||||
*/
|
||||
private ?string $name;
|
||||
/**
|
||||
* @var mixed The response data from the function.
|
||||
*/
|
||||
private $response;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string|null $id The ID of the function call this is responding to.
|
||||
* @param string|null $name The name of the function that was called.
|
||||
* @param mixed $response The response data from the function.
|
||||
* @throws InvalidArgumentException If neither id nor name is provided.
|
||||
*/
|
||||
public function __construct(?string $id, ?string $name, $response)
|
||||
{
|
||||
if ($id === null && $name === null) {
|
||||
throw new InvalidArgumentException('At least one of id or name must be provided.');
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->response = $response;
|
||||
}
|
||||
/**
|
||||
* Gets the function call ID.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The function call ID.
|
||||
*/
|
||||
public function getId(): ?string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
/**
|
||||
* Gets the function name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The function name.
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Gets the function response.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return mixed The response data.
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
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]]]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FunctionResponseArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [];
|
||||
if ($this->id !== null) {
|
||||
$data[self::KEY_ID] = $this->id;
|
||||
}
|
||||
if ($this->name !== null) {
|
||||
$data[self::KEY_NAME] = $this->name;
|
||||
}
|
||||
$data[self::KEY_RESPONSE] = $this->response;
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_RESPONSE]);
|
||||
return new self($array[self::KEY_ID] ?? null, $array[self::KEY_NAME] ?? null, $array[self::KEY_RESPONSE]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Tools\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
/**
|
||||
* Represents web search configuration for AI models.
|
||||
*
|
||||
* This DTO defines constraints for web searches that AI models can perform,
|
||||
* including allowed and disallowed domains.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type WebSearchArrayShape array{allowedDomains?: string[], disallowedDomains?: string[]}
|
||||
*
|
||||
* @extends AbstractDataTransferObject<WebSearchArrayShape>
|
||||
*/
|
||||
class WebSearch extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_ALLOWED_DOMAINS = 'allowedDomains';
|
||||
public const KEY_DISALLOWED_DOMAINS = 'disallowedDomains';
|
||||
/**
|
||||
* @var string[] List of domains that are allowed for web search.
|
||||
*/
|
||||
private array $allowedDomains;
|
||||
/**
|
||||
* @var string[] List of domains that are disallowed for web search.
|
||||
*/
|
||||
private array $disallowedDomains;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string[] $allowedDomains List of domains that are allowed for web search.
|
||||
* @param string[] $disallowedDomains List of domains that are disallowed for web search.
|
||||
*/
|
||||
public function __construct(array $allowedDomains = [], array $disallowedDomains = [])
|
||||
{
|
||||
$this->allowedDomains = $allowedDomains;
|
||||
$this->disallowedDomains = $disallowedDomains;
|
||||
}
|
||||
/**
|
||||
* Gets the allowed domains.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string[] The allowed domains.
|
||||
*/
|
||||
public function getAllowedDomains(): array
|
||||
{
|
||||
return $this->allowedDomains;
|
||||
}
|
||||
/**
|
||||
* Gets the disallowed domains.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string[] The disallowed domains.
|
||||
*/
|
||||
public function getDisallowedDomains(): array
|
||||
{
|
||||
return $this->disallowedDomains;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_ALLOWED_DOMAINS => ['type' => 'array', 'items' => ['type' => 'string'], 'description' => 'List of domains that are allowed for web search.'], self::KEY_DISALLOWED_DOMAINS => ['type' => 'array', 'items' => ['type' => 'string'], 'description' => 'List of domains that are disallowed for web search.']], 'required' => []];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return WebSearchArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [self::KEY_ALLOWED_DOMAINS => $this->allowedDomains, self::KEY_DISALLOWED_DOMAINS => $this->disallowedDomains];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self($array[self::KEY_ALLOWED_DOMAINS] ?? [], $array[self::KEY_DISALLOWED_DOMAINS] ?? []);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user