Skip to content

Chat & Messaging

This guide covers how messages flow through MateClaw, from user input to agent response, including SSE streaming and conversation history.

Message Flow

User Input (Web / IM Channel)


┌───────────────┐
│  REST API      │  POST /api/v1/chat/{agentId}/message
└───────┬───────┘


┌───────────────┐
│  Conversation  │  Load or create conversation, append user message
│  Manager       │
└───────┬───────┘


┌───────────────┐
│  Agent Engine  │  ReAct loop / Plan-and-Execute
└───────┬───────┘


┌───────────────┐
│  SSE / Direct  │  Stream tokens or return complete response
│  Response      │
└───────┬───────┘


┌───────────────┐
│  Persist       │  Save assistant message to database
└───────────────┘

Sending a Message

REST Endpoint

bash
curl -X POST http://localhost:18088/api/v1/chat/1/message \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "What is the current time in Tokyo?",
    "conversationId": "conv-abc123"
  }'

If conversationId is omitted, a new conversation is created automatically.

Response

json
{
  "code": 200,
  "data": {
    "messageId": "msg-xyz789",
    "conversationId": "conv-abc123",
    "content": "The current time in Tokyo is 2025-03-15 23:30:22 JST.",
    "role": "assistant"
  }
}

SSE Streaming

For real-time token-by-token responses, use the SSE endpoint:

Connecting

javascript
const eventSource = new EventSource(
  '/api/v1/chat/1/stream?conversationId=conv-abc123',
  { headers: { 'Authorization': 'Bearer YOUR_JWT_TOKEN' } }
);

eventSource.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log(data.content);  // Partial response token
};

eventSource.onerror = (error) => {
  console.error('SSE connection error', error);
  eventSource.close();
};

Event Types

The SSE stream emits these event types:

EventDescription
messageA chunk of the assistant's response text
tool_callThe agent is invoking a tool (includes tool name and parameters)
tool_resultThe tool returned a result
thinkingThe agent's internal reasoning step (ReAct thought)
planA plan was generated (Plan-and-Execute mode)
errorAn error occurred during processing
doneThe response is complete

Example SSE Stream

event: thinking
data: {"content": "The user wants the current time in Tokyo. I should use DateTimeTool."}

event: tool_call
data: {"tool": "DateTimeTool", "input": {"timezone": "Asia/Tokyo"}}

event: tool_result
data: {"tool": "DateTimeTool", "output": "2025-03-15T23:30:22"}

event: message
data: {"content": "The current time in Tokyo is "}

event: message
data: {"content": "2025-03-15 23:30:22 JST."}

event: done
data: {}

File & Directory Upload

Upload Methods

MethodDescription
ClickClick the attachment button to select files
PastePaste files from clipboard (Ctrl+V / Cmd+V)
Drag & DropDrag files or folders into the chat area

Drag & Drop

When dragging files or folders into the chat area, a translucent overlay appears. On drop:

  • Files — Uploaded to the server as attachments
  • Folders (Electron) — Local absolute path recorded as reference; Agent accesses files via ReadFileTool / ShellExecuteTool
  • Folders (Web) — Directory recursively expanded, all files uploaded individually

Upload Limits

SettingDefault
Max file size100 MB
Max request size200 MB
File typesAll (*/*)

Conversation Management

Data Model

mate_conversation

ColumnDescription
idConversation ID
user_idOwner
agent_idAssociated agent
titleConversation title (auto-generated from first message)
create_timeWhen the conversation started
update_timeLast activity time

mate_message

ColumnDescription
idMessage ID
conversation_idParent conversation
roleuser, assistant, system, tool
contentMessage text
tool_callsJSON array of tool calls (for assistant messages)
tool_call_idAssociated tool call ID (for tool messages)
create_timeTimestamp

API Endpoints

List conversations:

bash
curl http://localhost:18088/api/v1/conversations \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Get conversation history:

bash
curl http://localhost:18088/api/v1/conversations/conv-abc123/messages \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Delete a conversation:

bash
curl -X DELETE http://localhost:18088/api/v1/conversations/conv-abc123 \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Context Window

MateClaw automatically manages the context window sent to the LLM:

  1. System prompt -- Always included
  2. Recent messages -- As many recent messages as fit in the model's context limit
  3. Tool results -- Included inline with the conversation
  4. Memory -- Workspace memory entries if enabled

When the conversation exceeds the context limit, older messages are truncated from the beginning while preserving the system prompt and the most recent exchanges.

Multi-Channel Support

Different channels handle messages using different protocols:

ChannelProtocolDescription
WebSSEReal-time streaming output in the browser
DingTalkStream (long connection)Sends and receives messages via DingTalk official SDK WebSocket
FeishuWebSocketSends and receives messages via Feishu long-connection mode
WeComLong connectionSends and receives messages via WeChat Work long-connection mode
TelegramLong-Polling / WebhookDefault Long-Polling mode, switchable to Webhook
DiscordGateway WebSocketSends and receives messages via JDA library Gateway long connection

All channels share the same Agent engine and tool system -- only the message input/output adapters differ.

Next Steps