fix(coding-agent): add OpenRouter attribution headers

closes #3414
This commit is contained in:
Mario Zechner
2026-04-20 14:13:49 +02:00
parent 23569e304b
commit 62c1c4031c
5 changed files with 220 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ import type { ResourceLoader } from "./resource-loader.js";
import { DefaultResourceLoader } from "./resource-loader.js";
import { getDefaultSessionDir, SessionManager } from "./session-manager.js";
import { SettingsManager } from "./settings-manager.js";
import { isInstallTelemetryEnabled } from "./telemetry.js";
import { time } from "./timings.js";
import {
allTools,
@@ -131,6 +132,23 @@ function getDefaultAgentDir(): string {
return getAgentDir();
}
function getOpenRouterAttributionHeaders(
model: Model<any>,
settingsManager: SettingsManager,
): Record<string, string> | undefined {
if (!isInstallTelemetryEnabled(settingsManager)) {
return undefined;
}
if (model.provider !== "openrouter" && !model.baseUrl.includes("openrouter.ai")) {
return undefined;
}
return {
"HTTP-Referer": "https://pi.dev",
"X-OpenRouter-Title": "pi",
"X-OpenRouter-Categories": "cli-agent",
};
}
/**
* Create an AgentSession with the specified options.
*
@@ -301,10 +319,14 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
if (!auth.ok) {
throw new Error(auth.error);
}
const openRouterAttributionHeaders = getOpenRouterAttributionHeaders(model, settingsManager);
return streamSimple(model, context, {
...options,
apiKey: auth.apiKey,
headers: auth.headers || options?.headers ? { ...auth.headers, ...options?.headers } : undefined,
headers:
openRouterAttributionHeaders || auth.headers || options?.headers
? { ...openRouterAttributionHeaders, ...auth.headers, ...options?.headers }
: undefined,
});
},
onPayload: async (payload, _model) => {

View File

@@ -0,0 +1,13 @@
import type { SettingsManager } from "./settings-manager.js";
function isTruthyEnvFlag(value: string | undefined): boolean {
if (!value) return false;
return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
}
export function isInstallTelemetryEnabled(
settingsManager: SettingsManager,
telemetryEnv: string | undefined = process.env.PI_TELEMETRY,
): boolean {
return telemetryEnv !== undefined ? isTruthyEnvFlag(telemetryEnv) : settingsManager.getEnableInstallTelemetry();
}

View File

@@ -65,6 +65,7 @@ import { formatMissingSessionCwdPrompt, MissingSessionCwdError } from "../../cor
import { type SessionContext, SessionManager } from "../../core/session-manager.js";
import { BUILTIN_SLASH_COMMANDS } from "../../core/slash-commands.js";
import type { SourceInfo } from "../../core/source-info.js";
import { isInstallTelemetryEnabled } from "../../core/telemetry.js";
import type { TruncationResult } from "../../core/tools/truncate.js";
import { getChangelogPath, getNewEntries, parseChangelog } from "../../utils/changelog.js";
import { copyToClipboard } from "../../utils/clipboard.js";
@@ -154,11 +155,6 @@ function isAnthropicSubscriptionAuthKey(apiKey: string | undefined): boolean {
return typeof apiKey === "string" && apiKey.startsWith("sk-ant-oat");
}
function isTruthyEnvFlag(value: string | undefined): boolean {
if (!value) return false;
return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
}
function isUnknownModel(model: Model<any> | undefined): boolean {
return !!model && model.provider === "unknown" && model.id === "unknown" && model.api === "unknown";
}
@@ -844,10 +840,7 @@ export class InteractiveMode {
return;
}
const telemetryEnv = process.env.PI_TELEMETRY;
const telemetryEnabled =
telemetryEnv !== undefined ? isTruthyEnvFlag(telemetryEnv) : this.settingsManager.getEnableInstallTelemetry();
if (!telemetryEnabled) {
if (!isInstallTelemetryEnabled(this.settingsManager)) {
return;
}