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
3 changes: 2 additions & 1 deletion FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Record the new base tag and any dropped or reworked patch in the table below.

| Commit | What | Upstream status |
|---|---|---|
| — | none yet | — |
| — | `agent-state` terminal title item (`codex: ready\|working\|awaiting approval`), written only on change, with the user's title pushed (`CSI 22;0t`) at the first title write and popped (`CSI 23;0t`) on exit. Idle with `animations = false` schedules no refresh. Landed on `fork/wi-1127-agent-state` at `b5f9b014`. | fork-only; Klaudia contract from vogt `internal/tui/idle.go` |
| — | `wire_api = "chat"`: Chat Completions client restored for providers whose Responses path drops tool calls (grok via theclawbay/LiteLLM, WI-1139). New files only, plus one dispatch arm: `core/src/chat_request.rs` (request conversion; namespaced tools flattened to `ns__name`, freeform tools such as apply_patch exposed as a function taking one `input` string, calls restored by `ChatToolMap`), `codex-api/src/{endpoint,sse}/chat.rs` (client and SSE parser, fixture tests in `sse/chat_tests.rs`). Reasoning, web search and tool search are not sent over chat. Branch `fork/wi-1286-chat-wire`. | fork-only; upstream removed the chat client |

## External adapters

Expand Down
72 changes: 72 additions & 0 deletions codex-rs/codex-api/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! Chat Completions HTTP client for `wire_api = "chat"` (fork-only, WI-1286).

use crate::auth::SharedAuthProvider;
use crate::common::ResponseStream;
use crate::endpoint::session::EndpointSession;
use crate::error::ApiError;
use crate::provider::Provider;
use crate::sse::spawn_chat_stream;
use crate::telemetry::SseTelemetry;
use codex_client::EncodedJsonBody;
use codex_client::HttpTransport;
use codex_client::RequestTelemetry;
use http::HeaderMap;
use http::HeaderValue;
use http::Method;
use serde_json::Value;
use std::sync::Arc;

pub struct ChatClient<T: HttpTransport> {
session: EndpointSession<T>,
sse_telemetry: Option<Arc<dyn SseTelemetry>>,
}

impl<T: HttpTransport> ChatClient<T> {
pub fn new(transport: T, provider: Provider, auth: SharedAuthProvider) -> Self {
Self {
session: EndpointSession::new(transport, provider, auth),
sse_telemetry: None,
}
}

pub fn with_telemetry(
self,
request: Option<Arc<dyn RequestTelemetry>>,
sse: Option<Arc<dyn SseTelemetry>>,
) -> Self {
Self {
session: self.session.with_request_telemetry(request),
sse_telemetry: sse,
}
}

/// POST a chat request body to `/chat/completions` and stream the reply.
pub async fn stream(
&self,
body: &Value,
extra_headers: HeaderMap,
) -> Result<ResponseStream, ApiError> {
let body = EncodedJsonBody::encode(body)
.map_err(|e| ApiError::Stream(format!("failed to encode chat request: {e}")))?;
let stream_response = self
.session
.stream_encoded_json_with(
Method::POST,
"/chat/completions",
extra_headers,
Some(body),
|req| {
req.headers.insert(
http::header::ACCEPT,
HeaderValue::from_static("text/event-stream"),
);
},
)
.await?;
Ok(spawn_chat_stream(
stream_response,
self.session.provider().stream_idle_timeout,
self.sse_telemetry.clone(),
))
}
}
2 changes: 2 additions & 0 deletions codex-rs/codex-api/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod chat;
pub(crate) mod images;
pub(crate) mod memories;
pub(crate) mod models;
Expand All @@ -8,6 +9,7 @@ pub(crate) mod responses_websocket;
pub(crate) mod search;
mod session;

pub use chat::ChatClient;
pub use images::ImageRequestError;
pub use images::ImagesClient;
pub use memories::MemoriesClient;
Expand Down
1 change: 1 addition & 0 deletions codex-rs/codex-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub use crate::common::WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY;
pub use crate::common::WS_REQUEST_HEADER_TRACESTATE_CLIENT_METADATA_KEY;
pub use crate::common::create_text_param_for_request;
pub use crate::common::response_create_client_metadata;
pub use crate::endpoint::ChatClient;
pub use crate::endpoint::ImageRequestError;
pub use crate::endpoint::ImagesClient;
pub use crate::endpoint::MemoriesClient;
Expand Down
Loading
Loading