braintrust 3.13.0 → 3.14.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 (50) hide show
  1. package/dev/dist/index.d.mts +6 -8
  2. package/dev/dist/index.d.ts +6 -8
  3. package/dev/dist/index.js +809 -466
  4. package/dev/dist/index.mjs +367 -24
  5. package/dist/apply-auto-instrumentation.js +204 -174
  6. package/dist/apply-auto-instrumentation.mjs +35 -5
  7. package/dist/auto-instrumentations/bundler/esbuild.cjs +225 -1
  8. package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -1
  9. package/dist/auto-instrumentations/bundler/next.cjs +225 -1
  10. package/dist/auto-instrumentations/bundler/next.mjs +3 -2
  11. package/dist/auto-instrumentations/bundler/rollup.cjs +225 -1
  12. package/dist/auto-instrumentations/bundler/rollup.mjs +2 -1
  13. package/dist/auto-instrumentations/bundler/vite.cjs +225 -1
  14. package/dist/auto-instrumentations/bundler/vite.mjs +2 -1
  15. package/dist/auto-instrumentations/bundler/webpack-loader.cjs +8 -0
  16. package/dist/auto-instrumentations/bundler/webpack.cjs +225 -1
  17. package/dist/auto-instrumentations/bundler/webpack.mjs +3 -2
  18. package/dist/auto-instrumentations/chunk-J57YF7WS.mjs +208 -0
  19. package/dist/auto-instrumentations/{chunk-WFEUJACP.mjs → chunk-OTUQ7KH5.mjs} +1 -1
  20. package/dist/auto-instrumentations/chunk-QFMACSOL.mjs +280 -0
  21. package/dist/auto-instrumentations/{chunk-GJOO4ESL.mjs → chunk-XKAAVWT6.mjs} +23 -1
  22. package/dist/auto-instrumentations/hook.mjs +7980 -7
  23. package/dist/auto-instrumentations/loader/cjs-patch.cjs +194 -4
  24. package/dist/auto-instrumentations/loader/cjs-patch.mjs +13 -27
  25. package/dist/auto-instrumentations/loader/esm-hook.mjs +24 -10
  26. package/dist/browser.d.mts +127 -11
  27. package/dist/browser.d.ts +127 -11
  28. package/dist/browser.js +293 -24
  29. package/dist/browser.mjs +293 -24
  30. package/dist/{chunk-26JGOELH.js → chunk-NKD77KGB.js} +179 -1
  31. package/dist/{chunk-75IQCUB2.mjs → chunk-NU2GSPHX.mjs} +179 -1
  32. package/dist/cli.js +374 -51
  33. package/dist/edge-light.d.mts +1 -1
  34. package/dist/edge-light.d.ts +1 -1
  35. package/dist/edge-light.js +293 -24
  36. package/dist/edge-light.mjs +293 -24
  37. package/dist/index.d.mts +127 -11
  38. package/dist/index.d.ts +127 -11
  39. package/dist/index.js +1113 -838
  40. package/dist/index.mjs +305 -30
  41. package/dist/instrumentation/index.d.mts +7 -8
  42. package/dist/instrumentation/index.d.ts +7 -8
  43. package/dist/instrumentation/index.js +101 -24
  44. package/dist/instrumentation/index.mjs +101 -24
  45. package/dist/workerd.d.mts +1 -1
  46. package/dist/workerd.d.ts +1 -1
  47. package/dist/workerd.js +293 -24
  48. package/dist/workerd.mjs +293 -24
  49. package/package.json +1 -7
  50. package/dist/auto-instrumentations/chunk-MWZXZQUO.mjs +0 -81
