Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_ai;
18
 
19
use core\exception\coding_exception;
20
use core_ai\aiactions\base;
21
use core_ai\aiactions\responses;
22
use core\plugininfo\aiprovider as aiproviderplugin;
23
/**
24
 * AI subsystem manager.
25
 *
26
 * @package    core_ai
27
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class manager {
31
    /**
32
     * Create a new AI manager.
33
     *
34
     * @param \moodle_database $db
35
     */
36
    public function __construct(
37
        /** @var \moodle_database The database instance */
38
        protected readonly \moodle_database $db,
39
    ) {
40
    }
41
 
42
    /**
43
     * Get communication provider class name from the plugin name.
44
     *
45
     * @param string $plugin The component name.
46
     * @return string The class name of the provider.
47
     */
48
    private static function get_ai_plugin_classname(string $plugin): string {
49
        if (str_starts_with($plugin, 'aiprovider_')) {
50
            return "{$plugin}\\provider";
51
        } else if (str_starts_with($plugin, 'aiplacement_')) {
52
            return "{$plugin}\\placement";
53
        } else {
54
            // Explode if neither.
55
            throw new coding_exception("Plugin name does not start with 'aiprovider_' or 'aiplacement_': {$plugin}");
56
        }
57
    }
58
 
59
    /**
60
     * Get the list of actions that this provider or placement supports,
61
     * given the name of the plugin.
62
     *
63
     * @param string $pluginname The name of the plugin to get the actions for.
64
     * @return array An array of action class names.
65
     */
66
    public static function get_supported_actions(string $pluginname): array {
67
        $pluginclassname = static::get_ai_plugin_classname($pluginname);
68
        return $pluginclassname::get_action_list();
69
    }
70
 
71
    /**
72
     * Given a list of actions get the provider instances that support them.
73
     *
74
     * Will return an array of arrays, indexed by action name.
75
     *
76
     * @param array $actions An array of fully qualified action class names.
77
     * @param bool $enabledonly If true, only return enabled providers.
78
     * @return array An array of provider instances indexed by action name.
79
     */
80
    public function get_providers_for_actions(array $actions, bool $enabledonly = false): array {
81
        $providers = [];
82
        $instances = $this->get_sorted_providers();
83
        foreach ($actions as $action) {
84
            $providers[$action] = [];
85
            foreach ($instances as $instance) {
86
                // Check the plugin is enabled and the provider is configured before making the action available.
87
                if ($enabledonly && (!$instance->enabled
88
                        || !$this->is_action_enabled($instance->provider, $action, $instance->id))
89
                        || $enabledonly && !$instance->is_provider_configured()) {
90
                    continue;
91
                }
92
                if (in_array($action, $instance->get_action_list())) {
93
                    $providers[$action][] = $instance;
94
                }
95
            }
96
        }
97
        return $providers;
98
    }
99
 
100
    /**
101
     * Call the action provider.
102
     *
103
     * The named provider will process the action and return the result.
104
     *
105
     * @param provider $provider The provider to call.
106
     * @param base $action The action to process.
107
     * @return responses\response_base The result of the action.
108
     */
109
    protected function call_action_provider(provider $provider, base $action): responses\response_base {
110
        $classname = 'process_' . $action->get_basename();
111
        $classpath = substr($provider::class, 0, strpos($provider::class, '\\') + 1);
112
        $processclass = $classpath . $classname;
113
        $processor = new $processclass($provider, $action);
114
 
115
        return $processor->process($action);
116
    }
117
 
118
    /**
119
     * Process an action.
120
     *
121
     * This is the entry point for processing an action.
122
     *
123
     * @param base $action The action to process. Action must be configured.
124
     * @return responses\response_base The result of the action.
125
     */
