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

@ottrix/nextjs

v0.1.0

Published

Next.js API route handlers for ottrix agents

Readme

@ottrix/nextjs

Drop an ottrix agent into any Next.js app. App Router Route Handlers, Server Actions, middleware injection guards, and Vercel AI SDK-compatible streaming.

Version: 0.1.0 · Requires: ottrix ≥2.0.0 · Node: ≥20 · License: MIT


Install

npm install @ottrix/nextjs ottrix next

next is an optional peer for middleware types — Route Handlers use standard Web Request/Response only.


Quick start (3 lines)

// app/api/chat/route.ts
import { createAgentHandlers } from '@ottrix/nextjs';
import { createAgent } from 'ottrix';

const agent = createAgent({ provider: 'anthropic', systemPrompt: 'You are helpful.' });
export const { POST, GET, OPTIONS } = createAgentHandlers({ agent });

That gives you:

  • POST /api/chat — send { message: "..." }, get JSON AgentResult
  • GET /api/chat?message=... — SSE streaming
  • OPTIONS /api/chat — CORS preflight
  • Prompt injection protection (on by default)
  • RunContext per request (Node.js runtime)

Route Handler factories

| Export | Purpose | |--------|---------| | createPostHandler(options) | JSON POST handler | | createStreamHandler(options) | SSE GET handler (?message=) | | createAgentHandlers(options) | { POST, GET, OPTIONS } bundle | | createHealthHandler({ registry? }) | Provider health check | | createChatHandler(options) | Vercel AI SDK useChat streaming POST | | createAIStreamResponse(agent, message) | Low-level data-stream Response |

Options

interface AgentHandlerOptions {
  agent: Agent;
  bodyField?: string;              // default 'message'
  injection?: 'block' | 'flag' | false;  // default 'block'
  cors?: boolean;                  // default true
  runContext?: boolean;            // default true (Node.js only)
  queryField?: string;             // default 'message'
}

Separate routes (contract-compatible layout):

// app/chat/route.ts
export const POST = createPostHandler({ agent });

// app/stream/route.ts
export const GET = createStreamHandler({ agent });

// app/health/route.ts
export const GET = createHealthHandler({ registry });

With Vercel AI SDK useChat

// app/api/chat/route.ts
import { createChatHandler } from '@ottrix/nextjs';
import { createAgent } from 'ottrix';

const agent = createAgent({ provider: 'anthropic', systemPrompt: 'You are helpful.' });
export const POST = createChatHandler({ agent });
// app/page.tsx
'use client';
import { useChat } from '@ai-sdk/react';

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat();
  return (
    <form onSubmit={handleSubmit}>
      {messages.map((m) => (
        <div key={m.id}>{m.content}</div>
      ))}
      <input value={input} onChange={handleInputChange} />
    </form>
  );
}

Uses the Vercel AI data-stream wire format — no ai package required on the server.

For full Vercel AI SDK provider integration, see @ottrix/vercel-ai.


Middleware (injection guard for all API routes)

// middleware.ts
import { createOttrixMiddleware, ottrixMatcher } from '@ottrix/nextjs';

export default createOttrixMiddleware({ injection: { mode: 'block' } });
export const config = ottrixMatcher;

Regex pattern matching only — edge-safe, no LLM calls in middleware.


Server Actions

// app/actions.ts
'use server';
import { runAgent } from '@ottrix/nextjs';
import { createAgent } from 'ottrix';

const agent = createAgent({ provider: 'anthropic' });

export async function chat(message: string) {
  return runAgent(agent, message);
}

Runtime compatibility

| Feature | Node.js Runtime | Edge Runtime | |---------|-----------------|--------------| | POST handler | ✓ | ✓ | | SSE streaming | ✓ | ✓ | | RunContext (ALS) | ✓ | ✗ (skipped) | | Injection guard | ✓ | ✓ (regex only) | | Health check | ✓ | ✓ | | Middleware injection | ✓ | ✓ |

RunContext uses AsyncLocalStorage (node:async_hooks) and is automatically skipped on Edge.


Links

MIT © ashwinpaulallen