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

@reinsjs/lowering-fetch

v0.10.0

Published

reins — zero-dependency lowering layer on fetch: events ⇄ OpenAI Chat Completions / Anthropic Messages / OpenAI Responses, hand-written SSE, no provider SDK

Readme

@reinsjs/lowering-fetch

A zero-dependency lowering layer for reins: timeline events → provider wire protocol → event drafts, using nothing but fetch and a hand-written SSE parser. No provider SDK, no node:*, the same code on Node, Workers, Deno and Bun.

Three wire protocols: OpenAI Chat Completions (the front door of the OpenAI-compatible ecosystem: DeepSeek, Qwen, vLLM, gateways), Anthropic Messages (mid-conversation system placement, signed thinking replay, explicit cache breakpoints) and OpenAI Responses (stateless store: false, encrypted reasoning replay, developer messages anywhere).

pnpm add @reinsjs/lowering-fetch
import { anthropic, anthropicMessages, deepseek, deepseekAnthropic, openai, openaiChat, openaiResponses, chatCompletions } from "@reinsjs/lowering-fetch"
import { createAgent } from "@reinsjs/agent"

const opus = anthropic("claude-opus-5", { apiKey: process.env.ANTHROPIC_API_KEY! })
const ds = deepseek("deepseek-flash", { apiKey: process.env.DEEPSEEK_API_KEY! })
const gpt = openai("gpt-5-mini", { apiKey: process.env.OPENAI_API_KEY!, requestOptions: { reasoning: { effort: "low", summary: "auto" } } })
const mini = openaiChat("gpt-4o-mini", { apiKey: process.env.OPENAI_API_KEY! })
// DeepSeek's own Anthropic-compatible endpoint (native deferred tools for lazyTools(), mid-conversation system)
const dsAnthropic = deepseekAnthropic("deepseek-flash", { apiKey: process.env.DEEPSEEK_API_KEY! })
// any other Anthropic-protocol endpoint (a relay, a gateway): declare what you have verified
const relay = anthropicMessages("some-model", {
  provider: "relay",
  baseUrl: "https://relay.example.com/anthropic",
  apiKey: process.env.RELAY_API_KEY!,
  reasoning: true,
  midConversationSystem: true,
})
const qwen = chatCompletions("qwen-max", {
  provider: "qwen",
  baseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
  apiKey: process.env.DASHSCOPE_API_KEY!,
  contextWindow: 128_000,
})

export const agent = createAgent({ model: ds, store, tools })

Each factory returns a BoundModel = { model, lowering }. apiKey is required and never read from the environment. For several models on one lowering instance, construct FetchLowering yourself and pass models.

Choosing between @reinsjs/lowering-fetch and @reinsjs/lowering-pi

Both implement the same Lowering interface from @reinsjs/core and both declare every landing in a LOSS_MATRIX; createAgent does not care which one you pass. They coexist on purpose — pick per host, not per project.

| | @reinsjs/lowering-fetch | @reinsjs/lowering-pi | | --- | --- | --- | | Protocols | OpenAI Chat Completions, Anthropic Messages, OpenAI Responses | Anthropic Messages, OpenAI Responses | | Deferred tools (Anthropic defer_loading + tool_reference, used by lazyTools()) | Yes — capabilities.deferredTools on official Anthropic models and on DeepSeek's own Anthropic-compatible endpoint | No (request shaping is pi-ai's; references are rendered as text) | | Dependencies | @reinsjs/core only (≈90 KB of ESM) | pi-ai and its ten dependencies (≈65 MB installed) | | Request body | payload.body is what is sent | pre-rewrite shape; the real body is produced in pi-ai's onPayload hook | | Mid-conversation system on Anthropic | placed by the encoder | placed by rewriting pi-ai's payload | | Unsigned thinking | dropped (declared) | may fall back to visible text | | Model table | a minimal built-in table you override per model | pi-ai's table, maintained upstream | | Runtime | verified on Cloudflare workerd at the strictest tier (2023 compatibility date, no nodejs_compat), Bun, Deno (--allow-net only) and Vercel edge-runtime, all three protocols against live providers | verified on the same four runtimes, both protocols; Deno needs --allow-sys=osRelease, an Edge runtime needs a process global for the OpenAI path |