@@ -685,6 +685,7 @@ var envIntegrationAliases = {
685
685
  cursorsdk: "cursorSDK",
686
686
  flue: "flue",
687
687
  "flue-runtime": "flue",
688
+ mastra: "mastra",
688
689
  "openai-agents": "openAIAgents",
689
690
  openaiagents: "openAIAgents",
690
691
  "openai-agents-core": "openAIAgents",
@@ -727,6 +728,7 @@ function getDefaultInstrumentationIntegrations() {
727
728
  cursor: true,
728
729
  cursorSDK: true,
729
730
  flue: true,
731
+ mastra: true,
730
732
  openAIAgents: true,
731
733
  openrouter: true,
732
734
  openrouterAgent: true,
@@ -755,6 +757,179 @@ function isInstrumentationIntegrationDisabled(integrations, ...names) {
755
757
  return names.some((name) => integrations?.[name] === false);
756
758
  }
757
759
 
760
+ // src/auto-instrumentations/loader/mastra-observability-patch.ts
761
+ var MASTRA_EXPORTER_FACTORY_GLOBAL = "__braintrustMastraExporterFactory";
762
+ function installMastraExporterFactory(factory) {
763
+ const globals = globalThis;
764
+ globals[MASTRA_EXPORTER_FACTORY_GLOBAL] ??= factory;
765
+ }
766
+ var MASTRA_CORE_PACKAGE = "@mastra/core";
767
+ var MASTRA_OBSERVABILITY_PACKAGE = "@mastra/observability";
768
+ var MASTRA_CORE_ENTRY_PATHS = /* @__PURE__ */ new Set([
769
+ "dist/index.js",
770
+ "dist/index.cjs",
771
+ "dist/mastra/index.js",
772
+ "dist/mastra/index.cjs"
773
+ ]);
774
+ var MASTRA_OBSERVABILITY_ENTRY_PATHS = /* @__PURE__ */ new Set([
775
+ "dist/index.js",
776
+ "dist/index.cjs"
777
+ ]);
778
+ function classifyMastraTarget(packageName, modulePath) {
779
+ if (packageName === MASTRA_CORE_PACKAGE && MASTRA_CORE_ENTRY_PATHS.has(modulePath)) {
780
+ return "core";
781
+ }
782
+ if (packageName === MASTRA_OBSERVABILITY_PACKAGE && MASTRA_OBSERVABILITY_ENTRY_PATHS.has(modulePath)) {
783
+ return "observability";
784
+ }
785
+ return null;
786
+ }
787
+ function extractChunkPath(source) {
788
+ const esmMatch = source.match(
789
+ /export\s*\{\s*Mastra(?:\s+as\s+\w+)?\s*\}\s*from\s*['"]([^'"]+)['"]/
790
+ );
791
+ if (esmMatch) return esmMatch[1];
792
+ const cjsMatch = source.match(
793
+ /require\s*\(\s*['"]([^'"]+chunk-[^'"]+)['"]\s*\)/
794
+ );
795
+ if (cjsMatch) return cjsMatch[1];
796
+ return null;
797
+ }
798
+ var EXPORTER_FACTORY_KEY = JSON.stringify(MASTRA_EXPORTER_FACTORY_GLOBAL);
799
+ var MASTRA_PROXY_HANDLER_BODY = `
800
+ {
801
+ construct(target, args, newTarget) {
802
+ var firstArg = args[0];
803
+ if (
804
+ (!firstArg || typeof firstArg !== "object" || !firstArg.observability) &&
805
+ __braintrustObservabilityClass
806
+ ) {
807
+ try {
808
+ // serviceName is required by Mastra's Observability validator; pass
809
+ // something sensible by default. Users who want a different name
810
+ // should construct Observability themselves.
811
+ var observability = new __braintrustObservabilityClass({
812
+ configs: { default: { serviceName: "mastra" } },
813
+ });
814
+ args = args.slice();
815
+ args[0] = Object.assign({}, firstArg, { observability: observability });
816
+ } catch (e) {
817
+ // Fall through. Mastra will use its own NoOp; user code still works,
818
+ // just without auto-instrumentation.
819
+ }
820
+ }
821
+ return Reflect.construct(target, args, newTarget);
822
+ },
823
+ }`;
824
+ function buildMastraEsmWrapper(chunkPath) {
825
+ const chunk = JSON.stringify(chunkPath);
826
+ return `import { Mastra as __braintrustOrigMastra } from ${chunk};
827
+ import { createRequire as __braintrustCreateRequire } from "node:module";
828
+
829
+ let __braintrustObservabilityClass = null;
830
+ try {
831
+ // Resolve @mastra/observability relative to this module (the Mastra entry),
832
+ // so it's looked up from the user's node_modules tree.
833
+ const __braintrustRequire = __braintrustCreateRequire(import.meta.url);
834
+ __braintrustObservabilityClass =
835
+ __braintrustRequire("@mastra/observability").Observability;
836
+ } catch (e) {
837
+ // @mastra/observability isn't installed; the construct trap will skip the
838
+ // auto-construct branch and Mastra falls back to its own NoOp.
839
+ }
840
+ const Mastra = new Proxy(__braintrustOrigMastra, ${MASTRA_PROXY_HANDLER_BODY});
841
+ export { Mastra };
842
+ `;
843
+ }
844
+ function buildMastraCjsWrapper(chunkPath) {
845
+ const chunk = JSON.stringify(chunkPath);
846
+ return `'use strict';
847
+ const __braintrustChunk = require(${chunk});
848
+
849
+ let __braintrustObservabilityClass = null;
850
+ try {
851
+ __braintrustObservabilityClass = require("@mastra/observability").Observability;
852
+ } catch (e) {
853
+ // @mastra/observability isn't installed; same fallback as the ESM wrapper.
854
+ }
855
+
856
+ const __braintrustWrappedMastra = new Proxy(
857
+ __braintrustChunk.Mastra,
858
+ ${MASTRA_PROXY_HANDLER_BODY},
859
+ );
860
+ Object.defineProperty(exports, "Mastra", {
861
+ enumerable: true,
862
+ configurable: true,
863
+ get: function () { return __braintrustWrappedMastra; }
864
+ });
865
+ `;
866
+ }
867
+ var OBSERVABILITY_APPEND_BODY = `
868
+ ;(function __braintrustWrapObservability() {
869
+ // Top-level so we can both read and reassign the var binding the original
870
+ // entry declared.
871
+ if (typeof Observability === "undefined") return;
872
+ if (Observability.__braintrustWrapped) return;
873
+ function __braintrustEnsureExporter(rawConfig) {
874
+ try {
875
+ var factory = globalThis[${EXPORTER_FACTORY_KEY}];
876
+ if (typeof factory !== "function") return rawConfig;
877
+ var config = rawConfig && typeof rawConfig === "object" ? rawConfig : {};
878
+ var configsIn = config.configs && typeof config.configs === "object" ? config.configs : null;
879
+ var configsOut = {};
880
+ var hadEntries = false;
881
+ if (configsIn) {
882
+ for (var name in configsIn) {
883
+ if (!Object.prototype.hasOwnProperty.call(configsIn, name)) continue;
884
+ hadEntries = true;
885
+ var inst = configsIn[name] || {};
886
+ var existing = Array.isArray(inst.exporters) ? inst.exporters : [];
887
+ var hasOurs = existing.some(function (e) { return e && e.name === "braintrust"; });
888
+ configsOut[name] = Object.assign({}, inst, {
889
+ exporters: hasOurs ? existing : existing.concat([factory()]),
890
+ });
891
+ }
892
+ }
893
+ if (!hadEntries) {
894
+ configsOut.default = {
895
+ serviceName: "mastra",
896
+ exporters: [factory()],
897
+ };
898
+ }
899
+ return Object.assign({}, config, { configs: configsOut });
900
+ } catch (e) {
901
+ return rawConfig;
902
+ }
903
+ }
904
+ var __OriginalObservability = Observability;
905
+ Observability = new Proxy(__OriginalObservability, {
906
+ construct: function (target, args, newTarget) {
907
+ var nextArgs = args.slice();
908
+ nextArgs[0] = __braintrustEnsureExporter(nextArgs[0]);
909
+ return Reflect.construct(target, nextArgs, newTarget);
910
+ },
911
+ });
912
+ Observability.__braintrustWrapped = true;
913
+ if (typeof exports !== "undefined" && exports && typeof exports === "object") {
914
+ try {
915
+ Object.defineProperty(exports, "Observability", {
916
+ enumerable: true,
917
+ configurable: true,
918
+ get: function () { return Observability; },
919
+ });
920
+ } catch (e) {}
921
+ }
922
+ })();
923
+ `;
924
+ function patchMastraSource(source, target, format) {
925
+ if (target === "core") {
926
+ const chunkPath = extractChunkPath(source);
927
+ if (!chunkPath) return source;
928
+ return format === "esm" ? buildMastraEsmWrapper(chunkPath) : buildMastraCjsWrapper(chunkPath);
929
+ }
930
+ return source + OBSERVABILITY_APPEND_BODY;
931
+ }
932
+
758
933
  // src/instrumentation/plugins/openai-agents-channels.ts
759
934
  var openAIAgentsCoreChannels = defineChannels("@openai/agents-core", {
760
935
  onTraceStart: channel({
@@ -813,5 +988,8 @@ export {
813
988
  langChainChannels,
814
989
  getDefaultInstrumentationIntegrations,
815
990
  readDisabledInstrumentationEnvConfig,
816
- isInstrumentationIntegrationDisabled
991
+ isInstrumentationIntegrationDisabled,
992
+ installMastraExporterFactory,
993
+ classifyMastraTarget,
994
+ patchMastraSource
817
995
  };