Skip to content

feat: upstream request headers, and the client API read from the path - #34

Merged
fylorn merged 1 commit into
mainfrom
feat/headers-dialect
Sep 17, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/headers-dialect

Conversation

@fylorn

@fylorn fylorn commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Why

Two problems surfaced while planning account-login upstreams:

  1. Only one API key could be configured. A relay that authenticates with its own header (X-Relay-Token), or needs anthropic-version / an organization header pinned, could not be connected. The enterprise gateway already models upstream credentials as a header table.
  2. The client dialect was guessed from where the gateway key was put. Claude Code adopted through ANTHROPIC_AUTH_TOKEN sends Authorization: Bearer, so it was treated as an OpenAI client. Anthropic models were then refused by model admission, and errors came back in the wrong shape.

This is phase 1 of the upstream work (headers and dialect foundation). Any-to-any conversion and ChatGPT account login follow in later releases.

What changes

Credentials (tw-config::credential)

  • key: one API key, sent in the protocol's auth header (x-api-key / Authorization: Bearer / x-goog-api-key).
  • headers: an ordered name: value map. Values may use ${ENV}, {{client}} (calling gateway key name) and {{access_token}} (requires oauth). Configured headers replace same-name client headers.
  • oauth: its own block instead of living inside key. Tokens for Claude subscriptions (anthropic.com / claude.ai / claude.com) and the Gemini CLI login (cloudcode-pa) are rejected.
  • Provider::check_credential validates names, reserved headers, duplicates, placeholders, and key/oauth conflicts, and points at the offending row.

Control API (tw-api, tw-control)

  • ProviderView: masked key (plus env var name when the whole value is ${VAR}), auth_header, headers with per-row masked, oauth endpoint and client id.
  • ProviderInput: key: SecretChange (keep / none / set), headers: Vec<HeaderInput> (missing value keeps the saved one), oauth: OAuthChange. CredentialInput is removed.
  • ProviderPreviewRequest.protocol (optional) lets the edit dialog show which header the key goes in for the chosen protocol.

Gateway (tw-gateway)

  • New ClientApi::of_path decides the dialect, the error shape and model admission (servable_by). Unknown paths still pass through.
  • GET /v1/models and GET /v1/models/{id} have no body and no distinguishing path, so either anthropic-version or a key in x-api-key marks an Anthropic client.
  • POST /v1beta/models/{model}:generateContent (and the other Gemini actions) now reaches the pipeline instead of returning 405.
  • The gateway key is stripped from ?key= before building the upstream URL, and x-thinkwatch-* headers are no longer forwarded.

Breaking

  • Config: key: { oauth: … } becomes a top-level oauth: block on the provider. The project has no installed base to migrate, so there is no compatibility shim.
  • Control API: provider view and input shapes changed as above. ThinkWatch Lite is updated in a follow-up PR pinned to this release.

Tests

  • tests/headers_and_paths.rs: Bearer-keyed Claude Code on an Anthropic upstream, model listing, Gemini generateContent forwarding without the gateway key, configured headers replacing client headers, relay credentials in a custom header.
  • Credential validation, masking (tw-secret), control API keep/set/none and Claude token rejection, preview with a chosen protocol.
  • an_error_comes_back_in_the_dialect_the_client_speaks now asserts by path, including a Bearer key on /v1/messages.

cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace and ./scripts/smoke.sh pass locally.

🤖 Generated with Claude Code

Upstream credentials follow the enterprise gateway's header model:

- `key` is one API key, sent in the header the protocol uses
  (`x-api-key`, `Authorization: Bearer`, `x-goog-api-key`)
- `headers` is an ordered map of any other headers to send, or the whole
  credential for relays that authenticate with their own header. Values
  may use `${ENV}`, `{{client}}` (the calling gateway key's name) and
  `{{access_token}}` (requires `oauth`). Configured headers replace the
  ones the client sent under the same name
- `oauth` moves out of `key` into its own block. OAuth credentials for
  Claude subscriptions and the Gemini CLI's Google login are rejected

The control API mirrors this: `ProviderView` carries a masked `key`,
`auth_header`, masked `headers` and `oauth`; `ProviderInput` takes
`SecretChange` / `HeaderInput` / `OAuthChange`, where a missing header
value keeps the saved one. Provider preview accepts the chosen protocol
so the UI can say which header the key goes in.

The client API is now read from the request path instead of where the
gateway key was put. Fixes found along the way:

- Claude Code adopted with `ANTHROPIC_AUTH_TOKEN` sends a Bearer key and
  was treated as an OpenAI client: Anthropic models were refused by model
  admission and errors came back in OpenAI shape
- `POST /v1beta/models/{model}:generateContent` returned 405
- the gateway key in `?key=` was forwarded to the upstream URL
- `x-thinkwatch-*` request headers were forwarded upstream

`GET /v1/models` has no path to go by, so an `anthropic-version` header or
a key in `x-api-key` marks an Anthropic client there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit ccb0324 into main Sep 17, 2026
2 checks passed
@fylorn
fylorn deleted the feat/headers-dialect branch September 17, 2026 11:08
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