{
  "openapi": "3.1.0",
  "info": {
    "title": "Devil's Advocate API",
    "version": "1.0.0",
    "description": "Production HTTP API at https://devadv.top — thesis risk audit (POST /check_risk) and single-ticker multi-agent view (POST /check_risk_stocks, optional start_date/end_date window) with optional x402 v2 settlement (USDC on Base) when billing is enabled. Human-readable examples, typical pricing, and links: https://devadv.top/x402/SKILL.md",
    "contact": { "url": "https://devadv.top" }
  },
  "servers": [
    {
      "url": "https://devadv.top",
      "description": "Production"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "Health check",
        "description": "Public liveness endpoint. No authentication or payment.",
        "operationId": "health",
        "responses": {
          "200": {
            "description": "Plain text body: ok",
            "content": {
              "text/plain": {
                "schema": { "type": "string", "example": "ok" }
              }
            }
          }
        }
      }
    },
    "/check_risk": {
      "post": {
        "summary": "Thesis risk audit",
        "description": "Returns a structured risk report. When x402 billing is enabled, the first unpaid request receives HTTP 402 with PAYMENT-REQUIRED; after payment, retry with PAYMENT-SIGNATURE.",
        "operationId": "checkRisk",
        "parameters": [
          {
            "name": "X-Client",
            "in": "header",
            "required": false,
            "description": "Optional client label for logs and support correlation.",
            "schema": { "type": "string" }
          },
          {
            "name": "PAYMENT-SIGNATURE",
            "in": "header",
            "required": false,
            "description": "x402 v2: base64-encoded JSON PaymentPayload. Required on the paid retry after HTTP 402 when billing is on.",
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["thesis"],
                "properties": {
                  "thesis": {
                    "type": "string",
                    "description": "Investment or trade thesis to stress-test (non-empty)."
                  },
                  "ticker": {
                    "type": "string",
                    "description": "Optional primary symbol or token (e.g. btc, $BTC)."
                  },
                  "tickers": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Optional additional symbols for retrieval."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Audit completed. When x402 settled, PAYMENT-RESPONSE may be present.",
            "headers": {
              "PAYMENT-RESPONSE": {
                "description": "x402 v2: base64 SettlementResponse after successful settlement.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CheckRiskResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request body (e.g. missing or empty thesis) or malformed payment payload.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          },
          "402": {
            "description": "Payment required (no or failed settlement) when x402 billing is enabled.",
            "headers": {
              "PAYMENT-REQUIRED": {
                "description": "Base64-encoded PaymentRequired JSON.",
                "schema": { "type": "string" }
              },
              "PAYMENT-RESPONSE": {
                "description": "On failed verify/settle: base64 SettlementResponse with success=false.",
                "schema": { "type": "string" }
              }
            }
          },
          "500": {
            "description": "Upstream or internal failure after a successful paid gate (e.g. model error). User-facing message in check_risk_response; details in server logs.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          }
        }
      }
    },
    "/check_risk_stocks": {
      "post": {
        "summary": "Single-ticker multi-agent stock view (optional date window)",
        "description": "Runs the internal AI Hedge Fund pipeline for one US ticker. Optional start_date and end_date (YYYY-MM-DD, inclusive) narrow the data window; if both are omitted, the window is the last three calendar months through today; if only end_date is set, start is three months before that end; if only start_date is set, end is today; when both are set, start_date must be on or before end_date. When x402 billing is enabled, uses a separate USDC price from POST /check_risk (see PAYMENT-REQUIRED). Long-running (up to several minutes); clients should use generous HTTP read timeouts.",
        "operationId": "checkRiskStocks",
        "parameters": [
          {
            "name": "X-Client",
            "in": "header",
            "required": false,
            "description": "Optional client label for logs and support correlation.",
            "schema": { "type": "string" }
          },
          {
            "name": "PAYMENT-SIGNATURE",
            "in": "header",
            "required": false,
            "description": "x402 v2: base64-encoded JSON PaymentPayload. Required on the paid retry after HTTP 402 when billing is on.",
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["ticker"],
                "properties": {
                  "ticker": {
                    "type": "string",
                    "description": "Single equity symbol (e.g. AAPL). Do not send a tickers array."
                  },
                  "start_date": {
                    "type": "string",
                    "format": "date",
                    "description": "Optional inclusive start of the analysis window (YYYY-MM-DD). Omit with end_date to default start to three months before end; omit both for last three months through today."
                  },
                  "end_date": {
                    "type": "string",
                    "format": "date",
                    "description": "Optional inclusive end of the analysis window (YYYY-MM-DD). Omit with start_date to default end to today; omit both for last three months through today."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Analysis completed. When x402 settled, PAYMENT-RESPONSE may be present.",
            "headers": {
              "PAYMENT-RESPONSE": {
                "description": "x402 v2: base64 SettlementResponse after successful settlement.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskStocksResponse" }
              }
            }
          },
          "400": {
            "description": "Invalid JSON, missing ticker, disallowed tickers array, invalid date fields, or start_date after end_date.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          },
          "402": {
            "description": "Payment required when x402 billing is enabled for this route.",
            "headers": {
              "PAYMENT-REQUIRED": {
                "description": "Base64-encoded PaymentRequired JSON.",
                "schema": { "type": "string" }
              },
              "PAYMENT-RESPONSE": {
                "description": "On failed verify/settle: base64 SettlementResponse with success=false.",
                "schema": { "type": "string" }
              }
            }
          },
          "502": {
            "description": "Upstream hedge-fund error or unexpected response shape.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          },
          "503": {
            "description": "Upstream hedge-fund unavailable or transport failure.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          },
          "500": {
            "description": "Internal error after a successful paid gate.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckRiskResponse" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CheckRiskResponse": {
        "type": "object",
        "required": [
          "task_id",
          "check_risk_response",
          "narrative_heat",
          "narrative_heat_score"
        ],
        "properties": {
          "task_id": {
            "type": "string",
            "description": "Unique id for this request (support, audits)."
          },
          "check_risk_response": {
            "type": "string",
            "description": "Full report text, or a short user-facing error message on 4xx/5xx."
          },
          "narrative_heat": {
            "type": "string",
            "description": "low | medium | high; empty string on errors."
          },
          "narrative_heat_score": {
            "type": "integer",
            "description": "0–100; 0 on errors."
          }
        }
      },
      "CheckRiskStocksSummary": {
        "type": "object",
        "required": ["ticker", "action", "bullish", "bearish", "neutral"],
        "properties": {
          "ticker": { "type": "string" },
          "action": { "type": "string", "description": "Portfolio decision literal from upstream (e.g. BUY, SELL, HOLD, SHORT, COVER)." },
          "bullish": { "type": "number" },
          "bearish": { "type": "number" },
          "neutral": { "type": "number" }
        }
      },
      "CheckRiskStocksAgentRow": {
        "type": "object",
        "required": ["agent", "signal", "confidence", "reasoning"],
        "properties": {
          "agent": { "type": "string" },
          "signal": { "type": "string", "description": "bullish | bearish | neutral" },
          "confidence": { "type": "number" },
          "reasoning": { "description": "Structured or string reasoning from the analyst signal." }
        }
      },
      "CheckRiskStocksResponse": {
        "type": "object",
        "required": ["task_id", "summary", "agent_analysis"],
        "properties": {
          "task_id": { "type": "string" },
          "summary": { "$ref": "#/components/schemas/CheckRiskStocksSummary" },
          "agent_analysis": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CheckRiskStocksAgentRow" }
          }
        }
      }
    }
  }
}
