| @@ -101,40 +101,28 @@ | ||
| 101 | 101 | * @return array The list of snippets. |
| 102 | 102 | */ |
| 103 | 103 | public function getSnippets( $safe = true, $scope = null ) { |
| 104 | 104 | try { |
| 105 | - | |
| 106 | - $allSnippets = $this->snippet_module->select( 0, 9999, | |
| 107 | - [ ['accessor' => 'scope', 'value' => $scope] ] | |
| 108 | - , [] ); | |
| 109 | - | |
| 110 | - $snippets = $allSnippets['data'] ?? []; | |
| 105 | + // Don't log here to avoid infinite loops during initialization | |
| 111 | 106 | |
| 107 | + // If scope is specified, use getSnippetsByScope | |
| 108 | + if ( $scope ) { | |
| 109 | + $snippets = $this->getSnippetsByScope( $scope ); | |
| 110 | + } else { | |
| 111 | + // Get all snippets from the database | |
| 112 | + $allSnippets = $this->snippet_module->select( 0, 9999, [], [] ); | |
| 113 | + $snippets = $allSnippets['data'] ?? []; | |
| 114 | + } | |
| 112 | 115 | |
| 113 | 116 | if ( $safe ) { |
| 114 | - | |
| 115 | 117 | $snippets = array_filter( $snippets, function( $snippet ) { |
| 116 | - | |
| 117 | - if( $snippet['scope'] === 'function' ) { | |
| 118 | - | |
| 119 | - if( array_key_exists( 'functionName', $snippet ) ) | |
| 120 | - { | |
| 121 | - $name = $snippet['functionName']; | |
| 122 | - if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { | |
| 123 | - if ( $this->debug ) { | |
| 124 | - $this->core->log( sprintf( 'API [GetSnippets]: Filtered out snippet with invalid name: %s', $name ) ); | |
| 125 | - } | |
| 126 | - return false; | |
| 127 | - } | |
| 128 | - } else { | |
| 129 | - if ( $this->debug ) { | |
| 130 | - $this->core->log( 'API [GetSnippets]: Filtered out snippet with missing functionName.' ); | |
| 131 | - } | |
| 132 | - return false; | |
| 118 | + $name = $snippet['name']; | |
| 119 | + if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { | |
| 120 | + if ( $this->debug ) { | |
| 121 | + $this->core->log( sprintf( 'API [GetSnippets]: Filtered out snippet with invalid name: %s', $name ) ); | |
| 133 | 122 | } |
| 134 | - | |
| 123 | + return false; | |
| 135 | 124 | } |
| 136 | - | |
| 137 | 125 | return true; |
| 138 | 126 | } ); |
| 139 | 127 | } |
| 140 | 128 | |
| @@ -155,9 +143,9 @@ | ||
| 155 | 143 | * @param bool $safe Whether to filter out snippets with invalid names. |
| 156 | 144 | * @return array The list of function snippets only. |
| 157 | 145 | */ |
| 158 | 146 | public function get_functions( $safe = true ) { |
| 159 | - $this->core->log( '⚠️ API Warning: get_functions() is deprecated. Please use getSnippets() instead.' ); | |
| 147 | + error_log( 'Code Engine API: get_functions() is deprecated. Please use getSnippets() instead.' ); | |
| 160 | 148 | // Return only function snippets for backward compatibility |
| 161 | 149 | return $this->getSnippets( $safe, 'function' ); |
| 162 | 150 | } |
| 163 | 151 | |
| @@ -171,9 +159,9 @@ | ||
| 171 | 159 | * |
| 172 | 160 | * @return mixed The result of the snippet execution. |
| 173 | 161 | * @throws Exception If the snippet cannot be executed. |
| 174 | 162 | */ |
| 175 | - public function executeSnippet( $id, $args = [], $reply = null ) { | |
| 163 | + public function executeSnippet( $id, $args = [] ) { | |
| 176 | 164 | try { |
| 177 | 165 | if ( empty( $id ) ) { |
| 178 | 166 | throw new Exception( 'The snippet ID is required.' ); |
| 179 | 167 | } |
| @@ -186,34 +174,9 @@ | ||
| 186 | 174 | $snippet = $this->snippet_module->get_function( $id ); |
| 187 | 175 | if ( empty( $snippet ) ) { |
| 188 | 176 | throw new Exception( sprintf( 'Snippet with ID %d not found.', $id ) ); |
| 189 | 177 | } |
| 190 | - | |
| 191 | - $query = $reply ? $reply->query : null; | |
| 192 | - $array_query = []; | |
| 193 | - if( $query ) { | |
| 194 | - $array_query['envId'] = $query->envId ?? 'No EnvId in Query'; | |
| 195 | - $array_query['botId'] = $query->botId ?? 'No BotId in Query'; | |
| 196 | - $array_query['customId'] = $query->customId ?? 'No CustomId in Query'; | |
| 197 | - $array_query['embeddingsEnvId'] = $query->embeddingsEnvId ?? 'No EmbeddingsEnvId in Query'; | |
| 198 | - | |
| 199 | - | |
| 200 | - $array_query['chatId'] = $query->chatId ?? 'No ChatId in Query'; | |
| 201 | - $array_query['session'] = $query->session ?? 'No Session in Query'; | |
| 202 | - $array_query['scope'] = $query->scope ?? 'No Scope in Query'; | |
| 203 | - | |
| 204 | - $array_query['instructions'] = $query->instructions ?? 'No Instructions in Query'; | |
| 205 | - $array_query['context'] = $query->context ?? 'No Context in Query'; | |
| 206 | - | |
| 207 | - $array_query['messages'] = $query->messages ? json_encode( $query->messages ) : 'No Messages in Query'; | |
| 208 | - $array_query['message'] = $query->message ?? 'No Message in Query'; | |
| 209 | - | |
| 210 | - $array_query['model'] = $query->model ?? 'No Model in Query'; | |
| 211 | - $array_query['feature'] = $query->feature ?? 'No Feature in Query'; | |
| 212 | - } | |
| 213 | - | |
| 214 | - $args['__mwai_query'] = $array_query; | |
| 215 | - | |
| 178 | + | |
| 216 | 179 | $output = $this->core->run_snippet( $id, $args ); |
| 217 | 180 | |
| 218 | 181 | if ( $this->debug ) { |
| 219 | 182 | $shortOutput = is_string( $output ) ? substr( $output, 0, 100 ) : json_encode( $output ); |
| @@ -241,20 +204,8 @@ | ||
| 241 | 204 | * |
| 242 | 205 | * @return mixed The result of the snippet execution. |
| 243 | 206 | * @throws Exception If the snippet cannot be executed. |
| 244 | 207 | */ |
| 245 | - /** | |
| 246 | - * Declare every active PHP function snippet in the current request so they can call | |
| 247 | - * one another, without executing any of them. Idempotent and cheap (runs once per | |
| 248 | - * request). executeSnippet()/executeSnippetByName() already call this internally, so | |
| 249 | - * you only need it to make function snippets callable from your own custom PHP. | |
| 250 | - * | |
| 251 | - * @return void | |
| 252 | - */ | |
| 253 | - public function loadFunctions() { | |
| 254 | - $this->core->define_all_functions(); | |
| 255 | - } | |
| 256 | - | |
| 257 | 208 | public function executeSnippetByName( $name, $args = [] ) { |
| 258 | 209 | try { |
| 259 | 210 | if ( empty( $name ) ) { |
| 260 | 211 | throw new Exception( 'The snippet name is required.' ); |
| @@ -287,9 +238,9 @@ | ||
| 287 | 238 | * Create a new snippet. |
| 288 | 239 | * |
| 289 | 240 | * @param string $name Name of the snippet. |
| 290 | 241 | * @param string $code Code of the snippet. |
| 291 | - * @param string $scope Scope of the snippet: 'function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js'. | |
| 242 | + * @param string $scope Scope of the snippet: 'function', 'backend', 'frontend', 'scheduled', 'persistent'. | |
| 292 | 243 | * @param array $options Additional options: |
| 293 | 244 | * - target: 'php' or 'js' (for function snippets) |
| 294 | 245 | * - description: Description of the snippet |
| 295 | 246 | * - args: Arguments for function snippets |
| @@ -294,9 +245,8 @@ | ||
| 294 | 245 | * - description: Description of the snippet |
| 295 | 246 | * - args: Arguments for function snippets |
| 296 | 247 | * - argsData: Argument data for function snippets (use 'description' for each argument's description) |
| 297 | 248 | * - behavior: 'dynamic' or 'static' (for function snippets) |
| 298 | - * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets) | |
| 299 | 249 | * - tags: Array of tags |
| 300 | 250 | * - priority: Execution priority |
| 301 | 251 | * - active: Active status (true/false) |
| 302 | 252 | * - endpoint: REST endpoint |
| @@ -317,13 +267,13 @@ | ||
| 317 | 267 | if ( empty( $code ) ) { |
| 318 | 268 | throw new Exception( 'The snippet code is required.' ); |
| 319 | 269 | } |
| 320 | 270 | |
| 321 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js']; | |
| 271 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 322 | 272 | if ( !in_array( $scope, $validScopes ) ) { |
| 323 | 273 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 324 | 274 | } |
| 325 | - | |
| 275 | + | |
| 326 | 276 | // Extract options with defaults |
| 327 | 277 | $target = $options['target'] ?? 'php'; |
| 328 | 278 | $description = $options['description'] ?? ''; |
| 329 | 279 | $args = $options['args'] ?? []; |
| @@ -328,9 +278,8 @@ | ||
| 328 | 278 | $description = $options['description'] ?? ''; |
| 329 | 279 | $args = $options['args'] ?? []; |
| 330 | 280 | $argsData = $options['argsData'] ?? []; |
| 331 | 281 | $behavior = $options['behavior'] ?? 'dynamic'; |
| 332 | - $mcp = $options['mcp'] ?? false; | |
| 333 | 282 | $tags = $options['tags'] ?? ['api']; |
| 334 | 283 | $priority = $options['priority'] ?? 10; |
| 335 | 284 | $active = $options['active'] ?? true; |
| 336 | 285 | $endpoint = $options['endpoint'] ?? ''; |
| @@ -374,9 +323,8 @@ | ||
| 374 | 323 | $params['functionTarget'] = $target; |
| 375 | 324 | $params['functionArgs'] = $args; |
| 376 | 325 | $params['functionArgsDict'] = $argsData; |
| 377 | 326 | $params['functionBehavior'] = $behavior; |
| 378 | - $params['functionMcp'] = $mcp; | |
| 379 | 327 | } |
| 380 | 328 | |
| 381 | 329 | // Add scheduled-specific params |
| 382 | 330 | if ( $scope === 'scheduled' ) { |
| @@ -418,9 +366,8 @@ | ||
| 418 | 366 | * - target: 'php' or 'js' (for function snippets) |
| 419 | 367 | * - args: Arguments (for function snippets) |
| 420 | 368 | * - argsData: Argument data (for function snippets) |
| 421 | 369 | * - behavior: 'dynamic' or 'static' (for function snippets) |
| 422 | - * - mcp: Expose this function as its own MCP tool in AI Engine (for function snippets) | |
| 423 | 370 | * - intervalHours: Hours (for scheduled snippets) |
| 424 | 371 | * - intervalMinutes: Minutes (for scheduled snippets) |
| 425 | 372 | * |
| 426 | 373 | * @return array The updated snippet data. |
| @@ -443,9 +390,9 @@ | ||
| 443 | 390 | } |
| 444 | 391 | |
| 445 | 392 | // Validate scope if provided |
| 446 | 393 | if ( isset( $params['scope'] ) ) { |
| 447 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_php', 'content_js']; | |
| 394 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 448 | 395 | if ( !in_array( $params['scope'], $validScopes ) ) { |
| 449 | 396 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 450 | 397 | } |
| 451 | 398 | } |
| @@ -481,12 +428,8 @@ | ||
| 481 | 428 | $updateParams['functionTarget'] = $params['target'] ?? $snippet['functionTarget'] ?? 'php'; |
| 482 | 429 | $updateParams['functionArgs'] = $params['args'] ?? $snippet['functionArgs'] ?? []; |
| 483 | 430 | $updateParams['functionArgsDict'] = $params['argsData'] ?? $snippet['functionArgsDict'] ?? []; |
| 484 | 431 | $updateParams['functionBehavior'] = $params['behavior'] ?? $snippet['functionBehavior'] ?? 'dynamic'; |
| 485 | - // select_one() doesn't carry function metadata, so read the current MCP flag | |
| 486 | - // from the stored function to preserve it when the caller doesn't set it. | |
| 487 | - $existingFn = $this->snippet_module->get_function( $id ); | |
| 488 | - $updateParams['functionMcp'] = $params['mcp'] ?? ( $existingFn['mcp'] ?? false ); | |
| 489 | 432 | } |
| 490 | 433 | |
| 491 | 434 | // Add scheduled-specific params if it's a scheduled snippet |
| 492 | 435 | if ( $scope === 'scheduled' ) { |
| @@ -775,9 +718,9 @@ | ||
| 775 | 718 | |
| 776 | 719 | /** |
| 777 | 720 | * Get snippets by scope. |
| 778 | 721 | * |
| 779 | - * @param string $scope The scope to filter by: 'backend', 'frontend', 'function', 'scheduled', 'persistent', 'content_php', 'content_js'. | |
| 722 | + * @param string $scope The scope to filter by: 'backend', 'frontend', 'function', 'scheduled', 'persistent'. | |
| 780 | 723 | * @param array $filters Additional filters: 'active' (bool), 'tags' (array). |
| 781 | 724 | * @return array The list of snippets matching the criteria. |
| 782 | 725 | * @throws Exception If the scope is invalid. |
| 783 | 726 | */ |
| @@ -782,9 +725,9 @@ | ||
| 782 | 725 | * @throws Exception If the scope is invalid. |
| 783 | 726 | */ |
| 784 | 727 | public function getSnippetsByScope( $scope, $filters = [] ) { |
| 785 | 728 | try { |
| 786 | - $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent', 'content_js', 'content_php']; | |
| 729 | + $validScopes = ['function', 'backend', 'frontend', 'scheduled', 'persistent']; | |
| 787 | 730 | if ( !in_array( $scope, $validScopes ) ) { |
| 788 | 731 | throw new Exception( sprintf( 'Invalid scope. Must be one of: %s', implode( ', ', $validScopes ) ) ); |
| 789 | 732 | } |
| 790 | 733 | |