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

openoracle-react-sdk

v1.0.0

Published

OpenOracle React SDK - AI routing with multiple LLM providers including free options

Downloads

6

Readme

@polypoll/react-ai-router

A pure AI routing mechanism for React applications with local LLM support and strict JSON output validation. Built for the PolyPoll prediction market platform but designed as a general-purpose library.

Features

  • 🎯 Pure AI Routing: Route-based AI interactions with schema validation
  • 🦙 Local LLM Support: Run Llama-3 locally with WebLLM/WebGPU
  • 🛡️ Strict JSON Validation: Zod-based schema validation with error recovery
  • 🔄 Provider Fallbacks: Automatic failover between OpenAI, local models
  • ⚛️ React Integration: Hooks and context for seamless React integration
  • 🎛️ Configuration Presets: Production-ready configurations out of the box
  • 🔁 Retry Logic: Smart retry with exponential backoff
  • 📊 Batch Processing: Process multiple AI requests in parallel

Installation

npm install @polypoll/react-ai-router

Optional for local LLM support:

npm install @mlc-ai/web-llm

Quick Start

Basic Setup

import React from 'react';
import {
  AIRouterProvider,
  useAIRouter, 
  generateViralPollRoute,
  AI_CONFIG_PRESETS
} from '@polypoll/react-ai-router';

function App() {
  const config = AI_CONFIG_PRESETS.development('your-openai-api-key');
  
  return (
    <AIRouterProvider config={config}>
      <PollGenerator />
    </AIRouterProvider>
  );
}

function PollGenerator() {
  const { execute, isLoading, error } = useAIRouter(generateViralPollRoute);
  
  const handleGenerate = async () => {
    try {
      const poll = await execute({
        article_data: {
          url: 'https://example.com/news',
          title: 'AI Breakthrough: GPT-5 Announced',
          summary: 'OpenAI unveils next-generation model',
          news_site: 'tech-news.com',
          tags: ['ai', 'gpt5']
        },
        perspective: 'balanced',
        payment_token: 'FLOW'
      });
      
      console.log('Generated poll:', poll);
    } catch (err) {
      console.error('Generation failed:', err);
    }
  };
  
  return (
    <button onClick={handleGenerate} disabled={isLoading}>
      {isLoading ? 'Generating...' : 'Generate Poll'}
    </button>
  );
}

Local LLM Setup (Free, Private)

import { AI_CONFIG_PRESETS } from '@polypoll/react-ai-router';

// Use local Llama-3 models (no API keys needed)
const config = AI_CONFIG_PRESETS.localFirst();

function LocalApp() {
  return (
    <AIRouterProvider config={config}>
      {/* Your components */}
    </AIRouterProvider>
  );
}

Configuration

Environment-Based Configuration

import { buildConfigForEnvironment } from '@polypoll/react-ai-router';

// Development: Fast, cheap models with local fallback
const devConfig = buildConfigForEnvironment('development', {
  openai: process.env.REACT_APP_OPENAI_KEY
});

// Production: High-quality models with redundancy  
const prodConfig = buildConfigForEnvironment('production', {
  openai: process.env.REACT_APP_OPENAI_KEY
});

Custom Configuration

import { createOpenAIConfig, createWebLLMConfig } from '@polypoll/react-ai-router';

const customConfig = {
  defaultProvider: createOpenAIConfig('your-api-key', 'gpt-4'),
  fallbackProviders: [
    createWebLLMConfig('Llama-3.2-3B-Instruct-q4f32_1'),
    createWebLLMConfig('Llama-3.2-1B-Instruct-q4f32_1') // Fastest fallback
  ],
  enableRetries: true,
  maxRetries: 2,
  timeout: 30000,
  validateOutput: true
};

AI Routes

Built-in Routes

import { 
  generateViralPollRoute,      // Generate viral prediction markets
  generateTwitterPollRoute,    // Binary polls from tweets
  analyzeArticleContextRoute   // Article analysis
} from '@polypoll/react-ai-router';

Custom Routes

import { z } from 'zod';

const customRoute = {
  path: '/analysis/sentiment',
  description: 'Analyze sentiment with market predictions',
  inputSchema: z.object({
    text: z.string().min(10),
    domain: z.enum(['tech', 'finance', 'politics'])
  }),
  outputSchema: z.object({
    sentiment: z.number().min(-1).max(1),
    confidence: z.number().min(0).max(1),
    themes: z.array(z.string()),
    prediction: z.string()
  }),
  systemPrompt: 'You are a financial analyst...',
  temperature: 0.4,
  maxTokens: 800
};

// Use with any provider
const { execute } = useAIRouter(customRoute);

Advanced Usage

Batch Processing

