@godspeedai/cognate-continuations

中文文档
0.10.0发布于 2026/11/16Apache-2.0ES Module构建来源已验证近 30 天下载 3589

Durable continuations and external commitments on the durable store (spec §9.3, §12).

原始使用文档
文档目录6 节
  1. Installation
  2. Continuations quick start
  3. Commitments quick start
  4. Limitations
  5. Related packages
  6. License

@godspeedai/cognate-continuations

Durable pauses and durable external effects for Cognate applications. ContinuationService parks a workflow until the outside world answers (a human approval, a callback); CommitmentService records an intention to affect an external system and drives it to a known outcome. Both keep every transition as committed facts in your @godspeedai/cognate-events store, so they survive crashes and restarts.

When to use this package: your run must wait for input that arrives later, or it must call a system where "did it happen?" matters (payments, emails, tickets). Continuations are pauses with an audit trail; commitments are the alternative to fire-and-forget HTTP calls.

Installation

bun add @godspeedai/cognate-continuations

Requires Bun >= 1.4.0 and a store: @godspeedai/cognate-store-sqlite for durability or @godspeedai/cognate-store-memory for tests. The SDK ships Bun-native TypeScript source, so add @types/bun and @types/node as dev dependencies when typechecking your project.

Continuations quick start

import { openSqliteStore } from "@godspeedai/cognate-store-sqlite";
import { ContinuationService } from "@godspeedai/cognate-continuations";

const store = openSqliteStore({ path: "./app.sqlite" });
const continuations = new ContinuationService(store, {
  policy: {
    authorize: ({ actor }) =>
      actor.kind === "user" ? { allow: true, reason: "approver" } : { allow: false, reason: "not an approver" },
  },
});

// Register the code that runs when a continuation of this type is resumed.
const unregister = continuations.registerHandler({
  id: "refund.resume",
  version: "1.0.0",
  supports: (storedVersion) => storedVersion.startsWith("1."),
  handle: async (record) => {
    // continue the workflow with record.resolution.input
  },
});

const request = await continuations.open({
  type: "refund-approval",
  tenant: "tenant-a",
  runId: "run-1",
  scopeId: "root",
  correlationId: "corr-1",
  condition: { kind: "human_approval", role: "manager" },
  payload: { amount: 42 },
  handler: { id: "refund.resume", version: "1.0.0" },
  expiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000, // optional
});

// Later — when the approval arrives:
const outcome = await continuations.resume(request.id, {
  actor: { id: "alice", kind: "user" },
  idempotencyKey: "approval-form-8123",
  input: { approved: true },
});
// outcome.kind is one of:
//   "transitioned" | "duplicate" | "already_terminal" | "denied" | "version_mismatch" | "not_found"

// From a background loop: deliver resumed continuations to handlers, expire due ones.
await continuations.dispatchResumes();
await continuations.expireDue();

A continuation moves OPEN → RESUMED | CANCELLED | EXPIRED | FAILED, exactly once: concurrent resume/cancel/expiry attempts resolve to a single terminal transition, and losers get already_terminal. Repeating a request with the same actor and idempotency key returns duplicate with the original result. A resumed continuation whose registered handler does not support the stored handler version goes to BLOCKED instead of being delivered; unblock returns it to RESUMED for delivery after you register a compatible handler.

Commitments quick start

import { CommitmentService } from "@godspeedai/cognate-continuations";

const commitments = new CommitmentService(store);

commitments.registerTarget({
  id: "payment-api",
  idempotent: true, // true only if the target deduplicates retries by idempotency key
  async dispatch({ idempotencyKey, payload }) {
    const remoteRef = await chargeCard(idempotencyKey, payload); // your call
    return { remoteRef, result: { charged: true } };
  },
  // optional: lets reconcile() ask the target what happened
  async status({ idempotencyKey }) {
    return { state: "succeeded", remoteRef: "ch_123", result: { charged: true } };
  },
});

await commitments.intend({
  targetId: "payment-api",
  operation: "charge",
  idempotencyKey: "order-42",
  payload: { cents: 1999 },
  tenant: "tenant-a",
  scopeId: "root",
  correlationId: "corr-1",
});

// From a background loop:
await commitments.dispatch();

A commitment moves INTENDED → DISPATCHING → SUCCEEDED | FAILED | UNKNOWN_RECONCILIATION_REQUIRED. dispatch records DISPATCHING before calling the target, so a crash can never lose the fact that an attempt started. A definitive rejection (throw CommitmentRejectedError from dispatch) is terminal FAILED; any other error is UNKNOWN — retried automatically only when the target is idempotent, otherwise left for reconcile() (asks target.status) or resolveManually(). To undo a succeeded commitment, compensate() creates a new commitment with compensates set; the original is never mutated.

Limitations

  • Handlers and targets are registered in-process. Re-register them after a restart before dispatchResumes()/dispatch() will succeed; undeliverable work retries with backoff (default base 1 s, max 30 s, 8 attempts) and then waits for operator attention.
  • Delivery is at-least-once: handlers must tolerate being called more than once.
  • Nothing here runs timers. Call expireDue() and dispatch()/dispatchResumes() from your own background loop (the reference runtime does this for you).
  • Commitments do not make external systems transactional. They make outcomes knowable: UNKNOWN_RECONCILIATION_REQUIRED still needs reconcile or resolveManually to settle.

License

Apache-2.0

版本与发布记录

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

版本发布日期距今
0.10.0latest2026/11/16今天
0.9.22026/11/152 天前
0.9.12026/11/143 天前
0.9.02026/11/0610 天前
0.8.02026/10/2720 天前
0.7.12026/10/2523 天前

关于 @godspeedai/cognate-continuations 的常见问题

@godspeedai/cognate-continuations 是什么?

Durable continuations and external commitments on the durable store (spec §9.3, §12).

如何安装 @godspeedai/cognate-continuations?

在项目目录运行 npm install @godspeedai/cognate-continuations,或使用 pnpm add / yarn add / bun add。

@godspeedai/cognate-continuations 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/godspeedai__cognate-continuations 是否存在。

@godspeedai/cognate-continuations 支持 ESM 还是 CommonJS?

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

@godspeedai/cognate-continuations 可以商用吗?

@godspeedai/cognate-continuations 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@godspeedai/cognate-continuations 的下载量有多少?

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

@godspeedai/cognate-continuations 还在维护吗?

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