{
 "components": {
  "schemas": {
   "ChatRequest": {
    "properties": {
     "messages": {
      "items": {
       "properties": {
        "content": {
         "type": "string"
        },
        "role": {
         "type": "string"
        }
       },
       "type": "object"
      },
      "type": "array"
     },
     "model": {
      "example": "muse-agent",
      "type": "string"
     },
     "provider": {
      "type": "string"
     },
     "session_id": {
      "type": "string"
     },
     "stream": {
      "type": "boolean"
     },
     "template": {
      "type": "string"
     },
     "text": {
      "type": "string"
     },
     "vars": {
      "type": "object"
     },
     "tools": {
      "type": "array",
      "description": "OpenAI-style function tools (v6 tool calling); validated, 400 on malformed schemas"
     },
     "tool_choice": {
      "description": "auto | none | required | {type:function, function:{name}}"
     },
     "response_format": {
      "type": "object",
      "description": "{type: json_object} or {type: json_schema, json_schema:{schema}}; validated with one repair retry, status in the doapi field"
     },
     "guardrails": {
      "type": "object",
      "description": "{mask_pii: bool, block_secrets: bool}; blocked secrets get an explicit 422"
     },
     "prompt_id": {
      "type": "string",
      "description": "Prompt-library id; rendered with `variables`"
     },
     "variables": {
      "type": "object",
      "description": "Variables for prompt_id rendering"
     },
     "sensitivity": {
      "type": "string",
      "description": "Data-sensitivity label for this call: public | internal | confidential | restricted (v6.343.0, B32). The gateway routes only to providers on the label's allowlist (admin policy, else built-in default: confidential allows mock+official only, restricted allows mock only); a provider outside the list gets 403 + sensitivity.blocked event; an unknown label gets 400. Same label also accepted as header X-DoAPI-Sensitivity; the effective label is the MORE restrictive of the two (header is a floor, body can only raise — v6.351.0, B32-A). No label = previous behaviour. Content-detected PII also raises the label (v6.353.0, B32-B): email/VN phone in the messages raise to confidential, Luhn-valid card / CCCD with a valid province code / secret raise to restricted; detection never lowers a declared label. Response carries doapi.pii_detected {types, raised_to} + header X-DoAPI-PII-Detected (type names only, never values)."
     },
     "route": {
      "type": "object",
      "description": "Capability routing (v6.345.0, B36; v6.347.0, B41 adds auto/fastest + max_latency_ms): only with a virtual model (auto/cheap, auto/fast, auto/balanced, auto/quality, auto/fastest). {requires: {vision|reasoning|tools: bool, min_context_window: int, max_input_price_per_1m|max_output_price_per_1m: number}, max_latency_ms: int} — candidates are the 6 official models, filtered by requires, the key allowlist and the latency budget, then picked by objective; auto/fastest picks by measured EWMA latency (500ms buffer, cheapest in the buffer group) and falls back to declared speed-rank with no measurements yet. No candidate = 400. A route with a concrete model = 400, except max_latency_ms alone which guards that model against a measured latency over the budget."
     },
     "mcp_servers": {
      "type": "array",
      "description": "Gateway-run MCP loop (v6.348.0, B38): list of registered MCP server names (must exist + be admin-approved; the key needs a grant when the server has grants) whose builtin tools the gateway loads, calls the model with, executes itself, and feeds back for up to mcp_max_rounds rounds — instead of returning tool_calls for the client to run. Only builtin tools run: an approved remote server is still 400 (no live transport yet, B38-a). Cannot be combined with tools or stream (400). The tools path never uses ResponseCache, so the loop always bypasses the cache (live tool output is never served stale)."
     },
     "skills": {
      "type": "array",
      "description": "Skills Hub (v6.352.0, B42-S): list of skill refs 'name' (current version) or 'name@N' (pinned version) whose approved content is injected as system messages. Only approved skills run (pending/revoked/missing/unknown version = 400); calls with skills bypass ResponseCache and stay buffered (even with stream=true). Response carries doapi.skills [{name, version}] + header X-DoAPI-Skills."
     },
     "mask_pii": {
      "type": "boolean",
      "description": "Reversible PII masking (v6.354.0, B32-C): true = replace detected PII (email / VN mobile phone / Luhn-valid card / CCCD with a valid province code / secret — same structurally-validated detector as B32-B) with placeholders before the provider call, then restore the original values in the response. The provider only ever sees placeholders; the mapping lives in request memory only, never in DB/log/event/header/cache. Response carries doapi.pii_masked {types, placeholders} + header X-DoAPI-PII-Masked (type names + counts only) + event pii.masked. Non-boolean mask_pii = 400. Masked turns bypass ResponseCache and are not saved to session history; tool-call arguments keep placeholders (tools are outside the gateway). Combines with guardrails.mask_pii (reversible wins) and block_secrets (still 422s first)."
     },
     "scan_response_pii": {
      "type": "boolean",
      "description": "Response PII scan (v6.355.0, B32-D): true = scan the provider's reply text for formatted PII (email / VN mobile phone / Luhn-valid card / CCCD with a valid province code / secret — same structurally-validated detector as B32-B) and report it. Report-only: response carries doapi.response_pii {types} + header X-DoAPI-Response-PII (type names only) + event pii.response_detected; nothing is blocked or redacted by this scan (secrets in replies are already masked by the default reply scrub, B128). The scan runs on the provider's raw reply before B32-C rehydration, so PII the caller sent (restored by rehydration) is not misreported as provider leakage; tool-call arguments are not scanned. Non-boolean scan_response_pii = 400. Live streams report through the event only (headers are sent before the reply exists)."
     },
     "total_timeout": {
      "type": "number",
      "description": "Whole-request time budget (v6.356.0, total_timeout — LiteLLM v1.110.0 total_timeout): seconds 0.1..3600 for the entire non-streaming chat request, retries/fallbacks included; when the budget runs out the gateway answers HTTP 408 (error type request_timeout) with a hint, publishes event request.total_timeout, and abandons the in-flight upstream call (the upstream may still finish server-side, but its result is discarded and the timed-out turn is not billed). Non-numeric/boolean/NaN/out-of-range total_timeout = 400. Live streaming (SSE) + total_timeout = 400 (streams are not covered, like LiteLLM); tools + stream (fake-chunk JSON, really buffered) still gets the budget. On success the response carries doapi.total_timeout {budget_s, elapsed_s} + header X-DoAPI-Total-Timeout (budget=5;elapsed=1.234). Applies to buffered + tools + MCP loop + the structured-output repair retry."
     },
     "mid_stream_fallback": {
      "type": "boolean",
      "description": "Mid-stream fallback continuation (v6.357.0 — LiteLLM v1.109.0 #41127): true = when a live SSE stream fails mid-way (provider error after headers, or the upstream stream ends without the [DONE] sentinel) the gateway continues the same stream on the next provider from provider_order instead of ending with an error event. The switch is announced in-band as an SSE comment ': doapi stream-fallback <from> -> <to>' plus event stream.fallback {provider_from, provider_to, deltas_before, reason}. Only fallback-eligible failures switch (server_error / rate_limit / network / insufficient_balance / content_policy); context_window / client_error / total_timeout fail fast, and a non-ProviderError mid-stream still ends with an error event. Requires provider_order with at least 2 entries and allow_fallbacks true (default); without a usable chain the field is a no-op. The fallback re-runs the full prompt (it does not resume at the exact cut point), so the start of the reply may repeat. Live-stream path only (stream=true, no tools/session/template/response_format/prompt_id/skills); buffered requests already fall back before responding, and private deployments never switch providers mid-stream. Non-boolean mid_stream_fallback = 400."
     },
     "mcp_max_rounds": {
      "type": "integer",
      "description": "Max model rounds in the MCP loop (v6.348.0, B38): integer 1..10, default 10. When the cap is hit with tool calls still pending, the response carries finish_reason=tool_calls with the pending calls so the client can run them; doapi.mcp_loop.truncated=true marks it."
     },
     "auto_router": {
      "type": "object",
      "description": "Auto-router by task type + internal spend share (v6.76.0, B71; live-switch v6.358.0, B1 remainder): {enabled, cost_tier, pinned_model, models, live}. When enabled, the gateway classifies the new user prompt with a local lexical heuristic (coding / mathematics / reasoning / data_extraction / translation / summarization / writing / editing / planning / brainstorming / question_answering / general) and ranks the candidate models by the gateway's OWN internal spend share (trailing spend in the requests table, never community data), filtered by cost_tier (low/medium/high/xhigh/max; unknown tier counts as max). pinned_model wins the recommendation regardless of ranking. Advisory by default: the decision is reported in doapi.auto_router meta + chat.completed event and the live turn keeps the requested model. live=true (requires enabled=true) switches the LIVE turn to the recommended model: the switch happens in the chat handler BEFORE the allowlist / deprecation / GPT-5.1-params / budget / downgrade / sensitivity gates, so every gate runs on the switched model; the response reports doapi.auto_router.live {switched, from_model, to_model, reason} + header X-DoAPI-Auto-Router (\"from->to\", only when switched). live + a virtual model (auto/*) = 400; live with no recommendation (no candidates / no candidate in the cost tier) = 400, fail-closed. Malformed auto_router (non-object, unknown key, non-bool enabled/live, unknown cost_tier, empty pinned_model, malformed models) = 400. Private deployments keep their own model: a live switch does not apply there."
     },
     "agent_id": {
      "type": "string",
      "description": "Agent identity on chat (v6.374.0, B44 remainder): the registered agent_id, always together with task_token (a task token minted for that agent). The gateway verifies the token (agent exists + active, token belongs to that agent, not revoked/expired, bound key if any matches the calling key) before any provider call; a bad token is 403 agent_not_allowed. The requested model must also be in the agent's allowed_models (empty list = no model limit); otherwise 403 agent_model_not_allowed. Both gates must pass (key allowlist + agent allowlist). The response carries doapi.agent {agent_id, allowed_models} + header X-DoAPI-Agent on buffered/tools/live-stream (stream: header only). No agent_id/task_token = previous behaviour."
     },
     "task_token": {
      "type": "string",
      "description": "Task token for agent_id on chat (v6.374.0, B44 remainder): a dtk_... token minted for the agent (POST /admin/api/agents/{id}/tasks). Proves the caller is that registered agent; never logged or returned. Must come with agent_id (one without the other is 400)."
     },
     "run_token": {
      "type": "string",
      "description": "Run token for agent_id on chat (v6.383.0, B12): a drk_... token minted for one (agent, task, session). Binds the turn to that session: the request MUST carry session_id equal to the session the token was minted for, otherwise 403 agent_run_not_allowed. The parent task token must still be valid (not revoked/expired); if task_token is also sent it must be the run token's parent. A run token dies with its run: deleting the session revokes every run token bound to it. No run_token = B44 behaviour (agent_id + task_token only)."
     }
    },
    "required": [
     "messages"
    ],
    "type": "object"
   }
  },
  "securitySchemes": {
   "bearer": {
    "scheme": "bearer",
    "type": "http"
   }
  }
 },
 "info": {
  "description": "OpenAI-compatible gateway over official API / ChatGPT web / muse.ai web. Route by model name: muse-* -> muse, chatgpt-* -> ChatGPT web, gpt-* -> official API.",
  "title": "doapi",
  "version": "6.412.3"
 },
 "openapi": "3.0.3",
 "paths": {
  "/health": {
   "get": {
    "responses": {
     "200": {
      "description": "OK"
     }
    },
    "summary": "Health, queue, cache, jobs"
   }
  },
  "/metrics": {
   "get": {
    "responses": {
     "200": {
      "description": "Prometheus text"
     }
    },
    "summary": "Metrics"
   }
  },
  "/v1/chat/completions": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChatRequest"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Chat completion (or SSE when stream=true)"
     },
     "401": {
      "description": "Bad key — DoAPI key, or BYO X-Provider-Key rejected upstream (401)"
     },
     "429": {
      "description": "Rate limit / quota — gateway-side, or BYO X-Provider-Key rate-limited upstream (429 + Retry-After); every 429 names the limit in error.rate_limited_by + header X-DoAPI-Rate-Limited-By (key_quota/key_rate_limit/spike_limit/run_step_cap/run_velocity_cap/run_tpm_cap/admin_token_lockout/trial_ip_limit/provider_rate_limit)"
     },
     "502": {
      "description": "Provider error (gateway-side; a BYO key rejected upstream returns 401, a BYO key rate-limited upstream returns 429)"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Chat completions (OpenAI-compatible)"
   }
  },
  "/v1/index": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "files": {
          "items": {
           "properties": {
            "content": {
             "type": "string"
            },
            "path": {
             "type": "string"
            }
           },
           "type": "object"
          },
          "type": "array"
         },
         "project": {
          "type": "string"
         }
        },
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Chunks indexed"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Index project files for codebase context"
   }
  },
  "/v1/jobs": {
   "get": {
    "parameters": [
     {
      "in": "query",
      "name": "status",
      "required": false,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "limit",
      "required": false,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Jobs + count"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'read'"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "List local + remote jobs (scope: read)"
   },
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChatRequest"
       }
      }
     },
     "required": true
    },
    "responses": {
     "202": {
      "description": "Job created"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Create async job"
   }
  },
  "/v1/jobs/{id}": {
   "get": {
    "parameters": [
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Job status/result"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Poll job"
   }
  },
  "/v1/models": {
   "get": {
    "responses": {
     "200": {
      "description": "Model list"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "List models (deprecated models annotated with successor)"
   }
  },
  "/v1/keys/me/budget": {
   "get": {
    "responses": {
     "200": {
      "description": "Own key budget, spend estimate and remaining"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Self-serve: read own key budget (opt-in flag not required for read)",
    "description": "Own key budget, spend estimate, remaining and budget change history (?limit=, default 20, max 100)"
   },
   "post": {
    "responses": {
     "200": {
      "description": "Own key budget tightened"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Self-serve: tighten own key budget (admin opt-in, decrease only)"
   }
  },
  "/v1/search": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "project": {
          "type": "string"
         },
         "query": {
          "type": "string"
         },
         "top_k": {
          "type": "integer"
         }
        },
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Relevant chunks"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Search indexed codebase"
   }
  },
  "/v1/usage": {
   "get": {
    "responses": {
     "200": {
      "description": "Usage for the calling key"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Usage of calling key"
   }
  },
  "/v1/utils/{name}": {
   "post": {
    "parameters": [
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "enum": [
        "summarize",
        "translate",
        "rewrite",
        "explain",
        "marketing",
        "test",
        "document",
        "fixerror",
        "commit"
       ],
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "text": {
          "type": "string"
         }
        },
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Utility result"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Quick utilities"
   }
  },
  "security": [
   {
    "bearer": []
   }
  ],
  "servers": [
   {
    "url": "http://127.0.0.1:8787"
   }
  ],
  "/v1/completions": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "model": {
          "type": "string"
         },
         "prompt": {
          "type": "string"
         },
         "provider": {
          "type": "string"
         },
         "suffix": {
          "type": "string"
         }
        },
        "required": [
         "prompt"
        ],
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Completion text (fast tier)"
     },
     "400": {
      "description": "prompt/model/provider/suffix invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'chat'"
     },
     "422": {
      "description": "Web provider too slow for ghost-text; use a fast model"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearerAuth": []
     }
    ],
    "summary": "Ghost-text completion (fast tier only; 422 for web providers)"
   }
  },
  "/v1/redteam/bounties": {
   "get": {
    "summary": "Open red-team bounties (eligible modules)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/redteam/findings": {
   "post": {
    "summary": "Submit a red-team finding; executor verifies live, points only on ground truth",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/concepts": {
   "get": {
    "summary": "Concept map learned from an indexed codebase",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/admin/api/passports": {
   "get": {
    "summary": "List recorded passports (admin)"
   },
   "post": {
    "summary": "Record a passport (admin)",
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Ho chieu da ton tai, tra lai ho chieu"
     },
     "201": {
      "description": "Ho chieu moi (MED/HIGH duoc neo plan_hash vao cay Merkle)"
     },
     "400": {
      "description": "Field sai kieu (lines_added/lines_removed/risk/paths/scope_lock/stages/proof_plan/ticket_id/signature)"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Ho chieu HIGH thieu/sai/het han/dung lai approval HMAC"
     },
     "423": {
      "description": "Kill-switch dang bat: phat hanh ho chieu bi dong bang"
     }
    }
   }
  },
  "/admin/api/merkle": {
   "get": {
    "summary": "Verify passport chain + latest Merkle anchor (admin)"
   }
  },
  "/admin/api/merkle/anchor": {
   "post": {
    "summary": "Anchor current Merkle root (admin)",
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header rieng, khong phai Bearer user)"
     }
    ],
    "responses": {
     "200": {
      "description": "Row neo moi {id, created_at, leaf_count, root, prev_root}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/shadow/run": {
   "post": {
    "summary": "Run a full shadow-mode check against the latest passport baseline (admin)",
    "tags": [
     "trust"
    ],
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header only, never Bearer, never query)"
     }
    ],
    "responses": {
     "200": {
      "description": "Shadow report; comparable=false when no passport baseline exists yet"
     },
     "401": {
      "description": "Wrong admin token"
     }
    }
   }
  },
  "/admin/api/redteam": {
   "get": {
    "summary": "Bounties + findings (admin)"
   }
  },
  "/admin/api/trust/decay": {
   "post": {
    "summary": "Decay positive Trust Capital (admin)",
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header rieng, khong phai Bearer user)"
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "percent": {
          "type": "integer",
          "description": "Phan tram phai mon; vang mat mac dinh 10, clamp 1..50"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Da phai mon {decayed, percent}"
     },
     "400": {
      "description": "percent la bool hoac khong phai so"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/v1/events": {
   "get": {
    "summary": "SSE stream of the caller's job/chat events (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/jobs/{id}/events": {
   "get": {
    "summary": "SSE stream for one job; closes on the terminal event (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/batches": {
   "post": {
    "summary": "Create a batch of up to 50 chat requests (scope: jobs)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/batches/{id}": {
   "get": {
    "summary": "Batch status counts (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/batches/{id}/results": {
   "get": {
    "summary": "Per-item batch results (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/embeddings": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "input": {
          "oneOf": [
           {
            "type": "string"
           },
           {
            "items": {
             "type": "string"
            },
            "maxItems": 100,
            "type": "array"
           }
          ]
         },
         "model": {
          "type": "string"
         },
         "project": {
          "type": "string"
         }
        },
        "required": [
         "input"
        ],
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Embedding vectors (local lexical TF-IDF, 256-dim)"
     },
     "400": {
      "description": "input/project invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'chat'"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearerAuth": []
     }
    ],
    "summary": "Local lexical TF-IDF embeddings, 256-dim feature hashing - NOT neural embeddings (scope: chat)"
   }
  },
  "/v1/rerank": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "documents": {
          "items": {
           "type": "string"
          },
          "maxItems": 100,
          "minItems": 1,
          "type": "array"
         },
         "query": {
          "type": "string"
         },
         "top_n": {
          "type": "integer"
         }
        },
        "required": [
         "query",
         "documents"
        ],
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Documents ranked by TF-IDF cosine relevance_score"
     },
     "400": {
      "description": "query/documents/top_n invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'chat'"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearerAuth": []
     }
    ],
    "summary": "TF-IDF cosine rerank of documents against a query (scope: chat)"
   }
  },
  "/v1/moderate": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "blocklist": {
          "items": {
           "type": "string"
          },
          "type": "array"
         },
         "input": {
          "description": "Alias of text",
          "type": "string"
         },
         "text": {
          "type": "string"
         }
        },
        "required": [
         "text"
        ],
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Moderation report: flagged, categories, pii_found, masked_text"
     },
     "400": {
      "description": "text/blocklist invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'chat'"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearerAuth": []
     }
    ],
    "summary": "Guardrails scan: PII, secrets, blocklist; returns masked text (scope: chat)"
   }
  },
  "/v1/prompts": {
   "get": {
    "summary": "List the caller's prompt library (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "post": {
    "summary": "Create a prompt {name, body} with {{var}} placeholders (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/prompts/{id}": {
   "get": {
    "summary": "Get one prompt (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "put": {
    "summary": "Update a prompt — creates a new version (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "delete": {
    "summary": "Delete a prompt (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/prompts/{id}/render": {
   "post": {
    "summary": "Render a prompt with {variables}; 400 on missing vars (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/sessions": {
   "get": {
    "summary": "List stored conversation sessions (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/sessions/{id}": {
   "get": {
    "summary": "Get a session's messages (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "delete": {
    "summary": "Delete a session (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/sessions/{id}/fork": {
   "post": {
    "summary": "Copy a session to a new id (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/sessions/{id}/compact": {
   "post": {
    "summary": "Summarize old turns into one system message, keep last N (scope: chat)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/admin/api/keys/{id}/rotate": {
   "post": {
    "summary": "Rotate an API key: new secret shown once, old one invalidated (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "New secret shown once; old secret invalidated"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/mcp-tools": {
   "post": {
    "summary": "Set a key's MCP tool allowlist {mcp_tools: [...] | null}; null clears back to unrestricted (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, mcp_tools}; null clears back to unrestricted"
     },
     "400": {
      "description": "mcp_tools khong phai list con cua ten tool MCP, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/mcp/audit": {
   "get": {
    "summary": "Append-only MCP tool-call audit: allowed, denied and unknown-tool attempts (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "limit",
      "required": false,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp/tools": {
   "get": {
    "summary": "MCP tool catalog with governance tiers read/execute (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/webhooks": {
   "get": {
    "summary": "List webhook subscriptions, secrets masked (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Create a webhook subscription {url, events}; secret shown once (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "201": {
      "description": "Subscription created, secret shown once"
     },
     "400": {
      "description": "url/events/secret invalid"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/webhooks/{id}": {
   "post": {
    "summary": "Enable/disable a webhook subscription {active} (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay webhook"
     }
    }
   },
   "delete": {
    "summary": "Delete a webhook subscription (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay webhook"
     }
    }
   }
  },
  "/status": {
   "get": {
    "summary": "Public system status (version, uptime, queues, kill-switch)",
    "tags": [
     "system"
    ],
    "responses": {
     "200": {
      "description": "OK"
     }
    }
   }
  },
  "/badge.svg": {
   "get": {
    "summary": "SVG status badge: DoAPI version + LIVE/FROZEN",
    "tags": [
     "system"
    ],
    "responses": {
     "200": {
      "description": "image/svg+xml"
     }
    }
   }
  },
  "/v1/billing/status": {
   "get": {
    "summary": "Billing status (public, published tiers, manual payment only)",
    "tags": [
     "billing"
    ],
    "responses": {
     "200": {
      "description": "OK"
     }
    }
   }
  },
  "/v1/billing/upgrade-demo": {
   "post": {
    "summary": "Demo upgrade request (simulated only, charges nothing, changes nothing)",
    "tags": [
     "billing"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "quota/budget_usd invalid"
     }
    }
   }
  },
  "/v1/keys/trial": {
   "post": {
    "summary": "Self-serve trial key (only when admin enabled DOAPI_TRIAL_KEYS_ENABLED=1, else 403)",
    "tags": [
     "keys"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "201": {
      "description": "Trial key issued (shown once)"
     },
     "403": {
      "description": "Trial keys not enabled"
     },
     "429": {
      "description": "One trial key per IP per 24h"
     }
    }
   }
  },
  "/admin/api/invariants/live": {
   "get": {
    "summary": "Latest invariant stage run per invariant + shadow radar + kill-switch state",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "tags": [
     "trust"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/invariants/run": {
   "post": {
    "summary": "Run the invariant stage now, record it and stream invariant.stage events",
    "tags": [
     "trust"
    ],
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header only, never Bearer, never query)"
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "fuzz_trials": {
          "type": "integer",
          "description": "Default 10, clamped 0..40; a JSON bool returns 400"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Invariant stage report, recorded for the live dashboard"
     },
     "400": {
      "description": "Invalid JSON body or fuzz_trials is a bool"
     },
     "401": {
      "description": "Wrong admin token"
     }
    }
   }
  },
  "/admin/api/killswitch": {
   "get": {
    "summary": "Kill-switch state (derived from the hash-chained event log) + recent events",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "tags": [
     "trust"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/killswitch/activate": {
   "post": {
    "summary": "Freeze the system (evidence-first: invariant stage snapshot required)",
    "tags": [
     "trust"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Scope/target khong phai chuoi hoac stage sai kieu, hoac body khong phai JSON hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "409": {
      "description": "He thong dang dong bang san"
     },
     "422": {
      "description": "Thieu bang chung, pham vi sai, hoac scope module thieu target"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "scope": {
          "type": "string",
          "enum": [
           "module",
           "global"
          ],
          "description": "module: chan phat hanh moi, chat van chay; global: chan ca chat. Vang mat mac dinh module."
         },
         "target": {
          "type": "string",
          "description": "Ten module bi dong bang; scope module bat buoc khac rong."
         },
         "note": {
          "type": "string"
         },
         "stage": {
          "type": "object",
          "description": "Stage bat bien kem bang chung; vang mat thi server tu chay toan bo stage roi moi dong bang."
         },
         "use_latest_stage": {
          "type": "boolean",
          "description": "Dung stage moi nhat thay vi chay lai."
         },
         "fuzz_trials": {
          "type": "integer",
          "description": "Chi doc o duong fallback chay stage."
         }
        }
       }
      }
     }
    }
   }
  },
  "/admin/api/killswitch/release": {
   "post": {
    "summary": "Release the kill-switch (admin only, recorded as a chained event)",
    "tags": [
     "trust"
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Body khong phai JSON hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "409": {
      "description": "He thong khong o trang thai dong bang"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "note": {
          "type": "string"
         }
        }
       }
      }
     }
    }
   }
  },
  "/admin/api/approvals/ticket": {
   "post": {
    "summary": "Cap ticket phe duyet cho mot plan_hash (admin) — chu so huu ky HMAC tren ticket trong 10 phut",
    "responses": {
     "201": {
      "description": "ticket + signing_payload"
     },
     "400": {
      "description": "risk/paths/scope_lock/plan_hash/actor sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     },
     "422": {
      "description": "plan khong hop le (approval_error)"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "plan_hash": {
          "type": "string",
          "description": "plan_hash co san; vang mat thi server tu tinh tu proof_plan/paths/risk/tier/scope_lock"
         },
         "proof_plan": {
          "type": "object",
          "description": "plan chung minh de server tinh plan_hash khi khong truyen plan_hash"
         },
         "paths": {
          "type": "array",
          "items": {
           "type": "string"
          }
         },
         "risk": {
          "type": "object",
          "description": "doi tuong risk, dung risk.tier khi khong truyen tier"
         },
         "tier": {
          "type": "string"
         },
         "scope_lock": {
          "type": "array",
          "items": {
           "type": "string"
          }
         },
         "actor": {
          "type": "string",
          "description": "mac dinh admin"
         }
        }
       }
      }
     }
    }
   }
  },
  "/admin/api/approvals/verify": {
   "post": {
    "summary": "Kiem tra chu ky HMAC cua mot approval ticket (khong tieu thu ticket)",
    "responses": {
     "200": {
      "description": "{valid, reason}"
     },
     "400": {
      "description": "ticket_id dict/list hoac signature khong phai chuoi"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "ticket_id": {
          "type": "string"
         },
         "signature": {
          "type": "string",
          "description": "vang mat thi server dung chuoi rong"
         },
         "plan_hash": {
          "type": "string",
          "description": "vang mat thi khong rang buoc plan"
         }
        }
       }
      }
     }
    }
   }
  },
  "/admin/api/passports/{id}/anchor-proof": {
   "get": {
    "summary": "Bang chung Merkle cho plan_hash cua ho chieu MED/HIGH (B2)",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "leaf_index, root, proof"
     }
    }
   }
  },
  "/admin/api/lawforge/draft": {
   "post": {
    "summary": "LawForge NL: mo ta bang ngon ngu tu nhien -> du thao luat co cau truc (chi proposed, validate chat)",
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "description": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "draft da luu"
     },
     "400": {
      "description": "description phai la chuoi"
     },
     "401": {
      "description": "Sai admin token"
     },
     "422": {
      "description": "provider khong tra cau truc hop le"
     },
     "502": {
      "description": "Provider loi"
     }
    }
   }
  },
  "/admin/api/redteam/simulate": {
   "post": {
    "summary": "RedTeam Simulator v2: chay 5 kich ban vao ban sandbox (simulated:true); escape tu dong ren du thao luat",
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "per-scenario caught/escaped + evidence hashes"
     },
     "401": {
      "description": "Sai admin token"
     },
     "500": {
      "description": "Mo phong khong tra ket qua"
     },
     "504": {
      "description": "Mo phong qua thoi gian (240s)"
     }
    }
   }
  },
  "/admin/api/webhooks/test": {
   "post": {
    "summary": "Ban su kien thu doapi.test co ky HMAC toi mot subscription, tra ma giao hang + xem truoc chu ky",
    "responses": {
     "200": {
      "description": "delivery result"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong thay webhook subscription"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ]
   }
  },
  "/v1/sessions/{id}/share": {
   "post": {
    "summary": "Tao link chia se chi-doc cho session: /s/<token> (thu hoi duoc)",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "201": {
      "description": "{url}"
     }
    }
   },
   "delete": {
    "summary": "Thu hoi moi link chia se cua session",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{revoked}"
     }
    }
   }
  },
  "/s/{token}": {
   "get": {
    "summary": "Trang phien chia se cong khai (chi doc, khong can key)",
    "parameters": [
     {
      "name": "token",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "HTML"
     }
    }
   }
  },
  "/v1/prompts/{id}/versions": {
   "get": {
    "summary": "List version history of a prompt (append-only, oldest first) + current_version (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/prompts/{id}/versions/{version}": {
   "get": {
    "summary": "Get one immutable prompt version {version} (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/prompts/{id}/rollback": {
   "post": {
    "summary": "Rollback a prompt: create a new version copying version {version} (history is never rewritten) (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills": {
   "get": {
    "summary": "List skills in the hub (scope: prompts; shared library, ?status=pending|approved|revoked)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "post": {
    "summary": "Create a skill {name, description, content} — always starts pending (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}": {
   "get": {
    "summary": "Get one skill (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   },
   "put": {
    "summary": "Update a skill — creates a new version (scope: prompts; name cannot change)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}/versions": {
   "get": {
    "summary": "List version history of a skill (append-only, oldest first) + current_version (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}/versions/{version}": {
   "get": {
    "summary": "Get one immutable skill version {version} (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}/rollback": {
   "post": {
    "summary": "Rollback a skill: create a new version copying version {version} (history is never rewritten) (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}/approve": {
   "post": {
    "summary": "Approve a skill (pending/revoked -> approved; only approved skills run in chat) (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/{id}/revoke": {
   "post": {
    "summary": "Revoke a skill (pending/approved -> revoked) (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/skills/import": {
   "post": {
    "summary": "Import a portable skill {name, description, content} — always starts pending (scope: prompts)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/prompts/gallery": {
   "get": {
    "summary": "Prompt Gallery: bo template mau cong khai (>= 8)",
    "responses": {
     "200": {
      "description": "gallery pack"
     }
    }
   }
  },
  "/v1/prompts/import": {
   "post": {
    "summary": "Import mot template tu gallery vao thu vien cua key dang goi",
    "responses": {
     "201": {
      "description": "prompt da tao"
     }
    }
   }
  },
  "/v1/decisions": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "engine": {
          "enum": [
           "rule",
           "provider"
          ],
          "type": "string"
         },
         "input": {
          "maxLength": 10000,
          "type": "string"
         },
         "model": {
          "description": "Model chay engine=provider (vd gpt-4o); vang mat = doapi-default. Gate ngan sach uoc theo model nay va rescue B24 ha cap khi 402. engine=rule bo qua.",
          "type": "string"
         },
         "questions": {
          "items": {
           "properties": {
            "name": {
             "type": "string"
            },
            "options": {
             "items": {
              "type": "string"
             },
             "type": "array"
            },
            "type": {
             "enum": [
              "boolean",
              "choice",
              "score"
             ],
             "type": "string"
            }
           },
           "required": [
            "name",
            "type"
           ],
           "type": "object"
          },
          "maxItems": 20,
          "type": "array"
         }
        },
        "required": [
         "input",
         "questions"
        ],
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "One typed decision per question (value + probability/scores)"
     },
     "400": {
      "description": "input/questions/engine/model invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "402": {
      "description": "Budget khong du (ke ca sau rescue B24)"
     },
     "403": {
      "description": "Key lacks scope 'chat'"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearerAuth": []
     }
    ],
    "summary": "Decisions API (v6.81 B141, v6.83 B142): typed boolean/choice/score answers only, no free text (scope: chat); body engine rule (default) or provider (strict-parsed provider JSON, rule fallback)"
   }
  },
  "/v1/runs/{session_id}/trace": {
   "get": {
    "summary": "Run trace: per-step cost spans of one session run (scope: read)",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/v1/runs/{session_id}/trace/otlp": {
   "get": {
    "summary": "Run trace as OTLP/HTTP JSON for any collector (scope: read); ?min_cost_usd=&min_steps= tail sampling",
    "security": [
     {
      "bearerAuth": []
     }
    ]
   }
  },
  "/admin/api/private-deployments": {
   "get": {
    "summary": "List private deployments (admin token)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Register a private deployment: slug, provider, model, region us/eu, allowed_teams (admin token)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "required": [
         "slug",
         "provider",
         "model",
         "region",
         "allowed_teams"
        ],
        "properties": {
         "slug": {
          "type": "string"
         },
         "provider": {
          "type": "string",
          "enum": [
           "official",
           "muse",
           "browser",
           "chatgpt",
           "mock"
          ]
         },
         "model": {
          "type": "string"
         },
         "region": {
          "type": "string",
          "enum": [
           "us",
           "eu"
          ]
         },
         "allowed_teams": {
          "type": "array",
          "items": {
           "type": "string"
          }
         }
        }
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "Deployment da dang ky"
     },
     "400": {
      "description": "Slug/provider/model/region/allowed_teams khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "409": {
      "description": "Slug da dang ky"
     }
    }
   }
  },
  "/admin/api/keys/{id}/budget-history": {
   "get": {
    "summary": "Admin: read one key's budget change history",
    "responses": {
     "200": {
      "description": "Newest-first budget change rows for one key, plus its current budget_usd, spent_usd_est, remaining_usd and self_serve_budget"
     },
     "400": {
      "description": "Id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "integer"
      }
     },
     {
      "in": "query",
      "name": "limit",
      "required": false,
      "schema": {
       "type": "integer"
      }
     }
    ]
   }
  },
  "/v1/compare": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChatRequest"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Per-provider results"
     },
     "400": {
      "description": "messages/providers/model invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Arena compare: same prompt across up to 3 providers (scope: chat)"
   }
  },
  "/v1/estimate": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChatRequest"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "prompt/output tokens + cost_usd_est (+ budget check when budgeted)"
     },
     "400": {
      "description": "provider/messages/max_output_tokens invalid"
     },
     "401": {
      "description": "Bad key"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Cost estimate before spending (scope: chat)"
   }
  },
  "/v1/rules": {
   "get": {
    "parameters": [
     {
      "in": "query",
      "name": "project",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Project rules"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'read'"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Read project rules (scope: read)"
   },
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "project": {
          "type": "string"
         },
         "rules": {
          "type": "string"
         }
        },
        "type": "object"
       }
      }
     },
     "required": true
    },
    "responses": {
     "200": {
      "description": "Rules saved"
     },
     "400": {
      "description": "project/rules invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'prompts'"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Save project rules (scope: prompts)"
   }
  },
  "/v1/run-guard/usage": {
   "get": {
    "parameters": [
     {
      "in": "query",
      "name": "session_id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Own run-cap usage for one session"
     },
     "400": {
      "description": "Missing session_id"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'read'"
     },
     "404": {
      "description": "Session not found or run guard not configured"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Own run-guard usage for one session (scope: read)"
   }
  },
  "/v1/workers": {
   "get": {
    "responses": {
     "200": {
      "description": "Workers"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'read'"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Worker fleet registry (scope: read)"
   }
  },
  "/v1/system": {
   "get": {
    "responses": {
     "200": {
      "description": "Name, version, provider, features, counters"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'read'"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "System info: version, features, counters (scope: read)"
   }
  },
  "/v1/remote-jobs": {
   "post": {
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChatRequest"
       }
      }
     },
     "required": true
    },
    "responses": {
     "202": {
      "description": "Remote job created"
     },
     "400": {
      "description": "messages/provider/webhook_url invalid"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'jobs'"
     },
     "429": {
      "description": "Rate limit / quota"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Create a job for a local worker (scope: jobs)"
   }
  },
  "/v1/jobs/{id}/cancel": {
   "post": {
    "parameters": [
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Cancelled job"
     },
     "401": {
      "description": "Bad key"
     },
     "403": {
      "description": "Key lacks scope 'jobs'"
     },
     "409": {
      "description": "Job cannot be cancelled (done/failed/not found)"
     }
    },
    "security": [
     {
      "bearer": []
     }
    ],
    "summary": "Cancel a queued or running remote job (scope: jobs)"
   }
  },
  "/admin/api/keys": {
   "get": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "All keys ordered by id, revoked included for audit (key_prefix only, no secret)"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "summary": "Admin: list all API keys (key_prefix only, no secret)"
   },
   "post": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "201": {
      "description": "Key created; raw key shown once only, only its hash is kept"
     },
     "400": {
      "description": "Thieu name, name/scopes/quota/budget_usd/team/expires_at/audit_retention_days sai kieu, quota am, hoac expires_at o qua khu"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "summary": "Admin: create API key (raw key shown once; optional expires_at epoch, audit_retention_days 1..3650)"
   }
  },
  "/admin/api/keys/usage": {
   "get": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Live keys only: requests, quota, budget, spent, remaining, used pct, near_budget at >=80% (key_prefix only, no secret)"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "summary": "Admin: usage per live key, heaviest first (P3-1)"
   }
  },
  "/admin/api/alerts/quota-check": {
   "post": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "warned + already_warned key names"
     },
     "400": {
      "description": "Body phai la JSON object"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Dynamic config da tat"
     }
    },
    "summary": "Admin: scan live keys and fire quota.warning now (P3-2)",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     },
     "required": false
    }
   }
  },
  "/admin/api/backup/export": {
   "get": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "doapi-backup format_version 1, no secrets, no request logs"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "summary": "Admin: export backup (keys prefix only, templates, team budgets, model deprecations) (P3-3)"
   }
  },
  "/admin/api/export": {
   "get": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "version, exported_at, stats, keys metadata (prefix only), templates, cache stats"
     },
     "401": {
      "description": "Sai admin token"
     }
    },
    "summary": "Admin: legacy export (version, stats, keys metadata, templates, cache) (P3-17)"
   }
  },
  "/admin/api/backup/restore": {
   "post": {
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "restored + skipped per section; keys never created, secrets never imported"
     },
     "400": {
      "description": "backup/dry_run/section invalid"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Dynamic config da tat"
     }
    },
    "summary": "Admin: restore a backup from export without replacing the DB file (P3-3)",
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "properties": {
         "backup": {
          "type": "object"
         },
         "dry_run": {
          "type": "boolean"
         }
        },
        "type": "object"
       }
      }
     },
     "required": false
    }
   }
  },
  "/admin/api/analytics": {
   "get": {
    "summary": "Analytics by provider/model/errors/today by hour (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/sales-funnel": {
   "get": {
    "summary": "Sales funnel for admin: trials issued by day, active keys by tier, trial conversion, estimated monthly revenue (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/stats": {
   "get": {
    "summary": "Gateway stats: totals, today, latency, cost estimate, per day/provider/key (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/customer-spend": {
   "get": {
    "summary": "Per-customer spend for one key: list or single customer (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "key",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "customer_id",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Thieu key"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key"
     }
    }
   }
  },
  "/admin/api/requests": {
   "get": {
    "summary": "Recent gateway requests, newest first (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "limit",
      "required": false,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/egress-blocks": {
   "get": {
    "summary": "Blocked egress webhook URLs + probe flags (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "limit",
      "required": false,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/providers": {
   "get": {
    "summary": "Admin: provider readiness (mock/official/browser/muse)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{data: [{name, ready, note}]} for mock/official/browser/muse; ready reflects local config only, no provider call"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-deprecations": {
   "get": {
    "summary": "Admin: model deprecation map (deprecated -> successor)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deprecations: [{deprecated, successor, note, retirement_date}]} sorted by deprecated name; retirement_date '' = unknown retirement date; empty list when no edges"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: set or clear one model deprecation edge {deprecated, successor, note?, retirement_date?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Edge set (or cleared when successor is null); returns deprecated/successor/note/retirement_date (or cleared) + full deprecations list"
     },
     "400": {
      "description": "deprecated rong / successor rong / vong lap / tu tro / note sai kieu / retirement_date sai dinh dang hoac khong phai ngay lich that"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-aliases": {
   "get": {
    "summary": "Admin: model alias registry (alias -> target)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{aliases: [{alias, target, note, created_at, updated_at}]} sorted by alias name; empty list when no aliases"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: set or replace one model alias {alias, target, note?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Alias set; returns alias/target/note/created_at/updated_at + full aliases list"
     },
     "400": {
      "description": "alias rong / sai ky tu / trung ten model that / target khong phai model da quang ba hoac alias da co / vong lap / note sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-aliases/{alias}": {
   "delete": {
    "summary": "Admin: delete one model alias",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "alias",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: alias}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay alias"
     }
    }
   }
  },
  "/admin/api/sensitivity-policies": {
   "get": {
    "summary": "Admin: sensitivity policies (label -> allowed providers) + labels + built-in defaults",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{policies: [{label, providers, updated_by, updated_at}], labels: [public, internal, confidential, restricted], defaults: {label: [providers]}}; a label with no row uses its built-in default"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: set or replace one sensitivity policy {label, providers, updated_by?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Policy set; returns policy + full policies list"
     },
     "400": {
      "description": "label la / providers rong / provider khong biet / updated_by sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/sensitivity-policies/{label}": {
   "delete": {
    "summary": "Admin: delete one sensitivity policy (label returns to its built-in default)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "label",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: label}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay policy"
     }
    }
   }
  },
  "/admin/api/customer-budget": {
   "post": {
    "summary": "Admin: set or clear one customer's USD cap on one key {key, customer_id, budget_usd}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{key, customer_id, budget_usd, spent_usd_est}; budget null or 0 clears the cap (stored 0), spent never reset"
     },
     "400": {
      "description": "Thieu key/customer_id/budget_usd, key dict/list, cap am hoac label xau"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key"
     }
    }
   }
  },
  "/admin/api/run-guard/usage": {
   "get": {
    "summary": "Admin: run-cap usage fractions for one session ?key=&session_id=",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "key",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "session_id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{key, session_id, dimensions, thresholds_claimed}; read-only, polling never moves the run"
     },
     "400": {
      "description": "Thieu key hoac session_id"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key hoac run guard chua cau hinh"
     }
    }
   }
  },
  "/admin/api/cache/clear": {
   "post": {
    "summary": "Admin: clear the gateway response cache",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{cleared: true}; next requests call the provider again instead of serving cache"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Dynamic config da tat"
     }
    }
   }
  },
  "/admin/api/templates": {
   "get": {
    "summary": "Admin: list prompt templates",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{data: [{id, slug, name, prompt, created_at}]} ordered by slug"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: save or upsert one prompt template {slug, prompt, name?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{saved: slug}; name defaults to slug when omitted"
     },
     "400": {
      "description": "Thieu slug hoac prompt, slug/prompt/name sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Dynamic config da tat"
     }
    }
   }
  },
  "/admin/api/templates/{slug}": {
   "delete": {
    "summary": "Admin: delete one prompt template by slug",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "slug",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: slug}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "403": {
      "description": "Dynamic config da tat"
     },
     "404": {
      "description": "Khong tim thay template"
     }
    }
   }
  },
  "/admin/api/keys/{id}/allowed-models": {
   "post": {
    "summary": "Admin: set or clear one key's model allowlist {allowed_models: [...] | null, allowlist_report_only?: bool} (P3-19)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, allowed_models, allowlist_report_only} read back from the row; null clears the list, absent allowed_models leaves it alone"
     },
     "400": {
      "description": "allowed_models khong phai list ten model (chuoi khong rong), allowlist_report_only khong phai bool, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/egress-allowlist": {
   "post": {
    "summary": "Admin: set or clear one key's egress allowlist {egress_allowlist: [...] | null} (required) (P3-19)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, egress_allowlist} read back from the row; null clears the SSRF exception list"
     },
     "400": {
      "description": "Thieu egress_allowlist, entry khong phai hostname / IP / CIDR hop le, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/budget": {
   "post": {
    "summary": "Admin: set or clear one key's USD budget {budget_usd: number | null} (P3-20)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, previous_budget_usd, budget_usd, spent_usd_est, remaining_usd, self_serve_budget, budget_history}; null clears the cap to 0 (unlimited), spent is never reset"
     },
     "400": {
      "description": "budget_usd khong phai so huu han khong am, budget_usd la bool, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/daily-limits": {
   "post": {
    "summary": "Admin: set one key's daily caps (rpd/tpd) {daily_requests?, daily_tokens?} (v6.404.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, daily_requests, daily_tokens, requests_today, tokens_today, daily_requests_remaining, daily_tokens_remaining, daily_resets_at}; absent field keeps its cap, null clears to 0 (unlimited), usage today is never reset"
     },
     "400": {
      "description": "ca 2 truong deu vang mat, gia tri khong phai so nguyen >= 0, gia tri la bool, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/self-serve-budget": {
   "post": {
    "summary": "Admin: enable or disable self-serve budget tightening for one key {enabled: bool} (P3-20)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, self_serve_budget}; opt-in alone changes no enforcement, it only unlocks POST /v1/keys/me/budget for that key's holder"
     },
     "400": {
      "description": "enabled vang mat hoac khong phai bool, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/team": {
   "post": {
    "summary": "Admin: move one key into a team {team: string | null} (P3-20)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, team}; null clears the team, the key's own budget still applies on top of the team cap"
     },
     "400": {
      "description": "team khong phai ten hop le [A-Za-z0-9_-] bat dau bang chu/so, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/mcp-approval-tools": {
   "post": {
    "summary": "Admin: set or clear one key's MCP approval list {mcp_approval_tools: [...] | null} (P3-20)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, mcp_approval_tools}; a listed tool then only runs with a one-time HMAC ticket, null clears the list"
     },
     "400": {
      "description": "mcp_approval_tools khong phai list con cua cac ten tool MCP, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/run-guard": {
   "post": {
    "summary": "Admin: set one key's per-session run guard, absent fields stay unchanged (P3-20)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "current run guard of the key; null/0 resets a numeric field to unlimited, run_guard_mode null resets to block, downgrade_model null clears it"
     },
     "400": {
      "description": "field so am / la bool / khong huu han, run_guard_mode khong phai block, warn hoac throttle, downgrade_model khong phai chuoi, id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/admin/api/teams/{team}/chargeback": {
   "get": {
    "summary": "Team chargeback/showback report (admin token)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "team",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "since",
      "required": false,
      "schema": {
       "type": "integer",
       "minimum": 0
      }
     },
     {
      "in": "query",
      "name": "until",
      "required": false,
      "schema": {
       "type": "integer",
       "minimum": 0
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK"
     },
     "400": {
      "description": "Team khong hop le hoac since/until sai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/teams/{team}/budget": {
   "get": {
    "summary": "Team budget/cap report (admin token)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "team",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{team, budget_usd, spent_usd_est, boost_usd, boost_expires_at, effective_budget_usd, period_budgets}; budget 0 = unlimited, spent is current-membership estimate"
     },
     "400": {
      "description": "Team khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: set or clear one team's USD budget/cap {budget_usd, boost_usd?, boost_expires_at?, period_budgets?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "team",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "budget_usd": {
          "type": "number",
          "description": "null hoac 0 xoa cap co dinh ve 0 (khong gioi han); so phai huu han, khong am, khong phai bool"
         },
         "boost_usd": {
          "type": "number",
          "description": "null xoa boost tam thoi (khong kem expiry); so can boost_expires_at"
         },
         "boost_expires_at": {
          "type": "integer",
          "description": "epoch tuong lai cua boost; expiry ma khong kem boost tra 400"
         },
         "period_budgets": {
          "type": "object",
          "description": "keyed hourly/daily/weekly/monthly; null hoac 0 xoa cap ky do, so > 0 dat cap"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{team, previous_budget_usd, budget_usd, spent_usd_est, boost_usd, boost_expires_at, effective_budget_usd, remaining_usd_est, period_budgets}; spent never reset; bare POST (no cap field) resets base cap to 0"
     },
     "400": {
      "description": "Team khong hop le, budget/boost/period cap xau, expiry khong kem boost, hoac boost khong kem expiry"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp/approvals/ticket": {
   "post": {
    "summary": "Cap ticket phe duyet MCP mot lan cho 1 tool + 1 key (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "required": [
         "tool",
         "key_name"
        ],
        "properties": {
         "tool": {
          "type": "string",
          "description": "1 trong doapi_chat/doapi_summarize/doapi_search_codebase/doapi_usage"
         },
         "key_name": {
          "type": "string",
          "description": "ten key dang song, chua thu hoi"
         },
         "actor": {
          "type": "string",
          "description": "mac dinh admin"
         }
        }
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "ticket_id + plan_hash + tool + key_name + definition + definitionHash + issued_at + expires_at + signing_payload"
     },
     "400": {
      "description": "tool khong phai ten tool MCP, thieu key_name, actor dict/list"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     },
     "422": {
      "description": "cap ticket loi (approval_error)"
     }
    }
   }
  },
  "/admin/api/keys/{id}/tier": {
   "post": {
    "summary": "Apply a pricing tier to one key (admin, manual payment)",
    "tags": [
     "billing"
    ],
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "required": [
         "tier"
        ],
        "properties": {
         "tier": {
          "type": "string",
          "enum": [
           "starter",
           "pro",
           "team"
          ]
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Tier applied: quota/budget/scopes copied from the tier table"
     },
     "400": {
      "description": "Invalid key id or tier"
     },
     "401": {
      "description": "Wrong admin token"
     },
     "404": {
      "description": "Key not found or revoked"
     }
    }
   }
  },
  "/admin/api/keys/{id}/expiry": {
   "post": {
    "summary": "Admin: set or clear one key's expiry {expires_at} (epoch seconds; null/0 = never expires)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "expires_at": {
          "type": "integer"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, expires_at, expired: false}; takes effect on the next call"
     },
     "400": {
      "description": "Id key khong hop le, thieu expires_at, hoac expires_at phai la epoch giay o tuong lai (bool/khong phai so/am/qua khu tra 400)"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    }
   }
  },
  "/v1/payos/webhook": {
   "post": {
    "summary": "payOS webhook (frame OFF by default; HMAC signature is the only auth; applies tier to a pre-registered order)",
    "tags": [
     "billing"
    ],
    "responses": {
     "200": {
      "description": "Verified; outcome in body (applied / duplicate / mismatch / unmatched)"
     },
     "401": {
      "description": "Invalid webhook signature"
     },
     "503": {
      "description": "payOS frame not enabled"
     }
    }
   }
  },
  "/admin/api/payos/orders": {
   "post": {
    "summary": "Admin: register a payOS order before the customer pays (admin token header)",
    "tags": [
     "billing"
    ],
    "responses": {
     "201": {
      "description": "Order registered (returns orderCode for the payOS payment link)"
     },
     "400": {
      "description": "Bad key_id / tier / amount_vnd"
     },
     "401": {
      "description": "Bad admin token"
     },
     "404": {
      "description": "Key not found"
     }
    }
   },
   "get": {
    "summary": "Admin: list payOS orders for reconciliation",
    "tags": [
     "billing"
    ],
    "responses": {
     "200": {
      "description": "Orders + payos_enabled flag"
     },
     "401": {
      "description": "Bad admin token"
     }
    }
   }
  },
  "/admin/api/payos/events": {
   "get": {
    "summary": "Admin: payOS webhook event log, newest first (cap 200)",
    "tags": [
     "billing"
    ],
    "responses": {
     "200": {
      "description": "Event log"
     },
     "401": {
      "description": "Bad admin token"
     }
    }
   }
  },
  "/admin/api/keys/{id}/tier-cycle": {
   "get": {
    "summary": "Admin: read one key's tier cycle (current 30-day cycle + closed-cycle history)",
    "responses": {
     "200": {
      "description": "Key id, tier, current cycle (null when untiered or revoked), quota, quota_used, budget_usd, budget_spent_usd_est and closed-cycle history"
     },
     "400": {
      "description": "Id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key (hoac da thu hoi)"
     }
    },
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ]
   }
  },
  "/admin/api/playground": {
   "post": {
    "summary": "Admin: run a chat probe without a user key (playground)",
    "tags": [
     "admin"
    ],
    "parameters": [
     {
      "name": "X-Admin-Token",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header only, never Bearer, never query)"
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "messages": {
          "type": "array",
          "items": {
           "type": "object",
           "properties": {
            "role": {
             "type": "string"
            },
            "content": {
             "type": "string"
            }
           }
          }
         },
         "model": {
          "type": "string"
         },
         "text": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{reply, provider, latency_ms}"
     },
     "400": {
      "description": "messages invalid"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/requests.csv": {
   "get": {
    "summary": "Export recent gateway requests as CSV (admin)",
    "tags": [
     "admin"
    ],
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      },
      "description": "Admin token (header only, never Bearer, never query)"
     }
    ],
    "responses": {
     "200": {
      "description": "text/csv: header id,ts,key,provider,model,status,latency_ms,prompt_chars,reply_chars,error; up to 1000 newest rows",
      "content": {
       "text/csv": {
        "schema": {
         "type": "string"
        }
       }
      }
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/keys/{id}": {
   "delete": {
    "summary": "Revoke an API key permanently: old secret invalidated, no new secret issued (admin)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Revoked: {revoked: id}"
     },
     "400": {
      "description": "Id key khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key"
     }
    }
   }
  },
  "/admin/api/agents": {
   "get": {
    "summary": "Admin: list agents (?status=active|revoked)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agents: [...]} sap theo agent_id; trang thai loc sai tra 400"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: register one agent {agent_id, name, owner?, allowed_tools, allowed_models?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "Agent da dang ky; tra {agent}"
     },
     "400": {
      "description": "Validation that bai (xem R400) hoac allowed_tools rong"
     },
     "409": {
      "description": "agent_id da ton tai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}": {
   "get": {
    "summary": "Admin: one agent + its task tokens",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agent, tasks: [...]} — dang public, khong mang token/hash"
     },
     "404": {
      "description": "agent khong ton tai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}/tasks": {
   "post": {
    "summary": "Admin: mint task token {task_id, tools, expires_in_s?, key_name?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "Tra {task_token (chi hien 1 lan), task}; store chi luu sha256"
     },
     "400": {
      "description": "Validation that bai, tools vuot pham vi agent, hoac key_name khong ton tai"
     },
     "409": {
      "description": "agent da thu hoi hoac (agent_id, task_id) da co token"
     },
     "404": {
      "description": "agent khong ton tai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}/revoke": {
   "post": {
    "summary": "Admin: revoke one agent",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agent, revoked: true}; task token dang song cua agent het hieu luc ngay"
     },
     "409": {
      "description": "agent da thu hoi"
     },
     "404": {
      "description": "agent khong ton tai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}/tasks/{task_id}/revoke": {
   "post": {
    "summary": "Admin: revoke one task token",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agent_id, task_id, revoked: true}"
     },
     "404": {
      "description": "agent hoac task token khong ton tai / da thu hoi"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-deprecations/impact": {
   "get": {
    "summary": "Admin: model deprecation impact — which keys still call each retired model",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deprecations: [{deprecated, successor, note, retirement_date, status, days_remaining, total_calls, affected: [{key_name, calls, first_seen, last_seen}]}]} sorted by deprecated name; status unknown/retired/imminent/upcoming, days_remaining null when unknown; affected=[] when no key called the old name"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp-tools/fingerprints": {
   "get": {
    "summary": "Admin: MCP tool fingerprints — pinned baselines + drift status + policy",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{tools: [{tool_name, fingerprint, status: pinned|drifted, first_seen, last_seen, drift_first_seen, approved_by}], policy: report|deny} sorted by tool_name; never serves a definition snapshot"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp-tools/fingerprints/{name}/approve": {
   "post": {
    "summary": "Admin: re-pin a drifted MCP tool's current definition as the new baseline {actor?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "actor": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{tool: {tool_name, fingerprint, status, first_seen, last_seen, drift_first_seen, approved_by}, policy} — the re-pinned baseline"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "tool khong ton tai"
     }
    }
   }
  },
  "/admin/api/mcp-registry": {
   "get": {
    "summary": "Admin: MCP server registry (list) + builtin server + statuses + transports; ?status=pending|approved|revoked filters",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "status",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{servers: [{name, endpoint, transport, environment, owner, tools, status, approved_by, approved_at, created_at, updated_at, review_interval_days, next_review_at, last_reviewed_at}], builtin: doapi-builtin, statuses, transports}"
     },
     "400": {
      "description": "status loc khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: register one MCP server {name, endpoint, transport, environment, owner, tools?} — always starts pending",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "Server registered (pending); returns server + full servers list"
     },
     "400": {
      "description": "ten trung / ten la / endpoint, transport, environment, owner, tools la / tool dung do voi builtin hoac server khac"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/latency-stats": {
   "get": {
    "summary": "Admin: measured latency stats per (provider, model) — EWMA (alpha 0.2) + samples + errors + p50/p95 from the in-memory window (null when the window is empty after restart)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{stats: [{provider, model, ewma_ms, samples, errors, last_seen, p50_ms, p95_ms}], config: {alpha, buffer_ms, window, min_samples_for_p95}}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "delete": {
    "summary": "Admin: reset the latency stats table + in-memory windows (auto/fastest falls back to declared speed-rank until new samples arrive)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: <so hang da xoa>}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-leaderboard": {
   "get": {
    "summary": "Admin: model leaderboard from real gateway traffic in a window (?window_days=7, 1..90) — success rate + p50/p95 latency + estimated $/1k tokens + persistent EWMA; models with < 10 turns are flagged reliable=false and ranked after fully-sampled models; fallback rate not reported (no persistent attempt ledger yet)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "window_days",
      "required": false,
      "schema": {
       "type": "integer",
       "default": 7,
       "minimum": 1,
       "maximum": 90
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{window_days, window_start, window_end, generated_at, reliable_min_turns, models: [{rank, provider, model, turns, success_rate, p50_ms, p95_ms, ewma_ms, errors_lifetime, est_spend_usd, est_tokens, usd_per_1k_tokens, reliable, attempted_turns, fallback_turns, fallback_rate}], fallback: {turns, fallback_turns, success_after_fallback, failed_turns, failures_by_provider, fallback_rate, success_after_fallback_rate}, note}"
     },
     "400": {
      "description": "window_days khong phai so nguyen hoac ngoai khoang 1..90"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/route-health": {
   "get": {
    "summary": "Admin: route health warnings — single-provider route, fallback disabled, single point of failure (1 provider + fallback off), provider in recent 30s outage window or circuit breaker open; default route (primary + DOAPI_FALLBACK) or a proposed route via ?provider_order=a,b&allow_fallbacks=false",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "provider_order",
      "required": false,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "allow_fallbacks",
      "required": false,
      "schema": {
       "type": "boolean",
       "default": true
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{providers, available, allow_fallbacks, warnings: [{code, severity, message, provider?}], healthy}"
     },
     "400": {
      "description": "provider_order chua provider khong biet hoac allow_fallbacks khong phai true/false"
     },
     "401": {
      "description": "Sai admin token"
     },
     "500": {
      "description": "Khong tinh duoc suc khoe route mac dinh"
     }
    }
   }
  },
  "/admin/api/usage/by-label": {
   "get": {
    "summary": "Admin: usage grouped by team/customer label in a window — per-label turns, success rate, avg latency, est tokens/spend, key count, top models; team by the key's CURRENT label, customer by the per-request customer_id (v6.364.0, B40)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "window_days",
      "required": false,
      "schema": {
       "type": "integer",
       "default": 7
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{window_days, window_start, window_end, generated_at, teams: [{label, total_requests, success_requests, success_rate, avg_latency_ms, est_prompt_tokens, est_completion_tokens, est_spend_usd, key_count, top_models}], customers: [same], no_team, no_customer, summary, note}"
     },
     "400": {
      "description": "window_days khong phai so nguyen 1..90"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/performance-alerts": {
   "get": {
    "summary": "Admin: performance alerts — today's traffic, error rate and p95 latency vs the previous 7-day baseline (UTC days); traffic_spike / error_rate_high / latency_high when thresholds are crossed, empty alerts + note when the baseline has < 10 turns (v6.372.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{day, baseline_days, today: {requests, errors, error_rate, success, p50_ms, p95_ms}, baseline: {requests, requests_per_day, errors, error_rate, success, p50_ms, p95_ms}, alerts: [{code, severity, message, ...numbers}], healthy, note, generated_at}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/model-deprecations/known": {
   "get": {
    "summary": "Admin: known model retirements from OpenAI's official deprecations page (snapshot 2026-11-23) checked against the live deprecation table; gpt-4o + gpt-4o-mini shut down 2027-01-14 (OpenAI recommends gpt-6-luna), gpt-5.1 shuts down 2027-04-01 (gpt-6-sol); read-only, no edge is registered by this call (v6.377.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{snapshot_date, source, known: [{model, shutdown_date, notice_date, openai_replacement, registered, registered_successor, status, days_remaining}], unregistered, advertised_not_listed, generated_at, note}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/web-screen/open": {
   "post": {
    "summary": "Admin: open (or reuse) the VPS browser screen session on the muse profile and navigate to muse.ai so the owner can log in via /admin/web-login (v6.366.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{open, url, state, detail} — state from the live DOM probe (logged_in | not_logged_in | ui_changed)"
     },
     "401": {
      "description": "Sai admin token"
     },
     "502": {
      "description": "browser unavailable (no DISPLAY / Playwright missing / profile locked)"
     }
    }
   }
  },
  "/admin/api/web-screen/screenshot": {
   "get": {
    "summary": "Admin: JPEG screenshot of the VPS browser screen session + X-DoAPI-Screen-State / X-DoAPI-Screen-Url headers (v6.366.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "image/jpeg of the current screen (1280x800 viewport)"
     },
     "401": {
      "description": "Sai admin token"
     },
     "502": {
      "description": "browser unavailable"
     }
    }
   }
  },
  "/admin/api/web-screen/input": {
   "post": {
    "summary": "Admin: forward one input action to the screen session {type: click|type|key|goto|reload, x, y, text, key, url} — keys are an allowlist, goto is https-only (v6.366.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{ok, type} (+ state/url for goto/reload)"
     },
     "400": {
      "description": "invalid action (unknown type/key, non-https url, empty text)"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/web-screen/status": {
   "get": {
    "summary": "Admin: screen session status without launching the browser {open, url, idle_s, profile, profile_exists} (v6.366.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "screen session status"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/web-screen/close": {
   "post": {
    "summary": "Admin: close the screen session browser and free its RAM; the on-disk profile (login) persists (v6.366.0)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{open: false, closed}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "502": {
      "description": "close failed in the browser queue"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}": {
   "get": {
    "summary": "Admin: one MCP server + its key grants",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{server: {name, endpoint, transport, environment, owner, tools, status, approved_by, approved_at, created_at, updated_at}, access: [{server_name, key_name, granted_by, granted_at}]}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}/approve": {
   "post": {
    "summary": "Admin: approve one MCP server {approved_by?, review_interval_days?} (pending/revoked -> approved); publishes mcp.server_approved",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "approved_by": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{server, servers} — the approved server + full list"
     },
     "400": {
      "description": "approved_by sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}/revoke": {
   "post": {
    "summary": "Admin: revoke one MCP server {actor?} (pending/approved -> revoked); its tools hide from tools/list and fail tools/call; publishes mcp.server_revoked",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "actor": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{server, servers} — the revoked server + full list"
     },
     "400": {
      "description": "actor sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}/access": {
   "post": {
    "summary": "Admin: grant one key access to one MCP server {key_name, granted_by?}; a server with >=1 grant only serves granted keys",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{grant, access} — the grant + full grant list of the server"
     },
     "400": {
      "description": "key_name rong / granted_by sai kieu"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}/access/{key_name}": {
   "delete": {
    "summary": "Admin: revoke one key's access to one MCP server",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "key_name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: key_name, server, access} — remaining grants"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server / grant"
     }
    }
   }
  },
  "/admin/api/provider-budgets": {
   "get": {
    "summary": "Admin: provider budgets — USD cap + spend per provider",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{budgets: [{provider, budget_usd, spent_usd_est}]} sorted by provider; providers without a cap row do not appear (uncapped = unlimited)"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/provider-budgets/{provider}": {
   "post": {
    "summary": "Admin: set or clear one provider's USD cap {budget_usd}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "provider",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "budget_usd": {
          "type": "number"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{provider, budget_usd, spent_usd_est}; spent is never reset by this call"
     },
     "400": {
      "description": "provider khong hop le / budget_usd phai la so huu han khong am (bool tra 400)"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "delete": {
    "summary": "Admin: clear one provider's USD cap",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "provider",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: provider}"
     },
     "400": {
      "description": "provider khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay provider budget"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}/run-tokens": {
   "get": {
    "summary": "Admin: list run tokens of an agent",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Tra {run_tokens: [run_id, agent_id, task_id, session_id, expires_at, revoked_at, created_at, created_by]} — khong bao gio mang token tho/hash"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "agent khong ton tai"
     }
    }
   },
   "post": {
    "summary": "Admin: mint run token {task_id, session_id, expires_in_s?}",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object"
       }
      }
     }
    },
    "responses": {
     "201": {
      "description": "Tra {run_token (chi hien 1 lan), run}; store chi luu sha256; TTL mac dinh 900s, toi da 3600s"
     },
     "400": {
      "description": "Validation that bai (task_id/session_id/expires_in_s sai dang)"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "agent khong ton tai"
     },
     "409": {
      "description": "agent da thu hoi, hoac task token cha khong ton tai/da thu hoi/het han"
     }
    }
   }
  },
  "/admin/api/agents/{agent_id}/run-tokens/{run_id}/revoke": {
   "post": {
    "summary": "Admin: revoke one run token",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "Tra {agent_id, run_id, revoked: true}"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "agent khong ton tai, hoac run token khong ton tai/da thu hoi"
     }
    }
   }
  },
  "/admin/api/mcp-registry/reviews": {
   "get": {
    "summary": "Admin: MCP registry re-review schedule — overdue + due-soon servers + approval latency (v6.387.0, B37)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{overdue: [{name, next_review_at, owner}], due_soon: [...], due_soon_days: 7, review_interval_days_default: 90, approval_latency: {approved_count, mean_seconds, median_seconds, pending_count, oldest_pending_age_seconds}}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp-registry/{name}/review": {
   "post": {
    "summary": "Admin: re-review an approved MCP server — resets last_reviewed_at + next_review_at (v6.387.0, B37)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "reviewed_by": {
          "type": "string"
         },
         "review_interval_days": {
          "type": "integer"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{server, servers}"
     },
     "400": {
      "description": "review_interval_days sai"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay server"
     },
     "409": {
      "description": "Server chua duoc duyet"
     }
    }
   }
  },
  "/admin/api/mcp-shadow-sightings": {
   "get": {
    "summary": "Admin: list shadow MCP sightings — servers seen running outside the gateway (v6.387.0, B37)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "status",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{sightings: [{id, name, endpoint, environment, source, observed_by, note, status, registered_server, in_registry, created_at, resolved_at, resolved_by}]}"
     },
     "400": {
      "description": "status loc khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   },
   "post": {
    "summary": "Admin: record a shadow MCP sighting {name, endpoint?, environment?, source? (laptop|ci|traffic|other), observed_by?, note?} (v6.387.0, B37)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "name": {
          "type": "string"
         },
         "endpoint": {
          "type": "string"
         },
         "environment": {
          "type": "string"
         },
         "source": {
          "type": "string"
         },
         "observed_by": {
          "type": "string"
         },
         "note": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{sighting, sightings}"
     },
     "400": {
      "description": "name/source sai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/mcp-shadow-sightings/{id}/resolve": {
   "post": {
    "summary": "Admin: resolve a shadow MCP sighting {resolved_by?, status? (investigated|resolved)} (v6.387.0, B37)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "resolved_by": {
          "type": "string"
         },
         "status": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{sighting, sightings}"
     },
     "400": {
      "description": "id/status sai"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay sighting"
     }
    }
   }
  },
  "/admin/api/keys/{id}/audit-retention": {
   "post": {
    "summary": "Admin: set per-key audit retention {audit_retention_days (1..3650, null = global default)} (v6.388.0, B69)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "audit_retention_days": {
          "type": "integer"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, name, audit_retention_days, effective_retention_days}"
     },
     "400": {
      "description": "Id/audit_retention_days sai"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key"
     }
    }
   }
  },
  "/admin/api/keys/{id}/audit-export": {
   "get": {
    "summary": "Admin: export one key's audit package — requests + mcp_audit rows with sha256 chain + merkle_root (?format=json|csv, ?window_days=7) (v6.388.0, B69)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "format",
      "required": false,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "window_days",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{key_name, window_days, window_start, window_end, generated_at, retention_days, request_count, mcp_audit_count, row_count, rows, sha256, merkle_root, note} (hoac CSV khi format=csv)"
     },
     "400": {
      "description": "Id/format/window_days sai"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay key"
     }
    }
   }
  },
  "/admin/api/audit/prune": {
   "post": {
    "summary": "Admin: prune request logs now, per each key's audit-retention policy (v6.388.0, B69)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{pruned_by_key, total_pruned, global_default_days, note}"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/sod-conflict-pairs": {
   "get": {
    "summary": "Admin: list SoD conflict pairs declared by admin + current SoD mode (v6.400.0, B67)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{pairs [{tool_a, tool_b, declared_by, declared_at}], count, mode enforce|report}"
     }
    }
   },
   "post": {
    "summary": "Admin: declare one SoD conflict pair {tool_a, tool_b, declared_by?} — both must be served tools, different names (v6.400.0, B67)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "tool_a": {
          "type": "string"
         },
         "tool_b": {
          "type": "string"
         },
         "declared_by": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{tool_a, tool_b, declared_by, declared_at, created, affected_keys [key names already holding both tools]}"
     },
     "201": {
      "description": "Cap moi duoc khai bao"
     },
     "400": {
      "description": "tool_a/tool_b sai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/sod-conflict-pairs/{tool_a}/{tool_b}": {
   "delete": {
    "summary": "Admin: remove one declared SoD conflict pair (v6.400.0, B67)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "tool_a",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "tool_b",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: [tool_a, tool_b]}"
     },
     "400": {
      "description": "Duong dan cap sai"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay cap"
     }
    }
   }
  },
  "/admin/api/allocations": {
   "get": {
    "summary": "Admin: list allocation rules (team/time-window -> model) + count; optional ?team= filter (v6.401.0, B36-b)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "team",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{allocations [{id, team, days, start_min, end_min, model, priority, created_by, created_at}], count}"
     }
    }
   },
   "post": {
    "summary": "Admin: declare one allocation rule {team, model, days?, start, end, priority?, created_by?} — model must be a real model in the capability table, start/end HH:MM UTC (v6.401.0, B36-b)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "team": {
          "type": "string"
         },
         "model": {
          "type": "string"
         },
         "days": {
          "type": "string"
         },
         "start": {
          "type": "string"
         },
         "end": {
          "type": "string"
         },
         "priority": {
          "type": "integer"
         },
         "created_by": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{id, team, days, start_min, end_min, model, priority, created_by, created_at}"
     },
     "201": {
      "description": "Rule moi duoc khai bao"
     },
     "400": {
      "description": "team/model/gio sai"
     },
     "401": {
      "description": "Sai admin token"
     }
    }
   }
  },
  "/admin/api/allocations/{id}": {
   "delete": {
    "summary": "Admin: remove one allocation rule by id (v6.401.0, B36-b)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "id",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: id}"
     },
     "400": {
      "description": "Id khong hop le"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay rule"
     }
    }
   }
  },
  "/admin/api/agent-cards": {
   "get": {
    "summary": "Admin: list registered A2A agent cards + count (v6.402.0, B58-a)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agent_cards [card + id/created_by/created_at/updated_at], count}"
     }
    }
   },
   "post": {
    "summary": "Admin: register one A2A agent card {name, description, version, supportedInterfaces, capabilities?, defaultInputModes?, defaultOutputModes?, skills, provider?, documentationUrl?, iconUrl?, securitySchemes?, created_by?} — A2A v1.0, v0.3 card (top-level url + protocolVersion) is normalized to one interface (v6.402.0, B58-a)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "requestBody": {
     "required": false,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "name": {
          "type": "string"
         },
         "description": {
          "type": "string"
         },
         "version": {
          "type": "string"
         },
         "supportedInterfaces": {
          "type": "string"
         },
         "capabilities": {
          "type": "string"
         },
         "defaultInputModes": {
          "type": "string"
         },
         "defaultOutputModes": {
          "type": "string"
         },
         "skills": {
          "type": "string"
         },
         "provider": {
          "type": "string"
         },
         "documentationUrl": {
          "type": "string"
         },
         "iconUrl": {
          "type": "string"
         },
         "securitySchemes": {
          "type": "string"
         },
         "created_by": {
          "type": "string"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "{name, description, version, supportedInterfaces, capabilities, defaultInputModes, defaultOutputModes, skills, id, created_by, created_at, updated_at}"
     },
     "201": {
      "description": "Card moi duoc dang ky"
     },
     "400": {
      "description": "Card sai chuan A2A"
     },
     "401": {
      "description": "Sai admin token"
     },
     "409": {
      "description": "Ten card da ton tai"
     }
    }
   }
  },
  "/admin/api/agent-cards/{name}": {
   "delete": {
    "summary": "Admin: remove one A2A agent card by name (v6.402.0, B58-a)",
    "parameters": [
     {
      "in": "header",
      "name": "X-Admin-Token",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{deleted: name}"
     },
     "400": {
      "description": "Thieu name"
     },
     "401": {
      "description": "Sai admin token"
     },
     "404": {
      "description": "Khong tim thay card"
     }
    }
   }
  },
  "/a2a/cards": {
   "get": {
    "summary": "Public: A2A agent card catalog (registered cards + count); ?skill= filters by skill id or tag, ?q= substring search over name/description/skills (v6.402.0, B58-a)",
    "parameters": [
     {
      "in": "query",
      "name": "skill",
      "required": false,
      "schema": {
       "type": "string"
      }
     },
     {
      "in": "query",
      "name": "q",
      "required": false,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{agent_cards [card + id/created_by/created_at/updated_at], count}"
     }
    }
   }
  },
  "/a2a/cards/{name}": {
   "get": {
    "summary": "Public: one A2A agent card by name, with ETag + Cache-Control (If-None-Match match returns 304) (v6.402.0, B58-a)",
    "parameters": [
     {
      "in": "path",
      "name": "name",
      "required": true,
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "{name, description, version, supportedInterfaces, capabilities, defaultInputModes, defaultOutputModes, skills, id, created_by, created_at, updated_at}"
     },
     "304": {
      "description": "Card khong doi (ETag)"
     },
     "404": {
      "description": "Khong tim thay card"
     }
    }
   }
  }
 }
}
