Skip to content

Add hook for registering providers before metadata collection #43

@Jameswlepage

Description

@Jameswlepage

Problem

The API_Credentials_Manager::collect_providers() method collects provider metadata during AI_Client::init(). This happens early in the WordPress init action, making it difficult for plugins to register additional providers that appear on the credentials screen.

Currently, any providers registered after AI_Client::init() runs will:

  • Be available in the runtime registry
  • Not appear on the Settings → AI Credentials screen (because collection already happened)

Current Workaround

Plugins must carefully order their initialization to register providers before calling AI_Client::init():

// 1. Init HTTP discovery first
WP_AI_Client_Discovery_Strategy::init();

// 2. Register custom providers to the registry
AiClient::defaultRegistry()->registerProvider(MyProvider::class);

// 3. Then init AI_Client (which collects providers)
AI_Client::init();

This is fragile and requires knowledge of internal implementation details.

Proposed Solution

Add a hook before provider collection:

private function collect_providers(): void {
    global $wp_ai_client_providers_metadata;

    if ( ! isset( $wp_ai_client_providers_metadata ) ) {
        $wp_ai_client_providers_metadata = array();
    }

    // Allow plugins to register providers before collection
    do_action( 'wp_ai_client_before_collect_providers', AiClient::defaultRegistry() );

    $registry = AiClient::defaultRegistry();
    // ... rest of collection logic
}

This would allow plugins to simply:

add_action( 'wp_ai_client_before_collect_providers', function( $registry ) {
    $registry->registerProvider( MyCustomProvider::class );
});

Alternative

Collect providers lazily on admin_menu (only when the credentials screen is actually needed) instead of eagerly during init.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions