> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/zh/apis/annotations-and-workflow-api.md).

# 标注与工作流 API

Unitlab 标注与工作流 API：通过 HTTP 读取工作流阶段与任务、执行任务操作、读取和保存标注、导入标注文件，并标记基准测试。

所有路由都位于 `https://api.unitlab.ai/api/sdk/` 下。

## 阶段与任务 <a href="#stages-and-tasks" id="stages-and-tasks"></a>

| 方法与路由                                              | 请求体                                                              | 权限                                    |
| -------------------------------------------------- | ---------------------------------------------------------------- | ------------------------------------- |
| `GET projects/{id}/workflow/stages/`               |                                                                  | Assign project members 或 Manage data  |
| `GET projects/{id}/workflow/stages/{stage}/tasks/` | 查询参数：`include_unavailable=1`、`limit`、`offset`                    | 同上                                    |
| `GET workflow-tasks/{task}/`                       |                                                                  | View labeling interface               |
| `POST workflow-tasks/{task}/claim/`                |                                                                  | View labeling interface、Create labels |
| `POST workflow-tasks/{task}/assign/`               | `{"user_id"}`                                                    | Assign project members 或 Manage data  |
| `POST workflow-tasks/{task}/release/`              |                                                                  | 同上                                    |
| `POST workflow-tasks/{task}/priority/`             | `{"priority": 5}`                                                | 同上                                    |
| `POST workflow-tasks/{task}/move/`                 | `{"stage_id", "reason", "expected_stage_id", "idempotency_key"}` | 同上                                    |
| `POST workflow-tasks/{task}/actions/`              | 见下文                                                              | Create labels、Manage data             |
| `GET workflow-tasks/{task}/timeline/`              |                                                                  | View labeling interface、Manage data   |
| `POST projects/{id}/workflow/tasks/assign/`        | `{"task_ids", "user_id"}`，最多 500 个                               | Assign project members 或 Manage data  |
| `POST projects/{id}/workflow/tasks/move/`          | `{"task_ids", "stage_id", "reason", "dry_run"}`，最多 500 个         | 同上                                    |

`{stage}` 是阶段列表中的阶段 ID，例如 `annotate`。

### 任务操作 <a href="#task-actions" id="task-actions"></a>

```bash
curl -s -X POST "https://api.unitlab.ai/api/sdk/workflow-tasks/$TASK_ID/actions/" \
  -H "Authorization: Api-Key $UNITLAB_API_KEY" -H "Content-Type: application/json" \
  -d '{"action": "reject", "rejection_reason": "Box misses the gripper", "comment": "Frames 40 to 55",
       "expected_stage_id": "review", "idempotency_key": "7b0c6f1e-2f4e-4a51-9e0b-1f3d2c9a8e77"}'
```

| `action`   | 效果     |
| ---------- | ------ |
| `complete` | 沿前进边提交 |
| `approve`  | 通过审核任务 |
| `reject`   | 沿驳回边退回 |
| `skip`     | 沿跳过边流转 |

如果任务在你读取之后已移动，`expected_stage_id` 会拒绝该操作。重复的 `idempotency_key` 不会被执行两次。每个预期操作使用一个新的 UUID，只在重试同一操作时复用它。

## 任务标注 <a href="#task-annotations" id="task-annotations"></a>

| 方法与路由                                                        | 请求体 / 响应                                                                                                   |
| ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| `GET workflow-tasks/{task}/annotations/?only_keyframes=true` | 包含 `document`（UUEF）的行，较大的文档则返回 `document_url`                                                              |
| `PUT workflow-tasks/{task}/annotations/`                     | `{"datasource_id", "format": "uuef" \| "geojson" \| "model", "document"}`                                  |
| `POST workflow-tasks/{task}/annotations/uploads/`            | 超过 64 MiB 的文档使用 `{"datasource_id", "format", "size"}`；将 JSON PUT 到 `upload_url`，再用 `document_upload_id` 保存 |
| `POST workflow-tasks/{task}/agent-result/`                   | `{"run_id", "status": "complete"}` 或 `{"run_id", "status": "failed", "error"}`                             |
| `POST workflow-tasks/{task}/model-result/`                   | `{"run_id", "status", "result" or "results", "model_version"}`                                             |

保存会替换整个条目的标注，并且只在任务处于 Agent 阶段时有效。规则和 LiDAR 变体见：[任务标注与 Agent Webhook](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/task-annotations-and-agent-webhooks.md)。文档结构见：[标注 JSON 参考](https://docs.unitlab.ai/api-sdk-cli/zh/apis/annotation-json-reference.md)。

## 标注导入 <a href="#annotation-imports" id="annotation-imports"></a>

| 方法与路由                                                    | 作用                                           |
| -------------------------------------------------------- | -------------------------------------------- |
| `POST projects/{id}/annotation-imports/`                 | 上传标注文件并获取预览                                  |
| `POST projects/{id}/annotation-imports/{batch}/preview/` | 使用新选项重新预览                                    |
| `POST projects/{id}/annotation-imports/{batch}/commit/`  | 保存：`{"token", "decisions", "allow_partial"}` |
| `GET projects/{id}/annotation-imports/sources/`          | 已保存的标注来源                                     |
| `GET projects/{id}/annotation-imports/media/?search=`    | 用于映射的候选媒体                                    |

```bash
curl -s -X POST "https://api.unitlab.ai/api/sdk/projects/$PROJECT_ID/annotation-imports/" \
  -H "Authorization: Api-Key $UNITLAB_API_KEY" \
  -F "annotation_file=@detector-v3.json;type=application/json" \
  --form-string "source_name=detector-v3" \
  --form-string "track_profile=none" \
  --form-string 'options={}'
```

响应即预览结果，包含 `batch_id`、`counts`、`issues`、`annotations` 和一个 `token`。使用该 token 提交。重复提交会返回已保存的报告，不会重复创建标注。需要 **Manage data** 和 **Create labels** 权限，并且密钥须由工作区成员创建。格式和选项见：[标注导入](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/annotation-imports.md)。

## 基准测试 <a href="#benchmarks" id="benchmarks"></a>

```bash
curl -s -X POST "https://api.unitlab.ai/api/sdk/projects/$PROJECT_ID/benchmarks/" \
  -H "Authorization: Api-Key $UNITLAB_API_KEY" -H "Content-Type: application/json" \
  -d '{"datasource_ids": ["DATASOURCE_ID"], "when_ready": true}'
```

每个请求最多发送 100 个 ID。响应统计 `created`、`existing`、`deferred` 和 `skipped_by_reason`。参见 [基准测试](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/benchmarks.md)。

## 相关内容 <a href="#related" id="related"></a>

[SDK：工作流阶段与任务](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/workflow-stages-and-tasks.md) · [错误、限流与分页](https://docs.unitlab.ai/api-sdk-cli/zh/apis/errors-rate-limits-and-pagination.md)
