use std::path::{Path, PathBuf};
use rig_core::{
Model,
completion::CompletionRequest,
error::ProviderError,
http_client::Builder,
providers::{
chatgpt,
openai::{
self,
wire::{CallerIdentity, DialectHooks},
},
},
};
use serde_json::Value;
use crate::auth::codex::{self as auth, ORIGINATOR};
pub const BASE_URL: &str = "https://chatgpt.com/backend-api/codex";
pub const CLIENT_VERSION: &str = "0.160.0";
static HOOKS: DialectHooks = DialectHooks {
default_endpoint: None,
model_route: None,
completion_envelope: Some(envelope),
modality_envelope: None,
};
fn envelope(_: &openai::OpenAIConfig, request: &CompletionRequest, builder: Builder) -> Builder {
let builder = builder
.header("OpenAI-Beta", "responses=experimental")
.header("accept", "text/event-stream");
match session(request) {
Some(session) => builder.header("session-id", session),
None => builder,
}
}
fn session(request: &CompletionRequest) -> Option<&str> {
request
.additional_params
.as_ref()?
.get("prompt_cache_key")?
.as_str()
}
pub fn user_agent() -> String {
format!(
"artifactize/{} ({} {}; {ORIGINATOR})",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH,
)
}
pub struct Codex {
model: String,
base: String,
state: PathBuf,
repo: PathBuf,
http: reqwest::Client,
}
impl Codex {
pub(super) fn new(
model: &str,
base: String,
state: &Path,
repo: &Path,
) -> Result<Self, String> {
Ok(Self {
model: model.into(),
base,
state: state.into(),
repo: repo.into(),
http: super::http_client()?,
})
}
pub(super) async fn model(
&self,
) -> Result<Model<openai::responses_api::wire::Responses>, auth::TokenError> {
let token = auth::access_token(Some(&self.state), Some(&self.repo)).await?;
let mut dialect = chatgpt::DIALECT;
dialect.quirks.hooks = Some(&HOOKS);
if let Some(identity) = &mut dialect.quirks.identity {
identity.session_ids = false;
}
let mut config = openai::OpenAIConfig::with_key(&dialect, token.access_token)
.with_base_url(&self.base)
.with_account_id(token.account_id);
config.instructions = None;
config.identity = Some(CallerIdentity {
originator: ORIGINATOR.into(),
user_agent: user_agent(),
});
Ok(config
.connect(super::delivery::Tap::new(self.http.clone()))
.responses(&self.model))
}
}
#[derive(Default)]
struct ModelCatalog {
models: crate::json::Optional<Vec<crate::json::Optional<crate::json::Object<PickerModel>>>>,
}
#[derive(Default)]
struct PickerModel {
visibility: crate::json::Optional<String>,
slug: crate::json::Optional<String>,
display_name: crate::json::Optional<String>,
}
impl<'de> serde::Deserialize<'de> for ModelCatalog {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct CatalogVisitor;
impl<'de> serde::de::Visitor<'de> for CatalogVisitor {
type Value = ModelCatalog;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a model catalog object")
}
fn visit_map<M: serde::de::MapAccess<'de>>(
self,
mut map: M,
) -> Result<Self::Value, M::Error> {
let mut catalog = ModelCatalog::default();
while let Some(field) = map.next_key::<String>()? {
if field == "models" {
catalog.models = map.next_value()?;
} else {
map.next_value::<crate::json::Ignored>()?;
}
}
Ok(catalog)
}
}
deserializer.deserialize_map(CatalogVisitor)
}
}
impl<'de> serde::Deserialize<'de> for PickerModel {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct ModelVisitor;
impl<'de> serde::de::Visitor<'de> for ModelVisitor {
type Value = PickerModel;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a picker model object")
}
fn visit_map<M: serde::de::MapAccess<'de>>(
self,
mut map: M,
) -> Result<Self::Value, M::Error> {
let mut model = PickerModel::default();
while let Some(field) = map.next_key::<String>()? {
match field.as_str() {
"visibility" => model.visibility = map.next_value()?,
"slug" => model.slug = map.next_value()?,
"display_name" => model.display_name = map.next_value()?,
_ => {
map.next_value::<crate::json::Ignored>()?;
}
}
}
Ok(model)
}
}
deserializer.deserialize_map(ModelVisitor)
}
}
fn picker_models(data: &[u8]) -> Result<Vec<super::models::ListedModel>, String> {
let invalid = || "Invalid Codex models response.".to_owned();
let crate::json::Object(catalog): crate::json::Object<ModelCatalog> =
serde_json::from_slice(data).map_err(|_| invalid())?;
catalog
.models
.0
.ok_or_else(invalid)?
.into_iter()
.filter_map(|model| model.0.map(|crate::json::Object(model)| model))
.filter(|model| model.visibility.0.as_deref() == Some("list"))
.map(|model| {
let slug = model
.slug
.0
.filter(|slug| !slug.is_empty())
.ok_or_else(invalid)?;
Ok(super::models::ListedModel {
display_name: model.display_name.0.unwrap_or_else(|| slug.clone()),
slug,
})
})
.collect()
}
pub(super) async fn models(
base: &str,
state: Option<&Path>,
repo: Option<&Path>,
) -> Result<Vec<super::models::ListedModel>, String> {
let token = auth::access_token(state, repo)
.await
.map_err(|error| error.message)?;
let response = super::http_client()?
.get(format!("{base}/models?client_version={CLIENT_VERSION}"))
.bearer_auth(&token.access_token)
.header("ChatGPT-Account-Id", &token.account_id)
.header("originator", ORIGINATOR)
.header(reqwest::header::USER_AGENT, user_agent())
.timeout(super::models::LIST_TIMEOUT)
.send()
.await
.map_err(|_| "Codex model listing failed; check your connection.".to_owned())?;
let status = response.status();
let data = response.bytes().await.unwrap_or_default();
if !status.is_success() {
let body: Value = serde_json::from_slice(&data).unwrap_or(Value::Null);
return Err(describe(
Some(status.as_u16()),
&body,
"Codex model listing failed.",
));
}
picker_models(&data)
}
fn error_object(body: &Value) -> &Value {
body.pointer("/response/error")
.filter(|error| error.is_object())
.or_else(|| body.get("error").filter(|error| error.is_object()))
.unwrap_or(body)
}
fn error_code(body: &Value) -> Option<&str> {
let error = error_object(body);
error["code"]
.as_str()
.or_else(|| error["type"].as_str())
.filter(|code| !code.is_empty())
}
fn usage_limit(status: Option<u16>, body: &Value) -> bool {
error_code(body)
.is_some_and(|code| matches!(code, "usage_limit_reached" | "usage_not_included"))
|| (status == Some(429) && error_object(body)["resets_at"].is_u64())
}
pub(super) fn usage_limited(error: &ProviderError) -> bool {
usage_limit(error.report().http_status, &body(error))
}
fn body(error: &ProviderError) -> Value {
error
.provider_response()
.and_then(|response| serde_json::from_str(&response.body).ok())
.unwrap_or(Value::Null)
}
pub(super) fn diagnostic(error: &ProviderError) -> String {
let body = body(error);
if body.is_null() {
return super::diagnostic(error);
}
describe(error.report().http_status, &body, &super::diagnostic(error))
}
pub(super) fn describe(status: Option<u16>, body: &Value, fallback: &str) -> String {
let error = error_object(body);
let code = error_code(body);
let mut message = if usage_limit(status, body) {
let plan = error["plan_type"]
.as_str()
.map(|plan| format!(" ({} plan)", plan.to_lowercase()))
.unwrap_or_default();
let reset = error["resets_at"]
.as_u64()
.zip(auth::now().ok())
.map(|(at, now)| {
format!(
" Try again in ~{} min.",
at.saturating_sub(now).div_ceil(60)
)
})
.unwrap_or_default();
format!("You have hit your ChatGPT usage limit{plan}.{reset}")
} else {
error["message"]
.as_str()
.or_else(|| body["detail"].as_str())
.unwrap_or(fallback)
.to_owned()
};
if let Some(code) = code {
message = format!("{code}: {message}");
}
if let Some(status) = status {
message.push_str(&format!(" (HTTP {status})"));
}
if matches!(status, Some(401 | 403)) {
message.push_str(
" Sign in again with `artifactize login codex`, or with Codex when ARTIFACTIZE_CODEX_AUTH_FILE is set.",
);
}
super::clean_diagnostic(&message)
}
#[cfg(test)]
mod tests;