@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.
- package/README.md +24 -8
- package/dist/cli.js +2695 -657
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2675 -637
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +1 -0
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.js +1443 -36
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +1441 -28
- package/dist/files/index.mjs.map +1 -1
- package/dist/{index-Cz8Q6Keo.d.mts → index-D6oi1TWE.d.mts} +93 -34
- package/dist/{index-BO2SNSgy.d.ts → index-DAb-FCMg.d.ts} +93 -34
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +1986 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1941 -218
- package/dist/index.mjs.map +1 -1
- package/dist/media-CbcAfQvd.d.mts +83 -0
- package/dist/media-CbcAfQvd.d.ts +83 -0
- package/dist/traces/index.d.mts +1 -0
- package/dist/traces/index.d.ts +1 -0
- package/dist/traces/index.js +1440 -33
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +1438 -25
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +2 -1
- package/dist/tracing/index.d.ts +2 -1
- package/dist/tracing/index.js +512 -11
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +501 -10
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +614 -25
- package/dist/worlds/index.d.ts +614 -25
- package/dist/worlds/index.js +1923 -214
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1910 -201
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +1 -1
- package/runtime/gatewaysdk/environment/_toml.py +3 -10
- package/runtime/gatewaysdk/environment/schema/api.py +38 -56
- package/runtime/gatewaysdk/environment/schema/grading.py +1 -5
- package/runtime/gatewaysdk/environment/schema/host.py +53 -90
- package/runtime/gatewaysdk/environment/schema/local.py +15 -7
- package/runtime/gatewaysdk/environment/schema/runtime.py +9 -9
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +3 -3
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +18 -18
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +4 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +5 -1
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +5 -3
- package/runtime/gatewaysdk/environment/schema/store.py +15 -6
- package/runtime/gatewaysdk/environment/schema/tasks.py +25 -1
- package/skills/benchmarks-getting-started/SKILL.md +3 -3
- package/skills/connector-schema-authoring/SKILL.md +1 -1
- package/skills/connector-schema-authoring/world-schema.md +1 -1
- package/skills/worlds-getting-started/SKILL.md +18 -18
- package/skills/worlds-getting-started/references/cli-mcp-map.md +4 -2
- package/skills/worlds-getting-started/references/gotchas.md +5 -1
- package/skills/worlds-getting-started/references/new-world-sandbox.md +5 -3
package/dist/tracing/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as
|
|
1
|
+
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SECRET_KEYS, d as SESSION_ID_ATTRIBUTE, e as SecretRedactingSpanProcessor, f as SessionSpanProcessor, g as SpanSecrets, T as TracingConfig, h as TracingHandle, j as TracingState, U as USER_ID_ATTRIBUTE, W as WORLD_SESSION_ID_ATTRIBUTE, k as flush, l as getAmbientSessionId, m as getCurrentWorldSessionId, i as identify, n as init, o as initIsolated, p as isGatewayConfigured, q as isInitialized, s as observe, t as observeAnthropic, u as observeClaude, v as observeOpenAI, w as resetAmbientUser, r as resolveUserId, x as shutdown, y as withSession, z as withWorldSession } from '../index-D6oi1TWE.mjs';
|
|
2
|
+
export { M as MAX_MEDIA_BYTES, f as MediaField, g as MediaReference, h as MediaUploadError, i as MediaUploader } from '../media-CbcAfQvd.mjs';
|
|
2
3
|
import '@opentelemetry/api';
|
|
3
4
|
import '../scores/index.mjs';
|
|
4
5
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as
|
|
1
|
+
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SECRET_KEYS, d as SESSION_ID_ATTRIBUTE, e as SecretRedactingSpanProcessor, f as SessionSpanProcessor, g as SpanSecrets, T as TracingConfig, h as TracingHandle, j as TracingState, U as USER_ID_ATTRIBUTE, W as WORLD_SESSION_ID_ATTRIBUTE, k as flush, l as getAmbientSessionId, m as getCurrentWorldSessionId, i as identify, n as init, o as initIsolated, p as isGatewayConfigured, q as isInitialized, s as observe, t as observeAnthropic, u as observeClaude, v as observeOpenAI, w as resetAmbientUser, r as resolveUserId, x as shutdown, y as withSession, z as withWorldSession } from '../index-DAb-FCMg.js';
|
|
2
|
+
export { M as MAX_MEDIA_BYTES, f as MediaField, g as MediaReference, h as MediaUploadError, i as MediaUploader } from '../media-CbcAfQvd.js';
|
|
2
3
|
import '@opentelemetry/api';
|
|
3
4
|
import '../scores/index.js';
|
|
4
5
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/index.js
CHANGED
|
@@ -29,7 +29,7 @@ var require_package = __commonJS({
|
|
|
29
29
|
"package.json"(exports2, module2) {
|
|
30
30
|
module2.exports = {
|
|
31
31
|
name: "@withgateway/sdk",
|
|
32
|
-
version: "0.
|
|
32
|
+
version: "0.3.0",
|
|
33
33
|
description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
|
|
34
34
|
main: "./dist/index.js",
|
|
35
35
|
module: "./dist/index.mjs",
|
|
@@ -232,11 +232,20 @@ var require_package = __commonJS({
|
|
|
232
232
|
var tracing_exports = {};
|
|
233
233
|
__export(tracing_exports, {
|
|
234
234
|
IdentitySpanProcessor: () => IdentitySpanProcessor,
|
|
235
|
+
MAX_MEDIA_BYTES: () => MAX_MEDIA_BYTES,
|
|
236
|
+
MediaReference: () => MediaReference,
|
|
237
|
+
MediaUploadError: () => MediaUploadError,
|
|
238
|
+
MediaUploader: () => MediaUploader,
|
|
239
|
+
SECRET_KEYS: () => SECRET_KEYS,
|
|
235
240
|
SESSION_ID_ATTRIBUTE: () => SESSION_ID_ATTRIBUTE,
|
|
241
|
+
SecretRedactingSpanProcessor: () => SecretRedactingSpanProcessor,
|
|
236
242
|
SessionSpanProcessor: () => SessionSpanProcessor,
|
|
243
|
+
SpanSecrets: () => SpanSecrets,
|
|
237
244
|
USER_ID_ATTRIBUTE: () => USER_ID_ATTRIBUTE,
|
|
245
|
+
WORLD_SESSION_ID_ATTRIBUTE: () => WORLD_SESSION_ID_ATTRIBUTE,
|
|
238
246
|
flush: () => flush,
|
|
239
247
|
getAmbientSessionId: () => getAmbientSessionId,
|
|
248
|
+
getCurrentWorldSessionId: () => getCurrentWorldSessionId,
|
|
240
249
|
identify: () => identify,
|
|
241
250
|
init: () => init,
|
|
242
251
|
initIsolated: () => initIsolated,
|
|
@@ -249,7 +258,8 @@ __export(tracing_exports, {
|
|
|
249
258
|
resetAmbientUser: () => resetAmbientUser,
|
|
250
259
|
resolveUserId: () => resolveUserId,
|
|
251
260
|
shutdown: () => shutdown,
|
|
252
|
-
withSession: () => withSession
|
|
261
|
+
withSession: () => withSession,
|
|
262
|
+
withWorldSession: () => withWorldSession
|
|
253
263
|
});
|
|
254
264
|
module.exports = __toCommonJS(tracing_exports);
|
|
255
265
|
|
|
@@ -520,26 +530,267 @@ function debugLog(message, ...args) {
|
|
|
520
530
|
}
|
|
521
531
|
}
|
|
522
532
|
|
|
533
|
+
// src/tracing/secrets.ts
|
|
534
|
+
var SECRET_KEYS = /* @__PURE__ */ new Set([
|
|
535
|
+
"authorization_token",
|
|
536
|
+
"authorizationToken"
|
|
537
|
+
]);
|
|
538
|
+
var REDACTED = "[redacted: holds a credential]";
|
|
539
|
+
var MIN_SECRET_LENGTH = 8;
|
|
540
|
+
var SpanSecrets = class {
|
|
541
|
+
constructor(keys = SECRET_KEYS) {
|
|
542
|
+
this.keys = keys;
|
|
543
|
+
}
|
|
544
|
+
keys;
|
|
545
|
+
isSecret(key) {
|
|
546
|
+
return this.keys.has(key);
|
|
547
|
+
}
|
|
548
|
+
drops(attribute) {
|
|
549
|
+
return this.mentions(attribute);
|
|
550
|
+
}
|
|
551
|
+
strip(value) {
|
|
552
|
+
if (typeof value === "string") return this.stripNested(value);
|
|
553
|
+
if (Array.isArray(value)) return value.map((item) => this.strip(item));
|
|
554
|
+
if (value === null || typeof value !== "object") return value;
|
|
555
|
+
return Object.fromEntries(
|
|
556
|
+
Object.entries(value).filter(([key]) => !this.isSecret(key)).map(([key, item]) => [key, this.strip(item)])
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
scrubAttribute(value) {
|
|
560
|
+
try {
|
|
561
|
+
return this.scrubbedValue(value);
|
|
562
|
+
} catch {
|
|
563
|
+
return Array.isArray(value) ? [REDACTED] : REDACTED;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
scrubbedValue(value) {
|
|
567
|
+
if (typeof value === "string") return this.scrubbedText(value);
|
|
568
|
+
if (!Array.isArray(value)) return value;
|
|
569
|
+
const results = value.map(
|
|
570
|
+
(item) => typeof item === "string" ? this.cleaned(item) : { text: item, removed: /* @__PURE__ */ new Set() }
|
|
571
|
+
);
|
|
572
|
+
const removed = new Set(results.flatMap((r) => [...r.removed]));
|
|
573
|
+
const scrubbed = results.map((r) => r.text);
|
|
574
|
+
const withheld = scrubbed.some(
|
|
575
|
+
(item) => typeof item === "string" && this.carries(item, removed)
|
|
576
|
+
);
|
|
577
|
+
return withheld ? [REDACTED] : scrubbed;
|
|
578
|
+
}
|
|
579
|
+
scrubAttributes(attributes) {
|
|
580
|
+
return Object.fromEntries(
|
|
581
|
+
Object.entries(attributes).filter(([key]) => !this.drops(key)).map(([key, value]) => [key, this.scrubAttribute(value)])
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
mentions(text) {
|
|
585
|
+
return [...this.keys].some((key) => text.includes(key));
|
|
586
|
+
}
|
|
587
|
+
scrubbedText(text) {
|
|
588
|
+
const { text: cleaned, removed } = this.cleaned(text);
|
|
589
|
+
return this.carries(cleaned, removed) ? REDACTED : cleaned;
|
|
590
|
+
}
|
|
591
|
+
cleaned(text) {
|
|
592
|
+
if (!this.mentions(text)) return { text, removed: /* @__PURE__ */ new Set() };
|
|
593
|
+
const parsed = jsonOrUndefined(text);
|
|
594
|
+
if (parsed === void 0) return { text: REDACTED, removed: /* @__PURE__ */ new Set() };
|
|
595
|
+
return {
|
|
596
|
+
text: JSON.stringify(this.strip(parsed)),
|
|
597
|
+
removed: this.removed(parsed)
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
carries(text, removed) {
|
|
601
|
+
if (text === REDACTED || this.mentions(text)) return true;
|
|
602
|
+
return [...removed].some(
|
|
603
|
+
(value) => encodings(value).some((form) => text.includes(form))
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
removed(value) {
|
|
607
|
+
if (typeof value === "string") {
|
|
608
|
+
const nested = this.mentions(value) ? jsonOrUndefined(value) : void 0;
|
|
609
|
+
return nested === void 0 ? /* @__PURE__ */ new Set() : this.removed(nested);
|
|
610
|
+
}
|
|
611
|
+
if (Array.isArray(value)) return union(value.map((v) => this.removed(v)));
|
|
612
|
+
if (value === null || typeof value !== "object") return /* @__PURE__ */ new Set();
|
|
613
|
+
return union(
|
|
614
|
+
Object.entries(value).map(
|
|
615
|
+
([key, item]) => this.isSecret(key) ? leaves(item) : this.removed(item)
|
|
616
|
+
)
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
stripNested(text) {
|
|
620
|
+
if (!this.mentions(text)) return text;
|
|
621
|
+
const parsed = jsonOrUndefined(text);
|
|
622
|
+
if (parsed === void 0) return text;
|
|
623
|
+
return JSON.stringify(this.strip(parsed));
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
function jsonOrUndefined(text) {
|
|
627
|
+
let parsed;
|
|
628
|
+
try {
|
|
629
|
+
parsed = JSON.parse(text);
|
|
630
|
+
} catch {
|
|
631
|
+
return void 0;
|
|
632
|
+
}
|
|
633
|
+
if (typeof parsed === "string") return parsed;
|
|
634
|
+
return parsed !== null && typeof parsed === "object" ? parsed : void 0;
|
|
635
|
+
}
|
|
636
|
+
function encodings(value) {
|
|
637
|
+
return [value, percentEncoded(value), JSON.stringify(value).slice(1, -1)];
|
|
638
|
+
}
|
|
639
|
+
function percentEncoded(value) {
|
|
640
|
+
try {
|
|
641
|
+
return encodeURIComponent(value);
|
|
642
|
+
} catch {
|
|
643
|
+
return value;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function union(sets) {
|
|
647
|
+
return new Set(sets.flatMap((set) => [...set]));
|
|
648
|
+
}
|
|
649
|
+
function leaves(value) {
|
|
650
|
+
if (typeof value === "string") {
|
|
651
|
+
return [...value].length >= MIN_SECRET_LENGTH ? /* @__PURE__ */ new Set([value]) : /* @__PURE__ */ new Set();
|
|
652
|
+
}
|
|
653
|
+
if (Array.isArray(value)) return union(value.map(leaves));
|
|
654
|
+
if (value === null || typeof value !== "object") return /* @__PURE__ */ new Set();
|
|
655
|
+
return union(Object.values(value).map(leaves));
|
|
656
|
+
}
|
|
657
|
+
var SPAN_SECRETS = new SpanSecrets();
|
|
658
|
+
function sameAttributes(before, after) {
|
|
659
|
+
const keys = Object.keys(before);
|
|
660
|
+
if (keys.length !== Object.keys(after).length) return false;
|
|
661
|
+
return keys.every(
|
|
662
|
+
(key) => before[key] === after[key] || sameArray(before[key], after[key])
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
function sameArray(before, after) {
|
|
666
|
+
if (!Array.isArray(before) || !Array.isArray(after)) return false;
|
|
667
|
+
return before.length === after.length && before.every((item, i) => item === after[i]);
|
|
668
|
+
}
|
|
669
|
+
var SecretRedactingSpanProcessor = class {
|
|
670
|
+
constructor(inner, secrets = SPAN_SECRETS) {
|
|
671
|
+
this.inner = inner;
|
|
672
|
+
this.secrets = secrets;
|
|
673
|
+
}
|
|
674
|
+
inner;
|
|
675
|
+
secrets;
|
|
676
|
+
views = /* @__PURE__ */ new WeakMap();
|
|
677
|
+
onStart(span, parentContext) {
|
|
678
|
+
this.inner.onStart(this.viewFor(span), parentContext);
|
|
679
|
+
}
|
|
680
|
+
onEnd(span) {
|
|
681
|
+
const view = this.viewFor(span);
|
|
682
|
+
try {
|
|
683
|
+
void [view.attributes, view.events, view.status];
|
|
684
|
+
} catch (error) {
|
|
685
|
+
console.warn(
|
|
686
|
+
`[gatewaysdk.tracing] Dropped span "${span.name}": could not redact it: ${error?.name}`
|
|
687
|
+
);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
this.inner.onEnd(view);
|
|
691
|
+
}
|
|
692
|
+
viewFor(span) {
|
|
693
|
+
const known = this.views.get(span);
|
|
694
|
+
if (known) return known;
|
|
695
|
+
const view = viewOf(span, {
|
|
696
|
+
attributes: () => this.scrubbedAttributes(span.attributes),
|
|
697
|
+
events: () => span.events.map((event) => this.scrubbedEvent(event)),
|
|
698
|
+
status: () => this.scrubbedStatus(span.status)
|
|
699
|
+
});
|
|
700
|
+
this.views.set(span, view);
|
|
701
|
+
return view;
|
|
702
|
+
}
|
|
703
|
+
forceFlush() {
|
|
704
|
+
return this.inner.forceFlush();
|
|
705
|
+
}
|
|
706
|
+
shutdown() {
|
|
707
|
+
return this.inner.shutdown();
|
|
708
|
+
}
|
|
709
|
+
scrubbedAttributes(attributes) {
|
|
710
|
+
const clean = this.secrets.scrubAttributes(attributes);
|
|
711
|
+
return sameAttributes(attributes, clean) ? attributes : clean;
|
|
712
|
+
}
|
|
713
|
+
scrubbedStatus(status) {
|
|
714
|
+
if (!status.message) return status;
|
|
715
|
+
const message = this.secrets.scrubAttribute(status.message);
|
|
716
|
+
return message === status.message ? status : { ...status, message };
|
|
717
|
+
}
|
|
718
|
+
scrubbedEvent(event) {
|
|
719
|
+
if (!event.attributes) return event;
|
|
720
|
+
const attributes = this.scrubbedAttributes(event.attributes);
|
|
721
|
+
if (attributes === event.attributes) return event;
|
|
722
|
+
return { ...event, attributes };
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
function scrubbedRead(reads, property) {
|
|
726
|
+
return typeof property === "string" && property in reads ? reads[property] : void 0;
|
|
727
|
+
}
|
|
728
|
+
function viewOf(span, reads) {
|
|
729
|
+
const view = new Proxy(span, {
|
|
730
|
+
get(target, property) {
|
|
731
|
+
const read = scrubbedRead(reads, property);
|
|
732
|
+
if (read) return read();
|
|
733
|
+
const value = Reflect.get(target, property, target);
|
|
734
|
+
if (typeof value !== "function") return value;
|
|
735
|
+
return (...args) => {
|
|
736
|
+
const result = value.apply(target, args);
|
|
737
|
+
return result === target ? view : result;
|
|
738
|
+
};
|
|
739
|
+
},
|
|
740
|
+
getOwnPropertyDescriptor(target, property) {
|
|
741
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(target, property);
|
|
742
|
+
const read = scrubbedRead(reads, property);
|
|
743
|
+
if (!read || !descriptor || !("value" in descriptor)) return descriptor;
|
|
744
|
+
return { ...descriptor, value: read() };
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
return view;
|
|
748
|
+
}
|
|
749
|
+
|
|
523
750
|
// src/tracing/session.ts
|
|
524
751
|
var import_async_hooks2 = require("async_hooks");
|
|
525
752
|
var SESSION_ID_ATTRIBUTE = "gateway.session.id";
|
|
526
|
-
var
|
|
753
|
+
var WORLD_SESSION_ID_ATTRIBUTE = "gateway.trace.metadata.world_session_id";
|
|
754
|
+
var processStorage = (name) => {
|
|
755
|
+
const key = /* @__PURE__ */ Symbol.for(`@withgateway/sdk/${name}`);
|
|
756
|
+
const slots = globalThis;
|
|
757
|
+
slots[key] ??= new import_async_hooks2.AsyncLocalStorage();
|
|
758
|
+
return slots[key];
|
|
759
|
+
};
|
|
760
|
+
var currentSessionStorage = processStorage("session-storage");
|
|
761
|
+
var currentWorldSessionStorage = processStorage("world-session-storage");
|
|
527
762
|
function getAmbientSessionId() {
|
|
528
763
|
return currentSessionStorage.getStore();
|
|
529
764
|
}
|
|
530
|
-
function
|
|
765
|
+
function getCurrentWorldSessionId() {
|
|
766
|
+
return currentWorldSessionStorage.getStore();
|
|
767
|
+
}
|
|
768
|
+
function runIn(storage, value, fn) {
|
|
531
769
|
return new Promise((resolve, reject) => {
|
|
532
|
-
|
|
770
|
+
storage.run(value, () => {
|
|
533
771
|
Promise.resolve().then(fn).then(resolve, reject);
|
|
534
772
|
});
|
|
535
773
|
});
|
|
536
774
|
}
|
|
775
|
+
function withSession(sessionId, fn) {
|
|
776
|
+
return runIn(currentSessionStorage, sessionId, fn);
|
|
777
|
+
}
|
|
778
|
+
function withWorldSession(worldSessionId, fn) {
|
|
779
|
+
return runIn(
|
|
780
|
+
currentWorldSessionStorage,
|
|
781
|
+
worldSessionId,
|
|
782
|
+
() => withSession(worldSessionId, fn)
|
|
783
|
+
);
|
|
784
|
+
}
|
|
537
785
|
var SessionSpanProcessor = class {
|
|
538
786
|
onStart(span, _parentContext) {
|
|
787
|
+
const worldSessionId = getCurrentWorldSessionId();
|
|
788
|
+
if (worldSessionId)
|
|
789
|
+
span.setAttribute(WORLD_SESSION_ID_ATTRIBUTE, worldSessionId);
|
|
539
790
|
const sessionId = getAmbientSessionId();
|
|
540
|
-
if (sessionId)
|
|
541
|
-
|
|
542
|
-
|
|
791
|
+
if (!sessionId || span.attributes?.[SESSION_ID_ATTRIBUTE] !== void 0)
|
|
792
|
+
return;
|
|
793
|
+
span.setAttribute(SESSION_ID_ATTRIBUTE, sessionId);
|
|
543
794
|
}
|
|
544
795
|
onEnd(_span) {
|
|
545
796
|
}
|
|
@@ -1076,7 +1327,7 @@ function buildProvider(config, logFn) {
|
|
|
1076
1327
|
const spanProcessors = [];
|
|
1077
1328
|
if (config.customSpanProcessors) {
|
|
1078
1329
|
for (const sp of config.customSpanProcessors) {
|
|
1079
|
-
spanProcessors.push(sp);
|
|
1330
|
+
spanProcessors.push(new SecretRedactingSpanProcessor(sp));
|
|
1080
1331
|
logFn(
|
|
1081
1332
|
"Added custom span processor: %s",
|
|
1082
1333
|
sp.constructor?.name ?? "unknown"
|
|
@@ -1095,7 +1346,11 @@ function buildProvider(config, logFn) {
|
|
|
1095
1346
|
if (config.batch?.maxExportBatchSize) {
|
|
1096
1347
|
batchConfig.maxExportBatchSize = config.batch.maxExportBatchSize;
|
|
1097
1348
|
}
|
|
1098
|
-
spanProcessors.push(
|
|
1349
|
+
spanProcessors.push(
|
|
1350
|
+
new SecretRedactingSpanProcessor(
|
|
1351
|
+
new BatchSpanProcessor(exporter, batchConfig)
|
|
1352
|
+
)
|
|
1353
|
+
);
|
|
1099
1354
|
let provider = new NodeTracerProvider({
|
|
1100
1355
|
resource: createResource(resourceAttrs)
|
|
1101
1356
|
});
|
|
@@ -1485,14 +1740,259 @@ async function* observeClaude(stream, options) {
|
|
|
1485
1740
|
}
|
|
1486
1741
|
}
|
|
1487
1742
|
}
|
|
1743
|
+
|
|
1744
|
+
// src/tracing/media.ts
|
|
1745
|
+
var import_node_crypto2 = require("crypto");
|
|
1746
|
+
|
|
1747
|
+
// src/worlds/transport.ts
|
|
1748
|
+
var PLATFORM_TIMEOUT_ENV = "GATEWAY_TIMEOUT_MS";
|
|
1749
|
+
var PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
|
|
1750
|
+
function platformTimeoutMs(env = process.env) {
|
|
1751
|
+
const raw = env[PLATFORM_TIMEOUT_ENV];
|
|
1752
|
+
if (raw === void 0 || raw === "") return PLATFORM_TIMEOUT_DEFAULT_MS;
|
|
1753
|
+
const value = Number(raw);
|
|
1754
|
+
if (Number.isInteger(value) && value > 0) return value;
|
|
1755
|
+
throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
|
|
1756
|
+
}
|
|
1757
|
+
var CHECK_HOST = "Check GATEWAY_HOST (or --host) points at a running platform, then retry.";
|
|
1758
|
+
var RETRY = "Retry; if it repeats, check the network between here and the platform.";
|
|
1759
|
+
var CAUSES = {
|
|
1760
|
+
ECONNREFUSED: { reason: "connection refused", next: CHECK_HOST },
|
|
1761
|
+
ENOTFOUND: { reason: "DNS lookup failed", next: CHECK_HOST },
|
|
1762
|
+
EAI_AGAIN: { reason: "DNS lookup failed", next: CHECK_HOST },
|
|
1763
|
+
ETIMEDOUT: { reason: "connect timed out", next: CHECK_HOST },
|
|
1764
|
+
UND_ERR_CONNECT_TIMEOUT: { reason: "connect timed out", next: CHECK_HOST },
|
|
1765
|
+
EHOSTUNREACH: { reason: "host unreachable", next: CHECK_HOST },
|
|
1766
|
+
ENETUNREACH: { reason: "network unreachable", next: RETRY },
|
|
1767
|
+
ECONNRESET: { reason: "connection dropped", next: RETRY },
|
|
1768
|
+
UND_ERR_SOCKET: { reason: "connection dropped", next: RETRY },
|
|
1769
|
+
EPIPE: { reason: "connection dropped", next: RETRY }
|
|
1770
|
+
};
|
|
1771
|
+
var UNKNOWN_CAUSE = { reason: "network error", next: CHECK_HOST };
|
|
1772
|
+
var TransportCause = class _TransportCause {
|
|
1773
|
+
constructor(reason, detail, next) {
|
|
1774
|
+
this.reason = reason;
|
|
1775
|
+
this.detail = detail;
|
|
1776
|
+
this.next = next;
|
|
1777
|
+
}
|
|
1778
|
+
reason;
|
|
1779
|
+
detail;
|
|
1780
|
+
next;
|
|
1781
|
+
static timeout(timeoutMs) {
|
|
1782
|
+
return new _TransportCause(
|
|
1783
|
+
`timed out after ${timeoutMs / 1e3}s`,
|
|
1784
|
+
null,
|
|
1785
|
+
`Retry; if the platform is slow, raise ${PLATFORM_TIMEOUT_ENV} (milliseconds per request).`
|
|
1786
|
+
);
|
|
1787
|
+
}
|
|
1788
|
+
static of(error) {
|
|
1789
|
+
const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
|
|
1790
|
+
const code = cause && "code" in cause && typeof cause.code === "string" ? cause.code : null;
|
|
1791
|
+
const message = cause?.message ?? (error instanceof Error ? error.message : String(error));
|
|
1792
|
+
const known = code && CAUSES[code] || UNKNOWN_CAUSE;
|
|
1793
|
+
return new _TransportCause(known.reason, code ? `${code}: ${message}` : message, known.next);
|
|
1794
|
+
}
|
|
1795
|
+
describe() {
|
|
1796
|
+
return this.detail === null ? this.reason : `${this.reason} (${this.detail})`;
|
|
1797
|
+
}
|
|
1798
|
+
};
|
|
1799
|
+
var UnreachableError = class extends Error {
|
|
1800
|
+
constructor(host, method, route, failure) {
|
|
1801
|
+
super(`${method} ${route}: no answer from ${host}: ${failure.describe()}. ${failure.next}`);
|
|
1802
|
+
this.host = host;
|
|
1803
|
+
this.method = method;
|
|
1804
|
+
this.route = route;
|
|
1805
|
+
this.failure = failure;
|
|
1806
|
+
this.name = "UnreachableError";
|
|
1807
|
+
}
|
|
1808
|
+
host;
|
|
1809
|
+
method;
|
|
1810
|
+
route;
|
|
1811
|
+
failure;
|
|
1812
|
+
};
|
|
1813
|
+
var PlatformTransport = class {
|
|
1814
|
+
constructor(endpoint, timeoutMs = platformTimeoutMs()) {
|
|
1815
|
+
this.endpoint = endpoint;
|
|
1816
|
+
this.timeoutMs = timeoutMs;
|
|
1817
|
+
}
|
|
1818
|
+
endpoint;
|
|
1819
|
+
timeoutMs;
|
|
1820
|
+
/** A request that legitimately takes longer (a sandbox exec, a world create) waits its own budget; every other one waits the default. */
|
|
1821
|
+
budget(ownMs) {
|
|
1822
|
+
return Math.max(ownMs ?? 0, this.timeoutMs);
|
|
1823
|
+
}
|
|
1824
|
+
async send(method, route, body, opts = {}) {
|
|
1825
|
+
const timeoutMs = this.budget(opts.timeoutMs);
|
|
1826
|
+
const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
|
|
1827
|
+
const controller = new AbortController();
|
|
1828
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
1829
|
+
try {
|
|
1830
|
+
const res = await fetch(`${this.endpoint.host}${route}`, {
|
|
1831
|
+
method,
|
|
1832
|
+
headers: {
|
|
1833
|
+
"content-type": "application/json",
|
|
1834
|
+
accept: "application/json",
|
|
1835
|
+
authorization: "Basic " + Buffer.from(`${this.endpoint.publicKey}:${this.endpoint.secretKey}`).toString("base64")
|
|
1836
|
+
},
|
|
1837
|
+
...payload,
|
|
1838
|
+
signal: controller.signal
|
|
1839
|
+
});
|
|
1840
|
+
return { status: res.status, ok: res.ok, text: await res.text() };
|
|
1841
|
+
} catch (error) {
|
|
1842
|
+
const cause = controller.signal.aborted ? TransportCause.timeout(timeoutMs) : TransportCause.of(error);
|
|
1843
|
+
throw new UnreachableError(this.endpoint.host, method, route, cause);
|
|
1844
|
+
} finally {
|
|
1845
|
+
clearTimeout(timer);
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
};
|
|
1849
|
+
|
|
1850
|
+
// src/tracing/media.ts
|
|
1851
|
+
var MEDIA_PATH = "/api/public/media";
|
|
1852
|
+
var MAX_MEDIA_BYTES = 5 * 1024 * 1024;
|
|
1853
|
+
var PUT_TIMEOUT_MS = 3e4;
|
|
1854
|
+
var MAX_UPLOAD_ERROR = 500;
|
|
1855
|
+
var NETWORK_FAILURE_STATUS = 599;
|
|
1856
|
+
var MediaUploadError = class extends Error {
|
|
1857
|
+
constructor(message, status) {
|
|
1858
|
+
super(message);
|
|
1859
|
+
this.status = status;
|
|
1860
|
+
this.name = "MediaUploadError";
|
|
1861
|
+
}
|
|
1862
|
+
status;
|
|
1863
|
+
};
|
|
1864
|
+
var MediaReference = class {
|
|
1865
|
+
constructor(mediaId, contentType) {
|
|
1866
|
+
this.mediaId = mediaId;
|
|
1867
|
+
this.contentType = contentType;
|
|
1868
|
+
}
|
|
1869
|
+
mediaId;
|
|
1870
|
+
contentType;
|
|
1871
|
+
toString() {
|
|
1872
|
+
return `@@@langfuseMedia:type=${this.contentType}|id=${this.mediaId}|source=bytes@@@`;
|
|
1873
|
+
}
|
|
1874
|
+
};
|
|
1875
|
+
async function putBytes(url, data, headers) {
|
|
1876
|
+
const controller = new AbortController();
|
|
1877
|
+
const timer = setTimeout(() => controller.abort(), PUT_TIMEOUT_MS);
|
|
1878
|
+
try {
|
|
1879
|
+
const res = await fetch(url, {
|
|
1880
|
+
method: "PUT",
|
|
1881
|
+
body: new Uint8Array(data),
|
|
1882
|
+
headers,
|
|
1883
|
+
signal: controller.signal
|
|
1884
|
+
});
|
|
1885
|
+
return {
|
|
1886
|
+
status: res.status,
|
|
1887
|
+
error: res.ok ? null : (await res.text()).slice(0, MAX_UPLOAD_ERROR)
|
|
1888
|
+
};
|
|
1889
|
+
} catch (error) {
|
|
1890
|
+
return {
|
|
1891
|
+
status: NETWORK_FAILURE_STATUS,
|
|
1892
|
+
error: error instanceof Error ? error.name : "Error"
|
|
1893
|
+
};
|
|
1894
|
+
} finally {
|
|
1895
|
+
clearTimeout(timer);
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
var MediaUploader = class _MediaUploader {
|
|
1899
|
+
constructor(transport, put = putBytes, maxBytes = MAX_MEDIA_BYTES) {
|
|
1900
|
+
this.transport = transport;
|
|
1901
|
+
this.put = put;
|
|
1902
|
+
this.maxBytes = maxBytes;
|
|
1903
|
+
}
|
|
1904
|
+
transport;
|
|
1905
|
+
put;
|
|
1906
|
+
maxBytes;
|
|
1907
|
+
/** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
|
|
1908
|
+
* `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
|
|
1909
|
+
static fromEnv(env = process.env) {
|
|
1910
|
+
const host = env.GATEWAY_HOST;
|
|
1911
|
+
const publicKey = env.GATEWAY_PUBLIC_KEY;
|
|
1912
|
+
const secretKey = env.GATEWAY_SECRET_KEY;
|
|
1913
|
+
if (!host || !publicKey || !secretKey) return null;
|
|
1914
|
+
return new _MediaUploader(
|
|
1915
|
+
new PlatformTransport({
|
|
1916
|
+
host: host.replace(/\/$/, ""),
|
|
1917
|
+
publicKey,
|
|
1918
|
+
secretKey
|
|
1919
|
+
})
|
|
1920
|
+
);
|
|
1921
|
+
}
|
|
1922
|
+
/** Store `data` linked to the trace (and observation) and return its reference. */
|
|
1923
|
+
async upload(data, link) {
|
|
1924
|
+
if (data.length > this.maxBytes)
|
|
1925
|
+
throw new MediaUploadError(
|
|
1926
|
+
`media is ${data.length} bytes, over the ${this.maxBytes}-byte cap`
|
|
1927
|
+
);
|
|
1928
|
+
const sha256Hash = (0, import_node_crypto2.createHash)("sha256").update(data).digest("base64");
|
|
1929
|
+
const answer = await this.send("POST", MEDIA_PATH, {
|
|
1930
|
+
traceId: link.traceId,
|
|
1931
|
+
observationId: link.observationId,
|
|
1932
|
+
contentType: link.contentType,
|
|
1933
|
+
contentLength: data.length,
|
|
1934
|
+
sha256Hash,
|
|
1935
|
+
field: link.field
|
|
1936
|
+
});
|
|
1937
|
+
if (typeof answer.mediaId !== "string" || !answer.mediaId)
|
|
1938
|
+
throw new MediaUploadError(
|
|
1939
|
+
"POST /api/public/media answered without a mediaId"
|
|
1940
|
+
);
|
|
1941
|
+
const reference = new MediaReference(answer.mediaId, link.contentType);
|
|
1942
|
+
if (answer.uploadUrl)
|
|
1943
|
+
await this.store(reference, answer.uploadUrl, data, sha256Hash);
|
|
1944
|
+
return reference;
|
|
1945
|
+
}
|
|
1946
|
+
async store(reference, url, data, sha256Hash) {
|
|
1947
|
+
const started = Date.now();
|
|
1948
|
+
const { status, error } = await this.put(url, data, {
|
|
1949
|
+
"Content-Type": reference.contentType,
|
|
1950
|
+
"x-amz-checksum-sha256": sha256Hash,
|
|
1951
|
+
"x-ms-blob-type": "BlockBlob"
|
|
1952
|
+
});
|
|
1953
|
+
await this.send(
|
|
1954
|
+
"PATCH",
|
|
1955
|
+
`${MEDIA_PATH}/${encodeURIComponent(reference.mediaId)}`,
|
|
1956
|
+
{
|
|
1957
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1958
|
+
uploadHttpStatus: status,
|
|
1959
|
+
uploadHttpError: error,
|
|
1960
|
+
uploadTimeMs: Date.now() - started
|
|
1961
|
+
}
|
|
1962
|
+
);
|
|
1963
|
+
if (status < 200 || status >= 300)
|
|
1964
|
+
throw new MediaUploadError(
|
|
1965
|
+
`media storage refused the upload (HTTP ${status})`,
|
|
1966
|
+
status
|
|
1967
|
+
);
|
|
1968
|
+
}
|
|
1969
|
+
async send(method, route, body) {
|
|
1970
|
+
const reply = await this.transport.send(method, route, body);
|
|
1971
|
+
if (!reply.ok)
|
|
1972
|
+
throw new MediaUploadError(
|
|
1973
|
+
`${method} ${route} -> ${reply.status}`,
|
|
1974
|
+
reply.status
|
|
1975
|
+
);
|
|
1976
|
+
return reply.text ? JSON.parse(reply.text) : {};
|
|
1977
|
+
}
|
|
1978
|
+
};
|
|
1488
1979
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1489
1980
|
0 && (module.exports = {
|
|
1490
1981
|
IdentitySpanProcessor,
|
|
1982
|
+
MAX_MEDIA_BYTES,
|
|
1983
|
+
MediaReference,
|
|
1984
|
+
MediaUploadError,
|
|
1985
|
+
MediaUploader,
|
|
1986
|
+
SECRET_KEYS,
|
|
1491
1987
|
SESSION_ID_ATTRIBUTE,
|
|
1988
|
+
SecretRedactingSpanProcessor,
|
|
1492
1989
|
SessionSpanProcessor,
|
|
1990
|
+
SpanSecrets,
|
|
1493
1991
|
USER_ID_ATTRIBUTE,
|
|
1992
|
+
WORLD_SESSION_ID_ATTRIBUTE,
|
|
1494
1993
|
flush,
|
|
1495
1994
|
getAmbientSessionId,
|
|
1995
|
+
getCurrentWorldSessionId,
|
|
1496
1996
|
identify,
|
|
1497
1997
|
init,
|
|
1498
1998
|
initIsolated,
|
|
@@ -1505,6 +2005,7 @@ async function* observeClaude(stream, options) {
|
|
|
1505
2005
|
resetAmbientUser,
|
|
1506
2006
|
resolveUserId,
|
|
1507
2007
|
shutdown,
|
|
1508
|
-
withSession
|
|
2008
|
+
withSession,
|
|
2009
|
+
withWorldSession
|
|
1509
2010
|
});
|
|
1510
2011
|
//# sourceMappingURL=index.js.map
|