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_ai\form\action_settings_form;
|
|
|
20 |
use Psr\Http\Message\RequestInterface;
|
|
|
21 |
use Spatie\Cloneable\Cloneable;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Class provider.
|
|
|
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 |
abstract class provider {
|
|
|
31 |
use Cloneable;
|
|
|
32 |
|
|
|
33 |
/** @var string $provider The provider used to make this instance */
|
|
|
34 |
public readonly string $provider;
|
|
|
35 |
|
|
|
36 |
/** @var array The configuration for this instance. */
|
|
|
37 |
public readonly array $config;
|
|
|
38 |
|
|
|
39 |
/** @var array The action specific settings for this instance. */
|
|
|
40 |
public readonly array $actionconfig;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Create a new provider.
|
|
|
44 |
*
|
|
|
45 |
* @param bool $enabled Whether the gateway is enabled
|
|
|
46 |
* @param string $name The name of the provider config.
|
|
|
47 |
* @param string $config The configuration for this instance.
|
|
|
48 |
* @param string $actionconfig The action specific settings for this instance.
|
|
|
49 |
* @param int|null $id The id of the provider in the database.
|
|
|
50 |
*/
|
|
|
51 |
public function __construct(
|
|
|
52 |
/** @var bool Whether the gateway is enabled */
|
|
|
53 |
public readonly bool $enabled,
|
|
|
54 |
/** @var string The name of the provider config. */
|
|
|
55 |
public string $name,
|
|
|
56 |
string $config,
|
|
|
57 |
string $actionconfig = '',
|
|
|
58 |
/** @var null|int The ID of the provider in the database, or null if it has not been persisted yet. */
|
|
|
59 |
public readonly ?int $id = null,
|
|
|
60 |
) {
|
|
|
61 |
$this->provider = strstr(get_class($this), '\\', true);
|
|
|
62 |
$this->config = json_decode($config, true);
|
|
|
63 |
if ($actionconfig == '') {
|
|
|
64 |
$this->actionconfig = static::initialise_action_settings();
|
|
|
65 |
} else {
|
|
|
66 |
$this->actionconfig = json_decode($actionconfig, true);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Get the actions that this provider supports.
|
|
|
72 |
*
|
|
|
73 |
* Returns an array of action class names.
|
|
|
74 |
*
|
|
|
75 |
* @return array An array of action class names.
|
|
|
76 |
*/
|
|
|
77 |
abstract public static function get_action_list(): array;
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Initialise the action settings array.
|
|
|
81 |
*
|
|
|
82 |
* @return array The initialised action settings.
|
|
|
83 |
*/
|
|
|
84 |
public static function initialise_action_settings(): array {
|
|
|
85 |
$actions = static::get_action_list();
|
|
|
86 |
$actionconfig = [];
|
|
|
87 |
foreach ($actions as $action) {
|
|
|
88 |
$actionconfig[$action] = [
|
|
|
89 |
'enabled' => true,
|
|
|
90 |
'settings' => static::get_action_setting_defaults($action),
|
|
|
91 |
];
|
|
|
92 |
}
|
|
|
93 |
return $actionconfig;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Given an action class name, return an array of sub actions
|
|
|
98 |
* that this provider supports.
|
|
|
99 |
*
|
|
|
100 |
* @param string $classname The action class name.
|
|
|
101 |
* @return array An array of supported sub actions.
|
|
|
102 |
*/
|
|
|
103 |
public function get_sub_actions(string $classname): array {
|
|
|
104 |
return [];
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Get the name of the provider.
|
|
|
109 |
*
|
|
|
110 |
* @return string The name of the provider.
|
|
|
111 |
*/
|
|
|
112 |
public function get_name(): string {
|
|
|
113 |
return \core\component::get_component_from_classname(get_class($this));
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Get any action settings for this provider.
|
|
|
118 |
*
|
|
|
119 |
* @param string $action The action class name.
|
|
|
120 |
* @param array $customdata The customdata for the form.
|
|
|
121 |
* @return action_settings_form|bool The settings form for this action or false in no settings.
|
|
|
122 |
*/
|
|
|
123 |
public static function get_action_settings(
|
|
|
124 |
string $action,
|
|
|
125 |
array $customdata = [],
|
|
|
126 |
): action_settings_form|bool {
|
|
|
127 |
return false;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Get the default settings for an action.
|
|
|
132 |
*
|
|
|
133 |
* @param string $action The action class name.
|
|
|
134 |
* @return array The default settings for the action.
|
|
|
135 |
*/
|
|
|
136 |
public static function get_action_setting_defaults(string $action): array {
|
|
|
137 |
return [];
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Check if the request is allowed by the rate limiter.
|
|
|
142 |
*
|
|
|
143 |
* @param aiactions\base $action The action to check.
|
|
|
144 |
* @return array|bool True on success, array of error details on failure.
|
|
|
145 |
*/
|
|
|
146 |
public function is_request_allowed(aiactions\base $action): array|bool {
|
|
|
147 |
$ratelimiter = \core\di::get(rate_limiter::class);
|
|
|
148 |
$component = \core\component::get_component_from_classname(get_class($this));
|
|
|
149 |
|
|
|
150 |
// Check the user rate limit.
|
|
|
151 |
if (isset($this->config['enableuserratelimit']) && $this->config['enableuserratelimit']) {
|
|
|
152 |
if (!$ratelimiter->check_user_rate_limit(
|
|
|
153 |
component: $component,
|
|
|
154 |
ratelimit: $this->config['userratelimit'],
|
|
|
155 |
userid: $action->get_configuration('userid')
|
|
|
156 |
)) {
|
|
|
157 |
return [
|
|
|
158 |
'success' => false,
|
|
|
159 |
'errorcode' => 429,
|
|
|
160 |
'errormessage' => 'User rate limit exceeded',
|
|
|
161 |
];
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Check the global rate limit.
|
|
|
166 |
if (isset($this->config['enableglobalratelimit']) && $this->config['enableglobalratelimit']) {
|
|
|
167 |
if (!$ratelimiter->check_global_rate_limit(
|
|
|
168 |
component: $component,
|
|
|
169 |
ratelimit: $this->config['globalratelimit']
|
|
|
170 |
)) {
|
|
|
171 |
return [
|
|
|
172 |
'success' => false,
|
|
|
173 |
'errorcode' => 429,
|
|
|
174 |
'errormessage' => 'Global rate limit exceeded',
|
|
|
175 |
];
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
return true;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Check if a provider has the minimal configuration to work.
|
|
|
184 |
*
|
|
|
185 |
* @return bool Return true if configured.
|
|
|
186 |
*/
|
|
|
187 |
public function is_provider_configured(): bool {
|
|
|
188 |
return false;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Update a request to add any headers required by the provider (if needed).
|
|
|
193 |
* AI providers will need to override this method to add their own headers.
|
|
|
194 |
*
|
|
|
195 |
* @param RequestInterface $request
|
|
|
196 |
* @return RequestInterface
|
|
|
197 |
*/
|
|
|
198 |
public function add_authentication_headers(RequestInterface $request): RequestInterface {
|
|
|
199 |
return $request;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Generate a user id.
|
|
|
204 |
*
|
|
|
205 |
* This is a hash of the site id and user id,
|
|
|
206 |
* this means we can determine who made the request
|
|
|
207 |
* but don't pass any personal data to the AI provider.
|
|
|
208 |
*
|
|
|
209 |
* @param string $userid The user id.
|
|
|
210 |
* @return string The generated user id.
|
|
|
211 |
*/
|
|
|
212 |
public function generate_userid(string $userid): string {
|
|
|
213 |
global $CFG;
|
|
|
214 |
return hash('sha256', $CFG->siteidentifier . $userid);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Convert this object to a stdClass, suitable for saving to the database.
|
|
|
219 |
*
|
|
|
220 |
* @return \stdClass
|
|
|
221 |
*/
|
|
|
222 |
public function to_record(): \stdClass {
|
|
|
223 |
return (object) [
|
|
|
224 |
'id' => $this->id,
|
|
|
225 |
'name' => $this->name,
|
|
|
226 |
'provider' => get_class($this),
|
|
|
227 |
'enabled' => $this->enabled,
|
|
|
228 |
'config' => json_encode($this->config),
|
|
|
229 |
'actionconfig' => json_encode($this->actionconfig),
|
|
|
230 |
];
|
|
|
231 |
}
|
|
|
232 |
}
|