@raybase/openai-subscription
v0.1.60
Published
Experimental first-party Raybase module for ChatGPT/Codex subscription-backed Agent inference.
Downloads
10,536
Readme
@raybase/openai-subscription
Experimental, explicitly opt-in Raybase module for ChatGPT/Codex subscription-backed Raybase Agent inference.
This is compatibility code, not the public OpenAI API and not API-key billing.
The Worker-native transport is pinned to the reviewed upstream Codex revision
recorded in compatibility/manifest.json. Unknown protocol shapes fail closed
as protocol_update_required.
Codex 400 responses are classified only from bounded machine fields. A
parseable error.code, or error.type when the code is absent or unusable,
becomes a lowercase, punctuation-normalized token capped at 32 characters in a
fingerprint such as protocol:codex_400_invalid_json_schema. This is a
health-neutral request failure: it does not change a connected subscription or
create a protocol incident. Free-form upstream message text is discarded,
and param is used only by the exact invalid_function_parameters tool-schema
case. Empty, malformed, oversized, non-object, and detail-free 400 bodies
remain protocol:unexpected_http_status_400 with
protocol_update_required because ownership cannot be established safely.
A trimmed, non-empty x-oai-request-id of at most 128 characters is retained
as opaque oaiRequestId metadata on the thrown transport/request error. It is
not part of the fingerprint and is not automatically persisted or displayed
in subscription health, incidents, notifications, or audits.
import {
openaiSubscriptionModule,
type OpenAISubscriptionFetcher
} from "@raybase/openai-subscription";
import type { RaybaseEnv } from "@raybase/core/worker";
type Env = RaybaseEnv & { CODEX_EGRESS: OpenAISubscriptionFetcher };
openaiSubscriptionModule<Env>({
credentialMode: "user",
background: { strategy: "run-as-user" },
codexEgress: import.meta.env.DEV
? { strategy: "local-relay" }
: { strategy: "cloudflare-vpc", binding: "CODEX_EGRESS" }
});For a production Cloudflare Worker, add the matching Workers VPC Network
binding to wrangler.jsonc:
{
"vpc_networks": [
{
"binding": "CODEX_EGRESS",
"network_id": "cf1:network",
"remote": true
}
]
}raybase modules add openai-subscription writes this binding to production.
raybase modules doctor reports a missing or malformed production binding.
For local development, the installed module selects local-relay.
raybase dev starts an ephemeral Node relay bound to 127.0.0.1, injects a
per-boot URL and shared secret into the local Worker, and removes them on clean
shutdown. Only Codex response traffic uses that relay; device and token traffic
continues directly to https://auth.openai.com. The relay accepts only the
Codex responses route and never logs request header values. local-relay
fails closed outside a Worker whose RAYBASE_ENV is local.
The binding is resolved from the active request's Worker environment. Codex
response traffic goes only through CODEX_EGRESS.fetch(); device and token
traffic remains on canonical https://auth.openai.com through normal fetch.
A missing, malformed, throwing, or 403 VPC binding fails closed. There is no
retry through global fetch. Standalone Node tools may omit codexEgress or use
{ strategy: "direct" } for backward-compatible direct traffic.
Cloudflare documents this public-Internet Gateway route in the Workers VPC overview, binding API, and Gateway egress announcement. Workers VPC is currently beta, so re-check those documents before deployment.
maxOutputTokens remains a Raybase admission and accounting input. Core
reserves the effective value before provider dispatch and records observed
usage, but the subscription wire does not send unsupported
max_output_tokens. An admitted response is bounded by the transport's 4 MB
SSE ceiling, not by server-side per-response token truncation.
Image inputs
User messages may interleave text, HTTPS image URLs, and inline base64/data-URI
images. The subscription adapter preserves that order and emits Responses
input_text and input_image items; an image always uses the verified
input_image.image_url field. Supported media types are PNG, JPEG, WEBP, and
non-animated GIF. System, assistant, and tool message content remains
string-only.
This boundary is intentionally strict. Unsupported kinds such as audio or
files, malformed MIME/base64/URL values, and extra fields are rejected before
fetch with caller errors that name the kind or field. In particular, Raybase
does not expose per-image detail or quality: it omits both and uses the
endpoint's accepted omitted-field behavior. See the dated evidence and exact
unsupported-kind inventory in
skills/raybase-openai-subscription/references/compatibility-and-testing.md.
Live Worker verification
Mock Fetcher tests prove routing and fail-closed behavior, not ChatGPT
reachability. The fixture is a Raybase repository source-checkout acceptance
gate under packages/openai-subscription/test-worker/; it is intentionally
not included in the published package and published consumers receive no
fixture scripts. From the repository root:
- Run
pnpm deploy:openai-subscription-worker-egress:remotefor the narrowly named ephemeral Worker inpackages/openai-subscription/test-worker/wrangler.jsonc. - Run
pnpm exec wrangler secret put OPENAI_SUBSCRIPTION_TOKEN --config packages/openai-subscription/test-worker/wrangler.jsoncand the same command forOPENAI_SUBSCRIPTION_VERIFICATION_KEY. Let Wrangler prompt on stdin; never put either value in configuration, source, command arguments, logs, or output. - POST
/verifywith the one-time key in the Authorization header. A pass reports only route, model, status, the validated SSE protocol, and final token counts. The canonical endpoint can omitContent-Type; a missing or empty value is accepted only when the bounded strict SSE parser reaches a completed response. A declared non-SSE media type fails before body read and reports only normalized content type,cf-raypresence, and redirect state; the upstream body remains unread and is cancelled. - Immediately run
pnpm delete:openai-subscription-worker-egress:remote. A 403 is a platform blocker; do not add browser headers or a direct-fetch retry. - Toby must rotate the furnished development subscription token after the verification evidence is captured.
The cloudflare-vpc lane, verified 2026-08-10
Acceptance item met: one real turn through CODEX_EGRESS from a genuinely
deployed Worker (wrangler deploy, binding cf1:network), carrying a
credential minted by this module's own device-code flow, returned inner status
200, validated SSE, gpt-5.6-terra, usage 11 in / 5 out. The spike ran from
committed 75dfca7 — that is, without the outbound header allowlist below
— which is what makes it decisive: neither the Workers runtime nor Cloudflare's
VPC egress decorates those subrequests with the headers that block the front
door. Production device-auth traffic was never affected.
chatgpt.com sits behind Cloudflare, and its front door blocks any request
carrying Cloudflare's own hop metadata. cf-worker — which workerd stamps on
every outbound subrequest — plus cf-connecting-ip and cf-visitor each
return 403 text/html with the blocked page and no x-oai-request-id,
because the request never reaches the Codex API. (cf-ray and cf-ipcountry
do not trigger it.) This, not any property of the credential, is what blocked
the local lane through August 2026: raybase dev's relay moved the TCP client
to Node but forwarded workerd's cf-worker header unchanged.
Two boundaries now guard this: the relay strips that header class at its own
edge, and the transport authors its outbound header set from an allowlist
(content-type, authorization, chatgpt-account-id, originator,
accept) for every egress strategy.
The allowlist is a guard, not a repair: the module has never authored a cf-*
header on any strategy, and the deploy above proves the runtime does not add
one either. It exists so a future edit cannot reintroduce the dev failure on
the VPC path, which has no relay boundary to catch it.
What still cannot be observed locally: headers appended after fetch()
leaves the module. That is now evidenced rather than assumed, but the evidence
is a point-in-time deploy — re-run the turn above if Cloudflare changes VPC
egress behaviour, since Workers VPC is still beta.
A 403 there is diagnosable in one glance: codex_egress_forbidden
(disconnected) is the front door and names the hop-header cause;
codex_account_forbidden (reauth_required) is the Codex API refusing the
account. Per step 4 above, a front-door 403 is a platform blocker — do not
answer it with browser headers or a direct-fetch retry.
Set appConfig.ai.provider to openai-codex-subscription. Create
RAYBASE_OPENAI_SUBSCRIPTION_KEY with at least 32 random bytes encoded as
base64/base64url and store it only as a local/deployed Worker secret. Never add
its value to Wrangler configuration, settings, source, or manifests.
Credential modes are explicit:
operator: one designated responsible operator subject.user: each signed-in user links their own eligible subscription.organization: an organization-controlled account, scoped to the resolved organization runtime.
protocol_update_required means update to a newly published
@raybase/openai-subscription version containing a reviewed upstream
pin/schema/fixture update. Do not edit the production allowlist or retry
blindly. A protocol:codex_400_<token> fingerprint without a health field is a
request rejection instead: fix the caller-owned request or schema and use a
bounded oaiRequestId, when present on the thrown error, for upstream support
correlation. For key rotation, deploy the reviewed replacement key and
reconnect each scope; old ciphertext is intentionally not decryptable under an
unrelated key.
The module informs operators about usage scope; it does not certify or enforce eligibility.
