@coval/sdk

中文文档
0.9.34发布于 2026/11/11MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 7788

Typed TypeScript client for the Coval evaluation platform API.

原始使用文档
文档目录17 节
  1. Install
  2. Quick start
  3. What the wrapper adds
  4. Auth — x-api-key (lowercase)
  5. Retries with exponential backoff
  6. Typed errors
  7. Pagination iterator
  8. Diagnosing intermittent failures
  9. Connection reuse
  10. Customization
  11. Override base URL (staging, self-host)
  12. Plug in custom middleware
  13. Override fetch (e.g., undici with keep-alive)
  14. Examples
  15. Versioning
  16. Regenerating
  17. License

@coval/sdk

Typed TypeScript client for the Coval evaluation platform API.

The bulk of this SDK is generated from Coval's public OpenAPI specs (the same ones that power docs.coval.dev/api-reference). A small hand-written layer adds auth, retries with backoff, pagination iteration, and typed errors so the generated client is ergonomic for production use.

Install

npm install @coval/sdk

Requires Node 18+.

Quick start

import { CovalClient } from '@coval/sdk';

const coval = new CovalClient({ apiKey: process.env.COVAL_API_KEY! });

// List agents
const { agents } = await coval.agents.listAgents({ pageSize: 25 });

// Submit a production conversation for evaluation
await coval.conversations.submitConversation({
  covalConversationsAPISubmitConversationRequest: {
    agent_id: 'my-agent-id',
    external_conversation_id: 'call-2026-05-11-abc',
    occurred_at: new Date(),
    transcript: [
      { role: 'agent', content: 'Hi, thanks for calling.' },
      { role: 'user', content: "I'd like to update my account." },
    ],
  },
});

What the wrapper adds

Auth — x-api-key (lowercase)

Coval's gateway is case-sensitive about the API-key header. Uppercase X-API-Key returns Missing API Key. The CovalClient always sends lowercase x-api-key, so you don't have to think about it.

Retries with exponential backoff

For idempotent GET, HEAD, and OPTIONS requests, the client retries automatically on 408, 429, and 5xx responses, plus network errors. Defaults: 3 attempts, 200ms base delay with jitter, 5s cap. Retry-After headers (both seconds and HTTP-date) are respected. Mutating requests are not retried unless you explicitly add their method to retryableMethods.

const coval = new CovalClient({
  apiKey: process.env.COVAL_API_KEY!,
  retry: { maxAttempts: 5, baseDelayMs: 500, maxDelayMs: 10_000 },
});

// Disable retries entirely:
const coval2 = new CovalClient({ apiKey, retry: false });

Typed errors

Every non-2xx HTTP response becomes a CovalApiError after retries are exhausted. Network failures become CovalNetworkError. Both extend Error, so plain try/catch works.

import { CovalApiError } from '@coval/sdk';

try {
  await coval.agents.getAgent({ agentId: 'unknown' });
} catch (err) {
  if (err instanceof CovalApiError) {
    console.error(err.status, err.code, err.message, err.requestId);
  }
}

Pagination iterator

List endpoints return a single page plus a next_page_token. The paginate helper turns that into an async iterator so you don't have to manage tokens.

import { paginate, collectAll } from '@coval/sdk';

for await (const agent of paginate({
  fetchPage: (pageToken) => coval.agents.listAgents({ pageToken, pageSize: 50 }),
  items: (page) => page.agents,
  nextToken: (page) => page.next_page_token,
})) {
  console.log(agent.id, agent.display_name);
}

// Or collect everything into an array:
const all = await collectAll({
  fetchPage: (pageToken) => coval.simulations.listSimulations({ pageToken }),
  items: (p) => p.simulations,
  nextToken: (p) => p.next_page_token,
});

Diagnosing intermittent failures

A request that dies in transit never reaches the API, so there is no server-side trace of it. The client keeps live counters:

coval.stats;
// { calls: 12, requests: 14, retries: 2, networkErrors: 1 }

calls is logical API calls, requests counts every fetch including retries, and networkErrors counts failures where no response came back at all. Include these when reporting sporadic timeouts. For a line per retry and per in-transit failure:

const coval = new CovalClient({ apiKey, onDebug: console.debug });

Connection reuse

On Node the client uses the global fetch (undici), which expires idle keep-alive connections after roughly 4 seconds. That bound matters: some network paths silently drop an idle connection without closing it, and reusing one stalls the request until your timeout, because it never reaches the server.

Requests made within that window can still reuse a pooled connection. If you submit infrequently, or you have seen unexplained timeouts, pass an explicit dispatcher:

import { Agent, fetch as undiciFetch } from 'undici';

const coval = new CovalClient({
  apiKey,
  fetch: ((input, init) =>
    undiciFetch(input as never, { ...init, dispatcher: new Agent({ keepAliveTimeout: 1_000 }) })) as typeof fetch,
});

The Python SDK exposes the same bound directly as max_idle_seconds.

Customization

Override base URL (staging, self-host)

const coval = new CovalClient({
  apiKey,
  baseUrl: 'https://staging.api.coval.dev/v1',
});

Plug in custom middleware

const coval = new CovalClient({
  apiKey,
  middleware: [
    {
      async pre({ url, init }) {
        console.log('→', init.method, url);
        return undefined;
      },
    },
  ],
});

Override fetch (e.g., undici with keep-alive)

import { fetch as undiciFetch } from 'undici';

const coval = new CovalClient({ apiKey, fetch: undiciFetch });

Examples

Runnable in this repo. Set COVAL_API_KEY (and COVAL_AGENT_ID for the submit example) and run:

npm run example:list-agents
npm run example:submit-conversation

Versioning

The generated surface tracks the underlying Coval API. Spec changes that break the generated client increment the minor version. Hand-written wrapper changes that break the public TypeScript API also increment the minor version. Bug fixes increment the patch version.

Regenerating

If you want to regenerate the client locally:

# From the repo root, with the docs repo checked out as a sibling:
node scripts/bundle-spec.mjs
bash scripts/generate-sdks.sh

See Contributing for local generation and the release guide for scheduled regeneration setup.

License

MIT.

版本与发布记录

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

版本发布日期距今
0.9.34latest2026/11/11今天
0.9.332026/11/101 天前
0.9.322026/11/101 天前
0.9.312026/11/083 天前
0.9.302026/11/073 天前
0.9.292026/11/074 天前

关于 @coval/sdk 的常见问题

@coval/sdk 是什么?

Typed TypeScript client for the Coval evaluation platform API.

如何安装 @coval/sdk?

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

@coval/sdk 支持 TypeScript 吗?

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

@coval/sdk 支持 ESM 还是 CommonJS?

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

@coval/sdk 需要什么版本的 Node.js?

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

@coval/sdk 可以商用吗?

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

@coval/sdk 的下载量有多少?

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

@coval/sdk 还在维护吗?

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