@copilotkit/shared 1.0.0-beta.0 → 1.0.0-beta.1

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.
Files changed (46) hide show
  1. package/.turbo/turbo-build.log +56 -25
  2. package/CHANGELOG.md +6 -0
  3. package/dist/chunk-3VA36BKL.mjs +122 -0
  4. package/dist/chunk-3VA36BKL.mjs.map +1 -0
  5. package/dist/chunk-6QGXWNS5.mjs +34 -0
  6. package/dist/chunk-6QGXWNS5.mjs.map +1 -0
  7. package/dist/chunk-P7STFMPO.mjs +1 -0
  8. package/dist/chunk-P7STFMPO.mjs.map +1 -0
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.js +156 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +15 -9
  13. package/dist/telemetry/events.d.ts +9 -0
  14. package/dist/telemetry/events.js +19 -0
  15. package/dist/telemetry/events.js.map +1 -0
  16. package/dist/telemetry/events.mjs +1 -0
  17. package/dist/telemetry/events.mjs.map +1 -0
  18. package/dist/telemetry/index.d.ts +3 -0
  19. package/dist/telemetry/index.js +184 -0
  20. package/dist/telemetry/index.js.map +1 -0
  21. package/dist/telemetry/index.mjs +9 -0
  22. package/dist/telemetry/index.mjs.map +1 -0
  23. package/dist/telemetry/security-check.d.ts +2 -0
  24. package/dist/telemetry/security-check.js +2 -0
  25. package/dist/telemetry/security-check.js.map +1 -0
  26. package/dist/telemetry/security-check.mjs +1 -0
  27. package/dist/telemetry/security-check.mjs.map +1 -0
  28. package/dist/telemetry/telemetry-client.d.ts +36 -0
  29. package/dist/telemetry/telemetry-client.js +182 -0
  30. package/dist/telemetry/telemetry-client.js.map +1 -0
  31. package/dist/telemetry/telemetry-client.mjs +8 -0
  32. package/dist/telemetry/telemetry-client.mjs.map +1 -0
  33. package/dist/telemetry/utils.d.ts +8 -0
  34. package/dist/telemetry/utils.js +69 -0
  35. package/dist/telemetry/utils.js.map +1 -0
  36. package/dist/telemetry/utils.mjs +9 -0
  37. package/dist/telemetry/utils.mjs.map +1 -0
  38. package/dist/types/index.mjs +1 -1
  39. package/dist/utils/index.mjs +3 -3
  40. package/package.json +5 -3
  41. package/src/index.ts +1 -0
  42. package/src/telemetry/events.ts +7 -0
  43. package/src/telemetry/index.ts +1 -0
  44. package/src/telemetry/security-check.ts +0 -0
  45. package/src/telemetry/telemetry-client.ts +164 -0
  46. package/src/telemetry/utils.ts +39 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/telemetry/utils.ts"],"sourcesContent":["import chalk from \"chalk\";\n\nexport function flattenObject(\n obj: Record<string, any>,\n parentKey = \"\",\n res: Record<string, any> = {},\n): Record<string, any> {\n for (let key in obj) {\n const propName = parentKey ? `${parentKey}.${key}` : key;\n if (typeof obj[key] === \"object\" && obj[key] !== null) {\n flattenObject(obj[key], propName, res);\n } else {\n res[propName] = obj[key];\n }\n }\n return res;\n}\n\nexport function printSecurityNotice(advisory: {\n advisory: string | null;\n message: string;\n severity: \"low\" | \"medium\" | \"high\" | \"none\";\n}) {\n const severityColor =\n {\n low: chalk.blue,\n medium: chalk.yellow,\n high: chalk.red,\n }[advisory.severity.toLowerCase()] || chalk.white;\n\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━ ${chalk.bold(`CopilotKit`)} ━━━━━━━━━━━━━━━━━━`);\n console.log();\n console.log(`${chalk.bold(`Severity: ${severityColor(advisory.severity.toUpperCase())}`)}`);\n console.log();\n console.log(`${chalk.bold(advisory.message)}`);\n console.log();\n console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAEX,SAAS,cACd,KACA,YAAY,IACZ,MAA2B,CAAC,GACP;AACrB,WAAS,OAAO,KAAK;AACnB,UAAM,WAAW,YAAY,GAAG,aAAa,QAAQ;AACrD,QAAI,OAAO,IAAI,GAAG,MAAM,YAAY,IAAI,GAAG,MAAM,MAAM;AACrD,oBAAc,IAAI,GAAG,GAAG,UAAU,GAAG;AAAA,IACvC,OAAO;AACL,UAAI,QAAQ,IAAI,IAAI,GAAG;AAAA,IACzB;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,oBAAoB,UAIjC;AACD,QAAM,gBACJ;AAAA,IACE,KAAK,aAAAA,QAAM;AAAA,IACX,QAAQ,aAAAA,QAAM;AAAA,IACd,MAAM,aAAAA,QAAM;AAAA,EACd,EAAE,SAAS,SAAS,YAAY,CAAC,KAAK,aAAAA,QAAM;AAE9C,UAAQ,IAAI;AACZ,UAAQ,IAAI,gHAAsB,aAAAA,QAAM,KAAK,YAAY,gHAAsB;AAC/E,UAAQ,IAAI;AACZ,UAAQ,IAAI,GAAG,aAAAA,QAAM,KAAK,aAAa,cAAc,SAAS,SAAS,YAAY,CAAC,GAAG,GAAG;AAC1F,UAAQ,IAAI;AACZ,UAAQ,IAAI,GAAG,aAAAA,QAAM,KAAK,SAAS,OAAO,GAAG;AAC7C,UAAQ,IAAI;AACZ,UAAQ,IAAI,kSAAkD;AAChE;","names":["chalk"]}
