assist-feedback.php
10 months ago
assistant.php
10 months ago
base.php
10 months ago
dropped-file.php
11 months ago
edit-image.php
11 months ago
embed.php
10 months ago
feedback.php
10 months ago
function.php
11 months ago
image.php
10 months ago
parameter.php
11 months ago
text.php
10 months ago
transcribe.php
11 months ago
function.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Function { |
| 4 | public string $name; |
| 5 | public string $description; |
| 6 | public array $parameters; |
| 7 | public string $type; // 'code-engine', etc... |
| 8 | public string $target; // 'server' or 'client' |
| 9 | public ?string $id; |
| 10 | |
| 11 | public function __construct( |
| 12 | string $name, |
| 13 | string $description, |
| 14 | array $parameters = [], |
| 15 | string $type = null, |
| 16 | string $id = null, |
| 17 | string $target = null |
| 18 | ) { |
| 19 | // $name: The name of the function to be called. |
| 20 | // Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. |
| 21 | if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { |
| 22 | throw new InvalidArgumentException( "AI Engine: Invalid function name ($name) for Meow_MWAI_Query_Function." ); |
| 23 | } |
| 24 | |
| 25 | foreach ( $parameters as $parameter ) { |
| 26 | if ( !( $parameter instanceof Meow_MWAI_Query_Parameter ) ) { |
| 27 | throw new InvalidArgumentException( 'AI Engine: Invalid parameter for Meow_MWAI_Query_Function.' ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | $this->name = $name; |
| 32 | $this->description = $description; |
| 33 | $this->parameters = $parameters; |
| 34 | $this->type = $type ?? 'manual'; |
| 35 | $this->id = $id; |
| 36 | $this->target = $target ?? 'server'; |
| 37 | } |
| 38 | |
| 39 | public function serializeForOpenAI() { |
| 40 | // Initialize the base structure with name and description |
| 41 | $json = [ 'name' => $this->name, 'description' => $this->description ]; |
| 42 | |
| 43 | // Check if parameters are set and not empty |
| 44 | if ( !empty( $this->parameters ) ) { |
| 45 | $properties = []; |
| 46 | $required = []; |
| 47 | |
| 48 | // Loop through each parameter to construct the properties object |
| 49 | foreach ( $this->parameters as $parameter ) { |
| 50 | |
| 51 | $properties[$parameter->name] = [ |
| 52 | 'type' => $parameter->type, // Assuming each parameter has a 'type' attribute |
| 53 | 'description' => $parameter->description, // Assuming each parameter has a 'description' attribute |
| 54 | ]; |
| 55 | |
| 56 | // If the parameter type is "array" and has a "items" attribute, include it |
| 57 | if ( $parameter->type === 'array' ) { |
| 58 | $properties[$parameter->name]['items'] = [ |
| 59 | 'type' => 'string', // Assuming the items are strings |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | // If an enum is set for the parameter, include it |
| 64 | if ( isset( $parameter->enum ) ) { |
| 65 | $properties[$parameter->name]['enum'] = $parameter->enum; |
| 66 | } |
| 67 | |
| 68 | // If the parameter is required, add its name to the required array |
| 69 | if ( $parameter->required ) { |
| 70 | $required[] = $parameter->name; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Assemble the parameters part of the JSON |
| 75 | $json['parameters'] = [ |
| 76 | 'type' => 'object', |
| 77 | 'properties' => $properties, |
| 78 | 'required' => $required, |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | return $json; |
| 83 | } |
| 84 | |
| 85 | public function serializeForAnthropic() { |
| 86 | $json = [ |
| 87 | 'name' => $this->name, |
| 88 | 'description' => $this->description, |
| 89 | 'input_schema' => [ |
| 90 | 'type' => 'object', |
| 91 | 'properties' => new stdClass() |
| 92 | ], |
| 93 | ]; |
| 94 | |
| 95 | if ( !empty( $this->parameters ) ) { |
| 96 | $properties = []; |
| 97 | $required = []; |
| 98 | foreach ( $this->parameters as $parameter ) { |
| 99 | $properties[$parameter->name] = [ |
| 100 | 'type' => $parameter->type, |
| 101 | 'description' => $parameter->description, |
| 102 | ]; |
| 103 | if ( isset( $parameter->enum ) ) { |
| 104 | $properties[$parameter->name]['enum'] = $parameter->enum; |
| 105 | } |
| 106 | if ( $parameter->required ) { |
| 107 | $required[] = $parameter->name; |
| 108 | } |
| 109 | } |
| 110 | $json['input_schema']['properties'] = empty( $properties ) ? new stdClass() : $properties; |
| 111 | if ( !empty( $required ) ) { |
| 112 | $json['input_schema']['required'] = $required; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return $json; |
| 117 | } |
| 118 | |
| 119 | public static function fromJson( array $json ): Meow_MWAI_Query_Function { |
| 120 | $funcName = $json['name']; |
| 121 | $funcDesc = $json['description'] ?? ''; |
| 122 | $funcType = $json['type'] ?? null; |
| 123 | $funcId = $json['id'] ?? null; |
| 124 | $funcTarget = $json['target'] ?? null; |
| 125 | if ( $funcId === null && !empty( $json['snippetId'] ) ) { |
| 126 | $funcId = $json['snippetId']; |
| 127 | } |
| 128 | $args = []; |
| 129 | if ( !empty( $json['args'] ) ) { |
| 130 | foreach ( $json['args'] as $arg ) { |
| 131 | $name = ltrim( $arg['name'], '$' ); |
| 132 | $desc = $arg['description'] ?? null; |
| 133 | $type = $arg['type'] ?? 'string'; |
| 134 | $required = $arg['required'] ?? false; |
| 135 | $args[] = new Meow_MWAI_Query_Parameter( $name, $desc, $type, $required ); |
| 136 | } |
| 137 | } |
| 138 | return new self( $funcName, $funcDesc, $args, $funcType, $funcId, $funcTarget ); |
| 139 | } |
| 140 | |
| 141 | public static function toJson( Meow_MWAI_Query_Function $function ): array { |
| 142 | $json = [ |
| 143 | 'name' => $function->name, |
| 144 | 'desc' => $function->description, |
| 145 | 'type' => $function->type, |
| 146 | 'id' => $function->id, |
| 147 | 'target' => $function->target, |
| 148 | 'args' => [], |
| 149 | ]; |
| 150 | |
| 151 | foreach ( $function->parameters as $param ) { |
| 152 | $json['args'][] = [ |
| 153 | 'name' => $param->name, |
| 154 | 'desc' => $param->description, |
| 155 | 'type' => $param->type, |
| 156 | 'required' => $param->required, |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | return $json; |
| 161 | } |
| 162 | } |
| 163 |