@agentskit/statechart

中文文档
0.3.2发布于 2026/10/10MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 9773

Deterministic, serializable interaction state for AgentsKit applications.

原始使用文档
文档目录11 节
  1. Why
  2. Verified proof
  3. Install
  4. Usage
  5. Contract
  6. Deliberate boundaries
  7. License
  8. Quick start
  9. Maturity and compatibility
  10. Contributing
  11. How this fits the ecosystem

@agentskit/statechart

Profile: concise-package

AgentsKit

Tags: agentskit · typescript · ai-agents

stability

Deterministic, serializable interaction state for AgentsKit applications. It is framework-neutral and has zero runtime dependencies.

Why

Interactive agent experiences often need explicit states such as waiting for input, confirming an action, or completing a task. This package gives every UI binding and host the same small transition and snapshot contract without coupling them to React, an LLM provider, ChatController, or the Runtime execution engine.

Use Runtime flows for durable execution, DAGs, tools, and effects. Use this package for local or host-managed interaction state.

Verified proof

Install

npm install @agentskit/statechart

Usage

import {
  createStatechartInstance,
  defineStatechart,
  serializeStatechart,
  transitionStatechart,
  type StatechartEvent,
} from '@agentskit/statechart'

type Context = { confirmed: boolean }
type Event = StatechartEvent<'confirm'> | StatechartEvent<'cancel'>

const parseContext = (input: unknown): Context => {
  if (
    input === null ||
    typeof input !== 'object' ||
    typeof (input as { confirmed?: unknown }).confirmed !== 'boolean'
  ) {
    throw new TypeError('invalid context')
  }
  return input as Context
}

const confirmation = defineStatechart<
  Context,
  Event,
  'waiting' | 'confirmed' | 'cancelled'
>({
  id: 'confirmation',
  version: '1',
  initial: 'waiting',
  parseContext,
  states: {
    waiting: {
      on: {
        confirm: {
          target: 'confirmed',
          reduce: (context) => ({ ...context, confirmed: true }),
        },
        cancel: { target: 'cancelled' },
      },
    },
    confirmed: {},
    cancelled: {},
  },
})

const initial = createStatechartInstance(
  confirmation,
  { confirmed: false },
  { instanceId: 'interaction-42', now: '2026-07-13T12:00:00.000Z' },
)

const result = transitionStatechart(
  confirmation,
  initial,
  { type: 'confirm' },
  { now: '2026-07-13T12:01:00.000Z' },
)

if (result.status === 'accepted') {
  const snapshot = serializeStatechart(result.instance)
  await yourStorage.save(snapshot)
}

The host supplies IDs, timestamps, storage, and event delivery. That keeps transitions reproducible and the package portable across browser, server, native, and terminal runtimes.

Contract

  • defineStatechart validates targets and freezes a trusted runtime definition.
  • createStatechartInstance validates and freezes JSON-compatible context.
  • transitionStatechart is synchronous and returns an accepted or rejected result.
  • serializeStatechart creates a versioned JSON-compatible snapshot.
  • restoreStatechart accepts unknown, validates metadata and context, and never trusts serialized definitions.
  • notifyStatechartObserver delivers a completed result separately; observer failure cannot alter state.

Events, contexts, and snapshots use an exact JSON boundary: sparse or decorated arrays, accessors, symbols, exotic prototypes, and non-finite numbers are rejected without invoking getters. Hostile but valid object keys such as __proto__ remain ordinary data keys. Observers must complete synchronously; returned thenables are isolated as typed failures.

Context validation is injected through parseContext, so applications can use any validation library without adding one to this package.

Deliberate boundaries

This package does not execute actions, persist snapshots, call agents or tools, retry events, deduplicate delivery, render components, or define product-specific states. Repeated events follow the current state's transition table; delivery idempotency belongs to the host.

See ADR-0020 for the ownership decision and ADR-0027 for the hardened beta boundaries.

License

MIT

Quick start

import { createStatechartInstance, defineStatechart } from '@agentskit/statechart'

const toggle = defineStatechart({
  id: 'toggle',
  version: '1',
  initial: 'off',
  parseContext: () => ({}),
  states: { off: { on: { toggle: { target: 'on' } } }, on: {} },
})

const instance = createStatechartInstance(toggle, {}, {
  instanceId: 'toggle-1',
  now: '2026-07-17T12:00:00.000Z',
})

console.log(instance.state)

Maturity and compatibility

  • Stability: beta — see docs/STABILITY.md
  • Node.js 20+ and TypeScript strict mode
  • Published as @agentskit/statechart

Contributing

See CONTRIBUTING.md and the monorepo LICENSE.

How this fits the ecosystem

Use AgentsKit Registry for ready-made agents and the Agents Playbook for production practices.

版本与发布记录

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

版本发布日期距今
0.3.2latest2026/10/1026 天前
0.3.12026/09/032 个月前
0.3.02026/07/203 个月前
0.2.12026/07/203 个月前
0.2.02026/07/143 个月前

关于 @agentskit/statechart 的常见问题

@agentskit/statechart 是什么?

Deterministic, serializable interaction state for AgentsKit applications.

如何安装 @agentskit/statechart?

在项目目录运行 npm install @agentskit/statechart,或使用 pnpm add / yarn add / bun add。

@agentskit/statechart 支持 TypeScript 吗?

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

@agentskit/statechart 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agentskit/statechart 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agentskit/statechart 可以商用吗?

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

@agentskit/statechart 的下载量有多少?

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

@agentskit/statechart 还在维护吗?

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