Skip to content
Merged
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
25 changes: 14 additions & 11 deletions crates/api/src/routes/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,12 @@ fn build_final_usage_chunk_bytes(
}

// Helper function to extract inference ID from a parsed stream chunk
fn extract_inference_id_from_chunk(chunk: &inference_providers::StreamChunk) -> Uuid {
fn extract_inference_id_from_chunk(chunk: &inference_providers::StreamChunk) -> Option<Uuid> {
let id = match chunk {
inference_providers::StreamChunk::Chat(c) => &c.id,
inference_providers::StreamChunk::Text(c) => &c.id,
};
hash_inference_id_to_uuid(id)
(!id.is_empty()).then(|| hash_inference_id_to_uuid(id))
}

// Convert MessageContent to serde_json::Value, preserving multimodal parts (images, audio, etc.)
Expand Down Expand Up @@ -1635,8 +1635,9 @@ async fn chat_completions_inner(
stream_chat_id = Some(match chunk {
inference_providers::StreamChunk::Chat(c) => c.id.clone(),
inference_providers::StreamChunk::Text(c) => c.id.clone(),
});
break Some(extract_inference_id_from_chunk(chunk));
})
.filter(|id| !id.is_empty());
break extract_inference_id_from_chunk(chunk);
}
true
}
Expand Down Expand Up @@ -2730,8 +2731,9 @@ async fn completions_inner(
stream_chat_id = Some(match chunk {
inference_providers::StreamChunk::Chat(c) => c.id.clone(),
inference_providers::StreamChunk::Text(c) => c.id.clone(),
});
break Some(extract_inference_id_from_chunk(chunk));
})
.filter(|id| !id.is_empty());
break extract_inference_id_from_chunk(chunk);
}
true
}
Expand Down Expand Up @@ -4162,13 +4164,14 @@ mod tests {

#[test]
fn test_extract_inference_id_from_chunk_empty_id() {
// Given: Chutes omitted its provider inference id at the parsing boundary.
let chunk = make_chat_chunk("");

// When: the route derives the optional public inference id.
let result = extract_inference_id_from_chunk(&chunk);
// Empty string should still produce a valid UUID
assert!(
!result.is_nil(),
"empty provider ID should still produce a non-nil UUID"
);

// Then: absence stays absent instead of becoming a hash of the empty string.
assert_eq!(result, None);
}

fn empty_delta() -> inference_providers::models::ChatDelta {
Expand Down
10 changes: 9 additions & 1 deletion crates/inference_providers/src/attested/chutes/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub struct PreparedRequest {
/// so this must outlive the whole response.
pub struct ResponseSession {
response_dk: ml_kem::DecapsulationKey768,
synthetic_stream_id: String,
}

/// Build an E2EE request blob for `request_json` (the OpenAI request body)
Expand Down Expand Up @@ -215,11 +216,18 @@ pub fn build_request(

Ok(PreparedRequest {
blob,
session: ResponseSession { response_dk },
session: ResponseSession {
response_dk,
synthetic_stream_id: format!("chutes-gateway-{}", uuid::Uuid::new_v4()),
},
})
}

impl ResponseSession {
pub(super) fn synthetic_stream_id(&self) -> &str {
&self.synthetic_stream_id
}

/// Decrypt a non-streaming response blob (`mlkem_ct ‖ nonce ‖ ct ‖ tag`,
/// keyed with `info="e2e-resp-v1"`, gzip-compressed) into the OpenAI
/// response JSON bytes.
Expand Down
Loading
Loading