{
  "openapi": "3.1.0",
  "info": {
    "title": "ClodHost API",
    "version": "1.0.0",
    "description": "Official ClodHost API. Auth via OAuth2 Device Authorization Grant (RFC 8628) at /api/oauth/device and /api/oauth/token. Most /api/* endpoints the website uses also accept Bearer tokens."
  },
  "servers": [
    { "url": "https://clodhost.com" }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Access token from the device flow (clodat_...). Expires in 24h; refresh via /api/oauth/token grant_type=refresh_token."
      }
    },
    "schemas": {
      "Envelope": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean" },
          "message": { "type": "string" },
          "data": { "type": "object" }
        },
        "required": ["success"]
      },
      "DeviceCodeResponse": {
        "type": "object",
        "properties": {
          "device_code": { "type": "string" },
          "user_code": { "type": "string", "example": "ABCD-1234" },
          "verification_uri": { "type": "string", "format": "uri" },
          "verification_uri_complete": { "type": "string", "format": "uri" },
          "expires_in": { "type": "integer", "example": 900 },
          "interval": { "type": "integer", "example": 5 }
        }
      },
      "TokenResponse": {
        "type": "object",
        "properties": {
          "access_token": { "type": "string" },
          "token_type": { "type": "string", "example": "Bearer" },
          "expires_in": { "type": "integer", "example": 86400 },
          "refresh_token": { "type": "string" },
          "scope": { "type": "string", "example": "all" }
        }
      },
      "ChatStatus": {
        "type": "object",
        "properties": {
          "instance_id": { "type": "integer" },
          "status": { "type": "string", "enum": ["waiting", "working", "stalled", "stopped"] },
          "pending_message_id": { "type": "integer", "nullable": true },
          "pending_since": { "type": "string", "nullable": true },
          "completed_at": { "type": "string", "nullable": true }
        }
      },
      "ChatResponseItem": {
        "type": "object",
        "properties": {
          "type": { "type": "string", "example": "text" },
          "role": { "type": "string", "enum": ["user", "assistant"] },
          "text": { "type": "string" },
          "ts": { "type": "string" },
          "message_id": { "type": "integer" },
          "pending": { "type": "boolean" },
          "completed_at": { "type": "string", "nullable": true }
        }
      }
    }
  },
  "paths": {
    "/api/oauth/device": {
      "post": {
        "summary": "Start device authorization",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "required": ["client_id"],
                "properties": { "client_id": { "type": "string" } }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Device code issued",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeviceCodeResponse" } } }
          }
        }
      }
    },
    "/api/oauth/token": {
      "post": {
        "summary": "Exchange device_code or refresh_token for tokens",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "required": ["grant_type", "client_id"],
                "properties": {
                  "grant_type": { "type": "string", "enum": ["device_code", "refresh_token"] },
                  "client_id": { "type": "string" },
                  "device_code": { "type": "string" },
                  "refresh_token": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tokens issued",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/TokenResponse" } } }
          },
          "400": {
            "description": "authorization_pending | slow_down | expired_token | access_denied | invalid_grant | invalid_client",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": { "type": "string" },
                    "error_description": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/oauth/signup": {
      "post": {
        "summary": "Request account signup/sign-in magic link (first-party clients only)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["client_id", "email"],
                "properties": {
                  "client_id": { "type": "string" },
                  "email": { "type": "string", "format": "email" },
                  "user_code": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Always success (no account-existence leak)" }
        }
      }
    },
    "/api/oauth/userinfo": {
      "get": {
        "summary": "Identity echo for the current token",
        "security": [{ "bearerAuth": [] }],
        "responses": {
          "200": {
            "description": "User identity",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "id": { "type": "integer" },
                        "email": { "type": "string" },
                        "client_id": { "type": "string" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/chat-status": {
      "get": {
        "summary": "Pollable chat status (working/waiting/stopped/stalled)",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          { "name": "instance_id", "in": "query", "required": true, "schema": { "type": "integer" } }
        ],
        "responses": {
          "200": {
            "description": "Status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "data": { "$ref": "#/components/schemas/ChatStatus" }
                  }
                }
              }
            }
          },
          "403": { "description": "No access to this chat" }
        }
      }
    },
    "/api/chat-responses": {
      "get": {
        "summary": "Clean conversation feed (human turns + readable AI responses, no tool noise)",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          { "name": "instance_id", "in": "query", "required": true, "schema": { "type": "integer" } },
          { "name": "after_id", "in": "query", "required": false, "schema": { "type": "integer", "default": 0 } },
          { "name": "limit", "in": "query", "required": false, "schema": { "type": "integer", "default": 100, "maximum": 500 } }
        ],
        "responses": {
          "200": {
            "description": "Turn pairs; tail with data.after_id",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "instance_id": { "type": "integer" },
                        "items": {
                          "type": "array",
                          "items": { "$ref": "#/components/schemas/ChatResponseItem" }
                        },
                        "after_id": { "type": "integer" },
                        "pending_message_id": { "type": "integer", "nullable": true }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/get-chat-sessions": {
      "get": {
        "summary": "List the user's chats (same call the website makes)",
        "security": [{ "bearerAuth": [] }],
        "responses": { "200": { "description": "Chat list envelope" } }
      }
    },
    "/api/claude-output": {
      "get": {
        "summary": "Raw terminal output short-poll (returns unchanged:true when nothing new)",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          { "name": "instance_id", "in": "query", "required": true, "schema": { "type": "integer" } },
          { "name": "last_hash", "in": "query", "required": false, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Output diff or unchanged" } }
      }
    }
  }
}
