pub struct SseEncoder {
pub id: String,
pub model: String,
pub created: u64,
}Expand description
Stateful encoder that produces OpenAI-shape SSE frames for one response.
The id, model, and created fields are stable across all frames the
encoder produces, matching the OpenAI streaming contract.
Fields§
§id: StringStable response id, e.g. "chatcmpl-…".
model: StringModel name as advertised to the client (NOT the upstream alias).
created: u64Unix epoch seconds when the response was created.
Implementations§
Source§impl SseEncoder
impl SseEncoder
Sourcepub fn new(
id: impl Into<String>,
model: impl Into<String>,
created: u64,
) -> Self
pub fn new( id: impl Into<String>, model: impl Into<String>, created: u64, ) -> Self
Construct a new encoder with the stable response metadata.
Sourcepub fn encode(&self, event: &LlmStreamEvent) -> Option<String>
pub fn encode(&self, event: &LlmStreamEvent) -> Option<String>
Encode a single LlmStreamEvent into one or more SSE frames.
Returns None when the event is not meant to appear on the wire (e.g.
LlmStreamEvent::NormalizationError, which the proxy logs but never
forwards to clients).
For LlmStreamEvent::Done, the returned String is only the
terminating chunk (with finish_reason set) — it deliberately does
not include the trailing data: [DONE]\n\n sentinel
(DONE_SENTINEL). Done is not guaranteed to be the last event on
the wire: a trailing LlmStreamEvent::Usage can legitimately arrive
afterward (see that variant’s doc), and nothing may follow [DONE]
once it’s sent. Callers must append DONE_SENTINEL themselves,
exactly once, only after the entire event stream is truly exhausted.
Sourcefn usage_frame(
&self,
prompt_tokens: u32,
completion_tokens: u32,
total_tokens: u32,
cached_tokens: Option<u32>,
) -> String
fn usage_frame( &self, prompt_tokens: u32, completion_tokens: u32, total_tokens: u32, cached_tokens: Option<u32>, ) -> String
Encode a LlmStreamEvent::Usage event.
Per the OpenAI stream_options.include_usage convention, the
usage-totals chunk carries an empty choices array (not omitted —
see crate::LlmStreamEvent::Usage doc) and a top-level usage
object.
Sourcefn upstream_error_frame(message: &str, error_type: &str, code: &str) -> String
fn upstream_error_frame(message: &str, error_type: &str, code: &str) -> String
Encode a LlmStreamEvent::UpstreamError event.
Deliberately bare — no id/object/created/model envelope and,
crucially, no choices key at all (unlike every other frame this
encoder produces). Clients such as the GitHub Copilot LLM Gateway
extension detect this exact shape
('error' in obj && !('choices' in obj)) to recognise an inline
mid-stream failure; wrapping it in the usual envelope or adding an
empty choices: [] would hide it as an ordinary chunk instead.
Does not append DONE_SENTINEL — see Self::encode doc; the
caller appends it exactly once after the stream is truly exhausted.
Trait Implementations§
Source§impl Clone for SseEncoder
impl Clone for SseEncoder
Source§fn clone(&self) -> SseEncoder
fn clone(&self) -> SseEncoder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more