@copilotkit/shared 1.81.0 → 1.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.umd.js +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/telemetry-client.cjs +1 -1
- package/dist/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/telemetry/telemetry-client.d.cts.map +1 -1
- package/dist/telemetry/telemetry-client.d.mts.map +1 -1
- package/dist/telemetry/telemetry-client.mjs +1 -1
- package/dist/telemetry/telemetry-client.mjs.map +1 -1
- package/package.json +1 -1
- package/src/telemetry/telemetry-client.test.ts +18 -0
- package/src/telemetry/telemetry-client.ts +3 -1
package/dist/index.umd.js
CHANGED
package/dist/package.cjs
CHANGED
package/dist/package.mjs
CHANGED
|
@@ -39,7 +39,7 @@ var TelemetryClient = class {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
async captureWithIdentity(event, properties, identity) {
|
|
42
|
-
if (this.telemetryDisabled) return;
|
|
42
|
+
if (this.telemetryDisabled || require_telemetry_disabled.isTelemetryDisabled()) return;
|
|
43
43
|
const sendToSegment = Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
|
|
44
44
|
const lambdaSamplingMeta = require_sampling.computeSamplingMeta({
|
|
45
45
|
telemetryId: identity.licenseTelemetryId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.cjs","names":["isTelemetryDisabled","Analytics","computeSamplingMeta","UNSAMPLED_RATE","TELEMETRY_EMITTER_V1","TELEMETRY_SURFACE_V1","flattenObject","lambdaClient","firstNonBlankTelemetryId","parseAndWarnTelemetryId"],"sources":["../../src/telemetry/telemetry-client.ts"],"sourcesContent":["import { Analytics } from \"@segment/analytics-node\";\nimport type { AnalyticsEvents } from \"./events\";\nimport { flattenObject } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n firstNonBlankTelemetryId,\n lambdaClient,\n parseAndWarnTelemetryId,\n} from \"./lambda-client\";\nimport {\n computeSamplingMeta,\n TELEMETRY_EMITTER_V1,\n TELEMETRY_SURFACE_V1,\n UNSAMPLED_RATE,\n} from \"./sampling\";\nimport { isTelemetryDisabled } from \"./telemetry-disabled\";\n\nexport { isTelemetryDisabled };\n\n/** Transport identity and sampling authority resolved for one runtime. */\nexport interface TelemetryIdentity {\n telemetryId?: string;\n licenseToken?: string;\n}\n\n/** Capture-only telemetry client bound to one runtime identity. */\nexport interface TelemetryCapture {\n capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void>;\n}\n\ninterface ResolvedTelemetryIdentity {\n telemetryId: string | null;\n licenseToken: string | null;\n licenseTelemetryId: string | null;\n}\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n // EIP / Intelligence license token (Ed25519-signed JWT). The lambda\n // client decodes its payload to extract telemetry_id. Customer API\n // keys are NOT used here — they flow only into Segment.\n private licenseToken: string | null = null;\n // Standalone analytics identity. This stays separate from the effective\n // identity so legacy callers continue sending only their license token to\n // the Lambda transport.\n private telemetryId: string | null = null;\n // License-derived identity used only as sampling authority. A standalone\n // telemetry id remains a transport claim and does not bypass the\n // Segment gate.\n private licenseTelemetryId: string | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n // Client-side sampling rate for anonymous events on the Segment wire\n // only. The lambda sink is ours and takes every event; Segment is\n // billed per event, so the anonymous population stays capped there.\n // Identified events (those whose license token yielded a telemetry_id)\n // bypass the gate on both wires.\n private segmentSampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n // Anonymous sampling rate for the Segment wire. Named without the\n // prefix for backward compatibility; the lambda sink ignores it.\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey =\n process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||\n \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendToSegment() {\n const randomNumber = Math.random();\n return randomNumber < this.segmentSampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void> {\n return this.captureWithIdentity(event, properties, {\n telemetryId: this.telemetryId,\n licenseToken: this.licenseToken,\n licenseTelemetryId: this.licenseTelemetryId,\n });\n }\n\n private async captureWithIdentity<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n identity: ResolvedTelemetryIdentity,\n ): Promise<void> {\n if (this.telemetryDisabled) {\n return;\n }\n\n // The two wires are gated separately. The lambda sink takes every\n // event: it is CopilotKit-owned, so anonymous volume costs nothing\n // per event there and a real count beats one extrapolated from 5% of\n // the population. Segment is billed per event, so it keeps the gate —\n // callers without license-derived sampling authority are dropped from\n // that copy at segmentSampleRate. Legacy license tokens with a\n // telemetry_id send on both wires: the volume is bounded by\n // paying-customer count and full fidelity per identified customer is\n // worth the marginal cost.\n const sendToSegment =\n Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();\n\n // Sampling metadata is computed in ./sampling so this client and the\n // v2 runtime client can't drift apart again — see the note there.\n // One call per wire, because the two wires were gated differently and\n // a shared block would overweight the lambda copy by 1/sampleRate.\n const lambdaSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: UNSAMPLED_RATE,\n });\n const segmentSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.segmentSampleRate,\n });\n\n // Everything below travels identically on both copies of this event.\n // The event id is what makes the dual-write dedupable downstream:\n // one capture() produces one id, stamped on the lambda copy and the\n // Segment copy alike, so consumers no longer have to infer the\n // duplication from $lib or from which fields happen to be present\n // (OSS-1019).\n const eventMeta = {\n telemetry_emitter: TELEMETRY_EMITTER_V1,\n telemetry_surface: TELEMETRY_SURFACE_V1,\n telemetry_event_id: uuidv4(),\n };\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal: Record<string, any> = {\n ...this.globalProperties,\n ...eventMeta,\n ...segmentSamplingMeta,\n telemetry_transport: \"segment\",\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n await lambdaClient.send({\n event,\n properties: flattenedProperties,\n globalProperties: {\n ...this.globalProperties,\n ...eventMeta,\n ...lambdaSamplingMeta,\n telemetry_transport: \"lambda\",\n },\n packageName: this.packageName,\n packageVersion: this.packageVersion,\n telemetryId: identity.telemetryId ?? undefined,\n licenseToken: identity.licenseToken ?? undefined,\n });\n\n if (this.segment && sendToSegment) {\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n /**\n * Atomically configure standalone, legacy, or anonymous telemetry identity.\n *\n * A standalone id takes transport precedence over a supplied legacy license\n * token, but only a license-derived id grants sampling authority. Neither\n * value is added to event properties.\n *\n * @param identity - One standalone id, one legacy license token, or neither.\n */\n setTelemetryIdentity(identity: {\n telemetryId?: string;\n licenseToken?: string;\n }): void {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n this.telemetryId = resolvedIdentity.telemetryId;\n this.licenseToken = resolvedIdentity.licenseToken;\n this.licenseTelemetryId = resolvedIdentity.licenseTelemetryId;\n }\n\n /**\n * Configure legacy license-derived telemetry identity.\n *\n * @param licenseToken - License token whose telemetry claim identifies sends.\n */\n setLicenseToken(licenseToken: string) {\n this.setTelemetryIdentity({ licenseToken });\n }\n\n /**\n * Create an immutable capture scope for one runtime.\n *\n * The scope shares this client's sinks, process-wide opt-out, global\n * properties, and Segment sampling rate, but snapshots transport identity and\n * license-derived sampling authority. Constructing another runtime cannot\n * rewrite an existing scope.\n *\n * @param identity - The runtime's construction-time telemetry identity.\n * @returns A capture-only client bound to that identity.\n */\n createScope(identity: TelemetryIdentity): TelemetryCapture {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n\n return {\n capture: <K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ) => this.captureWithIdentity(event, properties, resolvedIdentity),\n };\n }\n\n private resolveTelemetryIdentity(\n identity: TelemetryIdentity,\n ): ResolvedTelemetryIdentity {\n const telemetryId = firstNonBlankTelemetryId(identity.telemetryId);\n if (telemetryId !== undefined) {\n return {\n telemetryId,\n licenseToken: null,\n licenseTelemetryId: null,\n };\n }\n\n return {\n telemetryId: null,\n licenseToken: identity.licenseToken ?? null,\n licenseTelemetryId: identity.licenseToken\n ? parseAndWarnTelemetryId(identity.licenseToken)\n : null,\n };\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n // Number.isNaN guards against parseFloat(\"nonsense\") slipping past the\n // range check (all NaN comparisons are false), which would silently\n // drop every anonymous Segment event with no signal.\n if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.segmentSampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling, once per\n // wire. Only license-authorized events get effectiveSampleRate=1 on\n // the Segment copy; standalone transport identity stays in the\n // sampled population there. The lambda copy is always unsampled.\n }\n}\n"],"mappings":";;;;;;;;;AAuCA,IAAa,kBAAb,MAA6B;CA2B3B,YAAY,EACV,aACA,gBACA,mBACA,kBACA,cASC;0BAvCqC,EAAE;4BAC6B;sBAIjC;qBAID;4BAIO;2BAGP;2BAMD;qBACd,sBAAgB;AAiBpC,OAAK,cAAc;AACnB,OAAK,iBAAiB;AACtB,OAAK,oBAAoB,qBAAqBA,gDAAqB;AAEnE,MAAI,KAAK,kBACP;AAGF,OAAK,cAAc,WAAW;AAO9B,OAAK,UAAU,IAAIC,kCAAU,EAC3B,UAJA,QAAQ,IAAI,gCACZ,oCAID,CAAC;AAEF,OAAK,oBAAoB;GACvB,2BAA2B;GAC3B,8BAA8B;GAC/B,CAAC;;CAGJ,AAAQ,sBAAsB;AAE5B,SADqB,KAAK,QAAQ,GACZ,KAAK;;CAG7B,MAAM,QACJ,OACA,YACe;AACf,SAAO,KAAK,oBAAoB,OAAO,YAAY;GACjD,aAAa,KAAK;GAClB,cAAc,KAAK;GACnB,oBAAoB,KAAK;GAC1B,CAAC;;CAGJ,MAAc,oBACZ,OACA,YACA,UACe;AACf,MAAI,KAAK,kBACP;EAYF,MAAM,gBACJ,QAAQ,SAAS,mBAAmB,IAAI,KAAK,qBAAqB;EAMpE,MAAM,qBAAqBC,qCAAoB;GAC7C,aAAa,SAA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;;;;;;;;;;;;;CAc7C,YAAY,UAA+C;EACzD,MAAM,mBAAmB,KAAK,yBAAyB,SAAS;AAEhE,SAAO,EACL,UACE,OACA,eACG,KAAK,oBAAoB,OAAO,YAAY,iBAAiB,EACnE;;CAGH,AAAQ,yBACN,UAC2B;EAC3B,MAAM,cAAcE,+CAAyB,SAAS,YAAY;AAClE,MAAI,gBAAgB,OAClB,QAAO;GACL;GACA,cAAc;GACd,oBAAoB;GACrB;AAGH,SAAO;GACL,aAAa;GACb,cAAc,SAAS,gBAAgB;GACvC,oBAAoB,SAAS,eACzBC,8CAAwB,SAAS,aAAa,GAC9C;GACL;;CAGH,AAAQ,cAAc,YAAgC;EACpD,IAAI;AAEJ,gBAAc,cAAc;AAG5B,MAAI,QAAQ,IAAI,iCAEd,eAAc,WAAW,QAAQ,IAAI,iCAAiC;AAMxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,oBAAoB"}
|
|
1
|
+
{"version":3,"file":"telemetry-client.cjs","names":["isTelemetryDisabled","Analytics","computeSamplingMeta","UNSAMPLED_RATE","TELEMETRY_EMITTER_V1","TELEMETRY_SURFACE_V1","flattenObject","lambdaClient","firstNonBlankTelemetryId","parseAndWarnTelemetryId"],"sources":["../../src/telemetry/telemetry-client.ts"],"sourcesContent":["import { Analytics } from \"@segment/analytics-node\";\nimport type { AnalyticsEvents } from \"./events\";\nimport { flattenObject } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n firstNonBlankTelemetryId,\n lambdaClient,\n parseAndWarnTelemetryId,\n} from \"./lambda-client\";\nimport {\n computeSamplingMeta,\n TELEMETRY_EMITTER_V1,\n TELEMETRY_SURFACE_V1,\n UNSAMPLED_RATE,\n} from \"./sampling\";\nimport { isTelemetryDisabled } from \"./telemetry-disabled\";\n\nexport { isTelemetryDisabled };\n\n/** Transport identity and sampling authority resolved for one runtime. */\nexport interface TelemetryIdentity {\n telemetryId?: string;\n licenseToken?: string;\n}\n\n/** Capture-only telemetry client bound to one runtime identity. */\nexport interface TelemetryCapture {\n capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void>;\n}\n\ninterface ResolvedTelemetryIdentity {\n telemetryId: string | null;\n licenseToken: string | null;\n licenseTelemetryId: string | null;\n}\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n // EIP / Intelligence license token (Ed25519-signed JWT). The lambda\n // client decodes its payload to extract telemetry_id. Customer API\n // keys are NOT used here — they flow only into Segment.\n private licenseToken: string | null = null;\n // Standalone analytics identity. This stays separate from the effective\n // identity so legacy callers continue sending only their license token to\n // the Lambda transport.\n private telemetryId: string | null = null;\n // License-derived identity used only as sampling authority. A standalone\n // telemetry id remains a transport claim and does not bypass the\n // Segment gate.\n private licenseTelemetryId: string | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n // Client-side sampling rate for anonymous events on the Segment wire\n // only. The lambda sink is ours and takes every event; Segment is\n // billed per event, so the anonymous population stays capped there.\n // Identified events (those whose license token yielded a telemetry_id)\n // bypass the gate on both wires.\n private segmentSampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n // Anonymous sampling rate for the Segment wire. Named without the\n // prefix for backward compatibility; the lambda sink ignores it.\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey =\n process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||\n \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendToSegment() {\n const randomNumber = Math.random();\n return randomNumber < this.segmentSampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void> {\n return this.captureWithIdentity(event, properties, {\n telemetryId: this.telemetryId,\n licenseToken: this.licenseToken,\n licenseTelemetryId: this.licenseTelemetryId,\n });\n }\n\n private async captureWithIdentity<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n identity: ResolvedTelemetryIdentity,\n ): Promise<void> {\n // Re-read the environment: an opt-out set after import (for example a\n // dotenv call below the imports) must still stop events.\n if (this.telemetryDisabled || isTelemetryDisabled()) {\n return;\n }\n\n // The two wires are gated separately. The lambda sink takes every\n // event: it is CopilotKit-owned, so anonymous volume costs nothing\n // per event there and a real count beats one extrapolated from 5% of\n // the population. Segment is billed per event, so it keeps the gate —\n // callers without license-derived sampling authority are dropped from\n // that copy at segmentSampleRate. Legacy license tokens with a\n // telemetry_id send on both wires: the volume is bounded by\n // paying-customer count and full fidelity per identified customer is\n // worth the marginal cost.\n const sendToSegment =\n Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();\n\n // Sampling metadata is computed in ./sampling so this client and the\n // v2 runtime client can't drift apart again — see the note there.\n // One call per wire, because the two wires were gated differently and\n // a shared block would overweight the lambda copy by 1/sampleRate.\n const lambdaSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: UNSAMPLED_RATE,\n });\n const segmentSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.segmentSampleRate,\n });\n\n // Everything below travels identically on both copies of this event.\n // The event id is what makes the dual-write dedupable downstream:\n // one capture() produces one id, stamped on the lambda copy and the\n // Segment copy alike, so consumers no longer have to infer the\n // duplication from $lib or from which fields happen to be present\n // (OSS-1019).\n const eventMeta = {\n telemetry_emitter: TELEMETRY_EMITTER_V1,\n telemetry_surface: TELEMETRY_SURFACE_V1,\n telemetry_event_id: uuidv4(),\n };\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal: Record<string, any> = {\n ...this.globalProperties,\n ...eventMeta,\n ...segmentSamplingMeta,\n telemetry_transport: \"segment\",\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n await lambdaClient.send({\n event,\n properties: flattenedProperties,\n globalProperties: {\n ...this.globalProperties,\n ...eventMeta,\n ...lambdaSamplingMeta,\n telemetry_transport: \"lambda\",\n },\n packageName: this.packageName,\n packageVersion: this.packageVersion,\n telemetryId: identity.telemetryId ?? undefined,\n licenseToken: identity.licenseToken ?? undefined,\n });\n\n if (this.segment && sendToSegment) {\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n /**\n * Atomically configure standalone, legacy, or anonymous telemetry identity.\n *\n * A standalone id takes transport precedence over a supplied legacy license\n * token, but only a license-derived id grants sampling authority. Neither\n * value is added to event properties.\n *\n * @param identity - One standalone id, one legacy license token, or neither.\n */\n setTelemetryIdentity(identity: {\n telemetryId?: string;\n licenseToken?: string;\n }): void {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n this.telemetryId = resolvedIdentity.telemetryId;\n this.licenseToken = resolvedIdentity.licenseToken;\n this.licenseTelemetryId = resolvedIdentity.licenseTelemetryId;\n }\n\n /**\n * Configure legacy license-derived telemetry identity.\n *\n * @param licenseToken - License token whose telemetry claim identifies sends.\n */\n setLicenseToken(licenseToken: string) {\n this.setTelemetryIdentity({ licenseToken });\n }\n\n /**\n * Create an immutable capture scope for one runtime.\n *\n * The scope shares this client's sinks, process-wide opt-out, global\n * properties, and Segment sampling rate, but snapshots transport identity and\n * license-derived sampling authority. Constructing another runtime cannot\n * rewrite an existing scope.\n *\n * @param identity - The runtime's construction-time telemetry identity.\n * @returns A capture-only client bound to that identity.\n */\n createScope(identity: TelemetryIdentity): TelemetryCapture {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n\n return {\n capture: <K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ) => this.captureWithIdentity(event, properties, resolvedIdentity),\n };\n }\n\n private resolveTelemetryIdentity(\n identity: TelemetryIdentity,\n ): ResolvedTelemetryIdentity {\n const telemetryId = firstNonBlankTelemetryId(identity.telemetryId);\n if (telemetryId !== undefined) {\n return {\n telemetryId,\n licenseToken: null,\n licenseTelemetryId: null,\n };\n }\n\n return {\n telemetryId: null,\n licenseToken: identity.licenseToken ?? null,\n licenseTelemetryId: identity.licenseToken\n ? parseAndWarnTelemetryId(identity.licenseToken)\n : null,\n };\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n // Number.isNaN guards against parseFloat(\"nonsense\") slipping past the\n // range check (all NaN comparisons are false), which would silently\n // drop every anonymous Segment event with no signal.\n if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.segmentSampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling, once per\n // wire. Only license-authorized events get effectiveSampleRate=1 on\n // the Segment copy; standalone transport identity stays in the\n // sampled population there. The lambda copy is always unsampled.\n }\n}\n"],"mappings":";;;;;;;;;AAuCA,IAAa,kBAAb,MAA6B;CA2B3B,YAAY,EACV,aACA,gBACA,mBACA,kBACA,cASC;0BAvCqC,EAAE;4BAC6B;sBAIjC;qBAID;4BAIO;2BAGP;2BAMD;qBACd,sBAAgB;AAiBpC,OAAK,cAAc;AACnB,OAAK,iBAAiB;AACtB,OAAK,oBAAoB,qBAAqBA,gDAAqB;AAEnE,MAAI,KAAK,kBACP;AAGF,OAAK,cAAc,WAAW;AAO9B,OAAK,UAAU,IAAIC,kCAAU,EAC3B,UAJA,QAAQ,IAAI,gCACZ,oCAID,CAAC;AAEF,OAAK,oBAAoB;GACvB,2BAA2B;GAC3B,8BAA8B;GAC/B,CAAC;;CAGJ,AAAQ,sBAAsB;AAE5B,SADqB,KAAK,QAAQ,GACZ,KAAK;;CAG7B,MAAM,QACJ,OACA,YACe;AACf,SAAO,KAAK,oBAAoB,OAAO,YAAY;GACjD,aAAa,KAAK;GAClB,cAAc,KAAK;GACnB,oBAAoB,KAAK;GAC1B,CAAC;;CAGJ,MAAc,oBACZ,OACA,YACA,UACe;AAGf,MAAI,KAAK,qBAAqBD,gDAAqB,CACjD;EAYF,MAAM,gBACJ,QAAQ,SAAS,mBAAmB,IAAI,KAAK,qBAAqB;EAMpE,MAAM,qBAAqBE,qCAAoB;GAC7C,aAAa,SAAS;GACtB,YAAYC;GACb,CAAC;EACF,MAAM,sBAAsBD,qCAAoB;GAC9C,aAAa,SAAS;GACtB,YAAY,KAAK;GAClB,CAAC;EAQF,MAAM,YAAY;GAChB,mBAAmBE;GACnB,mBAAmBC;GACnB,kC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,OAAA;EAAA,QAQW,mBAAA;
|
|
1
|
+
{"version":3,"file":"telemetry-client.d.cts","names":[],"sources":["../../src/telemetry/telemetry-client.ts"],"mappings":";;;;;;UAoBiB,iBAAA;EACf,WAAA;EACA,YAAA;AAAA;;UAIe,gBAAA;EACf,OAAA,iBAAwB,eAAA,EACtB,KAAA,EAAO,CAAA,EACP,UAAA,EAAY,eAAA,CAAgB,CAAA,IAC3B,OAAA;AAAA;AAAA,cASQ,eAAA;EACX,OAAA,EAAS,SAAA;EACT,gBAAA,EAAkB,MAAA;EAClB,kBAAA;IAAsB,YAAA;IAAsB,OAAA;EAAA;EAAA,QAIpC,YAAA;EAAA,QAIA,WAAA;EAAA,QAIA,kBAAA;EACR,WAAA;EACA,cAAA;EAAA,QACQ,iBAAA;EAAA,QAMA,iBAAA;EAAA,QACA,WAAA;;IAGN,WAAA;IACA,cAAA;IACA,iBAAA;IACA,gBAAA;IACA;EAAA;IAEA,WAAA;IACA,cAAA;IACA,iBAAA;IACA,gBAAA;IAGA,UAAA;EAAA;EAAA,QA2BM,mBAAA;EAKF,OAAA,iBAAwB,eAAA,CAAA,CAC5B,KAAA,EAAO,CAAA,EACP,UAAA,EAAY,eAAA,CAAgB,CAAA,IAC3B,OAAA;EAAA,QAQW,mBAAA;EA0Fd,mBAAA,CAAoB,UAAA,EAAY,MAAA;EAQhC,qBAAA,CAAsB,UAAA;IAAc,YAAA;IAAsB,OAAA;EAAA;EA3G5C;;;;;;;;;EA+Hd,oBAAA,CAAqB,QAAA;IACnB,WAAA;IACA,YAAA;EAAA;EAxMoB;;;;;EAqNtB,eAAA,CAAgB,YAAA;EAvMhB;;;;;;;;;;;EAsNA,WAAA,CAAY,QAAA,EAAU,iBAAA,GAAoB,gBAAA;EAAA,QAWlC,wBAAA;EAAA,QAqBA,aAAA;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.d.mts","names":[],"sources":["../../src/telemetry/telemetry-client.ts"],"mappings":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A;EAAA;IAEA,WAAA;IACA,cAAA;IACA,iBAAA;IACA,gBAAA;IAGA,UAAA;EAAA;EAAA,QA2BM,mBAAA;EAKF,OAAA,iBAAwB,eAAA,CAAA,CAC5B,KAAA,EAAO,CAAA,EACP,UAAA,EAAY,eAAA,CAAgB,CAAA,IAC3B,OAAA;EAAA,QAQW,mBAAA;EA0Fd,mBAAA,CAAoB,UAAA,EAAY,MAAA;EAQhC,qBAAA,CAAsB,UAAA;IAAc,YAAA;IAAsB,OAAA;EAAA;EA3G5C;;;;;;;;;EA+Hd,oBAAA,CAAqB,QAAA;IACnB,WAAA;IACA,YAAA;EAAA;EAxMoB;;;;;EAqNtB,eAAA,CAAgB,YAAA;EAvMhB;;;;;;;;;;;EAsNA,WAAA,CAAY,QAAA,EAAU,iBAAA,GAAoB,gBAAA;EAAA,QAWlC,wBAAA;EAAA,QAqBA,aAAA;AAAA"}
|
|
@@ -38,7 +38,7 @@ var TelemetryClient = class {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
async captureWithIdentity(event, properties, identity) {
|
|
41
|
-
if (this.telemetryDisabled) return;
|
|
41
|
+
if (this.telemetryDisabled || isTelemetryDisabled()) return;
|
|
42
42
|
const sendToSegment = Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
|
|
43
43
|
const lambdaSamplingMeta = computeSamplingMeta({
|
|
44
44
|
telemetryId: identity.licenseTelemetryId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.mjs","names":["uuidv4"],"sources":["../../src/telemetry/telemetry-client.ts"],"sourcesContent":["import { Analytics } from \"@segment/analytics-node\";\nimport type { AnalyticsEvents } from \"./events\";\nimport { flattenObject } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n firstNonBlankTelemetryId,\n lambdaClient,\n parseAndWarnTelemetryId,\n} from \"./lambda-client\";\nimport {\n computeSamplingMeta,\n TELEMETRY_EMITTER_V1,\n TELEMETRY_SURFACE_V1,\n UNSAMPLED_RATE,\n} from \"./sampling\";\nimport { isTelemetryDisabled } from \"./telemetry-disabled\";\n\nexport { isTelemetryDisabled };\n\n/** Transport identity and sampling authority resolved for one runtime. */\nexport interface TelemetryIdentity {\n telemetryId?: string;\n licenseToken?: string;\n}\n\n/** Capture-only telemetry client bound to one runtime identity. */\nexport interface TelemetryCapture {\n capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void>;\n}\n\ninterface ResolvedTelemetryIdentity {\n telemetryId: string | null;\n licenseToken: string | null;\n licenseTelemetryId: string | null;\n}\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n // EIP / Intelligence license token (Ed25519-signed JWT). The lambda\n // client decodes its payload to extract telemetry_id. Customer API\n // keys are NOT used here — they flow only into Segment.\n private licenseToken: string | null = null;\n // Standalone analytics identity. This stays separate from the effective\n // identity so legacy callers continue sending only their license token to\n // the Lambda transport.\n private telemetryId: string | null = null;\n // License-derived identity used only as sampling authority. A standalone\n // telemetry id remains a transport claim and does not bypass the\n // Segment gate.\n private licenseTelemetryId: string | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n // Client-side sampling rate for anonymous events on the Segment wire\n // only. The lambda sink is ours and takes every event; Segment is\n // billed per event, so the anonymous population stays capped there.\n // Identified events (those whose license token yielded a telemetry_id)\n // bypass the gate on both wires.\n private segmentSampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n // Anonymous sampling rate for the Segment wire. Named without the\n // prefix for backward compatibility; the lambda sink ignores it.\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey =\n process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||\n \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendToSegment() {\n const randomNumber = Math.random();\n return randomNumber < this.segmentSampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void> {\n return this.captureWithIdentity(event, properties, {\n telemetryId: this.telemetryId,\n licenseToken: this.licenseToken,\n licenseTelemetryId: this.licenseTelemetryId,\n });\n }\n\n private async captureWithIdentity<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n identity: ResolvedTelemetryIdentity,\n ): Promise<void> {\n if (this.telemetryDisabled) {\n return;\n }\n\n // The two wires are gated separately. The lambda sink takes every\n // event: it is CopilotKit-owned, so anonymous volume costs nothing\n // per event there and a real count beats one extrapolated from 5% of\n // the population. Segment is billed per event, so it keeps the gate —\n // callers without license-derived sampling authority are dropped from\n // that copy at segmentSampleRate. Legacy license tokens with a\n // telemetry_id send on both wires: the volume is bounded by\n // paying-customer count and full fidelity per identified customer is\n // worth the marginal cost.\n const sendToSegment =\n Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();\n\n // Sampling metadata is computed in ./sampling so this client and the\n // v2 runtime client can't drift apart again — see the note there.\n // One call per wire, because the two wires were gated differently and\n // a shared block would overweight the lambda copy by 1/sampleRate.\n const lambdaSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: UNSAMPLED_RATE,\n });\n const segmentSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.segmentSampleRate,\n });\n\n // Everything below travels identically on both copies of this event.\n // The event id is what makes the dual-write dedupable downstream:\n // one capture() produces one id, stamped on the lambda copy and the\n // Segment copy alike, so consumers no longer have to infer the\n // duplication from $lib or from which fields happen to be present\n // (OSS-1019).\n const eventMeta = {\n telemetry_emitter: TELEMETRY_EMITTER_V1,\n telemetry_surface: TELEMETRY_SURFACE_V1,\n telemetry_event_id: uuidv4(),\n };\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal: Record<string, any> = {\n ...this.globalProperties,\n ...eventMeta,\n ...segmentSamplingMeta,\n telemetry_transport: \"segment\",\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n await lambdaClient.send({\n event,\n properties: flattenedProperties,\n globalProperties: {\n ...this.globalProperties,\n ...eventMeta,\n ...lambdaSamplingMeta,\n telemetry_transport: \"lambda\",\n },\n packageName: this.packageName,\n packageVersion: this.packageVersion,\n telemetryId: identity.telemetryId ?? undefined,\n licenseToken: identity.licenseToken ?? undefined,\n });\n\n if (this.segment && sendToSegment) {\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n /**\n * Atomically configure standalone, legacy, or anonymous telemetry identity.\n *\n * A standalone id takes transport precedence over a supplied legacy license\n * token, but only a license-derived id grants sampling authority. Neither\n * value is added to event properties.\n *\n * @param identity - One standalone id, one legacy license token, or neither.\n */\n setTelemetryIdentity(identity: {\n telemetryId?: string;\n licenseToken?: string;\n }): void {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n this.telemetryId = resolvedIdentity.telemetryId;\n this.licenseToken = resolvedIdentity.licenseToken;\n this.licenseTelemetryId = resolvedIdentity.licenseTelemetryId;\n }\n\n /**\n * Configure legacy license-derived telemetry identity.\n *\n * @param licenseToken - License token whose telemetry claim identifies sends.\n */\n setLicenseToken(licenseToken: string) {\n this.setTelemetryIdentity({ licenseToken });\n }\n\n /**\n * Create an immutable capture scope for one runtime.\n *\n * The scope shares this client's sinks, process-wide opt-out, global\n * properties, and Segment sampling rate, but snapshots transport identity and\n * license-derived sampling authority. Constructing another runtime cannot\n * rewrite an existing scope.\n *\n * @param identity - The runtime's construction-time telemetry identity.\n * @returns A capture-only client bound to that identity.\n */\n createScope(identity: TelemetryIdentity): TelemetryCapture {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n\n return {\n capture: <K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ) => this.captureWithIdentity(event, properties, resolvedIdentity),\n };\n }\n\n private resolveTelemetryIdentity(\n identity: TelemetryIdentity,\n ): ResolvedTelemetryIdentity {\n const telemetryId = firstNonBlankTelemetryId(identity.telemetryId);\n if (telemetryId !== undefined) {\n return {\n telemetryId,\n licenseToken: null,\n licenseTelemetryId: null,\n };\n }\n\n return {\n telemetryId: null,\n licenseToken: identity.licenseToken ?? null,\n licenseTelemetryId: identity.licenseToken\n ? parseAndWarnTelemetryId(identity.licenseToken)\n : null,\n };\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n // Number.isNaN guards against parseFloat(\"nonsense\") slipping past the\n // range check (all NaN comparisons are false), which would silently\n // drop every anonymous Segment event with no signal.\n if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.segmentSampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling, once per\n // wire. Only license-authorized events get effectiveSampleRate=1 on\n // the Segment copy; standalone transport identity stays in the\n // sampled population there. The lambda copy is always unsampled.\n }\n}\n"],"mappings":";;;;;;;;AAuCA,IAAa,kBAAb,MAA6B;CA2B3B,YAAY,EACV,aACA,gBACA,mBACA,kBACA,cASC;0BAvCqC,EAAE;4BAC6B;sBAIjC;qBAID;4BAIO;2BAGP;2BAMD;qBACd,QAAQA,IAAQ;AAiBpC,OAAK,cAAc;AACnB,OAAK,iBAAiB;AACtB,OAAK,oBAAoB,qBAAqB,qBAAqB;AAEnE,MAAI,KAAK,kBACP;AAGF,OAAK,cAAc,WAAW;AAO9B,OAAK,UAAU,IAAI,UAAU,EAC3B,UAJA,QAAQ,IAAI,gCACZ,oCAID,CAAC;AAEF,OAAK,oBAAoB;GACvB,2BAA2B;GAC3B,8BAA8B;GAC/B,CAAC;;CAGJ,AAAQ,sBAAsB;AAE5B,SADqB,KAAK,QAAQ,GACZ,KAAK;;CAG7B,MAAM,QACJ,OACA,YACe;AACf,SAAO,KAAK,oBAAoB,OAAO,YAAY;GACjD,aAAa,KAAK;GAClB,cAAc,KAAK;GACnB,oBAAoB,KAAK;GAC1B,CAAC;;CAGJ,MAAc,oBACZ,OACA,YACA,UACe;AACf,MAAI,KAAK,kBACP;EAYF,MAAM,gBACJ,QAAQ,SAAS,mBAAmB,IAAI,KAAK,qBAAqB;EAMpE,MAAM,qBAAqB,oBAAoB;GAC7C,aAAa,SAAS;GACtB,YAAY;GACb,CAAC;EACF,MAAM,sBAAsB,oBAAoB;GAC9C,aAAa,SAAS;GACtB,YAAY,KAAK;GAClB,CAAC;EAQF,MA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,oBAAoB,OAAO,YAAY,iBAAiB,EACnE;;CAGH,AAAQ,yBACN,UAC2B;EAC3B,MAAM,cAAc,yBAAyB,SAAS,YAAY;AAClE,MAAI,gBAAgB,OAClB,QAAO;GACL;GACA,cAAc;GACd,oBAAoB;GACrB;AAGH,SAAO;GACL,aAAa;GACb,cAAc,SAAS,gBAAgB;GACvC,oBAAoB,SAAS,eACzB,wBAAwB,SAAS,aAAa,GAC9C;GACL;;CAGH,AAAQ,cAAc,YAAgC;EACpD,IAAI;AAEJ,gBAAc,cAAc;AAG5B,MAAI,QAAQ,IAAI,iCAEd,eAAc,WAAW,QAAQ,IAAI,iCAAiC;AAMxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,oBAAoB"}
|
|
1
|
+
{"version":3,"file":"telemetry-client.mjs","names":["uuidv4"],"sources":["../../src/telemetry/telemetry-client.ts"],"sourcesContent":["import { Analytics } from \"@segment/analytics-node\";\nimport type { AnalyticsEvents } from \"./events\";\nimport { flattenObject } from \"./utils\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n firstNonBlankTelemetryId,\n lambdaClient,\n parseAndWarnTelemetryId,\n} from \"./lambda-client\";\nimport {\n computeSamplingMeta,\n TELEMETRY_EMITTER_V1,\n TELEMETRY_SURFACE_V1,\n UNSAMPLED_RATE,\n} from \"./sampling\";\nimport { isTelemetryDisabled } from \"./telemetry-disabled\";\n\nexport { isTelemetryDisabled };\n\n/** Transport identity and sampling authority resolved for one runtime. */\nexport interface TelemetryIdentity {\n telemetryId?: string;\n licenseToken?: string;\n}\n\n/** Capture-only telemetry client bound to one runtime identity. */\nexport interface TelemetryCapture {\n capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void>;\n}\n\ninterface ResolvedTelemetryIdentity {\n telemetryId: string | null;\n licenseToken: string | null;\n licenseTelemetryId: string | null;\n}\n\nexport class TelemetryClient {\n segment: Analytics | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n // EIP / Intelligence license token (Ed25519-signed JWT). The lambda\n // client decodes its payload to extract telemetry_id. Customer API\n // keys are NOT used here — they flow only into Segment.\n private licenseToken: string | null = null;\n // Standalone analytics identity. This stays separate from the effective\n // identity so legacy callers continue sending only their license token to\n // the Lambda transport.\n private telemetryId: string | null = null;\n // License-derived identity used only as sampling authority. A standalone\n // telemetry id remains a transport claim and does not bypass the\n // Segment gate.\n private licenseTelemetryId: string | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n // Client-side sampling rate for anonymous events on the Segment wire\n // only. The lambda sink is ours and takes every event; Segment is\n // billed per event, so the anonymous population stays capped there.\n // Identified events (those whose license token yielded a telemetry_id)\n // bypass the gate on both wires.\n private segmentSampleRate: number = 0.05;\n private anonymousId = `anon_${uuidv4()}`;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n // Anonymous sampling rate for the Segment wire. Named without the\n // prefix for backward compatibility; the lambda sink ignores it.\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n // eslint-disable-next-line\n const writeKey =\n process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||\n \"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja\";\n\n this.segment = new Analytics({\n writeKey,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendToSegment() {\n const randomNumber = Math.random();\n return randomNumber < this.segmentSampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ): Promise<void> {\n return this.captureWithIdentity(event, properties, {\n telemetryId: this.telemetryId,\n licenseToken: this.licenseToken,\n licenseTelemetryId: this.licenseTelemetryId,\n });\n }\n\n private async captureWithIdentity<K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n identity: ResolvedTelemetryIdentity,\n ): Promise<void> {\n // Re-read the environment: an opt-out set after import (for example a\n // dotenv call below the imports) must still stop events.\n if (this.telemetryDisabled || isTelemetryDisabled()) {\n return;\n }\n\n // The two wires are gated separately. The lambda sink takes every\n // event: it is CopilotKit-owned, so anonymous volume costs nothing\n // per event there and a real count beats one extrapolated from 5% of\n // the population. Segment is billed per event, so it keeps the gate —\n // callers without license-derived sampling authority are dropped from\n // that copy at segmentSampleRate. Legacy license tokens with a\n // telemetry_id send on both wires: the volume is bounded by\n // paying-customer count and full fidelity per identified customer is\n // worth the marginal cost.\n const sendToSegment =\n Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();\n\n // Sampling metadata is computed in ./sampling so this client and the\n // v2 runtime client can't drift apart again — see the note there.\n // One call per wire, because the two wires were gated differently and\n // a shared block would overweight the lambda copy by 1/sampleRate.\n const lambdaSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: UNSAMPLED_RATE,\n });\n const segmentSamplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.segmentSampleRate,\n });\n\n // Everything below travels identically on both copies of this event.\n // The event id is what makes the dual-write dedupable downstream:\n // one capture() produces one id, stamped on the lambda copy and the\n // Segment copy alike, so consumers no longer have to infer the\n // duplication from $lib or from which fields happen to be present\n // (OSS-1019).\n const eventMeta = {\n telemetry_emitter: TELEMETRY_EMITTER_V1,\n telemetry_surface: TELEMETRY_SURFACE_V1,\n telemetry_event_id: uuidv4(),\n };\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal: Record<string, any> = {\n ...this.globalProperties,\n ...eventMeta,\n ...segmentSamplingMeta,\n telemetry_transport: \"segment\",\n ...flattenedProperties,\n };\n const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)\n .sort()\n .reduce(\n (obj, key) => {\n obj[key] = propertiesWithGlobal[key];\n return obj;\n },\n {} as Record<string, any>,\n );\n\n await lambdaClient.send({\n event,\n properties: flattenedProperties,\n globalProperties: {\n ...this.globalProperties,\n ...eventMeta,\n ...lambdaSamplingMeta,\n telemetry_transport: \"lambda\",\n },\n packageName: this.packageName,\n packageVersion: this.packageVersion,\n telemetryId: identity.telemetryId ?? undefined,\n licenseToken: identity.licenseToken ?? undefined,\n });\n\n if (this.segment && sendToSegment) {\n this.segment.track({\n anonymousId: this.anonymousId,\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = {\n ...this.globalProperties,\n ...flattenedProperties,\n };\n }\n\n setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {\n this.cloudConfiguration = properties;\n\n this.setGlobalProperties({\n cloud: {\n publicApiKey: properties.publicApiKey,\n baseUrl: properties.baseUrl,\n },\n });\n }\n\n /**\n * Atomically configure standalone, legacy, or anonymous telemetry identity.\n *\n * A standalone id takes transport precedence over a supplied legacy license\n * token, but only a license-derived id grants sampling authority. Neither\n * value is added to event properties.\n *\n * @param identity - One standalone id, one legacy license token, or neither.\n */\n setTelemetryIdentity(identity: {\n telemetryId?: string;\n licenseToken?: string;\n }): void {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n this.telemetryId = resolvedIdentity.telemetryId;\n this.licenseToken = resolvedIdentity.licenseToken;\n this.licenseTelemetryId = resolvedIdentity.licenseTelemetryId;\n }\n\n /**\n * Configure legacy license-derived telemetry identity.\n *\n * @param licenseToken - License token whose telemetry claim identifies sends.\n */\n setLicenseToken(licenseToken: string) {\n this.setTelemetryIdentity({ licenseToken });\n }\n\n /**\n * Create an immutable capture scope for one runtime.\n *\n * The scope shares this client's sinks, process-wide opt-out, global\n * properties, and Segment sampling rate, but snapshots transport identity and\n * license-derived sampling authority. Constructing another runtime cannot\n * rewrite an existing scope.\n *\n * @param identity - The runtime's construction-time telemetry identity.\n * @returns A capture-only client bound to that identity.\n */\n createScope(identity: TelemetryIdentity): TelemetryCapture {\n const resolvedIdentity = this.resolveTelemetryIdentity(identity);\n\n return {\n capture: <K extends keyof AnalyticsEvents>(\n event: K,\n properties: AnalyticsEvents[K],\n ) => this.captureWithIdentity(event, properties, resolvedIdentity),\n };\n }\n\n private resolveTelemetryIdentity(\n identity: TelemetryIdentity,\n ): ResolvedTelemetryIdentity {\n const telemetryId = firstNonBlankTelemetryId(identity.telemetryId);\n if (telemetryId !== undefined) {\n return {\n telemetryId,\n licenseToken: null,\n licenseTelemetryId: null,\n };\n }\n\n return {\n telemetryId: null,\n licenseToken: identity.licenseToken ?? null,\n licenseTelemetryId: identity.licenseToken\n ? parseAndWarnTelemetryId(identity.licenseToken)\n : null,\n };\n }\n\n private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n // eslint-disable-next-line\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n // eslint-disable-next-line\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n // Number.isNaN guards against parseFloat(\"nonsense\") slipping past the\n // range check (all NaN comparisons are false), which would silently\n // drop every anonymous Segment event with no signal.\n if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.segmentSampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling, once per\n // wire. Only license-authorized events get effectiveSampleRate=1 on\n // the Segment copy; standalone transport identity stays in the\n // sampled population there. The lambda copy is always unsampled.\n }\n}\n"],"m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AAgC;EACpD,IAAI;AAEJ,gBAAc,cAAc;AAG5B,MAAI,QAAQ,IAAI,iCAEd,eAAc,WAAW,QAAQ,IAAI,iCAAiC;AAMxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,oBAAoB"}
|
package/package.json
CHANGED
|
@@ -214,6 +214,24 @@ describe("v1 TelemetryClient", () => {
|
|
|
214
214
|
});
|
|
215
215
|
});
|
|
216
216
|
|
|
217
|
+
test("an opt-out set after the client is created stops both sinks", async () => {
|
|
218
|
+
// e.g. dotenv.config() placed below the import that creates the client.
|
|
219
|
+
vi.stubEnv("COPILOTKIT_TELEMETRY_DISABLED", "");
|
|
220
|
+
vi.stubEnv("DO_NOT_TRACK", "");
|
|
221
|
+
try {
|
|
222
|
+
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
223
|
+
const client = makeClient({ sampleRate: 0.05 });
|
|
224
|
+
vi.stubEnv("DO_NOT_TRACK", "1");
|
|
225
|
+
|
|
226
|
+
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
|
|
227
|
+
|
|
228
|
+
expect(lambdaSpy).not.toHaveBeenCalled();
|
|
229
|
+
expect(segmentTrackMock).not.toHaveBeenCalled();
|
|
230
|
+
} finally {
|
|
231
|
+
vi.unstubAllEnvs();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
217
235
|
test("an anonymous event sampled out of Segment still reaches the lambda sink", async () => {
|
|
218
236
|
// Math.random=0.99 vs sampleRate=0.05. The gate is Segment's alone now:
|
|
219
237
|
// the lambda sink is ours and takes every event, so a rate that drops
|
|
@@ -125,7 +125,9 @@ export class TelemetryClient {
|
|
|
125
125
|
properties: AnalyticsEvents[K],
|
|
126
126
|
identity: ResolvedTelemetryIdentity,
|
|
127
127
|
): Promise<void> {
|
|
128
|
-
|
|
128
|
+
// Re-read the environment: an opt-out set after import (for example a
|
|
129
|
+
// dotenv call below the imports) must still stop events.
|
|
130
|
+
if (this.telemetryDisabled || isTelemetryDisabled()) {
|
|
129
131
|
return;
|
|
130
132
|
}
|
|
131
133
|
|