@raindrop-ai/ai-sdk 0.0.28 → 0.0.30

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.
@@ -1,6 +1,6 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks';
2
2
 
3
- // ../core/dist/chunk-LMIWKHOH.js
3
+ // ../core/dist/chunk-VUNUOE2X.js
4
4
  function getCrypto() {
5
5
  const c = globalThis.crypto;
6
6
  return c;
@@ -223,6 +223,114 @@ function buildExportTraceServiceRequest(spans, serviceName = "raindrop.core", se
223
223
  ]
224
224
  };
225
225
  }
226
+ var LOCAL_DEBUGGER_ENV_VAR = "RAINDROP_LOCAL_DEBUGGER";
227
+ var WORKSHOP_ENV_VAR = "RAINDROP_WORKSHOP";
228
+ var DEFAULT_LOCAL_WORKSHOP_URL = "http://localhost:5899/v1/";
229
+ function readEnvVar(name) {
230
+ var _a;
231
+ try {
232
+ const env = (_a = globalThis == null ? void 0 : globalThis.process) == null ? void 0 : _a.env;
233
+ if (env && typeof env[name] === "string" && env[name].length > 0) {
234
+ return env[name];
235
+ }
236
+ } catch (e) {
237
+ }
238
+ return void 0;
239
+ }
240
+ function readWorkshopEnv() {
241
+ const raw = readEnvVar(WORKSHOP_ENV_VAR);
242
+ if (raw === void 0) return void 0;
243
+ const trimmed = raw.trim();
244
+ if (trimmed.length === 0) return void 0;
245
+ if (/^https?:\/\//i.test(trimmed)) return { url: trimmed };
246
+ if (/^(1|true|yes|on)$/i.test(trimmed)) return "enable";
247
+ if (/^(0|false|no|off)$/i.test(trimmed)) return "disable";
248
+ return void 0;
249
+ }
250
+ function isLocalDevHost(hostname) {
251
+ if (!hostname) return false;
252
+ if (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "0.0.0.0" || hostname === "::1") {
253
+ return true;
254
+ }
255
+ if (hostname.endsWith(".localhost")) return true;
256
+ return false;
257
+ }
258
+ function readRuntimeHostname() {
259
+ try {
260
+ const loc = globalThis == null ? void 0 : globalThis.location;
261
+ if (loc && typeof loc.hostname === "string" && loc.hostname.length > 0) {
262
+ return loc.hostname;
263
+ }
264
+ } catch (e) {
265
+ }
266
+ return void 0;
267
+ }
268
+ function shouldAutoEnableLocalWorkshop() {
269
+ if (isLocalDevHost(readRuntimeHostname())) return true;
270
+ if (readEnvVar("NODE_ENV") === "development") return true;
271
+ return false;
272
+ }
273
+ function resolveLocalDebuggerBaseUrl(baseUrl) {
274
+ var _a, _b, _c;
275
+ if (baseUrl === null) return null;
276
+ if (typeof baseUrl === "string" && baseUrl.length > 0) {
277
+ return (_a = formatEndpoint(baseUrl)) != null ? _a : null;
278
+ }
279
+ const explicitUrlEnv = readEnvVar(LOCAL_DEBUGGER_ENV_VAR);
280
+ if (explicitUrlEnv) return (_b = formatEndpoint(explicitUrlEnv)) != null ? _b : null;
281
+ const workshopEnv = readWorkshopEnv();
282
+ if (workshopEnv === "disable") return null;
283
+ if (workshopEnv === "enable") return DEFAULT_LOCAL_WORKSHOP_URL;
284
+ if (workshopEnv && "url" in workshopEnv) return (_c = formatEndpoint(workshopEnv.url)) != null ? _c : null;
285
+ if (shouldAutoEnableLocalWorkshop()) return DEFAULT_LOCAL_WORKSHOP_URL;
286
+ return null;
287
+ }
288
+ function localDebuggerEnabled(baseUrl) {
289
+ return resolveLocalDebuggerBaseUrl(baseUrl) !== null;
290
+ }
291
+ function mirrorTraceExportToLocalDebugger(body, options = {}) {
292
+ var _a;
293
+ const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
294
+ if (!baseUrl) return;
295
+ void postJson(`${baseUrl}traces`, body, {}, {
296
+ maxAttempts: 1,
297
+ debug: (_a = options.debug) != null ? _a : false,
298
+ sdkName: options.sdkName
299
+ }).catch(() => {
300
+ });
301
+ }
302
+ function mirrorPartialEventToLocalDebugger(event, options = {}) {
303
+ var _a;
304
+ const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
305
+ if (!baseUrl) return;
306
+ const headers = options.writeKey ? { Authorization: `Bearer ${options.writeKey}` } : {};
307
+ void postJson(`${baseUrl}events/track_partial`, event, headers, {
308
+ maxAttempts: 1,
309
+ debug: (_a = options.debug) != null ? _a : false,
310
+ sdkName: options.sdkName
311
+ }).catch(() => {
312
+ });
313
+ }
314
+ function sendLocalDebuggerLiveEvent(event, options = {}) {
315
+ var _a, _b;
316
+ const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
317
+ if (!baseUrl) return;
318
+ void postJson(
319
+ `${baseUrl}live`,
320
+ {
321
+ ...event,
322
+ type: event.type,
323
+ timestamp: (_a = event.timestamp) != null ? _a : Date.now()
324
+ },
325
+ {},
326
+ {
327
+ maxAttempts: 1,
328
+ debug: (_b = options.debug) != null ? _b : false,
329
+ sdkName: options.sdkName
330
+ }
331
+ ).catch(() => {
332
+ });
333
+ }
226
334
  function mergePatches(target, source) {
227
335
  var _a, _b, _c, _d;
228
336
  const out = { ...target, ...source };
@@ -240,7 +348,7 @@ var EventShipper = class {
240
348
  this.sticky = /* @__PURE__ */ new Map();
241
349
  this.timers = /* @__PURE__ */ new Map();
242
350
  this.inFlight = /* @__PURE__ */ new Set();
243
- var _a, _b, _c, _d, _e, _f, _g;
351
+ var _a, _b, _c, _d, _e, _f, _g, _h;
244
352
  this.writeKey = (_a = opts.writeKey) == null ? void 0 : _a.trim();
245
353
  this.baseUrl = (_b = formatEndpoint(opts.endpoint)) != null ? _b : "https://api.raindrop.ai/v1/";
246
354
  this.enabled = opts.enabled !== false;
@@ -249,11 +357,15 @@ var EventShipper = class {
249
357
  this.sdkName = (_d = opts.sdkName) != null ? _d : "core";
250
358
  this.prefix = `[raindrop-ai/${this.sdkName}]`;
251
359
  this.defaultEventName = (_e = opts.defaultEventName) != null ? _e : "ai_generation";
360
+ this.localDebuggerUrl = (_f = resolveLocalDebuggerBaseUrl(opts.localDebuggerUrl)) != null ? _f : void 0;
361
+ if (this.debug && this.localDebuggerUrl) {
362
+ console.log(`${this.prefix} Local debugger mirroring: ${this.localDebuggerUrl}`);
363
+ }
252
364
  const isNode = typeof process !== "undefined" && typeof process.version === "string";
253
365
  this.context = {
254
366
  library: {
255
- name: (_f = opts.libraryName) != null ? _f : "@raindrop-ai/core",
256
- version: (_g = opts.libraryVersion) != null ? _g : "0.0.0"
367
+ name: (_g = opts.libraryName) != null ? _g : "@raindrop-ai/core",
368
+ version: (_h = opts.libraryVersion) != null ? _h : "0.0.0"
257
369
  },
258
370
  metadata: {
259
371
  jsRuntime: isNode ? "node" : "web",
@@ -339,6 +451,7 @@ var EventShipper = class {
339
451
  }
340
452
  }
341
453
  ];
454
+ if (!this.writeKey) return;
342
455
  const url = `${this.baseUrl}signals/track`;
343
456
  try {
344
457
  await postJson(url, body, this.authHeaders(), {
@@ -369,6 +482,7 @@ var EventShipper = class {
369
482
  traits: (_a = user.traits) != null ? _a : {}
370
483
  };
371
484
  });
485
+ if (!this.writeKey) return;
372
486
  if (body.length === 0) return;
373
487
  const url = `${this.baseUrl}users/identify`;
374
488
  try {
@@ -445,6 +559,18 @@ var EventShipper = class {
445
559
  endpoint: url
446
560
  });
447
561
  }
562
+ if (this.localDebuggerUrl) {
563
+ mirrorPartialEventToLocalDebugger(payload, {
564
+ baseUrl: this.localDebuggerUrl,
565
+ writeKey: this.writeKey,
566
+ debug: this.debug,
567
+ sdkName: this.sdkName
568
+ });
569
+ }
570
+ if (!this.writeKey) {
571
+ if (!isPending) this.sticky.delete(eventId);
572
+ return;
573
+ }
448
574
  const p = postJson(url, payload, this.authHeaders(), {
449
575
  maxAttempts: 3,
450
576
  debug: this.debug,
@@ -469,101 +595,94 @@ var EventShipper = class {
469
595
  }
470
596
  }
471
597
  };
472
- var LOCAL_DEBUGGER_ENV_VAR = "RAINDROP_LOCAL_DEBUGGER";
473
- var WORKSHOP_ENV_VAR = "RAINDROP_WORKSHOP";
474
- var DEFAULT_LOCAL_WORKSHOP_URL = "http://localhost:5899/v1/";
475
- function readEnvVar(name) {
476
- var _a;
477
- try {
478
- const env = (_a = globalThis == null ? void 0 : globalThis.process) == null ? void 0 : _a.env;
479
- if (env && typeof env[name] === "string" && env[name].length > 0) {
480
- return env[name];
598
+ var DEFAULT_SECRET_KEY_NAMES = [
599
+ "apikey",
600
+ "apisecret",
601
+ "apitoken",
602
+ "secretaccesskey",
603
+ "sessiontoken",
604
+ "privatekey",
605
+ "privatekeyid",
606
+ "clientsecret",
607
+ "accesstoken",
608
+ "refreshtoken",
609
+ "oauthtoken",
610
+ "bearertoken",
611
+ "authorization",
612
+ "password",
613
+ "passphrase"
614
+ ];
615
+ var REDACTED_PLACEHOLDER = "[REDACTED]";
616
+ function normalizeKeyName(name) {
617
+ return name.toLowerCase().replace(/[-_.]/g, "");
618
+ }
619
+ function redactSecretsInObject(value, options) {
620
+ var _a, _b;
621
+ const normalizedSecretSet = buildSecretSet((_a = options == null ? void 0 : options.secretKeyNames) != null ? _a : DEFAULT_SECRET_KEY_NAMES);
622
+ const placeholder = (_b = options == null ? void 0 : options.placeholder) != null ? _b : REDACTED_PLACEHOLDER;
623
+ const seen = /* @__PURE__ */ new WeakSet();
624
+ const walk = (node) => {
625
+ if (node === null || typeof node !== "object") return node;
626
+ if (seen.has(node)) return "[CIRCULAR]";
627
+ seen.add(node);
628
+ if (Array.isArray(node)) {
629
+ return node.map((item) => walk(item));
481
630
  }
631
+ const out = {};
632
+ for (const [k, v] of Object.entries(node)) {
633
+ if (normalizedSecretSet.has(normalizeKeyName(k))) {
634
+ out[k] = placeholder;
635
+ } else {
636
+ out[k] = walk(v);
637
+ }
638
+ }
639
+ return out;
640
+ };
641
+ return walk(value);
642
+ }
643
+ function buildSecretSet(names) {
644
+ const set = /* @__PURE__ */ new Set();
645
+ for (const name of names) set.add(normalizeKeyName(name));
646
+ return set;
647
+ }
648
+ var DEFAULT_REDACT_ATTRIBUTE_KEYS = [
649
+ "ai.request.providerOptions",
650
+ "ai.response.providerMetadata"
651
+ ];
652
+ function defaultTransformSpan(span) {
653
+ const attrs = span.attributes;
654
+ if (!attrs || attrs.length === 0) return span;
655
+ let nextAttrs;
656
+ for (let i = 0; i < attrs.length; i++) {
657
+ const attr = attrs[i];
658
+ const redacted = redactJsonAttributeValue(attr.key, attr.value);
659
+ if (redacted === void 0) continue;
660
+ if (!nextAttrs) nextAttrs = attrs.slice();
661
+ nextAttrs[i] = { key: attr.key, value: redacted };
662
+ }
663
+ if (!nextAttrs) return span;
664
+ return { ...span, attributes: nextAttrs };
665
+ }
666
+ var REDACT_JSON_ATTRIBUTE_KEYS = new Set(DEFAULT_REDACT_ATTRIBUTE_KEYS);
667
+ function redactJsonAttributeValue(key, value) {
668
+ if (!REDACT_JSON_ATTRIBUTE_KEYS.has(key)) return void 0;
669
+ const json = value.stringValue;
670
+ if (typeof json !== "string" || json.length === 0) return void 0;
671
+ let parsed;
672
+ try {
673
+ parsed = JSON.parse(json);
482
674
  } catch (e) {
675
+ return void 0;
483
676
  }
484
- return void 0;
485
- }
486
- function readWorkshopEnv() {
487
- const raw = readEnvVar(WORKSHOP_ENV_VAR);
488
- if (raw === void 0) return void 0;
489
- const trimmed = raw.trim();
490
- if (trimmed.length === 0) return void 0;
491
- if (/^https?:\/\//i.test(trimmed)) return { url: trimmed };
492
- if (/^(1|true|yes|on)$/i.test(trimmed)) return "enable";
493
- if (/^(0|false|no|off)$/i.test(trimmed)) return "disable";
494
- return void 0;
495
- }
496
- function isLocalDevHost(hostname) {
497
- if (!hostname) return false;
498
- if (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "0.0.0.0" || hostname === "::1") {
499
- return true;
500
- }
501
- if (hostname.endsWith(".localhost")) return true;
502
- return false;
503
- }
504
- function readRuntimeHostname() {
677
+ const scrubbed = redactSecretsInObject(parsed);
678
+ let scrubbedJson;
505
679
  try {
506
- const loc = globalThis == null ? void 0 : globalThis.location;
507
- if (loc && typeof loc.hostname === "string" && loc.hostname.length > 0) {
508
- return loc.hostname;
509
- }
680
+ scrubbedJson = JSON.stringify(scrubbed);
510
681
  } catch (e) {
682
+ return void 0;
511
683
  }
512
- return void 0;
513
- }
514
- function shouldAutoEnableLocalWorkshop() {
515
- if (isLocalDevHost(readRuntimeHostname())) return true;
516
- if (readEnvVar("NODE_ENV") === "development") return true;
517
- return false;
518
- }
519
- function resolveLocalDebuggerBaseUrl(baseUrl) {
520
- var _a, _b, _c;
521
- if (baseUrl === null) return null;
522
- if (typeof baseUrl === "string" && baseUrl.length > 0) {
523
- return (_a = formatEndpoint(baseUrl)) != null ? _a : null;
524
- }
525
- const explicitUrlEnv = readEnvVar(LOCAL_DEBUGGER_ENV_VAR);
526
- if (explicitUrlEnv) return (_b = formatEndpoint(explicitUrlEnv)) != null ? _b : null;
527
- const workshopEnv = readWorkshopEnv();
528
- if (workshopEnv === "disable") return null;
529
- if (workshopEnv === "enable") return DEFAULT_LOCAL_WORKSHOP_URL;
530
- if (workshopEnv && "url" in workshopEnv) return (_c = formatEndpoint(workshopEnv.url)) != null ? _c : null;
531
- if (shouldAutoEnableLocalWorkshop()) return DEFAULT_LOCAL_WORKSHOP_URL;
532
- return null;
533
- }
534
- function localDebuggerEnabled(baseUrl) {
535
- return resolveLocalDebuggerBaseUrl(baseUrl) !== null;
536
- }
537
- function mirrorTraceExportToLocalDebugger(body, options = {}) {
538
- var _a;
539
- const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
540
- if (!baseUrl) return;
541
- void postJson(`${baseUrl}traces`, body, {}, {
542
- maxAttempts: 1,
543
- debug: (_a = options.debug) != null ? _a : false,
544
- sdkName: options.sdkName
545
- }).catch(() => {
546
- });
547
- }
548
- function sendLocalDebuggerLiveEvent(event, options = {}) {
549
- var _a, _b;
550
- const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
551
- if (!baseUrl) return;
552
- void postJson(
553
- `${baseUrl}live`,
554
- {
555
- ...event,
556
- type: event.type,
557
- timestamp: (_a = event.timestamp) != null ? _a : Date.now()
558
- },
559
- {},
560
- {
561
- maxAttempts: 1,
562
- debug: (_b = options.debug) != null ? _b : false,
563
- sdkName: options.sdkName
564
- }
565
- ).catch(() => {
566
- });
684
+ if (scrubbedJson === json) return void 0;
685
+ return { stringValue: scrubbedJson };
567
686
  }
568
687
  var TraceShipper = class {
569
688
  constructor(opts) {
@@ -586,6 +705,42 @@ var TraceShipper = class {
586
705
  if (this.debug && this.localDebuggerUrl) {
587
706
  console.log(`${this.prefix} Local debugger mirroring: ${this.localDebuggerUrl}`);
588
707
  }
708
+ this.transformSpanHook = opts.transformSpan;
709
+ this.disableDefaultRedaction = opts.disableDefaultRedaction === true;
710
+ }
711
+ /**
712
+ * Apply the user `transformSpan` hook (if any) followed by the default
713
+ * redactor (unless disabled). Returns either the (possibly new) span to
714
+ * ship, or `null` to drop the span entirely.
715
+ *
716
+ * Ordering: user hook runs first so callers can rewrite the span freely
717
+ * (rename attrs, add new ones, scrub things the default doesn't know
718
+ * about). The default redactor then runs on whatever the user produced,
719
+ * acting as the always-on floor for documented BYOK secrets. If the user
720
+ * sets `disableDefaultRedaction: true`, the floor is skipped.
721
+ *
722
+ * Fail-closed: if the user hook throws, the span is dropped — a buggy
723
+ * hook can never accidentally ship raw, un-redacted spans.
724
+ */
725
+ redactSpan(span) {
726
+ let current = span;
727
+ if (this.transformSpanHook) {
728
+ try {
729
+ const result = this.transformSpanHook(current);
730
+ if (result === null) return null;
731
+ if (result !== void 0) current = result;
732
+ } catch (err) {
733
+ if (this.debug) {
734
+ const msg = err instanceof Error ? err.message : String(err);
735
+ console.warn(`${this.prefix} transformSpan hook threw: ${msg}`);
736
+ }
737
+ return null;
738
+ }
739
+ }
740
+ if (!this.disableDefaultRedaction) {
741
+ current = defaultTransformSpan(current);
742
+ }
743
+ return current;
589
744
  }
590
745
  isDebugEnabled() {
591
746
  return this.debug;
@@ -603,8 +758,8 @@ var TraceShipper = class {
603
758
  ];
604
759
  if ((_b = args.attributes) == null ? void 0 : _b.length) attrs.push(...args.attributes);
605
760
  const span = { ids, name: args.name, startTimeUnixNano: started, attributes: attrs };
606
- if (this.localDebuggerUrl) {
607
- const openSpan = buildOtlpSpan({
761
+ this.mirrorToLocalDebugger(
762
+ buildOtlpSpan({
608
763
  ids: span.ids,
609
764
  name: span.name,
610
765
  startTimeUnixNano: span.startTimeUnixNano,
@@ -612,16 +767,21 @@ var TraceShipper = class {
612
767
  // placeholder — will be updated on endSpan
613
768
  attributes: span.attributes,
614
769
  status: { code: SpanStatusCode.UNSET }
615
- });
616
- const body = buildExportTraceServiceRequest([openSpan], this.serviceName, this.serviceVersion);
617
- mirrorTraceExportToLocalDebugger(body, {
618
- baseUrl: this.localDebuggerUrl,
619
- debug: false,
620
- sdkName: this.sdkName
621
- });
622
- }
770
+ })
771
+ );
623
772
  return span;
624
773
  }
774
+ mirrorToLocalDebugger(span) {
775
+ if (!this.localDebuggerUrl) return;
776
+ const redacted = this.redactSpan(span);
777
+ if (redacted === null) return;
778
+ const body = buildExportTraceServiceRequest([redacted], this.serviceName, this.serviceVersion);
779
+ mirrorTraceExportToLocalDebugger(body, {
780
+ baseUrl: this.localDebuggerUrl,
781
+ debug: false,
782
+ sdkName: this.sdkName
783
+ });
784
+ }
625
785
  endSpan(span, extra) {
626
786
  var _a, _b;
627
787
  if (span.endTimeUnixNano) return;
@@ -643,14 +803,7 @@ var TraceShipper = class {
643
803
  status
644
804
  });
645
805
  this.enqueue(otlp);
646
- if (this.localDebuggerUrl) {
647
- const body = buildExportTraceServiceRequest([otlp], this.serviceName, this.serviceVersion);
648
- mirrorTraceExportToLocalDebugger(body, {
649
- baseUrl: this.localDebuggerUrl,
650
- debug: false,
651
- sdkName: this.sdkName
652
- });
653
- }
806
+ this.mirrorToLocalDebugger(otlp);
654
807
  }
655
808
  createSpan(args) {
656
809
  var _a;
@@ -668,14 +821,7 @@ var TraceShipper = class {
668
821
  status: args.status
669
822
  });
670
823
  this.enqueue(otlp);
671
- if (this.localDebuggerUrl) {
672
- const body = buildExportTraceServiceRequest([otlp], this.serviceName, this.serviceVersion);
673
- mirrorTraceExportToLocalDebugger(body, {
674
- baseUrl: this.localDebuggerUrl,
675
- debug: false,
676
- sdkName: this.sdkName
677
- });
678
- }
824
+ this.mirrorToLocalDebugger(otlp);
679
825
  }
680
826
  enqueue(span) {
681
827
  if (!this.enabled) return;
@@ -687,10 +833,12 @@ var TraceShipper = class {
687
833
  )}`
688
834
  );
689
835
  }
836
+ const redacted = this.redactSpan(span);
837
+ if (redacted === null) return;
690
838
  if (this.queue.length >= this.maxQueueSize) {
691
839
  this.queue.shift();
692
840
  }
693
- this.queue.push(span);
841
+ this.queue.push(redacted);
694
842
  if (this.queue.length >= this.maxBatchSize) {
695
843
  void this.flush().catch(() => {
696
844
  });
@@ -712,6 +860,7 @@ var TraceShipper = class {
712
860
  }
713
861
  while (this.queue.length > 0) {
714
862
  const batch = this.queue.splice(0, this.maxBatchSize);
863
+ if (!this.writeKey) continue;
715
864
  const body = buildExportTraceServiceRequest(batch, this.serviceName, this.serviceVersion);
716
865
  const url = `${this.baseUrl}traces`;
717
866
  if (this.debug) {
@@ -1565,13 +1714,76 @@ function readRaindropCallMetadataFromArgs(args) {
1565
1714
  return meta;
1566
1715
  }
1567
1716
 
1717
+ // src/internal/parent-tool-context.ts
1718
+ var SyncFallbackStorage2 = class {
1719
+ constructor() {
1720
+ this._stack = [];
1721
+ }
1722
+ getStore() {
1723
+ return this._stack[this._stack.length - 1];
1724
+ }
1725
+ run(store, callback) {
1726
+ this._stack.push(store);
1727
+ try {
1728
+ return callback();
1729
+ } finally {
1730
+ this._stack.pop();
1731
+ }
1732
+ }
1733
+ // `enterWith` is intentionally absent on the sync fallback — the
1734
+ // event-driven enter/clear pattern only makes sense when the runtime
1735
+ // can propagate state across awaits, which the sync stack cannot.
1736
+ };
1737
+ var _storage2 = null;
1738
+ function getStorage2() {
1739
+ if (_storage2) return _storage2;
1740
+ const Ctor = globalThis.RAINDROP_ASYNC_LOCAL_STORAGE;
1741
+ _storage2 = Ctor ? new Ctor() : new SyncFallbackStorage2();
1742
+ return _storage2;
1743
+ }
1744
+ function _resetParentToolContextStorage() {
1745
+ _storage2 = null;
1746
+ }
1747
+ function getCurrentParentToolContext() {
1748
+ return getStorage2().getStore();
1749
+ }
1750
+ function enterParentToolContext(ctx) {
1751
+ var _a;
1752
+ const storage = getStorage2();
1753
+ (_a = storage.enterWith) == null ? void 0 : _a.call(storage, ctx);
1754
+ }
1755
+ function clearParentToolContext() {
1756
+ var _a;
1757
+ const storage = getStorage2();
1758
+ (_a = storage.enterWith) == null ? void 0 : _a.call(storage, void 0);
1759
+ }
1760
+ function runWithParentToolContext(ctx, fn) {
1761
+ return getStorage2().run(ctx, fn);
1762
+ }
1763
+
1568
1764
  // src/internal/raindrop-telemetry-integration.ts
1569
1765
  var RaindropTelemetryIntegration = class {
1570
1766
  constructor(opts) {
1571
1767
  this.callStates = /* @__PURE__ */ new Map();
1768
+ /**
1769
+ * Per-tool-call snapshot of the parent-tool ALS context taken right
1770
+ * before `enterParentToolContext` overwrites it in `toolExecutionStart`.
1771
+ * Kept at the integration level (rather than on `CallState`) so the
1772
+ * snapshot survives even when the parent generation has no tracked
1773
+ * `CallState` — e.g. the AI SDK dispatches a tool callback for a
1774
+ * `callId` we never registered via `onStart`. Without this, the
1775
+ * unconditional ALS enter in `toolExecutionStart` would leave
1776
+ * `toolExecutionEnd` no way to restore the prior context, so it would
1777
+ * clear and wipe whatever the outer scope had set.
1778
+ *
1779
+ * Keyed by `toolCallId` because that's the only identifier guaranteed
1780
+ * to round-trip between `toolExecutionStart` and `toolExecutionEnd`
1781
+ * (the `event.callId` can be the same for parallel sibling tools).
1782
+ */
1783
+ this.priorParentContexts = /* @__PURE__ */ new Map();
1572
1784
  // ── onStart ─────────────────────────────────────────────────────────────
1573
1785
  this.onStart = (event) => {
1574
- var _a, _b, _c, _d;
1786
+ var _a, _b, _c, _d, _e;
1575
1787
  if (event.isEnabled === false) return;
1576
1788
  const isEmbed = event.operationId === "ai.embed" || event.operationId === "ai.embedMany";
1577
1789
  const recordInputs = event.recordInputs !== false;
@@ -1590,6 +1802,47 @@ var RaindropTelemetryIntegration = class {
1590
1802
  event.operationId,
1591
1803
  functionId
1592
1804
  );
1805
+ const parentToolContext = !isEmbed && this.subagentWrapping ? getCurrentParentToolContext() : void 0;
1806
+ const metadataSubagentName = !isEmbed && this.subagentWrapping && parentToolContext === void 0 ? typeof (metadata == null ? void 0 : metadata["ash.subagent.name"]) === "string" ? metadata["ash.subagent.name"] : void 0 : void 0;
1807
+ const subagentName = (_e = parentToolContext == null ? void 0 : parentToolContext.toolName) != null ? _e : metadataSubagentName;
1808
+ let subagentToolCallSpan;
1809
+ let rootParentOverride;
1810
+ if (subagentName && this.sendTraces) {
1811
+ const parentAttrs = parentToolContext ? [
1812
+ attrString("raindrop.parent.callId", parentToolContext.parentCallId),
1813
+ attrString("raindrop.parent.toolCallId", parentToolContext.toolCallId),
1814
+ attrString("raindrop.parent.toolName", parentToolContext.toolName)
1815
+ ] : [];
1816
+ subagentToolCallSpan = this.traceShipper.startSpan({
1817
+ name: subagentName,
1818
+ parent: inheritedParent,
1819
+ eventId,
1820
+ operationId: "ai.toolCall",
1821
+ attributes: [
1822
+ attrString("operation.name", "ai.toolCall"),
1823
+ attrString("resource.name", subagentName),
1824
+ attrString("raindrop.span.kind", "tool_call"),
1825
+ attrString("ai.toolCall.name", subagentName),
1826
+ attrString("raindrop.subagent.name", subagentName),
1827
+ attrString("raindrop.agent.role", "subagent"),
1828
+ ...parentAttrs
1829
+ ]
1830
+ });
1831
+ const subagentParentRef = this.spanParentRef(subagentToolCallSpan);
1832
+ const markerSpan = this.traceShipper.startSpan({
1833
+ name: "agent.subagent",
1834
+ parent: subagentParentRef,
1835
+ eventId,
1836
+ operationId: "agent.subagent",
1837
+ attributes: [
1838
+ attrString("operation.name", "agent.subagent"),
1839
+ attrString("raindrop.span.kind", "llm_call"),
1840
+ attrString("raindrop.subagent.name", subagentName)
1841
+ ]
1842
+ });
1843
+ this.traceShipper.endSpan(markerSpan);
1844
+ rootParentOverride = subagentParentRef;
1845
+ }
1593
1846
  let rootSpan;
1594
1847
  if (this.sendTraces) {
1595
1848
  const promptAttrs = !isEmbed && recordInputs ? [
@@ -1610,7 +1863,7 @@ var RaindropTelemetryIntegration = class {
1610
1863
  ] : [attrString("ai.value", safeJsonWithUint8(event.value))] : [];
1611
1864
  rootSpan = this.traceShipper.startSpan({
1612
1865
  name: event.operationId,
1613
- parent: inheritedParent,
1866
+ parent: rootParentOverride != null ? rootParentOverride : inheritedParent,
1614
1867
  eventId,
1615
1868
  operationId: event.operationId,
1616
1869
  attributes: [
@@ -1638,18 +1891,21 @@ var RaindropTelemetryIntegration = class {
1638
1891
  operationId: event.operationId,
1639
1892
  eventId,
1640
1893
  rootSpan,
1641
- rootParent: rootSpan ? this.spanParentRef(rootSpan) : inheritedParent,
1894
+ rootParent: rootSpan ? this.spanParentRef(rootSpan) : rootParentOverride != null ? rootParentOverride : inheritedParent,
1642
1895
  stepSpan: void 0,
1643
1896
  stepParent: void 0,
1644
1897
  toolSpans: /* @__PURE__ */ new Map(),
1645
1898
  embedSpans: /* @__PURE__ */ new Map(),
1899
+ parentContextToolCallIds: /* @__PURE__ */ new Set(),
1646
1900
  recordInputs,
1647
1901
  recordOutputs,
1648
1902
  functionId,
1649
1903
  metadata,
1650
1904
  accumulatedText: "",
1651
1905
  inputText: isEmbed ? void 0 : this.extractInputText(event),
1652
- toolCallCount: 0
1906
+ toolCallCount: 0,
1907
+ subagentName,
1908
+ subagentToolCallSpan
1653
1909
  });
1654
1910
  };
1655
1911
  // ── onStepStart ─────────────────────────────────────────────────────────
@@ -1873,6 +2129,9 @@ var RaindropTelemetryIntegration = class {
1873
2129
  } else {
1874
2130
  this.finishGenerate(event, state);
1875
2131
  }
2132
+ if (state.subagentToolCallSpan) {
2133
+ this.traceShipper.endSpan(state.subagentToolCallSpan);
2134
+ }
1876
2135
  this.cleanup(event.callId);
1877
2136
  };
1878
2137
  // ── onError ─────────────────────────────────────────────────────────────
@@ -1890,6 +2149,10 @@ var RaindropTelemetryIntegration = class {
1890
2149
  this.traceShipper.endSpan(embedSpan, { error: actualError });
1891
2150
  }
1892
2151
  state.embedSpans.clear();
2152
+ for (const toolCallId of state.parentContextToolCallIds) {
2153
+ this.priorParentContexts.delete(toolCallId);
2154
+ }
2155
+ state.parentContextToolCallIds.clear();
1893
2156
  for (const toolSpan of state.toolSpans.values()) {
1894
2157
  this.traceShipper.endSpan(toolSpan, { error: actualError });
1895
2158
  }
@@ -1897,6 +2160,9 @@ var RaindropTelemetryIntegration = class {
1897
2160
  if (state.rootSpan) {
1898
2161
  this.traceShipper.endSpan(state.rootSpan, { error: actualError });
1899
2162
  }
2163
+ if (state.subagentToolCallSpan) {
2164
+ this.traceShipper.endSpan(state.subagentToolCallSpan, { error: actualError });
2165
+ }
1900
2166
  this.cleanup(event.callId);
1901
2167
  };
1902
2168
  // ── executeTool ─────────────────────────────────────────────────────────
@@ -1921,6 +2187,7 @@ var RaindropTelemetryIntegration = class {
1921
2187
  this.eventShipper = opts.eventShipper;
1922
2188
  this.sendTraces = opts.sendTraces !== false;
1923
2189
  this.sendEvents = opts.sendEvents !== false;
2190
+ this.subagentWrapping = opts.subagentWrapping !== false;
1924
2191
  this.debug = opts.debug === true;
1925
2192
  this.defaultContext = opts.context;
1926
2193
  }
@@ -2002,9 +2269,20 @@ var RaindropTelemetryIntegration = class {
2002
2269
  // (see https://github.com/vercel/ai/pull/14589). Event shape is identical.
2003
2270
  // Both names are exposed and forward to a single implementation.
2004
2271
  toolExecutionStart(event) {
2272
+ const { toolCall } = event;
2273
+ const priorParent = getCurrentParentToolContext();
2274
+ this.priorParentContexts.set(
2275
+ toolCall.toolCallId,
2276
+ priorParent != null ? priorParent : null
2277
+ );
2278
+ enterParentToolContext({
2279
+ parentCallId: event.callId,
2280
+ toolCallId: toolCall.toolCallId,
2281
+ toolName: toolCall.toolName
2282
+ });
2005
2283
  const state = this.getState(event.callId);
2284
+ state == null ? void 0 : state.parentContextToolCallIds.add(toolCall.toolCallId);
2006
2285
  if (!(state == null ? void 0 : state.stepParent)) return;
2007
- const { toolCall } = event;
2008
2286
  const { operationName, resourceName } = opName(
2009
2287
  "ai.toolCall",
2010
2288
  state.functionId
@@ -2028,6 +2306,17 @@ var RaindropTelemetryIntegration = class {
2028
2306
  this.emitLive(state, "tool_start", toolCall.toolName, { args: toolCall.input });
2029
2307
  }
2030
2308
  toolExecutionEnd(event) {
2309
+ var _a;
2310
+ const toolCallId = (_a = event.toolCall) == null ? void 0 : _a.toolCallId;
2311
+ if (toolCallId && this.priorParentContexts.has(toolCallId)) {
2312
+ const prior = this.priorParentContexts.get(toolCallId);
2313
+ this.priorParentContexts.delete(toolCallId);
2314
+ if (prior) {
2315
+ enterParentToolContext(prior);
2316
+ } else {
2317
+ clearParentToolContext();
2318
+ }
2319
+ }
2031
2320
  const state = this.getState(event.callId);
2032
2321
  if (!state) return;
2033
2322
  const toolSpan = state.toolSpans.get(event.toolCall.toolCallId);
@@ -2091,7 +2380,8 @@ var RaindropTelemetryIntegration = class {
2091
2380
  );
2092
2381
  this.traceShipper.endSpan(state.rootSpan, { attributes: outputAttrs });
2093
2382
  }
2094
- if (this.sendEvents) {
2383
+ const suppressSubagentEvent = state.subagentName !== void 0 && state.subagentToolCallSpan !== void 0;
2384
+ if (this.sendEvents && !suppressSubagentEvent) {
2095
2385
  const callMeta = this.extractRaindropMetadata(state.metadata);
2096
2386
  const userId = (_f = callMeta.userId) != null ? _f : (_e = this.defaultContext) == null ? void 0 : _e.userId;
2097
2387
  if (userId) {
@@ -4184,7 +4474,7 @@ function extractNestedTokens(usage, key) {
4184
4474
  // package.json
4185
4475
  var package_default = {
4186
4476
  name: "@raindrop-ai/ai-sdk",
4187
- version: "0.0.28"};
4477
+ version: "0.0.30"};
4188
4478
 
4189
4479
  // src/internal/version.ts
4190
4480
  var libraryName = package_default.name;
@@ -4271,16 +4561,20 @@ function envDebugEnabled() {
4271
4561
  return flag === "1" || flag === "true";
4272
4562
  }
4273
4563
  function createRaindropAISDK(opts) {
4274
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
4564
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
4275
4565
  const writeKey = opts.writeKey;
4276
4566
  const eventsRequested = ((_a = opts.events) == null ? void 0 : _a.enabled) !== false;
4277
4567
  const tracesRequested = ((_b = opts.traces) == null ? void 0 : _b.enabled) !== false;
4278
- const eventsEnabled = eventsRequested && !!writeKey;
4279
- const tracesEnabled = tracesRequested && !!writeKey;
4280
4568
  const envDebug = envDebugEnabled();
4281
- if (!writeKey && (eventsRequested || tracesRequested)) {
4569
+ const localWorkshopInput = opts.localWorkshopUrl === false ? null : opts.localWorkshopUrl;
4570
+ const resolvedLocalDebuggerUrl = resolveLocalDebuggerBaseUrl(localWorkshopInput);
4571
+ const localDebuggerUrl = localWorkshopInput === null ? null : resolvedLocalDebuggerUrl != null ? resolvedLocalDebuggerUrl : void 0;
4572
+ const hasDestination = !!writeKey || !!resolvedLocalDebuggerUrl;
4573
+ const eventsEnabled = eventsRequested && hasDestination;
4574
+ const tracesEnabled = tracesRequested && hasDestination;
4575
+ if (!hasDestination && (eventsRequested || tracesRequested)) {
4282
4576
  console.warn(
4283
- "[raindrop-ai/ai-sdk] writeKey not provided; telemetry shipping is disabled"
4577
+ "[raindrop-ai/ai-sdk] writeKey not provided and no local Workshop reachable; telemetry shipping is disabled"
4284
4578
  );
4285
4579
  }
4286
4580
  const eventShipper = new EventShipper2({
@@ -4288,9 +4582,9 @@ function createRaindropAISDK(opts) {
4288
4582
  endpoint: opts.endpoint,
4289
4583
  enabled: eventsEnabled,
4290
4584
  debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true || envDebug,
4291
- partialFlushMs: (_d = opts.events) == null ? void 0 : _d.partialFlushMs
4585
+ partialFlushMs: (_d = opts.events) == null ? void 0 : _d.partialFlushMs,
4586
+ localDebuggerUrl
4292
4587
  });
4293
- const localDebuggerUrl = opts.localWorkshopUrl === false ? null : opts.localWorkshopUrl;
4294
4588
  const traceShipper = new TraceShipper2({
4295
4589
  writeKey,
4296
4590
  endpoint: opts.endpoint,
@@ -4300,7 +4594,9 @@ function createRaindropAISDK(opts) {
4300
4594
  flushIntervalMs: (_g = opts.traces) == null ? void 0 : _g.flushIntervalMs,
4301
4595
  maxBatchSize: (_h = opts.traces) == null ? void 0 : _h.maxBatchSize,
4302
4596
  maxQueueSize: (_i = opts.traces) == null ? void 0 : _i.maxQueueSize,
4303
- localDebuggerUrl
4597
+ localDebuggerUrl,
4598
+ transformSpan: (_j = opts.traces) == null ? void 0 : _j.transformSpan,
4599
+ disableDefaultRedaction: (_k = opts.traces) == null ? void 0 : _k.disableDefaultRedaction
4304
4600
  });
4305
4601
  return {
4306
4602
  wrap(aiSDK, options) {
@@ -4310,12 +4606,30 @@ function createRaindropAISDK(opts) {
4310
4606
  traceShipper
4311
4607
  });
4312
4608
  },
4313
- createTelemetryIntegration(context) {
4609
+ createTelemetryIntegration(contextOrOptions) {
4610
+ const FLAT_CONTEXT_KEYS = [
4611
+ "userId",
4612
+ "eventId",
4613
+ "eventName",
4614
+ "convoId",
4615
+ "properties"
4616
+ ];
4617
+ let context;
4618
+ let subagentWrapping;
4619
+ if (contextOrOptions === void 0) ; else if ("context" in contextOrOptions) {
4620
+ context = contextOrOptions.context;
4621
+ subagentWrapping = contextOrOptions.subagentWrapping;
4622
+ } else if ("subagentWrapping" in contextOrOptions && !FLAT_CONTEXT_KEYS.some((k) => k in contextOrOptions)) {
4623
+ subagentWrapping = contextOrOptions.subagentWrapping;
4624
+ } else {
4625
+ context = contextOrOptions;
4626
+ }
4314
4627
  return new RaindropTelemetryIntegration({
4315
4628
  traceShipper,
4316
4629
  eventShipper,
4317
4630
  sendTraces: tracesEnabled,
4318
4631
  sendEvents: eventsEnabled,
4632
+ subagentWrapping,
4319
4633
  debug: envDebug,
4320
4634
  context
4321
4635
  });
@@ -4423,4 +4737,4 @@ function createRaindropAISDK(opts) {
4423
4737
  };
4424
4738
  }
4425
4739
 
4426
- export { RaindropTelemetryIntegration, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, createRaindropAISDK, currentSpan, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentRaindropCallMetadata, readRaindropCallMetadataFromArgs, runWithRaindropCallMetadata, withCurrent };
4740
+ export { DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, REDACTED_PLACEHOLDER, RaindropTelemetryIntegration, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };