Make HTTP transporter setup in registry more flexible to not throw discovery exception when no HTTP request is even being attempted#164
Conversation
…scovery exception when no HTTP request is even being attempted.
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @dgrieser. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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. |
|
Really interesting, @felixarntz! If I'm following, this comes down to the fact that the WP AI Client doesn't connect the I mean, really, the discovery system can be hooked up as soon as possible. It doesn't require that anything in WordPress itself is loaded at that time. I could see providers wanting to be registered (and even usable) prior to As a note, while the WP AI Client does load in If I'm tracking with all that, the goal of this PR is to at least make it so that when a provider is registered prior to this point it still works. This done is by suppressing the My only concern with this is the remaining race condition of someone trying to use Am I tracking with all this? |
|
@JasonTheAdams I think you're right on all accounts. But I'd like us to look at this from a less WordPress-centric angle too. While we can shuffle things around in the WP AI Client, and maybe set up the HTTP client very early in the WordPress bootstrap cycle, I think there's still no point in throwing an HTTP discovery exception when a provider is registered. So this PR is mostly about being more "forgiving", or less aggressive. An HTTP client and HTTP transporter only need to be set up once any AI HTTP request is made (e.g. via So to your last point, I'm not sure I follow what your concern is with |
|
Got it. I'm tracking with what you mean. The think the nested I think if we drop the use of that trait and make it a nullable property, then we can clean that up. The only other place the getter is used is in |
|
Hmm, but why not use the trait? Can you clarify what you think is the problem with it? |
The problem is that the trait assumes the transporter is non-nullable, which is why we're having to catch the exception for the getter. In the case of the Make sense? |
I'm not sure I agree. Why is catching an exception a bad thing to do here? The trait still takes away responsibility of having the property, setter, and getter. It's all super basic functionality, but I'd argue the trait simply reduces the amount of code we have to (re-)write. |
JasonTheAdams
left a comment
There was a problem hiding this comment.
@felixarntz I think we just disagree, then. Hahah! But it's not a hill I'd die on.
Personally, I think the trait is not matching the actual intentions of the class — namely, that a nullable property is valid. I don't even think we need the getter, either, because it's only used internally. So I think we're applying something to reduce code that isn't actually a proper fit, it just feels close enough. The exception catching becomes a code smell, not that catching exceptions is bad in any way.
I'm approving because I've made my point and am fine either way. 😄
This partially will fix WordPress/wp-ai-client#41 - specifically, when a provider is registered before an HTTP client has been configured in the PSR-17 discovery mechanism.
This is probably not sufficient by itself as a proper fix for that issue, but it's already a partial fix, which means a workaround will no longer be needed. And since this library is not meant to be only used with WordPress, it makes sense to optimize for general flexibility:
This PR fixes that, by dynamically attempting to set the default HTTP transporter when no (other) HTTP transporter has already been set, and doing that in a way that is forgiving in terms of the discovery exception.
cc @martin-helmich @LukasFritzeDev