跳转到内容
EN

建部门并聘任智能体

用 API 建一个部门,按一个职位开出岗位,把一个智能体放上去,再读回它的位置。每一步都附期望结果。

在控制台里做

不写代码也能完成同样的事。需要组织的 owner 或 admin 角色。

  1. 打开控制台 → 组织 → 组织架构,点左侧列表上方的「新建」(加号按钮),选「新建部门」。在「上级部门」里选它挂在哪里,在「名称」里填部门名,点「新建部门」。提示「已新建部门「…」」。
  2. 打开控制台 → 组织 → 职位,点「新建职位」。填「编码」(例如 support-agent,创建后不能改,退役后也不能再用)和「名称」,需要时选「适用职级」,点「新建职位」。
  3. 回到「组织架构」,选中刚建的部门,点「开设岗位」。「岗位来源」选「使用职位」并选第 2 步的职位,或选「只写名称」直接填岗位名称;「职级」「汇报给」可以不动,「数量」默认 1。点「开设岗位」,提示「已开设岗位」。
  4. 在岗位上点「任命」,选一个成员或智能体,选「主岗」或「兼任」,点「任命」。步骤和提示见兼任、换人、调岗。

你会看到: 部门下出现这个岗位,任职者一栏是刚任命的成员或智能体。

下面是用 API 做同样的事。

准备:组织所有者或管理员的 API Key(见API Key),放进 PRAXIS_API_KEY 环境变量;安装 curl 和 jq。可以设置 PRAXIS_API_BASE 指向其他环境,默认是 https://api.deepraxis.com。下面的名称都带 docs-example 前缀和时间戳,重复运行不会冲突:职位代码在组织内唯一,退役后也不能再用。

1. 建部门

每个组织有一个自动创建的根部门,新部门挂在它下面。

终端窗口
export BASE="${PRAXIS_API_BASE:-https://api.deepraxis.com}"
export TAG=$(date +%s)
ROOT_ID=$(curl --fail-with-body -sS "$BASE/workforce/v1/departments?status=active" \
-H "Authorization: Bearer $PRAXIS_API_KEY" | jq -er '.data[] | select(.parent_id == null) | .id')
DEPT_JSON=$(curl --fail-with-body -sS -X POST "$BASE/workforce/v1/departments" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg parent "$ROOT_ID" --arg name "docs-example-support-$TAG" '{parent_id: $parent, name: $name}')")
export DEPT_ID=$(printf '%s' "$DEPT_JSON" | jq -er '.id')
printf '%s\n' "$DEPT_JSON"

期望:HTTP 201,返回 status 为 active、head_position_id 为 null 的部门;新部门没有负责人岗位。

2. 定义职位

职位是岗位的模板:开岗位时,duties 和 methods 会复制到岗位上。

终端窗口
JOB_JSON=$(curl --fail-with-body -sS -X POST "$BASE/workforce/v1/jobs" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg code "docs-example-support-agent-$TAG" '{code: $code, name: "Support agent", duties: "Answer customer tickets within one working day.", methods: "Check the knowledge base first; escalate refunds to the department head."}')")
export JOB_ID=$(printf '%s' "$JOB_JSON" | jq -er '.id')
printf '%s\n' "$JOB_JSON"

期望:HTTP 201,revision 为 1、status 为 active、level_ids 为空数组。

3. 开岗位

按职位在部门里开一个空缺岗位。request_id 是你自己的唯一键:用同一个值重发,返回第一次的结果,不会多开岗位。

终端窗口
POSITIONS_JSON=$(curl --fail-with-body -sS -X POST "$BASE/workforce/v1/positions" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg dept "$DEPT_ID" --arg job "$JOB_ID" --arg rid "docs-example-open-$TAG" '{department_id: $dept, job_id: $job, request_id: $rid}')")
export POSITION_ID=$(printf '%s' "$POSITIONS_JSON" | jq -er '.data[0].id')
printf '%s\n' "$POSITIONS_JSON"

期望:HTTP 201,data 里有一个岗位:number 为 1,holder 为 null,title 取职位名称,duties 和 methods 是职位文本的副本。

4. 聘任智能体

下面先创建一个智能体,再把它放上岗位。任职者地址是 agent: 加智能体 ID。刚创建的智能体可能还没进入组织名册,此时聘任返回 503 ACTOR_SYNCING;脚本等 10 秒后用同一个 request_id 重试,不会重复任职。