Start with lowering-fetch for a new host: it covers everything lowering-pi covers plus the OpenAI-compatible ecosystem, ships nothing but this package, and the request you debug is the request that was sent. Stay on lowering-pi when you already run on it (there is nothing to migrate for), or when you want the provider table and new wire-protocol features to arrive through pi-ai rather than through this package. Events written by one layer replay through the other: replay.thinkingSignature uses the same shape on both, and tool call ids written by the other layer are translated on the way out (on Anthropic, ids are mapped to the provider's character set the same way pi-ai does; on Responses, pi's composite call_x|fc_y id is split back into call_id and the fc_ item id; on Chat, ids with | or over 40 characters are shortened the same way pi-ai does), so switching does not invalidate a stored timeline.

Runtime

No node:*, no process, no Buffer — the same dist/index.js runs on Node ≥ 22 and on Cloudflare Workers with a 2023 compatibility_date and no compatibility flags (spikes/edge-runtime-check, tier "最严档": module load, a byte-sliced SSE stream against a local fake endpoint, and one live request per protocol). Bun 1.4, Deno 2.9 (only --allow-net needed — the layer reads no system information) and Vercel's edge-runtime (bare vm, no process, no eval) pass the same probes, all three protocols against live providers (spikes/runtime-matrix).

Options

| option | meaning | | --- | --- | | apiKey | required; use auth: "none" when the credential lives in headers (Cloudflare AI Gateway's cf-aig-authorization) | | baseUrl | protocol root; /chat/completions (OpenAI: …/v1, DeepSeek: the bare host), /messages (Anthropic: …/v1) or /responses (OpenAI: …/v1) is appended | | contextWindow, maxOutputTokens, reasoning, images, cost, midConversationSystem | override the built-in table; unknown models start from conservative defaults (128k / 16k / no reasoning / no images) | | video, audio | whether the model takes video / audio content parts (default false everywhere, including the built-in table). See "Video and audio" below before turning them on | | chat.reasoningContent | DeepSeek dialect: replay reasoning_content (on by default in deepseek(), see below) | | chat.liftToolImages | when the model takes images, move images in tool results into a user message right after the batch of tool messages (default true); set false for an upstream that rejects that shape and images become a placeholder again | | anthropic.betas | values for the anthropic-beta header; sent only when set (mid-conversation system needs none) | | anthropic.cacheBreakpoints, anthropic.cacheTtl, anthropic.midSystemCacheBreakpoint | explicit cache breakpoints on Anthropic (default on, 5 min; costUsd prices 1-hour writes at twice the input rate, or at cost.cacheWrite1h when given, reading the provider's per-TTL split when present); what to do when a note ends the request — "automatic" (default, top-level cache_control), "previous-user", "drop" | | anthropic.deferredTools | whether ToolSpec.deferLoading becomes defer_loading: true and tool_reference content parts become native tool_reference blocks. Default: on for provider: "anthropic" and for provider: "deepseek" at DeepSeek's own Anthropic-compatible endpoint (https://api.deepseek.com…, verified 2026-11-30 with deepseek-v4-flash and deepseek-flash; other models on that endpoint are assumed to behave the same, not measured), off for any other provider on this line — including provider: "deepseek" behind a relay — until you verify it and set this flag (provider: "anthropic" is on whatever its baseUrl, as before). When off, deferred tools are simply not sent and references are rendered as text. With lazyTools() on DeepSeek this is the reason to prefer the Anthropic-compatible endpoint (deepseekAnthropic()): on the Chat line loading a tool changes tools and the cached conversation after it is recomputed (measured with deepseek-flash: 1.0k–10.1k uncached tokens on the request after a load, growing with the conversation), on the native path the tool list never changes (0.35k–1.8k) | | responses.systemRole | role for the system prompt and notes on Responses: default developer for reasoning models, system otherwise; pin it for an upstream that rejects one of them | | responses.encryptedReasoning | default on for reasoning models: include: ["reasoning.encrypted_content"] is always sent so reasoning items can be replayed under store: false; off for upstreams that reject include (then thinkingReplay reports false) | | requestOptions | spread into the body (max_tokens, temperature, thinking, OpenAI's parallel_tool_calls, Responses' reasoning / max_output_tokens / prompt_cache_key…); messages / input / tools / system / model / stream / store / previous_response_id cannot be overridden. On Anthropic max_tokens defaults to the model's maxOutputTokens and thinking is left to you (Opus 5+ defaults to adaptive server-side; Haiku 4.5 still needs budget_tokens); on Responses reasoning is likewise yours (gpt-5 defaults to medium, gpt-5.1+ to none) | | timeoutMs | local deadline, default 600 000: covers the wait for response headers and every gap between two stream frames, not the total length of the stream (like the official SDKs, a long stream that keeps producing output is never cut). A timeout is an error: retried when nothing was produced yet, not retried once part of the output has arrived. A host signal abort is aborted; cap the total time with your own signal | | trustMarkers | default on: tool output is wrapped in <untrusted source="tool:…"> in the request; the log keeps the original |

