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

@simulacra-ai/stream-server

v0.0.13

Published

Server-side encoder for streaming Simulacra Conversation events over HTTP using the @simulacra-ai/core wire format

Readme

@simulacra-ai/stream-server

Server-side encoder that exposes a @simulacra-ai/core Conversation as a typed event stream, plus thin transport adapters for NDJSON, SSE, and WebSockets. Pair with @simulacra-ai/stream-client on the consumer side.

Install

npm install @simulacra-ai/stream-server @simulacra-ai/core

Encode a conversation

encode_conversation returns a ReadableStream<WireEvent> — typed events, no byte encoding. Pipe through a transport adapter (or iterate directly).

import { encode_conversation, to_ndjson_stream } from "@simulacra-ai/stream-server";

app.post("/api/chat", async (c) => {
  const conversation = build_conversation_for(c.req);
  const abort = new AbortController();
  c.req.raw.signal.addEventListener("abort", () => abort.abort(), { once: true });

  const events = encode_conversation(conversation, {
    abort_signal: abort.signal,   // cancels conversation on client disconnect
    timeout_ms: 60_000,            // emits lifecycle_error and closes on expiry
  });

  // Kick off the model turn. Errors surface through the stream as
  // `request_error` events, so swallowing the unhandled-rejection here is safe.
  void conversation.prompt(await c.req.text()).catch(() => {});

  const { body, headers } = to_ndjson_stream(events);
  return new Response(body, { headers });
});

The encoder closes the stream automatically on message_complete, on request_error, on timeout, on abort, or when the conversation is disposed. lifecycle_error is non-fatal and does not close the stream.

SSE

import { encode_conversation, to_sse_stream } from "@simulacra-ai/stream-server";

const { body, headers } = to_sse_stream(encode_conversation(conversation));
return new Response(body, { headers });

Each event is framed as event: <type>\ndata: <json>\n\n.

WebSocket

No adapter needed — iterate and send each event:

for await (const event of encode_conversation(conversation)) {
  ws.send(JSON.stringify(event));
}

Node / Fastify integration

For Fastify, Express, or raw http.createServer use the /node sub-export to pump the framed body into a ServerResponse:

import { encode_conversation, to_ndjson_stream } from "@simulacra-ai/stream-server";
import { pipe_to_node_response } from "@simulacra-ai/stream-server/node";

reply.hijack();
const abort = new AbortController();
reply.raw.on("close", () => abort.abort());

const { body, headers } = to_ndjson_stream(
  encode_conversation(conversation, { abort_signal: abort.signal }),
);
for (const [k, v] of Object.entries(headers)) reply.raw.setHeader(k, v);
reply.raw.statusCode = 200;
await pipe_to_node_response(body, reply.raw, { abort_signal: abort.signal });

pipe_to_node_response handles backpressure (drain waits), client-close detection, and res.end() when the stream finishes.

API

encode_conversation(conversation, options?) → ReadableStream<WireEvent>

Options:

  • events — subset of event types to forward. Default forwards:
    • message_start
    • content_start
    • content_update
    • content_complete
    • message_complete
    • request_error
    • lifecycle_error
  • include_prompt_send (default false) — forward prompt_send events. SECURITY: echoes the user's prompt to every consumer.
  • include_error_stack (default false) — include error.stack on serialized errors.
  • transform(event) — return undefined to drop the event, or a replacement event of the same type. Intended for redaction.
  • abort_signal — when aborted, calls conversation.cancel_response() and closes the stream.
  • timeout_ms — emits lifecycle_error with operation: "encoder_timeout" on expiry, then cancels the conversation and closes.

to_ndjson_stream(events) → { body, headers }

Frames a ReadableStream<WireEvent> as NDJSON bytes (one JSON per line). Headers set Content-Type: application/x-ndjson plus anti-buffering hints.

to_sse_stream(events) → { body, headers }

Frames as SSE (event: <type>\ndata: <json>\n\n). Headers set Content-Type: text/event-stream.

pipe_to_node_response(stream, res, options?) (sub-export /node)

Pipes a ReadableStream<Uint8Array> into a Node ServerResponse with backpressure handling and abort propagation.