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) => {