braintrust 3.2.0 → 3.3.0-rc.45

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.
@@ -5460,6 +5460,7 @@ function utf8ByteLength(value) {
5460
5460
  function now() {
5461
5461
  return (/* @__PURE__ */ new Date()).getTime();
5462
5462
  }
5463
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5463
5464
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
5464
5465
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5465
5466
  apiConn;
@@ -5478,7 +5479,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5478
5479
  queueDropLoggingPeriod = 60;
5479
5480
  failedPublishPayloadsDir = void 0;
5480
5481
  allPublishPayloadsDir = void 0;
5481
- flushChunkSize = 25;
5482
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5483
+ _pendingBytes = 0;
5482
5484
  _disabled = false;
5483
5485
  queueDropLoggingState = {
5484
5486
  numDropped: 0,
@@ -5518,11 +5520,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5518
5520
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5519
5521
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5520
5522
  }
5521
- const flushChunkSizeEnv = Number(
5522
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5523
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5524
+ console.warn(
5525
+ "BRAINTRUST_LOG_FLUSH_CHUNK_SIZE is deprecated and no longer has any effect. Log flushing now sends all items at once and batches them automatically. This environment variable will be removed in a future major release."
5526
+ );
5527
+ }
5528
+ const flushBackpressureBytesEnv = Number(
5529
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5523
5530
  );
5524
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5525
- this.flushChunkSize = flushChunkSizeEnv;
5531
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5532
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5526
5533
  }
5527
5534
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5528
5535
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5546,6 +5553,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5546
5553
  setMaskingFunction(maskingFunction) {
5547
5554
  this.maskingFunction = maskingFunction;
5548
5555
  }
