-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
97 lines (82 loc) · 2.96 KB
/
Copy pathplugin.php
File metadata and controls
97 lines (82 loc) · 2.96 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
<?php
/**
* Plugin Name: AI Provider for WebLLM
* Plugin URI: https://github.com/ProgressPlanner/ai-provider-for-webllm
* Description: Registers an in-browser WebLLM model as a client-side AI Provider for the WordPress AI Client.
* Requires at least: 7.0
* Requires PHP: 7.4
* Version: 0.1.0
* Author: Joost de Valk
* Author URI: https://progressplanner.com/
* License: GPL-2.0-or-later
* License URI: https://spdx.org/licenses/GPL-2.0-or-later.html
* Text Domain: ai-provider-for-webllm
*
* @package WordPress\WebLlmAiProvider
*/
declare(strict_types=1);
namespace WordPress\WebLlmAiProvider;
use WordPress\AiClient\AiClient;
use WordPress\WebLlmAiProvider\Bridge\RestController;
use WordPress\WebLlmAiProvider\Bridge\WebLlmBridge;
use WordPress\WebLlmAiProvider\Provider\WebLlmProvider;
use WordPress\WebLlmAiProvider\Settings\AdminPage;
use WordPress\WebLlmAiProvider\Worker\WorkerController;
if (!defined('ABSPATH')) {
return;
}
/*
* WebLLM runs entirely in the browser and needs no API key. But core's Connectors
* screen only surfaces connectors that authenticate with an `api_key`, so the provider
* declares that method (see WebLlmProvider). To keep users from ever being asked for a
* key, we hardcode the key constant — core derives the name `WEBLLM_API_KEY` from the
* provider id `webllm` — to a sentinel value, so the credential is always "supplied".
*/
if (!defined('WEBLLM_API_KEY')) {
define('WEBLLM_API_KEY', 'not-needed');
}
require_once __DIR__ . '/src/autoload.php';
/**
* Registers the AI Provider for WebLLM with the AI Client.
*
* Unlike the cloud providers, WebLLM is a client-side (browser) provider: the
* model runs in the visitor's browser via WebGPU. The PHP classes registered
* here describe the provider and its models so the AI Client and the
* Settings > Connectors UI recognise it. Server-initiated (PHP) generation is
* bridged to a connected browser worker (see Bridge\WebLlmBridge); if no worker
* is connected it fails loudly, since there is no headless path.
*
* @since 0.1.0
*
* @return void
*/
function register_provider(): void
{
if (!class_exists(AiClient::class)) {
return;
}
$registry = AiClient::defaultRegistry();
if ($registry->hasProvider(WebLlmProvider::class)) {
return;
}
$registry->registerProvider(WebLlmProvider::class);
}
add_action('init', __NAMESPACE__ . '\\register_provider', 5);
/**
* Registers the Settings > WebLLM admin page (model selection).
*
* @since 0.2.0
*
* @return void
*/
function register_admin_page(): void
{
AdminPage::register();
}
add_action('init', __NAMESPACE__ . '\\register_admin_page');
// The browser-worker bridge: REST endpoints + the jobs table that lets PHP-initiated
// generation run in a connected browser (see Bridge\WebLlmBridge).
RestController::register();
WorkerController::register();
add_action('init', [WebLlmBridge::class, 'maybeInstall']);
register_activation_hook(__FILE__, [WebLlmBridge::class, 'maybeInstall']);