base.php
2 years ago
embed.php
2 years ago
function.php
2 years ago
image.php
2 years ago
parameter.php
2 years ago
text.php
2 years ago
transcribe.php
2 years ago
function.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Function implements JsonSerializable { |
| 4 | public string $name; |
| 5 | public string $description; |
| 6 | public array $parameters; |
| 7 | public string $type; |
| 8 | |
| 9 | public function __construct( string $name, string $description, array $parameters = [], string $type = 'PHP' ) { |
| 10 | // $name: The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. |
| 11 | if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { |
| 12 | throw new InvalidArgumentException( "Invalid function name ($name). It must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64." ); |
| 13 | } |
| 14 | |
| 15 | foreach ( $parameters as $parameter ) { |
| 16 | if ( !( $parameter instanceof Meow_MWAI_Query_Parameter ) ) { |
| 17 | throw new InvalidArgumentException( "Invalid parameter." ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | $this->name = $name; |
| 22 | $this->description = $description; |
| 23 | $this->parameters = $parameters; |
| 24 | $this->type = $type; |
| 25 | } |
| 26 | |
| 27 | public function jsonSerialize() { |
| 28 | $params = []; |
| 29 | foreach( $this->parameters as $parameter ) { |
| 30 | $params[ $parameter->name ] = $parameter; |
| 31 | } |
| 32 | $required = array_filter( $this->parameters, function( $param ) { return $param->required; } ); |
| 33 | $required = array_map( function( $param ) { return $param->name; }, $required ); |
| 34 | $json = [ |
| 35 | 'name' => $this->name, |
| 36 | 'description' => $this->description, |
| 37 | 'parameters' => [ |
| 38 | 'type' => 'object', |
| 39 | 'properties' => $params, |
| 40 | ], |
| 41 | 'required' => $required |
| 42 | ]; |
| 43 | return $json; |
| 44 | } |
| 45 | } |
| 46 |