Skip to content

Conversation

@simandebvu
Copy link
Owner

@simandebvu simandebvu commented Oct 13, 2025

Implement Core Utilities for Chrome's Built-in AI APIs

📋 Summary

Complete implementation of TypeScript wrappers for Chrome's three built-in AI APIs (Rewriter, Translator, Prompt/Language Model) with comprehensive error handling, browser compatibility detection, and interactive demo pages.

Closes: Backlog card - "Implement core utilities for Chrome's Built-in AI APIs with proper error handling"


✅ Acceptance Criteria Met

  • All three APIs have utility wrappers - Complete implementations with high-level service classes and low-level wrappers
  • Browser compatibility is checked on load - Full detection system with compatibility banner
  • Clear error messages for unavailable APIs - Custom error classes with 25+ error codes
  • API availability detection - Runtime capability detection for all APIs
  • Graceful fallback messaging - Browser compatibility checks and user-friendly messages
  • Loading states and progress indicators - Real-time download progress visualization

🎯 Tasks Completed

1. ✅ Create Chrome Rewriter API Wrapper

Implementation:

  • Low-level wrappers: createRewriter(), rewriteText(), rewriteTextStreaming()
  • RewriterService class with reading level presets
  • Content chunking for long text
  • Retry logic with exponential backoff
  • Input validation and sanitization
  • Enhanced: Download progress monitoring

Files: rewriter.ts, services/RewriterService.ts, test/RewriterTest.tsx

2. ✅ Create Chrome Prompt API Wrapper

Implementation:

  • Low-level wrappers: createSession(), sendPrompt(), sendPromptStreaming()
  • PromptService class with conversation management
  • System prompts and context handling
  • Temperature and Top-K configuration
  • Multimodal support (text, images, audio)
  • Session cloning and history tracking
  • Download progress monitoring

Files: prompt.ts, services/PromptService.ts, test/PromptTest.tsx

3. ✅ Create Chrome Translation API Wrapper

Implementation:

  • Low-level wrappers: createTranslator(), translateText(), detectLanguage()
  • TranslatorService class with auto-detection
  • Language detection with confidence scores
  • Support for multiple language pairs (BCP 47)
  • Streaming translation
  • Auto-detect and translate workflow
  • Download progress monitoring

Files: translator.ts, services/TranslatorService.ts, test/TranslatorTest.tsx

4. ✅ Implement API Availability Detection

Implementation:

  • capabilities.ts - Runtime availability checking for all APIs
  • ChromeAiDiagnostics component - Visual diagnostics dashboard
  • Checks three states: unavailable, available, downloadable
  • Model availability status display
  • Environment validation

Files: capabilities.ts, test/ChromeAiDiagnostics.tsx

5. ✅ Add Graceful Fallback Messaging

Implementation:

  • browserCompatibility.ts - Browser detection and version checking
  • BrowserCompatibilityBanner component - Requirements display
  • Chrome version validation (137+ required)
  • Flag configuration guidance
  • Clear setup instructions
  • User-friendly error messages

Files: browserCompatibility.ts, common/BrowserCompatibilityBanner.tsx

6. ✅ Add Loading States and Progress Indicators

Implementation:

  • ModelDownloadProgress component - Download visualization
  • Real-time progress bars with percentages
  • Byte count display (e.g., "1.5 MB / 2.3 MB")
  • Status messages: Checking → Downloading → Ready
  • Monitor callbacks integrated into all services
  • 8 UI components: LoadingSpinner, ProgressBar, Skeleton variants

Files: ui/ModelDownloadProgress.tsx, ui/LoadingSpinner.tsx, ui/ProgressBar.tsx, ui/Skeleton.tsx


🏗️ Architecture

Three-Layer Design

┌─────────────────────────────────┐
│ Service Layer (Business Logic) │
│ - RewriterService               │
│ - TranslatorService             │
│ - PromptService                 │
└─────────────────────────────────┘
              ↓
┌─────────────────────────────────┐
│ Wrapper Layer (API Interaction) │
│ - rewriter.ts                   │
│ - translator.ts                 │
│ - prompt.ts                     │
└─────────────────────────────────┘
              ↓
