Agent Zero AI framework
Fix Codex OAuth request metadata
Alessandro6dfe33c4932d
diff --git a/plugins/_oauth/AGENTS.md b/plugins/_oauth/AGENTS.mdindex efd6f98fa..7bf1f3348 100644--- a/plugins/_oauth/AGENTS.md+++ b/plugins/_oauth/AGENTS.md@@ -30,6 +30,7 @@- OAuth settings model slots must keep provider choice editable per slot, list only connected OAuth account providers, and persist the selected provider IDs into `chat_model.provider` and `utility_model.provider`.- OAuth settings must dispatch `model-setup-changed` when a provider connection completes, but model selection remains explicit and must not be filled automatically.- `helpers/providers/registry.py` is the source of truth for connectable OAuth providers.+- The models API must preserve the legacy plain `models` slug list and may add `model_metadata` entries for richer provider catalogs.- OAuth provider config must not expose the dummy `oauth` API key in `conf/model_providers.yaml`; the dummy key is a runtime-only shim supplied by the `get_api_key` extension after the account provider reports connected.- Usage-plan metadata belongs only to connectable providers. Do not add metadata-only subscription families for providers this plugin cannot connect.- API handlers should remain provider-aware. Missing or blank `provider_id` defaults to Codex only for existing backward compatibility; falsey non-string IDs must not silently default.@@ -39,6 +40,7 @@- Stored upstream base URLs and OAuth token endpoints must be validated against provider-owned allowlists before sending bearer or refresh tokens.- Browser callback providers must support manual callback paste when the browser cannot reach the local callback route.- Local proxy routes must remain loopback or token protected and must not add broad CORS access.+- Codex Responses proxy requests must include Codex client metadata and compatibility headers such as `client_metadata`, `x-codex-installation-id`, `originator`, `session-id`, and `thread-id`.## Work Guidancediff --git a/plugins/_oauth/api/models.py b/plugins/_oauth/api/models.pyindex ffc63afe3..493017ade 100644--- a/plugins/_oauth/api/models.py+++ b/plugins/_oauth/api/models.py@@ -10,8 +10,23 @@ class Models(ApiHandler):raw_provider_id = _provider_id(input)try:provider = get_provider(raw_provider_id)- models = provider.models()- return {"ok": True, "provider_id": provider.provider_id, "models": models}+ model_catalog = getattr(provider, "model_catalog", None)+ if callable(model_catalog):+ catalog = model_catalog()+ models = [+ str(item.get("slug") or item.get("id") or "")+ for item in catalog+ if isinstance(item, dict) and (item.get("slug") or item.get("id"))+ ]+ else:+ catalog = []+ models = provider.models()+ return {+ "ok": True,+ "provider_id": provider.provider_id,+ "models": models,+ "model_metadata": catalog,+ }except Exception as exc:return {"ok": False,diff --git a/plugins/_oauth/helpers/codex.py b/plugins/_oauth/helpers/codex.pyindex 824e19a92..a0fc20c5b 100644--- a/plugins/_oauth/helpers/codex.py+++ b/plugins/_oauth/helpers/codex.py@@ -6,9 +6,11 @@ import hashlibimport jsonimport osimport secrets+import statimport subprocessimport threadingimport time+import uuidfrom contextlib import contextmanagerfrom dataclasses import dataclassfrom datetime import datetime, timedelta, timezone@@ -33,9 +35,28 @@ except ImportError:AUTH_FILENAME = "auth.json"+INSTALLATION_ID_FILENAME = "installation_id"ACCESS_EXPIRY_MARGIN = timedelta(minutes=5)REFRESH_INTERVAL = timedelta(minutes=55)DEFAULT_CODEX_MODEL = "gpt-5.5"+CODEX_ORIGINATOR = "codex_cli_rs"+CLIENT_METADATA_INSTALLATION_ID = "x-codex-installation-id"+CLIENT_METADATA_WINDOW_ID = "x-codex-window-id"+CLIENT_METADATA_KEYS = (+ "slug",+ "id",+ "display_name",+ "description",+ "visibility",+ "supported_in_api",+ "default_reasoning_level",+ "supported_reasoning_levels",+ "additional_speed_tiers",+ "service_tiers",+ "context_window",+ "max_context_window",+ "priority",+)OAUTH_ERROR_KEYS = ("error_description", "error")DEVICE_CODE_TIMEOUT_SECONDS = 15 * 60WINDOWS_LOCK_RETRY_SECONDS = 0.05@@ -565,11 +586,17 @@ def request_codex(auth = load_auth()target = build_upstream_url(path, cfg["upstream_base_url"])request_headers = sanitize_forward_headers(headers or {})+ metadata = client_metadata_from_body(body) or build_client_metadata()request_headers.update({"Authorization": f"Bearer {auth.access_token}","chatgpt-account-id": auth.account_id,"OpenAI-Beta": "responses=experimental",+ "originator": CODEX_ORIGINATOR,+ CLIENT_METADATA_INSTALLATION_ID: metadata[CLIENT_METADATA_INSTALLATION_ID],+ CLIENT_METADATA_WINDOW_ID: metadata[CLIENT_METADATA_WINDOW_ID],+ "session-id": metadata["session_id"],+ "thread-id": metadata["thread_id"],})@@ -584,11 +611,14 @@ def request_codex()-def fetch_models() -> list[str]:+def fetch_model_catalog() -> list[dict[str, Any]]:cfg = codex_config()configured = cfg["models"]if configured:- return configured+ return [+ {"slug": model, "id": model, "display_name": model}+ for model in configured+ ]client_version = resolve_codex_version()params = {"client_version": client_version} if client_version else None@@ -601,31 +631,103 @@ def fetch_models() -> list[str]:payload = response.json()raw_models = payload.get("models") if isinstance(payload, dict) else None+ if raw_models is None and isinstance(payload, dict):+ raw_models = payload.get("data")if not isinstance(raw_models, list):raise RuntimeError("Codex returned a malformed models response.")- models: list[str] = []+ catalog: list[dict[str, Any]] = []seen: set[str] = set()for item in raw_models:- slug = item.get("slug") if isinstance(item, dict) else None- if isinstance(slug, str) and slug and slug not in seen:+ if isinstance(item, dict):+ slug = _string(item.get("slug") or item.get("id"))+ model = {+ key: item[key]+ for key in CLIENT_METADATA_KEYS+ if key in item+ }+ else:+ slug = _string(item)+ model = {}+ if slug and slug not in seen:seen.add(slug)- models.append(slug)- if not models:+ model["slug"] = slug+ model.setdefault("id", slug)+ model.setdefault("display_name", slug)+ catalog.append(model)+ if not catalog:raise RuntimeError("Codex returned an empty models list.")- return models+ return catalog+++def fetch_models() -> list[str]:+ return [model["slug"] for model in fetch_model_catalog()]def prepare_responses_body(body: dict[str, Any], *, force_stream: bool) -> dict[str, Any]:normalized = dict(body)normalized.setdefault("instructions", "")normalized.setdefault("store", False)+ normalized["client_metadata"] = merge_client_metadata(normalized.get("client_metadata"))if force_stream:normalized["stream"] = True+ if isinstance(normalized.get("reasoning"), dict):+ include = normalized.get("include")+ values = list(include) if isinstance(include, list) else []+ if "reasoning.encrypted_content" not in values:+ values.append("reasoning.encrypted_content")+ normalized["include"] = valuesnormalized.pop("max_output_tokens", None)return normalized+def build_client_metadata() -> dict[str, str]:+ request_id = f"agent-zero-{uuid.uuid4()}"+ return {+ CLIENT_METADATA_INSTALLATION_ID: resolve_installation_id(),+ "session_id": request_id,+ "thread_id": request_id,+ CLIENT_METADATA_WINDOW_ID: "agent-zero",+ }+++def merge_client_metadata(value: Any) -> dict[str, str]:+ metadata = {+ str(key): str(item)+ for key, item in (value.items() if isinstance(value, dict) else [])+ if item is not None and str(item)+ }+ metadata.update(build_client_metadata())+ return metadata+++def client_metadata_from_body(body: bytes | str | None) -> dict[str, str] | None:+ if body is None:+ return None+ try:+ text = body.decode("utf-8") if isinstance(body, bytes) else body+ payload = json.loads(text)+ except Exception:+ return None+ if not isinstance(payload, dict):+ return None+ metadata = payload.get("client_metadata")+ if not isinstance(metadata, dict):+ return None+ result = {+ str(key): str(value)+ for key, value in metadata.items()+ if value is not None and str(value)+ }+ required = {+ CLIENT_METADATA_INSTALLATION_ID,+ "session_id",+ "thread_id",+ CLIENT_METADATA_WINDOW_ID,+ }+ return result if required.issubset(result) else None++def collect_completed_response(response: requests.Response) -> dict[str, Any]:latest_response: dict[str, Any] | None = Nonelatest_error: Any = None@@ -728,9 +830,7 @@ def extract_sse_text_deltas(payload: dict[str, Any], event_type: str = "") -> liif (payload.get("type") or event_type) in {"response.output_text.delta",- "response.output_text.done","response.text.delta",- "response.text.done",}:_append_text_value(pieces, payload.get("text"))@@ -985,6 +1085,39 @@ def resolve_codex_version() -> str:return ""+def resolve_installation_id() -> str:+ for path in installation_id_candidates():+ try:+ value = path.read_text(encoding="utf-8").strip()+ except OSError:+ continue+ if value:+ return value++ value = str(uuid.uuid4())+ path = plugin_installation_id_path()+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(value, encoding="utf-8")+ os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)+ except OSError:+ pass+ return value+++def installation_id_candidates() -> list[Path]:+ candidates = [plugin_installation_id_path()]+ codex_home = os.environ.get("CODEX_HOME")+ if codex_home:+ candidates.append(Path(codex_home).expanduser() / INSTALLATION_ID_FILENAME)+ candidates.append(Path.home() / ".codex" / INSTALLATION_ID_FILENAME)+ return candidates+++def plugin_installation_id_path() -> Path:+ return Path(files.get_abs_path("usr", "plugins", "_oauth", "codex", INSTALLATION_ID_FILENAME))++def resolve_auth_file_candidates() -> list[Path]:return [resolve_auth_write_path()]diff --git a/plugins/_oauth/helpers/providers/codex.py b/plugins/_oauth/helpers/providers/codex.pyindex d619927da..0a59a79a0 100644--- a/plugins/_oauth/helpers/providers/codex.py+++ b/plugins/_oauth/helpers/providers/codex.py@@ -232,6 +232,11 @@ class CodexOAuthProvider:return codex.fetch_models()+ def model_catalog(self) -> list[dict[str, Any]]:+ from plugins._oauth.helpers import codex++ return codex.fetch_model_catalog()+def disconnect(self) -> dict[str, Any]:from plugins._oauth.helpers import codexdiff --git a/plugins/_oauth/helpers/summary.py b/plugins/_oauth/helpers/summary.pyindex 107228811..2585859e1 100644--- a/plugins/_oauth/helpers/summary.py+++ b/plugins/_oauth/helpers/summary.py@@ -63,13 +63,25 @@ def _provider_status(provider: Any) -> dict[str, Any]:status["usage_windows"] = usage_windowsreturn statusexcept Exception as exc:+ metadata = _provider_metadata(provider)return {+ **metadata,"provider_id": str(getattr(provider, "provider_id", "")),"connected": False,"error": str(exc),}+def _provider_metadata(provider: Any) -> dict[str, Any]:+ try:+ metadata = provider.metadata()+ to_dict = getattr(metadata, "to_dict", None)+ value = to_dict() if callable(to_dict) else metadata+ return value if isinstance(value, dict) else {}+ except Exception:+ return {}++def _account_summary(provider: dict[str, Any], catalog: dict[str, Any]) -> dict[str, Any]:provider_id = str(provider.get("provider_id") or "")plan_entry = catalog.get(provider_id) if isinstance(catalog, dict) else Nonediff --git a/plugins/_oauth/webui/config.html b/plugins/_oauth/webui/config.htmlindex 72177d99d..2e04cd876 100644--- a/plugins/_oauth/webui/config.html+++ b/plugins/_oauth/webui/config.html@@ -259,8 +259,16 @@type="button"class="oauth-model-item"@click="$store.oauthConfig.selectModel(slot.key, model)"- x-text="model"- ></button>+ >+ <span+ class="oauth-model-name"+ x-text="model"+ ></span>+ <small+ x-show="$store.oauthConfig.modelDescription(slot.provider, model)"+ x-text="$store.oauthConfig.modelDescription(slot.provider, model)"+ ></small>+ </button></template><div class="oauth-model-item muted" x-show="$store.oauthConfig.filteredModels(slot.key).length === 0">No models found. You can still type the model name manually.@@ -282,7 +290,10 @@</div><div class="oauth-models"><template x-for="model in $store.oauthConfig.activeProviderModels()" :key="model">- <span x-text="model"></span>+ <span+ :title="$store.oauthConfig.modelDescription($store.oauthConfig.activeModelProvider, model)"+ x-text="model"+ ></span></template></div></section>@@ -991,7 +1002,9 @@}.oauth-model-item {- display: block;+ display: flex;+ flex-direction: column;+ gap: 3px;width: 100%;min-height: 34px;padding: 8px 10px;@@ -1004,6 +1017,19 @@cursor: pointer;}+ .oauth-model-name {+ font-weight: 650;+ }++ .oauth-model-item small {+ overflow: hidden;+ color: var(--color-text-secondary);+ font-size: 0.72rem;+ line-height: 1.3;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+.oauth-model-item:hover,.oauth-model-item:focus-visible {background: color-mix(in srgb, var(--color-border) 42%, transparent);diff --git a/plugins/_oauth/webui/oauth-config-store.js b/plugins/_oauth/webui/oauth-config-store.jsindex c77701631..004093662 100644--- a/plugins/_oauth/webui/oauth-config-store.js+++ b/plugins/_oauth/webui/oauth-config-store.js@@ -107,6 +107,7 @@ export const store = createStore("oauthConfig", {disconnecting: false,loadingModels: false,providerModels: {},+ providerModelMetadata: {},providerUi: providerUiDefaults(),connectingProvider: "",disconnectingProvider: "",@@ -150,6 +151,7 @@ export const store = createStore("oauthConfig", {this.disconnecting = false;this.loadingModels = false;this.providerModels = {};+ this.providerModelMetadata = {};this.providerUi = providerUiDefaults();this.connectingProvider = "";this.disconnectingProvider = "";@@ -556,6 +558,14 @@ export const store = createStore("oauthConfig", {return this.providerModels[this.activeModelProvider] || [];},+ modelMetadata(providerId, model) {+ return this.providerModelMetadata[providerId]?.[model] || {};+ },++ modelDescription(providerId, model) {+ return this.modelMetadata(providerId, model).description || "";+ },+activeModelsDescription() {if (!this.providerConnected(this.activeModelProvider)) return "";return `Available models from ${this.providerLabel(this.activeModelProvider)}`;@@ -967,12 +977,20 @@ export const store = createStore("oauthConfig", {throw new Error(response?.error || `Could not load ${this.providerLabel(selectedProvider)} models.`);}const models = Array.isArray(response.models) ? response.models : [];+ const metadata = Array.isArray(response.model_metadata) ? response.model_metadata : [];+ const metadataMap = metadata.reduce((result, item) => {+ const key = String(item?.slug || item?.id || "");+ if (key) result[key] = item;+ return result;+ }, {});this.providerModels = { ...this.providerModels, [selectedProvider]: models };+ this.providerModelMetadata = { ...this.providerModelMetadata, [selectedProvider]: metadataMap };this.models = models;if (openDropdown) this.openModelDropdown(openDropdown);if (!silent) void toastFrontendSuccess(`${this.providerLabel(selectedProvider)} models loaded.`, "OAuth Connections");} catch (error) {this.providerModels = { ...this.providerModels, [selectedProvider]: [] };+ this.providerModelMetadata = { ...this.providerModelMetadata, [selectedProvider]: {} };this.models = [];if (!silent) void toastFrontendError(messageOf(error), "OAuth Connections");} finally {@@ -999,6 +1017,9 @@ export const store = createStore("oauthConfig", {const providerModels = { ...this.providerModels };delete providerModels[providerId];this.providerModels = providerModels;+ const providerModelMetadata = { ...this.providerModelMetadata };+ delete providerModelMetadata[providerId];+ this.providerModelMetadata = providerModelMetadata;if (this.activeModelProvider === providerId) this.models = [];this.clearProviderDevice(providerId);if (this.connectingProvider === providerId) this.connectingProvider = "";diff --git a/tests/test_oauth_codex.py b/tests/test_oauth_codex.pyindex 89c0a6e17..396584c18 100644--- a/tests/test_oauth_codex.py+++ b/tests/test_oauth_codex.py@@ -137,6 +137,145 @@ def test_codex_fetch_models_omits_fake_client_version(monkeypatch):assert calls == [("/models", None)]+def test_codex_fetch_model_catalog_preserves_model_metadata(monkeypatch):+ class FakeResponse:+ ok = True++ def json(self):+ return {+ "models": [+ {+ "slug": "gpt-5.5",+ "display_name": "GPT-5.5",+ "description": "Frontier coding model.",+ "default_reasoning_level": "medium",+ "base_instructions": "too large for the settings UI",+ }+ ]+ }++ monkeypatch.setattr(codex, "codex_config", lambda: {"models": []})+ monkeypatch.setattr(codex, "resolve_codex_version", lambda: "0.142.5")+ monkeypatch.setattr(codex, "request_codex", lambda path, params=None: FakeResponse())++ assert codex.fetch_model_catalog() == [+ {+ "slug": "gpt-5.5",+ "id": "gpt-5.5",+ "display_name": "GPT-5.5",+ "description": "Frontier coding model.",+ "default_reasoning_level": "medium",+ }+ ]+++def test_prepare_responses_body_adds_codex_client_metadata(monkeypatch):+ monkeypatch.setattr(+ codex,+ "build_client_metadata",+ lambda: {+ "x-codex-installation-id": "install-1",+ "session_id": "session-1",+ "thread_id": "thread-1",+ "x-codex-window-id": "agent-zero",+ },+ )++ body = codex.prepare_responses_body(+ {+ "model": "gpt-5.5",+ "input": "hello",+ "client_metadata": {+ "caller": "plugin-test",+ "x-codex-installation-id": "stale",+ },+ "reasoning": {"effort": "medium"},+ "include": ["output_text"],+ },+ force_stream=True,+ )++ assert body["client_metadata"] == {+ "caller": "plugin-test",+ "x-codex-installation-id": "install-1",+ "session_id": "session-1",+ "thread_id": "thread-1",+ "x-codex-window-id": "agent-zero",+ }+ assert body["stream"] is True+ assert body["include"] == ["output_text", "reasoning.encrypted_content"]+++def test_request_codex_sends_current_codex_headers_from_body(monkeypatch):+ calls = []+ body = json.dumps(+ {+ "client_metadata": {+ "x-codex-installation-id": "install-1",+ "session_id": "session-1",+ "thread_id": "thread-1",+ "x-codex-window-id": "agent-zero",+ }+ }+ )++ class FakeResponse:+ ok = True++ monkeypatch.setattr(+ codex,+ "codex_config",+ lambda: {+ "upstream_base_url": "https://chatgpt.example/backend-api/codex",+ "request_timeout_seconds": 120,+ },+ )+ monkeypatch.setattr(+ codex,+ "load_auth",+ lambda: codex.EffectiveAuth(+ access_token="access-token",+ account_id="account-1",+ ),+ )++ def fake_request(method, target, headers, data, params, timeout, stream):+ calls.append(+ {+ "method": method,+ "target": target,+ "headers": headers,+ "data": data,+ "params": params,+ "timeout": timeout,+ "stream": stream,+ }+ )+ return FakeResponse()++ monkeypatch.setattr(codex.requests, "request", fake_request)++ response = codex.request_codex(+ "/responses",+ method="POST",+ headers={"Content-Type": "application/json"},+ body=body,+ stream=True,+ )++ assert response.ok is True+ call = calls[0]+ assert call["target"] == "https://chatgpt.example/backend-api/codex/responses"+ assert call["headers"]["Authorization"] == "Bearer access-token"+ assert call["headers"]["chatgpt-account-id"] == "account-1"+ assert call["headers"]["OpenAI-Beta"] == "responses=experimental"+ assert call["headers"]["originator"] == "codex_cli_rs"+ assert call["headers"]["x-codex-installation-id"] == "install-1"+ assert call["headers"]["session-id"] == "session-1"+ assert call["headers"]["thread-id"] == "thread-1"+ assert call["headers"]["x-codex-window-id"] == "agent-zero"++def test_chat_messages_to_response_body_preserves_image_parts_for_responses():data_url = "data:image/png;base64,abcd"@@ -231,6 +370,16 @@ def test_extract_sse_text_deltas_reads_chat_completion_chunks():assert deltas == ["Hel", "lo"]+def test_extract_sse_text_deltas_ignores_final_done_text():+ assert (+ codex.extract_sse_text_deltas(+ {"type": "response.output_text.done", "text": "Hello"},+ "response.output_text.done",+ )+ == []+ )++def test_collect_completed_response_falls_back_to_text_deltas():class FakeResponse:encoding = "utf-8"diff --git a/tests/test_oauth_providers.py b/tests/test_oauth_providers.pyindex 155c2bcba..febccead0 100644--- a/tests/test_oauth_providers.py+++ b/tests/test_oauth_providers.py@@ -390,6 +390,13 @@ def test_status_api_contains_provider_status_exceptions(monkeypatch):class FailingProvider:provider_id = XAI_GROK_PROVIDER_ID+ def metadata(self):+ return {+ "provider_id": XAI_GROK_PROVIDER_ID,+ "display_name": "xAI Grok",+ "auth_flow": "browser_pkce",+ }+def status(self):raise RuntimeError("status failed")@@ -409,6 +416,8 @@ def test_status_api_contains_provider_status_exceptions(monkeypatch):assert response["provider_map"][CODEX_PROVIDER_ID]["connected"] is Trueassert response["provider_map"][XAI_GROK_PROVIDER_ID] == {"provider_id": XAI_GROK_PROVIDER_ID,+ "display_name": "xAI Grok",+ "auth_flow": "browser_pkce","connected": False,"error": "status failed",}@@ -951,6 +960,38 @@ def test_manual_callback_unknown_provider_returns_structured_error():assert "Unknown OAuth provider" in response["error"]+def test_models_api_returns_optional_model_metadata(monkeypatch):+ class FakeProvider:+ provider_id = CODEX_PROVIDER_ID++ def model_catalog(self):+ return [+ {+ "slug": "gpt-5.5",+ "display_name": "GPT-5.5",+ "description": "Frontier coding model.",+ }+ ]++ def models(self):+ return ["ignored-when-catalog-exists"]++ models_module = sys.modules["plugins._oauth.api.models"]+ monkeypatch.setattr(models_module, "get_provider", lambda provider_id: FakeProvider())++ response = asyncio.run(Models(None, None).process({"provider_id": CODEX_PROVIDER_ID}, FakeRequest()))++ assert response["ok"] is True+ assert response["models"] == ["gpt-5.5"]+ assert response["model_metadata"] == [+ {+ "slug": "gpt-5.5",+ "display_name": "GPT-5.5",+ "description": "Frontier coding model.",+ }+ ]++def test_unknown_provider_id_on_provider_aware_api_returns_structured_error():response = asyncio.run(Models(None, None).process({"provider_id": "missing"}, FakeRequest()))