@anvia/core

中文文档
1.18.0发布于 2026/11/06MITTypeScriptES Module构建来源已验证近 30 天下载 4618

Core runtime primitives for context-aware Anvia agents.

原始使用文档
文档目录6 节
  1. Install
  2. Quickstart
  3. Ask the user a question
  4. What you can build
  5. Learn more
  6. Stream consumption

@anvia/core

Build provider-independent AI agents and workflows with typed tools, streaming, structured output, and application-owned memory. Core runs the model/tool loop while your application chooses providers, credentials, storage, and delivery.

Install

pnpm add @anvia/core @anvia/openai zod

This example uses OpenAI. Other provider adapters work with the same Core APIs.

Quickstart

Set OPENAI_API_KEY in your server environment, then create an agent with a typed tool:

import { Agent, createTool } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { z } from "zod";

const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) throw new Error("Set OPENAI_API_KEY.");
const client = new OpenAIClient({ apiKey });
const lookupOrder = createTool({
  name: "lookup_order",
  description: "Look up an order by ID.",
  inputSchema: z.object({ orderId: z.string() }),
  execute: async ({ orderId }) => ({ orderId, status: "processing" }),
});

const agent = new Agent({
  id: "support",
  model: client.completionModel({ modelId: "gpt-5", api: "responses" }),
  instructions: "Help customers with order questions.",
  tools: [lookupOrder],
  maxTurns: 4,
});

const result = await agent.generate({ prompt: "What is happening with order A123?" });
if (result.type === "response") console.log(result.output);

Replace the sample tool body with your application's data access. Agent outcomes distinguish a response, a guardrail block, and an interaction that needs application input.

Ask the user a question

Add createQuestionTool() to an agent's tools to let it ask for missing information:

import {
  createQuestionTool,
  DEFAULT_QUESTION_TOOL_NAME,
  DEFAULT_QUESTION_TOOL_DESCRIPTION,
} from "@anvia/core/tool";

const askUser = createQuestionTool(); // name: "ask_user", with built-in model instructions
const clarify = createQuestionTool({ name: "clarify" }); // either option can be overridden

The defaults are also exported from @anvia/core. The description guides the model to ask only when it cannot proceed, batch questions, and use sensible defaults for routine choices. Supply nonblank custom names and descriptions. Defaults apply when an option is omitted; explicit strings, including blanks, are preserved for compatibility with existing callers. New tool calls accept 1–4 questions with unique, nonblank IDs and nonblank text. Optional choices must contain 2–4 entries with nonblank labels and unique, nonblank values. Values are trimmed before validation. Set allowCustom: true to allow answers outside the choices, or omit choices for free text. A question without choices cannot set allowCustom: false. Snake-case IDs, question marks, and mutually exclusive choices are model guidance, not additional schema restrictions.

The run returns a type: "interaction" outcome with a tool-question request and a continuation. Collect a nonblank answer for every question, then resume with the continuation and { type: "tool-question", answers: [{ questionId, value }] }. The model receives { answers: [...] } as the tool result. Calling the question tool directly does not prompt a UI; your application renders the interaction and supplies the answers.

Stored interaction requests and continuations keep their existing parsing rules; the new array limits apply only to new tool inputs. See the interaction guide for the resume protocol.

What you can build

  • Agents with typed tools, approvals, questions, guardrails, and streaming responses.
  • Agent teams with isolated conversations, delegation, and coordinated cancellation.
  • Direct completions and schema-validated extraction without an agent loop.
  • Typed decisions for classification, tagging, scoring, and routing.
  • Sequential or parallel pipelines and evaluations with built-in or custom metrics.
  • Stateful conversations using memory adapters and explicit compaction policies, including active tool loops.
  • RAG with document chunking, embeddings, and vector-store contracts.
  • Image generation, speech, transcription, MCP tools, and local Agent Skills.

Use @anvia/client, @anvia/server, and @anvia/react to deliver agent output to an application UI. Use provider, memory, vector-store, and observability adapters for the infrastructure you choose.

InMemoryVectorStore.inspect() accepts canonical nonnegative decimal cursor strings within the safe-integer range; omit the cursor to start at zero. Invalid cursors throw VectorInspectCursorError, a RangeError. The @anvia/core/vector-store subpath also exports parseVectorInspectCursor() and VectorInspectCursorError.isCursorError() for validation and error handling. See the inspection guide.

Streaming completions preserve empty or encrypted reasoning supplied only in the final provider response, including Azure Responses streams. Final encrypted details can also enrich matching streamed reasoning for replay on subsequent turns. Conflicting text, reasoning, and tool calls still fail provider output validation.

For recoverable execution, register a supported agent with the experimental @anvia/durable runtime. Internal execution protocol 4 adds a synchronous steering checkpoint boundary for persisting queued user input and replaying it consistently. Upgrade core and durable together. Durable tools receive an optional stable ToolCallContext.operationId for external idempotency. Direct execution does not supply it. Durable registrations can opt into persisted streaming with stream: true; core execution protocol version 4 checkpoints validated streamed completions alongside tool results.

Learn more

Stream consumption

An AgentStream has one consumer. Reading stream.result or stream.text schedules automatic draining on the next microtask if event iteration has not started. When you need events and the final outcome, iterate first and read result after the loop:

const stream = agent.stream({ prompt: "Hello" });
for await (const event of stream) {
  if (event.type === "text_delta") process.stdout.write(event.delta);
}
const outcome = await stream.result;

For HTTP adapters whose consumption starts later, pass the stream without reading its final promises first; capture terminal outcome events while forwarding events.

版本与发布记录

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

版本发布日期距今
1.18.0latest2026/11/061 天前
1.17.32026/10/2910 天前
1.17.22026/10/2711 天前
1.17.12026/10/2018 天前
1.17.02026/10/1919 天前
1.16.02026/10/1622 天前

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

关于 @anvia/core 的常见问题

@anvia/core 是什么?

Core runtime primitives for context-aware Anvia agents.

如何安装 @anvia/core?

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

@anvia/core 支持 TypeScript 吗?

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

@anvia/core 支持 ESM 还是 CommonJS?

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

@anvia/core 可以商用吗?

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

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

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

@anvia/core 还在维护吗?

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