跳转到内容
EN

审批后执行动作并回读

经营中台不只看数据,还要动手:改价、上下架、调预算。这份配方定下动手的规矩:智能体不直接操作后台,只提动作申请;工作流先发审批单,你批准后由一个只做这件事的脚本执行,执行完再读一次后台确认真的改了,结果记进动作表。它是企业经营中台的第 ⑤ 层。

本页在 Develop 上实跑时,「后台」是同一个库里的 listings 表,动作是改一个商品的价格。换成你的后台时,只换动作脚本里「执行」和「回读」两步:在已登录的后台页面里调用后台自己的改价接口和查询接口,做法同同步配方。

前提

  • 已按同步配方建好库 DB,沿用那里的 api 函数。
  • 审批人的用户 ID(usr_…),记为 APPROVER。

1. 建动作表和替身后台

终端窗口
api /database/v1/table/$DB/tables --data '{"name":"actions","fields":[
{"name":"request_id","type":"string","required":true,"unique":true},
{"name":"sku","type":"string"},{"name":"field","type":"string"},
{"name":"before","type":"number"},{"name":"after","type":"number"},
{"name":"approved_by","type":"string"},{"name":"status","type":"string"},
{"name":"executed_at","type":"datetime","indexed":true}]}' | jq -e '.name == "actions"'
api /database/v1/table/$DB/tables --data '{"name":"listings","fields":[
{"name":"sku","type":"string","required":true,"unique":true},{"name":"price","type":"number","required":true}]}' | jq -e '.name == "listings"'
api /database/v1/table/$DB/rest/listings --data '{"sku":"RING-001","price":1299}'

actions.request_id 是唯一键:一份申请一个编号,同一编号只执行一次。

2. 写动作脚本

保存为 apply-price.ts。一个脚本只做一个动作,五步:查这份申请是否已执行 → 记改前的值 → 执行 → 回读 → 记入动作表。

type Input = {
database_id: string;
request_id: string; // 一份申请一个编号:同一编号只执行一次
sku: string;
price: number;
approved_by: string;
};
export default async (input: Input): Promise<{ status: string; before: number | null; after: number | null }> => {
const db = (table: string) => ({ database_id: input.database_id, table });
const rows = (r: unknown) => r as Array<Record<string, unknown>>;
// 1. 同一申请编号已经执行过,就直接返回上次的结果,不再改第二次。
const done = rows(await praxis.database.table.rest.retrieve({ path: db("actions"), query: { request_id: `eq.${input.request_id}` } }));
if (done.length > 0) return { status: `already ${done[0].status}`, before: done[0].before as number, after: done[0].after as number };
// 2. 记下改之前的值。
const read = async () => rows(await praxis.database.table.rest.retrieve({ path: db("listings"), query: { sku: `eq.${input.sku}` } }))[0];
const before = (await read())?.price as number | undefined;
if (before === undefined) throw new Error(`no listing ${input.sku}`);
// 3. 执行这一个动作。真实后台里这一步在已登录的页面里调用后台自己的改价接口;本例的后台是 listings 表。
await praxis.database.table.rest.update({ path: db("listings"), query: { sku: `eq.${input.sku}` }, body: { price: input.price } });
// 4. 回读:再读一次后台,确认真的变了。
const after = (await read())?.price as number;
const status = after === input.price ? "done" : "mismatch";
// 5. 记入动作表:谁批的、改前改后、回读结果。
await praxis.database.table.rest.create({
path: db("actions"),
body: { request_id: input.request_id, sku: input.sku, field: "price", before, after, approved_by: input.approved_by, status, executed_at: new Date().toISOString() },
});
if (status !== "done") throw new Error(`read-back ${after} != requested ${input.price}`);
return { status, before, after };
};

回读不一致时,脚本记一行 mismatch 并让运行失败,不当作成功。

3. 写审批工作流

保存为 approve-price.yaml。第一步发审批单并停下来等;批准才运行动作脚本,驳回或超时就结束。

document: { dsl: "1.0.3", namespace: ops-hub, name: approve-price, version: "1.0.0" }
do:
- ask:
call: praxis/approval
with:
title: '${ "改价:" + .sku + " → " + (.price | tostring) }'
body: '${ "经营分析员的理由:" + .reason }'
context: '${ { sku: .sku, price: .price, request_id: .request_id } }'
assignees:
users: '${ [ .approver ] }'
allow_self_approval: true
due: P1D
on_timeout: { action: reject }
- decide:
switch:
- approved:
when: '${ .decision == "approve" }'
then: apply
- otherwise:
then: end
- apply:
run:
script:
namespace: ops-hub
name: apply-price
version: "1.0.0"
input:
database_id: '${ $workflow.input.database_id }'
request_id: '${ $workflow.input.request_id }'
sku: '${ $workflow.input.sku }'
price: '${ $workflow.input.price }'
approved_by: '${ .decided_by.id }'

