Skip to content

feat(ai-gateway): add generic provider proxy for 11 OpenAI-compatible providers#8

Open
theredspoon wants to merge 10 commits intosimple10:mainfrom
theredspoon:feat/generic-providers
Open

feat(ai-gateway): add generic provider proxy for 11 OpenAI-compatible providers#8
theredspoon wants to merge 10 commits intosimple10:mainfrom
theredspoon:feat/generic-providers

Conversation

@theredspoon
Copy link
Contributor

@theredspoon theredspoon commented Mar 9, 2026

Summary

  • Add 11 OpenAI-compatible providers (Cohere, DeepSeek, Fireworks, Groq, MiniMax, Mistral, Moonshot, OpenRouter, Perplexity, Together, xAI) via /proxy/{provider}/v1/... routes
  • Provider + endpoint whitelist prevents open proxy risk
  • Per-user credentials stored in KV under providers.*, managed via the Config UI
  • CF AI Gateway support for generic providers (same auto-detection as legacy)
  • Llemtry telemetry for all generic provider traffic
  • Self-service Config UI with collapsible "Additional Providers" section

Bug fixes (added during review)

  • CF Access headers decoupled from egress proxy authbuildCodexHeaders() now builds proxy auth and CF Access headers independently. Previously CF Access headers were silently dropped when EGRESS_PROXY_AUTH_TOKEN was unset.
  • Partial CF Access config rejected — both CF_ACCESS_CLIENT_ID and CF_ACCESS_CLIENT_SECRET must be set or neither Access header is emitted. Previously a missing secret produced a broken header value.
  • Config UI aborts save on parse errorbuildUpdate() now uses a hasError flag to abort on Codex OAuth parse failure instead of submitting a partial update. save() guards against the resulting null return.

Depends on #7 (fix/openai-proxy-headers) — that commit is included in this branch. Once #7 merges, rebasing this branch will drop it cleanly.

Test plan

  • POST /proxy/deepseek/v1/chat/completions with valid key — success
  • POST /proxy/unknown/v1/chat/completions — 404 (unknown provider)
  • POST /proxy/deepseek/v1/responses — 404 (non-whitelisted endpoint)
  • POST /proxy/groq/v1/chat/completions without key — 401
  • PUT /auth/creds with { "providers": { "deepseek": { "apiKey": "..." } } } — saves
  • GET /auth/creds — shows masked providers.* keys
  • PUT /auth/creds with { "providers": { "deepseek": null } } — deletes
  • Legacy routes (/anthropic/v1/messages, /openai/v1/chat/completions) — still work
  • Config UI — save/load/clear generic provider keys
  • Config UI — invalid Codex OAuth JSON shows error, does not submit partial update
  • CF AI Gateway mode — generic providers route through gateway with correct path format
  • buildCodexHeaders({}) returns undefined
  • buildCodexHeaders with only CF Access vars (no proxy auth) returns Access headers
  • buildCodexHeaders with only CF_ACCESS_CLIENT_ID (no secret) returns undefined

cf-connecting-ip, cf-ipcountry, cf-ray, cf-visitor were forwarded to
upstream in direct mode. Also prevents sending a request body on GET
/v1/models requests.
… providers

Generalize the AI Gateway Worker from 3 hardcoded providers to support
11 additional OpenAI-compatible providers (DeepSeek, Groq, Mistral,
Together, xAI, OpenRouter, Perplexity, Cohere, Fireworks, MiniMax,
Moonshot) via /proxy/{provider}/v1/... routes.

- routing: GENERIC_PROVIDERS whitelist, GENERIC_ENDPOINTS whitelist,
  matchGenericRoute() parser
- config: PROVIDER_DEFAULTS with verified base URLs,
  getGenericProviderConfig() lookup
- keys: getGenericApiKey() reads from creds.providers[provider].apiKey
- providers/generic: OpenAI-compatible passthrough proxy
- index: generic route handling with auth, key lookup, llemtry telemetry
- admin: mergeCredentials/maskCredentials extended for providers field
- config-ui: collapsible Additional Providers section with 11 API key
  fields, 3-segment data-field path support in buildUpdate()
- llemtry: widen ReportOptions.provider to string for generic names
- types: LegacyProvider alias, GenericRouteMatch, providers on
  UserCredentials
… generic provider

- generic.ts: preserve all request headers (matching openai.ts pattern),
  only rewrite Authorization; support ProviderConfig with egress proxy
  URL wrapping and CF header stripping
- config.ts: getGenericProviderConfig returns full ProviderConfig with
  egressProxyUrl and proxy auth headers when configured
- index.ts: pass ProviderConfig to proxyGeneric; fix error code for
  missing API key from 500 to 401 (consistency with generic route)
…ndant log

- config.ts: getGenericProviderConfig no longer inherits EGRESS_PROXY_URL;
  the egress sidecar is a chatgpt.com WAF workaround scoped to openai-codex
  and should not add an extra hop for generic providers
- index.ts: remove redundant console.error on missing API key (already
  logged by getProviderApiKey via log.warn)
…ove dead code

- config.ts: getGenericProviderConfig now returns CF AI Gateway config
  when CF_AI_GATEWAY_* env vars are set, matching legacy provider behavior
- index.ts: compute gateway path for generic routes ({provider}/chat/completions
  format, stripping v1/ prefix)
- generic.ts: remove dead egress proxy URL wrapping and CF header stripping
  (egress proxy is scoped to openai-codex only); keep config.headers
  handling for CF AI Gateway auth header
- generic.ts: strip Cloudflare-injected metadata headers (cf-*) before
  forwarding to upstream providers (defense in depth)
- config-ui.ts: convert early return in buildUpdate to if/else for
  clarity
- README.md: document generic provider routes and 14-provider support
- AI-GATEWAY-CONFIG.md: add generic provider credential types, route
  pattern, and verification example
…gle provider list

- Remove unused LegacyProvider type and GenericRouteMatch re-export
  from types.ts
- Extract shared loadCredentials() helper in keys.ts, eliminating
  duplicate KV read/parse boilerplate
- Derive GENERIC_PROVIDERS from exported PROVIDER_DEFAULTS so the
  provider list is defined once
CF Access headers were silently dropped when EGRESS_PROXY_AUTH_TOKEN was
unset because the entire headers object was gated on that var. Extract
buildCodexHeaders() that builds each header set independently.

Also fix buildUpdate() in config-ui: a parse error in the forEach callback
only exited that iteration, allowing a partial update to be submitted.
Add hasError flag to abort the entire build and null guard in save().
buildCodexHeaders() used a non-null assertion for CF_ACCESS_CLIENT_SECRET
when only CF_ACCESS_CLIENT_ID was checked. Now both must be set or neither
Access header is emitted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant