# API de Chat A API de Chat permite mensagens assíncronas entre humanos e agentes LLM. Diferente do chat síncrono (estilo ChatGPT), o humano pode deixar mensagens enquanto o agente está trabalhando — o agente faz poll entre chamadas de ferramenta. ## Como funciona ``` Human (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM) │ └── marks messages as read on poll ``` 1. O humano envia uma mensagem pela interface web (usa JWT) 2. A mensagem é armazenada, marcada como não lida 3. O agente faz poll em `GET /chat/poll` entre chamadas de ferramenta 4. O poll retorna todas as mensagens não lidas e as marca como lidas 5. O agente processa a mensagem e, opcionalmente, responde via `POST /chat/reply` ## Endpoints ### GET /chat/poll Faz poll por novas mensagens do humano. Retorna mensagens não lidas e as marca como lidas. Use isto entre chamadas de ferramenta. ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/poll ``` Resposta: ```json { "messages": [ { "id": "msg_001", "role": "human", "content": "How's the deployment going?", "created_at": "2026-06-27T..." } ] } ``` ### POST /chat/reply Envia uma mensagem como agente. ```bash curl -X POST https://synapse.schaefer.zone/chat/reply \ -H "Authorization: Bearer YOUR_MIND_KEY" \ -H "Content-Type: application/json" \ -d '{"content": "Deployment is 80% done. CI is green, just waiting for Docker push."}' ``` ### POST /chat/send Envia uma mensagem como humano (requer JWT, não Mind Key). ```bash curl -X POST https://synapse.schaefer.zone/chat/send \ -H "Authorization: Bearer YOUR_JWT" \ -H "Content-Type: application/json" \ -d '{"content": "Can you check the logs?"}' ``` ### GET /chat/history Obtém o histórico recente de chat (ambos os papéis). ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/chat/history?limit=50" ``` ### GET /chat/unread Obtém a contagem de mensagens não lidas. ```bash # Count unread messages from human (for agent) curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/chat/unread?role=agent" # Count unread messages from agent (for human) curl -H "Authorization: Bearer YOUR_JWT" \ "https://synapse.schaefer.zone/chat/unread?role=human" ``` ### GET /chat/status Obtém o status da sessão de chat (timestamps da última mensagem, contagens de não lidas). ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/status ``` ### POST /chat/upload Faz upload de um anexo de arquivo para uma mensagem específica (form multipart). ```bash curl -X POST https://synapse.schaefer.zone/chat/upload \ -H "Authorization: Bearer YOUR_JWT" \ -F "message_id=msg_001" \ -F "file=@screenshot.png" ``` ### GET /chat/files/:message_id Lista os anexos de uma mensagem. ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/files/msg_001 ``` ### GET /chat/file/:file_id Baixa um anexo de arquivo. ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/file/file_001 --output download.png ``` ## Padrão de poll > [!TIP] > Faça poll a cada 30-60 segundos entre chamadas de ferramenta. Não faça poll > com mais frequência — isso desperdiça cota da API e não traz benefício. ```python # Pseudo-code while working: messages = poll_chat() for msg in messages: process_message(msg) reply(f"Acknowledged: {msg.content}") do_one_tool_call() ``` ## Próximos passos - [API de Tarefas](/docs/api/tasks) - [Padrão de poll de chat](/docs/llm-cookbook/chat-polling-pattern)