@contraagent/api

中文文档
0.10.1发布于 2026/10/31MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 3336

Typed client for the contraagent monitor API: generated types, REST client, live feeds and the run state they fold into

原始使用文档
文档目录8 节
  1. Getting it
  2. Use it from a checkout
  3. Types
  4. The client
  5. The live feeds
  6. An export
  7. The store
  8. Tasks and their outcomes

@contraagent/api

A typed client for the contraagent monitor's HTTP API: types generated from the monitor's OpenAPI document, a REST client, the two live feeds, and a store that folds a run's feed into one typed state. No UI: @contraagent/ui draws it.

The types are generated from the monitor's pydantic models via OpenAPI, so a field the server removes fails your type check, not your page.

A ::code line quotes a file of the package; the documentation site shows the quoted lines in place. The same declarations, with their comments, are in dist/**/*.d.ts.

Getting it

Install the package from the npm registry:

npm install @contraagent/api

A checkout of the repository packs its own version (Use it from a checkout). Every monitor also serves the client, the feeds and the store as exports of its /app.js (@contraagent/ui's documentation).

Use it from a checkout

In the repository's web/ (Node 22, pnpm through corepack enable):

pnpm install && pnpm tarballs

pnpm tarballs builds the packages and writes one .tgz file for each to web/tarballs/. Then, in your project, with npm or pnpm:

npm install <checkout>/web/tarballs/contraagent-api-*.tgz
pnpm add <checkout>/web/tarballs/contraagent-api-*.tgz

Types

Every response and feed message has a type, with readable aliases: JournalRow, RunSummary, Plan, Outputs, TaskDetail, StreamPage, TranscriptEvent, and so on. Fields the server may leave out are optional. Closed vocabularies are unions: Stop (how an attempt ended, JournalRow['stop']), TaskState, ContractStatus.

The live feeds are discriminated unions on the SSE event name, so a switch on msg.event narrows msg.data:

::code{file="src/run-state.ts" from="export function reduceRun" to="// --- selectors"}

The client

createMonitorClient({ baseUrl }) has one typed method per JSON endpoint (src/client.ts); each throws ApiError when it gets no JSON answer: its failure is network, status or not-json (such as an HTML page from a server without the API), with the url, status and media type.

The live feeds

  • openRunFeed(run, panels, handlers): the run, live. On every (re)connect the server replays the whole journal and then sends ready; every message after ready is new.
  • openStreamFeed(run, key, handlers): one transcript, tailed: the tail page first, then only what is new.

Each returns a function that closes it.

An export

An export (contraagent monitor export) is the monitor's views of some runs, written as files and read with no server. createExportClient(url) is a client of its data/ directory: the same methods as the server's client, each read from its file. openExportFeed(client) is the run feed for it: it plays a run's files once and ends with ready. Give both to MonitorStore (client and openRunFeed) to read an export as a server is read.

The store

MonitorStore keeps the run list fresh, opens the selected run's feed, folds its messages into one RunState, and tells subscribers. It knows nothing of any UI framework: a Lit controller, a React hook or plain code subscribe alike. Its snapshot's error is the ApiError of the last request for the title or the run list that got no answer, and runsLoaded says the run list has been answered. The reducer behind it, reduceRun(state, actions), is pure: use it with your own state management if you prefer. @contraagent/ui's documentation shows the store feeding its elements.

Tasks and their outcomes

A task is one task key. deriveRun(journal, plan) gives what every view counts and lists about the run's tasks, computed once per state (the same journal and plan objects give the same result):

  • outcomes and rows: every task of the run, the plan joined with the journal, with its outcome (done, recovered, partial, failed, not_started, running, waiting, skipped; partial: ok, its output a placeholder, neither done nor failed);
  • counts: tasks by outcome, doneTotal (done and recovered: "N of M tasks done") and finished (done, recovered, partial and skipped);
  • problems: failed tasks with the tasks they stopped, tasks not started for a reason of their own, and recovered tasks;
  • kinds and tries.

latestTask(rows) is the task a page opens by default. A refusal before launch (invalid_input) is not a try: a task refused and then done is done, not recovered.

版本与发布记录

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

版本发布日期距今
0.10.1latest2026/10/3110 天前
0.10.02026/10/3111 天前
0.9.22026/10/2516 天前
0.9.12026/10/2517 天前
0.9.02026/10/2417 天前
0.8.12026/10/2318 天前

关于 @contraagent/api 的常见问题

@contraagent/api 是什么?

Typed client for the contraagent monitor API: generated types, REST client, live feeds and the run state they fold into

如何安装 @contraagent/api?

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

@contraagent/api 支持 TypeScript 吗?

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

@contraagent/api 支持 ESM 还是 CommonJS?

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

@contraagent/api 可以商用吗?

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

@contraagent/api 的下载量有多少?

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

@contraagent/api 还在维护吗?

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