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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rejax/browser-ai

v0.0.22

Published

A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.

Downloads

72

Readme

@rejax/browser-ai

A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.

Installation

npm install @rejax/browser-ai

Features

  • Language Detection
  • Translation
  • Prompt/Chat Capabilities
  • Text Summarization
  • Origin Trial Token Setting

Language Detection

The library provides automatic language detection capabilities:

import { detect, checkDetectorUsability } from '@rejax/browser-ai'

// Check if language detection is available
const usability = await checkDetectorUsability()

if (usability.available) {
  // Detect language of text
  const result = await detect('Hello world')
  console.log(result)
  // Output: { text: 'en', value: 'en' }
}

The detector will use either the Chrome AI API (ai.languageDetector) or Translation API (translation) depending on availability.

Translation

The translation module allows translating text between languages:

import { translate, checkTranslatorUsability, updateTranslator } from '@rejax/browser-ai'

// Check translation availability for language pair
const usability = await checkTranslatorUsability({
  sourceLanguage: 'en',
  targetLanguage: 'es'
})

if (usability.available) {
  // Translate text
  const translated = await translate({
    text: 'Hello world',
    sourceLanguage: 'en',
    targetLanguage: 'es'
  })
  console.log(translated) // "Hola mundo"
}

// Update translator for different languages
await updateTranslator({
  sourceLanguage: 'en',
  targetLanguage: 'fr'
})

Prompt/Chat Capabilities

The library provides access to language model and assistant capabilities:

import { promptStreaming, checkPromptUsability } from '@rejax/browser-ai'

// Check if prompt capabilities are available
const usability = await checkPromptUsability()

if (usability.available) {
  // Stream responses from the model
  const stream = await promptStreaming('Tell me a joke')
  // Handle stream response...
}

Text Summarization

Provides text summarization capabilities:

import { summarize, summarizeStreaming, checkSummarizerUsability } from '@rejax/browser-ai'

// Check if summarization is available
const usability = await checkSummarizerUsability()

if (usability.available) {
  // Get a summary
  const summary = await summarize('Long text to summarize...')
  
  // Or use streaming for real-time results
  const stream = await summarizeStreaming('Long text to summarize...')
}

Origin Trial Token

Set an Origin Trial token for API access:

import { setOriginTrialToken } from '@rejax/browser-ai'

setOriginTrialToken('your-token-here')

API Types

Language Detection

interface DetectionResult {
  text: string  // Detected language code
  value: string // Same as text
}

interface UsabilityResult {
  available: boolean
  apiPath: string[]
  createFuncName: string
}

Translation

interface TranslationParams {
  sourceLanguage: string
  targetLanguage: string
}

interface TranslationRequest extends TranslationParams {
  text: string
}

interface TranslationResult {
  available: boolean
  apiPath: string | string[]
  createFuncName: string
  canFuncName?: string
  msg?: string
}

Browser Compatibility

This library requires a browser that supports one or more of:

  • Chrome AI API (window.ai)
    • Language Model/Assistant API
    • Language Detector API
    • Summarizer API
  • Translation API (window.translation)

Currently Chrome/Chromium browsers version 120+ are supported.

Error Handling

The library will throw errors if:

  • Browser APIs are not available
  • Requested language pair is not supported
  • Translation fails
  • Session initialization fails
  • Summarization is not supported

Be sure to wrap API calls in try/catch blocks for proper error handling.

License

MIT