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

@ainative/ai-kit-svelte

v0.2.0

Published

AI Kit - Svelte stores and actions for building AI-powered applications

Readme

@ainative/ai-kit-svelte

Svelte adapter for AI Kit with reactive stores for AI streaming.

Installation

pnpm add @ainative/ai-kit-svelte

Usage

Basic Example

<script lang="ts">
  import { createAIStream } from '@ainative/ai-kit-svelte'

  const aiStream = createAIStream({
    endpoint: '/api/chat',
    model: 'gpt-4',
    systemPrompt: 'You are a helpful assistant.'
  })

  // Subscribe to reactive stores
  $: messages = $aiStream.messages
  $: isStreaming = $aiStream.isStreaming
  $: error = $aiStream.error

  let userInput = ''

  async function handleSend() {
    if (!userInput.trim()) return
    await aiStream.send(userInput)
    userInput = ''
  }

  // Clean up when component is destroyed
  import { onDestroy } from 'svelte'
  onDestroy(() => {
    aiStream.destroy()
  })
</script>

<div class="chat-container">
  {#each $messages as message (message.id)}
    <div class="message {message.role}">
      <strong>{message.role}:</strong>
      <p>{message.content}</p>
    </div>
  {/each}
</div>

<form on:submit|preventDefault={handleSend}>
  <input
    type="text"
    bind:value={userInput}
    disabled={$isStreaming}
    placeholder="Type your message..."
  />
  <button type="submit" disabled={$isStreaming}>
    {$isStreaming ? 'Sending...' : 'Send'}
  </button>
</form>

{#if $error}
  <div class="error">Error: {$error.message}</div>
{/if}

Advanced Example with Callbacks

<script lang="ts">
  import { createAIStream } from '@ainative/ai-kit-svelte'

  const aiStream = createAIStream({
    endpoint: '/api/chat',
    model: 'gpt-4',
    onToken: (token) => {
      console.log('Received token:', token)
    },
    onCost: (usage) => {
      console.log('Token usage:', usage)
    },
    onError: (error) => {
      console.error('Stream error:', error)
    },
    retry: {
      maxRetries: 3,
      backoff: 'exponential',
      initialDelay: 1000,
      maxDelay: 10000
    }
  })

  // Usage statistics
  $: usage = $aiStream.usage
  $: console.log('Total tokens:', $usage.totalTokens)

  function handleRetry() {
    aiStream.retry()
  }

  function handleReset() {
    aiStream.reset()
  }

  function handleStop() {
    aiStream.stop()
  }
</script>

<!-- UI implementation -->

API Reference

createAIStream(config: StreamConfig): AIStreamStore

Creates a new AI streaming store.

Parameters

  • config.endpoint (string, required): The API endpoint for streaming
  • config.model (string, optional): The AI model to use
  • config.systemPrompt (string, optional): System prompt for the AI
  • config.onToken (function, optional): Callback fired for each token
  • config.onCost (function, optional): Callback fired when usage stats update
  • config.onError (function, optional): Callback fired on errors
  • config.retry (object, optional): Retry configuration
    • maxRetries (number): Maximum number of retries
    • backoff ('linear' | 'exponential'): Backoff strategy
    • initialDelay (number): Initial retry delay in ms
    • maxDelay (number): Maximum retry delay in ms
  • config.headers (object, optional): Additional HTTP headers

Returns

An AIStreamStore object with the following properties:

Stores (Readable)
  • messages: Readable store containing the message history
  • isStreaming: Readable store indicating if currently streaming
  • error: Readable store containing any errors
  • usage: Readable store with token usage statistics
Methods
  • send(content: string): Promise<void>: Send a user message
  • reset(): void: Clear all messages and reset state
  • retry(): Promise<void>: Retry the last message
  • stop(): void: Stop the current stream
  • destroy(): void: Clean up resources and event listeners

Types

interface Message {
  id: string
  role: 'user' | 'assistant' | 'system'
  content: string
  timestamp: number
}

interface Usage {
  promptTokens: number
  completionTokens: number
  totalTokens: number
  estimatedCost?: number
  latency?: number
  model?: string
  cacheHit?: boolean
}

Features

  • ✅ Reactive Svelte stores for all state
  • ✅ Automatic message history management
  • ✅ Server-sent events (SSE) streaming
  • ✅ Automatic retry with exponential backoff
  • ✅ Token usage tracking
  • ✅ Error handling
  • ✅ TypeScript support
  • ✅ Framework-agnostic core

Testing

# Run tests
pnpm test

# Run tests with coverage
pnpm test:coverage

# Type check
pnpm type-check

License

MIT