*/
private array $services = [];
private function __construct() {}
public static function boot(): self
{
if (self::$instance === null) {
self::$instance = new self();
add_action('plugins_loaded', [self::$instance, 'register'], 9);
}
return self::$instance;
}
public static function instance(): self
{
return self::$instance ?? self::boot();
}
public function version(): string
{
return self::VERSION;
}
public function register(): void
{
$store = new Store();
$this->set(Store::class, $store);
// One Access for the request, over the same Store: it holds nothing else, and sharing the
// Store is what keeps the settings option at one read however many rows are asked.
$this->set(Access::class, new Access($store));
$factory = new Provider\Factory($store, new Provider\WpAi\CoreClient());
$this->set(Provider\Factory::class, $factory);
$this->set(Provider\ModelCatalog::class, new Provider\ModelCatalog($factory));
// The provider picker's two loose ends. The notice: `provider.kind` may say `wp-ai` on a
// WordPress without the AI client (the setting outlived a downgrade, or was written
// over REST), and the factory then builds Ollama; it is the factory's verdict
// (fallbackNotice()), printed here because the factory also runs under REST and WP-CLI,
// and only to someone who can change the setting. The catalog bust: the model list is
// one site-wide transient for five minutes, and a save that changes the provider (its
// kind, where it is, or the key that reaches it) would otherwise leave the previous
// provider's models on offer, and a turn routed at one of them, until it expired.
// There is no per-kind default model (`models.default` is the one default for both
// kinds), and that is safe only because of this bust: Chat\Pipeline resolves the model
// as UserPrefs::modelFor() ?? ModelCatalog::defaultId(), and both validate the stored
// id against the catalogue's list, so after a switch a stale id is replaced by a model
// the new provider lists. Against the previous provider's cached list they would pass a
// model the new provider does not have, and on `wp-ai` CoreClient refuses it by name
// on every turn for the transient's lifetime. Removing this bust needs a per-kind
// default (or an eager re-list) to go in with it.
// On `update_option_*` rather than in Store: every writer (the settings page, the REST
// route, WP-CLI, a filter) goes through the option, and not every writer goes through
// Store: the settings page saves through core's options.php, and any other code can call
// update_option().
// Neither closure is `static`, for the reason the receipt-retention hook below gives.
add_action('admin_notices', function () use ($factory): void {
$notice = $factory->fallbackNotice();
if ($notice === null || !current_user_can('manage_options')) {
return;
}
printf('
', esc_html($notice));
});
add_action('update_option_' . self::OPTION, function (mixed $old, mixed $new): void {
if (is_array($old) && is_array($new)) {
$changed = false;
foreach (['provider.kind', 'provider.base_url', 'provider.api_key'] as $key) {
$changed = $changed || ($old[$key] ?? null) !== ($new[$key] ?? null);
}
if (!$changed) {
return;
}
}
delete_transient(Provider\ModelCatalog::TRANSIENT);
}, 10, 2);
// And on the add, which is what a site's *first* save fires. update_option() creates the
// row rather than updating it when the current value is the registered default — with no
// row, get_option() returns that default, the `default_option_alpaca_bot_settings ===
// $old_value` branch is taken, and add_option() runs instead (core option.php:928-930,
// firing `add_option_{$option}` at :1176). So the settings screen's first Save fired
// neither this hook nor the one above, and an operator who picked their provider in that
// save kept the previous one's models for the transient's five minutes — which, by the
// argument above, means every turn in that window refused by name on `wp-ai`.
// Unconditional: an add has no old value to compare the three keys against, and the one
// deleted transient it can cost is on a save that creates the row: a site's first, or the
// first after the row was deleted.
add_action('add_option_' . self::OPTION, function (): void {
delete_transient(Provider\ModelCatalog::TRANSIENT);
});
// The list is flushed whenever a write through the options API changes the key the site
// uses (Settings\ProviderKey::resolve() of the row's `provider.api_key`). The key is not
// in the row: the row reads MASK before and after a change of key, so the row's
// update_option() can change nothing and fire neither hook above. The key's own option is
// added, updated or deleted on every change of a held key, and each of the three busts the
// list the old key was answered with. The rest of the changes move the row's own value
// and bust on it: a row still carrying a plaintext key (one migrate() has not reached)
// that is saved with '' holds no option to delete, so the row's `provider.api_key` going
// from the plaintext to '' is what the update hook above sees (it compares that key too);
// a row deleted outright reads back as the defaults, whose key is '', so its delete busts
// as well; a row's first write is the add above. A plaintext key moving out of the row
// (migrate(), or beforeSave() over a row migrate() has not reached), with the key
// unchanged, busts twice, on the option's add and on the row's plaintext going to MASK.
$providerKey = new Settings\ProviderKey();
$providerKey->register();
foreach (['add_option_', 'update_option_', 'delete_option_'] as $hook) {
add_action($hook . Settings\ProviderKey::OPTION, function (): void {
delete_transient(Provider\ModelCatalog::TRANSIENT);
});
}
add_action('delete_option_' . self::OPTION, function (): void {
delete_transient(Provider\ModelCatalog::TRANSIENT);
});
// An MCP server's header value is taken out of its row on every update_option() of the
// option, whoever calls it (Mcp\ServerSettings says why that is a filter, and what
// add_option() on its own does instead). The settings page and
// the REST route are handed this instance for the address check they run before a write.
$servers = new Mcp\ServerSettings();
$this->set(Mcp\ServerSettings::class, $servers);
$servers->register();
// Where every MCP client comes from: php-agents' client (Mcp\PhpAgentsClient), over an
// Mcp\Egress that checks each server's address when a client is built and pins the
// connection to the addresses that passed. Mcp\Discovery and Mcp\Toolkits are both
// handed this one.
$this->set(Mcp\ClientFactory::class, new Mcp\ClientFactory(null, new Mcp\Egress()));
// A save that re-pins a drifted tool, or drops its approval, answers the drift marker for
// it (Mcp\Drift). On `update_option_*` for the reason the catalog bust above gives: every
// writer of the option fires it. A site's first save fires `add_option_*` instead, and
// there is no stored pin for it to have changed.
add_action('update_option_' . self::OPTION, [Mcp\Drift::class, 'afterSave'], 10, 2);
$conversations = new Chat\ConversationStore($store);
$this->set(Chat\ConversationStore::class, $conversations);
add_action('init', [$conversations, 'registerPostType']);
$meter = new Chat\UsageMeter($store);
$this->set(Chat\UsageMeter::class, $meter);
add_action('init', [$meter, 'registerPostType']);
// The receipt retention cron: (re)scheduled on init so an already-installed site gets it
// without a reactivation (UsageMeter::scheduleCleanup() says why), cleared by deactivate().
add_action('init', [$meter, 'scheduleCleanup']);
// Not `static`: Brain Monkey (PHP 8.4) warns when it inspects a static closure hooked
// through add_action(), as the migration closure below found first.
add_action(Chat\UsageMeter::CLEANUP_HOOK, function () use ($meter): void {
$meter->cleanup();
});
$caps = new Chat\CapPolicy($store, $meter);
$this->set(Chat\CapPolicy::class, $caps);
$collector = new Context\Collector([new Context\CurrentScreenSource()]);
$this->set(Context\Collector::class, $collector);
$prefs = new Chat\UserPrefs();
$this->set(Chat\UserPrefs::class, $prefs);
// The registry is built empty before the pipeline and filled after it: the pipeline asks
// the registry what a turn may run, and the summarize toolkit runs its inner turn through
// the pipeline, so one of the two has to exist before the other is complete. Nothing is
// read from the registry until a turn runs, well after plugins_loaded. It is handed the
// container's Access rather than making one: enabled() holds each toolkit to its row, and
// sharing the instance is what keeps that on the one memoised read of the settings option.
// The MCP servers reach it through Mcp\Toolkits over the same Store and Access, and the
// container's ClientFactory, the seam Mcp\Discovery lists servers through as well.
$registry = new Toolkit\Registry($store, $this->get(Access::class), new Mcp\Toolkits($store, $this->get(Access::class), $this->get(Mcp\ClientFactory::class)));
$this->set(Chat\Pipeline::class, new Chat\Pipeline($store, $factory, $this->get(Provider\ModelCatalog::class), $conversations, $meter, $caps, $collector, $prefs, $registry));
// The built-in toolkits, under the ids Schema's `toolkits.enabled` options name. The ones
// that act as a user (summarize, draft_post and abilities) take get_current_user_id as a
// closure and ask it when a tool runs, never here. Not because the id is unreadable
// here -- this runs on plugins_loaded:9,
// and core has loaded pluggable.php and registered all three `determine_current_user`
// filters before `plugins_loaded` fires (WP 7.1 wp-settings.php:612 and :154 against the
// do_action at :630; default-filters.php:520-522), so a cookie request would resolve
// here and cache itself for the rest of the request. It is that
// an id read here is the id for the whole request, and the acting user moves after this
// point: WP-CLI leaves it at 0 until `wp alpaca-bot chat` calls wp_set_current_user()
// (Cli\ChatCommand says why it does), and any wp_set_current_user() elsewhere moves it
// again. A boot-time read would freeze the wrong one, and would also resolve and cache
// $current_user from inside a plugin's boot, ahead of anything that wanted to hook
// `determine_current_user` on plugins_loaded itself. The registry decides what is enabled
// when a turn asks (Toolkit\Registry), so nothing about the setting is read here either.
$webFetch = new Toolkit\WebFetchToolkit($store);
$registry->register('web_fetch', $webFetch);
$summarize = new Toolkit\SummarizeToolkit($this->get(Chat\Pipeline::class), get_current_user_id(...));
$registry->register('summarize', $summarize);
$draftPost = new Toolkit\DraftPostToolkit(get_current_user_id(...));
$registry->register('draft_post', $draftPost);
$abilitiesKit = new Toolkit\AbilitiesToolkit($store, get_current_user_id(...));
$registry->register(Toolkit\AbilitiesToolkit::ID, $abilitiesKit);
$this->set(Toolkit\Registry::class, $registry);
// The abilities, on core's two hooks and no other (Abilities\Register says why there is
// no init fallback), the category's hook first because core fires it first and refuses
// an ability whose category it does not know. The toolkits come from the registry when
// a callback runs, and the same user closure, so an ability and a chat turn act as one
// user over the same toolkits.
$abilities = new Abilities\Register($this->get(Chat\Pipeline::class), $registry, get_current_user_id(...), access: $this->get(Access::class));
$this->set(Abilities\Register::class, $abilities);
add_action('wp_abilities_api_categories_init', [$abilities, 'registerCategory']);
add_action('wp_abilities_api_init', [$abilities, 'register']);
// On init, after the post types (priority 10), not on admin_init: WP-CLI loads WordPress
// and fires init but never admin_init, and in P1 the CLI is the whole user surface, so an
// upgraded 0.4 site's first `wp alpaca-bot chat` must already see its configured
// settings. Store::all() runs Schema::defaults() through __(), which is safe during init
// (the just-in-time textdomain loader only objects before it).
add_action('init', function () use ($store): void {
$migration = new Migrate04($store);
if ($migration->needed()) {
$migration->run();
}
// Its own class, not a step of Migrate04: that class's steps are flagged, and this one
// needs no flag, since the row it reads is already in alloptions (ProviderKey::migrate()).
Settings\ProviderKey::migrate();
}, 20);
add_action('rest_api_init', function (): void {
foreach ($this->controllers() as $controller) {
$controller->register();
}
});
// The admin screens hook admin_init and admin_menu without an is_admin() gate: neither
// action fires outside wp-admin, so the gate would only decide whether two small objects
// are built, and it would decide wrong where is_admin() is false at plugins_loaded but a
// test (wp-phpunit sets no screen until a test does) later fires the actions itself.
// options.php, which every save posts to, is wp-admin and fires admin_init as any screen.
// The page is handed controllers() itself, uncalled, so the Chat row's note asks the routes
// the REST API registers (Kanboard #4537), and lists them only when that row is rendered.
$settingsPage = new Admin\SettingsPage($store, $this->get(Provider\ModelCatalog::class), $this->get(Access::class), null, $servers, $this->controllers(...));
$this->set(Admin\SettingsPage::class, $settingsPage);
add_action('admin_init', [$settingsPage, 'register']);
$chatScreen = new Admin\ChatScreen($store, $this->get(Provider\ModelCatalog::class), $conversations, $prefs);
$this->set(Admin\ChatScreen::class, $chatScreen);
$menu = new Admin\Menu($settingsPage, [$chatScreen, 'render'], $this->get(Access::class));
add_action('admin_menu', [$menu, 'register']);
// Assets::enqueue() gates on the hook suffix itself, so this listens on every admin
// screen and acts on two: the chat screen's assets; the settings page's inline rules and
// htmx, for the Tools tab's Discover button.
// The heartbeat answer runs on admin-ajax, where no enqueue hook fires, so it is hooked here.
$assets = new Admin\Assets();
add_action('admin_enqueue_scripts', [$assets, 'enqueue']);
add_filter('heartbeat_received', [$assets, 'heartbeat'], 10, 2);
// The help tabs of the chat screen and the settings page; HelpTabs gates on the screen id.
add_action('current_screen', [new Admin\HelpTabs(), 'add']);
// The chat on the other admin screens: a launcher and an empty drawer, and the chat itself
// only once it is opened (Admin\Drawer says which screens, who, and what it costs a screen).
$drawer = new Admin\Drawer($this->get(Access::class), $prefs, $assets);
$this->set(Admin\Drawer::class, $drawer);
add_action('admin_enqueue_scripts', [$drawer, 'enqueue']);
add_action('admin_footer', [$drawer, 'footer']);
// And on a block editor screen, where there is no drawer, the editor's own sidebar.
add_action('enqueue_block_editor_assets', [$drawer, 'enqueueEditor']);
// Site Health's direct test for web_fetch: whether it can run pinned here (Admin\SiteHealth).
// Hooked unconditionally: core applies the filter only on its own screen and cron run.
add_filter('site_status_tests', [new Admin\SiteHealth($store), 'register']);
// The two shortcodes, registered now rather than on init: `$shortcode_tags` exists from
// shortcodes.php's load, and a shortcode registered on plugins_loaded is there for
// whatever renders content first. The shim runs the same web_fetch instance the
// registry holds, so the two fetch under one guard and one user agent.
$shortcode = new Shortcodes\Chat($store, $this->get(Provider\ModelCatalog::class), $conversations, $prefs, $this->get(Chat\Pipeline::class), new View\Markdown(), $assets, $this->get(Access::class));
$this->set(Shortcodes\Chat::class, $shortcode);
$shortcode->register();
$shim = new Shortcodes\AgentShim($shortcode, $this->get(Chat\Pipeline::class), $registry);
$this->set(Shortcodes\AgentShim::class, $shim);
$shim->register();
// WP-CLI is not a dependency: the command is only registered when WP-CLI is the
// process running us, and the class itself never references WP_CLI until then.
if (defined('WP_CLI') && constant('WP_CLI')) {
\WP_CLI::add_command('alpaca-bot', new Cli\ChatCommand($this->get(Chat\Pipeline::class), $this->get(Provider\ModelCatalog::class), $meter, $store, servers: $this->get(Mcp\ServerSettings::class)));
// A raw `wp option update|patch|add` of the option goes through the same Store and
// checks (Cli\RawOptionWrite says how, and why it has to end the command itself).
(new Cli\RawOptionWrite($store, $this->get(Mcp\ServerSettings::class)))->register();
}
}
/**
* The deactivation hook (alpaca-bot.php registers it): the one thing the plugin leaves in
* the database that would keep running without it is the retention cron event. Options,
* conversations and receipts stay; deactivation is not uninstall.
*/
public static function deactivate(): void
{
Chat\UsageMeter::unscheduleCleanup();
}
/**
* The REST controllers to register, through filter `alpaca_bot/rest/controllers`. The plugin's
* own controllers are the array handed to the filter (later tasks add theirs here); a third
* party appends its own Rest\Controller subclass to get the same namespace, capability
* filters and rate limit, or drops one of ours to unregister its routes. Resolved on
* rest_api_init, not at register(), so a filter added on plugins_loaded or init is seen, and
* the controllers are built then too: they hold the container's services, which all exist by
* plugins_loaded, but building them only for a REST request keeps every other request free
* of them. The one other caller is Settings › Access, which lists them when it renders the
* Chat row, to ask each route that follows that row (Admin\SettingsPage::renderAccess()).
* Anything that is not a Controller is dropped rather than left to fatal inside register().
* Every controller that survives is handed the container's Access, a third party's included,
* so a route registered *through this filter* may declare Rest\Controller::CHAT and resolve
* to the Chat row this site saved. A controller registered outside it — built and
* register()ed on rest_api_init by hand — is never handed one, and its CHAT routes resolve to
* Access::defaults() instead, the shipped `edit_posts`, whatever the site saved.
*
* @return list
*/
private function controllers(): array
{
/**
* Filters the REST controllers registered under `alpaca-bot/v1`, on `rest_api_init`, and
* on Settings › Access, which lists their Chat-row routes to ask each one's filter. Append
* a Rest\Controller subclass to get the namespace, the `alpaca_bot/capability/{route}`
* permission filters and the rate limit without writing them, and the Chat row for a route
* that declares `Controller::CHAT` — or drop one of the plugin's to unregister its routes
* (each controller's docblock says what a site loses with it).
* Anything that is not a Controller is dropped rather than left to fatal inside register().
*
* @since 0.5.0
* @param list $controllers the plugin's controllers
* @var mixed $controllers what the filter returned, checked before it is trusted
*/
$controllers = apply_filters('alpaca_bot/rest/controllers', [
new Rest\ChatController($this->get(Chat\Pipeline::class)),
new Rest\StreamController($this->get(Chat\Pipeline::class), $this->get(Store::class)),
new Rest\ConversationsController($this->get(Chat\ConversationStore::class), $this->get(Store::class)),
new Rest\ModelsController($this->get(Provider\ModelCatalog::class), $this->get(Store::class)),
new Rest\SettingsController($this->get(Store::class), $this->get(Mcp\ServerSettings::class)),
new Rest\UsageController($this->get(Chat\UsageMeter::class), $this->get(Store::class)),
new Rest\ViewController($this->get(Chat\ConversationStore::class), $this->get(Store::class), $this->get(Provider\ModelCatalog::class), new View\Markdown(), $this->get(Chat\UserPrefs::class), new Mcp\Discovery($this->get(Store::class), $this->get(Mcp\ClientFactory::class))),
]);
$access = $this->get(Access::class);
$controllers = array_values(array_filter(
is_array($controllers) ? $controllers : [],
static fn(mixed $controller): bool => $controller instanceof Rest\Controller,
));
foreach ($controllers as $controller) {
$controller->useAccess($access);
}
return $controllers;
}
public function set(string $id, object $service): void
{
$this->services[$id] = $service;
}
/**
* @template T of object
* @param class-string $id
* @return T
*/
public function get(string $id): object
{
if (!isset($this->services[$id])) {
throw new \RuntimeException("Alpaca Bot service not registered: {$id}");
}
/** @var T */
return $this->services[$id];
}
}