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.1.6 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / query / base.php
ai-engine / classes / query Last commit date
assist-feedback.php 10 months ago assistant.php 8 months ago base.php 8 months ago dropped-file.php 8 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 1 year ago text.php 8 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