Skip to content
Open
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
3 changes: 3 additions & 0 deletions core/bifrost.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/maximhq/bifrost/core/providers/azure"
"github.com/maximhq/bifrost/core/providers/bedrock"
"github.com/maximhq/bifrost/core/providers/cerebras"
"github.com/maximhq/bifrost/core/providers/codex"
"github.com/maximhq/bifrost/core/providers/cohere"
"github.com/maximhq/bifrost/core/providers/elevenlabs"
"github.com/maximhq/bifrost/core/providers/fireworks"
Expand Down Expand Up @@ -3636,6 +3637,8 @@ func (bifrost *Bifrost) createBaseProvider(providerKey schemas.ModelProvider, co
switch targetProviderKey {
case schemas.OpenAI:
return openai.NewOpenAIProvider(config, bifrost.logger), nil
case schemas.Codex:
return codex.NewCodexProvider(config, bifrost.logger)
Comment on lines +3640 to +3641
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Provider registration check =="
rg -n --type=go -C2 'case\s+schemas\.Codex|NewCodexProvider' core/bifrost.go

echo
echo "== Workflow Codex env/config check =="
for f in .github/workflows/pr-tests.yml .github/workflows/release-pipeline.yml; do
  if [[ -f "$f" ]]; then
    echo "---- $f ----"
    rg -n -C3 'codex|CODEX' "$f" || true
  else
    echo "Missing expected workflow file: $f"
  fi
done

Repository: maximhq/bifrost

Length of output: 507


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== All workflow files in .github/workflows =="
fd -e yml -e yaml . .github/workflows/ 2>/dev/null | sort

echo
echo "== Check what env vars/configs are in pr-tests.yml (full file) =="
head -100 .github/workflows/pr-tests.yml

echo
echo "== Check what env vars/configs are in release-pipeline.yml (full file) =="
head -100 .github/workflows/release-pipeline.yml

echo
echo "== Search for any existing provider env patterns to understand the structure =="
rg -n 'OPENAI|ANTHROPIC' .github/workflows/ -C 2

Repository: maximhq/bifrost

Length of output: 19520


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Check return statements for all providers in bifrost.go =="
rg -n 'return.*Provider.*config.*bifrost.logger' core/bifrost.go | head -20

Repository: maximhq/bifrost

Length of output: 1447


Add CODEX_API_KEY to workflow environments and fix return statement.

The Codex provider registration exists but has two issues:

  1. Missing , nil in return statement (line 3641) — inconsistent with providers like OpenAI, Anthropic, Mistral, Gemini that return (Provider, error).
  2. CODEX_API_KEY not wired into workflows — add CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} to env sections in pr-tests.yml and release-pipeline.yml (approximately 6–7 job env blocks where OPENAI_API_KEY and ANTHROPIC_API_KEY appear).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/bifrost.go` around lines 3640 - 3641, The Codex provider branch returns
codex.NewCodexProvider(config, bifrost.logger) but must return the
Provider,error tuple like the other providers; change the return in the
schemas.Codex case to return codex.NewCodexProvider(config, bifrost.logger), nil
(or adjust to match NewCodexProvider's signature) so it returns (Provider,
error). Also add CODEX_API_KEY into the workflow environment blocks in
pr-tests.yml and release-pipeline.yml — in each job env section where
OPENAI_API_KEY and ANTHROPIC_API_KEY are set (around 6–7 job env blocks), add
CODEX_API_KEY: ${{ secrets.CODEX_API_KEY }} so Codex is available to CI jobs.

case schemas.Anthropic:
return anthropic.NewAnthropicProvider(config, bifrost.logger), nil
case schemas.Bedrock:
Expand Down
212 changes: 212 additions & 0 deletions core/providers/codex/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package codex

import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/bytedance/sonic"
)

const (
OAuthClientID = "app_EMoamEEZ73f0CkXaXp7hrann"
OAuthIssuer = "https://auth.openai.com"
DeviceVerificationURL = OAuthIssuer + "/codex/device"
deviceCallbackRedirect = OAuthIssuer + "/deviceauth/callback"
defaultPollingMarginSecs = 3
)

type TokenResponse struct {
IDToken string `json:"id_token"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}

type DeviceAuthorizationResponse struct {
DeviceAuthID string `json:"device_auth_id"`
UserCode string `json:"user_code"`
Interval string `json:"interval"`
}

type DeviceTokenResponse struct {
AuthorizationCode string `json:"authorization_code"`
CodeVerifier string `json:"code_verifier"`
}

type IDTokenClaims struct {
ChatGPTAccountID string `json:"chatgpt_account_id,omitempty"`
Organizations []struct {
ID string `json:"id"`
} `json:"organizations,omitempty"`
OpenAIAuth *struct {
ChatGPTAccountID string `json:"chatgpt_account_id,omitempty"`
} `json:"https://api.openai.com/auth,omitempty"`
}

func RefreshAccessToken(ctx context.Context, client *http.Client, refreshToken string) (*TokenResponse, error) {
form := url.Values{}
form.Set("grant_type", "refresh_token")
form.Set("refresh_token", refreshToken)
form.Set("client_id", OAuthClientID)
return executeTokenRequest(ctx, client, OAuthIssuer+"/oauth/token", strings.NewReader(form.Encode()))
}

