Skip to content

Commit b01c52a

Browse files
committed
update docs for SSE
1 parent cf9dc1a commit b01c52a

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

api-docs.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,97 @@ curl -X POST "http://localhost:3432/api/generate-message" \
108108
}'
109109
```
110110

111+
#### POST `/api/generate-message/stream`
112+
Generates a response from an AI model with real-time Server-Sent Events (SSE) streaming. Ideal for native apps that want to display tokens as they arrive instead of polling.
113+
114+
**Authentication**: Session or API Key
115+
116+
**Request Body**: Same as `/api/generate-message` (excluding `image_params` since image/video models are not supported for streaming).
117+
118+
**Response**: `text/event-stream` with the following SSE events:
119+
120+
**Event Types**:
121+
122+
1. **`message_start`** - Sent immediately when generation begins
123+
```
124+
event: message_start
125+
data: {"conversation_id":"conv_abc123","message_id":"msg_xyz789"}
126+
```
127+
128+
2. **`delta`** - Sent for each token/chunk received (may be many of these)
129+
```
130+
event: delta
131+
data: {"content":"Hello","reasoning":""}
132+
```
133+
134+
3. **`message_complete`** - Sent when generation finishes successfully
135+
```
136+
event: message_complete
137+
data: {"token_count":123,"cost_usd":0.001,"response_time_ms":1500}
138+
```
139+
140+
4. **`error`** - Sent if an error occurs during generation
141+
```
142+
event: error
143+
data: {"error":"Something went wrong"}
144+
```
145+
146+
**Notes**:
147+
- Image and video generation models are **not supported** for streaming. Use `/api/generate-message` instead.
148+
- The connection remains open until generation completes or an error occurs.
149+
- Messages are still saved to the database, preserving conversation history.
150+
- Client disconnection is handled gracefully (generation is cancelled).
151+
- Supports all features of the non-streaming endpoint (web search, MCP tools, reasoning models, etc.).
152+
153+
**CURL Example**:
154+
```bash
155+
curl -N -X POST "http://localhost:3432/api/generate-message/stream" \
156+
-H "Content-Type: application/json" \
157+
-H "Authorization: Bearer your_api_key" \
158+
-d '{
159+
"message": "What is the capital of France?",
160+
"model_id": "gpt-4"
161+
}'
162+
```
163+
164+
**iOS/Swift Example**:
165+
```swift
166+
let url = URL(string: "https://your-app.com/api/generate-message/stream")!
167+
var request = URLRequest(url: url)
168+
request.httpMethod = "POST"
169+
request.setValue("Bearer nc_xxx", forHTTPHeaderField: "Authorization")
170+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
171+
request.httpBody = try JSONEncoder().encode(["message": "Hello", "model_id": "gpt-4o"])
172+
173+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
174+
// Process SSE events line by line
175+
}
176+
task.resume()
177+
```
178+
179+
**Android/Kotlin Example**:
180+
```kotlin
181+
val client = OkHttpClient()
182+
val jsonBody = """{"message":"Hello","model_id":"gpt-4o"}""".toRequestBody("application/json".toMediaType())
183+
val request = Request.Builder()
184+
.url("https://your-app.com/api/generate-message/stream")
185+
.post(jsonBody)
186+
.addHeader("Authorization", "Bearer nc_xxx")
187+
.build()
188+
189+
client.newCall(request).enqueue(object : Callback {
190+
override fun onResponse(call: Call, response: Response) {
191+
response.body?.source()?.let { source ->
192+
while (!source.exhausted()) {
193+
val line = source.readUtf8Line() ?: break
194+
// Parse SSE event lines
195+
}
196+
}
197+
}
198+
override fun onFailure(call: Call, e: IOException) { /* handle error */ }
199+
})
200+
```
201+
111202
---
112203

113204
### Text-to-Speech

0 commit comments

Comments
 (0)