┌─────────────────────────────────┐
│ Chrome AI Native APIs           │
│ - self.Rewriter                 │
│ - self.Translator               │
│ - self.LanguageModel            │
└─────────────────────────────────┘

📊 Implementation Stats

  • 36 files changed
  • 6,287 lines added
  • 3 complete API wrappers
  • 3 service classes (high-level)
  • 3 error classes (25 error codes)
  • 8 UI components
  • 3 interactive test pages
  • 1 central demos landing page

🎨 Key Features

Error Handling

  • RewriterError - 8 error codes (NOT_AVAILABLE, NOT_SUPPORTED, etc.)
  • TranslatorError - 9 error codes (UNSUPPORTED_LANGUAGE, etc.)
  • PromptError - 8 error codes (PROMPT_FAILED, RESPONSE_ERROR, etc.)
  • Type guards for error checking
  • Automatic retry for retryable errors
  • Detailed error messages with context

Browser Compatibility

  • Runtime browser detection
  • Chrome version validation (137+ required)
  • Flag configuration checklist
  • Compatibility banner with setup instructions
  • Model download status checking

Progress Tracking

  • Visual progress bars during model downloads
  • Percentage indicators (0-100%)
  • Byte count display with human-readable format
  • Status messages throughout lifecycle
  • Monitor callbacks in all service classes

Type Safety

  • Full TypeScript implementation
  • Comprehensive type definitions
  • Type inference for all operations
  • No any types used
  • Proper error type guards

🧪 Testing

Build Status

✅ TypeScript compilation successful
✅ ESLint: 0 errors, 0 warnings
✅ Vite build successful
✅ Bundle: 371 kB (109 kB gzipped)

Manual Testing

✅ All three APIs tested in Chrome Canary
✅ Browser compatibility detection working
✅ Download progress visualization verified
✅ Error handling validated
✅ Fallback messages displayed correctly
✅ Loading states functioning properly


💡 Usage Examples

Rewriter API

import { RewriterService } from '@/lib/chrome-ai';

const service = new RewriterService({
  language: 'en',
  monitor: (progress) => {
    console.log(`Downloading: ${progress.loaded}/${progress.total} bytes`);
  }
});

await service.initialize();
const simplified = await service.simplify(text, {
  readingLevel: 'elementary'
});
service.destroy();

Translator API

import { TranslatorService } from '@/lib/chrome-ai';

const service = new TranslatorService({
  minConfidence: 0.5,
  monitor: (progress) => showProgress(progress)
});

// Auto-detect and translate
const result = await service.detectAndTranslate('Hello world', 'es');
console.log(result); // { detectedLanguage: 'en', translatedText: 'Hola mundo' }

Prompt API

import { PromptService } from '@/lib/chrome-ai';

const service = new PromptService({
  temperature: 0.7,
  initialPrompts: [{ role: 'system', content: 'You are helpful.' }],
  monitor: (progress) => showProgress(progress)
});

await service.initialize();
const response = await service.prompt('What is TypeScript?');

🎮 Demo Pages

Central Landing Page (/demos)

  • Overview of all three APIs
  • Status badges (Available/Coming Soon/Planned)
  • Feature lists and documentation links
  • Requirements banner

Interactive Test Pages

  1. Rewriter Demo (/demos/rewriter) - Text simplification with reading levels
  2. Translator Demo (/demos/translator) - Translation with language detection
  3. Prompt Demo (/demos/prompt) - Conversational AI interface

All demos include:

  • Live diagnostics dashboard
  • Browser compatibility checks
  • Download progress visualization
  • Event logging
  • Error handling demonstrations

📂 Files Added/Modified

New Core Files

src/lib/chrome-ai/
├── services/
│   ├── PromptService.ts      [NEW] 401 lines
│   ├── TranslatorService.ts  [NEW] 465 lines
│   └── RewriterService.ts    [ENHANCED]
├── errors/
│   ├── PromptError.js        [NEW]
│   ├── TranslatorError.js    [NEW]
│   └── errorMessages.ts      [NEW] 183 lines
├── prompt.ts                 [NEW] 323 lines
├── translator.ts             [NEW] 441 lines
├── browserCompatibility.ts   [NEW] 239 lines
├── capabilities.ts           [NEW] 134 lines
└── types.ts                  [ENHANCED +408 lines]

