@affordance/core

中文文档
0.6.0发布于 2026/09/18MITTypeScriptES Module构建来源已验证近 30 天下载 5.5万

Adaptive case management with guarded steps, async execution, and pluggable storage.

原始使用文档

@affordance/core

Compute available work from current domain facts and actor permissions. A Step's handler owns persistence, transactions, final business admission and external calls. Affordance validates state/input, evaluates Guards and invokes it once.

import { actor, caseType, createBackgroundRuntime, createEngine, stepsOf } from '@affordance/core'
import { z } from 'zod'

const State = z.object({ ein: z.string().nullable() })
const step = stepsOf(State, actor<{ id: string }>())
const definition = caseType({
  name: 'deal', state: State,
  steps: [
    step({
      name: 'record-ein', input: z.object({ ein: z.string() }),
      handler: async ({ reference, actor, input, state }) => {
        const saved = await deals.recordEin(reference, actor, input)
        return { ...state, ein: saved.ein } // Already persisted; evidence only.
      },
    }),
    step({
      name: 'notify',
      handler: async ({ reference, executionId }) => {
        await notifications.send(reference, { executionId })
        // No return: success without a diff journal.
      },
    }),
  ],
})

// Bind an authoritative loader using your adapter, e.g. storage.bindCase(definition, { load }).
const runtime = createBackgroundRuntime()
const engine = createEngine({
  storage, caseTypes: [binding],
  operations: { journalTimeoutMs: 1000 },
  launch: { runtime, leaseMs: 60_000 },
})
const result = await engine.run(caseId, 'record-ein', { actor, input: { ein: '12-3456789' } })
// result.journal.status: 'skipped' | 'recorded' | 'failed'
const { executionId } = await engine.launch(caseId, 'notify', { actor })
const execution = await engine.getExecution(executionId)
// running | completed | unresolved | resolved; null for unknown IDs and ordinary runs.
await runtime.drain() // Stop new launches and await owned tasks during application shutdown.

run waits for handler completion and a bounded optional journal attempt. launch returns after handler entry while the runtime owns the remaining work. Both use the same Promise<State | void> contract and handler context. There are no injected repositories, transaction callbacks, or completion wrappers. undefined skips the journal; a valid State produces an observed diff, even if unchanged. Returned State never becomes current Case State. Subsequent reads use the binding.

A thrown handler error propagates unchanged from run; committed effects may already exist. A launched handler error appears as unresolved status. Invalid evidence and journal failure do not reverse successful handler completion or retry business code. The journal timeout includes adapter acquisition wait. An already-started write may finish late under the original execution ID.

The supplied runtime is for a long-lived Node process and keeps outstanding tasks alive until they settle. It cannot survive process death or a serverless host freezing after a request. Such hosts need an independently owned runtime with a real handler-entry acknowledgment. A queue acceptance alone is insufficient. There is no replay, heartbeat, automatic retry, compensation or recovery worker.

Launch ownership excludes other participating launches for the same Case. Expiration leaves a block until application/operator reconciliation:

await engine.resolveExecution(executionId, { actor, reason: 'Reconciled with provider' })

Resolution is not cancellation. Leases do not fence domain or provider writes; an old handler can still write after expiry or resolution. Ordinary runs and unrelated writers do not participate. The host must authorize status and resolution.

Safe actor attribution defaults to a string actor or string actor.id, otherwise null. Configure operations.actorIdentity for another string identity. Request input and authentication objects are not journaled by this pipeline. Define Case State as the intended journal projection; schema validation applies before and after. Keep credentials and unrelated data out of that schema.

This breaking beta removes execute, executeNonAtomic, transaction-bound handler repositories and the atomic adapter port. Framework journal/lease data is not migrated. See architecture.

版本与发布记录

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

版本发布日期距今
0.6.0latest2026/09/181 个月前
0.5.02026/09/171 个月前
0.4.02026/09/141 个月前
0.3.02026/09/121 个月前
0.2.02026/09/091 个月前
0.1.02026/09/072 个月前

关于 @affordance/core 的常见问题

@affordance/core 是什么?

Adaptive case management with guarded steps, async execution, and pluggable storage.

如何安装 @affordance/core?

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

@affordance/core 支持 TypeScript 吗?

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

@affordance/core 支持 ESM 还是 CommonJS?

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

@affordance/core 需要什么版本的 Node.js?

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

@affordance/core 可以商用吗?

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

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

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

@affordance/core 还在维护吗?

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