# 查询自动化任务运行事件

查询一条自动化任务运行记录的生命周期事件。

```text
GET https://moi.matrixorigin.cn/newmoi/workspaces/{workspace_id}/agent-automation-runs/{run_id}/events
```

## 调用前准备

准备运行记录 ID、有目标工作区访问权限的个人访问令牌和目标工作区 ID。

下方示例使用：

- `$AI_STUDIO_API_KEY`：实际个人访问令牌，通过 `X-API-Key` Header 传递。
- `$WORKSPACE_ID`：目标工作区 ID，通过 `X-Workspace-ID` Header 传递，同时作为路径中的 `workspace_id`。
- `$RUN_ID`：要查询事件的运行记录 ID，作为路径中的 `run_id`。

## 路径参数

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `workspace_id` | string | 是 | 工作区 ID。 |
| `run_id` | string | 是 | 运行记录 ID。 |

## 查询参数

未提供 `limit` 或 `offset` 时，服务默认返回 50 条记录并从第 0 条开始；`limit` 最大为 200。

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `limit` | integer | 否 | 返回数量。 |
| `offset` | integer | 否 | 返回列表的起始位置。 |

## 请求示例

```bash
curl "https://moi.matrixorigin.cn/newmoi/workspaces/$WORKSPACE_ID/agent-automation-runs/$RUN_ID/events?limit=50&offset=0" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID"
```

## 成功响应

成功时返回 `200`。`data.items` 为事件列表，`data.total` 为匹配总数。

```json
{
  "code": 0,
  "data": {
    "items": [
      {
        "id": "event_01",
        "workspace_id": "ws_01",
        "run_id": "run_01",
        "automation_task_id": "task_01",
        "type": "status_changed",
        "status": "running",
        "message": "运行已开始。",
        "created_at": "2026-08-18T04:00:00Z"
      }
    ],
    "total": 1,
    "limit": 50,
    "offset": 0
  }
}
```

响应字段如下。

本文中，字段路径中的 `[]` 表示数组中的每一项。例如，`items[].name` 表示 `items` 数组中每一项的 `name` 字段。

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | integer | 成功时为 `0`。 |
| `data.items` | array | 运行生命周期事件。 |
| `data.items[].id` | string | 事件、工作区、运行和任务标识。 |
| `data.items[].workspace_id` | string | 事件、工作区、运行和任务标识。 |
| `data.items[].run_id` | string | 事件、工作区、运行和任务标识。 |
| `data.items[].automation_task_id` | string | 事件、工作区、运行和任务标识。 |
| `data.items[].type` | string | 事件类型、可选状态和说明。 |
| `data.items[].status` | string | 事件类型、可选状态和说明。 |
| `data.items[].message` | string | 事件类型、可选状态和说明。 |
| `data.items[].created_at` | string | 事件创建时间，使用 RFC 3339 格式。 |
| `data.total` | integer | 匹配总数、当前页大小和偏移量。 |
| `data.limit` | integer | 匹配总数、当前页大小和偏移量。 |
| `data.offset` | integer | 匹配总数、当前页大小和偏移量。 |

## 错误响应

```json
{
  "code": 3,
  "message": "<错误信息>"
}
```

### 常见 HTTP 错误

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP 状态码
  - 错误代码
  - 常见原因
  - 建议操作
* - `400`
  - `2`（`INVALID_ARGUMENT`）
  - 路径参数无效。
  - 检查工作区和运行 ID。
* - `401`
  - `6`（`UNAUTHENTICATED`）
  - 缺少有效身份凭据。
  - 检查 API Key。
* - `404`
  - `3`（`NOT_FOUND`）
  - 运行记录不存在。
  - 先查询运行列表。
* - `503`
  - `15`（`UNAVAILABLE`）
  - 自动化任务服务不可用。
  - 稍后重试。
```

## 后续操作

事件中的状态用于辅助诊断；最终输出请[查询自动化任务运行结果](get-run-result.md)。
