openapi: 3.0.3
info:
  title: 开放平台客户接口
  version: "2.0"
  description: |
    客户系统调用开放平台单元的 HTTP 接口。主机是你那套 IM 对应的单元：`ac-<imId>.laya-open.lol:8443`。

    所有请求都要过三层认证：mTLS 客户端证书、运行令牌、逐请求签名头。发指令另外带一份你自己签名的意图，
    IM 一侧只认这份签名。用 SDK 时这些都由 SDK 完成；这一页给自己实现客户端或排查问题的人看。

    指令入口 `/account-control/v2/commands` 的请求体、应答状态和认证方式，与平台服务导出的接口定义
    （`services/open-service/openapi/account-control-v2.json`）一致；两者不一致时以平台导出的为准。

    事件流、启动授权、令牌刷新的路径前缀仍是 `/account-control/v1`，指令入口是 `/account-control/v2/commands`。
    旧的逐动作接口（`/account-control/v1/accounts/{accountUserId}/…`）已经下线，调用得到 410。
servers:
  - url: https://ac-{imId}.laya-open.lol:8443
    variables:
      imId:
        default: im-a2b3c4d5
        description: 你那套 IM 的标识，接入时由运营方给出
tags:
  - name: 指令
    description: 以托管账号的身份执行操作
  - name: 认证
    description: 进程启动时换取运行令牌，之后定期刷新
  - name: 事件
    description: 拉取托管账号的事件并提交读到的位置
