Skip to content

Fix: register providers and load .env in cli.php#224

Open
sarthak-19 wants to merge 1 commit into
WordPress:trunkfrom
sarthak-19:feature/fix-cli-provider-registration
Open

Fix: register providers and load .env in cli.php#224
sarthak-19 wants to merge 1 commit into
WordPress:trunkfrom
sarthak-19:feature/fix-cli-provider-registration

Conversation

@sarthak-19

Copy link
Copy Markdown
Contributor

What

cli.php was missing two bootstrapping steps, causing every run to fail with:

[ERROR] Invalid arguments while trying to set up prompt builder: Provider not registered: google

Why

The ProviderRegistry has no built-in knowledge of provider packages they must be explicitly registered. Additionally, API keys stored in .env were never loaded into the environment, so providers could not authenticate even if registered.

Changes

Added to cli.php (mirroring tests/integration/bootstrap.php):

  1. .env loading via symfony/dotenv (already a dev dependency).
  2. Provider registration for AnthropicProvider, GoogleProvider, and OpenAiProvider, guarded with class_exists() so the script works even if only a subset of provider packages are installed.

Testing

# With a key in .env, e.g. GOOGLE_API_KEY=...
php cli.php 'Write a 2-verse poem about PHP.' --providerId=google --modelId=gemini-2.5-flash

References

Fixes : #223

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: sarthak-19 <sarthak8858@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov

codecov Bot commented Mar 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.12%. Comparing base (6317042) to head (b669b5d).

Additional details and impacted files
@@            Coverage Diff            @@
##              trunk     #224   +/-   ##
=========================================
  Coverage     88.12%   88.12%           
  Complexity     1213     1213           
=========================================
  Files            60       60           
  Lines          3934     3934           
=========================================
  Hits           3467     3467           
  Misses          467      467           
Flag Coverage Δ
unit 88.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@felixarntz felixarntz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarthak-19 Thank you for the PR!

The .env integration looks good, but the provider registration needs a cleaner approach.

Comment thread cli.php
Comment on lines +39 to +49
// Register provider packages so the registry can discover them.
$registry = AiClient::defaultRegistry();
if (class_exists(AnthropicProvider::class)) {
$registry->registerProvider(AnthropicProvider::class);
}
if (class_exists(GoogleProvider::class)) {
$registry->registerProvider(GoogleProvider::class);
}
if (class_exists(OpenAiProvider::class)) {
$registry->registerProvider(OpenAiProvider::class);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal to hard-code specific providers here. What if you want to test the CLI with other ones?

I think this should be removed. It would be better to instead support a simple system where another user-controlled file handles the registration of providers. Could look like this:

  1. check if the file (e.g. __DIR__ . '/cli-providers.php) exists
  2. if so, implement a closure here in which that file is loaded, with only access to the $registry instance so that it cannot do anything else

The cli-providers.php file would be git-ignored. It would need to look something like this (with $registry being in the local scope of where it is required):

use WordPress\AnthropicAiProvider\Provider\AnthropicProvider;
use WordPress\GoogleAiProvider\Provider\GoogleProvider;
use WordPress\OpenAiAiProvider\Provider\OpenAiProvider;

$registry->registerProvider(AnthropicProvider::class);
$registry->registerProvider(GoogleProvider::class);
$registry->registerProvider(OpenAiProvider::class);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cli.php: missing provider registration and .env loading causes "Provider not registered" error

2 participants