{
  "openapi": "3.1.0",
  "info": {
    "title": "闲力云 Token 调用 API · 预览规范",
    "version": "0.1.0",
    "description": "接口设计预览。示例请求地址、鉴权凭据、模型能力和响应均为契约示例，可调用地址、可用模型和凭据随正式开放发布。"
  },
  "servers": [
    {
      "url": "https://api.example.com",
      "description": "文档占位地址"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "paths": {
    "/v1/models": {
      "get": {
        "operationId": "listTokenModels",
        "summary": "列出模型及调用能力",
        "description": "在预览契约中，data 包含当前凭据对应的模型标识及可用于本文档所列接口的能力。",
        "x-xianliyun-availability": "preview",
        "responses": {
          "200": {
            "description": "模型列表",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ModelList"
                },
                "example": {
                  "data": [
                    {
                      "id": "Qwen/Qwen3-8B",
                      "capabilities": [
                        "chat"
                      ]
                    },
                    {
                      "id": "Tongyi-MAI/Z-Image-Turbo",
                      "capabilities": [
                        "image_generation"
                      ]
                    }
                  ]
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "operationId": "createTokenChatCompletion",
        "summary": "创建非流式文本响应",
        "description": "发送文本消息并接收完整 JSON 响应。usage 返回该次调用的输入和输出 Token 数量。",
        "x-xianliyun-availability": "preview",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatRequest"
              },
              "example": {
                "model": "Qwen/Qwen3-8B",
                "messages": [
                  {
                    "role": "user",
                    "content": "用一句话介绍闲置算力的用途。"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "文本响应及用量",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChatResponse"
                },
                "example": {
                  "id": "chat_demo",
                  "model": "Qwen/Qwen3-8B",
                  "choices": [
                    {
                      "index": 0,
                      "message": {
                        "role": "assistant",
                        "content": "闲置算力可用于执行可调度的 AI 推理和批量处理任务。"
                      },
                      "finish_reason": "stop"
                    }
                  ],
                  "usage": {
                    "prompt_tokens": 18,
                    "completion_tokens": 25,
                    "total_tokens": 43
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/ModelNotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/v1/generate": {
      "post": {
        "operationId": "createTokenGenerationTask",
        "summary": "提交异步文生图任务",
        "description": "保存返回的 task_id，随后调用任务查询接口。支持用于文生图的模型标识与文本提示词。",
        "x-xianliyun-availability": "preview",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/GenerationRequest"
              },
              "example": {
                "model": "Tongyi-MAI/Z-Image-Turbo",
                "prompt": "一座晨光中的山间小屋",
                "response_format": "url"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "生成任务已入队",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationAccepted"
                },
                "example": {
                  "task_id": "task_demo",
                  "status": "queued"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/ModelNotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/v1/tasks/{task_id}": {
      "get": {
        "operationId": "getTokenGenerationTask",
        "summary": "查询生成任务和结果",
        "description": "在任务仍处于排队或运行状态时稍后重查。成功状态包含结果链接，失败状态包含错误详情。",
        "x-xianliyun-availability": "preview",
        "parameters": [
          {
            "name": "task_id",
            "in": "path",
            "required": true,
            "description": "提交生成任务时返回的 task_id。",
            "schema": {
              "type": "string",
              "minLength": 1,
              "examples": [
                "task_demo"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "生成任务状态",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationStatus"
                },
                "example": {
                  "task_id": "task_demo",
                  "status": "succeeded",
                  "result": {
                    "url": "https://cdn.example.com/results/sample.webp"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/TaskNotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "使用平台未来发放的 API 密钥作为 Bearer 凭据。"
      }
    },
    "schemas": {
      "ModelList": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ModelSummary"
            }
          }
        }
      },
      "ModelSummary": {
        "type": "object",
        "required": [
          "id",
          "capabilities"
        ],
        "properties": {
          "id": {
            "type": "string",
            "description": "用于后续调用的模型标识。"
          },
          "capabilities": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "chat",
                "image_generation"
              ]
            }
          }
        }
      },
      "ChatRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "model",
          "messages"
        ],
        "properties": {
          "model": {
            "type": "string",
            "minLength": 1,
            "description": "支持 chat 的模型标识。"
          },
          "messages": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/ChatMessage"
            }
          },
          "stream": {
            "type": "boolean",
            "const": false,
            "default": false,
            "description": "当前契约定义非流式响应。"
          }
        }
      },
      "ChatMessage": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "role",
          "content"
        ],
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "system",
              "user",
              "assistant"
            ]
          },
          "content": {
            "type": "string",
            "minLength": 1
          }
        }
      },
      "ChatResponse": {
        "type": "object",
        "required": [
          "id",
          "model",
          "choices",
          "usage"
        ],
        "properties": {
          "id": {
            "type": "string",
            "description": "本次文本响应的标识。"
          },
          "model": {
            "type": "string"
          },
          "choices": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/ChatChoice"
            }
          },
          "usage": {
            "$ref": "#/components/schemas/TokenUsage"
          }
        }
      },
      "ChatChoice": {
        "type": "object",
        "required": [
          "index",
          "message",
          "finish_reason"
        ],
        "properties": {
          "index": {
            "type": "integer",
            "minimum": 0
          },
          "message": {
            "type": "object",
            "required": [
              "role",
              "content"
            ],
            "properties": {
              "role": {
                "const": "assistant"
              },
              "content": {
                "type": "string"
              }
            }
          },
          "finish_reason": {
            "type": "string",
            "enum": [
              "stop",
              "length"
            ]
          }
        }
      },
      "TokenUsage": {
        "type": "object",
        "required": [
          "prompt_tokens",
          "completion_tokens",
          "total_tokens"
        ],
        "properties": {
          "prompt_tokens": {
            "type": "integer",
            "minimum": 0,
            "description": "输入消息使用的 Token 数量。"
          },
          "completion_tokens": {
            "type": "integer",
            "minimum": 0,
            "description": "输出文本使用的 Token 数量。"
          },
          "total_tokens": {
            "type": "integer",
            "minimum": 0,
            "description": "输入和输出的 Token 数量之和。"
          }
        }
      },
      "GenerationRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "model",
          "prompt"
        ],
        "properties": {
          "model": {
            "type": "string",
            "minLength": 1,
            "description": "支持 image_generation 的模型标识。"
          },
          "prompt": {
            "type": "string",
            "minLength": 1
          },
          "response_format": {
            "type": "string",
            "const": "url",
            "default": "url"
          }
        }
      },
      "GenerationAccepted": {
        "type": "object",
        "required": [
          "task_id",
          "status"
        ],
        "properties": {
          "task_id": {
            "type": "string"
          },
          "status": {
            "const": "queued"
          }
        }
      },
      "GenerationStatus": {
        "type": "object",
        "required": [
          "task_id",
          "status"
        ],
        "properties": {
          "task_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "queued",
              "running",
              "succeeded",
              "failed"
            ]
          },
          "result": {
            "type": "object",
            "required": [
              "url"
            ],
            "properties": {
              "url": {
                "type": "string",
                "format": "uri",
                "description": "生成结果的访问地址。"
              }
            }
          },
          "error": {
            "$ref": "#/components/schemas/ErrorDetail"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "status": {
                  "const": "succeeded"
                }
              }
            },
            "then": {
              "required": [
                "result"
              ]
            },
            "else": {
              "not": {
                "required": [
                  "result"
                ]
              }
            }
          },
          {
            "if": {
              "properties": {
                "status": {
                  "const": "failed"
                }
              }
            },
            "then": {
              "required": [
                "error"
              ]
            },
            "else": {
              "not": {
                "required": [
                  "error"
                ]
              }
            }
          }
        ]
      },
      "ErrorDetail": {
        "type": "object",
        "required": [
          "code",
          "message"
        ],
        "properties": {
          "code": {
            "type": "string",
            "description": "用于程序判断的错误码。"
          },
          "message": {
            "type": "string",
            "description": "可读错误说明。"
          }
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "$ref": "#/components/schemas/ErrorDetail"
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "凭据缺失或无效",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "invalid_api_key",
                "message": "请提供有效的 API 凭据。"
              }
            }
          }
        }
      },
      "InvalidRequest": {
        "description": "请求体或字段无效",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "invalid_request",
                "message": "请检查请求参数。"
              }
            }
          }
        }
      },
      "ModelNotFound": {
        "description": "模型不可用",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "model_not_found",
                "message": "该模型对当前凭据不可用。"
              }
            }
          }
        }
      },
      "TaskNotFound": {
        "description": "任务不存在或不可访问",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "task_not_found",
                "message": "请核对任务标识。"
              }
            }
          }
        }
      },
      "RateLimited": {
        "description": "请求频率受限",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "rate_limited",
                "message": "请降低请求频率后重试。"
              }
            }
          }
        }
      },
      "Unavailable": {
        "description": "服务临时不可用",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "service_unavailable",
                "message": "请稍后重试或查询任务状态。"
              }
            }
          }
        }
      }
    }
  }
}