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
15 changes: 5 additions & 10 deletions include/agentty/provider/chatgpt/oauth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,11 @@ struct OAuthConfig {
// Codex CLI (drives the simplified consent screen + org-in-id_token).
static constexpr const char* originator = "codex_cli_rs";

// The Codex CLI version agentty impersonates on Codex backend requests.
// CRITICAL: the /backend-api/codex/models catalog endpoint gates the
// returned model list on `client_version` — an old/unknown version gets a
// 200 with an EMPTY `{"models":[]}` (the server withholds the catalog from
// stale clients). agentty's own version (0.2.x) is far below any codex
// minimum, so we MUST send a current codex version here (both the
// client_version query param and the codex_cli_rs/<ver> user-agent) or the
// picker falls back to a bundled guess and the first turn 400s on a slug
// the account no longer offers. Bump this when the catalog empties again.
static constexpr const char* codex_client_version = "0.145.0";
// The catalog is gated by client_version and older versions can silently
// return only a subset. Keep this aligned with a recent Codex release.
// AGENTTY_CODEX_CLIENT_VERSION may override this for catalog requests with
// a numeric major.minor.patch version; invalid values use this fallback.
static constexpr const char* codex_client_version = "0.167.0";

// RFC-8693 token-exchange used to mint a usable OpenAI API key from the
// signed id_token — this is what lets agentty call the API directly
Expand Down
3 changes: 3 additions & 0 deletions include/agentty/provider/chatgpt/responses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct CatalogModel {
std::string display_name; // human label for the picker
int context_window = 272000;
bool is_default = false;
int priority = 0;
};

// GET https://chatgpt.com/backend-api/codex/models?client_version=… with the
Expand All @@ -49,6 +50,8 @@ provider::StreamResult stream_responses(const maya::unlocked& cap, provider::Req
provider::EventSink sink);

// ── Test seams (pure, no network) ──────────────────────────────────────────
[[nodiscard]] std::vector<CatalogModel> parse_models_payload_for_test(std::string_view body);
[[nodiscard]] std::string codex_client_version_for_test(std::string_view override_version);
// The Request → Responses-API JSON body the transport would POST.
[[nodiscard]] nlohmann::json build_body_for_test(const provider::Request& req);
// Stable per-conversation session id derived from a session_key (the value
Expand Down
103 changes: 77 additions & 26 deletions src/provider/chatgpt/responses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
#include "agentty/provider/responses/responses.hpp"

#include <maya/runtime.hpp>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <filesystem>
#include <mutex>
#include <random>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>

#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -302,61 +305,109 @@ bool responses_available() {
return ok;
}

std::string codex_client_version_for_test(std::string_view override_version) {
// Accept only dotted numeric versions, bounded for use in URL and headers.
if (override_version.empty() || override_version.size() > 32)
return OAuthConfig::codex_client_version;
int dots = 0;
bool digit = false;
for (char c : override_version) {
if (c >= '0' && c <= '9') digit = true;
else if (c == '.' && digit) { ++dots; digit = false; }
else return OAuthConfig::codex_client_version;
}
return dots == 2 && digit ? std::string(override_version) : OAuthConfig::codex_client_version;
}

static std::string codex_client_version() {
const char* value = std::getenv("AGENTTY_CODEX_CLIENT_VERSION");
return codex_client_version_for_test(value ? std::string_view(value) : std::string_view{});
}

