写一篇稿子
用 API 为一个网店客服的场景写一篇两节的博客,每一步都附期望结果。
准备:组织 API Key(见API Key),放进 PRAXIS_API_KEY 环境变量;安装 curl 和 jq。可以设置 PRAXIS_API_BASE 指向其他环境,默认是 https://api.deepraxis.com。下面的企划标题带 docs-example 前缀和时间戳,重复运行不会撞上已有的 slug。
1. 建企划
企划保存稿子的目标:读者、读完得到什么、一句话信息。三项都必填。
export BASE="${PRAXIS_API_BASE:-https://api.deepraxis.com}"export TAG=$(date +%s)WORK_JSON=$(curl --fail-with-body -sS -X POST "$BASE/studio/v1/works" \ -H "Authorization: Bearer $PRAXIS_API_KEY" \ -H "Content-Type: application/json" \ -d "$(jq -n --arg title "docs-example-support-$TAG" '{title: $title, goal: {reader: "Shop owners who answer customer questions by hand", outcome: "Decide whether to put an AI agent on their support inbox", message: "An agent answers the routine questions so you can handle the exceptions."}}')")export WORK=$(printf '%s' "$WORK_JSON" | jq -er '.slug')printf '%s\n' "$WORK_JSON" | jq '{id, slug, revision, drafts}'期望:HTTP 201,revision 为 0,drafts 为空数组;slug 由标题生成,后面的调用都用它。同一个组织里 slug 已被占用时返回 409 SLUG_TAKEN。
2. 建文体稿
文体稿按一个文体写成,文体决定段落的种类。blog_article 的顶层种类是 section;先用 GET /studio/v1/genres/blog_article 读它允许的种类、属性和检查。outline 一次给出段落结构,key 是之后调用里的文体稿名字。
DRAFT_JSON=$(curl --fail-with-body -sS -X POST "$BASE/studio/v1/works/$WORK/drafts" \ -H "Authorization: Bearer $PRAXIS_API_KEY" \ -H "Content-Type: application/json" \ -d '{"genre": "blog_article", "key": "post", "outline": [{"kind": "section", "name": "Problem", "synopsis": "The same shipping question, asked nine times a day."}, {"kind": "section", "name": "Answer", "synopsis": "What an agent takes over and what stays with you."}]}')printf '%s\n' "$DRAFT_JSON" | jq '.value | {id, key, genre, revision, units: [.units[] | {number, kind, name, status}]}'期望:HTTP 201,答复是 {value, checks, next};value.units 里有两个段落,序号 1、2,status 都是 outlined。
3. 读待办
curl --fail-with-body -sS "$BASE/studio/v1/works/$WORK/drafts/post/status" \ -H "Authorization: Bearer $PRAXIS_API_KEY" | jq '{revision, todos: [.todos[] | {kind, number, text}]}'期望:HTTP 200,todos 里有 prop_missing(文体稿属性「标题」未填)、两条 body_missing(每个段落有意图没有正文)和一条 check_failed(全文字数不足)。每条待办的 call 字段给出该用的 SDK 方法。
4. 写段落正文
写整段正文要带刚读到的修订号,别人先改过会返回 409 REVISION_CONFLICT。
for N in 1 2; do REV=$(curl --fail-with-body -sS "$BASE/studio/v1/works/$WORK/units/$N" \ -H "Authorization: Bearer $PRAXIS_API_KEY" | jq -er '.revision') if [ "$N" = 1 ]; then TEXT='Last spring a customer asked the same shipping question for the ninth time in one day.
That is when we stopped answering by hand.'; else TEXT='An agent takes the routine questions: where is my order, what is your return window.
You keep the exceptions: damaged goods, refunds, anything a person should decide.'; fi curl --fail-with-body -sS -X PATCH "$BASE/studio/v1/works/$WORK/units/$N" \ -H "Authorization: Bearer $PRAXIS_API_KEY" \ -H "Content-Type: application/json" \ -d "$(jq -n --argjson rev "$REV" --arg body "$TEXT" '{expected_revision: $rev, body: $body}')" \ | jq '{unit: .value | {number, status, chars, revision}, checks: [.checks[] | {check, ok}]}'done期望:每次 HTTP 200,段落 status 变为 drafted,revision 加一;checks 里可能有 heading_present(成品小标题还没写),这是警告,不阻止后面的步骤。
5. 导出全文
curl --fail-with-body -sS "$BASE/studio/v1/works/$WORK/drafts/post/export?format=markdown" \ -H "Authorization: Bearer $PRAXIS_API_KEY" | jq -r '.content'期望:HTTP 200,content 是两个段落按序拼成的 Markdown,revision 是文体稿当前的修订号。format 必填:markdown 保留标题和块锚点,text 去掉全部标记。省略 format 返回 400 INVALID_PARAMS。
用 SDK 做同一件事
官方 @deepraxis/sdk 的方法在 praxis.studio.* 下。
import { Praxis } from '@deepraxis/sdk';
const praxis = new Praxis({ apiKey: process.env.PRAXIS_API_KEY!, baseUrl: process.env.PRAXIS_API_BASE ?? 'https://api.deepraxis.com',});
const tag = Date.now().toString(36);
// 1. 建企划const work = await praxis.studio.works.create({ body: { title: `docs-example-support-${tag}`, goal: { reader: 'Shop owners who answer customer questions by hand', outcome: 'Decide whether to put an AI agent on their support inbox', message: 'An agent answers the routine questions so you can handle the exceptions.', }, },});
// 2. 建文体稿const created = await praxis.studio.works.drafts.create({ path: { work: work.slug }, body: { genre: 'blog_article', key: 'post', outline: [ { kind: 'section', name: 'Problem', synopsis: 'The same shipping question, asked nine times a day.' }, { kind: 'section', name: 'Answer', synopsis: 'What an agent takes over and what stays with you.' }, ], },});console.log(created.value.units.map(u => `${u.number} ${u.name} ${u.status}`));
// 3. 读待办const status = await praxis.studio.works.drafts.status.list({ path: { work: work.slug, draft: 'post' } });console.log(status.todos.map(t => t.kind));
// 4. 带修订号写正文for (const [number, body] of [ ['1', 'Last spring a customer asked the same shipping question for the ninth time in one day.'], ['2', 'An agent takes the routine questions. You keep the exceptions.'],] as const) { const unit = await praxis.studio.works.units.retrieve({ path: { work: work.slug, unit: number } }); const written = await praxis.studio.works.units.update({ path: { work: work.slug, unit: number }, body: { expected_revision: unit.revision, body }, }); console.log(written.value.number, written.value.status, written.value.chars);}
// 5. 导出const exported = await praxis.studio.works.drafts.retrieveExport({ path: { work: work.slug, draft: 'post' }, query: { format: 'markdown' },});console.log(exported.content);清理
企划没有删除接口,归档后它不再出现在默认列表里,数据保留。
curl --fail-with-body -sS -X PATCH "$BASE/studio/v1/works/$WORK" \ -H "Authorization: Bearer $PRAXIS_API_KEY" \ -H "Content-Type: application/json" \ -d '{"update_mask": ["archived"], "archived": true}' | jq '.value | {slug, archived}'期望:HTTP 200,archived 为 true。用 GET /studio/v1/works?archived=true 列出已归档的企划;每一页只列已归档或未归档其中一种。