braintrust 0.3.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -87,6 +87,44 @@ var Queue = (_class = class {
87
87
  }
88
88
  }, _class);
89
89
 
90
+ // src/id-gen.ts
91
+
92
+ function generateHexId(bytes) {
93
+ let result = "";
94
+ for (let i = 0; i < bytes; i++) {
95
+ result += Math.floor(Math.random() * 256).toString(16).padStart(2, "0");
96
+ }
97
+ return result;
98
+ }
99
+ var IDGenerator = class {
100
+ };
101
+ var UUIDGenerator = class extends IDGenerator {
102
+ getSpanId() {
103
+ return _uuid.v4.call(void 0, );
104
+ }
105
+ getTraceId() {
106
+ return _uuid.v4.call(void 0, );
107
+ }
108
+ shareRootSpanId() {
109
+ return true;
110
+ }
111
+ };
112
+ var OTELIDGenerator = class extends IDGenerator {
113
+ getSpanId() {
114
+ return generateHexId(8);
115
+ }
116
+ getTraceId() {
117
+ return generateHexId(16);
118
+ }
119
+ shareRootSpanId() {
120
+ return false;
121
+ }
122
+ };
123
+ function getIdGenerator() {
124
+ const useOtel = typeof process !== "undefined" && _optionalChain([process, 'access', _2 => _2.env, 'optionalAccess', _3 => _3.BRAINTRUST_OTEL_COMPAT, 'optionalAccess', _4 => _4.toLowerCase, 'call', _5 => _5()]) === "true";
125
+ return useOtel ? new OTELIDGenerator() : new UUIDGenerator();
126
+ }
127
+
90
128
  // util/db_fields.ts
91
129
  var TRANSACTION_ID_FIELD = "_xact_id";
92
130
  var IS_MERGE_FIELD = "_is_merge";
@@ -237,7 +275,7 @@ var SpanComponentsV1 = class _SpanComponentsV1 {
237
275
  return {
238
276
  objectType: this.objectType,
239
277
  objectId: this.objectId,
240
- rowIds: _optionalChain([this, 'access', _2 => _2.rowIds, 'optionalAccess', _3 => _3.toObject, 'call', _4 => _4()])
278
+ rowIds: _optionalChain([this, 'access', _6 => _6.rowIds, 'optionalAccess', _7 => _7.toObject, 'call', _8 => _8()])
241
279
  };
242
280
  }
243
281
  };
@@ -451,7 +489,7 @@ var SpanComponentsV2 = class _SpanComponentsV2 {
451
489
  objectType: this.objectType,
452
490
  objectId: this.objectId,
453
491
  computeObjectMetadataArgs: this.computeObjectMetadataArgs,
454
- rowIds: _optionalChain([this, 'access', _5 => _5.rowIds, 'optionalAccess', _6 => _6.toObject, 'call', _7 => _7()])
492
+ rowIds: _optionalChain([this, 'access', _9 => _9.rowIds, 'optionalAccess', _10 => _10.toObject, 'call', _11 => _11()])
455
493
  };
456
494
  }
457
495
  };