126
    public function process_action(base $action): responses\response_base {
127
        // Get the action response_base name.
128
        $actionname = $action::class;
129
        $responseclassname = 'core_ai\\aiactions\\responses\\response_' . $action->get_basename();
130
 
131
        // Get the providers that support the action.
132
        $providers = $this->get_providers_for_actions([$actionname], true);
133
 
134
        // Loop through the providers and process the action.
135
        foreach ($providers[$actionname] as $provider) {
136
            $result = $this->call_action_provider($provider, $action);
137
 
138
            // Store the result (success or failure).
139
            $this->store_action_result($provider, $action, $result);
140
 
141
            // If the result is successful, return the result.
142
            // No need to keep looping.
143
            if ($result->get_success()) {
144
                return $result;
145
            }
146
        }
147
 
148
        // If we get here we've all available providers have failed.
149
        // Return the result if we have one.
150
        if (isset($result)) {
151
            return $result;
152
        }
153
 
154
        // Response if there are no providers available.
155
        return new $responseclassname(
156
            success: false,
157
            errorcode: -1,
158
            errormessage: 'No providers available to process the action.');
159
    }
160
 
161
    /**
162
     * Store the action result.
163
     *
164
     * @param provider $provider The provider that processed the action.
165
     * @param base $action The action that was processed.
166
     * @param responses\response_base $response The result of the action.
167
     * @return int The id of the stored action result.
168
     */
169
    private function store_action_result(
170
        provider $provider,
171
        base $action,
172
        responses\response_base $response,
173
    ): int {
174
        global $DB;
175
        // Store the action result.
176
        $record = (object) [
177
            'actionname' => $action->get_basename(),
178
            'success' => $response->get_success(),
179
            'userid' => $action->get_configuration('userid'),
180
            'contextid' => $action->get_configuration('contextid'),
181
            'provider' => $provider->get_name(),
182
            'errorcode' => $response->get_errorcode(),
183
            'errormessage' => $response->get_errormessage(),
184
            'timecreated' => $action->get_configuration('timecreated'),
185
            'timecompleted' => $response->get_timecreated(),
186
            'model' => $response->get_model_used(),
187
        ];
188
 
189
        try {
190
            // Do everything in a transaction.
191
            $transaction = $DB->start_delegated_transaction();
192
 
193
            // Create the record for the action result.
194
            $record->actionid = $action->store($response);
195
            $recordid = $DB->insert_record('ai_action_register', $record);
196
 
197
            // Commit the transaction.
198
            $transaction->allow_commit();
199
        } catch (\Exception $e) {
200
            // Rollback the transaction.
201
            $transaction->rollback($e);
202
            // Re throw the exception.
203
            throw $e;
204
        }
205
 
206
        return $recordid;
207
    }
208
 
209
    /**
210
     * Set the policy acceptance for a given user.
211
     *
212
     * @param int $userid The user id.
213
     * @param int $contextid The context id the policy was accepted in.
214
     * @return bool True if the policy was set, false otherwise.
215
     */
216
    public static function user_policy_accepted(int $userid, int $contextid): bool {
217
        global $DB;
218
 
219
        $record = (object) [
220
            'userid' => $userid,
221
            'contextid' => $contextid,
222
            'timeaccepted' => \core\di::get(\core\clock::class)->time(),
223
        ];
224
 
225
        if ($DB->insert_record('ai_policy_register', $record)) {
226
            $policycache = \cache::make('core', 'ai_policy');
227
            return $policycache->set($userid, true);
228
        } else {
229
            return false;
230
        }
231
    }
232
 
233
    /**
234
     * Get the user policy.
235
     *
236
     * @param int $userid The user id.
237
     * @return bool True if the policy was accepted, false otherwise.
238
     */
239
    public static function get_user_policy_status(int $userid): bool {
240
        $policycache = \cache::make('core', 'ai_policy');
241
        return $policycache->get($userid);
242
    }
243
 
244
    /**
245
     * Set the action state for a given plugin.
246
     *
247
     * @param string $plugin The name of the plugin.
248
     * @param string $actionbasename The action to be set.
249
     * @param int $enabled The state to be set (e.g., enabled or disabled).
250
     * @param int $instanceid The instance id of the instance.
251
     * @return bool Returns true if the configuration was successfully set, false otherwise.
252
     */
