Fix token usage not displayed in Namer result#1225
Open
aslamdoctor wants to merge 2 commits intoWordPress:trunkfrom
Open
Fix token usage not displayed in Namer result#1225aslamdoctor wants to merge 2 commits intoWordPress:trunkfrom
aslamdoctor wants to merge 2 commits intoWordPress:trunkfrom
Conversation
…#1220) The token usage was always showing "undefined total" because generate_text_result() was never called on the AI client. The WP_AI_Client_Prompt_Builder provides its methods via PHP's __call() magic method, but the code used method_exists() which returns false for magic methods. This caused the code to always fall through to generate_text() which returns a plain string without token usage data. Changes: - Replace method_exists() with is_callable() in execute_ai_request() so generate_text_result() is properly detected and returns a GenerativeAiResult object that includes token usage. - Compute total_tokens from prompt_tokens + completion_tokens as a fallback if the AI client doesn't provide it directly. - Add defensive JS handling to prevent "undefined total" display when total_tokens is missing from the response.
|
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. |
Contributor
|
here with the token I am also see the model name. I think here should be one more section call as Model used @davidperezgar what do you think? |
Member
|
Look nice! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Fixes #1220
WP_AI_Client_Prompt_Builderprovides methods likegenerate_text_result()via PHP's__call()magic method. The code usedmethod_exists()which returnsfalsefor magic methods, sogenerate_text_result()was never called. Instead, it always fell through togenerate_text()which returns a plain string with no token usage data — resulting in "undefined total" in the UI.method_exists()withis_callable()inexecute_ai_request()so the rich result path is used and token usage is returned.total_tokensfromprompt_tokens + completion_tokensin PHP if not directly available, and add defensive JS handling to prevent "undefined total" display.Screenshot
Test plan
1,234 total (850 prompt + 384 completion))