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

chat-nest-server

v1.1.2

Published

Streaming AI backend server with cost controls, rate limiting, and cancellation support.

Readme

chat-nest-server

Streaming AI backend server for Chat Nest with built-in cost protection and cancellation propagation using Server-Side Events (SSE).

This package exposes an Express-compatible request handler that:

  • Streams AI responses using Server-Side Events (SSE)
  • Sends real-time tokens via SSE protocol
  • Enforces rate limits and profile-based limits
  • Supports abort propagation
  • Protects against runaway usage

✨ Features

  • Server-Side Events (SSE) streaming over HTTP
  • Real-time token streaming via SSE protocol
  • SSE event types: start, token, done, error, ping
  • Heartbeat pings to keep connection alive
  • End-to-end cancellation support
  • Daily token limit enforcement (profile-based)
  • Rate limiting
  • Message trimming
  • Safe retry semantics
  • OpenAI adapter included

📦 Installation

npm install chat-nest-server

🚀 Usage

Express Integration

The handler automatically uses Server-Side Events (SSE) for streaming responses:

import express from "express";
import cors from "cors";
import { createChatHandler } from "chat-nest-server";

const app = express();

app.use(cors());
app.use(express.json());

app.post(
  "/api/chat",
  createChatHandler({
    apiKey: process.env.OPENAI_API_KEY!,
  })
);

app.listen(3001, () => {
  console.log("API running on http://localhost:3001");
});

The handler sends SSE-formatted events:

  • event: start\ndata: \n\n - Stream started
  • event: token\ndata: <token>\n\n - Each token chunk
  • event: done\ndata: \n\n - Stream completed
  • event: error\ndata: <error_json>\n\n - Error occurred
  • event: ping\ndata: \n\n - Heartbeat (every 15s)

🔐 Environment Variables

| Variable | Description | | -------------- | -------------- | | OPENAI_API_KEY | OpenAI API key |

💰 Cost Controls

Profiles (constrained, balanced, expanded) control limits. HARD_CAPS clamp all profiles. Server enforces:

  • Maximum tokens per request – Profile-based; client can override via maxTokensPerRequest (server applies min(profile limit, client value))
  • Daily token limit – Profile-based; client can override via dailyTokenLimit (same min rule)
  • Request rate limiting (profile)
  • Prompt size trimming (profile)
  • Retry classification

Request body options

| Field | Type | Description | | ------------------- | ------ | --------------------------------------------------------------------------- | | messages | array | Chat messages (required) | | profile / aiUsageProfile | string | Profile: constrained, balanced, expanded (legacy: budget, moderate, free) | | dailyTokenLimit | number | Optional. Cap daily tokens; server uses min(profile limit, this) | | maxTokensPerRequest | number | Optional. Cap tokens per request; server uses min(profile limit, this) |

This prevents accidental overspending and abuse.

🔄 Server-Side Events (SSE)

This package uses SSE protocol for efficient streaming:

  • Content-Type: text/event-stream
  • Connection: keep-alive
  • Cache-Control: no-cache
  • Heartbeat: Ping every 15 seconds to keep connection alive
  • Event Format: event: <type>\ndata: <data>\n\n

SSE provides better efficiency and real-time streaming compared to traditional polling or chunked responses.


⚙ Configuration

  • config/profiles.ts – AI usage profiles (constrained, balanced, expanded), HARD_CAPS, and resolveProfile(). Frontend may send aiUsageProfile string; backend resolves safely.
  • config/aiLimits.tsAI_MODEL only. All limits come from profiles.

📄 License

ISC