braintrust 0.0.132 → 0.0.133

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
@@ -5458,16 +5458,148 @@ function tryMakeUuid(s) {
5458
5458
  return { bytes: Buffer.from(s, "utf-8"), isUUID: false };
5459
5459
  }
5460
5460
  }
5461
- var ENCODING_VERSION_NUMBER = 2;
5461
+ var ENCODING_VERSION_NUMBER = 1;
5462
5462
  var INVALID_ENCODING_ERRMSG = "SpanComponents string is not properly encoded. This may be due to a version mismatch between the SDK library used to export the span and the library used to decode it. Please make sure you are using the same SDK version across the board";
5463
+ var SpanObjectTypeV1 = /* @__PURE__ */ ((SpanObjectTypeV12) => {
5464
+ SpanObjectTypeV12[SpanObjectTypeV12["EXPERIMENT"] = 0] = "EXPERIMENT";
5465
+ SpanObjectTypeV12[SpanObjectTypeV12["PROJECT_LOGS"] = 1] = "PROJECT_LOGS";
5466
+ return SpanObjectTypeV12;
5467
+ })(SpanObjectTypeV1 || {});
5468
+ var SpanObjectTypeV1EnumSchema = z.nativeEnum(SpanObjectTypeV1);
5469
+ var SpanRowIdsV1 = class {
5470
+ constructor(args) {
5471
+ this.rowId = args.rowId;
5472
+ this.spanId = args.spanId;
5473
+ this.rootSpanId = args.rootSpanId;
5474
+ if (!this.rowId) {
5475
+ throw new Error("rowId must be nonempty string");
5476
+ }
5477
+ if (!this.spanId) {
5478
+ throw new Error("spanId must be nonempty string");
5479
+ }
5480
+ if (!this.rootSpanId) {
5481
+ throw new Error("rootSpanId must be nonempty string");
5482
+ }
5483
+ }
5484
+ toObject() {
5485
+ return {
5486
+ rowId: this.rowId,
5487
+ spanId: this.spanId,
5488
+ rootSpanId: this.rootSpanId
5489
+ };
5490
+ }
5491
+ };
5492
+ var SpanComponentsV1 = class _SpanComponentsV1 {
5493
+ constructor(args) {
5494
+ this.objectType = args.objectType;
5495
+ this.objectId = args.objectId;
5496
+ this.rowIds = args.rowIds;
5497
+ }
5498
+ toStr() {
5499
+ const allBuffers = [];
5500
+ const { bytes: rowIdBytes, isUUID: rowIdIsUUID } = this.rowIds ? tryMakeUuid(this.rowIds.rowId) : { bytes: Buffer.from(""), isUUID: false };
5501
+ allBuffers.push(
5502
+ Buffer.from([
5503
+ ENCODING_VERSION_NUMBER,
5504
+ this.objectType,
5505
+ this.rowIds ? 1 : 0,
5506
+ rowIdIsUUID ? 1 : 0
5507
+ ])
5508
+ );
5509
+ const { bytes: objectIdBytes, isUUID: objectIdIsUUID } = tryMakeUuid(
5510
+ this.objectId
5511
+ );
5512
+ if (!objectIdIsUUID) {
5513
+ throw new Error("object_id component must be a valid UUID");
5514
+ }
5515
+ allBuffers.push(objectIdBytes);
5516
+ if (this.rowIds) {
5517
+ const { bytes: spanIdBytes, isUUID: spanIdIsUUID } = tryMakeUuid(
5518
+ this.rowIds.spanId
5519
+ );
5520
+ if (!spanIdIsUUID) {
5521
+ throw new Error("span_id component must be a valid UUID");
5522
+ }
5523
+ const { bytes: rootSpanIdBytes, isUUID: rootSpanIdIsUUID } = tryMakeUuid(
5524
+ this.rowIds.rootSpanId
5525
+ );
5526
+ if (!rootSpanIdIsUUID) {
5527
+ throw new Error("root_span_id component must be a valid UUID");
5528
+ }
5529
+ allBuffers.push(spanIdBytes, rootSpanIdBytes, rowIdBytes);
5530
+ }
5531
+ return Buffer.concat(allBuffers).toString("base64");
5532
+ }
5533
+ static fromStr(s) {
5534
+ try {
5535
+ const rawBytes = Buffer.from(s, "base64");
5536
+ if (rawBytes[0] !== ENCODING_VERSION_NUMBER) {
5537
+ throw new Error();
5538
+ }
5539
+ const objectType2 = SpanObjectTypeV1EnumSchema.parse(rawBytes[1]);
5540
+ if (![0, 1].includes(rawBytes[2])) {
5541
+ throw new Error();
5542
+ }
5543
+ if (![0, 1].includes(rawBytes[3])) {
5544
+ throw new Error();
5545
+ }
5546
+ const hasRowId = rawBytes[2] == 1;
5547
+ const rowIdIsUUID = rawBytes[3] == 1;
5548
+ const objectId = stringify_default(rawBytes.subarray(4, 20));
5549
+ const rowIds = (() => {
5550
+ if (!hasRowId) {
5551
+ return void 0;
5552
+ }
5553
+ const spanId = stringify_default(rawBytes.subarray(20, 36));
5554
+ const rootSpanId = stringify_default(rawBytes.subarray(36, 52));
5555
+ const rowId = rowIdIsUUID ? stringify_default(rawBytes.subarray(52)) : rawBytes.subarray(52).toString("utf-8");
5556
+ return new SpanRowIdsV1({ rowId, spanId, rootSpanId });
5557
+ })();
5558
+ return new _SpanComponentsV1({ objectType: objectType2, objectId, rowIds });
5559
+ } catch (e) {
5560
+ throw new Error(INVALID_ENCODING_ERRMSG);
5561
+ }
5562
+ }
5563
+ objectIdFields() {
5564
+ switch (this.objectType) {
5565
+ case 0:
5566
+ return { experiment_id: this.objectId };
5567
+ case 1:
5568
+ return { project_id: this.objectId, log_id: "g" };
5569
+ default:
5570
+ throw new Error("Impossible");
5571
+ }
5572
+ }
5573
+ toObject() {
5574
+ var _a2;
5575
+ return {
5576
+ objectType: this.objectType,
5577
+ objectId: this.objectId,
5578
+ rowIds: (_a2 = this.rowIds) == null ? void 0 : _a2.toObject()
5579
+ };
5580
+ }
5581
+ };
5582
+ function tryMakeUuid2(s) {
5583
+ try {
5584
+ const ret = parse_default(s);
5585
+ if (ret.length !== 16) {
5586
+ throw new Error();
5587
+ }
5588
+ return { bytes: Buffer.from(ret), isUUID: true };
5589
+ } catch (e) {
5590
+ return { bytes: Buffer.from(s, "utf-8"), isUUID: false };
5591
+ }
5592
+ }
5593
+ var ENCODING_VERSION_NUMBER2 = 2;
5594
+ var INVALID_ENCODING_ERRMSG2 = `SpanComponents string is not properly encoded. This library only supports encoding versions up to ${ENCODING_VERSION_NUMBER2}. Please make sure the SDK library used to decode the SpanComponents is at least as new as any library used to encode it.`;
5463
5595
  var INTEGER_ENCODING_NUM_BYTES = 4;
