跳转到内容
EN

事件与订阅

事件是关于你组织里某个资源的一件已经发生的事,例如一次自动化运行结束了。你订阅它,它发生时你的收件箱收到一封信:智能体被这封信唤醒,人在站内信里看到它。不用轮询,也不用在自己的代码里等。

接口都在 /events/v1 下,用组织 API Key、会话或智能体令牌调用,SDK 方法在 praxis.events.* 下:

SDK 方法HTTP做什么
praxis.events.types.listGET /events/v1/types列出可订阅的事件类型
praxis.events.subscriptions.createPOST /events/v1/subscriptions创建订阅
praxis.events.subscriptions.listGET /events/v1/subscriptions列出你还有效的订阅
praxis.events.subscriptions.retrieveGET /events/v1/subscriptions/{subscription_id}读一个订阅
praxis.events.subscriptions.deleteDELETE /events/v1/subscriptions/{subscription_id}删除订阅,成功返回 204

租户事件

平台里的事件分两级。租户事件是关于你自己资源的事实(一次运行、一个晋升),是公开契约:同一个 schemaversion 内只会加字段;有不兼容的变化时发布新的 schemaversion,新旧两版同时发布一段时间。平台事件是平台自己的运行信号,不能订阅,也不会列在这里。只有租户事件能订阅。

可订阅的类型随平台增加,下面的列表由各服务声明的事件文件生成,与 GET /events/v1/types 返回的是同一份。每个类型的说明和字段名保持声明里的英文原文。

可订阅的事件类型

automation.run.finished

schemaversion 1 · 发布方 praxis-automation

A run reached a terminal state. Published once per run, after the terminal write is persisted; the envelope id is automation.run.finished:<runId>, so a replay of the same terminal write dedupes on the bus. subject is runs/<runId>, tenantid the run’s owning organization. Carries no input, output or credentials. bestEffort: Automation has no outbox for the terminal write, and a lost event is recoverable by reading the run.

字段类型必有说明可用于过滤
runIdstring是data.runId
targetstring | null是namespace/name as requested, or null for an ad-hoc source rundata.target
kind“script” | “workflow”是data.kind
status“completed” | “faulted” | “cancelled”是data.status
durationMsinteger是wall clock in milliseconds from startedAt (createdAt when it never started) to endedAt, including time spent waitingdata.durationMs
channel“console” | “api” | “internal” | “run” | “trigger”是how the run was started: the run record’s channel, as isdata.channel
cancel_reasonstring | null是the run’s cancel_reason as is (user, parent_cancelled, deadline, executor_lost); always present, null unless the run was cancelled. A timeout is status cancelled with cancel_reason deadlinedata.cancel_reason
startedAtstring | null是RFC3339 UTCdata.startedAt
endedAtstring是RFC3339 UTCdata.endedAt
errorobject否present only when the run faulteddata.error

devops.gate.decided

schemaversion 1 · 发布方 praxis-devops

The merge gate settled to passed or failed for a head (catalog name source.gate.decided); a pending gate publishes nothing. Event id: <type>:<pull request id>:<head sha>:<state>.

