mirror of
https://github.com/openai/codex.git
synced 2026-04-30 17:36:40 +00:00
remote tasks
This commit is contained in:
135
codex-rs/cloud-tasks/src/bin/conncheck.rs
Normal file
135
codex-rs/cloud-tasks/src/bin/conncheck.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use base64::Engine;
|
||||
use codex_backend_client::Client as BackendClient;
|
||||
use codex_core::config::find_codex_home;
|
||||
use codex_core::default_client::get_codex_user_agent;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::AuthMode;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Base URL (default to ChatGPT backend API)
|
||||
let base_url = std::env::var("CODEX_CLOUD_TASKS_BASE_URL")
|
||||
.unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string());
|
||||
println!("base_url: {base_url}");
|
||||
let path_style = if base_url.contains("/backend-api") {
|
||||
"wham"
|
||||
} else {
|
||||
"codex-api"
|
||||
};
|
||||
println!("path_style: {path_style}");
|
||||
|
||||
// Locate CODEX_HOME and try to load ChatGPT auth
|
||||
let codex_home = match find_codex_home() {
|
||||
Ok(p) => {
|
||||
println!("codex_home: {}", p.display());
|
||||
Some(p)
|
||||
}
|
||||
Err(e) => {
|
||||
println!("codex_home: <not found> ({e})");
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
// Build backend client with UA
|
||||
let ua = get_codex_user_agent(Some("codex_cloud_tasks_conncheck"));
|
||||
let mut client = BackendClient::new(base_url.clone())?.with_user_agent(ua);
|
||||
|
||||
// Attach bearer token if available from ChatGPT auth
|
||||
let mut have_auth = false;
|
||||
if let Some(home) = codex_home {
|
||||
let authm = AuthManager::new(
|
||||
home,
|
||||
AuthMode::ChatGPT,
|
||||
"codex_cloud_tasks_conncheck".to_string(),
|
||||
);
|
||||
if let Some(auth) = authm.auth() {
|
||||
match auth.get_token().await {
|
||||
Ok(token) if !token.is_empty() => {
|
||||
have_auth = true;
|
||||
println!("auth: ChatGPT token present ({} chars)", token.len());
|
||||
// Add Authorization header
|
||||
client = client.with_bearer_token(&token);
|
||||
|
||||
// Attempt to extract ChatGPT account id from the JWT and set header.
|
||||
if let Some(account_id) = extract_chatgpt_account_id(&token) {
|
||||
println!("auth: ChatGPT-Account-Id: {account_id}");
|
||||
client = client.with_chatgpt_account_id(account_id);
|
||||
} else if let Some(acc) = auth.get_account_id() {
|
||||
// Fallback: some older auth.jsons persist account_id
|
||||
println!("auth: ChatGPT-Account-Id (from auth.json): {acc}");
|
||||
client = client.with_chatgpt_account_id(acc);
|
||||
}
|
||||
}
|
||||
Ok(_) => {
|
||||
println!("auth: ChatGPT token empty");
|
||||
}
|
||||
Err(e) => {
|
||||
println!("auth: failed to load ChatGPT token: {e}");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("auth: no ChatGPT auth.json");
|
||||
}
|
||||
}
|
||||
|
||||
if !have_auth {
|
||||
println!("note: Online endpoints typically require ChatGPT sign-in. Run: `codex login`");
|
||||
}
|
||||
|
||||
// Attempt the /list call with a short timeout to avoid hanging
|
||||
match path_style {
|
||||
"wham" => println!("request: GET /wham/tasks/list?limit=5&task_filter=current"),
|
||||
_ => println!("request: GET /api/codex/tasks/list?limit=5&task_filter=current"),
|
||||
}
|
||||
let fut = client.list_tasks(Some(5), Some("current"), None);
|
||||
let res = tokio::time::timeout(Duration::from_secs(30), fut).await;
|
||||
match res {
|
||||
Err(_) => {
|
||||
println!("error: request timed out after 30s");
|
||||
std::process::exit(2);
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
// backend-client includes HTTP status and body in errors.
|
||||
println!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
Ok(Ok(list)) => {
|
||||
println!("ok: received {} tasks", list.items.len());
|
||||
for item in list.items.iter().take(5) {
|
||||
println!("- {} — {}", item.id, item.title);
|
||||
}
|
||||
// Print the full response object for debugging/inspection.
|
||||
match serde_json::to_string_pretty(&list) {
|
||||
Ok(json) => {
|
||||
println!("\nfull response object (pretty JSON):\n{}", json);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("failed to serialize response to JSON: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_chatgpt_account_id(token: &str) -> Option<String> {
|
||||
// JWT: header.payload.signature
|
||||
let mut parts = token.split('.');
|
||||
let (_h, payload_b64, _s) = match (parts.next(), parts.next(), parts.next()) {
|
||||
(Some(h), Some(p), Some(s)) if !h.is_empty() && !p.is_empty() && !s.is_empty() => (h, p, s),
|
||||
_ => return None,
|
||||
};
|
||||
let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(payload_b64)
|
||||
.ok()?;
|
||||
let v: serde_json::Value = serde_json::from_slice(&payload_bytes).ok()?;
|
||||
v.get("https://api.openai.com/auth")
|
||||
.and_then(|auth| auth.get("chatgpt_account_id"))
|
||||
.and_then(|id| id.as_str())
|
||||
.map(|s| s.to_string())
|
||||
}
|
||||
50
codex-rs/cloud-tasks/src/bin/detailcheck.rs
Normal file
50
codex-rs/cloud-tasks/src/bin/detailcheck.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use codex_backend_client::Client as BackendClient;
|
||||
use codex_core::config::find_codex_home;
|
||||
use codex_core::default_client::get_codex_user_agent;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::AuthMode;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let base_url = std::env::var("CODEX_CLOUD_TASKS_BASE_URL")
|
||||
.unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string());
|
||||
let ua = get_codex_user_agent(Some("codex_cloud_tasks_detailcheck"));
|
||||
let mut client = BackendClient::new(base_url)?.with_user_agent(ua);
|
||||
|
||||
if let Ok(home) = find_codex_home() {
|
||||
let am = AuthManager::new(
|
||||
home,
|
||||
AuthMode::ChatGPT,
|
||||
"codex_cloud_tasks_detailcheck".to_string(),
|
||||
);
|
||||
if let Some(auth) = am.auth() {
|
||||
if let Ok(tok) = auth.get_token().await {
|
||||
client = client.with_bearer_token(tok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let list = client.list_tasks(Some(5), Some("current"), None).await?;
|
||||
println!("items: {}", list.items.len());
|
||||
for item in list.items.iter().take(5) {
|
||||
println!("item: {} {}", item.id, item.title);
|
||||
let (details, body, ct) = client.get_task_details_with_body(&item.id).await?;
|
||||
let diff = codex_backend_client::CodeTaskDetailsResponseExt::unified_diff(&details);
|
||||
match diff {
|
||||
Some(d) => println!(
|
||||
"unified diff len={} sample=\n{}",
|
||||
d.len(),
|
||||
&d.lines().take(10).collect::<Vec<_>>().join("\n")
|
||||
),
|
||||
None => {
|
||||
println!(
|
||||
"no unified diff found; ct={ct}; body sample=\n{}",
|
||||
&body.chars().take(5000).collect::<String>()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
141
codex-rs/cloud-tasks/src/bin/envcheck.rs
Normal file
141
codex-rs/cloud-tasks/src/bin/envcheck.rs
Normal file
@@ -0,0 +1,141 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use base64::Engine;
|
||||
use clap::Parser;
|
||||
use codex_core::config::find_codex_home;
|
||||
use codex_core::default_client::get_codex_user_agent;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::AuthMode;
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
use reqwest::header::HeaderValue;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(version, about = "Resolve Codex environment id (debug helper)")]
|
||||
struct Args {
|
||||
/// Optional override for environment id; if present we just echo it.
|
||||
#[arg(long = "env-id")]
|
||||
environment_id: Option<String>,
|
||||
/// Optional label to select a matching environment (case-insensitive exact match).
|
||||
#[arg(long = "env-label")]
|
||||
environment_label: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
// Base URL (default to ChatGPT backend API) with normalization
|
||||
let mut base_url = std::env::var("CODEX_CLOUD_TASKS_BASE_URL")
|
||||
.unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string());
|
||||
while base_url.ends_with('/') {
|
||||
base_url.pop();
|
||||
}
|
||||
if (base_url.starts_with("https://chatgpt.com")
|
||||
|| base_url.starts_with("https://chat.openai.com"))
|
||||
&& !base_url.contains("/backend-api")
|
||||
{
|
||||
base_url = format!("{}/backend-api", base_url);
|
||||
}
|
||||
println!("base_url: {base_url}");
|
||||
println!(
|
||||
"path_style: {}",
|
||||
if base_url.contains("/backend-api") {
|
||||
"wham"
|
||||
} else {
|
||||
"codex-api"
|
||||
}
|
||||
);
|
||||
|
||||
// Build headers: UA + ChatGPT auth if available
|
||||
let ua = get_codex_user_agent(Some("codex_cloud_tasks_envcheck"));
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::USER_AGENT,
|
||||
HeaderValue::from_str(&ua).unwrap_or(HeaderValue::from_static("codex-cli")),
|
||||
);
|
||||
|
||||
// Locate CODEX_HOME and try to load ChatGPT auth
|
||||
if let Ok(home) = find_codex_home() {
|
||||
println!("codex_home: {}", home.display());
|
||||
let authm = AuthManager::new(
|
||||
home,
|
||||
AuthMode::ChatGPT,
|
||||
"codex_cloud_tasks_envcheck".to_string(),
|
||||
);
|
||||
if let Some(auth) = authm.auth() {
|
||||
match auth.get_token().await {
|
||||
Ok(token) if !token.is_empty() => {
|
||||
println!("auth: ChatGPT token present ({} chars)", token.len());
|
||||
let value = format!("Bearer {}", token);
|
||||
if let Ok(hv) = HeaderValue::from_str(&value) {
|
||||
headers.insert(AUTHORIZATION, hv);
|
||||
}
|
||||
if let Some(account_id) = auth
|
||||
.get_account_id()
|
||||
.or_else(|| extract_chatgpt_account_id(&token))
|
||||
{
|
||||
println!("auth: ChatGPT-Account-Id: {account_id}");
|
||||
if let Ok(name) = HeaderName::from_bytes(b"ChatGPT-Account-Id") {
|
||||
if let Ok(hv) = HeaderValue::from_str(&account_id) {
|
||||
headers.insert(name, hv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(_) => println!("auth: ChatGPT token empty"),
|
||||
Err(e) => println!("auth: failed to load ChatGPT token: {e}"),
|
||||
}
|
||||
} else {
|
||||
println!("auth: no ChatGPT auth.json");
|
||||
}
|
||||
} else {
|
||||
println!("codex_home: <not found>");
|
||||
}
|
||||
|
||||
// If user supplied an environment id, just echo it and exit.
|
||||
if let Some(id) = args.environment_id {
|
||||
println!("env: provided env-id={id}");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Auto-detect environment id using shared env_detect
|
||||
match codex_cloud_tasks::env_detect::autodetect_environment_id(
|
||||
&base_url,
|
||||
&headers,
|
||||
args.environment_label,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(sel) => {
|
||||
println!(
|
||||
"env: selected environment_id={} label={}",
|
||||
sel.id,
|
||||
sel.label.unwrap_or_else(|| "<none>".to_string())
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
println!("env: failed: {e}");
|
||||
std::process::exit(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_chatgpt_account_id(token: &str) -> Option<String> {
|
||||
// JWT: header.payload.signature
|
||||
let mut parts = token.split('.');
|
||||
let (_h, payload_b64, _s) = match (parts.next(), parts.next(), parts.next()) {
|
||||
(Some(h), Some(p), Some(s)) if !h.is_empty() && !p.is_empty() && !s.is_empty() => (h, p, s),
|
||||
_ => return None,
|
||||
};
|
||||
let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(payload_b64)
|
||||
.ok()?;
|
||||
let v: serde_json::Value = serde_json::from_slice(&payload_bytes).ok()?;
|
||||
v.get("https://api.openai.com/auth")
|
||||
.and_then(|auth| auth.get("chatgpt_account_id"))
|
||||
.and_then(|id| id.as_str())
|
||||
.map(|s| s.to_string())
|
||||
}
|
||||
211
codex-rs/cloud-tasks/src/bin/newtask.rs
Normal file
211
codex-rs/cloud-tasks/src/bin/newtask.rs
Normal file
@@ -0,0 +1,211 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use base64::Engine;
|
||||
use clap::Parser;
|
||||
use codex_core::config::find_codex_home;
|
||||
use codex_core::default_client::get_codex_user_agent;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::AuthMode;
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
use reqwest::header::CONTENT_TYPE;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
use reqwest::header::HeaderValue;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(version, about = "Create a new Codex cloud task (debug helper)")]
|
||||
struct Args {
|
||||
/// Optional override for environment id; if absent we auto-detect.
|
||||
#[arg(long = "env-id")]
|
||||
environment_id: Option<String>,
|
||||
/// Optional label match for environment selection (case-insensitive, exact match).
|
||||
#[arg(long = "env-label")]
|
||||
environment_label: Option<String>,
|
||||
/// Branch or ref to use (e.g., main)
|
||||
#[arg(long = "ref", default_value = "main")]
|
||||
git_ref: String,
|
||||
/// Run environment in QA (ask) mode
|
||||
#[arg(long = "qa-mode", default_value_t = false)]
|
||||
qa_mode: bool,
|
||||
/// Task prompt text
|
||||
#[arg(required = true)]
|
||||
prompt: Vec<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
let prompt = args.prompt.join(" ");
|
||||
|
||||
// Base URL (default to ChatGPT backend API)
|
||||
let mut base_url = std::env::var("CODEX_CLOUD_TASKS_BASE_URL")
|
||||
.unwrap_or_else(|_| "https://chatgpt.com/backend-api".to_string());
|
||||
while base_url.ends_with('/') {
|
||||
base_url.pop();
|
||||
}
|
||||
if (base_url.starts_with("https://chatgpt.com")
|
||||
|| base_url.starts_with("https://chat.openai.com"))
|
||||
&& !base_url.contains("/backend-api")
|
||||
{
|
||||
base_url = format!("{}/backend-api", base_url);
|
||||
}
|
||||
println!("base_url: {base_url}");
|
||||
let is_wham = base_url.contains("/backend-api");
|
||||
println!("path_style: {}", if is_wham { "wham" } else { "codex-api" });
|
||||
|
||||
// Build headers: UA + ChatGPT auth if available
|
||||
let ua = get_codex_user_agent(Some("codex_cloud_tasks_newtask"));
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::USER_AGENT,
|
||||
HeaderValue::from_str(&ua).unwrap_or(HeaderValue::from_static("codex-cli")),
|
||||
);
|
||||
let mut have_auth = false;
|
||||
// Locate CODEX_HOME and try to load ChatGPT auth
|
||||
if let Ok(home) = find_codex_home() {
|
||||
let authm = AuthManager::new(
|
||||
home,
|
||||
AuthMode::ChatGPT,
|
||||
"codex_cloud_tasks_newtask".to_string(),
|
||||
);
|
||||
if let Some(auth) = authm.auth() {
|
||||
match auth.get_token().await {
|
||||
Ok(token) if !token.is_empty() => {
|
||||
have_auth = true;
|
||||
println!("auth: ChatGPT token present ({} chars)", token.len());
|
||||
let value = format!("Bearer {}", token);
|
||||
if let Ok(hv) = HeaderValue::from_str(&value) {
|
||||
headers.insert(AUTHORIZATION, hv);
|
||||
}
|
||||
if let Some(account_id) = auth
|
||||
.get_account_id()
|
||||
.or_else(|| extract_chatgpt_account_id(&token))
|
||||
{
|
||||
println!("auth: ChatGPT-Account-Id: {account_id}");
|
||||
if let Ok(name) = HeaderName::from_bytes(b"ChatGPT-Account-Id") {
|
||||
if let Ok(hv) = HeaderValue::from_str(&account_id) {
|
||||
headers.insert(name, hv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(_) => println!("auth: ChatGPT token empty"),
|
||||
Err(e) => println!("auth: failed to load ChatGPT token: {e}"),
|
||||
}
|
||||
} else {
|
||||
println!("auth: no ChatGPT auth.json");
|
||||
}
|
||||
}
|
||||
if !have_auth {
|
||||
println!("note: Online endpoints typically require ChatGPT sign-in. Run: `codex login`");
|
||||
}
|
||||
|
||||
// Determine environment id: prefer flag, then by-repo lookup, then full list.
|
||||
let env_id = if let Some(id) = args.environment_id.clone() {
|
||||
println!("env: using provided env-id={id}");
|
||||
id
|
||||
} else {
|
||||
match codex_cloud_tasks::env_detect::autodetect_environment_id(
|
||||
&base_url,
|
||||
&headers,
|
||||
args.environment_label.clone(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(sel) => sel.id,
|
||||
Err(e) => {
|
||||
println!("env: failed to auto-detect environment: {e}");
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
};
|
||||
println!("env: selected environment_id={env_id}");
|
||||
|
||||
// Build request payload patterned after VSCode: POST /wham/tasks
|
||||
let url = if is_wham {
|
||||
format!("{}/wham/tasks", base_url)
|
||||
} else {
|
||||
format!("{}/api/codex/tasks", base_url)
|
||||
};
|
||||
println!(
|
||||
"request: POST {}",
|
||||
url.strip_prefix(&base_url).unwrap_or(&url)
|
||||
);
|
||||
|
||||
// input_items
|
||||
let mut input_items: Vec<serde_json::Value> = Vec::new();
|
||||
input_items.push(serde_json::json!({
|
||||
"type": "message",
|
||||
"role": "user",
|
||||
"content": [{ "content_type": "text", "text": prompt }]
|
||||
}));
|
||||
|
||||
// Optional: starting diff via env var for quick testing
|
||||
if let Ok(diff) = std::env::var("CODEX_STARTING_DIFF") {
|
||||
if !diff.is_empty() {
|
||||
input_items.push(serde_json::json!({
|
||||
"type": "pre_apply_patch",
|
||||
"output_diff": { "diff": diff }
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
let request_body = serde_json::json!({
|
||||
"new_task": {
|
||||
"environment_id": env_id,
|
||||
"branch": args.git_ref,
|
||||
"run_environment_in_qa_mode": args.qa_mode,
|
||||
},
|
||||
"input_items": input_items,
|
||||
});
|
||||
|
||||
let http = reqwest::Client::builder().build()?;
|
||||
let res = http
|
||||
.post(&url)
|
||||
.headers(headers)
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let status = res.status();
|
||||
let ct = res
|
||||
.headers()
|
||||
.get(CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let body = res.text().await.unwrap_or_default();
|
||||
println!("status: {}", status);
|
||||
println!("content-type: {}", ct);
|
||||
match serde_json::from_str::<serde_json::Value>(&body) {
|
||||
Ok(v) => println!(
|
||||
"response (pretty JSON):\n{}",
|
||||
serde_json::to_string_pretty(&v).unwrap_or(body)
|
||||
),
|
||||
Err(_) => println!("response (raw):\n{}", body),
|
||||
}
|
||||
|
||||
if !status.is_success() {
|
||||
// Exit non-zero on failure
|
||||
std::process::exit(1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_chatgpt_account_id(token: &str) -> Option<String> {
|
||||
// JWT: header.payload.signature
|
||||
let mut parts = token.split('.');
|
||||
let (_h, payload_b64, _s) = match (parts.next(), parts.next(), parts.next()) {
|
||||
(Some(h), Some(p), Some(s)) if !h.is_empty() && !p.is_empty() && !s.is_empty() => (h, p, s),
|
||||
_ => return None,
|
||||
};
|
||||
let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(payload_b64)
|
||||
.ok()?;
|
||||
let v: serde_json::Value = serde_json::from_slice(&payload_bytes).ok()?;
|
||||
v.get("https://api.openai.com/auth")
|
||||
.and_then(|auth| auth.get("chatgpt_account_id"))
|
||||
.and_then(|id| id.as_str())
|
||||
.map(|s| s.to_string())
|
||||
}
|
||||
Reference in New Issue
Block a user