5556
+ pendingFlushBytes() {
5557
+ return this._pendingBytes;
5558
+ }
5559
+ flushBackpressureBytes() {
5560
+ return this._flushBackpressureBytes;
5561
+ }
5549
5562
  log(items) {
5550
5563
  if (this._disabled) {
5551
5564
  return;
@@ -5608,14 +5621,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5608
5621
  if (wrappedItems.length === 0) {
5609
5622
  return;
5610
5623
  }
5611
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
5612
- let index = 0;
5613
- while (index < wrappedItems.length) {
5614
- const chunk = wrappedItems.slice(index, index + chunkSize);
5615
- await this.flushWrappedItemsChunk(chunk, batchSize);
5616
- index += chunk.length;
5617
- }
5618
- wrappedItems.length = 0;
5624
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
5619
5625
  if (this.queue.length() > 0) {
5620
5626
  await this.flushOnce(args);
5621
5627
  }
@@ -5628,9 +5634,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5628
5634
  if (allItems.length === 0) {
5629
5635
  return;
5630
5636
  }
5631
- const allItemsWithMeta = allItems.map(
5632
- (item) => stringifyWithOverflowMeta(item)
5633
- );
5637
+ let chunkBytes = 0;
5638
+ const allItemsWithMeta = allItems.map((item) => {
5639
+ const withMeta = stringifyWithOverflowMeta(item);
5640
+ chunkBytes += withMeta.str.length;
5641
+ return withMeta;
5642
+ });
5643
+ this._pendingBytes += chunkBytes;
5634
5644
  const maxRequestSizeResult = await this.getMaxRequestSize();
5635
5645
  const batches = batchItems({
5636
5646
  items: allItemsWithMeta,
@@ -5649,6 +5659,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5649
5659
  })()
5650
5660
  );
5651
5661
  const results = await Promise.all(postPromises);
5662
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
5652
5663
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5653
5664
  if (failingResultErrors.length) {
5654
5665
  throw new AggregateError(
package/dist/workerd.js CHANGED
@@ -5855,6 +5855,7 @@ function utf8ByteLength(value) {
5855
5855
  function now() {
5856
5856
  return (/* @__PURE__ */ new Date()).getTime();
5857
5857
  }
5858
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5858
5859
  var TestBackgroundLogger = class {
5859
5860
  items = [];
5860
5861
  maskingFunction = null;
@@ -5867,6 +5868,12 @@ var TestBackgroundLogger = class {
5867
5868
  async flush() {
5868
5869
  return Promise.resolve();
5869
5870
  }
5871
+ pendingFlushBytes() {
5872
+ return 0;
5873
+ }
5874
+ flushBackpressureBytes() {
5875
+ return DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5876
+ }
5870
5877
  async drain() {
5871
5878
  const items = this.items;
5872
5879
  this.items = [];
@@ -5924,7 +5931,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5924
5931
  queueDropLoggingPeriod = 60;
5925
5932
  failedPublishPayloadsDir = void 0;
5926
5933
  allPublishPayloadsDir = void 0;
5927
- flushChunkSize = 25;
5934
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5935
+ _pendingBytes = 0;
5928
5936
  _disabled = false;
5929
5937
  queueDropLoggingState = {
5930
5938
  numDropped: 0,
@@ -5964,11 +5972,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5964
5972
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5965
5973
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5966
5974
  }
5967
- const flushChunkSizeEnv = Number(
5968
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5975
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5976
+ console.warn(
5977
+ "BRAINTRUST_LOG_FLUSH_CHUNK_SIZE is deprecated and no longer has any effect. Log flushing now sends all items at once and batches them automatically. This environment variable will be removed in a future major release."
5978
+ );
5979
+ }
5980
+ const flushBackpressureBytesEnv = Number(
5981
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5969
5982
  );
5970
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5971
- this.flushChunkSize = flushChunkSizeEnv;
5983
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5984
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5972
5985
  }
5973
5986
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5974
5987
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5992,6 +6005,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5992
6005
  setMaskingFunction(maskingFunction) {
5993
6006
  this.maskingFunction = maskingFunction;
5994
6007
  }
6008
+ pendingFlushBytes() {
6009
+ return this._pendingBytes;
6010
+ }
6011
+ flushBackpressureBytes() {
6012
+ return this._flushBackpressureBytes;
6013
+ }
5995
6014
  log(items) {
5996
6015
  if (this._disabled) {
5997
6016
  return;
@@ -6054,14 +6073,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6054
6073
  if (wrappedItems.length === 0) {
6055
6074
  return;
6056
6075
  }
6057
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
6058
- let index = 0;
6059
- while (index < wrappedItems.length) {
6060
- const chunk = wrappedItems.slice(index, index + chunkSize);
6061
- await this.flushWrappedItemsChunk(chunk, batchSize);
6062
- index += chunk.length;
6063
- }
6064
- wrappedItems.length = 0;
6076
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
6065
6077
  if (this.queue.length() > 0) {
6066
6078
  await this.flushOnce(args);
6067
6079
  }
@@ -6074,9 +6086,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6074
6086
  if (allItems.length === 0) {
6075
6087
  return;
6076
6088
  }
6077
- const allItemsWithMeta = allItems.map(
6078
- (item) => stringifyWithOverflowMeta(item)
6079
- );
6089
+ let chunkBytes = 0;
6090
+ const allItemsWithMeta = allItems.map((item) => {
6091
+ const withMeta = stringifyWithOverflowMeta(item);
6092
+ chunkBytes += withMeta.str.length;
6093
+ return withMeta;
6094
+ });
6095
+ this._pendingBytes += chunkBytes;
6080
6096
  const maxRequestSizeResult = await this.getMaxRequestSize();
6081
6097
  const batches = batchItems({
6082
6098
  items: allItemsWithMeta,
@@ -6095,6 +6111,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6095
6111
  })()
6096
6112
  );
6097
6113
  const results = await Promise.all(postPromises);
6114
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
6098
6115
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
6099
6116
  if (failingResultErrors.length) {
6100
6117
  throw new AggregateError(
@@ -16679,7 +16696,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
16679
16696
  });
16680
16697
  } else {
16681
16698
  const result = await experiment.traced(callback, baseEvent);
16682
- if (evaluator.maxConcurrency !== void 0) {
16699
+ const bgLogger = experiment.loggingState.bgLogger();
16700
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
16683
16701
  await experiment.flush();
16684
16702
  }
16685
16703
  return result;
package/dist/workerd.mjs CHANGED
@@ -5697,6 +5697,7 @@ function utf8ByteLength(value) {
5697
5697
  function now() {
5698
5698
  return (/* @__PURE__ */ new Date()).getTime();
5699
5699
  }
5700
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5700
5701
  var TestBackgroundLogger = class {
5701
5702
  items = [];
5702
5703
  maskingFunction = null;
@@ -5709,6 +5710,12 @@ var TestBackgroundLogger = class {
5709
5710
  async flush() {
5710
5711
  return Promise.resolve();
5711
5712
  }
5713
+ pendingFlushBytes() {
5714
+ return 0;
5715
+ }
5716
+ flushBackpressureBytes() {
5717
+ return DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5718
+ }
5712
5719
  async drain() {
5713
5720
  const items = this.items;
5714
5721
  this.items = [];
@@ -5766,7 +5773,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5766
5773
  queueDropLoggingPeriod = 60;
5767
5774
  failedPublishPayloadsDir = void 0;
5768
5775
  allPublishPayloadsDir = void 0;
5769
- flushChunkSize = 25;
5776
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5777
+ _pendingBytes = 0;
5770
5778
  _disabled = false;
5771
5779
  queueDropLoggingState = {
5772
5780
  numDropped: 0,
@@ -5806,11 +5814,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5806
5814
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5807
5815
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5808
5816
  }
5809
- const flushChunkSizeEnv = Number(
5810
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5817
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5818
+ console.warn(
5819
+ "BRAINTRUST_LOG_FLUSH_CHUNK_SIZE is deprecated and no longer has any effect. Log flushing now sends all items at once and batches them automatically. This environment variable will be removed in a future major release."
5820
+ );
5821
+ }
5822
+ const flushBackpressureBytesEnv = Number(
5823
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5811
5824
  );
5812
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5813
- this.flushChunkSize = flushChunkSizeEnv;
5825
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5826
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5814
5827
  }
5815
5828
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5816
5829
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5834,6 +5847,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5834
5847
  setMaskingFunction(maskingFunction) {
5835
5848
  this.maskingFunction = maskingFunction;
5836
5849
  }
5850
+ pendingFlushBytes() {
5851
+ return this._pendingBytes;
5852
+ }
5853
+ flushBackpressureBytes() {
5854
+ return this._flushBackpressureBytes;
5855
+ }
5837
5856
  log(items) {
5838
5857
  if (this._disabled) {
5839
5858
  return;
@@ -5896,14 +5915,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5896
5915
  if (wrappedItems.length === 0) {
5897
5916
  return;
5898
5917
  }
5899
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
5900
- let index = 0;
5901
- while (index < wrappedItems.length) {
5902
- const chunk = wrappedItems.slice(index, index + chunkSize);
5903
- await this.flushWrappedItemsChunk(chunk, batchSize);
5904
- index += chunk.length;
5905
- }
5906
- wrappedItems.length = 0;
5918
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
5907
5919
  if (this.queue.length() > 0) {
5908
5920
  await this.flushOnce(args);
5909
5921
  }
@@ -5916,9 +5928,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5916
5928
  if (allItems.length === 0) {
5917
5929
  return;
5918
5930
  }
5919
- const allItemsWithMeta = allItems.map(
5920
- (item) => stringifyWithOverflowMeta(item)
5921
- );
5931
+ let chunkBytes = 0;
5932
+ const allItemsWithMeta = allItems.map((item) => {
5933
+ const withMeta = stringifyWithOverflowMeta(item);
5934
+ chunkBytes += withMeta.str.length;
5935
+ return withMeta;
5936
+ });
5937
+ this._pendingBytes += chunkBytes;
5922
5938
  const maxRequestSizeResult = await this.getMaxRequestSize();
5923
5939
  const batches = batchItems({
5924
5940
  items: allItemsWithMeta,
@@ -5937,6 +5953,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5937
5953
  })()
5938
5954
  );
5939
5955
  const results = await Promise.all(postPromises);
5956
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
5940
5957
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5941
5958
  if (failingResultErrors.length) {
5942
5959
  throw new AggregateError(
@@ -16521,7 +16538,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
16521
16538
  });
16522
16539
  } else {
16523
16540
  const result = await experiment.traced(callback, baseEvent);
16524
- if (evaluator.maxConcurrency !== void 0) {
16541
+ const bgLogger = experiment.loggingState.bgLogger();
16542
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
16525
16543
  await experiment.flush();
16526
16544
  }
16527
16545
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braintrust",
3
- "version": "3.2.0",
3
+ "version": "3.3.0-rc.45",
4
4
  "description": "SDK for integrating Braintrust",
5
5
  "repository": {
6
6
  "type": "git",