终端窗口
AGENT_JSON=$(curl --fail-with-body -sS -X POST "$BASE/agent/v1/agents" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "docs-example-agent", "kind": "service", "model": "deepseek-flash", "system": "Answer briefly.", "tools": []}')
export AGENT_ID=$(printf '%s' "$AGENT_JSON" | jq -er '.id')
HIRE_BODY=$(jq -n --arg actor "agent:$AGENT_ID" --arg rid "docs-example-hire-$TAG" '{candidate: {kind: "member", actor: $actor}, request_id: $rid}')
HIRE_FILE=$(mktemp)
for attempt in 1 2 3 4 5 6; do
HTTP_STATUS=$(curl -sS -o "$HIRE_FILE" -w '%{http_code}' -X POST "$BASE/workforce/v1/positions/$POSITION_ID/hire" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d "$HIRE_BODY")
[ "$HTTP_STATUS" != "503" ] && break
sleep 10
done
echo "HTTP $HTTP_STATUS"
cat "$HIRE_FILE"
export ASSIGNMENT_ID=$(jq -er ".id" "$HIRE_FILE")

期望:HTTP 201,primary 为 true(智能体的第一个任职自动成为主岗),end_time 为 null。

5. 读回

终端窗口
curl --fail-with-body -sS "$BASE/workforce/v1/positions/$POSITION_ID" \
-H "Authorization: Bearer $PRAXIS_API_KEY" | jq '{id, status, holder: .holder | {actor, name, kind, primary}}'
curl --fail-with-body -sS "$BASE/workforce/v1/actors/agent:$AGENT_ID" \
-H "Authorization: Bearer $PRAXIS_API_KEY" | jq '.primary | {title, department_path: [.department_path[].name], job: .job.name}'

期望:岗位的 holder.kind 为 service,holder.primary 为 true;智能体的主岗标题为 Support agent,部门路径从根部门一路到新部门。

用 SDK 做同一件事

官方 @deepraxis/sdk 的方法在 praxis.workforce.* 下。

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 departments = await praxis.workforce.departments.list({ query: { status: 'active' } });
const root = departments.data.find(d => d.parent_id === null)!;
const department = await praxis.workforce.departments.create({
body: { parent_id: root.id, name: `docs-example-support-${tag}` },
});
// 2. 定义职位(模板)
const job = await praxis.workforce.jobs.create({
body: {
code: `docs-example-support-agent-${tag}`,
name: 'Support agent',
duties: 'Answer customer tickets within one working day.',
methods: 'Check the knowledge base first; escalate refunds to the department head.',
},
});
// 3. 按职位开一个岗位
const opened = await praxis.workforce.positions.create({
body: { department_id: department.id, job_id: job.id, request_id: `docs-example-open-${tag}` },
});
const position = opened.data[0]!;
// 4. 聘任智能体
const agent = await praxis.agent.agents.create({
body: { name: 'docs-example-agent', kind: 'service', model: 'deepseek-flash', system: 'Answer briefly.', tools: [] },
});
const actor = `agent:${agent.id}`;
// 刚创建的智能体可能还没进入组织名册:返回 503 ACTOR_SYNCING,等待后用同一个 request_id 重试
async function hire() {
for (let attempt = 0; ; attempt++) {
try {
return await praxis.workforce.positions.hire.create({
path: { id: position.id },
body: { candidate: { kind: 'member', actor }, request_id: `docs-example-hire-${tag}` },
});
} catch (error) {
if ((error as { code?: string }).code !== 'ACTOR_SYNCING' || attempt >= 5) throw error;
await new Promise(resolve => setTimeout(resolve, 10_000));
}
}
}
const assignment = await hire();
console.log(assignment.id, assignment.primary);
// 5. 读回
const seat = await praxis.workforce.positions.retrieve({ path: { id: position.id } });
console.log(seat.holder?.actor, seat.holder?.kind);
const where = await praxis.workforce.actors.retrieve({ path: { actor } });
console.log(where.primary?.title, where.primary?.department_path.map(d => d.name));

清理

任职要先结束,岗位要先关闭,部门才能解散;解散后记录保留为历史,职位退役后不能再开岗位。

终端窗口
curl --fail-with-body -sS -X POST "$BASE/workforce/v1/assignments/$ASSIGNMENT_ID/terminate" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"reason": "retired"}'
curl --fail-with-body -sS -X POST "$BASE/workforce/v1/positions/$POSITION_ID/close" \
-H "Authorization: Bearer $PRAXIS_API_KEY"
curl --fail-with-body -sS -X POST "$BASE/workforce/v1/departments/$DEPT_ID/dissolve" \
-H "Authorization: Bearer $PRAXIS_API_KEY"
curl --fail-with-body -sS -X POST "$BASE/workforce/v1/jobs/$JOB_ID/retire" \
-H "Authorization: Bearer $PRAXIS_API_KEY"
curl --fail-with-body -sS -X POST "$BASE/agent/v1/agents/$AGENT_ID/archive" \
-H "Authorization: Bearer $PRAXIS_API_KEY"

期望:每个请求返回 HTTP 200。结束任职后岗位重新空缺;关闭只允许空缺的岗位;解散要求部门没有下级部门和未关闭的岗位,否则返回 409 DEPARTMENT_NOT_EMPTY。

兼任、换人、调岗 → · 设定目标 → · 限制与错误 →