Skip to content

fix: send OpenRouter app attribution headers on compaction requests - #2

Open
KD-MM2 wants to merge 4 commits into
JMHSV:mainfrom
KD-MM2:main
Open

KD-MM2 wants to merge 4 commits into
JMHSV:mainfrom
KD-MM2:main

Conversation

@KD-MM2

@KD-MM2 KD-MM2 commented Sep 16, 2026

Copy link
Copy Markdown

Problem

Compaction routed to OpenRouter free models fails(somes, not all) with a 403(). OpenRouter
only serves its free endpoints to registered agentic harnesses, identified by
app-attribution headers (HTTP-Referer, X-OpenRouter-Title,
X-OpenRouter-Categories). Pi adds these headers to session requests via its
provider-attribution module, but the exported compact() path bypasses that
pipeline(so requests made by this extension never carried the attribution
and were rejected).

Fix

The extension now adds the same attribution headers Pi sends, with the same
gating semantics:

  • withOpenRouterAttribution() (src/index.ts) merges
    HTTP-Referer: https://pi.dev, X-OpenRouter-Title: pi, and
    X-OpenRouter-Categories: cli-agent into the compaction request headers,
    only for OpenRouter models (provider openrouter, or a base URL on
    openrouter.ai). Caller-provided auth headers take precedence, matching
    Pi's mergeProviderAttributionHeaders merge order.
  • isInstallTelemetryEnabled() (src/config.ts) mirrors Pi's gate:
    attribution is only sent when install telemetry is enabled
    (enableInstallTelemetry setting, overridable via PI_TELEMETRY with
    1/true/yes).
  • createSettings() (src/config.ts) extracts SettingsManager creation so
    the hook builds one manager shared by config loading and the telemetry
    check.

Tests

  • New test/headers.test.ts (5 tests): attribution added for OpenRouter,
    baseUrl-based detection for custom providers, non-OpenRouter models
    untouched, telemetry-disabled untouched, caller-header precedence.
  • test/config.test.ts (+3 tests): telemetry gate settings fallback and
    truthy/falsy PI_TELEMETRY values.
  • bun run check (typecheck + full suite): 15/15 passing.

Docs

  • README: new "OpenRouter free models" section explaining the behavior and
    the telemetry gate.

Notes

  • No User-Agent change was needed: pi-ai already sends Pi's UA on every
    request; OpenRouter's free-tier check is the attribution headers.
  • Includes a one-line bun.lock update (peerDependency range sync).

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