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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 56 additions & 1 deletion crates/openai-frontend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Instant,
};

use async_trait::async_trait;
Expand Down Expand Up @@ -61,16 +62,58 @@ impl CancellationToken {
}
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct OpenAiRequestContext {
cancellation: CancellationToken,
request_id: Option<String>,
started_at: Instant,
stream_usage_observation: bool,
trusted_agent_session: bool,
}

impl OpenAiRequestContext {
pub fn new() -> Self {
Self::default()
}

pub fn with_request_id(request_id: impl Into<String>) -> Self {
Self {
request_id: Some(request_id.into()),
..Self::default()
}
}

/// Enables internal stream-usage observation for an HTTP adapter. This is
/// deliberately separate from the client's `include_usage` wire option so
/// direct backend consumers retain their existing stream semantics.
pub fn with_stream_usage_observation(mut self) -> Self {
self.stream_usage_observation = true;
self
}

pub fn observes_stream_usage(&self) -> bool {
self.stream_usage_observation
}

pub(crate) fn with_trusted_agent_session(mut self) -> Self {
self.trusted_agent_session = true;
self
}

/// Returns whether the request entered through the configured trusted
/// agent-session header. Direct backend calls cannot set this marker.
pub fn has_trusted_agent_session(&self) -> bool {
self.trusted_agent_session
}

pub fn request_id(&self) -> Option<&str> {
self.request_id.as_deref()
}

pub fn elapsed(&self) -> std::time::Duration {
self.started_at.elapsed()
}

pub fn cancellation_token(&self) -> CancellationToken {
self.cancellation.clone()
}
Expand All @@ -84,6 +127,18 @@ impl OpenAiRequestContext {
}
}

impl Default for OpenAiRequestContext {
fn default() -> Self {
Self {
cancellation: CancellationToken::new(),
request_id: None,
started_at: Instant::now(),
stream_usage_observation: false,
trusted_agent_session: false,
}
}
}

#[async_trait]
pub trait OpenAiBackend: Send + Sync + 'static {
async fn models(&self) -> OpenAiResult<Vec<ModelObject>>;
Expand Down
2 changes: 2 additions & 0 deletions crates/openai-frontend/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::errors::OpenAiError;
const MAX_AGENT_SESSION_ID_BYTES: usize = 512;
const INTERNAL_AGENT_SESSION_ID: &str = "mesh_internal_agent_session_id";
const INTERNAL_AGENT_SESSION_SOURCE: &str = "mesh_internal_agent_session_source";
const INTERNAL_AGENT_SESSION_TRUSTED: &str = "mesh_internal_agent_session_trusted";

/// Where the OpenAI frontend obtained a stable, caller-authenticated session
/// identity. The identity is routing/lifecycle metadata, not a request ID. A
Expand Down Expand Up @@ -77,6 +78,7 @@ pub(crate) fn set_agent_session_metadata(
) {
extra.remove(INTERNAL_AGENT_SESSION_ID);
extra.remove(INTERNAL_AGENT_SESSION_SOURCE);
extra.remove(INTERNAL_AGENT_SESSION_TRUSTED);
if let Some(identity) = identity {
extra.insert(
INTERNAL_AGENT_SESSION_SOURCE.to_owned(),
Expand Down
Loading
Loading