@godspeedai/cognate-capabilities

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

Two-phase capability selection: deterministic eligibility, then contained adaptive ranking with durable explanations (spec §8.2, §17, §30).

原始使用文档
文档目录7 节
  1. Installation
  2. Quick start
  3. How selection works
  4. The model provider contract
  5. Limitations
  6. Related packages
  7. License

@godspeedai/cognate-capabilities

Deterministic-first capability selection and the model provider contract for Cognate applications. Given a requirement and a set of candidate providers, it decides — explainably and durably — which provider to use, and it defines the plain-TypeScript ModelProvider interface that model integrations implement.

When to use this package: you choose between multiple providers for the same capability (for example several models) and need eligibility rules that always apply, ranking that can never override policy, and a recorded explanation of every decision. Implementors of model providers also need it for the ModelProvider, ModelRequest/ModelResponse, and ModelProviderError types.

Installation

bun add @godspeedai/cognate-capabilities

Requires Bun >= 1.4.0. Recording selections needs any @godspeedai/cognate-events store. The SDK ships Bun-native TypeScript source, so add @types/bun and @types/node as dev dependencies when typechecking your project.

Quick start

import { createMemoryStore } from "@godspeedai/cognate-store-memory";
import {
  compatibilityRule,
  createSelector,
  recordSelection,
} from "@godspeedai/cognate-capabilities";

const store = createMemoryStore();

const selector = createSelector({
  rules: [
    compatibilityRule, // contract id, major version, and attributes must satisfy the requirement
    {
      id: "policy.no-cloud",
      evaluate: (candidate) =>
        candidate.contract.attributes?.hosting === "cloud"
          ? { eligible: false, reason: "tenant forbids cloud models" }
          : { eligible: true, reason: "hosting allowed" },
    },
  ],
  // ranker: optional; without one, eligible providers are picked in provider-id order
});

const decision = await selector.select({
  requirement: { capability: "model.generate", version: "1.0", attributes: { structured_output: true } },
  candidates: [
    { providerId: "cloud-a", contract: { id: "model.generate", version: "1.2.0", attributes: { structured_output: true, hosting: "cloud" } } },
    { providerId: "local-b", contract: { id: "model.generate", version: "1.0.0", attributes: { structured_output: true } } },
  ],
  principal: { id: "alice", kind: "user" },
  tenant: "tenant-a",
});

decision.eligible;  // ["local-b"] — cloud-a failed the policy rule
decision.selected;  // "local-b"
decision.filters;   // every (provider, rule) result with its reason

// Make the decision a durable fact with its full explanation:
await recordSelection(store, decision, { tenant: "tenant-a", scopeId: "root", correlationId: "corr-1", runId: "run-1" });

How selection works

  1. Eligibility first. Every EligibilityRule evaluates every candidate. Rules must be pure and deterministic; a candidate is eligible only if all rules pass.
  2. Ranking second — and contained. The optional Ranker sees only the eligible set and can narrow it, never widen it. Ranker entries that are unknown, ineligible, duplicated, or non-finite are dropped. A missing or throwing ranker falls back to deterministic provider-id order, recorded in decision.fallback. With no eligible candidate, selected is null and the ranker is never called.
  3. Durable feedback. recordSelection appends a capability.selected event with the full explanation; recordOutcome appends a capability.outcome for a recorded selection. Run outcomeProjection() in your projection runner to derive per-capability success counts, then pass createOutcomeRanker(store) as the selector's ranker: it scores providers by an upper-confidence-bound rule over recorded outcomes, tries untried providers first, and is deterministic — replaying the same outcomes yields the same ranking. Feedback only influences ranking after it is committed and projected.

The model provider contract

The package also defines the model integration contract every provider implements:

  • ModelProvider — id, model, declared features (streaming, tool use, structured output, multimodal input, context limit), generate(request) and stream(request).
  • ModelRequest / ModelResponse / ModelDelta — messages, tools, responseFormat, and the streamed delta shapes (text, tool_call_start, tool_call_args, tool_call_end, tool_call, done).
  • ModelProviderError — typed failure kinds: unauthorized, rate_limited, invalid_request, unavailable, cancelled, malformed_response.
  • modelContract(provider) — the CapabilityContract a provider publishes (capability id model.generate, features as attributes, never credentials).

Ready-made providers: @godspeedai/cognate-provider-local (deterministic, offline) and @godspeedai/cognate-provider-openai-compatible (any OpenAI-compatible HTTP endpoint).

Limitations

  • Rules and rankers are yours to write; this package guarantees containment and honest bookkeeping, not ranking quality.
  • The built-in outcome ranker is a deterministic bandit heuristic over your own recorded outcomes — not a trained model — and scores are only comparable within one capability.
  • recordSelection/recordOutcome write to the store you give them; without a durable store, feedback is lost on restart.

License

Apache-2.0

版本与发布记录

@godspeedai/cognate-capabilities 共发布 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/2423 天前

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

@godspeedai/cognate-capabilities 是什么?

Two-phase capability selection: deterministic eligibility, then contained adaptive ranking with durable explanations (spec §8.2, §17, §30).

如何安装 @godspeedai/cognate-capabilities?

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

@godspeedai/cognate-capabilities 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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