@anvia/server

中文文档
1.4.0发布于 2026/11/06MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1976

Server-side client protocol and generic event stream responses for Anvia.

原始使用文档
文档目录5 节
  1. Install
  2. Quickstart
  3. Capabilities
  4. Learn more
  5. Durable execution routes

@anvia/server

Serve Anvia completions and Agent runs over HTTP. These framework-neutral response helpers turn async events into streaming Response objects for Anvia clients, with JSONL, SSE, and replay support.

Install

pnpm add @anvia/server @anvia/client @anvia/core @anvia/openai

The quickstart uses OpenAI; choose another provider adapter if needed.

Quickstart

Set OPENAI_API_KEY on your server and mount this handler in a framework that accepts Web Request and Response objects:

import { completionToClientStream, parseClientStreamRequest } from "@anvia/client";
import { streamCompletion } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { createClientStreamResponse } from "@anvia/server";

const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) throw new Error("Set OPENAI_API_KEY.");
const model = new OpenAIClient({ apiKey }).completionModel({
  modelId: "gpt-5",
  api: "responses",
});

export async function POST(request: Request): Promise<Response> {
  const body = parseClientStreamRequest(await request.json());
  if (body.type !== "messages") {
    return new Response("This endpoint accepts messages only.", { status: 400 });
  }
  const events = completionToClientStream({
    events: streamCompletion({ model, messages: body.messages, abortSignal: request.signal }),
  });
  return createClientStreamResponse({ events });
}

Connect with createHttpClientTransport from @anvia/client, or use it with the @anvia/react hooks. Add format: "sse" to the response options for SSE instead of the default JSONL.

Capabilities

  • Framed client responses with protocol headers and ordered event IDs.
  • Resumable streams backed by a store you provide, with an in-memory store for local use. createMemoryResumableStreamStore({ closedStreamTtlMs, maxClosedStreams }) defaults to retaining closed streams for 10 minutes, up to 1000 closed streams.
  • Separate generic event responses for application-defined protocols.
  • Lower-level JSONL and SSE stream helpers.

Your application owns routing, authentication, request error handling, and durable storage.

Both resume response helpers require after to be a nonnegative safe integer; omitted values default to zero. Invalid values return HTTP 400 JSON with error code invalid_resumable_after. The lower-level resumeStreamEvents() throws InvalidResumableAfterError before yielding.

Learn more

Durable execution routes

The optional @anvia/server/durable entrypoint exports createDurableHandler for @anvia/durable runtimes. It provides submission, session-scoped run listing, snapshots, SSE events with persisted IDs, queued steering, approval responses, retry, cancellation, and reconciliation. POST /durable/runs/:id/steer accepts { input: { prompt } } or { input: { messages } }, plus an optional requestId, and returns a persisted receipt with HTTP 202. It requires authorization action steer and the matching durable release (0.8 or newer within the supported peer range). An authorize(request, resource) callback is required for all reads and writes. The host owns runtime startup (resume) and shutdown (close). Client disconnects detach observers. Task submission requires @anvia/durable 0.6 or newer so authorization receives each effective agent ID, including goal defaults and saved bindings. Older runtimes return HTTP 503 for task submission; existing run routes remain available. No work is created when agent access is denied, and a changed binding during authorization returns HTTP 409 for the caller to retry. Clients cannot send trace. An optional trace(request, resource) callback sets observer trace context, such as the authenticated user ID, for authorized run and task submissions. It requires @anvia/durable 0.11 or newer.

Install @anvia/durable when using this subpath. See the durable HTTP guide.

Generic SseStreamOptions also supports eventId(event) for native SSE IDs and onCancel() to interrupt a pending producer read before its iterator is returned.

Durable graph routes under /durable/graphs expose submission, paginated discovery, topology snapshots, task SSE events, and cancellation. Graph-wide operations authorize every task; individual child-run routes authorize against the graph's owning session. See the task graph guide.

版本与发布记录

@anvia/server 共发布 84 个版本,首次发布于 2026/06/02;过去 12 个月发布了 84 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.0latest2026/11/061 天前
1.3.62026/11/043 天前
1.3.52026/10/2018 天前
1.3.42026/10/1325 天前
1.3.32026/10/1325 天前
1.3.22026/10/1127 天前

其他发布通道:preview → 0.4.3-preview.20260628T094557.2ab9488,rc → 1.0.0-rc.11。

关于 @anvia/server 的常见问题

@anvia/server 是什么?

Server-side client protocol and generic event stream responses for Anvia.

如何安装 @anvia/server?

在项目目录运行 npm install @anvia/server,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@anvia/client(^1.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@anvia/server 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anvia/server 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@anvia/server 可以商用吗?

@anvia/server 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anvia/server 的下载量有多少?

根据 npm 官方下载统计,@anvia/server 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,976 次,日均约 66 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anvia/server 还在维护吗?

最新版本 1.4.0 发布于 2026/11/06(1 天前);至今共发布 84 个版本,其中过去 12 个月发布了 84 个;首次发布于 2026/06/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。