@copilotkit/shared 2.0.0-rc.1 → 2.0.0-rc.10
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/README.md +3 -14
- package/dist/a2ui-prompts.cjs +21 -62
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +8 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +8 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +22 -62
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +1 -1
- package/dist/generative-ui/basic-functions.cjs.map +1 -1
- package/dist/generative-ui/basic-functions.mjs +1 -1
- package/dist/generative-ui/basic-functions.mjs.map +1 -1
- package/dist/generative-ui/compact-schema.cjs +187 -0
- package/dist/generative-ui/compact-schema.cjs.map +1 -0
- package/dist/generative-ui/compact-schema.d.cts +55 -0
- package/dist/generative-ui/compact-schema.d.cts.map +1 -0
- package/dist/generative-ui/compact-schema.d.mts +55 -0
- package/dist/generative-ui/compact-schema.d.mts.map +1 -0
- package/dist/generative-ui/compact-schema.mjs +185 -0
- package/dist/generative-ui/compact-schema.mjs.map +1 -0
- package/dist/generative-ui/prompt-spec.cjs +225 -0
- package/dist/generative-ui/prompt-spec.cjs.map +1 -0
- package/dist/generative-ui/prompt-spec.d.cts +94 -0
- package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
- package/dist/generative-ui/prompt-spec.d.mts +94 -0
- package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
- package/dist/generative-ui/prompt-spec.mjs +220 -0
- package/dist/generative-ui/prompt-spec.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +10 -36
- package/dist/generative-ui/prompt.cjs.map +1 -1
- package/dist/generative-ui/prompt.d.cts +2 -1
- package/dist/generative-ui/prompt.d.cts.map +1 -1
- package/dist/generative-ui/prompt.d.mts +2 -1
- package/dist/generative-ui/prompt.d.mts.map +1 -1
- package/dist/generative-ui/prompt.mjs +12 -38
- package/dist/generative-ui/prompt.mjs.map +1 -1
- package/dist/generative-ui/schema-render.cjs +242 -0
- package/dist/generative-ui/schema-render.cjs.map +1 -0
- package/dist/generative-ui/schema-render.d.cts +44 -0
- package/dist/generative-ui/schema-render.d.cts.map +1 -0
- package/dist/generative-ui/schema-render.d.mts +44 -0
- package/dist/generative-ui/schema-render.d.mts.map +1 -0
- package/dist/generative-ui/schema-render.mjs +239 -0
- package/dist/generative-ui/schema-render.mjs.map +1 -0
- package/dist/index.cjs +17 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -9
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +10 -9
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -7
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +808 -438
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/standard-schema.cjs +20 -7
- package/dist/standard-schema.cjs.map +1 -1
- package/dist/standard-schema.d.cts +15 -16
- package/dist/standard-schema.d.cts.map +1 -1
- package/dist/standard-schema.d.mts +15 -16
- package/dist/standard-schema.d.mts.map +1 -1
- package/dist/standard-schema.mjs +20 -8
- package/dist/standard-schema.mjs.map +1 -1
- package/dist/telemetry/lambda-client.cjs.map +1 -1
- package/dist/telemetry/lambda-client.d.cts.map +1 -1
- package/dist/telemetry/lambda-client.d.mts.map +1 -1
- package/dist/telemetry/lambda-client.mjs.map +1 -1
- package/dist/telemetry/telemetry-disabled.cjs +3 -6
- package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
- package/dist/telemetry/telemetry-disabled.d.cts +0 -5
- package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
- package/dist/telemetry/telemetry-disabled.d.mts +0 -5
- package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
- package/dist/telemetry/telemetry-disabled.mjs +4 -6
- package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
- package/dist/types/message.d.cts +1 -18
- package/dist/types/message.d.cts.map +1 -1
- package/dist/types/message.d.mts +1 -18
- package/dist/types/message.d.mts.map +1 -1
- package/dist/utils/errors.cjs +4 -42
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.d.cts +16 -34
- package/dist/utils/errors.d.cts.map +1 -1
- package/dist/utils/errors.d.mts +16 -34
- package/dist/utils/errors.d.mts.map +1 -1
- package/dist/utils/errors.mjs +5 -41
- package/dist/utils/errors.mjs.map +1 -1
- package/dist/utils/index.cjs +1 -2
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +4 -5
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +4 -5
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +4 -5
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/json-schema.cjs +0 -79
- package/dist/utils/json-schema.cjs.map +1 -1
- package/dist/utils/json-schema.d.cts +1 -4
- package/dist/utils/json-schema.d.cts.map +1 -1
- package/dist/utils/json-schema.d.mts +1 -4
- package/dist/utils/json-schema.d.mts.map +1 -1
- package/dist/utils/json-schema.mjs +1 -78
- package/dist/utils/json-schema.mjs.map +1 -1
- package/dist/utils/process-env.cjs +14 -0
- package/dist/utils/process-env.cjs.map +1 -0
- package/dist/utils/process-env.d.cts +10 -0
- package/dist/utils/process-env.d.cts.map +1 -0
- package/dist/utils/process-env.d.mts +10 -0
- package/dist/utils/process-env.d.mts.map +1 -0
- package/dist/utils/process-env.mjs +13 -0
- package/dist/utils/process-env.mjs.map +1 -0
- package/dist/utils/random-id.cjs +13 -26
- package/dist/utils/random-id.cjs.map +1 -1
- package/dist/utils/random-id.d.cts +6 -3
- package/dist/utils/random-id.d.cts.map +1 -1
- package/dist/utils/random-id.d.mts +6 -3
- package/dist/utils/random-id.d.mts.map +1 -1
- package/dist/utils/random-id.mjs +14 -25
- package/dist/utils/random-id.mjs.map +1 -1
- package/package.json +7 -31
- package/src/__tests__/a2ui-prompts.test.ts +42 -9
- package/src/__tests__/fixtures/prompt-goldens.json +354 -0
- package/src/__tests__/prompt-spec.test.ts +191 -0
- package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
- package/src/__tests__/schema-render.test.ts +245 -0
- package/src/__tests__/standard-schema.test.ts +61 -101
- package/src/a2ui-prompts.ts +47 -96
- package/src/generative-ui/basic-functions.ts +1 -1
- package/src/generative-ui/compact-schema.ts +277 -0
- package/src/generative-ui/index.ts +3 -0
- package/src/generative-ui/prompt-spec.ts +335 -0
- package/src/generative-ui/prompt.ts +25 -75
- package/src/generative-ui/schema-render.ts +374 -0
- package/src/index.ts +3 -16
- package/src/standard-schema.ts +24 -27
- package/src/telemetry/lambda-client.ts +3 -2
- package/src/telemetry/telemetry-disabled.ts +7 -12
- package/src/types/message.ts +1 -19
- package/src/utils/__tests__/errors.test.ts +44 -0
- package/src/utils/__tests__/json-schema.test.ts +3 -479
- package/src/utils/__tests__/process-env.test.ts +22 -0
- package/src/utils/__tests__/random-id.test.ts +37 -0
- package/src/utils/errors.ts +27 -85
- package/src/utils/index.ts +1 -2
- package/src/utils/json-schema.ts +1 -152
- package/src/utils/process-env.ts +14 -0
- package/src/utils/random-id.ts +17 -37
- package/tsdown.config.ts +2 -19
- package/dist/event-transforms/index.cjs +0 -15
- package/dist/event-transforms/index.d.cts +0 -4
- package/dist/event-transforms/index.d.mts +0 -4
- package/dist/event-transforms/index.mjs +0 -5
- package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
- package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
- package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
- package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
- package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
- package/dist/event-transforms/recorded-events.cjs +0 -59
- package/dist/event-transforms/recorded-events.cjs.map +0 -1
- package/dist/event-transforms/recorded-events.d.cts +0 -23
- package/dist/event-transforms/recorded-events.d.cts.map +0 -1
- package/dist/event-transforms/recorded-events.d.mts +0 -23
- package/dist/event-transforms/recorded-events.d.mts.map +0 -1
- package/dist/event-transforms/recorded-events.mjs +0 -58
- package/dist/event-transforms/recorded-events.mjs.map +0 -1
- package/dist/telemetry/events.d.cts +0 -72
- package/dist/telemetry/events.d.cts.map +0 -1
- package/dist/telemetry/events.d.mts +0 -72
- package/dist/telemetry/events.d.mts.map +0 -1
- package/dist/telemetry/index.cjs +0 -18
- package/dist/telemetry/index.d.cts +0 -5
- package/dist/telemetry/index.d.mts +0 -5
- package/dist/telemetry/index.mjs +0 -6
- package/dist/telemetry/telemetry-client.cjs +0 -165
- package/dist/telemetry/telemetry-client.cjs.map +0 -1
- package/dist/telemetry/telemetry-client.d.cts +0 -87
- package/dist/telemetry/telemetry-client.d.cts.map +0 -1
- package/dist/telemetry/telemetry-client.d.mts +0 -87
- package/dist/telemetry/telemetry-client.d.mts.map +0 -1
- package/dist/telemetry/telemetry-client.mjs +0 -164
- package/dist/telemetry/telemetry-client.mjs.map +0 -1
- package/dist/telemetry/utils.cjs +0 -14
- package/dist/telemetry/utils.cjs.map +0 -1
- package/dist/telemetry/utils.mjs +0 -13
- package/dist/telemetry/utils.mjs.map +0 -1
- package/dist/utils/conditions.cjs +0 -32
- package/dist/utils/conditions.cjs.map +0 -1
- package/dist/utils/conditions.d.cts +0 -31
- package/dist/utils/conditions.d.cts.map +0 -1
- package/dist/utils/conditions.d.mts +0 -31
- package/dist/utils/conditions.d.mts.map +0 -1
- package/dist/utils/conditions.mjs +0 -31
- package/dist/utils/conditions.mjs.map +0 -1
- package/dist/utils/console-styling.cjs +0 -91
- package/dist/utils/console-styling.cjs.map +0 -1
- package/dist/utils/console-styling.d.cts +0 -65
- package/dist/utils/console-styling.d.cts.map +0 -1
- package/dist/utils/console-styling.d.mts +0 -65
- package/dist/utils/console-styling.d.mts.map +0 -1
- package/dist/utils/console-styling.mjs +0 -85
- package/dist/utils/console-styling.mjs.map +0 -1
- package/src/__tests__/zod-regression.test.ts +0 -319
- package/src/event-transforms/index.ts +0 -5
- package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
- package/src/event-transforms/recorded-events.test.ts +0 -166
- package/src/event-transforms/recorded-events.ts +0 -74
- package/src/telemetry/events.ts +0 -68
- package/src/telemetry/index.ts +0 -9
- package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
- package/src/telemetry/telemetry-client.test.ts +0 -798
- package/src/telemetry/telemetry-client.ts +0 -328
- package/src/telemetry/utils.ts +0 -15
- package/src/utils/__tests__/conditions.test.ts +0 -161
- package/src/utils/conditions.ts +0 -116
- package/src/utils/console-styling.ts +0 -126
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
import { Analytics } from "@segment/analytics-node";
|
|
2
|
-
import type { AnalyticsEvents } from "./events";
|
|
3
|
-
import { flattenObject } from "./utils";
|
|
4
|
-
import { v4 as uuidv4 } from "uuid";
|
|
5
|
-
import {
|
|
6
|
-
firstNonBlankTelemetryId,
|
|
7
|
-
lambdaClient,
|
|
8
|
-
parseAndWarnTelemetryId,
|
|
9
|
-
} from "./lambda-client";
|
|
10
|
-
import {
|
|
11
|
-
computeSamplingMeta,
|
|
12
|
-
TELEMETRY_EMITTER_V1,
|
|
13
|
-
TELEMETRY_SURFACE_V1,
|
|
14
|
-
UNSAMPLED_RATE,
|
|
15
|
-
} from "./sampling";
|
|
16
|
-
import { isTelemetryDisabled } from "./telemetry-disabled";
|
|
17
|
-
|
|
18
|
-
export { isTelemetryDisabled };
|
|
19
|
-
|
|
20
|
-
/** Transport identity and sampling authority resolved for one runtime. */
|
|
21
|
-
export interface TelemetryIdentity {
|
|
22
|
-
telemetryId?: string;
|
|
23
|
-
licenseToken?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Capture-only telemetry client bound to one runtime identity. */
|
|
27
|
-
export interface TelemetryCapture {
|
|
28
|
-
capture<K extends keyof AnalyticsEvents>(
|
|
29
|
-
event: K,
|
|
30
|
-
properties: AnalyticsEvents[K],
|
|
31
|
-
): Promise<void>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface ResolvedTelemetryIdentity {
|
|
35
|
-
telemetryId: string | null;
|
|
36
|
-
licenseToken: string | null;
|
|
37
|
-
licenseTelemetryId: string | null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class TelemetryClient {
|
|
41
|
-
segment: Analytics | undefined;
|
|
42
|
-
globalProperties: Record<string, any> = {};
|
|
43
|
-
cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;
|
|
44
|
-
// EIP / Intelligence license token (Ed25519-signed JWT). The lambda
|
|
45
|
-
// client decodes its payload to extract telemetry_id. Customer API
|
|
46
|
-
// keys are NOT used here — they flow only into Segment.
|
|
47
|
-
private licenseToken: string | null = null;
|
|
48
|
-
// Standalone analytics identity. This stays separate from the effective
|
|
49
|
-
// identity so legacy callers continue sending only their license token to
|
|
50
|
-
// the Lambda transport.
|
|
51
|
-
private telemetryId: string | null = null;
|
|
52
|
-
// License-derived identity used only as sampling authority. A standalone
|
|
53
|
-
// telemetry id remains a transport claim and does not bypass the
|
|
54
|
-
// Segment gate.
|
|
55
|
-
private licenseTelemetryId: string | null = null;
|
|
56
|
-
packageName: string;
|
|
57
|
-
packageVersion: string;
|
|
58
|
-
private telemetryDisabled: boolean = false;
|
|
59
|
-
// Client-side sampling rate for anonymous events on the Segment wire
|
|
60
|
-
// only. The lambda sink is ours and takes every event; Segment is
|
|
61
|
-
// billed per event, so the anonymous population stays capped there.
|
|
62
|
-
// Identified events (those whose license token yielded a telemetry_id)
|
|
63
|
-
// bypass the gate on both wires.
|
|
64
|
-
private segmentSampleRate: number = 0.05;
|
|
65
|
-
private anonymousId = `anon_${uuidv4()}`;
|
|
66
|
-
|
|
67
|
-
constructor({
|
|
68
|
-
packageName,
|
|
69
|
-
packageVersion,
|
|
70
|
-
telemetryDisabled,
|
|
71
|
-
telemetryBaseUrl,
|
|
72
|
-
sampleRate,
|
|
73
|
-
}: {
|
|
74
|
-
packageName: string;
|
|
75
|
-
packageVersion: string;
|
|
76
|
-
telemetryDisabled?: boolean;
|
|
77
|
-
telemetryBaseUrl?: string;
|
|
78
|
-
// Anonymous sampling rate for the Segment wire. Named without the
|
|
79
|
-
// prefix for backward compatibility; the lambda sink ignores it.
|
|
80
|
-
sampleRate?: number;
|
|
81
|
-
}) {
|
|
82
|
-
this.packageName = packageName;
|
|
83
|
-
this.packageVersion = packageVersion;
|
|
84
|
-
this.telemetryDisabled = telemetryDisabled || isTelemetryDisabled();
|
|
85
|
-
|
|
86
|
-
if (this.telemetryDisabled) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
this.setSampleRate(sampleRate);
|
|
91
|
-
|
|
92
|
-
// eslint-disable-next-line
|
|
93
|
-
const writeKey =
|
|
94
|
-
process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||
|
|
95
|
-
"n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja";
|
|
96
|
-
|
|
97
|
-
this.segment = new Analytics({
|
|
98
|
-
writeKey,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
this.setGlobalProperties({
|
|
102
|
-
"copilotkit.package.name": packageName,
|
|
103
|
-
"copilotkit.package.version": packageVersion,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private shouldSendToSegment() {
|
|
108
|
-
const randomNumber = Math.random();
|
|
109
|
-
return randomNumber < this.segmentSampleRate;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async capture<K extends keyof AnalyticsEvents>(
|
|
113
|
-
event: K,
|
|
114
|
-
properties: AnalyticsEvents[K],
|
|
115
|
-
): Promise<void> {
|
|
116
|
-
return this.captureWithIdentity(event, properties, {
|
|
117
|
-
telemetryId: this.telemetryId,
|
|
118
|
-
licenseToken: this.licenseToken,
|
|
119
|
-
licenseTelemetryId: this.licenseTelemetryId,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private async captureWithIdentity<K extends keyof AnalyticsEvents>(
|
|
124
|
-
event: K,
|
|
125
|
-
properties: AnalyticsEvents[K],
|
|
126
|
-
identity: ResolvedTelemetryIdentity,
|
|
127
|
-
): Promise<void> {
|
|
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()) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// The two wires are gated separately. The lambda sink takes every
|
|
135
|
-
// event: it is CopilotKit-owned, so anonymous volume costs nothing
|
|
136
|
-
// per event there and a real count beats one extrapolated from 5% of
|
|
137
|
-
// the population. Segment is billed per event, so it keeps the gate —
|
|
138
|
-
// callers without license-derived sampling authority are dropped from
|
|
139
|
-
// that copy at segmentSampleRate. Legacy license tokens with a
|
|
140
|
-
// telemetry_id send on both wires: the volume is bounded by
|
|
141
|
-
// paying-customer count and full fidelity per identified customer is
|
|
142
|
-
// worth the marginal cost.
|
|
143
|
-
const sendToSegment =
|
|
144
|
-
Boolean(identity.licenseTelemetryId) || this.shouldSendToSegment();
|
|
145
|
-
|
|
146
|
-
// Sampling metadata is computed in ./sampling so this client and the
|
|
147
|
-
// v2 runtime client can't drift apart again — see the note there.
|
|
148
|
-
// One call per wire, because the two wires were gated differently and
|
|
149
|
-
// a shared block would overweight the lambda copy by 1/sampleRate.
|
|
150
|
-
const lambdaSamplingMeta = computeSamplingMeta({
|
|
151
|
-
telemetryId: identity.licenseTelemetryId,
|
|
152
|
-
sampleRate: UNSAMPLED_RATE,
|
|
153
|
-
});
|
|
154
|
-
const segmentSamplingMeta = computeSamplingMeta({
|
|
155
|
-
telemetryId: identity.licenseTelemetryId,
|
|
156
|
-
sampleRate: this.segmentSampleRate,
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
// Everything below travels identically on both copies of this event.
|
|
160
|
-
// The event id is what makes the dual-write dedupable downstream:
|
|
161
|
-
// one capture() produces one id, stamped on the lambda copy and the
|
|
162
|
-
// Segment copy alike, so consumers no longer have to infer the
|
|
163
|
-
// duplication from $lib or from which fields happen to be present
|
|
164
|
-
// (OSS-1019).
|
|
165
|
-
const eventMeta = {
|
|
166
|
-
telemetry_emitter: TELEMETRY_EMITTER_V1,
|
|
167
|
-
telemetry_surface: TELEMETRY_SURFACE_V1,
|
|
168
|
-
telemetry_event_id: uuidv4(),
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
const flattenedProperties = flattenObject(properties);
|
|
172
|
-
const propertiesWithGlobal: Record<string, any> = {
|
|
173
|
-
...this.globalProperties,
|
|
174
|
-
...eventMeta,
|
|
175
|
-
...segmentSamplingMeta,
|
|
176
|
-
telemetry_transport: "segment",
|
|
177
|
-
...flattenedProperties,
|
|
178
|
-
};
|
|
179
|
-
const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)
|
|
180
|
-
.sort()
|
|
181
|
-
.reduce(
|
|
182
|
-
(obj, key) => {
|
|
183
|
-
obj[key] = propertiesWithGlobal[key];
|
|
184
|
-
return obj;
|
|
185
|
-
},
|
|
186
|
-
{} as Record<string, any>,
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
await lambdaClient.send({
|
|
190
|
-
event,
|
|
191
|
-
properties: flattenedProperties,
|
|
192
|
-
globalProperties: {
|
|
193
|
-
...this.globalProperties,
|
|
194
|
-
...eventMeta,
|
|
195
|
-
...lambdaSamplingMeta,
|
|
196
|
-
telemetry_transport: "lambda",
|
|
197
|
-
},
|
|
198
|
-
packageName: this.packageName,
|
|
199
|
-
packageVersion: this.packageVersion,
|
|
200
|
-
telemetryId: identity.telemetryId ?? undefined,
|
|
201
|
-
licenseToken: identity.licenseToken ?? undefined,
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
if (this.segment && sendToSegment) {
|
|
205
|
-
this.segment.track({
|
|
206
|
-
anonymousId: this.anonymousId,
|
|
207
|
-
event,
|
|
208
|
-
properties: { ...orderedPropertiesWithGlobal },
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
setGlobalProperties(properties: Record<string, any>) {
|
|
214
|
-
const flattenedProperties = flattenObject(properties);
|
|
215
|
-
this.globalProperties = {
|
|
216
|
-
...this.globalProperties,
|
|
217
|
-
...flattenedProperties,
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {
|
|
222
|
-
this.cloudConfiguration = properties;
|
|
223
|
-
|
|
224
|
-
this.setGlobalProperties({
|
|
225
|
-
cloud: {
|
|
226
|
-
publicApiKey: properties.publicApiKey,
|
|
227
|
-
baseUrl: properties.baseUrl,
|
|
228
|
-
},
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Atomically configure standalone, legacy, or anonymous telemetry identity.
|
|
234
|
-
*
|
|
235
|
-
* A standalone id takes transport precedence over a supplied legacy license
|
|
236
|
-
* token, but only a license-derived id grants sampling authority. Neither
|
|
237
|
-
* value is added to event properties.
|
|
238
|
-
*
|
|
239
|
-
* @param identity - One standalone id, one legacy license token, or neither.
|
|
240
|
-
*/
|
|
241
|
-
setTelemetryIdentity(identity: {
|
|
242
|
-
telemetryId?: string;
|
|
243
|
-
licenseToken?: string;
|
|
244
|
-
}): void {
|
|
245
|
-
const resolvedIdentity = this.resolveTelemetryIdentity(identity);
|
|
246
|
-
this.telemetryId = resolvedIdentity.telemetryId;
|
|
247
|
-
this.licenseToken = resolvedIdentity.licenseToken;
|
|
248
|
-
this.licenseTelemetryId = resolvedIdentity.licenseTelemetryId;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Configure legacy license-derived telemetry identity.
|
|
253
|
-
*
|
|
254
|
-
* @param licenseToken - License token whose telemetry claim identifies sends.
|
|
255
|
-
*/
|
|
256
|
-
setLicenseToken(licenseToken: string) {
|
|
257
|
-
this.setTelemetryIdentity({ licenseToken });
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Create an immutable capture scope for one runtime.
|
|
262
|
-
*
|
|
263
|
-
* The scope shares this client's sinks, process-wide opt-out, global
|
|
264
|
-
* properties, and Segment sampling rate, but snapshots transport identity and
|
|
265
|
-
* license-derived sampling authority. Constructing another runtime cannot
|
|
266
|
-
* rewrite an existing scope.
|
|
267
|
-
*
|
|
268
|
-
* @param identity - The runtime's construction-time telemetry identity.
|
|
269
|
-
* @returns A capture-only client bound to that identity.
|
|
270
|
-
*/
|
|
271
|
-
createScope(identity: TelemetryIdentity): TelemetryCapture {
|
|
272
|
-
const resolvedIdentity = this.resolveTelemetryIdentity(identity);
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
capture: <K extends keyof AnalyticsEvents>(
|
|
276
|
-
event: K,
|
|
277
|
-
properties: AnalyticsEvents[K],
|
|
278
|
-
) => this.captureWithIdentity(event, properties, resolvedIdentity),
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
private resolveTelemetryIdentity(
|
|
283
|
-
identity: TelemetryIdentity,
|
|
284
|
-
): ResolvedTelemetryIdentity {
|
|
285
|
-
const telemetryId = firstNonBlankTelemetryId(identity.telemetryId);
|
|
286
|
-
if (telemetryId !== undefined) {
|
|
287
|
-
return {
|
|
288
|
-
telemetryId,
|
|
289
|
-
licenseToken: null,
|
|
290
|
-
licenseTelemetryId: null,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return {
|
|
295
|
-
telemetryId: null,
|
|
296
|
-
licenseToken: identity.licenseToken ?? null,
|
|
297
|
-
licenseTelemetryId: identity.licenseToken
|
|
298
|
-
? parseAndWarnTelemetryId(identity.licenseToken)
|
|
299
|
-
: null,
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private setSampleRate(sampleRate: number | undefined) {
|
|
304
|
-
let _sampleRate: number;
|
|
305
|
-
|
|
306
|
-
_sampleRate = sampleRate ?? 0.05;
|
|
307
|
-
|
|
308
|
-
// eslint-disable-next-line
|
|
309
|
-
if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {
|
|
310
|
-
// eslint-disable-next-line
|
|
311
|
-
_sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
// Number.isNaN guards against parseFloat("nonsense") slipping past the
|
|
315
|
-
// range check (all NaN comparisons are false), which would silently
|
|
316
|
-
// drop every anonymous Segment event with no signal.
|
|
317
|
-
if (Number.isNaN(_sampleRate) || _sampleRate < 0 || _sampleRate > 1) {
|
|
318
|
-
throw new Error("Sample rate must be between 0 and 1");
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
this.segmentSampleRate = _sampleRate;
|
|
322
|
-
// Per-event sampling metadata (sampleRate/sampleRateAdjustmentFactor/
|
|
323
|
-
// sampleWeight) is computed per capture() in ./sampling, once per
|
|
324
|
-
// wire. Only license-authorized events get effectiveSampleRate=1 on
|
|
325
|
-
// the Segment copy; standalone transport identity stays in the
|
|
326
|
-
// sampled population there. The lambda copy is always unsampled.
|
|
327
|
-
}
|
|
328
|
-
}
|
package/src/telemetry/utils.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export function flattenObject(
|
|
2
|
-
obj: Record<string, any>,
|
|
3
|
-
parentKey = "",
|
|
4
|
-
res: Record<string, any> = {},
|
|
5
|
-
): Record<string, any> {
|
|
6
|
-
for (let key in obj) {
|
|
7
|
-
const propName = parentKey ? `${parentKey}.${key}` : key;
|
|
8
|
-
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
9
|
-
flattenObject(obj[key], propName, res);
|
|
10
|
-
} else {
|
|
11
|
-
res[propName] = obj[key];
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return res;
|
|
15
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
executeConditions,
|
|
4
|
-
Condition,
|
|
5
|
-
ComparisonCondition,
|
|
6
|
-
LogicalCondition,
|
|
7
|
-
ExistenceCondition,
|
|
8
|
-
} from "../conditions";
|
|
9
|
-
|
|
10
|
-
describe("executeConditions", () => {
|
|
11
|
-
it("returns true when conditions is empty or undefined", () => {
|
|
12
|
-
expect(executeConditions({ value: { a: 1 } })).toBe(true);
|
|
13
|
-
expect(executeConditions({ conditions: [], value: { a: 1 } })).toBe(true);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("treats multiple conditions as an implicit AND", () => {
|
|
17
|
-
const conditions: Condition[] = [
|
|
18
|
-
{ rule: "EQUALS", path: "a", value: 1 },
|
|
19
|
-
{ rule: "EQUALS", path: "b", value: 2 },
|
|
20
|
-
];
|
|
21
|
-
expect(executeConditions({ conditions, value: { a: 1, b: 2 } })).toBe(true);
|
|
22
|
-
expect(executeConditions({ conditions, value: { a: 1, b: 3 } })).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("comparison rules", () => {
|
|
26
|
-
it("EQUALS and NOT_EQUALS", () => {
|
|
27
|
-
const eq: ComparisonCondition = { rule: "EQUALS", path: "a", value: 5 };
|
|
28
|
-
const neq: ComparisonCondition = { rule: "NOT_EQUALS", path: "a", value: 5 };
|
|
29
|
-
expect(executeConditions({ conditions: [eq], value: { a: 5 } })).toBe(true);
|
|
30
|
-
expect(executeConditions({ conditions: [eq], value: { a: 6 } })).toBe(false);
|
|
31
|
-
expect(executeConditions({ conditions: [neq], value: { a: 6 } })).toBe(true);
|
|
32
|
-
expect(executeConditions({ conditions: [neq], value: { a: 5 } })).toBe(false);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("GREATER_THAN and LESS_THAN", () => {
|
|
36
|
-
const gt: ComparisonCondition = { rule: "GREATER_THAN", path: "n", value: 10 };
|
|
37
|
-
const lt: ComparisonCondition = { rule: "LESS_THAN", path: "n", value: 10 };
|
|
38
|
-
expect(executeConditions({ conditions: [gt], value: { n: 11 } })).toBe(true);
|
|
39
|
-
expect(executeConditions({ conditions: [gt], value: { n: 10 } })).toBe(false);
|
|
40
|
-
expect(executeConditions({ conditions: [lt], value: { n: 9 } })).toBe(true);
|
|
41
|
-
expect(executeConditions({ conditions: [lt], value: { n: 10 } })).toBe(false);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("CONTAINS and NOT_CONTAINS only apply to arrays", () => {
|
|
45
|
-
const contains: ComparisonCondition = { rule: "CONTAINS", path: "tags", value: "ts" };
|
|
46
|
-
const notContains: ComparisonCondition = { rule: "NOT_CONTAINS", path: "tags", value: "ts" };
|
|
47
|
-
expect(
|
|
48
|
-
executeConditions({ conditions: [contains], value: { tags: ["js", "ts"] } }),
|
|
49
|
-
).toBe(true);
|
|
50
|
-
expect(
|
|
51
|
-
executeConditions({ conditions: [contains], value: { tags: ["js"] } }),
|
|
52
|
-
).toBe(false);
|
|
53
|
-
expect(
|
|
54
|
-
executeConditions({ conditions: [notContains], value: { tags: ["js"] } }),
|
|
55
|
-
).toBe(true);
|
|
56
|
-
// non-array target always fails both CONTAINS and NOT_CONTAINS
|
|
57
|
-
expect(
|
|
58
|
-
executeConditions({ conditions: [contains], value: { tags: "ts" } }),
|
|
59
|
-
).toBe(false);
|
|
60
|
-
expect(
|
|
61
|
-
executeConditions({ conditions: [notContains], value: { tags: "ts" } }),
|
|
62
|
-
).toBe(false);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("MATCHES uses the value as a regular expression", () => {
|
|
66
|
-
const matches: ComparisonCondition = { rule: "MATCHES", path: "code", value: "^CK-[0-9]+$" };
|
|
67
|
-
expect(executeConditions({ conditions: [matches], value: { code: "CK-123" } })).toBe(true);
|
|
68
|
-
expect(executeConditions({ conditions: [matches], value: { code: "nope" } })).toBe(false);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("STARTS_WITH and ENDS_WITH", () => {
|
|
72
|
-
const sw: ComparisonCondition = { rule: "STARTS_WITH", path: "s", value: "foo" };
|
|
73
|
-
const ew: ComparisonCondition = { rule: "ENDS_WITH", path: "s", value: "bar" };
|
|
74
|
-
expect(executeConditions({ conditions: [sw], value: { s: "foobar" } })).toBe(true);
|
|
75
|
-
expect(executeConditions({ conditions: [sw], value: { s: "barfoo" } })).toBe(false);
|
|
76
|
-
expect(executeConditions({ conditions: [ew], value: { s: "foobar" } })).toBe(true);
|
|
77
|
-
expect(executeConditions({ conditions: [ew], value: { s: "barfoo" } })).toBe(false);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
describe("existence rules", () => {
|
|
82
|
-
it("EXISTS passes only for defined non-null values", () => {
|
|
83
|
-
const exists: ExistenceCondition = { rule: "EXISTS", path: "a" };
|
|
84
|
-
expect(executeConditions({ conditions: [exists], value: { a: 0 } })).toBe(true);
|
|
85
|
-
expect(executeConditions({ conditions: [exists], value: { a: "" } })).toBe(true);
|
|
86
|
-
expect(executeConditions({ conditions: [exists], value: { a: false } })).toBe(true);
|
|
87
|
-
expect(executeConditions({ conditions: [exists], value: { a: null } })).toBe(false);
|
|
88
|
-
expect(executeConditions({ conditions: [exists], value: {} })).toBe(false);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("NOT_EXISTS is the inverse of EXISTS", () => {
|
|
92
|
-
const notExists: ExistenceCondition = { rule: "NOT_EXISTS", path: "a" };
|
|
93
|
-
expect(executeConditions({ conditions: [notExists], value: { a: 1 } })).toBe(false);
|
|
94
|
-
expect(executeConditions({ conditions: [notExists], value: { a: null } })).toBe(true);
|
|
95
|
-
expect(executeConditions({ conditions: [notExists], value: {} })).toBe(true);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe("logical rules", () => {
|
|
100
|
-
it("AND requires all nested conditions to pass", () => {
|
|
101
|
-
const and: LogicalCondition = {
|
|
102
|
-
rule: "AND",
|
|
103
|
-
conditions: [
|
|
104
|
-
{ rule: "EQUALS", path: "a", value: 1 },
|
|
105
|
-
{ rule: "EQUALS", path: "b", value: 2 },
|
|
106
|
-
],
|
|
107
|
-
};
|
|
108
|
-
expect(executeConditions({ conditions: [and], value: { a: 1, b: 2 } })).toBe(true);
|
|
109
|
-
expect(executeConditions({ conditions: [and], value: { a: 1, b: 3 } })).toBe(false);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("OR passes when any nested condition passes", () => {
|
|
113
|
-
const or: LogicalCondition = {
|
|
114
|
-
rule: "OR",
|
|
115
|
-
conditions: [
|
|
116
|
-
{ rule: "EQUALS", path: "a", value: 1 },
|
|
117
|
-
{ rule: "EQUALS", path: "b", value: 2 },
|
|
118
|
-
],
|
|
119
|
-
};
|
|
120
|
-
expect(executeConditions({ conditions: [or], value: { a: 9, b: 2 } })).toBe(true);
|
|
121
|
-
expect(executeConditions({ conditions: [or], value: { a: 9, b: 9 } })).toBe(false);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("NOT inverts the AND of nested conditions", () => {
|
|
125
|
-
const notAll: LogicalCondition = {
|
|
126
|
-
rule: "NOT",
|
|
127
|
-
conditions: [
|
|
128
|
-
{ rule: "EQUALS", path: "a", value: 1 },
|
|
129
|
-
{ rule: "EQUALS", path: "b", value: 2 },
|
|
130
|
-
],
|
|
131
|
-
};
|
|
132
|
-
// both true → NOT(AND) = false
|
|
133
|
-
expect(executeConditions({ conditions: [notAll], value: { a: 1, b: 2 } })).toBe(false);
|
|
134
|
-
// one false → NOT(AND) = true
|
|
135
|
-
expect(executeConditions({ conditions: [notAll], value: { a: 1, b: 3 } })).toBe(true);
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
describe("path resolution", () => {
|
|
140
|
-
it("resolves nested dot paths", () => {
|
|
141
|
-
const cond: ComparisonCondition = { rule: "EQUALS", path: "user.profile.age", value: 30 };
|
|
142
|
-
expect(
|
|
143
|
-
executeConditions({ conditions: [cond], value: { user: { profile: { age: 30 } } } }),
|
|
144
|
-
).toBe(true);
|
|
145
|
-
expect(
|
|
146
|
-
executeConditions({ conditions: [cond], value: { user: { profile: { age: 31 } } } }),
|
|
147
|
-
).toBe(false);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("returns false for a missing nested path (undefined target)", () => {
|
|
151
|
-
const eq: ComparisonCondition = { rule: "EQUALS", path: "a.b.c", value: 1 };
|
|
152
|
-
expect(executeConditions({ conditions: [eq], value: {} })).toBe(false);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("uses the whole value when no path is given", () => {
|
|
156
|
-
const eq: ComparisonCondition = { rule: "EQUALS", value: 42 };
|
|
157
|
-
expect(executeConditions({ conditions: [eq], value: 42 })).toBe(true);
|
|
158
|
-
expect(executeConditions({ conditions: [eq], value: 43 })).toBe(false);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
});
|
package/src/utils/conditions.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
export type ComparisonRule =
|
|
2
|
-
| "EQUALS"
|
|
3
|
-
| "NOT_EQUALS"
|
|
4
|
-
| "GREATER_THAN"
|
|
5
|
-
| "LESS_THAN"
|
|
6
|
-
| "CONTAINS"
|
|
7
|
-
| "NOT_CONTAINS"
|
|
8
|
-
| "MATCHES"
|
|
9
|
-
| "STARTS_WITH"
|
|
10
|
-
| "ENDS_WITH";
|
|
11
|
-
export type LogicalRule = "AND" | "OR" | "NOT";
|
|
12
|
-
export type ExistenceRule = "EXISTS" | "NOT_EXISTS";
|
|
13
|
-
|
|
14
|
-
export type Rule = ComparisonRule | LogicalRule | ExistenceRule;
|
|
15
|
-
|
|
16
|
-
export interface BaseCondition {
|
|
17
|
-
rule: Rule;
|
|
18
|
-
path?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ComparisonCondition extends BaseCondition {
|
|
22
|
-
rule: ComparisonRule;
|
|
23
|
-
value: any;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface LogicalCondition extends BaseCondition {
|
|
27
|
-
rule: LogicalRule;
|
|
28
|
-
conditions: Condition[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface ExistenceCondition extends BaseCondition {
|
|
32
|
-
rule: ExistenceRule;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type Condition =
|
|
36
|
-
| ComparisonCondition
|
|
37
|
-
| LogicalCondition
|
|
38
|
-
| ExistenceCondition;
|
|
39
|
-
|
|
40
|
-
export function executeConditions({
|
|
41
|
-
conditions,
|
|
42
|
-
value,
|
|
43
|
-
}: {
|
|
44
|
-
conditions?: Condition[];
|
|
45
|
-
value: any;
|
|
46
|
-
}): boolean {
|
|
47
|
-
// If no conditions, consider it a pass
|
|
48
|
-
if (!conditions?.length) return true;
|
|
49
|
-
|
|
50
|
-
// Run all conditions (implicit AND)
|
|
51
|
-
return conditions.every((condition) => executeCondition(condition, value));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function executeCondition(condition: Condition, value: any): boolean {
|
|
55
|
-
const targetValue = condition.path
|
|
56
|
-
? getValueFromPath(value, condition.path)
|
|
57
|
-
: value;
|
|
58
|
-
|
|
59
|
-
switch (condition.rule) {
|
|
60
|
-
// Logical
|
|
61
|
-
case "AND":
|
|
62
|
-
return (condition as LogicalCondition).conditions.every((c) =>
|
|
63
|
-
executeCondition(c, value),
|
|
64
|
-
);
|
|
65
|
-
case "OR":
|
|
66
|
-
return (condition as LogicalCondition).conditions.some((c) =>
|
|
67
|
-
executeCondition(c, value),
|
|
68
|
-
);
|
|
69
|
-
case "NOT":
|
|
70
|
-
return !(condition as LogicalCondition).conditions.every((c) =>
|
|
71
|
-
executeCondition(c, value),
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
// Comparison
|
|
75
|
-
case "EQUALS":
|
|
76
|
-
return targetValue === (condition as ComparisonCondition).value;
|
|
77
|
-
case "NOT_EQUALS":
|
|
78
|
-
return targetValue !== (condition as ComparisonCondition).value;
|
|
79
|
-
case "GREATER_THAN":
|
|
80
|
-
return targetValue > (condition as ComparisonCondition).value;
|
|
81
|
-
case "LESS_THAN":
|
|
82
|
-
return targetValue < (condition as ComparisonCondition).value;
|
|
83
|
-
case "CONTAINS":
|
|
84
|
-
return (
|
|
85
|
-
Array.isArray(targetValue) &&
|
|
86
|
-
targetValue.includes((condition as ComparisonCondition).value)
|
|
87
|
-
);
|
|
88
|
-
case "NOT_CONTAINS":
|
|
89
|
-
return (
|
|
90
|
-
Array.isArray(targetValue) &&
|
|
91
|
-
!targetValue.includes((condition as ComparisonCondition).value)
|
|
92
|
-
);
|
|
93
|
-
case "MATCHES":
|
|
94
|
-
return new RegExp((condition as ComparisonCondition).value).test(
|
|
95
|
-
String(targetValue),
|
|
96
|
-
);
|
|
97
|
-
case "STARTS_WITH":
|
|
98
|
-
return String(targetValue).startsWith(
|
|
99
|
-
(condition as ComparisonCondition).value,
|
|
100
|
-
);
|
|
101
|
-
case "ENDS_WITH":
|
|
102
|
-
return String(targetValue).endsWith(
|
|
103
|
-
(condition as ComparisonCondition).value,
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// Existence
|
|
107
|
-
case "EXISTS":
|
|
108
|
-
return targetValue !== undefined && targetValue !== null;
|
|
109
|
-
case "NOT_EXISTS":
|
|
110
|
-
return targetValue === undefined || targetValue === null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function getValueFromPath(obj: any, path: string): any {
|
|
115
|
-
return path.split(".").reduce((acc, part) => acc?.[part], obj);
|
|
116
|
-
}
|