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/index.js CHANGED
@@ -5921,6 +5921,7 @@ function utf8ByteLength(value) {
5921
5921
  function now() {
5922
5922
  return (/* @__PURE__ */ new Date()).getTime();
5923
5923
  }
5924
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5924
5925
  var TestBackgroundLogger = (_class9 = class {constructor() { _class9.prototype.__init35.call(this);_class9.prototype.__init36.call(this); }
5925
5926
  __init35() {this.items = []}
5926
5927
  __init36() {this.maskingFunction = null}
@@ -5933,6 +5934,12 @@ var TestBackgroundLogger = (_class9 = class {constructor() { _class9.prototype._
5933
5934
  async flush() {
5934
5935
  return Promise.resolve();
5935
5936
  }
5937
+ pendingFlushBytes() {
5938
+ return 0;
5939
+ }
5940
+ flushBackpressureBytes() {
5941
+ return DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5942
+ }
5936
5943
  async drain() {
5937
5944
  const items = this.items;
5938
5945
  this.items = [];
@@ -5990,13 +5997,14 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
5990
5997
  __init47() {this.queueDropLoggingPeriod = 60}
5991
5998
  __init48() {this.failedPublishPayloadsDir = void 0}
5992
5999
  __init49() {this.allPublishPayloadsDir = void 0}
5993
- __init50() {this.flushChunkSize = 25}
5994
- __init51() {this._disabled = false}
5995
- __init52() {this.queueDropLoggingState = {
6000
+ __init50() {this._flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES}
6001
+ __init51() {this._pendingBytes = 0}
6002
+ __init52() {this._disabled = false}
6003
+ __init53() {this.queueDropLoggingState = {
5996
6004
  numDropped: 0,
5997
6005
  lastLoggedTimestamp: 0
5998
6006
  }}
5999
- constructor(apiConn, opts) {;_class10.prototype.__init37.call(this);_class10.prototype.__init38.call(this);_class10.prototype.__init39.call(this);_class10.prototype.__init40.call(this);_class10.prototype.__init41.call(this);_class10.prototype.__init42.call(this);_class10.prototype.__init43.call(this);_class10.prototype.__init44.call(this);_class10.prototype.__init45.call(this);_class10.prototype.__init46.call(this);_class10.prototype.__init47.call(this);_class10.prototype.__init48.call(this);_class10.prototype.__init49.call(this);_class10.prototype.__init50.call(this);_class10.prototype.__init51.call(this);_class10.prototype.__init52.call(this);
6007
+ constructor(apiConn, opts) {;_class10.prototype.__init37.call(this);_class10.prototype.__init38.call(this);_class10.prototype.__init39.call(this);_class10.prototype.__init40.call(this);_class10.prototype.__init41.call(this);_class10.prototype.__init42.call(this);_class10.prototype.__init43.call(this);_class10.prototype.__init44.call(this);_class10.prototype.__init45.call(this);_class10.prototype.__init46.call(this);_class10.prototype.__init47.call(this);_class10.prototype.__init48.call(this);_class10.prototype.__init49.call(this);_class10.prototype.__init50.call(this);_class10.prototype.__init51.call(this);_class10.prototype.__init52.call(this);_class10.prototype.__init53.call(this);
6000
6008
  opts = _nullishCoalesce(opts, () => ( {}));
6001
6009
  this.apiConn = apiConn;
6002
6010
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -6030,11 +6038,16 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6030
6038
  if (!isNaN(queueDropLoggingPeriodEnv)) {
6031
6039
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
6032
6040
  }
6033
- const flushChunkSizeEnv = Number(
6034
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
6041
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
6042
+ console.warn(
6043
+ "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."
6044
+ );
6045
+ }
6046
+ const flushBackpressureBytesEnv = Number(
6047
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
6035
6048
  );
6036
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
6037
- this.flushChunkSize = flushChunkSizeEnv;
6049
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
6050
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
6038
6051
  }
6039
6052
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
6040
6053
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -6058,6 +6071,12 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6058
6071
  setMaskingFunction(maskingFunction) {
6059
6072
  this.maskingFunction = maskingFunction;
6060
6073
  }
6074
+ pendingFlushBytes() {
6075
+ return this._pendingBytes;
6076
+ }
6077
+ flushBackpressureBytes() {
6078
+ return this._flushBackpressureBytes;
6079
+ }
6061
6080
  log(items) {
6062
6081
  if (this._disabled) {
6063
6082
  return;
@@ -6120,14 +6139,7 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6120
6139
  if (wrappedItems.length === 0) {
6121
6140
  return;
6122
6141
  }
6123
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
6124
- let index = 0;
6125
- while (index < wrappedItems.length) {
6126
- const chunk = wrappedItems.slice(index, index + chunkSize);
6127
- await this.flushWrappedItemsChunk(chunk, batchSize);
6128
- index += chunk.length;
6129
- }
6130
- wrappedItems.length = 0;
6142
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
6131
6143
  if (this.queue.length() > 0) {
6132
6144
  await this.flushOnce(args);
6133
6145
  }
@@ -6140,9 +6152,13 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6140
6152
  if (allItems.length === 0) {
6141
6153
  return;
6142
6154
  }
6143
- const allItemsWithMeta = allItems.map(
6144
- (item) => stringifyWithOverflowMeta(item)
6145
- );
6155
+ let chunkBytes = 0;
6156
+ const allItemsWithMeta = allItems.map((item) => {
6157
+ const withMeta = stringifyWithOverflowMeta(item);
6158
+ chunkBytes += withMeta.str.length;
6159
+ return withMeta;
6160
+ });
6161
+ this._pendingBytes += chunkBytes;
6146
6162
  const maxRequestSizeResult = await this.getMaxRequestSize();
6147
6163
  const batches = batchItems({
6148
6164
  items: allItemsWithMeta,
@@ -6161,6 +6177,7 @@ var HTTPBackgroundLogger = (_class10 = class _HTTPBackgroundLogger {
6161
6177
  })()
6162
6178
  );
6163
6179
  const results = await Promise.all(postPromises);
6180
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
6164
6181
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
6165
6182
  if (failingResultErrors.length) {
6166
6183
  throw new AggregateError(
@@ -7719,13 +7736,13 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
7719
7736
  var DEFAULT_FETCH_BATCH_SIZE = 1e3;
7720
7737
  var MAX_BTQL_ITERATIONS = 1e4;
7721
7738
  var ObjectFetcher = (_class11 = class {
7722
- constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class11.prototype.__init53.call(this);
7739
+ constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class11.prototype.__init54.call(this);
7723
7740
  this.objectType = objectType;
7724
7741
  this.pinnedVersion = pinnedVersion;
7725
7742
  this.mutateRecord = mutateRecord;
7726
7743
  this._internal_btql = _internal_btql;
7727
7744
  }
7728
- __init53() {this._fetchedData = void 0}
7745
+ __init54() {this._fetchedData = void 0}
7729
7746
  get id() {
7730
7747
  throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
7731
7748
  }
@@ -7846,9 +7863,9 @@ var Experiment2 = (_class12 = class extends ObjectFetcher {
7846
7863
 
7847
7864
 
7848
7865
  // For type identification.
7849
- __init54() {this.kind = "experiment"}
7866
+ __init55() {this.kind = "experiment"}
7850
7867
  constructor(state, lazyMetadata, dataset) {
7851
- super("experiment", void 0, (r) => enrichAttachments(r, state));_class12.prototype.__init54.call(this);;
7868
+ super("experiment", void 0, (r) => enrichAttachments(r, state));_class12.prototype.__init55.call(this);;
7852
7869
  this.lazyMetadata = lazyMetadata;
7853
7870
  this.dataset = dataset;
7854
7871
  this.lastStartTime = getCurrentUnixTimestamp();
@@ -8207,8 +8224,8 @@ var SpanImpl = (_class13 = class _SpanImpl {
8207
8224
 
8208
8225
 
8209
8226
 
8210
- __init55() {this.kind = "span"}
8211
- constructor(args) {;_class13.prototype.__init55.call(this);
8227
+ __init56() {this.kind = "span"}
8228
+ constructor(args) {;_class13.prototype.__init56.call(this);
8212
8229
  this._state = args.state;
8213
8230
  const spanAttributes = _nullishCoalesce(args.spanAttributes, () => ( {}));
8214
8231
  const rawEvent = _nullishCoalesce(args.event, () => ( {}));
@@ -8561,13 +8578,13 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
8561
8578
  )
8562
8579
  ),
8563
8580
  _internal_btql
8564
- );_class14.prototype.__init56.call(this);_class14.prototype.__init57.call(this);;
8581
+ );_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);;
8565
8582
  this.state = state;
8566
8583
  this.lazyMetadata = lazyMetadata;
8567
8584
  }
8568
8585
 
8569
- __init56() {this.__braintrust_dataset_marker = true}
8570
- __init57() {this.newRecords = 0}
8586
+ __init57() {this.__braintrust_dataset_marker = true}
8587
+ __init58() {this.newRecords = 0}
8571
8588
  get id() {
8572
8589
  return (async () => {
8573
8590
  return (await this.lazyMetadata.get()).dataset.id;
@@ -8908,14 +8925,14 @@ function renderPromptParams(params, args, options = {}) {
8908
8925
  return params;
8909
8926
  }
8910
8927
  var Prompt2 = (_class15 = class _Prompt {
8911
- constructor(metadata, defaults, noTrace) {;_class15.prototype.__init58.call(this);_class15.prototype.__init59.call(this);
8928
+ constructor(metadata, defaults, noTrace) {;_class15.prototype.__init59.call(this);_class15.prototype.__init60.call(this);
8912
8929
  this.metadata = metadata;
8913
8930
  this.defaults = defaults;
8914
8931
  this.noTrace = noTrace;
8915
8932
  }
8916
8933
 
8917
- __init58() {this.hasParsedPromptData = false}
8918
- __init59() {this.__braintrust_prompt_marker = true}
8934
+ __init59() {this.hasParsedPromptData = false}
8935
+ __init60() {this.__braintrust_prompt_marker = true}
8919
8936
  get id() {
8920
8937
  return this.metadata.id;
8921
8938
  }
@@ -9144,10 +9161,10 @@ var Prompt2 = (_class15 = class _Prompt {
9144
9161
  }
9145
9162
  }, _class15);
9146
9163
  var RemoteEvalParameters = (_class16 = class {
9147
- constructor(metadata) {;_class16.prototype.__init60.call(this);
9164
+ constructor(metadata) {;_class16.prototype.__init61.call(this);
9148
9165
  this.metadata = metadata;
9149
9166
  }
9150
- __init60() {this.__braintrust_parameters_marker = true}
9167
+ __init61() {this.__braintrust_parameters_marker = true}
9151
9168
  get id() {
9152
9169
  return this.metadata.id;
9153
9170
  }
@@ -9394,9 +9411,9 @@ function patchStreamIfNeeded(stream, options) {
9394
9411
  }
9395
9412
 
9396
9413
  // src/instrumentation/core/plugin.ts
9397
- var BasePlugin = (_class17 = class {constructor() { _class17.prototype.__init61.call(this);_class17.prototype.__init62.call(this); }
9398
- __init61() {this.enabled = false}
9399
- __init62() {this.unsubscribers = []}
9414
+ var BasePlugin = (_class17 = class {constructor() { _class17.prototype.__init62.call(this);_class17.prototype.__init63.call(this); }
9415
+ __init62() {this.enabled = false}
9416
+ __init63() {this.unsubscribers = []}
9400
9417
  /**
9401
9418
  * Enables the plugin. Must be called before the plugin will receive events.
9402
9419
  */
@@ -10175,8 +10192,8 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
10175
10192
  }
10176
10193
 
10177
10194
  // src/instrumentation/plugins/anthropic-plugin.ts
10178
- var AnthropicPlugin = (_class18 = class extends BasePlugin {constructor(...args2) { super(...args2); _class18.prototype.__init63.call(this); }
10179
- __init63() {this.unsubscribers = []}
10195
+ var AnthropicPlugin = (_class18 = class extends BasePlugin {constructor(...args2) { super(...args2); _class18.prototype.__init64.call(this); }
10196
+ __init64() {this.unsubscribers = []}
10180
10197
  onEnable() {
10181
10198
  this.subscribeToAnthropicChannels();
10182
10199
  }
@@ -10522,10 +10539,10 @@ var DEFAULT_DENY_OUTPUT_PATHS = [
10522
10539
  "steps[].response.headers"
10523
10540
  ];
10524
10541
  var AISDKPlugin = (_class19 = class extends BasePlugin {
10525
- __init64() {this.unsubscribers = []}
10542
+ __init65() {this.unsubscribers = []}
10526
10543
 
10527
10544
  constructor(config = {}) {
10528
- super();_class19.prototype.__init64.call(this);;
10545
+ super();_class19.prototype.__init65.call(this);;
10529
10546
  this.config = config;
10530
10547
  }
10531
10548
  onEnable() {
@@ -11142,8 +11159,8 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
11142
11159
  await span.end();
11143
11160
  return _optionalChain([lastMessage, 'access', _176 => _176.message, 'optionalAccess', _177 => _177.content]) && _optionalChain([lastMessage, 'access', _178 => _178.message, 'optionalAccess', _179 => _179.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
11144
11161
  }
11145
- var ClaudeAgentSDKPlugin = (_class20 = class extends BasePlugin {constructor(...args3) { super(...args3); _class20.prototype.__init65.call(this); }
11146
- __init65() {this.unsubscribers = []}
11162
+ var ClaudeAgentSDKPlugin = (_class20 = class extends BasePlugin {constructor(...args3) { super(...args3); _class20.prototype.__init66.call(this); }
11163
+ __init66() {this.unsubscribers = []}
11147
11164
  onEnable() {
11148
11165
  this.subscribeToQuery();
11149
11166
  }
@@ -11335,8 +11352,8 @@ var ClaudeAgentSDKPlugin = (_class20 = class extends BasePlugin {constructor(...
11335
11352
 
11336
11353
  // src/instrumentation/plugins/google-genai-plugin.ts
11337
11354
 
11338
- var GoogleGenAIPlugin = (_class21 = class extends BasePlugin {constructor(...args4) { super(...args4); _class21.prototype.__init66.call(this); }
11339
- __init66() {this.unsubscribers = []}
11355
+ var GoogleGenAIPlugin = (_class21 = class extends BasePlugin {constructor(...args4) { super(...args4); _class21.prototype.__init67.call(this); }
11356
+ __init67() {this.unsubscribers = []}
11340
11357
  onEnable() {
11341
11358
  this.subscribeToGoogleGenAIChannels();
11342
11359
  }
@@ -11763,13 +11780,13 @@ function tryToDict(obj) {
11763
11780
  // src/instrumentation/braintrust-plugin.ts
11764
11781
  var BraintrustPlugin = (_class22 = class extends BasePlugin {
11765
11782
 
11766
- __init67() {this.openaiPlugin = null}
11767
- __init68() {this.anthropicPlugin = null}
11768
- __init69() {this.aiSDKPlugin = null}
11769
- __init70() {this.claudeAgentSDKPlugin = null}
11770
- __init71() {this.googleGenAIPlugin = null}
11783
+ __init68() {this.openaiPlugin = null}
11784
+ __init69() {this.anthropicPlugin = null}
11785
+ __init70() {this.aiSDKPlugin = null}
11786
+ __init71() {this.claudeAgentSDKPlugin = null}
11787
+ __init72() {this.googleGenAIPlugin = null}
11771
11788
  constructor(config = {}) {
11772
- super();_class22.prototype.__init67.call(this);_class22.prototype.__init68.call(this);_class22.prototype.__init69.call(this);_class22.prototype.__init70.call(this);_class22.prototype.__init71.call(this);;
11789
+ super();_class22.prototype.__init68.call(this);_class22.prototype.__init69.call(this);_class22.prototype.__init70.call(this);_class22.prototype.__init71.call(this);_class22.prototype.__init72.call(this);;
11773
11790
  this.config = config;
11774
11791
  }
11775
11792
  onEnable() {
@@ -11820,10 +11837,10 @@ var BraintrustPlugin = (_class22 = class extends BasePlugin {
11820
11837
  }, _class22);
11821
11838
 
11822
11839
  // src/instrumentation/registry.ts
11823
- var PluginRegistry = (_class23 = class {constructor() { _class23.prototype.__init72.call(this);_class23.prototype.__init73.call(this);_class23.prototype.__init74.call(this); }
11824
- __init72() {this.braintrustPlugin = null}
11825
- __init73() {this.config = {}}
11826
- __init74() {this.enabled = false}
11840
+ var PluginRegistry = (_class23 = class {constructor() { _class23.prototype.__init73.call(this);_class23.prototype.__init74.call(this);_class23.prototype.__init75.call(this); }
11841
+ __init73() {this.braintrustPlugin = null}
11842
+ __init74() {this.config = {}}
11843
+ __init75() {this.enabled = false}
11827
11844
  /**
11828
11845
  * Configure which integrations should be enabled.
11829
11846
  * This must be called before any SDK imports to take effect.
@@ -16432,8 +16449,8 @@ function getVitestContextManager() {
16432
16449
  }
16433
16450
 
16434
16451
  // src/wrappers/vitest/flush-manager.ts
16435
- var FlushCoordinator = (_class24 = class {constructor() { _class24.prototype.__init75.call(this); }
16436
- __init75() {this.activeFlushes = /* @__PURE__ */ new Map()}
16452
+ var FlushCoordinator = (_class24 = class {constructor() { _class24.prototype.__init76.call(this); }
16453
+ __init76() {this.activeFlushes = /* @__PURE__ */ new Map()}
16437
16454
  async coordinateFlush(context, config) {
16438
16455
  if (!context) return;
16439
16456
  const experimentId = await context.experiment.id;
@@ -16984,14 +17001,14 @@ __export(graph_framework_exports, {
16984
17001
  unescapePath: () => unescapePath
16985
17002
  });
16986
17003
  var GraphBuilder = (_class25 = class {
16987
- __init76() {this.nodes = /* @__PURE__ */ new Map()}
16988
- __init77() {this.edges = {}}
16989
- __init78() {this.nodeLikeNodes = /* @__PURE__ */ new Map()}
17004
+ __init77() {this.nodes = /* @__PURE__ */ new Map()}
17005
+ __init78() {this.edges = {}}
17006
+ __init79() {this.nodeLikeNodes = /* @__PURE__ */ new Map()}
16990
17007
  // Maps node-like objects, like prompts, to their nodes
16991
17008
  // Special nodes
16992
17009
 
16993
17010
 
16994
- constructor() {;_class25.prototype.__init76.call(this);_class25.prototype.__init77.call(this);_class25.prototype.__init78.call(this);
17011
+ constructor() {;_class25.prototype.__init77.call(this);_class25.prototype.__init78.call(this);_class25.prototype.__init79.call(this);
16995
17012
  this.IN = this.createInputNode();
16996
17013
  this.OUT = this.createOutputNode();
16997
17014
  }
@@ -17130,12 +17147,12 @@ function proxyVariableToNode(proxy) {
17130
17147
  return [proxy.__node, proxy.__path];
17131
17148
  }
17132
17149
  var BaseNode = (_class26 = class {
17133
- constructor(graph, id) {;_class26.prototype.__init79.call(this);_class26.prototype.__init80.call(this);
17150
+ constructor(graph, id) {;_class26.prototype.__init80.call(this);_class26.prototype.__init81.call(this);
17134
17151
  this.graph = graph;
17135
17152
  this.id = id;
17136
17153
  }
17137
- __init79() {this.__type = "node"}
17138
- __init80() {this.dependencies = []}
17154
+ __init80() {this.__type = "node"}
17155
+ __init81() {this.dependencies = []}
17139
17156
  addDependency(dependency) {
17140
17157
  this.dependencies.push(dependency);
17141
17158
  }
@@ -18342,10 +18359,10 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
18342
18359
  }
18343
18360
  };
18344
18361
  var CachedSpanFetcher = (_class27 = class {
18345
- __init81() {this.spanCache = /* @__PURE__ */ new Map()}
18346
- __init82() {this.allFetched = false}
18362
+ __init82() {this.spanCache = /* @__PURE__ */ new Map()}
18363
+ __init83() {this.allFetched = false}
18347
18364
 
18348
- constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class27.prototype.__init81.call(this);_class27.prototype.__init82.call(this);
18365
+ constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class27.prototype.__init82.call(this);_class27.prototype.__init83.call(this);
18349
18366
  if (typeof objectTypeOrFetchFn === "function") {
18350
18367
  this.fetchFn = objectTypeOrFetchFn;
18351
18368
  } else {
@@ -18418,17 +18435,17 @@ var LocalTrace = (_class28 = class {
18418
18435
 
18419
18436
 
18420
18437
 
18421
- __init83() {this.spansFlushed = false}
18422
- __init84() {this.spansFlushPromise = null}
18438
+ __init84() {this.spansFlushed = false}
18439
+ __init85() {this.spansFlushPromise = null}
18423
18440
 
18424
- __init85() {this.threadCache = /* @__PURE__ */ new Map()}
18441
+ __init86() {this.threadCache = /* @__PURE__ */ new Map()}
18425
18442
  constructor({
18426
18443
  objectType,
18427
18444
  objectId,
18428
18445
  rootSpanId,
18429
18446
  ensureSpansFlushed,
18430
18447
  state
18431
- }) {;_class28.prototype.__init83.call(this);_class28.prototype.__init84.call(this);_class28.prototype.__init85.call(this);
18448
+ }) {;_class28.prototype.__init84.call(this);_class28.prototype.__init85.call(this);_class28.prototype.__init86.call(this);
18432
18449
  this.objectType = objectType;
18433
18450
  this.objectId = objectId;
18434
18451
  this.rootSpanId = rootSpanId;
@@ -19227,7 +19244,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
19227
19244
  });
19228
19245
  } else {
19229
19246
  const result = await experiment.traced(callback, baseEvent);
19230
- if (evaluator.maxConcurrency !== void 0) {
19247
+ const bgLogger = experiment.loggingState.bgLogger();
19248
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
19231
19249
  await experiment.flush();
19232
19250
  }
19233
19251
  return result;
@@ -19506,10 +19524,10 @@ var Project2 = (_class29 = class {
19506
19524
 
19507
19525
 
19508
19526
 
19509
- __init86() {this._publishableCodeFunctions = []}
19510
- __init87() {this._publishablePrompts = []}
19511
- __init88() {this._publishableParameters = []}
19512
- constructor(args) {;_class29.prototype.__init86.call(this);_class29.prototype.__init87.call(this);_class29.prototype.__init88.call(this);
19527
+ __init87() {this._publishableCodeFunctions = []}
19528
+ __init88() {this._publishablePrompts = []}
19529
+ __init89() {this._publishableParameters = []}
19530
+ constructor(args) {;_class29.prototype.__init87.call(this);_class29.prototype.__init88.call(this);_class29.prototype.__init89.call(this);
19513
19531
  _initializeSpanContext();
19514
19532
  this.name = "name" in args ? args.name : void 0;
19515
19533
  this.id = "id" in args ? args.id : void 0;
@@ -19563,10 +19581,10 @@ var Project2 = (_class29 = class {
19563
19581
  }
19564
19582
  }, _class29);
19565
19583
  var ToolBuilder = (_class30 = class {
19566
- constructor(project) {;_class30.prototype.__init89.call(this);
19584
+ constructor(project) {;_class30.prototype.__init90.call(this);
19567
19585
  this.project = project;
19568
19586
  }
19569
- __init89() {this.taskCounter = 0}
19587
+ __init90() {this.taskCounter = 0}
19570
19588
  // This type definition is just a catch all so that the implementation can be
19571
19589
  // less specific than the two more specific declarations above.
19572
19590
  create(opts) {
@@ -19593,10 +19611,10 @@ var ToolBuilder = (_class30 = class {
19593
19611
  }
19594
19612
  }, _class30);
19595
19613
  var ScorerBuilder = (_class31 = class {
19596
- constructor(project) {;_class31.prototype.__init90.call(this);
19614
+ constructor(project) {;_class31.prototype.__init91.call(this);
19597
19615
  this.project = project;
19598
19616
  }
19599
- __init90() {this.taskCounter = 0}
19617
+ __init91() {this.taskCounter = 0}
19600
19618
  create(opts) {
19601
19619
  this.taskCounter++;
19602
19620
  let resolvedName = opts.name;
@@ -19875,9 +19893,9 @@ function getDefaultDataFromParametersSchema(schema) {
19875
19893
  })
19876
19894
  );
19877
19895
  }
19878
- var ProjectNameIdMap = (_class32 = class {constructor() { _class32.prototype.__init91.call(this);_class32.prototype.__init92.call(this); }
19879
- __init91() {this.nameToId = {}}
19880
- __init92() {this.idToName = {}}
19896
+ var ProjectNameIdMap = (_class32 = class {constructor() { _class32.prototype.__init92.call(this);_class32.prototype.__init93.call(this); }
19897
+ __init92() {this.nameToId = {}}
19898
+ __init93() {this.idToName = {}}
19881
19899
  async getId(projectName) {
19882
19900
  if (!(projectName in this.nameToId)) {
19883
19901
  const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
package/dist/index.mjs CHANGED
@@ -5921,6 +5921,7 @@ function utf8ByteLength(value) {
5921
5921
  function now() {
5922
5922
  return (/* @__PURE__ */ new Date()).getTime();
5923
5923
  }
5924
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5924
5925
  var TestBackgroundLogger = class {
5925
5926
  items = [];
5926
5927
  maskingFunction = null;
@@ -5933,6 +5934,12 @@ var TestBackgroundLogger = class {
5933
5934
  async flush() {
5934
5935
  return Promise.resolve();
5935
5936
  }
5937
+ pendingFlushBytes() {
5938
+ return 0;
5939
+ }
5940
+ flushBackpressureBytes() {
5941
+ return DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5942
+ }
5936
5943
  async drain() {
5937
5944
  const items = this.items;
5938
5945
  this.items = [];
@@ -5990,7 +5997,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5990
5997
  queueDropLoggingPeriod = 60;
5991
5998
  failedPublishPayloadsDir = void 0;
5992
5999
  allPublishPayloadsDir = void 0;
5993
- flushChunkSize = 25;
6000
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
6001
+ _pendingBytes = 0;
5994
6002
  _disabled = false;
5995
6003
  queueDropLoggingState = {
5996
6004
  numDropped: 0,
@@ -6030,11 +6038,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6030
6038
  if (!isNaN(queueDropLoggingPeriodEnv)) {
6031
6039
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
6032
6040
  }
6033
- const flushChunkSizeEnv = Number(
6034
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
6041
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
6042
+ console.warn(
6043
+ "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."
6044
+ );
6045
+ }
6046
+ const flushBackpressureBytesEnv = Number(
6047
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
6035
6048
  );
6036
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
6037
- this.flushChunkSize = flushChunkSizeEnv;
6049
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
6050
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
6038
6051
  }
6039
6052
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
6040
6053
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -6058,6 +6071,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6058
6071
  setMaskingFunction(maskingFunction) {
6059
6072
  this.maskingFunction = maskingFunction;
6060
6073
  }
6074
+ pendingFlushBytes() {
6075
+ return this._pendingBytes;
6076
+ }
6077
+ flushBackpressureBytes() {
6078
+ return this._flushBackpressureBytes;
6079
+ }
6061
6080
  log(items) {
6062
6081
  if (this._disabled) {
6063
6082
  return;
@@ -6120,14 +6139,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6120
6139
  if (wrappedItems.length === 0) {
6121
6140
  return;
6122
6141
  }
6123
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
6124
- let index = 0;
6125
- while (index < wrappedItems.length) {
6126
- const chunk = wrappedItems.slice(index, index + chunkSize);
6127
- await this.flushWrappedItemsChunk(chunk, batchSize);
6128
- index += chunk.length;
6129
- }
6130
- wrappedItems.length = 0;
6142
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
6131
6143
  if (this.queue.length() > 0) {
6132
6144
  await this.flushOnce(args);
6133
6145
  }
@@ -6140,9 +6152,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6140
6152
  if (allItems.length === 0) {
6141
6153
  return;
6142
6154
  }
6143
- const allItemsWithMeta = allItems.map(
6144
- (item) => stringifyWithOverflowMeta(item)
6145
- );
6155
+ let chunkBytes = 0;
6156
+ const allItemsWithMeta = allItems.map((item) => {
6157
+ const withMeta = stringifyWithOverflowMeta(item);
6158
+ chunkBytes += withMeta.str.length;
6159
+ return withMeta;
6160
+ });
6161
+ this._pendingBytes += chunkBytes;
6146
6162
  const maxRequestSizeResult = await this.getMaxRequestSize();
6147
6163
  const batches = batchItems({
6148
6164
  items: allItemsWithMeta,
@@ -6161,6 +6177,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
6161
6177
  })()
6162
6178
  );
6163
6179
  const results = await Promise.all(postPromises);
6180
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
6164
6181
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
6165
6182
  if (failingResultErrors.length) {
6166
6183
  throw new AggregateError(
@@ -19227,7 +19244,8 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
19227
19244
  });
19228
19245
  } else {
19229
19246
  const result = await experiment.traced(callback, baseEvent);
19230
- if (evaluator.maxConcurrency !== void 0) {
19247
+ const bgLogger = experiment.loggingState.bgLogger();
19248
+ if (evaluator.maxConcurrency !== void 0 && bgLogger.pendingFlushBytes() >= bgLogger.flushBackpressureBytes()) {
19231
19249
  await experiment.flush();
19232
19250
  }
19233
19251
  return result;
@@ -5499,6 +5499,7 @@ function utf8ByteLength(value) {
5499
5499
  function now() {
5500
5500
  return (/* @__PURE__ */ new Date()).getTime();
5501
5501
  }
5502
+ var DEFAULT_FLUSH_BACKPRESSURE_BYTES = 10 * 1024 * 1024;
5502
5503
  var BACKGROUND_LOGGER_BASE_SLEEP_TIME_S = 1;
5503
5504
  var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5504
5505
  apiConn;
@@ -5517,7 +5518,8 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5517
5518
  queueDropLoggingPeriod = 60;
5518
5519
  failedPublishPayloadsDir = void 0;
5519
5520
  allPublishPayloadsDir = void 0;
5520
- flushChunkSize = 25;
5521
+ _flushBackpressureBytes = DEFAULT_FLUSH_BACKPRESSURE_BYTES;
5522
+ _pendingBytes = 0;
5521
5523
  _disabled = false;
5522
5524
  queueDropLoggingState = {
5523
5525
  numDropped: 0,
@@ -5557,11 +5559,16 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5557
5559
  if (!isNaN(queueDropLoggingPeriodEnv)) {
5558
5560
  this.queueDropLoggingPeriod = queueDropLoggingPeriodEnv;
5559
5561
  }
5560
- const flushChunkSizeEnv = Number(
5561
- isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")
5562
+ if (isomorph_default.getEnv("BRAINTRUST_LOG_FLUSH_CHUNK_SIZE")) {
5563
+ console.warn(
5564
+ "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."
5565
+ );
5566
+ }
5567
+ const flushBackpressureBytesEnv = Number(
5568
+ isomorph_default.getEnv("BRAINTRUST_FLUSH_BACKPRESSURE_BYTES")
5562
5569
  );
5563
- if (!isNaN(flushChunkSizeEnv) && flushChunkSizeEnv > 0) {
5564
- this.flushChunkSize = flushChunkSizeEnv;
5570
+ if (!isNaN(flushBackpressureBytesEnv) && flushBackpressureBytesEnv > 0) {
5571
+ this._flushBackpressureBytes = flushBackpressureBytesEnv;
5565
5572
  }
5566
5573
  const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
5567
5574
  "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
@@ -5585,6 +5592,12 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5585
5592
  setMaskingFunction(maskingFunction) {
5586
5593
  this.maskingFunction = maskingFunction;
5587
5594
  }
5595
+ pendingFlushBytes() {
5596
+ return this._pendingBytes;
5597
+ }
5598
+ flushBackpressureBytes() {
5599
+ return this._flushBackpressureBytes;
5600
+ }
5588
5601
  log(items) {
5589
5602
  if (this._disabled) {
5590
5603
  return;
@@ -5647,14 +5660,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5647
5660
  if (wrappedItems.length === 0) {
5648
5661
  return;
5649
5662
  }
5650
- const chunkSize = Math.max(1, Math.min(batchSize, this.flushChunkSize));
5651
- let index = 0;
5652
- while (index < wrappedItems.length) {
5653
- const chunk = wrappedItems.slice(index, index + chunkSize);
5654
- await this.flushWrappedItemsChunk(chunk, batchSize);
5655
- index += chunk.length;
5656
- }
5657
- wrappedItems.length = 0;
5663
+ await this.flushWrappedItemsChunk(wrappedItems, batchSize);
5658
5664
  if (this.queue.length() > 0) {
5659
5665
  await this.flushOnce(args);
5660
5666
  }
@@ -5667,9 +5673,13 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5667
5673
  if (allItems.length === 0) {
5668
5674
  return;
5669
5675
  }
5670
- const allItemsWithMeta = allItems.map(
5671
- (item) => stringifyWithOverflowMeta(item)
5672
- );
5676
+ let chunkBytes = 0;
5677
+ const allItemsWithMeta = allItems.map((item) => {
5678
+ const withMeta = stringifyWithOverflowMeta(item);
5679
+ chunkBytes += withMeta.str.length;
5680
+ return withMeta;
5681
+ });
5682
+ this._pendingBytes += chunkBytes;
5673
5683
  const maxRequestSizeResult = await this.getMaxRequestSize();
5674
5684
  const batches = batchItems({
5675
5685
  items: allItemsWithMeta,
@@ -5688,6 +5698,7 @@ var HTTPBackgroundLogger = class _HTTPBackgroundLogger {
5688
5698
  })()
5689
5699
  );
5690
5700
  const results = await Promise.all(postPromises);
5701
+ this._pendingBytes = Math.max(0, this._pendingBytes - chunkBytes);
5691
5702
  const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
5692
5703
  if (failingResultErrors.length) {
5693
5704
  throw new AggregateError(