paths:
  /account-control/v2/commands:
    post:
      tags: [指令]
      summary: 执行一条指令
      description: |
        30 条指令都走这一个入口，查询也走它。请求体恰好三个字符串字段。`intent` 和 `body` 是两段 JSON 文本，
        签名和哈希都对文本的 UTF-8 原字节计算，平台原样转给 IM，不改动任何一个字节。

        逐请求签名的规范串里，最后一行 `accountUserId` 在这个入口上是空串；`X-Key-Id` 必须等于意图里的 `kid`。

        HTTP 200 不等于成功，要看应答体的 `status`。
      security:
        - AccountControlSignedRequest: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CommandRequest'
            example:
              intent: '{"v":2,"typ":"ac-intent","imId":"im-a2b3c4d5","installationId":"inst_acme01","tenantId":"t_acme","accountId":"u_1001","command":"message.send_private","bodySha256":"d07db8b94a84b7b44afe8f9b0e3a6c0f80545edbd957a19423761c57203594cb","operationId":"9ae56f21c4cc8c4385a7ebfa05bbc820406c7032b8ef87b9d36782bed640d14e","iat":1795427995,"exp":1795428115,"kid":"acme-2026-11"}'
              intentSig: QiIyVl4Jl-kRxnN3X_8bLEhmhmWdj_BwRL7C2-_olv5E6ZUsruAJ1EQbCrWI1qKeQVElhnjX93worCXjXvY0DQ
              body: '{"toUserId":"u_2002","contentType":"TEXT","content":"你好，订单已发货","clientMessageId":"cmsg_7f3a9c"}'
      responses:
        '200':
          description: 指令已经发到 IM 一侧（已发帧），按 `status` 判断结局。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CommandResponse'
        '400':
          description: 签名请求无效，或者意图预检被拒绝（格式、过期、哈希对不上、目标不是本单元）。没有发到 IM。
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ErrorEnvelope'
                  - $ref: '#/components/schemas/CommandResponse'
        '401':
          description: 运行会话或逐请求认证失败。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '403':
          description: 认证、归属或访问控制拒绝。没有发到 IM。意图里的 Installation 或 `kid` 与已认证的会话不符时是 `AC_PLATFORM_INSTALLATION_MISMATCH`；账号的托管写入被暂停时是错误信封 `ACCOUNT_PAUSED`，只影响这一个账号。
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ErrorEnvelope'
                  - $ref: '#/components/schemas/CommandResponse'
        '404':
          description: 这个账号没有可用的授权。没有发到 IM。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '413':
          description: 请求、指令体（超过 96 KiB）或整帧（超过 256 KiB）超限。
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ErrorEnvelope'
                  - $ref: '#/components/schemas/CommandResponse'
        '429':
          description: 限流或配额不足。没有发到 IM。等待时间在 `Retry-After` 头（秒）和应答体的 `data.retryAfterMillis` 里，之后用同一个 `operationId` 重试。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorEnvelope'
        '503':
          description: 两种应答体。指令应答（`AC_PLATFORM_LINK_DOWN`）：本单元当前没有连上 IM，指令没有发到 IM，退避后用同一个 `operationId` 重试。标准错误信封：平台的某个依赖暂时不可用，它不说明指令有没有发到 IM，变更类用同一个 `operationId` 重试，仍然如此就按结局不明处理。
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ErrorEnvelope'
                  - $ref: '#/components/schemas/CommandResponse'
  /account-control/v1/auth/challenge:
    post:
      tags: [认证]
      summary: 取一个启动挑战
      description: 进程启动的第一步。应答里的挑战要连同启动授权一起签名后提交。只需要通道证书（mTLS），不带令牌和签名头。
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [tenantId, installationId, instanceId]
              properties:
                tenantId: { type: string, description: 租户 }
                installationId: { type: string, description: 你的 Installation }
                instanceId: { type: string, description: 这个进程实例的名字，自己起，重启前后保持不变 }
      responses:
        '200':
          description: 挑战
          content:
            application/json:
              schema:
                type: object
                properties:
                  success: { type: boolean }
                  data:
                    type: object
                    properties:
                      challengeId: { type: string }
                      nonce: { type: string }
                      expiresAt: { type: string, format: date-time }
                      observedSourceIp: { type: string, description: 平台看到的来源地址，要签进启动签名里 }
  /account-control/v1/auth/startup:complete:
    post:
      tags: [认证]
      summary: 用启动授权换运行令牌
      description: |
        启动授权是一次性的，每次进程重启都要一份新的，由运营方签发。签名覆盖挑战、Installation、实例名、
        来源地址、到期时间和启动授权编号。
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [challengeId, startupAuthorizationId, startupAuthorizationSecret, keyId, signature]
              properties:
                challengeId: { type: string }
                startupAuthorizationId: { type: string }
                startupAuthorizationSecret: { type: string }
                keyId: { type: string, description: 签名钥的标识（`kid`） }
                signature: { type: string, description: ES256，P1363 编码，base64url 无填充 }
      responses:
        '200':
          description: 运行会话
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SessionEnvelope'
  /account-control/v1/auth/token:refresh:
    post:
      tags: [认证]
      summary: 刷新运行令牌
      description: 令牌到期前刷新。签名覆盖 `account-control-refresh-v1`、会话号、`kid`、时间戳和随机数，各占一行。
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [sessionId, keyId, signature, timestamp, nonce]
              properties:
                sessionId: { type: string }
                keyId: { type: string }
                signature: { type: string }
                timestamp: { type: string, format: date-time }
                nonce: { type: string }
      responses:
        '200':
          description: 新的运行会话
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SessionEnvelope'
  /account-control/v1/stream/events:fetch:
    post:
      tags: [事件]
      summary: 拉取事件
      description: |
        从上一次提交的位置往后读。一页里可能有普通事件，也可能有缺口标记（`control.stream.gap`）：
        看到缺口，就要把对应时间段的数据当成不完整。
      security:
        - AccountControlSignedRequest: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [consumer, limit]
              properties:
                consumer: { type: string, description: 消费者名字。同一个名字共用一个读取位置 }
                limit: { type: integer, minimum: 1, maximum: 200 }
                waitSeconds: { type: integer, description: 没有新事件时最多等几秒，0 表示立即返回 }
                after: { type: string, description: 从这个位置之后读。不给就从已提交的位置读 }
      responses:
        '200':
          description: 一页事件
          content:
            application/json:
              schema:
                type: object
                properties:
                  success: { type: boolean }
                  data:
                    type: object
                    properties:
                      events:
                        type: array
                        items:
                          $ref: '#/components/schemas/Event'
                      cursor: { type: string, description: 这一页读到的位置，形如 `<代次>:<偏移>:<序号>`。处理完这一页后提交它 }
                      gap: { type: boolean, description: 这一页里有缺口 }
  /account-control/v1/stream/events:commit:
    post:
      tags: [事件]
      summary: 提交读到的位置
      description: 只能往前提交。处理完一页之后再提交，进程中途退出时下次会从上一次提交的位置重读。
      security:
        - AccountControlSignedRequest: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [consumer, position]
              properties:
                consumer: { type: string }
                position: { type: string, description: 拉取应答里的 `cursor` }
      responses:
        '200':
          description: 已提交
          content:
            application/json:
              schema:
                type: object
                properties:
                  success: { type: boolean }