@@ -0,0 +1,9 @@
1
+ import {
2
+ flattenObject,
3
+ printSecurityNotice
4
+ } from "../chunk-6QGXWNS5.mjs";
5
+ export {
6
+ flattenObject,
7
+ printSecurityNotice
8
+ };
9
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,6 +1,6 @@
1
1
  import "../chunk-BANDZXMP.mjs";
2
- import "../chunk-NAFEBKSO.mjs";
3
2
  import "../chunk-YBHX4Y25.mjs";
4
3
  import "../chunk-MSUB6DGR.mjs";
5
4
  import "../chunk-IAFBVORQ.mjs";
5
+ import "../chunk-NAFEBKSO.mjs";
6
6
  //# sourceMappingURL=index.mjs.map
@@ -1,12 +1,12 @@
1
1
  import "../chunk-CYDWEPFL.mjs";
2
- import {
3
- actionParametersToJsonSchema
4
- } from "../chunk-CIPF7PMC.mjs";
5
2
  import {
6
3
  actionToChatCompletionFunction,
7
4
  annotatedFunctionToAction,
8
5
  annotatedFunctionToChatCompletionFunction
9
6
  } from "../chunk-S4HGLK2E.mjs";
7
+ import {
8
+ actionParametersToJsonSchema
9
+ } from "../chunk-CIPF7PMC.mjs";
10
10
  export {
11
11
  actionParametersToJsonSchema,
12
12
  actionToChatCompletionFunction,
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "1.0.0-beta.0",
7
+ "version": "1.0.0-beta.1",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -17,7 +17,6 @@
17
17
  },
18
18
  "types": "./dist/index.d.ts",
19
19
  "license": "MIT",
20
- "peerDependencies": {},
21
20
  "devDependencies": {
22
21
  "@types/jest": "^29.5.4",
23
22
  "eslint": "^8.56.0",
@@ -28,7 +27,10 @@
28
27
  "eslint-config-custom": "1.0.0-beta.0",
29
28
  "tsconfig": "1.0.0-beta.0"
30
29
  },
