@openagentsinc/pylon 0.1.3 → 0.1.5

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.
@@ -0,0 +1,160 @@
1
+ import { randomUUID } from "node:crypto";
2
+
3
+ export const DEFAULT_TELEMETRY_ENDPOINT =
4
+ "https://openagents.com/api/telemetry/events";
5
+ const DEFAULT_TELEMETRY_TIMEOUT_MS = 2_000;
6
+
7
+ function timedSignal(timeoutMs = DEFAULT_TELEMETRY_TIMEOUT_MS) {
8
+ if (typeof AbortSignal?.timeout === "function") {
9
+ return {
10
+ signal: AbortSignal.timeout(timeoutMs),
11
+ dispose() {},
12
+ };
13
+ }
14
+
15
+ const controller = new AbortController();
16
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
17
+ return {
18
+ signal: controller.signal,
19
+ dispose() {
20
+ clearTimeout(timer);
21
+ },
22
+ };
23
+ }
24
+
25
+ function cleanString(value, fallback = null) {
26
+ if (typeof value !== "string") {
27
+ return fallback;
28
+ }
29
+
30
+ const trimmed = value.trim();
31
+ return trimmed.length > 0 ? trimmed : fallback;
32
+ }
33
+
34
+ function normalizeProperties(properties = {}) {
35
+ try {
36
+ return JSON.parse(JSON.stringify(properties ?? {}));
37
+ } catch {
38
+ return {};
39
+ }
40
+ }
41
+
42
+ export function detectPackageInvoker(env = process.env) {
43
+ const userAgent = cleanString(env.npm_config_user_agent);
44
+ if (userAgent?.includes("bun/")) {
45
+ return "bun";
46
+ }
47
+ if (userAgent?.includes("npm/")) {
48
+ return "npm";
49
+ }
50
+
51
+ const execPath = cleanString(env.npm_execpath);
52
+ if (execPath?.includes("bun")) {
53
+ return "bun";
54
+ }
55
+ if (execPath?.includes("npm")) {
56
+ return "npm";
57
+ }
58
+
59
+ return "unknown";
60
+ }
61
+
62
+ export function installSourceForTelemetry(installMethod, cached) {
63
+ if (installMethod === "source_build") {
64
+ return cached ? "cached_source_build" : "source_build";
65
+ }
66
+
67
+ return cached ? "cached_prebuilt" : "prebuilt";
68
+ }
69
+
70
+ export function telemetryFailureContext(error, fallbackStage = "unknown") {
71
+ const cause = error?.cause ?? null;
72
+ const stage = cleanString(error?.stage)?.toLowerCase().replace(/[^a-z0-9]+/g, "_");
73
+ const code =
74
+ cleanString(error?.code) ??
75
+ cleanString(error?.errno) ??
76
+ cleanString(cause?.code) ??
77
+ cleanString(cause?.errno) ??
78
+ (typeof error?.httpStatus === "number" ? `http_${error.httpStatus}` : null) ??
79
+ "unknown";
80
+ const message =
81
+ (error instanceof Error ? error.message : String(error)).split("\n")[0] ??
82
+ "unknown error";
83
+
84
+ return {
85
+ error_stage: stage || fallbackStage,
86
+ error_code: code,
87
+ error_message: message.slice(0, 240),
88
+ };
89
+ }
90
+
91
+ export function createTelemetryClient({
92
+ endpoint = process.env.OPENAGENTS_TELEMETRY_URL ?? DEFAULT_TELEMETRY_ENDPOINT,
93
+ fetchImpl = globalThis.fetch,
94
+ anonymousActorId = randomUUID(),
95
+ sessionId = anonymousActorId,
96
+ installId = anonymousActorId,
97
+ appVersion = null,
98
+ sourceSurface = "installer",
99
+ } = {}) {
100
+ const pending = new Set();
101
+ const enabled =
102
+ typeof fetchImpl === "function" &&
103
+ Boolean(cleanString(endpoint)) &&
104
+ process.env.OPENAGENTS_DISABLE_TELEMETRY !== "1";
105
+
106
+ async function post(payload) {
107
+ if (!enabled) {
108
+ return false;
109
+ }
110
+
111
+ const timeout = timedSignal();
112
+ try {
113
+ const response = await fetchImpl(endpoint, {
114
+ method: "POST",
115
+ headers: {
116
+ accept: "application/json",
117
+ "content-type": "application/json",
118
+ },
119
+ body: JSON.stringify(payload),
120
+ signal: timeout.signal,
121
+ });
122
+ return response.ok;
123
+ } catch {
124
+ return false;
125
+ } finally {
126
+ timeout.dispose();
127
+ }
128
+ }
129
+
130
+ return {
131
+ endpoint,
132
+ anonymousActorId,
133
+ sessionId,
134
+ installId,
135
+ emit(eventName, properties = {}) {
136
+ const promise = post({
137
+ event_name: eventName,
138
+ source_surface: sourceSurface,
139
+ occurred_at: new Date().toISOString(),
140
+ anonymous_actor_id: anonymousActorId,
141
+ session_id: sessionId,
142
+ install_id: installId,
143
+ app_version: cleanString(appVersion),
144
+ properties: normalizeProperties(properties),
145
+ });
146
+ pending.add(promise);
147
+ promise.finally(() => {
148
+ pending.delete(promise);
149
+ });
150
+ return promise;
151
+ },
152
+ async flush() {
153
+ const current = [...pending];
154
+ if (current.length === 0) {
155
+ return;
156
+ }
157
+ await Promise.allSettled(current);
158
+ },
159
+ };
160
+ }