components:
  securitySchemes:
    AccountControlSignedRequest:
      type: http
      scheme: bearer
      description: |
        控制器运行令牌（`Authorization: Bearer …`，不是用户令牌）。同时必须提供 `X-Controller-Installation`、`X-Controller-Instance`、
        `X-Controller-Session`、`X-Key-Id`、`X-Request-Timestamp`、`X-Request-Nonce`、`X-Content-SHA256`、`X-Request-Signature`；
        签名覆盖规范请求：`account-control-request-v1`、方法、路径、查询串哈希、请求体哈希、Installation、实例、会话、`kid`、
        时间戳、随机数、账号，共 12 行。连接本身还要出示通道证书（mTLS），由本单元的客户端 CA 签发，见「领取通道证书」。
  schemas:
    CommandRequest:
      type: object
      required: [intent, intentSig, body]
      additionalProperties: false
      properties:
        intent:
          type: string
          description: |
            意图文本：一个 JSON 对象，恰好 12 个字段。`v` 固定 2；`typ` 固定 `ac-intent`；`imId`、`installationId`、`tenantId`；
            `accountId`（以哪个托管账号执行）；`command`（指令名）；`bodySha256`（指令体文本的 SHA-256，小写十六进制）；
            `operationId`（你生成的 32 字节随机数，64 位小写十六进制）；`iat`、`exp`（epoch 秒，有效期不超过 300 秒）；`kid`。
        intentSig:
          type: string
          description: 对意图文本的 UTF-8 原字节做 ES256，P1363 编码（r、s 各 32 字节），base64url 无填充。
        body:
          type: string
          description: 指令体文本：一个 JSON 对象，字段见「指令与字段」。解码后不超过 96 KiB，不能有重复键。
    CommandResponse:
      type: object
      required: [status]
      properties:
        operationId:
          type: string
          nullable: true
          description: 意图里的那个 `operationId`。意图解析不出来时没有这个字段。
        status:
          type: string
          enum: [OK, REJECTED, FAILED, UNKNOWN, DEPENDENCY]
          description: |
            `OK` 成功。`REJECTED` 被拒绝，这次调用没有留下记录。`FAILED` 已经记成失败（只有变更类）。
            `UNKNOWN` 变更类结局不明，用同一个 `operationId` 重发。`DEPENDENCY` 查询类没等到应答。
        code:
          type: string
          nullable: true
          description: 不是 `OK` 时的错误符号，见「错误码」。
        result:
          type: object
          nullable: true
          description: 成功时的结果，各指令的字段见「指令与字段」。
    SessionEnvelope:
      type: object
      properties:
        success: { type: boolean }
        data:
          type: object
          properties:
            sessionId: { type: string }
            runtimeToken: { type: string }
            tokenExpiresAt: { type: string, format: date-time }
            installationId: { type: string }
            instanceId: { type: string }
            keyId: { type: string }
            boundSourceIp: { type: string }
            publicKeyFingerprint: { type: string }
    Event:
      type: object
      properties:
        eventId: { type: string, description: 事件的唯一标识。重复投递时相同，按它去重 }
        type: { type: string, description: '事件类型，例如 `message.created`、`control.stream.gap`' }
        installationId: { type: string }
        accountUserId: { type: string, description: 这条事件属于哪个托管账号；群消息这一项为空，看 `accountUserIds` }
        occurredAt: { type: string, format: date-time }
        gap: { type: boolean, description: 这是一条缺口标记 }
        backfilled: { type: boolean, description: 这是恢复之后补回来的事件 }
        windowStart: { type: string, description: 缺口的起点 }
        windowEnd: { type: string, description: 缺口的终点 }
    ErrorEnvelope:
      type: object
      description: 平台自己拒绝请求时的标准错误信封。它没有 `status` 字段：出现它说明这一次请求没有发到 IM。
      required: [success]
      properties:
        success: { type: boolean, description: 恒为 `false` }
        data: { type: object, nullable: true, description: 附带信息。限流时里面有 `retryAfterMillis`（毫秒） }
        errorCode: { type: string, nullable: true }
        errorMessage: { type: string, nullable: true }
        retryable: { type: boolean, nullable: true, description: 稍后原样重试有没有意义 }
