{
  "openapi": "3.1.0",
  "info": {
    "title": "HTMLRun Agent API",
    "version": "1.0.0",
    "description": "把一份 HTML 发布成可分享的在线链接（`https://html.run/s/<slug>`）。本文档只列出**给程序调用**的接口 —— 能用 publish token 或协助码直接调的那些。"
  },
  "servers": [
    {
      "url": "https://html.run",
      "description": "生产环境"
    }
  ],
  "paths": {
    "/api/publish": {
      "post": {
        "tags": [
          "发布"
        ],
        "summary": "发布或更新页面",
        "operationId": "publish_api_publish_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PublishReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {},
                "example": {
                  "code": 200,
                  "message": "OK",
                  "data": {
                    "artifactId": "a_8Kd2pQ",
                    "version": 3,
                    "url": "https://html.run/s/demo",
                    "slug": "demo",
                    "created": false,
                    "accessMode": "public",
                    "viewPassword": "",
                    "accessNotice": "该页面已公开，任何人凭链接即可访问。",
                    "sensitiveWords": [],
                    "risk": {
                      "keywordScore": 0,
                      "level": "low",
                      "blocked": false
                    },
                    "agentHint": {
                      "summary": "已追加为 v3，链接不变。",
                      "do": [
                        "把 url 交给用户",
                        "用自己的话转述 accessNotice",
                        "继续修改仍用同一绝对路径 publish，勿加 --new"
                      ],
                      "dont": [
                        "不要对同一文件使用 --new / forceNew（除非用户明确要求另起新链接）",
                        "不要盲猜 sensitiveWords 以外的词"
                      ],
                      "nextCommand": "htmlrun.cjs publish <同一绝对路径>（勿加 --new）",
                      "tips": [
                        "htmlrun-cli 有新版本 3.0.2（当前 3.0.1），建议升级：请根据 https://skillhub.cn/install/skillhub.md 重新安装 @org-q447viqi/htmlrun"
                      ]
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "token 无效/吊销/过期"
          },
          "403": {
            "description": "无权操作该工件"
          },
          "404": {
            "description": "工件不存在"
          },
          "422": {
            "description": "html 为空"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/api/publish/online": {
      "patch": {
        "tags": [
          "发布"
        ],
        "summary": "设置展示状态与展示时间窗",
        "operationId": "cli_update_online_api_publish_online_patch",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CliOnlineReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {},
                "example": {
                  "code": 200,
                  "message": "OK",
                  "data": {
                    "artifactId": "a_8Kd2pQ",
                    "url": "https://html.run/s/demo",
                    "slug": "demo",
                    "title": "国庆活动页",
                    "online_state": "online",
                    "online_from": "2026-09-01T09:00:00",
                    "online_until": "2026-09-30T18:00:00",
                    "online_meta": {
                      "label": "未到展示时间",
                      "type": "warning"
                    },
                    "agentHint": {
                      "summary": "已设置，页面**当前对外不可访问**，到 2026-09-01 09:00 才开始展示。",
                      "do": [
                        "明确告诉用户页面现在打不开，2026-09-01 09:00 自动开始展示",
                        "不要说「发布成功可以分享了」——现在分享出去对方看到的是无法访问"
                      ],
                      "dont": [
                        "不要把设置成功说成「页面可以访问了」——两者不是一回事",
                        "不要凭记忆报时间，用本次响应里的值"
                      ],
                      "nextCommand": "",
                      "tips": [
                        "当前状态：未到展示时间"
                      ]
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "token 无效/吊销/过期"
          },
          "403": {
            "description": "该工件不属于当前 token 的归属用户"
          },
          "404": {
            "description": "工件不存在，或该路径从未发布过"
          },
          "422": {
            "description": "path/artifactId 都没传、没有需要修改的字段、状态非法、或开始时间不早于结束时间"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/api/token/bind": {
      "post": {
        "tags": [
          "凭据"
        ],
        "summary": "校验 token 并登记设备",
        "operationId": "bind_token_api_token_bind_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BindTokenReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "校验通过，绑定已记录",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "401": {
            "description": "token 无效/已吊销/已暂停/已过期/已删除，message 说明具体原因"
          },
          "422": {
            "description": "缺少 clientId"
          },
          "429": {
            "description": "超出限流额度，响应头带 Retry-After"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/api/token/status": {
      "get": {
        "tags": [
          "凭据"
        ],
        "summary": "查询 token 状态",
        "operationId": "token_status_api_token_status_get",
        "responses": {
          "200": {
            "description": "token 有效",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "401": {
            "description": "token 不可用，message 说明原因"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/api/cli/auth/start": {
      "post": {
        "tags": [
          "凭据"
        ],
        "summary": "发起设备激活（本机还没有 token 时）",
        "operationId": "cli_auth_start_api_cli_auth_start_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CliAuthStartReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "会话已创建，返回激活链接与预 token",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "422": {
            "description": "缺少 clientId"
          },
          "429": {
            "description": "超出限流额度，响应头带 Retry-After"
          }
        },
        "security": []
      }
    },
    "/api/cli/bind/confirm": {
      "post": {
        "tags": [
          "凭据"
        ],
        "summary": "用配对码换取 token",
        "operationId": "cli_bind_confirm_api_cli_bind_confirm_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CliBindConfirmReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "400": {
            "description": "配对码已使用或已过期"
          },
          "404": {
            "description": "配对码不存在，或绑定的 token 不存在"
          },
          "422": {
            "description": "缺少 clientId"
          }
        },
        "security": []
      }
    },
    "/api/draft/pull": {
      "get": {
        "tags": [
          "AI 协助编辑"
        ],
        "summary": "拉取草稿正文（协助码）",
        "operationId": "pull_draft_api_draft_pull_get",
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "401": {
            "description": "协助码无效或已失效"
          },
          "409": {
            "description": "草稿正文已被取走，需用户重新发起"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/api/draft/push": {
      "post": {
        "tags": [
          "AI 协助编辑"
        ],
        "summary": "回推改好的正文（协助码）",
        "operationId": "push_draft_api_draft_push_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DraftPushReq"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "401": {
            "description": "协助码无效或已失效"
          },
          "409": {
            "description": "协助已被用户取消或已发布"
          },
          "422": {
            "description": "正文为空或超出体积上限"
          }
        },
        "security": [
          {
            "APIKeyHeader": []
          }
        ]
      }
    },
    "/health": {
      "get": {
        "tags": [
          "其它"
        ],
        "summary": "探活",
        "operationId": "health_health_get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        },
        "security": [],
        "description": "返回裸 JSON（无 {code,message,data} 信封）"
      }
    }
  },
  "components": {
    "schemas": {
      "BindTokenReq": {
        "properties": {
          "clientId": {
            "type": "string",
            "title": "Clientid",
            "description": "客户端设备标识，CLI 首次 login 本地生成的 UUID",
            "examples": [
              "3f2b8c1e-5a47-4d9b-8e21-7c6f0a1b2d34"
            ]
          },
          "hostname": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Hostname",
            "description": "主机名，仅供辨认机器",
            "examples": [
              "MacBook-Pro"
            ]
          },
          "osInfo": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Osinfo",
            "description": "系统信息",
            "examples": [
              "Darwin-24.6.0"
            ]
          },
          "cliVersion": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Cliversion",
            "description": "htmlrun-cli 版本",
            "examples": [
              "0.2.0"
            ]
          }
        },
        "type": "object",
        "required": [
          "clientId"
        ],
        "title": "BindTokenReq",
        "description": "CLI htmlrun.cjs login 的绑定请求。除 clientId 外都是可选的自报信息。\n\n这些字段客户端可以随便填，服务端不做真实性校验 —— 它们只用于让用户辨认\n\"这是哪台机器\"，以及统计 CLI 版本分布。可信的只有服务端观测到的 IP。"
      },
      "CliAuthStartReq": {
        "properties": {
          "clientId": {
            "type": "string",
            "title": "Clientid",
            "description": "客户端设备标识，CLI 本地生成的 UUID",
            "examples": [
              "3f2b8c1e-5a47-4d9b-8e21-7c6f0a1b2d34"
            ]
          },
          "hostname": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Hostname",
            "description": "主机名，用于生成 token 名称与设备辨认",
            "examples": [
              "MacBook-Pro"
            ]
          },
          "osInfo": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Osinfo",
            "description": "系统信息",
            "examples": [
              "Darwin-24.6.0"
            ]
          },
          "cliVersion": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Cliversion",
            "description": "htmlrun-cli 版本",
            "examples": [
              "0.3.0"
            ]
          }
        },
        "type": "object",
        "required": [
          "clientId"
        ],
        "title": "CliAuthStartReq",
        "description": "CLI 发起设备激活。除 clientId 外都是可选的自报信息。\n\n与 BindTokenReq 同源：这些字段客户端可以随便填，服务端不校验真实性，\n只用于生成 token 默认名称、在激活页让用户辨认「是不是我这台机器」。\n可信的只有服务端观测到的 IP。"
      },
      "CliBindConfirmReq": {
        "properties": {
          "code": {
            "type": "string",
            "title": "Code",
            "description": "8 位字母数字配对码（去 0/O、1/I/L 易混字符），来自管理端弹窗",
            "examples": [
              "Ab3fK9zq"
            ]
          },
          "clientId": {
            "type": "string",
            "title": "Clientid",
            "description": "客户端设备标识，CLI 本地生成的 UUID",
            "examples": [
              "3f2b8c1e-5a47-4d9b-8e21-7c6f0a1b2d34"
            ]
          },
          "hostname": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Hostname",
            "description": "主机名，用于设备绑定记录展示",
            "examples": [
              "MacBook-Pro"
            ]
          },
          "osInfo": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Osinfo",
            "description": "系统信息",
            "examples": [
              "Darwin-24.6.0"
            ]
          },
          "cliVersion": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Cliversion",
            "description": "htmlrun-cli 版本",
            "examples": [
              "0.3.0"
            ]
          }
        },
        "type": "object",
        "required": [
          "code",
          "clientId"
        ],
        "title": "CliBindConfirmReq",
        "description": "CLI 输入配对码换取 token 明文。除 code/clientId 外都是可选的自报信息。\n\n与 CliAuthConfirmReq 对称：code 本身不是认证凭据，只指明「换哪一条 pending 记录」，\n真正的鉴权边界是 code 的一次性 + 短命 + IP 限流。"
      },
      "CliOnlineReq": {
        "properties": {
          "onlineState": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Onlinestate",
            "description": "展示状态：online 在线｜offline 已下线。不传=不改",
            "examples": [
              "offline"
            ]
          },
          "onlineFrom": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ],
            "title": "Onlinefrom",
            "description": "展示开始时间（含）。传 null=清空限制（立即可展示）；不传=不改",
            "examples": [
              "2026-09-01 09:00:00"
            ]
          },
          "onlineUntil": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ],
            "title": "Onlineuntil",
            "description": "展示结束时间（不含）。传 null=清空限制（永不到期）；不传=不改",
            "examples": [
              "2026-09-30 18:00:00"
            ]
          },
          "path": {
            "anyOf": [
              {
                "type": "string",
                "maxLength": 1024
              },
              {
                "type": "null"
              }
            ],
            "title": "Path",
            "description": "客户端本地文件**绝对路径**，按 (归属用户, 路径) 反查最近一次发布的工件。与 artifactId 二选一",
            "examples": [
              "/Users/me/report.html"
            ]
          },
          "artifactId": {
            "anyOf": [
              {
                "type": "string",
                "maxLength": 32
              },
              {
                "type": "null"
              }
            ],
            "title": "Artifactid",
            "description": "工件 ID。与 path 二选一；同时传时以 artifactId 为准（更精确）",
            "examples": [
              "a_8Kd2pQxx"
            ]
          }
        },
        "type": "object",
        "title": "CliOnlineReq",
        "description": "CLI 改展示设置：Bearer publish token 鉴权，按**文件路径**定位工件。\n\n单独一个模型而非复用 OnlineUpdateReq，因为定位方式不同：管理端把 artifact_id 放在\nURL 路径里（前端手上有列表数据），CLI 手上只有本地文件的绝对路径 —— agent 不该\n被要求理解、更不该被要求记住 artifactId。\n\npath 与 artifactId 二选一，都不传是 422。path 走与 publish 完全相同的认领口径\n（ArtifactRepository.find_latest_by_path），所以「同一路径 publish 完再 online」\n命中的必定是刚发布那个工件，不会张冠李戴。\n\n清空时间限制走显式 null（同 mixin 语义）。CLI 侧无法在命令行参数里表达 JSON 的\nnull，故 htmlrun.cjs 另给 --clear-from / --clear-until 两个 flag 翻译成 null。"
      },
      "DraftPushReq": {
        "properties": {
          "htmlEncoding": {
            "anyOf": [
              {
                "type": "string",
                "enum": [
                  "plain",
                  "base64"
                ]
              },
              {
                "type": "null"
              }
            ],
            "title": "Htmlencoding",
            "description": "html 字段的传输编码：plain=明文（老客户端缺省）；base64=Base64(UTF-8)。",
            "default": "plain",
            "examples": [
              "plain",
              "base64"
            ]
          },
          "html": {
            "type": "string",
            "maxLength": 6700000,
            "title": "Html",
            "description": "修改后的完整 HTML（或 Base64(UTF-8) 编码，见 htmlEncoding）。服务端按 utf-8 字节数二次校验上限",
            "examples": [
              "<html><head><title>周报</title></head><body>...</body></html>"
            ]
          }
        },
        "type": "object",
        "required": [
          "html"
        ],
        "title": "DraftPushReq",
        "description": "Agent 经 CLI 回推草稿正文。\n\n上限与 MAX_DRAFT_BYTES 同源，但模型层与服务层口径不同**且两道都需要**：\n这里的 max_length 数的是字符数，服务层量的是 utf-8 字节数。中文正文下字节数\n约为字符数的 3 倍，故模型层拦不住\"字符数合规但字节数超限\"——真正的闸门在服务层。\n模型层这道只是让明显过大的请求早点被拒。\n\nmax_length 是 TRANSPORT_MAX_CHARS（传输层）：`htmlEncoding=base64` 时 5M 明文\n的 Base64 膨胀后仍能传进来，解码在路由层（routers/draft.py push_draft）、\n服务层字节校验之前。口径说明见 app/lib/html_encoding.py。"
      },
      "PublishReq": {
        "properties": {
          "htmlEncoding": {
            "anyOf": [
              {
                "type": "string",
                "enum": [
                  "plain",
                  "base64"
                ]
              },
              {
                "type": "null"
              }
            ],
            "title": "Htmlencoding",
            "description": "html 字段的传输编码：plain=明文（老客户端缺省）；base64=Base64(UTF-8)。",
            "default": "plain",
            "examples": [
              "plain",
              "base64"
            ]
          },
          "html": {
            "type": "string",
            "title": "Html",
            "description": "完整的 HTML 内容（或 Base64(UTF-8)，见 htmlEncoding）",
            "examples": [
              "<html><head><title>周报</title></head><body>...</body></html>"
            ]
          },
          "artifactId": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Artifactid",
            "description": "工件 ID。不传=按 path 自动认领或新建；传入=对该工件追加新版本（链接不变）",
            "examples": [
              "a_8Kd2pQxx"
            ]
          },
          "note": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Note",
            "description": "版本备注",
            "default": "",
            "examples": [
              "修正图表配色"
            ]
          },
          "path": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Path",
            "description": "客户端本地文件**绝对路径**。不传 artifactId 时据此自动认领同路径的历史工件（追加新版本、链接不变）",
            "default": "",
            "examples": [
              "/Users/me/report.html"
            ]
          },
          "forceNew": {
            "anyOf": [
              {
                "type": "boolean"
              },
              {
                "type": "null"
              }
            ],
            "title": "Forcenew",
            "description": "强制新建工件，忽略按 path 的自动认领（用于「同一路径换成全新内容」）",
            "default": false,
            "examples": [
              false
            ]
          },
          "cliVersion": {
            "anyOf": [
              {
                "type": "string",
                "maxLength": 32
              },
              {
                "type": "null"
              }
            ],
            "title": "Cliversion",
            "description": "htmlrun-cli 版本号（如 0.1.1）。CLI 发布时自动上报；服务端与配置的最新版本比对，过旧则写入 agentHint.tips 提示升级。非 CLI 入口可空。",
            "default": "",
            "examples": [
              "0.1.1"
            ]
          }
        },
        "type": "object",
        "required": [
          "html"
        ],
        "title": "PublishReq",
        "description": "CLI / Skill 同步发布。\n\n传输编码见 HtmlEncodingMixin。这里 `html` 不设 max_length：CLI 的内容来自本地\n文件，历史上一向不设长度上限（网页端才有，见 WebPublishReq）。"
      }
    },
    "securitySchemes": {
      "APIKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "Authorization"
      }
    }
  }
}