| @@ -26,9 +26,26 @@ | ||
| 26 | 26 | $snippet = $this->snippet->get_function( $id, $options ); |
| 27 | 27 | return $snippet; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | + | |
| 30 | 31 | /** |
| 32 | + * Get a function by its name. | |
| 33 | + * | |
| 34 | + * The options are used to filter the functions: | |
| 35 | + * - 'php_ready_args' (bool): If false, the arguments will not be formatted for PHP. (no $ before the names). | |
| 36 | + * | |
| 37 | + * @param $name The name of the function to be retrieved. | |
| 38 | + * @param array $options | |
| 39 | + * | |
| 40 | + * @return mixed | |
| 41 | + */ | |
| 42 | + public function get_function_by_name( $name, $options = [] ) { | |
| 43 | + $snippet = $this->snippet->get_function_by_name( $name, $options ); | |
| 44 | + return $snippet; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 31 | 48 | * Get all functions. |
| 32 | 49 | * |
| 33 | 50 | * @return mixed |
| 34 | 51 | * |
| @@ -51,8 +68,9 @@ | ||
| 51 | 68 | |
| 52 | 69 | return $snippets; |
| 53 | 70 | } |
| 54 | 71 | |
| 72 | + | |
| 55 | 73 | /** |
| 56 | 74 | * Execute a function by its ID. |
| 57 | 75 | * The arguments should be an associative array with the argument names as keys. |
| 58 | 76 | * Example: [ "$city" => " 'Tokyo' ", "$date" => "1999" ] |
| @@ -65,5 +83,129 @@ | ||
| 65 | 83 | public function execute_function( $id, $args ) { |
| 66 | 84 | $output = $this->core->run_snippet( $id, $args ); |
| 67 | 85 | return $output; |
| 68 | 86 | } |
| 87 | + | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Create a new callable snippet ( Function ). | |
| 91 | + * | |
| 92 | + * @param string $name Name of the function. This should be the name of the function declaration, like 'get_weather'. | |
| 93 | + * @param string $target Target of the snippet. This should be either 'php' or 'js'. This will be used to determine the type of the function. | |
| 94 | + * @param string $code Code of the snippet. This should be either a PHP or a JS function. No code outside of the function itself should be included. | |
| 95 | + * @param string $description Description of the snippet. This will be displayed in Code Engine and used for Function Calling. | |
| 96 | + * @param array $args Arguments of the function. This array should have the full name of each argument, like ['$city', '$date']. The '$' is required if the function is PHP. | |
| 97 | + * @param array $args_data Data of the arguments. This array should have the name of each argument as key and an array with the type, description and default value. Example: [ '$city' => ['type' => 'string', 'desc' => 'City name', 'default' => 'Tokyo' ] ]. This will be used for Function Calling. | |
| 98 | + * @param string $behavior Behavior of the function. This should be either 'dynamic' or 'static'. This will be used for Function Calling. | |
| 99 | + * | |
| 100 | + */ | |
| 101 | + public function create_function( $name, $target, $code, $description, $args, $args_data, $behavior ) { | |
| 102 | + | |
| 103 | + $temp_name = 'mwcode_' . time(); | |
| 104 | + | |
| 105 | + $params = [ | |
| 106 | + // Predefined values | |
| 107 | + 'id' => null, | |
| 108 | + 'active' => 1, | |
| 109 | + 'priority' => 10, | |
| 110 | + 'tags' => ['api'], | |
| 111 | + 'endpoint' => '', | |
| 112 | + 'scope' => 'function', | |
| 113 | + | |
| 114 | + // Custom values | |
| 115 | + 'name' => $name ?? $temp_name, | |
| 116 | + 'code' => $code ?? "function $temp_name() { return 'Hello World'; }", | |
| 117 | + 'description' => $description ?? '', | |
| 118 | + | |
| 119 | + 'functionName' => $name ?? $temp_name, | |
| 120 | + 'functionTarget' => $target ?? 'php', | |
| 121 | + 'functionArgs' => $args ?? [], | |
| 122 | + 'functionArgsDict' => $args_data ?? [], | |
| 123 | + 'functionBehavior' => $behavior ?? 'dynamic', | |
| 124 | + ]; | |
| 125 | + | |
| 126 | + $res = $this->core->add_snippet( $params ); | |
| 127 | + | |
| 128 | + return $res; | |
| 129 | + } | |
| 130 | + | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Update a function by its name. If the function does not exist, it will return false. The parameters are the same as create_function, but except the name the rest are optional. If not specified, the current values will be used. | |
| 134 | + * | |
| 135 | + * @param string $name Name of the function. This should be the name of the function declaration, like 'get_weather'. | |
| 136 | + * @param string $target Target of the snippet. This should be either 'php' or 'js'. This will be used to determine the type of the function. | |
| 137 | + * @param string $code Code of the snippet. This should be either a PHP or a JS function. No code outside of the function itself should be included. | |
| 138 | + * @param string $description Description of the snippet. This will be displayed in Code Engine and used for Function Calling. | |
| 139 | + * @param array $args Arguments of the function. This array should have the full name of each argument, like ['$city', '$date']. The '$' is required if the function is PHP. | |
| 140 | + * @param array $args_data Data of the arguments. This array should have the name of each argument as key and an array with the type, description and default value. Example: [ '$city' => ['type' => 'string', 'desc' => 'City name', 'default' => 'Tokyo' ] ]. This will be used for Function Calling. | |
| 141 | + * @param string $behavior Behavior of the function. This should be either 'dynamic' or 'static'. This will be used for Function Calling. | |
| 142 | + */ | |
| 143 | + public function update_function( $name, $target, $code, $description, $args, $args_data, $behavior ) { | |
| 144 | + $snippet = $this->snippet->get_function_by_name( $name ); | |
| 145 | + if ( !$snippet ) { return false; } | |
| 146 | + | |
| 147 | + $params = [ | |
| 148 | + // Predefined values | |
| 149 | + 'id' => $snippet['snippetId'], | |
| 150 | + 'active' => 1, | |
| 151 | + 'priority' => 10, | |
| 152 | + 'tags' => ['api'], | |
| 153 | + 'endpoint' => '', | |
| 154 | + 'scope' => 'function', | |
| 155 | + | |
| 156 | + // Custom values | |
| 157 | + 'name' => $name ?? $snippet['name'], | |
| 158 | + 'code' => $code ?? $snippet['code'], | |
| 159 | + 'description' => $description ?? '', | |
| 160 | + | |
| 161 | + 'functionName' => $name ?? $snippet['functionName'], | |
| 162 | + 'functionTarget' => $target ?? $snippet['functionTarget'], | |
| 163 | + 'functionArgs' => $args ?? $snippet['functionArgs'], | |
| 164 | + 'functionArgsDict' => $args_data ?? $snippet['functionArgsDict'], | |
| 165 | + 'functionBehavior' => $behavior ?? $snippet['functionBehavior'], | |
| 166 | + ]; | |
| 167 | + | |
| 168 | + | |
| 169 | + $res = $this->core->add_snippet( $params ); | |
| 170 | + | |
| 171 | + return $res; | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Delete a function by its ID. | |
| 176 | + * | |
| 177 | + * @param int $id Function ID | |
| 178 | + * @return mixed | |
| 179 | + */ | |
| 180 | + public function delete_function( $id ) { | |
| 181 | + $params = [ | |
| 182 | + 'id' => $id, | |
| 183 | + ]; | |
| 184 | + | |
| 185 | + $this->snippet->delete_function_snippet( $params ); | |
| 186 | + $result = $this->snippet->delete( $params ); | |
| 187 | + | |
| 188 | + return $result; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Delete a function by its name. | |
| 193 | + * | |
| 194 | + * @param string $name Function name | |
| 195 | + * @return mixed | |
| 196 | + */ | |
| 197 | + public function delete_function_by_name( $name ) { | |
| 198 | + $snippet = $this->snippet->get_function_by_name( $name ); | |
| 199 | + if ( !$snippet ) { return false; } | |
| 200 | + | |
| 201 | + $params = [ | |
| 202 | + 'id' => $snippet['snippetId'], | |
| 203 | + ]; | |
| 204 | + | |
| 205 | + $this->snippet->delete_function_snippet( $params ); | |
| 206 | + $result = $this->snippet->delete( $params ); | |
| 207 | + | |
| 208 | + return $result; | |
| 209 | + } | |
| 210 | + | |
| 69 | 211 | } |