@@ -783,14 +821,14 @@ function depthFirstSearch(args) {
783
821
  while (events.length) {
784
822
  const { eventType, vertex, extras } = events.pop();
785
823
  if (eventType === "last") {
786
- _optionalChain([lastVisitF, 'optionalCall', _8 => _8(vertex)]);
824
+ _optionalChain([lastVisitF, 'optionalCall', _12 => _12(vertex)]);
787
825
  continue;
788
826
  }
789
827
  if (firstVisitedVertices.has(vertex)) {
790
828
  continue;
791
829
  }
792
830
  firstVisitedVertices.add(vertex);
793
- _optionalChain([firstVisitF, 'optionalCall', _9 => _9(vertex, { parentVertex: extras.parentVertex })]);
831
+ _optionalChain([firstVisitF, 'optionalCall', _13 => _13(vertex, { parentVertex: extras.parentVertex })]);
794
832
  events.push({ eventType: "last", vertex, extras: {} });
795
833
  mapAt(graph, vertex).forEach((child) => {
796
834
  events.push({
@@ -812,7 +850,7 @@ function undirectedConnectedComponents(graph) {
812
850
  let labelCounter = 0;
813
851
  const vertexLabels = /* @__PURE__ */ new Map();
814
852
  const firstVisitF = (vertex, args) => {
815
- const label = _optionalChain([args, 'optionalAccess', _10 => _10.parentVertex]) !== void 0 ? mapAt(vertexLabels, _optionalChain([args, 'optionalAccess', _11 => _11.parentVertex])) : labelCounter++;
853
+ const label = _optionalChain([args, 'optionalAccess', _14 => _14.parentVertex]) !== void 0 ? mapAt(vertexLabels, _optionalChain([args, 'optionalAccess', _15 => _15.parentVertex])) : labelCounter++;
816
854
  vertexLabels.set(vertex, label);
817
855
  };
818
856
  depthFirstSearch({ graph: directedGraph, firstVisitF });
@@ -1034,6 +1072,41 @@ function _urljoin(...parts) {
1034
1072
  ).filter((x) => x.trim() !== "").join("/");
1035
1073
  }
1036
1074
 
1075
+ // util/span_identifier_v4.ts
1076
+
1077
+ var ENCODING_VERSION_NUMBER_V4 = 4;
1078
+ var INVALID_ENCODING_ERRMSG_V4 = `SpanComponents string is not properly encoded. This library only supports encoding versions up to ${ENCODING_VERSION_NUMBER_V4}. Please make sure the SDK library used to decode the SpanComponents is at least as new as any library used to encode it.`;
1079
+ var spanComponentsV4Schema = _v3.z.object({
1080
+ object_type: spanObjectTypeV3EnumSchema,
1081
+ propagated_event: _v3.z.record(_v3.z.unknown()).nullish()
1082
+ }).and(
1083
+ _v3.z.union([
1084
+ // Must provide one or the other.
1085
+ _v3.z.object({
1086
+ object_id: _v3.z.string().nullish(),
1087
+ compute_object_metadata_args: _v3.z.optional(_v3.z.null())
1088
+ }),
1089
+ _v3.z.object({
1090
+ object_id: _v3.z.optional(_v3.z.null()),
1091
+ compute_object_metadata_args: _v3.z.record(_v3.z.unknown())
1092
+ })
1093
+ ])
1094
+ ).and(
1095
+ _v3.z.union([
1096
+ // Either all of these must be provided or none.
1097
+ _v3.z.object({
1098
+ row_id: _v3.z.string(),
1099
+ span_id: _v3.z.string(),
1100
+ root_span_id: _v3.z.string()
1101
+ }),
1102
+ _v3.z.object({
1103
+ row_id: _v3.z.optional(_v3.z.null()),
1104
+ span_id: _v3.z.optional(_v3.z.null()),
1105
+ root_span_id: _v3.z.optional(_v3.z.null())
1106
+ })
1107
+ ])
1108
+ );
1109
+
1037
1110
  // util/git_fields.ts
1038
1111
  function mergeGitMetadataSettings(s1, s2) {
1039
1112
  if (s1.collect === "all") {
@@ -3034,7 +3107,7 @@ var NoopSpan = (_class4 = class {
3034
3107
  return this;
3035
3108
  }
3036
3109
  end(args) {
3037
- return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _12 => _12.endTime]), () => ( getCurrentUnixTimestamp()));
3110
+ return _nullishCoalesce(_optionalChain([args, 'optionalAccess', _16 => _16.endTime]), () => ( getCurrentUnixTimestamp()));
3038
3111
  }
3039
3112
  async export() {
3040
3113
  return "";
@@ -3087,7 +3160,7 @@ var loginSchema = _zod.z.strictObject({
3087
3160
  });
3088
3161
  var stateNonce = 0;
3089
3162
  var BraintrustState = (_class5 = class _BraintrustState {
3090
- constructor(loginParams) {;_class5.prototype.__init7.call(this);_class5.prototype.__init8.call(this);_class5.prototype.__init9.call(this);_class5.prototype.__init10.call(this);_class5.prototype.__init11.call(this);_class5.prototype.__init12.call(this);_class5.prototype.__init13.call(this);_class5.prototype.__init14.call(this);_class5.prototype.__init15.call(this);_class5.prototype.__init16.call(this);_class5.prototype.__init17.call(this);_class5.prototype.__init18.call(this);_class5.prototype.__init19.call(this);
3163
+ constructor(loginParams) {;_class5.prototype.__init7.call(this);_class5.prototype.__init8.call(this);_class5.prototype.__init9.call(this);_class5.prototype.__init10.call(this);_class5.prototype.__init11.call(this);_class5.prototype.__init12.call(this);_class5.prototype.__init13.call(this);_class5.prototype.__init14.call(this);_class5.prototype.__init15.call(this);_class5.prototype.__init16.call(this);_class5.prototype.__init17.call(this);_class5.prototype.__init18.call(this);_class5.prototype.__init19.call(this);_class5.prototype.__init20.call(this);
3091
3164
  this.loginParams = loginParams;
3092
3165
  this.id = `${(/* @__PURE__ */ new Date()).toLocaleString()}-${stateNonce++}`;
3093
3166
  this.currentExperiment = void 0;
@@ -3141,6 +3214,7 @@ var BraintrustState = (_class5 = class _BraintrustState {
3141
3214
  __init18() {this._apiConn = null}
3142
3215
  __init19() {this._proxyConn = null}
3143
3216
 
3217
+ __init20() {this._idGenerator = null}
3144
3218
  resetLoginInfo() {
3145
3219
  this.appUrl = null;
3146
3220
  this.appPublicUrl = null;
@@ -3155,6 +3229,15 @@ var BraintrustState = (_class5 = class _BraintrustState {
3155
3229
  this._apiConn = null;
3156
3230
  this._proxyConn = null;
3157
3231
  }
3232
+ resetIdGenState() {
3233
+ this._idGenerator = null;
3234
+ }
3235
+ get idGenerator() {
3236
+ if (this._idGenerator === null) {
3237
+ this._idGenerator = getIdGenerator();
3238
+ }
3239
+ return this._idGenerator;
3240
+ }
3158
3241
  copyLoginInfo(other) {
3159
3242
  this.appUrl = other.appUrl;
3160
3243
  this.appPublicUrl = other.appPublicUrl;
@@ -3222,8 +3305,8 @@ var BraintrustState = (_class5 = class _BraintrustState {
3222
3305
  setFetch(fetch2) {
3223
3306
  this.loginParams.fetch = fetch2;
3224
3307
  this.fetch = fetch2;
3225
- _optionalChain([this, 'access', _13 => _13._apiConn, 'optionalAccess', _14 => _14.setFetch, 'call', _15 => _15(fetch2)]);
3226
- _optionalChain([this, 'access', _16 => _16._appConn, 'optionalAccess', _17 => _17.setFetch, 'call', _18 => _18(fetch2)]);
3308
+ _optionalChain([this, 'access', _17 => _17._apiConn, 'optionalAccess', _18 => _18.setFetch, 'call', _19 => _19(fetch2)]);
3309
+ _optionalChain([this, 'access', _20 => _20._appConn, 'optionalAccess', _21 => _21.setFetch, 'call', _22 => _22(fetch2)]);
3227
3310
  }
3228
3311
  setMaskingFunction(maskingFunction) {
3229
3312
  this.bgLogger().setMaskingFunction(maskingFunction);
@@ -3335,7 +3418,7 @@ function useTestBackgroundLogger() {
3335
3418
  return logger;
3336
3419
  }
3337
3420
  function clearTestBackgroundLogger() {
3338
- _optionalChain([_internalGetGlobalState, 'call', _19 => _19(), 'optionalAccess', _20 => _20.setOverrideBgLogger, 'call', _21 => _21(null)]);
3421
+ _optionalChain([_internalGetGlobalState, 'call', _23 => _23(), 'optionalAccess', _24 => _24.setOverrideBgLogger, 'call', _25 => _25(null)]);
3339
3422
  }
3340
3423
  function initTestExperiment(experimentName, projectName) {
3341
3424
  setInitialTestState();
@@ -4008,9 +4091,9 @@ async function permalink(slug, opts) {
4008
4091
  if (slug === "") {
4009
4092
  return NOOP_SPAN_PERMALINK;
4010
4093
  }
4011
- const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _22 => _22.state]), () => ( _globalState));
4094
+ const state = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _26 => _26.state]), () => ( _globalState));
4012
4095
  const getOrgName = async () => {
4013
- if (_optionalChain([opts, 'optionalAccess', _23 => _23.orgName])) {
4096
+ if (_optionalChain([opts, 'optionalAccess', _27 => _27.orgName])) {
4014
4097
  return opts.orgName;
4015
4098
  }
4016
4099
  await state.login({});
@@ -4020,7 +4103,7 @@ async function permalink(slug, opts) {
4020
4103
  return state.orgName;
4021
4104
  };
4022
4105
  const getAppUrl = async () => {
4023
- if (_optionalChain([opts, 'optionalAccess', _24 => _24.appUrl])) {
4106
+ if (_optionalChain([opts, 'optionalAccess', _28 => _28.appUrl])) {
4024
4107
  return opts.appUrl;
4025
4108
  }
4026
4109
  await state.login({});
@@ -4107,8 +4190,8 @@ var Logger = (_class6 = class {
4107
4190
 
4108
4191
 
4109
4192
  // For type identification.
4110
- __init20() {this.kind = "logger"}
4111
- constructor(state, lazyMetadata, logOptions = {}) {;_class6.prototype.__init20.call(this);
4193
+ __init21() {this.kind = "logger"}
4194
+ constructor(state, lazyMetadata, logOptions = {}) {;_class6.prototype.__init21.call(this);
4112
4195
  this.lazyMetadata = lazyMetadata;
4113
4196
  this._asyncFlush = logOptions.asyncFlush;
4114
4197
  this.computeMetadataArgs = logOptions.computeMetadataArgs;
@@ -4150,7 +4233,7 @@ var Logger = (_class6 = class {
4150
4233
  * @returns The `id` of the logged event.
4151
4234
  */
4152
4235
  log(event, options) {
4153
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _25 => _25.allowConcurrentWithSpans])) {
4236
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _29 => _29.allowConcurrentWithSpans])) {
4154
4237
  throw new Error(
4155
4238
  "Cannot run toplevel `log` method while using spans. To log to the span, call `logger.traced` and then log with `span.log`"
4156
4239
  );
@@ -4218,12 +4301,12 @@ var Logger = (_class6 = class {
4218
4301
  state: this.state,
4219
4302
  ...startSpanParentArgs({
4220
4303
  state: this.state,
4221
- parent: _optionalChain([args, 'optionalAccess', _26 => _26.parent]),
4304
+ parent: _optionalChain([args, 'optionalAccess', _30 => _30.parent]),
4222
4305
  parentObjectType: this.parentObjectType(),
4223
4306
  parentObjectId: this.lazyId,
4224
4307
  parentComputeObjectMetadataArgs: this.computeMetadataArgs,
4225
- parentSpanIds: _optionalChain([args, 'optionalAccess', _27 => _27.parentSpanIds]),
4226
- propagatedEvent: _optionalChain([args, 'optionalAccess', _28 => _28.propagatedEvent])
4308
+ parentSpanIds: _optionalChain([args, 'optionalAccess', _31 => _31.parentSpanIds]),
4309
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _32 => _32.propagatedEvent])
4227
4310
  }),
4228
4311
  defaultRootType: "task" /* TASK */
4229
4312
  });
@@ -4297,9 +4380,9 @@ function constructLogs3Data(items) {
4297
4380
  function now() {
4298
4381
  return (/* @__PURE__ */ new Date()).getTime();
4299
4382
  }
4300
- var TestBackgroundLogger = (_class7 = class {constructor() { _class7.prototype.__init21.call(this);_class7.prototype.__init22.call(this); }
4301
- __init21() {this.items = []}
4302
- __init22() {this.maskingFunction = null}
4383
+ var TestBackgroundLogger = (_class7 = class {constructor() { _class7.prototype.__init22.call(this);_class7.prototype.__init23.call(this); }
4384
+ __init22() {this.items = []}
4385
+ __init23() {this.maskingFunction = null}
4303
4386
  log(items) {
4304
4387
  this.items.push(items);
4305
4388
  }
@@ -4353,26 +4436,26 @@ var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
4353
4436
  var HTTPBackgroundLogger = (_class8 = class _HTTPBackgroundLogger {
4354
4437
 
4355
4438
 
4356
- __init23() {this.activeFlush = Promise.resolve()}
4357
- __init24() {this.activeFlushResolved = true}
4358
- __init25() {this.activeFlushError = void 0}
4439
+ __init24() {this.activeFlush = Promise.resolve()}
4440
+ __init25() {this.activeFlushResolved = true}
4441
+ __init26() {this.activeFlushError = void 0}
4359
4442
 
4360
- __init26() {this.maskingFunction = null}
4361
- __init27() {this.syncFlush = false}
4443
+ __init27() {this.maskingFunction = null}
4444
+ __init28() {this.syncFlush = false}
4362
4445
  // 6 MB for the AWS lambda gateway (from our own testing).
4363
- __init28() {this.maxRequestSize = 6 * 1024 * 1024}
4364
- __init29() {this.defaultBatchSize = 100}
4365
- __init30() {this.numTries = 3}
4366
- __init31() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
4367
- __init32() {this.queueDropLoggingPeriod = 60}
4368
- __init33() {this.failedPublishPayloadsDir = void 0}
4369
- __init34() {this.allPublishPayloadsDir = void 0}
4370
- __init35() {this._disabled = false}
4371
- __init36() {this.queueDropLoggingState = {
4446
+ __init29() {this.maxRequestSize = 6 * 1024 * 1024}
4447
+ __init30() {this.defaultBatchSize = 100}
4448
+ __init31() {this.numTries = 3}
4449
+ __init32() {this.queueDropExceedingMaxsize = DEFAULT_QUEUE_SIZE}
4450
+ __init33() {this.queueDropLoggingPeriod = 60}
4451
+ __init34() {this.failedPublishPayloadsDir = void 0}
4452
+ __init35() {this.allPublishPayloadsDir = void 0}
4453
+ __init36() {this._disabled = false}
4454
+ __init37() {this.queueDropLoggingState = {
4372
4455
  numDropped: 0,
4373
4456
  lastLoggedTimestamp: 0
4374
4457
  }}
4375
- constructor(apiConn, opts) {;_class8.prototype.__init23.call(this);_class8.prototype.__init24.call(this);_class8.prototype.__init25.call(this);_class8.prototype.__init26.call(this);_class8.prototype.__init27.call(this);_class8.prototype.__init28.call(this);_class8.prototype.__init29.call(this);_class8.prototype.__init30.call(this);_class8.prototype.__init31.call(this);_class8.prototype.__init32.call(this);_class8.prototype.__init33.call(this);_class8.prototype.__init34.call(this);_class8.prototype.__init35.call(this);_class8.prototype.__init36.call(this);
4458
+ constructor(apiConn, opts) {;_class8.prototype.__init24.call(this);_class8.prototype.__init25.call(this);_class8.prototype.__init26.call(this);_class8.prototype.__init27.call(this);_class8.prototype.__init28.call(this);_class8.prototype.__init29.call(this);_class8.prototype.__init30.call(this);_class8.prototype.__init31.call(this);_class8.prototype.__init32.call(this);_class8.prototype.__init33.call(this);_class8.prototype.__init34.call(this);_class8.prototype.__init35.call(this);_class8.prototype.__init36.call(this);_class8.prototype.__init37.call(this);
4376
4459
  opts = _nullishCoalesce(opts, () => ( {}));
4377
4460
  this.apiConn = apiConn;
4378
4461
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -4461,7 +4544,7 @@ var HTTPBackgroundLogger = (_class8 = class _HTTPBackgroundLogger {
4461
4544
  this.queue.clear();
4462
4545
  return;
4463
4546
  }
4464
- const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _29 => _29.batchSize]), () => ( this.defaultBatchSize));
4547
+ const batchSize = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _33 => _33.batchSize]), () => ( this.defaultBatchSize));
4465
4548
  const wrappedItems = this.queue.drain();
4466
4549
  const [allItems, attachments] = await this.unwrapLazyValues(wrappedItems);
4467
4550
  if (allItems.length === 0) {
@@ -4710,10 +4793,10 @@ Error: ${errorText}`;
4710
4793
  } catch (err) {
4711
4794
  if (err instanceof AggregateError) {
4712
4795
  for (const e of err.errors) {
4713
- _optionalChain([this, 'access', _30 => _30.onFlushError, 'optionalCall', _31 => _31(e)]);
4796
+ _optionalChain([this, 'access', _34 => _34.onFlushError, 'optionalCall', _35 => _35(e)]);
4714
4797
  }
4715
4798
  } else {
4716
- _optionalChain([this, 'access', _32 => _32.onFlushError, 'optionalCall', _33 => _33(err)]);
4799
+ _optionalChain([this, 'access', _36 => _36.onFlushError, 'optionalCall', _37 => _37(err)]);
4717
4800
  }
4718
4801
  this.activeFlushError = err;
4719
4802
  } finally {
@@ -5326,24 +5409,24 @@ async function summarize(options = {}) {
5326
5409
  return await e.summarize(options);
5327
5410
  }
5328
5411
  function currentExperiment(options) {
5329
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _34 => _34.state]), () => ( _globalState));
5412
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _38 => _38.state]), () => ( _globalState));
5330
5413
  return state.currentExperiment;
5331
5414
  }
5332
5415
  function currentLogger(options) {
5333
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _35 => _35.state]), () => ( _globalState));
5334
- return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _36 => _36.asyncFlush]));
5416
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _39 => _39.state]), () => ( _globalState));
5417
+ return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _40 => _40.asyncFlush]));
5335
5418
  }
5336
5419
  function currentSpan(options) {
5337
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _37 => _37.state]), () => ( _globalState));
5420
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _41 => _41.state]), () => ( _globalState));
5338
5421
  return _nullishCoalesce(state.currentSpan.getStore(), () => ( NOOP_SPAN));
5339
5422
  }
5340
5423
  function getSpanParentObject(options) {
5341
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _38 => _38.state]), () => ( _globalState));
5424
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _42 => _42.state]), () => ( _globalState));
5342
5425
  const parentSpan = currentSpan({ state });
5343
5426
  if (!Object.is(parentSpan, NOOP_SPAN)) {
5344
5427
  return parentSpan;
5345
5428
  }
5346
- const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _39 => _39.parent]), () => ( state.currentParent.getStore()));
5429
+ const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _43 => _43.parent]), () => ( state.currentParent.getStore()));
5347
5430
  if (parentStr) return SpanComponentsV3.fromStr(parentStr);
5348
5431
  const experiment = currentExperiment();
5349
5432
  if (experiment) {
@@ -5372,7 +5455,7 @@ function traced(callback, args) {
5372
5455
  const { span, isSyncFlushLogger } = startSpanAndIsLogger(args);
5373
5456
  const ret = runCatchFinally(
5374
5457
  () => {
5375
- if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _40 => _40.setCurrent]), () => ( true))) {
5458
+ if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _44 => _44.setCurrent]), () => ( true))) {
5376
5459
  return withCurrent(span, callback);
5377
5460
  } else {
5378
5461
  return callback(span);
@@ -5384,7 +5467,7 @@ function traced(callback, args) {
5384
5467
  },
5385
5468
  () => span.end()
5386
5469
  );
5387
- if (_optionalChain([args, 'optionalAccess', _41 => _41.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _42 => _42.asyncFlush])) {
5470
+ if (_optionalChain([args, 'optionalAccess', _45 => _45.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _46 => _46.asyncFlush])) {
5388
5471
  return ret;
5389
5472
  } else {
5390
5473
  return (async () => {
@@ -5508,14 +5591,14 @@ function wrapTraced(fn, args) {
5508
5591
  };
5509
5592
  const hasExplicitInput = args && args.event && "input" in args.event && args.event.input !== void 0;
5510
5593
  const hasExplicitOutput = args && args.event && args.event.output !== void 0;
5511
- const noTraceIO = _optionalChain([args, 'optionalAccess', _43 => _43.noTraceIO]) || hasExplicitInput || hasExplicitOutput;
5594
+ const noTraceIO = _optionalChain([args, 'optionalAccess', _47 => _47.noTraceIO]) || hasExplicitInput || hasExplicitOutput;
5512
5595
  if (isGeneratorFunction(fn)) {
5513
5596
  return wrapTracedSyncGenerator(fn, spanArgs, !!noTraceIO);
5514
5597
  }
5515
5598
  if (isAsyncGeneratorFunction(fn)) {
5516
5599
  return wrapTracedAsyncGenerator(fn, spanArgs, !!noTraceIO);
5517
5600
  }
5518
- if (_optionalChain([args, 'optionalAccess', _44 => _44.asyncFlush])) {
5601
+ if (_optionalChain([args, 'optionalAccess', _48 => _48.asyncFlush])) {
5519
5602
  return (...fnArgs) => traced((span) => {
5520
5603
  if (!hasExplicitInput) {
5521
5604
  span.log({ input: fnArgs });
@@ -5553,17 +5636,17 @@ function startSpan(args) {
5553
5636
  return startSpanAndIsLogger(args).span;
5554
5637
  }
5555
5638
  async function flush(options) {
5556
- const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _45 => _45.state]), () => ( _globalState));
5639
+ const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _49 => _49.state]), () => ( _globalState));
5557
5640
  return await state.bgLogger().flush();
5558
5641
  }
5559
5642
  function setFetch(fetch2) {
5560
5643
  _globalState.setFetch(fetch2);
5561
5644
  }
5562
5645
  function startSpanAndIsLogger(args) {
5563
- const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _46 => _46.state]), () => ( _globalState));
5646
+ const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _50 => _50.state]), () => ( _globalState));
5564
5647
  const parentObject = getSpanParentObject({
5565
- asyncFlush: _optionalChain([args, 'optionalAccess', _47 => _47.asyncFlush]),
5566
- parent: _optionalChain([args, 'optionalAccess', _48 => _48.parent]),
5648
+ asyncFlush: _optionalChain([args, 'optionalAccess', _51 => _51.asyncFlush]),
5649
+ parent: _optionalChain([args, 'optionalAccess', _52 => _52.parent]),
5567
5650
  state
5568
5651
  });
5569
5652
  if (parentObject instanceof SpanComponentsV3) {
@@ -5580,14 +5663,14 @@ function startSpanAndIsLogger(args) {
5580
5663
  ),
5581
5664
  parentComputeObjectMetadataArgs: _nullishCoalesce(parentObject.data.compute_object_metadata_args, () => ( void 0)),
5582
5665
  parentSpanIds,
5583
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _49 => _49.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
5666
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _53 => _53.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
5584
5667
  (_nullishCoalesce(parentObject.data.propagated_event, () => ( void 0)))))
5585
5668
  });
5586
5669
  return {
5587
5670
  span,
5588
5671
  isSyncFlushLogger: parentObject.data.object_type === 2 /* PROJECT_LOGS */ && // Since there's no parent logger here, we're free to choose the async flush
5589
5672
  // behavior, and therefore propagate along whatever we get from the arguments
5590
- _optionalChain([args, 'optionalAccess', _50 => _50.asyncFlush]) === false
5673
+ _optionalChain([args, 'optionalAccess', _54 => _54.asyncFlush]) === false
5591
5674
  };
5592
5675
  } else {
5593
5676
  const span = parentObject.startSpan(args);
@@ -5776,10 +5859,10 @@ function extractAttachments(event, attachments) {
5776
5859
  event[key] = value.reference;
5777
5860
  continue;
5778
5861
  }
5779
- if (_optionalChain([value, 'optionalAccess', _51 => _51.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
5862
+ if (_optionalChain([value, 'optionalAccess', _55 => _55.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
5780
5863
  continue;
5781
5864
  }
5782
- if (_optionalChain([value, 'optionalAccess', _52 => _52.reference, 'optionalAccess', _53 => _53.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _54 => _54.uploader])) {
5865
+ if (_optionalChain([value, 'optionalAccess', _56 => _56.reference, 'optionalAccess', _57 => _57.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _58 => _58.uploader])) {
5783
5866
  const attachment = new Attachment({
5784
5867
  data: value.dataDebugString,
5785
5868
  filename: value.reference.filename,
@@ -5846,13 +5929,13 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
5846
5929
  var INTERNAL_BTQL_LIMIT = 1e3;
5847
5930
  var MAX_BTQL_ITERATIONS = 1e4;
5848
5931
  var ObjectFetcher = (_class9 = class {
5849
- constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class9.prototype.__init37.call(this);
5932
+ constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class9.prototype.__init38.call(this);
5850
5933
  this.objectType = objectType;
5851
5934
  this.pinnedVersion = pinnedVersion;
5852
5935
  this.mutateRecord = mutateRecord;
5853
5936
  this._internal_btql = _internal_btql;
5854
5937
  }
5855
- __init37() {this._fetchedData = void 0}
5938
+ __init38() {this._fetchedData = void 0}
5856
5939
  get id() {
5857
5940
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
5858
5941
  }
@@ -5917,7 +6000,7 @@ var ObjectFetcher = (_class9 = class {
5917
6000
  throw new Error("Too many BTQL iterations");
5918
6001
  }
5919
6002
  }
5920
- this._fetchedData = this.mutateRecord ? _optionalChain([data, 'optionalAccess', _55 => _55.map, 'call', _56 => _56(this.mutateRecord)]) : data;
6003
+ this._fetchedData = this.mutateRecord ? _optionalChain([data, 'optionalAccess', _59 => _59.map, 'call', _60 => _60(this.mutateRecord)]) : data;
5921
6004
  }
5922
6005
  return this._fetchedData || [];
5923
6006
  }
@@ -5948,9 +6031,9 @@ var Experiment2 = (_class10 = class extends ObjectFetcher {
5948
6031
 
5949
6032
 
5950
6033
  // For type identification.
5951
- __init38() {this.kind = "experiment"}
6034
+ __init39() {this.kind = "experiment"}
5952
6035
  constructor(state, lazyMetadata, dataset) {
5953
- super("experiment", void 0, (r) => enrichAttachments(r, state));_class10.prototype.__init38.call(this);;
6036
+ super("experiment", void 0, (r) => enrichAttachments(r, state));_class10.prototype.__init39.call(this);;
5954
6037
  this.lazyMetadata = lazyMetadata;
5955
6038
  this.dataset = dataset;
5956
6039
  this.lastStartTime = getCurrentUnixTimestamp();
@@ -5998,7 +6081,7 @@ var Experiment2 = (_class10 = class extends ObjectFetcher {
5998
6081
  * @returns The `id` of the logged event.
5999
6082
  */
6000
6083
  log(event, options) {
6001
- if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _57 => _57.allowConcurrentWithSpans])) {
6084
+ if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _61 => _61.allowConcurrentWithSpans])) {
6002
6085
  throw new Error(
6003
6086
  "Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
6004
6087
  );
@@ -6051,12 +6134,12 @@ var Experiment2 = (_class10 = class extends ObjectFetcher {
6051
6134
  state: this.state,
6052
6135
  ...startSpanParentArgs({
6053
6136
  state: this.state,
6054
- parent: _optionalChain([args, 'optionalAccess', _58 => _58.parent]),
6137
+ parent: _optionalChain([args, 'optionalAccess', _62 => _62.parent]),
6055
6138
  parentObjectType: this.parentObjectType(),
6056
6139
  parentObjectId: this.lazyId,
6057
6140
  parentComputeObjectMetadataArgs: void 0,
6058
6141
  parentSpanIds: void 0,
6059
- propagatedEvent: _optionalChain([args, 'optionalAccess', _59 => _59.propagatedEvent])
6142
+ propagatedEvent: _optionalChain([args, 'optionalAccess', _63 => _63.propagatedEvent])
6060
6143
  }),
6061
6144
  defaultRootType: "eval" /* EVAL */
6062
6145
  });
@@ -6257,8 +6340,8 @@ var SpanImpl = (_class11 = class _SpanImpl {
6257
6340
 
6258
6341
 
6259
6342
 
6260
- __init39() {this.kind = "span"}
6261
- constructor(args) {;_class11.prototype.__init39.call(this);
6343
+ __init40() {this.kind = "span"}
6344
+ constructor(args) {;_class11.prototype.__init40.call(this);
6262
6345
  this._state = args.state;
6263
6346
  const spanAttributes = _nullishCoalesce(args.spanAttributes, () => ( {}));
6264
6347
  const rawEvent = _nullishCoalesce(args.event, () => ( {}));
@@ -6298,13 +6381,17 @@ var SpanImpl = (_class11 = class _SpanImpl {
6298
6381
  },
6299
6382
  created: (/* @__PURE__ */ new Date()).toISOString()
6300
6383
  };
6301
- this._id = _nullishCoalesce(eventId, () => ( _uuid.v4.call(void 0, )));
6302
- this._spanId = _nullishCoalesce(args.spanId, () => ( _uuid.v4.call(void 0, )));
6384
+ this._id = _nullishCoalesce(eventId, () => ( this._state.idGenerator.getSpanId()));
6385
+ this._spanId = _nullishCoalesce(args.spanId, () => ( this._state.idGenerator.getSpanId()));
6303
6386
  if (args.parentSpanIds) {
6304
6387
  this._rootSpanId = args.parentSpanIds.rootSpanId;
6305
6388
  this._spanParents = "parentSpanIds" in args.parentSpanIds ? args.parentSpanIds.parentSpanIds : [args.parentSpanIds.spanId];
6306
6389
  } else {
6307
- this._rootSpanId = this._spanId;
6390
+ if (this._state.idGenerator.shareRootSpanId()) {
6391
+ this._rootSpanId = this._spanId;
6392
+ } else {
6393
+ this._rootSpanId = this._state.idGenerator.getTraceId();
6394
+ }
6308
6395
  this._spanParents = void 0;
6309
6396
  }
6310
6397
  this.isMerge = false;
@@ -6348,10 +6435,10 @@ var SpanImpl = (_class11 = class _SpanImpl {
6348
6435
  ...serializableInternalData,
6349
6436
  [IS_MERGE_FIELD]: this.isMerge
6350
6437
  });
6351
- if (_optionalChain([partialRecord, 'access', _60 => _60.metrics, 'optionalAccess', _61 => _61.end])) {
6352
- this.loggedEndTime = _optionalChain([partialRecord, 'access', _62 => _62.metrics, 'optionalAccess', _63 => _63.end]);
6438
+ if (_optionalChain([partialRecord, 'access', _64 => _64.metrics, 'optionalAccess', _65 => _65.end])) {
6439
+ this.loggedEndTime = _optionalChain([partialRecord, 'access', _66 => _66.metrics, 'optionalAccess', _67 => _67.end]);
6353
6440
  }
6354
- if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access', _64 => _64._spanParents, 'optionalAccess', _65 => _65.length])) {
6441
+ if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access', _68 => _68._spanParents, 'optionalAccess', _69 => _69.length])) {
6355
6442
  throw new Error("Tags can only be logged to the root span");
6356
6443
  }
6357
6444
  const computeRecord = async () => ({
@@ -6396,18 +6483,18 @@ var SpanImpl = (_class11 = class _SpanImpl {
6396
6483
  );
6397
6484
  }
6398
6485
  startSpan(args) {
6399
- const parentSpanIds = _optionalChain([args, 'optionalAccess', _66 => _66.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
6486
+ const parentSpanIds = _optionalChain([args, 'optionalAccess', _70 => _70.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
6400
6487
  return new _SpanImpl({
6401
6488
  state: this._state,
6402
6489
  ...args,
6403
6490
  ...startSpanParentArgs({
6404
6491
  state: this._state,
6405
- parent: _optionalChain([args, 'optionalAccess', _67 => _67.parent]),
6492
+ parent: _optionalChain([args, 'optionalAccess', _71 => _71.parent]),
6406
6493
  parentObjectType: this.parentObjectType,
6407
6494
  parentObjectId: this.parentObjectId,
6408
6495
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
6409
6496
  parentSpanIds,
6410
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _68 => _68.propagatedEvent]), () => ( this.propagatedEvent))
6497
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _72 => _72.propagatedEvent]), () => ( this.propagatedEvent))
6411
6498
  })
6412
6499
  });
6413
6500
  }
@@ -6421,12 +6508,12 @@ var SpanImpl = (_class11 = class _SpanImpl {
6421
6508
  ...args,
6422
6509
  ...startSpanParentArgs({
6423
6510
  state: this._state,
6424
- parent: _optionalChain([args, 'optionalAccess', _69 => _69.parent]),
6511
+ parent: _optionalChain([args, 'optionalAccess', _73 => _73.parent]),
6425
6512
  parentObjectType: this.parentObjectType,
6426
6513
  parentObjectId: this.parentObjectId,
6427
6514
  parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
6428
6515
  parentSpanIds,
6429
- propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _70 => _70.propagatedEvent]), () => ( this.propagatedEvent))
6516
+ propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _74 => _74.propagatedEvent]), () => ( this.propagatedEvent))
6430
6517
  }),
6431
6518
  spanId
6432
6519
  });
@@ -6435,7 +6522,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
6435
6522
  let endTime;
6436
6523
  let internalData = {};
6437
6524
  if (!this.loggedEndTime) {
6438
- endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _71 => _71.endTime]), () => ( getCurrentUnixTimestamp()));
6525
+ endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _75 => _75.endTime]), () => ( getCurrentUnixTimestamp()));
6439
6526
  internalData = { metrics: { end: endTime } };
6440
6527
  } else {
6441
6528
  endTime = this.loggedEndTime;
@@ -6478,8 +6565,8 @@ var SpanImpl = (_class11 = class _SpanImpl {
6478
6565
  const args = this.parentComputeObjectMetadataArgs;
6479
6566
  switch (this.parentObjectType) {
6480
6567
  case 2 /* PROJECT_LOGS */: {
6481
- const projectID = _optionalChain([args, 'optionalAccess', _72 => _72.project_id]) || this.parentObjectId.getSync().value;
6482
- const projectName = _optionalChain([args, 'optionalAccess', _73 => _73.project_name]);
6568
+ const projectID = _optionalChain([args, 'optionalAccess', _76 => _76.project_id]) || this.parentObjectId.getSync().value;
6569
+ const projectName = _optionalChain([args, 'optionalAccess', _77 => _77.project_name]);
6483
6570
  if (projectID) {
6484
6571
  return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
6485
6572
  } else if (projectName) {
@@ -6489,7 +6576,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
6489
6576
  }
6490
6577
  }
6491
6578
  case 1 /* EXPERIMENT */: {
6492
- const expID = _optionalChain([args, 'optionalAccess', _74 => _74.experiment_id]) || _optionalChain([this, 'access', _75 => _75.parentObjectId, 'optionalAccess', _76 => _76.getSync, 'call', _77 => _77(), 'optionalAccess', _78 => _78.value]);
6579
+ const expID = _optionalChain([args, 'optionalAccess', _78 => _78.experiment_id]) || _optionalChain([this, 'access', _79 => _79.parentObjectId, 'optionalAccess', _80 => _80.getSync, 'call', _81 => _81(), 'optionalAccess', _82 => _82.value]);
6493
6580
  if (!expID) {
6494
6581
  return getErrPermlink("provide-experiment-id");
6495
6582
  } else {
@@ -6579,13 +6666,13 @@ var Dataset2 = (_class12 = class extends ObjectFetcher {
6579
6666
  )
6580
6667
  ),
6581
6668
  _internal_btql
6582
- );_class12.prototype.__init40.call(this);_class12.prototype.__init41.call(this);;
6669
+ );_class12.prototype.__init41.call(this);_class12.prototype.__init42.call(this);;
6583
6670
  this.state = state;
6584
6671
  this.lazyMetadata = lazyMetadata;
6585
6672
  }
6586
6673
 
6587
- __init40() {this.__braintrust_dataset_marker = true}
6588
- __init41() {this.newRecords = 0}
6674
+ __init41() {this.__braintrust_dataset_marker = true}
6675
+ __init42() {this.newRecords = 0}
6589
6676
  get id() {
6590
6677
  return (async () => {
6591
6678
  return (await this.lazyMetadata.get()).dataset.id;
@@ -6908,14 +6995,14 @@ function renderPromptParams(params, args, options) {
6908
6995
  return params;
6909
6996
  }
6910
6997
  var Prompt2 = (_class13 = class _Prompt {
6911
- constructor(metadata, defaults, noTrace) {;_class13.prototype.__init42.call(this);_class13.prototype.__init43.call(this);
6998
+ constructor(metadata, defaults, noTrace) {;_class13.prototype.__init43.call(this);_class13.prototype.__init44.call(this);
6912
6999
  this.metadata = metadata;
6913
7000
  this.defaults = defaults;
6914
7001
  this.noTrace = noTrace;
6915
7002
  }
6916
7003
 
6917
- __init42() {this.hasParsedPromptData = false}
6918
- __init43() {this.__braintrust_prompt_marker = true}
7004
+ __init43() {this.hasParsedPromptData = false}
7005
+ __init44() {this.__braintrust_prompt_marker = true}
6919
7006
  get id() {
6920
7007
  return this.metadata.id;
6921
7008
  }
@@ -6929,13 +7016,13 @@ var Prompt2 = (_class13 = class _Prompt {
6929
7016
  return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
6930
7017
  }
6931
7018
  get prompt() {
6932
- return _optionalChain([this, 'access', _79 => _79.getParsedPromptData, 'call', _80 => _80(), 'optionalAccess', _81 => _81.prompt]);
7019
+ return _optionalChain([this, 'access', _83 => _83.getParsedPromptData, 'call', _84 => _84(), 'optionalAccess', _85 => _85.prompt]);
6933
7020
  }
6934
7021
  get version() {
6935
7022
  return this.metadata[TRANSACTION_ID_FIELD];
6936
7023
  }
6937
7024
  get options() {
6938
- return _optionalChain([this, 'access', _82 => _82.getParsedPromptData, 'call', _83 => _83(), 'optionalAccess', _84 => _84.options]) || {};
7025
+ return _optionalChain([this, 'access', _86 => _86.getParsedPromptData, 'call', _87 => _87(), 'optionalAccess', _88 => _88.options]) || {};
6939
7026
  }
6940
7027
  get promptData() {
6941
7028
  return this.getParsedPromptData();
@@ -7086,7 +7173,7 @@ var Prompt2 = (_class13 = class _Prompt {
7086
7173
  return {
7087
7174
  type: "chat",
7088
7175
  messages,
7089
- ..._optionalChain([prompt, 'access', _85 => _85.tools, 'optionalAccess', _86 => _86.trim, 'call', _87 => _87()]) ? {
7176
+ ..._optionalChain([prompt, 'access', _89 => _89.tools, 'optionalAccess', _90 => _90.trim, 'call', _91 => _91()]) ? {
7090
7177
  tools: render(prompt.tools)
7091
7178
  } : void 0
7092
7179
  };
@@ -7196,9 +7283,15 @@ async function getPromptVersions(projectId, promptId) {
7196
7283
  );
7197
7284
  }
7198
7285
  const result = await response.json();
7199
- return _optionalChain([result, 'access', _88 => _88.data, 'optionalAccess', _89 => _89.filter, 'call', _90 => _90(
7200
- (entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access', _91 => _91.audit_data, 'optionalAccess', _92 => _92.action]))
7201
- ), 'access', _93 => _93.map, 'call', _94 => _94((entry) => prettifyXact(entry._xact_id))]) || [];
7286
+ return _optionalChain([result, 'access', _92 => _92.data, 'optionalAccess', _93 => _93.filter, 'call', _94 => _94(
7287
+ (entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access', _95 => _95.audit_data, 'optionalAccess', _96 => _96.action]))
7288
+ ), 'access', _97 => _97.map, 'call', _98 => _98((entry) => prettifyXact(entry._xact_id))]) || [];
7289
+ }
7290
+ function resetIdGenStateForTests() {
7291
+ const state = _internalGetGlobalState();
7292
+ if (state) {
7293
+ state.resetIdGenState();
7294
+ }
7202
7295
  }
7203
7296
  var _exportsForTestingOnly = {
7204
7297
  extractAttachments,
@@ -7210,7 +7303,8 @@ var _exportsForTestingOnly = {
7210
7303
  setInitialTestState,
7211
7304
  initTestExperiment,
7212
7305
  isGeneratorFunction,
7213
- isAsyncGeneratorFunction
7306
+ isAsyncGeneratorFunction,
7307
+ resetIdGenStateForTests
7214
7308
  };
7215
7309
 
7216
7310
  // src/browser-config.ts
@@ -7457,7 +7551,7 @@ function parseSpanFromResponseCreateParams(params) {
7457
7551
  }
7458
7552
  function parseEventFromResponseCreateResult(result) {
7459
7553
  const data = {};
7460
- if (_optionalChain([result, 'optionalAccess', _95 => _95.output]) !== void 0) {
7554
+ if (_optionalChain([result, 'optionalAccess', _99 => _99.output]) !== void 0) {
7461
7555
  data.output = processImagesInOutput(result.output);
7462
7556
  }
7463
7557
  if (result) {
@@ -7466,7 +7560,7 @@ function parseEventFromResponseCreateResult(result) {
7466
7560
  data.metadata = metadata;
7467
7561
  }
7468
7562
  }
7469
- data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _96 => _96.usage]));
7563
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _100 => _100.usage]));
7470
7564
  return data;
7471
7565
  }
7472
7566
  function processImagesInOutput(output) {
@@ -7520,7 +7614,7 @@ function parseSpanFromResponseParseParams(params) {
7520
7614
  }
7521
7615
  function parseEventFromResponseParseResult(result) {
7522
7616
  const data = {};
7523
- if (_optionalChain([result, 'optionalAccess', _97 => _97.output]) !== void 0) {
7617
+ if (_optionalChain([result, 'optionalAccess', _101 => _101.output]) !== void 0) {
7524
7618
  data.output = processImagesInOutput(result.output);
7525
7619
  }
7526
7620
  if (result) {
@@ -7529,7 +7623,7 @@ function parseEventFromResponseParseResult(result) {
7529
7623
  data.metadata = metadata;
7530
7624
  }
7531
7625
  }
7532
- data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _98 => _98.usage]));
7626
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _102 => _102.usage]));
7533
7627
  return data;
7534
7628
  }
7535
7629
  function traceResponseCreateStream(stream, timedSpan) {
@@ -7546,7 +7640,7 @@ function traceResponseCreateStream(stream, timedSpan) {
7546
7640
  return result;
7547
7641
  }
7548
7642
  const item = result.value;
7549
- if (!item || !_optionalChain([item, 'optionalAccess', _99 => _99.type]) || !_optionalChain([item, 'optionalAccess', _100 => _100.response])) {
7643
+ if (!item || !_optionalChain([item, 'optionalAccess', _103 => _103.type]) || !_optionalChain([item, 'optionalAccess', _104 => _104.response])) {
7550
7644
  return result;
7551
7645
  }
7552
7646
  const event = parseLogFromItem(item);
@@ -7557,14 +7651,14 @@ function traceResponseCreateStream(stream, timedSpan) {
7557
7651
  };
7558
7652
  }
7559
7653
  function parseLogFromItem(item) {
7560
- if (!item || !_optionalChain([item, 'optionalAccess', _101 => _101.type]) || !_optionalChain([item, 'optionalAccess', _102 => _102.response])) {
7654
+ if (!item || !_optionalChain([item, 'optionalAccess', _105 => _105.type]) || !_optionalChain([item, 'optionalAccess', _106 => _106.response])) {
7561
7655
  return {};
7562
7656
  }
7563
7657
  const response = item.response;
7564
7658
  switch (item.type) {
7565
7659
  case "response.completed":
7566
7660
  const data = {};
7567
- if (_optionalChain([response, 'optionalAccess', _103 => _103.output]) !== void 0) {
7661
+ if (_optionalChain([response, 'optionalAccess', _107 => _107.output]) !== void 0) {
7568
7662
  data.output = processImagesInOutput(response.output);
7569
7663
  }
7570
7664
  if (response) {
@@ -7573,7 +7667,7 @@ function parseLogFromItem(item) {
7573
7667
  data.metadata = metadata;
7574
7668
  }
7575
7669
  }
7576
- data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _104 => _104.usage]));
7670
+ data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _108 => _108.usage]));
7577
7671
  return data;
7578
7672
  default:
7579
7673
  return {};
@@ -7757,8 +7851,8 @@ function wrapOpenAIv4(openai) {
7757
7851
  const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
7758
7852
  const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
7759
7853
  let betaProxy;
7760
- if (_optionalChain([openai, 'access', _105 => _105.beta, 'optionalAccess', _106 => _106.chat, 'optionalAccess', _107 => _107.completions, 'optionalAccess', _108 => _108.stream])) {
7761
- const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _109 => _109.beta, 'optionalAccess', _110 => _110.chat, 'access', _111 => _111.completions]), {
7854
+ if (_optionalChain([openai, 'access', _109 => _109.beta, 'optionalAccess', _110 => _110.chat, 'optionalAccess', _111 => _111.completions, 'optionalAccess', _112 => _112.stream])) {
7855
+ const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _113 => _113.beta, 'optionalAccess', _114 => _114.chat, 'access', _115 => _115.completions]), {
7762
7856
  get(target, name, receiver) {
7763
7857
  const baseVal = Reflect.get(target, name, receiver);
7764
7858
  if (name === "parse") {
@@ -7806,7 +7900,7 @@ function wrapOpenAIv4(openai) {
7806
7900
  });
7807
7901
  }
7808
7902
  function logCompletionResponse(startTime, response, span) {
7809
- const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _112 => _112.usage]));
7903
+ const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _116 => _116.usage]));
7810
7904
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
7811
7905
  span.log({
7812
7906
  output: response.choices,
@@ -8036,7 +8130,7 @@ function parseChatCompletionParams(params) {
8036
8130
  function processEmbeddingResponse(result, span) {
8037
8131
  span.log({
8038
8132
  output: { embedding_length: result.data[0].embedding.length },
8039
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _113 => _113.usage]))
8133
+ metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _117 => _117.usage]))
8040
8134
  });
8041
8135
  }
8042
8136
  function processModerationResponse(result, span) {
@@ -8066,10 +8160,10 @@ function postprocessStreamingResults(allResults) {
8066
8160
  if (result.usage) {
8067
8161
  metrics = {
8068
8162
  ...metrics,
8069
- ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _114 => _114.usage]))
8163
+ ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _118 => _118.usage]))
8070
8164
  };
8071
8165
  }
8072
- const delta = _optionalChain([result, 'access', _115 => _115.choices, 'optionalAccess', _116 => _116[0], 'optionalAccess', _117 => _117.delta]);
8166
+ const delta = _optionalChain([result, 'access', _119 => _119.choices, 'optionalAccess', _120 => _120[0], 'optionalAccess', _121 => _121.delta]);
8073
8167
  if (!delta) {
8074
8168
  continue;
8075
8169
  }