-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
383 lines (321 loc) · 10.5 KB
/
plugin.php
File metadata and controls
383 lines (321 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* Plugin Name: Infomaniak AI Toolkit
* Plugin URI: https://www.infomaniak.com/en/hosting/ai-tools
* Description: AI toolkit for WordPress powered by Infomaniak. Provides access to open-source models (Llama, Mistral, DeepSeek, Qwen) hosted in Switzerland.
* Requires at least: 6.9
* Requires PHP: 8.0
* Version: 1.0.0
* Author: Custom
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: infomaniak-ai-toolkit
*
* @package WordPress\InfomaniakAiToolkit
*/
declare(strict_types=1);
namespace WordPress\InfomaniakAiToolkit;
use WordPress\AiClient\AiClient;
use WordPress\InfomaniakAiToolkit\Commands\CommandLoader;
use WordPress\InfomaniakAiToolkit\Commands\CommandSchema;
use WordPress\InfomaniakAiToolkit\Provider\InfomaniakProvider;
use WordPress\InfomaniakAiToolkit\Memory\MemorySchema;
use WordPress\InfomaniakAiToolkit\Usage\UsageSchema;
use WordPress\InfomaniakAiToolkit\Usage\UsageTracker;
if (!defined('ABSPATH')) {
return;
}
define('INFOMANIAK_AI_PLUGIN_FILE', __FILE__);
require_once __DIR__ . '/src/autoload.php';
// Create database tables on plugin activation.
register_activation_hook(__FILE__, [UsageSchema::class, 'install']);
register_activation_hook(__FILE__, [MemorySchema::class, 'install']);
register_activation_hook(__FILE__, [CommandSchema::class, 'install']);
/**
* Upgrades the usage table schema if needed and starts usage tracking.
*
* @since 1.0.0
*/
function init_usage_tracking(): void
{
UsageSchema::maybeUpgrade();
UsageTracker::init();
}
add_action('init', __NAMESPACE__ . '\\init_usage_tracking');
/**
* Upgrades the memory table schema if needed.
*
* @since 1.0.0
*/
function init_memory(): void
{
MemorySchema::maybeUpgrade();
}
add_action('init', __NAMESPACE__ . '\\init_memory');
/**
* Upgrades the commands table schema if needed.
*
* @since 1.0.0
*/
function init_commands(): void
{
CommandSchema::maybeUpgrade();
}
add_action('init', __NAMESPACE__ . '\\init_commands');
/**
* Loads the plugin text domain for translations.
*
* @since 1.0.0
*/
function load_textdomain(): void
{
static $loading = false;
if ($loading) {
return;
}
$loading = true;
$domain = 'infomaniak-ai-toolkit';
$locale = determine_locale();
$dir = __DIR__ . '/languages';
// Prefer .l10n.php (WP 6.5+), fall back to .mo.
$php_file = "$dir/$domain-$locale.l10n.php";
$mo_file = "$dir/$domain-$locale.mo";
if (file_exists($php_file)) {
\load_textdomain($domain, $php_file);
} elseif (file_exists($mo_file)) {
\load_textdomain($domain, $mo_file);
}
$loading = false;
}
add_action('init', __NAMESPACE__ . '\\load_textdomain');
/**
* Registers the Infomaniak AI Toolkit with the AI Client.
*
* @since 1.0.0
*
* @return void
*/
function register_provider(): void
{
if (!class_exists(AiClient::class)) {
return;
}
$registry = AiClient::defaultRegistry();
if ($registry->hasProvider(InfomaniakProvider::class)) {
return;
}
$registry->registerProvider(InfomaniakProvider::class);
}
add_action('init', __NAMESPACE__ . '\\register_provider', 5);
// Admin settings page.
if (is_admin()) {
Admin\SettingsPage::init();
}
/**
* Fetches and caches the list of available models from Infomaniak.
*
* Uses a transient with a 12-hour expiry. The list is stored as an array of
* ['id' => '...', 'name' => '...', 'type' => '...'] entries.
* Only models with info_status "ready" and supported types (llm, image) are included.
*
* @since 1.0.0
*
* @param bool $force Force a refresh, ignoring the cache.
* @return array List of available models, each with 'id', 'name', and 'type' keys.
*/
function refresh_models_cache(bool $force = false): array
{
$transient_key = 'infomaniak_ai_models';
if (!$force) {
$cached = get_transient($transient_key);
if (is_array($cached)) {
return $cached;
}
}
// Read the API key from the connector setting.
try {
remove_filter('option_connectors_ai_infomaniak_api_key', '_wp_connectors_mask_api_key');
$apiKey = get_option('connectors_ai_infomaniak_api_key', '');
} finally {
add_filter('option_connectors_ai_infomaniak_api_key', '_wp_connectors_mask_api_key');
}
if (empty($apiKey)) {
return [];
}
$url = 'https://api.infomaniak.com/1/ai/models';
$response = wp_remote_get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
],
'timeout' => 15,
]);
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
return [];
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (!isset($body['data']) || !is_array($body['data'])) {
return [];
}
$supported_types = ['llm', 'image'];
$models = [];
foreach ($body['data'] as $model) {
if (!is_array($model) || empty($model['name'])) {
continue;
}
// Only include models that are ready.
if (($model['info_status'] ?? '') !== 'ready') {
continue;
}
// Only include supported types.
$type = $model['type'] ?? '';
if (!in_array($type, $supported_types, true)) {
continue;
}
$models[] = [
'id' => $model['name'],
'name' => $model['description'] ?? $model['name'],
'type' => $type,
];
}
set_transient($transient_key, $models, 12 * HOUR_IN_SECONDS);
return $models;
}
/**
* Returns the cached list of available Infomaniak models.
*
* @since 1.0.0
*
* @return array List of models with 'id', 'name', and 'type' keys.
*/
function get_available_models(): array
{
return refresh_models_cache();
}
/**
* Refreshes the models cache after the connector keys are passed to the AI client.
*
* Runs at init priority 25, after connectors pass keys at priority 20.
*
* @since 1.0.0
*/
function maybe_refresh_models(): void
{
if (empty(InfomaniakProvider::getProductId())) {
return;
}
$cached = get_transient('infomaniak_ai_models');
// Refresh if no cache exists or if cache has old format (missing 'type' key).
$needs_refresh = false === $cached;
if (!$needs_refresh && is_array($cached) && !empty($cached)) {
$first = reset($cached);
if (is_array($first) && !array_key_exists('type', $first)) {
$needs_refresh = true;
}
}
if ($needs_refresh) {
refresh_models_cache(true);
}
}
add_action('init', __NAMESPACE__ . '\\maybe_refresh_models', 25);
/**
* Forces a models cache refresh when the product ID setting is updated.
*
* @since 1.0.0
*/
function on_product_id_updated(): void
{
delete_transient('infomaniak_ai_models');
}
add_action('update_option_infomaniak_ai_product_id', __NAMESPACE__ . '\\on_product_id_updated');
/**
* Adds plugin slug to the Infomaniak connector script module data.
*
* This ensures the connector item gets a CSS class we can target
* for icon injection on the Settings > Connectors page.
*
* @since 1.0.0
*
* @param array $data Script module data.
* @return array Modified data with plugin slug added.
*/
function add_connector_plugin_data(array $data): array
{
if (isset($data['connectors']['infomaniak'])) {
if (!isset($data['connectors']['infomaniak']['plugin'])) {
$data['connectors']['infomaniak']['plugin'] = [];
}
$data['connectors']['infomaniak']['plugin']['slug'] = 'infomaniak-ai-toolkit';
}
return $data;
}
add_filter('script_module_data_options-connectors-wp-admin', __NAMESPACE__ . '\\add_connector_plugin_data');
/**
* Conditionally registers the icon injection script for the Connectors page.
*
* @since 1.0.0
*
* @param string $hook_suffix The current admin page.
*/
function enqueue_connector_icon(string $hook_suffix): void
{
$screen = get_current_screen();
$is_connectors = (
(isset($_GET['page']) && 'options-connectors-wp-admin' === $_GET['page']) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|| ($screen && 'options-connectors' === $screen->id)
);
if (!$is_connectors) {
return;
}
add_action('admin_print_footer_scripts', __NAMESPACE__ . '\\print_connector_icon_script');
}
/**
* Prints the inline script that injects the Infomaniak logo SVG.
*
* Since WordPress core hardcodes connector logos for built-in providers only,
* this uses a MutationObserver to inject the SVG icon into the DOM
* once the React-rendered connector item appears.
*
* @since 1.0.0
*/
function print_connector_icon_script(): void
{
// Infomaniak logo SVG adapted to 40x40 to match other connector logos.
$svg = '<svg width="40" height="40" viewBox="0 0 81 80" fill="none" xmlns="http://www.w3.org/2000/svg">'
. '<rect x="0.666504" width="80" height="80" rx="13.3333" fill="#0098FF"/>'
. '<path d="M34.5674 13.3333H19.3331V66.6666H34.5674V56.6257L40.1704 51.1686'
. 'L48.044 66.6666H64.853L50.0947 41.5643L64.0473 28.0308H45.7002L34.5674 40.8367V13.3333Z" fill="white"/>'
. '</svg>';
?>
<script>
(function() {
var svg = <?php echo wp_json_encode( $svg ); ?>;
var selector = '.connector-item--infomaniak-ai-toolkit';
function injectIcon() {
var item = document.querySelector(selector);
if (!item || item.dataset.infomaniakIcon) return;
var hstack = item.firstElementChild && item.firstElementChild.firstElementChild;
if (!hstack) return;
if (hstack.querySelector('svg')) return;
var wrapper = document.createElement('div');
wrapper.style.flex = 'none';
wrapper.style.lineHeight = '0';
wrapper.innerHTML = svg;
hstack.insertBefore(wrapper, hstack.firstElementChild);
item.dataset.infomaniakIcon = '1';
}
injectIcon();
var observer = new MutationObserver(function() { injectIcon(); });
observer.observe(document.body, { childList: true, subtree: true });
setTimeout(function() { observer.disconnect(); }, 10000);
})();
</script>
<?php
}
add_action('admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_connector_icon', 20);
// Register markdown commands as WordPress Abilities.
add_action('wp_abilities_api_init', [CommandLoader::class, 'registerAll']);
// WP-CLI command registration.
if (defined('WP_CLI') && WP_CLI) {
\WP_CLI::add_command('infomaniak-ai', CLI\Command::class);
}