Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/switchyard-nemo-relay-plugin/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ mod tests {
assert!(captured.lock().unwrap().is_empty());
assert!(stream.next().await.expect("encoded stream event").is_ok());
assert!(captured.lock().unwrap().is_empty());
assert!(stream.next().await.expect("encoded usage event").is_ok());
assert!(stream.next().await.is_none());

let events = captured.lock().unwrap();
assert_eq!(events.len(), 3);
Expand Down
3 changes: 2 additions & 1 deletion crates/switchyard-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,8 @@ async fn handle_llm_request(
route.algorithm_name(),
)
});
// Only the Codex namespace mapping is needed downstream, not the whole request.
// Response encoding needs caller-owned extensions such as Chat stream usage
// opt-in and Codex tool identity restoration.
let request_extensions = request.llm_request.extensions.clone();
let observer = stats_observer(
state.stats.clone(),
Expand Down
50 changes: 40 additions & 10 deletions crates/switchyard-translation/src/codecs/openai_chat/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ impl StreamCodec for OpenAiChatStreamCodec {
encode_openai_chat_stream(state, event)
}

fn observe_replayed_event(
&self,
state: &mut StreamTranslationState,
_raw: &Value,
normalized: Vec<LlmResponseChunk>,
) {
let replayed_terminal = normalized
.iter()
.any(|chunk| matches!(chunk, LlmResponseChunk::MessageStop { .. }));
for chunk in normalized {
drop(self.encode_event(state, chunk));
}
if replayed_terminal {
state.finished = true;
state.openai_chat_usage_finalized = true;
}
}

fn finish(&self, state: &mut StreamTranslationState) -> Vec<Value> {
finish_openai_chat_stream(state)
}
Expand Down Expand Up @@ -292,11 +310,7 @@ fn encode_openai_chat_stream(
LlmResponseChunk::Usage(usage) => {
state.usage = usage;
state.saw_backend_usage = true;
if state.finished {
vec![openai_usage_chunk(state)]
} else {
Vec::new()
}
Vec::new()
}
LlmResponseChunk::MessageStop { reason } => {
if state.finished {
Expand All @@ -307,7 +321,7 @@ fn encode_openai_chat_stream(
state,
json!({}),
Some(openai_finish_reason(reason.as_deref())),
state.saw_backend_usage.then(|| openai_usage_value(state)),
None,
)]
}
LlmResponseChunk::DecodeError { message } | LlmResponseChunk::StreamError { message } => {
Expand All @@ -321,16 +335,32 @@ fn encode_openai_chat_stream(

// Emits a terminal chunk if the source stream ended before a stop event arrived.
fn finish_openai_chat_stream(state: &mut StreamTranslationState) -> Vec<Value> {
if state.finished || !state.saw_message_start {
if state.errored {
return Vec::new();
}
if state.finished {
return finalize_openai_chat_usage(state);
}
if !state.saw_message_start {
return Vec::new();
}
state.finished = true;
vec![openai_stream_chunk(
let mut out = vec![openai_stream_chunk(
state,
json!({}),
Some(openai_finish_reason(state.stop_reason.as_deref())),
state.saw_backend_usage.then(|| openai_usage_value(state)),
)]
None,
)];
out.extend(finalize_openai_chat_usage(state));
out
}

fn finalize_openai_chat_usage(state: &mut StreamTranslationState) -> Vec<Value> {
if !state.openai_chat_include_usage || state.openai_chat_usage_finalized {
return Vec::new();
}
state.openai_chat_usage_finalized = true;
vec![openai_usage_chunk(state)]
}

// Normalizes OpenAI token usage fields.
Expand Down
6 changes: 6 additions & 0 deletions crates/switchyard-translation/src/codecs/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub struct StreamTranslationState {
/// Set once an in-band error event was emitted; the encoder then emits nothing further.
pub errored: bool,
pub usage: Usage,
/// Whether the inbound Chat caller requested the final usage-only chunk.
#[serde(default)]
pub(crate) openai_chat_include_usage: bool,
/// Marks generated or preserved Chat usage finalization as complete.
#[serde(default)]
pub(crate) openai_chat_usage_finalized: bool,

pub(crate) output_tokens_seen: u64,
pub(crate) saw_backend_usage: bool,
Expand Down
Loading
Loading