253
    public function set_action_state(
254
        string $plugin,
255
        string $actionbasename,
256
        int $enabled,
257
        int $instanceid = 0
258
    ): bool {
259
        $actionclass = 'core_ai\\aiactions\\' . $actionbasename;
260
        $oldvalue = $this->is_action_enabled($plugin, $actionclass, $instanceid);
261
 
262
        // Check if we are setting an action for a provider or placement.
263
        if (str_contains($plugin, 'aiprovider')) {
264
            // Handle provider actions.
265
            $providers = $this->get_provider_instances(['id' => $instanceid]);
266
            $provider = reset($providers);
267
 
268
            // Update the enabled state of the action.
269
            $actionconfig = $provider->actionconfig;
270
            $actionconfig[$actionclass]['enabled'] = (bool)$enabled;
271
 
272
            return $this->update_provider_instance(
273
                provider: $provider,
274
                actionconfig: $actionconfig)->actionconfig[$actionclass]['enabled'];
275
 
276
        } else {
277
            // Handle placement actions.
278
            // Only set value if there is no config setting or if the value is different from the previous one.
279
            if ($oldvalue !== (bool)$enabled) {
280
                set_config($actionbasename, $enabled, $plugin);
281
                add_to_config_log('disabled', !$oldvalue, !$enabled, $plugin);
282
                \core_plugin_manager::reset_caches();
283
                return true;
284
            }
285
            return false;
286
        }
287
    }
288
 
289
    /**
290
     * Check if an action is enabled for a given provider.
291
     *
292
     * @param string $plugin The name of the plugin.
293
     * @param string $actionclass The fully qualified action class name to be checked.
294
     * @param int $instanceid The instance id of the plugin.
295
     * @return bool Returns the configuration value of the action for the given plugin.
296
     */
297
    private function is_provider_action_enabled(string $plugin, string $actionclass, int $instanceid): bool {
298
        // If there is no instance id, we are checking the provider itself.
299
        // So get the defaults.
300
        if ($instanceid === 0) {
301
            // Get the defaults for this provider type.
302
            $classname = "\\{$plugin}\\provider";
303
            $defaultconfig = $classname::initialise_action_settings();
304
 
305
            // Return the default value.
306
            return array_key_exists($actionclass, $defaultconfig) && $defaultconfig[$actionclass]['enabled'];
307
 
308
        } else {
309
            // Get the provider instance.
310
            $providers = $this->get_provider_instances(['id' => $instanceid]);
311
            $provider = reset($providers);
312
            return array_key_exists($actionclass, $provider->actionconfig) && $provider->actionconfig[$actionclass]['enabled'];
313
        }
314
    }
315
 
316
    /**
317
     * Check if an action is enabled for a given plugin.
318
     *
319
     * @param string $plugin The name of the plugin.
320
     * @param string $actionclass The fully qualified action class name to be checked.
321
     * @param int $instanceid The instance id of the plugin.
322
     * @return bool Returns the configuration value of the action for the given plugin.
323
     */
324
    public function is_action_enabled(string $plugin, string $actionclass, int $instanceid = 0): bool {
325
        if (str_contains($plugin, 'aiprovider')) {
326
            // Handle provider actions.
327
            return $this->is_provider_action_enabled($plugin, $actionclass, $instanceid);
328
        } else {
329
            // Handle placement actions.
330
            $value = get_config($plugin, $actionclass::get_basename());
331
            // If not exist in DB, set it to true (enabled).
332
            if ($value === false) {
333
                return true;
334
            }
335
            return (bool) $value;
336
        }
337
    }
338
 
339
    /**
340
     * Check if an action is available.
341
     * Action is available if it is enabled for at least one enabled provider.
342
     *
343
     * @param string $actionclass The fully qualified action class name to be checked.
344
     * @return bool
345
     */
