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

@pedrofariasx/toolstream

v0.3.0

Published

Incremental streaming parser for LLM tool calls — framework-agnostic, event-driven, multi-provider

Readme

ToolStream

Parser incremental de tool calls para LLMs em streaming. Inspirado em llhttp e tree-sitter, o ToolStream é uma biblioteca framework-agnostic para parsear tool calls de LLMs em tempo real, com suporte a múltiplos provedores e execução antecipada.

Features

  • Streaming first: Aceita chunks arbitrários via parser.push(chunk) e constrói tool calls incrementalmente
  • Multi-provedor: OpenAI, Anthropic, Gemini, DeepSeek, Qwen, XML e formatos customizados
  • Partial JSON: Tolerância a JSON parcial/incompleto com reparo inteligente
  • Event-driven: Emite eventos durante toda a construção dos argumentos
  • Early execution: Permite executar ferramentas antes da resposta completa quando a confiança é suficiente
  • Error recovery: Estratégias de recuperação para chunks malformados
  • Framework-agnostic: Compatível com Node.js, Bun, Deno, Browser e Cloudflare Workers
  • Zero dependências: Apenas TypeScript

Instalação

npm install toolstream

Uso Básico

import { ToolStream } from 'toolstream'

const parser = new ToolStream({ provider: 'openai' })

parser.on('tool-call:complete', (toolCall) => {
  console.log(`Tool: ${toolCall.name}`, toolCall.arguments)
})

// Streaming chunks da OpenAI
for await (const chunk of stream) {
  parser.push(chunk)
}

parser.finalize()

Arquitetura

Tokenizar → State Machine → Parser → Recovery → Eventos
    ↓                            ↓
  Chunks                    Provider Adapters
                               ↓
                          OpenAI · Anthropic · Gemini · DeepSeek · Qwen · XML
  • StreamDecoder: Gerencia o buffer de entrada e quebra em tokens
  • StateMachine: DFA que rastreia o estado do parsing
  • JsonStreamParser: Parseia JSON incrementalmente
  • RecoveryEngine: Estratégias de recuperação de erros
  • ConfidenceScorer: Avalia confiança para early execution

Provedores

// Auto-detecção (detecta automaticamente o formato)
const parser = new ToolStream({ autoDetectProvider: true })

// Provedor explícito
const parser = new ToolStream({ provider: 'anthropic' })
const parser = new ToolStream({ provider: 'gemini' })
const parser = new ToolStream({ provider: 'deepseek' })
const parser = new ToolStream({ provider: 'qwen' })
const parser = new ToolStream({ provider: 'xml' })

// Adapter customizado
parser.setCustomAdapter({
  name: 'custom',
  processChunk: function* (chunk) { /* ... */ },
  detect: (chunk) => chunk.includes('my-format'),
})

Eventos

| Evento | Descrição | |--------|-----------| | tool-call:start | Nova tool call detectada | | tool-call:id | ID da tool call identificado | | tool-call:name | Nome da função identificado | | tool-call:arguments:delta | Delta dos argumentos parciais | | tool-call:arguments:key | Chave de argumento parseada | | tool-call:arguments:value | Valor de argumento parseado | | tool-call:arguments:partial | Objeto parcial dos argumentos | | tool-call:early-execute | Pronto para execução antecipada | | tool-call:complete | Tool call completamente parseada | | tool-call:error | Erro durante parsing |

Early Execution

const parser = new ToolStream({
  provider: 'openai',
  earlyExecutionThreshold: 0.85 // confiança mínima
})

parser.on('tool-call:early-execute', (toolCall) => {
  // Executa a ferramenta com argumentos parciais
  executeTool(toolCall.name, toolCall.partialArguments)
})

Documentação

A documentação completa da API está em docs/api.md.

Benchmarks

npm run bench     # Executa suite de benchmarks

Testes

npm test          # Executa testes (vitest)
npm run test:watch  # Modo watch
npm run test:coverage # Cobertura

Build

npm run build     # Gera dist/ com ESM, CJS e DTS

CI

O projeto usa GitHub Actions para:

  • Type checking (tsc --noEmit)
  • Testes unitários (Node 18, 20, 22)
  • Build (ESM + CJS + DTS)
  • Benchmarks automáticos no main

Licença

MIT