# Chat API Chat API เปิดใช้งานการส่งข้อความแบบ asynchronous ระหว่าง human และ LLM agent ต่างจากการแช็ตแบบ synchronous (สไตล์ ChatGPT) ตรงที่ human สามารถฝากข้อความไว้ขณะที่ agent กำลังทำงาน — agent จะ poll ระหว่างการเรียก tool ## วิธีการทำงาน ``` Human (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM) │ └── marks messages as read on poll ``` 1. Human ส่งข้อความผ่าน web UI (ใช้ JWT) 2. ข้อความถูกเก็บไว้ และทำเครื่องหมายว่ายังไม่ได้อ่าน 3. Agent poll `GET /chat/poll` ระหว่างการเรียก tool 4. Poll ส่งกลับข้อความที่ยังไม่ได้อ่านทั้งหมดและทำเครื่องหมายว่าอ่านแล้ว 5. Agent ประมวลผลข้อความ และอาจตอบกลับผ่าน `POST /chat/reply` ## Endpoints ### GET /chat/poll Poll ข้อความใหม่จาก human ส่งกลับข้อความที่ยังไม่ได้อ่านและทำเครื่องหมายว่าอ่านแล้ว ใช้ระหว่างการเรียก tool ```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 ส่งข้อความในฐานะ 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 ส่งข้อความในฐานะ human (ต้องใช้ JWT ไม่ใช่ 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 ดึงประวัติแช็ตล่าสุด (ทั้งสอง role) ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ "https://synapse.schaefer.zone/chat/history?limit=50" ``` ### GET /chat/unread นับข้อความที่ยังไม่ได้อ่าน ```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 ดึงสถานะของ chat session (timestamp ของข้อความล่าสุด, จำนวนที่ยังไม่ได้อ่าน) ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/chat/status ``` ### POST /chat/upload อัปโหลดไฟล์แนบสำหรับข้อความเฉพาะ (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 รายการไฟล์แนบสำหรับข้อความ ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/files/msg_001 ``` ### GET /chat/file/:file_id ดาวน์โหลดไฟล์แนบ ```bash curl -H "Authorization: Bearer YOUR_JWT" \ https://synapse.schaefer.zone/chat/file/file_001 --output download.png ``` ## รูปแบบการ Poll > [!TIP] > Poll ทุก 30-60 วินาทีระหว่างการเรียก tool อย่า poll ถี่กว่านี้ — จะเปลือง API quota โดยไม่ได้ประโยชน์ ```python # Pseudo-code while working: messages = poll_chat() for msg in messages: process_message(msg) reply(f"Acknowledged: {msg.content}") do_one_tool_call() ``` ## ขั้นตอนถัดไป - [Tasks API](/docs/api/tasks) - [Chat Polling Pattern](/docs/llm-cookbook/chat-polling-pattern)