| @@ -101,28 +101,40 @@ | ||
| 101 | 101 | * @return array The list of snippets. |
| 102 | 102 | */ |
| 103 | 103 | public function getSnippets( $safe = true, $scope = null ) { |
| 104 | 104 | try { |
| 105 | - // Don't log here to avoid infinite loops during initialization | |
| 105 | + | |
| 106 | + $allSnippets = $this->snippet_module->select( 0, 9999, | |
| 107 | + [ ['accessor' => 'scope', 'value' => $scope] ] | |
| 108 | + , [] ); | |
| 109 | + | |
| 110 | + $snippets = $allSnippets['data'] ?? []; | |
| 106 | 111 | |
| 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 | - } | |
| 115 | 112 | |
| 116 | 113 | if ( $safe ) { |
| 114 | + | |
| 117 | 115 | $snippets = array_filter( $snippets, function( $snippet ) { |
| 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 ) ); | |
| 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; | |
| 122 | 133 | } |
| 123 | - return false; | |
| 134 | + | |
| 124 | 135 | } |
| 136 | + | |
| 125 | 137 | return true; |
| 126 | 138 | } ); |
| 127 | 139 | } |
| 128 | 140 | |
| @@ -159,9 +171,9 @@ | ||
| 159 | 171 | * |
| 160 | 172 | * @return mixed The result of the snippet execution. |
| 161 | 173 | * @throws Exception If the snippet cannot be executed. |
| 162 | 174 | */ |
| 163 | - public function executeSnippet( $id, $args = [] ) { | |
| 175 | + public function executeSnippet( $id, $args = [], $reply = null ) { | |
| 164 | 176 | try { |
| 165 | 177 | if ( empty( $id ) ) { |
| 166 | 178 | throw new Exception( 'The snippet ID is required.' ); |
| 167 | 179 | } |
| @@ -174,9 +186,34 @@ | ||
| 174 | 186 | $snippet = $this->snippet_module->get_function( $id ); |
| 175 | 187 | if ( empty( $snippet ) ) { |
| 176 | 188 | throw new Exception( sprintf( 'Snippet with ID %d not found.', $id ) ); |
| 177 | 189 | } |
| 178 | - | |
| 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 | + | |
| 179 | 216 | $output = $this->core->run_snippet( $id, $args ); |
| 180 | 217 | |
| 181 | 218 | if ( $this->debug ) { |
| 182 | 219 | $shortOutput = is_string( $output ) ? substr( $output, 0, 100 ) : json_encode( $output ); |