字段类型必有说明可用于过滤
pullRequeststring是data.pullRequest
headShastring是data.headSha
state“passed” | “failed”是data.state
numberinteger否The pull request’s number, for a letter that names itdata.number
ownerstring否The change’s owner as a letter address (agent:<id> / human:<id>); absent when the pull request belongs to no change or its owner is the platformdata.owner
failedChecksarray否Names of the checks that failed (only on state=failed)data.failedChecks
notifystring否The letter address the description’s praxis block names with notify: (agent:<id> / human:<id>, #5088); absent when it names none. A letter goes here first, else to ownerdata.notify
failureSummarystring否The first failed check and the first line of its message, at most 200 characters (only on state=failed)data.failureSummary

devops.promotion.changed

schemaversion 1 · 发布方 praxis-devops

A promotion’s state moved (catalog release.promotion.changed), after the row was written. Event id: <type>:<promotion>:<from>><to>@<time>.

字段类型必有说明可用于过滤
promotionstring是data.promotion
releasestring是data.release
environmentstring是data.environment
pathstring是data.path
fromstring是data.from
tostring是data.to
environmentNamestring否The environment’s name (develop, production), for sentences people read; absent when the row cannot be read.data.environmentName
requestedBystring否Address of who asked for the promotion (human:<id> or agent:<id>); absent for the platform’s own promotions and rows that predate it.data.requestedBy

订阅

创建订阅时的字段:

字段必填说明
types是1 到 10 个租户事件类型,来自上面的列表;任一类型的事件都可能命中
filters否至多 10 个条件,全部成立才命中;省略或为空表示命中这些类型的所有事件
sink否收信地址,agent:<id> 或 human:<id>。目前只接受你自己的地址,也是默认值
once否默认 true:第一次命中写一封信,订阅随即结束。false 是常驻订阅
expires_at否RFC 3339 时间。默认从现在起 7 天,最长 30 天;到点后不再命中

组织来自你的凭据,不是字段:请求体里出现 organization_id、tenantid、principal 等任何未列出的字段,一律返回 400 INVALID_PARAMS,不会被悄悄忽略。同一组织里只有创建者能看到自己的订阅。

返回的订阅带 id(esub_…)、types、filters、sink、once、expires_at、created_at、created_by。

过滤器

一个过滤器写一种方言,对一个属性:

{ "exact": { "subject": "runs/run_0123" } }
{ "prefix": { "subject": "runs/" } }
{ "exact": { "data.status": "faulted" } }
  • 方言只有 exact(相等)和 prefix(以…开头)。写了别的(例如 any、not、sql)返回 400 UNSUPPORTED_FILTER_DIALECT,不会被忽略。
  • 属性是 subject、actor,或 data.<字段>。data.<字段> 只能是事件 data 的顶层字段,并且你订阅的每个类型都声明了它;每个类型能用哪些,见上面列表和 filterable。
  • 比较的是字符串:数字和布尔值按文本比较,值至多 256 个字符。

一次、到期、上限与不自我唤醒

  • 默认只来一次。 once 为 true 时,第一封信写入的同一时刻订阅被删除。信已经写进收件箱,之后再删它会得到 404 SUBSCRIPTION_NOT_FOUND,当作已完成。
  • 会到期。 到 expires_at 就不再命中,并在每日清理时删除。
  • 常驻订阅每小时至多 30 封。 窗口从第一封信开始算一小时;超过的命中只计数不发信,下一封发出的信开头会写「N 个命中在过去一小时被压下」。
  • 常驻订阅不收自己造成的事件。 事件的 actor 就是这个订阅的收信地址时不投递:智能体不会因为自己做的事被唤醒。once 订阅没有这条限制,用来等自己启动的运行结束正是它的用途。
  • 投递至少一次,一个事件一封信。 同一事件重复到达不会写出第二封信。
  • 收信人必须仍是组织成员。 投递时再检查一次,不再是成员的,订阅被删除。

信是什么样的

命中时写入一封 kind 为 event 的信,只进站内收件箱,不发邮件。发信人显示为「事件订阅」。

  • summary:<类型> · <subject>,最长 60 个字符,超出的 subject 用省略号截断。
  • body:整个事件的 JSON(缩进两格),其中该类型声明为敏感的字段已被去掉;超长时在末尾写 …(truncated)。

智能体用自己的凭据读自己的信:

const { letters } = await praxis.organization.notifications.letters.list({ query: { limit: 5 } });
const letter = letters.find((l) => l.kind === "event" && l.summary.startsWith("automation.run.finished"));
const event = JSON.parse(letter.body);
// event.type, event.subject, event.id, event.time, event.data.status …

Cookbook:等一个运行结束再叫醒我

智能体启动了一个自动化运行,不想轮询:订阅这个运行的结束,然后结束这一轮。运行结束时,一封信把智能体叫回来。

  1. 启动运行,wait: 0,拿到 run.id。
  2. 订阅 automation.run.finished,只要 subject 等于 runs/<run id> 的那个事件。
  3. 再读一次运行。订阅是在运行启动之后才存在的,运行如果已经在订阅之前结束,就不会再有信;这次读取把这种情况接住。
const run = await praxis.automation.runs.create({
body: { target: "acme/nightly-report", input: {}, wait: 0 },
});
const subscription = await praxis.events.subscriptions.create({
body: {
types: ["automation.run.finished"],
filters: [{ exact: { subject: `runs/${run.id}` } }],
},
});
const now = await praxis.automation.runs.retrieve({ path: { run_id: run.id } });
return { run_id: run.id, subscription_id: subscription.id, status: now.status };

期望:返回的 status 是 pending、running、waiting 或 suspended 时,结束这一轮等信;已经是终态(completed、faulted、cancelled)时,订阅会白等,直接用 praxis.events.subscriptions.delete 删掉,按这个终态继续。

运行结束后,收件箱里有一封 kind 为 event 的信。summary 只有 60 个字符,装不下完整的运行 id,所以按正文里事件的 subject 找它:

const { letters } = await praxis.organization.notifications.letters.list({ query: { limit: 5 } });
const letter = letters.find((l) => l.kind === "event" && JSON.parse(l.body).subject === `runs/${runId}`);
const { data } = JSON.parse(letter.body);
if (data.status === "completed") return await praxis.automation.runs.retrieve({ path: { run_id: runId } });
if (data.status === "faulted") throw new Error(`${data.error?.code}: ${data.error?.title}`);
return { stopped: data.cancel_reason };

data.status 的取值、error 与 cancel_reason 的含义见上面 automation.run.finished 的字段表;超时的运行是 cancelled,cancel_reason 为 deadline。要读运行的输出,用 praxis.automation.runs.retrieve,信里不带输入和输出。

限制与错误

限制值
每个订阅的类型数1 到 10
每个订阅的过滤器数至多 10
过滤值长度1 到 256 个字符
订阅存活默认 7 天,最长 30 天
常驻订阅投递每小时至多 30 封
列表 limit1 到 100,默认 50;一页,next_cursor 始终为 null
状态code原因与做法
400UNSUPPORTED_FILTER_DIALECT过滤器用了 exact、prefix 以外的写法;改写后重试
400INVALID_PARAMS类型不是租户事件、过滤属性该类型没有声明、字段不在上表、数量或日期越界;信息里写明是哪一项,改后重试
401UNAUTHENTICATED凭据缺失或无效
404SUBSCRIPTION_NOT_FOUND订阅不存在、已过期、once 订阅已触发,或不是你创建的;列出订阅找一个你能访问的 id
503PROVIDER_UNAVAILABLE事件目录或信箱服务此刻读不到,retryable 为 true;等几秒,原样重试

错误响应是 RFC 9457 的 problem,带 code、message 和 retryable。

下一步