AI Chat
The AI chat endpoint provides a streaming conversation interface powered by X.AI (Grok). The AI assistant can execute thogit and tag operations as tools during the conversation, allowing natural-language interactions with your knowledge base.
POST /chat
Section titled “POST /chat”Send a message and receive a streaming AI response via Server-Sent Events (SSE).
Auth required: Yes
Request body:
| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | The user’s message |
chat_id | string | No | Chat session ULID to continue an existing conversation |
previous_response_id | string | No | Response ID from a previous turn for conversation continuity |
curl -X POST https://app.thogits.com/api/chat \ -H "Content-Type: application/json" \ -b cookies.txt -c cookies.txt \ -N \ -d '{"message": "Create a thogit called Meeting Notes and tag it with Project"}'The -N flag disables output buffering, which is important for streaming responses.
Starting a New Chat
Section titled “Starting a New Chat”Omit chat_id to start a new conversation. The server will create a chat session and return its ID in a chat_session event.
Continuing an Existing Chat
Section titled “Continuing an Existing Chat”Pass a chat_id and optionally previous_response_id to continue a conversation with full history context.
curl -X POST https://app.thogits.com/api/chat \ -H "Content-Type: application/json" \ -b cookies.txt -c cookies.txt \ -N \ -d '{ "message": "Now add a due_date field set to next Friday", "chat_id": "01HQ9C3D4EFGHIJKL5MNOPQ6R", "previous_response_id": "resp_abc123" }'SSE Event Types
Section titled “SSE Event Types”The response is a stream of Server-Sent Events. Each event has a data field containing a JSON object with a type discriminator.
text_delta
Section titled “text_delta”A chunk of the assistant’s text response. Concatenate these to build the full message.
{"type": "text_delta", "content": "I'll create that thogit"}tool_call
Section titled “tool_call”The assistant is executing a tool (e.g., creating a thogit or tag). Sent at both the start and completion of tool execution.
{"type": "tool_call", "name": "create_thogit", "status": "running", "result": null}{"type": "tool_call", "name": "create_thogit", "status": "completed", "result": {"thogit_id": "01HQ4M8R3ZBNWY6K9J2X5V7Q0A"}}The response is complete. Includes a response_id to use as previous_response_id in the next turn.
{"type": "done", "response_id": "resp_abc123"}chat_session
Section titled “chat_session”Sent when a new chat session is created (when chat_id was not provided).
{"type": "chat_session", "chat_id": "01HQ9C3D4EFGHIJKL5MNOPQ6R"}react_render
Section titled “react_render”A dynamically rendered UI component from the assistant.
{"type": "react_render", "id": "component_1", "js": "...", "css": "..."}An error occurred during processing.
{"type": "error", "message": "XAI_API_KEY not configured"}Tool Execution
Section titled “Tool Execution”The AI assistant has access to tools that mirror the Thogits API:
- Creating, updating, and deleting thogits
- Creating and managing tags
- Applying tags to thogits and setting field values
- Searching thogits and tags
- Querying the audit log
The assistant can chain multiple tool calls in a single response. The server enforces a maximum of 200 tool loops per request to prevent runaway execution.
Client Example
Section titled “Client Example”const response = await fetch('/api/chat', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'List all my thogits tagged with Project', chat_id: existingChatId, }),});
const reader = response.body.getReader();const decoder = new TextDecoder();let buffer = '';
while (true) { const { done, value } = await reader.read(); if (done) break;
buffer += decoder.decode(value, { stream: true }); const lines = buffer.split('\n'); buffer = lines.pop(); // keep incomplete line in buffer
for (const line of lines) { if (line.startsWith('data: ')) { const event = JSON.parse(line.slice(6)); switch (event.type) { case 'text_delta': process.stdout.write(event.content); break; case 'tool_call': console.log(`Tool: ${event.name} [${event.status}]`); break; case 'done': console.log(`\nDone. Response ID: ${event.response_id}`); break; case 'error': console.error(`Error: ${event.message}`); break; } } }}