New UI Components

src/components/ui/
├── ModelDownloadProgress.tsx [NEW] 161 lines
├── LoadingSpinner.tsx        [NEW] 149 lines
├── ProgressBar.tsx           [NEW] 345 lines
└── Skeleton.tsx              [NEW] 212 lines

Test Infrastructure

src/components/test/
├── PromptTest.tsx            [NEW] 512 lines
├── TranslatorTest.tsx        [NEW] 542 lines
├── RewriterTest.tsx          [ENHANCED]
└── ChromeAiDiagnostics.tsx   [ENHANCED]

src/pages/
└── DemosPage.tsx             [NEW] 279 lines

🚀 Technical Highlights

Performance

  • Automatic content chunking for long text
  • Streaming support for all APIs
  • Retry with exponential backoff
  • Efficient resource management

Reliability

  • Comprehensive error handling
  • Browser compatibility validation
  • Graceful degradation
  • Clear user feedback

Developer Experience

  • Full TypeScript support
  • Interactive test pages
  • Live diagnostics dashboard
  • Extensive inline documentation

User Experience

  • Download progress visualization
  • Real-time status updates
  • Clear error messages
  • Responsive UI components
  • Accessible design (ARIA labels)

🔗 Related

Branch: feat/2-apis-integration
Base: main

Chrome Requirements:

  • Chrome Canary/Dev 137+
  • Flags: optimization-guide-on-device-model, prompt-api-for-gemini-nano
  • Model download may be required

✅ Checklist

  • All three API wrappers implemented
  • Browser compatibility detection added
  • Clear error messages for unavailable APIs
  • API availability detection functional
  • Graceful fallback messaging implemented
  • Loading states and progress indicators working
  • TypeScript compilation successful
  • ESLint passing (0 errors, 0 warnings)
  • Build successful
  • Manual testing completed
  • Demo pages functional
  • Documentation inline with code

Ready for review! 🎉

This PR delivers a production-ready, type-safe implementation of Chrome's Built-in AI APIs with comprehensive error handling, browser compatibility detection, and excellent developer experience.

Demo

@simandebvu simandebvu requested a review from seshxn October 13, 2025 14:00
@vercel
Copy link

vercel bot commented Oct 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
synapse Ready Ready Preview Comment Oct 14, 2025 3:33pm

Resolved conflicts by keeping both Workspace and Demos navigation items, and all page imports (TranslatorTestPage, PromptTestPage, and WorkspacePage).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…examples

- Revised application overview to include interactive demos and practical implementations of Chrome's AI APIs.
- Added detailed features section outlining integrations with Rewriter, Translator, and Prompt APIs.
- Introduced new UI components and developer experience enhancements.
- Included API usage examples for Rewriter, Translator, and Prompt services.
- Updated browser requirements and next steps for future development.
@simandebvu simandebvu self-assigned this Oct 13, 2025
@simandebvu simandebvu marked this pull request as draft October 13, 2025 14:28
@simandebvu simandebvu marked this pull request as ready for review October 13, 2025 14:48
@seshxn
Copy link
Collaborator

seshxn commented Oct 13, 2025

@simandebvu Awesome stuff!!! I am having some issues with the setup locally, but it should be resolved soon 👍

Are you also able to update the .env.example with the new environment variable placeholders?

- Added default language support ('en') in TextInputPanel and rewriter options.
- Updated ChromeAiDiagnostics to reflect prompt API availability status.
- Improved download progress handling in PromptTest, RewriterTest, and TranslatorTest to manage undefined values.
- Enhanced TranslatorTest with lazy initialization notes and improved error handling for service initialization.
- Updated translator API to require source and target language parameters for availability checks.
- Refined documentation for global AI API capabilities and added logging for debugging purposes.
…arget language parameters

- Modified the availability check for the Translator API to include specific source ('en') and target ('es') language parameters, enhancing accuracy in detecting service availability.
- Introduced a new env.example file containing placeholders for API keys, facilitating easier setup for development and testing environments.
@seshxn seshxn merged commit 395fc1f into main Oct 14, 2025
3 of 4 checks passed
@simandebvu simandebvu deleted the feat/2-apis-integration branch November 1, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants