@withgateway/sdk 0.2.8 → 0.3.0

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.
Files changed (61) hide show
  1. package/README.md +24 -8
  2. package/dist/cli.js +2695 -657
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +2675 -637
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/files/index.d.mts +1 -0
  7. package/dist/files/index.d.ts +1 -0
  8. package/dist/files/index.js +1443 -36
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +1441 -28
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/{index-Cz8Q6Keo.d.mts → index-D6oi1TWE.d.mts} +93 -34
  13. package/dist/{index-BO2SNSgy.d.ts → index-DAb-FCMg.d.ts} +93 -34
  14. package/dist/index.d.mts +4 -3
  15. package/dist/index.d.ts +4 -3
  16. package/dist/index.js +1986 -235
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +1941 -218
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/media-CbcAfQvd.d.mts +83 -0
  21. package/dist/media-CbcAfQvd.d.ts +83 -0
  22. package/dist/traces/index.d.mts +1 -0
  23. package/dist/traces/index.d.ts +1 -0
  24. package/dist/traces/index.js +1440 -33
  25. package/dist/traces/index.js.map +1 -1
  26. package/dist/traces/index.mjs +1438 -25
  27. package/dist/traces/index.mjs.map +1 -1
  28. package/dist/tracing/index.d.mts +2 -1
  29. package/dist/tracing/index.d.ts +2 -1
  30. package/dist/tracing/index.js +512 -11
  31. package/dist/tracing/index.js.map +1 -1
  32. package/dist/tracing/index.mjs +501 -10
  33. package/dist/tracing/index.mjs.map +1 -1
  34. package/dist/worlds/index.d.mts +614 -25
  35. package/dist/worlds/index.d.ts +614 -25
  36. package/dist/worlds/index.js +1923 -214
  37. package/dist/worlds/index.js.map +1 -1
  38. package/dist/worlds/index.mjs +1910 -201
  39. package/dist/worlds/index.mjs.map +1 -1
  40. package/package.json +1 -1
  41. package/runtime/gatewaysdk/environment/_toml.py +3 -10
  42. package/runtime/gatewaysdk/environment/schema/api.py +38 -56
  43. package/runtime/gatewaysdk/environment/schema/grading.py +1 -5
  44. package/runtime/gatewaysdk/environment/schema/host.py +53 -90
  45. package/runtime/gatewaysdk/environment/schema/local.py +15 -7
  46. package/runtime/gatewaysdk/environment/schema/runtime.py +9 -9
  47. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +3 -3
  48. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +1 -1
  49. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +18 -18
  50. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +4 -2
  51. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +5 -1
  52. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +5 -3
  53. package/runtime/gatewaysdk/environment/schema/store.py +15 -6
  54. package/runtime/gatewaysdk/environment/schema/tasks.py +25 -1
  55. package/skills/benchmarks-getting-started/SKILL.md +3 -3
  56. package/skills/connector-schema-authoring/SKILL.md +1 -1
  57. package/skills/connector-schema-authoring/world-schema.md +1 -1
  58. package/skills/worlds-getting-started/SKILL.md +18 -18
  59. package/skills/worlds-getting-started/references/cli-mcp-map.md +4 -2
  60. package/skills/worlds-getting-started/references/gotchas.md +5 -1
  61. package/skills/worlds-getting-started/references/new-world-sandbox.md +5 -3
