@glasstrace/sdk 0.2.3 → 0.4.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.
package/dist/index.d.cts CHANGED
@@ -467,6 +467,9 @@ declare function uploadSourceMaps(apiKey: string, endpoint: string, buildHash: s
467
467
  * explicit, opt-in API for manual error reporting. If no span is active
468
468
  * or OTel is not available, the call is silently ignored.
469
469
  *
470
+ * On the first captured error, may display a one-time diagnostic nudge
471
+ * to stderr if the MCP connection marker is absent (dev environments only).
472
+ *
470
473
  * @param error - The error to capture. Accepts `Error` objects, strings, or any value.
471
474
  *
472
475
  * @example
package/dist/index.d.ts CHANGED
@@ -467,6 +467,9 @@ declare function uploadSourceMaps(apiKey: string, endpoint: string, buildHash: s
467
467
  * explicit, opt-in API for manual error reporting. If no span is active
468
468
  * or OTel is not available, the call is silently ignored.
469
469
  *
470
+ * On the first captured error, may display a one-time diagnostic nudge
471
+ * to stderr if the MCP connection marker is absent (dev environments only).
472
+ *
470
473
  * @param error - The error to capture. Accepts `Error` objects, strings, or any value.
471
474
  *
472
475
  * @example
package/dist/index.js CHANGED
@@ -1,16 +1,18 @@
1
1
  import {
2
- AnonApiKeySchema,
2
+ buildImportGraph,
3
+ discoverTestFiles,
4
+ extractImports
5
+ } from "./chunk-LAMTBURS.js";
6
+ import {
3
7
  DEFAULT_CAPTURE_CONFIG,
4
8
  GLASSTRACE_ATTRIBUTE_NAMES,
5
9
  SdkCachedConfigSchema,
6
10
  SdkInitResponseSchema,
7
11
  SessionIdSchema,
8
12
  SourceMapUploadResponseSchema,
9
- buildImportGraph,
10
- createAnonApiKey,
11
- discoverTestFiles,
12
- extractImports
13
- } from "./chunk-CUFIV225.js";
13
+ getOrCreateAnonKey,
14
+ readAnonKey
15
+ } from "./chunk-EC5IINUT.js";
14
16
  import {
15
17
  INVALID_SPAN_CONTEXT,
16
18
  SamplingDecision,
@@ -180,60 +182,11 @@ function classifyFetchTarget(url) {
180
182
  return "unknown";
181
183
  }
182
184
 
183
- // src/anon-key.ts
184
- import { readFile, writeFile, mkdir, chmod } from "fs/promises";
185
- import { join } from "path";
186
- var GLASSTRACE_DIR = ".glasstrace";
187
- var ANON_KEY_FILE = "anon_key";
188
- var ephemeralKeyCache = /* @__PURE__ */ new Map();
189
- async function readAnonKey(projectRoot) {
190
- const root = projectRoot ?? process.cwd();
191
- const keyPath = join(root, GLASSTRACE_DIR, ANON_KEY_FILE);
192
- try {
193
- const content = await readFile(keyPath, "utf-8");
194
- const result = AnonApiKeySchema.safeParse(content);
195
- if (result.success) {
196
- return result.data;
197
- }
198
- } catch {
199
- }
200
- const cached = ephemeralKeyCache.get(root);
201
- if (cached !== void 0) {
202
- return cached;
203
- }
204
- return null;
205
- }
206
- async function getOrCreateAnonKey(projectRoot) {
207
- const root = projectRoot ?? process.cwd();
208
- const dirPath = join(root, GLASSTRACE_DIR);
209
- const keyPath = join(dirPath, ANON_KEY_FILE);
210
- const existingKey = await readAnonKey(root);
211
- if (existingKey !== null) {
212
- return existingKey;
213
- }
214
- const cached = ephemeralKeyCache.get(root);
215
- if (cached !== void 0) {
216
- return cached;
217
- }
218
- const newKey = createAnonApiKey();
219
- try {
220
- await mkdir(dirPath, { recursive: true, mode: 448 });
221
- await writeFile(keyPath, newKey, "utf-8");
222
- await chmod(keyPath, 384);
223
- } catch (err) {
224
- ephemeralKeyCache.set(root, newKey);
225
- console.warn(
226
- `[glasstrace] Failed to persist anonymous key to ${keyPath}: ${err instanceof Error ? err.message : String(err)}. Using ephemeral key.`
227
- );
228
- }
229
- return newKey;
230
- }
231
-
232
185
  // src/init-client.ts
233
186
  import { readFileSync } from "fs";
234
- import { writeFile as writeFile2, mkdir as mkdir2 } from "fs/promises";
235
- import { join as join2 } from "path";
236
- var GLASSTRACE_DIR2 = ".glasstrace";
187
+ import { writeFile, mkdir } from "fs/promises";
188
+ import { join } from "path";
189
+ var GLASSTRACE_DIR = ".glasstrace";
237
190
  var CONFIG_FILE = "config";
238
191
  var TWENTY_FOUR_HOURS_MS = 24 * 60 * 60 * 1e3;
239
192
  var INIT_TIMEOUT_MS = 1e4;
@@ -241,7 +194,7 @@ var currentConfig = null;
241
194
  var rateLimitBackoff = false;
242
195
  function loadCachedConfig(projectRoot) {
243
196
  const root = projectRoot ?? process.cwd();
244
- const configPath = join2(root, GLASSTRACE_DIR2, CONFIG_FILE);
197
+ const configPath = join(root, GLASSTRACE_DIR, CONFIG_FILE);
245
198
  try {
246
199
  const content = readFileSync(configPath, "utf-8");
247
200
  const parsed = JSON.parse(content);
@@ -264,15 +217,15 @@ function loadCachedConfig(projectRoot) {
264
217
  }
265
218
  async function saveCachedConfig(response, projectRoot) {
266
219
  const root = projectRoot ?? process.cwd();
267
- const dirPath = join2(root, GLASSTRACE_DIR2);
268
- const configPath = join2(dirPath, CONFIG_FILE);
220
+ const dirPath = join(root, GLASSTRACE_DIR);
221
+ const configPath = join(dirPath, CONFIG_FILE);
269
222
  try {
270
- await mkdir2(dirPath, { recursive: true });
223
+ await mkdir(dirPath, { recursive: true });
271
224
  const cached = {
272
225
  response,
273
226
  cachedAt: Date.now()
274
227
  };
275
- await writeFile2(configPath, JSON.stringify(cached), "utf-8");
228
+ await writeFile(configPath, JSON.stringify(cached), "utf-8");
276
229
  } catch (err) {
277
230
  console.warn(
278
231
  `[glasstrace] Failed to cache config to ${configPath}: ${err instanceof Error ? err.message : String(err)}`
@@ -3589,7 +3542,7 @@ function registerGlasstrace(options) {
3589
3542
  if (config.verbose) {
3590
3543
  console.info("[glasstrace] Background init firing.");
3591
3544
  }
3592
- await performInit(config, anonKey, "0.2.3");
3545
+ await performInit(config, anonKey, "0.4.1");
3593
3546
  maybeInstallConsoleCapture();
3594
3547
  } catch (err) {
3595
3548
  console.warn(
@@ -3609,7 +3562,7 @@ function registerGlasstrace(options) {
3609
3562
  if (config.verbose) {
3610
3563
  console.info("[glasstrace] Background init firing.");
3611
3564
  }
3612
- await performInit(config, anonKey, "0.2.3");
3565
+ await performInit(config, anonKey, "0.4.1");
3613
3566
  maybeInstallConsoleCapture();
3614
3567
  } catch (err) {
3615
3568
  console.warn(
@@ -3631,7 +3584,7 @@ function registerGlasstrace(options) {
3631
3584
  if (config.verbose) {
3632
3585
  console.info("[glasstrace] Background init firing.");
3633
3586
  }
3634
- await performInit(config, anonKeyForInit, "0.2.3");
3587
+ await performInit(config, anonKeyForInit, "0.4.1");
3635
3588
  maybeInstallConsoleCapture();
3636
3589
  } catch (err) {
3637
3590
  console.warn(
@@ -3730,7 +3683,10 @@ async function uploadSourceMaps(apiKey, endpoint, buildHash, maps) {
3730
3683
  sourceMap: m.content
3731
3684
  }))
3732
3685
  };
3733
- const baseUrl = endpoint.replace(/\/+$/, "");
3686
+ let baseUrl = endpoint;
3687
+ while (baseUrl.endsWith("/")) {
3688
+ baseUrl = baseUrl.slice(0, -1);
3689
+ }
3734
3690
  const response = await fetch(`${baseUrl}/v1/source-maps`, {
3735
3691
  method: "POST",
3736
3692
  headers: {
@@ -3814,6 +3770,41 @@ async function handleSourceMapUpload(distDir) {
3814
3770
  }
3815
3771
  }
3816
3772
 
3773
+ // src/nudge/error-nudge.ts
3774
+ import { existsSync } from "fs";
3775
+ import { join as join3 } from "path";
3776
+ var hasFired = false;
3777
+ function sanitize(input) {
3778
+ return input.replace(/[\x00-\x1f\x7f]/g, "");
3779
+ }
3780
+ function maybeShowMcpNudge(errorSummary) {
3781
+ if (hasFired) {
3782
+ return;
3783
+ }
3784
+ const config = resolveConfig();
3785
+ if (isProductionDisabled(config)) {
3786
+ return;
3787
+ }
3788
+ let markerExists = false;
3789
+ try {
3790
+ const markerPath = join3(process.cwd(), ".glasstrace", "mcp-connected");
3791
+ markerExists = existsSync(markerPath);
3792
+ } catch {
3793
+ markerExists = false;
3794
+ }
3795
+ if (markerExists) {
3796
+ return;
3797
+ }
3798
+ hasFired = true;
3799
+ const safe = sanitize(errorSummary);
3800
+ process.stderr.write(
3801
+ `[glasstrace] Error captured: ${safe}
3802
+ Debug with AI: ask your agent "What's the latest Glasstrace error?"
3803
+ Not connected? Run: npx glasstrace mcp add
3804
+ `
3805
+ );
3806
+ }
3807
+
3817
3808
  // src/capture-error.ts
3818
3809
  function captureError(error) {
3819
3810
  try {
@@ -3826,6 +3817,7 @@ function captureError(error) {
3826
3817
  attributes["error.type"] = error.constructor.name;
3827
3818
  }
3828
3819
  span.addEvent("glasstrace.error", attributes);
3820
+ maybeShowMcpNudge(String(error));
3829
3821
  } catch {
3830
3822
  }
3831
3823
  }