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

chrome-web-ai-types

v0.1.2

Published

TypeScript definitions for Chrome Web AI APIs

Readme

chrome-web-ai-types

TypeScript definitions for Chrome’s built-in AI APIs (Summarizer, Writer, Rewriter, Proofreader, Translator, Language Detector, and Prompt API). Works with TypeScript, React, Angular, Vue, and any project using TypeScript.

Installation

npm install chrome-web-ai-types
# or
yarn add chrome-web-ai-types
# or
pnpm add chrome-web-ai-types

Reference the types in your project

For the global types (Summarizer, Writer, Translator, etc.) to be recognized, add this at the top of a TypeScript file that uses the APIs (or in a global .d.ts in your project):

/// <reference types="chrome-web-ai-types" />

Example: in a React component, Angular service, or Vue component that calls the APIs, put that line before any other code. Alternatively, add it once in a file like src/global.d.ts or types/global.d.ts so all files in the project get the types.

Requirements

  • TypeScript 4.5+ (5.x recommended)
  • Chrome 138+ (APIs are evolving; see Chrome docs)
  • Some APIs may require an origin trial or Chrome flags

APIs covered

| API | Description | | ------------------------------ | --------------------------------------------------- | | Summarizer | Summarize text (key-points, tldr, teaser, headline) | | Writer | Generate text (tone, format, length) | | Rewriter | Rewrite text (tone, format, length) | | Proofreader | Grammar and spelling review | | Translator | Translate between languages | | LanguageDetector | Detect text language | | LanguageModel (Prompt API) | Prompts and sessions with Gemini Nano | | window.ai | createLanguageModel() (legacy) |

All expose availability(), create(options?), streaming where applicable, and language options when documented.


Usage by project type

TypeScript (Node / generic bundler)

The declarations are global. After installing the package, types are picked up automatically.

// No import needed
async function main() {
  if (typeof Summarizer === "undefined") {
    console.log("Summarizer not available in this environment.");
    return;
  }

  const availability = await Summarizer.availability();
  if (availability === "unavailable") return;

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

  const summary = await summarizer.summarize("Your long text here...");
  console.log(summary);
}

Ensure your tsconfig.json includes the right lib (usually automatic after install):

{
  "compilerOptions": {
    "lib": ["ES2020", "DOM"]
  }
}

React (TypeScript)

Install the package and use the APIs in components or hooks. Global types are available without imports.

// src/hooks/useSummarizer.ts
/// <reference types="chrome-web-ai-types" />
import { useState, useCallback } from "react";

export function useSummarizer() {
  const [summary, setSummary] = useState<string>("");
  const [loading, setLoading] = useState(false);

  const summarize = useCallback(async (text: string) => {
    if (typeof Summarizer === "undefined") {
      throw new Error("Summarizer API not available.");
    }
    setLoading(true);
    try {
      const availability = await Summarizer.availability();
      if (availability === "unavailable") {
        throw new Error("Model unavailable.");
      }
      const summarizer = await Summarizer.create({ type: "key-points" });
      const result = await summarizer.summarize(text);
      setSummary(result);
      return result;
    } finally {
      setLoading(false);
    }
  }, []);

  return { summary, loading, summarize };
}
// src/components/SummarizerDemo.tsx
import { useSummarizer } from "../hooks/useSummarizer";

export function SummarizerDemo() {
  const { summary, loading, summarize } = useSummarizer();

  return (
    <div>
      <button
        disabled={loading}
        onClick={() => summarize("Long text to summarize...")}
      >
        {loading ? "Summarizing..." : "Summarize"}
      </button>
      {summary && <p>{summary}</p>}
    </div>
  );
}

In React + Vite or Create React App, a tsconfig with "lib": ["ES2020", "DOM"] is enough.

--

Install and use in <script setup lang="ts"> or composables; global types are recognized.

<!-- src/components/LanguageDetector.vue -->
<script setup lang="ts">
import { ref } from "vue";

const text = ref("");
const detectedLanguage = ref<string | null>(null);
const loading = ref(false);

async function detect() {
  if (typeof LanguageDetector === "undefined") return;
  loading.value = true;
  try {
    const detector = await LanguageDetector.create();
    const results = await detector.detect(text.value);
    detectedLanguage.value = results[0]?.detectedLanguage ?? null;
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div>
    <textarea v-model="text" placeholder="Enter some text" />
    <button :disabled="loading" @click="detect">Detect language</button>
    <p v-if="detectedLanguage">Language: {{ detectedLanguage }}</p>
  </div>
</template>

Checking availability

APIs may be missing (older browser) or not ready (model not downloaded). Always check at runtime:

if (typeof Summarizer === "undefined") {
  // API does not exist in this environment
  return;
}

const availability = await Summarizer.availability();
// "available" | "unavailable" | "downloadable"

if (availability === "unavailable") {
  // Cannot use
  return;
}

if (availability === "downloadable") {
  // Can call create(); model will download (may take a while)
  const summarizer = await Summarizer.create({
    monitor(m) {
      m.addEventListener("downloadprogress", (e) => {
        console.log(`Download: ${e.loaded * 100}%`);
      });
    },
  });
  // ...
}

Type tests (this repo)

npm install
npm run typecheck
# or
npm test

Files under tests/ assert that the definitions accept valid usage and reject invalid usage.


Official documentation


License

MIT