31
- "dependencies": {},
30
+ "dependencies": {
31
+ "chalk": "4.1.2",
32
+ "posthog-node": "^4.0.1"
33
+ },
32
34
  "scripts": {
33
35
  "build": "tsup --clean",
34
36
  "dev": "tsup --watch --no-splitting",
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./types";
2
2
  export * from "./utils";
3
3
  export * from "./constants";
4
+ export * from "./telemetry";
@@ -0,0 +1,7 @@
1
+ export type AnalyticsEvents = {
2
+ "oss.runtime.instance_created": {};
3
+ "oss.runtime.copilot_request_created": {
4
+ "cloud.guardrails.enabled": boolean;
5
+ requestType: string;
6
+ };
7
+ };
@@ -0,0 +1 @@
1
+ export * from "./telemetry-client";
File without changes
@@ -0,0 +1,164 @@
1
+ import { PostHog } from "posthog-node";
2
+ import { AnalyticsEvents } from "./events";
3
+ import { flattenObject, printSecurityNotice } from "./utils";
4
+ import { randomUUID } from "crypto";
5
+
6
+ export class TelemetryClient {
7
+ posthog: PostHog | undefined;
8
+ globalProperties: Record<string, any> = {};
9
+ cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;
10
+ packageName: string;
11
+ packageVersion: string;
12
+ private telemetryDisabled: boolean = false;
13
+ private telemetryBaseUrl: string | undefined;
14
+ private sampleRate: number = 0.05;
15
+
16
+ constructor({
17
+ packageName,
18
+ packageVersion,
19
+ telemetryDisabled,
20
+ telemetryBaseUrl,
21
+ posthogToken,
22
+ sampleRate,
23
+ }: {
24
+ packageName: string;
25
+ packageVersion: string;
26
+ telemetryDisabled?: boolean;
27
+ telemetryBaseUrl?: string;
28
+ posthogToken?: string;
29
+ sampleRate?: number;
30
+ }) {
31
+ this.packageName = packageName;
32
+ this.packageVersion = packageVersion;
33
+ this.telemetryDisabled =
34
+ telemetryDisabled ||
35
+ (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === "true" ||
36
+ (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === "1" ||
37
+ (process.env as any).DO_NOT_TRACK === "true" ||
38
+ (process.env as any).DO_NOT_TRACK === "1";
39
+
40
+ if (this.telemetryDisabled) {
41
+ return;
42
+ }
43
+
44
+ this.setSampleRate(sampleRate);
45
+
46
+ this.telemetryBaseUrl =
47
+ telemetryBaseUrl ||
48
+ (process.env as any).COPILOTKIT_TELEMETRY_BASE_URL ||
49
+ "https://telemetry.copilotkit.ai";
50
+
51
+ this.posthog = new PostHog(posthogToken || "token", {
52
+ host: `${this.telemetryBaseUrl}/telemetry/ingest`,
53
+ });
54
+
55
+ this.setGlobalProperties({
56
+ "copilotkit.package.name": packageName,
57
+ "copilotkit.package.version": packageVersion,
58
+ });
59
+
60
+ // Eliminates a PostHog error on Next.js
61
+ if (typeof (globalThis as any).navigator !== "undefined") {
62
+ (globalThis as any).navigator = {};
63
+ }
64
+ }
65
+
66
+ private shouldSendEvent() {
67
+ if (!this.telemetryBaseUrl) {
68
+ return false;
69
+ }
70
+
71
+ const randomNumber = Math.random();
72
+ return randomNumber < this.sampleRate;
73
+ }
74
+
75
+ async capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]) {
76
+ if (!this.shouldSendEvent() || !this.posthog) {
77
+ return;
78
+ }
79
+
80
+ const flattenedProperties = flattenObject(properties);
81
+ const propertiesWithGlobal = {
82
+ ...this.globalProperties,
83
+ ...flattenedProperties,
84
+ };
85
+ const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)
86
+ .sort()
87
+ .reduce(
88
+ (obj, key) => {
89
+ obj[key] = propertiesWithGlobal[key];
90
+ return obj;
91
+ },
92
+ {} as Record<string, any>,
93
+ );
94
+
95
+ this.posthog.capture({
96
+ distinctId: randomUUID(),
97
+ event,
98
+ properties: { ...orderedPropertiesWithGlobal },
99
+ });
100
+ }
101
+
102
+ async checkForUpdates() {
103
+ const url = `${this.telemetryBaseUrl}/check-for-updates?packageName=${this.packageName}&packageVersion=${this.packageVersion}`;
104
+
105
+ const response = await fetch(url);
106
+
107
+ if (!response.ok) {
108
+ return;
109
+ }
110
+
111
+ const result = await response.json();
112
+ const { advisory, severity } = result;
113
+
114
+ if (!advisory && (severity === "low" || severity === "none")) {
115
+ return;
116
+ }
117
+
118
+ printSecurityNotice(result);
119
+ }
120
+
121
+ setTelemetryBaseUrl(url: string) {
122
+ this.telemetryBaseUrl = url;
123
+
124
+ if (this.posthog) {
125
+ this.posthog.host = `${url}/telemetry/ingest`;
126
+ }
127
+ }
128
+
129
+ setGlobalProperties(properties: Record<string, any>) {
130
+ const flattenedProperties = flattenObject(properties);
131
+ this.globalProperties = { ...this.globalProperties, ...flattenedProperties };
132
+ }
133
+
134
+ setCloudConfiguration(properties: { publicApiKey: string; baseUrl: string }) {
135
+ this.cloudConfiguration = properties;
136
+
137
+ this.setGlobalProperties({
138
+ cloud: {
139
+ publicApiKey: properties.publicApiKey,
140
+ baseUrl: properties.baseUrl,
141
+ },
142
+ });
143
+ }
144
+
145
+ private setSampleRate(sampleRate: number | undefined) {
146
+ let _sampleRate: number;
147
+
148
+ _sampleRate = sampleRate ?? 0.05;
149
+
150
+ if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {
151
+ _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);
152
+ }
153
+
154
+ if (_sampleRate < 0 || _sampleRate > 1) {
155
+ throw new Error("Sample rate must be between 0 and 1");
156
+ }
157
+
158
+ this.sampleRate = _sampleRate;
159
+ this.setGlobalProperties({
160
+ sampleRate: this.sampleRate,
161
+ sampleRateAdjustmentFactor: 1 - this.sampleRate,
162
+ });
163
+ }
164
+ }
@@ -0,0 +1,39 @@
1
+ import chalk from "chalk";
2
+
3
+ export function flattenObject(
4
+ obj: Record<string, any>,
5
+ parentKey = "",
6
+ res: Record<string, any> = {},
7
+ ): Record<string, any> {
8
+ for (let key in obj) {
9
+ const propName = parentKey ? `${parentKey}.${key}` : key;
10
+ if (typeof obj[key] === "object" && obj[key] !== null) {
11
+ flattenObject(obj[key], propName, res);
12
+ } else {
13
+ res[propName] = obj[key];
14
+ }
15
+ }
16
+ return res;
17
+ }
18
+
19
+ export function printSecurityNotice(advisory: {
20
+ advisory: string | null;
21
+ message: string;
22
+ severity: "low" | "medium" | "high" | "none";
23
+ }) {
24
+ const severityColor =
25
+ {
26
+ low: chalk.blue,
27
+ medium: chalk.yellow,
28
+ high: chalk.red,
29
+ }[advisory.severity.toLowerCase()] || chalk.white;
30
+
31
+ console.log();
32
+ console.log(`━━━━━━━━━━━━━━━━━━ ${chalk.bold(`CopilotKit`)} ━━━━━━━━━━━━━━━━━━`);
33
+ console.log();
34
+ console.log(`${chalk.bold(`Severity: ${severityColor(advisory.severity.toUpperCase())}`)}`);
35
+ console.log();
36
+ console.log(`${chalk.bold(advisory.message)}`);
37
+ console.log();
38
+ console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
39
+ }