@copilotkit/shared 1.71.2 → 1.72.1-canary.sp-longthread-perf
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/LICENSE +21 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +126 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/events.d.cts +33 -0
- package/dist/telemetry/events.d.cts.map +1 -1
- package/dist/telemetry/events.d.mts +33 -0
- package/dist/telemetry/events.d.mts.map +1 -1
- package/dist/telemetry/index.cjs +3 -0
- package/dist/telemetry/index.d.cts +2 -2
- package/dist/telemetry/index.d.mts +2 -2
- package/dist/telemetry/index.mjs +2 -2
- package/dist/telemetry/model-host.cjs +104 -0
- package/dist/telemetry/model-host.cjs.map +1 -0
- package/dist/telemetry/model-host.d.cts +26 -0
- package/dist/telemetry/model-host.d.cts.map +1 -0
- package/dist/telemetry/model-host.d.mts +26 -0
- package/dist/telemetry/model-host.d.mts.map +1 -0
- package/dist/telemetry/model-host.mjs +102 -0
- package/dist/telemetry/model-host.mjs.map +1 -0
- package/dist/telemetry/sampling.cjs +22 -0
- package/dist/telemetry/sampling.cjs.map +1 -1
- package/dist/telemetry/sampling.d.cts +22 -2
- package/dist/telemetry/sampling.d.cts.map +1 -1
- package/dist/telemetry/sampling.d.mts +22 -2
- package/dist/telemetry/sampling.d.mts.map +1 -1
- package/dist/telemetry/sampling.mjs +20 -1
- package/dist/telemetry/sampling.mjs.map +1 -1
- package/dist/telemetry/telemetry-client.cjs +18 -11
- package/dist/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/telemetry/telemetry-client.d.cts +3 -3
- package/dist/telemetry/telemetry-client.d.cts.map +1 -1
- package/dist/telemetry/telemetry-client.d.mts +3 -3
- package/dist/telemetry/telemetry-client.d.mts.map +1 -1
- package/dist/telemetry/telemetry-client.mjs +19 -12
- package/dist/telemetry/telemetry-client.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -0
- package/src/telemetry/events.ts +33 -0
- package/src/telemetry/model-host.test.ts +145 -0
- package/src/telemetry/model-host.ts +155 -0
- package/src/telemetry/sampling.ts +39 -6
- package/src/telemetry/telemetry-blank-license-identity.test.ts +11 -4
- package/src/telemetry/telemetry-client.test.ts +107 -39
- package/src/telemetry/telemetry-client.ts +48 -27
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
//#region src/telemetry/sampling.d.ts
|
|
2
|
+
/** The rate a sink that takes every event is "gated" at. */
|
|
3
|
+
declare const UNSAMPLED_RATE = 1;
|
|
2
4
|
/** Identifies which client emitted an event. */
|
|
3
5
|
declare const TELEMETRY_EMITTER_V1 = "v1-shared";
|
|
4
6
|
declare const TELEMETRY_EMITTER_V2 = "v2-runtime";
|
|
5
7
|
type TelemetryEmitter = typeof TELEMETRY_EMITTER_V1 | typeof TELEMETRY_EMITTER_V2;
|
|
8
|
+
/**
|
|
9
|
+
* Which public API surface the developer built against.
|
|
10
|
+
*
|
|
11
|
+
* Separate from {@link TelemetryEmitter} on purpose. The emitter names a
|
|
12
|
+
* client library, and there are six of them across four languages; the
|
|
13
|
+
* surface answers the question anyone actually asks of this data, which
|
|
14
|
+
* is how much traffic still arrives through the deprecated v1 API. The
|
|
15
|
+
* native runtimes (Go, Python, Ruby, .NET) expose v2 only, so they report
|
|
16
|
+
* `v2` without having a v1 notion of their own.
|
|
17
|
+
*/
|
|
18
|
+
declare const TELEMETRY_SURFACE_V1: "v1";
|
|
19
|
+
declare const TELEMETRY_SURFACE_V2: "v2";
|
|
20
|
+
type TelemetrySurface = typeof TELEMETRY_SURFACE_V1 | typeof TELEMETRY_SURFACE_V2;
|
|
6
21
|
/**
|
|
7
22
|
* Which wire an event copy travelled on. The v1 client sends each capture
|
|
8
23
|
* to both, so this is what tells the two copies apart downstream
|
|
@@ -10,7 +25,7 @@ type TelemetryEmitter = typeof TELEMETRY_EMITTER_V1 | typeof TELEMETRY_EMITTER_V
|
|
|
10
25
|
*/
|
|
11
26
|
type TelemetryTransport = "segment" | "lambda";
|
|
12
27
|
interface SamplingMeta {
|
|
13
|
-
/** The rate this event was actually gated at: 1 when identified. */
|
|
28
|
+
/** The rate this event was actually gated at: 1 when unsampled or identified. */
|
|
14
29
|
sampleRate: number;
|
|
15
30
|
sampleRateAdjustmentFactor: number;
|
|
16
31
|
/** Multiply by this to extrapolate the population the event stands for. */
|
|
@@ -24,6 +39,11 @@ interface SamplingMeta {
|
|
|
24
39
|
* `telemetryId` is the caller's parsed license telemetry_id, or null when
|
|
25
40
|
* anonymous — it decides the branch, and is deliberately not returned:
|
|
26
41
|
* only the non-PII shape below travels on the event.
|
|
42
|
+
*
|
|
43
|
+
* `sampleRate` is the rate of the wire this copy travels on, so an
|
|
44
|
+
* unsampled sink passes {@link UNSAMPLED_RATE}. Call it once per wire:
|
|
45
|
+
* the v1 client sends one capture to two wires gated differently, and a
|
|
46
|
+
* shared block would misweight one of them.
|
|
27
47
|
*/
|
|
28
48
|
declare function computeSamplingMeta({
|
|
29
49
|
telemetryId,
|
|
@@ -33,5 +53,5 @@ declare function computeSamplingMeta({
|
|
|
33
53
|
sampleRate: number;
|
|
34
54
|
}): SamplingMeta;
|
|
35
55
|
//#endregion
|
|
36
|
-
export { SamplingMeta, TELEMETRY_EMITTER_V1, TELEMETRY_EMITTER_V2, TelemetryEmitter, TelemetryTransport, computeSamplingMeta };
|
|
56
|
+
export { SamplingMeta, TELEMETRY_EMITTER_V1, TELEMETRY_EMITTER_V2, TELEMETRY_SURFACE_V1, TELEMETRY_SURFACE_V2, TelemetryEmitter, TelemetrySurface, TelemetryTransport, UNSAMPLED_RATE, computeSamplingMeta };
|
|
37
57
|
//# sourceMappingURL=sampling.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sampling.d.mts","names":[],"sources":["../../src/telemetry/sampling.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"sampling.d.mts","names":[],"sources":["../../src/telemetry/sampling.ts"],"mappings":";;cAoBa,cAAA;;cAGA,oBAAA;AAAA,cACA,oBAAA;AAAA,KAED,gBAAA,UACD,oBAAA,UACA,oBAAA;AALX;;;;;AACA;;;;;AADA,cAoBa,oBAAA;AAAA,cACA,oBAAA;AAAA,KAED,gBAAA,UACD,oBAAA,UACA,oBAAA;;;AALX;;;KAYY,kBAAA;AAAA,UAEK,YAAA;EAbJ;EAeX,UAAA;EACA,0BAAA;EAhB+C;EAkB/C,YAAA;EAhBU;EAkBV,oBAAA;AAAA;;;AATF;;;;;AAEA;;;;;iBAsBgB,mBAAA,CAAA;EACd,WAAA;EACA;AAAA;EAEA,WAAA;EACA,UAAA;AAAA,IACE,YAAA"}
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
//#region src/telemetry/sampling.ts
|
|
2
|
+
/** The rate a sink that takes every event is "gated" at. */
|
|
3
|
+
const UNSAMPLED_RATE = 1;
|
|
2
4
|
/** Identifies which client emitted an event. */
|
|
3
5
|
const TELEMETRY_EMITTER_V1 = "v1-shared";
|
|
4
6
|
const TELEMETRY_EMITTER_V2 = "v2-runtime";
|
|
5
7
|
/**
|
|
8
|
+
* Which public API surface the developer built against.
|
|
9
|
+
*
|
|
10
|
+
* Separate from {@link TelemetryEmitter} on purpose. The emitter names a
|
|
11
|
+
* client library, and there are six of them across four languages; the
|
|
12
|
+
* surface answers the question anyone actually asks of this data, which
|
|
13
|
+
* is how much traffic still arrives through the deprecated v1 API. The
|
|
14
|
+
* native runtimes (Go, Python, Ruby, .NET) expose v2 only, so they report
|
|
15
|
+
* `v2` without having a v1 notion of their own.
|
|
16
|
+
*/
|
|
17
|
+
const TELEMETRY_SURFACE_V1 = "v1";
|
|
18
|
+
const TELEMETRY_SURFACE_V2 = "v2";
|
|
19
|
+
/**
|
|
6
20
|
* Compute the sampling block for one captured event.
|
|
7
21
|
*
|
|
8
22
|
* `telemetryId` is the caller's parsed license telemetry_id, or null when
|
|
9
23
|
* anonymous — it decides the branch, and is deliberately not returned:
|
|
10
24
|
* only the non-PII shape below travels on the event.
|
|
25
|
+
*
|
|
26
|
+
* `sampleRate` is the rate of the wire this copy travels on, so an
|
|
27
|
+
* unsampled sink passes {@link UNSAMPLED_RATE}. Call it once per wire:
|
|
28
|
+
* the v1 client sends one capture to two wires gated differently, and a
|
|
29
|
+
* shared block would misweight one of them.
|
|
11
30
|
*/
|
|
12
31
|
function computeSamplingMeta({ telemetryId, sampleRate }) {
|
|
13
32
|
const identified = Boolean(telemetryId);
|
|
@@ -21,5 +40,5 @@ function computeSamplingMeta({ telemetryId, sampleRate }) {
|
|
|
21
40
|
}
|
|
22
41
|
|
|
23
42
|
//#endregion
|
|
24
|
-
export { TELEMETRY_EMITTER_V1, TELEMETRY_EMITTER_V2, computeSamplingMeta };
|
|
43
|
+
export { TELEMETRY_EMITTER_V1, TELEMETRY_EMITTER_V2, TELEMETRY_SURFACE_V1, TELEMETRY_SURFACE_V2, UNSAMPLED_RATE, computeSamplingMeta };
|
|
25
44
|
//# sourceMappingURL=sampling.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sampling.mjs","names":[],"sources":["../../src/telemetry/sampling.ts"],"sourcesContent":["// Per-event telemetry metadata shared by both TelemetryClients.\n//\n// Two clients emit `oss.runtime.*`: the v1 client in this package and the\n// v2 client in @copilotkit/runtime.
|
|
1
|
+
{"version":3,"file":"sampling.mjs","names":[],"sources":["../../src/telemetry/sampling.ts"],"sourcesContent":["// Per-event telemetry metadata shared by both TelemetryClients.\n//\n// Two clients emit `oss.runtime.*`: the v1 client in this package and the\n// v2 client in @copilotkit/runtime. Neither samples the lambda sink any\n// more — that sink is ours, so anonymous volume costs us nothing per\n// event and full fidelity beats an extrapolation. One wire is still\n// gated: the Segment copy the v1 client sends, which is billed per event.\n//\n// Every event therefore still carries a sampling block, because the two\n// populations remain distinguishable and a Segment-sourced count still\n// needs sum(sampleWeight) to mean anything. On the lambda wire the block\n// reads sampleRate=1/sampleWeight=1, which is the honest answer rather\n// than a placeholder.\n//\n// The two clients computed this independently and drifted: v2 sampled but\n// stamped nothing, leaving ~24% of runtime volume unweightable from the\n// data alone (OSS-1017). Computing it here is what stops them drifting\n// again.\n\n/** The rate a sink that takes every event is \"gated\" at. */\nexport const UNSAMPLED_RATE = 1;\n\n/** Identifies which client emitted an event. */\nexport const TELEMETRY_EMITTER_V1 = \"v1-shared\";\nexport const TELEMETRY_EMITTER_V2 = \"v2-runtime\";\n\nexport type TelemetryEmitter =\n | typeof TELEMETRY_EMITTER_V1\n | typeof TELEMETRY_EMITTER_V2;\n\n/**\n * Which public API surface the developer built against.\n *\n * Separate from {@link TelemetryEmitter} on purpose. The emitter names a\n * client library, and there are six of them across four languages; the\n * surface answers the question anyone actually asks of this data, which\n * is how much traffic still arrives through the deprecated v1 API. The\n * native runtimes (Go, Python, Ruby, .NET) expose v2 only, so they report\n * `v2` without having a v1 notion of their own.\n */\n// `as const` so the values keep their literal types when they land in a\n// mutable object property, which is how the v1 shim passes the surface\n// into the V2 runtime's options.\nexport const TELEMETRY_SURFACE_V1 = \"v1\" as const;\nexport const TELEMETRY_SURFACE_V2 = \"v2\" as const;\n\nexport type TelemetrySurface =\n | typeof TELEMETRY_SURFACE_V1\n | typeof TELEMETRY_SURFACE_V2;\n\n/**\n * Which wire an event copy travelled on. The v1 client sends each capture\n * to both, so this is what tells the two copies apart downstream\n * (OSS-1019); the v2 client only ever sends to the lambda sink.\n */\nexport type TelemetryTransport = \"segment\" | \"lambda\";\n\nexport interface SamplingMeta {\n /** The rate this event was actually gated at: 1 when unsampled or identified. */\n sampleRate: number;\n sampleRateAdjustmentFactor: number;\n /** Multiply by this to extrapolate the population the event stands for. */\n sampleWeight: number;\n /** Whether the event bypassed the sample gate. */\n telemetry_identified: boolean;\n}\n\n/**\n * Compute the sampling block for one captured event.\n *\n * `telemetryId` is the caller's parsed license telemetry_id, or null when\n * anonymous — it decides the branch, and is deliberately not returned:\n * only the non-PII shape below travels on the event.\n *\n * `sampleRate` is the rate of the wire this copy travels on, so an\n * unsampled sink passes {@link UNSAMPLED_RATE}. Call it once per wire:\n * the v1 client sends one capture to two wires gated differently, and a\n * shared block would misweight one of them.\n */\nexport function computeSamplingMeta({\n telemetryId,\n sampleRate,\n}: {\n telemetryId: string | null;\n sampleRate: number;\n}): SamplingMeta {\n const identified = Boolean(telemetryId);\n // Identified events ship at a 100% effective rate, anonymous ones at\n // sampleRate. Computed per event because a single global weight would\n // overweight identified-customer counts by 1/sampleRate.\n const effectiveSampleRate = identified ? 1 : sampleRate;\n\n return {\n sampleRate: effectiveSampleRate,\n sampleRateAdjustmentFactor: 1 - effectiveSampleRate,\n sampleWeight: 1 / effectiveSampleRate,\n // Stated outright rather than inferred from sampleWeight === 1:\n // under COPILOTKIT_TELEMETRY_SAMPLE_RATE=1 anonymous events also\n // weigh 1, and the two populations stop being distinguishable\n // (OSS-1018).\n telemetry_identified: identified,\n };\n}\n"],"mappings":";;AAoBA,MAAa,iBAAiB;;AAG9B,MAAa,uBAAuB;AACpC,MAAa,uBAAuB;;;;;;;;;;;AAmBpC,MAAa,uBAAuB;AACpC,MAAa,uBAAuB;;;;;;;;;;;;;AAmCpC,SAAgB,oBAAoB,EAClC,aACA,cAIe;CACf,MAAM,aAAa,QAAQ,YAAY;CAIvC,MAAM,sBAAsB,aAAa,IAAI;AAE7C,QAAO;EACL,YAAY;EACZ,4BAA4B,IAAI;EAChC,cAAc,IAAI;EAKlB,sBAAsB;EACvB"}
|
|
@@ -15,7 +15,7 @@ var TelemetryClient = class {
|
|
|
15
15
|
this.telemetryId = null;
|
|
16
16
|
this.licenseTelemetryId = null;
|
|
17
17
|
this.telemetryDisabled = false;
|
|
18
|
-
this.
|
|
18
|
+
this.segmentSampleRate = .05;
|
|
19
19
|
this.anonymousId = `anon_${(0, uuid.v4)()}`;
|
|
20
20
|
this.packageName = packageName;
|
|
21
21
|
this.packageVersion = packageVersion;
|
|
@@ -28,8 +28,8 @@ var TelemetryClient = class {
|
|
|
28
28
|
"copilotkit.package.version": packageVersion
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
return Math.random() < this.
|
|
31
|
+
shouldSendToSegment() {
|
|
32
|
+
return Math.random() < this.segmentSampleRate;
|
|
33
33
|
}
|
|
34
34
|
async capture(event, properties) {
|
|
35
35
|
return this.captureWithIdentity(event, properties, {
|
|
@@ -40,19 +40,25 @@ var TelemetryClient = class {
|
|
|
40
40
|
}
|
|
41
41
|
async captureWithIdentity(event, properties, identity) {
|
|
42
42
|
if (this.telemetryDisabled) return;
|
|
43
|
-
|
|
43
|
+
const sendToSegment = Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
|
|
44
|
+
const lambdaSamplingMeta = require_sampling.computeSamplingMeta({
|
|
45
|
+
telemetryId: identity.licenseTelemetryId,
|
|
46
|
+
sampleRate: require_sampling.UNSAMPLED_RATE
|
|
47
|
+
});
|
|
48
|
+
const segmentSamplingMeta = require_sampling.computeSamplingMeta({
|
|
49
|
+
telemetryId: identity.licenseTelemetryId,
|
|
50
|
+
sampleRate: this.segmentSampleRate
|
|
51
|
+
});
|
|
44
52
|
const eventMeta = {
|
|
45
|
-
...require_sampling.computeSamplingMeta({
|
|
46
|
-
telemetryId: identity.licenseTelemetryId,
|
|
47
|
-
sampleRate: this.sampleRate
|
|
48
|
-
}),
|
|
49
53
|
telemetry_emitter: require_sampling.TELEMETRY_EMITTER_V1,
|
|
54
|
+
telemetry_surface: require_sampling.TELEMETRY_SURFACE_V1,
|
|
50
55
|
telemetry_event_id: (0, uuid.v4)()
|
|
51
56
|
};
|
|
52
57
|
const flattenedProperties = require_utils.flattenObject(properties);
|
|
53
58
|
const propertiesWithGlobal = {
|
|
54
59
|
...this.globalProperties,
|
|
55
60
|
...eventMeta,
|
|
61
|
+
...segmentSamplingMeta,
|
|
56
62
|
telemetry_transport: "segment",
|
|
57
63
|
...flattenedProperties
|
|
58
64
|
};
|
|
@@ -66,6 +72,7 @@ var TelemetryClient = class {
|
|
|
66
72
|
globalProperties: {
|
|
67
73
|
...this.globalProperties,
|
|
68
74
|
...eventMeta,
|
|
75
|
+
...lambdaSamplingMeta,
|
|
69
76
|
telemetry_transport: "lambda"
|
|
70
77
|
},
|
|
71
78
|
packageName: this.packageName,
|
|
@@ -73,7 +80,7 @@ var TelemetryClient = class {
|
|
|
73
80
|
telemetryId: identity.telemetryId ?? void 0,
|
|
74
81
|
licenseToken: identity.licenseToken ?? void 0
|
|
75
82
|
});
|
|
76
|
-
if (this.segment) this.segment.track({
|
|
83
|
+
if (this.segment && sendToSegment) this.segment.track({
|
|
77
84
|
anonymousId: this.anonymousId,
|
|
78
85
|
event,
|
|
79
86
|
properties: { ...orderedPropertiesWithGlobal }
|
|
@@ -120,7 +127,7 @@ var TelemetryClient = class {
|
|
|
120
127
|
* Create an immutable capture scope for one runtime.
|
|
121
128
|
*
|
|
122
129
|
* The scope shares this client's sinks, process-wide opt-out, global
|
|
123
|
-
* properties, and sampling
|
|
130
|
+
* properties, and Segment sampling rate, but snapshots transport identity and
|
|
124
131
|
* license-derived sampling authority. Constructing another runtime cannot
|
|
125
132
|
* rewrite an existing scope.
|
|
126
133
|
*
|
|
@@ -149,7 +156,7 @@ var TelemetryClient = class {
|
|
|
149
156
|
_sampleRate = sampleRate ?? .05;
|
|
150
157
|
if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);
|
|
151
158
|
if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) throw new Error("Sample rate must be between 0 and 1");
|
|
152
|
-
this.
|
|
159
|
+
this.segmentSampleRate = _sampleRate;
|
|
153
160
|
}
|
|
154
161
|
};
|
|
155
162
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.cjs","names":["isTelemetryDisabled","Analytics","computeSamplingMeta","TELEMETRY_EMITTER_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 { computeSamplingMeta, TELEMETRY_EMITTER_V1 } 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 sampleRate.\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. Identified events\n // (those whose license token yielded a telemetry_id) bypass the gate\n // entirely. Applied uniformly to both the lambda sink and Segment —\n // one dice roll per capture, both sinks see the same decision.\n private sampleRate: 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 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 shouldSendEvent() {\n const randomNumber = Math.random();\n return randomNumber < this.sampleRate;\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 // Callers without license-derived sampling authority are gated by\n // sampleRate. Legacy license tokens with telemetry_id always send —\n // the volume is bounded by paying-customer count and full fidelity\n // per identified customer is worth the marginal cost.\n if (!identity.licenseTelemetryId && !this.shouldSendEvent()) {\n return;\n }\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 const samplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.sampleRate,\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 ...samplingMeta,\n telemetry_emitter: TELEMETRY_EMITTER_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 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 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) {\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 sampling settings, 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 event with no signal — especially important\n // since the default is now 0.05, making env-var overrides more common.\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.sampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling. Only license-\n // authorized events get effectiveSampleRate=1; standalone transport\n // identity stays in the sampled population.\n }\n}\n"],"mappings":";;;;;;;;;AAkCA,IAAa,kBAAb,MAA6B;CAyB3B,YAAY,EACV,aACA,gBACA,mBACA,kBACA,cAOC;0BAnCqC,EAAE;4BAC6B;sBAIjC;qBAID;4BAGO;2BAGP;oBAKR;qBACP,sBAAgB;AAepC,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,kBAAkB;AAExB,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;AAOF,MAAI,CAAC,SAAS,sBAAsB,CAAC,KAAK,iBAAiB,CACzD;EAgBF,MAAM,YAAY;GAChB,GAZmBC,qCAAoB;IACvC,aAAa,SAAS;IACtB,YAAY,KAAK;IAClB,CAAC;GAUA,mBAAmBC;GACnB,kCAA4B;GAC7B;EAED,MAAM,sBAAsBC,4BAAc,WAAW;EACrD,MAAM,uBAA4C;GAChD,GAAG,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,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;AAOxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,aAAa"}
|
|
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;0B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C;AAMxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,oBAAoB"}
|
|
@@ -25,7 +25,7 @@ declare class TelemetryClient {
|
|
|
25
25
|
packageName: string;
|
|
26
26
|
packageVersion: string;
|
|
27
27
|
private telemetryDisabled;
|
|
28
|
-
private
|
|
28
|
+
private segmentSampleRate;
|
|
29
29
|
private anonymousId;
|
|
30
30
|
constructor({
|
|
31
31
|
packageName,
|
|
@@ -40,7 +40,7 @@ declare class TelemetryClient {
|
|
|
40
40
|
telemetryBaseUrl?: string;
|
|
41
41
|
sampleRate?: number;
|
|
42
42
|
});
|
|
43
|
-
private
|
|
43
|
+
private shouldSendToSegment;
|
|
44
44
|
capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]): Promise<void>;
|
|
45
45
|
private captureWithIdentity;
|
|
46
46
|
setGlobalProperties(properties: Record<string, any>): void;
|
|
@@ -71,7 +71,7 @@ declare class TelemetryClient {
|
|
|
71
71
|
* Create an immutable capture scope for one runtime.
|
|
72
72
|
*
|
|
73
73
|
* The scope shares this client's sinks, process-wide opt-out, global
|
|
74
|
-
* properties, and sampling
|
|
74
|
+
* properties, and Segment sampling rate, but snapshots transport identity and
|
|
75
75
|
* license-derived sampling authority. Constructing another runtime cannot
|
|
76
76
|
* rewrite an existing scope.
|
|
77
77
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.d.cts","names":[],"sources":["../../src/telemetry/telemetry-client.ts"],"mappings":";;;;;;
|
|
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;EAwFd,mBAAA,CAAoB,UAAA,EAAY,MAAA;EAQhC,qBAAA,CAAsB,UAAA;IAAc,YAAA;IAAsB,OAAA;EAAA;EAzG5C;;;;;;;;;EA6Hd,oBAAA,CAAqB,QAAA;IACnB,WAAA;IACA,YAAA;EAAA;EAtMoB;;;;;EAmNtB,eAAA,CAAgB,YAAA;EArMhB;;;;;;;;;;;EAoNA,WAAA,CAAY,QAAA,EAAU,iBAAA,GAAoB,gBAAA;EAAA,QAWlC,wBAAA;EAAA,QAqBA,aAAA;AAAA"}
|
|
@@ -25,7 +25,7 @@ declare class TelemetryClient {
|
|
|
25
25
|
packageName: string;
|
|
26
26
|
packageVersion: string;
|
|
27
27
|
private telemetryDisabled;
|
|
28
|
-
private
|
|
28
|
+
private segmentSampleRate;
|
|
29
29
|
private anonymousId;
|
|
30
30
|
constructor({
|
|
31
31
|
packageName,
|
|
@@ -40,7 +40,7 @@ declare class TelemetryClient {
|
|
|
40
40
|
telemetryBaseUrl?: string;
|
|
41
41
|
sampleRate?: number;
|
|
42
42
|
});
|
|
43
|
-
private
|
|
43
|
+
private shouldSendToSegment;
|
|
44
44
|
capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]): Promise<void>;
|
|
45
45
|
private captureWithIdentity;
|
|
46
46
|
setGlobalProperties(properties: Record<string, any>): void;
|
|
@@ -71,7 +71,7 @@ declare class TelemetryClient {
|
|
|
71
71
|
* Create an immutable capture scope for one runtime.
|
|
72
72
|
*
|
|
73
73
|
* The scope shares this client's sinks, process-wide opt-out, global
|
|
74
|
-
* properties, and sampling
|
|
74
|
+
* properties, and Segment sampling rate, but snapshots transport identity and
|
|
75
75
|
* license-derived sampling authority. Constructing another runtime cannot
|
|
76
76
|
* rewrite an existing scope.
|
|
77
77
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry-client.d.mts","names":[],"sources":["../../src/telemetry/telemetry-client.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"telemetry-client.d.mts","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;EAwFd,mBAAA,CAAoB,UAAA,EAAY,MAAA;EAQhC,qBAAA,CAAsB,UAAA;IAAc,YAAA;IAAsB,OAAA;EAAA;EAzG5C;;;;;;;;;EA6Hd,oBAAA,CAAqB,QAAA;IACnB,WAAA;IACA,YAAA;EAAA;EAtMoB;;;;;EAmNtB,eAAA,CAAgB,YAAA;EArMhB;;;;;;;;;;;EAoNA,WAAA,CAAY,QAAA,EAAU,iBAAA,GAAoB,gBAAA;EAAA,QAWlC,wBAAA;EAAA,QAqBA,aAAA;AAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isTelemetryDisabled } from "./telemetry-disabled.mjs";
|
|
2
|
-
import { TELEMETRY_EMITTER_V1, computeSamplingMeta } from "./sampling.mjs";
|
|
2
|
+
import { TELEMETRY_EMITTER_V1, TELEMETRY_SURFACE_V1, UNSAMPLED_RATE, computeSamplingMeta } from "./sampling.mjs";
|
|
3
3
|
import { firstNonBlankTelemetryId, lambdaClient, parseAndWarnTelemetryId } from "./lambda-client.mjs";
|
|
4
4
|
import { flattenObject } from "./utils.mjs";
|
|
5
5
|
import { v4 } from "uuid";
|
|
@@ -14,7 +14,7 @@ var TelemetryClient = class {
|
|
|
14
14
|
this.telemetryId = null;
|
|
15
15
|
this.licenseTelemetryId = null;
|
|
16
16
|
this.telemetryDisabled = false;
|
|
17
|
-
this.
|
|
17
|
+
this.segmentSampleRate = .05;
|
|
18
18
|
this.anonymousId = `anon_${v4()}`;
|
|
19
19
|
this.packageName = packageName;
|
|
20
20
|
this.packageVersion = packageVersion;
|
|
@@ -27,8 +27,8 @@ var TelemetryClient = class {
|
|
|
27
27
|
"copilotkit.package.version": packageVersion
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
return Math.random() < this.
|
|
30
|
+
shouldSendToSegment() {
|
|
31
|
+
return Math.random() < this.segmentSampleRate;
|
|
32
32
|
}
|
|
33
33
|
async capture(event, properties) {
|
|
34
34
|
return this.captureWithIdentity(event, properties, {
|
|
@@ -39,19 +39,25 @@ var TelemetryClient = class {
|
|
|
39
39
|
}
|
|
40
40
|
async captureWithIdentity(event, properties, identity) {
|
|
41
41
|
if (this.telemetryDisabled) return;
|
|
42
|
-
|
|
42
|
+
const sendToSegment = Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
|
|
43
|
+
const lambdaSamplingMeta = computeSamplingMeta({
|
|
44
|
+
telemetryId: identity.licenseTelemetryId,
|
|
45
|
+
sampleRate: UNSAMPLED_RATE
|
|
46
|
+
});
|
|
47
|
+
const segmentSamplingMeta = computeSamplingMeta({
|
|
48
|
+
telemetryId: identity.licenseTelemetryId,
|
|
49
|
+
sampleRate: this.segmentSampleRate
|
|
50
|
+
});
|
|
43
51
|
const eventMeta = {
|
|
44
|
-
...computeSamplingMeta({
|
|
45
|
-
telemetryId: identity.licenseTelemetryId,
|
|
46
|
-
sampleRate: this.sampleRate
|
|
47
|
-
}),
|
|
48
52
|
telemetry_emitter: TELEMETRY_EMITTER_V1,
|
|
53
|
+
telemetry_surface: TELEMETRY_SURFACE_V1,
|
|
49
54
|
telemetry_event_id: v4()
|
|
50
55
|
};
|
|
51
56
|
const flattenedProperties = flattenObject(properties);
|
|
52
57
|
const propertiesWithGlobal = {
|
|
53
58
|
...this.globalProperties,
|
|
54
59
|
...eventMeta,
|
|
60
|
+
...segmentSamplingMeta,
|
|
55
61
|
telemetry_transport: "segment",
|
|
56
62
|
...flattenedProperties
|
|
57
63
|
};
|
|
@@ -65,6 +71,7 @@ var TelemetryClient = class {
|
|
|
65
71
|
globalProperties: {
|
|
66
72
|
...this.globalProperties,
|
|
67
73
|
...eventMeta,
|
|
74
|
+
...lambdaSamplingMeta,
|
|
68
75
|
telemetry_transport: "lambda"
|
|
69
76
|
},
|
|
70
77
|
packageName: this.packageName,
|
|
@@ -72,7 +79,7 @@ var TelemetryClient = class {
|
|
|
72
79
|
telemetryId: identity.telemetryId ?? void 0,
|
|
73
80
|
licenseToken: identity.licenseToken ?? void 0
|
|
74
81
|
});
|
|
75
|
-
if (this.segment) this.segment.track({
|
|
82
|
+
if (this.segment && sendToSegment) this.segment.track({
|
|
76
83
|
anonymousId: this.anonymousId,
|
|
77
84
|
event,
|
|
78
85
|
properties: { ...orderedPropertiesWithGlobal }
|
|
@@ -119,7 +126,7 @@ var TelemetryClient = class {
|
|
|
119
126
|
* Create an immutable capture scope for one runtime.
|
|
120
127
|
*
|
|
121
128
|
* The scope shares this client's sinks, process-wide opt-out, global
|
|
122
|
-
* properties, and sampling
|
|
129
|
+
* properties, and Segment sampling rate, but snapshots transport identity and
|
|
123
130
|
* license-derived sampling authority. Constructing another runtime cannot
|
|
124
131
|
* rewrite an existing scope.
|
|
125
132
|
*
|
|
@@ -148,7 +155,7 @@ var TelemetryClient = class {
|
|
|
148
155
|
_sampleRate = sampleRate ?? .05;
|
|
149
156
|
if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);
|
|
150
157
|
if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) throw new Error("Sample rate must be between 0 and 1");
|
|
151
|
-
this.
|
|
158
|
+
this.segmentSampleRate = _sampleRate;
|
|
152
159
|
}
|
|
153
160
|
};
|
|
154
161
|
|
|
@@ -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 { computeSamplingMeta, TELEMETRY_EMITTER_V1 } 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 sampleRate.\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. Identified events\n // (those whose license token yielded a telemetry_id) bypass the gate\n // entirely. Applied uniformly to both the lambda sink and Segment —\n // one dice roll per capture, both sinks see the same decision.\n private sampleRate: 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 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 shouldSendEvent() {\n const randomNumber = Math.random();\n return randomNumber < this.sampleRate;\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 // Callers without license-derived sampling authority are gated by\n // sampleRate. Legacy license tokens with telemetry_id always send —\n // the volume is bounded by paying-customer count and full fidelity\n // per identified customer is worth the marginal cost.\n if (!identity.licenseTelemetryId && !this.shouldSendEvent()) {\n return;\n }\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 const samplingMeta = computeSamplingMeta({\n telemetryId: identity.licenseTelemetryId,\n sampleRate: this.sampleRate,\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 ...samplingMeta,\n telemetry_emitter: TELEMETRY_EMITTER_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 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 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) {\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 sampling settings, 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 event with no signal — especially important\n // since the default is now 0.05, making env-var overrides more common.\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.sampleRate = _sampleRate;\n // Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/\n // sampleWeight) is computed per capture() in ./sampling. Only license-\n // authorized events get effectiveSampleRate=1; standalone transport\n // identity stays in the sampled population.\n }\n}\n"],"mappings":";;;;;;;;AAkCA,IAAa,kBAAb,MAA6B;CAyB3B,YAAY,EACV,aACA,gBACA,mBACA,kBACA,cAOC;0BAnCqC,EAAE;4BAC6B;sBAIjC;qBAID;4BAGO;2BAGP;oBAKR;qBACP,QAAQA,IAAQ;AAepC,OAAK,cAAc;AACnB,OAAK,iBAAiB;AACtB,O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oBAAoB,YAAiC;EACnD,MAAM,sBAAsB,cAAc,WAAW;AACrD,OAAK,mBAAmB;GACtB,GAAG,KAAK;GACR,GAAG;GACJ;;CAGH,sBAAsB,YAAuD;AAC3E,OAAK,qBAAqB;AAE1B,OAAK,oBAAoB,EACvB,OAAO;GACL,cAAc,WAAW;GACzB,SAAS,WAAW;GACrB,EACF,CAAC;;;;;;;;;;;CAYJ,qBAAqB,UAGZ;EACP,MAAM,mBAAmB,KAAK,yBAAyB,SAAS;AAChE,OAAK,cAAc,iBAAiB;AACpC,OAAK,eAAe,iBAAiB;AACrC,OAAK,qBAAqB,iBAAiB;;;;;;;CAQ7C,gBAAgB,cAAsB;AACpC,OAAK,qBAAqB,EAAE,cAAc,CAAC;;;;;;;;;;;;;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,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;AAOxE,MAAI,OAAO,MAAM,YAAY,IAAI,cAAc,KAAK,cAAc,EAChE,OAAM,IAAI,MAAM,sCAAsC;AAGxD,OAAK,aAAa"}
|
|
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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AiB;;;;;;;CAQ7C,gBAAgB,cAAsB;AACpC,OAAK,qBAAqB,EAAE,cAAc,CAAC;;;;;;;;;;;;;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,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"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -13,6 +13,9 @@ export * from "./constants";
|
|
|
13
13
|
// This mirrors the license-verifier note below.
|
|
14
14
|
export { isTelemetryDisabled } from "./telemetry/telemetry-disabled";
|
|
15
15
|
export * from "./telemetry/sampling";
|
|
16
|
+
// Pure string classification with no imports, so it carries no runtime edge
|
|
17
|
+
// into browser builds — same reasoning as `sampling` above.
|
|
18
|
+
export * from "./telemetry/model-host";
|
|
16
19
|
export {
|
|
17
20
|
firstNonBlankTelemetryId,
|
|
18
21
|
lambdaClient,
|
package/src/telemetry/events.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ModelHostClass } from "./model-host";
|
|
2
|
+
|
|
1
3
|
export type AnalyticsEvents = {
|
|
2
4
|
"oss.runtime.instance_created": RuntimeInstanceCreatedInfo;
|
|
3
5
|
"oss.runtime.copilot_request_created": {
|
|
@@ -13,8 +15,29 @@ export type AnalyticsEvents = {
|
|
|
13
15
|
hashedLgcKey?: string;
|
|
14
16
|
error?: string;
|
|
15
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* A managed Channel lost its gateway link. Carries only the cause we already
|
|
20
|
+
* compute for the log line — never the Channel name, which is a
|
|
21
|
+
* customer-chosen identifier, and never message content.
|
|
22
|
+
*/
|
|
23
|
+
"oss.runtime.channel_session_dropped": ChannelSessionDroppedInfo;
|
|
24
|
+
/** A managed Channel's gateway link came back, and how long it was gone. */
|
|
25
|
+
"oss.runtime.channel_session_recovered": { downForMs: number };
|
|
16
26
|
};
|
|
17
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Kept identical to the v2 runtime's catalog in
|
|
30
|
+
* `packages/runtime/src/v2/runtime/telemetry/events.ts`. The v1 entrypoint
|
|
31
|
+
* hands its capture scope to the V2 runtime it builds, so that scope has to
|
|
32
|
+
* accept every event the V2 runtime can send, channel events included.
|
|
33
|
+
*/
|
|
34
|
+
export interface ChannelSessionDroppedInfo {
|
|
35
|
+
/** Diagnosis of the drop, e.g. `the gateway host answered HTTP 502`. */
|
|
36
|
+
reason?: string;
|
|
37
|
+
/** Transport/OS code when the transport named one, e.g. `ECONNRESET`. */
|
|
38
|
+
code?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
18
41
|
export interface RuntimeInstanceCreatedInfo {
|
|
19
42
|
actionsAmount: number;
|
|
20
43
|
endpointTypes: string[];
|
|
@@ -27,6 +50,16 @@ export interface RuntimeInstanceCreatedInfo {
|
|
|
27
50
|
}
|
|
28
51
|
|
|
29
52
|
export interface AgentExecutionResponseInfo {
|
|
53
|
+
/**
|
|
54
|
+
* Which vendor the runtime actually sent the completion to, as a closed
|
|
55
|
+
* vocabulary — see `classifyModelHost`. Distinct from `provider` below,
|
|
56
|
+
* which the upstream reports as a model name and which cannot tell Azure,
|
|
57
|
+
* OpenRouter or a local server apart from OpenAI.
|
|
58
|
+
*
|
|
59
|
+
* `unknown` when the developer handed us an already-built model, because
|
|
60
|
+
* the endpoint is unrecoverable from one.
|
|
61
|
+
*/
|
|
62
|
+
llmHostClass?: ModelHostClass;
|
|
30
63
|
provider?: string;
|
|
31
64
|
model?: string;
|
|
32
65
|
langGraphHost?: string;
|