@@ -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) => {
|
||||
|
||||
13
packages/coding-agent/src/core/telemetry.ts
Normal file
13
packages/coding-agent/src/core/telemetry.ts
Normal 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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user