@godspeedai/cognate-client

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

Headless client: runs, reconnecting event subscriptions, derived run state, shared projections, continuations, frontend capabilities, artifacts. No React (spec §25.1).

原始使用文档
文档目录6 节
  1. Installation
  2. Quick start
  3. What the client covers
  4. Limitations
  5. Related packages
  6. License

@godspeedai/cognate-client

The headless Cognate client: start and watch runs, resolve approvals, write shared state, serve frontend capabilities to agents, and read projections — over any RuntimeService, in-process or remote. All client state is derived from durable events, so it survives reconnects and can be rebuilt from scratch. Framework-neutral: no React (React bindings live in @godspeedai/cognate-react).

When to use this package: any front end, service, or script that talks to a Cognate runtime. For a browser UI, pair it with the React bindings; for a remote runtime, hand it a transport-backed RuntimeService such as the one from @godspeedai/cognate-protocol-connect.

Installation

bun add @godspeedai/cognate-client

Requires Bun >= 1.4.0.

Quick start

A complete round trip against the in-process reference runtime — the same flow works unchanged against a remote service:

import { createClient } from "@godspeedai/cognate-client";
import { alice } from "@godspeedai/cognate-runtime-api/conformance"; // a ready-made caller
import type { AgentDefinition } from "@godspeedai/cognate-runtime-api";
import { createRuntime } from "@godspeedai/cognate-runtime-bun";

const refundAgent: AgentDefinition = {
  id: "agent.refund",
  version: "1.0.0",
  async run(input, ctx) {
    const { amount } = input as { amount: number };
    const decision = (await ctx.waitFor("approve", {
      type: "refund-approval",
      condition: { kind: "human_approval" },
      payload: { amount },
    })) as { approved: boolean };
    return { amount, approved: decision.approved };
  },
};

const runtime = await createRuntime({
  store: ":memory:",
  policy: { authorize: () => ({ allow: true, policyId: "p", reason: "ok" }) },
  actions: { authorize: () => ({ allow: true, reason: "ok" }) },
  agents: [refundAgent],
});

// `caller` is the identity established by your transport's authenticator — never a request body.
const client = createClient({ service: runtime.service, caller: alice });

const run = await client.startRun("agent.refund", { amount: 75 });

async function waitUntil<T>(read: () => T, ok: (v: T) => boolean): Promise<T> {
  for (;;) {
    const value = read();
    if (ok(value)) return value;
    await Bun.sleep(10);
  }
}

// `run(id)` is a live, reactive view derived only from the run's durable events.
const waiting = await waitUntil(
  () => client.run(run.runId).get(),
  (s) => s.openContinuations.length === 1,
);
const approval = waiting.openContinuations[0]!;

await client.resolveContinuation(approval.id, "resume", { approved: true });

const done = await waitUntil(
  () => client.run(run.runId).get(),
  (s) => s.status === "completed",
);
console.log(done.output); // { amount: 75, approved: true }

await client.close();
await runtime.close();

Subscribe instead of polling where it suits you:

const stop = client.run(runId).subscribe((state) => {
  console.log(state.status, state.text, state.state);
});
// state.text accumulates message.delta events; state.state folds state.changed
// snapshots and RFC 6902 patches; stop() unsubscribes.

What the client covers

  • Runs — startRun(agent, input, { correlationId?, state? }), cancelRun(runId, reason), and run(runId): a Readable<RunState> with status, accumulated text, folded shared state, openContinuations, artifacts, output, and error.
  • Shared state, both directions — startRun(..., { state }) seeds a run's initial state; updateState(runId, update) writes while the run waits (snapshot or JSON Patch), and the agent observes it through its own state reads. threadSharedState(threadId) / updateThreadSharedState do the same for durable state shared by every run in a thread.
  • A reconnecting event stream — subscribe({ afterPosition?, runId? }, listener) resumes from the last delivered position after a disconnect: no gaps, no duplicates. status reports connecting / live / reconnecting / closed (backoff defaults to 50 ms doubling to 2 s).
  • Frontend capabilities — provideCapability("map.focus", handler) executes open frontend_capability continuations that name the capability and resolves them for the run; the agent side is invokeFrontend from @godspeedai/cognate-runtime-api. offerCapability(offer, handler) additionally publishes the capability as a lease-bound offer for one agent, renewed automatically until you unregister it.
  • Continuations, projections, invocations, artifacts — resolveContinuation(id, "resume" | "cancel", input), projection(name) (a Readable refreshed as events commit, with refresh() for on-demand re-reads), invoke(capability, input) through the runtime's authorization path, and artifact(artifactId) to fetch bytes.
  • Cleanup — close() stops the stream and revokes any remote offers this client published.

Limitations

  • Derived, never authoritative. Everything the client shows is a fold of the tenant's durable events; the runtime and its store own the truth. A projection read may briefly lag the event that produced it — call refresh() when you need the latest read.
  • No authentication here. The client takes a Caller you established elsewhere (your transport's authenticator); it never sends credentials itself.
  • No React. The Readable interface (get/subscribe) is deliberately framework-neutral; use @godspeedai/cognate-react for React 19 bindings.

License

Apache-2.0

版本与发布记录

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

@godspeedai/cognate-client 是什么?

Headless client: runs, reconnecting event subscriptions, derived run state, shared projections, continuations, frontend capabilities, artifacts. No React (spec §25.1).

如何安装 @godspeedai/cognate-client?

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

@godspeedai/cognate-client 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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