braintrust 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22;var __defProp = Object.defineProperty;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23;var __defProp = Object.defineProperty;
2
2
  var __export = (target, all) => {
3
3
  for (var name in all)
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -110,11 +110,19 @@ function getIdGenerator() {
110
110
 
111
111
  // util/db_fields.ts
112
112
  var TRANSACTION_ID_FIELD = "_xact_id";
113
+ var OBJECT_DELETE_FIELD = "_object_delete";
113
114
  var IS_MERGE_FIELD = "_is_merge";
114
115
  var AUDIT_SOURCE_FIELD = "_audit_source";
115
116
  var AUDIT_METADATA_FIELD = "_audit_metadata";
116
117
  var VALID_SOURCES = ["app", "api", "external"];
117
- var PARENT_ID_FIELD = "_parent_id";
118
+ var OBJECT_ID_KEYS = [
119
+ "experiment_id",
120
+ "dataset_id",
121
+ "prompt_session_id",
122
+ "project_id",
123
+ "log_id",
124
+ "function_data"
125
+ ];
118
126
 
119
127
  // util/span_identifier_v3.ts
120
128
 
@@ -785,13 +793,6 @@ function mergeDictsWithPathsHelper({
785
793
  function mergeDicts(mergeInto, mergeFrom) {
786
794
  return mergeDictsWithPaths({ mergeInto, mergeFrom, mergePaths: [] });
787
795
  }
788
- function mapAt(m, k) {
789
- const ret = m.get(k);
790
- if (ret === void 0) {
791
- throw new Error(`Map does not contain key ${k}`);
792
- }
793
- return ret;
794
- }
795
796
  function recordFind(m, k) {
796
797
  return m[k];
797
798
  }
@@ -806,72 +807,8 @@ function getObjValueByPath(row, path) {
806
807
  return curr;
807
808
  }
808
809
 
809
- // util/graph_util.ts
810
- function depthFirstSearch(args) {
811
- const { graph, firstVisitF, lastVisitF } = args;
812
- for (const vs of graph.values()) {
813
- for (const v of vs.values()) {
814
- if (!graph.has(v)) {
815
- throw new Error(`Outgoing vertex ${v} must be a key in the graph`);
816
- }
817
- }
818
- }
819
- const firstVisitedVertices = /* @__PURE__ */ new Set();
820
- const visitationOrder = _nullishCoalesce(args.visitationOrder, () => ( [...graph.keys()]));
821
- const events = visitationOrder.map((vertex) => ({ eventType: "first", vertex, extras: {} })).reverse();
822
- while (events.length) {
823
- const { eventType, vertex, extras } = events.pop();
824
- if (eventType === "last") {
825
- _optionalChain([lastVisitF, 'optionalCall', _8 => _8(vertex)]);
826
- continue;
827
- }
828
- if (firstVisitedVertices.has(vertex)) {
829
- continue;
830
- }
831
- firstVisitedVertices.add(vertex);
832
- _optionalChain([firstVisitF, 'optionalCall', _9 => _9(vertex, { parentVertex: extras.parentVertex })]);
833
- events.push({ eventType: "last", vertex, extras: {} });
834
- mapAt(graph, vertex).forEach((child) => {
835
- events.push({
836
- eventType: "first",
837
- vertex: child,
838
- extras: { parentVertex: vertex }
839
- });
840
- });
841
- }
842
- }
843
- function undirectedConnectedComponents(graph) {
844
- const directedGraph = new Map(
845
- [...graph.vertices].map((v) => [v, /* @__PURE__ */ new Set()])
846
- );
847
- for (const [i, j] of graph.edges) {
848
- mapAt(directedGraph, i).add(j);
849
- mapAt(directedGraph, j).add(i);
850
- }
851
- let labelCounter = 0;
852
- const vertexLabels = /* @__PURE__ */ new Map();
853
- const firstVisitF = (vertex, args) => {
854
- const label = _optionalChain([args, 'optionalAccess', _10 => _10.parentVertex]) !== void 0 ? mapAt(vertexLabels, _optionalChain([args, 'optionalAccess', _11 => _11.parentVertex])) : labelCounter++;
855
- vertexLabels.set(vertex, label);
856
- };
857
- depthFirstSearch({ graph: directedGraph, firstVisitF });
858
- const output = Array.from({ length: labelCounter }).map(() => []);
859
- for (const [vertex, label] of vertexLabels.entries()) {
860
- output[label].push(vertex);
861
- }
862
- return output;
863
- }
864
- function topologicalSort(graph, visitationOrder) {
865
- const reverseOrdering = [];
866
- const lastVisitF = (vertex) => {
867
- reverseOrdering.push(vertex);
868
- };
869
- depthFirstSearch({ graph, lastVisitF, visitationOrder });
870
- return reverseOrdering.reverse();
871
- }
872
-
873
810
  // util/merge_row_batch.ts
874
- function generateMergedRowKey(row, useParentIdForId) {
811
+ function generateMergedRowKey(row) {
875
812
  return JSON.stringify(
876
813
  [
877
814
  "org_id",
@@ -880,7 +817,7 @@ function generateMergedRowKey(row, useParentIdForId) {
880
817
  "dataset_id",
881
818
  "prompt_session_id",
882
819
  "log_id",
883
- _nullishCoalesce(useParentIdForId, () => ( false)) ? PARENT_ID_FIELD : "id"
820
+ "id"
884
821
  ].map((k) => row[k])
885
822
  );
886
823
  }
@@ -934,96 +871,34 @@ function mergeRowBatch(rows) {
934
871
  rowGroups.set(key, row);
935
872
  }
936
873
  }
937
- const merged = [...rowGroups.values()];
938
- const rowToLabel = new Map(
939
- merged.map((r, i) => [generateMergedRowKey(r), i])
940
- );
941
- const graph = new Map(
942
- Array.from({ length: merged.length }).map((_, i) => [i, /* @__PURE__ */ new Set()])
943
- );
944
- merged.forEach((r, i) => {
945
- const parentId = r[PARENT_ID_FIELD];
946
- if (!parentId) {
947
- return;
948
- }
949
- const parentRowKey = generateMergedRowKey(
950
- r,
951
- true
952
- /* useParentIdForId */
953
- );
954
- const parentLabel = rowToLabel.get(parentRowKey);
955
- if (parentLabel !== void 0) {
956
- mapAt(graph, parentLabel).add(i);
957
- }
958
- });
959
- const connectedComponents = undirectedConnectedComponents({
960
- vertices: new Set(graph.keys()),
961
- edges: new Set(
962
- [...graph.entries()].flatMap(
963
- ([k, vs]) => [...vs].map((v) => {
964
- const ret = [k, v];
965
- return ret;
966
- })
967
- )
968
- )
969
- });
970
- const buckets = connectedComponents.map(
971
- (cc) => topologicalSort(
972
- graph,
973
- cc
974
- /* visitationOrder */
975
- )
976
- );
977
- return buckets.map((bucket) => bucket.map((i) => merged[i]));
874
+ return [...rowGroups.values()];
978
875
  }
979
876
  function batchItems(args) {
980
- let { items } = args;
877
+ const { items } = args;
981
878
  const batchMaxNumItems = _nullishCoalesce(args.batchMaxNumItems, () => ( Number.POSITIVE_INFINITY));
982
879
  const batchMaxNumBytes = _nullishCoalesce(args.batchMaxNumBytes, () => ( Number.POSITIVE_INFINITY));
880
+ const getByteSize = args.getByteSize;
983
881
  const output = [];
984
- let nextItems = [];
985
- let batchSet = [];
986
882
  let batch = [];
987
883
  let batchLen = 0;
988
884
  function addToBatch(item) {
989
885
  batch.push(item);
990
- batchLen += item.length;
886
+ batchLen += getByteSize(item);
991
887
  }
992
888
  function flushBatch() {
993
- batchSet.push(batch);
889
+ output.push(batch);
994
890
  batch = [];
995
891
  batchLen = 0;
996
892
  }
997
- while (items.length) {
998
- for (const bucket of items) {
999
- let i = 0;
1000
- for (const item of bucket) {
1001
- if (batch.length === 0 || item.length + batchLen < batchMaxNumBytes && batch.length < batchMaxNumItems) {
1002
- addToBatch(item);
1003
- } else if (i === 0) {
1004
- flushBatch();
1005
- addToBatch(item);
1006
- } else {
1007
- break;
1008
- }
1009
- ++i;
1010
- }
1011
- if (i < bucket.length) {
1012
- nextItems.push(bucket.slice(i));
1013
- }
1014
- if (batchLen >= batchMaxNumBytes || batch.length > batchMaxNumItems) {
1015
- flushBatch();
1016
- }
1017
- }
1018
- if (batch.length) {
893
+ for (const item of items) {
894
+ const itemSize = getByteSize(item);
895
+ if (batch.length > 0 && !(itemSize + batchLen < batchMaxNumBytes && batch.length < batchMaxNumItems)) {
1019
896
  flushBatch();
1020
897
  }
1021
- if (batchSet.length) {
1022
- output.push(batchSet);
1023
- batchSet = [];
1024
- }
1025
- items = nextItems;
1026
- nextItems = [];
898
+ addToBatch(item);
899
+ }
900
+ if (batch.length > 0) {
901
+ flushBatch();
1027
902
  }
1028
903
  return output;
1029
904
  }
@@ -1141,20 +1016,20 @@ function slugify(text, options) {
1141
1016
  \u00FF: "y"
1142
1017
  };
1143
1018
  const replacement = "-";
1144
- const trim = _optionalChain([options, 'optionalAccess', _12 => _12.trim]) !== false;
1019
+ const trim = _optionalChain([options, 'optionalAccess', _8 => _8.trim]) !== false;
1145
1020
  let slug = text.normalize().split("").reduce((result, ch) => {
1146
1021
  const mapped = charMap[ch] || ch;
1147
1022
  const appendChar = mapped === replacement ? " " : mapped;
1148
1023
  return result + appendChar.replace(/[^\w\s$*_+~.()'"!\-:@]+/g, "");
1149
1024
  }, "");
1150
- if (_optionalChain([options, 'optionalAccess', _13 => _13.strict])) {
1025
+ if (_optionalChain([options, 'optionalAccess', _9 => _9.strict])) {
1151
1026
  slug = slug.replace(/[^A-Za-z0-9\s]/g, "");
1152
1027
  }
1153
1028
  if (trim) {
1154
1029
  slug = slug.trim();
1155
1030
  }
1156
1031
  slug = slug.replace(/\s+/g, replacement);
1157
- if (_optionalChain([options, 'optionalAccess', _14 => _14.lower])) {
1032
+ if (_optionalChain([options, 'optionalAccess', _10 => _10.lower])) {
1158
1033
  slug = slug.toLowerCase();
1159
1034
  }
1160
1035
  return slug;
@@ -1610,10 +1485,15 @@ var FunctionTypeEnum = _v3.z.enum([
1610
1485
  "preprocessor",
1611
1486
  "facet",
1612
1487
  "classifier",
1613
- "tag"
1488
+ "tag",
1489
+ "parameters"
1614
1490
  ]);
1615
1491
  var NullableSavedFunctionId = _v3.z.union([
1616
- _v3.z.object({ type: _v3.z.literal("function"), id: _v3.z.string() }),
1492
+ _v3.z.object({
1493
+ type: _v3.z.literal("function"),
1494
+ id: _v3.z.string(),
1495
+ version: _v3.z.string().optional()
1496
+ }),
1617
1497
  _v3.z.object({
1618
1498
  type: _v3.z.literal("global"),
1619
1499
  name: _v3.z.string(),
@@ -1621,6 +1501,67 @@ var NullableSavedFunctionId = _v3.z.union([
1621
1501
  }),
1622
1502
  _v3.z.null()
1623
1503
  ]);
1504
+ var TopicMapReport = _v3.z.object({
1505
+ version: _v3.z.literal(1),
1506
+ created_at: _v3.z.string().optional(),
1507
+ settings: _v3.z.object({
1508
+ algorithm: _v3.z.enum(["hdbscan", "kmeans", "hierarchical"]),
1509
+ dimension_reduction: _v3.z.enum(["umap", "pca", "none"]),
1510
+ vector_field: _v3.z.string(),
1511
+ embedding_model: _v3.z.string(),
1512
+ n_clusters: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1513
+ umap_dimensions: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1514
+ min_cluster_size: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1515
+ min_samples: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional()
1516
+ }),
1517
+ query_settings: _v3.z.object({
1518
+ hierarchy_threshold: _v3.z.union([_v3.z.number(), _v3.z.null()]),
1519
+ auto_naming: _v3.z.boolean(),
1520
+ skip_cache: _v3.z.boolean(),
1521
+ viz_mode: _v3.z.enum(["bar", "scatter"]),
1522
+ naming_model: _v3.z.string()
1523
+ }).partial(),
1524
+ clusters: _v3.z.array(
1525
+ _v3.z.object({
1526
+ cluster_id: _v3.z.number(),
1527
+ parent_cluster_id: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1528
+ topic_id: _v3.z.string(),
1529
+ count: _v3.z.number(),
1530
+ sample_texts: _v3.z.array(_v3.z.string()),
1531
+ samples: _v3.z.array(
1532
+ _v3.z.object({
1533
+ id: _v3.z.string(),
1534
+ text: _v3.z.string(),
1535
+ root_span_id: _v3.z.string(),
1536
+ span_id: _v3.z.string()
1537
+ })
1538
+ ),
1539
+ name: _v3.z.string().optional(),
1540
+ description: _v3.z.string().optional(),
1541
+ keywords: _v3.z.array(_v3.z.string()).optional(),
1542
+ centroid: _v3.z.array(_v3.z.number()).optional(),
1543
+ parent_id: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1544
+ is_leaf: _v3.z.boolean().optional(),
1545
+ depth: _v3.z.number().optional()
1546
+ })
1547
+ ),
1548
+ embedding_points: _v3.z.array(
1549
+ _v3.z.object({
1550
+ x: _v3.z.number(),
1551
+ y: _v3.z.number(),
1552
+ cluster: _v3.z.number(),
1553
+ text: _v3.z.string().optional()
1554
+ })
1555
+ ).optional()
1556
+ });
1557
+ var TopicMapData = _v3.z.object({
1558
+ type: _v3.z.literal("topic_map"),
1559
+ source_facet: _v3.z.string(),
1560
+ embedding_model: _v3.z.string(),
1561
+ bundle_key: _v3.z.string(),
1562
+ distance_threshold: _v3.z.number().optional(),
1563
+ report: TopicMapReport.optional()
1564
+ });
1624
1565
  var BatchedFacetData = _v3.z.object({
1625
1566
  type: _v3.z.literal("batched_facet"),
1626
1567
  preprocessor: NullableSavedFunctionId.and(_v3.z.unknown()).optional(),
@@ -1629,9 +1570,17 @@ var BatchedFacetData = _v3.z.object({
1629
1570
  name: _v3.z.string(),
1630
1571
  prompt: _v3.z.string(),
1631
1572
  model: _v3.z.string().optional(),
1573
+ embedding_model: _v3.z.string().optional(),
1632
1574
  no_match_pattern: _v3.z.string().optional()
1633
1575
  })
1634
- )
1576
+ ),
1577
+ topic_maps: _v3.z.record(
1578
+ _v3.z.object({
1579
+ function_name: _v3.z.string(),
1580
+ topic_map_id: _v3.z.string().optional(),
1581
+ topic_map_data: TopicMapData
1582
+ })
1583
+ ).optional()
1635
1584
  });
1636
1585
  var BraintrustModelParams = _v3.z.object({
1637
1586
  use_cache: _v3.z.boolean(),
@@ -1842,6 +1791,18 @@ var ObjectReferenceNullish = _v3.z.union([
1842
1791
  }),
1843
1792
  _v3.z.null()
1844
1793
  ]);
1794
+ var SavedFunctionId = _v3.z.union([
1795
+ _v3.z.object({
1796
+ type: _v3.z.literal("function"),
1797
+ id: _v3.z.string(),
1798
+ version: _v3.z.string().optional()
1799
+ }),
1800
+ _v3.z.object({
1801
+ type: _v3.z.literal("global"),
1802
+ name: _v3.z.string(),
1803
+ function_type: FunctionTypeEnum.optional().default("scorer")
1804
+ })
1805
+ ]);
1845
1806
  var DatasetEvent = _v3.z.object({
1846
1807
  id: _v3.z.string(),
1847
1808
  _xact_id: _v3.z.string(),
@@ -1861,7 +1822,36 @@ var DatasetEvent = _v3.z.object({
1861
1822
  is_root: _v3.z.union([_v3.z.boolean(), _v3.z.null()]).optional(),
1862
1823
  origin: ObjectReferenceNullish.optional(),
1863
1824
  comments: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
1864
- audit_data: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional()
1825
+ audit_data: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
1826
+ facets: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
1827
+ classifications: _v3.z.union([
1828
+ _v3.z.record(
1829
+ _v3.z.array(
1830
+ _v3.z.object({
1831
+ id: _v3.z.string(),
1832
+ label: _v3.z.string().optional(),
1833
+ confidence: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1834
+ metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
1835
+ source: SavedFunctionId.and(
1836
+ _v3.z.union([
1837
+ _v3.z.object({
1838
+ type: _v3.z.literal("function"),
1839
+ id: _v3.z.string(),
1840
+ version: _v3.z.string().optional()
1841
+ }),
1842
+ _v3.z.object({
1843
+ type: _v3.z.literal("global"),
1844
+ name: _v3.z.string(),
1845
+ function_type: FunctionTypeEnum.optional().default("scorer")
1846
+ }),
1847
+ _v3.z.null()
1848
+ ])
1849
+ ).optional()
1850
+ })
1851
+ )
1852
+ ),
1853
+ _v3.z.null()
1854
+ ]).optional()
1865
1855
  });
1866
1856
  var EnvVar = _v3.z.object({
1867
1857
  id: _v3.z.string().uuid(),
@@ -1938,7 +1928,8 @@ var SpanType = _v3.z.union([
1938
1928
  "automation",
1939
1929
  "facet",
1940
1930
  "preprocessor",
1941
- "classifier"
1931
+ "classifier",
1932
+ "review"
1942
1933
  ]),
1943
1934
  _v3.z.null()
1944
1935
  ]);
@@ -1979,10 +1970,43 @@ var ExperimentEvent = _v3.z.object({
1979
1970
  is_root: _v3.z.union([_v3.z.boolean(), _v3.z.null()]).optional(),
1980
1971
  origin: ObjectReferenceNullish.optional(),
1981
1972
  comments: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
1982
- audit_data: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional()
1973
+ audit_data: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
1974
+ facets: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
1975
+ classifications: _v3.z.union([
1976
+ _v3.z.record(
1977
+ _v3.z.array(
1978
+ _v3.z.object({
1979
+ id: _v3.z.string(),
1980
+ label: _v3.z.string().optional(),
1981
+ confidence: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
1982
+ metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
1983
+ source: SavedFunctionId.and(
1984
+ _v3.z.union([
1985
+ _v3.z.object({
1986
+ type: _v3.z.literal("function"),
1987
+ id: _v3.z.string(),
1988
+ version: _v3.z.string().optional()
1989
+ }),
1990
+ _v3.z.object({
1991
+ type: _v3.z.literal("global"),
1992
+ name: _v3.z.string(),
1993
+ function_type: FunctionTypeEnum.optional().default("scorer")
1994
+ }),
1995
+ _v3.z.null()
1996
+ ])
1997
+ ).optional()
1998
+ })
1999
+ )
2000
+ ),
2001
+ _v3.z.null()
2002
+ ]).optional()
1983
2003
  });
1984
2004
  var ExtendedSavedFunctionId = _v3.z.union([
1985
- _v3.z.object({ type: _v3.z.literal("function"), id: _v3.z.string() }),
2005
+ _v3.z.object({
2006
+ type: _v3.z.literal("function"),
2007
+ id: _v3.z.string(),
2008
+ version: _v3.z.string().optional()
2009
+ }),
1986
2010
  _v3.z.object({
1987
2011
  type: _v3.z.literal("global"),
1988
2012
  name: _v3.z.string(),
@@ -1999,6 +2023,7 @@ var FacetData = _v3.z.object({
1999
2023
  preprocessor: NullableSavedFunctionId.and(_v3.z.unknown()).optional(),
2000
2024
  prompt: _v3.z.string(),
2001
2025
  model: _v3.z.string().optional(),
2026
+ embedding_model: _v3.z.string().optional(),
2002
2027
  no_match_pattern: _v3.z.string().optional()
2003
2028
  });
2004
2029
  var PromptBlockDataNullish = _v3.z.union([
@@ -2088,14 +2113,6 @@ var PromptParserNullish = _v3.z.union([
2088
2113
  }),
2089
2114
  _v3.z.null()
2090
2115
  ]);
2091
- var SavedFunctionId = _v3.z.union([
2092
- _v3.z.object({ type: _v3.z.literal("function"), id: _v3.z.string() }),
2093
- _v3.z.object({
2094
- type: _v3.z.literal("global"),
2095
- name: _v3.z.string(),
2096
- function_type: FunctionTypeEnum.optional().default("scorer")
2097
- })
2098
- ]);
2099
2116
  var PromptDataNullish = _v3.z.union([
2100
2117
  _v3.z.object({
2101
2118
  prompt: PromptBlockDataNullish,
@@ -2146,7 +2163,8 @@ var FunctionTypeEnumNullish = _v3.z.union([
2146
2163
  "preprocessor",
2147
2164
  "facet",
2148
2165
  "classifier",
2149
- "tag"
2166
+ "tag",
2167
+ "parameters"
2150
2168
  ]),
2151
2169
  _v3.z.null()
2152
2170
  ]);
@@ -2238,7 +2256,8 @@ var FunctionData = _v3.z.union([
2238
2256
  type: _v3.z.literal("remote_eval"),
2239
2257
  endpoint: _v3.z.string(),
2240
2258
  eval_name: _v3.z.string(),
2241
- parameters: _v3.z.object({}).partial().passthrough()
2259
+ parameters: _v3.z.object({}).partial().passthrough(),
2260
+ parameters_version: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional()
2242
2261
  }),
2243
2262
  _v3.z.object({
2244
2263
  type: _v3.z.literal("global"),
@@ -2247,7 +2266,18 @@ var FunctionData = _v3.z.union([
2247
2266
  config: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional()
2248
2267
  }),
2249
2268
  FacetData,
2250
- BatchedFacetData
2269
+ BatchedFacetData,
2270
+ _v3.z.object({
2271
+ type: _v3.z.literal("parameters"),
2272
+ data: _v3.z.object({}).partial().passthrough(),
2273
+ __schema: _v3.z.object({
2274
+ type: _v3.z.literal("object"),
2275
+ properties: _v3.z.record(_v3.z.object({}).partial().passthrough()),
2276
+ required: _v3.z.array(_v3.z.string()).optional(),
2277
+ additionalProperties: _v3.z.boolean().optional()
2278
+ })
2279
+ }),
2280
+ TopicMapData.and(_v3.z.unknown())
2251
2281
  ]);
2252
2282
  var Function = _v3.z.object({
2253
2283
  id: _v3.z.string().uuid(),
@@ -2277,7 +2307,13 @@ var Function = _v3.z.object({
2277
2307
  _v3.z.null()
2278
2308
  ]).optional()
2279
2309
  });
2280
- var FunctionFormat = _v3.z.enum(["llm", "code", "global", "graph"]);
2310
+ var FunctionFormat = _v3.z.enum([
2311
+ "llm",
2312
+ "code",
2313
+ "global",
2314
+ "graph",
2315
+ "topic_map"
2316
+ ]);
2281
2317
  var PromptData = _v3.z.object({
2282
2318
  prompt: PromptBlockDataNullish,
2283
2319
  options: PromptOptionsNullish,
@@ -2360,13 +2396,14 @@ var FunctionObjectType = _v3.z.enum([
2360
2396
  "custom_view",
2361
2397
  "preprocessor",
2362
2398
  "facet",
2363
- "classifier"
2399
+ "classifier",
2400
+ "parameters"
2364
2401
  ]);
2365
2402
  var FunctionOutputType = _v3.z.enum([
2366
2403
  "completion",
2367
2404
  "score",
2368
2405
  "facet",
2369
- "tag",
2406
+ "classification",
2370
2407
  "any"
2371
2408
  ]);
2372
2409
  var GitMetadataSettings = _v3.z.object({
@@ -2402,6 +2439,10 @@ var GroupScope = _v3.z.object({
2402
2439
  idle_seconds: _v3.z.number().optional()
2403
2440
  });
2404
2441
  var IfExists = _v3.z.enum(["error", "ignore", "replace"]);
2442
+ var ImageRenderingMode = _v3.z.union([
2443
+ _v3.z.enum(["auto", "click_to_load", "blocked"]),
2444
+ _v3.z.null()
2445
+ ]);
2405
2446
  var InvokeParent = _v3.z.union([
2406
2447
  _v3.z.object({
2407
2448
  object_type: _v3.z.enum(["project_logs", "experiment", "playground_logs"]),
@@ -2494,7 +2535,8 @@ var Organization = _v3.z.object({
2494
2535
  is_universal_api: _v3.z.union([_v3.z.boolean(), _v3.z.null()]).optional(),
2495
2536
  proxy_url: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2496
2537
  realtime_url: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2497
- created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional()
2538
+ created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
2539
+ image_rendering_mode: ImageRenderingMode.optional()
2498
2540
  });
2499
2541
  var ProjectSettings = _v3.z.union([
2500
2542
  _v3.z.object({
@@ -2635,7 +2677,36 @@ var ProjectLogsEvent = _v3.z.object({
2635
2677
  origin: ObjectReferenceNullish.optional(),
2636
2678
  comments: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
2637
2679
  audit_data: _v3.z.union([_v3.z.array(_v3.z.unknown()), _v3.z.null()]).optional(),
2638
- _async_scoring_state: _v3.z.unknown().optional()
2680
+ _async_scoring_state: _v3.z.unknown().optional(),
2681
+ facets: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
2682
+ classifications: _v3.z.union([
2683
+ _v3.z.record(
2684
+ _v3.z.array(
2685
+ _v3.z.object({
2686
+ id: _v3.z.string(),
2687
+ label: _v3.z.string().optional(),
2688
+ confidence: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional(),
2689
+ metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional(),
2690
+ source: SavedFunctionId.and(
2691
+ _v3.z.union([
2692
+ _v3.z.object({
2693
+ type: _v3.z.literal("function"),
2694
+ id: _v3.z.string(),
2695
+ version: _v3.z.string().optional()
2696
+ }),
2697
+ _v3.z.object({
2698
+ type: _v3.z.literal("global"),
2699
+ name: _v3.z.string(),
2700
+ function_type: FunctionTypeEnum.optional().default("scorer")
2701
+ }),
2702
+ _v3.z.null()
2703
+ ])
2704
+ ).optional()
2705
+ })
2706
+ )
2707
+ ),
2708
+ _v3.z.null()
2709
+ ]).optional()
2639
2710
  });
2640
2711
  var ProjectScoreType = _v3.z.enum([
2641
2712
  "slider",
@@ -2937,12 +3008,15 @@ var View = _v3.z.object({
2937
3008
  "datasets",
2938
3009
  "dataset",
2939
3010
  "prompts",
3011
+ "parameters",
2940
3012
  "tools",
2941
3013
  "scorers",
2942
3014
  "classifiers",
2943
3015
  "logs",
2944
3016
  "monitor",
2945
- "for_review"
3017
+ "for_review_project_log",
3018
+ "for_review_experiments",
3019
+ "for_review_datasets"
2946
3020
  ]),
2947
3021
  name: _v3.z.string(),
2948
3022
  created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
@@ -3687,6 +3761,52 @@ var PromptCache = class {
3687
3761
  }
3688
3762
  };
3689
3763
 
3764
+ // src/prompt-cache/parameters-cache.ts
3765
+ function createCacheKey2(key) {
3766
+ if (key.id) {
3767
+ return `parameters:id:${key.id}`;
3768
+ }
3769
+ const prefix = _nullishCoalesce(key.projectId, () => ( key.projectName));
3770
+ if (!prefix) {
3771
+ throw new Error("Either projectId or projectName must be provided");
3772
+ }
3773
+ if (!key.slug) {
3774
+ throw new Error("Slug must be provided when not using ID");
3775
+ }
3776
+ return `parameters:${prefix}:${key.slug}:${_nullishCoalesce(key.version, () => ( "latest"))}`;
3777
+ }
3778
+ var ParametersCache = class {
3779
+
3780
+
3781
+ constructor(options) {
3782
+ this.memoryCache = options.memoryCache;
3783
+ this.diskCache = options.diskCache;
3784
+ }
3785
+ async get(key) {
3786
+ const cacheKey = createCacheKey2(key);
3787
+ const memoryParams = this.memoryCache.get(cacheKey);
3788
+ if (memoryParams !== void 0) {
3789
+ return memoryParams;
3790
+ }
3791
+ if (this.diskCache) {
3792
+ const diskParams = await this.diskCache.get(cacheKey);
3793
+ if (!diskParams) {
3794
+ return void 0;
3795
+ }
3796
+ this.memoryCache.set(cacheKey, diskParams);
3797
+ return diskParams;
3798
+ }
3799
+ return void 0;
3800
+ }
3801
+ async set(key, value) {
3802
+ const cacheKey = createCacheKey2(key);
3803
+ this.memoryCache.set(cacheKey, value);
3804
+ if (this.diskCache) {
3805
+ await this.diskCache.set(cacheKey, value);
3806
+ }
3807
+ }
3808
+ };
3809
+
3690
3810
  // src/span-cache.ts
3691
3811
  var activeCaches = /* @__PURE__ */ new Set();
3692
3812
  var exitHandlersRegistered = false;
@@ -3708,7 +3828,7 @@ var SpanCache = (_class4 = class {
3708
3828
  // Small in-memory index tracking which rootSpanIds have data
3709
3829
  __init12() {this.rootSpanIndex = /* @__PURE__ */ new Set()}
3710
3830
  constructor(options) {;_class4.prototype.__init6.call(this);_class4.prototype.__init7.call(this);_class4.prototype.__init8.call(this);_class4.prototype.__init9.call(this);_class4.prototype.__init10.call(this);_class4.prototype.__init11.call(this);_class4.prototype.__init12.call(this);_class4.prototype.__init13.call(this);_class4.prototype.__init14.call(this);_class4.prototype.__init15.call(this);
3711
- this._explicitlyDisabled = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _15 => _15.disabled]), () => ( false));
3831
+ this._explicitlyDisabled = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _11 => _11.disabled]), () => ( false));
3712
3832
  }
3713
3833
  /**
3714
3834
  * Disable the cache at runtime. This is called automatically when
@@ -3977,7 +4097,24 @@ var SpanCache = (_class4 = class {
3977
4097
  // src/logger.ts
3978
4098
  var BRAINTRUST_ATTACHMENT = BraintrustAttachmentReference.shape.type.value;
3979
4099
  var EXTERNAL_ATTACHMENT = ExternalAttachmentReference.shape.type.value;
4100
+ var LOGS3_OVERFLOW_REFERENCE_TYPE = "logs3_overflow";
3980
4101
  var BRAINTRUST_PARAMS = Object.keys(BraintrustModelParams.shape);
4102
+ var DEFAULT_MAX_REQUEST_SIZE = 6 * 1024 * 1024;
4103
+ var parametersRowSchema = _v3.z.object({
4104
+ id: _v3.z.string().uuid(),
4105
+ _xact_id: _v3.z.string(),
4106
+ project_id: _v3.z.string().uuid(),
4107
+ name: _v3.z.string(),
4108
+ slug: _v3.z.string(),
4109
+ description: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
4110
+ function_type: _v3.z.literal("parameters"),
4111
+ function_data: _v3.z.object({
4112
+ type: _v3.z.literal("parameters"),
4113
+ data: _v3.z.record(_v3.z.unknown()).optional(),
4114
+ __schema: _v3.z.record(_v3.z.unknown())
4115
+ }),
4116
+ metadata: _v3.z.union([_v3.z.object({}).partial().passthrough(), _v3.z.null()]).optional()
4117
+ });
3981
4118
  var LoginInvalidOrgError = class extends Error {
3982
4119
  constructor(message) {
3983
4120
  super(message);
@@ -4075,7 +4212,7 @@ var NoopSpan = (_class5 = class {
4075
4212
  return this;
4076
4213
  }
4077
4214
  end(args) {
4078
- return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _16 => _16.endTime]), () => ( getCurrentUnixTimestamp()));
4215
+ return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _12 => _12.endTime]), () => ( getCurrentUnixTimestamp()));
4079
4216
  }
4080
4217
  async export() {
4081
4218
  return "";
@@ -4154,6 +4291,17 @@ var BraintrustState = (_class6 = class _BraintrustState {
4154
4291
  max: _nullishCoalesce(Number(isomorph_default.getEnv("BRAINTRUST_PROMPT_CACHE_DISK_MAX")), () => ( 1 << 20))
4155
4292
  }) : void 0;
4156
4293
  this.promptCache = new PromptCache({ memoryCache, diskCache });
4294
+ const parametersMemoryCache = new LRUCache({
4295
+ max: _nullishCoalesce(Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX")), () => ( 1 << 10))
4296
+ });
4297
+ const parametersDiskCache = canUseDiskCache() ? new DiskCache({
4298
+ cacheDir: _nullishCoalesce(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DIR"), () => ( `${_nullishCoalesce(isomorph_default.getEnv("HOME"), () => ( isomorph_default.homedir()))}/.braintrust/parameters_cache`)),
4299
+ max: _nullishCoalesce(Number(isomorph_default.getEnv("BRAINTRUST_PARAMETERS_CACHE_DISK_MAX")), () => ( 1 << 20))
4300
+ }) : void 0;
4301
+ this.parametersCache = new ParametersCache({
4302
+ memoryCache: parametersMemoryCache,
4303
+ diskCache: parametersDiskCache
4304
+ });
4157
4305
  this.spanCache = new SpanCache({ disabled: loginParams.disableSpanCache });
4158
4306
  }
4159
4307
 
@@ -4184,6 +4332,7 @@ var BraintrustState = (_class6 = class _BraintrustState {
4184
4332
  __init29() {this._proxyConn = null}
4185
4333
 
4186
4334
 
4335
+
4187
4336
  __init30() {this._idGenerator = null}
4188
4337
  __init31() {this._contextManager = null}
4189
4338
  __init32() {this._otelFlushCallback = null}
@@ -4299,8 +4448,8 @@ var BraintrustState = (_class6 = class _BraintrustState {
4299
4448
  setFetch(fetch2) {
4300
4449
  this.loginParams.fetch = fetch2;
4301
4450
  this.fetch = fetch2;
4302
- _optionalChain([this, 'access', _17 => _17._apiConn, 'optionalAccess', _18 => _18.setFetch, 'call', _19 => _19(fetch2)]);
4303
- _optionalChain([this, 'access', _20 => _20._appConn, 'optionalAccess', _21 => _21.setFetch, 'call', _22 => _22(fetch2)]);
4451
+ _optionalChain([this, 'access', _13 => _13._apiConn, 'optionalAccess', _14 => _14.setFetch, 'call', _15 => _15(fetch2)]);
4452
+ _optionalChain([this, 'access', _16 => _16._appConn, 'optionalAccess', _17 => _17.setFetch, 'call', _18 => _18(fetch2)]);
4304
4453
  }
4305
4454
  setMaskingFunction(maskingFunction) {
4306
4455
  this.bgLogger().setMaskingFunction(maskingFunction);
@@ -4412,7 +4561,7 @@ function useTestBackgroundLogger() {
4412
4561
  return logger;
4413
4562
  }
4414
4563
  function clearTestBackgroundLogger() {
4415
- _optionalChain([_internalGetGlobalState, 'call', _23 => _23(), 'optionalAccess', _24 => _24.setOverrideBgLogger, 'call', _25 => _25(null)]);
4564
+ _optionalChain([_internalGetGlobalState, 'call', _19 => _19(), 'optionalAccess', _20 => _20.setOverrideBgLogger, 'call', _21 => _21(null)]);
4416
4565
  }
4417
4566
  function initTestExperiment(experimentName, projectName) {
4418
4567
  setInitialTestState();
@@ -4439,8 +4588,8 @@ function _internalSetInitialState() {
4439
4588
  }
4440
4589
  var _internalGetGlobalState = () => _globalState;
4441
4590
  function registerOtelFlush(callback) {
4442
- _optionalChain([_globalState, 'optionalAccess', _26 => _26.registerOtelFlush, 'call', _27 => _27(callback)]);
4443
- _optionalChain([_globalState, 'optionalAccess', _28 => _28.spanCache, 'optionalAccess', _29 => _29.disable, 'call', _30 => _30()]);
4591
+ _optionalChain([_globalState, 'optionalAccess', _22 => _22.registerOtelFlush, 'call', _23 => _23(callback)]);
4592
+ _optionalChain([_globalState, 'optionalAccess', _24 => _24.spanCache, 'optionalAccess', _25 => _25.disable, 'call', _26 => _26()]);
4444
4593
  }
4445
4594
  var FailedHTTPResponse = class extends Error {
4446
4595
 
@@ -5130,8 +5279,8 @@ function _getOrgName(orgName) {
5130
5279
  return orgName || isomorph_default.getEnv("BRAINTRUST_ORG_NAME") || void 0;
5131
5280
  }
5132
5281
  function _getLinkBaseUrl(state, linkArgs) {
5133
- const appUrl = _getAppUrl(state.appUrl || _optionalChain([linkArgs, 'optionalAccess', _31 => _31.app_url]));
5134
- const orgName = _getOrgName(state.orgName || _optionalChain([linkArgs, 'optionalAccess', _32 => _32.org_name]));
5282
+ const appUrl = _getAppUrl(state.appUrl || _optionalChain([linkArgs, 'optionalAccess', _27 => _27.app_url]));
5283
+ const orgName = _getOrgName(state.orgName || _optionalChain([linkArgs, 'optionalAccess', _28 => _28.org_name]));
5135
5284
  if (!orgName) {
5136
5285
  return null;
5137
5286
  }
@@ -5141,9 +5290,9 @@ async function permalink(slug, opts) {
5141
5290
  if (slug === "") {
5142
5291
  return NOOP_SPAN_PERMALINK;
5143
5292
  }
5144
- const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _33 => _33.state]), () => ( _globalState));
5293
+ const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _29 => _29.state]), () => ( _globalState));
5145
5294
  const getOrgName = async () => {
5146
- if (_optionalChain([opts, 'optionalAccess', _34 => _34.orgName])) {
5295
+ if (_optionalChain([opts, 'optionalAccess', _30 => _30.orgName])) {
5147
5296
  return opts.orgName;
5148
5297
  }
5149
5298
  await state.login({});
@@ -5153,7 +5302,7 @@ async function permalink(slug, opts) {
5153
5302
  return state.orgName;
5154
5303
  };
5155
5304
  const getAppUrl = async () => {
5156
- if (_optionalChain([opts, 'optionalAccess', _35 => _35.appUrl])) {
5305
+ if (_optionalChain([opts, 'optionalAccess', _31 => _31.appUrl])) {
5157
5306
  return opts.appUrl;
5158
5307
  }
5159
5308
  await state.login({});
@@ -5288,7 +5437,7 @@ var Logger = (_class7 = class {
5288
5437
  * @returns The `id` of the logged event.
5289
5438
  */
5290
5439
  log(event, options) {
5291
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _36 => _36.allowConcurrentWithSpans])) {
5440
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _32 => _32.allowConcurrentWithSpans])) {
5292
5441
  throw new Error(
5293
5442
  "Cannot run toplevel `log` method while using spans. To log to the span, call `logger.traced` and then log with `span.log`"
5294
5443
  );
@@ -5356,12 +5505,12 @@ var Logger = (_class7 = class {
5356
5505
  state: this.state,
5357
5506
  ...startSpanParentArgs({
5358
5507
  state: this.state,
5359
- parent: _optionalChain([args, 'optionalAccess', _37 => _37.parent]),
5508
+ parent: _optionalChain([args, 'optionalAccess', _33 => _33.parent]),
5360
5509
  parentObjectType: this.parentObjectType(),
5361
5510
  parentObjectId: this.lazyId,
5362
5511
  parentComputeObjectMetadataArgs: this.computeMetadataArgs,
5363
- parentSpanIds: _optionalChain([args, 'optionalAccess', _38 => _38.parentSpanIds]),
5364
- propagatedEvent: _optionalChain([args, 'optionalAccess', _39 => _39.propagatedEvent])
5512
+ parentSpanIds: _optionalChain([args, 'optionalAccess', _34 => _34.parentSpanIds]),
5513
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _35 => _35.propagatedEvent])
5365
5514
  }),
5366
5515
  defaultRootType: "task" /* TASK */
5367
5516
  });
@@ -5437,8 +5586,100 @@ function castLogger(logger, asyncFlush) {
5437
5586
  }
5438
5587
  return logger;
5439
5588
  }
5589
+ var logs3OverflowUploadSchema = _v3.z.object({
5590
+ method: _v3.z.enum(["PUT", "POST"]),
5591
+ signedUrl: _v3.z.string().url(),
5592
+ headers: _v3.z.record(_v3.z.string()).optional(),
5593
+ fields: _v3.z.record(_v3.z.string()).optional(),
5594
+ key: _v3.z.string().min(1)
5595
+ });
5440
5596
  function constructLogs3Data(items) {
5441
- return `{"rows": ${constructJsonArray(items)}, "api_version": 2}`;
5597
+ return `{"rows": ${constructJsonArray(items.map((i) => i.str))}, "api_version": 2}`;
5598
+ }
5599
+ function constructLogs3OverflowRequest(key) {
5600
+ return {
5601
+ rows: {
5602
+ type: LOGS3_OVERFLOW_REFERENCE_TYPE,
5603
+ key
5604
+ },
5605
+ api_version: 2
5606
+ };
5607
+ }
5608
+ function pickLogs3OverflowObjectIds(row) {
5609
+ const objectIds = {};
5610
+ for (const key of OBJECT_ID_KEYS) {
5611
+ if (key in row) {
5612
+ objectIds[key] = row[key];
5613
+ }
5614
+ }
5615
+ return objectIds;
5616
+ }
5617
+ async function uploadLogs3OverflowPayload(upload, payload, fetchFn = fetch) {
5618
+ if (upload.method === "POST") {
5619
+ if (!upload.fields) {
5620
+ throw new Error("Missing logs3 overflow upload fields");
5621
+ }
5622
+ if (typeof FormData === "undefined" || typeof Blob === "undefined") {
5623
+ throw new Error("FormData is not available for logs3 overflow upload");
5624
+ }
5625
+ const form = new FormData();
5626
+ for (const [key, value] of Object.entries(upload.fields)) {
5627
+ form.append(key, value);
5628
+ }
5629
+ const contentType = _nullishCoalesce(upload.fields["Content-Type"], () => ( "application/json"));
5630
+ form.append("file", new Blob([payload], { type: contentType }));
5631
+ const headers2 = {};
5632
+ for (const [key, value] of Object.entries(_nullishCoalesce(upload.headers, () => ( {})))) {
5633
+ if (key.toLowerCase() !== "content-type") {
5634
+ headers2[key] = value;
5635
+ }
5636
+ }
5637
+ const response2 = await fetchFn(upload.signedUrl, {
5638
+ method: "POST",
5639
+ headers: headers2,
5640
+ body: form
5641
+ });
5642
+ if (!response2.ok) {
5643
+ const responseText = await response2.text().catch(() => "");
5644
+ throw new Error(
5645
+ `Failed to upload logs3 overflow payload: ${response2.status} ${responseText}`
5646
+ );
5647
+ }
5648
+ return;
5649
+ }
5650
+ const headers = { ..._nullishCoalesce(upload.headers, () => ( {})) };
5651
+ addAzureBlobHeaders(headers, upload.signedUrl);
5652
+ const response = await fetchFn(upload.signedUrl, {
5653
+ method: "PUT",
5654
+ headers,
5655
+ body: payload
5656
+ });
5657
+ if (!response.ok) {
5658
+ const responseText = await response.text().catch(() => "");
5659
+ throw new Error(
5660
+ `Failed to upload logs3 overflow payload: ${response.status} ${responseText}`
5661
+ );
5662
+ }
5663
+ }
5664
+ function stringifyWithOverflowMeta(item) {
5665
+ const str = JSON.stringify(item);
5666
+ const record = item;
5667
+ return {
5668
+ str,
5669
+ overflowMeta: {
5670
+ object_ids: pickLogs3OverflowObjectIds(record),
5671
+ is_delete: record[OBJECT_DELETE_FIELD] === true,
5672
+ input_row: {
5673
+ byte_size: utf8ByteLength(str)
5674
+ }
5675
+ }
5676
+ };
5677
+ }
5678
+ function utf8ByteLength(value) {
5679
+ if (typeof TextEncoder !== "undefined") {
5680
+ return new TextEncoder().encode(value).length;
5681
+ }
5682
+ return value.length;
5442
5683
  }
5443
5684
  function now() {
5444
5685
  return (/* @__PURE__ */ new Date()).getTime();
@@ -5464,10 +5705,9 @@ var TestBackgroundLogger = (_class8 = class {constructor() { _class8.prototype._
5464
5705
  events.push(await event.get());
5465
5706
  }
5466
5707
  }
5467
- const batch = mergeRowBatch(events);
5468
- let flatBatch = batch.flat();
5708
+ let batch = mergeRowBatch(events);
5469
5709
  if (this.maskingFunction) {
5470
- flatBatch = flatBatch.map((item) => {
5710
+ batch = batch.map((item) => {
5471
5711
  const maskedItem = { ...item };
5472
5712
  for (const field of REDACTION_FIELDS) {
5473
5713
  if (item[field] !== void 0) {
@@ -5492,7 +5732,7 @@ var TestBackgroundLogger = (_class8 = class {constructor() { _class8.prototype._
5492
5732
  return maskedItem;
5493
5733
  });
5494
5734
  }
5495
- return flatBatch;
5735
+ return batch;
5496
5736
  }
5497
5737
  }, _class8);
5498
5738
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
@@ -5505,21 +5745,21 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5505
5745
 
5506
5746
  __init39() {this.maskingFunction = null}
5507
5747
  __init40() {this.syncFlush = false}
5508
- // 6 MB for the AWS lambda gateway (from our own testing).
5509
- __init41() {this.maxRequestSize = 6 * 1024 * 1024}
5510
- __init42() {this.defaultBatchSize = 100}
5511
- __init43() {this.numTries = 3}
5512
- __init44() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
5513
- __init45() {this.queueDropLoggingPeriod = 60}
5514
- __init46() {this.failedPublishPayloadsDir = void 0}
5515
- __init47() {this.allPublishPayloadsDir = void 0}
5516
- __init48() {this.flushChunkSize = 25}
5517
- __init49() {this._disabled = false}
5518
- __init50() {this.queueDropLoggingState = {
5748
+ __init41() {this.maxRequestSizeOverride = null}
5749
+ __init42() {this._maxRequestSizePromise = null}
5750
+ __init43() {this.defaultBatchSize = 100}
5751
+ __init44() {this.numTries = 3}
5752
+ __init45() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
5753
+ __init46() {this.queueDropLoggingPeriod = 60}
5754
+ __init47() {this.failedPublishPayloadsDir = void 0}
5755
+ __init48() {this.allPublishPayloadsDir = void 0}
5756
+ __init49() {this.flushChunkSize = 25}
5757
+ __init50() {this._disabled = false}
5758
+ __init51() {this.queueDropLoggingState = {
5519
5759
  numDropped: 0,
5520
5760
  lastLoggedTimestamp: 0
5521
5761
  }}
5522
- constructor(apiConn, opts) {;_class9.prototype.__init36.call(this);_class9.prototype.__init37.call(this);_class9.prototype.__init38.call(this);_class9.prototype.__init39.call(this);_class9.prototype.__init40.call(this);_class9.prototype.__init41.call(this);_class9.prototype.__init42.call(this);_class9.prototype.__init43.call(this);_class9.prototype.__init44.call(this);_class9.prototype.__init45.call(this);_class9.prototype.__init46.call(this);_class9.prototype.__init47.call(this);_class9.prototype.__init48.call(this);_class9.prototype.__init49.call(this);_class9.prototype.__init50.call(this);
5762
+ constructor(apiConn, opts) {;_class9.prototype.__init36.call(this);_class9.prototype.__init37.call(this);_class9.prototype.__init38.call(this);_class9.prototype.__init39.call(this);_class9.prototype.__init40.call(this);_class9.prototype.__init41.call(this);_class9.prototype.__init42.call(this);_class9.prototype.__init43.call(this);_class9.prototype.__init44.call(this);_class9.prototype.__init45.call(this);_class9.prototype.__init46.call(this);_class9.prototype.__init47.call(this);_class9.prototype.__init48.call(this);_class9.prototype.__init49.call(this);_class9.prototype.__init50.call(this);_class9.prototype.__init51.call(this);
5523
5763
  opts = _nullishCoalesce(opts, () => ( {}));
5524
5764
  this.apiConn = apiConn;
5525
5765
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -5534,7 +5774,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5534
5774
  }
5535
5775
  const maxRequestSizeEnv = Number(isomorph_default.getEnv("BRAINTRUST_MAX_REQUEST_SIZE"));
5536
5776
  if (!isNaN(maxRequestSizeEnv)) {
5537
- this.maxRequestSize = maxRequestSizeEnv;
5777
+ this.maxRequestSizeOverride = maxRequestSizeEnv;
5538
5778
  }
5539
5779
  const numTriesEnv = Number(isomorph_default.getEnv("BRAINTRUST_NUM_RETRIES"));
5540
5780
  if (!isNaN(numTriesEnv)) {
@@ -5596,6 +5836,30 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5596
5836
  }
5597
5837
  }
5598
5838
  }
5839
+ getMaxRequestSize() {
5840
+ if (!this._maxRequestSizePromise) {
5841
+ this._maxRequestSizePromise = (async () => {
5842
+ let serverLimit = null;
5843
+ try {
5844
+ const conn = await this.apiConn.get();
5845
+ const versionInfo = await conn.get_json("version");
5846
+ serverLimit = _nullishCoalesce(_v3.z.object({ logs3_payload_max_bytes: _v3.z.number().nullish() }).parse(versionInfo).logs3_payload_max_bytes, () => ( null));
5847
+ } catch (e) {
5848
+ console.warn("Failed to fetch version info for payload limit:", e);
5849
+ }
5850
+ const validServerLimit = serverLimit !== null && serverLimit > 0 ? serverLimit : null;
5851
+ const canUseOverflow = validServerLimit !== null;
5852
+ let maxRequestSize = DEFAULT_MAX_REQUEST_SIZE;
5853
+ if (this.maxRequestSizeOverride !== null) {
5854
+ maxRequestSize = validServerLimit !== null ? Math.min(this.maxRequestSizeOverride, validServerLimit) : this.maxRequestSizeOverride;
5855
+ } else if (validServerLimit !== null) {
5856
+ maxRequestSize = validServerLimit;
5857
+ }
5858
+ return { maxRequestSize, canUseOverflow };
5859
+ })();
5860
+ }
5861
+ return this._maxRequestSizePromise;
5862
+ }
5599
5863
  async flush() {
5600
5864
  if (this.syncFlush) {
5601
5865
  this.triggerActiveFlush();
@@ -5614,7 +5878,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5614
5878
  this.queue.clear();
5615
5879
  return;
5616
5880
  }
5617
- const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _40 => _40.batchSize]), () => ( this.defaultBatchSize));
5881
+ const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _36 => _36.batchSize]), () => ( this.defaultBatchSize));
5618
5882
  const wrappedItems = this.queue.drain();
5619
5883
  if (wrappedItems.length === 0) {
5620
5884
  return;
@@ -5639,33 +5903,33 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5639
5903
  if (allItems.length === 0) {
5640
5904
  return;
5641
5905
  }
5642
- const allItemsStr = allItems.map(
5643
- (bucket) => bucket.map((item) => JSON.stringify(item))
5906
+ const allItemsWithMeta = allItems.map(
5907
+ (item) => stringifyWithOverflowMeta(item)
5644
5908
  );
5645
- const batchSets = batchItems({
5646
- items: allItemsStr,
5909
+ const maxRequestSizeResult = await this.getMaxRequestSize();
5910
+ const batches = batchItems({
5911
+ items: allItemsWithMeta,
5647
5912
  batchMaxNumItems: batchSize,
5648
- batchMaxNumBytes: this.maxRequestSize / 2
5913
+ batchMaxNumBytes: maxRequestSizeResult.maxRequestSize / 2,
5914
+ getByteSize: (item) => item.str.length
5649
5915
  });
5650
- for (const batchSet of batchSets) {
5651
- const postPromises = batchSet.map(
5652
- (batch) => (async () => {
5653
- try {
5654
- await this.submitLogsRequest(batch);
5655
- return { type: "success" };
5656
- } catch (e) {
5657
- return { type: "error", value: e };
5658
- }
5659
- })()
5916
+ const postPromises = batches.map(
5917
+ (batch) => (async () => {
5918
+ try {
5919
+ await this.submitLogsRequest(batch, maxRequestSizeResult);
5920
+ return { type: "success" };
5921
+ } catch (e) {
5922
+ return { type: "error", value: e };
5923
+ }
5924
+ })()
5925
+ );
5926
+ const results = await Promise.all(postPromises);
5927
+ const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5928
+ if (failingResultErrors.length) {
5929
+ throw new AggregateError(
5930
+ failingResultErrors,
5931
+ `Encountered the following errors while logging:`
5660
5932
  );
5661
- const results = await Promise.all(postPromises);
5662
- const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5663
- if (failingResultErrors.length) {
5664
- throw new AggregateError(
5665
- failingResultErrors,
5666
- `Encountered the following errors while logging:`
5667
- );
5668
- }
5669
5933
  }
5670
5934
  const attachmentErrors = [];
5671
5935
  for (const attachment of attachments) {
@@ -5695,32 +5959,30 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5695
5959
  items.forEach((item) => extractAttachments(item, attachments));
5696
5960
  let mergedItems = mergeRowBatch(items);
5697
5961
  if (this.maskingFunction) {
5698
- mergedItems = mergedItems.map(
5699
- (batch) => batch.map((item) => {
5700
- const maskedItem = { ...item };
5701
- for (const field of REDACTION_FIELDS) {
5702
- if (item[field] !== void 0) {
5703
- const maskedValue = applyMaskingToField(
5704
- this.maskingFunction,
5705
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
5706
- item[field],
5707
- field
5708
- );
5709
- if (maskedValue instanceof MaskingError) {
5710
- delete maskedItem[field];
5711
- if (maskedItem.error) {
5712
- maskedItem.error = `${maskedItem.error}; ${maskedValue.errorMsg}`;
5713
- } else {
5714
- maskedItem.error = maskedValue.errorMsg;
5715
- }
5962
+ mergedItems = mergedItems.map((item) => {
5963
+ const maskedItem = { ...item };
5964
+ for (const field of REDACTION_FIELDS) {
5965
+ if (item[field] !== void 0) {
5966
+ const maskedValue = applyMaskingToField(
5967
+ this.maskingFunction,
5968
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5969
+ item[field],
5970
+ field
5971
+ );
5972
+ if (maskedValue instanceof MaskingError) {
5973
+ delete maskedItem[field];
5974
+ if (maskedItem.error) {
5975
+ maskedItem.error = `${maskedItem.error}; ${maskedValue.errorMsg}`;
5716
5976
  } else {
5717
- maskedItem[field] = maskedValue;
5977
+ maskedItem.error = maskedValue.errorMsg;
5718
5978
  }
5979
+ } else {
5980
+ maskedItem[field] = maskedValue;
5719
5981
  }
5720
5982
  }
5721
- return maskedItem;
5722
- })
5723
- );
5983
+ }
5984
+ return maskedItem;
5985
+ });
5724
5986
  }
5725
5987
  return [mergedItems, attachments];
5726
5988
  } catch (e) {
@@ -5747,20 +6009,73 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5747
6009
  }
5748
6010
  throw new Error("Impossible");
5749
6011
  }
5750
- async submitLogsRequest(items) {
6012
+ async requestLogs3OverflowUpload(conn, args) {
6013
+ let response;
6014
+ try {
6015
+ response = await conn.post_json("logs3/overflow", {
6016
+ content_type: "application/json",
6017
+ size_bytes: args.sizeBytes,
6018
+ rows: args.rows
6019
+ });
6020
+ } catch (error) {
6021
+ const errorStr = JSON.stringify(error);
6022
+ throw new Error(
6023
+ `Failed to request logs3 overflow upload URL: ${errorStr}`
6024
+ );
6025
+ }
6026
+ try {
6027
+ return logs3OverflowUploadSchema.parse(response);
6028
+ } catch (error) {
6029
+ if (error instanceof _v3.ZodError) {
6030
+ const errorStr = JSON.stringify(error.flatten());
6031
+ throw new Error(`Invalid response from API server: ${errorStr}`);
6032
+ }
6033
+ throw error;
6034
+ }
6035
+ }
6036
+ async _uploadLogs3OverflowPayload(conn, upload, payload) {
6037
+ await uploadLogs3OverflowPayload(upload, payload, conn.fetch.bind(conn));
6038
+ }
6039
+ async submitLogsRequest(items, {
6040
+ maxRequestSize,
6041
+ canUseOverflow
6042
+ }) {
5751
6043
  const conn = await this.apiConn.get();
5752
6044
  const dataStr = constructLogs3Data(items);
6045
+ const payloadBytes = utf8ByteLength(dataStr);
6046
+ const useOverflow = canUseOverflow && payloadBytes > maxRequestSize;
5753
6047
  if (this.allPublishPayloadsDir) {
5754
6048
  await _HTTPBackgroundLogger.writePayloadToDir({
5755
6049
  payloadDir: this.allPublishPayloadsDir,
5756
6050
  payload: dataStr
5757
6051
  });
5758
6052
  }
6053
+ let overflowUpload = null;
6054
+ const overflowRows = useOverflow ? items.map((item) => item.overflowMeta) : null;
5759
6055
  for (let i = 0; i < this.numTries; i++) {
5760
6056
  const startTime = now();
5761
6057
  let error = void 0;
5762
6058
  try {
5763
- await conn.post_json("logs3", dataStr);
6059
+ if (overflowRows) {
6060
+ if (!overflowUpload) {
6061
+ const currentUpload = await this.requestLogs3OverflowUpload(conn, {
6062
+ rows: overflowRows,
6063
+ sizeBytes: payloadBytes
6064
+ });
6065
+ await this._uploadLogs3OverflowPayload(
6066
+ conn,
6067
+ currentUpload,
6068
+ dataStr
6069
+ );
6070
+ overflowUpload = currentUpload;
6071
+ }
6072
+ await conn.post_json(
6073
+ "logs3",
6074
+ constructLogs3OverflowRequest(overflowUpload.key)
6075
+ );
6076
+ } else {
6077
+ await conn.post_json("logs3", dataStr);
6078
+ }
5764
6079
  } catch (e) {
5765
6080
  error = e;
5766
6081
  }
@@ -5776,7 +6091,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5776
6091
  return `${error}`;
5777
6092
  }
5778
6093
  })();
5779
- const errMsg = `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}.${retryingText}
6094
+ const errMsg = `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${payloadBytes}.${retryingText}
5780
6095
  Error: ${errorText}`;
5781
6096
  if (!isRetrying && this.failedPublishPayloadsDir) {
5782
6097
  await _HTTPBackgroundLogger.writePayloadToDir({
@@ -5830,7 +6145,7 @@ Error: ${errorText}`;
5830
6145
  try {
5831
6146
  const [allItems, allAttachments] = await this.unwrapLazyValues(wrappedItems);
5832
6147
  const dataStr = constructLogs3Data(
5833
- allItems.map((x) => JSON.stringify(x))
6148
+ allItems.map((x) => stringifyWithOverflowMeta(x))
5834
6149
  );
5835
6150
  const attachmentStr = JSON.stringify(
5836
6151
  allAttachments.map((a) => a.debugInfo())
@@ -5879,10 +6194,10 @@ Error: ${errorText}`;
5879
6194
  } catch (err) {
5880
6195
  if (err instanceof AggregateError) {
5881
6196
  for (const e of err.errors) {
5882
- _optionalChain([this, 'access', _41 => _41.onFlushError, 'optionalCall', _42 => _42(e)]);
6197
+ _optionalChain([this, 'access', _37 => _37.onFlushError, 'optionalCall', _38 => _38(e)]);
5883
6198
  }
5884
6199
  } else {
5885
- _optionalChain([this, 'access', _43 => _43.onFlushError, 'optionalCall', _44 => _44(err)]);
6200
+ _optionalChain([this, 'access', _39 => _39.onFlushError, 'optionalCall', _40 => _40(err)]);
5886
6201
  }
5887
6202
  this.activeFlushError = err;
5888
6203
  } finally {
@@ -6397,12 +6712,133 @@ async function loadPrompt({
6397
6712
  }
6398
6713
  return prompt;
6399
6714
  }
6400
- function setMaskingFunction(maskingFunction) {
6401
- _globalState.setMaskingFunction(maskingFunction);
6402
- }
6403
- async function login(options = {}) {
6404
- const { forceLogin = false } = options || {};
6405
- if (_globalState.loggedIn && !forceLogin) {
6715
+ async function loadParameters({
6716
+ projectName,
6717
+ projectId,
6718
+ slug,
6719
+ version,
6720
+ environment,
6721
+ id,
6722
+ appUrl,
6723
+ apiKey,
6724
+ orgName,
6725
+ fetch: fetch2,
6726
+ forceLogin,
6727
+ state: stateArg
6728
+ }) {
6729
+ if (version && environment) {
6730
+ throw new Error(
6731
+ "Cannot specify both 'version' and 'environment' parameters. Please use only one (remove the other)."
6732
+ );
6733
+ }
6734
+ if (id) {
6735
+ } else if (isEmpty2(projectName) && isEmpty2(projectId)) {
6736
+ throw new Error("Must specify either projectName or projectId");
6737
+ } else if (isEmpty2(slug)) {
6738
+ throw new Error("Must specify slug");
6739
+ }
6740
+ const state = _nullishCoalesce(stateArg, () => ( _globalState));
6741
+ let response;
6742
+ try {
6743
+ await state.login({
6744
+ orgName,
6745
+ apiKey,
6746
+ appUrl,
6747
+ fetch: fetch2,
6748
+ forceLogin
6749
+ });
6750
+ if (id) {
6751
+ response = await state.apiConn().get_json(`v1/function/${id}`, {
6752
+ ...version && { version },
6753
+ ...environment && { environment }
6754
+ });
6755
+ if (response) {
6756
+ response = { objects: [response] };
6757
+ }
6758
+ } else {
6759
+ response = await state.apiConn().get_json("v1/function", {
6760
+ project_name: projectName,
6761
+ project_id: projectId,
6762
+ slug,
6763
+ version,
6764
+ function_type: "parameters",
6765
+ ...environment && { environment }
6766
+ });
6767
+ }
6768
+ } catch (e) {
6769
+ if (environment || version) {
6770
+ throw new Error(`Parameters not found with specified parameters: ${e}`);
6771
+ }
6772
+ console.warn(
6773
+ "Failed to load parameters, attempting to fall back to cache:",
6774
+ e
6775
+ );
6776
+ let parameters2;
6777
+ if (id) {
6778
+ parameters2 = await state.parametersCache.get({ id });
6779
+ if (!parameters2) {
6780
+ throw new Error(
6781
+ `Parameters with id ${id} not found (not found on server or in local cache): ${e}`
6782
+ );
6783
+ }
6784
+ } else {
6785
+ parameters2 = await state.parametersCache.get({
6786
+ slug,
6787
+ projectId,
6788
+ projectName,
6789
+ version: _nullishCoalesce(version, () => ( "latest"))
6790
+ });
6791
+ if (!parameters2) {
6792
+ throw new Error(
6793
+ `Parameters ${slug} (version ${_nullishCoalesce(version, () => ( "latest"))}) not found in ${[
6794
+ _nullishCoalesce(projectName, () => ( projectId))
6795
+ ]} (not found on server or in local cache): ${e}`
6796
+ );
6797
+ }
6798
+ }
6799
+ return parameters2;
6800
+ }
6801
+ if (!("objects" in response) || response.objects.length === 0) {
6802
+ if (id) {
6803
+ throw new Error(`Parameters with id ${id} not found.`);
6804
+ } else {
6805
+ throw new Error(
6806
+ `Parameters ${slug} not found in ${[_nullishCoalesce(projectName, () => ( projectId))]}`
6807
+ );
6808
+ }
6809
+ } else if (response.objects.length > 1) {
6810
+ if (id) {
6811
+ throw new Error(
6812
+ `Multiple parameters found with id ${id}. This should never happen.`
6813
+ );
6814
+ } else {
6815
+ throw new Error(
6816
+ `Multiple parameters found with slug ${slug} in project ${_nullishCoalesce(projectName, () => ( projectId))}. This should never happen.`
6817
+ );
6818
+ }
6819
+ }
6820
+ const metadata = parametersRowSchema.parse(response["objects"][0]);
6821
+ const parameters = new RemoteEvalParameters(metadata);
6822
+ try {
6823
+ if (id) {
6824
+ await state.parametersCache.set({ id }, parameters);
6825
+ } else if (slug) {
6826
+ await state.parametersCache.set(
6827
+ { slug, projectId, projectName, version: _nullishCoalesce(version, () => ( "latest")) },
6828
+ parameters
6829
+ );
6830
+ }
6831
+ } catch (e) {
6832
+ console.warn("Failed to set parameters in cache:", e);
6833
+ }
6834
+ return parameters;
6835
+ }
6836
+ function setMaskingFunction(maskingFunction) {
6837
+ _globalState.setMaskingFunction(maskingFunction);
6838
+ }
6839
+ async function login(options = {}) {
6840
+ const { forceLogin = false } = options || {};
6841
+ if (_globalState.loggedIn && !forceLogin) {
6406
6842
  let checkUpdatedParam2 = function(varname, arg, orig) {
6407
6843
  if (!isEmpty2(arg) && !isEmpty2(orig) && arg !== orig) {
6408
6844
  throw new Error(
@@ -6513,24 +6949,24 @@ async function summarize(options = {}) {
6513
6949
  return await e.summarize(options);
6514
6950
  }
6515
6951
  function currentExperiment(options) {
6516
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _45 => _45.state]), () => ( _globalState));
6952
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _41 => _41.state]), () => ( _globalState));
6517
6953
  return state.currentExperiment;
6518
6954
  }
6519
6955
  function currentLogger(options) {
6520
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _46 => _46.state]), () => ( _globalState));
6521
- return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _47 => _47.asyncFlush]));
6956
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _42 => _42.state]), () => ( _globalState));
6957
+ return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _43 => _43.asyncFlush]));
6522
6958
  }
6523
6959
  function currentSpan(options) {
6524
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _48 => _48.state]), () => ( _globalState));
6960
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _44 => _44.state]), () => ( _globalState));
6525
6961
  return _nullishCoalesce(state.contextManager.getCurrentSpan(), () => ( NOOP_SPAN));
6526
6962
  }
6527
6963
  function getSpanParentObject(options) {
6528
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _49 => _49.state]), () => ( _globalState));
6964
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _45 => _45.state]), () => ( _globalState));
6529
6965
  const parentSpan = currentSpan({ state });
6530
6966
  if (!Object.is(parentSpan, NOOP_SPAN)) {
6531
6967
  return parentSpan;
6532
6968
  }
6533
- const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _50 => _50.parent]), () => ( state.currentParent.getStore()));
6969
+ const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _46 => _46.parent]), () => ( state.currentParent.getStore()));
6534
6970
  if (parentStr) return getSpanComponentsClass().fromStr(parentStr);
6535
6971
  const experiment = currentExperiment();
6536
6972
  if (experiment) {
@@ -6559,7 +6995,7 @@ function traced(callback, args) {
6559
6995
  const { span, isSyncFlushLogger } = startSpanAndIsLogger(args);
6560
6996
  const ret = runCatchFinally(
6561
6997
  () => {
6562
- if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _51 => _51.setCurrent]), () => ( true))) {
6998
+ if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _47 => _47.setCurrent]), () => ( true))) {
6563
6999
  return withCurrent(span, callback);
6564
7000
  } else {
6565
7001
  return callback(span);
@@ -6571,7 +7007,7 @@ function traced(callback, args) {
6571
7007
  },
6572
7008
  () => span.end()
6573
7009
  );
6574
- if (_optionalChain([args, 'optionalAccess', _52 => _52.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _53 => _53.asyncFlush])) {
7010
+ if (_optionalChain([args, 'optionalAccess', _48 => _48.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _49 => _49.asyncFlush])) {
6575
7011
  return ret;
6576
7012
  } else {
6577
7013
  return (async () => {
@@ -6695,14 +7131,14 @@ function wrapTraced(fn, args) {
6695
7131
  };
6696
7132
  const hasExplicitInput = args && args.event && "input" in args.event && args.event.input !== void 0;
6697
7133
  const hasExplicitOutput = args && args.event && args.event.output !== void 0;
6698
- const noTraceIO = _optionalChain([args, 'optionalAccess', _54 => _54.noTraceIO]) || hasExplicitInput || hasExplicitOutput;
7134
+ const noTraceIO = _optionalChain([args, 'optionalAccess', _50 => _50.noTraceIO]) || hasExplicitInput || hasExplicitOutput;
6699
7135
  if (isGeneratorFunction(fn)) {
6700
7136
  return wrapTracedSyncGenerator(fn, spanArgs, !!noTraceIO);
6701
7137
  }
6702
7138
  if (isAsyncGeneratorFunction(fn)) {
6703
7139
  return wrapTracedAsyncGenerator(fn, spanArgs, !!noTraceIO);
6704
7140
  }
6705
- if (_optionalChain([args, 'optionalAccess', _55 => _55.asyncFlush])) {
7141
+ if (_optionalChain([args, 'optionalAccess', _51 => _51.asyncFlush])) {
6706
7142
  return ((...fnArgs) => traced((span) => {
6707
7143
  if (!hasExplicitInput) {
6708
7144
  span.log({ input: fnArgs });
@@ -6740,17 +7176,17 @@ function startSpan(args) {
6740
7176
  return startSpanAndIsLogger(args).span;
6741
7177
  }
6742
7178
  async function flush(options) {
6743
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.state]), () => ( _globalState));
7179
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _52 => _52.state]), () => ( _globalState));
6744
7180
  return await state.bgLogger().flush();
6745
7181
  }
6746
7182
  function setFetch(fetch2) {
6747
7183
  _globalState.setFetch(fetch2);
6748
7184
  }
6749
7185
  function startSpanAndIsLogger(args) {
6750
- const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _57 => _57.state]), () => ( _globalState));
7186
+ const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _53 => _53.state]), () => ( _globalState));
6751
7187
  const parentObject = getSpanParentObject({
6752
- asyncFlush: _optionalChain([args, 'optionalAccess', _58 => _58.asyncFlush]),
6753
- parent: _optionalChain([args, 'optionalAccess', _59 => _59.parent]),
7188
+ asyncFlush: _optionalChain([args, 'optionalAccess', _54 => _54.asyncFlush]),
7189
+ parent: _optionalChain([args, 'optionalAccess', _55 => _55.parent]),
6754
7190
  state
6755
7191
  });
6756
7192
  if (parentObject instanceof SpanComponentsV3 || parentObject instanceof SpanComponentsV4) {
@@ -6767,14 +7203,14 @@ function startSpanAndIsLogger(args) {
6767
7203
  ),
6768
7204
  parentComputeObjectMetadataArgs: _nullishCoalesce(parentObject.data.compute_object_metadata_args, () => ( void 0)),
6769
7205
  parentSpanIds,
6770
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _60 => _60.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
7206
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _56 => _56.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6771
7207
  (_nullishCoalesce(parentObject.data.propagated_event, () => ( void 0)))))
6772
7208
  });
6773
7209
  return {
6774
7210
  span,
6775
7211
  isSyncFlushLogger: parentObject.data.object_type === 2 /* PROJECT_LOGS */ && // Since there's no parent logger here, we're free to choose the async flush
6776
7212
  // behavior, and therefore propagate along whatever we get from the arguments
6777
- _optionalChain([args, 'optionalAccess', _61 => _61.asyncFlush]) === false
7213
+ _optionalChain([args, 'optionalAccess', _57 => _57.asyncFlush]) === false
6778
7214
  };
6779
7215
  } else {
6780
7216
  const span = parentObject.startSpan(args);
@@ -6966,10 +7402,10 @@ function extractAttachments(event, attachments) {
6966
7402
  event[key] = value.reference;
6967
7403
  continue;
6968
7404
  }
6969
- if (_optionalChain([value, 'optionalAccess', _62 => _62.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
7405
+ if (_optionalChain([value, 'optionalAccess', _58 => _58.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
6970
7406
  continue;
6971
7407
  }
6972
- if (_optionalChain([value, 'optionalAccess', _63 => _63.reference, 'optionalAccess', _64 => _64.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _65 => _65.uploader])) {
7408
+ if (_optionalChain([value, 'optionalAccess', _59 => _59.reference, 'optionalAccess', _60 => _60.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _61 => _61.uploader])) {
6973
7409
  const attachment = new Attachment({
6974
7410
  data: value.dataDebugString,
6975
7411
  filename: value.reference.filename,
@@ -7036,13 +7472,13 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
7036
7472
  var DEFAULT_FETCH_BATCH_SIZE = 1e3;
7037
7473
  var MAX_BTQL_ITERATIONS = 1e4;
7038
7474
  var ObjectFetcher = (_class10 = class {
7039
- constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class10.prototype.__init51.call(this);
7475
+ constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class10.prototype.__init52.call(this);
7040
7476
  this.objectType = objectType;
7041
7477
  this.pinnedVersion = pinnedVersion;
7042
7478
  this.mutateRecord = mutateRecord;
7043
7479
  this._internal_btql = _internal_btql;
7044
7480
  }
7045
- __init51() {this._fetchedData = void 0}
7481
+ __init52() {this._fetchedData = void 0}
7046
7482
  get id() {
7047
7483
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
7048
7484
  }
@@ -7120,7 +7556,7 @@ var ObjectFetcher = (_class10 = class {
7120
7556
  }
7121
7557
  return;
7122
7558
  }
7123
- for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _66 => _66.batchSize]))) {
7559
+ for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _62 => _62.batchSize]))) {
7124
7560
  yield record;
7125
7561
  }
7126
7562
  }
@@ -7130,7 +7566,7 @@ var ObjectFetcher = (_class10 = class {
7130
7566
  async fetchedData(options) {
7131
7567
  if (this._fetchedData === void 0) {
7132
7568
  const data = [];
7133
- for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _67 => _67.batchSize]))) {
7569
+ for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _63 => _63.batchSize]))) {
7134
7570
  data.push(record);
7135
7571
  }
7136
7572
  this._fetchedData = data;
@@ -7163,9 +7599,9 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7163
7599
 
7164
7600
 
7165
7601
  // For type identification.
7166
- __init52() {this.kind = "experiment"}
7602
+ __init53() {this.kind = "experiment"}
7167
7603
  constructor(state, lazyMetadata, dataset) {
7168
- super("experiment", void 0, (r) => enrichAttachments(r, state));_class11.prototype.__init52.call(this);;
7604
+ super("experiment", void 0, (r) => enrichAttachments(r, state));_class11.prototype.__init53.call(this);;
7169
7605
  this.lazyMetadata = lazyMetadata;
7170
7606
  this.dataset = dataset;
7171
7607
  this.lastStartTime = getCurrentUnixTimestamp();
@@ -7225,7 +7661,7 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7225
7661
  * @returns The `id` of the logged event.
7226
7662
  */
7227
7663
  log(event, options) {
7228
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _68 => _68.allowConcurrentWithSpans])) {
7664
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _64 => _64.allowConcurrentWithSpans])) {
7229
7665
  throw new Error(
7230
7666
  "Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
7231
7667
  );
@@ -7278,12 +7714,12 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7278
7714
  state: this.state,
7279
7715
  ...startSpanParentArgs({
7280
7716
  state: this.state,
7281
- parent: _optionalChain([args, 'optionalAccess', _69 => _69.parent]),
7717
+ parent: _optionalChain([args, 'optionalAccess', _65 => _65.parent]),
7282
7718
  parentObjectType: this.parentObjectType(),
7283
7719
  parentObjectId: this.lazyId,
7284
7720
  parentComputeObjectMetadataArgs: void 0,
7285
7721
  parentSpanIds: void 0,
7286
- propagatedEvent: _optionalChain([args, 'optionalAccess', _70 => _70.propagatedEvent])
7722
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _66 => _66.propagatedEvent])
7287
7723
  }),
7288
7724
  defaultRootType: "eval" /* EVAL */
7289
7725
  });
@@ -7518,8 +7954,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7518
7954
 
7519
7955
 
7520
7956
 
7521
- __init53() {this.kind = "span"}
7522
- constructor(args) {;_class12.prototype.__init53.call(this);
7957
+ __init54() {this.kind = "span"}
7958
+ constructor(args) {;_class12.prototype.__init54.call(this);
7523
7959
  this._state = args.state;
7524
7960
  const spanAttributes = _nullishCoalesce(args.spanAttributes, () => ( {}));
7525
7961
  const rawEvent = _nullishCoalesce(args.event, () => ( {}));
@@ -7621,8 +8057,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7621
8057
  ...serializableInternalData,
7622
8058
  [IS_MERGE_FIELD]: this.isMerge
7623
8059
  });
7624
- if (_optionalChain([partialRecord, 'access', _71 => _71.metrics, 'optionalAccess', _72 => _72.end])) {
7625
- this.loggedEndTime = _optionalChain([partialRecord, 'access', _73 => _73.metrics, 'optionalAccess', _74 => _74.end]);
8060
+ if (_optionalChain([partialRecord, 'access', _67 => _67.metrics, 'optionalAccess', _68 => _68.end])) {
8061
+ this.loggedEndTime = _optionalChain([partialRecord, 'access', _69 => _69.metrics, 'optionalAccess', _70 => _70.end]);
7626
8062
  }
7627
8063
  if (this.parentObjectType === 1 /* EXPERIMENT */) {
7628
8064
  const cachedSpan = {
@@ -7681,18 +8117,18 @@ var SpanImpl = (_class12 = class _SpanImpl {
7681
8117
  );
7682
8118
  }
7683
8119
  startSpan(args) {
7684
- const parentSpanIds = _optionalChain([args, 'optionalAccess', _75 => _75.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
8120
+ const parentSpanIds = _optionalChain([args, 'optionalAccess', _71 => _71.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
7685
8121
  return new _SpanImpl({
7686
8122
  state: this._state,
7687
8123
  ...args,
7688
8124
  ...startSpanParentArgs({
7689
8125
  state: this._state,
7690
- parent: _optionalChain([args, 'optionalAccess', _76 => _76.parent]),
8126
+ parent: _optionalChain([args, 'optionalAccess', _72 => _72.parent]),
7691
8127
  parentObjectType: this.parentObjectType,
7692
8128
  parentObjectId: this.parentObjectId,
7693
8129
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
7694
8130
  parentSpanIds,
7695
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _77 => _77.propagatedEvent]), () => ( this.propagatedEvent))
8131
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _73 => _73.propagatedEvent]), () => ( this.propagatedEvent))
7696
8132
  })
7697
8133
  });
7698
8134
  }
@@ -7706,12 +8142,12 @@ var SpanImpl = (_class12 = class _SpanImpl {
7706
8142
  ...args,
7707
8143
  ...startSpanParentArgs({
7708
8144
  state: this._state,
7709
- parent: _optionalChain([args, 'optionalAccess', _78 => _78.parent]),
8145
+ parent: _optionalChain([args, 'optionalAccess', _74 => _74.parent]),
7710
8146
  parentObjectType: this.parentObjectType,
7711
8147
  parentObjectId: this.parentObjectId,
7712
8148
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
7713
8149
  parentSpanIds,
7714
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _79 => _79.propagatedEvent]), () => ( this.propagatedEvent))
8150
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _75 => _75.propagatedEvent]), () => ( this.propagatedEvent))
7715
8151
  }),
7716
8152
  spanId
7717
8153
  });
@@ -7720,7 +8156,7 @@ var SpanImpl = (_class12 = class _SpanImpl {
7720
8156
  let endTime;
7721
8157
  let internalData = {};
7722
8158
  if (!this.loggedEndTime) {
7723
- endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _80 => _80.endTime]), () => ( getCurrentUnixTimestamp()));
8159
+ endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _76 => _76.endTime]), () => ( getCurrentUnixTimestamp()));
7724
8160
  internalData = { metrics: { end: endTime } };
7725
8161
  } else {
7726
8162
  endTime = this.loggedEndTime;
@@ -7771,8 +8207,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7771
8207
  const args = this.parentComputeObjectMetadataArgs;
7772
8208
  switch (this.parentObjectType) {
7773
8209
  case 2 /* PROJECT_LOGS */: {
7774
- const projectID = _optionalChain([args, 'optionalAccess', _81 => _81.project_id]) || this.parentObjectId.getSync().value;
7775
- const projectName = _optionalChain([args, 'optionalAccess', _82 => _82.project_name]);
8210
+ const projectID = _optionalChain([args, 'optionalAccess', _77 => _77.project_id]) || this.parentObjectId.getSync().value;
8211
+ const projectName = _optionalChain([args, 'optionalAccess', _78 => _78.project_name]);
7776
8212
  if (projectID) {
7777
8213
  return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
7778
8214
  } else if (projectName) {
@@ -7782,7 +8218,7 @@ var SpanImpl = (_class12 = class _SpanImpl {
7782
8218
  }
7783
8219
  }
7784
8220
  case 1 /* EXPERIMENT */: {
7785
- const expID = _optionalChain([args, 'optionalAccess', _83 => _83.experiment_id]) || _optionalChain([this, 'access', _84 => _84.parentObjectId, 'optionalAccess', _85 => _85.getSync, 'call', _86 => _86(), 'optionalAccess', _87 => _87.value]);
8221
+ const expID = _optionalChain([args, 'optionalAccess', _79 => _79.experiment_id]) || _optionalChain([this, 'access', _80 => _80.parentObjectId, 'optionalAccess', _81 => _81.getSync, 'call', _82 => _82(), 'optionalAccess', _83 => _83.value]);
7786
8222
  if (!expID) {
7787
8223
  return getErrPermlink("provide-experiment-id");
7788
8224
  } else {
@@ -7872,13 +8308,13 @@ var Dataset2 = (_class13 = class extends ObjectFetcher {
7872
8308
  )
7873
8309
  ),
7874
8310
  _internal_btql
7875
- );_class13.prototype.__init54.call(this);_class13.prototype.__init55.call(this);;
8311
+ );_class13.prototype.__init55.call(this);_class13.prototype.__init56.call(this);;
7876
8312
  this.state = state;
7877
8313
  this.lazyMetadata = lazyMetadata;
7878
8314
  }
7879
8315
 
7880
- __init54() {this.__braintrust_dataset_marker = true}
7881
- __init55() {this.newRecords = 0}
8316
+ __init55() {this.__braintrust_dataset_marker = true}
8317
+ __init56() {this.newRecords = 0}
7882
8318
  get id() {
7883
8319
  return (async () => {
7884
8320
  return (await this.lazyMetadata.get()).dataset.id;
@@ -8232,14 +8668,14 @@ function renderPromptParams(params, args, options = {}) {
8232
8668
  return params;
8233
8669
  }
8234
8670
  var Prompt2 = (_class14 = class _Prompt {
8235
- constructor(metadata, defaults, noTrace) {;_class14.prototype.__init56.call(this);_class14.prototype.__init57.call(this);
8671
+ constructor(metadata, defaults, noTrace) {;_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);
8236
8672
  this.metadata = metadata;
8237
8673
  this.defaults = defaults;
8238
8674
  this.noTrace = noTrace;
8239
8675
  }
8240
8676
 
8241
- __init56() {this.hasParsedPromptData = false}
8242
- __init57() {this.__braintrust_prompt_marker = true}
8677
+ __init57() {this.hasParsedPromptData = false}
8678
+ __init58() {this.__braintrust_prompt_marker = true}
8243
8679
  get id() {
8244
8680
  return this.metadata.id;
8245
8681
  }
@@ -8253,16 +8689,16 @@ var Prompt2 = (_class14 = class _Prompt {
8253
8689
  return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
8254
8690
  }
8255
8691
  get prompt() {
8256
- return _optionalChain([this, 'access', _88 => _88.getParsedPromptData, 'call', _89 => _89(), 'optionalAccess', _90 => _90.prompt]);
8692
+ return _optionalChain([this, 'access', _84 => _84.getParsedPromptData, 'call', _85 => _85(), 'optionalAccess', _86 => _86.prompt]);
8257
8693
  }
8258
8694
  get version() {
8259
8695
  return this.metadata[TRANSACTION_ID_FIELD];
8260
8696
  }
8261
8697
  get options() {
8262
- return _optionalChain([this, 'access', _91 => _91.getParsedPromptData, 'call', _92 => _92(), 'optionalAccess', _93 => _93.options]) || {};
8698
+ return _optionalChain([this, 'access', _87 => _87.getParsedPromptData, 'call', _88 => _88(), 'optionalAccess', _89 => _89.options]) || {};
8263
8699
  }
8264
8700
  get templateFormat() {
8265
- return _optionalChain([this, 'access', _94 => _94.getParsedPromptData, 'call', _95 => _95(), 'optionalAccess', _96 => _96.template_format]);
8701
+ return _optionalChain([this, 'access', _90 => _90.getParsedPromptData, 'call', _91 => _91(), 'optionalAccess', _92 => _92.template_format]);
8266
8702
  }
8267
8703
  get promptData() {
8268
8704
  return this.getParsedPromptData();
@@ -8422,7 +8858,7 @@ var Prompt2 = (_class14 = class _Prompt {
8422
8858
  return {
8423
8859
  type: "chat",
8424
8860
  messages,
8425
- ..._optionalChain([prompt, 'access', _97 => _97.tools, 'optionalAccess', _98 => _98.trim, 'call', _99 => _99()]) ? {
8861
+ ..._optionalChain([prompt, 'access', _93 => _93.tools, 'optionalAccess', _94 => _94.trim, 'call', _95 => _95()]) ? {
8426
8862
  tools: render(prompt.tools)
8427
8863
  } : void 0
8428
8864
  };
@@ -8467,6 +8903,55 @@ var Prompt2 = (_class14 = class _Prompt {
8467
8903
  );
8468
8904
  }
8469
8905
  }, _class14);
8906
+ var RemoteEvalParameters = (_class15 = class {
8907
+ constructor(metadata) {;_class15.prototype.__init59.call(this);
8908
+ this.metadata = metadata;
8909
+ }
8910
+ __init59() {this.__braintrust_parameters_marker = true}
8911
+ get id() {
8912
+ return this.metadata.id;
8913
+ }
8914
+ get projectId() {
8915
+ return this.metadata.project_id;
8916
+ }
8917
+ get name() {
8918
+ return this.metadata.name;
8919
+ }
8920
+ get slug() {
8921
+ return this.metadata.slug;
8922
+ }
8923
+ get version() {
8924
+ return this.metadata[TRANSACTION_ID_FIELD];
8925
+ }
8926
+ get schema() {
8927
+ return this.metadata.function_data.__schema;
8928
+ }
8929
+ get data() {
8930
+ return _nullishCoalesce(this.metadata.function_data.data, () => ( {}));
8931
+ }
8932
+ validate(data) {
8933
+ if (typeof data !== "object" || data === null) {
8934
+ return false;
8935
+ }
8936
+ const schemaProps = this.schema.properties;
8937
+ if (typeof schemaProps !== "object" || schemaProps === null) {
8938
+ return true;
8939
+ }
8940
+ for (const key of Object.keys(schemaProps)) {
8941
+ if (!(key in data)) {
8942
+ const required = Array.isArray(this.schema.required) ? this.schema.required : [];
8943
+ if (required.includes(key)) {
8944
+ return false;
8945
+ }
8946
+ }
8947
+ }
8948
+ return true;
8949
+ }
8950
+ static isParameters(x) {
8951
+ return typeof x === "object" && x !== null && "__braintrust_parameters_marker" in x && // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
8952
+ x.__braintrust_parameters_marker === true;
8953
+ }
8954
+ }, _class15);
8470
8955
  var TEST_API_KEY = "___TEST_API_KEY__THIS_IS_NOT_REAL___";
8471
8956
  function setInitialTestState() {
8472
8957
  if (!_internalGetGlobalState()) {
@@ -8531,9 +9016,9 @@ async function getPromptVersions(projectId, promptId) {
8531
9016
  );
8532
9017
  }
8533
9018
  const result = await response.json();
8534
- return _optionalChain([result, 'access', _100 => _100.data, 'optionalAccess', _101 => _101.filter, 'call', _102 => _102(
8535
- (entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access', _103 => _103.audit_data, 'optionalAccess', _104 => _104.action]))
8536
- ), 'access', _105 => _105.map, 'call', _106 => _106((entry) => prettifyXact(entry._xact_id))]) || [];
9019
+ return _optionalChain([result, 'access', _96 => _96.data, 'optionalAccess', _97 => _97.filter, 'call', _98 => _98(
9020
+ (entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access', _99 => _99.audit_data, 'optionalAccess', _100 => _100.action]))
9021
+ ), 'access', _101 => _101.map, 'call', _102 => _102((entry) => prettifyXact(entry._xact_id))]) || [];
8537
9022
  }
8538
9023
  function resetIdGenStateForTests() {
8539
9024
  const state = _internalGetGlobalState();
@@ -8602,6 +9087,7 @@ __export(exports_exports, {
8602
9087
  CodePrompt: () => CodePrompt,
8603
9088
  ContextManager: () => ContextManager,
8604
9089
  DEFAULT_FETCH_BATCH_SIZE: () => DEFAULT_FETCH_BATCH_SIZE,
9090
+ DEFAULT_MAX_REQUEST_SIZE: () => DEFAULT_MAX_REQUEST_SIZE,
8605
9091
  Dataset: () => Dataset2,
8606
9092
  ERR_PERMALINK: () => ERR_PERMALINK,
8607
9093
  Eval: () => Eval,
@@ -8612,6 +9098,7 @@ __export(exports_exports, {
8612
9098
  IDGenerator: () => IDGenerator,
8613
9099
  JSONAttachment: () => JSONAttachment,
8614
9100
  LEGACY_CACHED_HEADER: () => LEGACY_CACHED_HEADER,
9101
+ LOGS3_OVERFLOW_REFERENCE_TYPE: () => LOGS3_OVERFLOW_REFERENCE_TYPE,
8615
9102
  LazyValue: () => LazyValue,
8616
9103
  Logger: () => Logger,
8617
9104
  LoginInvalidOrgError: () => LoginInvalidOrgError,
@@ -8636,8 +9123,10 @@ __export(exports_exports, {
8636
9123
  _exportsForTestingOnly: () => _exportsForTestingOnly,
8637
9124
  _internalGetGlobalState: () => _internalGetGlobalState,
8638
9125
  _internalSetInitialState: () => _internalSetInitialState,
9126
+ addAzureBlobHeaders: () => addAzureBlobHeaders,
8639
9127
  braintrustStreamChunkSchema: () => braintrustStreamChunkSchema,
8640
9128
  buildLocalSummary: () => buildLocalSummary,
9129
+ constructLogs3OverflowRequest: () => constructLogs3OverflowRequest,
8641
9130
  createFinalValuePassThroughStream: () => createFinalValuePassThroughStream,
8642
9131
  currentExperiment: () => currentExperiment,
8643
9132
  currentLogger: () => currentLogger,
@@ -8661,15 +9150,18 @@ __export(exports_exports, {
8661
9150
  initLogger: () => initLogger,
8662
9151
  invoke: () => invoke,
8663
9152
  isTemplateFormat: () => isTemplateFormat,
9153
+ loadParameters: () => loadParameters,
8664
9154
  loadPrompt: () => loadPrompt,
8665
9155
  log: () => log,
8666
9156
  logError: () => logError,
8667
9157
  login: () => login,
8668
9158
  loginToState: () => loginToState,
9159
+ logs3OverflowUploadSchema: () => logs3OverflowUploadSchema,
8669
9160
  newId: () => newId,
8670
9161
  parseCachedHeader: () => parseCachedHeader,
8671
9162
  parseTemplateFormat: () => parseTemplateFormat,
8672
9163
  permalink: () => permalink,
9164
+ pickLogs3OverflowObjectIds: () => pickLogs3OverflowObjectIds,
8673
9165
  projects: () => projects,
8674
9166
  promptContentsSchema: () => promptContentsSchema,
8675
9167
  promptDefinitionSchema: () => promptDefinitionSchema,
@@ -8690,6 +9182,8 @@ __export(exports_exports, {
8690
9182
  traceable: () => traceable,
8691
9183
  traced: () => traced,
8692
9184
  updateSpan: () => updateSpan,
9185
+ uploadLogs3OverflowPayload: () => uploadLogs3OverflowPayload,
9186
+ utf8ByteLength: () => utf8ByteLength,
8693
9187
  withCurrent: () => withCurrent,
8694
9188
  withDataset: () => withDataset,
8695
9189
  withExperiment: () => withExperiment,
@@ -8792,7 +9286,7 @@ function initFunction({
8792
9286
  state
8793
9287
  }) {
8794
9288
  const s = _nullishCoalesce(state, () => ( _internalGetGlobalState()));
8795
- _optionalChain([s, 'optionalAccess', _107 => _107.spanCache, 'optionalAccess', _108 => _108.disable, 'call', _109 => _109()]);
9289
+ _optionalChain([s, 'optionalAccess', _103 => _103.spanCache, 'optionalAccess', _104 => _104.disable, 'call', _105 => _105()]);
8796
9290
  const f = async (input) => {
8797
9291
  return await invoke({
8798
9292
  projectName,
@@ -8856,7 +9350,7 @@ function parseSpanFromResponseCreateParams(params) {
8856
9350
  }
8857
9351
  function parseEventFromResponseCreateResult(result) {
8858
9352
  const data = {};
8859
- if (_optionalChain([result, 'optionalAccess', _110 => _110.output]) !== void 0) {
9353
+ if (_optionalChain([result, 'optionalAccess', _106 => _106.output]) !== void 0) {
8860
9354
  data.output = processImagesInOutput(result.output);
8861
9355
  }
8862
9356
  if (result) {
@@ -8865,7 +9359,7 @@ function parseEventFromResponseCreateResult(result) {
8865
9359
  data.metadata = metadata;
8866
9360
  }
8867
9361
  }
8868
- data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _111 => _111.usage]));
9362
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _107 => _107.usage]));
8869
9363
  return data;
8870
9364
  }
8871
9365
  function processImagesInOutput(output) {
@@ -8919,7 +9413,7 @@ function parseSpanFromResponseParseParams(params) {
8919
9413
  }
8920
9414
  function parseEventFromResponseParseResult(result) {
8921
9415
  const data = {};
8922
- if (_optionalChain([result, 'optionalAccess', _112 => _112.output]) !== void 0) {
9416
+ if (_optionalChain([result, 'optionalAccess', _108 => _108.output]) !== void 0) {
8923
9417
  data.output = processImagesInOutput(result.output);
8924
9418
  }
8925
9419
  if (result) {
@@ -8928,7 +9422,7 @@ function parseEventFromResponseParseResult(result) {
8928
9422
  data.metadata = metadata;
8929
9423
  }
8930
9424
  }
8931
- data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _113 => _113.usage]));
9425
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _109 => _109.usage]));
8932
9426
  return data;
8933
9427
  }
8934
9428
  function traceResponseCreateStream(stream, timedSpan) {
@@ -8945,7 +9439,7 @@ function traceResponseCreateStream(stream, timedSpan) {
8945
9439
  return result;
8946
9440
  }
8947
9441
  const item = result.value;
8948
- if (!item || !_optionalChain([item, 'optionalAccess', _114 => _114.type]) || !_optionalChain([item, 'optionalAccess', _115 => _115.response])) {
9442
+ if (!item || !_optionalChain([item, 'optionalAccess', _110 => _110.type]) || !_optionalChain([item, 'optionalAccess', _111 => _111.response])) {
8949
9443
  return result;
8950
9444
  }
8951
9445
  const event = parseLogFromItem(item);
@@ -8956,14 +9450,14 @@ function traceResponseCreateStream(stream, timedSpan) {
8956
9450
  };
8957
9451
  }
8958
9452
  function parseLogFromItem(item) {
8959
- if (!item || !_optionalChain([item, 'optionalAccess', _116 => _116.type]) || !_optionalChain([item, 'optionalAccess', _117 => _117.response])) {
9453
+ if (!item || !_optionalChain([item, 'optionalAccess', _112 => _112.type]) || !_optionalChain([item, 'optionalAccess', _113 => _113.response])) {
8960
9454
  return {};
8961
9455
  }
8962
9456
  const response = item.response;
8963
9457
  switch (item.type) {
8964
9458
  case "response.completed":
8965
9459
  const data = {};
8966
- if (_optionalChain([response, 'optionalAccess', _118 => _118.output]) !== void 0) {
9460
+ if (_optionalChain([response, 'optionalAccess', _114 => _114.output]) !== void 0) {
8967
9461
  data.output = processImagesInOutput(response.output);
8968
9462
  }
8969
9463
  if (response) {
@@ -8972,7 +9466,7 @@ function parseLogFromItem(item) {
8972
9466
  data.metadata = metadata;
8973
9467
  }
8974
9468
  }
8975
- data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _119 => _119.usage]));
9469
+ data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _115 => _115.usage]));
8976
9470
  return data;
8977
9471
  default:
8978
9472
  return {};
@@ -9169,8 +9663,8 @@ function wrapOpenAIv4(openai) {
9169
9663
  const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
9170
9664
  const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
9171
9665
  let betaProxy2;
9172
- if (_optionalChain([openai, 'access', _120 => _120.beta, 'optionalAccess', _121 => _121.chat, 'optionalAccess', _122 => _122.completions, 'optionalAccess', _123 => _123.stream])) {
9173
- const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _124 => _124.beta, 'optionalAccess', _125 => _125.chat, 'access', _126 => _126.completions]), {
9666
+ if (_optionalChain([openai, 'access', _116 => _116.beta, 'optionalAccess', _117 => _117.chat, 'optionalAccess', _118 => _118.completions, 'optionalAccess', _119 => _119.stream])) {
9667
+ const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _120 => _120.beta, 'optionalAccess', _121 => _121.chat, 'access', _122 => _122.completions]), {
9174
9668
  get(target, name, receiver) {
9175
9669
  const baseVal = Reflect.get(target, name, receiver);
9176
9670
  if (name === "parse") {
@@ -9218,7 +9712,7 @@ function wrapOpenAIv4(openai) {
9218
9712
  });
9219
9713
  }
9220
9714
  function logCompletionResponse(startTime, response, span) {
9221
- const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _127 => _127.usage]));
9715
+ const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _123 => _123.usage]));
9222
9716
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
9223
9717
  span.log({
9224
9718
  output: response.choices,
@@ -9516,7 +10010,7 @@ function parseChatCompletionParams(params) {
9516
10010
  function processEmbeddingResponse(result, span) {
9517
10011
  span.log({
9518
10012
  output: { embedding_length: result.data[0].embedding.length },
9519
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _128 => _128.usage]))
10013
+ metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _124 => _124.usage]))
9520
10014
  });
9521
10015
  }
9522
10016
  function processModerationResponse(result, span) {
@@ -9546,10 +10040,10 @@ function postprocessStreamingResults(allResults) {
9546
10040
  if (result.usage) {
9547
10041
  metrics = {
9548
10042
  ...metrics,
9549
- ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _129 => _129.usage]))
10043
+ ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _125 => _125.usage]))
9550
10044
  };
9551
10045
  }
9552
- const delta = _optionalChain([result, 'access', _130 => _130.choices, 'optionalAccess', _131 => _131[0], 'optionalAccess', _132 => _132.delta]);
10046
+ const delta = _optionalChain([result, 'access', _126 => _126.choices, 'optionalAccess', _127 => _127[0], 'optionalAccess', _128 => _128.delta]);
9553
10047
  if (!delta) {
9554
10048
  continue;
9555
10049
  }
@@ -9793,7 +10287,7 @@ function isModuleNamespace(obj) {
9793
10287
  if (!obj || typeof obj !== "object") {
9794
10288
  return false;
9795
10289
  }
9796
- if (_optionalChain([obj, 'access', _133 => _133.constructor, 'optionalAccess', _134 => _134.name]) === "Module") {
10290
+ if (_optionalChain([obj, 'access', _129 => _129.constructor, 'optionalAccess', _130 => _130.name]) === "Module") {
9797
10291
  return true;
9798
10292
  }
9799
10293
  try {
@@ -9892,10 +10386,10 @@ var makeGenerateTextWrapper = (name, options, generateText, aiSDK) => {
9892
10386
  });
9893
10387
  const gatewayInfo = extractGatewayRoutingInfo(result);
9894
10388
  const resolvedMetadata = {};
9895
- if (_optionalChain([gatewayInfo, 'optionalAccess', _135 => _135.provider])) {
10389
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _131 => _131.provider])) {
9896
10390
  resolvedMetadata.provider = gatewayInfo.provider;
9897
10391
  }
9898
- if (_optionalChain([gatewayInfo, 'optionalAccess', _136 => _136.model])) {
10392
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _132 => _132.model])) {
9899
10393
  resolvedMetadata.model = gatewayInfo.model;
9900
10394
  }
9901
10395
  span.log({
@@ -9933,7 +10427,7 @@ var resolveModel = (model, ai) => {
9933
10427
  if (typeof model !== "string") {
9934
10428
  return model;
9935
10429
  }
9936
- const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([ai, 'optionalAccess', _137 => _137.gateway]))), () => ( null));
10430
+ const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([ai, 'optionalAccess', _133 => _133.gateway]))), () => ( null));
9937
10431
  if (provider && typeof provider.languageModel === "function") {
9938
10432
  return provider.languageModel(model);
9939
10433
  }
@@ -9948,7 +10442,7 @@ var wrapModel = (model, ai) => {
9948
10442
  return resolvedModel;
9949
10443
  }
9950
10444
  const originalDoGenerate = resolvedModel.doGenerate.bind(resolvedModel);
9951
- const originalDoStream = _optionalChain([resolvedModel, 'access', _138 => _138.doStream, 'optionalAccess', _139 => _139.bind, 'call', _140 => _140(resolvedModel)]);
10445
+ const originalDoStream = _optionalChain([resolvedModel, 'access', _134 => _134.doStream, 'optionalAccess', _135 => _135.bind, 'call', _136 => _136(resolvedModel)]);
9952
10446
  const { model: modelId, provider } = serializeModelWithProvider(resolvedModel);
9953
10447
  const wrappedDoGenerate = async (options) => {
9954
10448
  const processedInput = await processInputAttachments2(options);
@@ -9957,10 +10451,10 @@ var wrapModel = (model, ai) => {
9957
10451
  const result = await originalDoGenerate(options);
9958
10452
  const gatewayInfo = extractGatewayRoutingInfo(result);
9959
10453
  const resolvedMetadata = {};
9960
- if (_optionalChain([gatewayInfo, 'optionalAccess', _141 => _141.provider])) {
10454
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _137 => _137.provider])) {
9961
10455
  resolvedMetadata.provider = gatewayInfo.provider;
9962
10456
  }
9963
- if (_optionalChain([gatewayInfo, 'optionalAccess', _142 => _142.model])) {
10457
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _138 => _138.model])) {
9964
10458
  resolvedMetadata.model = gatewayInfo.model;
9965
10459
  }
9966
10460
  if (result.finishReason !== void 0) {
@@ -10056,9 +10550,9 @@ var wrapModel = (model, ai) => {
10056
10550
  case "raw":
10057
10551
  if (chunk.rawValue) {
10058
10552
  const rawVal = chunk.rawValue;
10059
- if (_optionalChain([rawVal, 'access', _143 => _143.delta, 'optionalAccess', _144 => _144.content])) {
10553
+ if (_optionalChain([rawVal, 'access', _139 => _139.delta, 'optionalAccess', _140 => _140.content])) {
10060
10554
  text += rawVal.delta.content;
10061
- } else if (_optionalChain([rawVal, 'access', _145 => _145.choices, 'optionalAccess', _146 => _146[0], 'optionalAccess', _147 => _147.delta, 'optionalAccess', _148 => _148.content])) {
10555
+ } else if (_optionalChain([rawVal, 'access', _141 => _141.choices, 'optionalAccess', _142 => _142[0], 'optionalAccess', _143 => _143.delta, 'optionalAccess', _144 => _144.content])) {
10062
10556
  text += rawVal.choices[0].delta.content;
10063
10557
  } else if (typeof rawVal.text === "string") {
10064
10558
  text += rawVal.text;
@@ -10078,10 +10572,10 @@ var wrapModel = (model, ai) => {
10078
10572
  }
10079
10573
  const gatewayInfo = extractGatewayRoutingInfo(output);
10080
10574
  const resolvedMetadata = {};
10081
- if (_optionalChain([gatewayInfo, 'optionalAccess', _149 => _149.provider])) {
10575
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _145 => _145.provider])) {
10082
10576
  resolvedMetadata.provider = gatewayInfo.provider;
10083
10577
  }
10084
- if (_optionalChain([gatewayInfo, 'optionalAccess', _150 => _150.model])) {
10578
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _146 => _146.model])) {
10085
10579
  resolvedMetadata.model = gatewayInfo.model;
10086
10580
  }
10087
10581
  if (chunk.finishReason !== void 0) {
@@ -10141,10 +10635,10 @@ var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
10141
10635
  const output = await processOutput(result, options.denyOutputPaths);
10142
10636
  const gatewayInfo = extractGatewayRoutingInfo(result);
10143
10637
  const resolvedMetadata = {};
10144
- if (_optionalChain([gatewayInfo, 'optionalAccess', _151 => _151.provider])) {
10638
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _147 => _147.provider])) {
10145
10639
  resolvedMetadata.provider = gatewayInfo.provider;
10146
10640
  }
10147
- if (_optionalChain([gatewayInfo, 'optionalAccess', _152 => _152.model])) {
10641
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _148 => _148.model])) {
10148
10642
  resolvedMetadata.model = gatewayInfo.model;
10149
10643
  }
10150
10644
  span.log({
@@ -10229,16 +10723,16 @@ var makeStreamTextWrapper = (name, options, streamText, aiSDK) => {
10229
10723
  }
10230
10724
  });
10231
10725
  }
10232
- _optionalChain([params, 'access', _153 => _153.onChunk, 'optionalCall', _154 => _154(chunk)]);
10726
+ _optionalChain([params, 'access', _149 => _149.onChunk, 'optionalCall', _150 => _150(chunk)]);
10233
10727
  },
10234
10728
  onFinish: async (event) => {
10235
- _optionalChain([params, 'access', _155 => _155.onFinish, 'optionalCall', _156 => _156(event)]);
10729
+ _optionalChain([params, 'access', _151 => _151.onFinish, 'optionalCall', _152 => _152(event)]);
10236
10730
  const gatewayInfo = extractGatewayRoutingInfo(event);
10237
10731
  const resolvedMetadata = {};
10238
- if (_optionalChain([gatewayInfo, 'optionalAccess', _157 => _157.provider])) {
10732
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _153 => _153.provider])) {
10239
10733
  resolvedMetadata.provider = gatewayInfo.provider;
10240
10734
  }
10241
- if (_optionalChain([gatewayInfo, 'optionalAccess', _158 => _158.model])) {
10735
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _154 => _154.model])) {
10242
10736
  resolvedMetadata.model = gatewayInfo.model;
10243
10737
  }
10244
10738
  span.log({
@@ -10249,7 +10743,7 @@ var makeStreamTextWrapper = (name, options, streamText, aiSDK) => {
10249
10743
  span.end();
10250
10744
  },
10251
10745
  onError: async (err) => {
10252
- _optionalChain([params, 'access', _159 => _159.onError, 'optionalCall', _160 => _160(err)]);
10746
+ _optionalChain([params, 'access', _155 => _155.onError, 'optionalCall', _156 => _156(err)]);
10253
10747
  span.log({
10254
10748
  error: serializeError(err)
10255
10749
  });
@@ -10353,16 +10847,16 @@ var wrapStreamObject = (streamObject, options = {}, aiSDK) => {
10353
10847
  }
10354
10848
  });
10355
10849
  }
10356
- _optionalChain([params, 'access', _161 => _161.onChunk, 'optionalCall', _162 => _162(chunk)]);
10850
+ _optionalChain([params, 'access', _157 => _157.onChunk, 'optionalCall', _158 => _158(chunk)]);
10357
10851
  },
10358
10852
  onFinish: async (event) => {
10359
- _optionalChain([params, 'access', _163 => _163.onFinish, 'optionalCall', _164 => _164(event)]);
10853
+ _optionalChain([params, 'access', _159 => _159.onFinish, 'optionalCall', _160 => _160(event)]);
10360
10854
  const gatewayInfo = extractGatewayRoutingInfo(event);
10361
10855
  const resolvedMetadata = {};
10362
- if (_optionalChain([gatewayInfo, 'optionalAccess', _165 => _165.provider])) {
10856
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _161 => _161.provider])) {
10363
10857
  resolvedMetadata.provider = gatewayInfo.provider;
10364
10858
  }
10365
- if (_optionalChain([gatewayInfo, 'optionalAccess', _166 => _166.model])) {
10859
+ if (_optionalChain([gatewayInfo, 'optionalAccess', _162 => _162.model])) {
10366
10860
  resolvedMetadata.model = gatewayInfo.model;
10367
10861
  }
10368
10862
  span.log({
@@ -10373,7 +10867,7 @@ var wrapStreamObject = (streamObject, options = {}, aiSDK) => {
10373
10867
  span.end();
10374
10868
  },
10375
10869
  onError: async (err) => {
10376
- _optionalChain([params, 'access', _167 => _167.onError, 'optionalCall', _168 => _168(err)]);
10870
+ _optionalChain([params, 'access', _163 => _163.onError, 'optionalCall', _164 => _164(err)]);
10377
10871
  span.log({
10378
10872
  error: serializeError(err)
10379
10873
  });
@@ -10554,8 +11048,8 @@ function parseGatewayModelString(modelString) {
10554
11048
  return { model: modelString };
10555
11049
  }
10556
11050
  function serializeModelWithProvider(model) {
10557
- const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _169 => _169.modelId]);
10558
- const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _170 => _170.provider]) : void 0;
11051
+ const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _165 => _165.modelId]);
11052
+ const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _166 => _166.provider]) : void 0;
10559
11053
  if (!modelId) {
10560
11054
  return { model: modelId, provider: explicitProvider };
10561
11055
  }
@@ -10566,8 +11060,8 @@ function serializeModelWithProvider(model) {
10566
11060
  };
10567
11061
  }
10568
11062
  function extractGatewayRoutingInfo(result) {
10569
- if (_optionalChain([result, 'optionalAccess', _171 => _171.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
10570
- const routing2 = _optionalChain([result, 'access', _172 => _172.steps, 'access', _173 => _173[0], 'optionalAccess', _174 => _174.providerMetadata, 'optionalAccess', _175 => _175.gateway, 'optionalAccess', _176 => _176.routing]);
11063
+ if (_optionalChain([result, 'optionalAccess', _167 => _167.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
11064
+ const routing2 = _optionalChain([result, 'access', _168 => _168.steps, 'access', _169 => _169[0], 'optionalAccess', _170 => _170.providerMetadata, 'optionalAccess', _171 => _171.gateway, 'optionalAccess', _172 => _172.routing]);
10571
11065
  if (routing2) {
10572
11066
  return {
10573
11067
  provider: routing2.resolvedProvider || routing2.finalProvider,
@@ -10575,7 +11069,7 @@ function extractGatewayRoutingInfo(result) {
10575
11069
  };
10576
11070
  }
10577
11071
  }
10578
- const routing = _optionalChain([result, 'optionalAccess', _177 => _177.providerMetadata, 'optionalAccess', _178 => _178.gateway, 'optionalAccess', _179 => _179.routing]);
11072
+ const routing = _optionalChain([result, 'optionalAccess', _173 => _173.providerMetadata, 'optionalAccess', _174 => _174.gateway, 'optionalAccess', _175 => _175.routing]);
10579
11073
  if (routing) {
10580
11074
  return {
10581
11075
  provider: routing.resolvedProvider || routing.finalProvider,
@@ -10847,7 +11341,7 @@ var convertImageToAttachment = (image, explicitMimeType) => {
10847
11341
  try {
10848
11342
  if (typeof image === "string" && image.startsWith("data:")) {
10849
11343
  const [mimeTypeSection, base64Data] = image.split(",");
10850
- const mimeType = _optionalChain([mimeTypeSection, 'access', _180 => _180.match, 'call', _181 => _181(/data:(.*?);/), 'optionalAccess', _182 => _182[1]]);
11344
+ const mimeType = _optionalChain([mimeTypeSection, 'access', _176 => _176.match, 'call', _177 => _177(/data:(.*?);/), 'optionalAccess', _178 => _178[1]]);
10851
11345
  if (mimeType && base64Data) {
10852
11346
  const blob = convertDataToBlob(base64Data, mimeType);
10853
11347
  if (blob) {
@@ -11014,7 +11508,7 @@ function firstNumber(...values) {
11014
11508
  }
11015
11509
  function extractTokenMetrics(result) {
11016
11510
  const metrics = {};
11017
- let usage = _optionalChain([result, 'optionalAccess', _183 => _183.totalUsage]) || _optionalChain([result, 'optionalAccess', _184 => _184.usage]);
11511
+ let usage = _optionalChain([result, 'optionalAccess', _179 => _179.totalUsage]) || _optionalChain([result, 'optionalAccess', _180 => _180.usage]);
11018
11512
  if (!usage && result) {
11019
11513
  try {
11020
11514
  if ("totalUsage" in result && typeof result.totalUsage !== "function") {
@@ -11029,7 +11523,7 @@ function extractTokenMetrics(result) {
11029
11523
  return metrics;
11030
11524
  }
11031
11525
  const promptTokens = firstNumber(
11032
- _optionalChain([usage, 'access', _185 => _185.inputTokens, 'optionalAccess', _186 => _186.total]),
11526
+ _optionalChain([usage, 'access', _181 => _181.inputTokens, 'optionalAccess', _182 => _182.total]),
11033
11527
  usage.inputTokens,
11034
11528
  usage.promptTokens,
11035
11529
  usage.prompt_tokens
@@ -11038,7 +11532,7 @@ function extractTokenMetrics(result) {
11038
11532
  metrics.prompt_tokens = promptTokens;
11039
11533
  }
11040
11534
  const completionTokens = firstNumber(
11041
- _optionalChain([usage, 'access', _187 => _187.outputTokens, 'optionalAccess', _188 => _188.total]),
11535
+ _optionalChain([usage, 'access', _183 => _183.outputTokens, 'optionalAccess', _184 => _184.total]),
11042
11536
  usage.outputTokens,
11043
11537
  usage.completionTokens,
11044
11538
  usage.completion_tokens
@@ -11055,7 +11549,7 @@ function extractTokenMetrics(result) {
11055
11549
  metrics.tokens = totalTokens;
11056
11550
  }
11057
11551
  const promptCachedTokens = firstNumber(
11058
- _optionalChain([usage, 'access', _189 => _189.inputTokens, 'optionalAccess', _190 => _190.cacheRead]),
11552
+ _optionalChain([usage, 'access', _185 => _185.inputTokens, 'optionalAccess', _186 => _186.cacheRead]),
11059
11553
  usage.cachedInputTokens,
11060
11554
  usage.promptCachedTokens,
11061
11555
  usage.prompt_cached_tokens
@@ -11085,7 +11579,7 @@ function extractTokenMetrics(result) {
11085
11579
  metrics.completion_cached_tokens = completionCachedTokens;
11086
11580
  }
11087
11581
  const reasoningTokenCount = firstNumber(
11088
- _optionalChain([usage, 'access', _191 => _191.outputTokens, 'optionalAccess', _192 => _192.reasoning]),
11582
+ _optionalChain([usage, 'access', _187 => _187.outputTokens, 'optionalAccess', _188 => _188.reasoning]),
11089
11583
  usage.reasoningTokens,
11090
11584
  usage.completionReasoningTokens,
11091
11585
  usage.completion_reasoning_tokens,
@@ -11111,12 +11605,12 @@ function extractTokenMetrics(result) {
11111
11605
  return metrics;
11112
11606
  }
11113
11607
  function extractCostFromResult(result) {
11114
- if (_optionalChain([result, 'optionalAccess', _193 => _193.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
11608
+ if (_optionalChain([result, 'optionalAccess', _189 => _189.steps]) && Array.isArray(result.steps) && result.steps.length > 0) {
11115
11609
  let totalCost = 0;
11116
11610
  let foundCost = false;
11117
11611
  for (const step of result.steps) {
11118
- const gateway2 = _optionalChain([step, 'optionalAccess', _194 => _194.providerMetadata, 'optionalAccess', _195 => _195.gateway]);
11119
- const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _196 => _196.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _197 => _197.marketCost]));
11612
+ const gateway2 = _optionalChain([step, 'optionalAccess', _190 => _190.providerMetadata, 'optionalAccess', _191 => _191.gateway]);
11613
+ const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _192 => _192.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _193 => _193.marketCost]));
11120
11614
  if (stepCost !== void 0 && stepCost > 0) {
11121
11615
  totalCost += stepCost;
11122
11616
  foundCost = true;
@@ -11126,8 +11620,8 @@ function extractCostFromResult(result) {
11126
11620
  return totalCost;
11127
11621
  }
11128
11622
  }
11129
- const gateway = _optionalChain([result, 'optionalAccess', _198 => _198.providerMetadata, 'optionalAccess', _199 => _199.gateway]);
11130
- const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _200 => _200.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _201 => _201.marketCost]));
11623
+ const gateway = _optionalChain([result, 'optionalAccess', _194 => _194.providerMetadata, 'optionalAccess', _195 => _195.gateway]);
11624
+ const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _196 => _196.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _197 => _197.marketCost]));
11131
11625
  if (directCost !== void 0 && directCost > 0) {
11132
11626
  return directCost;
11133
11627
  }
@@ -11221,7 +11715,7 @@ var omit = (obj, paths) => {
11221
11715
  // src/wrappers/ai-sdk/deprecated/wrapAISDKModel.ts
11222
11716
  function wrapAISDKModel(model) {
11223
11717
  const m = model;
11224
- if (_optionalChain([m, 'optionalAccess', _202 => _202.specificationVersion]) === "v1" && typeof _optionalChain([m, 'optionalAccess', _203 => _203.provider]) === "string" && typeof _optionalChain([m, 'optionalAccess', _204 => _204.modelId]) === "string") {
11718
+ if (_optionalChain([m, 'optionalAccess', _198 => _198.specificationVersion]) === "v1" && typeof _optionalChain([m, 'optionalAccess', _199 => _199.provider]) === "string" && typeof _optionalChain([m, 'optionalAccess', _200 => _200.modelId]) === "string") {
11225
11719
  return new BraintrustLanguageModelWrapper(m);
11226
11720
  } else {
11227
11721
  console.warn("Unsupported AI SDK model. Not wrapping.");
@@ -11278,10 +11772,10 @@ var BraintrustLanguageModelWrapper = class {
11278
11772
  metrics: {
11279
11773
  time_to_first_token: getCurrentUnixTimestamp() - startTime,
11280
11774
  tokens: !isEmpty2(ret.usage) ? ret.usage.promptTokens + ret.usage.completionTokens : void 0,
11281
- prompt_tokens: _optionalChain([ret, 'access', _205 => _205.usage, 'optionalAccess', _206 => _206.promptTokens]),
11282
- completion_tokens: _optionalChain([ret, 'access', _207 => _207.usage, 'optionalAccess', _208 => _208.completionTokens]),
11775
+ prompt_tokens: _optionalChain([ret, 'access', _201 => _201.usage, 'optionalAccess', _202 => _202.promptTokens]),
11776
+ completion_tokens: _optionalChain([ret, 'access', _203 => _203.usage, 'optionalAccess', _204 => _204.completionTokens]),
11283
11777
  cached: parseCachedHeader(
11284
- _nullishCoalesce(_optionalChain([ret, 'access', _209 => _209.rawResponse, 'optionalAccess', _210 => _210.headers, 'optionalAccess', _211 => _211[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _212 => _212.rawResponse, 'optionalAccess', _213 => _213.headers, 'optionalAccess', _214 => _214[LEGACY_CACHED_HEADER]])))
11778
+ _nullishCoalesce(_optionalChain([ret, 'access', _205 => _205.rawResponse, 'optionalAccess', _206 => _206.headers, 'optionalAccess', _207 => _207[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _208 => _208.rawResponse, 'optionalAccess', _209 => _209.headers, 'optionalAccess', _210 => _210[LEGACY_CACHED_HEADER]])))
11285
11779
  )
11286
11780
  }
11287
11781
  });
@@ -11373,10 +11867,10 @@ var BraintrustLanguageModelWrapper = class {
11373
11867
  metrics: {
11374
11868
  time_to_first_token,
11375
11869
  tokens: !isEmpty2(usage) ? usage.promptTokens + usage.completionTokens : void 0,
11376
- prompt_tokens: _optionalChain([usage, 'optionalAccess', _215 => _215.promptTokens]),
11377
- completion_tokens: _optionalChain([usage, 'optionalAccess', _216 => _216.completionTokens]),
11870
+ prompt_tokens: _optionalChain([usage, 'optionalAccess', _211 => _211.promptTokens]),
11871
+ completion_tokens: _optionalChain([usage, 'optionalAccess', _212 => _212.completionTokens]),
11378
11872
  cached: parseCachedHeader(
11379
- _nullishCoalesce(_optionalChain([ret, 'access', _217 => _217.rawResponse, 'optionalAccess', _218 => _218.headers, 'optionalAccess', _219 => _219[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _220 => _220.rawResponse, 'optionalAccess', _221 => _221.headers, 'optionalAccess', _222 => _222[LEGACY_CACHED_HEADER]])))
11873
+ _nullishCoalesce(_optionalChain([ret, 'access', _213 => _213.rawResponse, 'optionalAccess', _214 => _214.headers, 'optionalAccess', _215 => _215[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _216 => _216.rawResponse, 'optionalAccess', _217 => _217.headers, 'optionalAccess', _218 => _218[LEGACY_CACHED_HEADER]])))
11380
11874
  )
11381
11875
  }
11382
11876
  });
@@ -11420,7 +11914,7 @@ function postProcessPrompt(prompt) {
11420
11914
  return [
11421
11915
  {
11422
11916
  role: "assistant",
11423
- content: _optionalChain([textPart, 'optionalAccess', _223 => _223.text]),
11917
+ content: _optionalChain([textPart, 'optionalAccess', _219 => _219.text]),
11424
11918
  ...toolCallParts.length > 0 ? {
11425
11919
  tool_calls: toolCallParts.map((part) => ({
11426
11920
  id: part.toolCallId,
@@ -11513,23 +12007,23 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
11513
12007
 
11514
12008
  // src/wrappers/ai-sdk/deprecated/BraintrustMiddleware.ts
11515
12009
  function detectProviderFromResult(result) {
11516
- if (!_optionalChain([result, 'optionalAccess', _224 => _224.providerMetadata])) {
12010
+ if (!_optionalChain([result, 'optionalAccess', _220 => _220.providerMetadata])) {
11517
12011
  return void 0;
11518
12012
  }
11519
12013
  const keys = Object.keys(result.providerMetadata);
11520
- return _optionalChain([keys, 'optionalAccess', _225 => _225.at, 'call', _226 => _226(0)]);
12014
+ return _optionalChain([keys, 'optionalAccess', _221 => _221.at, 'call', _222 => _222(0)]);
11521
12015
  }
11522
12016
  function extractModelFromResult(result) {
11523
- if (_optionalChain([result, 'optionalAccess', _227 => _227.response, 'optionalAccess', _228 => _228.modelId])) {
12017
+ if (_optionalChain([result, 'optionalAccess', _223 => _223.response, 'optionalAccess', _224 => _224.modelId])) {
11524
12018
  return result.response.modelId;
11525
12019
  }
11526
- if (_optionalChain([result, 'optionalAccess', _229 => _229.request, 'optionalAccess', _230 => _230.body, 'optionalAccess', _231 => _231.model])) {
12020
+ if (_optionalChain([result, 'optionalAccess', _225 => _225.request, 'optionalAccess', _226 => _226.body, 'optionalAccess', _227 => _227.model])) {
11527
12021
  return result.request.body.model;
11528
12022
  }
11529
12023
  return void 0;
11530
12024
  }
11531
12025
  function extractModelFromWrapGenerateCallback(model) {
11532
- return _optionalChain([model, 'optionalAccess', _232 => _232.modelId]);
12026
+ return _optionalChain([model, 'optionalAccess', _228 => _228.modelId]);
11533
12027
  }
11534
12028
  function camelToSnake(str) {
11535
12029
  return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
@@ -11574,7 +12068,7 @@ function normalizeUsageMetrics(usage, provider, providerMetadata) {
11574
12068
  metrics.prompt_cached_tokens = cachedInputTokens;
11575
12069
  }
11576
12070
  if (provider === "anthropic") {
11577
- const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _233 => _233.anthropic]);
12071
+ const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _229 => _229.anthropic]);
11578
12072
  if (anthropicMetadata) {
11579
12073
  const cacheReadTokens = getNumberProperty(anthropicMetadata.usage, "cache_read_input_tokens") || 0;
11580
12074
  const cacheCreationTokens = getNumberProperty(
@@ -11600,7 +12094,7 @@ function buildAssistantOutputWithToolCalls(result, toolCalls) {
11600
12094
  {
11601
12095
  index: 0,
11602
12096
  logprobs: null,
11603
- finish_reason: _nullishCoalesce(normalizeFinishReason(_optionalChain([result, 'optionalAccess', _234 => _234.finishReason])), () => ( (toolCalls.length ? "tool_calls" : void 0))),
12097
+ finish_reason: _nullishCoalesce(normalizeFinishReason(_optionalChain([result, 'optionalAccess', _230 => _230.finishReason])), () => ( (toolCalls.length ? "tool_calls" : void 0))),
11604
12098
  message: {
11605
12099
  role: "assistant",
11606
12100
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0
@@ -11613,7 +12107,7 @@ function extractToolCallsFromSteps(steps) {
11613
12107
  if (!Array.isArray(steps)) return toolCalls;
11614
12108
  let idx = 0;
11615
12109
  for (const step of steps) {
11616
- const blocks = _optionalChain([step, 'optionalAccess', _235 => _235.content]);
12110
+ const blocks = _optionalChain([step, 'optionalAccess', _231 => _231.content]);
11617
12111
  if (!Array.isArray(blocks)) continue;
11618
12112
  for (const block of blocks) {
11619
12113
  if (block && typeof block === "object" && block.type === "tool-call") {
@@ -11636,7 +12130,7 @@ function extractToolCallsFromBlocks(blocks) {
11636
12130
  return extractToolCallsFromSteps([{ content: blocks }]);
11637
12131
  }
11638
12132
  function extractInput(params) {
11639
- return _nullishCoalesce(_nullishCoalesce(_optionalChain([params, 'optionalAccess', _236 => _236.prompt]), () => ( _optionalChain([params, 'optionalAccess', _237 => _237.messages]))), () => ( _optionalChain([params, 'optionalAccess', _238 => _238.system])));
12133
+ return _nullishCoalesce(_nullishCoalesce(_optionalChain([params, 'optionalAccess', _232 => _232.prompt]), () => ( _optionalChain([params, 'optionalAccess', _233 => _233.messages]))), () => ( _optionalChain([params, 'optionalAccess', _234 => _234.system])));
11640
12134
  }
11641
12135
  var V2_EXCLUDE_KEYS = /* @__PURE__ */ new Set([
11642
12136
  "prompt",
@@ -11660,16 +12154,16 @@ function BraintrustMiddleware(config = {}) {
11660
12154
  const rawInput = extractInput(params);
11661
12155
  const processedInput = processInputAttachments(rawInput);
11662
12156
  const spanArgs = {
11663
- name: _optionalChain([config, 'access', _239 => _239.spanInfo, 'optionalAccess', _240 => _240.name]) || "ai-sdk.doGenerate",
12157
+ name: _optionalChain([config, 'access', _235 => _235.spanInfo, 'optionalAccess', _236 => _236.name]) || "ai-sdk.doGenerate",
11664
12158
  spanAttributes: {
11665
12159
  type: "llm" /* LLM */,
11666
- ..._optionalChain([config, 'access', _241 => _241.spanInfo, 'optionalAccess', _242 => _242.spanAttributes]) || {}
12160
+ ..._optionalChain([config, 'access', _237 => _237.spanInfo, 'optionalAccess', _238 => _238.spanAttributes]) || {}
11667
12161
  },
11668
12162
  event: {
11669
12163
  input: processedInput,
11670
12164
  metadata: {
11671
12165
  ...extractModelParameters(params, V2_EXCLUDE_KEYS),
11672
- ..._optionalChain([config, 'access', _243 => _243.spanInfo, 'optionalAccess', _244 => _244.metadata]) || {}
12166
+ ..._optionalChain([config, 'access', _239 => _239.spanInfo, 'optionalAccess', _240 => _240.metadata]) || {}
11673
12167
  }
11674
12168
  }
11675
12169
  };
@@ -11695,12 +12189,12 @@ function BraintrustMiddleware(config = {}) {
11695
12189
  metadata.model = modelId;
11696
12190
  }
11697
12191
  }
11698
- let toolCalls = extractToolCallsFromSteps(_optionalChain([result, 'optionalAccess', _245 => _245.steps]));
12192
+ let toolCalls = extractToolCallsFromSteps(_optionalChain([result, 'optionalAccess', _241 => _241.steps]));
11699
12193
  if (!toolCalls || toolCalls.length === 0) {
11700
- toolCalls = extractToolCallsFromBlocks(_optionalChain([result, 'optionalAccess', _246 => _246.content]));
12194
+ toolCalls = extractToolCallsFromBlocks(_optionalChain([result, 'optionalAccess', _242 => _242.content]));
11701
12195
  }
11702
12196
  span.log({
11703
- output: toolCalls.length > 0 ? buildAssistantOutputWithToolCalls(result, toolCalls) : _optionalChain([result, 'optionalAccess', _247 => _247.content]),
12197
+ output: toolCalls.length > 0 ? buildAssistantOutputWithToolCalls(result, toolCalls) : _optionalChain([result, 'optionalAccess', _243 => _243.content]),
11704
12198
  metadata,
11705
12199
  metrics: normalizeUsageMetrics(
11706
12200
  result.usage,
@@ -11722,16 +12216,16 @@ function BraintrustMiddleware(config = {}) {
11722
12216
  const rawInput = extractInput(params);
11723
12217
  const processedInput = processInputAttachments(rawInput);
11724
12218
  const spanArgs = {
11725
- name: _optionalChain([config, 'access', _248 => _248.spanInfo, 'optionalAccess', _249 => _249.name]) || "ai-sdk.doStream",
12219
+ name: _optionalChain([config, 'access', _244 => _244.spanInfo, 'optionalAccess', _245 => _245.name]) || "ai-sdk.doStream",
11726
12220
  spanAttributes: {
11727
12221
  type: "llm" /* LLM */,
11728
- ..._optionalChain([config, 'access', _250 => _250.spanInfo, 'optionalAccess', _251 => _251.spanAttributes]) || {}
12222
+ ..._optionalChain([config, 'access', _246 => _246.spanInfo, 'optionalAccess', _247 => _247.spanAttributes]) || {}
11729
12223
  },
11730
12224
  event: {
11731
12225
  input: processedInput,
11732
12226
  metadata: {
11733
12227
  ...extractModelParameters(params, V2_EXCLUDE_KEYS),
11734
- ..._optionalChain([config, 'access', _252 => _252.spanInfo, 'optionalAccess', _253 => _253.metadata]) || {}
12228
+ ..._optionalChain([config, 'access', _248 => _248.spanInfo, 'optionalAccess', _249 => _249.metadata]) || {}
11735
12229
  }
11736
12230
  }
11737
12231
  };
@@ -12002,9 +12496,9 @@ function streamNextProxy(stream, sspan) {
12002
12496
  }
12003
12497
  const item = result.value;
12004
12498
  const blockIndex = item.index;
12005
- switch (_optionalChain([item, 'optionalAccess', _254 => _254.type])) {
12499
+ switch (_optionalChain([item, 'optionalAccess', _250 => _250.type])) {
12006
12500
  case "message_start":
12007
- const msg = _optionalChain([item, 'optionalAccess', _255 => _255.message]);
12501
+ const msg = _optionalChain([item, 'optionalAccess', _251 => _251.message]);
12008
12502
  if (msg) {
12009
12503
  const event = parseEventFromMessage(msg);
12010
12504
  totals = { ...totals, ...event.metrics };
@@ -12021,23 +12515,23 @@ function streamNextProxy(stream, sspan) {
12021
12515
  if (!contentBlockDeltas[blockIndex]) {
12022
12516
  contentBlockDeltas[blockIndex] = [];
12023
12517
  }
12024
- if (_optionalChain([item, 'optionalAccess', _256 => _256.delta, 'optionalAccess', _257 => _257.type]) === "text_delta") {
12025
- const text2 = _optionalChain([item, 'optionalAccess', _258 => _258.delta, 'optionalAccess', _259 => _259.text]);
12518
+ if (_optionalChain([item, 'optionalAccess', _252 => _252.delta, 'optionalAccess', _253 => _253.type]) === "text_delta") {
12519
+ const text2 = _optionalChain([item, 'optionalAccess', _254 => _254.delta, 'optionalAccess', _255 => _255.text]);
12026
12520
  if (text2) {
12027
12521
  contentBlockDeltas[blockIndex].push(text2);
12028
12522
  }
12029
- } else if (_optionalChain([item, 'optionalAccess', _260 => _260.delta, 'optionalAccess', _261 => _261.type]) === "input_json_delta") {
12030
- const partialJson = _optionalChain([item, 'optionalAccess', _262 => _262.delta, 'optionalAccess', _263 => _263.partial_json]);
12523
+ } else if (_optionalChain([item, 'optionalAccess', _256 => _256.delta, 'optionalAccess', _257 => _257.type]) === "input_json_delta") {
12524
+ const partialJson = _optionalChain([item, 'optionalAccess', _258 => _258.delta, 'optionalAccess', _259 => _259.partial_json]);
12031
12525
  if (partialJson) {
12032
12526
  contentBlockDeltas[blockIndex].push(partialJson);
12033
12527
  }
12034
12528
  }
12035
12529
  break;
12036
12530
  case "content_block_stop":
12037
- const text = _optionalChain([contentBlockDeltas, 'access', _264 => _264[blockIndex], 'optionalAccess', _265 => _265.join, 'call', _266 => _266("")]);
12531
+ const text = _optionalChain([contentBlockDeltas, 'access', _260 => _260[blockIndex], 'optionalAccess', _261 => _261.join, 'call', _262 => _262("")]);
12038
12532
  if (!text) break;
12039
12533
  const block = contentBlocks[blockIndex];
12040
- if (_optionalChain([block, 'optionalAccess', _267 => _267.type]) === "tool_use") {
12534
+ if (_optionalChain([block, 'optionalAccess', _263 => _263.type]) === "tool_use") {
12041
12535
  try {
12042
12536
  span.log({
12043
12537
  output: {
@@ -12053,12 +12547,12 @@ function streamNextProxy(stream, sspan) {
12053
12547
  }
12054
12548
  break;
12055
12549
  case "message_delta":
12056
- const usage = _optionalChain([item, 'optionalAccess', _268 => _268.usage]);
12550
+ const usage = _optionalChain([item, 'optionalAccess', _264 => _264.usage]);
12057
12551
  if (usage) {
12058
12552
  const metrics = parseMetricsFromUsage2(usage);
12059
12553
  totals = { ...totals, ...metrics };
12060
12554
  }
12061
- const delta = _optionalChain([item, 'optionalAccess', _269 => _269.delta]);
12555
+ const delta = _optionalChain([item, 'optionalAccess', _265 => _265.delta]);
12062
12556
  if (delta) {
12063
12557
  metadata = { ...metadata, ...delta };
12064
12558
  }
@@ -12071,7 +12565,7 @@ function streamNextProxy(stream, sspan) {
12071
12565
  }
12072
12566
  function parseEventFromMessage(message) {
12073
12567
  const output = message ? { role: message.role, content: message.content } : null;
12074
- const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _270 => _270.usage]));
12568
+ const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _266 => _266.usage]));
12075
12569
  const metas = ["stop_reason", "stop_sequence"];
12076
12570
  const metadata = {};
12077
12571
  for (const m of metas) {
@@ -12160,6 +12654,189 @@ function wrapMastraAgent(agent, _options) {
12160
12654
  }
12161
12655
 
12162
12656
  // src/wrappers/claude-agent-sdk/claude-agent-sdk.ts
12657
+ function getMcpServerMetadata(serverName, mcpServers) {
12658
+ if (!serverName || !mcpServers) {
12659
+ return {};
12660
+ }
12661
+ const serverConfig = mcpServers[serverName];
12662
+ if (!serverConfig) {
12663
+ return {};
12664
+ }
12665
+ const metadata = {};
12666
+ if (serverConfig.type) {
12667
+ metadata["mcp.type"] = serverConfig.type;
12668
+ } else if (typeof serverConfig === "object" && "transport" in serverConfig) {
12669
+ metadata["mcp.type"] = "sdk";
12670
+ }
12671
+ if (serverConfig.url) {
12672
+ metadata["mcp.url"] = serverConfig.url;
12673
+ }
12674
+ if (serverConfig.command) {
12675
+ metadata["mcp.command"] = serverConfig.command;
12676
+ if (serverConfig.args) {
12677
+ metadata["mcp.args"] = serverConfig.args.join(" ");
12678
+ }
12679
+ }
12680
+ return metadata;
12681
+ }
12682
+ function parseToolName(rawToolName) {
12683
+ const mcpMatch = rawToolName.match(/^mcp__([^_]+)__(.+)$/);
12684
+ if (mcpMatch) {
12685
+ const [, mcpServer, toolName] = mcpMatch;
12686
+ return {
12687
+ displayName: `tool: ${mcpServer}/${toolName}`,
12688
+ toolName,
12689
+ mcpServer,
12690
+ rawToolName
12691
+ };
12692
+ }
12693
+ return {
12694
+ displayName: `tool: ${rawToolName}`,
12695
+ toolName: rawToolName,
12696
+ rawToolName
12697
+ };
12698
+ }
12699
+ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers, subAgentSpans, endedSubAgentSpans) {
12700
+ const preToolUse = async (input, toolUseID) => {
12701
+ if (input.hook_event_name !== "PreToolUse" || !toolUseID) {
12702
+ return {};
12703
+ }
12704
+ if (input.tool_name === "Task") {
12705
+ return {};
12706
+ }
12707
+ const parsed = parseToolName(input.tool_name);
12708
+ const mcpMetadata = getMcpServerMetadata(parsed.mcpServer, mcpServers);
12709
+ const parentExport = await resolveParentSpan(toolUseID);
12710
+ const toolSpan = startSpan({
12711
+ name: parsed.displayName,
12712
+ spanAttributes: { type: "tool" /* TOOL */ },
12713
+ event: {
12714
+ input: input.tool_input,
12715
+ metadata: {
12716
+ // GenAI semantic conventions
12717
+ "gen_ai.tool.name": parsed.toolName,
12718
+ "gen_ai.tool.call.id": toolUseID,
12719
+ // MCP-specific metadata
12720
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
12721
+ ...mcpMetadata,
12722
+ // Claude SDK metadata
12723
+ "claude_agent_sdk.raw_tool_name": parsed.rawToolName,
12724
+ "claude_agent_sdk.session_id": input.session_id,
12725
+ "claude_agent_sdk.cwd": input.cwd
12726
+ }
12727
+ },
12728
+ parent: parentExport
12729
+ });
12730
+ activeToolSpans.set(toolUseID, toolSpan);
12731
+ return {};
12732
+ };
12733
+ const postToolUse = async (input, toolUseID) => {
12734
+ if (input.hook_event_name !== "PostToolUse" || !toolUseID) {
12735
+ return {};
12736
+ }
12737
+ const subAgentSpan = subAgentSpans.get(toolUseID);
12738
+ if (subAgentSpan) {
12739
+ try {
12740
+ const response = input.tool_response;
12741
+ const metadata = {};
12742
+ if (_optionalChain([response, 'optionalAccess', _267 => _267.status])) {
12743
+ metadata["claude_agent_sdk.status"] = response.status;
12744
+ }
12745
+ if (_optionalChain([response, 'optionalAccess', _268 => _268.totalDurationMs])) {
12746
+ metadata["claude_agent_sdk.duration_ms"] = response.totalDurationMs;
12747
+ }
12748
+ if (_optionalChain([response, 'optionalAccess', _269 => _269.totalToolUseCount]) !== void 0) {
12749
+ metadata["claude_agent_sdk.tool_use_count"] = response.totalToolUseCount;
12750
+ }
12751
+ subAgentSpan.log({
12752
+ output: _optionalChain([response, 'optionalAccess', _270 => _270.content]),
12753
+ metadata
12754
+ });
12755
+ } finally {
12756
+ subAgentSpan.end();
12757
+ endedSubAgentSpans.add(toolUseID);
12758
+ }
12759
+ return {};
12760
+ }
12761
+ const toolSpan = activeToolSpans.get(toolUseID);
12762
+ if (!toolSpan) {
12763
+ return {};
12764
+ }
12765
+ try {
12766
+ toolSpan.log({ output: input.tool_response });
12767
+ } finally {
12768
+ toolSpan.end();
12769
+ activeToolSpans.delete(toolUseID);
12770
+ }
12771
+ return {};
12772
+ };
12773
+ const postToolUseFailure = async (input, toolUseID) => {
12774
+ if (input.hook_event_name !== "PostToolUseFailure" || !toolUseID) {
12775
+ return {};
12776
+ }
12777
+ const subAgentSpan = subAgentSpans.get(toolUseID);
12778
+ if (subAgentSpan) {
12779
+ try {
12780
+ subAgentSpan.log({ error: input.error });
12781
+ } finally {
12782
+ subAgentSpan.end();
12783
+ endedSubAgentSpans.add(toolUseID);
12784
+ }
12785
+ return {};
12786
+ }
12787
+ const toolSpan = activeToolSpans.get(toolUseID);
12788
+ if (!toolSpan) {
12789
+ return {};
12790
+ }
12791
+ const parsed = parseToolName(input.tool_name);
12792
+ try {
12793
+ toolSpan.log({
12794
+ error: input.error,
12795
+ metadata: {
12796
+ "gen_ai.tool.name": parsed.toolName,
12797
+ "gen_ai.tool.call.id": toolUseID,
12798
+ ...parsed.mcpServer && { "mcp.server": parsed.mcpServer },
12799
+ "claude_agent_sdk.is_interrupt": input.is_interrupt,
12800
+ "claude_agent_sdk.session_id": input.session_id
12801
+ }
12802
+ });
12803
+ } finally {
12804
+ toolSpan.end();
12805
+ activeToolSpans.delete(toolUseID);
12806
+ }
12807
+ return {};
12808
+ };
12809
+ return { preToolUse, postToolUse, postToolUseFailure };
12810
+ }
12811
+ function injectTracingHooks(options, resolveParentSpan, activeToolSpans, subAgentSpans, endedSubAgentSpans) {
12812
+ const mcpServers = options.mcpServers;
12813
+ const { preToolUse, postToolUse, postToolUseFailure } = createToolTracingHooks(
12814
+ resolveParentSpan,
12815
+ activeToolSpans,
12816
+ mcpServers,
12817
+ subAgentSpans,
12818
+ endedSubAgentSpans
12819
+ );
12820
+ const existingHooks = _nullishCoalesce(options.hooks, () => ( {}));
12821
+ return {
12822
+ ...options,
12823
+ hooks: {
12824
+ ...existingHooks,
12825
+ PreToolUse: [
12826
+ ..._nullishCoalesce(existingHooks.PreToolUse, () => ( [])),
12827
+ { hooks: [preToolUse] }
12828
+ ],
12829
+ PostToolUse: [
12830
+ ..._nullishCoalesce(existingHooks.PostToolUse, () => ( [])),
12831
+ { hooks: [postToolUse] }
12832
+ ],
12833
+ PostToolUseFailure: [
12834
+ ..._nullishCoalesce(existingHooks.PostToolUseFailure, () => ( [])),
12835
+ { hooks: [postToolUseFailure] }
12836
+ ]
12837
+ }
12838
+ };
12839
+ }
12163
12840
  function filterSerializableOptions(options) {
12164
12841
  const allowedKeys = [
12165
12842
  "model",
@@ -12184,18 +12861,45 @@ function filterSerializableOptions(options) {
12184
12861
  }
12185
12862
  return filtered;
12186
12863
  }
12864
+ function isAsyncIterable(value) {
12865
+ return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
12866
+ }
12187
12867
  function wrapClaudeAgentQuery(queryFn, defaultThis) {
12188
12868
  const proxy = new Proxy(queryFn, {
12189
12869
  apply(target, thisArg, argArray) {
12190
12870
  const params = _nullishCoalesce(argArray[0], () => ( {}));
12191
12871
  const { prompt, options = {} } = params;
12872
+ const promptIsAsyncIterable = isAsyncIterable(prompt);
12873
+ let capturedPromptMessages;
12874
+ let promptForQuery = prompt;
12875
+ let promptStarted = false;
12876
+ let resolvePromptDone;
12877
+ const promptDone = new Promise((resolve) => {
12878
+ resolvePromptDone = resolve;
12879
+ });
12880
+ if (promptIsAsyncIterable) {
12881
+ capturedPromptMessages = [];
12882
+ const originalPrompt = prompt;
12883
+ const capturingPrompt = (async function* () {
12884
+ promptStarted = true;
12885
+ try {
12886
+ for await (const msg of originalPrompt) {
12887
+ capturedPromptMessages.push(msg);
12888
+ yield msg;
12889
+ }
12890
+ } finally {
12891
+ _optionalChain([resolvePromptDone, 'optionalCall', _271 => _271()]);
12892
+ }
12893
+ })();
12894
+ promptForQuery = capturingPrompt;
12895
+ }
12192
12896
  const span = startSpan({
12193
12897
  name: "Claude Agent",
12194
12898
  spanAttributes: {
12195
12899
  type: "task" /* TASK */
12196
12900
  },
12197
12901
  event: {
12198
- input: typeof prompt === "string" ? prompt : { type: "streaming", description: "AsyncIterable<SDKMessage>" },
12902
+ input: typeof prompt === "string" ? prompt : promptIsAsyncIterable ? void 0 : prompt !== void 0 ? String(prompt) : void 0,
12199
12903
  metadata: filterSerializableOptions(options)
12200
12904
  }
12201
12905
  });
@@ -12206,47 +12910,120 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
12206
12910
  let currentMessageStartTime = getCurrentUnixTimestamp();
12207
12911
  const currentMessages = [];
12208
12912
  const createLLMSpan = async () => {
12913
+ const parentToolUseId = _nullishCoalesce(_optionalChain([currentMessages, 'access', _272 => _272[0], 'optionalAccess', _273 => _273.parent_tool_use_id]), () => ( null));
12914
+ let parentSpanExport;
12915
+ if (parentToolUseId) {
12916
+ const subAgentSpan = subAgentSpans.get(parentToolUseId);
12917
+ parentSpanExport = subAgentSpan ? await subAgentSpan.export() : await span.export();
12918
+ } else {
12919
+ parentSpanExport = await span.export();
12920
+ }
12209
12921
  const finalMessageContent = await _createLLMSpanForMessages(
12210
12922
  currentMessages,
12211
12923
  prompt,
12212
12924
  finalResults,
12213
12925
  options,
12214
12926
  currentMessageStartTime,
12215
- await span.export()
12927
+ capturedPromptMessages,
12928
+ parentSpanExport
12216
12929
  );
12217
12930
  if (finalMessageContent) {
12218
12931
  finalResults.push(finalMessageContent);
12219
12932
  }
12220
12933
  const lastMessage = currentMessages[currentMessages.length - 1];
12221
- if (_optionalChain([lastMessage, 'optionalAccess', _271 => _271.message, 'optionalAccess', _272 => _272.usage])) {
12934
+ if (_optionalChain([lastMessage, 'optionalAccess', _274 => _274.message, 'optionalAccess', _275 => _275.usage])) {
12222
12935
  const outputTokens = getNumberProperty2(lastMessage.message.usage, "output_tokens") || 0;
12223
12936
  accumulatedOutputTokens += outputTokens;
12224
12937
  }
12225
12938
  currentMessages.length = 0;
12226
12939
  };
12227
12940
  const invocationTarget = thisArg === proxy || thisArg === void 0 ? _nullishCoalesce(defaultThis, () => ( thisArg)) : thisArg;
12941
+ const activeToolSpans = /* @__PURE__ */ new Map();
12942
+ const subAgentSpans = /* @__PURE__ */ new Map();
12943
+ const endedSubAgentSpans = /* @__PURE__ */ new Set();
12944
+ const toolUseToParent = /* @__PURE__ */ new Map();
12945
+ const pendingSubAgentNames = /* @__PURE__ */ new Map();
12946
+ const resolveParentSpan = async (toolUseID) => {
12947
+ const parentToolUseId = toolUseToParent.get(toolUseID);
12948
+ if (parentToolUseId) {
12949
+ const subAgentSpan = subAgentSpans.get(parentToolUseId);
12950
+ if (subAgentSpan) {
12951
+ return subAgentSpan.export();
12952
+ }
12953
+ }
12954
+ return span.export();
12955
+ };
12956
+ const optionsWithHooks = injectTracingHooks(
12957
+ options,
12958
+ resolveParentSpan,
12959
+ activeToolSpans,
12960
+ subAgentSpans,
12961
+ endedSubAgentSpans
12962
+ );
12963
+ const modifiedArgArray = [
12964
+ {
12965
+ ...params,
12966
+ ...promptForQuery !== void 0 ? { prompt: promptForQuery } : {},
12967
+ options: optionsWithHooks
12968
+ }
12969
+ ];
12228
12970
  const originalGenerator = withCurrent(
12229
12971
  span,
12230
- () => Reflect.apply(target, invocationTarget, argArray)
12972
+ () => Reflect.apply(target, invocationTarget, modifiedArgArray)
12231
12973
  );
12232
12974
  const wrappedGenerator = (async function* () {
12233
12975
  try {
12234
12976
  for await (const message of originalGenerator) {
12235
12977
  const currentTime = getCurrentUnixTimestamp();
12236
- const messageId = _optionalChain([message, 'access', _273 => _273.message, 'optionalAccess', _274 => _274.id]);
12978
+ if (message.type === "assistant" && Array.isArray(_optionalChain([message, 'access', _276 => _276.message, 'optionalAccess', _277 => _277.content]))) {
12979
+ const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
12980
+ for (const block of message.message.content) {
12981
+ if (block.type === "tool_use" && block.id) {
12982
+ toolUseToParent.set(block.id, parentToolUseId);
12983
+ if (block.name === "Task" && _optionalChain([block, 'access', _278 => _278.input, 'optionalAccess', _279 => _279.subagent_type])) {
12984
+ pendingSubAgentNames.set(
12985
+ block.id,
12986
+ block.input.subagent_type
12987
+ );
12988
+ }
12989
+ }
12990
+ }
12991
+ }
12992
+ if ("parent_tool_use_id" in message) {
12993
+ const parentToolUseId = message.parent_tool_use_id;
12994
+ if (parentToolUseId && !subAgentSpans.has(parentToolUseId)) {
12995
+ const agentName = pendingSubAgentNames.get(parentToolUseId);
12996
+ const spanName = agentName ? `Agent: ${agentName}` : "Agent: sub-agent";
12997
+ const parentExport = await span.export();
12998
+ const subAgentSpan = startSpan({
12999
+ name: spanName,
13000
+ spanAttributes: { type: "task" /* TASK */ },
13001
+ event: {
13002
+ metadata: {
13003
+ ...agentName && {
13004
+ "claude_agent_sdk.agent_type": agentName
13005
+ }
13006
+ }
13007
+ },
13008
+ parent: parentExport
13009
+ });
13010
+ subAgentSpans.set(parentToolUseId, subAgentSpan);
13011
+ }
13012
+ }
13013
+ const messageId = _optionalChain([message, 'access', _280 => _280.message, 'optionalAccess', _281 => _281.id]);
12237
13014
  if (messageId && messageId !== currentMessageId) {
12238
13015
  await createLLMSpan();
12239
13016
  currentMessageId = messageId;
12240
13017
  currentMessageStartTime = currentTime;
12241
13018
  }
12242
- if (message.type === "assistant" && _optionalChain([message, 'access', _275 => _275.message, 'optionalAccess', _276 => _276.usage])) {
13019
+ if (message.type === "assistant" && _optionalChain([message, 'access', _282 => _282.message, 'optionalAccess', _283 => _283.usage])) {
12243
13020
  currentMessages.push(message);
12244
13021
  }
12245
13022
  if (message.type === "result" && message.usage) {
12246
13023
  finalUsageMetrics = _extractUsageFromMessage(message);
12247
13024
  if (currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
12248
13025
  const lastMessage = currentMessages[currentMessages.length - 1];
12249
- if (_optionalChain([lastMessage, 'optionalAccess', _277 => _277.message, 'optionalAccess', _278 => _278.usage])) {
13026
+ if (_optionalChain([lastMessage, 'optionalAccess', _284 => _284.message, 'optionalAccess', _285 => _285.usage])) {
12250
13027
  const adjustedTokens = finalUsageMetrics.completion_tokens - accumulatedOutputTokens;
12251
13028
  if (adjustedTokens >= 0) {
12252
13029
  lastMessage.message.usage.output_tokens = adjustedTokens;
@@ -12278,6 +13055,22 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
12278
13055
  });
12279
13056
  throw error;
12280
13057
  } finally {
13058
+ for (const [id, subSpan] of subAgentSpans) {
13059
+ if (!endedSubAgentSpans.has(id)) {
13060
+ subSpan.end();
13061
+ }
13062
+ }
13063
+ subAgentSpans.clear();
13064
+ if (capturedPromptMessages) {
13065
+ if (promptStarted) {
13066
+ await promptDone;
13067
+ }
13068
+ if (capturedPromptMessages.length > 0) {
13069
+ span.log({
13070
+ input: _formatCapturedMessages(capturedPromptMessages)
13071
+ });
13072
+ }
13073
+ }
12281
13074
  span.end();
12282
13075
  }
12283
13076
  })();
@@ -12305,48 +13098,30 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
12305
13098
  });
12306
13099
  return proxy;
12307
13100
  }
12308
- function wrapClaudeAgentTool(toolDef) {
12309
- const originalHandler = toolDef.handler;
12310
- const wrappedHandler = (args, extra) => traced(
12311
- async (span) => {
12312
- span.log({
12313
- input: args,
12314
- metadata: {
12315
- tool_name: toolDef.name,
12316
- tool_description: toolDef.description
12317
- }
12318
- });
12319
- const result = await originalHandler(args, extra);
12320
- span.log({
12321
- output: result
12322
- });
12323
- return result;
12324
- },
12325
- {
12326
- name: `${toolDef.name}`,
12327
- spanAttributes: {
12328
- type: "tool" /* TOOL */
13101
+ function _buildLLMInput(prompt, conversationHistory, capturedPromptMessages) {
13102
+ const promptMessages = [];
13103
+ if (typeof prompt === "string") {
13104
+ promptMessages.push({ content: prompt, role: "user" });
13105
+ } else if (capturedPromptMessages && capturedPromptMessages.length > 0) {
13106
+ for (const msg of capturedPromptMessages) {
13107
+ const role = _optionalChain([msg, 'access', _286 => _286.message, 'optionalAccess', _287 => _287.role]);
13108
+ const content = _optionalChain([msg, 'access', _288 => _288.message, 'optionalAccess', _289 => _289.content]);
13109
+ if (role && content !== void 0) {
13110
+ promptMessages.push({ content, role });
12329
13111
  }
12330
13112
  }
12331
- );
12332
- return {
12333
- ...toolDef,
12334
- handler: wrappedHandler
12335
- };
12336
- }
12337
- function _buildLLMInput(prompt, conversationHistory) {
12338
- const promptMessage = typeof prompt === "string" ? { content: prompt, role: "user" } : void 0;
12339
- const inputParts = [
12340
- ...promptMessage ? [promptMessage] : [],
12341
- ...conversationHistory
12342
- ];
13113
+ }
13114
+ const inputParts = [...promptMessages, ...conversationHistory];
12343
13115
  return inputParts.length > 0 ? inputParts : void 0;
12344
13116
  }
13117
+ function _formatCapturedMessages(messages) {
13118
+ return messages.length > 0 ? messages : [];
13119
+ }
12345
13120
  function _extractUsageFromMessage(message) {
12346
13121
  const metrics = {};
12347
13122
  let usage;
12348
13123
  if (message.type === "assistant") {
12349
- usage = _optionalChain([message, 'access', _279 => _279.message, 'optionalAccess', _280 => _280.usage]);
13124
+ usage = _optionalChain([message, 'access', _290 => _290.message, 'optionalAccess', _291 => _291.usage]);
12350
13125
  } else if (message.type === "result") {
12351
13126
  usage = message.usage;
12352
13127
  }
@@ -12375,17 +13150,21 @@ function _extractUsageFromMessage(message) {
12375
13150
  }
12376
13151
  return metrics;
12377
13152
  }
12378
- async function _createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, parentSpan) {
13153
+ async function _createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, capturedPromptMessages, parentSpan) {
12379
13154
  if (messages.length === 0) return void 0;
12380
13155
  const lastMessage = messages[messages.length - 1];
12381
- if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _281 => _281.message, 'optionalAccess', _282 => _282.usage])) {
13156
+ if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _292 => _292.message, 'optionalAccess', _293 => _293.usage])) {
12382
13157
  return void 0;
12383
13158
  }
12384
13159
  const model = lastMessage.message.model || options.model;
12385
13160
  const usage = _extractUsageFromMessage(lastMessage);
12386
- const input = _buildLLMInput(prompt, conversationHistory);
13161
+ const input = _buildLLMInput(
13162
+ prompt,
13163
+ conversationHistory,
13164
+ capturedPromptMessages
13165
+ );
12387
13166
  const outputs = messages.map(
12388
- (m) => _optionalChain([m, 'access', _283 => _283.message, 'optionalAccess', _284 => _284.content]) && _optionalChain([m, 'access', _285 => _285.message, 'optionalAccess', _286 => _286.role]) ? { content: m.message.content, role: m.message.role } : void 0
13167
+ (m) => _optionalChain([m, 'access', _294 => _294.message, 'optionalAccess', _295 => _295.content]) && _optionalChain([m, 'access', _296 => _296.message, 'optionalAccess', _297 => _297.role]) ? { content: m.message.content, role: m.message.role } : void 0
12389
13168
  ).filter((c) => c !== void 0);
12390
13169
  await traced(
12391
13170
  (llmSpan) => {
@@ -12405,7 +13184,7 @@ async function _createLLMSpanForMessages(messages, prompt, conversationHistory,
12405
13184
  parent: parentSpan
12406
13185
  }
12407
13186
  );
12408
- return _optionalChain([lastMessage, 'access', _287 => _287.message, 'optionalAccess', _288 => _288.content]) && _optionalChain([lastMessage, 'access', _289 => _289.message, 'optionalAccess', _290 => _290.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
13187
+ return _optionalChain([lastMessage, 'access', _298 => _298.message, 'optionalAccess', _299 => _299.content]) && _optionalChain([lastMessage, 'access', _300 => _300.message, 'optionalAccess', _301 => _301.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
12409
13188
  }
12410
13189
  function wrapClaudeAgentSDK(sdk) {
12411
13190
  const cache = /* @__PURE__ */ new Map();
@@ -12424,25 +13203,9 @@ function wrapClaudeAgentSDK(sdk) {
12424
13203
  return wrappedQuery;
12425
13204
  }
12426
13205
  if (prop === "tool" && typeof value === "function") {
12427
- const toolFn = value;
12428
- const wrappedToolFactory = new Proxy(toolFn, {
12429
- apply(toolTarget, thisArg, argArray) {
12430
- const invocationTarget = thisArg === receiver || thisArg === void 0 ? target : thisArg;
12431
- const toolDef = Reflect.apply(
12432
- toolTarget,
12433
- invocationTarget,
12434
- argArray
12435
- );
12436
- if (toolDef && typeof toolDef === "object" && "handler" in toolDef) {
12437
- return wrapClaudeAgentTool(
12438
- toolDef
12439
- );
12440
- }
12441
- return toolDef;
12442
- }
12443
- });
12444
- cache.set(prop, wrappedToolFactory);
12445
- return wrappedToolFactory;
13206
+ const bound = value.bind(target);
13207
+ cache.set(prop, bound);
13208
+ return bound;
12446
13209
  }
12447
13210
  if (typeof value === "function") {
12448
13211
  const bound = value.bind(target);
@@ -12695,7 +13458,7 @@ function serializePart(part) {
12695
13458
  return part;
12696
13459
  }
12697
13460
  function serializeTools(params) {
12698
- if (!_optionalChain([params, 'access', _291 => _291.config, 'optionalAccess', _292 => _292.tools])) {
13461
+ if (!_optionalChain([params, 'access', _302 => _302.config, 'optionalAccess', _303 => _303.tools])) {
12699
13462
  return null;
12700
13463
  }
12701
13464
  try {
@@ -12778,7 +13541,7 @@ function aggregateGenerateContentChunks(chunks, start, firstTokenTime) {
12778
13541
  }
12779
13542
  if (chunk.candidates && Array.isArray(chunk.candidates)) {
12780
13543
  for (const candidate of chunk.candidates) {
12781
- if (_optionalChain([candidate, 'access', _293 => _293.content, 'optionalAccess', _294 => _294.parts])) {
13544
+ if (_optionalChain([candidate, 'access', _304 => _304.content, 'optionalAccess', _305 => _305.parts])) {
12782
13545
  for (const part of candidate.content.parts) {
12783
13546
  if (part.text !== void 0) {
12784
13547
  if (part.thought) {
@@ -12809,7 +13572,7 @@ function aggregateGenerateContentChunks(chunks, start, firstTokenTime) {
12809
13572
  parts.push({ text });
12810
13573
  }
12811
13574
  parts.push(...otherParts);
12812
- if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _295 => _295.candidates])) {
13575
+ if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _306 => _306.candidates])) {
12813
13576
  const candidates = [];
12814
13577
  for (const candidate of lastResponse.candidates) {
12815
13578
  const candidateDict = {
@@ -12889,15 +13652,15 @@ __export(graph_framework_exports, {
12889
13652
  escapePath: () => escapePath,
12890
13653
  unescapePath: () => unescapePath
12891
13654
  });
12892
- var GraphBuilder = (_class15 = class {
12893
- __init58() {this.nodes = /* @__PURE__ */ new Map()}
12894
- __init59() {this.edges = {}}
12895
- __init60() {this.nodeLikeNodes = /* @__PURE__ */ new Map()}
13655
+ var GraphBuilder = (_class16 = class {
13656
+ __init60() {this.nodes = /* @__PURE__ */ new Map()}
13657
+ __init61() {this.edges = {}}
13658
+ __init62() {this.nodeLikeNodes = /* @__PURE__ */ new Map()}
12896
13659
  // Maps node-like objects, like prompts, to their nodes
12897
13660
  // Special nodes
12898
13661
 
12899
13662
 
12900
- constructor() {;_class15.prototype.__init58.call(this);_class15.prototype.__init59.call(this);_class15.prototype.__init60.call(this);
13663
+ constructor() {;_class16.prototype.__init60.call(this);_class16.prototype.__init61.call(this);_class16.prototype.__init62.call(this);
12901
13664
  this.IN = this.createInputNode();
12902
13665
  this.OUT = this.createOutputNode();
12903
13666
  }
@@ -13027,7 +13790,7 @@ var GraphBuilder = (_class15 = class {
13027
13790
  this.nodes.set(id, promptNode);
13028
13791
  return promptNode;
13029
13792
  }
13030
- }, _class15);
13793
+ }, _class16);
13031
13794
  function isProxyVariable(node) {
13032
13795
  return typeof node === "object" && node !== null && "__type" in node && // @ts-ignore
13033
13796
  node.__type === "proxy-variable";
@@ -13035,17 +13798,17 @@ function isProxyVariable(node) {
13035
13798
  function proxyVariableToNode(proxy) {
13036
13799
  return [proxy.__node, proxy.__path];
13037
13800
  }
13038
- var BaseNode = (_class16 = class {
13039
- constructor(graph, id) {;_class16.prototype.__init61.call(this);_class16.prototype.__init62.call(this);
13801
+ var BaseNode = (_class17 = class {
13802
+ constructor(graph, id) {;_class17.prototype.__init63.call(this);_class17.prototype.__init64.call(this);
13040
13803
  this.graph = graph;
13041
13804
  this.id = id;
13042
13805
  }
13043
- __init61() {this.__type = "node"}
13044
- __init62() {this.dependencies = []}
13806
+ __init63() {this.__type = "node"}
13807
+ __init64() {this.dependencies = []}
13045
13808
  addDependency(dependency) {
13046
13809
  this.dependencies.push(dependency);
13047
13810
  }
13048
- }, _class16);
13811
+ }, _class17);
13049
13812
  var InputNode = class extends BaseNode {
13050
13813
  constructor(graph, id) {
13051
13814
  super(graph, id);
@@ -13227,7 +13990,7 @@ function isAsync(fn) {
13227
13990
  function isAsyncGenerator2(fn) {
13228
13991
  return fn[Symbol.toStringTag] === "AsyncGenerator";
13229
13992
  }
13230
- function isAsyncIterable(obj) {
13993
+ function isAsyncIterable2(obj) {
13231
13994
  return typeof obj[Symbol.asyncIterator] === "function";
13232
13995
  }
13233
13996
  function wrapAsync(asyncFn) {
@@ -13398,7 +14161,7 @@ var eachOfLimit$2 = (limit) => {
13398
14161
  if (isAsyncGenerator2(obj)) {
13399
14162
  return asyncEachOfLimit(obj, limit, iteratee, callback);
13400
14163
  }
13401
- if (isAsyncIterable(obj)) {
14164
+ if (isAsyncIterable2(obj)) {
13402
14165
  return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback);
13403
14166
  }
13404
14167
  var nextElem = createIterator(obj);
@@ -14247,11 +15010,11 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
14247
15010
  return this._state;
14248
15011
  }
14249
15012
  };
14250
- var CachedSpanFetcher = (_class17 = class {
14251
- __init63() {this.spanCache = /* @__PURE__ */ new Map()}
14252
- __init64() {this.allFetched = false}
15013
+ var CachedSpanFetcher = (_class18 = class {
15014
+ __init65() {this.spanCache = /* @__PURE__ */ new Map()}
15015
+ __init66() {this.allFetched = false}
14253
15016
 
14254
- constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class17.prototype.__init63.call(this);_class17.prototype.__init64.call(this);
15017
+ constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class18.prototype.__init65.call(this);_class18.prototype.__init66.call(this);
14255
15018
  if (typeof objectTypeOrFetchFn === "function") {
14256
15019
  this.fetchFn = objectTypeOrFetchFn;
14257
15020
  } else {
@@ -14266,7 +15029,7 @@ var CachedSpanFetcher = (_class17 = class {
14266
15029
  spanType
14267
15030
  );
14268
15031
  const rows = await fetcher.fetchedData();
14269
- return rows.filter((row) => _optionalChain([row, 'access', _296 => _296.span_attributes, 'optionalAccess', _297 => _297.purpose]) !== "scorer").map((row) => ({
15032
+ return rows.filter((row) => _optionalChain([row, 'access', _307 => _307.span_attributes, 'optionalAccess', _308 => _308.purpose]) !== "scorer").map((row) => ({
14270
15033
  input: row.input,
14271
15034
  output: row.output,
14272
15035
  metadata: row.metadata,
@@ -14300,7 +15063,7 @@ var CachedSpanFetcher = (_class17 = class {
14300
15063
  async fetchSpans(spanType) {
14301
15064
  const spans = await this.fetchFn(spanType);
14302
15065
  for (const span of spans) {
14303
- const type = _nullishCoalesce(_optionalChain([span, 'access', _298 => _298.span_attributes, 'optionalAccess', _299 => _299.type]), () => ( ""));
15066
+ const type = _nullishCoalesce(_optionalChain([span, 'access', _309 => _309.span_attributes, 'optionalAccess', _310 => _310.type]), () => ( ""));
14304
15067
  const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
14305
15068
  existing.push(span);
14306
15069
  this.spanCache.set(type, existing);
@@ -14317,23 +15080,24 @@ var CachedSpanFetcher = (_class17 = class {
14317
15080
  }
14318
15081
  return result;
14319
15082
  }
14320
- }, _class17);
14321
- var LocalTrace = (_class18 = class {
15083
+ }, _class18);
15084
+ var LocalTrace = (_class19 = class {
14322
15085
 
14323
15086
 
14324
15087
 
14325
15088
 
14326
15089
 
14327
- __init65() {this.spansFlushed = false}
14328
- __init66() {this.spansFlushPromise = null}
15090
+ __init67() {this.spansFlushed = false}
15091
+ __init68() {this.spansFlushPromise = null}
14329
15092
 
15093
+ __init69() {this.threadCache = /* @__PURE__ */ new Map()}
14330
15094
  constructor({
14331
15095
  objectType,
14332
15096
  objectId,
14333
15097
  rootSpanId,
14334
15098
  ensureSpansFlushed,
14335
15099
  state
14336
- }) {;_class18.prototype.__init65.call(this);_class18.prototype.__init66.call(this);
15100
+ }) {;_class19.prototype.__init67.call(this);_class19.prototype.__init68.call(this);_class19.prototype.__init69.call(this);
14337
15101
  this.objectType = objectType;
14338
15102
  this.objectId = objectId;
14339
15103
  this.rootSpanId = rootSpanId;
@@ -14379,11 +15143,11 @@ var LocalTrace = (_class18 = class {
14379
15143
  const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
14380
15144
  if (cachedSpans && cachedSpans.length > 0) {
14381
15145
  let spans = cachedSpans.filter(
14382
- (span) => _optionalChain([span, 'access', _300 => _300.span_attributes, 'optionalAccess', _301 => _301.purpose]) !== "scorer"
15146
+ (span) => _optionalChain([span, 'access', _311 => _311.span_attributes, 'optionalAccess', _312 => _312.purpose]) !== "scorer"
14383
15147
  );
14384
15148
  if (spanType && spanType.length > 0) {
14385
15149
  spans = spans.filter(
14386
- (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _302 => _302.span_attributes, 'optionalAccess', _303 => _303.type]), () => ( "")))
15150
+ (span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _313 => _313.span_attributes, 'optionalAccess', _314 => _314.type]), () => ( "")))
14387
15151
  );
14388
15152
  }
14389
15153
  return spans.map((span) => ({
@@ -14397,6 +15161,36 @@ var LocalTrace = (_class18 = class {
14397
15161
  }
14398
15162
  return this.cachedFetcher.getSpans({ spanType });
14399
15163
  }
15164
+ /**
15165
+ * Get the thread (preprocessed messages) for this trace.
15166
+ * Calls the API with the project_default preprocessor (which falls back to "thread").
15167
+ */
15168
+ async getThread(options) {
15169
+ const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _315 => _315.preprocessor]), () => ( "project_default"));
15170
+ if (!this.threadCache.has(cacheKey)) {
15171
+ const promise = this.fetchThread(options);
15172
+ this.threadCache.set(cacheKey, promise);
15173
+ }
15174
+ return this.threadCache.get(cacheKey);
15175
+ }
15176
+ async fetchThread(options) {
15177
+ await this.ensureSpansReady();
15178
+ await this.state.login({});
15179
+ const result = await invoke({
15180
+ globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _316 => _316.preprocessor]), () => ( "project_default")),
15181
+ functionType: "preprocessor",
15182
+ input: {
15183
+ trace_ref: {
15184
+ object_type: this.objectType,
15185
+ object_id: this.objectId,
15186
+ root_span_id: this.rootSpanId
15187
+ }
15188
+ },
15189
+ mode: "json",
15190
+ state: this.state
15191
+ });
15192
+ return Array.isArray(result) ? result : [];
15193
+ }
14400
15194
  async ensureSpansReady() {
14401
15195
  if (this.spansFlushed || !this.ensureSpansFlushed) {
14402
15196
  return;
@@ -14414,7 +15208,7 @@ var LocalTrace = (_class18 = class {
14414
15208
  }
14415
15209
  await this.spansFlushPromise;
14416
15210
  }
14417
- }, _class18);
15211
+ }, _class19);
14418
15212
 
14419
15213
  // src/reporters/progress.ts
14420
15214
  var SimpleProgressReporter = class {
@@ -14431,248 +15225,10 @@ var SimpleProgressReporter = class {
14431
15225
 
14432
15226
  // src/eval-parameters.ts
14433
15227
 
15228
+ var _ajv = require('ajv'); var _ajv2 = _interopRequireDefault(_ajv);
14434
15229
 
14435
- // src/framework2.ts
15230
+ // src/prompt-schemas.ts
14436
15231
 
14437
- var currentFilename = typeof __filename !== "undefined" ? __filename : "unknown";
14438
- var ProjectBuilder = class {
14439
- create(opts) {
14440
- return new Project2(opts);
14441
- }
14442
- };
14443
- var projects = new ProjectBuilder();
14444
- var Project2 = (_class19 = class {
14445
-
14446
-
14447
-
14448
-
14449
-
14450
- __init67() {this._publishableCodeFunctions = []}
14451
- __init68() {this._publishablePrompts = []}
14452
- constructor(args) {;_class19.prototype.__init67.call(this);_class19.prototype.__init68.call(this);
14453
- _initializeSpanContext();
14454
- this.name = "name" in args ? args.name : void 0;
14455
- this.id = "id" in args ? args.id : void 0;
14456
- this.tools = new ToolBuilder(this);
14457
- this.prompts = new PromptBuilder(this);
14458
- this.scorers = new ScorerBuilder(this);
14459
- }
14460
- addPrompt(prompt) {
14461
- this._publishablePrompts.push(prompt);
14462
- if (globalThis._lazy_load) {
14463
- globalThis._evals.prompts.push(prompt);
14464
- }
14465
- }
14466
- addCodeFunction(fn) {
14467
- this._publishableCodeFunctions.push(fn);
14468
- if (globalThis._lazy_load) {
14469
- globalThis._evals.functions.push(fn);
14470
- }
14471
- }
14472
- async publish() {
14473
- if (globalThis._lazy_load) {
14474
- console.warn("publish() is a no-op when running `braintrust push`.");
14475
- return;
14476
- }
14477
- await login();
14478
- const projectMap = new ProjectNameIdMap();
14479
- const functionDefinitions = [];
14480
- if (this._publishableCodeFunctions.length > 0) {
14481
- console.warn(
14482
- "Code functions cannot be published directly. Use `braintrust push` instead."
14483
- );
14484
- }
14485
- if (this._publishablePrompts.length > 0) {
14486
- for (const prompt of this._publishablePrompts) {
14487
- const functionDefinition = await prompt.toFunctionDefinition(projectMap);
14488
- functionDefinitions.push(functionDefinition);
14489
- }
14490
- }
14491
- await _internalGetGlobalState().apiConn().post_json("insert-functions", {
14492
- functions: functionDefinitions
14493
- });
14494
- }
14495
- }, _class19);
14496
- var ToolBuilder = (_class20 = class {
14497
- constructor(project) {;_class20.prototype.__init69.call(this);
14498
- this.project = project;
14499
- }
14500
- __init69() {this.taskCounter = 0}
14501
- // This type definition is just a catch all so that the implementation can be
14502
- // less specific than the two more specific declarations above.
14503
- create(opts) {
14504
- this.taskCounter++;
14505
- opts = _nullishCoalesce(opts, () => ( {}));
14506
- const { handler, name, slug, parameters, returns, ...rest } = opts;
14507
- let resolvedName = _nullishCoalesce(name, () => ( handler.name));
14508
- if (resolvedName.trim().length === 0) {
14509
- resolvedName = `Tool ${isomorph_default.basename(currentFilename)} ${this.taskCounter}`;
14510
- }
14511
- const tool = new CodeFunction(this.project, {
14512
- handler,
14513
- name: resolvedName,
14514
- slug: _nullishCoalesce(slug, () => ( slugify(resolvedName, { lower: true, strict: true }))),
14515
- type: "tool",
14516
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
14517
- parameters,
14518
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
14519
- returns,
14520
- ...rest
14521
- });
14522
- this.project.addCodeFunction(tool);
14523
- return tool;
14524
- }
14525
- }, _class20);
14526
- var ScorerBuilder = (_class21 = class {
14527
- constructor(project) {;_class21.prototype.__init70.call(this);
14528
- this.project = project;
14529
- }
14530
- __init70() {this.taskCounter = 0}
14531
- create(opts) {
14532
- this.taskCounter++;
14533
- let resolvedName = opts.name;
14534
- if (!resolvedName && "handler" in opts) {
14535
- resolvedName = opts.handler.name;
14536
- }
14537
- if (!resolvedName || resolvedName.trim().length === 0) {
14538
- resolvedName = `Scorer ${isomorph_default.basename(currentFilename)} ${this.taskCounter}`;
14539
- }
14540
- const slug = _nullishCoalesce(opts.slug, () => ( slugify(resolvedName, { lower: true, strict: true })));
14541
- if ("handler" in opts) {
14542
- const scorer = new CodeFunction(this.project, {
14543
- ...opts,
14544
- name: resolvedName,
14545
- slug,
14546
- type: "scorer"
14547
- });
14548
- this.project.addCodeFunction(scorer);
14549
- } else {
14550
- const promptBlock = "messages" in opts ? {
14551
- type: "chat",
14552
- messages: opts.messages
14553
- } : {
14554
- type: "completion",
14555
- content: opts.prompt
14556
- };
14557
- const promptData = {
14558
- prompt: promptBlock,
14559
- options: {
14560
- model: opts.model,
14561
- params: opts.params
14562
- },
14563
- parser: {
14564
- type: "llm_classifier",
14565
- use_cot: opts.useCot,
14566
- choice_scores: opts.choiceScores
14567
- }
14568
- };
14569
- const codePrompt = new CodePrompt(
14570
- this.project,
14571
- promptData,
14572
- [],
14573
- {
14574
- ...opts,
14575
- name: resolvedName,
14576
- slug
14577
- },
14578
- "scorer"
14579
- );
14580
- this.project.addPrompt(codePrompt);
14581
- }
14582
- }
14583
- }, _class21);
14584
- var CodeFunction = class {
14585
- constructor(project, opts) {
14586
- this.project = project;
14587
- this.handler = opts.handler;
14588
- this.name = opts.name;
14589
- this.slug = opts.slug;
14590
- this.description = opts.description;
14591
- this.type = opts.type;
14592
- this.ifExists = opts.ifExists;
14593
- this.metadata = opts.metadata;
14594
- this.parameters = opts.parameters;
14595
- this.returns = opts.returns;
14596
- if (this.returns && !this.parameters) {
14597
- throw new Error("parameters are required if return type is defined");
14598
- }
14599
- }
14600
-
14601
-
14602
-
14603
-
14604
-
14605
-
14606
-
14607
-
14608
-
14609
- key() {
14610
- return JSON.stringify([
14611
- _nullishCoalesce(this.project.id, () => ( "")),
14612
- _nullishCoalesce(this.project.name, () => ( "")),
14613
- this.slug
14614
- ]);
14615
- }
14616
- };
14617
- var CodePrompt = class {
14618
-
14619
-
14620
-
14621
-
14622
-
14623
-
14624
-
14625
-
14626
-
14627
-
14628
- constructor(project, prompt, toolFunctions, opts, functionType) {
14629
- this.project = project;
14630
- this.name = opts.name;
14631
- this.slug = opts.slug;
14632
- this.prompt = prompt;
14633
- this.toolFunctions = toolFunctions;
14634
- this.ifExists = opts.ifExists;
14635
- this.description = opts.description;
14636
- this.id = opts.id;
14637
- this.functionType = functionType;
14638
- this.metadata = opts.metadata;
14639
- }
14640
- async toFunctionDefinition(projectNameToId) {
14641
- const prompt_data = {
14642
- ...this.prompt
14643
- };
14644
- if (this.toolFunctions.length > 0) {
14645
- const resolvableToolFunctions = await Promise.all(
14646
- this.toolFunctions.map(async (fn) => {
14647
- if ("slug" in fn) {
14648
- return {
14649
- type: "slug",
14650
- project_id: await projectNameToId.resolve(fn.project),
14651
- slug: fn.slug
14652
- };
14653
- } else {
14654
- return fn;
14655
- }
14656
- })
14657
- );
14658
- prompt_data.tool_functions = // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
14659
- resolvableToolFunctions;
14660
- }
14661
- return {
14662
- project_id: await projectNameToId.resolve(this.project),
14663
- name: this.name,
14664
- slug: this.slug,
14665
- description: _nullishCoalesce(this.description, () => ( "")),
14666
- function_data: {
14667
- type: "prompt"
14668
- },
14669
- function_type: this.functionType,
14670
- prompt_data,
14671
- if_exists: this.ifExists,
14672
- metadata: this.metadata
14673
- };
14674
- }
14675
- };
14676
15232
  var promptContentsSchema = _v3.z.union([
14677
15233
  _v3.z.object({
14678
15234
  prompt: _v3.z.string()
@@ -14693,46 +15249,6 @@ var promptDefinitionWithToolsSchema = promptDefinitionSchema.and(
14693
15249
  tools: _v3.z.array(ToolFunctionDefinition).optional()
14694
15250
  })
14695
15251
  );
14696
- var PromptBuilder = class {
14697
- constructor(project) {
14698
- this.project = project;
14699
- }
14700
- create(opts) {
14701
- const toolFunctions = [];
14702
- const rawTools = [];
14703
- for (const tool of _nullishCoalesce(opts.tools, () => ( []))) {
14704
- if (tool instanceof CodeFunction) {
14705
- toolFunctions.push(tool);
14706
- } else if ("type" in tool && !("function" in tool)) {
14707
- toolFunctions.push(tool);
14708
- } else {
14709
- rawTools.push(tool);
14710
- }
14711
- }
14712
- const slug = _nullishCoalesce(opts.slug, () => ( slugify(opts.name, { lower: true, strict: true })));
14713
- const promptData = promptDefinitionToPromptData(opts, rawTools);
14714
- const promptRow = {
14715
- id: opts.id,
14716
- _xact_id: opts.version ? loadPrettyXact(opts.version) : void 0,
14717
- name: opts.name,
14718
- slug,
14719
- prompt_data: promptData,
14720
- ...this.project.id !== void 0 ? { project_id: this.project.id } : {}
14721
- };
14722
- const prompt = new Prompt2(
14723
- promptRow,
14724
- {},
14725
- // It doesn't make sense to specify defaults here.
14726
- _nullishCoalesce(opts.noTrace, () => ( false))
14727
- );
14728
- const codePrompt = new CodePrompt(this.project, promptData, toolFunctions, {
14729
- ...opts,
14730
- slug
14731
- });
14732
- this.project.addPrompt(codePrompt);
14733
- return prompt;
14734
- }
14735
- };
14736
15252
  function promptDefinitionToPromptData(promptDefinition, rawTools) {
14737
15253
  const promptBlock = "messages" in promptDefinition ? {
14738
15254
  type: "chat",
@@ -14751,42 +15267,6 @@ function promptDefinitionToPromptData(promptDefinition, rawTools) {
14751
15267
  ...promptDefinition.templateFormat ? { template_format: promptDefinition.templateFormat } : {}
14752
15268
  };
14753
15269
  }
14754
- var ProjectNameIdMap = (_class22 = class {constructor() { _class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this); }
14755
- __init71() {this.nameToId = {}}
14756
- __init72() {this.idToName = {}}
14757
- async getId(projectName) {
14758
- if (!(projectName in this.nameToId)) {
14759
- const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
14760
- project_name: projectName
14761
- });
14762
- const result = _v3.z.object({
14763
- project: Project
14764
- }).parse(response);
14765
- const projectId = result.project.id;
14766
- this.nameToId[projectName] = projectId;
14767
- this.idToName[projectId] = projectName;
14768
- }
14769
- return this.nameToId[projectName];
14770
- }
14771
- async getName(projectId) {
14772
- if (!(projectId in this.idToName)) {
14773
- const response = await _internalGetGlobalState().appConn().post_json("api/project/get", {
14774
- id: projectId
14775
- });
14776
- const result = _v3.z.array(Project).nonempty().parse(response);
14777
- const projectName = result[0].name;
14778
- this.idToName[projectId] = projectName;
14779
- this.nameToId[projectName] = projectId;
14780
- }
14781
- return this.idToName[projectId];
14782
- }
14783
- async resolve(project) {
14784
- if (project.id) {
14785
- return project.id;
14786
- }
14787
- return this.getId(project.name);
14788
- }
14789
- }, _class22);
14790
15270
 
14791
15271
  // src/eval-parameters.ts
14792
15272
  var evalParametersSchema = _v3.z.record(
@@ -14801,7 +15281,31 @@ var evalParametersSchema = _v3.z.record(
14801
15281
  // For Zod schemas
14802
15282
  ])
14803
15283
  );
14804
- function validateParameters(parameters, parameterSchema) {
15284
+ async function validateParameters(parameters, parameterSchema) {
15285
+ let resolvedSchema = parameterSchema;
15286
+ if (resolvedSchema instanceof Promise) {
15287
+ resolvedSchema = await resolvedSchema;
15288
+ }
15289
+ if (resolvedSchema === void 0 || resolvedSchema === null) {
15290
+ return parameters;
15291
+ }
15292
+ if (RemoteEvalParameters.isParameters(resolvedSchema)) {
15293
+ const mergedParameters = parameters && Object.keys(parameters).length > 0 ? {
15294
+ ...resolvedSchema.data,
15295
+ ...parameters
15296
+ } : resolvedSchema.data;
15297
+ return validateParametersWithJsonSchema(
15298
+ mergedParameters,
15299
+ resolvedSchema.schema
15300
+ );
15301
+ }
15302
+ return validateParametersWithZod(
15303
+ parameters,
15304
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
15305
+ resolvedSchema
15306
+ );
15307
+ }
15308
+ function validateParametersWithZod(parameters, parameterSchema) {
14805
15309
  return Object.fromEntries(
14806
15310
  Object.entries(parameterSchema).map(([name, schema]) => {
14807
15311
  const value = parameters[name];
@@ -14828,6 +15332,18 @@ function validateParameters(parameters, parameterSchema) {
14828
15332
  })
14829
15333
  );
14830
15334
  }
15335
+ function validateParametersWithJsonSchema(parameters, schema) {
15336
+ const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
15337
+ const validate = ajv.compile(schema);
15338
+ if (!validate(parameters)) {
15339
+ const errorMessages = _optionalChain([validate, 'access', _317 => _317.errors, 'optionalAccess', _318 => _318.map, 'call', _319 => _319((err) => {
15340
+ const path = err.instancePath || "root";
15341
+ return `${path}: ${err.message}`;
15342
+ }), 'access', _320 => _320.join, 'call', _321 => _321(", ")]);
15343
+ throw Error(`Invalid parameters: ${errorMessages}`);
15344
+ }
15345
+ return parameters;
15346
+ }
14831
15347
 
14832
15348
  // src/framework.ts
14833
15349
  function BaseExperiment(options = {}) {
@@ -14879,7 +15395,7 @@ function callEvaluatorData(data) {
14879
15395
  baseExperiment
14880
15396
  };
14881
15397
  }
14882
- function isAsyncIterable2(value) {
15398
+ function isAsyncIterable3(value) {
14883
15399
  return typeof value === "object" && value !== null && typeof value[Symbol.asyncIterator] === "function";
14884
15400
  }
14885
15401
  function isIterable(value) {
@@ -14888,6 +15404,7 @@ function isIterable(value) {
14888
15404
  globalThis._evals = {
14889
15405
  functions: [],
14890
15406
  prompts: [],
15407
+ parameters: [],
14891
15408
  evaluators: {},
14892
15409
  reporters: {}
14893
15410
  };
@@ -15063,16 +15580,16 @@ var defaultErrorScoreHandler = ({
15063
15580
  };
15064
15581
  async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
15065
15582
  if (enableCache) {
15066
- _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _304 => _304.spanCache, 'optionalAccess', _305 => _305.start, 'call', _306 => _306()]);
15583
+ _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _322 => _322.spanCache, 'optionalAccess', _323 => _323.start, 'call', _324 => _324()]);
15067
15584
  }
15068
15585
  try {
15069
15586
  if (typeof evaluator.data === "string") {
15070
15587
  throw new Error("Unimplemented: string data paths");
15071
15588
  }
15072
15589
  let dataResult = typeof evaluator.data === "function" ? evaluator.data() : evaluator.data;
15073
- parameters = validateParameters(
15590
+ parameters = await validateParameters(
15074
15591
  _nullishCoalesce(parameters, () => ( {})),
15075
- _nullishCoalesce(evaluator.parameters, () => ( {}))
15592
+ evaluator.parameters
15076
15593
  );
15077
15594
  if ("_type" in dataResult) {
15078
15595
  if (dataResult._type !== "BaseExperiment") {
@@ -15099,7 +15616,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15099
15616
  }
15100
15617
  const resolvedDataResult = dataResult instanceof Promise ? await dataResult : dataResult;
15101
15618
  const dataIterable = (() => {
15102
- if (isAsyncIterable2(resolvedDataResult)) {
15619
+ if (isAsyncIterable3(resolvedDataResult)) {
15103
15620
  return resolvedDataResult;
15104
15621
  }
15105
15622
  if (Array.isArray(resolvedDataResult) || isIterable(resolvedDataResult)) {
@@ -15174,7 +15691,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15174
15691
  objectType: parentComponents ? spanObjectTypeV3ToTypedString(
15175
15692
  parentComponents.data.object_type
15176
15693
  ) : "experiment",
15177
- objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _307 => _307.data, 'access', async _308 => _308.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
15694
+ objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _325 => _325.data, 'access', async _326 => _326.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
15178
15695
  rootSpanId: rootSpan.rootSpanId,
15179
15696
  ensureSpansFlushed,
15180
15697
  state
@@ -15200,10 +15717,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15200
15717
  span,
15201
15718
  parameters: _nullishCoalesce(parameters, () => ( {})),
15202
15719
  reportProgress: (event) => {
15203
- _optionalChain([stream, 'optionalCall', _309 => _309({
15720
+ _optionalChain([stream, 'optionalCall', _327 => _327({
15204
15721
  ...event,
15205
15722
  id: rootSpan.id,
15206
- origin: _optionalChain([baseEvent, 'access', _310 => _310.event, 'optionalAccess', _311 => _311.origin]),
15723
+ origin: _optionalChain([baseEvent, 'access', _328 => _328.event, 'optionalAccess', _329 => _329.origin]),
15207
15724
  name: evaluator.evalName,
15208
15725
  object_type: "task"
15209
15726
  })]);
@@ -15231,6 +15748,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15231
15748
  } else {
15232
15749
  rootSpan.log({ output, metadata, expected });
15233
15750
  }
15751
+ if (evaluator.flushBeforeScoring) {
15752
+ await rootSpan.flush();
15753
+ }
15234
15754
  const scoringArgs = {
15235
15755
  input: datum.input,
15236
15756
  expected: "expected" in datum ? datum.expected : void 0,
@@ -15364,7 +15884,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15364
15884
  metadata,
15365
15885
  scores: mergedScores,
15366
15886
  error,
15367
- origin: _optionalChain([baseEvent, 'access', _312 => _312.event, 'optionalAccess', _313 => _313.origin])
15887
+ origin: _optionalChain([baseEvent, 'access', _330 => _330.event, 'optionalAccess', _331 => _331.origin])
15368
15888
  });
15369
15889
  }
15370
15890
  };
@@ -15397,7 +15917,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15397
15917
  break;
15398
15918
  }
15399
15919
  scheduledTrials++;
15400
- _optionalChain([progressReporter, 'access', _314 => _314.setTotal, 'optionalCall', _315 => _315(evaluator.evalName, scheduledTrials)]);
15920
+ _optionalChain([progressReporter, 'access', _332 => _332.setTotal, 'optionalCall', _333 => _333(evaluator.evalName, scheduledTrials)]);
15401
15921
  q.push({ datum, trialIndex });
15402
15922
  }
15403
15923
  }
@@ -15472,9 +15992,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
15472
15992
  );
15473
15993
  } finally {
15474
15994
  if (enableCache) {
15475
- const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _316 => _316.spanCache]);
15476
- _optionalChain([spanCache, 'optionalAccess', _317 => _317.dispose, 'call', _318 => _318()]);
15477
- _optionalChain([spanCache, 'optionalAccess', _319 => _319.stop, 'call', _320 => _320()]);
15995
+ const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _334 => _334.spanCache]);
15996
+ _optionalChain([spanCache, 'optionalAccess', _335 => _335.dispose, 'call', _336 => _336()]);
15997
+ _optionalChain([spanCache, 'optionalAccess', _337 => _337.stop, 'call', _338 => _338()]);
15478
15998
  }
15479
15999
  }
15480
16000
  }
@@ -15638,6 +16158,417 @@ var defaultReporter = {
15638
16158
  }
15639
16159
  };
15640
16160
 
16161
+ // src/framework2.ts
16162
+
16163
+ var currentFilename = typeof __filename !== "undefined" ? __filename : "unknown";
16164
+ var ProjectBuilder = class {
16165
+ create(opts) {
16166
+ return new Project2(opts);
16167
+ }
16168
+ };
16169
+ var projects = new ProjectBuilder();
16170
+ var Project2 = (_class20 = class {
16171
+
16172
+
16173
+
16174
+
16175
+
16176
+
16177
+ __init70() {this._publishableCodeFunctions = []}
16178
+ __init71() {this._publishablePrompts = []}
16179
+ __init72() {this._publishableParameters = []}
16180
+ constructor(args) {;_class20.prototype.__init70.call(this);_class20.prototype.__init71.call(this);_class20.prototype.__init72.call(this);
16181
+ _initializeSpanContext();
16182
+ this.name = "name" in args ? args.name : void 0;
16183
+ this.id = "id" in args ? args.id : void 0;
16184
+ this.tools = new ToolBuilder(this);
16185
+ this.prompts = new PromptBuilder(this);
16186
+ this.parameters = new ParametersBuilder(this);
16187
+ this.scorers = new ScorerBuilder(this);
16188
+ }
16189
+ addPrompt(prompt) {
16190
+ this._publishablePrompts.push(prompt);
16191
+ if (globalThis._lazy_load) {
16192
+ globalThis._evals.prompts.push(prompt);
16193
+ }
16194
+ }
16195
+ addParameters(parameters) {
16196
+ this._publishableParameters.push(parameters);
16197
+ if (globalThis._lazy_load) {
16198
+ if (globalThis._evals.parameters == null)
16199
+ globalThis._evals.parameters = [];
16200
+ globalThis._evals.parameters.push(parameters);
16201
+ }
16202
+ }
16203
+ addCodeFunction(fn) {
16204
+ this._publishableCodeFunctions.push(fn);
16205
+ if (globalThis._lazy_load) {
16206
+ globalThis._evals.functions.push(fn);
16207
+ }
16208
+ }
16209
+ async publish() {
16210
+ if (globalThis._lazy_load) {
16211
+ console.warn("publish() is a no-op when running `braintrust push`.");
16212
+ return;
16213
+ }
16214
+ await login();
16215
+ const projectMap = new ProjectNameIdMap();
16216
+ const functionDefinitions = [];
16217
+ if (this._publishableCodeFunctions.length > 0) {
16218
+ console.warn(
16219
+ "Code functions cannot be published directly. Use `braintrust push` instead."
16220
+ );
16221
+ }
16222
+ if (this._publishablePrompts.length > 0) {
16223
+ for (const prompt of this._publishablePrompts) {
16224
+ const functionDefinition = await prompt.toFunctionDefinition(projectMap);
16225
+ functionDefinitions.push(functionDefinition);
16226
+ }
16227
+ }
16228
+ await _internalGetGlobalState().apiConn().post_json("insert-functions", {
16229
+ functions: functionDefinitions
16230
+ });
16231
+ }
16232
+ }, _class20);
16233
+ var ToolBuilder = (_class21 = class {
16234
+ constructor(project) {;_class21.prototype.__init73.call(this);
16235
+ this.project = project;
16236
+ }
16237
+ __init73() {this.taskCounter = 0}
16238
+ // This type definition is just a catch all so that the implementation can be
16239
+ // less specific than the two more specific declarations above.
16240
+ create(opts) {
16241
+ this.taskCounter++;
16242
+ opts = _nullishCoalesce(opts, () => ( {}));
16243
+ const { handler, name, slug, parameters, returns, ...rest } = opts;
16244
+ let resolvedName = _nullishCoalesce(name, () => ( handler.name));
16245
+ if (resolvedName.trim().length === 0) {
16246
+ resolvedName = `Tool ${isomorph_default.basename(currentFilename)} ${this.taskCounter}`;
16247
+ }
16248
+ const tool = new CodeFunction(this.project, {
16249
+ handler,
16250
+ name: resolvedName,
16251
+ slug: _nullishCoalesce(slug, () => ( slugify(resolvedName, { lower: true, strict: true }))),
16252
+ type: "tool",
16253
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
16254
+ parameters,
16255
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
16256
+ returns,
16257
+ ...rest
16258
+ });
16259
+ this.project.addCodeFunction(tool);
16260
+ return tool;
16261
+ }
16262
+ }, _class21);
16263
+ var ScorerBuilder = (_class22 = class {
16264
+ constructor(project) {;_class22.prototype.__init74.call(this);
16265
+ this.project = project;
16266
+ }
16267
+ __init74() {this.taskCounter = 0}
16268
+ create(opts) {
16269
+ this.taskCounter++;
16270
+ let resolvedName = opts.name;
16271
+ if (!resolvedName && "handler" in opts) {
16272
+ resolvedName = opts.handler.name;
16273
+ }
16274
+ if (!resolvedName || resolvedName.trim().length === 0) {
16275
+ resolvedName = `Scorer ${isomorph_default.basename(currentFilename)} ${this.taskCounter}`;
16276
+ }
16277
+ const slug = _nullishCoalesce(opts.slug, () => ( slugify(resolvedName, { lower: true, strict: true })));
16278
+ if ("handler" in opts) {
16279
+ const scorer = new CodeFunction(this.project, {
16280
+ ...opts,
16281
+ name: resolvedName,
16282
+ slug,
16283
+ type: "scorer"
16284
+ });
16285
+ this.project.addCodeFunction(scorer);
16286
+ } else {
16287
+ const promptBlock = "messages" in opts ? {
16288
+ type: "chat",
16289
+ messages: opts.messages
16290
+ } : {
16291
+ type: "completion",
16292
+ content: opts.prompt
16293
+ };
16294
+ const promptData = {
16295
+ prompt: promptBlock,
16296
+ options: {
16297
+ model: opts.model,
16298
+ params: opts.params
16299
+ },
16300
+ parser: {
16301
+ type: "llm_classifier",
16302
+ use_cot: opts.useCot,
16303
+ choice_scores: opts.choiceScores
16304
+ }
16305
+ };
16306
+ const codePrompt = new CodePrompt(
16307
+ this.project,
16308
+ promptData,
16309
+ [],
16310
+ {
16311
+ ...opts,
16312
+ name: resolvedName,
16313
+ slug
16314
+ },
16315
+ "scorer"
16316
+ );
16317
+ this.project.addPrompt(codePrompt);
16318
+ }
16319
+ }
16320
+ }, _class22);
16321
+ var CodeFunction = class {
16322
+ constructor(project, opts) {
16323
+ this.project = project;
16324
+ this.handler = opts.handler;
16325
+ this.name = opts.name;
16326
+ this.slug = opts.slug;
16327
+ this.description = opts.description;
16328
+ this.type = opts.type;
16329
+ this.ifExists = opts.ifExists;
16330
+ this.metadata = opts.metadata;
16331
+ this.parameters = opts.parameters;
16332
+ this.returns = opts.returns;
16333
+ if (this.returns && !this.parameters) {
16334
+ throw new Error("parameters are required if return type is defined");
16335
+ }
16336
+ }
16337
+
16338
+
16339
+
16340
+
16341
+
16342
+
16343
+
16344
+
16345
+
16346
+ key() {
16347
+ return JSON.stringify([
16348
+ _nullishCoalesce(this.project.id, () => ( "")),
16349
+ _nullishCoalesce(this.project.name, () => ( "")),
16350
+ this.slug
16351
+ ]);
16352
+ }
16353
+ };
16354
+ var CodePrompt = class {
16355
+
16356
+
16357
+
16358
+
16359
+
16360
+
16361
+
16362
+
16363
+
16364
+
16365
+ constructor(project, prompt, toolFunctions, opts, functionType) {
16366
+ this.project = project;
16367
+ this.name = opts.name;
16368
+ this.slug = opts.slug;
16369
+ this.prompt = prompt;
16370
+ this.toolFunctions = toolFunctions;
16371
+ this.ifExists = opts.ifExists;
16372
+ this.description = opts.description;
16373
+ this.id = opts.id;
16374
+ this.functionType = functionType;
16375
+ this.metadata = opts.metadata;
16376
+ }
16377
+ async toFunctionDefinition(projectNameToId) {
16378
+ const prompt_data = {
16379
+ ...this.prompt
16380
+ };
16381
+ if (this.toolFunctions.length > 0) {
16382
+ const resolvableToolFunctions = await Promise.all(
16383
+ this.toolFunctions.map(async (fn) => {
16384
+ if ("slug" in fn) {
16385
+ return {
16386
+ type: "slug",
16387
+ project_id: await projectNameToId.resolve(fn.project),
16388
+ slug: fn.slug
16389
+ };
16390
+ } else {
16391
+ return fn;
16392
+ }
16393
+ })
16394
+ );
16395
+ prompt_data.tool_functions = // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
16396
+ resolvableToolFunctions;
16397
+ }
16398
+ return {
16399
+ project_id: await projectNameToId.resolve(this.project),
16400
+ name: this.name,
16401
+ slug: this.slug,
16402
+ description: _nullishCoalesce(this.description, () => ( "")),
16403
+ function_data: {
16404
+ type: "prompt"
16405
+ },
16406
+ function_type: this.functionType,
16407
+ prompt_data,
16408
+ if_exists: this.ifExists,
16409
+ metadata: this.metadata
16410
+ };
16411
+ }
16412
+ };
16413
+ var PromptBuilder = class {
16414
+ constructor(project) {
16415
+ this.project = project;
16416
+ }
16417
+ create(opts) {
16418
+ const toolFunctions = [];
16419
+ const rawTools = [];
16420
+ for (const tool of _nullishCoalesce(opts.tools, () => ( []))) {
16421
+ if (tool instanceof CodeFunction) {
16422
+ toolFunctions.push(tool);
16423
+ } else if ("type" in tool && !("function" in tool)) {
16424
+ toolFunctions.push(tool);
16425
+ } else {
16426
+ rawTools.push(tool);
16427
+ }
16428
+ }
16429
+ const slug = _nullishCoalesce(opts.slug, () => ( slugify(opts.name, { lower: true, strict: true })));
16430
+ const promptData = promptDefinitionToPromptData(opts, rawTools);
16431
+ const promptRow = {
16432
+ id: opts.id,
16433
+ _xact_id: opts.version ? loadPrettyXact(opts.version) : void 0,
16434
+ name: opts.name,
16435
+ slug,
16436
+ prompt_data: promptData,
16437
+ ...this.project.id !== void 0 ? { project_id: this.project.id } : {}
16438
+ };
16439
+ const prompt = new Prompt2(
16440
+ promptRow,
16441
+ {},
16442
+ // It doesn't make sense to specify defaults here.
16443
+ _nullishCoalesce(opts.noTrace, () => ( false))
16444
+ );
16445
+ const codePrompt = new CodePrompt(this.project, promptData, toolFunctions, {
16446
+ ...opts,
16447
+ slug
16448
+ });
16449
+ this.project.addPrompt(codePrompt);
16450
+ return prompt;
16451
+ }
16452
+ };
16453
+ var CodeParameters = class {
16454
+
16455
+
16456
+
16457
+
16458
+
16459
+
16460
+
16461
+ constructor(project, opts) {
16462
+ this.project = project;
16463
+ this.name = opts.name;
16464
+ this.slug = opts.slug;
16465
+ this.description = opts.description;
16466
+ this.schema = opts.schema;
16467
+ this.ifExists = opts.ifExists;
16468
+ this.metadata = opts.metadata;
16469
+ }
16470
+ async toFunctionDefinition(projectNameToId) {
16471
+ return {
16472
+ project_id: await projectNameToId.resolve(this.project),
16473
+ name: this.name,
16474
+ slug: this.slug,
16475
+ description: _nullishCoalesce(this.description, () => ( "")),
16476
+ function_type: "parameters",
16477
+ function_data: {
16478
+ type: "parameters",
16479
+ data: {},
16480
+ __schema: serializeEvalParameterstoParametersSchema(this.schema)
16481
+ },
16482
+ if_exists: this.ifExists,
16483
+ metadata: this.metadata
16484
+ };
16485
+ }
16486
+ };
16487
+ var ParametersBuilder = class {
16488
+ constructor(project) {
16489
+ this.project = project;
16490
+ }
16491
+ create(opts) {
16492
+ const slug = _nullishCoalesce(opts.slug, () => ( slugify(opts.name, { lower: true, strict: true })));
16493
+ const codeParameters = new CodeParameters(this.project, {
16494
+ name: opts.name,
16495
+ slug,
16496
+ description: opts.description,
16497
+ schema: opts.schema,
16498
+ ifExists: opts.ifExists,
16499
+ metadata: opts.metadata
16500
+ });
16501
+ this.project.addParameters(codeParameters);
16502
+ return opts.schema;
16503
+ }
16504
+ };
16505
+ function serializeEvalParameterstoParametersSchema(parameters) {
16506
+ const properties = {};
16507
+ const required = [];
16508
+ for (const [name, value] of Object.entries(parameters)) {
16509
+ if ("type" in value && value.type === "prompt") {
16510
+ const defaultPromptData = value.default ? promptDefinitionToPromptData(value.default) : void 0;
16511
+ properties[name] = {
16512
+ type: "object",
16513
+ "x-bt-type": "prompt",
16514
+ ...value.description ? { description: value.description } : {},
16515
+ ...defaultPromptData ? { default: defaultPromptData } : {}
16516
+ };
16517
+ if (!defaultPromptData) {
16518
+ required.push(name);
16519
+ }
16520
+ } else {
16521
+ const schemaObj = zodToJsonSchema(value);
16522
+ properties[name] = schemaObj;
16523
+ if (!("default" in schemaObj)) {
16524
+ required.push(name);
16525
+ }
16526
+ }
16527
+ }
16528
+ return {
16529
+ type: "object",
16530
+ properties,
16531
+ ...required.length > 0 ? { required } : {},
16532
+ additionalProperties: true
16533
+ };
16534
+ }
16535
+ var ProjectNameIdMap = (_class23 = class {constructor() { _class23.prototype.__init75.call(this);_class23.prototype.__init76.call(this); }
16536
+ __init75() {this.nameToId = {}}
16537
+ __init76() {this.idToName = {}}
16538
+ async getId(projectName) {
16539
+ if (!(projectName in this.nameToId)) {
16540
+ const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
16541
+ project_name: projectName
16542
+ });
16543
+ const result = _v3.z.object({
16544
+ project: Project
16545
+ }).parse(response);
16546
+ const projectId = result.project.id;
16547
+ this.nameToId[projectName] = projectId;
16548
+ this.idToName[projectId] = projectName;
16549
+ }
16550
+ return this.nameToId[projectName];
16551
+ }
16552
+ async getName(projectId) {
16553
+ if (!(projectId in this.idToName)) {
16554
+ const response = await _internalGetGlobalState().appConn().post_json("api/project/get", {
16555
+ id: projectId
16556
+ });
16557
+ const result = _v3.z.array(Project).nonempty().parse(response);
16558
+ const projectName = result[0].name;
16559
+ this.idToName[projectId] = projectName;
16560
+ this.nameToId[projectName] = projectId;
16561
+ }
16562
+ return this.idToName[projectId];
16563
+ }
16564
+ async resolve(project) {
16565
+ if (project.id) {
16566
+ return project.id;
16567
+ }
16568
+ return this.getId(project.name);
16569
+ }
16570
+ }, _class23);
16571
+
15641
16572
  // dev/types.ts
15642
16573
 
15643
16574
  var evalBodySchema = _v3.z.object({
@@ -15655,7 +16586,7 @@ var evalBodySchema = _v3.z.object({
15655
16586
  parent: InvokeParent.optional(),
15656
16587
  stream: _v3.z.boolean().optional()
15657
16588
  });
15658
- var evalParametersSerializedSchema = _v3.z.record(
16589
+ var staticParametersSchema = _v3.z.record(
15659
16590
  _v3.z.string(),
15660
16591
  _v3.z.union([
15661
16592
  _v3.z.object({
@@ -15666,14 +16597,43 @@ var evalParametersSerializedSchema = _v3.z.record(
15666
16597
  _v3.z.object({
15667
16598
  type: _v3.z.literal("data"),
15668
16599
  schema: _v3.z.record(_v3.z.unknown()),
15669
- // JSON Schema
15670
16600
  default: _v3.z.unknown().optional(),
15671
16601
  description: _v3.z.string().optional()
15672
16602
  })
15673
16603
  ])
15674
16604
  );
16605
+ var parametersSchema = _v3.z.object({
16606
+ type: _v3.z.literal("object"),
16607
+ properties: _v3.z.record(_v3.z.string(), _v3.z.record(_v3.z.unknown())),
16608
+ required: _v3.z.array(_v3.z.string()).optional(),
16609
+ additionalProperties: _v3.z.boolean().optional()
16610
+ });
16611
+ var parametersSourceSchema = _v3.z.object({
16612
+ parametersId: _v3.z.string().optional(),
16613
+ slug: _v3.z.string(),
16614
+ name: _v3.z.string(),
16615
+ projectId: _v3.z.string().optional(),
16616
+ version: _v3.z.string().optional()
16617
+ });
16618
+ var parametersContainerSchema = _v3.z.object({
16619
+ type: _v3.z.literal("braintrust.parameters"),
16620
+ schema: parametersSchema,
16621
+ source: parametersSourceSchema
16622
+ });
16623
+ var staticParametersContainerSchema = _v3.z.object({
16624
+ type: _v3.z.literal("braintrust.staticParameters"),
16625
+ schema: staticParametersSchema,
16626
+ source: _v3.z.null()
16627
+ });
16628
+ var serializedParametersContainerSchema = _v3.z.union([
16629
+ parametersContainerSchema,
16630
+ staticParametersContainerSchema,
16631
+ // keeping this type here since old versions of the SDK will still pass the unwrapped schema and we need to handle this in the app
16632
+ staticParametersSchema
16633
+ ]);
15675
16634
  var evaluatorDefinitionSchema = _v3.z.object({
15676
- parameters: evalParametersSerializedSchema.optional()
16635
+ parameters: serializedParametersContainerSchema.optional(),
16636
+ scores: _v3.z.array(_v3.z.object({ name: _v3.z.string() })).optional()
15677
16637
  });
15678
16638
  var evaluatorDefinitionsSchema = _v3.z.record(
15679
16639
  _v3.z.string(),
@@ -15799,4 +16759,13 @@ configureBrowser();
15799
16759
 
15800
16760
 
15801
16761
 
15802
- exports.Attachment = Attachment; exports.AttachmentReference = AttachmentReference; exports.BaseAttachment = BaseAttachment; exports.BaseExperiment = BaseExperiment; exports.BraintrustMiddleware = BraintrustMiddleware; exports.BraintrustState = BraintrustState; exports.BraintrustStream = BraintrustStream; exports.CachedSpanFetcher = CachedSpanFetcher; exports.CodeFunction = CodeFunction; exports.CodePrompt = CodePrompt; exports.ContextManager = ContextManager; exports.DEFAULT_FETCH_BATCH_SIZE = DEFAULT_FETCH_BATCH_SIZE; exports.Dataset = Dataset2; exports.ERR_PERMALINK = ERR_PERMALINK; exports.Eval = Eval; exports.EvalResultWithSummary = EvalResultWithSummary; exports.Experiment = Experiment2; exports.ExternalAttachment = ExternalAttachment; exports.FailedHTTPResponse = FailedHTTPResponse; exports.IDGenerator = IDGenerator; exports.JSONAttachment = JSONAttachment; exports.LEGACY_CACHED_HEADER = LEGACY_CACHED_HEADER; exports.LazyValue = LazyValue; exports.Logger = Logger; exports.LoginInvalidOrgError = LoginInvalidOrgError; exports.NOOP_SPAN = NOOP_SPAN; exports.NOOP_SPAN_PERMALINK = NOOP_SPAN_PERMALINK; exports.NoopSpan = NoopSpan; exports.ObjectFetcher = ObjectFetcher; exports.Project = Project2; exports.ProjectNameIdMap = ProjectNameIdMap; exports.Prompt = Prompt2; exports.PromptBuilder = PromptBuilder; exports.ReadonlyAttachment = ReadonlyAttachment; exports.ReadonlyExperiment = ReadonlyExperiment; exports.Reporter = Reporter; exports.ScorerBuilder = ScorerBuilder; exports.SpanFetcher = SpanFetcher; exports.SpanImpl = SpanImpl; exports.TestBackgroundLogger = TestBackgroundLogger; exports.ToolBuilder = ToolBuilder; exports.UUIDGenerator = UUIDGenerator; exports.X_CACHED_HEADER = X_CACHED_HEADER; exports._exportsForTestingOnly = _exportsForTestingOnly; exports._internalGetGlobalState = _internalGetGlobalState; exports._internalSetInitialState = _internalSetInitialState; exports.braintrustStreamChunkSchema = braintrustStreamChunkSchema; exports.buildLocalSummary = buildLocalSummary; exports.createFinalValuePassThroughStream = createFinalValuePassThroughStream; exports.currentExperiment = currentExperiment; exports.currentLogger = currentLogger; exports.currentSpan = currentSpan; exports.deepCopyEvent = deepCopyEvent; exports.default = exports_exports; exports.defaultErrorScoreHandler = defaultErrorScoreHandler; exports.deserializePlainStringAsJSON = deserializePlainStringAsJSON; exports.devNullWritableStream = devNullWritableStream; exports.evaluatorDefinitionSchema = evaluatorDefinitionSchema; exports.evaluatorDefinitionsSchema = evaluatorDefinitionsSchema; exports.flush = flush; exports.getContextManager = getContextManager; exports.getIdGenerator = getIdGenerator; exports.getPromptVersions = getPromptVersions; exports.getSpanParentObject = getSpanParentObject; exports.graph = graph_framework_exports; exports.init = init; exports.initDataset = initDataset; exports.initExperiment = initExperiment; exports.initFunction = initFunction; exports.initLogger = initLogger; exports.invoke = invoke; exports.isTemplateFormat = isTemplateFormat; exports.loadPrompt = loadPrompt; exports.log = log; exports.logError = logError; exports.login = login; exports.loginToState = loginToState; exports.newId = newId; exports.parseCachedHeader = parseCachedHeader; exports.parseTemplateFormat = parseTemplateFormat; exports.permalink = permalink; exports.projects = projects; exports.promptContentsSchema = promptContentsSchema; exports.promptDefinitionSchema = promptDefinitionSchema; exports.promptDefinitionToPromptData = promptDefinitionToPromptData; exports.promptDefinitionWithToolsSchema = promptDefinitionWithToolsSchema; exports.registerOtelFlush = registerOtelFlush; exports.renderMessage = renderMessage; exports.renderPromptParams = renderPromptParams; exports.renderTemplateContent = renderTemplateContent; exports.reportFailures = reportFailures; exports.runEvaluator = runEvaluator; exports.setFetch = setFetch; exports.setMaskingFunction = setMaskingFunction; exports.spanComponentsToObjectId = spanComponentsToObjectId; exports.startSpan = startSpan; exports.summarize = summarize; exports.toolFunctionDefinitionSchema = ToolFunctionDefinition; exports.traceable = traceable; exports.traced = traced; exports.updateSpan = updateSpan; exports.withCurrent = withCurrent; exports.withDataset = withDataset; exports.withExperiment = withExperiment; exports.withLogger = withLogger; exports.withParent = withParent; exports.wrapAISDK = wrapAISDK; exports.wrapAISDKModel = wrapAISDKModel; exports.wrapAgentClass = wrapAgentClass; exports.wrapAnthropic = wrapAnthropic; exports.wrapClaudeAgentSDK = wrapClaudeAgentSDK; exports.wrapGoogleGenAI = wrapGoogleGenAI; exports.wrapMastraAgent = wrapMastraAgent; exports.wrapOpenAI = wrapOpenAI; exports.wrapOpenAIv4 = wrapOpenAIv4; exports.wrapTraced = wrapTraced;
16762
+
16763
+
16764
+
16765
+
16766
+
16767
+
16768
+
16769
+
16770
+
16771
+ exports.Attachment = Attachment; exports.AttachmentReference = AttachmentReference; exports.BaseAttachment = BaseAttachment; exports.BaseExperiment = BaseExperiment; exports.BraintrustMiddleware = BraintrustMiddleware; exports.BraintrustState = BraintrustState; exports.BraintrustStream = BraintrustStream; exports.CachedSpanFetcher = CachedSpanFetcher; exports.CodeFunction = CodeFunction; exports.CodePrompt = CodePrompt; exports.ContextManager = ContextManager; exports.DEFAULT_FETCH_BATCH_SIZE = DEFAULT_FETCH_BATCH_SIZE; exports.DEFAULT_MAX_REQUEST_SIZE = DEFAULT_MAX_REQUEST_SIZE; exports.Dataset = Dataset2; exports.ERR_PERMALINK = ERR_PERMALINK; exports.Eval = Eval; exports.EvalResultWithSummary = EvalResultWithSummary; exports.Experiment = Experiment2; exports.ExternalAttachment = ExternalAttachment; exports.FailedHTTPResponse = FailedHTTPResponse; exports.IDGenerator = IDGenerator; exports.JSONAttachment = JSONAttachment; exports.LEGACY_CACHED_HEADER = LEGACY_CACHED_HEADER; exports.LOGS3_OVERFLOW_REFERENCE_TYPE = LOGS3_OVERFLOW_REFERENCE_TYPE; exports.LazyValue = LazyValue; exports.Logger = Logger; exports.LoginInvalidOrgError = LoginInvalidOrgError; exports.NOOP_SPAN = NOOP_SPAN; exports.NOOP_SPAN_PERMALINK = NOOP_SPAN_PERMALINK; exports.NoopSpan = NoopSpan; exports.ObjectFetcher = ObjectFetcher; exports.Project = Project2; exports.ProjectNameIdMap = ProjectNameIdMap; exports.Prompt = Prompt2; exports.PromptBuilder = PromptBuilder; exports.ReadonlyAttachment = ReadonlyAttachment; exports.ReadonlyExperiment = ReadonlyExperiment; exports.Reporter = Reporter; exports.ScorerBuilder = ScorerBuilder; exports.SpanFetcher = SpanFetcher; exports.SpanImpl = SpanImpl; exports.TestBackgroundLogger = TestBackgroundLogger; exports.ToolBuilder = ToolBuilder; exports.UUIDGenerator = UUIDGenerator; exports.X_CACHED_HEADER = X_CACHED_HEADER; exports._exportsForTestingOnly = _exportsForTestingOnly; exports._internalGetGlobalState = _internalGetGlobalState; exports._internalSetInitialState = _internalSetInitialState; exports.addAzureBlobHeaders = addAzureBlobHeaders; exports.braintrustStreamChunkSchema = braintrustStreamChunkSchema; exports.buildLocalSummary = buildLocalSummary; exports.constructLogs3OverflowRequest = constructLogs3OverflowRequest; exports.createFinalValuePassThroughStream = createFinalValuePassThroughStream; exports.currentExperiment = currentExperiment; exports.currentLogger = currentLogger; exports.currentSpan = currentSpan; exports.deepCopyEvent = deepCopyEvent; exports.default = exports_exports; exports.defaultErrorScoreHandler = defaultErrorScoreHandler; exports.deserializePlainStringAsJSON = deserializePlainStringAsJSON; exports.devNullWritableStream = devNullWritableStream; exports.evaluatorDefinitionSchema = evaluatorDefinitionSchema; exports.evaluatorDefinitionsSchema = evaluatorDefinitionsSchema; exports.flush = flush; exports.getContextManager = getContextManager; exports.getIdGenerator = getIdGenerator; exports.getPromptVersions = getPromptVersions; exports.getSpanParentObject = getSpanParentObject; exports.graph = graph_framework_exports; exports.init = init; exports.initDataset = initDataset; exports.initExperiment = initExperiment; exports.initFunction = initFunction; exports.initLogger = initLogger; exports.invoke = invoke; exports.isTemplateFormat = isTemplateFormat; exports.loadParameters = loadParameters; exports.loadPrompt = loadPrompt; exports.log = log; exports.logError = logError; exports.login = login; exports.loginToState = loginToState; exports.logs3OverflowUploadSchema = logs3OverflowUploadSchema; exports.newId = newId; exports.parseCachedHeader = parseCachedHeader; exports.parseTemplateFormat = parseTemplateFormat; exports.permalink = permalink; exports.pickLogs3OverflowObjectIds = pickLogs3OverflowObjectIds; exports.projects = projects; exports.promptContentsSchema = promptContentsSchema; exports.promptDefinitionSchema = promptDefinitionSchema; exports.promptDefinitionToPromptData = promptDefinitionToPromptData; exports.promptDefinitionWithToolsSchema = promptDefinitionWithToolsSchema; exports.registerOtelFlush = registerOtelFlush; exports.renderMessage = renderMessage; exports.renderPromptParams = renderPromptParams; exports.renderTemplateContent = renderTemplateContent; exports.reportFailures = reportFailures; exports.runEvaluator = runEvaluator; exports.setFetch = setFetch; exports.setMaskingFunction = setMaskingFunction; exports.spanComponentsToObjectId = spanComponentsToObjectId; exports.startSpan = startSpan; exports.summarize = summarize; exports.toolFunctionDefinitionSchema = ToolFunctionDefinition; exports.traceable = traceable; exports.traced = traced; exports.updateSpan = updateSpan; exports.uploadLogs3OverflowPayload = uploadLogs3OverflowPayload; exports.utf8ByteLength = utf8ByteLength; exports.withCurrent = withCurrent; exports.withDataset = withDataset; exports.withExperiment = withExperiment; exports.withLogger = withLogger; exports.withParent = withParent; exports.wrapAISDK = wrapAISDK; exports.wrapAISDKModel = wrapAISDKModel; exports.wrapAgentClass = wrapAgentClass; exports.wrapAnthropic = wrapAnthropic; exports.wrapClaudeAgentSDK = wrapClaudeAgentSDK; exports.wrapGoogleGenAI = wrapGoogleGenAI; exports.wrapMastraAgent = wrapMastraAgent; exports.wrapOpenAI = wrapOpenAI; exports.wrapOpenAIv4 = wrapOpenAIv4; exports.wrapTraced = wrapTraced;