{
  "swagger": "2.0",
  "info": {
    "title": "MasiDyn Cloud API (Copilot Studio Connector)",
    "version": "1.0.0",
    "description": "Multi-tenant 24/7 MasiDyn Cloud API hosted on https://api.masidyn.dev and backed by Google Gemini. Connect Microsoft Copilot Studio agents via OAuth 2.0 Authorization Code flow against https://account.masidyn.dev or a MasiDyn Bearer token."
  },
  "host": "api.masidyn.dev",
  "basePath": "/",
  "schemes": [
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "securityDefinitions": {
    "oauth2": {
      "type": "oauth2",
      "flow": "accessCode",
      "authorizationUrl": "https://account.masidyn.dev/oauth/authorize?resource=https%3A%2F%2Fapi.masidyn.dev%2Fv1",
      "tokenUrl": "https://account.masidyn.dev/oauth/token",
      "scopes": {
        "openid": "OpenID Connect user identity",
        "profile": "Read user profile metadata",
        "masidyn.workspace.read": "Access MasiDyn Cloud workspace, models, and chat completions"
      },
      "description": "Authorization Code with mandatory PKCE S256. Register an exact callback URI at /dashboard/apps. The authorization URL requests the cloud API resource and the issuer retains it for the code exchange. Grant masidyn.workspace.read and use the returned access_token. Reconnect older connections authorized without this resource."
    },
    "bearerAuth": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header",
      "description": "Pass Bearer <access_token> from the OAuth code exchange or an existing MasiDyn service credential. Portal session cookies and OIDC id_token values are not API access tokens."
    }
  },
  "paths": {
    "/health": {
      "get": {
        "operationId": "GetCloudHealth",
        "summary": "Check MasiDyn Cloud API health",
        "description": "Checks whether MasiDyn Cloud API, OIDC issuer, database, and Gemini gateway are online and ready.",
        "responses": {
          "200": {
            "description": "Cloud gateway health status",
            "schema": {
              "$ref": "#/definitions/HealthResponse"
            }
          }
        }
      }
    },
    "/v1/models": {
      "get": {
        "operationId": "ListCloudModels",
        "summary": "List available MasiDyn Cloud models",
        "description": "Lists all available multi-tenant MasiDyn Cloud chat and embedding model identifiers.",
        "responses": {
          "200": {
            "description": "Model list",
            "schema": {
              "$ref": "#/definitions/ModelListResponse"
            }
          }
        }
      }
    },
    "/v1/identity": {
      "get": {
        "operationId": "GetCloudIdentity",
        "summary": "Get MasiDyn Cloud deployment identity",
        "description": "Returns MasiDyn Cloud API deployment mode, backing provider, MCP URL, and supported routes.",
        "responses": {
          "200": {
            "description": "Cloud deployment identity",
            "schema": {
              "$ref": "#/definitions/IdentityResponse"
            }
          }
        }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "operationId": "CreateChatCompletion",
        "summary": "Send a chat prompt to MasiDyn Cloud",
        "description": "Executes a multi-turn or single-turn chat completion in MasiDyn Cloud (backed by Google Gemini) and returns the assistant response.",
        "security": [
          {
            "oauth2": [
              "openid",
              "profile",
              "masidyn.workspace.read"
            ]
          },
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": true,
            "description": "Chat completion request payload containing model ID and ordered messages.",
            "schema": {
              "$ref": "#/definitions/ChatRequest"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Chat completion result from MasiDyn Cloud",
            "schema": {
              "$ref": "#/definitions/ChatResponse"
            }
          },
          "400": {
            "description": "Invalid request payload",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "401": {
            "description": "Missing or invalid Bearer credential",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              },
              "WWW-Authenticate": {
                "type": "string",
                "description": "Bearer challenge with resource_metadata."
              }
            }
          },
          "403": {
            "description": "Operation is not authorized",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "405": {
            "description": "HTTP method is not supported",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "413": {
            "description": "Request body exceeds the limit",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "422": {
            "description": "Provider rejected the supplied input",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "429": {
            "description": "Request limit reached",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "500": {
            "description": "Unexpected server failure",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "502": {
            "description": "Unusable upstream response",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "503": {
            "description": "Provider or service unavailable",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "504": {
            "description": "Upstream request timed out",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "default": {
            "description": "Other cloud gateway failure",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          }
        }
      }
    },
    "/v1/embeddings": {
      "post": {
        "operationId": "CreateEmbeddings",
        "summary": "Generate text embeddings in MasiDyn Cloud",
        "description": "Generates dense numerical embedding vectors for input text strings using MasiDyn Cloud.",
        "security": [
          {
            "oauth2": [
              "openid",
              "profile",
              "masidyn.workspace.read"
            ]
          },
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": true,
            "description": "Embedding model name and input strings.",
            "schema": {
              "$ref": "#/definitions/EmbeddingRequest"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Generated embedding vectors",
            "schema": {
              "$ref": "#/definitions/EmbeddingResponse"
            }
          },
          "400": {
            "description": "Invalid request payload",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "401": {
            "description": "Missing or invalid Bearer credential",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              },
              "WWW-Authenticate": {
                "type": "string",
                "description": "Bearer challenge with resource_metadata."
              }
            }
          },
          "403": {
            "description": "Operation is not authorized",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "405": {
            "description": "HTTP method is not supported",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "413": {
            "description": "Request body exceeds the limit",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "422": {
            "description": "Provider rejected the supplied input",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "429": {
            "description": "Request limit reached",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "500": {
            "description": "Unexpected server failure",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "502": {
            "description": "Unusable upstream response",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "503": {
            "description": "Provider or service unavailable",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "504": {
            "description": "Upstream request timed out",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          },
          "default": {
            "description": "Other cloud gateway failure",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            },
            "headers": {
              "X-Request-ID": {
                "type": "string",
                "format": "uuid",
                "description": "Server-generated identifier matching the JSON request_id."
              }
            }
          }
        }
      }
    },
    "/api/harvest/nodes": {
      "get": {
        "operationId": "FindFreeFoodNear",
        "summary": "Find free food pantries, soup kitchens, and community fridges anywhere on Earth",
        "description": "Searches OpenStreetMap and HarvestBridge community check-ins globally by city, postal code, or coordinates and returns open/freshness status without requiring login.",
        "parameters": [
          {
            "in": "query",
            "name": "q",
            "type": "string",
            "required": false,
            "description": "City, address, or postal code anywhere in the world (e.g. London, UK or 10001)."
          },
          {
            "in": "query",
            "name": "radiusKm",
            "type": "number",
            "required": false,
            "description": "Search radius in kilometers (1 to 50, default 15)."
          },
          {
            "in": "query",
            "name": "category",
            "type": "string",
            "required": false,
            "description": "Optional category filter: food_bank, soup_kitchen, community_fridge, or pantry."
          }
        ],
        "responses": {
          "200": {
            "description": "Nearby food resources with distance and honest stock freshness status",
            "schema": {
              "$ref": "#/definitions/HarvestSearchResponse"
            }
          },
          "400": {
            "description": "Missing or invalid location query",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "HealthResponse": {
      "type": "object",
      "properties": {
        "status": {
          "type": "string",
          "description": "Overall cloud API status (ok or degraded)."
        },
        "product": {
          "type": "string",
          "description": "Service product identifier."
        },
        "version": {
          "type": "string",
          "description": "Service version."
        },
        "timestamp": {
          "type": "string",
          "description": "ISO-8601 server timestamp."
        }
      }
    },
    "ModelItem": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "description": "MasiDyn Cloud model ID such as masidyn-auto or masidyn-gemini."
        },
        "object": {
          "type": "string",
          "description": "Object type (model)."
        },
        "owned_by": {
          "type": "string",
          "description": "Model owner."
        },
        "provider": {
          "type": "string",
          "description": "Backing provider (google-gemini)."
        },
        "capability": {
          "type": "string",
          "description": "Primary capability (chat or embeddings)."
        },
        "description": {
          "type": "string",
          "description": "Human-readable description of the model."
        }
      }
    },
    "ModelListResponse": {
      "type": "object",
      "properties": {
        "object": {
          "type": "string",
          "description": "List envelope type."
        },
        "data": {
          "type": "array",
          "description": "Available cloud model items.",
          "items": {
            "$ref": "#/definitions/ModelItem"
          }
        }
      }
    },
    "IdentityResponse": {
      "type": "object",
      "properties": {
        "object": {
          "type": "string",
          "description": "Identity envelope type."
        },
        "product": {
          "type": "string",
          "description": "Product name."
        },
        "deployment_mode": {
          "type": "string",
          "description": "Active deployment mode (cloud)."
        },
        "provider": {
          "type": "string",
          "description": "Active inference provider."
        },
        "cloud_api_url": {
          "type": "string",
          "description": "Public HTTPS base URL for MasiDyn Cloud."
        },
        "mcp_url": {
          "type": "string",
          "description": "Remote MCP endpoint URL for Copilot Studio and MCP hosts."
        },
        "default_model": {
          "type": "string",
          "description": "Default model alias."
        }
      }
    },
    "ChatMessage": {
      "type": "object",
      "required": [
        "role",
        "content"
      ],
      "properties": {
        "role": {
          "type": "string",
          "enum": [
            "system",
            "user",
            "assistant",
            "tool"
          ],
          "description": "Message speaker role."
        },
        "content": {
          "type": "string",
          "description": "Message text content."
        }
      }
    },
    "ChatRequest": {
      "type": "object",
      "required": [
        "model",
        "messages"
      ],
      "properties": {
        "model": {
          "type": "string",
          "default": "masidyn-auto",
          "description": "Target MasiDyn Cloud model alias (e.g. masidyn-auto, masidyn-prime, masidyn-flash-low, masidyn-gemini)."
        },
        "messages": {
          "type": "array",
          "description": "Ordered conversation messages.",
          "items": {
            "$ref": "#/definitions/ChatMessage"
          }
        },
        "temperature": {
          "type": "number",
          "description": "Sampling temperature between 0 and 2."
        },
        "max_tokens": {
          "type": "integer",
          "description": "Generation budget including reasoning and visible output (capped at 2048). Exhaustion before answer text or tool calls returns PROVIDER_OUTPUT_LIMIT."
        },
        "stream": {
          "type": "boolean",
          "default": false,
          "description": "Keep false for Copilot Studio JSON connector responses. Raw SSE consumers must inspect the terminal event: HTTP 200 is sent before completion; a later failure emits data: {\"error\": RECEIPT} with ErrorResponse fields and closes without [DONE]."
        }
      }
    },
    "ChatChoice": {
      "type": "object",
      "properties": {
        "index": {
          "type": "integer",
          "description": "Zero-based choice index."
        },
        "message": {
          "$ref": "#/definitions/ChatMessage"
        },
        "finish_reason": {
          "type": "string",
          "description": "Completion stop reason."
        }
      }
    },
    "ChatUsage": {
      "type": "object",
      "properties": {
        "prompt_tokens": {
          "type": "integer",
          "description": "Input prompt token count."
        },
        "completion_tokens": {
          "type": "integer",
          "description": "Generated output token count."
        },
        "total_tokens": {
          "type": "integer",
          "description": "Combined token count."
        }
      }
    },
    "ChatResponse": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "description": "Completion ID."
        },
        "object": {
          "type": "string",
          "description": "Response type (chat.completion)."
        },
        "created": {
          "type": "integer",
          "description": "Unix timestamp."
        },
        "model": {
          "type": "string",
          "description": "Resolved model identifier."
        },
        "choices": {
          "type": "array",
          "description": "Generated assistant choices.",
          "items": {
            "$ref": "#/definitions/ChatChoice"
          }
        },
        "usage": {
          "$ref": "#/definitions/ChatUsage"
        }
      }
    },
    "EmbeddingRequest": {
      "type": "object",
      "required": [
        "model",
        "input"
      ],
      "properties": {
        "model": {
          "type": "string",
          "default": "masidyn-embed",
          "description": "Embedding model identifier."
        },
        "input": {
          "type": "array",
          "description": "Text strings to embed into vectors.",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "EmbeddingItem": {
      "type": "object",
      "properties": {
        "object": {
          "type": "string",
          "description": "Item type (embedding)."
        },
        "index": {
          "type": "integer",
          "description": "Input index."
        },
        "embedding": {
          "type": "array",
          "description": "Dense float embedding vector.",
          "items": {
            "type": "number"
          }
        }
      }
    },
    "EmbeddingResponse": {
      "type": "object",
      "properties": {
        "object": {
          "type": "string",
          "description": "Response type (list)."
        },
        "model": {
          "type": "string",
          "description": "Resolved embedding model."
        },
        "data": {
          "type": "array",
          "description": "Embedding output items.",
          "items": {
            "$ref": "#/definitions/EmbeddingItem"
          }
        }
      }
    },
    "HarvestSearchResponse": {
      "type": "object",
      "properties": {
        "ok": {
          "type": "boolean",
          "description": "Whether location lookup succeeded."
        },
        "resolvedLocation": {
          "type": "string",
          "description": "Geocoded global location string."
        },
        "radiusKm": {
          "type": "number",
          "description": "Search radius in kilometers."
        },
        "nodes": {
          "type": "array",
          "description": "Nearby food banks, soup kitchens, pantries, and 24/7 community fridges.",
          "items": {
            "type": "object"
          }
        }
      }
    },
    "ErrorResponse": {
      "type": "object",
      "required": [
        "error",
        "message",
        "code",
        "request_id",
        "stage",
        "retryable",
        "execution_status",
        "next_action"
      ],
      "properties": {
        "error": {
          "type": "string",
          "description": "Existing error code or message retained for compatibility."
        },
        "message": {
          "type": "string",
          "description": "Safe explanation of the failure."
        },
        "code": {
          "type": "string",
          "description": "Stable machine-readable diagnostic code, such as AUTH_REQUIRED."
        },
        "request_id": {
          "type": "string",
          "format": "uuid",
          "description": "Server-generated identifier matching the X-Request-ID response header."
        },
        "stage": {
          "type": "string",
          "enum": [
            "LOCAL_PREFLIGHT",
            "TRANSPORT",
            "PROVIDER",
            "UNKNOWN"
          ],
          "description": "Where the request stopped; transport failure does not establish provider unavailability."
        },
        "retryable": {
          "type": "boolean",
          "description": "Whether retrying is appropriate after following next_action."
        },
        "execution_status": {
          "type": "string",
          "enum": [
            "not_started",
            "unknown",
            "failed"
          ],
          "description": "Whether execution began and whether its outcome is known."
        },
        "next_action": {
          "type": "string",
          "description": "Next step for the caller or service operator."
        },
        "upstream_status": {
          "type": "integer",
          "minimum": 100,
          "maximum": 599,
          "description": "Upstream HTTP status when a response was received."
        }
      }
    }
  }
}
