pig_protocol/auth

Types

How to reach an OpenAI-shaped backend.

StandardApi targets the classic /v1/chat/completions (and /v1/responses) platform route. CodexOAuth targets the ChatGPT Plus/Enterprise chatgpt.com/backend-api/codex/responses route using a JWT access token.

pub type EndpointMode {
  StandardApi(api_key: String, base_url: String)
  CodexOAuth(access_token: String, base_url: String)
}

Constructors

  • StandardApi(api_key: String, base_url: String)
  • CodexOAuth(access_token: String, base_url: String)

Values

pub fn account_id_from_jwt(
  token: String,
) -> Result(String, error.AiError)

Extract the ChatGPT account ID from a Codex JWT access token.

The account ID lives under the claim https://api.openai.com/auth.chatgpt_account_id. The token is expected to be a standard three-part JWT; any malformed token returns an InvalidResponse error.

pub fn chat_url(mode: EndpointMode) -> String

Resolve the URL for the Chat Completions endpoint.

Standard API: base_url <> "/chat/completions". Codex mode: falls back to the Responses endpoint because Codex does not expose a Chat Completions route.

pub fn headers(
  mode: EndpointMode,
  streaming: Bool,
) -> Result(List(#(String, String)), error.AiError)

Build request headers for the given endpoint mode.

Standard API emits Authorization: Bearer <api_key> and JSON content headers. Codex OAuth derives chatgpt-account-id from the JWT and adds the required Responses API beta headers.

pub fn is_jwt(token: String) -> Bool

Resolve whether the token looks like a Codex JWT (has three parts).

pub fn responses_url(mode: EndpointMode) -> String

Resolve the URL for the Responses API endpoint.

Standard API: base_url <> "/responses". Codex mode: base_url normalized to end with /codex/responses.

Search Document