UNPKG

@copilotkit/shared

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

70 lines (68 loc) 2.49 kB
//#region src/telemetry/lambda-client.ts const TELEMETRY_SINK_URL = typeof process !== "undefined" && process.env?.COPILOTKIT_TELEMETRY_URL || "https://telemetry.copilotkit.ai/ingest"; const FETCH_TIMEOUT_MS = 3e3; const STRIPPED_KEYS = new Set(["cloud.public_api_key", "cloud.publicApiKey"]); function stripCloudKeys(obj) { if (!obj) return {}; const out = {}; for (const [k, v] of Object.entries(obj)) if (!STRIPPED_KEYS.has(k)) out[k] = v; return out; } function parseTelemetryIdFromLicense(token) { if (!token) return null; const parts = token.split("."); if (parts.length !== 3) return null; try { let b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/"); const padding = (4 - b64.length % 4) % 4; b64 += "=".repeat(padding); const json = typeof atob === "function" ? atob(b64) : Buffer.from(b64, "base64").toString("utf8"); const decoded = JSON.parse(json); return typeof decoded.telemetry_id === "string" ? decoded.telemetry_id : null; } catch { return null; } } function parseAndWarnTelemetryId(licenseToken) { const telemetryId = parseTelemetryIdFromLicense(licenseToken); if (!telemetryId) console.warn("[CopilotKit] License token did not yield a telemetry_id; telemetry events will be sent anonymously."); return telemetryId; } async function send(opts) { try { const body = JSON.stringify({ event: opts.event, properties: stripCloudKeys(opts.properties), global_properties: stripCloudKeys(opts.globalProperties), package: { name: opts.packageName, version: opts.packageVersion }, ts: Math.floor(Date.now() / 1e3) }); const telemetryId = parseTelemetryIdFromLicense(opts.licenseToken); const headers = { "Content-Type": "application/json", "User-Agent": opts.packageName ? `CopilotKit-Runtime/${opts.packageVersion ?? "unknown"} (${opts.packageName})` : "CopilotKit-Runtime" }; if (telemetryId) headers["X-CopilotKit-Telemetry-Id"] = telemetryId; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); try { await fetch(TELEMETRY_SINK_URL, { method: "POST", headers, body, signal: controller.signal }); } finally { clearTimeout(timeoutId); } } catch {} } const lambdaClient = { send }; //#endregion exports.lambdaClient = lambdaClient; exports.parseAndWarnTelemetryId = parseAndWarnTelemetryId; exports.parseTelemetryIdFromLicense = parseTelemetryIdFromLicense; //# sourceMappingURL=lambda-client.cjs.map