346
    public function is_action_available(string $actionclass): bool {
347
        $providers = $this->get_providers_for_actions([$actionclass], true);
348
        // Check if the requested action is enabled for at least one provider.
349
        foreach ($providers as $provideractions) {
350
            foreach ($provideractions as $provider) {
351
                $classnamearray = explode('\\', $provider::class);
352
                $pluginname = reset($classnamearray);
353
                if ($this->is_action_enabled($pluginname, $actionclass)) {
354
                    return true;
355
                }
356
            }
357
        }
358
        // There are no providers with this action enabled.
359
        return false;
360
    }
361
 
362
    /**
363
     * Create a new provider instance.
364
     *
365
     * @param string $classname Classname of the provider.
366
     * @param string $name The name of the provider config.
367
     * @param bool $enabled The enabled state of the provider.
368
     * @param array|null $config The config json.
369
     * @param array|null $actionconfig The action config json.
370
     * @return provider
371
     */
372
    public function create_provider_instance(
373
        string $classname,
374
        string $name,
375
        bool $enabled = false,
376
        ?array $config = null,
377
        ?array $actionconfig = null,
378
    ): provider {
379
        if (!class_exists($classname) || !is_a($classname, provider::class, true)) {
380
            throw new \coding_exception("Provider class not valid: {$classname}");
381
        }
382
        $provider = new $classname(
383
            enabled: $enabled,
384
            name: $name,
385
            config: json_encode($config ?? []),
386
            actionconfig: $actionconfig ? json_encode($actionconfig) : '',
387
        );
388
 
389
        $id = $this->db->insert_record('ai_providers', $provider->to_record());
390
 
391
        // Ensure the provider instance order config gets updated if the provider is enabled.
392
        if ($enabled) {
393
            $this->update_provider_order($id, \core\plugininfo\aiprovider::ENABLE);
394
        }
395
 
396
        return $provider->with(id: $id);
397
    }
398
 
399
    /**
400
     * Get single provider record according to the filter
401
     *
402
     * @param array $filter The filterable elements to get the record from
403
     * @param int $strictness
404
     * @return \stdClass|false
405
     */
406
    public function get_provider_record(array $filter = [], int $strictness = IGNORE_MISSING): \stdClass|false {
407
        return $this->db->get_record(
408
            table: 'ai_providers',
409
            conditions: $filter,
410
            strictness: $strictness,
411
        );
412
    }
413
 
414
    /**
415
     * Get the provider records according to the filter.
416
     *
417
     * @param array|null $filter The filterable elements to get the records from.
418
     * @return array
419
     */
420
    public function get_provider_records(?array $filter = null): array {
421
        return $this->db->get_records(
422
            table: 'ai_providers',
423
            conditions: $filter,
424
        );
425
    }
426
 
427
    /**
428
     * Get a list of all provider instances.
429
     *
430
     * This method retrieves provider records from the database, attempts to instantiate
431
     * each provider class, and returns an array of provider instances. It filters out
432
     * any records where the provider class does not exist.
433
     *
434
     * @param null|array $filter The database filter to apply when fetching provider records.
435
     * @return array An array of instantiated provider objects.
436
     */
437
    public function get_provider_instances(?array $filter = null): array {
438
        // Filter out any null values from the array (providers that couldn't be instantiated).
439
        return array_filter(
440
            // Apply a callback function to each provider record to instantiate the provider.
441
            array_map(
442
                function ($record): ?provider {
443
                    // Check if the provider class specified in the record exists.
444
                    if (!class_exists($record->provider)) {
445
                        // Log a debugging message if the provider class is not found.
446
                        debugging(
447
                            "Unable to find a provider class for {$record->provider}",
448
                            DEBUG_DEVELOPER,
449
                        );
450
                        // Return null to indicate that the provider could not be instantiated.
451
                        return null;
452
                    }
453
 
454
                    // Instantiate the provider class with the record's data.
455
                    return new $record->provider(
456
                        enabled: $record->enabled,
457
                        id: $record->id,
458
                        name: $record->name,
459
                        config: $record->config,
460
                        actionconfig: $record->actionconfig,
461
                    );
462
                },
463
                // Retrieve the provider records from the database with the optional filter.
464
                $this->get_provider_records($filter),
465
            )
466
        );
467
    }
