@copilotkit/shared 1.0.0-beta.0 → 1.0.0-beta.2
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/.turbo/turbo-build.log +62 -45
- package/CHANGELOG.md +12 -0
- package/dist/chunk-2WEXXBTK.mjs +119 -0
- package/dist/chunk-2WEXXBTK.mjs.map +1 -0
- package/dist/{chunk-2VLE6D3W.mjs → chunk-3AJ2GKWG.mjs} +2 -2
- package/dist/chunk-3AJ2GKWG.mjs.map +1 -0
- package/dist/chunk-6QGXWNS5.mjs +34 -0
- package/dist/chunk-6QGXWNS5.mjs.map +1 -0
- package/dist/chunk-DE5K76I2.mjs +1 -0
- package/dist/chunk-P7STFMPO.mjs +1 -0
- package/dist/chunk-YJLRG5U3.mjs +1 -0
- package/dist/constants/index.d.ts +1 -1
- package/dist/constants/index.js +1 -1
- package/dist/constants/index.js.map +1 -1
- package/dist/constants/index.mjs +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +159 -169
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -15
- package/dist/telemetry/events.d.ts +9 -0
- package/dist/{types/annotated-function.js → telemetry/events.js} +4 -4
- package/dist/telemetry/events.js.map +1 -0
- package/dist/telemetry/events.mjs +1 -0
- package/dist/telemetry/index.d.ts +3 -0
- package/dist/telemetry/index.js +181 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/index.mjs +9 -0
- package/dist/telemetry/security-check.d.ts +2 -0
- package/dist/telemetry/security-check.js +2 -0
- package/dist/telemetry/security-check.js.map +1 -0
- package/dist/telemetry/security-check.mjs +1 -0
- package/dist/telemetry/security-check.mjs.map +1 -0
- package/dist/telemetry/telemetry-client.d.ts +36 -0
- package/dist/telemetry/telemetry-client.js +179 -0
- package/dist/telemetry/telemetry-client.js.map +1 -0
- package/dist/telemetry/telemetry-client.mjs +8 -0
- package/dist/telemetry/telemetry-client.mjs.map +1 -0
- package/dist/telemetry/utils.d.ts +8 -0
- package/dist/telemetry/utils.js +69 -0
- package/dist/telemetry/utils.js.map +1 -0
- package/dist/telemetry/utils.mjs +9 -0
- package/dist/telemetry/utils.mjs.map +1 -0
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +2 -3
- package/dist/utils/index.d.ts +0 -3
- package/dist/utils/index.js +5 -168
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +2 -10
- package/package.json +5 -3
- package/src/constants/index.ts +1 -1
- package/src/index.ts +1 -0
- package/src/telemetry/events.ts +7 -0
- package/src/telemetry/index.ts +1 -0
- package/src/telemetry/security-check.ts +0 -0
- package/src/telemetry/telemetry-client.ts +159 -0
- package/src/telemetry/utils.ts +39 -0
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/dist/chunk-2VLE6D3W.mjs.map +0 -1
- package/dist/chunk-BANDZXMP.mjs +0 -1
- package/dist/chunk-CYDWEPFL.mjs +0 -1
- package/dist/chunk-S4HGLK2E.mjs +0 -163
- package/dist/chunk-S4HGLK2E.mjs.map +0 -1
- package/dist/chunk-YBHX4Y25.mjs +0 -1
- package/dist/types/annotated-function.d.ts +0 -24
- package/dist/types/annotated-function.js.map +0 -1
- package/dist/types/annotated-function.mjs +0 -2
- package/dist/utils/annotated-function.d.ts +0 -9
- package/dist/utils/annotated-function.js +0 -189
- package/dist/utils/annotated-function.js.map +0 -1
- package/dist/utils/annotated-function.mjs +0 -11
- package/src/types/annotated-function.ts +0 -27
- package/src/utils/annotated-function.ts +0 -179
- /package/dist/{chunk-BANDZXMP.mjs.map → chunk-DE5K76I2.mjs.map} +0 -0
- /package/dist/{chunk-CYDWEPFL.mjs.map → chunk-P7STFMPO.mjs.map} +0 -0
- /package/dist/{chunk-YBHX4Y25.mjs.map → chunk-YJLRG5U3.mjs.map} +0 -0
- /package/dist/{types/annotated-function.mjs.map → telemetry/events.mjs.map} +0 -0
- /package/dist/{utils/annotated-function.mjs.map → telemetry/index.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -24,175 +34,16 @@ __export(src_exports, {
|
|
|
24
34
|
COPILOT_CLOUD_CHAT_URL: () => COPILOT_CLOUD_CHAT_URL,
|
|
25
35
|
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER: () => COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
|
|
26
36
|
COPILOT_CLOUD_VERSION: () => COPILOT_CLOUD_VERSION,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
annotatedFunctionToAction: () => annotatedFunctionToAction,
|
|
30
|
-
annotatedFunctionToChatCompletionFunction: () => annotatedFunctionToChatCompletionFunction
|
|
37
|
+
TelemetryClient: () => TelemetryClient,
|
|
38
|
+
actionParametersToJsonSchema: () => actionParametersToJsonSchema
|
|
31
39
|
});
|
|
32
40
|
module.exports = __toCommonJS(src_exports);
|
|
33
41
|
|
|
34
|
-
// src/utils/annotated-function.ts
|
|
35
|
-
function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
|
|
36
|
-
let parameters = {};
|
|
37
|
-
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
38
|
-
let { name, required, ...forwardedArgs } = arg;
|
|
39
|
-
parameters[arg.name] = forwardedArgs;
|
|
40
|
-
}
|
|
41
|
-
let requiredParameterNames = [];
|
|
42
|
-
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
43
|
-
if (arg.required) {
|
|
44
|
-
requiredParameterNames.push(arg.name);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
let chatCompletionFunction = {
|
|
48
|
-
type: "function",
|
|
49
|
-
function: {
|
|
50
|
-
name: annotatedFunction.name,
|
|
51
|
-
description: annotatedFunction.description,
|
|
52
|
-
parameters: {
|
|
53
|
-
type: "object",
|
|
54
|
-
properties: parameters,
|
|
55
|
-
required: requiredParameterNames
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
return chatCompletionFunction;
|
|
60
|
-
}
|
|
61
|
-
function convertAttribute(attribute) {
|
|
62
|
-
var _a, _b, _c;
|
|
63
|
-
switch (attribute.type) {
|
|
64
|
-
case "string":
|
|
65
|
-
return {
|
|
66
|
-
type: "string",
|
|
67
|
-
description: attribute.description,
|
|
68
|
-
...attribute.enum && { enum: attribute.enum }
|
|
69
|
-
};
|
|
70
|
-
case "number":
|
|
71
|
-
case "boolean":
|
|
72
|
-
return {
|
|
73
|
-
type: attribute.type,
|
|
74
|
-
description: attribute.description
|
|
75
|
-
};
|
|
76
|
-
case "object":
|
|
77
|
-
case "object[]":
|
|
78
|
-
const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce(
|
|
79
|
-
(acc, attr) => {
|
|
80
|
-
acc[attr.name] = convertAttribute(attr);
|
|
81
|
-
return acc;
|
|
82
|
-
},
|
|
83
|
-
{}
|
|
84
|
-
);
|
|
85
|
-
const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
86
|
-
if (attribute.type === "object[]") {
|
|
87
|
-
return {
|
|
88
|
-
type: "array",
|
|
89
|
-
items: {
|
|
90
|
-
type: "object",
|
|
91
|
-
...properties && { properties },
|
|
92
|
-
...required && required.length > 0 && { required }
|
|
93
|
-
},
|
|
94
|
-
description: attribute.description
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
type: "object",
|
|
99
|
-
description: attribute.description,
|
|
100
|
-
...properties && { properties },
|
|
101
|
-
...required && required.length > 0 && { required }
|
|
102
|
-
};
|
|
103
|
-
default:
|
|
104
|
-
if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
|
|
105
|
-
const itemType = attribute.type.slice(0, -2);
|
|
106
|
-
return {
|
|
107
|
-
type: "array",
|
|
108
|
-
items: { type: itemType },
|
|
109
|
-
description: attribute.description
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
type: "string",
|
|
114
|
-
description: attribute.description
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function actionToChatCompletionFunction(action) {
|
|
119
|
-
let parameters = {};
|
|
120
|
-
for (let parameter of action.parameters || []) {
|
|
121
|
-
parameters[parameter.name] = convertAttribute(parameter);
|
|
122
|
-
}
|
|
123
|
-
let requiredParameterNames = [];
|
|
124
|
-
for (let arg of action.parameters || []) {
|
|
125
|
-
if (arg.required !== false) {
|
|
126
|
-
requiredParameterNames.push(arg.name);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
let chatCompletionFunction = {
|
|
130
|
-
type: "function",
|
|
131
|
-
function: {
|
|
132
|
-
name: action.name,
|
|
133
|
-
...action.description && { description: action.description },
|
|
134
|
-
parameters: {
|
|
135
|
-
type: "object",
|
|
136
|
-
properties: parameters,
|
|
137
|
-
required: requiredParameterNames
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
return chatCompletionFunction;
|
|
142
|
-
}
|
|
143
|
-
function annotatedFunctionToAction(annotatedFunction) {
|
|
144
|
-
const parameters = annotatedFunction.argumentAnnotations.map((annotation) => {
|
|
145
|
-
switch (annotation.type) {
|
|
146
|
-
case "string":
|
|
147
|
-
case "number":
|
|
148
|
-
case "boolean":
|
|
149
|
-
case "object":
|
|
150
|
-
return {
|
|
151
|
-
name: annotation.name,
|
|
152
|
-
description: annotation.description,
|
|
153
|
-
type: annotation.type,
|
|
154
|
-
required: annotation.required
|
|
155
|
-
};
|
|
156
|
-
case "array":
|
|
157
|
-
let type;
|
|
158
|
-
if (annotation.items.type === "string") {
|
|
159
|
-
type = "string[]";
|
|
160
|
-
} else if (annotation.items.type === "number") {
|
|
161
|
-
type = "number[]";
|
|
162
|
-
} else if (annotation.items.type === "boolean") {
|
|
163
|
-
type = "boolean[]";
|
|
164
|
-
} else if (annotation.items.type === "object") {
|
|
165
|
-
type = "object[]";
|
|
166
|
-
} else {
|
|
167
|
-
type = "string[]";
|
|
168
|
-
}
|
|
169
|
-
return {
|
|
170
|
-
name: annotation.name,
|
|
171
|
-
description: annotation.description,
|
|
172
|
-
type,
|
|
173
|
-
required: annotation.required
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return {
|
|
178
|
-
name: annotatedFunction.name,
|
|
179
|
-
description: annotatedFunction.description,
|
|
180
|
-
parameters,
|
|
181
|
-
handler: (args) => {
|
|
182
|
-
const paramsInCorrectOrder = [];
|
|
183
|
-
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
184
|
-
paramsInCorrectOrder.push(args[arg.name]);
|
|
185
|
-
}
|
|
186
|
-
return annotatedFunction.implementation(...paramsInCorrectOrder);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
42
|
// src/utils/json-schema.ts
|
|
192
43
|
function actionParametersToJsonSchema(actionParameters) {
|
|
193
44
|
let parameters = {};
|
|
194
45
|
for (let parameter of actionParameters || []) {
|
|
195
|
-
parameters[parameter.name] =
|
|
46
|
+
parameters[parameter.name] = convertAttribute(parameter);
|
|
196
47
|
}
|
|
197
48
|
let requiredParameterNames = [];
|
|
198
49
|
for (let arg of actionParameters || []) {
|
|
@@ -206,7 +57,7 @@ function actionParametersToJsonSchema(actionParameters) {
|
|
|
206
57
|
required: requiredParameterNames
|
|
207
58
|
};
|
|
208
59
|
}
|
|
209
|
-
function
|
|
60
|
+
function convertAttribute(attribute) {
|
|
210
61
|
var _a, _b, _c;
|
|
211
62
|
switch (attribute.type) {
|
|
212
63
|
case "string":
|
|
@@ -225,7 +76,7 @@ function convertAttribute2(attribute) {
|
|
|
225
76
|
case "object[]":
|
|
226
77
|
const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce(
|
|
227
78
|
(acc, attr) => {
|
|
228
|
-
acc[attr.name] =
|
|
79
|
+
acc[attr.name] = convertAttribute(attr);
|
|
229
80
|
return acc;
|
|
230
81
|
},
|
|
231
82
|
{}
|
|
@@ -267,17 +118,156 @@ function convertAttribute2(attribute) {
|
|
|
267
118
|
// src/constants/index.ts
|
|
268
119
|
var COPILOT_CLOUD_API_URL = "https://api.copilotkit.ai";
|
|
269
120
|
var COPILOT_CLOUD_VERSION = "v1";
|
|
270
|
-
var COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/${COPILOT_CLOUD_VERSION}
|
|
121
|
+
var COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;
|
|
271
122
|
var COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = "X-CopilotCloud-Public-Api-Key";
|
|
123
|
+
|
|
124
|
+
// src/telemetry/telemetry-client.ts
|
|
125
|
+
var import_posthog_node = require("posthog-node");
|
|
126
|
+
|
|
127
|
+
// src/telemetry/utils.ts
|
|
128
|
+
var import_chalk = __toESM(require("chalk"));
|
|
129
|
+
function flattenObject(obj, parentKey = "", res = {}) {
|
|
130
|
+
for (let key in obj) {
|
|
131
|
+
const propName = parentKey ? `${parentKey}.${key}` : key;
|
|
132
|
+
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
133
|
+
flattenObject(obj[key], propName, res);
|
|
134
|
+
} else {
|
|
135
|
+
res[propName] = obj[key];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return res;
|
|
139
|
+
}
|
|
140
|
+
function printSecurityNotice(advisory) {
|
|
141
|
+
const severityColor = {
|
|
142
|
+
low: import_chalk.default.blue,
|
|
143
|
+
medium: import_chalk.default.yellow,
|
|
144
|
+
high: import_chalk.default.red
|
|
145
|
+
}[advisory.severity.toLowerCase()] || import_chalk.default.white;
|
|
146
|
+
console.log();
|
|
147
|
+
console.log(`\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 ${import_chalk.default.bold(`CopilotKit`)} \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501`);
|
|
148
|
+
console.log();
|
|
149
|
+
console.log(`${import_chalk.default.bold(`Severity: ${severityColor(advisory.severity.toUpperCase())}`)}`);
|
|
150
|
+
console.log();
|
|
151
|
+
console.log(`${import_chalk.default.bold(advisory.message)}`);
|
|
152
|
+
console.log();
|
|
153
|
+
console.log(`\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/telemetry/telemetry-client.ts
|
|
157
|
+
var import_crypto = require("crypto");
|
|
158
|
+
var TelemetryClient = class {
|
|
159
|
+
constructor({
|
|
160
|
+
packageName,
|
|
161
|
+
packageVersion,
|
|
162
|
+
telemetryDisabled,
|
|
163
|
+
telemetryBaseUrl,
|
|
164
|
+
posthogToken,
|
|
165
|
+
sampleRate
|
|
166
|
+
}) {
|
|
167
|
+
this.globalProperties = {};
|
|
168
|
+
this.cloudConfiguration = null;
|
|
169
|
+
this.telemetryDisabled = false;
|
|
170
|
+
this.sampleRate = 0.05;
|
|
171
|
+
this.packageName = packageName;
|
|
172
|
+
this.packageVersion = packageVersion;
|
|
173
|
+
this.telemetryDisabled = telemetryDisabled || process.env.COPILOTKIT_TELEMETRY_DISABLED === "true" || process.env.COPILOTKIT_TELEMETRY_DISABLED === "1" || process.env.DO_NOT_TRACK === "true" || process.env.DO_NOT_TRACK === "1";
|
|
174
|
+
if (this.telemetryDisabled) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
this.setSampleRate(sampleRate);
|
|
178
|
+
this.telemetryBaseUrl = telemetryBaseUrl || process.env.COPILOTKIT_TELEMETRY_BASE_URL || "https://telemetry.copilotkit.ai";
|
|
179
|
+
this.posthog = new import_posthog_node.PostHog(posthogToken || "token", {
|
|
180
|
+
host: `${this.telemetryBaseUrl}/telemetry/ingest`
|
|
181
|
+
});
|
|
182
|
+
this.setGlobalProperties({
|
|
183
|
+
"copilotkit.package.name": packageName,
|
|
184
|
+
"copilotkit.package.version": packageVersion
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
shouldSendEvent() {
|
|
188
|
+
if (!this.telemetryBaseUrl) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
const randomNumber = Math.random();
|
|
192
|
+
return randomNumber < this.sampleRate;
|
|
193
|
+
}
|
|
194
|
+
async capture(event, properties) {
|
|
195
|
+
if (!this.shouldSendEvent() || !this.posthog) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const flattenedProperties = flattenObject(properties);
|
|
199
|
+
const propertiesWithGlobal = {
|
|
200
|
+
...this.globalProperties,
|
|
201
|
+
...flattenedProperties
|
|
202
|
+
};
|
|
203
|
+
const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal).sort().reduce(
|
|
204
|
+
(obj, key) => {
|
|
205
|
+
obj[key] = propertiesWithGlobal[key];
|
|
206
|
+
return obj;
|
|
207
|
+
},
|
|
208
|
+
{}
|
|
209
|
+
);
|
|
210
|
+
this.posthog.capture({
|
|
211
|
+
distinctId: (0, import_crypto.randomUUID)(),
|
|
212
|
+
event,
|
|
213
|
+
properties: { ...orderedPropertiesWithGlobal }
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
async checkForUpdates() {
|
|
217
|
+
const url = `${this.telemetryBaseUrl}/check-for-updates?packageName=${this.packageName}&packageVersion=${this.packageVersion}`;
|
|
218
|
+
const response = await fetch(url);
|
|
219
|
+
if (!response.ok) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const result = await response.json();
|
|
223
|
+
const { advisory, severity } = result;
|
|
224
|
+
if (!advisory && (severity === "low" || severity === "none")) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
printSecurityNotice(result);
|
|
228
|
+
}
|
|
229
|
+
setTelemetryBaseUrl(url) {
|
|
230
|
+
this.telemetryBaseUrl = url;
|
|
231
|
+
if (this.posthog) {
|
|
232
|
+
this.posthog.host = `${url}/telemetry/ingest`;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
setGlobalProperties(properties) {
|
|
236
|
+
const flattenedProperties = flattenObject(properties);
|
|
237
|
+
this.globalProperties = { ...this.globalProperties, ...flattenedProperties };
|
|
238
|
+
}
|
|
239
|
+
setCloudConfiguration(properties) {
|
|
240
|
+
this.cloudConfiguration = properties;
|
|
241
|
+
this.setGlobalProperties({
|
|
242
|
+
cloud: {
|
|
243
|
+
publicApiKey: properties.publicApiKey,
|
|
244
|
+
baseUrl: properties.baseUrl
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
setSampleRate(sampleRate) {
|
|
249
|
+
let _sampleRate;
|
|
250
|
+
_sampleRate = sampleRate ?? 0.05;
|
|
251
|
+
if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {
|
|
252
|
+
_sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);
|
|
253
|
+
}
|
|
254
|
+
if (_sampleRate < 0 || _sampleRate > 1) {
|
|
255
|
+
throw new Error("Sample rate must be between 0 and 1");
|
|
256
|
+
}
|
|
257
|
+
this.sampleRate = _sampleRate;
|
|
258
|
+
this.setGlobalProperties({
|
|
259
|
+
sampleRate: this.sampleRate,
|
|
260
|
+
sampleRateAdjustmentFactor: 1 - this.sampleRate
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
};
|
|
272
264
|
// Annotate the CommonJS export names for ESM import in node:
|
|
273
265
|
0 && (module.exports = {
|
|
274
266
|
COPILOT_CLOUD_API_URL,
|
|
275
267
|
COPILOT_CLOUD_CHAT_URL,
|
|
276
268
|
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
|
|
277
269
|
COPILOT_CLOUD_VERSION,
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
annotatedFunctionToAction,
|
|
281
|
-
annotatedFunctionToChatCompletionFunction
|
|
270
|
+
TelemetryClient,
|
|
271
|
+
actionParametersToJsonSchema
|
|
282
272
|
});
|
|
283
273
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils/annotated-function.ts","../src/utils/json-schema.ts","../src/constants/index.ts"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\n","import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nexport function annotatedFunctionToAction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Action<any> {\n const parameters: Parameter[] = annotatedFunction.argumentAnnotations.map((annotation) => {\n switch (annotation.type) {\n case \"string\":\n case \"number\":\n case \"boolean\":\n case \"object\":\n return {\n name: annotation.name,\n description: annotation.description,\n type: annotation.type,\n required: annotation.required,\n };\n case \"array\":\n let type;\n if (annotation.items.type === \"string\") {\n type = \"string[]\";\n } else if (annotation.items.type === \"number\") {\n type = \"number[]\";\n } else if (annotation.items.type === \"boolean\") {\n type = \"boolean[]\";\n } else if (annotation.items.type === \"object\") {\n type = \"object[]\";\n } else {\n type = \"string[]\";\n }\n return {\n name: annotation.name,\n description: annotation.description,\n type: type as any,\n required: annotation.required,\n };\n }\n });\n\n return {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: parameters,\n handler: (args) => {\n const paramsInCorrectOrder: any[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(args[arg.name]);\n }\n return annotatedFunction.implementation(...paramsInCorrectOrder);\n },\n };\n}\n","import { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n","export const COPILOT_CLOUD_API_URL = \"https://api.copilotkit.ai\";\nexport const COPILOT_CLOUD_VERSION = \"v1\";\nexport const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/${COPILOT_CLOUD_VERSION}/copilotkit/runtime/chat`;\nexport const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = \"X-CopilotCloud-Public-Api-Key\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,SAAS,0CACd,mBACgB;AAEhB,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAAI,EAAE,MAAM,UAAU,GAAG,cAAc,IAAI;AAC3C,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,iBAAiB,WAA2B;AArCrD;AAsCE,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK;AACH,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,UAAU;AAAA,QACvB,GAAI,UAAU,QAAQ,EAAE,MAAM,UAAU,KAAK;AAAA,MAC/C;AAAA,IACF,KAA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AAM;AAAA,YACN,GAAI,cAAc,EAAE,WAAW;AAAA,YAC/B,GAAI,YAAY,SAAS,SAAS,KAAK,EAAE,SAAS;AAAA,UACpD;AAAA,UACA,aAAa,UAAU;AAAA,QACzB;AAAA,MACF;AACA,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,UAAU;AAAA,QACvB,GAAI,cAAc,EAAE,WAAW;AAAA,QAC/B,GAAI,YAAY,SAAS,SAAS,KAAK,EAAE,SAAS;AAAA,MACpD;AAAA,IACF;AAEE,WAAI,eAAU,SAAV,mBAAgB,SAAS,OAAO;AAClC,cAAM,WAAW,UAAU,KAAK,MAAM,GAAG,EAAE;AAC3C,eAAO;AAAA,UACL,MAAM;AAAA,UACN,OAAO,EAAE,MAAM,SAAgB;AAAA,UAC/B,aAAa,UAAU;AAAA,QACzB;AAAA,MACF;AAEA,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,UAAU;AAAA,MACzB;AAAA,EACJ;AACF;;;ACvHO,IAAM,wBAAwB;AAC9B,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB,GAAG,yBAAyB;AAC3D,IAAM,sCAAsC;","names":["convertAttribute"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils/json-schema.ts","../src/constants/index.ts","../src/telemetry/telemetry-client.ts","../src/telemetry/utils.ts"],"sourcesContent":["export * from \"./types\";\nexport * from \"./utils\";\nexport * from \"./constants\";\nexport * from \"./telemetry\";\n","import { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n","export const COPILOT_CLOUD_API_URL = \"https://api.copilotkit.ai\";\nexport const COPILOT_CLOUD_VERSION = \"v1\";\nexport const COPILOT_CLOUD_CHAT_URL = `${COPILOT_CLOUD_API_URL}/copilotkit/${COPILOT_CLOUD_VERSION}`;\nexport const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = \"X-CopilotCloud-Public-Api-Key\";\n","import { PostHog } from \"posthog-node\";\nimport { AnalyticsEvents } from \"./events\";\nimport { flattenObject, printSecurityNotice } from \"./utils\";\nimport { randomUUID } from \"crypto\";\n\nexport class TelemetryClient {\n posthog: PostHog | undefined;\n globalProperties: Record<string, any> = {};\n cloudConfiguration: { publicApiKey: string; baseUrl: string } | null = null;\n packageName: string;\n packageVersion: string;\n private telemetryDisabled: boolean = false;\n private telemetryBaseUrl: string | undefined;\n private sampleRate: number = 0.05;\n\n constructor({\n packageName,\n packageVersion,\n telemetryDisabled,\n telemetryBaseUrl,\n posthogToken,\n sampleRate,\n }: {\n packageName: string;\n packageVersion: string;\n telemetryDisabled?: boolean;\n telemetryBaseUrl?: string;\n posthogToken?: string;\n sampleRate?: number;\n }) {\n this.packageName = packageName;\n this.packageVersion = packageVersion;\n this.telemetryDisabled =\n telemetryDisabled ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"true\" ||\n (process.env as any).COPILOTKIT_TELEMETRY_DISABLED === \"1\" ||\n (process.env as any).DO_NOT_TRACK === \"true\" ||\n (process.env as any).DO_NOT_TRACK === \"1\";\n\n if (this.telemetryDisabled) {\n return;\n }\n\n this.setSampleRate(sampleRate);\n\n this.telemetryBaseUrl =\n telemetryBaseUrl ||\n (process.env as any).COPILOTKIT_TELEMETRY_BASE_URL ||\n \"https://telemetry.copilotkit.ai\";\n\n this.posthog = new PostHog(posthogToken || \"token\", {\n host: `${this.telemetryBaseUrl}/telemetry/ingest`,\n });\n\n this.setGlobalProperties({\n \"copilotkit.package.name\": packageName,\n \"copilotkit.package.version\": packageVersion,\n });\n }\n\n private shouldSendEvent() {\n if (!this.telemetryBaseUrl) {\n return false;\n }\n\n const randomNumber = Math.random();\n return randomNumber < this.sampleRate;\n }\n\n async capture<K extends keyof AnalyticsEvents>(event: K, properties: AnalyticsEvents[K]) {\n if (!this.shouldSendEvent() || !this.posthog) {\n return;\n }\n\n const flattenedProperties = flattenObject(properties);\n const propertiesWithGlobal = {\n ...this.globalProperties,\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 this.posthog.capture({\n distinctId: randomUUID(),\n event,\n properties: { ...orderedPropertiesWithGlobal },\n });\n }\n\n async checkForUpdates() {\n const url = `${this.telemetryBaseUrl}/check-for-updates?packageName=${this.packageName}&packageVersion=${this.packageVersion}`;\n\n const response = await fetch(url);\n\n if (!response.ok) {\n return;\n }\n\n const result = await response.json();\n const { advisory, severity } = result;\n\n if (!advisory && (severity === \"low\" || severity === \"none\")) {\n return;\n }\n\n printSecurityNotice(result);\n }\n\n setTelemetryBaseUrl(url: string) {\n this.telemetryBaseUrl = url;\n\n if (this.posthog) {\n this.posthog.host = `${url}/telemetry/ingest`;\n }\n }\n\n setGlobalProperties(properties: Record<string, any>) {\n const flattenedProperties = flattenObject(properties);\n this.globalProperties = { ...this.globalProperties, ...flattenedProperties };\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 private setSampleRate(sampleRate: number | undefined) {\n let _sampleRate: number;\n\n _sampleRate = sampleRate ?? 0.05;\n\n if (process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE) {\n _sampleRate = parseFloat(process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE);\n }\n\n if (_sampleRate < 0 || _sampleRate > 1) {\n throw new Error(\"Sample rate must be between 0 and 1\");\n }\n\n this.sampleRate = _sampleRate;\n this.setGlobalProperties({\n sampleRate: this.sampleRate,\n sampleRateAdjustmentFactor: 1 - this.sampleRate,\n });\n }\n}\n","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;AAAA;AAAA;AAAA;;;ACsCO,SAAS,6BAA6B,kBAA2C;AAEtF,MAAI,aAAqC,CAAC;AAC1C,WAAS,aAAa,oBAAoB,CAAC,GAAG;AAC5C,eAAW,UAAU,IAAI,IAAI,iBAAiB,SAAS;AAAA,EACzD;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,oBAAoB,CAAC,GAAG;AACtC,QAAI,IAAI,aAAa,OAAO;AAC1B,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,IACZ,UAAU;AAAA,EACZ;AACF;AAEA,SAAS,iBAAiB,WAAkC;AA5D5D;AA6DE,UAAQ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|
+
import "./chunk-YJLRG5U3.mjs";
|
|
2
5
|
import {
|
|
3
6
|
actionParametersToJsonSchema
|
|
4
7
|
} from "./chunk-CIPF7PMC.mjs";
|
|
@@ -7,25 +10,19 @@ import {
|
|
|
7
10
|
COPILOT_CLOUD_CHAT_URL,
|
|
8
11
|
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
|
|
9
12
|
COPILOT_CLOUD_VERSION
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
import "./chunk-
|
|
12
|
-
import "./chunk-NAFEBKSO.mjs";
|
|
13
|
-
import "./chunk-YBHX4Y25.mjs";
|
|
14
|
-
import "./chunk-MSUB6DGR.mjs";
|
|
15
|
-
import "./chunk-IAFBVORQ.mjs";
|
|
13
|
+
} from "./chunk-3AJ2GKWG.mjs";
|
|
14
|
+
import "./chunk-P7STFMPO.mjs";
|
|
16
15
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
TelemetryClient
|
|
17
|
+
} from "./chunk-2WEXXBTK.mjs";
|
|
18
|
+
import "./chunk-6QGXWNS5.mjs";
|
|
19
|
+
import "./chunk-NAFEBKSO.mjs";
|
|
21
20
|
export {
|
|
22
21
|
COPILOT_CLOUD_API_URL,
|
|
23
22
|
COPILOT_CLOUD_CHAT_URL,
|
|
24
23
|
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
|
|
25
24
|
COPILOT_CLOUD_VERSION,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
annotatedFunctionToAction,
|
|
29
|
-
annotatedFunctionToChatCompletionFunction
|
|
25
|
+
TelemetryClient,
|
|
26
|
+
actionParametersToJsonSchema
|
|
30
27
|
};
|
|
31
28
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -13,7 +13,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
13
13
|
};
|
|
14
14
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
15
|
|
|
16
|
-
// src/
|
|
17
|
-
var
|
|
18
|
-
module.exports = __toCommonJS(
|
|
19
|
-
//# sourceMappingURL=
|
|
16
|
+
// src/telemetry/events.ts
|
|
17
|
+
var events_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(events_exports);
|
|
19
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/telemetry/events.ts"],"sourcesContent":["export type AnalyticsEvents = {\n \"oss.runtime.instance_created\": {};\n \"oss.runtime.copilot_request_created\": {\n \"cloud.guardrails.enabled\": boolean;\n requestType: string;\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=events.mjs.map
|