npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

built-in-ai-task-apis-polyfills

v1.8.0

Published

Polyfills for the Built-in AI Task APIs (Summarizer, Writer, Rewriter, etc.)

Downloads

824

Readme

Built-in AI Task APIs Polyfills

This package provides browser polyfills for the Built-in AI Task APIs, specifically:

  • Summarizer API
  • Writer API
  • Rewriter API
  • Language Detector API
  • Translator API
  • Classifier API

These polyfills are backed by the prompt-api-polyfill, which is automatically loaded if window.LanguageModel is not detected. This means they support the same dynamic backends.

When loaded in the browser, they define globals:

window.Summarizer;
window.Writer;
window.Rewriter;
window.LanguageDetector;
window.Translator;
window.Classifier;

so you can use these Task APIs even in environments where they are not yet natively available.

Installation

Install from npm:

npm install built-in-ai-task-apis-polyfills

Quick start

Recommended Loading Strategy

To ensure your app uses the native implementation when available, use a defensive dynamic import strategy:

<script type="module">
  import config from './.env.json' with { type: 'json' };

  // Example: Use Gemini backend
  window.GEMINI_CONFIG = config;

  // Load polyfills only if not natively supported
  const polyfills = [];
  if (!('Summarizer' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/summarizer'));
  }
  if (!('Writer' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/writer'));
  }
  if (!('Rewriter' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/rewriter'));
  }
  if (!('LanguageDetector' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/language-detector'));
  }
  if (!('Translator' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/translator'));
  }
  if (!('Classifier' in window)) {
    polyfills.push(import('built-in-ai-task-apis-polyfills/classifier'));
  }
  await Promise.all(polyfills);

  // Now you can use the APIs
  if ((await Summarizer.availability()) === 'available') {
    const summarizer = await Summarizer.create();
    const summary = await summarizer.summarize('Long text to summarize...');
    console.log(summary);
  }
</script>

API Usage Examples

Summarizer API

const summarizer = await Summarizer.create({
  type: 'key-points',
  format: 'markdown',
  length: 'short',
});

const result = await summarizer.summarize(text);
// or streaming
const stream = summarizer.summarizeStreaming(text);
for await (const chunk of stream) {
  console.log(chunk);
}

Writer API

const writer = await Writer.create({
  tone: 'professional',
  format: 'plain-text',
});

const result = await writer.write('Draft of an email to my boss');

Rewriter API

const rewriter = await Rewriter.create({
  tone: 'casual',
});

const result = await rewriter.rewrite('I am writing to inform you that...');

Language Detector API

const detector = await LanguageDetector.create();
const results = await detector.detect('C'est la vie');

for (const { detectedLanguage, confidence } of results) {
  console.log(`${detectedLanguage} (${(confidence * 100).toFixed(1)}%)`);
}

Translator API

const translator = await Translator.create({
  sourceLanguage: 'en',
  targetLanguage: 'fr',
});

const result = await translator.translate('Hello world');

Classifier API

const classifier = await Classifier.create();
const results = await classifier.classify('A story about a cat');

for (const { id, confidence } of results) {
  console.log(`${id} (${(confidence * 100).toFixed(1)}%)`);
}

Configuration

Configuring .env.json

This repo ships with a dot_env.json template. Copy it to .env.json and fill in your credentials:

cp dot_env.json .env.json

The polyfill will look for these configurations on the window object. Adjust your loading logic to pass the JSON content to the appropriate global (e.g., window.GEMINI_CONFIG).


API surface

Once the polyfills are loaded, you can use them as described in the official documentation:

For complete examples, see:


Running the demos locally

  1. Install dependencies:
    npm install
  2. Copy and fill in your config:
    cp dot_env.json .env.json
  3. Start the server:
    npm start

Testing

The project includes a comprehensive test suite based on Web Platform Tests (WPT).

npm run test:wpt

License

Apache 2.0