87 lines
3.2 KiB
JavaScript
87 lines
3.2 KiB
JavaScript
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
const require_utils = require('./utils.cjs');
|
|
const require_scarf_client = require('./scarf-client.cjs');
|
|
let uuid = require("uuid");
|
|
let _segment_analytics_node = require("@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_${(0, uuid.v4)()}`;
|
|
this.packageName = packageName;
|
|
this.packageVersion = packageVersion;
|
|
this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();
|
|
if (this.telemetryDisabled) return;
|
|
this.setSampleRate(sampleRate);
|
|
this.segment = new _segment_analytics_node.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 = require_utils.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 require_scarf_client.default.logEvent({ event });
|
|
}
|
|
setGlobalProperties(properties) {
|
|
const flattenedProperties = require_utils.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
|
|
exports.TelemetryClient = TelemetryClient;
|
|
exports.isTelemetryDisabled = isTelemetryDisabled;
|
|
//# sourceMappingURL=telemetry-client.cjs.map
|