Skip to content

trailblazers: Fix AI API key prompt when Writer/Rewriter polyfill falls back to native Prompt API - #1049

Merged
tomayac merged 1 commit into
gh-pagesfrom
fix-trailblazers-ai-native-detection-polyfill-fallback
Aug 4, 2026
Merged

trailblazers: Fix AI API key prompt when Writer/Rewriter polyfill falls back to native Prompt API#1049
tomayac merged 1 commit into
gh-pagesfrom
fix-trailblazers-ai-native-detection-polyfill-fallback

Conversation

@tomayac

@tomayac tomayac commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

  • Same bug and fix as Fix AI API key prompt when Writer/Rewriter polyfill falls back to native Prompt API starter-extended-blog#18, applied to trailblazers/.
  • The editor's AI features asked for a cloud AI API key even when the built-in-ai-task-apis-polyfills fallback was itself running entirely on-device via the browser's native Prompt API (LanguageModel, supported from Chrome 148).
  • Root cause: isNative was computed as apiClass.toString().includes('[native code]'). The polyfill installs a plain JS class (class extends apiClass {}), so this check is always false for the polyfill path — even when it needs no cloud key at all.
  • Replaces the toString() sniff with a shared isNativeAIFeature() helper (ai-ui-utils.js) that checks the polyfill's __isPolyfill marker and, when polyfilled, whether the underlying LanguageModel is itself native rather than also polyfilled.
  • Applied consistently across all affected features: Writer, Rewriter, Summarizer, Classifier, tag suggestions, Translator, and multimodal image metadata generation.
  • dist/js/ai/*.js is updated to mirror public/js/ai/*.js (eleventy passthrough-copies these files verbatim, no transform — verified byte-identical pre-fix). A full npm install/npm run build was skipped for this change since it would pull in large transitive ML runtime deps (transformers.js, onnxruntime-web, webllm) unrelated to the fix; please regenerate dist/ via the normal build/deploy flow if preferred before merging.

Test plan

  • node --check on all modified files
  • Manually verify in a browser without native Writer/Rewriter but with native Prompt API support (Chrome 148+) that AI actions run without prompting for an API key

…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.
@tomayac
tomayac merged commit 99da422 into gh-pages Aug 4, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant