Skip to content

Commit 2c4ef0a

Browse files
committed
fix: align streaming chunk event format to official API
1 parent 951d248 commit 2c4ef0a

1 file changed

Lines changed: 40 additions & 30 deletions

File tree

‎examples/server/routes_openai.cpp‎

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ static bool build_openai_edit_request(const httplib::Request& req,
8989
return false;
9090
}
9191

92-
// Parse stream boolean fro m form field
9392
stream = false;
9493
if (req.form.has_field("stream")) {
9594
std::string stream_val = req.form.get_field("stream");
@@ -263,6 +262,14 @@ struct PreviewStreamContext {
263262
const ImgGenJobRequest* request = nullptr;
264263
};
265264

265+
static void write_sse_event(httplib::DataSink& sink, const json& event) {
266+
if (!sink.is_writable()) {
267+
return;
268+
}
269+
std::string sse = "data: " + event.dump() + "\n\n";
270+
sink.write(sse.data(), sse.size());
271+
}
272+
266273
static void preview_callback(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data) {
267274
if (!data || frame_count <= 0 || !frames || !frames[0].data) {
268275
return;
@@ -286,13 +293,17 @@ static void preview_callback(int step, int frame_count, sd_image_t* frames, bool
286293
return;
287294
}
288295

289-
json preview_chunk;
290-
preview_chunk["object"] = "image.preview";
291-
preview_chunk["step"] = step;
292-
preview_chunk["data"] = json::array({{{"b64_json", base64_encode(image_bytes)}}});
293-
294-
std::string sse_data = "data: " + preview_chunk.dump() + "\n\n";
295-
ctx->sink->write(sse_data.data(), sse_data.size());
296+
json chunk;
297+
chunk["type"] = "image_generation.partial_image";
298+
chunk["b64_json"] = base64_encode(image_bytes);
299+
chunk["created_at"] = static_cast<long long>(std::time(nullptr));
300+
chunk["size"] = std::to_string(frames[0].width) + "x" + std::to_string(frames[0].height);
301+
chunk["quality"] = "auto";
302+
chunk["background"] = frames[0].channel != 4 ? "opaque" : "auto";
303+
chunk["output_format"] = "jpeg";
304+
chunk["partial_image_index"] = step;
305+
306+
write_sse_event(*ctx->sink, chunk);
296307
}
297308

298309
static void execute_streaming_img_gen(ServerRuntime& runtime,
@@ -324,21 +335,17 @@ static void execute_streaming_img_gen(ServerRuntime& runtime,
324335
sd_set_preview_callback(nullptr, PREVIEW_NONE, 0, false, false, nullptr);
325336
}
326337

327-
if (results.empty() || !sink.is_writable()) {
338+
if (results.empty()) {
328339
if (sink.is_writable()) {
329-
json err = {{"error", "generate_image returned no results"}};
330-
std::string err_msg = "data: " + err.dump() + "\n\n";
331-
sink.write(err_msg.data(), err_msg.size());
340+
json err;
341+
err["type"] = "image_generation.error";
342+
err["error"] = "generate_image returned no results";
343+
write_sse_event(sink, err);
332344
}
333345
sink.done();
334346
return;
335347
}
336348

337-
json final_out;
338-
final_out["created"] = static_cast<long long>(std::time(nullptr));
339-
final_out["data"] = json::array();
340-
final_out["output_format"] = request.output_format;
341-
342349
int result_count = results.count();
343350
int images_per_batch = request.gen_params.batch_count > 0
344351
? std::max(1, result_count / request.gen_params.batch_count)
@@ -358,17 +365,23 @@ static void execute_streaming_img_gen(ServerRuntime& runtime,
358365
: EncodedImageFormat::PNG,
359366
results[i].data, results[i].width, results[i].height,
360367
results[i].channel, params, request.output_compression);
361-
362-
if (!image_bytes.empty()) {
363-
final_out["data"].push_back({{"b64_json", base64_encode(image_bytes)}});
368+
if (image_bytes.empty()) {
369+
continue;
364370
}
371+
372+
json chunk;
373+
chunk["type"] = "image_generation.completed";
374+
chunk["b64_json"] = base64_encode(image_bytes);
375+
chunk["created_at"] = static_cast<long long>(std::time(nullptr));
376+
chunk["size"] = std::to_string(results[i].width) + "x" + std::to_string(results[i].height);
377+
chunk["quality"] = "auto";
378+
chunk["background"] = results[i].channel == 4 ? "transparent" : "opaque";
379+
chunk["output_format"] = request.output_format;
380+
write_sse_event(sink, chunk);
365381
}
366382

367383
if (sink.is_writable()) {
368-
std::string final_sse = "data: " + final_out.dump() + "\n\n";
369-
sink.write(final_sse.data(), final_sse.size());
370-
371-
const std::string done_msg = "data: [DONE]\n\n";
384+
static const std::string done_msg = "data: [DONE]\n\n";
372385
sink.write(done_msg.data(), done_msg.size());
373386
}
374387

@@ -411,13 +424,11 @@ void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
411424
"text/event-stream",
412425
[runtime, request](size_t /*offset*/, httplib::DataSink& sink) mutable {
413426
execute_streaming_img_gen(*runtime, request, sink);
414-
return false;
415-
}
416-
);
427+
return false;
428+
});
417429
return;
418430
}
419431

420-
421432
SDImageVec results;
422433
if (!execute_sync_img_gen_request(*runtime, request, results, error_message)) {
423434
res.status = 500;
@@ -503,8 +514,7 @@ void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
503514
[runtime, req_ptr](size_t /*offset*/, httplib::DataSink& sink) {
504515
execute_streaming_img_gen(*runtime, *req_ptr, sink);
505516
return false;
506-
}
507-
);
517+
});
508518
return;
509519
}
510520

0 commit comments

Comments
 (0)