trailblazers: Fix AI API key prompt when Writer/Rewriter polyfill falls back to native Prompt API - #1049
Merged
tomayac merged 1 commit intoAug 4, 2026
Conversation
…ls back to native Prompt API Same fix as GoogleChrome/starter-extended-blog#18: isNative was computed via apiClass.toString().includes('[native code]'), which is always false for the built-in-ai-task-apis-polyfills fallback since it installs a plain JS class. This wrongly triggered the "enter your AI API key" prompt even when the polyfill runs entirely on-device through the browser's native Prompt API (LanguageModel). Replace the toString() sniff with isNativeAIFeature(), which checks the polyfill's __isPolyfill marker and, when polyfilled, whether the underlying LanguageModel is itself native rather than also polyfilled. dist/js/ai/*.js is mirrored by hand from public/js/ai/*.js rather than via `npm run build`: eleventy passthrough-copies these files verbatim (no transform), and the pre-fix files were confirmed byte-identical between public/ and dist/, so the manual mirror matches what a full build would produce. A full `npm install` was skipped since it pulls in heavy transitive ML runtime deps (transformers.js, onnxruntime-web, webllm) unrelated to this fix.
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
trailblazers/.built-in-ai-task-apis-polyfillsfallback was itself running entirely on-device via the browser's native Prompt API (LanguageModel, supported from Chrome 148).isNativewas computed asapiClass.toString().includes('[native code]'). The polyfill installs a plain JS class (class extends apiClass {}), so this check is alwaysfalsefor the polyfill path — even when it needs no cloud key at all.toString()sniff with a sharedisNativeAIFeature()helper (ai-ui-utils.js) that checks the polyfill's__isPolyfillmarker and, when polyfilled, whether the underlyingLanguageModelis itself native rather than also polyfilled.dist/js/ai/*.jsis updated to mirrorpublic/js/ai/*.js(eleventy passthrough-copies these files verbatim, no transform — verified byte-identical pre-fix). A fullnpm install/npm run buildwas skipped for this change since it would pull in large transitive ML runtime deps (transformers.js, onnxruntime-web, webllm) unrelated to the fix; please regeneratedist/via the normal build/deploy flow if preferred before merging.Test plan
node --checkon all modified files