import { flattenObject } from "./utils.mjs"; import scarf_client_default from "./scarf-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.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.shouldSendEvent() || !this.segment) return; const flattenedProperties = flattenObject(properties); const propertiesWithGlobal = { ...this.globalProperties, ...flattenedProperties }; const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal).sort().reduce((obj, key) => { obj[key] = propertiesWithGlobal[key]; return obj; }, {}); this.segment.track({ anonymousId: this.anonymousId, event, properties: { ...orderedPropertiesWithGlobal } }); await scarf_client_default.logEvent({ event }); } 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 } }); } setSampleRate(sampleRate) { let _sampleRate; _sampleRate = sampleRate ?? .05; if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE); if (_sampleRate < 0 || _sampleRate > 1) throw new Error("Sample rate must be between 0 and 1"); this.sampleRate = _sampleRate; this.setGlobalProperties({ sampleRate: this.sampleRate, sampleRateAdjustmentFactor: 1 - this.sampleRate, sampleWeight: 1 / this.sampleRate }); } }; //#endregion export { TelemetryClient, isTelemetryDisabled }; //# sourceMappingURL=telemetry-client.mjs.map