Elffuss BraidHub

kikocis/continue public

94488b344dec · 3.7 KiBRawPermalink
1import os from "node:os";
2
3import { TeamAnalytics } from "../control-plane/TeamAnalytics.js";
4import { IdeInfo } from "../index.js";
5
6import type { PostHog as PostHogType } from "posthog-node";
7
8export enum PosthogFeatureFlag {
9 AutocompleteTimeout = "autocomplete-timeout",
10 RecentlyVisitedRangesNumSurroundingLines = "recently-visited-ranges-num-surrounding-lines",
11}
12
13export const EXPERIMENTS: {
14 [key in PosthogFeatureFlag]: {
15 [key: string]: { value: any };
16 };
17} = {
18 [PosthogFeatureFlag.AutocompleteTimeout]: {
19 control: { value: 150 },
20 "250": { value: 250 },
21 "350": { value: 350 },
22 "450": { value: 450 },
23 },
24 [PosthogFeatureFlag.RecentlyVisitedRangesNumSurroundingLines]: {
25 control: { value: null },
26 "5": { value: 5 },
27 "10": { value: 10 },
28 "15": { value: 15 },
29 "20": { value: 20 },
30 },
31};
32
33export class Telemetry {
34 // Set to undefined whenever telemetry is disabled
35 static client: PostHogType | undefined = undefined;
36 static uniqueId = "NOT_UNIQUE";
37 static os: string | undefined = undefined;
38 static ideInfo: IdeInfo | undefined = undefined;
39
40 static async capture(
41 event: string,
42 properties: { [key: string]: any },
43 sendToTeam: boolean = false,
44 isExtensionActivationError: boolean = false,
45 ) {
46 try {
47 const augmentedProperties = {
48 ...properties,
49 os: Telemetry.os,
50 extensionVersion: Telemetry.ideInfo?.extensionVersion,
51 ideName: Telemetry.ideInfo?.name,
52 ideType: Telemetry.ideInfo?.ideType,
53 };
54 const payload = {
55 distinctId: Telemetry.uniqueId,
56 event,
57 properties: augmentedProperties,
58 sendFeatureFlags: true,
59 };
60
61 // In cases where an extremely early fatal error occurs, we may not have initialized yet
62 if (isExtensionActivationError && !Telemetry.client) {
63 const client = await Telemetry.getTelemetryClient();
64 client?.capture(payload);
65 return;
66 }
67
68 if (process.env.NODE_ENV === "test") {
69 return;
70 }
71
72 Telemetry.client?.capture(payload);
73
74 if (sendToTeam) {
75 void TeamAnalytics.capture(event, properties);
76 }
77 } catch (e) {
78 console.error(`Failed to capture event: ${e}`);
79 }
80 }
81
82 static shutdownPosthogClient() {
83 Telemetry.client?.shutdown();
84 }
85
86 static async getTelemetryClient(): Promise<PostHogType | undefined> {
87 try {
88 const { PostHog } = await import("posthog-node");
89 return new PostHog("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
90 host: "https://app.posthog.com",
91 });
92 } catch (e) {
93 console.error(`Failed to setup telemetry: ${e}`);
94 }
95 }
96
97 static async setup(allow: boolean, uniqueId: string, ideInfo: IdeInfo) {
98 Telemetry.uniqueId = uniqueId;
99 Telemetry.os = os.platform();
100 Telemetry.ideInfo = ideInfo;
101
102 if (!allow || process.env.NODE_ENV === "test") {
103 Telemetry.client = undefined;
104 } else if (!Telemetry.client) {
105 Telemetry.client = await Telemetry.getTelemetryClient();
106 }
107 }
108
109 private static featureValueCache: Record<string, any> = {};
110
111 static async getFeatureFlag(flag: PosthogFeatureFlag) {
112 const value = Telemetry.client?.getFeatureFlag(flag, Telemetry.uniqueId);
113
114 Telemetry.featureValueCache[flag] = value;
115 return value;
116 }
117
118 static async getValueForFeatureFlag(flag: PosthogFeatureFlag) {
119 try {
120 if (Telemetry.featureValueCache[flag]) {
121 return Telemetry.featureValueCache[flag];
122 }
123
124 const userGroup = await Telemetry.getFeatureFlag(flag);
125 if (typeof userGroup === "string") {
126 return EXPERIMENTS[flag][userGroup].value;
127 }
128
129 return undefined;
130 } catch {
131 return undefined;
132 }
133 }
134}