File size: 2,561 Bytes
0ae3f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * Standalone telemetry sender — runs as a detached child process.
 *
 * Usage: node telemetry-sender.cjs '<json context>'
 *
 * This script is spawned by telemetry.captureEvent() and runs independently
 * of the parent CLI process. It:
 *
 * 1. Resolves the user's email via /v1/ping/ if not already cached
 * 2. Caches the email in ~/.mem0/config.json for future runs
 * 3. Sends the PostHog event
 *
 * All errors are silently swallowed — this process must never produce output
 * or affect the user experience.
 */

"use strict";

const https = require("https");
const fs = require("fs");

function httpsRequest(url, method, headers, body) {
	return new Promise((resolve, reject) => {
		const u = new URL(url);
		const opts = {
			hostname: u.hostname,
			path: u.pathname + u.search,
			method,
			headers,
			timeout: 10000,
		};
		const req = https.request(opts, (res) => {
			let data = "";
			res.on("data", (chunk) => (data += chunk));
			res.on("end", () => {
				try {
					resolve(JSON.parse(data));
				} catch {
					resolve({});
				}
			});
		});
		req.on("error", reject);
		req.on("timeout", () => {
			req.destroy();
			reject(new Error("timeout"));
		});
		if (body) {
			req.end(body);
		} else {
			req.end();
		}
	});
}

async function resolveAndCacheEmail(ctx, payload) {
	try {
		const pingUrl = ctx.mem0BaseUrl.replace(/\/+$/, "") + "/v1/ping/";
		const data = await httpsRequest(pingUrl, "GET", {
			Authorization: "Token " + ctx.mem0ApiKey,
			"Content-Type": "application/json",
		});
		if (data.user_email) {
			payload.distinct_id = data.user_email;
			cacheEmail(ctx.configPath, data.user_email);
		}
	} catch {
		// silently swallow
	}
}

function cacheEmail(configPath, email) {
	if (!configPath) return;
	try {
		const raw = fs.readFileSync(configPath, "utf-8");
		const cfg = JSON.parse(raw);
		if (!cfg.platform) cfg.platform = {};
		cfg.platform.user_email = email;
		fs.writeFileSync(configPath, JSON.stringify(cfg, null, 2));
	} catch {
		// silently swallow
	}
}

async function sendPosthogEvent(posthogHost, payload) {
	try {
		const body = JSON.stringify(payload);
		await httpsRequest(posthogHost, "POST", {
			"Content-Type": "application/json",
			"Content-Length": Buffer.byteLength(body),
		}, body);
	} catch {
		// silently swallow
	}
}

async function main() {
	const ctx = JSON.parse(process.argv[2]);
	const payload = ctx.payload;

	if (ctx.needsEmail && ctx.mem0ApiKey) {
		await resolveAndCacheEmail(ctx, payload);
	}

	await sendPosthogEvent(ctx.posthogHost, payload);
}

main().catch(() => {});