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

@ai-sdk-tool/middleware

v0.1.1

Published

Collection of reusable AI SDK middlewares

Downloads

161

Readme

AI SDK Middleware

npm npm

Reusable middleware utilities for the Vercel AI SDK v7 (LanguageModelV4), plus an evlog/ai LanguageModelV4 port.

Support matrix

Aligned with the official AI SDK layering and evlog AI integration:

| Layer | Spec | |-------|------| | AI SDK v6 | Primarily LanguageModelV3 / LanguageModelV3Middleware | | AI SDK v7 (ai@7) | Core is LanguageModelV4 (specificationVersion: "v4") | | This package (core middlewares) | LanguageModelV4Middleware only | | @ai-sdk-tool/middleware/evlog wrap | LanguageModelV4 + LanguageModelV4Middleware | | Upstream evlog/ai wrap | LanguageModelV3 + LanguageModelV3Middleware only | | Peer ai (optional, for ./evlog) | >=7.0.0 <8.0.0 (LanguageModelV4 wrap — not AI SDK v6) | | Peer evlog (optional, for ./evlog types) | >=2.0.0 — full RequestLogger type | | Peer @ai-sdk/provider | ^4 (LanguageModelV4 types) | | Node.js | >=22 (required for AI SDK v7) |

Meaning: this package targets AI SDK v7 / LanguageModelV4 only. Upstream evlog/ai still peers ai >= 6.0.168 and wraps V3; use that for AI SDK v6.

./evlog feature matrix

Capture token usage, tool calls, model info, and streaming metrics into wide events. Requires AI SDK v7 (ai >= 7, Node.js 22+).

For tool execution timing, abort tracking, and auto embed capture, pass createEvlogIntegration(ai) to telemetry.integrations.

| Data | Source | Description | |------|--------|-------------| | tokens, model, provider, stream metrics | middleware (wrap / createAIMiddleware) | inputTokens, outputTokens, cache/reasoning tokens, msToFirstChunk, msToFinish, tokensPerSecond, … | | ai.tools[] | onToolExecutionEnd | Per-tool name, durationMs, success, error | | ai.totalDurationMs | onStartonEnd | Wall time from generation start to completion | | ai.embedding | onEmbedEnd or captureEmbed() | Embeddings | | ai.finishReason: 'abort' | onAbort | Aborted stream | | ai.error | onAbort / onError | Abort reason or unrecoverable error |

Integration still implements the older v6 hook names (onToolCallFinish, onFinish) for source parity with upstream, but this package only supports AI SDK v7.

Installation

pnpm add @ai-sdk-tool/middleware
# for ./evlog:
pnpm add ai@^7

Exports

  • @ai-sdk-tool/middleware
  • @ai-sdk-tool/middleware/disk-cache
  • @ai-sdk-tool/middleware/reasoning-parser
  • @ai-sdk-tool/middleware/evlog — LanguageModelV4 port of evlog/ai (optional peer: ai)

Included middleware

All middlewares implement LanguageModelV4Middleware and work with wrapLanguageModel from ai@7:

  • createDiskCacheMiddleware: Disk-based response cache for generate and stream
  • defaultSystemPromptMiddleware: Inject or merge system prompts
  • extractReasoningMiddleware: Extract XML-tagged reasoning into reasoning parts
  • createAIMiddleware / createAILogger / createEvlogIntegration (./evlog): wide-event AI observability (log.set({ ai }))

evlog example (v7)

import { generateText } from "ai";
import {
  createAILogger,
  createEvlogIntegration,
} from "@ai-sdk-tool/middleware/evlog";

const ai = createAILogger(log); // full evlog RequestLogger

const result = await generateText({
  model: ai.wrap(yourLanguageModelV4), // or gateway model id string
  prompt: "hello",
  telemetry: {
    integrations: [createEvlogIntegration(ai)],
  },
});

Core middleware example

import { wrapLanguageModel } from "ai";
import { createDiskCacheMiddleware } from "@ai-sdk-tool/middleware/disk-cache";
import { defaultSystemPromptMiddleware } from "@ai-sdk-tool/middleware";

const model = wrapLanguageModel({
  model: yourLanguageModelV4,
  middleware: [
    defaultSystemPromptMiddleware({ systemPrompt: "You are helpful." }),
    createDiskCacheMiddleware({ cacheDir: ".ai-cache" }),
  ],
});

Development

pnpm install
pnpm run typecheck
pnpm run test
pnpm run build

Release flow

  • Add a patch/minor/major changeset in .changeset/*.md
  • Merge changes to main via a pull request, then let Release Changeset open/update the version PR
  • Merging the version PR publishes the package to npm automatically