import { useAIRouterBatch } from '@polypoll/react-ai-router';

function BatchProcessor() {
  const { executeBatch, isLoading, errors } = useAIRouterBatch();
  
  const processBatch = async () => {
    const requests = articles.map(article => ({
      route: generateViralPollRoute,
      input: { article_data: article, perspective: 'balanced' }
    }));
    
    const results = await executeBatch(requests);
    console.log('Batch results:', results);
  };
  
  return (
    <button onClick={processBatch} disabled={isLoading}>
      Process {articles.length} Articles
    </button>
  );
}

Provider Health Monitoring

import { useAIRouterContext } from '@polypoll/react-ai-router';

function ProviderStatus() {
  const { isProviderAvailable } = useAIRouterContext();
  
  return (
    <div>
      <div>OpenAI: {isProviderAvailable('openai') ? '✅' : '❌'}</div>
      <div>WebLLM: {isProviderAvailable('web-llm') ? '✅' : '❌'}</div>
    </div>
  );
}

Error Handling

const { execute, error, retry } = useAIRouter(route, {
  onError: (err) => {
    if (err.code === 'VALIDATION_ERROR') {
      console.log('Schema validation failed');
    } else if (err.code === 'RATE_LIMIT') {
      console.log('Rate limited, will auto-retry');
    }
  },
  onSuccess: (response) => {
    console.log('Success:', response);
  }
});

Local LLM Models

Supported Models

| Model | Size | Speed | Quality | RAM Required | |-------|------|-------|---------|--------------| | Llama-3.2-1B-Instruct-q4f32_1 | 1B | Fast | Good | 2GB | | Llama-3.2-3B-Instruct-q4f32_1 | 3B | Medium | Better | 4GB |
| Llama-3.1-8B-Instruct-q4f32_1 | 8B | Slow | Best | 8GB |

WebGPU Requirements

  • Chrome/Edge 113+ with WebGPU enabled
  • Dedicated GPU with 2GB+ VRAM
  • Hardware acceleration enabled

Check Support

import { isWebLLMSupported, getAvailableWebLLMModels } from '@polypoll/react-ai-router';

const supported = await isWebLLMSupported();
const models = getAvailableWebLLMModels();

JSON Validation

Automatic Validation

All AI responses are automatically validated against route schemas:

// Input validation
const input = MyInputSchema.parse(userInput);

// AI execution with output validation
const output = await execute(input); // Automatically validated

// Custom validation
import { validateJSON } from '@polypoll/react-ai-router';

const validated = validateJSON(rawResponse, MySchema, 'openai');

Error Recovery

The library includes multiple JSON parsing strategies:

  1. Direct JSON parsing
  2. Markdown code block extraction
  3. Regex-based JSON extraction
  4. Common error repair (trailing commas, quotes)
  5. Manual key-value extraction

Configuration Presets

Available Presets

// Production: OpenAI primary, WebLLM fallback
AI_CONFIG_PRESETS.production(apiKey)

// Development: Cheaper models, local fallback
AI_CONFIG_PRESETS.development(apiKey)

// Local-first: Privacy focused, no API calls
AI_CONFIG_PRESETS.localFirst()

// Cost-optimized: Minimize API costs
AI_CONFIG_PRESETS.costOptimized(apiKey)

// Performance: Best quality models
AI_CONFIG_PRESETS.performance(apiKey)

Optimal Provider Selection

import { getOptimalProvider } from '@polypoll/react-ai-router';

const provider = getOptimalProvider({
  speed: 'fast',      // fast | medium | slow
  cost: 'low',        // low | medium | high  
  quality: 'good',    // basic | good | premium
  privacy: 'local'    // cloud | local
}, { openai: apiKey });

API Reference

Hooks

  • useAIRouter(route, options?) - Execute single AI route
  • useAIRouterBatch() - Execute multiple routes in parallel
  • useAIRouterContext() - Access router context
  • useAIRouterStream(route, options?) - Streaming responses (planned)

Components

  • <AIRouterProvider config={config}> - Provide AI context
  • withAIRouter(Component, config) - HOC for AI context

Utilities

  • validateJSON(content, schema, provider) - Validate JSON responses
  • buildConfigForEnvironment(env, keys) - Environment-based config
  • isWebLLMSupported() - Check WebLLM compatibility

Examples

See the /examples directory for complete examples:

  • basic-usage.tsx - Getting started guide
  • advanced-usage.tsx - Complex scenarios and error handling
  • local-llm-demo.tsx - WebLLM integration
  • poll-generation.tsx - PolyPoll-specific examples

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

MIT License - see LICENSE file for details.

Support


Built with ❤️ for the PolyPoll prediction market ecosystem.