allow_self_approval: true 只是为了让你一个人就能走完本页;正式使用时去掉它,让发起运行的人不能批准自己的申请。审批单默认 1 天内没人处理就按驳回结束(on_timeout)。

4. 发布并发起一次申请

终端窗口
api /automation/v1/scripts --data "$(jq -n --rawfile s apply-price.ts '{namespace:"ops-hub",name:"apply-price",draft:{source:$s}}')" > /dev/null
REV=$(api /automation/v1/scripts/ops-hub/apply-price | jq -er .draft.revision)
api /automation/v1/scripts/ops-hub/apply-price/versions --data "{\"version\":\"1.0.0\",\"draft\":{\"revision\":\"$REV\"}}" | jq -e .version
api /automation/v1/workflows --data "$(jq -n --rawfile s approve-price.yaml '{namespace:"ops-hub",name:"approve-price",draft:{source:$s}}')" > /dev/null
REV=$(api /automation/v1/workflows/ops-hub/approve-price | jq -er .draft.revision)
api /automation/v1/workflows/ops-hub/approve-price/versions --data "{\"version\":\"1.0.0\",\"draft\":{\"revision\":\"$REV\"}}" | jq -e .version
RUN=$(api /automation/v1/runs --data "$(jq -n --arg db "$DB" --arg who "$APPROVER" '{
target:"ops-hub/approve-price@1.0.0", deadline_seconds:172800,
input:{database_id:$db, request_id:"req-ring-001-20261125", sku:"RING-001", price:1199,
reason:"近 7 天转化率低于同类商品,建议降价 100 试一周。", approver:$who}}')" | jq -er .id)
api /automation/v1/runs/$RUN | jq .status

期望:status 为 waiting,审批人在控制台的待办或审批任务列表里看到「改价:RING-001 → 1199」。分析员发现问题时,就用同样的输入发起这次运行。

5. 批准,然后核对

审批人在控制台点「同意」,或用 API 提交:

终端窗口
TASK=$(api /automation/v1/human-tasks | jq -er '[.data[] | select(.status == "open")][0].id')
api /automation/v1/human-tasks/$TASK/respond --data '{"action_id":"approve","comment":"同意,先试一周"}' | jq .status
until [[ "$(api /automation/v1/runs/$RUN | jq -r .status)" =~ ^(completed|faulted|cancelled)$ ]]; do sleep 5; done
api /automation/v1/runs/$RUN | jq '{status, output}'
api "/database/v1/table/$DB/rest/actions?select=request_id,before,after,approved_by,status"

期望:运行 completed,输出 {"status":"done","before":1299,"after":1199};动作表有一行,记着批准人和改前改后的值。

常见错误

现象原因与处理
同一申请被执行两次动作脚本没先查 request_id,或动作表没把 request_id 设为唯一键
脚本报 rest.list does not exist读行的方法是 praxis.database.table.rest.retrieve
审批单发给了自己却批不了默认不能批准自己发起的申请;本页用 allow_self_approval: true 演示,正式使用时去掉
运行一直 waiting审批没人处理。超时按 on_timeout 结束;需要更长时间就调大 due,并让 deadline_seconds 比它更长
改草稿返回「需要 If-Match」更新脚本或工作流草稿要在请求体 draft.revision 带上读到的修订;发布新版本后,把触发器或工作流里的版本号改过去

实跑记录

2026-11-25,Develop。替身后台为 listings 表,商品 RING-001 原价 1299。

项目结果
发起申请运行进入 waiting,审批单「改价:RING-001 → 1199」出现在审批人的待办
批准运行 completed,输出 done,before 1299、after 1199;listings 回读为 1199;actions 一行,approved_by 为审批人
同一申请再发一次(改成 999)并批准输出 already done,价格仍为 1199,动作表仍是一行
新申请被驳回运行 completed,decision: reject,没有执行动作,价格与动作表都没变
记录库 db_53906c278792437a,脚本 ops-hub/apply-price@1.0.0,工作流 ops-hub/approve-price@1.0.0

下一步

每加一个动作,就写一个只做这件事的动作脚本,和一个发审批单的工作流;不要写一个什么都能改的通用脚本。整体方案见企业经营中台,真实落地见抖店经营中台。