25
const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
26
const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
27
28
-// PostHog's single-event capture endpoint expects `$create_alias` to merge an
29
-// anonymous person into an identified one (`properties.alias` -> `distinct_id`).
28
const POSTHOG_ALIAS_EVENT = '$create_alias';
29
const POSTHOG_LIB = 'composio-cli';
30
100
)
101
);
102
105
-// Resolves the direct-to-PostHog transport target. Both fields fall back to the
106
-// embedded constants; env overrides exist for tests and staging. Crucially this
107
-// does NOT depend on the Composio API base URL (which is only written at login),
108
-// so pre-login install/setup events can be delivered.
103
+// Direct-to-PostHog target; env overrides fall back to the embedded constants.
104
+// No dependency on the API base URL, so pre-login events can send.
105
const getPostHogConfig = environmentProvider
106
.load(
107
Config.all({
171
return (yield* decodeJson(raw)) as A;
172
}).pipe(Effect.catchAll(() => Effect.succeed(undefined)));
173
178
-// Persisted `~/.composio/analytics.json`. `install_id` is the anonymous device
179
-// UUID; `apollo_user_id` (the Apollo `org_member.id`, persisted at login) becomes
180
-// the PostHog identity so CLI events join the same person as web onboarding.
174
+// `install_id` = anonymous device UUID; `apollo_user_id` = Apollo `org_member.id`
175
+// persisted at login, used as the PostHog identity post-login.
176
type AnalyticsState = {
177
readonly install_id?: string;
178
readonly apollo_user_id?: string;
216
: null
217
);
218
224
-// Persists the Apollo `org_member.id` into the analytics state, preserving the
225
-// existing `install_id`/`created_at` so the anonymous device identity is kept
226
-// for the login-time alias. Best-effort: never surfaces or throws.
219
+// Persists the Apollo `org_member.id`, preserving `install_id`/`created_at`.
220
const persistApolloUserId = (installId: string, apolloUserId: string) =>
221
Effect.gen(function* () {
222
const fs = yield* FileSystem.FileSystem;
256
: null;
257
});
258
266
-// Post-login events key on the Apollo `org_member.id` (persisted at login) so
267
-// they resolve to the same PostHog person as web onboarding. Pre-login, the raw
268
-// `install_id` is the distinct id (NOT wrapped as `anon_<id>` — the login-time
269
-// `$create_alias` merges this exact value into the Apollo person).
259
+// Post-login: the Apollo `org_member.id`. Pre-login: the raw `install_id` (the
260
+// login-time `$create_alias` merges this exact value into the Apollo person).
261
const getDistinctId = (installId: string) =>
262
Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
263
351
);
352
});
353
363
-// Shapes an analytics envelope into a PostHog single-event capture body. The
364
-// public project write key authenticates the ingest in-band (no user API key),
365
-// which is what lets pre-login events send. Only allowlisted, already-shaped
366
-// properties from the event builders flow through here — no secrets or tool-arg
367
-// values (see events.ts).
354
+// Shapes an envelope into a PostHog capture body. The public project key
355
+// authenticates the ingest in-band — no user API key, so pre-login events send.
356
const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
357
api_key: projectKey,
358
event: envelope.event,
492
});
493
});
494
507
-// Encodes an envelope and hands it to the detached best-effort worker, which
508
-// delivers it directly to PostHog. Shared by event tracking and login aliasing.
495
+// Hands an envelope to the detached best-effort worker for PostHog delivery.
496
const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
497
Effect.gen(function* () {
498
yield* telemetryDebugLog('enqueue', { envelope });
511
return;
512
}
513
527
- // The direct PostHog transport carries a fixed endpoint + embedded public
528
- // key, so — unlike the old Apollo path — there is no base-URL gate here and
529
- // pre-login install/setup events are delivered.
514
const disabled = yield* analyticsDisabled;
515
if (disabled) {
516
yield* telemetryDebugLog('skip', { reason: 'disabled', eventName: event.name });
537
yield* enqueuePostHogEnvelope(envelope);
538
}).pipe(Effect.catchAllCause(() => Effect.void));
539
556
-// Fires a single PostHog `$create_alias` merging the anonymous `install_id`
557
-// device person into the identified Apollo `org_member.id` person. Best-effort.
540
+// Fires one `$create_alias` merging the `install_id` device into the Apollo person.
541
export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
542
Effect.gen(function* () {
543
const disabled = yield* analyticsDisabled;
550
const envelope: AnalyticsEnvelope = {
551
event: POSTHOG_ALIAS_EVENT,
552
properties: {
570
- // PostHog merges `properties.alias` into the event's `distinct_id`.
553
+ // PostHog merges `alias` into `distinct_id`.
554
distinct_id: apolloUserId,
555
alias: installId,
556
cli_version: constants.APP_VERSION,
563
yield* enqueuePostHogEnvelope(envelope);
564
}).pipe(Effect.catchAllCause(() => Effect.void));
565
583
-// Called at login: persists the Apollo `org_member.id` as the analytics identity
584
-// and stitches the pre-login anonymous device into that person via one alias.
585
-// Fully non-fatal so login never fails on telemetry.
566
+// Called at login: persist the Apollo identity and alias the device into it.
567
+// Non-fatal so login never fails on telemetry.
568
export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
569
Effect.gen(function* () {
570
if (typeof apolloUserId !== 'string' || apolloUserId.trim().length === 0) {