{"title":"Chat API","slug":"chat","category":"api","summary":"การแช็ตแบบ asynchronous ระหว่าง human และ LLM agent — poll, reply, history, นับข้อความที่ยังไม่ได้อ่าน, อัปโหลดไฟล์","audience":["human","llm"],"tags":["api","chat","async","messaging"],"difficulty":"intermediate","updated":"2026-06-27","word_count":149,"read_minutes":1,"llm_context":"Auth: Mind Key (agent side, ?role=agent) or JWT (human side, ?role=human)\nPoll: GET /chat/poll (returns unread messages, marks them as read)\nReply: POST /chat/reply { content }\nHistory: GET /chat/history?limit=50\nUnread count: GET /chat/unread?role=agent (or ?role=human)\nUpload: POST /chat/upload (multipart, file attachment)\nPattern: poll between tool calls, reply when human asks questions\n","lang":"th","translated":true,"requested_lang":"th","content_markdown":"\n# Chat API\n\nChat API เปิดใช้งานการส่งข้อความแบบ asynchronous ระหว่าง human และ LLM agent ต่างจากการแช็ตแบบ synchronous (สไตล์ ChatGPT) ตรงที่ human สามารถฝากข้อความไว้ขณะที่ agent กำลังทำงาน — agent จะ poll ระหว่างการเรียก tool\n\n## วิธีการทำงาน\n\n```\nHuman (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM)\n                                     │\n                                     └── marks messages as read on poll\n```\n\n1. Human ส่งข้อความผ่าน web UI (ใช้ JWT)\n2. ข้อความถูกเก็บไว้ และทำเครื่องหมายว่ายังไม่ได้อ่าน\n3. Agent poll `GET /chat/poll` ระหว่างการเรียก tool\n4. Poll ส่งกลับข้อความที่ยังไม่ได้อ่านทั้งหมดและทำเครื่องหมายว่าอ่านแล้ว\n5. Agent ประมวลผลข้อความ และอาจตอบกลับผ่าน `POST /chat/reply`\n\n## Endpoints\n\n### GET /chat/poll\n\nPoll ข้อความใหม่จาก human ส่งกลับข้อความที่ยังไม่ได้อ่านและทำเครื่องหมายว่าอ่านแล้ว ใช้ระหว่างการเรียก tool\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     https://synapse.schaefer.zone/chat/poll\n```\n\nResponse:\n\n```json\n{\n  \"messages\": [\n    {\n      \"id\": \"msg_001\",\n      \"role\": \"human\",\n      \"content\": \"How's the deployment going?\",\n      \"created_at\": \"2026-06-27T...\"\n    }\n  ]\n}\n```\n\n### POST /chat/reply\n\nส่งข้อความในฐานะ agent\n\n```bash\ncurl -X POST https://synapse.schaefer.zone/chat/reply \\\n  -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"content\": \"Deployment is 80% done. CI is green, just waiting for Docker push.\"}'\n```\n\n### POST /chat/send\n\nส่งข้อความในฐานะ human (ต้องใช้ JWT ไม่ใช่ Mind Key)\n\n```bash\ncurl -X POST https://synapse.schaefer.zone/chat/send \\\n  -H \"Authorization: Bearer YOUR_JWT\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"content\": \"Can you check the logs?\"}'\n```\n\n### GET /chat/history\n\nดึงประวัติแช็ตล่าสุด (ทั้งสอง role)\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     \"https://synapse.schaefer.zone/chat/history?limit=50\"\n```\n\n### GET /chat/unread\n\nนับข้อความที่ยังไม่ได้อ่าน\n\n```bash\n# Count unread messages from human (for agent)\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     \"https://synapse.schaefer.zone/chat/unread?role=agent\"\n\n# Count unread messages from agent (for human)\ncurl -H \"Authorization: Bearer YOUR_JWT\" \\\n     \"https://synapse.schaefer.zone/chat/unread?role=human\"\n```\n\n### GET /chat/status\n\nดึงสถานะของ chat session (timestamp ของข้อความล่าสุด, จำนวนที่ยังไม่ได้อ่าน)\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     https://synapse.schaefer.zone/chat/status\n```\n\n### POST /chat/upload\n\nอัปโหลดไฟล์แนบสำหรับข้อความเฉพาะ (multipart form)\n\n```bash\ncurl -X POST https://synapse.schaefer.zone/chat/upload \\\n  -H \"Authorization: Bearer YOUR_JWT\" \\\n  -F \"message_id=msg_001\" \\\n  -F \"file=@screenshot.png\"\n```\n\n### GET /chat/files/:message_id\n\nรายการไฟล์แนบสำหรับข้อความ\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_JWT\" \\\n     https://synapse.schaefer.zone/chat/files/msg_001\n```\n\n### GET /chat/file/:file_id\n\nดาวน์โหลดไฟล์แนบ\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_JWT\" \\\n     https://synapse.schaefer.zone/chat/file/file_001 --output download.png\n```\n\n## รูปแบบการ Poll\n\n> [!TIP]\n> Poll ทุก 30-60 วินาทีระหว่างการเรียก tool อย่า poll ถี่กว่านี้ — จะเปลือง API quota โดยไม่ได้ประโยชน์\n\n```python\n# Pseudo-code\nwhile working:\n    messages = poll_chat()\n    for msg in messages:\n        process_message(msg)\n        reply(f\"Acknowledged: {msg.content}\")\n    do_one_tool_call()\n```\n\n## ขั้นตอนถัดไป\n\n- [Tasks API](/docs/api/tasks)\n- [Chat Polling Pattern](/docs/llm-cookbook/chat-polling-pattern)\n","content_html":"<h1>Chat API</h1>\n<p>Chat API เปิดใช้งานการส่งข้อความแบบ asynchronous ระหว่าง human และ LLM agent ต่างจากการแช็ตแบบ synchronous (สไตล์ ChatGPT) ตรงที่ human สามารถฝากข้อความไว้ขณะที่ agent กำลังทำงาน — agent จะ poll ระหว่างการเรียก tool</p>\n<h2>วิธีการทำงาน</h2>\n<pre><code class=\"hljs language-plaintext\">Human (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM)\n                                     │\n                                     └── marks messages as read on poll</code></pre><ol>\n<li>Human ส่งข้อความผ่าน web UI (ใช้ JWT)</li>\n<li>ข้อความถูกเก็บไว้ และทำเครื่องหมายว่ายังไม่ได้อ่าน</li>\n<li>Agent poll <code>GET /chat/poll</code> ระหว่างการเรียก tool</li>\n<li>Poll ส่งกลับข้อความที่ยังไม่ได้อ่านทั้งหมดและทำเครื่องหมายว่าอ่านแล้ว</li>\n<li>Agent ประมวลผลข้อความ และอาจตอบกลับผ่าน <code>POST /chat/reply</code></li>\n</ol>\n<h2>Endpoints</h2>\n<h3>GET /chat/poll</h3>\n<p>Poll ข้อความใหม่จาก human ส่งกลับข้อความที่ยังไม่ได้อ่านและทำเครื่องหมายว่าอ่านแล้ว ใช้ระหว่างการเรียก tool</p>\n<pre><code class=\"hljs language-bash\">curl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n     https://synapse.schaefer.zone/chat/poll</code></pre><p>Response:</p>\n<pre><code class=\"hljs language-json\"><span class=\"hljs-punctuation\">{</span>\n  <span class=\"hljs-attr\">&quot;messages&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-punctuation\">[</span>\n    <span class=\"hljs-punctuation\">{</span>\n      <span class=\"hljs-attr\">&quot;id&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;msg_001&quot;</span><span class=\"hljs-punctuation\">,</span>\n      <span class=\"hljs-attr\">&quot;role&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;human&quot;</span><span class=\"hljs-punctuation\">,</span>\n      <span class=\"hljs-attr\">&quot;content&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;How&#x27;s the deployment going?&quot;</span><span class=\"hljs-punctuation\">,</span>\n      <span class=\"hljs-attr\">&quot;created_at&quot;</span><span class=\"hljs-punctuation\">:</span> <span class=\"hljs-string\">&quot;2026-06-27T...&quot;</span>\n    <span class=\"hljs-punctuation\">}</span>\n  <span class=\"hljs-punctuation\">]</span>\n<span class=\"hljs-punctuation\">}</span></code></pre><h3>POST /chat/reply</h3>\n<p>ส่งข้อความในฐานะ agent</p>\n<pre><code class=\"hljs language-bash\">curl -X POST https://synapse.schaefer.zone/chat/reply \\\n  -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n  -H <span class=\"hljs-string\">&quot;Content-Type: application/json&quot;</span> \\\n  -d <span class=\"hljs-string\">&#x27;{&quot;content&quot;: &quot;Deployment is 80% done. CI is green, just waiting for Docker push.&quot;}&#x27;</span></code></pre><h3>POST /chat/send</h3>\n<p>ส่งข้อความในฐานะ human (ต้องใช้ JWT ไม่ใช่ Mind Key)</p>\n<pre><code class=\"hljs language-bash\">curl -X POST https://synapse.schaefer.zone/chat/send \\\n  -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_JWT&quot;</span> \\\n  -H <span class=\"hljs-string\">&quot;Content-Type: application/json&quot;</span> \\\n  -d <span class=\"hljs-string\">&#x27;{&quot;content&quot;: &quot;Can you check the logs?&quot;}&#x27;</span></code></pre><h3>GET /chat/history</h3>\n<p>ดึงประวัติแช็ตล่าสุด (ทั้งสอง role)</p>\n<pre><code class=\"hljs language-bash\">curl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n     <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/chat/history?limit=50&quot;</span></code></pre><h3>GET /chat/unread</h3>\n<p>นับข้อความที่ยังไม่ได้อ่าน</p>\n<pre><code class=\"hljs language-bash\"><span class=\"hljs-comment\"># Count unread messages from human (for agent)</span>\ncurl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n     <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/chat/unread?role=agent&quot;</span>\n\n<span class=\"hljs-comment\"># Count unread messages from agent (for human)</span>\ncurl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_JWT&quot;</span> \\\n     <span class=\"hljs-string\">&quot;https://synapse.schaefer.zone/chat/unread?role=human&quot;</span></code></pre><h3>GET /chat/status</h3>\n<p>ดึงสถานะของ chat session (timestamp ของข้อความล่าสุด, จำนวนที่ยังไม่ได้อ่าน)</p>\n<pre><code class=\"hljs language-bash\">curl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n     https://synapse.schaefer.zone/chat/status</code></pre><h3>POST /chat/upload</h3>\n<p>อัปโหลดไฟล์แนบสำหรับข้อความเฉพาะ (multipart form)</p>\n<pre><code class=\"hljs language-bash\">curl -X POST https://synapse.schaefer.zone/chat/upload \\\n  -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_JWT&quot;</span> \\\n  -F <span class=\"hljs-string\">&quot;message_id=msg_001&quot;</span> \\\n  -F <span class=\"hljs-string\">&quot;file=@screenshot.png&quot;</span></code></pre><h3>GET /chat/files/:message_id</h3>\n<p>รายการไฟล์แนบสำหรับข้อความ</p>\n<pre><code class=\"hljs language-bash\">curl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_JWT&quot;</span> \\\n     https://synapse.schaefer.zone/chat/files/msg_001</code></pre><h3>GET /chat/file/:file_id</h3>\n<p>ดาวน์โหลดไฟล์แนบ</p>\n<pre><code class=\"hljs language-bash\">curl -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_JWT&quot;</span> \\\n     https://synapse.schaefer.zone/chat/file/file_001 --output download.png</code></pre><h2>รูปแบบการ Poll</h2>\n<div class=\"callout callout-ok\">Poll ทุก 30-60 วินาทีระหว่างการเรียก tool อย่า poll ถี่กว่านี้ — จะเปลือง API quota โดยไม่ได้ประโยชน์</div><pre><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Pseudo-code</span>\n<span class=\"hljs-keyword\">while</span> working:\n    messages = poll_chat()\n    <span class=\"hljs-keyword\">for</span> msg <span class=\"hljs-keyword\">in</span> messages:\n        process_message(msg)\n        reply(<span class=\"hljs-string\">f&quot;Acknowledged: <span class=\"hljs-subst\">{msg.content}</span>&quot;</span>)\n    do_one_tool_call()</code></pre><h2>ขั้นตอนถัดไป</h2>\n<ul>\n<li><a href=\"/docs/api/tasks\">Tasks API</a></li>\n<li><a href=\"/docs/llm-cookbook/chat-polling-pattern\">Chat Polling Pattern</a></li>\n</ul>\n","urls":{"html":"/docs/api/chat","text":"/docs/api/chat?format=text","json":"/docs/api/chat?format=json","llm":"/docs/api/chat?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}