mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
R3
This commit is contained in:
@@ -1,167 +0,0 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::pin::Pin;
|
||||
use std::task::Context;
|
||||
use std::task::Poll;
|
||||
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use futures::Stream;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::stream::ResponseEvent;
|
||||
|
||||
pub trait AggregateStreamExt: Stream<Item = Result<ResponseEvent>> + Sized {
|
||||
fn aggregate(self) -> AggregatedChatStream<Self>
|
||||
where
|
||||
Self: Unpin,
|
||||
{
|
||||
AggregatedChatStream::new(self, AggregateMode::AggregatedOnly)
|
||||
}
|
||||
|
||||
fn streaming_mode(self) -> AggregatedChatStream<Self>
|
||||
where
|
||||
Self: Unpin,
|
||||
{
|
||||
AggregatedChatStream::new(self, AggregateMode::Streaming)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AggregateStreamExt for S where S: Stream<Item = Result<ResponseEvent>> + Sized + Unpin {}
|
||||
|
||||
enum AggregateMode {
|
||||
AggregatedOnly,
|
||||
Streaming,
|
||||
}
|
||||
|
||||
pub struct AggregatedChatStream<S> {
|
||||
inner: S,
|
||||
cumulative: String,
|
||||
cumulative_reasoning: String,
|
||||
pending: VecDeque<ResponseEvent>,
|
||||
mode: AggregateMode,
|
||||
}
|
||||
|
||||
impl<S> AggregatedChatStream<S>
|
||||
where
|
||||
S: Stream<Item = Result<ResponseEvent>> + Unpin,
|
||||
{
|
||||
fn new(inner: S, mode: AggregateMode) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
cumulative: String::new(),
|
||||
cumulative_reasoning: String::new(),
|
||||
pending: VecDeque::new(),
|
||||
mode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Stream for AggregatedChatStream<S>
|
||||
where
|
||||
S: Stream<Item = Result<ResponseEvent>> + Unpin,
|
||||
{
|
||||
type Item = Result<ResponseEvent>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
if let Some(ev) = self.pending.pop_front() {
|
||||
return Poll::Ready(Some(Ok(ev)));
|
||||
}
|
||||
|
||||
loop {
|
||||
match Pin::new(&mut self.inner).poll_next(cx) {
|
||||
Poll::Pending => return Poll::Pending,
|
||||
Poll::Ready(None) => return Poll::Ready(None),
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
return Poll::Ready(Some(Err(err)));
|
||||
}
|
||||
Poll::Ready(Some(Ok(ResponseEvent::OutputItemDone(item)))) => {
|
||||
let is_assistant_message = matches!(
|
||||
&item,
|
||||
ResponseItem::Message { role, .. } if role == "assistant"
|
||||
);
|
||||
|
||||
if is_assistant_message {
|
||||
if let ResponseItem::Message { role, content, .. } = item {
|
||||
let mut text = String::new();
|
||||
for c in content {
|
||||
match c {
|
||||
ContentItem::InputText { text: t }
|
||||
| ContentItem::OutputText { text: t } => text.push_str(&t),
|
||||
ContentItem::InputImage { image_url } => {
|
||||
text.push_str(&image_url)
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cumulative.push_str(&text);
|
||||
if matches!(self.mode, AggregateMode::Streaming) {
|
||||
let output_item =
|
||||
ResponseEvent::OutputItemDone(ResponseItem::Message {
|
||||
id: None,
|
||||
role,
|
||||
content: vec![ContentItem::OutputText {
|
||||
text: self.cumulative.clone(),
|
||||
}],
|
||||
});
|
||||
self.pending.push_back(output_item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Poll::Ready(Some(Ok(ResponseEvent::OutputItemDone(item))));
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Ok(ResponseEvent::OutputItemAdded(item)))) => {
|
||||
if !matches!(
|
||||
&item,
|
||||
ResponseItem::Message { role, .. } if role == "assistant"
|
||||
) {
|
||||
return Poll::Ready(Some(Ok(ResponseEvent::OutputItemAdded(item))));
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Ok(ResponseEvent::ReasoningContentDelta(delta)))) => {
|
||||
self.cumulative_reasoning.push_str(&delta);
|
||||
if matches!(self.mode, AggregateMode::Streaming) {
|
||||
let ev =
|
||||
ResponseEvent::ReasoningContentDelta(self.cumulative_reasoning.clone());
|
||||
self.pending.push_back(ev);
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Ok(ResponseEvent::ReasoningSummaryDelta(delta)))) => {
|
||||
if matches!(self.mode, AggregateMode::Streaming) {
|
||||
let ev = ResponseEvent::ReasoningSummaryDelta(delta);
|
||||
self.pending.push_back(ev);
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Ok(ResponseEvent::Completed {
|
||||
response_id,
|
||||
token_usage,
|
||||
}))) => {
|
||||
let assistant_event = ResponseEvent::OutputItemDone(ResponseItem::Message {
|
||||
id: None,
|
||||
role: "assistant".to_string(),
|
||||
content: vec![ContentItem::OutputText {
|
||||
text: self.cumulative.clone(),
|
||||
}],
|
||||
});
|
||||
let completion_event = ResponseEvent::Completed {
|
||||
response_id,
|
||||
token_usage,
|
||||
};
|
||||
|
||||
if matches!(self.mode, AggregateMode::Streaming) {
|
||||
self.pending.push_back(assistant_event);
|
||||
self.pending.push_back(completion_event);
|
||||
} else {
|
||||
return Poll::Ready(Some(Ok(assistant_event)));
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Ok(ev))) => {
|
||||
return Poll::Ready(Some(Ok(ev)));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ev) = self.pending.pop_front() {
|
||||
return Poll::Ready(Some(Ok(ev)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use crate::error::Error;
|
||||
use crate::error::Result;
|
||||
use crate::stream::ResponseEvent;
|
||||
use crate::stream::ResponseStream;
|
||||
use crate::stream::WireResponseStream;
|
||||
use codex_provider_config::ModelProviderInfo;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -25,6 +26,7 @@ pub struct ChatCompletionsApiClientConfig {
|
||||
pub model: String,
|
||||
pub otel_event_manager: OtelEventManager,
|
||||
pub session_source: SessionSource,
|
||||
pub extra_headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -56,12 +58,18 @@ impl PayloadClient for ChatCompletionsApiClient {
|
||||
}
|
||||
|
||||
let auth = crate::client::http::resolve_auth(&None).await;
|
||||
let extra_headers: Vec<(&str, String)> = self
|
||||
.config
|
||||
.extra_headers
|
||||
.iter()
|
||||
.map(|(k, v)| (k.as_str(), v.clone()))
|
||||
.collect();
|
||||
let mut req_builder = crate::client::http::build_request(
|
||||
&self.config.http_client,
|
||||
&self.config.provider,
|
||||
&auth,
|
||||
session_source,
|
||||
&[],
|
||||
&extra_headers,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -95,3 +103,25 @@ impl PayloadClient for ChatCompletionsApiClient {
|
||||
Ok(crate::stream::EventStream::from_receiver(rx_event))
|
||||
}
|
||||
}
|
||||
|
||||
impl ChatCompletionsApiClient {
|
||||
pub async fn stream_payload_wire(
|
||||
&self,
|
||||
payload_json: &serde_json::Value,
|
||||
session_source: Option<&codex_protocol::protocol::SessionSource>,
|
||||
) -> Result<WireResponseStream> {
|
||||
use futures::StreamExt;
|
||||
let legacy = self.stream_payload(payload_json, session_source).await?;
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1600);
|
||||
tokio::spawn(async move {
|
||||
futures::pin_mut!(legacy);
|
||||
while let Some(item) = legacy.next().await {
|
||||
let converted = item.and_then(|ev| crate::wire::map_response_event_to_wire(ev));
|
||||
if tx.send(converted).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(crate::stream::EventStream::from_receiver(rx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
pub mod aggregate;
|
||||
pub mod api;
|
||||
pub mod auth;
|
||||
pub mod chat;
|
||||
@@ -10,8 +9,8 @@ pub mod error;
|
||||
pub mod responses;
|
||||
pub mod routed_client;
|
||||
pub mod stream;
|
||||
mod wire;
|
||||
|
||||
pub use crate::aggregate::AggregateStreamExt;
|
||||
pub use crate::auth::AuthContext;
|
||||
pub use crate::auth::AuthProvider;
|
||||
pub use crate::chat::ChatCompletionsApiClient;
|
||||
@@ -32,9 +31,5 @@ pub use crate::stream::TextFormat;
|
||||
pub use crate::stream::TextFormatType;
|
||||
pub use crate::stream::WireEvent;
|
||||
pub use crate::stream::WireResponseStream;
|
||||
pub use codex_provider_config::BUILT_IN_OSS_MODEL_PROVIDER_ID;
|
||||
pub use codex_provider_config::ModelProviderInfo;
|
||||
pub use codex_provider_config::WireApi;
|
||||
pub use codex_provider_config::built_in_model_providers;
|
||||
pub use codex_provider_config::create_oss_provider;
|
||||
pub use codex_provider_config::create_oss_provider_with_base_url;
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::error::Error;
|
||||
use crate::error::Result;
|
||||
use crate::stream::ResponseEvent;
|
||||
use crate::stream::ResponseStream;
|
||||
use crate::stream::WireResponseStream;
|
||||
use codex_provider_config::ModelProviderInfo;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -34,6 +35,7 @@ pub struct ResponsesApiClientConfig {
|
||||
pub conversation_id: ConversationId,
|
||||
pub auth_provider: Option<Arc<dyn AuthProvider>>,
|
||||
pub otel_event_manager: OtelEventManager,
|
||||
pub extra_headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -77,10 +79,21 @@ impl PayloadClient for ResponsesApiClient {
|
||||
.unwrap_or_else(|_| "<unable to serialize payload>".to_string())
|
||||
);
|
||||
|
||||
let extra_headers = vec![
|
||||
("conversation_id", self.config.conversation_id.to_string()),
|
||||
("session_id", self.config.conversation_id.to_string()),
|
||||
let mut owned_headers: Vec<(String, String)> = vec![
|
||||
(
|
||||
"conversation_id".to_string(),
|
||||
self.config.conversation_id.to_string(),
|
||||
),
|
||||
(
|
||||
"session_id".to_string(),
|
||||
self.config.conversation_id.to_string(),
|
||||
),
|
||||
];
|
||||
owned_headers.extend(self.config.extra_headers.iter().cloned());
|
||||
let extra_headers: Vec<(&str, String)> = owned_headers
|
||||
.iter()
|
||||
.map(|(k, v)| (k.as_str(), v.clone()))
|
||||
.collect();
|
||||
let mut req_builder = crate::client::http::build_request(
|
||||
&self.config.http_client,
|
||||
&self.config.provider,
|
||||
@@ -140,3 +153,25 @@ impl PayloadClient for ResponsesApiClient {
|
||||
Ok(crate::stream::EventStream::from_receiver(rx_event))
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponsesApiClient {
|
||||
pub async fn stream_payload_wire(
|
||||
&self,
|
||||
payload_json: &Value,
|
||||
session_source: Option<&codex_protocol::protocol::SessionSource>,
|
||||
) -> Result<WireResponseStream> {
|
||||
use futures::StreamExt;
|
||||
let legacy = self.stream_payload(payload_json, session_source).await?;
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(1600);
|
||||
tokio::spawn(async move {
|
||||
futures::pin_mut!(legacy);
|
||||
while let Some(item) = legacy.next().await {
|
||||
let converted = item.and_then(|ev| crate::wire::map_response_event_to_wire(ev));
|
||||
if tx.send(converted).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(crate::stream::EventStream::from_receiver(rx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::WireResponseStream;
|
||||
use crate::api::PayloadClient;
|
||||
use crate::auth::AuthProvider;
|
||||
use crate::client::fixtures::stream_from_fixture;
|
||||
use crate::stream::WireTokenUsage;
|
||||
use codex_provider_config::ModelProviderInfo;
|
||||
|
||||
/// Dispatches to the appropriate API client implementation based on the provider wire API.
|
||||
@@ -62,6 +61,7 @@ impl RoutedApiClient {
|
||||
conversation_id: self.config.conversation_id,
|
||||
auth_provider: self.config.auth_provider.clone(),
|
||||
otel_event_manager: self.config.otel_event_manager.clone(),
|
||||
extra_headers: vec![],
|
||||
};
|
||||
let client = <ResponsesApiClient as crate::api::PayloadClient>::new(cfg)?;
|
||||
client
|
||||
@@ -75,6 +75,7 @@ impl RoutedApiClient {
|
||||
model: self.config.model.clone(),
|
||||
otel_event_manager: self.config.otel_event_manager.clone(),
|
||||
session_source: self.config.session_source.clone(),
|
||||
extra_headers: vec![],
|
||||
};
|
||||
let client = <ChatCompletionsApiClient as crate::api::PayloadClient>::new(cfg)?;
|
||||
client
|
||||
@@ -122,52 +123,5 @@ impl PayloadClient for RoutedApiClient {
|
||||
}
|
||||
|
||||
fn map_response_event_to_wire(ev: crate::stream::ResponseEvent) -> Result<WireEvent> {
|
||||
Ok(match ev {
|
||||
crate::stream::ResponseEvent::Created => WireEvent::Created,
|
||||
crate::stream::ResponseEvent::OutputItemDone(item) => {
|
||||
WireEvent::OutputItemDone(serde_json::to_value(item).unwrap_or(serde_json::Value::Null))
|
||||
}
|
||||
crate::stream::ResponseEvent::OutputItemAdded(item) => WireEvent::OutputItemAdded(
|
||||
serde_json::to_value(item).unwrap_or(serde_json::Value::Null),
|
||||
),
|
||||
crate::stream::ResponseEvent::Completed {
|
||||
response_id,
|
||||
token_usage,
|
||||
} => {
|
||||
let mapped = token_usage.map(|u| WireTokenUsage {
|
||||
input_tokens: u.input_tokens,
|
||||
cached_input_tokens: u.cached_input_tokens,
|
||||
output_tokens: u.output_tokens,
|
||||
reasoning_output_tokens: u.reasoning_output_tokens,
|
||||
total_tokens: u.total_tokens,
|
||||
});
|
||||
WireEvent::Completed {
|
||||
response_id,
|
||||
token_usage: mapped,
|
||||
}
|
||||
}
|
||||
crate::stream::ResponseEvent::OutputTextDelta(s) => WireEvent::OutputTextDelta(s),
|
||||
crate::stream::ResponseEvent::ReasoningSummaryDelta(s) => {
|
||||
WireEvent::ReasoningSummaryDelta(s)
|
||||
}
|
||||
crate::stream::ResponseEvent::ReasoningContentDelta(s) => {
|
||||
WireEvent::ReasoningContentDelta(s)
|
||||
}
|
||||
crate::stream::ResponseEvent::ReasoningSummaryPartAdded => {
|
||||
WireEvent::ReasoningSummaryPartAdded
|
||||
}
|
||||
crate::stream::ResponseEvent::RateLimits(s) => {
|
||||
let to_win = |w: Option<codex_protocol::protocol::RateLimitWindow>| -> Option<crate::stream::WireRateLimitWindow> {
|
||||
w.map(|w| crate::stream::WireRateLimitWindow {
|
||||
used_percent: Some(w.used_percent),
|
||||
window_minutes: w.window_minutes,
|
||||
resets_at: w.resets_at,
|
||||
})
|
||||
};
|
||||
WireEvent::RateLimits(crate::stream::WireRateLimitSnapshot {
|
||||
primary: to_win(s.primary),
|
||||
secondary: to_win(s.secondary),
|
||||
})
|
||||
}
|
||||
})
|
||||
crate::wire::map_response_event_to_wire(ev)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use crate::error::Result;
|
||||
use crate::stream::WireEvent;
|
||||
use crate::stream::WireRateLimitSnapshot;
|
||||
use crate::stream::WireRateLimitWindow;
|
||||
|
||||
pub fn map_response_event_to_wire(ev: crate::stream::ResponseEvent) -> Result<WireEvent> {
|
||||
Ok(match ev {
|
||||
crate::stream::ResponseEvent::Created => WireEvent::Created,
|
||||
crate::stream::ResponseEvent::OutputItemDone(item) => {
|
||||
WireEvent::OutputItemDone(serde_json::to_value(item).unwrap_or(serde_json::Value::Null))
|
||||
}
|
||||
crate::stream::ResponseEvent::OutputItemAdded(item) => WireEvent::OutputItemAdded(
|
||||
serde_json::to_value(item).unwrap_or(serde_json::Value::Null),
|
||||
),
|
||||
crate::stream::ResponseEvent::Completed {
|
||||
response_id,
|
||||
token_usage,
|
||||
} => {
|
||||
let mapped = token_usage.map(|u| crate::stream::WireTokenUsage {
|
||||
input_tokens: u.input_tokens,
|
||||
cached_input_tokens: u.cached_input_tokens,
|
||||
output_tokens: u.output_tokens,
|
||||
reasoning_output_tokens: u.reasoning_output_tokens,
|
||||
total_tokens: u.total_tokens,
|
||||
});
|
||||
WireEvent::Completed {
|
||||
response_id,
|
||||
token_usage: mapped,
|
||||
}
|
||||
}
|
||||
crate::stream::ResponseEvent::OutputTextDelta(s) => WireEvent::OutputTextDelta(s),
|
||||
crate::stream::ResponseEvent::ReasoningSummaryDelta(s) => {
|
||||
WireEvent::ReasoningSummaryDelta(s)
|
||||
}
|
||||
crate::stream::ResponseEvent::ReasoningContentDelta(s) => {
|
||||
WireEvent::ReasoningContentDelta(s)
|
||||
}
|
||||
crate::stream::ResponseEvent::ReasoningSummaryPartAdded => {
|
||||
WireEvent::ReasoningSummaryPartAdded
|
||||
}
|
||||
crate::stream::ResponseEvent::RateLimits(s) => {
|
||||
let to_win = |w: Option<codex_protocol::protocol::RateLimitWindow>| -> Option<WireRateLimitWindow> {
|
||||
w.map(|w| WireRateLimitWindow {
|
||||
used_percent: Some(w.used_percent),
|
||||
window_minutes: w.window_minutes,
|
||||
resets_at: w.resets_at,
|
||||
})
|
||||
};
|
||||
WireEvent::RateLimits(WireRateLimitSnapshot {
|
||||
primary: to_win(s.primary),
|
||||
secondary: to_win(s.secondary),
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user