Fix AI credentials detection#537
Conversation
…le, PHP constant and database value This fixes the invalid error on the AI settings page. Before this only the database option value was checked when evaluating if an API key is set. This introduces the helper `has_api_key_configured` that also checks for env value or PHP constant. If one of the values is set, the helper returns true.
This fixes the state display o a connector in the dashboard widget if the API key is configured using a env or a constant by using the new helper function `has_api_key_configured`
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #537 +/- ##
=============================================
+ Coverage 73.06% 73.16% +0.09%
+ Complexity 1731 1730 -1
=============================================
Files 85 85
Lines 7466 7467 +1
=============================================
+ Hits 5455 5463 +8
+ Misses 2011 2004 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I’m not sure how to test this in the e2e tests. Sure, we could add a test spec for this, but this would require to set the config value in .wp-env.test.json and this would break other tests. Should set up an additional env with the value and run a seperate test script for this? Do you have any other ideas? |
I don't think we need explicit e2e testing for this, since the outcome - behavior when there are/arent creds - hasn't changed. What's changing here is the internal handling for where credentials can be sourced from, so PHPUnit (Integration) testing should be enough IMO. |
|
Why don't you check directly on the AI provider instance whether it's configured? The connectors for AI providers gets registered automatically based on the info stored in AI provider registry. In fact, the names for the env variable and PHP constant are standardized there. |
I agree with @justlevine here that we don't really need an E2E test for this. That said, we do load a test plugin in our test environment and so you could use that to help with this. As an example of something I've done before, you could add something like this in the // Set the OpenAI API key as a constant when override param is set.
add_action(
'init',
static function () {
if (
isset( $_GET['ai_api_key_override'] ) &&
! defined( 'OPENAI_API_KEY' )
) {
define( 'OPENAI_API_KEY', 'valid-api-key' );
}
},
1
);And then in your E2E test, do the following:
Could then replicate this for the dashboard widget |
@gziolo Not sure I completely follow? Is this around the question about E2E tests? Or a suggestion on how the code in this PR could be better? Need to do a full review but in looking quickly, does look like it pulls the env and constant name from the Connector Registry. As far as I know though, that registry doesn't tell us if a Connector is actually configured. Let me know if that's not correct though. EDIT: Reading again, I think what you're referring to is the AI Client Registry, not the Connectors Registry. In that case I do think we can use that instead though it's not super different from the approach taken here |
|
@dkotter, I'm talking about production code and the fact that AI providers need to be configured before they get discovered by the connectors registry. Even when the AI provider uses db setting, it's the connector that sets everything on the AI provider instance. See: |
I think we're saying the same things, apologize for the back and forth (see my comment here on recommended changes that I think matches what you're saying). If we just use |
|
As suggested, I’ve refactored the function When it comes to testing:
I’ve not yet added any tests, because I’m still not sure how to add those.
If one of these strategies should be implemented, we would need to mock the |
|
@LukasFritzeDev Thanks for this PR! I've added some basic unit tests and things look good to me! |
Fixed - Utilize a new `is_connector_configured` function to properly determine if a connector is configured, whether via an API key, constant or ENV var Co-authored-by: LukasFritzeDev <lukasfritzedev@git.wordpress.org> Co-authored-by: dkotter <dkotter@git.wordpress.org> Co-authored-by: justlevine <justlevine@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
What?
Closes #536
Why?
if the provider API key is set up using constant in
wp-config.phpor an ENV (instead of providing the key via the connectors settings page)How?
has_api_key_configuredthat checks environment variable, PHP constant and database option.has_ai_credentialsandAI_Status_Widgetto get the correct state informationUse of AI Tools
AI assistance: Yes
Tool(s): IDE Code completion
Model(s): Devstral-Small-2-24B-Instruct-2512
Used for: IDE Code completion
Testing Instructions
Screenshots or screencast
Changelog Entry
Fixed connector status in settings page an dashboard widget if key is configured using env or constant