# Chat API The Chat API enables asynchronous messaging between humans and LLM agents. Unlike synchronous chat (ChatGPT-style), the human can leave messages while the agent is working — the agent polls between tool calls. ## How It Works ``` Human (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM) │ └── marks messages as read on poll ``` 1. Human sends a message via the web UI (uses JWT) 2. Message is stored, marked as unread 3. Agent polls `GET /chat/poll` between tool calls 4. Poll returns all unread messages and marks them as read 5. Agent processes the message, optionally replies via `POST /chat/reply` ## Endpoints ### GET /chat/poll Poll for new messages from the human. Returns unread messages and marks them as read. Use this between tool calls. ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/poll ``` Response: ```json { "messages": [ { "id": "msg_001", "role": "human", "content": "How's the deployment going?", "created_at": "2026-06-27T..." } ] } ``` ### POST /chat/reply Send a message as the agent. ```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 Send a message as the human (requires JWT, not 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 Get recent chat history (both roles). ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/chat/history?limit=50" ``` ### GET /chat/unread Get unread message count. ```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 Get chat session status (last message timestamps, unread counts). ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/status ``` ### POST /chat/upload Upload a file attachment for a specific message (multipart form). ```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 List file attachments for a message. ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/files/msg_001 ``` ### GET /chat/file/:file_id Download a file attachment. ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/file/file_001 --output download.png ``` ## Polling Pattern > [!TIP] > Poll every 30-60 seconds between tool calls. Don't poll more frequently — > it wastes API quota and provides no benefit. ```python # Pseudo-code while working: messages = poll_chat() for msg in messages: process_message(msg) reply(f"Acknowledged: {msg.content}") do_one_tool_call() ``` ## Next Steps - [Tasks API](/docs/api/tasks) - [Chat Polling Pattern](/docs/llm-cookbook/chat-polling-pattern)