assist-feedback.php
10 months ago
assistant.php
8 months ago
base.php
8 months ago
dropped-file.php
7 months ago
edit-image.php
8 months ago
embed.php
10 months ago
feedback.php
10 months ago
function.php
8 months ago
image.php
8 months ago
parameter.php
11 months ago
text.php
7 months ago
transcribe.php
8 months ago
base.php
450 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Base implements JsonSerializable { |
| 4 | // Environment |
| 5 | public ?string $session = null; |
| 6 | public ?string $chatId = null; |
| 7 | public string $scope = ''; |
| 8 | private $core = null; |
| 9 | |
| 10 | // Core Content |
| 11 | public ?string $instructions = null; |
| 12 | public array $messages = []; |
| 13 | public ?string $context = null; |
| 14 | public string $message = ''; |
| 15 | |
| 16 | // Parameters |
| 17 | public int $maxMessages = 15; |
| 18 | public int $maxResults = 1; |
| 19 | public ?string $model = null; |
| 20 | public string $feature = 'completion'; |
| 21 | |
| 22 | // Functions |
| 23 | public array $functions = []; |
| 24 | public ?string $functionCall = null; |
| 25 | |
| 26 | // MCP Servers |
| 27 | public array $mcpServers = []; |
| 28 | |
| 29 | // Tools (for Responses API) |
| 30 | public array $tools = []; |
| 31 | |
| 32 | // History strategy for Responses API |
| 33 | public ?string $historyStrategy = null; |
| 34 | public ?string $previousResponseId = null; |
| 35 | |
| 36 | // Overrides for env |
| 37 | public array $envSettings = []; |
| 38 | public string $envId = ''; |
| 39 | public ?string $apiKey = null; |
| 40 | |
| 41 | // Seem to be only used by the Assistants, to get the current thread/discussion. |
| 42 | // Maybe we should try to move this to the assistant class, or use it as ExtraParams. |
| 43 | public ?string $botId = null; |
| 44 | // Identifier for ad-hoc/custom chatbots (distinct from registered botId) |
| 45 | public ?string $customId = null; |
| 46 | |
| 47 | // Embeddings configuration |
| 48 | public ?string $embeddingsEnvId = null; |
| 49 | |
| 50 | // Extra Parameters (used by specific services, or for statistics, etc) |
| 51 | public array $extraParams = []; |
| 52 | |
| 53 | /** |
| 54 | * Internal state tracking for function call recursion. |
| 55 | * Stores the configured max depth to prevent re-applying the filter on recursive calls. |
| 56 | * Set once by engines/core.php during the first run() call. |
| 57 | * @internal |
| 58 | */ |
| 59 | public $_maxDepthConfigured = null; |
| 60 | |
| 61 | // Options |
| 62 | // Engine will either upload or share an URL to the image, for Vision, for example. |
| 63 | // Having this here allows other services to override it if needed (Ollama needs it false). |
| 64 | public ?string $image_remote_upload = null; |
| 65 | |
| 66 | #region Constructors, Serialization |
| 67 | |
| 68 | public function __construct( $message = '' ) { |
| 69 | global $mwai_core; |
| 70 | if ( is_string( $message ) ) { |
| 71 | $this->set_message( $message ); |
| 72 | } |
| 73 | $this->session = $mwai_core->get_session_id(); |
| 74 | $this->core = $mwai_core; |
| 75 | $this->image_remote_upload = $this->core->get_option( 'image_remote_upload' ); |
| 76 | } |
| 77 | |
| 78 | #[\ReturnTypeWillChange] |
| 79 | public function jsonSerialize(): array { |
| 80 | $json = [ |
| 81 | 'message' => $this->message, |
| 82 | 'instructions' => $this->instructions, |
| 83 | |
| 84 | 'ai' => [ |
| 85 | 'model' => $this->model, |
| 86 | 'feature' => $this->feature, |
| 87 | ], |
| 88 | |
| 89 | 'system' => [ |
| 90 | 'class' => get_class( $this ), |
| 91 | 'envId' => $this->envId, |
| 92 | 'scope' => $this->scope, |
| 93 | 'session' => $this->session, |
| 94 | 'customId' => $this->customId, |
| 95 | 'maxMessages' => $this->maxMessages, |
| 96 | ] |
| 97 | ]; |
| 98 | |
| 99 | if ( !empty( $this->context ) ) { |
| 100 | $json['context']['content'] = $this->context; |
| 101 | } |
| 102 | |
| 103 | return $json; |
| 104 | } |
| 105 | |
| 106 | #endregion |
| 107 | |
| 108 | #region Functions |
| 109 | |
| 110 | public function add_function( Meow_MWAI_Query_Function $function ): void { |
| 111 | $this->functions[] = $function; |
| 112 | $this->functionCall = 'auto'; |
| 113 | } |
| 114 | |
| 115 | public function set_functions( array $functions ): void { |
| 116 | $this->functions = $functions; |
| 117 | $this->functionCall = 'auto'; |
| 118 | } |
| 119 | |
| 120 | public function set_tools( array $tools ): void { |
| 121 | $this->tools = $tools; |
| 122 | } |
| 123 | |
| 124 | public function set_mcp_servers( array $mcpServers ): void { |
| 125 | $this->mcpServers = $mcpServers; |
| 126 | } |
| 127 | |
| 128 | #endregion |
| 129 | |
| 130 | #region Helpers |
| 131 | |
| 132 | public function replace( $search, $replace ) { |
| 133 | $this->message = str_replace( $search, $replace, $this->message ); |
| 134 | } |
| 135 | |
| 136 | #endregion |
| 137 | |
| 138 | public function get_message(): string { |
| 139 | return $this->message; |
| 140 | } |
| 141 | |
| 142 | public function get_in_tokens(): int { |
| 143 | $in_tokens = Meow_MWAI_Core::estimate_tokens( |
| 144 | $this->messages, |
| 145 | $this->message, |
| 146 | $this->context ?? '' |
| 147 | ); |
| 148 | return $in_tokens; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * The environment, like "chatbot", "imagesbot", "chatbot-007", "textwriter", etc... |
| 153 | * Used for statistics, mainly. |
| 154 | * @param string $env The environment. |
| 155 | */ |
| 156 | public function set_scope( string $scope ): void { |
| 157 | $this->scope = $scope; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * The environment ID for AI services. |
| 162 | * Used for statistics, mainly. |
| 163 | * @param string $envId The environment ID. |
| 164 | */ |
| 165 | public function set_env_id( string $envId ): void { |
| 166 | $this->envId = $envId; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * ID of the model to use. |
| 171 | * @param string $model ID of the model to use. |
| 172 | */ |
| 173 | public function set_model( string $model ) { |
| 174 | $this->model = $model; |
| 175 | } |
| 176 | |
| 177 | public function get_model() { |
| 178 | return $this->model; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * The chat ID to use. |
| 183 | * @param string $chatId The chat ID. |
| 184 | */ |
| 185 | public function set_chat_id( string $chatId ) { |
| 186 | $this->chatId = $chatId; |
| 187 | } |
| 188 | |
| 189 | public function get_chat_id() { |
| 190 | return $this->chatId; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * The instructions are used to define the personality of the AI, and to give it some context. |
| 195 | * @param string $instructions The instructions. |
| 196 | */ |
| 197 | public function set_instructions( string $instructions ): void { |
| 198 | // Decode HTML entities in case the instructions were sanitized at the UI level |
| 199 | // and ended up encoded when reaching the server. |
| 200 | $instructions = html_entity_decode( $instructions ); |
| 201 | |
| 202 | $this->instructions = apply_filters( 'mwai_ai_context', $instructions, $this ); |
| 203 | if ( $this->instructions !== $instructions ) { |
| 204 | Meow_MWAI_Logging::deprecated( '"mwai_ai_context" filter is deprecated. Please use "mwai_ai_instructions" instead.' ); |
| 205 | } |
| 206 | $this->instructions = apply_filters( 'mwai_ai_instructions', $this->instructions, $this ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Given a message, the model will return one or more predicted completions. |
| 211 | * It can also return the probabilities of alternative tokens at each position. |
| 212 | * @param string $message The message to generate completions. |
| 213 | */ |
| 214 | public function set_message( string $message ) { |
| 215 | $this->message = $message; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Similar to the prompt, but use an array of messages instead. |
| 220 | * @param string $messages The messages to generate completions. |
| 221 | */ |
| 222 | public function set_messages( array $messages ) { |
| 223 | $messages = array_map( function ( $message ) { |
| 224 | if ( is_array( $message ) ) { |
| 225 | return [ 'role' => $message['role'], 'content' => $message['content'] ]; |
| 226 | } |
| 227 | else if ( is_object( $message ) ) { |
| 228 | return [ 'role' => $message->role, 'content' => $message->content ]; |
| 229 | } |
| 230 | else { |
| 231 | throw new InvalidArgumentException( 'Unsupported message type.' ); |
| 232 | } |
| 233 | }, $messages ); |
| 234 | $this->messages = $messages; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * The context can be used to add additional information that is likely to be relevant to the model. |
| 239 | * @param string $context The context. |
| 240 | */ |
| 241 | public function set_context( string $context ): void { |
| 242 | $this->context = $context; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * The API key to use. |
| 247 | * @param string $apiKey The API key. |
| 248 | */ |
| 249 | public function set_api_key( string $apiKey ) { |
| 250 | $this->apiKey = $apiKey; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * The session ID to use. |
| 255 | * @param string $session The session ID. |
| 256 | */ |
| 257 | public function set_session( string $session ) { |
| 258 | $this->session = $session; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * The bot ID to use. |
| 263 | * @param string $botId The bot ID. |
| 264 | */ |
| 265 | public function set_bot_id( string $botId ) { |
| 266 | $this->botId = $botId; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * The custom ID to use for ad-hoc chatbots (shortcode/overrides). |
| 271 | * @param string $customId The custom chatbot ID. |
| 272 | */ |
| 273 | public function set_custom_id( string $customId ) { |
| 274 | $this->customId = $customId; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * The embeddings environment ID to use. |
| 279 | * @param string $embeddingsEnvId The embeddings environment ID. |
| 280 | */ |
| 281 | public function set_embeddings_env_id( string $embeddingsEnvId ) { |
| 282 | $this->embeddingsEnvId = $embeddingsEnvId; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * How many completions to generate for each prompt. |
| 287 | * Because this parameter generates many completions, it can quickly consume your token quota. |
| 288 | * Use carefully and ensure that you have reasonable settings for max_tokens and stop. |
| 289 | * @param float $maxResults Number of completions. |
| 290 | */ |
| 291 | public function set_max_results( int $maxResults ) { |
| 292 | $this->maxResults = $maxResults; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Set the history strategy for Responses API. |
| 297 | * @param string $historyStrategy The history strategy ('internal', 'response_id', or null). |
| 298 | */ |
| 299 | public function set_history_strategy( ?string $historyStrategy ) { |
| 300 | $this->historyStrategy = $historyStrategy; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Set the previous response ID for Responses API. |
| 305 | * @param string $previousResponseId The previous response ID. |
| 306 | */ |
| 307 | public function set_previous_response_id( ?string $previousResponseId ) { |
| 308 | $this->previousResponseId = $previousResponseId; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * This is run at the end of the process, to do some final checks. |
| 313 | */ |
| 314 | public function final_checks() { |
| 315 | if ( !empty( $this->maxMessages ) ) { |
| 316 | $context = array_shift( $this->messages ); |
| 317 | if ( !empty( $this->messages ) ) { |
| 318 | $this->messages = array_slice( $this->messages, -$this->maxMessages ); |
| 319 | } |
| 320 | else { |
| 321 | $this->messages = []; |
| 322 | } |
| 323 | if ( !empty( $context ) ) { |
| 324 | array_unshift( $this->messages, $context ); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | public function set_max_messages( int $maxMessages ): void { |
| 330 | if ( !empty( $maxMessages ) ) { |
| 331 | $this->maxMessages = intval( $maxMessages ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | protected function convert_keys( $params ) { |
| 336 | $newParams = []; |
| 337 | foreach ( $params as $key => $value ) { |
| 338 | // Skip non-string keys (numeric indices, booleans, etc.) |
| 339 | if ( !is_string( $key ) ) { |
| 340 | continue; |
| 341 | } |
| 342 | $newKey = ''; |
| 343 | $capitalizeNextChar = false; |
| 344 | for ( $i = 0; $i < strlen( $key ); $i++ ) { |
| 345 | if ( $key[$i] == '_' ) { |
| 346 | $capitalizeNextChar = true; |
| 347 | } |
| 348 | else { |
| 349 | $newKey .= $capitalizeNextChar ? strtoupper( $key[$i] ) : $key[$i]; |
| 350 | $capitalizeNextChar = false; |
| 351 | } |
| 352 | } |
| 353 | $newParams[$newKey] = $value; |
| 354 | } |
| 355 | return $newParams; |
| 356 | } |
| 357 | |
| 358 | public function toJson() { |
| 359 | return json_encode( $this ); |
| 360 | } |
| 361 | |
| 362 | #region Extra Params |
| 363 | public function setExtraParam( string $key, $value ): void { |
| 364 | $this->extraParams[$key] = $value; |
| 365 | } |
| 366 | |
| 367 | public function getExtraParam( string $key ) { |
| 368 | // Only if it exists |
| 369 | if ( !isset( $this->extraParams[$key] ) ) { |
| 370 | return null; |
| 371 | } |
| 372 | $value = $this->extraParams[$key]; |
| 373 | return $value; |
| 374 | } |
| 375 | #endregion Extra Params |
| 376 | |
| 377 | // Based on the params of the query, update the attributes |
| 378 | public function inject_params( array $params ): void { |
| 379 | // Those are for the keys passed directly by the shortcode. |
| 380 | $params = $this->convert_keys( $params ); |
| 381 | |
| 382 | if ( !empty( $params['instructions'] ) ) { |
| 383 | $this->set_instructions( $params['instructions'] ); |
| 384 | } |
| 385 | // Do not allow external params to clobber an already-set message |
| 386 | // The message is typically set via constructor (e.g., Chatbot newMessage). |
| 387 | // Some UIs may accidentally send a top-level 'message' param (e.g., from |
| 388 | // unrelated settings like verbosity defaults), which would override the |
| 389 | // real user input here. Only set it if it's not already set. |
| 390 | if ( !empty( $params['message'] ) && $this->message === '' ) { |
| 391 | $this->set_message( $params['message'] ); |
| 392 | } |
| 393 | if ( !empty( $params['messages'] ) ) { |
| 394 | $this->set_messages( $params['messages'] ); |
| 395 | } |
| 396 | if ( !empty( $params['maxMessages'] ) && intval( $params['maxMessages'] ) > 0 ) { |
| 397 | $this->set_max_messages( intval( $params['maxMessages'] ) ); |
| 398 | } |
| 399 | if ( !empty( $params['maxResults'] ) ) { |
| 400 | $this->set_max_results( $params['maxResults'] ); |
| 401 | } |
| 402 | if ( !empty( $params['scope'] ) ) { |
| 403 | $this->set_scope( $params['scope'] ); |
| 404 | } |
| 405 | if ( !empty( $params['session'] ) ) { |
| 406 | $this->set_session( $params['session'] ); |
| 407 | } |
| 408 | if ( !empty( $params['apiKey'] ) ) { |
| 409 | $this->set_api_key( $params['apiKey'] ); |
| 410 | } |
| 411 | if ( !empty( $params['botId'] ) ) { |
| 412 | $this->set_bot_id( $params['botId'] ); |
| 413 | } |
| 414 | if ( !empty( $params['customId'] ) ) { |
| 415 | $this->set_custom_id( $params['customId'] ); |
| 416 | } |
| 417 | if ( !empty( $params['envId'] ) ) { |
| 418 | $this->set_env_id( $params['envId'] ); |
| 419 | } |
| 420 | if ( !empty( $params['model'] ) ) { |
| 421 | $this->set_model( $params['model'] ); |
| 422 | } |
| 423 | if ( !empty( $params['chatId'] ) ) { |
| 424 | $this->set_chat_id( $params['chatId'] ); |
| 425 | } |
| 426 | if ( !empty( $params['tools'] ) && is_array( $params['tools'] ) ) { |
| 427 | $this->set_tools( $params['tools'] ); |
| 428 | } |
| 429 | if ( isset( $params['historyStrategy'] ) ) { |
| 430 | $this->set_history_strategy( $params['historyStrategy'] ); |
| 431 | } |
| 432 | if ( !empty( $params['previousResponseId'] ) ) { |
| 433 | $this->set_previous_response_id( $params['previousResponseId'] ); |
| 434 | } |
| 435 | if ( !empty( $params['embeddingsEnvId'] ) ) { |
| 436 | $this->set_embeddings_env_id( $params['embeddingsEnvId'] ); |
| 437 | } |
| 438 | if ( !empty( $params['mcpServers'] ) ) { |
| 439 | // Handle both JSON string and array formats |
| 440 | $mcpServers = $params['mcpServers']; |
| 441 | if ( is_string( $mcpServers ) ) { |
| 442 | $mcpServers = json_decode( $mcpServers, true ); |
| 443 | } |
| 444 | if ( is_array( $mcpServers ) ) { |
| 445 | $this->set_mcp_servers( $mcpServers ); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 |