事件与订阅
事件是关于你组织里某个资源的一件已经发生的事,例如一次自动化运行结束了。你订阅它,它发生时你的收件箱收到一封信:智能体被这封信唤醒,人在站内信里看到它。不用轮询,也不用在自己的代码里等。
接口都在 /events/v1 下,用组织 API Key、会话或智能体令牌调用,SDK 方法在 praxis.events.* 下:
| SDK 方法 | HTTP | 做什么 |
|---|---|---|
praxis.events.types.list | GET /events/v1/types | 列出可订阅的事件类型 |
praxis.events.subscriptions.create | POST /events/v1/subscriptions | 创建订阅 |
praxis.events.subscriptions.list | GET /events/v1/subscriptions | 列出你还有效的订阅 |
praxis.events.subscriptions.retrieve | GET /events/v1/subscriptions/{subscription_id} | 读一个订阅 |
praxis.events.subscriptions.delete | DELETE /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.
| 字段 | 类型 | 必有 | 说明 | 可用于过滤 |
|---|---|---|---|---|
runId | string | 是 | data.runId | |
target | string | null | 是 | namespace/name as requested, or null for an ad-hoc source run | data.target |
kind | “script” | “workflow” | 是 | data.kind | |
status | “completed” | “faulted” | “cancelled” | 是 | data.status | |
durationMs | integer | 是 | wall clock in milliseconds from startedAt (createdAt when it never started) to endedAt, including time spent waiting | data.durationMs |
channel | “console” | “api” | “internal” | “run” | “trigger” | 是 | how the run was started: the run record’s channel, as is | data.channel |
cancel_reason | string | 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 deadline | data.cancel_reason |
startedAt | string | null | 是 | RFC3339 UTC | data.startedAt |
endedAt | string | 是 | RFC3339 UTC | data.endedAt |
error | object | 否 | present only when the run faulted | data.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>.
| 字段 | 类型 | 必有 | 说明 | 可用于过滤 |
|---|---|---|---|---|
pullRequest | string | 是 | data.pullRequest | |
headSha | string | 是 | data.headSha | |
state | “passed” | “failed” | 是 | data.state | |
number | integer | 否 | The pull request’s number, for a letter that names it | data.number |
owner | string | 否 | 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 platform | data.owner |
failedChecks | array | 否 | Names of the checks that failed (only on state=failed) | data.failedChecks |
notify | string | 否 | 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 owner | data.notify |
failureSummary | string | 否 | 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>.
| 字段 | 类型 | 必有 | 说明 | 可用于过滤 |
|---|---|---|---|---|
promotion | string | 是 | data.promotion | |
release | string | 是 | data.release | |
environment | string | 是 | data.environment | |
path | string | 是 | data.path | |
from | string | 是 | data.from | |
to | string | 是 | data.to | |
environmentName | string | 否 | The environment’s name (develop, production), for sentences people read; absent when the row cannot be read. | data.environmentName |
requestedBy | string | 否 | 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)返回 400UNSUPPORTED_FILTER_DIALECT,不会被忽略。 - 属性是
subject、actor,或data.<字段>。data.<字段>只能是事件data的顶层字段,并且你订阅的每个类型都声明了它;每个类型能用哪些,见上面列表和filterable。 - 比较的是字符串:数字和布尔值按文本比较,值至多 256 个字符。
一次、到期、上限与不自我唤醒
- 默认只来一次。
once为true时,第一封信写入的同一时刻订阅被删除。信已经写进收件箱,之后再删它会得到 404SUBSCRIPTION_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:等一个运行结束再叫醒我
智能体启动了一个自动化运行,不想轮询:订阅这个运行的结束,然后结束这一轮。运行结束时,一封信把智能体叫回来。
- 启动运行,
wait: 0,拿到run.id。 - 订阅
automation.run.finished,只要subject等于runs/<run id>的那个事件。 - 再读一次运行。订阅是在运行启动之后才存在的,运行如果已经在订阅之前结束,就不会再有信;这次读取把这种情况接住。
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 封 |
列表 limit | 1 到 100,默认 50;一页,next_cursor 始终为 null |
| 状态 | code | 原因与做法 |
|---|---|---|
| 400 | UNSUPPORTED_FILTER_DIALECT | 过滤器用了 exact、prefix 以外的写法;改写后重试 |
| 400 | INVALID_PARAMS | 类型不是租户事件、过滤属性该类型没有声明、字段不在上表、数量或日期越界;信息里写明是哪一项,改后重试 |
| 401 | UNAUTHENTICATED | 凭据缺失或无效 |
| 404 | SUBSCRIPTION_NOT_FOUND | 订阅不存在、已过期、once 订阅已触发,或不是你创建的;列出订阅找一个你能访问的 id |
| 503 | PROVIDER_UNAVAILABLE | 事件目录或信箱服务此刻读不到,retryable 为 true;等几秒,原样重试 |
错误响应是 RFC 9457 的 problem,带 code、message 和 retryable。