npm package: @rejax/browser-ai
Description: A toolkit of browser built-in AI API, including Prompt API, Translation API, Write API, Summarize API, etc.
Author: Jax
Latest version: 0.0.22

# @rejax/browser-ai

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

## Installation

```bash
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:

```typescript
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:

```typescript
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:

```typescript
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:

```typescript
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:

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

setOriginTrialToken('your-token-here')
```

## API Types

### Language Detection

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

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

### Translation

```typescript
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