Chat Response Commands
This document describes the server-to-client message events for chat responses in the WebSocket Gateway.
Message Event Types
message
The primary event for streaming chat responses. Server sends incremental content as the AI generates the response.
Data Fields:
| Field | Type | Description |
|---|---|---|
output |
string | The generated text content chunk |
stop |
boolean | true when the response stream is complete |
ok |
boolean | Indicates success status |
Example:
{
"event": "message",
"data": {
"output": "Hello! I'd be happy to help",
"stop": false,
"ok": true
}
}
When stop is true, the stream has ended:
{
"event": "message",
"data": {
"output": "",
"stop": true,
"ok": true
}
}
conv-message
Sent when a conversation is created or updated, providing conversation identification information.
Data Fields:
| Field | Type | Description |
|---|---|---|
conversationId |
string | Unique identifier for the conversation |
messageId |
string | Unique identifier for the specific message |
Example:
{
"event": "conv-message",
"data": {
"conversationId": "conv-123456",
"messageId": "msg-789012"
}
}
error
Sent when an error occurs during processing.
Data Fields:
| Field | Type | Description |
|---|---|---|
error |
string | Error message description |
Example:
{
"event": "error",
"data": {
"error": "Model service temporarily unavailable"
}
}
auth-connect-complete
Sent when the WebSocket authentication handshake is successfully completed.
Example:
{
"event": "auth-connect-complete",
"data": {
"ok": true
}
}
set-model-complete
Sent when the model selection/setting operation is completed.
Data Fields:
| Field | Type | Description |
|---|---|---|
model |
string | Full model identifier |
modelShortName |
string | Short/display name of the model |
Example:
{
"event": "set-model-complete",
"data": {
"model": "gpt-4-turbo",
"modelShortName": "GPT-4",
"ok": true
}
}
pong
Heartbeat response from server to confirm connection is alive.
Example:
{
"event": "pong",
"data": {}
}
Message Flow Sequence
A typical chat interaction follows this message sequence:
- Connection established →
auth-connect-complete - Model set (if needed) →
set-model-complete - Send chat request (Client → Server with
event: "gen") - Multiple
messageevents with incrementaloutputcontent - Final
messageevent withstop: true - Conversation info →
conv-message(if new conversation)
Content Filtering
The client should filter special markers from the output content:
<|end▁of▁sentence|>- End of sentence marker<|begin▁of▁sentence|>- Begin of sentence marker###- Section marker- Leading question marks (
?or?) on first line
Error Handling
When receiving an error event:
- Display the error message to the user
- Consider disconnecting and reconnecting if the error is connection-related
- Log the error for debugging purposes