468
 
469
    /**
470
     * Update provider instance.
471
     *
472
     * @param provider $provider The provider instance.
473
     * @param array|null $config the configuration of the provider instance to be updated.
474
     * @param array|null $actionconfig the action configuration of the provider instance to be updated.
475
     * @return provider
476
     */
477
    public function update_provider_instance(
478
        provider $provider,
479
        ?array $config = null,
480
        ?array $actionconfig = null
481
    ): provider {
482
        $provider = $provider->with(
483
            name: $provider->name,
484
            config: $config ?? $provider->config,
485
            actionconfig: $actionconfig ?? $provider->actionconfig,
486
        );
487
        $this->db->update_record('ai_providers', $provider->to_record());
488
        return $provider;
489
    }
490
 
491
    /**
492
     * Delete the provider instance.
493
     *
494
     * @param provider $provider The provider instance.
495
     * @return bool
496
     */
497
    public function delete_provider_instance(provider $provider): bool {
498
        // Dispatch the hook before deleting the record.
499
        $hook = new \core_ai\hook\before_provider_deleted(
500
            provider: $provider,
501
        );
502
        $hookmanager = \core\di::get(\core\hook\manager::class)->dispatch($hook);
503
        if ($hookmanager->isPropagationStopped()) {
504
            $deleted = false;
505
        } else {
506
            $deleted = $this->db->delete_records('ai_providers', ['id' => $provider->id]);
507
        }
508
        return $deleted;
509
    }
510
 
511
    /**
512
     * Enable a provider instance.
513
     *
514
     * @param provider $provider
515
     * @return provider
516
     */
517
    public function enable_provider_instance(provider $provider): provider {
518
        if (!$provider->enabled) {
519
            $provider = $provider->with(enabled: true);
520
            $this->db->update_record('ai_providers', $provider->to_record());
521
            $this->update_provider_order($provider->id, aiproviderplugin::ENABLE);
522
        }
523
 
524
        return $provider;
525
    }
526
 
527
    /**
528
     * Disable a provider.
529
     *
530
     * @param provider $provider
531
     * @return provider
532
     */
533
    public function disable_provider_instance(provider $provider): provider {
534
        if ($provider->enabled) {
535
            $hook = new \core_ai\hook\before_provider_disabled(
536
                provider: $provider,
537
            );
538
            $hookmanager = \core\di::get(\core\hook\manager::class)->dispatch($hook);
539
            if (!$hookmanager->isPropagationStopped()) {
540
                $provider = $provider->with(enabled: false);
541
                $this->db->update_record('ai_providers', $provider->to_record());
542
            }
543
            $this->update_provider_order($provider->id, aiproviderplugin::DISABLE);
544
        }
545
 
546
        return $provider;
547
    }
548
 
549
    /**
550
     * Sorts provider instances by configured order.
551
     *
552
     * @param array $unsorted of provider instance objects
553
     * @return array of provider instance objects
554
     */
555
    public static function sort_providers_by_order(array $unsorted): array {
556
        $sorted = [];
557
        $orderarray = explode(',', get_config('core_ai', 'provider_order'));
558
 
559
        foreach ($orderarray as $notused => $providerid) {
560
            foreach ($unsorted as $key => $provider) {
561
                if ($provider->id == $providerid) {
562
                    $sorted[] = $provider;
563
                    unset($unsorted[$key]);
564
                }
565
            }
566
        }
567
 
568
        return array_merge($sorted, $unsorted);
569
    }
