mirror of
https://github.com/openai/codex.git
synced 2026-04-24 22:54:54 +00:00
This enables a new use case where `codex app-server` is embedded into a parent application that will directly own the user's ChatGPT auth lifecycle, which means it owns the user’s auth tokens and refreshes it when necessary. The parent application would just want a way to pass in the auth tokens for codex to use directly. The idea is that we are introducing a new "auth mode" currently only exposed via app server: **`chatgptAuthTokens`** which consist of the `id_token` (stores account metadata) and `access_token` (the bearer token used directly for backend API calls). These auth tokens are only stored in-memory. This new mode is in addition to the existing `apiKey` and `chatgpt` auth modes. This PR reuses the shape of our existing app-server account APIs as much as possible: - Update `account/login/start` with a new `chatgptAuthTokens` variant, which will allow the client to pass in the tokens and have codex app-server use them directly. Upon success, the server emits `account/login/completed` and `account/updated` notifications. - A new server->client request called `account/chatgptAuthTokens/refresh` which the server can use whenever the access token previously passed in has expired and it needs a new one from the parent application. I leveraged the core 401 retry loop which typically triggers auth token refreshes automatically, but made it pluggable: - **chatgpt** mode refreshes internally, as usual. - **chatgptAuthTokens** mode calls the client via `account/chatgptAuthTokens/refresh`, the client responds with updated tokens, codex updates its in-memory auth, then retries. This RPC has a 10s timeout and handles JSON-RPC errors from the client. Also some additional things: - chatgpt logins are blocked while external auth is active (have to log out first. typically clients will pick one OR the other, not support both) - `account/logout` clears external auth in memory - Ensures that if `forced_chatgpt_workspace_id` is set via the user's config, we respect it in both: - `account/login/start` with `chatgptAuthTokens` (returns a JSON-RPC error back to the client) - `account/chatgptAuthTokens/refresh` (fails the turn, and on next request app-server will send another `account/chatgptAuthTokens/refresh` request to the client).
340 lines
12 KiB
Rust
340 lines
12 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::atomic::AtomicI64;
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use codex_app_server_protocol::JSONRPCErrorError;
|
|
use codex_app_server_protocol::RequestId;
|
|
use codex_app_server_protocol::Result;
|
|
use codex_app_server_protocol::ServerNotification;
|
|
use codex_app_server_protocol::ServerRequest;
|
|
use codex_app_server_protocol::ServerRequestPayload;
|
|
use serde::Serialize;
|
|
use tokio::sync::Mutex;
|
|
use tokio::sync::mpsc;
|
|
use tokio::sync::oneshot;
|
|
use tracing::warn;
|
|
|
|
use crate::error_code::INTERNAL_ERROR_CODE;
|
|
|
|
#[cfg(test)]
|
|
use codex_protocol::account::PlanType;
|
|
|
|
/// Sends messages to the client and manages request callbacks.
|
|
pub(crate) struct OutgoingMessageSender {
|
|
next_request_id: AtomicI64,
|
|
sender: mpsc::Sender<OutgoingMessage>,
|
|
request_id_to_callback: Mutex<HashMap<RequestId, oneshot::Sender<Result>>>,
|
|
}
|
|
|
|
impl OutgoingMessageSender {
|
|
pub(crate) fn new(sender: mpsc::Sender<OutgoingMessage>) -> Self {
|
|
Self {
|
|
next_request_id: AtomicI64::new(0),
|
|
sender,
|
|
request_id_to_callback: Mutex::new(HashMap::new()),
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn send_request(
|
|
&self,
|
|
request: ServerRequestPayload,
|
|
) -> oneshot::Receiver<Result> {
|
|
let (_id, rx) = self.send_request_with_id(request).await;
|
|
rx
|
|
}
|
|
|
|
pub(crate) async fn send_request_with_id(
|
|
&self,
|
|
request: ServerRequestPayload,
|
|
) -> (RequestId, oneshot::Receiver<Result>) {
|
|
let id = RequestId::Integer(self.next_request_id.fetch_add(1, Ordering::Relaxed));
|
|
let outgoing_message_id = id.clone();
|
|
let (tx_approve, rx_approve) = oneshot::channel();
|
|
{
|
|
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
|
|
request_id_to_callback.insert(id, tx_approve);
|
|
}
|
|
|
|
let outgoing_message =
|
|
OutgoingMessage::Request(request.request_with_id(outgoing_message_id.clone()));
|
|
if let Err(err) = self.sender.send(outgoing_message).await {
|
|
warn!("failed to send request {outgoing_message_id:?} to client: {err:?}");
|
|
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
|
|
request_id_to_callback.remove(&outgoing_message_id);
|
|
}
|
|
(outgoing_message_id, rx_approve)
|
|
}
|
|
|
|
pub(crate) async fn notify_client_response(&self, id: RequestId, result: Result) {
|
|
let entry = {
|
|
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
|
|
request_id_to_callback.remove_entry(&id)
|
|
};
|
|
|
|
match entry {
|
|
Some((id, sender)) => {
|
|
if let Err(err) = sender.send(result) {
|
|
warn!("could not notify callback for {id:?} due to: {err:?}");
|
|
}
|
|
}
|
|
None => {
|
|
warn!("could not find callback for {id:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn notify_client_error(&self, id: RequestId, error: JSONRPCErrorError) {
|
|
let entry = {
|
|
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
|
|
request_id_to_callback.remove_entry(&id)
|
|
};
|
|
|
|
match entry {
|
|
Some((id, _sender)) => {
|
|
warn!("client responded with error for {id:?}: {error:?}");
|
|
}
|
|
None => {
|
|
warn!("could not find callback for {id:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn cancel_request(&self, id: &RequestId) -> bool {
|
|
let entry = {
|
|
let mut request_id_to_callback = self.request_id_to_callback.lock().await;
|
|
request_id_to_callback.remove_entry(id)
|
|
};
|
|
entry.is_some()
|
|
}
|
|
|
|
pub(crate) async fn send_response<T: Serialize>(&self, id: RequestId, response: T) {
|
|
match serde_json::to_value(response) {
|
|
Ok(result) => {
|
|
let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result });
|
|
if let Err(err) = self.sender.send(outgoing_message).await {
|
|
warn!("failed to send response to client: {err:?}");
|
|
}
|
|
}
|
|
Err(err) => {
|
|
self.send_error(
|
|
id,
|
|
JSONRPCErrorError {
|
|
code: INTERNAL_ERROR_CODE,
|
|
message: format!("failed to serialize response: {err}"),
|
|
data: None,
|
|
},
|
|
)
|
|
.await;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn send_server_notification(&self, notification: ServerNotification) {
|
|
if let Err(err) = self
|
|
.sender
|
|
.send(OutgoingMessage::AppServerNotification(notification))
|
|
.await
|
|
{
|
|
warn!("failed to send server notification to client: {err:?}");
|
|
}
|
|
}
|
|
|
|
/// All notifications should be migrated to [`ServerNotification`] and
|
|
/// [`OutgoingMessage::Notification`] should be removed.
|
|
pub(crate) async fn send_notification(&self, notification: OutgoingNotification) {
|
|
let outgoing_message = OutgoingMessage::Notification(notification);
|
|
if let Err(err) = self.sender.send(outgoing_message).await {
|
|
warn!("failed to send notification to client: {err:?}");
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn send_error(&self, id: RequestId, error: JSONRPCErrorError) {
|
|
let outgoing_message = OutgoingMessage::Error(OutgoingError { id, error });
|
|
if let Err(err) = self.sender.send(outgoing_message).await {
|
|
warn!("failed to send error to client: {err:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Outgoing message from the server to the client.
|
|
#[derive(Debug, Clone, Serialize)]
|
|
#[serde(untagged)]
|
|
pub(crate) enum OutgoingMessage {
|
|
Request(ServerRequest),
|
|
Notification(OutgoingNotification),
|
|
/// AppServerNotification is specific to the case where this is run as an
|
|
/// "app server" as opposed to an MCP server.
|
|
AppServerNotification(ServerNotification),
|
|
Response(OutgoingResponse),
|
|
Error(OutgoingError),
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
pub(crate) struct OutgoingNotification {
|
|
pub method: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub params: Option<serde_json::Value>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
pub(crate) struct OutgoingResponse {
|
|
pub id: RequestId,
|
|
pub result: Result,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
pub(crate) struct OutgoingError {
|
|
pub error: JSONRPCErrorError,
|
|
pub id: RequestId,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use codex_app_server_protocol::AccountLoginCompletedNotification;
|
|
use codex_app_server_protocol::AccountRateLimitsUpdatedNotification;
|
|
use codex_app_server_protocol::AccountUpdatedNotification;
|
|
use codex_app_server_protocol::AuthMode;
|
|
use codex_app_server_protocol::ConfigWarningNotification;
|
|
use codex_app_server_protocol::LoginChatGptCompleteNotification;
|
|
use codex_app_server_protocol::RateLimitSnapshot;
|
|
use codex_app_server_protocol::RateLimitWindow;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
use uuid::Uuid;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn verify_server_notification_serialization() {
|
|
let notification =
|
|
ServerNotification::LoginChatGptComplete(LoginChatGptCompleteNotification {
|
|
login_id: Uuid::nil(),
|
|
success: true,
|
|
error: None,
|
|
});
|
|
|
|
let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification);
|
|
assert_eq!(
|
|
json!({
|
|
"method": "loginChatGptComplete",
|
|
"params": {
|
|
"loginId": Uuid::nil(),
|
|
"success": true,
|
|
"error": null,
|
|
},
|
|
}),
|
|
serde_json::to_value(jsonrpc_notification)
|
|
.expect("ensure the strum macros serialize the method field correctly"),
|
|
"ensure the strum macros serialize the method field correctly"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn verify_account_login_completed_notification_serialization() {
|
|
let notification =
|
|
ServerNotification::AccountLoginCompleted(AccountLoginCompletedNotification {
|
|
login_id: Some(Uuid::nil().to_string()),
|
|
success: true,
|
|
error: None,
|
|
});
|
|
|
|
let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification);
|
|
assert_eq!(
|
|
json!({
|
|
"method": "account/login/completed",
|
|
"params": {
|
|
"loginId": Uuid::nil().to_string(),
|
|
"success": true,
|
|
"error": null,
|
|
},
|
|
}),
|
|
serde_json::to_value(jsonrpc_notification)
|
|
.expect("ensure the notification serializes correctly"),
|
|
"ensure the notification serializes correctly"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn verify_account_rate_limits_notification_serialization() {
|
|
let notification =
|
|
ServerNotification::AccountRateLimitsUpdated(AccountRateLimitsUpdatedNotification {
|
|
rate_limits: RateLimitSnapshot {
|
|
primary: Some(RateLimitWindow {
|
|
used_percent: 25,
|
|
window_duration_mins: Some(15),
|
|
resets_at: Some(123),
|
|
}),
|
|
secondary: None,
|
|
credits: None,
|
|
plan_type: Some(PlanType::Plus),
|
|
},
|
|
});
|
|
|
|
let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification);
|
|
assert_eq!(
|
|
json!({
|
|
"method": "account/rateLimits/updated",
|
|
"params": {
|
|
"rateLimits": {
|
|
"primary": {
|
|
"usedPercent": 25,
|
|
"windowDurationMins": 15,
|
|
"resetsAt": 123
|
|
},
|
|
"secondary": null,
|
|
"credits": null,
|
|
"planType": "plus"
|
|
}
|
|
},
|
|
}),
|
|
serde_json::to_value(jsonrpc_notification)
|
|
.expect("ensure the notification serializes correctly"),
|
|
"ensure the notification serializes correctly"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn verify_account_updated_notification_serialization() {
|
|
let notification = ServerNotification::AccountUpdated(AccountUpdatedNotification {
|
|
auth_mode: Some(AuthMode::ApiKey),
|
|
});
|
|
|
|
let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification);
|
|
assert_eq!(
|
|
json!({
|
|
"method": "account/updated",
|
|
"params": {
|
|
"authMode": "apikey"
|
|
},
|
|
}),
|
|
serde_json::to_value(jsonrpc_notification)
|
|
.expect("ensure the notification serializes correctly"),
|
|
"ensure the notification serializes correctly"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn verify_config_warning_notification_serialization() {
|
|
let notification = ServerNotification::ConfigWarning(ConfigWarningNotification {
|
|
summary: "Config error: using defaults".to_string(),
|
|
details: Some("error loading config: bad config".to_string()),
|
|
path: None,
|
|
range: None,
|
|
});
|
|
|
|
let jsonrpc_notification = OutgoingMessage::AppServerNotification(notification);
|
|
assert_eq!(
|
|
json!( {
|
|
"method": "configWarning",
|
|
"params": {
|
|
"summary": "Config error: using defaults",
|
|
"details": "error loading config: bad config",
|
|
},
|
|
}),
|
|
serde_json::to_value(jsonrpc_notification)
|
|
.expect("ensure the notification serializes correctly"),
|
|
"ensure the notification serializes correctly"
|
|
);
|
|
}
|
|
}
|