braintrust 0.0.100 → 0.0.101

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
@@ -673,57 +673,74 @@ function castLogger(logger, asyncFlush) {
673
673
  }
674
674
  return logger;
675
675
  }
676
- var MaxRequestSize = 6 * 1024 * 1024;
677
676
  function constructJsonArray(items) {
678
677
  return `[${items.join(",")}]`;
679
678
  }
680
679
  function constructLogs3Data(items) {
681
680
  return `{"rows": ${constructJsonArray(items)}, "api_version": 2}`;
682
681
  }
683
- var DefaultBatchSize = 100;
684
- var NumRetries = 3;
685
682
  function now() {
686
683
  return (/* @__PURE__ */ new Date()).getTime();
687
684
  }
688
685
  var BackgroundLogger = class {
689
686
  logConn;
690
687
  items = [];
691
- active_flush = Promise.resolve([]);
692
- active_flush_resolved = true;
688
+ activeFlush = Promise.resolve();
689
+ activeFlushResolved = true;
690
+ syncFlush = false;
691
+ // 6 MB for the AWS lambda gateway (from our own testing).
692
+ maxRequestSize = 6 * 1024 * 1024;
693
+ defaultBatchSize = 100;
694
+ numRetries = 3;
693
695
  constructor(logConn) {
694
696
  this.logConn = logConn;
697
+ const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
698
+ if (!isNaN(syncFlushEnv)) {
699
+ this.syncFlush = Boolean(syncFlushEnv);
700
+ }
701
+ const defaultBatchSizeEnv = Number(
702
+ isomorph_default.getEnv("BRAINTRUST_DEFAULT_BATCH_SIZE")
703
+ );
704
+ if (!isNaN(defaultBatchSizeEnv)) {
705
+ this.defaultBatchSize = defaultBatchSizeEnv;
706
+ }
707
+ const maxRequestSizeEnv = Number(isomorph_default.getEnv("BRAINTRUST_MAX_REQUEST_SIZE"));
708
+ if (!isNaN(maxRequestSizeEnv)) {
709
+ this.maxRequestSize = maxRequestSizeEnv;
710
+ }
711
+ const numRetriesEnv = Number(isomorph_default.getEnv("BRAINTRUST_NUM_RETRIES"));
712
+ if (!isNaN(numRetriesEnv)) {
713
+ this.numRetries = numRetriesEnv;
714
+ }
695
715
  isomorph_default.processOn("beforeExit", async () => {
696
716
  await this.flush();
697
717
  });
698
718
  }
699
719
  log(items) {
700
720
  this.items.push(...items);
701
- if (this.active_flush_resolved) {
702
- this.active_flush_resolved = false;
703
- this.active_flush = this.flush_once();
721
+ if (!this.syncFlush) {
722
+ this.triggerActiveFlush();
704
723
  }
705
724
  }
706
- async flush_once(batchSize = DefaultBatchSize) {
707
- this.active_flush_resolved = false;
708
- const itemLazyValues = this.items;
725
+ async flush() {
726
+ if (this.syncFlush) {
727
+ this.triggerActiveFlush();
728
+ }
729
+ await this.activeFlush;
730
+ }
731
+ async flushOnce(args) {
732
+ const batchSize = args?.batchSize ?? this.defaultBatchSize;
733
+ const wrappedItems = this.items;
709
734
  this.items = [];
710
- const allItems = await (async () => {
711
- try {
712
- const itemPromises = itemLazyValues.map((x) => x.get());
713
- return mergeRowBatch(await Promise.all(itemPromises)).reverse();
714
- } catch (e) {
715
- console.warn(
716
- "Encountered error when constructing records to flush:\n",
717
- e
718
- );
719
- return [];
720
- }
721
- })();
722
- let postPromises = [];
735
+ const allItems = await this.unwrapLazyValues(wrappedItems);
736
+ if (allItems.length === 0) {
737
+ return;
738
+ }
739
+ const postPromises = [];
723
740
  while (true) {
724
741
  const items = [];
725
742
  let itemsLen = 0;
726
- while (items.length < batchSize && itemsLen < MaxRequestSize / 2) {
743
+ while (items.length < batchSize && itemsLen < this.maxRequestSize / 2) {
727
744
  let item = null;
728
745
  if (allItems.length > 0) {
729
746
  item = allItems.pop();
@@ -739,55 +756,106 @@ var BackgroundLogger = class {
739
756
  }
740
757
  postPromises.push(
741
758
  (async () => {
742
- const dataStr = constructLogs3Data(items);
743
- for (let i = 0; i < NumRetries; i++) {
744
- const startTime = now();
745
- try {
746
- try {
747
- return (await (await this.logConn.get()).post_json("logs3", dataStr)).ids.map((res) => res.id);
748
- } catch (e) {
749
- const legacyDataS = constructJsonArray(
750
- items.map(
751
- (r) => JSON.stringify(makeLegacyEvent(JSON.parse(r)))
752
- )
753
- );
754
- return (await (await this.logConn.get()).post_json("logs", legacyDataS)).map((res) => res.id);
755
- }
756
- } catch (e) {
757
- const retryingText = i + 1 === NumRetries ? "" : " Retrying";
758
- const errMsg = (() => {
759
- if (e instanceof FailedHTTPResponse) {
760
- return `${e.status} (${e.text}): ${e.data}`;
761
- } else {
762
- return `${e}`;
763
- }
764
- })();
765
- console.warn(
766
- `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}. Error: ${errMsg}.${retryingText}`
767
- );
768
- }
759
+ try {
760
+ await this.submitLogsRequest(items);
761
+ return { type: "success" };
762
+ } catch (e) {
763
+ return { type: "error", value: e };
769
764
  }
770
- console.warn(
771
- `log request failed after ${NumRetries} retries. Dropping batch`
772
- );
773
- return [];
774
765
  })()
775
766
  );
776
767
  }
777
- let ret = await Promise.all(postPromises);
768
+ const results = await Promise.all(postPromises);
769
+ const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
770
+ if (failingResultErrors.length) {
771
+ throw new AggregateError(
772
+ failingResultErrors,
773
+ `Encountered the following errors while logging:`
774
+ );
775
+ }
778
776
  if (this.items.length > 0) {
779
- this.active_flush = this.flush_once();
780
- } else {
781
- this.active_flush_resolved = true;
777
+ await this.flushOnce(args);
782
778
  }
783
- return ret;
784
779
  }
785
- async flush() {
786
- while (true) {
787
- await this.active_flush;
788
- if (this.active_flush_resolved) {
789
- break;
780
+ async unwrapLazyValues(wrappedItems) {
781
+ for (let i = 0; i < this.numRetries; ++i) {
782
+ try {
783
+ const itemPromises = wrappedItems.map((x) => x.get());
784
+ return mergeRowBatch(await Promise.all(itemPromises));
785
+ } catch (e) {
786
+ let errmsg = "Encountered error when constructing records to flush";
787
+ const isRetrying = i + 1 < this.numRetries;
788
+ if (isRetrying) {
789
+ errmsg += ". Retrying";
790
+ }
791
+ console.warn(errmsg);
792
+ if (!isRetrying && this.syncFlush) {
793
+ throw e;
794
+ } else {
795
+ console.warn(e);
796
+ await new Promise((resolve) => setTimeout(resolve, 100));
797
+ }
798
+ }
799
+ }
800
+ console.warn(
801
+ `Failed to construct log records to flush after ${this.numRetries} retries. Dropping batch`
802
+ );
803
+ return [];
804
+ }
805
+ async submitLogsRequest(items) {
806
+ const conn = await this.logConn.get();
807
+ const dataStr = constructLogs3Data(items);
808
+ for (let i = 0; i < this.numRetries; i++) {
809
+ const startTime = now();
810
+ let error = void 0;
811
+ try {
812
+ await conn.post_json("logs3", dataStr);
813
+ } catch (e) {
814
+ try {
815
+ const legacyDataS = constructJsonArray(
816
+ items.map(
817
+ (r) => JSON.stringify(makeLegacyEvent(JSON.parse(r)))
818
+ )
819
+ );
820
+ await conn.post_json("logs", legacyDataS);
821
+ } catch (e2) {
822
+ error = e2;
823
+ }
824
+ }
825
+ if (error === void 0) {
826
+ return;
790
827
  }
828
+ const isRetrying = i + 1 < this.numRetries;
829
+ const retryingText = isRetrying ? "" : " Retrying";
830
+ const errorText = (() => {
831
+ if (error instanceof FailedHTTPResponse) {
832
+ return `${error.status} (${error.text}): ${error.data}`;
833
+ } else {
834
+ return `${error}`;
835
+ }
836
+ })();
837
+ const errMsg = `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}.${retryingText}
838
+ Error: ${errorText}`;
839
+ if (!isRetrying && this.syncFlush) {
840
+ throw new Error(errMsg);
841
+ } else {
842
+ console.warn(errMsg);
843
+ if (isRetrying) {
844
+ await new Promise((resolve) => setTimeout(resolve, 100));
845
+ }
846
+ }
847
+ }
848
+ console.warn(
849
+ `log request failed after ${this.numRetries} retries. Dropping batch`
850
+ );
851
+ return;
852
+ }
853
+ triggerActiveFlush() {
854
+ if (this.activeFlushResolved) {
855
+ this.activeFlushResolved = false;
856
+ this.activeFlush = this.flushOnce().finally(() => {
857
+ this.activeFlushResolved = true;
858
+ });
791
859
  }
792
860
  }
793
861
  };
@@ -1408,8 +1476,8 @@ var ObjectFetcher = class {
1408
1476
  const fetchedData = await this.fetchedData();
1409
1477
  let maxVersion = void 0;
1410
1478
  for (const record of fetchedData) {
1411
- const xactId = record[TRANSACTION_ID_FIELD];
1412
- if (maxVersion === void 0 || (xactId ?? xactId > maxVersion)) {
1479
+ const xactId = String(record[TRANSACTION_ID_FIELD] ?? "0");
1480
+ if (maxVersion === void 0 || xactId > maxVersion) {
1413
1481
  maxVersion = xactId;
1414
1482
  }
1415
1483
  }
package/dist/cli.js CHANGED
@@ -7772,35 +7772,41 @@ var require_ansi_styles = __commonJS({
7772
7772
  }
7773
7773
  });
7774
7774
 
7775
- // ../../node_modules/supports-color/node_modules/has-flag/index.js
7775
+ // ../../node_modules/.pnpm/has-flag@4.0.0/node_modules/has-flag/index.js
7776
7776
  var require_has_flag = __commonJS({
7777
- "../../node_modules/supports-color/node_modules/has-flag/index.js"(exports2, module2) {
7777
+ "../../node_modules/.pnpm/has-flag@4.0.0/node_modules/has-flag/index.js"(exports2, module2) {
7778
7778
  "use strict";
7779
- module2.exports = (flag, argv) => {
7780
- argv = argv || process.argv;
7779
+ module2.exports = (flag, argv = process.argv) => {
7781
7780
  const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
7782
- const pos = argv.indexOf(prefix + flag);
7783
- const terminatorPos = argv.indexOf("--");
7784
- return pos !== -1 && (terminatorPos === -1 ? true : pos < terminatorPos);
7781
+ const position = argv.indexOf(prefix + flag);
7782
+ const terminatorPosition = argv.indexOf("--");
7783
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
7785
7784
  };
7786
7785
  }
7787
7786
  });
7788
7787
 
7789
- // ../../node_modules/supports-color/index.js
7788
+ // ../../node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js
7790
7789
  var require_supports_color = __commonJS({
7791
- "../../node_modules/supports-color/index.js"(exports2, module2) {
7790
+ "../../node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js"(exports2, module2) {
7792
7791
  "use strict";
7793
7792
  var os2 = require("os");
7793
+ var tty = require("tty");
7794
7794
  var hasFlag = require_has_flag();
7795
- var env = process.env;
7795
+ var { env } = process;
7796
7796
  var forceColor;
7797
- if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false")) {
7798
- forceColor = false;
7797
+ if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
7798
+ forceColor = 0;
7799
7799
  } else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) {
7800
- forceColor = true;
7800
+ forceColor = 1;
7801
7801
  }
7802
7802
  if ("FORCE_COLOR" in env) {
7803
- forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0;
7803
+ if (env.FORCE_COLOR === "true") {
7804
+ forceColor = 1;
7805
+ } else if (env.FORCE_COLOR === "false") {
7806
+ forceColor = 0;
7807
+ } else {
7808
+ forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
7809
+ }
7804
7810
  }
7805
7811
  function translateLevel(level) {
7806
7812
  if (level === 0) {
@@ -7813,8 +7819,8 @@ var require_supports_color = __commonJS({
7813
7819
  has16m: level >= 3
7814
7820
  };
7815
7821
  }
7816
- function supportsColor(stream) {
7817
- if (forceColor === false) {
7822
+ function supportsColor(haveStream, streamIsTTY) {
7823
+ if (forceColor === 0) {
7818
7824
  return 0;
7819
7825
  }
7820
7826
  if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
@@ -7823,19 +7829,22 @@ var require_supports_color = __commonJS({
7823
7829
  if (hasFlag("color=256")) {
7824
7830
  return 2;
7825
7831
  }
7826
- if (stream && !stream.isTTY && forceColor !== true) {
7832
+ if (haveStream && !streamIsTTY && forceColor === void 0) {
7827
7833
  return 0;
7828
7834
  }
7829
- const min = forceColor ? 1 : 0;
7835
+ const min = forceColor || 0;
7836
+ if (env.TERM === "dumb") {
7837
+ return min;
7838
+ }
7830
7839
  if (process.platform === "win32") {
7831
7840
  const osRelease = os2.release().split(".");
7832
- if (Number(process.versions.node.split(".")[0]) >= 8 && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
7841
+ if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
7833
7842
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
7834
7843
  }
7835
7844
  return 1;
7836
7845
  }
7837
7846
  if ("CI" in env) {
7838
- if (["TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI"].some((sign) => sign in env) || env.CI_NAME === "codeship") {
7847
+ if (["TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI", "GITHUB_ACTIONS", "BUILDKITE"].some((sign) => sign in env) || env.CI_NAME === "codeship") {
7839
7848
  return 1;
7840
7849
  }
7841
7850
  return min;
@@ -7864,19 +7873,16 @@ var require_supports_color = __commonJS({
7864
7873
  if ("COLORTERM" in env) {
7865
7874
  return 1;
7866
7875
  }
7867
- if (env.TERM === "dumb") {
7868
- return min;
7869
- }
7870
7876
  return min;
7871
7877
  }
7872
7878
  function getSupportLevel(stream) {
7873
- const level = supportsColor(stream);
7879
+ const level = supportsColor(stream, stream && stream.isTTY);
7874
7880
  return translateLevel(level);
7875
7881
  }
7876
7882
  module2.exports = {
7877
7883
  supportsColor: getSupportLevel,
7878
- stdout: getSupportLevel(process.stdout),
7879
- stderr: getSupportLevel(process.stderr)
7884
+ stdout: translateLevel(supportsColor(true, tty.isatty(1))),
7885
+ stderr: translateLevel(supportsColor(true, tty.isatty(2)))
7880
7886
  };
7881
7887
  }
7882
7888
  });
@@ -9059,7 +9065,7 @@ var require_package = __commonJS({
9059
9065
  "package.json"(exports2, module2) {
9060
9066
  module2.exports = {
9061
9067
  name: "braintrust",
9062
- version: "0.0.100",
9068
+ version: "0.0.101",
9063
9069
  description: "SDK for integrating Braintrust",
9064
9070
  main: "./dist/index.js",
9065
9071
  browser: {
@@ -9102,7 +9108,7 @@ var require_package = __commonJS({
9102
9108
  typescript: "^5.3.3"
9103
9109
  },
9104
9110
  dependencies: {
9105
- "@braintrust/core": "0.0.18",
9111
+ "@braintrust/core": "0.0.19",
9106
9112
  argparse: "^2.0.1",
9107
9113
  chalk: "^4.1.2",
9108
9114
  "cli-progress": "^3.12.0",
@@ -10941,57 +10947,74 @@ function logFeedbackImpl(bgLogger, parentIds, {
10941
10947
  bgLogger.log([record]);
10942
10948
  }
10943
10949
  }
10944
- var MaxRequestSize = 6 * 1024 * 1024;
10945
10950
  function constructJsonArray(items) {
10946
10951
  return `[${items.join(",")}]`;
10947
10952
  }
10948
10953
  function constructLogs3Data(items) {
10949
10954
  return `{"rows": ${constructJsonArray(items)}, "api_version": 2}`;
10950
10955
  }
10951
- var DefaultBatchSize = 100;
10952
- var NumRetries = 3;
10953
10956
  function now() {
10954
10957
  return (/* @__PURE__ */ new Date()).getTime();
10955
10958
  }
10956
10959
  var BackgroundLogger = class {
10957
10960
  logConn;
10958
10961
  items = [];
10959
- active_flush = Promise.resolve([]);
10960
- active_flush_resolved = true;
10962
+ activeFlush = Promise.resolve();
10963
+ activeFlushResolved = true;
10964
+ syncFlush = false;
10965
+ // 6 MB for the AWS lambda gateway (from our own testing).
10966
+ maxRequestSize = 6 * 1024 * 1024;
10967
+ defaultBatchSize = 100;
10968
+ numRetries = 3;
10961
10969
  constructor(logConn) {
10962
10970
  this.logConn = logConn;
10971
+ const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
10972
+ if (!isNaN(syncFlushEnv)) {
10973
+ this.syncFlush = Boolean(syncFlushEnv);
10974
+ }
10975
+ const defaultBatchSizeEnv = Number(
10976
+ isomorph_default.getEnv("BRAINTRUST_DEFAULT_BATCH_SIZE")
10977
+ );
10978
+ if (!isNaN(defaultBatchSizeEnv)) {
10979
+ this.defaultBatchSize = defaultBatchSizeEnv;
10980
+ }
10981
+ const maxRequestSizeEnv = Number(isomorph_default.getEnv("BRAINTRUST_MAX_REQUEST_SIZE"));
10982
+ if (!isNaN(maxRequestSizeEnv)) {
10983
+ this.maxRequestSize = maxRequestSizeEnv;
10984
+ }
10985
+ const numRetriesEnv = Number(isomorph_default.getEnv("BRAINTRUST_NUM_RETRIES"));
10986
+ if (!isNaN(numRetriesEnv)) {
10987
+ this.numRetries = numRetriesEnv;
10988
+ }
10963
10989
  isomorph_default.processOn("beforeExit", async () => {
10964
10990
  await this.flush();
10965
10991
  });
10966
10992
  }
10967
10993
  log(items) {
10968
10994
  this.items.push(...items);
10969
- if (this.active_flush_resolved) {
10970
- this.active_flush_resolved = false;
10971
- this.active_flush = this.flush_once();
10995
+ if (!this.syncFlush) {
10996
+ this.triggerActiveFlush();
10997
+ }
10998
+ }
10999
+ async flush() {
11000
+ if (this.syncFlush) {
11001
+ this.triggerActiveFlush();
10972
11002
  }
11003
+ await this.activeFlush;
10973
11004
  }
10974
- async flush_once(batchSize = DefaultBatchSize) {
10975
- this.active_flush_resolved = false;
10976
- const itemLazyValues = this.items;
11005
+ async flushOnce(args) {
11006
+ const batchSize = args?.batchSize ?? this.defaultBatchSize;
11007
+ const wrappedItems = this.items;
10977
11008
  this.items = [];
10978
- const allItems = await (async () => {
10979
- try {
10980
- const itemPromises = itemLazyValues.map((x) => x.get());
10981
- return mergeRowBatch(await Promise.all(itemPromises)).reverse();
10982
- } catch (e) {
10983
- console.warn(
10984
- "Encountered error when constructing records to flush:\n",
10985
- e
10986
- );
10987
- return [];
10988
- }
10989
- })();
10990
- let postPromises = [];
11009
+ const allItems = await this.unwrapLazyValues(wrappedItems);
11010
+ if (allItems.length === 0) {
11011
+ return;
11012
+ }
11013
+ const postPromises = [];
10991
11014
  while (true) {
10992
11015
  const items = [];
10993
11016
  let itemsLen = 0;
10994
- while (items.length < batchSize && itemsLen < MaxRequestSize / 2) {
11017
+ while (items.length < batchSize && itemsLen < this.maxRequestSize / 2) {
10995
11018
  let item = null;
10996
11019
  if (allItems.length > 0) {
10997
11020
  item = allItems.pop();
@@ -11007,56 +11030,107 @@ var BackgroundLogger = class {
11007
11030
  }
11008
11031
  postPromises.push(
11009
11032
  (async () => {
11010
- const dataStr = constructLogs3Data(items);
11011
- for (let i = 0; i < NumRetries; i++) {
11012
- const startTime = now();
11013
- try {
11014
- try {
11015
- return (await (await this.logConn.get()).post_json("logs3", dataStr)).ids.map((res) => res.id);
11016
- } catch (e) {
11017
- const legacyDataS = constructJsonArray(
11018
- items.map(
11019
- (r) => JSON.stringify(makeLegacyEvent(JSON.parse(r)))
11020
- )
11021
- );
11022
- return (await (await this.logConn.get()).post_json("logs", legacyDataS)).map((res) => res.id);
11023
- }
11024
- } catch (e) {
11025
- const retryingText = i + 1 === NumRetries ? "" : " Retrying";
11026
- const errMsg = (() => {
11027
- if (e instanceof FailedHTTPResponse) {
11028
- return `${e.status} (${e.text}): ${e.data}`;
11029
- } else {
11030
- return `${e}`;
11031
- }
11032
- })();
11033
- console.warn(
11034
- `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}. Error: ${errMsg}.${retryingText}`
11035
- );
11036
- }
11033
+ try {
11034
+ await this.submitLogsRequest(items);
11035
+ return { type: "success" };
11036
+ } catch (e) {
11037
+ return { type: "error", value: e };
11037
11038
  }
11038
- console.warn(
11039
- `log request failed after ${NumRetries} retries. Dropping batch`
11040
- );
11041
- return [];
11042
11039
  })()
11043
11040
  );
11044
11041
  }
11045
- let ret = await Promise.all(postPromises);
11042
+ const results = await Promise.all(postPromises);
11043
+ const failingResultErrors = results.map((r) => r.type === "success" ? void 0 : r.value).filter((r) => r !== void 0);
11044
+ if (failingResultErrors.length) {
11045
+ throw new AggregateError(
11046
+ failingResultErrors,
11047
+ `Encountered the following errors while logging:`
11048
+ );
11049
+ }
11046
11050
  if (this.items.length > 0) {
11047
- this.active_flush = this.flush_once();
11048
- } else {
11049
- this.active_flush_resolved = true;
11051
+ await this.flushOnce(args);
11050
11052
  }
11051
- return ret;
11052
11053
  }
11053
- async flush() {
11054
- while (true) {
11055
- await this.active_flush;
11056
- if (this.active_flush_resolved) {
11057
- break;
11054
+ async unwrapLazyValues(wrappedItems) {
11055
+ for (let i = 0; i < this.numRetries; ++i) {
11056
+ try {
11057
+ const itemPromises = wrappedItems.map((x) => x.get());
11058
+ return mergeRowBatch(await Promise.all(itemPromises));
11059
+ } catch (e) {
11060
+ let errmsg = "Encountered error when constructing records to flush";
11061
+ const isRetrying = i + 1 < this.numRetries;
11062
+ if (isRetrying) {
11063
+ errmsg += ". Retrying";
11064
+ }
11065
+ console.warn(errmsg);
11066
+ if (!isRetrying && this.syncFlush) {
11067
+ throw e;
11068
+ } else {
11069
+ console.warn(e);
11070
+ await new Promise((resolve2) => setTimeout(resolve2, 100));
11071
+ }
11058
11072
  }
11059
11073
  }
11074
+ console.warn(
11075
+ `Failed to construct log records to flush after ${this.numRetries} retries. Dropping batch`
11076
+ );
11077
+ return [];
11078
+ }
11079
+ async submitLogsRequest(items) {
11080
+ const conn = await this.logConn.get();
11081
+ const dataStr = constructLogs3Data(items);
11082
+ for (let i = 0; i < this.numRetries; i++) {
11083
+ const startTime = now();
11084
+ let error2 = void 0;
11085
+ try {
11086
+ await conn.post_json("logs3", dataStr);
11087
+ } catch (e) {
11088
+ try {
11089
+ const legacyDataS = constructJsonArray(
11090
+ items.map(
11091
+ (r) => JSON.stringify(makeLegacyEvent(JSON.parse(r)))
11092
+ )
11093
+ );
11094
+ await conn.post_json("logs", legacyDataS);
11095
+ } catch (e2) {
11096
+ error2 = e2;
11097
+ }
11098
+ }
11099
+ if (error2 === void 0) {
11100
+ return;
11101
+ }
11102
+ const isRetrying = i + 1 < this.numRetries;
11103
+ const retryingText = isRetrying ? "" : " Retrying";
11104
+ const errorText = (() => {
11105
+ if (error2 instanceof FailedHTTPResponse) {
11106
+ return `${error2.status} (${error2.text}): ${error2.data}`;
11107
+ } else {
11108
+ return `${error2}`;
11109
+ }
11110
+ })();
11111
+ const errMsg = `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}.${retryingText}
11112
+ Error: ${errorText}`;
11113
+ if (!isRetrying && this.syncFlush) {
11114
+ throw new Error(errMsg);
11115
+ } else {
11116
+ console.warn(errMsg);
11117
+ if (isRetrying) {
11118
+ await new Promise((resolve2) => setTimeout(resolve2, 100));
11119
+ }
11120
+ }
11121
+ }
11122
+ console.warn(
11123
+ `log request failed after ${this.numRetries} retries. Dropping batch`
11124
+ );
11125
+ return;
11126
+ }
11127
+ triggerActiveFlush() {
11128
+ if (this.activeFlushResolved) {
11129
+ this.activeFlushResolved = false;
11130
+ this.activeFlush = this.flushOnce().finally(() => {
11131
+ this.activeFlushResolved = true;
11132
+ });
11133
+ }
11060
11134
  }
11061
11135
  };
11062
11136
  function init(projectOrOptions, optionalOptions) {
@@ -11449,8 +11523,8 @@ var ObjectFetcher = class {
11449
11523
  const fetchedData = await this.fetchedData();
11450
11524
  let maxVersion = void 0;
11451
11525
  for (const record of fetchedData) {
11452
- const xactId = record[TRANSACTION_ID_FIELD];
11453
- if (maxVersion === void 0 || (xactId ?? xactId > maxVersion)) {
11526
+ const xactId = String(record[TRANSACTION_ID_FIELD] ?? "0");
11527
+ if (maxVersion === void 0 || xactId > maxVersion) {
11454
11528
  maxVersion = xactId;
11455
11529
  }
11456
11530
  }