@agent-assistant/core

中文文档
0.4.37发布于 2026/10/04TypeScriptES Module零依赖构建来源已验证近 30 天下载 1.1万

Assistant definition, lifecycle, and runtime composition for Agent Assistant SDK

原始使用文档
文档目录12 节
  1. What It Owns
  2. What It Does Not Own
  3. Installation
  4. Public API
  5. createAssistant(definition, adapters)
  6. Runtime Behavior
  7. Lifecycle
  8. Dispatch
  9. Emit
  10. Example
  11. Traits
  12. Development

@agent-assistant/core

@agent-assistant/core is the root composition package for Agent Assistant SDK. It defines the assistant contract, creates the runtime, dispatches normalized inbound messages to capability handlers, and emits outbound events through injected adapters.

The package is TypeScript-first, has no cloud assumptions, and does not implement sessions, surfaces, memory, routing, or product logic. Those concerns stay in later packages and integrate through abstract contracts defined here.

What It Owns

  • AssistantDefinition for assistant identity, capabilities, hooks, runtime constraints, and traits
  • createAssistant() for constructing the runtime with injected inbound and outbound adapters
  • AssistantRuntime lifecycle methods: start(), stop(), dispatch(), emit(), register(), get(), and status()
  • capability dispatch with hook support, timeout handling, and concurrency limiting
  • outbound targeting by surfaceId or session fanout through an abstract sessions subsystem
  • storing, freezing, and exposing TraitsProvider on the runtime definition (store and expose — never interpret)

What It Does Not Own

  • relay transport, HTTP servers, sockets, or surface normalization
  • session creation or session lifecycle rules
  • memory persistence or retrieval
  • model routing, policy enforcement, or coordination logic
  • product-specific prompts, workflows, or adapters

Installation

npm install

From the package directory:

cd packages/core
npm install
npm test
npm run build

Public API

import {
  AssistantDefinitionError,
  OutboundEventError,
  createAssistant,
  type AssistantDefinition,
  type InboundMessage,
  type OutboundEvent,
} from "@agent-assistant/core";

createAssistant(definition, adapters)

Creates an AssistantRuntime from a validated assistant definition and two injected adapters:

  • inbound.onMessage(handler) and inbound.offMessage(handler) connect normalized inbound messages into core
  • outbound.send(event) delivers targeted outbound messages
  • outbound.fanout(event, surfaceIds) is optional and used for session fanout when available

The runtime is created in the created state. Call start() before dispatching messages.

Runtime Behavior

Lifecycle

  • start() is idempotent while started
  • stop() is idempotent after stop
  • a stopped runtime cannot be restarted
  • status() returns readiness, startup time, registered subsystems, registered capabilities, and current in-flight handler count

Dispatch

  • dispatch(message) runs the pre-dispatch onMessage hook when defined
  • returning false from onMessage drops the message without invoking a capability
  • missing capabilities do not throw from dispatch(); they report through onError
  • capability failures and timeouts report through onError
  • concurrency is limited by constraints.maxConcurrentHandlers and extra dispatches queue in FIFO order

Emit

  • emit({ surfaceId, text }) performs a targeted send
  • emit({ sessionId, text }) resolves the registered sessions subsystem and fans out to its attachedSurfaces
  • emit() throws OutboundEventError when neither surfaceId nor sessionId is present

Core keeps the sessions contract abstract. The runtime expects a subsystem registered under the string key sessions with either:

runtime.register("sessions", {
  async getSession(sessionId: string) {
    return {
      attachedSurfaces: ["surface-a", "surface-b"],
    };
  },
});

or:

runtime.register("sessions", {
  async get(sessionId: string) {
    return {
      attachedSurfaces: ["surface-a", "surface-b"],
    };
  },
});

Example

import { createAssistant, type InboundMessage } from "@agent-assistant/core";

const runtime = createAssistant(
  {
    id: "assistant-1",
    name: "Example Assistant",
    capabilities: {
      reply: async (message: InboundMessage, context) => {
        context.log.info("handling inbound message");
        await context.runtime.emit({
          surfaceId: message.surfaceId,
          text: `Echo: ${message.text}`,
        });
      },
    },
  },
  {
    inbound: {
      onMessage(handler) {
        void handler;
      },
      offMessage(handler) {
        void handler;
      },
    },
    outbound: {
      async send(event) {
        console.log("send", event);
      },
    },
  },
);

await runtime.start();

Traits

AssistantDefinition accepts an optional traits field of type TraitsProvider from @agent-assistant/traits. Core stores and freezes the provider during assembly. It does not read, branch on, or interpret any trait values.

import { createTraitsProvider } from "@agent-assistant/traits";
import { createAssistant } from "@agent-assistant/core";

const runtime = createAssistant(
  {
    id: "sage-assistant",
    name: "Sage",
    traits: createTraitsProvider(
      { voice: "concise", formality: "professional", proactivity: "medium", riskPosture: "moderate" },
      { preferMarkdown: true, preferredResponseLength: 800 },
    ),
    capabilities: {
      reply: async (message, context) => {
        // Capability handlers access traits as read-only data
        const voice = context.runtime.definition.traits?.traits.voice;
        const preferMarkdown = context.runtime.definition.traits?.surfaceFormatting?.preferMarkdown;
        // Surface formatting decisions live here — not in core
        void voice; void preferMarkdown;
      },
    },
  },
  adapters,
);

traits is optional. Existing definitions without traits work without modification.

@agent-assistant/traits is a peer dependency. Consumers that do not use traits do not need to install it.

Development

  • npm test runs the isolated Vitest suite for WF-1, WF-2, WF-4, and WF-5 core workflows
  • npm run build emits dist/ declarations and JavaScript via tsc

CORE_PACKAGE_IMPLEMENTED

版本与发布记录

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

版本发布日期距今
0.4.37latest2026/10/041 个月前
0.4.362026/10/041 个月前
0.4.352026/05/235 个月前
0.4.332026/05/155 个月前
0.4.322026/05/125 个月前
0.4.312026/05/086 个月前

关于 @agent-assistant/core 的常见问题

@agent-assistant/core 是什么?

Assistant definition, lifecycle, and runtime composition for Agent Assistant SDK

如何安装 @agent-assistant/core?

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

@agent-assistant/core 支持 TypeScript 吗?

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

@agent-assistant/core 支持 ESM 还是 CommonJS?

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

@agent-assistant/core 可以商用吗?

@agent-assistant/core 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@agent-assistant/core 的下载量有多少?

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

@agent-assistant/core 还在维护吗?

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