UNPKG

@copilotkit/shared

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

104 lines (102 loc) 3.69 kB
import { flattenObject } from "./utils.mjs"; import { lambdaClient, parseAndWarnTelemetryId } from "./lambda-client.mjs"; import { v4 } from "uuid"; import { Analytics } from "@segment/analytics-node"; //#region src/telemetry/telemetry-client.ts /** * Checks if telemetry is disabled via environment variables. * Users can opt out by setting: * - COPILOTKIT_TELEMETRY_DISABLED=true or COPILOTKIT_TELEMETRY_DISABLED=1 * - DO_NOT_TRACK=true or DO_NOT_TRACK=1 */ function isTelemetryDisabled() { return process.env.COPILOTKIT_TELEMETRY_DISABLED === "true" || process.env.COPILOTKIT_TELEMETRY_DISABLED === "1" || process.env.DO_NOT_TRACK === "true" || process.env.DO_NOT_TRACK === "1"; } var TelemetryClient = class { constructor({ packageName, packageVersion, telemetryDisabled, telemetryBaseUrl, sampleRate }) { this.globalProperties = {}; this.cloudConfiguration = null; this.licenseToken = null; this.telemetryId = null; this.telemetryDisabled = false; this.sampleRate = .05; this.anonymousId = `anon_${v4()}`; this.packageName = packageName; this.packageVersion = packageVersion; this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled(); if (this.telemetryDisabled) return; this.setSampleRate(sampleRate); this.segment = new Analytics({ writeKey: process.env.COPILOTKIT_SEGMENT_WRITE_KEY || "n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja" }); this.setGlobalProperties({ "copilotkit.package.name": packageName, "copilotkit.package.version": packageVersion }); } shouldSendEvent() { return Math.random() < this.sampleRate; } async capture(event, properties) { if (this.telemetryDisabled) return; if (!this.telemetryId && !this.shouldSendEvent()) return; const effectiveSampleRate = this.telemetryId ? 1 : this.sampleRate; const samplingMeta = { sampleRate: effectiveSampleRate, sampleRateAdjustmentFactor: 1 - effectiveSampleRate, sampleWeight: 1 / effectiveSampleRate }; const flattenedProperties = flattenObject(properties); const propertiesWithGlobal = { ...this.globalProperties, ...samplingMeta, ...flattenedProperties }; const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal).sort().reduce((obj, key) => { obj[key] = propertiesWithGlobal[key]; return obj; }, {}); await lambdaClient.send({ event, properties: flattenedProperties, globalProperties: { ...this.globalProperties, ...samplingMeta }, packageName: this.packageName, packageVersion: this.packageVersion, licenseToken: this.licenseToken ?? void 0 }); if (this.segment) this.segment.track({ anonymousId: this.anonymousId, event, properties: { ...orderedPropertiesWithGlobal } }); } setGlobalProperties(properties) { const flattenedProperties = flattenObject(properties); this.globalProperties = { ...this.globalProperties, ...flattenedProperties }; } setCloudConfiguration(properties) { this.cloudConfiguration = properties; this.setGlobalProperties({ cloud: { publicApiKey: properties.publicApiKey, baseUrl: properties.baseUrl } }); } setLicenseToken(licenseToken) { this.licenseToken = licenseToken; this.telemetryId = parseAndWarnTelemetryId(licenseToken); } setSampleRate(sampleRate) { let _sampleRate; _sampleRate = sampleRate ?? .05; if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE); if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) throw new Error("Sample rate must be between 0 and 1"); this.sampleRate = _sampleRate; } }; //#endregion export { TelemetryClient, isTelemetryDisabled }; //# sourceMappingURL=telemetry-client.mjs.map