🤖 feat: add AI app attribution headers #1141
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds default app attribution headers (
HTTP-Referer,X-Title) to all AI SDK provider requests, enabling OpenRouter (and compatible gateways) to attribute mux usage.buildAppAttributionHeaders(case-insensitive merge, never overwrites user-provided values)streamText()calls inherit it📋 Implementation Plan
🤖 Plan: Add app attribution headers to AI SDK streaming (OpenRouter leaderboard)
Goal
Ensure mux’s
streamText()calls include app attribution so OpenRouter (and any other compatible gateway) can attribute traffic to mux (e.g., for leaderboards).For OpenRouter specifically, this means sending these request headers:
HTTP-Referer: a stable URL for the app (recommended by OpenRouter)X-Title: a human-readable app nameRecommended approach (A): Inject attribution headers for all providers in
AIService.createModel()(default-on)Net new product LoC: ~30–55
Why this approach
streamText()call sites.providers.jsoncheaders(we never overwrite existingHTTP-Referer/X-Title, case-insensitive).Implementation steps
Add centralized constants for mux attribution values
src/constants/appAttribution.tsexporting:MUX_APP_ATTRIBUTION_TITLE="mux"MUX_APP_ATTRIBUTION_URL="https://mux.coder.com"Add a small, tested header-merge helper (defensive + case-insensitive)
src/node/services/aiService.ts, export:buildAppAttributionHeaders(existing?: Record<string, string>): Record<string, string>HTTP-Referer,X-Title).Wire into model creation once
AIService.createModel()(after baseUrl → baseURL normalization), apply:providerConfig.headers = buildAppAttributionHeaders(providerConfig.headers)headersthrough to the provider factory.Unit tests
src/node/services/aiService.test.tswithdescribe("buildAppAttributionHeaders", ...).http-referer,X-TITLE)Validation
make typecheckbun test src/node/services/aiService.test.tsRollout / compatibility notes
HTTP-Referer/X-Title(case-insensitive check).Generated with
mux• Model: "unknown" • Thinking: "unknown"