{
  "openapi": "3.1.0",
  "info": {
    "title": "Klaudija Public Integration API \u2014 source-verified subset",
    "version": "0.1.0-audit",
    "description": "Sanitized documentation draft derived from local source at 86ca4d24b6133d5a907e688b70599a1bd21f1071, reviewed 2026-09-05. Examples and schemas were not exercised against a live service. Placeholder servers require onboarding configuration. Includes current integration routes only; excludes operator-only configuration, internal recovery, provider webhooks, and advanced client-managed queue protocol. Fast direct chat, provider-neutral routing and voice are outside this current runtime contract."
  },
  "servers": [
    {
      "url": "https://{projectRef}.supabase.co/functions/v1/api",
      "description": "Replace the example project reference with your provisioned API project. No live production target is embedded.",
      "variables": {
        "projectRef": {
          "default": "example-project",
          "description": "Your provisioned Supabase project reference"
        }
      }
    }
  ],
  "tags": [
    {
      "name": "Execution"
    },
    {
      "name": "End-user API"
    },
    {
      "name": "Tenant backend"
    }
  ],
  "paths": {
    "/": {
      "post": {
        "operationId": "post_",
        "summary": "Submit an agent turn",
        "tags": [
          "Execution"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Async pending job, sync final text, or sync binary output.",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "$ref": "#/components/schemas/AsyncAccepted"
                    },
                    {
                      "$ref": "#/components/schemas/SyncText"
                    }
                  ]
                }
              },
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "description": "Query or nonempty file required",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid user token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "Plan allowance, grace and available credits exhausted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BudgetExceeded"
                }
              }
            }
          },
          "403": {
            "description": "No active platform attribution or blocked origin",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Missing/invalid Content-Length or request exceeds 50 MiB",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "File compatibility validation failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Provider/internal error or synchronous completion failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Agent/environment not configured",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Multipart. Use async=1 for HTTP 200 job acceptance. Sync is the default and returns final JSON or a binary artifact. Non-admin clients must send a registered Origin and valid Content-Length <= 52,428,800 bytes. Setup work occurs before async acceptance. Serialize turns per conversation; no general submission idempotency-key contract.",
        "parameters": [
          {
            "name": "Origin",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Registered platform origin; normal browsers set it automatically."
          },
          {
            "name": "Content-Length",
            "in": "header",
            "required": true,
            "schema": {
              "type": "integer",
              "maximum": 52428800
            },
            "description": "Total encoded multipart length, including boundary overhead. Typically set by the HTTP client."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/SubmitForm"
              }
            }
          }
        },
        "servers": [
          {
            "url": "https://{projectRef}.supabase.co/functions/v1/uni-agent",
            "description": "Run function base; do not append another /uni-agent prefix.",
            "variables": {
              "projectRef": {
                "default": "example-project"
              }
            }
          }
        ],
        "x-source-path": "supabase/functions/uni-agent/index.ts"
      }
    },
    "/status/{job_id}": {
      "get": {
        "operationId": "get_status_job_id",
        "summary": "Read job status",
        "tags": [
          "Execution"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/JobStatus"
                }
              }
            }
          },
          "401": {
            "description": "Invalid user token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Job not found or not owned",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Other-owner jobs return 404. May trigger recovery of a stale job. done/error/cancelled are terminal; internal running is pending on the wire.",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "servers": [
          {
            "url": "https://{projectRef}.supabase.co/functions/v1/uni-agent",
            "description": "Run function base; do not append another /uni-agent prefix.",
            "variables": {
              "projectRef": {
                "default": "example-project"
              }
            }
          }
        ],
        "x-source-path": "supabase/functions/uni-agent/index.ts"
      }
    },
    "/result/{job_id}": {
      "get": {
        "operationId": "get_result_job_id",
        "summary": "Download generated output",
        "tags": [
          "Execution"
        ],
        "security": [
          {
            "EndUserBearer": []
          },
          {
            "SignedDownloadToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "Generated binary. Content-Type reflects the artifact.",
            "headers": {
              "Content-Disposition": {
                "schema": {
                  "type": "string"
                },
                "description": "Attachment filename"
              }
            },
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "401": {
            "description": "No valid bearer or signed download token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Job not found, no output, or selected file not in this job",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Job is not done or cancelled",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "410": {
            "description": "Output is no longer available",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Binary success for done/cancelled jobs. Use file query parameter for a particular output belonging to this job. A valid expiring dl token authorizes that job without a bearer. Text-only responses are read from /status. Error responses are JSON.",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "file",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Output file ID recorded on this job."
          },
          {
            "name": "dl",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Job-scoped expiring signed token embedded in generated links."
          }
        ],
        "servers": [
          {
            "url": "https://{projectRef}.supabase.co/functions/v1/uni-agent",
            "description": "Run function base; do not append another /uni-agent prefix.",
            "variables": {
              "projectRef": {
                "default": "example-project"
              }
            }
          }
        ],
        "x-source-path": "supabase/functions/uni-agent/index.ts"
      }
    },
    "/cancel/{job_id}": {
      "post": {
        "operationId": "post_cancel_job_id",
        "summary": "Request cancellation",
        "tags": [
          "Execution"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "job_id": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string",
                      "enum": [
                        "cancelling",
                        "done",
                        "error",
                        "cancelled"
                      ]
                    }
                  },
                  "additionalProperties": true,
                  "required": [
                    "job_id",
                    "status"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Invalid user token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Job not found or not owned",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Cancel recorded but provider interrupt failed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "job_id": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string",
                      "const": "cancelling"
                    },
                    "warning": {
                      "type": "string"
                    },
                    "error": {
                      "type": "string"
                    }
                  },
                  "additionalProperties": true
                }
              }
            }
          }
        },
        "description": "Records cancellation and attempts provider interrupt. Already-terminal jobs are no-op successes. Continue polling until done/error/cancelled. A 502 does not undo the durable cancellation flag.",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "servers": [
          {
            "url": "https://{projectRef}.supabase.co/functions/v1/uni-agent",
            "description": "Run function base; do not append another /uni-agent prefix.",
            "variables": {
              "projectRef": {
                "default": "example-project"
              }
            }
          }
        ],
        "x-source-path": "supabase/functions/uni-agent/index.ts"
      }
    },
    "/me/threads": {
      "get": {
        "operationId": "get_me_threads",
        "summary": "List the current user conversations",
        "tags": [
          "End-user API"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Thread"
                      }
                    },
                    "has_more": {
                      "type": "boolean"
                    }
                  },
                  "additionalProperties": true,
                  "required": [
                    "data",
                    "has_more"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Provisioned end-user actor only. Physical rollover sessions collapse into logical client_session_id threads.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 50
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "x-source-path": "supabase/functions/api/routes/me/chats.ts"
      }
    },
    "/me/threads/{id}/messages": {
      "get": {
        "operationId": "get_me_threads_id_messages",
        "summary": "Read a conversation transcript",
        "tags": [
          "End-user API"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Message"
                      }
                    }
                  },
                  "additionalProperties": true,
                  "required": [
                    "data"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Only owned job rows are read. A foreign/missing thread returns an empty data array. Assistant text appears after done/cancelled. Historical is_from_admin=true denotes the assistant.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Client conversation key, not provider session ID."
          }
        ],
        "x-source-path": "supabase/functions/api/routes/me/chats.ts"
      }
    },
    "/me/usage": {
      "get": {
        "operationId": "get_me_usage",
        "summary": "Read current plan and usage",
        "tags": [
          "End-user API"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Usage"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Provisioned end-user actor only. Returns plan, current billing period, credits, and budget decision. Can degrade to null plan/period. Monthly accounting and next-turn admission checks are distinct from provider-side hard budgets.",
        "x-source-path": "supabase/functions/api/routes/me/chats.ts"
      }
    },
    "/me/mods": {
      "get": {
        "operationId": "get_me_mods",
        "summary": "List available custom MODs",
        "tags": [
          "End-user API"
        ],
        "security": [
          {
            "EndUserBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Mod"
                      }
                    }
                  },
                  "additionalProperties": true,
                  "required": [
                    "data"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Provisioned end-user actor only. Global catalogue of custom skills explicitly opted in with show_on_platform. Not tenant-filtered or guaranteed to be attached to the caller agent.",
        "x-source-path": "supabase/functions/api/routes/me/chats.ts"
      }
    },
    "/admin/platform-users": {
      "get": {
        "operationId": "get_admin_platform_users",
        "summary": "List users on your platform",
        "tags": [
          "Tenant backend"
        ],
        "security": [
          {
            "PlatformApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/PlatformUser"
                      }
                    },
                    "total": {
                      "type": "integer"
                    }
                  },
                  "additionalProperties": true,
                  "required": [
                    "data",
                    "total"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Tenant scope is fixed by the key; parameters cannot widen it.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 100,
              "minimum": 1
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 0,
              "minimum": 0
            }
          },
          {
            "name": "q",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "plan",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "order",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "user_id",
                "email",
                "last_seen",
                "spent",
                "created"
              ]
            }
          },
          {
            "name": "dir",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "asc",
                "desc"
              ]
            }
          }
        ],
        "x-source-path": "supabase/functions/api/routes/admin/platform-users.ts"
      },
      "post": {
        "operationId": "post_admin_platform_users",
        "summary": "Provision or update a platform user",
        "tags": [
          "Tenant backend"
        ],
        "security": [
          {
            "PlatformApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PlatformUser"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "400": {
            "description": "Invalid body or missing user_id / platform_slug-or-source",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "User ID belongs to another platform",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Idempotent on the business user_id, subject to scope. Requires platform_slug or source even with a scoped key. Default provision_memory_store is true. Other-tenant user IDs cannot be moved into your tenant. Optional setup email can create an external notification.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ProvisionUser"
              }
            }
          }
        },
        "x-source-path": "supabase/functions/api/routes/admin/platform-users.ts"
      }
    },
    "/admin/platform-users/{id}/auth": {
      "post": {
        "operationId": "post_admin_platform_users_id_auth",
        "summary": "Exchange a platform key for an end-user token",
        "tags": [
          "Tenant backend"
        ],
        "security": [
          {
            "PlatformApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenExchange"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Key rejected by IP allowlist",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "User not found or not in your platform",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "User lacks auth link or email",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Token mint failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Broker busy/deadline reached; retry after indicated delay.",
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "No request body. User must already have an auth-user link and email. Repeated/concurrent calls may reuse the same Supabase token pair; cache the access token and re-exchange near expiry. Do not independently rotate a shared refresh token.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Your canonical business user ID."
          }
        ],
        "x-source-path": "supabase/functions/api/routes/admin/platform-users.ts"
      }
    },
    "/admin/platform-users/{id}/plan": {
      "patch": {
        "operationId": "patch_admin_platform_users_id_plan",
        "summary": "Change a user plan",
        "tags": [
          "Tenant backend"
        ],
        "security": [
          {
            "PlatformApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PlatformUser"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "400": {
            "description": "Missing, unknown or inactive plan",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "User not found or not in your platform",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Use an existing active plan code supplied during onboarding. Global /admin/plans is operator-only and unavailable to tenant keys.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "plan_code": {
                    "type": "string",
                    "minLength": 1
                  }
                },
                "additionalProperties": true,
                "required": [
                  "plan_code"
                ]
              }
            }
          }
        },
        "x-source-path": "supabase/functions/api/routes/admin/platform-users.ts"
      }
    },
    "/admin/platform-users/{id}/credits": {
      "post": {
        "operationId": "post_admin_platform_users_id_credits",
        "summary": "Adjust a user credit balance",
        "tags": [
          "Tenant backend"
        ],
        "security": [
          {
            "PlatformApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreditResult"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden actor or scope",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "400": {
            "description": "Invalid amount/reason or missing required note",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "User not found or not in your platform",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Nonzero integer cents; admin_manual requires note. reference links external records but is not a documented idempotency key, so do not blindly retry an ambiguous result.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreditAdjustment"
              }
            }
          }
        },
        "x-source-path": "supabase/functions/api/routes/admin/platform-users.ts"
      }
    }
  },
  "components": {
    "securitySchemes": {
      "EndUserBearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Supabase JWT",
        "description": "Provisioned end-user access token; different from a tenant backend key."
      },
      "PlatformApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Platform-API-Key",
        "description": "Backend-only tenant key issued by a Klaudija operator. Optional configured IP/CIDR allowlist."
      },
      "SignedDownloadToken": {
        "type": "apiKey",
        "in": "query",
        "name": "dl",
        "description": "Expiring HMAC-signed authorization scoped to a single job download."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "warning": {
            "type": "string"
          },
          "findings": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {},
              "additionalProperties": true
            }
          }
        },
        "additionalProperties": true,
        "required": [
          "error"
        ]
      },
      "BudgetExceeded": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "reason": {
            "type": "string",
            "enum": [
              "budget_exceeded"
            ]
          },
          "plan_code": {
            "type": "string"
          },
          "spent_cents": {
            "type": "integer"
          },
          "plan_cents": {
            "type": "integer"
          },
          "grace_cents": {
            "type": "integer"
          },
          "credits_cents": {
            "type": "integer"
          },
          "resets_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": true,
        "required": [
          "error",
          "reason",
          "plan_code",
          "spent_cents",
          "plan_cents",
          "grace_cents",
          "credits_cents",
          "resets_at"
        ]
      },
      "AsyncAccepted": {
        "type": "object",
        "properties": {
          "job_id": {
            "type": "string",
            "format": "uuid"
          },
          "session_id": {
            "type": "string"
          },
          "memory_status": {
            "type": "string",
            "enum": [
              "attached",
              "degraded",
              "not_applicable",
              "unknown"
            ]
          },
          "memory_warning": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "const": "pending"
          },
          "anthropic_session_id": {
            "type": "string"
          },
          "rollover": {
            "type": "object",
            "properties": {
              "reason": {
                "type": "string",
                "enum": [
                  "max_model_requests",
                  "max_wall_clock_minutes",
                  "max_cost_usd"
                ]
              },
              "previous_session_id": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "null"
                  }
                ]
              },
              "new_session_id": {
                "type": "string"
              }
            },
            "additionalProperties": true
          }
        },
        "additionalProperties": true,
        "required": [
          "job_id",
          "session_id",
          "status",
          "anthropic_session_id"
        ]
      },
      "PendingStatus": {
        "type": "object",
        "properties": {
          "job_id": {
            "type": "string",
            "format": "uuid"
          },
          "session_id": {
            "type": "string"
          },
          "memory_status": {
            "type": "string",
            "enum": [
              "attached",
              "degraded",
              "not_applicable",
              "unknown"
            ]
          },
          "memory_warning": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "const": "pending"
          }
        },
        "additionalProperties": true,
        "required": [
          "job_id",
          "session_id",
          "status"
        ]
      },
      "FinishedStatus": {
        "type": "object",
        "properties": {
          "job_id": {
            "type": "string",
            "format": "uuid"
          },
          "session_id": {
            "type": "string"
          },
          "memory_status": {
            "type": "string",
            "enum": [
              "attached",
              "degraded",
              "not_applicable",
              "unknown"
            ]
          },
          "memory_warning": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "done",
              "cancelled"
            ]
          },
          "response": {
            "type": "string"
          },
          "action_taken": {
            "type": "string",
            "description": "Legacy action label; not a provider/model selector."
          },
          "file_download_available": {
            "type": "boolean"
          }
        },
        "additionalProperties": true,
        "required": [
          "job_id",
          "session_id",
          "status",
          "response",
          "action_taken",
          "file_download_available"
        ]
      },
      "FailedStatus": {
        "type": "object",
        "properties": {
          "job_id": {
            "type": "string",
            "format": "uuid"
          },
          "session_id": {
            "type": "string"
          },
          "memory_status": {
            "type": "string",
            "enum": [
              "attached",
              "degraded",
              "not_applicable",
              "unknown"
            ]
          },
          "memory_warning": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "const": "error"
          },
          "error": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "required": [
          "job_id",
          "session_id",
          "status",
          "error"
        ]
      },
      "JobStatus": {
        "oneOf": [
          {
            "$ref": "#/components/schemas/PendingStatus"
          },
          {
            "$ref": "#/components/schemas/FinishedStatus"
          },
          {
            "$ref": "#/components/schemas/FailedStatus"
          }
        ],
        "description": "HTTP 200 may contain status=error. Internal running is represented as pending. cancelled is terminal and may contain partial output."
      },
      "SyncText": {
        "type": "object",
        "properties": {
          "response": {
            "type": "string"
          },
          "session_id": {
            "type": "string"
          },
          "anthropic_session_id": {
            "type": "string"
          },
          "action_taken": {
            "type": "string"
          },
          "file_download_available": {
            "type": "boolean"
          },
          "tool_uses": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "memory_status": {
            "type": "string",
            "enum": [
              "attached",
              "degraded",
              "not_applicable",
              "unknown"
            ]
          },
          "memory_warning": {
            "type": "string"
          },
          "rollover": {
            "type": "object",
            "properties": {},
            "additionalProperties": true
          }
        },
        "additionalProperties": true,
        "required": [
          "response",
          "session_id",
          "anthropic_session_id",
          "action_taken",
          "file_download_available"
        ]
      },
      "SubmitForm": {
        "type": "object",
        "properties": {
          "query": {
            "type": "string",
            "description": "Required unless a nonempty file is present. File-only requests get a server-synthesized review prompt."
          },
          "session_id": {
            "type": "string",
            "description": "Stable client conversation key; generated when omitted."
          },
          "reset_session": {
            "type": "string",
            "enum": [
              "true",
              "false"
            ],
            "default": "false"
          },
          "async": {
            "type": "string",
            "description": "Use exactly 1 for asynchronous mode. Omit for synchronous mode.",
            "examples": [
              "1"
            ]
          },
          "file": {
            "type": "string",
            "format": "binary",
            "description": "One file. Entire multipart request including overhead must be <= 52,428,800 bytes. Filename 1\u2013255 characters without reserved/control characters."
          }
        },
        "additionalProperties": true,
        "description": "Multipart form. query or nonempty file is required. No public arbitrary agent selection."
      },
      "TokenExchange": {
        "type": "object",
        "properties": {
          "access_token": {
            "type": "string"
          },
          "refresh_token": {
            "type": "string"
          },
          "token_type": {
            "type": "string",
            "const": "Bearer"
          },
          "expires_in": {
            "type": "integer"
          },
          "user_id": {
            "type": "string",
            "description": "Supabase Auth user ID; different from the business ID in the route path."
          }
        },
        "additionalProperties": true,
        "required": [
          "access_token",
          "refresh_token",
          "token_type",
          "expires_in",
          "user_id"
        ]
      },
      "Thread": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "client_session_id"
          },
          "subject": {
            "type": "string"
          },
          "is_active": {
            "type": "boolean"
          },
          "isEmpty": {
            "type": "boolean",
            "const": false
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": true,
        "required": [
          "id",
          "subject",
          "is_active",
          "isEmpty",
          "created_at",
          "updated_at"
        ]
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "content": {
            "type": "string"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "is_from_admin": {
            "type": "boolean",
            "description": "Historical field: true means assistant reply; false means user message."
          },
          "sender_id": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "additionalProperties": true,
        "required": [
          "id",
          "content",
          "created_at",
          "is_from_admin",
          "sender_id"
        ]
      },
      "Mod": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "description": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "additionalProperties": true,
        "required": [
          "id",
          "title",
          "description"
        ]
      },
      "UsagePlan": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "label": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "cap_cents": {
            "type": "integer"
          },
          "grace_cents": {
            "type": "integer"
          }
        },
        "additionalProperties": true,
        "required": [
          "code",
          "label",
          "cap_cents",
          "grace_cents"
        ]
      },
      "UsagePeriod": {
        "type": "object",
        "properties": {
          "spent_cents": {
            "type": "integer"
          },
          "credits_cents": {
            "type": "integer"
          },
          "zone": {
            "anyOf": [
              {
                "type": "string",
                "enum": [
                  "plan",
                  "grace",
                  "credits"
                ]
              },
              {
                "type": "null"
              }
            ]
          },
          "ok": {
            "type": "boolean"
          },
          "resets_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": true,
        "required": [
          "spent_cents",
          "credits_cents",
          "zone",
          "ok",
          "resets_at"
        ]
      },
      "Usage": {
        "type": "object",
        "properties": {
          "plan": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/UsagePlan"
              },
              {
                "type": "null"
              }
            ]
          },
          "period": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/UsagePeriod"
              },
              {
                "type": "null"
              }
            ]
          },
          "billing_day": {
            "anyOf": [
              {
                "type": "integer",
                "minimum": 1,
                "maximum": 31
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "additionalProperties": true,
        "description": "Accounting degradation can return null plan/period. A budget read is not a hard provider-side spend guarantee.",
        "required": [
          "plan",
          "period",
          "billing_day"
        ]
      },
      "PlatformUser": {
        "type": "object",
        "properties": {
          "user_id": {
            "type": "string"
          },
          "email": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "source": {
            "type": "string"
          },
          "platform_slug": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "plan_code": {
            "type": "string"
          },
          "agent_override_id": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "memory_store_id": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "billing_day": {
            "type": "integer"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "last_seen_at": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ]
          },
          "period_start": {
            "type": "string",
            "format": "date-time"
          },
          "period_end": {
            "type": "string",
            "format": "date-time"
          },
          "spent_cents": {
            "type": "integer"
          },
          "credits_cents": {
            "type": "integer"
          }
        },
        "additionalProperties": true,
        "required": [
          "user_id",
          "email",
          "platform_slug",
          "plan_code",
          "billing_day"
        ]
      },
      "ProvisionUser": {
        "type": "object",
        "properties": {
          "user_id": {
            "type": "string",
            "minLength": 1
          },
          "platform_slug": {
            "type": "string",
            "minLength": 1,
            "description": "Must match the tenant key scope. Send this explicitly even when using a scoped API key."
          },
          "source": {
            "type": "string",
            "description": "Alternative to platform_slug; legacy source enum. Prefer platform_slug."
          },
          "plan_code": {
            "type": "string",
            "description": "Existing configured plan code; defaults are resolved from platform/global settings."
          },
          "email": {
            "type": "string"
          },
          "billing_day": {
            "type": "integer",
            "minimum": 1,
            "maximum": 31
          },
          "provision_memory_store": {
            "type": "boolean",
            "default": true
          },
          "password": {
            "type": "string",
            "minLength": 8,
            "writeOnly": true
          },
          "send_setup_email": {
            "type": "boolean",
            "default": false
          }
        },
        "additionalProperties": true,
        "anyOf": [
          {
            "required": [
              "platform_slug"
            ]
          },
          {
            "required": [
              "source"
            ]
          }
        ],
        "required": [
          "user_id"
        ]
      },
      "CreditAdjustment": {
        "type": "object",
        "properties": {
          "delta_cents": {
            "type": "integer",
            "not": {
              "const": 0
            }
          },
          "reason": {
            "type": "string",
            "enum": [
              "purchase",
              "admin_manual",
              "refund",
              "adjustment"
            ]
          },
          "note": {
            "type": "string"
          },
          "reference": {
            "type": "string",
            "description": "External linkage metadata, not a guaranteed idempotency key."
          }
        },
        "additionalProperties": true,
        "allOf": [
          {
            "if": {
              "properties": {
                "reason": {
                  "const": "admin_manual"
                }
              },
              "required": [
                "reason"
              ]
            },
            "then": {
              "required": [
                "note"
              ],
              "properties": {
                "note": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        ],
        "required": [
          "delta_cents",
          "reason"
        ]
      },
      "CreditResult": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": true
          },
          "new_balance_cents": {
            "type": "integer"
          },
          "balance_before_cents": {
            "type": "integer"
          }
        },
        "additionalProperties": true,
        "required": [
          "ok",
          "new_balance_cents",
          "balance_before_cents"
        ]
      }
    }
  },
  "x-audit": {
    "sourceCommit": "86ca4d24b6133d5a907e688b70599a1bd21f1071",
    "verification": "Local source inspection only. No live calls or production mutations.",
    "legacyOpenApiSource": "docs/for-api-consumers/reference/openapi.yaml",
    "routeRoots": [
      "supabase/functions/api/index.ts",
      "supabase/functions/uni-agent/index.ts"
    ],
    "limits": {
      "maxMultipartBytes": 52428800,
      "inputFilesPerTurn": 1,
      "syncMode": "Default, constrained by Edge worker lifetime; prefer async=1 for long work.",
      "statusEnum": [
        "pending",
        "done",
        "error",
        "cancelled"
      ],
      "plansCatalogAccess": "GET /admin/plans requires operator plan:read; not accessible with a tenant key."
    }
  }
}