// ── Live model catalog ────────────────────────────────────────────────────
std::vector<CatalogModel> fetch_models(Io io, const maya::unlocked& cap) {
auto creds = codex_fresh_credentials(cap);
if (!creds || creds->access_token.empty()) return {};

const auto client_version = codex_client_version();
http::Request hr;
hr.method = http::HttpMethod::Get;
hr.host = "chatgpt.com";
hr.port = 443;
hr.path = std::string("/backend-api/codex/models?client_version=")
+ OAuthConfig::codex_client_version;
+ client_version;
hr.headers = {
{"authorization", "Bearer " + creds->access_token},
{"accept", "application/json"},
{"openai-beta", "responses=experimental"},
{"originator", OAuthConfig::originator},
{"user-agent", std::string("codex_cli_rs/") + OAuthConfig::codex_client_version},
{"user-agent", std::string("codex_cli_rs/") + client_version},
};
if (!creds->account_id.empty())
hr.headers.push_back({"chatgpt-account-id", creds->account_id});

http::Timeouts tos;
tos.connect = std::chrono::milliseconds(8'000);
tos.total = std::chrono::milliseconds(15'000);
// The catalog is a small JSON list (~a few KB). Cap hard so a misbehaving
// proxy / replay loop can't stream us into OOM on a routine picker probe
// (parity with Anthropic's list_models).
hr.max_body_bytes = 1ull * 1024 * 1024;
// Catalogs can be large; bound the response to limit memory consumption.
hr.max_body_bytes = 2ull * 1024 * 1024;

auto result = http::default_client(io).send(hr, tos);
if (!result || result->status < 200 || result->status >= 300) return {};

return parse_models_payload_for_test(result->body);
}

std::vector<CatalogModel> parse_models_payload_for_test(std::string_view body) {
std::vector<CatalogModel> out;
std::unordered_set<std::string> seen;
json j;
try {
auto j = nlohmann::json::parse(result->body);
const auto& arr = j.contains("models") ? j["models"] : j;
if (!arr.is_array()) return {};
for (const auto& m : arr) {
// The catalog carries internal models the picker must not show
// (e.g. `codex-auto-review`, visibility="hide"). Mirror codex-rs:
// only surface models the account is meant to select. Anything not
// explicitly listed as visible is dropped.
const std::string vis = m.value("visibility", std::string{"list"});
if (vis == "hide" || vis == "hidden") continue;
CatalogModel cm;
cm.slug = m.value("slug", m.value("id", std::string{}));
if (cm.slug.empty()) continue;
cm.display_name = m.value("display_name", cm.slug);
cm.context_window = m.value("context_window", 272000);
cm.is_default = m.value("is_default", false);
out.push_back(std::move(cm));
}
} catch (...) {
return {};
j = json::parse(body.begin(), body.end(), nullptr, false);
} catch (const json::exception&) {
return out;
}
if (j.is_discarded()) return out;
const auto& arr = j.is_object() && j.contains("models") ? j["models"] : j;
if (!arr.is_array()) return out;
for (const auto& m : arr) {
if (!m.is_object()) continue;
auto str = [&](const char* key, std::string fallback) {
auto it = m.find(key);
return it != m.end() && it->is_string() ? it->get<std::string>() : fallback;
};
auto boolean = [&](const char* key, bool fallback) {
auto it = m.find(key);
return it != m.end() && it->is_boolean() ? it->get<bool>() : fallback;
};
auto integer = [&](const char* key, int fallback, int lo, int hi) {
auto it = m.find(key);
if (it == m.end() || !it->is_number_integer()) return fallback;
if (it->is_number_unsigned()) {
const auto n = it->get<std::uint64_t>();
return n <= static_cast<std::uint64_t>(hi) &&
(lo <= 0 || n >= static_cast<std::uint64_t>(lo))
? static_cast<int>(n) : fallback;
}
const auto n = it->get<std::int64_t>();
return n >= lo && n <= hi ? static_cast<int>(n) : fallback;
};
const auto vis = str("visibility", "list");
if (vis == "hide" || vis == "hidden") continue;
if (!boolean("supported_in_api", true)) continue;
CatalogModel cm;
cm.slug = str("slug", str("id", {}));
if (cm.slug.empty() || !seen.insert(cm.slug).second) continue;
cm.display_name = str("display_name", cm.slug);
cm.context_window = integer("context_window", cm.context_window, 1, 20000000);
cm.is_default = boolean("is_default", false);
// Invalid or absent priorities sort after every valid priority.
cm.priority = integer("priority", 1000001, -1000000, 1000000);
out.push_back(std::move(cm));
}
std::stable_sort(out.begin(), out.end(), [](const CatalogModel& a, const CatalogModel& b) {
return a.priority < b.priority;
});
return out;
}

Expand Down
58 changes: 58 additions & 0 deletions tests/chatgpt_bundled_models_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,69 @@
#include "agtest.hpp"

#include "agentty/provider/chatgpt/provider.hpp"
#include "agentty/provider/chatgpt/oauth.hpp"
#include "agentty/provider/chatgpt/responses.hpp"

using namespace agentty;
namespace cc = agentty::provider::chatgpt;

TEST_CASE("chatgpt catalog parsing filters and orders models") {
const auto models = cc::parse_models_payload_for_test(R"({"models":[
{"slug":"later","priority":20,"context_window":12345},
{"slug":"hidden","visibility":"hide"},
{"slug":"hidden2","visibility":"hidden"},
{"slug":"not-api","supported_in_api":false,"priority":4},
{"slug":"other","visibility":"internal","priority":5},
{"slug":"first","priority":1,"is_default":true},
{"slug":"first","priority":2},
{"id":"fallback","priority":3,"context_window":-1},
null, {"slug":42}
]})");
REQUIRE(models.size() == 4);
CHECK(models[0].slug == "first");
CHECK(models[0].is_default);
CHECK(models[1].slug == "fallback");
CHECK(models[1].context_window == 272000);
CHECK(models[2].slug == "other");
CHECK(models[3].slug == "later");
CHECK(models[3].context_window == 12345);
CHECK(cc::parse_models_payload_for_test("not json").empty());
CHECK(cc::parse_models_payload_for_test(R"({"models":{}})").empty());
CHECK(cc::parse_models_payload_for_test(R"({"models":[{"slug":"bad","priority":1e1000}]})").empty());
}

TEST_CASE("chatgpt catalog prioritizes valid priorities before missing or invalid ones") {
const auto models = cc::parse_models_payload_for_test(R"({"models":[
{"slug":"missing"},
{"slug":"unsigned-overflow","priority":18446744073709551615,"context_window":18446744073709551615},
{"slug":"invalid","priority":"1"},
{"slug":"out-of-range","priority":1000001},
{"slug":"last-valid","priority":1000000},
{"slug":"first","priority":1},
{"slug":"equal","priority":1}
]})");
REQUIRE(models.size() == 7);
CHECK(models[0].slug == "first");
CHECK(models[1].slug == "equal");
CHECK(models[2].slug == "last-valid");
CHECK(models[3].slug == "missing");
CHECK(models[4].slug == "unsigned-overflow");
CHECK(models[4].context_window == 272000);
CHECK(models[5].slug == "invalid");
CHECK(models[6].slug == "out-of-range");
}

TEST_CASE("chatgpt codex version override validation") {
const std::string fallback = cc::OAuthConfig::codex_client_version;
CHECK(cc::codex_client_version_for_test("") == fallback);
CHECK(cc::codex_client_version_for_test("1.2.3") == "1.2.3");
CHECK(cc::codex_client_version_for_test("1.2") == fallback);
CHECK(cc::codex_client_version_for_test("1..2.3") == fallback);
CHECK(cc::codex_client_version_for_test("1.2.3\r\nX:bad") == fallback);
CHECK(cc::codex_client_version_for_test("1.2.3&foo=bar") == fallback);
CHECK(cc::codex_client_version_for_test(std::string(33, '1')) == fallback);
}

TEST_CASE("chatgpt bundled model catalog") {
const bool signed_in = cc::responses_available();

Expand Down
Loading