570
 
571
    /**
572
     * Get the configured ai providers from the manager.
573
     *
574
     * @return array
575
     */
576
    public function get_sorted_providers(): array {
577
        $unsorted = $this->get_provider_instances();
578
        $orders = $this->sort_providers_by_order($unsorted);
579
        $sortedplugins = [];
580
 
581
        foreach ($orders as $order) {
582
            $sortedplugins[$order->id] = $unsorted[$order->id];
583
        }
584
 
585
        return $sortedplugins;
586
    }
587
 
588
    /**
589
     * Change the order of the provider instance relative to other provider instances.
590
     *
591
     * When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
592
     *
593
     * @param int $providerid The provider ID.
594
     * @param int $direction The direction to move the provider instance. Negative numbers mean up, Positive mean down.
595
     * @return bool Whether the provider has been updated or not.
596
     */
597
    public function change_provider_order(int $providerid, int $direction): bool {
598
        $activefactors = array_keys($this->get_sorted_providers());
599
        $key = array_search($providerid, $activefactors);
600
 
601
        if ($key === false) {
602
            return false;
603
        }
604
 
605
        $movedown = ($direction === aiproviderplugin::MOVE_DOWN && $key < count($activefactors) - 1);
606
        $moveup = ($direction === aiproviderplugin::MOVE_UP && $key >= 1);
607
        if ($movedown || $moveup) {
608
            $this->update_provider_order($providerid, $direction);
609
            return true;
610
        }
611
 
612
        return false;
613
    }
614
 
615
    /**
616
     * Update the provider instance order configuration.
617
     *
618
     * @param int $providerid The provider ID.
619
     * @param string|int $action
620
     *
621
     * @throws dml_exception
622
     */
623
    public function update_provider_order(int $providerid, string|int $action): void {
624
        $order = explode(',', get_config('core_ai', 'provider_order'));
625
        $key = array_search($providerid, $order);
626
 
627
        switch ($action) {
628
            case aiproviderplugin::MOVE_UP:
629
                if ($key >= 1) {
630
                    $fsave = $order[$key];
631
                    $order[$key] = $order[$key - 1];
632
                    $order[$key - 1] = $fsave;
633
                }
634
                break;
635
 
636
            case aiproviderplugin::MOVE_DOWN:
637
                if ($key < (count($order) - 1)) {
638
                    $fsave = $order[$key];
639
                    $order[$key] = $order[$key + 1];
640
                    $order[$key + 1] = $fsave;
641
                }
642
                break;
643
 
644
            case aiproviderplugin::ENABLE:
645
                if (!$key) {
646
                    $order[] = $providerid;
647
                }
648
                break;
649
 
650
            case aiproviderplugin::DISABLE:
651
                if ($key) {
652
                    unset($order[$key]);
653
                }
654
                break;
655
        }
656
 
657
        $this->set_provider_config(['provider_order' => implode(',', $order)], 'core_ai');
658
 
659
        \core\session\manager::gc(); // Remove stale sessions.
660
        \core_plugin_manager::reset_caches();
661
    }
662
 
663
    /**
664
     * Sets config variable for given provider instance.
665
     *
666
     * @param array $data The data to set.
667
     * @param string $plugin The plugin name.
668
     *
669
     * @return bool true or exception.
670
     * @throws dml_exception
671
     */
672
    public function set_provider_config(array $data, string $plugin): bool|dml_exception {
673
        $providerconf = get_config($plugin);
674
        foreach ($data as $key => $newvalue) {
675
            if (empty($providerconf->$key)) {
676
                add_to_config_log($key, null, $newvalue, $plugin);
677
                set_config($key, $newvalue, $plugin);
678
            } else if ($providerconf->$key != $newvalue) {
679
                add_to_config_log($key, $providerconf->$key, $newvalue, $plugin);
680
                set_config($key, $newvalue, $plugin);
681
            }
682
        }
683
        return true;
684
    }
685
}