braintrust 3.2.0 → 3.3.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.
@@ -10941,6 +10941,8 @@ interface BackgroundLoggerOpts {
10941
10941
  interface BackgroundLogger {
10942
10942
  log(items: LazyValue<BackgroundLogEvent>[]): void;
10943
10943
  flush(): Promise<void>;
10944
+ pendingFlushBytes(): number;
10945
+ flushBackpressureBytes(): number;
10944
10946
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
10945
10947
  }
10946
10948
  declare class HTTPBackgroundLogger implements BackgroundLogger {
@@ -10960,11 +10962,14 @@ declare class HTTPBackgroundLogger implements BackgroundLogger {
10960
10962
  queueDropLoggingPeriod: number;
10961
10963
  failedPublishPayloadsDir: string | undefined;
10962
10964
  allPublishPayloadsDir: string | undefined;
10963
- flushChunkSize: number;
10965
+ private _flushBackpressureBytes;
10966
+ private _pendingBytes;
10964
10967
  private _disabled;
10965
10968
  private queueDropLoggingState;
10966
10969
  constructor(apiConn: LazyValue<HTTPConnection>, opts?: BackgroundLoggerOpts);
10967
10970
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
10971
+ pendingFlushBytes(): number;
10972
+ flushBackpressureBytes(): number;
10968
10973
  log(items: LazyValue<BackgroundLogEvent>[]): void;
10969
10974
  private getMaxRequestSize;
10970
10975
  flush(): Promise<void>;
@@ -10941,6 +10941,8 @@ interface BackgroundLoggerOpts {
10941
10941
  interface BackgroundLogger {
10942
10942
  log(items: LazyValue<BackgroundLogEvent>[]): void;
10943
10943
  flush(): Promise<void>;
10944
+ pendingFlushBytes(): number;
10945
+ flushBackpressureBytes(): number;
10944
10946
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
10945
10947
  }
10946
10948
  declare class HTTPBackgroundLogger implements BackgroundLogger {
@@ -10960,11 +10962,14 @@ declare class HTTPBackgroundLogger implements BackgroundLogger {
10960
10962
  queueDropLoggingPeriod: number;
10961
10963
  failedPublishPayloadsDir: string | undefined;
10962
10964
  allPublishPayloadsDir: string | undefined;
10963
- flushChunkSize: number;
10965
+ private _flushBackpressureBytes;
10966
+ private _pendingBytes;
10964
10967
  private _disabled;
10965
10968
  private queueDropLoggingState;
10966
10969
  constructor(apiConn: LazyValue<HTTPConnection>, opts?: BackgroundLoggerOpts);
10967
10970
  setMaskingFunction(maskingFunction: ((value: unknown) => unknown) | null): void;
10971
+ pendingFlushBytes(): number;
10972
+ flushBackpressureBytes(): number;
10968
10973
  log(items: LazyValue<BackgroundLogEvent>[]): void;
10969
10974
  private getMaxRequestSize;
10970
10975
  flush(): Promise<void>;
package/dev/dist/index.js CHANGED
@@ -5763,6 +5763,7 @@ function utf8ByteLength(value) {
5763
5763
  function now() {
5764
5764
  return (/* @__PURE__ */ new Date()).getTime();
5765
5765
  }
5766
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5766
5767
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
5767
5768
  var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5768
5769
 
@@ -5781,13 +5782,14 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5781
5782
  __init45() {this.queueDropLoggingPeriod = 60}
5782
5783
  __init46() {this.failedPublishPayloadsDir = void 0}
5783
5784
  __init47() {this.allPublishPayloadsDir = void 0}
5784
- __init48() {this.flushChunkSize = 25}
5785
- __init49() {this._disabled = false}
5786
- __init50() {this.queueDropLoggingState = {
5785
+ __init48() {this._flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES}
5786
+ __init49() {this._pendingBytes = 0}
5787
+ __init50() {this._disabled = false}
5788
+ __init51() {this.queueDropLoggingState = {
5787
5789
  numDropped: 0,
5788
5790
  lastLoggedTimestamp: 0
5789
5791
  }}
5790
- constructor(apiConn, opts) {;_class9.prototype.__init35.call(this);_class9.prototype.__init36.call(this);_class9.prototype.__init37.call(this);_class9.prototype.__init38.call(this);_class9.prototype.__init39.call(this);_class9.prototype.__init40.call(this);_class9.prototype.__init41.call(this);_class9.prototype.__init42.call(this);_class9.prototype.__init43.call(this);_class9.prototype.__init44.call(this);_class9.prototype.__init45.call(this);_class9.prototype.__init46.call(this);_class9.prototype.__init47.call(this);_class9.prototype.__init48.call(this);_class9.prototype.__init49.call(this);_class9.prototype.__init50.call(this);
5792
+ constructor(apiConn, opts) {;_class9.prototype.__init35.call(this);_class9.prototype.__init36.call(this);_class9.prototype.__init37.call(this);_class9.prototype.__init38.call(this);_class9.prototype.__init39.call(this);_class9.prototype.__init40.call(this);_class9.prototype.__init41.call(this);_class9.prototype.__init42.call(this);_class9.prototype.__init43.call(this);_class9.prototype.__init44.call(this);_class9.prototype.__init45.call(this);_class9.prototype.__init46.call(this);_class9.prototype.__init47.call(this);_class9.prototype.__init48.call(this);_class9.prototype.__init49.call(this);_class9.prototype.__init50.call(this);_class9.prototype.__init51.call(this);
5791
5793
  opts = _nullishCoalesce(opts, () => ( {}));
5792
5794
  this.apiConn = apiConn;
5793
5795
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -5821,11 +5823,16 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5821
5823
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5822
5824
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5823
5825
  }
5824
- const flushChunkSizeEnv = Number(
5825
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5826
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5827
+ console.warn(
5828
+ "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."
5829
+ );
5830
+ }
5831
+ const flushBackpressureBytesEnv = Number(
5832
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5826
5833
  );
5827
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5828
- this.flushChunkSize = flushChunkSizeEnv;
5834
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5835
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5829
5836
  }
5830
5837
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5831
5838
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5849,6 +5856,12 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5849
5856
  setMaskingFunction(maskingFunction) {
5850
5857
  this.maskingFunction = maskingFunction;
5851
5858
  }
5859
+ pendingFlushBytes() {
5860
+ return this._pendingBytes;
5861
+ }
5862
+ flushBackpressureBytes() {
5863
+ return this._flushBackpressureBytes;
5864
+ }
5852
5865
  log(items) {
5853
5866
  if (this._disabled) {
5854
5867
  return;
@@ -5911,14 +5924,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5911
5924
  if (wrappedItems.length === 0) {
5912
5925
  return;
5913
5926
  }
5914
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
5915
- let index = 0;
5916
- while (index < wrappedItems.length) {
5917
- const chunk = wrappedItems.slice(index, index + chunkSize);
5918
- await this.flushWrappedItemsChunk(chunk, batchSize);
5919
- index += chunk.length;
5920
- }
5921
- wrappedItems.length = 0;
5927
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
5922
5928
  if (this.queue.length() > 0) {
5923
5929
  await this.flushOnce(args);
5924
5930
  }
@@ -5931,9 +5937,13 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5931
5937
  if (allItems.length === 0) {
5932
5938
  return;
5933
5939
  }
5934
- const allItemsWithMeta = allItems.map(
5935
- (item) => stringifyWithOverflowMeta(item)
5936
- );
5940
+ let chunkBytes = 0;
5941
+ const allItemsWithMeta = allItems.map((item) => {
5942
+ const withMeta = stringifyWithOverflowMeta(item);
5943
+ chunkBytes += withMeta.str.length;
5944
+ return withMeta;
5945
+ });
5946
+ this._pendingBytes += chunkBytes;
5937
5947
  const maxRequestSizeResult = await this.getMaxRequestSize();
5938
5948
  const batches = batchItems({
5939
5949
  items: allItemsWithMeta,
@@ -5952,6 +5962,7 @@ var HTTPBackgroundLogger = (_class9 = class _HTTPBackgroundLogger {
5952
5962
  })()
5953
5963
  );
5954
5964
  const results = await Promise.all(postPromises);
5965
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
5955
5966
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5956
5967
  if (failingResultErrors.length) {
5957
5968
  throw new AggregateError(
@@ -6962,13 +6973,13 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
6962
6973
  var DEFAULT_FETCH_BATCH_SIZE = 1e3;
6963
6974
  var MAX_BTQL_ITERATIONS = 1e4;
6964
6975
  var ObjectFetcher = (_class10 = class {
6965
- constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class10.prototype.__init51.call(this);
6976
+ constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class10.prototype.__init52.call(this);
6966
6977
  this.objectType = objectType;
6967
6978
  this.pinnedVersion = pinnedVersion;
6968
6979
  this.mutateRecord = mutateRecord;
6969
6980
  this._internal_btql = _internal_btql;
6970
6981
  }
6971
- __init51() {this._fetchedData = void 0}
6982
+ __init52() {this._fetchedData = void 0}
6972
6983
  get id() {
6973
6984
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
6974
6985
  }
@@ -7089,9 +7100,9 @@ var Experiment2 = (_class11 = class extends ObjectFetcher {
7089
7100
 
7090
7101
 
7091
7102
  // For type identification.
7092
- __init52() {this.kind = "experiment"}
7103
+ __init53() {this.kind = "experiment"}
7093
7104
  constructor(state, lazyMetadata, dataset) {
7094
- super("experiment", void 0, (r) => enrichAttachments(r, state));_class11.prototype.__init52.call(this);;
7105
+ super("experiment", void 0, (r) => enrichAttachments(r, state));_class11.prototype.__init53.call(this);;
7095
7106
  this.lazyMetadata = lazyMetadata;
7096
7107
  this.dataset = dataset;
7097
7108
  this.lastStartTime = getCurrentUnixTimestamp();
@@ -7450,8 +7461,8 @@ var SpanImpl = (_class12 = class _SpanImpl {
7450
7461
 
7451
7462
 
7452
7463
 
7453
- __init53() {this.kind = "span"}
7454
- constructor(args) {;_class12.prototype.__init53.call(this);
7464
+ __init54() {this.kind = "span"}
7465
+ constructor(args) {;_class12.prototype.__init54.call(this);
7455
7466
  this._state = args.state;
7456
7467
  const spanAttributes = _nullishCoalesce(args.spanAttributes, () => ( {}));
7457
7468
  const rawEvent = _nullishCoalesce(args.event, () => ( {}));
@@ -7804,13 +7815,13 @@ var Dataset2 = (_class13 = class extends ObjectFetcher {
7804
7815
  )
7805
7816
  ),
7806
7817
  _internal_btql
7807
- );_class13.prototype.__init54.call(this);_class13.prototype.__init55.call(this);;
7818
+ );_class13.prototype.__init55.call(this);_class13.prototype.__init56.call(this);;
7808
7819
  this.state = state;
7809
7820
  this.lazyMetadata = lazyMetadata;
7810
7821
  }
7811
7822
 
7812
- __init54() {this.__braintrust_dataset_marker = true}
7813
- __init55() {this.newRecords = 0}
7823
+ __init55() {this.__braintrust_dataset_marker = true}
7824
+ __init56() {this.newRecords = 0}
7814
7825
  get id() {
7815
7826
  return (async () => {
7816
7827
  return (await this.lazyMetadata.get()).dataset.id;
@@ -8151,14 +8162,14 @@ function renderPromptParams(params, args, options = {}) {
8151
8162
  return params;
8152
8163
  }
8153
8164
  var Prompt2 = (_class14 = class _Prompt {
8154
- constructor(metadata, defaults, noTrace) {;_class14.prototype.__init56.call(this);_class14.prototype.__init57.call(this);
8165
+ constructor(metadata, defaults, noTrace) {;_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);
8155
8166
  this.metadata = metadata;
8156
8167
  this.defaults = defaults;
8157
8168
  this.noTrace = noTrace;
8158
8169
  }
8159
8170
 
8160
- __init56() {this.hasParsedPromptData = false}
8161
- __init57() {this.__braintrust_prompt_marker = true}
8171
+ __init57() {this.hasParsedPromptData = false}
8172
+ __init58() {this.__braintrust_prompt_marker = true}
8162
8173
  get id() {
8163
8174
  return this.metadata.id;
8164
8175
  }
@@ -8387,10 +8398,10 @@ var Prompt2 = (_class14 = class _Prompt {
8387
8398
  }
8388
8399
  }, _class14);
8389
8400
  var RemoteEvalParameters = (_class15 = class {
8390
- constructor(metadata) {;_class15.prototype.__init58.call(this);
8401
+ constructor(metadata) {;_class15.prototype.__init59.call(this);
8391
8402
  this.metadata = metadata;
8392
8403
  }
8393
- __init58() {this.__braintrust_parameters_marker = true}
8404
+ __init59() {this.__braintrust_parameters_marker = true}
8394
8405
  get id() {
8395
8406
  return this.metadata.id;
8396
8407
  }
@@ -8547,9 +8558,9 @@ function patchStreamIfNeeded(stream, options) {
8547
8558
  }
8548
8559
 
8549
8560
  // src/instrumentation/core/plugin.ts
8550
- var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init59.call(this);_class16.prototype.__init60.call(this); }
8551
- __init59() {this.enabled = false}
8552
- __init60() {this.unsubscribers = []}
8561
+ var BasePlugin = (_class16 = class {constructor() { _class16.prototype.__init60.call(this);_class16.prototype.__init61.call(this); }
8562
+ __init60() {this.enabled = false}
8563
+ __init61() {this.unsubscribers = []}
8553
8564
  /**
8554
8565
  * Enables the plugin. Must be called before the plugin will receive events.
8555
8566
  */
@@ -9328,8 +9339,8 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
9328
9339
  }
9329
9340
 
9330
9341
  // src/instrumentation/plugins/anthropic-plugin.ts
9331
- var AnthropicPlugin = (_class17 = class extends BasePlugin {constructor(...args2) { super(...args2); _class17.prototype.__init61.call(this); }
9332
- __init61() {this.unsubscribers = []}
9342
+ var AnthropicPlugin = (_class17 = class extends BasePlugin {constructor(...args2) { super(...args2); _class17.prototype.__init62.call(this); }
9343
+ __init62() {this.unsubscribers = []}
9333
9344
  onEnable() {
9334
9345
  this.subscribeToAnthropicChannels();
9335
9346
  }
@@ -9675,10 +9686,10 @@ var DEFAULT_DENY_OUTPUT_PATHS = [
9675
9686
  "steps[].response.headers"
9676
9687
  ];
9677
9688
  var AISDKPlugin = (_class18 = class extends BasePlugin {
9678
- __init62() {this.unsubscribers = []}
9689
+ __init63() {this.unsubscribers = []}
9679
9690
 
9680
9691
  constructor(config = {}) {
9681
- super();_class18.prototype.__init62.call(this);;
9692
+ super();_class18.prototype.__init63.call(this);;
9682
9693
  this.config = config;
9683
9694
  }
9684
9695
  onEnable() {
@@ -10295,8 +10306,8 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
10295
10306
  await span.end();
10296
10307
  return _optionalChain([lastMessage, 'access', _159 => _159.message, 'optionalAccess', _160 => _160.content]) && _optionalChain([lastMessage, 'access', _161 => _161.message, 'optionalAccess', _162 => _162.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
10297
10308
  }
10298
- var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...args3) { super(...args3); _class19.prototype.__init63.call(this); }
10299
- __init63() {this.unsubscribers = []}
10309
+ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...args3) { super(...args3); _class19.prototype.__init64.call(this); }
10310
+ __init64() {this.unsubscribers = []}
10300
10311
  onEnable() {
10301
10312
  this.subscribeToQuery();
10302
10313
  }
@@ -10488,8 +10499,8 @@ var ClaudeAgentSDKPlugin = (_class19 = class extends BasePlugin {constructor(...
10488
10499
 
10489
10500
  // src/instrumentation/plugins/google-genai-plugin.ts
10490
10501
 
10491
- var GoogleGenAIPlugin = (_class20 = class extends BasePlugin {constructor(...args4) { super(...args4); _class20.prototype.__init64.call(this); }
10492
- __init64() {this.unsubscribers = []}
10502
+ var GoogleGenAIPlugin = (_class20 = class extends BasePlugin {constructor(...args4) { super(...args4); _class20.prototype.__init65.call(this); }
10503
+ __init65() {this.unsubscribers = []}
10493
10504
  onEnable() {
10494
10505
  this.subscribeToGoogleGenAIChannels();
10495
10506
  }
@@ -10916,13 +10927,13 @@ function tryToDict(obj) {
10916
10927
  // src/instrumentation/braintrust-plugin.ts
10917
10928
  var BraintrustPlugin = (_class21 = class extends BasePlugin {
10918
10929
 
10919
- __init65() {this.openaiPlugin = null}
10920
- __init66() {this.anthropicPlugin = null}
10921
- __init67() {this.aiSDKPlugin = null}
10922
- __init68() {this.claudeAgentSDKPlugin = null}
10923
- __init69() {this.googleGenAIPlugin = null}
10930
+ __init66() {this.openaiPlugin = null}
10931
+ __init67() {this.anthropicPlugin = null}
10932
+ __init68() {this.aiSDKPlugin = null}
10933
+ __init69() {this.claudeAgentSDKPlugin = null}
10934
+ __init70() {this.googleGenAIPlugin = null}
10924
10935
  constructor(config = {}) {
10925
- super();_class21.prototype.__init65.call(this);_class21.prototype.__init66.call(this);_class21.prototype.__init67.call(this);_class21.prototype.__init68.call(this);_class21.prototype.__init69.call(this);;
10936
+ super();_class21.prototype.__init66.call(this);_class21.prototype.__init67.call(this);_class21.prototype.__init68.call(this);_class21.prototype.__init69.call(this);_class21.prototype.__init70.call(this);;
10926
10937
  this.config = config;
10927
10938
  }
10928
10939
  onEnable() {
@@ -10973,10 +10984,10 @@ var BraintrustPlugin = (_class21 = class extends BasePlugin {
10973
10984
  }, _class21);
10974
10985
 
10975
10986
  // src/instrumentation/registry.ts
10976
- var PluginRegistry = (_class22 = class {constructor() { _class22.prototype.__init70.call(this);_class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this); }
10977
- __init70() {this.braintrustPlugin = null}
10978
- __init71() {this.config = {}}
10979
- __init72() {this.enabled = false}
10987
+ var PluginRegistry = (_class22 = class {constructor() { _class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this);_class22.prototype.__init73.call(this); }
10988
+ __init71() {this.braintrustPlugin = null}
10989
+ __init72() {this.config = {}}
10990
+ __init73() {this.enabled = false}
10980
10991
  /**
10981
10992
  * Configure which integrations should be enabled.
10982
10993
  * This must be called before any SDK imports to take effect.
@@ -12273,10 +12284,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
12273
12284
  }
12274
12285
  };
12275
12286
  var CachedSpanFetcher = (_class23 = class {
12276
- __init73() {this.spanCache = /* @__PURE__ */ new Map()}
12277
- __init74() {this.allFetched = false}
12287
+ __init74() {this.spanCache = /* @__PURE__ */ new Map()}
12288
+ __init75() {this.allFetched = false}
12278
12289
 
12279
- constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class23.prototype.__init73.call(this);_class23.prototype.__init74.call(this);
12290
+ constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class23.prototype.__init74.call(this);_class23.prototype.__init75.call(this);
12280
12291
  if (typeof objectTypeOrFetchFn === "function") {
12281
12292
  this.fetchFn = objectTypeOrFetchFn;
12282
12293
  } else {
@@ -12349,17 +12360,17 @@ var LocalTrace = (_class24 = class {
12349
12360
 
12350
12361
 
12351
12362
 
12352
- __init75() {this.spansFlushed = false}
12353
- __init76() {this.spansFlushPromise = null}
12363
+ __init76() {this.spansFlushed = false}
12364
+ __init77() {this.spansFlushPromise = null}
12354
12365
 
12355
- __init77() {this.threadCache = /* @__PURE__ */ new Map()}
12366
+ __init78() {this.threadCache = /* @__PURE__ */ new Map()}
12356
12367
  constructor({
12357
12368
  objectType,
12358
12369
  objectId,
12359
12370
  rootSpanId,
12360
12371
  ensureSpansFlushed,
12361
12372
  state
12362
- }) {;_class24.prototype.__init75.call(this);_class24.prototype.__init76.call(this);_class24.prototype.__init77.call(this);
12373
+ }) {;_class24.prototype.__init76.call(this);_class24.prototype.__init77.call(this);_class24.prototype.__init78.call(this);
12363
12374
  this.objectType = objectType;
12364
12375
  this.objectId = objectId;
12365
12376
  this.rootSpanId = rootSpanId;
@@ -13136,7 +13147,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13136
13147
  });
13137
13148
  } else {
13138
13149
  const result = await experiment.traced(callback, baseEvent);
13139
- if (evaluator.maxConcurrency !== void 0) {
13150
+ const bgLogger = experiment.loggingState.bgLogger();
13151
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
13140
13152
  await experiment.flush();
13141
13153
  }
13142
13154
  return result;
@@ -13690,10 +13702,10 @@ var Project2 = (_class25 = class {
13690
13702
 
13691
13703
 
13692
13704
 
13693
- __init78() {this._publishableCodeFunctions = []}
13694
- __init79() {this._publishablePrompts = []}
13695
- __init80() {this._publishableParameters = []}
13696
- constructor(args) {;_class25.prototype.__init78.call(this);_class25.prototype.__init79.call(this);_class25.prototype.__init80.call(this);
13705
+ __init79() {this._publishableCodeFunctions = []}
13706
+ __init80() {this._publishablePrompts = []}
13707
+ __init81() {this._publishableParameters = []}
13708
+ constructor(args) {;_class25.prototype.__init79.call(this);_class25.prototype.__init80.call(this);_class25.prototype.__init81.call(this);
13697
13709
  _initializeSpanContext();
13698
13710
  this.name = "name" in args ? args.name : void 0;
13699
13711
  this.id = "id" in args ? args.id : void 0;
@@ -13747,10 +13759,10 @@ var Project2 = (_class25 = class {
13747
13759
  }
13748
13760
  }, _class25);
13749
13761
  var ToolBuilder = (_class26 = class {
13750
- constructor(project) {;_class26.prototype.__init81.call(this);
13762
+ constructor(project) {;_class26.prototype.__init82.call(this);
13751
13763
  this.project = project;
13752
13764
  }
13753
- __init81() {this.taskCounter = 0}
13765
+ __init82() {this.taskCounter = 0}
13754
13766
  // This type definition is just a catch all so that the implementation can be
13755
13767
  // less specific than the two more specific declarations above.
13756
13768
  create(opts) {
@@ -13777,10 +13789,10 @@ var ToolBuilder = (_class26 = class {
13777
13789
  }
13778
13790
  }, _class26);
13779
13791
  var ScorerBuilder = (_class27 = class {
13780
- constructor(project) {;_class27.prototype.__init82.call(this);
13792
+ constructor(project) {;_class27.prototype.__init83.call(this);
13781
13793
  this.project = project;
13782
13794
  }
13783
- __init82() {this.taskCounter = 0}
13795
+ __init83() {this.taskCounter = 0}
13784
13796
  create(opts) {
13785
13797
  this.taskCounter++;
13786
13798
  let resolvedName = opts.name;
@@ -14107,9 +14119,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
14107
14119
  source: null
14108
14120
  };
14109
14121
  }
14110
- var ProjectNameIdMap = (_class28 = class {constructor() { _class28.prototype.__init83.call(this);_class28.prototype.__init84.call(this); }
14111
- __init83() {this.nameToId = {}}
14112
- __init84() {this.idToName = {}}
14122
+ var ProjectNameIdMap = (_class28 = class {constructor() { _class28.prototype.__init84.call(this);_class28.prototype.__init85.call(this); }
14123
+ __init84() {this.nameToId = {}}
14124
+ __init85() {this.idToName = {}}
14113
14125
  async getId(projectName) {
14114
14126
  if (!(projectName in this.nameToId)) {
14115
14127
  const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
@@ -5763,6 +5763,7 @@ function utf8ByteLength(value) {
5763
5763
  function now() {
5764
5764
  return (/* @__PURE__ */ new Date()).getTime();
5765
5765
  }
5766
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5766
5767
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
5767
5768
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5768
5769
  apiConn;
@@ -5781,7 +5782,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5781
5782
  queueDropLoggingPeriod = 60;
5782
5783
  failedPublishPayloadsDir = void 0;
5783
5784
  allPublishPayloadsDir = void 0;
5784
- flushChunkSize = 25;
5785
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5786
+ _pendingBytes = 0;
5785
5787
  _disabled = false;
5786
5788
  queueDropLoggingState = {
5787
5789
  numDropped: 0,
@@ -5821,11 +5823,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5821
5823
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5822
5824
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5823
5825
  }
5824
- const flushChunkSizeEnv = Number(
5825
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5826
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5827
+ console.warn(
5828
+ "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."
5829
+ );
5830
+ }
5831
+ const flushBackpressureBytesEnv = Number(
5832
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5826
5833
  );
5827
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5828
- this.flushChunkSize = flushChunkSizeEnv;
5834
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5835
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5829
5836
  }
5830
5837
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5831
5838
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5849,6 +5856,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5849
5856
  setMaskingFunction(maskingFunction) {
5850
5857
  this.maskingFunction = maskingFunction;
5851
5858
  }
5859
+ pendingFlushBytes() {
5860
+ return this._pendingBytes;
5861
+ }
5862
+ flushBackpressureBytes() {
5863
+ return this._flushBackpressureBytes;
5864
+ }
5852
5865
  log(items) {
5853
5866
  if (this._disabled) {
5854
5867
  return;
@@ -5911,14 +5924,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5911
5924
  if (wrappedItems.length === 0) {
5912
5925
  return;
5913
5926
  }
5914
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
5915
- let index = 0;
5916
- while (index < wrappedItems.length) {
5917
- const chunk = wrappedItems.slice(index, index + chunkSize);
5918
- await this.flushWrappedItemsChunk(chunk, batchSize);
5919
- index += chunk.length;
5920
- }
5921
- wrappedItems.length = 0;
5927
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
5922
5928
  if (this.queue.length() > 0) {
5923
5929
  await this.flushOnce(args);
5924
5930
  }
@@ -5931,9 +5937,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5931
5937
  if (allItems.length === 0) {
5932
5938
  return;
5933
5939
  }
5934
- const allItemsWithMeta = allItems.map(
5935
- (item) => stringifyWithOverflowMeta(item)
5936
- );
5940
+ let chunkBytes = 0;
5941
+ const allItemsWithMeta = allItems.map((item) => {
5942
+ const withMeta = stringifyWithOverflowMeta(item);
5943
+ chunkBytes += withMeta.str.length;
5944
+ return withMeta;
5945
+ });
5946
+ this._pendingBytes += chunkBytes;
5937
5947
  const maxRequestSizeResult = await this.getMaxRequestSize();
5938
5948
  const batches = batchItems({
5939
5949
  items: allItemsWithMeta,
@@ -5952,6 +5962,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5952
5962
  })()
5953
5963
  );
5954
5964
  const results = await Promise.all(postPromises);
5965
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
5955
5966
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5956
5967
  if (failingResultErrors.length) {
5957
5968
  throw new AggregateError(
@@ -13136,7 +13147,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
13136
13147
  });
13137
13148
  } else {
13138
13149
  const result = await experiment.traced(callback, baseEvent);
13139
- if (evaluator.maxConcurrency !== void 0) {
13150
+ const bgLogger = experiment.loggingState.bgLogger();
13151
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
13140
13152
  await experiment.flush();
13141
13153
  }
13142
13154
  return result;