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.
package/dist/browser.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(
@@ -19158,7 +19175,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
19158
19175
  });
19159
19176
  } else {
19160
19177
  const result = await experiment.traced(callback, baseEvent);
19161
- if (evaluator.maxConcurrency !== void 0) {
19178
+ const bgLogger = experiment.loggingState.bgLogger();
19179
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
19162
19180
  await experiment.flush();
19163
19181
  }
19164
19182
  return result;
package/dist/browser.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(
@@ -19000,7 +19017,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
19000
19017
  });
19001
19018
  } else {
19002
19019
  const result = await experiment.traced(callback, baseEvent);
19003
- if (evaluator.maxConcurrency !== void 0) {
19020
+ const bgLogger = experiment.loggingState.bgLogger();
19021
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
19004
19022
  await experiment.flush();
19005
19023
  }
19006
19024
  return result;
package/dist/cli.js CHANGED
@@ -1275,7 +1275,7 @@ var require_package = __commonJS({
1275
1275
  "package.json"(exports2, module2) {
1276
1276
  module2.exports = {
1277
1277
  name: "braintrust",
1278
- version: "3.2.0",
1278
+ version: "3.3.0-rc.45",
1279
1279
  description: "SDK for integrating Braintrust",
1280
1280
  repository: {
1281
1281
  type: "git",
@@ -7057,6 +7057,7 @@ function utf8ByteLength(value) {
7057
7057
  function now() {
7058
7058
  return (/* @__PURE__ */ new Date()).getTime();
7059
7059
  }
7060
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
7060
7061
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
7061
7062
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7062
7063
  apiConn;
@@ -7075,7 +7076,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7075
7076
  queueDropLoggingPeriod = 60;
7076
7077
  failedPublishPayloadsDir = void 0;
7077
7078
  allPublishPayloadsDir = void 0;
7078
- flushChunkSize = 25;
7079
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
7080
+ _pendingBytes = 0;
7079
7081
  _disabled = false;
7080
7082
  queueDropLoggingState = {
7081
7083
  numDropped: 0,
@@ -7115,11 +7117,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7115
7117
  if (!isNaN(queueDropLoggingPeriodEnv)) {
7116
7118
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
7117
7119
  }
7118
- const flushChunkSizeEnv = Number(
7119
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
7120
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
7121
+ console.warn(
7122
+ "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."
7123
+ );
7124
+ }
7125
+ const flushBackpressureBytesEnv = Number(
7126
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
7120
7127
  );
7121
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
7122
- this.flushChunkSize = flushChunkSizeEnv;
7128
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
7129
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
7123
7130
  }
7124
7131
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
7125
7132
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -7143,6 +7150,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7143
7150
  setMaskingFunction(maskingFunction) {
7144
7151
  this.maskingFunction = maskingFunction;
7145
7152
  }
7153
+ pendingFlushBytes() {
7154
+ return this._pendingBytes;
7155
+ }
7156
+ flushBackpressureBytes() {
7157
+ return this._flushBackpressureBytes;
7158
+ }
7146
7159
  log(items) {
7147
7160
  if (this._disabled) {
7148
7161
  return;
@@ -7205,14 +7218,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7205
7218
  if (wrappedItems.length === 0) {
7206
7219
  return;
7207
7220
  }
7208
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
7209
- let index = 0;
7210
- while (index < wrappedItems.length) {
7211
- const chunk = wrappedItems.slice(index, index + chunkSize);
7212
- await this.flushWrappedItemsChunk(chunk, batchSize);
7213
- index += chunk.length;
7214
- }
7215
- wrappedItems.length = 0;
7221
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
7216
7222
  if (this.queue.length() > 0) {
7217
7223
  await this.flushOnce(args);
7218
7224
  }
@@ -7225,9 +7231,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7225
7231
  if (allItems.length === 0) {
7226
7232
  return;
7227
7233
  }
7228
- const allItemsWithMeta = allItems.map(
7229
- (item) => stringifyWithOverflowMeta(item)
7230
- );
7234
+ let chunkBytes = 0;
7235
+ const allItemsWithMeta = allItems.map((item) => {
7236
+ const withMeta = stringifyWithOverflowMeta(item);
7237
+ chunkBytes += withMeta.str.length;
7238
+ return withMeta;
7239
+ });
7240
+ this._pendingBytes += chunkBytes;
7231
7241
  const maxRequestSizeResult = await this.getMaxRequestSize();
7232
7242
  const batches = batchItems({
7233
7243
  items: allItemsWithMeta,
@@ -7246,6 +7256,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
7246
7256
  })()
7247
7257
  );
7248
7258
  const results = await Promise.all(postPromises);
7259
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
7249
7260
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
7250
7261
  if (failingResultErrors.length) {
7251
7262
  throw new AggregateError(
@@ -11837,7 +11848,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
11837
11848
  });
11838
11849
  } else {
11839
11850
  const result = await experiment.traced(callback, baseEvent);
11840
- if (evaluator.maxConcurrency !== void 0) {
11851
+ const bgLogger = experiment.loggingState.bgLogger();
11852
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
11841
11853
  await experiment.flush();
11842
11854
  }
11843
11855
  return result;
@@ -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;
@@ -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/dist/index.d.mts CHANGED
@@ -19133,6 +19133,8 @@ interface BackgroundLoggerOpts {
19133
19133
  interface BackgroundLogger {
19134
19134
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19135
19135
  flush(): Promise<void>;
19136
+ pendingFlushBytes(): number;
19137
+ flushBackpressureBytes(): number;
19136
19138
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19137
19139
  }
19138
19140
  declare class TestBackgroundLogger implements BackgroundLogger {
@@ -19141,6 +19143,8 @@ declare class TestBackgroundLogger implements BackgroundLogger {
19141
19143
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19142
19144
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19143
19145
  flush(): Promise<void>;
19146
+ pendingFlushBytes(): number;
19147
+ flushBackpressureBytes(): number;
19144
19148
  drain(): Promise<BackgroundLogEvent[]>;
19145
19149
  }
19146
19150
  declare class HTTPBackgroundLogger implements BackgroundLogger {
@@ -19160,11 +19164,14 @@ declare class HTTPBackgroundLogger implements BackgroundLogger {
19160
19164
  queueDropLoggingPeriod: number;
19161
19165
  failedPublishPayloadsDir: string | undefined;
19162
19166
  allPublishPayloadsDir: string | undefined;
19163
- flushChunkSize: number;
19167
+ private _flushBackpressureBytes;
19168
+ private _pendingBytes;
19164
19169
  private _disabled;
19165
19170
  private queueDropLoggingState;
19166
19171
  constructor(apiConn: LazyValue<HTTPConnection>, opts?: BackgroundLoggerOpts);
19167
19172
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19173
+ pendingFlushBytes(): number;
19174
+ flushBackpressureBytes(): number;
19168
19175
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19169
19176
  private getMaxRequestSize;
19170
19177
  flush(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -19133,6 +19133,8 @@ interface BackgroundLoggerOpts {
19133
19133
  interface BackgroundLogger {
19134
19134
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19135
19135
  flush(): Promise<void>;
19136
+ pendingFlushBytes(): number;
19137
+ flushBackpressureBytes(): number;
19136
19138
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19137
19139
  }
19138
19140
  declare class TestBackgroundLogger implements BackgroundLogger {
@@ -19141,6 +19143,8 @@ declare class TestBackgroundLogger implements BackgroundLogger {
19141
19143
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19142
19144
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19143
19145
  flush(): Promise<void>;
19146
+ pendingFlushBytes(): number;
19147
+ flushBackpressureBytes(): number;
19144
19148
  drain(): Promise<BackgroundLogEvent[]>;
19145
19149
  }
19146
19150
  declare class HTTPBackgroundLogger implements BackgroundLogger {
@@ -19160,11 +19164,14 @@ declare class HTTPBackgroundLogger implements BackgroundLogger {
19160
19164
  queueDropLoggingPeriod: number;
19161
19165
  failedPublishPayloadsDir: string | undefined;
19162
19166
  allPublishPayloadsDir: string | undefined;
19163
- flushChunkSize: number;
19167
+ private _flushBackpressureBytes;
19168
+ private _pendingBytes;
19164
19169
  private _disabled;
19165
19170
  private queueDropLoggingState;
19166
19171
  constructor(apiConn: LazyValue<HTTPConnection>, opts?: BackgroundLoggerOpts);
19167
19172
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
19173
+ pendingFlushBytes(): number;
19174
+ flushBackpressureBytes(): number;
19168
19175
  log(items: LazyValue<BackgroundLogEvent>[]): void;
19169
19176
  private getMaxRequestSize;
19170
19177
  flush(): Promise<void>;