func StartDeviceAuthorization(ctx context.Context, client *http.Client, userAgent string) (*DeviceAuthorizationResponse, error) {
requestBody, err := sonic.Marshal(map[string]string{"client_id": OAuthClientID})
if err != nil {
return nil, err
}
request, err := http.NewRequestWithContext(ctx, http.MethodPost, OAuthIssuer+"/api/accounts/deviceauth/usercode", strings.NewReader(string(requestBody)))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
if userAgent != "" {
request.Header.Set("User-Agent", userAgent)
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("device authorization failed with status %d", response.StatusCode)
}
var result DeviceAuthorizationResponse
if err := sonic.ConfigDefault.NewDecoder(response.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}

func PollDeviceAuthorization(ctx context.Context, client *http.Client, deviceAuthID, userCode, userAgent string) (*DeviceTokenResponse, int, error) {
requestBody, err := sonic.Marshal(map[string]string{"device_auth_id": deviceAuthID, "user_code": userCode})
if err != nil {
return nil, 0, err
}
request, err := http.NewRequestWithContext(ctx, http.MethodPost, OAuthIssuer+"/api/accounts/deviceauth/token", strings.NewReader(string(requestBody)))
if err != nil {
return nil, 0, err
}
request.Header.Set("Content-Type", "application/json")
if userAgent != "" {
request.Header.Set("User-Agent", userAgent)
}
response, err := client.Do(request)
if err != nil {
return nil, 0, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, response.StatusCode, nil
}
var result DeviceTokenResponse
if err := sonic.ConfigDefault.NewDecoder(response.Body).Decode(&result); err != nil {
return nil, response.StatusCode, err
}
return &result, response.StatusCode, nil
}

func ExchangeDeviceAuthorizationCode(ctx context.Context, client *http.Client, code, codeVerifier string) (*TokenResponse, error) {
form := url.Values{}
form.Set("grant_type", "authorization_code")
form.Set("code", code)
form.Set("redirect_uri", deviceCallbackRedirect)
form.Set("client_id", OAuthClientID)
form.Set("code_verifier", codeVerifier)
return executeTokenRequest(ctx, client, OAuthIssuer+"/oauth/token", strings.NewReader(form.Encode()))
}

func ExtractAccountID(tokens *TokenResponse) string {
if tokens == nil {
return ""
}
for _, candidate := range []string{tokens.IDToken, tokens.AccessToken} {
claims := parseJWTClaims(candidate)
if claims == nil {
continue
}
if claims.ChatGPTAccountID != "" {
return claims.ChatGPTAccountID
}
if claims.OpenAIAuth != nil && claims.OpenAIAuth.ChatGPTAccountID != "" {
return claims.OpenAIAuth.ChatGPTAccountID
}
if len(claims.Organizations) > 0 && claims.Organizations[0].ID != "" {
return claims.Organizations[0].ID
}
}
return ""
}

func ExpiresAtFromNow(expiresIn int) string {
if expiresIn <= 0 {
expiresIn = 3600
}
return time.Now().Add(time.Duration(expiresIn) * time.Second).UTC().Format(time.RFC3339)
}

func NextPollTime(intervalSeconds int) time.Time {
if intervalSeconds <= 0 {
intervalSeconds = 5
}
return time.Now().Add(time.Duration(intervalSeconds+defaultPollingMarginSecs) * time.Second)
}

func executeTokenRequest(ctx context.Context, client *http.Client, endpoint string, body *strings.Reader) (*TokenResponse, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, body)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("token request failed with status %d", response.StatusCode)
}
var tokenResponse TokenResponse
if err := sonic.ConfigDefault.NewDecoder(response.Body).Decode(&tokenResponse); err != nil {
return nil, err
}
return &tokenResponse, nil
}

func generateRandomString(length int) (string, error) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
result := make([]byte, length)
for i, value := range bytes {
result[i] = chars[int(value)%len(chars)]
}
return string(result), nil
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Comment on lines +185 to +196
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for usages of generateRandomString across the codebase
rg -n 'generateRandomString' --type=go

Repository: maximhq/bifrost

Length of output: 147


Remove unused generateRandomString function.

This function is not called anywhere in the codebase and should be removed as dead code. If it is intended for future use, it can be added back later when needed.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/providers/codex/auth.go` around lines 185 - 196, Remove the
dead/unreferenced helper function generateRandomString from the file (the
function declaration and its body) since it isn't used anywhere; delete the
entire generateRandomString function definition to eliminate dead code and keep
the codebase clean, and run tests/linters to ensure no references remain to
generateRandomString.


func parseJWTClaims(token string) *IDTokenClaims {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return nil
}
decoded, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil
}
var claims IDTokenClaims
if err := sonic.Unmarshal(decoded, &claims); err != nil {
return nil
}
return &claims
}
19 changes: 19 additions & 0 deletions core/providers/codex/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package codex

import "testing"

func TestExtractAccountID(t *testing.T) {
token := &TokenResponse{
AccessToken: "eyJhbGciOiJub25lIn0.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoib3JnXzEyMyJ9fQ.",
}
if accountID := ExtractAccountID(token); accountID != "org_123" {
t.Fatalf("expected account id org_123, got %q", accountID)
}
}

func TestExpiresAtFromNow(t *testing.T) {
value := ExpiresAtFromNow(60)
if value == "" {
t.Fatal("expected non-empty RFC3339 expiry")
}
}
Loading