{
  "openapi": "3.1.0",
  "info": {
    "title": "Camile AI — Developer Platform API",
    "version": "1.1.0",
    "description": "API v1 para conectar sistemas externos às entidades da Camile AI. Três fluxos: (1) sua entidade via API key; (2) entidades criadas para os usuários do seu sistema; (3) conexão OAuth da entidade do próprio usuário (\"vincular minha entidade\").",
    "contact": {
      "url": "https://camileai.com/developers"
    },
    "license": {
      "name": "Proprietary"
    }
  },
  "servers": [
    {
      "url": "https://camileai.com",
      "description": "Produção"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key (cam_sk_live_…) ou access token OAuth (cam_at_…)."
      },
      "oauth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://camileai.com/pt-BR/oauth/authorize",
            "tokenUrl": "https://camileai.com/api/oauth/token",
            "refreshUrl": "https://camileai.com/api/oauth/token",
            "scopes": {
              "entity:chat": "Conversar com a entidade autorizada",
              "entity:read": "Ler o histórico de conversas da entidade autorizada",
              "entity:write": "Gerenciar a própria conta e criar entidades",
              "device:read": "Ler dispositivos e sessões de controle",
              "device:control": "Autorizar comandos de controle"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": false
          },
          "error": {
            "type": "string"
          },
          "code": {
            "type": "string",
            "enum": [
              "unauthorized",
              "insufficient_scope",
              "invalid_request",
              "entity_not_found",
              "entity_unavailable",
              "rate_limited",
              "internal_error",
              "forbidden",
              "invalid_email",
              "invalid_role",
              "user_not_found",
              "already_member",
              "member_limit",
              "membership_not_found",
              "last_owner",
              "self_role_change",
              "entity_archived"
            ]
          }
        },
        "required": [
          "ok",
          "error"
        ]
      },
      "Entity": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "slug": {
            "type": [
              "string",
              "null"
            ]
          },
          "kind": {
            "type": "string",
            "enum": [
              "reference",
              "standard",
              "canary",
              "admin"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "BOOTING",
              "ACTIVE",
              "PAUSED",
              "STOPPED",
              "ARCHIVED"
            ]
          },
          "locale": {
            "type": "string"
          },
          "purpose": {
            "type": [
              "string",
              "null"
            ]
          },
          "created_at": {
            "type": "integer",
            "description": "epoch ms"
          }
        },
        "required": [
          "id",
          "name",
          "status"
        ]
      },
      "Interaction": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "response": {
            "type": [
              "string",
              "null"
            ]
          },
          "preempted": {
            "type": "boolean"
          },
          "trajectoryId": {
            "type": [
              "string",
              "null"
            ]
          },
          "resumed": {
            "type": "boolean"
          },
          "ts": {
            "type": "integer",
            "description": "epoch ms"
          },
          "correlationId": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "message",
          "ts"
        ]
      },
      "Member": {
        "type": "object",
        "properties": {
          "userId": {
            "type": "string"
          },
          "displayName": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "role": {
            "type": "string",
            "enum": [
              "owner",
              "collaborator",
              "viewer"
            ]
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "isOwner": {
            "type": "boolean"
          }
        },
        "required": [
          "userId",
          "displayName",
          "email",
          "role",
          "createdAt",
          "isOwner"
        ]
      },
      "MembersResponse": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "entityId": {
            "type": "string"
          },
          "role": {
            "type": "string",
            "enum": [
              "owner",
              "collaborator",
              "viewer",
              "admin"
            ]
          },
          "canManage": {
            "type": "boolean"
          },
          "members": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Member"
            }
          },
          "total": {
            "type": "integer"
          }
        },
        "required": [
          "ok",
          "entityId",
          "role",
          "canManage",
          "members",
          "total"
        ]
      },
      "AddMemberRequest": {
        "type": "object",
        "properties": {
          "email": {
            "type": "string",
            "format": "email"
          },
          "role": {
            "type": "string",
            "enum": [
              "collaborator",
              "viewer"
            ]
          }
        },
        "required": [
          "email",
          "role"
        ]
      },
      "UpdateMemberRoleRequest": {
        "type": "object",
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "collaborator",
              "viewer"
            ]
          }
        },
        "required": [
          "role"
        ]
      }
    }
  },
  "paths": {
    "/api/v1/me": {
      "get": {
        "operationId": "getMe",
        "summary": "Identidade e limites da credencial",
        "responses": {
          "200": {
            "description": "Credencial válida",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "subject": {
                      "type": "object",
                      "properties": {
                        "type": {
                          "type": "string",
                          "enum": [
                            "api_key",
                            "oauth"
                          ]
                        },
                        "id": {
                          "type": "string"
                        },
                        "name": {
                          "type": "string"
                        },
                        "entity_id": {
                          "type": "string"
                        },
                        "client_id": {
                          "type": "string"
                        }
                      }
                    },
                    "tenant": {
                      "type": "object"
                    },
                    "scopes": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "limits": {
                      "type": "object",
                      "properties": {
                        "requests_per_minute": {
                          "type": "integer",
                          "const": 120
                        }
                      }
                    },
                    "api_version": {
                      "type": "string",
                      "const": "v1"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Credencial inválida",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/entities": {
      "get": {
        "operationId": "listEntities",
        "summary": "Lista entidades do tenant",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 50,
              "maximum": 200
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Lista paginada",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "entities": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Entity"
                      }
                    },
                    "total": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Não autorizado"
          },
          "403": {
            "description": "Escopo insuficiente (entities:read)"
          }
        }
      },
      "post": {
        "operationId": "createEntity",
        "summary": "Cria uma entidade (Protocolo de Gênese)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 80
                  },
                  "gender": {
                    "type": "string",
                    "enum": [
                      "feminino",
                      "masculino",
                      "neutro"
                    ]
                  },
                  "purpose": {
                    "type": "string",
                    "maxLength": 2000
                  },
                  "locale": {
                    "type": "string"
                  },
                  "external_user_id": {
                    "type": "string",
                    "description": "Identificador do seu usuário final (quando a entidade é criada para ele)"
                  }
                },
                "required": [
                  "name"
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Entidade criada",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "entity": {
                      "$ref": "#/components/schemas/Entity"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Requisição inválida"
          },
          "403": {
            "description": "Escopo insuficiente (entities:write)"
          }
        }
      }
    },
    "/api/v1/entities/{id}": {
      "get": {
        "operationId": "getEntity",
        "summary": "Detalhe de uma entidade",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Entidade",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "entity": {
                      "$ref": "#/components/schemas/Entity"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Não encontrada"
          }
        }
      }
    },
    "/api/v1/entities/{id}/messages": {
      "post": {
        "operationId": "sendMessage",
        "summary": "Envia mensagem à entidade e aguarda resposta (até 30s)",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "message": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 4000
                  },
                  "end_user_id": {
                    "type": "string",
                    "description": "Identificador do seu usuário final — a entidade desenvolve um modelo psicológico por pessoa"
                  },
                  "end_user_name": {
                    "type": "string"
                  },
                  "conversation_mode": {
                    "type": "string",
                    "enum": [
                      "social",
                      "professional"
                    ],
                    "default": "social"
                  }
                },
                "required": [
                  "message"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Resposta da entidade",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "interaction": {
                      "$ref": "#/components/schemas/Interaction"
                    }
                  }
                }
              }
            }
          },
          "202": {
            "description": "Enfileirada; resposta ainda não pronta (consulte o GET)"
          },
          "409": {
            "description": "Entidade indisponível"
          }
        }
      },
      "get": {
        "operationId": "listMessages",
        "summary": "Histórico recente de conversas",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 30,
              "maximum": 100
            }
          },
          {
            "name": "end_user_id",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Mensagens",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "messages": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Interaction"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/oauth/token": {
      "post": {
        "operationId": "oauthToken",
        "summary": "Troca code por tokens / refresh (RFC 6749)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "properties": {
                  "grant_type": {
                    "type": "string",
                    "enum": [
                      "authorization_code",
                      "refresh_token"
                    ]
                  },
                  "code": {
                    "type": "string"
                  },
                  "redirect_uri": {
                    "type": "string"
                  },
                  "refresh_token": {
                    "type": "string"
                  },
                  "client_id": {
                    "type": "string"
                  },
                  "client_secret": {
                    "type": "string"
                  }
                },
                "required": [
                  "grant_type",
                  "client_id",
                  "client_secret"
                ]
              }
            },
            "application/json": {
              "schema": {
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tokens emitidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "access_token": {
                      "type": "string"
                    },
                    "token_type": {
                      "type": "string",
                      "const": "Bearer"
                    },
                    "expires_in": {
                      "type": "integer"
                    },
                    "refresh_token": {
                      "type": "string"
                    },
                    "scope": {
                      "type": "string"
                    },
                    "entity_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Erro OAuth (invalid_grant, unsupported_grant_type…)"
          },
          "401": {
            "description": "invalid_client"
          }
        }
      }
    },
    "/api/oauth/revoke": {
      "post": {
        "operationId": "oauthRevoke",
        "summary": "Revoga um grant (RFC 7009)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "properties": {
                  "token": {
                    "type": "string"
                  },
                  "client_id": {
                    "type": "string"
                  },
                  "client_secret": {
                    "type": "string"
                  }
                },
                "required": [
                  "token",
                  "client_id",
                  "client_secret"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Revogado (ou token desconhecido — sempre 200 por RFC)"
          }
        }
      }
    },
    "/api/v1/entities/{id}/brain-status": {
      "get": {
        "operationId": "getBrainStatus",
        "summary": "Último evento cognitivo real do turno",
        "description": "Lê o evento mais recente publicado pelo cérebro para a conversa (start, thinking, tool, done, error). TTL de 5 minutos; use polling curto durante um turno.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "conversation_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Evento mais recente (ou null)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "evento": {
                      "type": [
                        "object",
                        "null"
                      ],
                      "properties": {
                        "kind": {
                          "type": "string"
                        },
                        "label": {
                          "type": "string"
                        },
                        "atividade": {
                          "type": "string"
                        },
                        "tool": {
                          "type": "string"
                        },
                        "ts": {
                          "type": "integer"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Entidade não encontrada"
          }
        }
      }
    },
    "/api/v1/entities/{id}/dashboard": {
      "get": {
        "operationId": "getEntityDashboard",
        "summary": "Leitura agregada para as superfícies do desktop/mobile",
        "description": "Pulsos (total, 24h e série horária), atividade (auditoria + interações), memória real com contagem de reativações e uso/custo do ciclo. Somente leitura.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 50,
              "minimum": 5,
              "maximum": 100
            }
          },
          {
            "name": "days",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 30,
              "minimum": 1,
              "maximum": 90
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Dashboard da entidade",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "dashboard": {
                      "type": "object",
                      "properties": {
                        "entity_id": {
                          "type": "string"
                        },
                        "pulses": {
                          "type": "object"
                        },
                        "activity": {
                          "type": "object"
                        },
                        "memory": {
                          "type": "object"
                        },
                        "usage": {
                          "type": "object"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Entidade não encontrada"
          }
        }
      }
    },
    "/api/v1/entities/{id}/members": {
      "get": {
        "operationId": "listEntityMembers",
        "summary": "Lista os membros da entidade",
        "description": "Owner, collaborator e admin veem a lista. Viewer recebe 403 para proteger e-mails.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Membros",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MembersResponse"
                }
              }
            }
          },
          "401": {
            "description": "Credencial ausente ou inválida"
          },
          "403": {
            "description": "Papel sem permissão para ver a lista"
          },
          "404": {
            "description": "Entidade não encontrada"
          }
        }
      },
      "post": {
        "operationId": "addEntityMember",
        "summary": "Vincula um usuário já cadastrado",
        "description": "Somente owner/admin. O e-mail precisa existir no mesmo tenant; owner não é criado por esta rota. Grava audit_events.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AddMemberRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Membro criado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "member": {
                      "$ref": "#/components/schemas/Member"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "E-mail ou papel inválido"
          },
          "401": {
            "description": "Credencial ausente ou inválida"
          },
          "403": {
            "description": "Papel sem permissão para gerenciar"
          },
          "404": {
            "description": "Entidade ou usuário não encontrado"
          },
          "409": {
            "description": "Já é membro, limite atingido ou entidade arquivada"
          }
        }
      }
    },
    "/api/v1/entities/{id}/members/{userId}": {
      "patch": {
        "operationId": "updateEntityMemberRole",
        "summary": "Troca o papel de um membro",
        "description": "Somente owner/admin. Papel aceito: collaborator ou viewer. Ninguém troca o próprio papel; o último owner é protegido.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "userId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateMemberRoleRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Papel atualizado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "member": {
                      "$ref": "#/components/schemas/Member"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Papel inválido"
          },
          "401": {
            "description": "Credencial ausente ou inválida"
          },
          "403": {
            "description": "Sem permissão ou tentativa de alterar o próprio papel"
          },
          "404": {
            "description": "Entidade ou membro não encontrado"
          },
          "409": {
            "description": "Último owner ou entidade arquivada"
          }
        }
      },
      "delete": {
        "operationId": "removeEntityMember",
        "summary": "Remove um membro da entidade",
        "description": "Somente owner/admin. O último owner não pode ser removido.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "userId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Removido"
          },
          "401": {
            "description": "Credencial ausente ou inválida"
          },
          "403": {
            "description": "Sem permissão"
          },
          "404": {
            "description": "Entidade ou membro não encontrado"
          },
          "409": {
            "description": "Último owner ou entidade arquivada"
          }
        }
      }
    }
  }
}