@anvia/client

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

Framework-neutral client protocol, transports, and UI message state for Anvia.

原始使用文档
文档目录7 节
  1. Install
  2. Quickstart
  3. Capabilities
  4. Learn more
  5. Durable HTTP client
  6. Chat UI transport
  7. Restarted generations

@anvia/client

Connect any frontend to Anvia with a framework-neutral streaming protocol. This package provides HTTP and direct transports, runtime validation, and UI message state for text, tools, and interactions.

Install

pnpm add @anvia/client @anvia/core

Quickstart

Consume a chat endpoint that returns an Anvia client stream, such as one created with @anvia/server:

import {
  applyClientStreamEvent,
  createHttpClientTransport,
  messagesToUIMessages,
  type UIMessage,
} from "@anvia/client";
import type { Message } from "@anvia/core/completion";

const transport = createHttpClientTransport({ endpoint: "/api/chat" });
const messages: Message[] = [{ role: "user", content: "Hello!" }];
let uiMessages: readonly UIMessage[] = messagesToUIMessages(messages);

for await (const frame of transport.send({ request: { type: "messages", messages } })) {
  if (frame.type === "stream_event") {
    uiMessages = applyClientStreamEvent(uiMessages, frame.event);
    console.log(uiMessages);
  }
}

The transport validates framing, event order, and stream identity. Send Core Message[] to the server; keep UIMessage[] as presentation state in your application.

Capabilities

  • HTTP transport for JSONL or SSE, plus direct transport for in-process integrations.
  • Adapters for native completion and Agent events at the server boundary.
  • Validated requests, events, frames, and persisted UI messages.
  • Message conversion and reduction, including streamed tool arguments and generation usage.
  • Typed custom stream data, Agent interactions, and HTTP resume cursors.

For React, @anvia/react manages this state through useChat and useCompletion. Generic JSONL/SSE readers are also available from @anvia/client/transport for other event contracts. Malformed frames are skipped and reported through onParseError (or thrown with strict: true), and readSseFrames exposes SSE event names, last event IDs, and retry hints.

Learn more

Durable HTTP client

The optional @anvia/client/durable entrypoint exports DurableClient and DurableHttpError. Install @anvia/durable to use it. It supports submit, list, snapshot, SSE observation, queued steering, approval responses, retry, cancellation, and tool reconciliation. Use client.steer(runId, { prompt: "Focus on recovery" }, { requestId: "correction-123" }) to receive a persisted receipt. Reuse the optional request ID to deduplicate delivery; observe steering_applied to confirm the input entered the conversation. Steering requires the matching durable release (0.8 or newer within the supported peer range).

Construct it with an absolute endpoint, optional fetch, credentials, and static or per-request headers. Restore UI state from snapshot(runId), then observe stream(runId, { after: snapshot.cursor }). Incoming records and event ordering are validated. Reconnection is explicit; aborting observation does not cancel execution. These committed progress events are separate from token-delta chat streams.

See the durable HTTP guide.

Chat UI transport

The optional @anvia/client/durable-chat entrypoint exports createDurableChatTransport, which adapts a durable endpoint to the ClientTransport contract used by useChat from @anvia/react and the @anvia/react-ui primitives. Create one transport per chat session; the agent, session and request identity live there, not in an individual message request.

const transport = createDurableChatTransport({ client, agentId, sessionId });
const chat = useChat({ transport });

The adapter submits the last user message with enqueue: true by default, maps durable journal events to anvia.client.v3 frames, routes interaction responses to the run that produced them, and uses the durable run id and sequence as { streamId, after } for resume. Aborting the subscription detaches only; use transport.controller.cancel(), retry(), resolveTool() or reattach(runId, after) for explicit run control. A busy session is reported as DurableBusyError with the run to reattach to. Client history is discarded in favour of the durable transcript, so regenerate() and reset() are approximate; thread lists are not covered.

DurableClient also exposes submitGraph, listGraphs, graphSnapshot, streamGraph, and cancelGraph. Graph snapshots contain renderable nodes/edges and explicit waiting reasons. Use each node's runId with the existing response/retry/reconciliation methods. Graph streams carry graphId and taskId; refresh the snapshot to obtain the latest graph-wide state.

Restarted generations

A repeated generation_start for an unfinished turn emits message_reset before new content. The reducer clears that message's parts, metadata, and generation state while preserving its ID and role. Completed turns and other agent scopes are unaffected. Upgrade both server and frontend copies of @anvia/client together so the frontend validator recognizes this event.

版本与发布记录

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

版本发布日期距今
1.8.0latest2026/11/061 天前
1.7.02026/11/043 天前
1.6.02026/10/1919 天前
1.5.02026/10/1622 天前
1.4.42026/10/1325 天前
1.4.32026/10/1325 天前

其他发布通道:rc → 1.0.0-rc.11。

关于 @anvia/client 的常见问题

@anvia/client 是什么?

Framework-neutral client protocol, transports, and UI message state for Anvia.

如何安装 @anvia/client?

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

@anvia/client 支持 TypeScript 吗?

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

@anvia/client 支持 ESM 还是 CommonJS?

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

@anvia/client 可以商用吗?

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

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

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

@anvia/client 还在维护吗?

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