Public API · v1

yhang harness API

一套基于 HTTP 与 SSE 的 Agent 运行时接口。所有请求使用租户 API Key 鉴权,事件带递增序号,断线后可以指定位置继续接收。

Base URL:https://yhangharness.comContent-Type:application/json鉴权:Bearer

快速开始

先在控制台创建 API Key,然后发起第一个 Run。

curl -N https://yhangharness.com/v1/runs \
  -H "Authorization: Bearer $YH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"input":"整理这份材料,生成一份汇报 PPT"}'

默认以 text/event-stream 返回。需要一次性结果时传 "stream": false。

认证

请求头携带 Authorization: Bearer <API_KEY>。 Key 在服务端以 SHA-256 哈希存储,明文只在创建时返回一次。

Scope允许的操作说明
runs:read查询 Run、事件、用量、Webhook 列表只读
runs:write发起、取消 Run,创建 Webhook写入
approvals:write批准或拒绝待审批操作写入

每个 Key 可设置每分钟请求上限。浏览器端不要把长期 Key 打进前端包, 应通过自己的 BFF 转发。

发起 Run

POST /v1/runs
字段类型说明
inputstring任务描述,与 message 二选一
conversationIdstring | number继续已有会话
mode"chat" | "work"对话或执行模式
modelstring覆盖默认模型
streamboolean默认 true,false 时直接返回结果

事件流

订阅地址为 GET /v1/runs/:id/events,支持 ?from=<seq> 或 Last-Event-ID 请求头。 每个事件带递增 seq,可以精确续传。

事件含义关键字段
run.startedRun 已创建并开始执行seq
run.resumed从检查点恢复seq
text.delta正文增量seq
text.reset正文重置seq
reasoning.delta推理增量seq
tool.call开始调用工具seq
tool.result工具返回结果seq
progress阶段性进度seq
approval.required需要人工审批approval
run.completed执行完成,含 output 与 usageoutput, usage
run.failed执行失败,含 errorerror
run.cancelled已取消seq

审批与取消

高风险工具调用会暂停并发出 approval.required。 人工确认后,Run 从断点继续,不会重新执行已完成的步骤。

# 批准
curl -X POST https://yhangharness.com/v1/runs/<runId>/approval \
  -H "Authorization: Bearer $YH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"decision":"approve"}'

# 拒绝
-d '{"decision":"deny"}'

# 取消
curl -X POST https://yhangharness.com/v1/runs/<runId>/cancel \
  -H "Authorization: Bearer $YH_API_KEY"

租户与成员

每个 API Key 属于一个租户。同一租户下可以添加多个成员, 数据、配额与用量都按租户隔离。

GET    /v1/tenants/me
GET    /v1/tenants/me/members
POST   /v1/tenants/me/members        { "email": "..." }
DELETE /v1/tenants/me/members/:userId

用量

GET /v1/usage 支持 from、to、groupBy=day|key、runId。 返回请求数、Run 数、token 与金额。

curl "https://yhangharness.com/v1/usage?groupBy=day" \
  -H "Authorization: Bearer $YH_API_KEY"

Webhook

Webhook 用于把 Run 状态推送到你的系统。请求体使用 HMAC-SHA256 签名,失败投递会重试并保留记录,可手动重放。

POST /v1/webhooks
{ "url": "https://your.app/hooks/harness",
  "events": ["run.completed", "run.failed"] }

GET  /v1/webhooks/deliveries
POST /v1/webhooks/deliveries/:id/replay

SDK

官方 SDK:@yhang-harness/sdk,同时提供 React 组件。

npm i @yhang-harness/sdk

import { Yuanhang } from "@yhang-harness/sdk";

const yh = new Yuanhang({
  apiKey: process.env.YH_API_KEY,
  baseUrl: "https://yhangharness.com",
});

const run = await yh.runs.create({ input: "生成一份汇报 PPT" });
for await (const event of run.stream()) {
  if (event.type === "text.delta") process.stdout.write(event.delta ?? "");
}

错误格式

{
  "error": {
    "code": "run_not_found",
    "message": "运行不存在"
  }
}
状态码含义处理建议
400参数不合法检查请求体字段
401Key 无效或已撤销重新创建 API Key
403缺少对应 scope换用有权限的 Key
404资源不存在确认 Run 属于当前租户
429超出限流退避重试

注册后进入控制台即可创建 API Key,Key 只在创建时显示一次。