How events land on Chat Completions

Every landing is declared in LOSS_MATRIX["openai-chat"] and asserted by tests — nothing is dropped silently.

  • system_note → a mid-conversation system message (exact; verified on OpenAI and DeepSeek). Set midConversationSystem: false for an upstream that rejects it and the note goes out as a framed user message (lossy). A note marked directive: true (an instruction the model must act on, such as compact's request) always goes out as the framed user message, on all three protocols.
  • A user message or note that arrives while tool results are still pending is moved after them on the wire (tool messages must directly follow the assistant that called them); the user message is recorded lossy(user), the log order is untouched.
  • Thinking: Chat Completions has no replay slot (no signature, no encrypted item). On the official API thinking events are dropped. With chat.reasoningContent (DeepSeek) they are replayed as reasoning_content — and DeepSeek requires the field on every historical assistant message once tools is present (400 otherwise), so the layer always sends it, empty when there is nothing to replay.
  • One turn with several text blocks is merged into one content string (lossy(merged-text)); tool messages carry text only: when the model takes images, each image leaves a pointer ([image n attached in the next user message]) and the images go into one user message right after the whole batch of tool messages — before any user message that was moved after the batch — with a line per result naming the tool, the call id and the image count and saying it is tool output, not the user; untrusted results keep their <untrusted> marking (lossy(tool+lifted-images); verified on DeepSeek's official API — check other OpenAI-compatible upstreams before relying on it, and turn it off with chat.liftToolImages: false if they reject the shape). Without image support, or with chat.liftToolImages: false, images become a placeholder (lossy(tool-text-only)); isError has no flag on this protocol and is expressed as a [tool error] prefix (lossy).
  • No explicit cache breakpoints exist on this protocol; provider-side automatic prefix caching applies and cached_tokens is reported as cacheRead.
  • Usage follows core semantics: input is the uncached prompt count. costUsd uses the built-in price table (DeepSeek: peak rates, an upper bound).

How events land on Anthropic Messages

Declared in LOSS_MATRIX["anthropic-messages"], cell for cell comparable with @reinsjs/lowering-pi.

  • system_note → a mid-conversation { role: "system" } message on model families that accept it (Fable 5.x / Mythos 5.x / Opus 5 / Opus 5.5 / Opus 4.8 / Sonnet 5.5 / Haiku 5.5, or midConversationSystem: true). The provider requires such a message to follow a user turn and be followed by an assistant turn or end the request, so notes are held and emitted right before the next assistant turn (or at the end); when that slot follows an assistant turn — or the note would be the first message — the note goes out as a framed user text (lossy(user-role)). Other models always get the framed text.

  • Tool results and anything moved after them (a user message that arrived while results were pending) share one user message, so tool_result blocks directly follow the tool_use turn. is_error is carried natively.

  • Thinking is replayed only when the event carries a signature from the same provider, API and model (thinking / redacted_thinking blocks); unsigned thinking (an interrupted stream), thinking from another provider, or thinking from another model of the same provider is dropped and declared — it is not turned into visible text (a signature is only valid for the model that produced it: replaying one across models is a 400). Empty-text thinking blocks with a signature (display: "omitted") are kept and replayed.

  • Cache breakpoints: one on the last system block, one on the last tool, one on the last block of the last user message. When a note is the last message, the conversation breakpoint becomes a top-level cache_control (measured on par with no injection). If a slot is left, a fourth goes where the previous request ended (the last block of the user message before the latest assistant turn), so the history still reads from cache after a turn that adds more than the provider's ~20-block lookback (ten or more parallel tool calls). Never more than four in total.

  • Usage: input_tokens is already the uncached count; cache_read_input_tokens / cache_creation_input_tokens map to cacheRead / cacheWrite. stop_reason: "refusal" becomes a non-retryable error carrying stop_details.

  • DeepSeek's Anthropic-compatible endpoint (deepseekAnthropic(id, { apiKey }), always https://api.deepseek.com/anthropic with provider: "deepseek" — the two things the deferred-tools default looks at). The built-in table has an anthropic-messages entry for deepseek-flash, deepseek-v4-flash and deepseek-v4-pro with the same window, output limit and prices as their Chat entries. Measured on 2026-11-30 (spikes/deepseek-anthropic-port): mid-conversation system is delivered (so midConversationSystem: true), signed thinking is replayed with tools present, deepseek-flash / deepseek-v4-flash read images and deepseek-v4-pro does not (images: false, see below). The endpoint does not verify thinking signatures and accepts a history without the thinking blocks, so a conversation started on the Chat line continues here. Unknown ids start as "reasoning, images, mid-conversation system" (the same leaning as deepseek()). For a relay in front of DeepSeek use anthropicMessages(); passing baseUrl to deepseekAnthropic() throws. anthropicMessages("deepseek-flash", { provider: "deepseek", baseUrl }) now picks up the same entry instead of the conservative defaults; a bare { provider: "deepseek", id } on a FetchLowering you construct yourself still resolves to the Chat entry.

  • Deferred tools (ToolSpec.deferLoading, set by lazyTools() when capabilities.deferredTools is true): defer_loading: true on the tool, the cache breakpoint moves to the last non-deferred tool (a deferred tool cannot carry cache_control), and if every tool would be deferred none is (the API rejects that). A tool_result whose content is only tool_reference parts, comes from a system-trusted result, and names tools present in this request's tools is sent as tool_reference blocks the API expands in place — the tool list never changes, so the cache prefix survives (exact / tool-reference); text parts in the same result are moved after the batch of tool_result blocks (lossy / tool-reference, they cannot be mixed). References to tools not in this request, or inside untrusted results, are rendered as text instead — the API validates every reference in the whole history and answers 400 for an unknown name.

How events land on OpenAI Responses

Declared in LOSS_MATRIX["openai-responses"], cell for cell comparable with @reinsjs/lowering-pi.

  • Stateless by construction. Every request carries the whole history in input; store: false is forced and a previous_response_id in requestOptions is dropped — the timeline is the only source of truth, OpenAI's server-side conversation state is never relied on.
  • The system prompt and system_note are developer messages (reasoning models) or system messages (others) at any position in input — no placement rules, always exact. compaction is a framed user text (lossy).
  • Reasoning: reasoning models always request include: ["reasoning.encrypted_content"]; each reasoning item is stored whole in the event's replay.thinkingSignature and replayed verbatim (the provider verifies the encrypted payload — a forged one is a 400). Items without encrypted_content (an interrupted stream, include refused), from another provider, or from another model are dropped and declared, never turned into text. Reasoning items with an encrypted payload but no summary are kept as empty-text thinking events so the next turn can replay them.
  • Assistant text becomes a type: "message" item per text block, replaying the provider's msg_ id (replay.textSignature, lowering-pi's JSON form is accepted too) or a generated one. Tool calls are function_call items: call_id is the event's toolCallId, the fc_ item id lives in replay.itemId and is replayed only for the same model (OpenAI validates the pairing of fc_ and rs_ items); arguments is a JSON string, so non-object arguments need no wrapping. Tools are sent with strict: false.
  • Tool results are function_call_output items matched by call_id: a string when text-only, an array of input_text / input_image blocks when the result has images and the model accepts them; isError has no flag on this protocol and is expressed as a [tool error] prefix (lossy).
  • Usage: input_tokens includes cached and cache-write tokens, so input is input_tokens − cached_tokens − cache_write_tokens, with the two mapped to cacheRead / cacheWrite. status: "incomplete" with max_output_tokens is length; with content_filter a non-retryable error; response.failed and error frames are errors carrying the provider's code and message. Automatic prefix caching is best-effort on OpenAI's side; pass prompt_cache_key in requestOptions to improve hit rates.
  • Built-in table: the gpt-5 family (5.5 / 5.4 / 5.4-mini / 5.2 / 5.1 / 5 / 5-mini / 5-nano), o3 / o4-mini and gpt-4.1 / 4.1-mini / 4o-mini on Responses; the same OpenAI ids also exist as Chat Completions entries, which openaiChat() picks by protocol. A bare { provider: "openai", id } on a FetchLowering you construct yourself resolves to Responses.

Video and audio

video and audio content parts follow one rule on all three protocols: when the model declares video: true / audio: true they travel in that protocol's image slot with their own mime type (Chat image_url data URI, Responses input_image, Anthropic image block — cache breakpoints still apply); otherwise they become a placeholder such as [video omitted: this model does not accept video (video/mp4, 72 s)] and the event is lossy. On Chat, video and audio in tool results are moved after the batch like images, labelled by kind (video 2:) with an "N attachment(s)" header; a result with images only is lowered exactly as before.

  • This is a relay dialect. The image-slot shape is what OpenAI-compatible relays in front of Gemini accept; the official OpenAI, Anthropic and DeepSeek endpoints reject it. Only declare video / audio for an endpoint you have verified (the landing note says "relay dialect"), and re-check when the relay changes (spikes/media-parts/).
  • Never input_audio. The layer deliberately does not use it: the relay we measured answers 200 to input_audio but silently drops the audio, and the model then makes up what it "heard" — worse than an error.
  • deepseek-v4-pro takes text only (its /models entry lists no image input; images were silently dropped on both the Chat line and the Anthropic-compatible endpoint), so both built-in entries say images: false and images become placeholders; pass images: true only if that changes.

Errors and retries

Non-2xx responses throw HttpError with status, headers and the raw body ("<status> <body>", the same shape the official SDKs use), so runLoop's transient-failure rule applies unchanged: 408 / 409 / 429 / 5xx retry, everything else does not. A 2xx response whose content-type is JSON (a gateway reporting an error with status 200) throws the same way and is not retried. An error frame inside a Chat Completions stream ends the turn with "<code>: <message>", as the Anthropic and Responses lines already did. Network failures propagate from fetch untouched.

Documentation

docs/archive/implementation-plans/2026-09-08-技术方案v1.0.md §11, docs/系统全景图_模块盘点/lowering-fetch.md, spikes/f1-chat-live/, spikes/f2-anthropic-live/, spikes/f3-responses-live/ (live verification against DeepSeek and, through the Cloudflare AI Gateway, OpenAI and Anthropic) and spikes/edge-runtime-check/ (Cloudflare workerd) — in Chinese, at the repository root.

MIT © 2026 NewRate Limited.