@@ -20,7 +20,7 @@ var require_package = __commonJS({
20
20
  "package.json"(exports, module) {
21
21
  module.exports = {
22
22
  name: "@withgateway/sdk",
23
- version: "0.2.8",
23
+ version: "0.3.0",
24
24
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
25
25
  main: "./dist/index.js",
26
26
  module: "./dist/index.mjs",
@@ -486,26 +486,267 @@ function debugLog(message, ...args) {
486
486
  }
487
487
  }
488
488
 
489
+ // src/tracing/secrets.ts
490
+ var SECRET_KEYS = /* @__PURE__ */ new Set([
491
+ "authorization_token",
492
+ "authorizationToken"
493
+ ]);
494
+ var REDACTED = "[redacted: holds a credential]";
495
+ var MIN_SECRET_LENGTH = 8;
496
+ var SpanSecrets = class {
497
+ constructor(keys = SECRET_KEYS) {
498
+ this.keys = keys;
499
+ }
500
+ keys;
501
+ isSecret(key) {
502
+ return this.keys.has(key);
503
+ }
504
+ drops(attribute) {
505
+ return this.mentions(attribute);
506
+ }
507
+ strip(value) {
508
+ if (typeof value === "string") return this.stripNested(value);
509
+ if (Array.isArray(value)) return value.map((item) => this.strip(item));
510
+ if (value === null || typeof value !== "object") return value;
511
+ return Object.fromEntries(
512
+ Object.entries(value).filter(([key]) => !this.isSecret(key)).map(([key, item]) => [key, this.strip(item)])
513
+ );
514
+ }
515
+ scrubAttribute(value) {
516
+ try {
517
+ return this.scrubbedValue(value);
518
+ } catch {
519
+ return Array.isArray(value) ? [REDACTED] : REDACTED;
520
+ }
521
+ }
522
+ scrubbedValue(value) {
523
+ if (typeof value === "string") return this.scrubbedText(value);
524
+ if (!Array.isArray(value)) return value;
525
+ const results = value.map(
526
+ (item) => typeof item === "string" ? this.cleaned(item) : { text: item, removed: /* @__PURE__ */ new Set() }
527
+ );
528
+ const removed = new Set(results.flatMap((r) => [...r.removed]));
529
+ const scrubbed = results.map((r) => r.text);
530
+ const withheld = scrubbed.some(
531
+ (item) => typeof item === "string" && this.carries(item, removed)
532
+ );
533
+ return withheld ? [REDACTED] : scrubbed;
534
+ }
535
+ scrubAttributes(attributes) {
536
+ return Object.fromEntries(
537
+ Object.entries(attributes).filter(([key]) => !this.drops(key)).map(([key, value]) => [key, this.scrubAttribute(value)])
538
+ );
539
+ }
540
+ mentions(text) {
541
+ return [...this.keys].some((key) => text.includes(key));
542
+ }
543
+ scrubbedText(text) {
544
+ const { text: cleaned, removed } = this.cleaned(text);
545
+ return this.carries(cleaned, removed) ? REDACTED : cleaned;
546
+ }
547
+ cleaned(text) {
548
+ if (!this.mentions(text)) return { text, removed: /* @__PURE__ */ new Set() };
549
+ const parsed = jsonOrUndefined(text);
550
+ if (parsed === void 0) return { text: REDACTED, removed: /* @__PURE__ */ new Set() };
551
+ return {
552
+ text: JSON.stringify(this.strip(parsed)),
553
+ removed: this.removed(parsed)
554
+ };
555
+ }
556
+ carries(text, removed) {
557
+ if (text === REDACTED || this.mentions(text)) return true;
558
+ return [...removed].some(
559
+ (value) => encodings(value).some((form) => text.includes(form))
560
+ );
561
+ }
562
+ removed(value) {
563
+ if (typeof value === "string") {
564
+ const nested = this.mentions(value) ? jsonOrUndefined(value) : void 0;
565
+ return nested === void 0 ? /* @__PURE__ */ new Set() : this.removed(nested);
566
+ }
567
+ if (Array.isArray(value)) return union(value.map((v) => this.removed(v)));
568
+ if (value === null || typeof value !== "object") return /* @__PURE__ */ new Set();
569
+ return union(
570
+ Object.entries(value).map(
571
+ ([key, item]) => this.isSecret(key) ? leaves(item) : this.removed(item)
572
+ )
573
+ );
574
+ }
575
+ stripNested(text) {
576
+ if (!this.mentions(text)) return text;
577
+ const parsed = jsonOrUndefined(text);
578
+ if (parsed === void 0) return text;
579
+ return JSON.stringify(this.strip(parsed));
580
+ }
581
+ };
582
+ function jsonOrUndefined(text) {
583
+ let parsed;
584
+ try {
585
+ parsed = JSON.parse(text);
586
+ } catch {
587
+ return void 0;
588
+ }
589
+ if (typeof parsed === "string") return parsed;
590
+ return parsed !== null && typeof parsed === "object" ? parsed : void 0;
591
+ }
592
+ function encodings(value) {
593
+ return [value, percentEncoded(value), JSON.stringify(value).slice(1, -1)];
594
+ }
595
+ function percentEncoded(value) {
596
+ try {
597
+ return encodeURIComponent(value);
598
+ } catch {
599
+ return value;
600
+ }
601
+ }
602
+ function union(sets) {
603
+ return new Set(sets.flatMap((set) => [...set]));
604
+ }
605
+ function leaves(value) {
606
+ if (typeof value === "string") {
607
+ return [...value].length >= MIN_SECRET_LENGTH ? /* @__PURE__ */ new Set([value]) : /* @__PURE__ */ new Set();
608
+ }
609
+ if (Array.isArray(value)) return union(value.map(leaves));
610
+ if (value === null || typeof value !== "object") return /* @__PURE__ */ new Set();
611
+ return union(Object.values(value).map(leaves));
612
+ }
613
+ var SPAN_SECRETS = new SpanSecrets();
614
+ function sameAttributes(before, after) {
615
+ const keys = Object.keys(before);
616
+ if (keys.length !== Object.keys(after).length) return false;
617
+ return keys.every(
618
+ (key) => before[key] === after[key] || sameArray(before[key], after[key])
619
+ );
620
+ }
621
+ function sameArray(before, after) {
622
+ if (!Array.isArray(before) || !Array.isArray(after)) return false;
623
+ return before.length === after.length && before.every((item, i) => item === after[i]);
624
+ }
625
+ var SecretRedactingSpanProcessor = class {
626
+ constructor(inner, secrets = SPAN_SECRETS) {
627
+ this.inner = inner;
628
+ this.secrets = secrets;
629
+ }
630
+ inner;
631
+ secrets;
632
+ views = /* @__PURE__ */ new WeakMap();
633
+ onStart(span, parentContext) {
634
+ this.inner.onStart(this.viewFor(span), parentContext);
635
+ }
636
+ onEnd(span) {
637
+ const view = this.viewFor(span);
638
+ try {
639
+ void [view.attributes, view.events, view.status];
640
+ } catch (error) {
641
+ console.warn(
642
+ `[gatewaysdk.tracing] Dropped span "${span.name}": could not redact it: ${error?.name}`
643
+ );
644
+ return;
645
+ }
646
+ this.inner.onEnd(view);
647
+ }
648
+ viewFor(span) {
649
+ const known = this.views.get(span);
650
+ if (known) return known;
651
+ const view = viewOf(span, {
652
+ attributes: () => this.scrubbedAttributes(span.attributes),
653
+ events: () => span.events.map((event) => this.scrubbedEvent(event)),
654
+ status: () => this.scrubbedStatus(span.status)
655
+ });
656
+ this.views.set(span, view);
657
+ return view;
658
+ }
659
+ forceFlush() {
660
+ return this.inner.forceFlush();
661
+ }
662
+ shutdown() {
663
+ return this.inner.shutdown();
664
+ }
665
+ scrubbedAttributes(attributes) {
666
+ const clean = this.secrets.scrubAttributes(attributes);
667
+ return sameAttributes(attributes, clean) ? attributes : clean;
668
+ }
669
+ scrubbedStatus(status) {
670
+ if (!status.message) return status;
671
+ const message = this.secrets.scrubAttribute(status.message);
672
+ return message === status.message ? status : { ...status, message };
673
+ }
674
+ scrubbedEvent(event) {
675
+ if (!event.attributes) return event;
676
+ const attributes = this.scrubbedAttributes(event.attributes);
677
+ if (attributes === event.attributes) return event;
678
+ return { ...event, attributes };
679
+ }
680
+ };
681
+ function scrubbedRead(reads, property) {
682
+ return typeof property === "string" && property in reads ? reads[property] : void 0;
683
+ }
684
+ function viewOf(span, reads) {
685
+ const view = new Proxy(span, {
686
+ get(target, property) {
687
+ const read = scrubbedRead(reads, property);
688
+ if (read) return read();
689
+ const value = Reflect.get(target, property, target);
690
+ if (typeof value !== "function") return value;
691
+ return (...args) => {
692
+ const result = value.apply(target, args);
693
+ return result === target ? view : result;
694
+ };
695
+ },
696
+ getOwnPropertyDescriptor(target, property) {
697
+ const descriptor = Reflect.getOwnPropertyDescriptor(target, property);
698
+ const read = scrubbedRead(reads, property);
699
+ if (!read || !descriptor || !("value" in descriptor)) return descriptor;
700
+ return { ...descriptor, value: read() };
701
+ }
702
+ });
703
+ return view;
704
+ }
705
+
489
706
  // src/tracing/session.ts
490
707
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
491
708
  var SESSION_ID_ATTRIBUTE = "gateway.session.id";
492
- var currentSessionStorage = new AsyncLocalStorage2();
709
+ var WORLD_SESSION_ID_ATTRIBUTE = "gateway.trace.metadata.world_session_id";
710
+ var processStorage = (name) => {
711
+ const key = /* @__PURE__ */ Symbol.for(`@withgateway/sdk/${name}`);
712
+ const slots = globalThis;
713
+ slots[key] ??= new AsyncLocalStorage2();
714
+ return slots[key];
715
+ };
716
+ var currentSessionStorage = processStorage("session-storage");
717
+ var currentWorldSessionStorage = processStorage("world-session-storage");
493
718
  function getAmbientSessionId() {
494
719
  return currentSessionStorage.getStore();
495
720
  }
496
- function withSession(sessionId, fn) {
721
+ function getCurrentWorldSessionId() {
722
+ return currentWorldSessionStorage.getStore();
723
+ }
724
+ function runIn(storage, value, fn) {
497
725
  return new Promise((resolve, reject) => {
498
- currentSessionStorage.run(sessionId, () => {
726
+ storage.run(value, () => {
499
727
  Promise.resolve().then(fn).then(resolve, reject);
500
728
  });
501
729
  });
502
730
  }
731
+ function withSession(sessionId, fn) {
732
+ return runIn(currentSessionStorage, sessionId, fn);
733
+ }
734
+ function withWorldSession(worldSessionId, fn) {
735
+ return runIn(
736
+ currentWorldSessionStorage,
737
+ worldSessionId,
738
+ () => withSession(worldSessionId, fn)
739
+ );
740
+ }
503
741
  var SessionSpanProcessor = class {
504
742
  onStart(span, _parentContext) {
743
+ const worldSessionId = getCurrentWorldSessionId();
744
+ if (worldSessionId)
745
+ span.setAttribute(WORLD_SESSION_ID_ATTRIBUTE, worldSessionId);
505
746
  const sessionId = getAmbientSessionId();
506
- if (sessionId) {
507
- span.setAttribute(SESSION_ID_ATTRIBUTE, sessionId);
508
- }
747
+ if (!sessionId || span.attributes?.[SESSION_ID_ATTRIBUTE] !== void 0)
748
+ return;
749
+ span.setAttribute(SESSION_ID_ATTRIBUTE, sessionId);
509
750
  }
510
751
  onEnd(_span) {
511
752
  }
@@ -1042,7 +1283,7 @@ function buildProvider(config, logFn) {
1042
1283
  const spanProcessors = [];
1043
1284
  if (config.customSpanProcessors) {
1044
1285
  for (const sp of config.customSpanProcessors) {
1045
- spanProcessors.push(sp);
1286
+ spanProcessors.push(new SecretRedactingSpanProcessor(sp));
1046
1287
  logFn(
1047
1288
  "Added custom span processor: %s",
1048
1289
  sp.constructor?.name ?? "unknown"
@@ -1061,7 +1302,11 @@ function buildProvider(config, logFn) {
1061
1302
  if (config.batch?.maxExportBatchSize) {
1062
1303
  batchConfig.maxExportBatchSize = config.batch.maxExportBatchSize;
1063
1304
  }
1064
- spanProcessors.push(new BatchSpanProcessor(exporter, batchConfig));
1305
+ spanProcessors.push(
1306
+ new SecretRedactingSpanProcessor(
1307
+ new BatchSpanProcessor(exporter, batchConfig)
1308
+ )
1309
+ );
1065
1310
  let provider = new NodeTracerProvider({
1066
1311
  resource: createResource(resourceAttrs)
1067
1312
  });
@@ -1455,13 +1700,258 @@ async function* observeClaude(stream, options) {
1455
1700
  }
1456
1701
  }
1457
1702
  }
1703
+
1704
+ // src/tracing/media.ts
1705
+ import { createHash as createHash2 } from "crypto";
1706
+
1707
+ // src/worlds/transport.ts
1708
+ var PLATFORM_TIMEOUT_ENV = "GATEWAY_TIMEOUT_MS";
1709
+ var PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
1710
+ function platformTimeoutMs(env = process.env) {
1711
+ const raw = env[PLATFORM_TIMEOUT_ENV];
1712
+ if (raw === void 0 || raw === "") return PLATFORM_TIMEOUT_DEFAULT_MS;
1713
+ const value = Number(raw);
1714
+ if (Number.isInteger(value) && value > 0) return value;
1715
+ throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
1716
+ }
1717
+ var CHECK_HOST = "Check GATEWAY_HOST (or --host) points at a running platform, then retry.";
1718
+ var RETRY = "Retry; if it repeats, check the network between here and the platform.";
1719
+ var CAUSES = {
1720
+ ECONNREFUSED: { reason: "connection refused", next: CHECK_HOST },
1721
+ ENOTFOUND: { reason: "DNS lookup failed", next: CHECK_HOST },
1722
+ EAI_AGAIN: { reason: "DNS lookup failed", next: CHECK_HOST },
1723
+ ETIMEDOUT: { reason: "connect timed out", next: CHECK_HOST },
1724
+ UND_ERR_CONNECT_TIMEOUT: { reason: "connect timed out", next: CHECK_HOST },
1725
+ EHOSTUNREACH: { reason: "host unreachable", next: CHECK_HOST },
1726
+ ENETUNREACH: { reason: "network unreachable", next: RETRY },
1727
+ ECONNRESET: { reason: "connection dropped", next: RETRY },
1728
+ UND_ERR_SOCKET: { reason: "connection dropped", next: RETRY },
1729
+ EPIPE: { reason: "connection dropped", next: RETRY }
1730
+ };
1731
+ var UNKNOWN_CAUSE = { reason: "network error", next: CHECK_HOST };
1732
+ var TransportCause = class _TransportCause {
1733
+ constructor(reason, detail, next) {
1734
+ this.reason = reason;
1735
+ this.detail = detail;
1736
+ this.next = next;
1737
+ }
1738
+ reason;
1739
+ detail;
1740
+ next;
1741
+ static timeout(timeoutMs) {
1742
+ return new _TransportCause(
1743
+ `timed out after ${timeoutMs / 1e3}s`,
1744
+ null,
1745
+ `Retry; if the platform is slow, raise ${PLATFORM_TIMEOUT_ENV} (milliseconds per request).`
1746
+ );
1747
+ }
1748
+ static of(error) {
1749
+ const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
1750
+ const code = cause && "code" in cause && typeof cause.code === "string" ? cause.code : null;
1751
+ const message = cause?.message ?? (error instanceof Error ? error.message : String(error));
1752
+ const known = code && CAUSES[code] || UNKNOWN_CAUSE;
1753
+ return new _TransportCause(known.reason, code ? `${code}: ${message}` : message, known.next);
1754
+ }
1755
+ describe() {
1756
+ return this.detail === null ? this.reason : `${this.reason} (${this.detail})`;
1757
+ }
1758
+ };
1759
+ var UnreachableError = class extends Error {
1760
+ constructor(host, method, route, failure) {
1761
+ super(`${method} ${route}: no answer from ${host}: ${failure.describe()}. ${failure.next}`);
1762
+ this.host = host;
1763
+ this.method = method;
1764
+ this.route = route;
1765
+ this.failure = failure;
1766
+ this.name = "UnreachableError";
1767
+ }
1768
+ host;
1769
+ method;
1770
+ route;
1771
+ failure;
1772
+ };
1773
+ var PlatformTransport = class {
1774
+ constructor(endpoint, timeoutMs = platformTimeoutMs()) {
1775
+ this.endpoint = endpoint;
1776
+ this.timeoutMs = timeoutMs;
1777
+ }
1778
+ endpoint;
1779
+ timeoutMs;
1780
+ /** A request that legitimately takes longer (a sandbox exec, a world create) waits its own budget; every other one waits the default. */
1781
+ budget(ownMs) {
1782
+ return Math.max(ownMs ?? 0, this.timeoutMs);
1783
+ }
1784
+ async send(method, route, body, opts = {}) {
1785
+ const timeoutMs = this.budget(opts.timeoutMs);
1786
+ const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
1787
+ const controller = new AbortController();
1788
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
1789
+ try {
1790
+ const res = await fetch(`${this.endpoint.host}${route}`, {
1791
+ method,
1792
+ headers: {
1793
+ "content-type": "application/json",
1794
+ accept: "application/json",
1795
+ authorization: "Basic " + Buffer.from(`${this.endpoint.publicKey}:${this.endpoint.secretKey}`).toString("base64")
1796
+ },
1797
+ ...payload,
1798
+ signal: controller.signal
1799
+ });
1800
+ return { status: res.status, ok: res.ok, text: await res.text() };
1801
+ } catch (error) {
1802
+ const cause = controller.signal.aborted ? TransportCause.timeout(timeoutMs) : TransportCause.of(error);
1803
+ throw new UnreachableError(this.endpoint.host, method, route, cause);
1804
+ } finally {
1805
+ clearTimeout(timer);
1806
+ }
1807
+ }
1808
+ };
1809
+
1810
+ // src/tracing/media.ts
1811
+ var MEDIA_PATH = "/api/public/media";
1812
+ var MAX_MEDIA_BYTES = 5 * 1024 * 1024;
1813
+ var PUT_TIMEOUT_MS = 3e4;
1814
+ var MAX_UPLOAD_ERROR = 500;
1815
+ var NETWORK_FAILURE_STATUS = 599;
1816
+ var MediaUploadError = class extends Error {
1817
+ constructor(message, status) {
1818
+ super(message);
1819
+ this.status = status;
1820
+ this.name = "MediaUploadError";
1821
+ }
1822
+ status;
1823
+ };
1824
+ var MediaReference = class {
1825
+ constructor(mediaId, contentType) {
1826
+ this.mediaId = mediaId;
1827
+ this.contentType = contentType;
1828
+ }
1829
+ mediaId;
1830
+ contentType;
1831
+ toString() {
1832
+ return `@@@langfuseMedia:type=${this.contentType}|id=${this.mediaId}|source=bytes@@@`;
1833
+ }
1834
+ };
1835
+ async function putBytes(url, data, headers) {
1836
+ const controller = new AbortController();
1837
+ const timer = setTimeout(() => controller.abort(), PUT_TIMEOUT_MS);
1838
+ try {
1839
+ const res = await fetch(url, {
1840
+ method: "PUT",
1841
+ body: new Uint8Array(data),
1842
+ headers,
1843
+ signal: controller.signal
1844
+ });
1845
+ return {
1846
+ status: res.status,
1847
+ error: res.ok ? null : (await res.text()).slice(0, MAX_UPLOAD_ERROR)
1848
+ };
1849
+ } catch (error) {
1850
+ return {
1851
+ status: NETWORK_FAILURE_STATUS,
1852
+ error: error instanceof Error ? error.name : "Error"
1853
+ };
1854
+ } finally {
1855
+ clearTimeout(timer);
1856
+ }
1857
+ }
1858
+ var MediaUploader = class _MediaUploader {
1859
+ constructor(transport, put = putBytes, maxBytes = MAX_MEDIA_BYTES) {
1860
+ this.transport = transport;
1861
+ this.put = put;
1862
+ this.maxBytes = maxBytes;
1863
+ }
1864
+ transport;
1865
+ put;
1866
+ maxBytes;
1867
+ /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1868
+ * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1869
+ static fromEnv(env = process.env) {
1870
+ const host = env.GATEWAY_HOST;
1871
+ const publicKey = env.GATEWAY_PUBLIC_KEY;
1872
+ const secretKey = env.GATEWAY_SECRET_KEY;
1873
+ if (!host || !publicKey || !secretKey) return null;
1874
+ return new _MediaUploader(
1875
+ new PlatformTransport({
1876
+ host: host.replace(/\/$/, ""),
1877
+ publicKey,
1878
+ secretKey
1879
+ })
1880
+ );
1881
+ }
1882
+ /** Store `data` linked to the trace (and observation) and return its reference. */
1883
+ async upload(data, link) {
1884
+ if (data.length > this.maxBytes)
1885
+ throw new MediaUploadError(
1886
+ `media is ${data.length} bytes, over the ${this.maxBytes}-byte cap`
1887
+ );
1888
+ const sha256Hash = createHash2("sha256").update(data).digest("base64");
1889
+ const answer = await this.send("POST", MEDIA_PATH, {
1890
+ traceId: link.traceId,
1891
+ observationId: link.observationId,
1892
+ contentType: link.contentType,
1893
+ contentLength: data.length,
1894
+ sha256Hash,
1895
+ field: link.field
1896
+ });
1897
+ if (typeof answer.mediaId !== "string" || !answer.mediaId)
1898
+ throw new MediaUploadError(
1899
+ "POST /api/public/media answered without a mediaId"
1900
+ );
1901
+ const reference = new MediaReference(answer.mediaId, link.contentType);
1902
+ if (answer.uploadUrl)
1903
+ await this.store(reference, answer.uploadUrl, data, sha256Hash);
1904
+ return reference;
1905
+ }
1906
+ async store(reference, url, data, sha256Hash) {
1907
+ const started = Date.now();
1908
+ const { status, error } = await this.put(url, data, {
1909
+ "Content-Type": reference.contentType,
1910
+ "x-amz-checksum-sha256": sha256Hash,
1911
+ "x-ms-blob-type": "BlockBlob"
1912
+ });
1913
+ await this.send(
1914
+ "PATCH",
1915
+ `${MEDIA_PATH}/${encodeURIComponent(reference.mediaId)}`,
1916
+ {
1917
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
1918
+ uploadHttpStatus: status,
1919
+ uploadHttpError: error,
1920
+ uploadTimeMs: Date.now() - started
1921
+ }
1922
+ );
1923
+ if (status < 200 || status >= 300)
1924
+ throw new MediaUploadError(
1925
+ `media storage refused the upload (HTTP ${status})`,
1926
+ status
1927
+ );
1928
+ }
1929
+ async send(method, route, body) {
1930
+ const reply = await this.transport.send(method, route, body);
1931
+ if (!reply.ok)
1932
+ throw new MediaUploadError(
1933
+ `${method} ${route} -> ${reply.status}`,
1934
+ reply.status
1935
+ );
1936
+ return reply.text ? JSON.parse(reply.text) : {};
1937
+ }
1938
+ };
1458
1939
  export {
1459
1940
  IdentitySpanProcessor,
1941
+ MAX_MEDIA_BYTES,
1942
+ MediaReference,
1943
+ MediaUploadError,
1944
+ MediaUploader,
1945
+ SECRET_KEYS,
1460
1946
  SESSION_ID_ATTRIBUTE,
1947
+ SecretRedactingSpanProcessor,
1461
1948
  SessionSpanProcessor,
1949
+ SpanSecrets,
1462
1950
  USER_ID_ATTRIBUTE,
1951
+ WORLD_SESSION_ID_ATTRIBUTE,
1463
1952
  flush,
1464
1953
  getAmbientSessionId,
1954
+ getCurrentWorldSessionId,
1465
1955
  identify,
1466
1956
  init,
1467
1957
  initIsolated,
@@ -1474,6 +1964,7 @@ export {
1474
1964
  resetAmbientUser,
1475
1965
  resolveUserId,
1476
1966
  shutdown,
1477
- withSession
1967
+ withSession,
1968
+ withWorldSession
1478
1969
  };
1479
1970
  //# sourceMappingURL=index.mjs.map