5464
- var SpanObjectType = /* @__PURE__ */ ((SpanObjectType2) => {
5465
- SpanObjectType2[SpanObjectType2["EXPERIMENT"] = 0] = "EXPERIMENT";
5466
- SpanObjectType2[SpanObjectType2["PROJECT_LOGS"] = 1] = "PROJECT_LOGS";
5467
- return SpanObjectType2;
5468
- })(SpanObjectType || {});
5469
- var SpanObjectTypeEnumSchema = z.nativeEnum(SpanObjectType);
5470
- var SpanRowIds = class {
5596
+ var SpanObjectTypeV2 = /* @__PURE__ */ ((SpanObjectTypeV22) => {
5597
+ SpanObjectTypeV22[SpanObjectTypeV22["EXPERIMENT"] = 0] = "EXPERIMENT";
5598
+ SpanObjectTypeV22[SpanObjectTypeV22["PROJECT_LOGS"] = 1] = "PROJECT_LOGS";
5599
+ return SpanObjectTypeV22;
5600
+ })(SpanObjectTypeV2 || {});
5601
+ var SpanObjectTypeV2EnumSchema = z.nativeEnum(SpanObjectTypeV2);
5602
+ var SpanRowIdsV2 = class {
5471
5603
  constructor(args) {
5472
5604
  this.rowId = args.rowId;
5473
5605
  this.spanId = args.spanId;
@@ -5490,7 +5622,7 @@ var SpanRowIds = class {
5490
5622
  };
5491
5623
  }
5492
5624
  };
5493
- var SpanComponents = class _SpanComponents {
5625
+ var SpanComponentsV2 = class _SpanComponentsV2 {
5494
5626
  constructor(args) {
5495
5627
  this.objectType = args.objectType;
5496
5628
  this.objectId = args.objectId;
@@ -5504,10 +5636,10 @@ var SpanComponents = class _SpanComponents {
5504
5636
  }
5505
5637
  toStr() {
5506
5638
  const allBuffers = [];
5507
- const { bytes: rowIdBytes, isUUID: rowIdIsUUID } = this.rowIds ? tryMakeUuid(this.rowIds.rowId) : { bytes: Buffer.from(""), isUUID: false };
5639
+ const { bytes: rowIdBytes, isUUID: rowIdIsUUID } = this.rowIds ? tryMakeUuid2(this.rowIds.rowId) : { bytes: Buffer.from(""), isUUID: false };
5508
5640
  allBuffers.push(
5509
5641
  Buffer.from([
5510
- ENCODING_VERSION_NUMBER,
5642
+ ENCODING_VERSION_NUMBER2,
5511
5643
  this.objectType,
5512
5644
  this.objectId ? 1 : 0,
5513
5645
  this.computeObjectMetadataArgs ? 1 : 0,
@@ -5516,7 +5648,7 @@ var SpanComponents = class _SpanComponents {
5516
5648
  ])
5517
5649
  );
5518
5650
  if (this.objectId) {
5519
- const { bytes: objectIdBytes, isUUID: objectIdIsUUID } = tryMakeUuid(
5651
+ const { bytes: objectIdBytes, isUUID: objectIdIsUUID } = tryMakeUuid2(
5520
5652
  this.objectId
5521
5653
  );
5522
5654
  if (!objectIdIsUUID) {
@@ -5534,13 +5666,13 @@ var SpanComponents = class _SpanComponents {
5534
5666
  allBuffers.push(serializedLenBytes, computeObjectMetadataBytes);
5535
5667
  }
5536
5668
  if (this.rowIds) {
5537
- const { bytes: spanIdBytes, isUUID: spanIdIsUUID } = tryMakeUuid(
5669
+ const { bytes: spanIdBytes, isUUID: spanIdIsUUID } = tryMakeUuid2(
5538
5670
  this.rowIds.spanId
5539
5671
  );
5540
5672
  if (!spanIdIsUUID) {
5541
5673
  throw new Error("span_id component must be a valid UUID");
5542
5674
  }
5543
- const { bytes: rootSpanIdBytes, isUUID: rootSpanIdIsUUID } = tryMakeUuid(
5675
+ const { bytes: rootSpanIdBytes, isUUID: rootSpanIdIsUUID } = tryMakeUuid2(
5544
5676
  this.rowIds.rootSpanId
5545
5677
  );
5546
5678
  if (!rootSpanIdIsUUID) {
@@ -5553,10 +5685,24 @@ var SpanComponents = class _SpanComponents {
5553
5685
  static fromStr(s) {
5554
5686
  try {
5555
5687
  const rawBytes = Buffer.from(s, "base64");
5556
- if (rawBytes[0] !== ENCODING_VERSION_NUMBER) {
5688
+ if (rawBytes[0] < ENCODING_VERSION_NUMBER2) {
5689
+ const spanComponentsOld = SpanComponentsV1.fromStr(s);
5690
+ return new _SpanComponentsV2({
5691
+ objectType: SpanObjectTypeV2EnumSchema.parse(
5692
+ spanComponentsOld.objectType
5693
+ ),
5694
+ objectId: spanComponentsOld.objectId,
5695
+ rowIds: spanComponentsOld.rowIds ? new SpanRowIdsV2({
5696
+ rowId: spanComponentsOld.rowIds.rowId,
5697
+ spanId: spanComponentsOld.rowIds.spanId,
5698
+ rootSpanId: spanComponentsOld.rowIds.rootSpanId
5699
+ }) : void 0
5700
+ });
5701
+ }
5702
+ if (rawBytes[0] !== ENCODING_VERSION_NUMBER2) {
5557
5703
  throw new Error();
5558
5704
  }
5559
- const objectType2 = SpanObjectTypeEnumSchema.parse(rawBytes[1]);
5705
+ const objectType2 = SpanObjectTypeV2EnumSchema.parse(rawBytes[1]);
5560
5706
  for (let i = 2; i < 6; ++i) {
5561
5707
  if (![0, 1].includes(rawBytes[i])) {
5562
5708
  throw new Error();
@@ -5601,22 +5747,22 @@ var SpanComponents = class _SpanComponents {
5601
5747
  );
5602
5748
  byteCursor = nextByteCursor;
5603
5749
  const rowId = rowIdIsUUID ? stringify_default(rawBytes.subarray(byteCursor)) : rawBytes.subarray(byteCursor).toString("utf-8");
5604
- return new SpanRowIds({ rowId, spanId, rootSpanId });
5750
+ return new SpanRowIdsV2({ rowId, spanId, rootSpanId });
5605
5751
  })();
5606
- return new _SpanComponents({
5752
+ return new _SpanComponentsV2({
5607
5753
  objectType: objectType2,
5608
5754
  objectId,
5609
5755
  computeObjectMetadataArgs,
5610
5756
  rowIds
5611
5757
  });
5612
5758
  } catch (e) {
5613
- throw new Error(INVALID_ENCODING_ERRMSG);
5759
+ throw new Error(INVALID_ENCODING_ERRMSG2);
5614
5760
  }
5615
5761
  }
5616
5762
  objectIdFields() {
5617
5763
  if (!this.objectId) {
5618
5764
  throw new Error(
5619
- "Impossible: cannot invoke `object_id_fields` unless SpanComponents is initialized with an `object_id`"
5765
+ "Impossible: cannot invoke `object_id_fields` unless SpanComponentsV2 is initialized with an `object_id`"
5620
5766
  );
5621
5767
  }
5622
5768
  switch (this.objectType) {
@@ -5969,7 +6115,6 @@ var apiKeyBaseSchema = generateBaseTableSchema("api key");
5969
6115
  var apiKeySchema = z.strictObject({
5970
6116
  id: apiKeyBaseSchema.shape.id,
5971
6117
  created: apiKeyBaseSchema.shape.created,
5972
- key_hash: z.string(),
5973
6118
  name: apiKeyBaseSchema.shape.name,
5974
6119
  preview_name: z.string(),
5975
6120
  user_id: userSchema.shape.id.nullish(),
@@ -6195,7 +6340,11 @@ var projectScoreSchema = z.strictObject({
6195
6340
  z.array(z.string()).describe(
6196
6341
  "For minimum-type project scores, the list of included scores"
6197
6342
  )
6198
- ]).nullish()
6343
+ ]).nullish(),
6344
+ config: z.strictObject({
6345
+ multi_select: z.boolean().nullish(),
6346
+ destination: z.literal("expected").nullish()
6347
+ }).nullish()
6199
6348
  }).describe(
6200
6349
  "A project score is a user-configured score, which can be manually-labeled through the UI"
6201
6350
  ).openapi("ProjectScore");
@@ -7722,7 +7871,7 @@ function logFeedbackImpl(parentObjectType, parentObjectId, {
7722
7871
  updateEvent = Object.fromEntries(
7723
7872
  Object.entries(updateEvent).filter(([_, v]) => !isEmpty(v))
7724
7873
  );
7725
- const parentIds = async () => new SpanComponents({
7874
+ const parentIds = async () => new SpanComponentsV2({
7726
7875
  objectType: parentObjectType,
7727
7876
  objectId: await parentObjectId.get()
7728
7877
  }).objectIdFields();
@@ -7771,11 +7920,11 @@ function spanComponentsToObjectIdLambda(components) {
7771
7920
  );
7772
7921
  }
7773
7922
  switch (components.objectType) {
7774
- case SpanObjectType.EXPERIMENT:
7923
+ case SpanObjectTypeV2.EXPERIMENT:
7775
7924
  throw new Error(
7776
7925
  "Impossible: computeObjectMetadataArgs not supported for experiments"
7777
7926
  );
7778
- case SpanObjectType.PROJECT_LOGS:
7927
+ case SpanObjectTypeV2.PROJECT_LOGS:
7779
7928
  const args = components.computeObjectMetadataArgs;
7780
7929
  return async () => (await computeLoggerMetadata(args)).project.id;
7781
7930
  default:
@@ -7790,7 +7939,7 @@ function startSpanParentArgs(args) {
7790
7939
  if (args.parentSpanIds) {
7791
7940
  throw new Error("Cannot specify both parent and parentSpanIds");
7792
7941
  }
7793
- const parentComponents = SpanComponents.fromStr(args.parent);
7942
+ const parentComponents = SpanComponentsV2.fromStr(args.parent);
7794
7943
  if (args.parentObjectType !== parentComponents.objectType) {
7795
7944
  throw new Error(
7796
7945
  `Mismatch between expected span parent object type ${args.parentObjectType} and provided type ${parentComponents.objectType}`
@@ -7855,7 +8004,7 @@ var Logger = class {
7855
8004
  return (async () => (await this.project).id)();
7856
8005
  }
7857
8006
  parentObjectType() {
7858
- return SpanObjectType.PROJECT_LOGS;
8007
+ return SpanObjectTypeV2.PROJECT_LOGS;
7859
8008
  }
7860
8009
  /**
7861
8010
  * Log a single event. The event will be batched and uploaded behind the scenes if `logOptions.asyncFlush` is true.
@@ -7967,7 +8116,7 @@ var Logger = class {
7967
8116
  } else {
7968
8117
  objectId = await this.lazyId.get();
7969
8118
  }
7970
- return new SpanComponents({
8119
+ return new SpanComponentsV2({
7971
8120
  objectType: this.parentObjectType(),
7972
8121
  objectId,
7973
8122
  computeObjectMetadataArgs
@@ -8821,7 +8970,7 @@ async function flush() {
8821
8970
  }
8822
8971
  function startSpanAndIsLogger(args) {
8823
8972
  if (args?.parent) {
8824
- const components = SpanComponents.fromStr(args?.parent);
8973
+ const components = SpanComponentsV2.fromStr(args?.parent);
8825
8974
  const parentSpanIds = components.rowIds ? {
8826
8975
  spanId: components.rowIds.spanId,
8827
8976
  rootSpanId: components.rowIds.rootSpanId
@@ -8835,7 +8984,7 @@ function startSpanAndIsLogger(args) {
8835
8984
  });
8836
8985
  return {
8837
8986
  span,
8838
- isLogger: components.objectType === SpanObjectType.PROJECT_LOGS
8987
+ isLogger: components.objectType === SpanObjectTypeV2.PROJECT_LOGS
8839
8988
  };
8840
8989
  } else {
8841
8990
  const parentObject = getSpanParentObject({
@@ -9046,7 +9195,7 @@ var Experiment = class extends ObjectFetcher {
9046
9195
  })();
9047
9196
  }
9048
9197
  parentObjectType() {
9049
- return SpanObjectType.EXPERIMENT;
9198
+ return SpanObjectTypeV2.EXPERIMENT;
9050
9199
  }
9051
9200
  async getState() {
9052
9201
  await this.lazyMetadata.get();
@@ -9212,7 +9361,7 @@ var Experiment = class extends ObjectFetcher {
9212
9361
  * Return a serialized representation of the experiment that can be used to start subspans in other places. See `Span.start_span` for more details.
9213
9362
  */
9214
9363
  async export() {
9215
- return new SpanComponents({
9364
+ return new SpanComponentsV2({
9216
9365
  objectType: this.parentObjectType(),
9217
9366
  objectId: await this.id
9218
9367
  }).toStr();
@@ -9367,7 +9516,7 @@ var SpanImpl = class _SpanImpl {
9367
9516
  }
9368
9517
  const computeRecord = async () => ({
9369
9518
  ...partialRecord,
9370
- ...new SpanComponents({
9519
+ ...new SpanComponentsV2({
9371
9520
  objectType: this.parentObjectType,
9372
9521
  objectId: await this.parentObjectId.get()
9373
9522
  }).objectIdFields()
@@ -9426,11 +9575,11 @@ var SpanImpl = class _SpanImpl {
9426
9575
  } else {
9427
9576
  objectId = await this.parentObjectId.get();
9428
9577
  }
9429
- return new SpanComponents({
9578
+ return new SpanComponentsV2({
9430
9579
  objectType: this.parentObjectType,
9431
9580
  objectId,
9432
9581
  computeObjectMetadataArgs,
9433
- rowIds: new SpanRowIds({
9582
+ rowIds: new SpanRowIdsV2({
9434
9583
  rowId: this.id,
9435
9584
  spanId: this.spanId,
9436
9585
  rootSpanId: this.rootSpanId