# Automation

## 列出自动化项目

`GET /automation/projects`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "列出自动化项目",
  "description": "保存在启动器中的每个项目及其块和运行设置。这些与桌面 **Automation** 部分编辑的项目相同。",
  "responses": {
    "200": {
      "description": "项目清单。",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationProject"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## 创建项目

`POST /automation/projects`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "创建项目",
  "requestBody": {
    "required": false,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "description": "空时使用 `Untitled`。"
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "创建项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## 获取项目

`GET /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "获取项目",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "项目详情。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 替换项目

`PUT /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "替换项目",
  "description": "完全替换项目。请发送 `GET` 返回的对象。路径中的 ID 优先于请求体中的 ID。项目不存在时返回 `404`；如需创建项目，请使用 `POST /automation/projects`。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/AutomationProject"
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "已保存的项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "400": {
      "description": "项目对象无效。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 删除项目

`DELETE /automation/projects/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "删除项目",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "已删除。",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "deleted": {
                "type": "boolean",
                "example": true
              },
              "id": {
                "type": "string",
                "format": "uuid"
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 重复项目

`POST /automation/projects/{id}/duplicate`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "重复项目",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "重复的项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 导出项目包

`GET /automation/projects/{id}/export`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "导出项目包",
  "description": "标记为秘密的项目的参数在退出时被清空，因此可以在不带密码的情况下传递包。进口方再次索要。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "导出捆绑。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationBundle"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 导入项目包

`POST /automation/import`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "导入项目包",
  "description": "该捆绑包作为具有新 ID 的新项目到达。",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/AutomationBundle"
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "导入的项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationProject"
          }
        }
      }
    },
    "400": {
      "description": "捆绑包无效或格式版本不受支持。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    }
  }
}
```

## 启动项目

`POST /automation/projects/{id}/run`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "启动项目",
  "description": "启动项目并立即返回。项目中的模块会启动所需的浏览器。使用 `/status` 查看进度。\n\n项目已在运行或没有启用的步骤时，返回 `400`。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "项目开始了。",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "project_id": {
                "type": "string",
                "format": "uuid"
              },
              "running": {
                "type": "boolean",
                "example": true
              }
            }
          }
        }
      }
    },
    "400": {
      "description": "项目已在运行或没有启用的步骤。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到项目。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 停止项目

`POST /automation/projects/{id}/stop`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "停止项目",
  "description": "请求停止运行。每个浏览器会先完成当前步骤再关闭，因此接口返回后运行不会立即消失。请通过 `/status` 查看 `running` 字段。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "要求停止。",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "project_id": {
                "type": "string",
                "format": "uuid"
              },
              "stopping": {
                "type": "boolean",
                "example": true
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 获取项目运行状态

`GET /automation/projects/{id}/status`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "获取项目运行状态",
  "description": "如果项目未运行且自启动程序启动后未运行，则返回 `null`。 `log` 包含队列窗口中显示的最近条目。完整日志存储在 `automation-runs/` 下。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "format": "uuid"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "运行状态或 `null`。",
      "content": {
        "application/json": {
          "schema": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/AutomationRun"
              },
              {
                "type": "null"
              }
            ]
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 列出活动运行

`GET /automation/runs`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "列出活动运行",
  "responses": {
    "200": {
      "description": "活动运行列表。",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationRun"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 列出已安装的模块

`GET /automation/modules`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "列出已安装的模块",
  "description": "返回操作者安装的 WebAssembly 模块。每个模块都会添加类型为 `module:<module id>:<block>` 的模块。模块返回由执行器执行的动作，不会直接访问页面。",
  "responses": {
    "200": {
      "description": "模块列表。",
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AutomationModule"
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 安装模块

`POST /automation/modules`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "安装模块",
  "description": "`path` 必须指向启动器所在机器上的本地 `.wasm` 文件。模块包含由操作者构建的原生代码。",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "path"
          ],
          "properties": {
            "path": {
              "type": "string",
              "example": "/home/me/module.wasm"
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "安装的模块。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/AutomationModule"
          }
        }
      }
    },
    "400": {
      "description": "缺少路径或不受支持的模块文件。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## 移除模块

`DELETE /automation/modules/{id}`

```json
{
  "tags": [
    "Automation"
  ],
  "summary": "移除模块",
  "description": "使用该模块的项目停止工作。现有项目定义不会被修改。",
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string"
      }
    }
  ],
  "responses": {
    "200": {
      "description": "已删除。",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "deleted": {
                "type": "boolean",
                "example": true
              },
              "id": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "description": "未找到模块。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "501": {
      "description": "此版本不支持自动化。",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```
