Skip to content

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.

Send a message and receive a streaming AI response via Server-Sent Events (SSE).

Auth required: Yes

Request body:

FieldTypeRequiredDescription
messagestringYesThe user’s message
chat_idstringNoChat session ULID to continue an existing conversation
previous_response_idstringNoResponse ID from a previous turn for conversation continuity
Terminal window
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.

Omit chat_id to start a new conversation. The server will create a chat session and return its ID in a chat_session event.

Pass a chat_id and optionally previous_response_id to continue a conversation with full history context.

Terminal window
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"
}'

The response is a stream of Server-Sent Events. Each event has a data field containing a JSON object with a type discriminator.

A chunk of the assistant’s text response. Concatenate these to build the full message.

{"type": "text_delta", "content": "I'll create that thogit"}

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"}

Sent when a new chat session is created (when chat_id was not provided).

{"type": "chat_session", "chat_id": "01HQ9C3D4EFGHIJKL5MNOPQ6R"}

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"}

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.


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;
}
}
}
}