Fix token usage not displayed in Namer result#1225
Fix token usage not displayed in Namer result#1225ernilambar merged 3 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. |
|
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? |
|
Look nice! |
|
@raftaar1191 should we show model name only when the "AI Model" dropdown is set to "Automatic" or always? |
|
@aslamdoctor I think always will be the best options. @davidperezgar Any comment of your on this? |
|
@aslamdoctor that would be another PR. LGTM |


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))