{
  "openapi": "3.1.0",
  "info": {
    "title": "ChatGPT Desktop API",
    "version": "1.0.0",
    "description": "Experimental localhost OpenAI-compatible bridge to the authenticated ChatGPT macOS desktop app."
  },
  "servers": [
    { "url": "http://127.0.0.1:1456", "description": "Local desktop bridge" }
  ],
  "paths": {
    "/v1/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Create a desktop chat completion, optionally streamed",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ChatCompletionRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Desktop completion",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ChatCompletion" }
              },
              "text/event-stream": {
                "schema": { "type": "string", "description": "OpenAI-compatible chat.completion.chunk events ending with data: [DONE]" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "413": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" },
          "502": { "$ref": "#/components/responses/Error" },
          "503": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/images/generations": {
      "post": {
        "operationId": "createImage",
        "summary": "Generate one PNG through ChatGPT Desktop",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ImageGenerationRequest" } } }
        },
        "responses": {
          "200": { "description": "Generated image", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ImageGenerationResponse" } } } },
          "400": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "413": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" },
          "502": { "$ref": "#/components/responses/Error" },
          "504": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/models": {
      "get": {
        "operationId": "listModels",
        "summary": "List bridge models",
        "responses": {
          "200": {
            "description": "Models discovered live from the authenticated ChatGPT desktop model picker",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["object", "data"],
                  "properties": {
                    "object": { "const": "list" },
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Model" } }
                  }
                }
              }
            }
          },
          "403": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" },
          "503": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Inspect bridge and desktop app health",
        "responses": {
          "200": {
            "description": "Bridge health",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Health" }
              }
            }
          },
          "403": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TextPart": {
        "type": "object",
        "required": ["type", "text"],
        "properties": {
          "type": { "type": "string", "enum": ["text", "input_text"] },
          "text": { "type": "string" }
        }
      },
      "Message": {
        "type": "object",
        "required": ["role", "content"],
        "properties": {
          "role": { "type": "string" },
          "content": {
            "oneOf": [
              { "type": "string" },
              { "type": "array", "items": { "oneOf": [{ "type": "string" }, { "$ref": "#/components/schemas/TextPart" }] } }
            ]
          }
        }
      },
      "ChatCompletionRequest": {
        "type": "object",
        "required": ["messages"],
        "properties": {
          "model": { "type": "string", "default": "chatgpt-desktop", "description": "A live model id from GET /v1/models. chatgpt-desktop preserves the app's current selection for backward compatibility." },
          "messages": { "type": "array", "minItems": 1, "items": { "$ref": "#/components/schemas/Message" } },
          "stream": { "type": "boolean", "default": false },
          "stream_options": {
            "type": "object",
            "properties": {
              "include_usage": { "type": "boolean", "default": false }
            }
          },
          "timeout_ms": { "type": "number", "minimum": 1, "maximum": 300000, "default": 180000 }
        }
      },
      "ChatCompletion": {
        "type": "object",
        "required": ["id", "object", "created", "model", "choices", "usage"],
        "properties": {
          "id": { "type": "string" },
          "object": { "const": "chat.completion" },
          "created": { "type": "integer" },
          "model": { "type": "string", "description": "The real desktop model used for this completion." },
          "choices": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["index", "message", "finish_reason"],
              "properties": {
                "index": { "type": "integer" },
                "message": { "$ref": "#/components/schemas/Message" },
                "logprobs": { "type": ["object", "null"] },
                "finish_reason": { "const": "stop" }
              }
            }
          },
          "usage": {
            "type": "object",
            "required": ["prompt_tokens", "completion_tokens", "total_tokens"],
            "properties": {
              "prompt_tokens": { "type": "integer", "const": 0 },
              "completion_tokens": { "type": "integer", "const": 0 },
              "total_tokens": { "type": "integer", "const": 0 }
            }
          }
        }
      },
      "ImageGenerationRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["prompt"],
        "properties": {
          "prompt": { "type": "string", "minLength": 1, "maxLength": 100000 },
          "model": { "const": "gpt-image-1", "default": "gpt-image-1" },
          "n": { "type": "integer", "const": 1, "default": 1 },
          "response_format": { "const": "b64_json", "default": "b64_json" },
          "timeout_ms": { "type": "integer", "minimum": 1000, "maximum": 600000, "default": 300000 }
        }
      },
      "ImageGenerationResponse": {
        "type": "object",
        "required": ["created", "data"],
        "properties": {
          "created": { "type": "integer" },
          "data": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "type": "object",
              "required": ["b64_json"],
              "properties": {
                "b64_json": { "type": "string", "contentEncoding": "base64", "contentMediaType": "image/png" },
                "revised_prompt": { "type": "string" }
              }
            }
          }
        }
      },
      "Model": {
        "type": "object",
        "required": ["id", "object", "created", "owned_by", "display_name", "active"],
        "properties": {
          "id": { "type": "string", "description": "Stable request id derived from the desktop model label, for example gpt-5.6-sol." },
          "object": { "const": "model" },
          "created": { "type": "integer", "const": 0 },
          "owned_by": { "const": "chatgpt-desktop" },
          "display_name": { "type": "string" },
          "active": { "type": "boolean", "description": "Whether this model is currently selected in the desktop app." }
        }
      },
      "Health": {
        "type": "object",
        "required": ["ok", "bridge", "version", "service", "app"],
        "properties": {
          "ok": { "const": true },
          "bridge": { "const": "chatgpt-desktop" },
          "version": { "type": "integer", "const": 7 },
          "service": {
            "type": "object",
            "required": ["managed", "id"],
            "properties": {
              "managed": { "type": "boolean" },
              "id": { "type": ["string", "null"], "format": "uuid" }
            }
          },
          "app": {
            "type": "object",
            "required": ["reachable", "authenticated", "temporaryChat"],
            "properties": {
              "reachable": { "type": "boolean" },
              "authenticated": { "type": "boolean" },
              "temporaryChat": { "type": "boolean" },
              "detail": { "type": "string" }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["message", "type", "param"],
            "properties": {
              "message": { "type": "string" },
              "type": { "type": "string" },
              "param": { "type": ["string", "null"] },
              "code": { "type": ["string", "null"] }
            }
          }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "OpenAI-style error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  }
}
