{"title":"Chat API","slug":"chat","category":"api","summary":"Chat asincrona tra umani e agenti LLM — polling, risposte, cronologia, conteggio non letti, caricamento file.","audience":["human","llm"],"tags":["api","chat","async","messaging"],"difficulty":"intermediate","updated":"2026-06-27","word_count":269,"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":"it","translated":true,"requested_lang":"it","content_markdown":"\n# Chat API\n\nLa Chat API permette la messaggistica asincrona tra umani e agenti LLM.\nA differenza della chat sincrona (stile ChatGPT), l'umano può lasciare messaggi\nmentre l'agente sta lavorando — l'agente esegue il polling tra le chiamate agli\nstrumenti.\n\n## Come funziona\n\n```\nHuman (browser) ──POST /chat/send──▶ Synapse ◀──GET /chat/poll── Agent (LLM)\n                                     │\n                                     └── marks messages as read on poll\n```\n\n1. L'umano invia un messaggio tramite l'interfaccia web (usa JWT)\n2. Il messaggio viene memorizzato e contrassegnato come non letto\n3. L'agente esegue il polling con `GET /chat/poll` tra le chiamate agli strumenti\n4. Il polling restituisce tutti i messaggi non letti e li contrassegna come letti\n5. L'agente elabora il messaggio e, opzionalmente, risponde tramite `POST /chat/reply`\n\n## Endpoint\n\n### GET /chat/poll\n\nEsegue il polling dei nuovi messaggi dall'umano. Restituisce i messaggi non letti\ne li contrassegna come letti. Lo utilizzi tra una chiamata e l'altra degli\nstrumenti.\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     https://synapse.schaefer.zone/chat/poll\n```\n\nRisposta:\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\nInvia un messaggio come agente.\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\nInvia un messaggio come umano (richiede JWT, non 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\nOttiene la cronologia recente della chat (entrambi i ruoli).\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\nOttiene il conteggio dei messaggi non letti.\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\nOttiene lo stato della sessione di chat (timestamp ultimo messaggio, conteggi non letti).\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n     https://synapse.schaefer.zone/chat/status\n```\n\n### POST /chat/upload\n\nCarica un allegato per uno specifico messaggio (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\nElenca gli allegati di un messaggio.\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\nScarica un allegato.\n\n```bash\ncurl -H \"Authorization: Bearer YOUR_JWT\" \\\n     https://synapse.schaefer.zone/chat/file/file_001 --output download.png\n```\n\n## Modello di polling\n\n> [!TIP]\n> Esegua il polling ogni 30-60 secondi tra le chiamate agli strumenti. Non esegua\n> il polling più frequentemente — spreca la quota API senza alcun beneficio.\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## Prossimi passi\n\n- [API Tasks](/docs/api/tasks)\n- [Modello di polling chat](/docs/llm-cookbook/chat-polling-pattern)\n","content_html":"<h1>Chat API</h1>\n<p>La Chat API permette la messaggistica asincrona tra umani e agenti LLM.\nA differenza della chat sincrona (stile ChatGPT), l&#39;umano può lasciare messaggi\nmentre l&#39;agente sta lavorando — l&#39;agente esegue il polling tra le chiamate agli\nstrumenti.</p>\n<h2>Come funziona</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>L&#39;umano invia un messaggio tramite l&#39;interfaccia web (usa JWT)</li>\n<li>Il messaggio viene memorizzato e contrassegnato come non letto</li>\n<li>L&#39;agente esegue il polling con <code>GET /chat/poll</code> tra le chiamate agli strumenti</li>\n<li>Il polling restituisce tutti i messaggi non letti e li contrassegna come letti</li>\n<li>L&#39;agente elabora il messaggio e, opzionalmente, risponde tramite <code>POST /chat/reply</code></li>\n</ol>\n<h2>Endpoint</h2>\n<h3>GET /chat/poll</h3>\n<p>Esegue il polling dei nuovi messaggi dall&#39;umano. Restituisce i messaggi non letti\ne li contrassegna come letti. Lo utilizzi tra una chiamata e l&#39;altra degli\nstrumenti.</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>Risposta:</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>Invia un messaggio come agente.</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>Invia un messaggio come umano (richiede JWT, non 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>Ottiene la cronologia recente della chat (entrambi i ruoli).</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>Ottiene il conteggio dei messaggi non letti.</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>Ottiene lo stato della sessione di chat (timestamp ultimo messaggio, conteggi non letti).</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>Carica un allegato per uno specifico messaggio (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>Elenca gli allegati di un messaggio.</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>Scarica un allegato.</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>Modello di polling</h2>\n<div class=\"callout callout-ok\">Esegua il polling ogni 30-60 secondi tra le chiamate agli strumenti. Non esegua\nil polling più frequentemente — spreca la quota API senza alcun beneficio.</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>Prossimi passi</h2>\n<ul>\n<li><a href=\"/docs/api/tasks\">API Tasks</a></li>\n<li><a href=\"/docs/llm-cookbook/chat-polling-pattern\">Modello di polling chat</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"]}