@godspeedai/cognate-runtime-api

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

Transport-independent runtime service contract, agent authoring surface, and service conformance kit (ADR-0011).

原始使用文档
文档目录9 节
  1. Installation
  2. Authoring an agent
  3. Calling the service
  4. Shared state
  5. Frontend capabilities
  6. Conformance subpath
  7. Limitations
  8. Related packages
  9. License

@godspeedai/cognate-runtime-api

The transport-independent runtime contract for Cognate: the RuntimeService interface (runs, events, continuations, artifacts, projections, shared state), the agent authoring surface (AgentDefinition / RunContext), and the canonical shared-state fold used by runtimes and clients alike.

When to use this package: to author agents, to build a client or transport, or to implement a RuntimeService. Applications get a working implementation from @godspeedai/cognate-runtime-bun and a ready-made caller from @godspeedai/cognate-client.

Installation

bun add @godspeedai/cognate-runtime-api

Requires Bun >= 1.4.0.

Authoring an agent

An agent is a deterministic function of its input plus recorded results:

import type { AgentDefinition } from "@godspeedai/cognate-runtime-api";

export const refundAgent: AgentDefinition = {
  id: "agent.refund",
  version: "1.0.0",
  async run(input, ctx) {
    const { amount } = input as { amount: number };
    const quote = await ctx.step("quote", () => ({ total: amount * 2 }));
    await ctx.emit("message.delta", { text: `Total: ${quote.total}` });
    const decision = (await ctx.waitFor("approval", {
      type: "refund-approval",
      condition: { kind: "human_approval" },
      payload: quote,
    })) as { approved: boolean };
    return { approved: decision.approved, total: quote.total };
  },
};

Every RunContext method is durable and replay-safe: results are recorded as events on the run's stream, and re-entry after a suspension or a restart returns the recorded result instead of repeating the work. Agent code must therefore be deterministic given those results.

  • step(id, fn) — run a nondeterministic unit (a model call, an API read) once and record its JSON result.
  • emit(eventType, payload) — record a run-scoped fact exactly once across replays.
  • invoke(capability, input) — call a kernel capability through policy; throws InvocationFailedError on a typed failure.
  • waitFor(id, spec) — suspend until a durable continuation is resolved; cancelling or expiring the continuation fails the run with ContinuationClosedError.
  • artifact({ name, mediaType, bytes }) — produce a durable artifact; returns its id.
  • state(id) / sharedState(id) / updateSharedState(id, update) — read and atomically update the run's and the thread's shared state.
  • recall(id, { text, k }) — recall authorized prior knowledge; returns { status: "degraded" } when cognition is unavailable.

Calling the service

Every RuntimeService operation takes a Caller — the identity the transport's authenticator established, never something read from the request body:

import type { Caller } from "@godspeedai/cognate-runtime-api";

const alice: Caller = { actor: { id: "alice", kind: "user" }, tenant: "tenant-a" };

const run = await service.startRun(alice, {
  agent: "agent.refund",
  input: { amount: 21 },
  idempotencyKey: "refund-1",
});

Failures are ServiceError with a typed code: not_found, permission_denied, invalid_argument, failed_precondition, unavailable, deadline_exceeded, already_exists, unauthenticated, or resource_exhausted.

Shared state

A run's state is the fold of its state.changed events in store position order. This package owns the canonical fold: applyStateUpdate applies one update (a full snapshot or an RFC 6902 patch), foldState folds a sequence of events, and both the runtime and the headless client use them — so every reader derives the same state. Applying a patch that does not fit throws StatePatchError; while folding agent-emitted events, such a patch is skipped rather than hiding later updates.

Frontend capabilities

invokeFrontend(ctx, id, capability, input) turns a client-provided capability into a durable continuation: the run suspends until a connected client that registered the capability executes it and resolves the continuation with a value. It survives disconnects and restarts like any continuation; on failure it throws FrontendCapabilityError.

Conformance subpath

@godspeedai/cognate-runtime-api/conformance runs the entire service contract unchanged in-process and over any transport — the same kit proves both. It exports runtimeServiceConformance (and agent/tool fixtures for it), plus ready-made Caller identities you can use in your own tests and examples:

import { alice, mallory, bob } from "@godspeedai/cognate-runtime-api/conformance";
// alice and mallory are users in tenant "tenant-a"; bob is a user in "tenant-b".

Limitations

  • Contract only: no server and no wire transport. Pair it with an implementation such as the Bun runtime, or map it onto your transport of choice.
  • RunContext is the only supported way for agent code to reach the outside world; anything it records must round-trip as JSON.

License

Apache-2.0

版本与发布记录

@godspeedai/cognate-runtime-api 共发布 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-runtime-api 的常见问题

@godspeedai/cognate-runtime-api 是什么?

Transport-independent runtime service contract, agent authoring surface, and service conformance kit (ADR-0011).

如何安装 @godspeedai/cognate-runtime-api?

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

@godspeedai/cognate-runtime-api 支持 TypeScript 吗?

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

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

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

@godspeedai/cognate-runtime-api 可以商用吗?

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

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

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

@godspeedai/cognate-runtime-api 还在维护吗?

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