use bytes::Bytes;
use serde_json::json;
use super::super::super::canonical_response::{CanonicalEvent, CanonicalResponse};
use super::render::{finish_reason, render_chunk, usage_object};
#[cfg_attr(
not(feature = "test-api"),
expect(
unreachable_pub,
reason = "re-exported via `test_api` only when the feature is on"
)
)]
pub fn render_terminal_event_frame(
event: &CanonicalEvent,
snapshot: &CanonicalResponse,
) -> Option<Bytes> {
let CanonicalEvent::MessageStop { stop_reason, .. } = event else {
return None;
};
let reason = finish_reason(stop_reason.or(snapshot.stop_reason));
Some(render_chunk(
&snapshot.model,
&json!({}),
Some(reason),
None,
))
}
pub(super) fn render_stream_tail_frames(
snapshot: &CanonicalResponse,
include_usage: bool,
) -> Bytes {
let mut frames: Vec<u8> = Vec::new();
if include_usage {
frames.extend_from_slice(&render_usage_chunk(snapshot));
}
frames.extend_from_slice(b"data: [DONE]\n\n");
Bytes::from(frames)
}
fn render_usage_chunk(snapshot: &CanonicalResponse) -> Bytes {
let payload = json!({
"id": snapshot.id,
"object": "chat.completion.chunk",
"created": super::render::current_unix_ts(),
"model": snapshot.model,
"choices": [],
"usage": usage_object(&snapshot.usage),
});
Bytes::from(format!(
"data: {}\n\n",
serde_json::to_string(&payload).unwrap_or_else(|_| "{}".into())
))
}