braintrust 0.0.93 → 0.0.94

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
@@ -183,6 +183,21 @@ function getCurrentUnixTimestamp() {
183
183
  function isEmpty(a) {
184
184
  return a === void 0 || a === null;
185
185
  }
186
+ var LazyValue = class {
187
+ constructor(callable) {
188
+ this.value = {
189
+ hasComputed: false
190
+ };
191
+ this.callable = callable;
192
+ }
193
+ async get() {
194
+ if (this.value.hasComputed) {
195
+ return this.value.val;
196
+ }
197
+ this.value = { hasComputed: true, val: await this.callable() };
198
+ return this.value.val;
199
+ }
200
+ };
186
201
 
187
202
  // src/logger.ts
188
203
  var NoopSpan = class {
@@ -397,25 +412,25 @@ function logFeedbackImpl(bgLogger, parentIds, {
397
412
  updateEvent = Object.fromEntries(
398
413
  Object.entries(updateEvent).filter(([_, v]) => !isEmpty(v))
399
414
  );
400
- const trueParentIds = (async () => {
401
- const { kind, ...ids } = await parentIds;
415
+ const trueParentIds = new LazyValue(async () => {
416
+ const { kind, ...ids } = await parentIds.get();
402
417
  return ids;
403
- })();
418
+ });
404
419
  if (Object.keys(updateEvent).length > 0) {
405
- const record = (async () => {
420
+ const record = new LazyValue(async () => {
406
421
  return {
407
422
  id,
408
423
  ...updateEvent,
409
- ...await trueParentIds,
424
+ ...await trueParentIds.get(),
410
425
  [AUDIT_SOURCE_FIELD]: source,
411
426
  [AUDIT_METADATA_FIELD]: metadata,
412
427
  [IS_MERGE_FIELD]: true
413
428
  };
414
- })();
429
+ });
415
430
  bgLogger.log([record]);
416
431
  }
417
432
  if (!isEmpty(comment)) {
418
- const record = (async () => {
433
+ const record = new LazyValue(async () => {
419
434
  return {
420
435
  id: v4_default(),
421
436
  created: (/* @__PURE__ */ new Date()).toISOString(),
@@ -427,11 +442,11 @@ function logFeedbackImpl(bgLogger, parentIds, {
427
442
  comment: {
428
443
  text: comment
429
444
  },
430
- ...await trueParentIds,
445
+ ...await trueParentIds.get(),
431
446
  [AUDIT_SOURCE_FIELD]: source,
432
447
  [AUDIT_METADATA_FIELD]: metadata
433
448
  };
434
- })();
449
+ });
435
450
  bgLogger.log([record]);
436
451
  }
437
452
  }
@@ -441,22 +456,24 @@ var Logger = class {
441
456
  this.kind = "logger";
442
457
  this.lazyMetadata = lazyMetadata;
443
458
  this.logOptions = logOptions;
444
- const logConn = this.getState().then((state) => state.logConn());
459
+ const logConn = new LazyValue(
460
+ () => this.getState().then((state) => state.logConn())
461
+ );
445
462
  this.bgLogger = new BackgroundLogger(logConn);
446
463
  this.lastStartTime = getCurrentUnixTimestamp();
447
464
  }
448
465
  get org_id() {
449
466
  return (async () => {
450
- return (await this.lazyMetadata).org_id;
467
+ return (await this.lazyMetadata.get()).org_id;
451
468
  })();
452
469
  }
453
470
  get project() {
454
471
  return (async () => {
455
- return (await this.lazyMetadata).project;
472
+ return (await this.lazyMetadata.get()).project;
456
473
  })();
457
474
  }
458
475
  async getState() {
459
- await this.lazyMetadata;
476
+ await this.lazyMetadata.get();
460
477
  return _state;
461
478
  }
462
479
  /**
@@ -531,7 +548,7 @@ var Logger = class {
531
548
  startSpan(args) {
532
549
  const { name, ...argsRest } = args ?? {};
533
550
  return new SpanImpl({
534
- parentIds: this.lazyParentIds(),
551
+ parentIds: new LazyValue(() => this.lazyParentIds()),
535
552
  bgLogger: this.bgLogger,
536
553
  name: name ?? "root",
537
554
  ...argsRest
@@ -549,7 +566,11 @@ var Logger = class {
549
566
  * @param event.source (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
550
567
  */
551
568
  logFeedback(event) {
552
- logFeedbackImpl(this.bgLogger, this.lazyParentIds(), event);
569
+ logFeedbackImpl(
570
+ this.bgLogger,
571
+ new LazyValue(() => this.lazyParentIds()),
572
+ event
573
+ );
553
574
  }
554
575
  /*
555
576
  * Flush any pending logs to the server.
@@ -599,9 +620,20 @@ var BackgroundLogger = class {
599
620
  }
600
621
  async flush_once(batchSize = DefaultBatchSize) {
601
622
  this.active_flush_resolved = false;
602
- const itemPromises = this.items;
623
+ const itemLazyValues = this.items;
603
624
  this.items = [];
604
- const allItems = mergeRowBatch(await Promise.all(itemPromises)).reverse();
625
+ const allItems = await (async () => {
626
+ try {
627
+ const itemPromises = itemLazyValues.map((x) => x.get());
628
+ return mergeRowBatch(await Promise.all(itemPromises)).reverse();
629
+ } catch (e) {
630
+ console.warn(
631
+ "Encountered error when constructing records to flush:\n",
632
+ e
633
+ );
634
+ return [];
635
+ }
636
+ })();
605
637
  let postPromises = [];
606
638
  while (true) {
607
639
  const items = [];
@@ -626,9 +658,7 @@ var BackgroundLogger = class {
626
658
  for (let i = 0; i < NumRetries; i++) {
627
659
  const startTime = now();
628
660
  try {
629
- return (await (await this.logConn).post_json("logs", itemsS)).map(
630
- (res) => res.id
631
- );
661
+ return (await (await this.logConn.get()).post_json("logs", itemsS)).map((res) => res.id);
632
662
  } catch (e) {
633
663
  const retryingText = i + 1 === NumRetries ? "" : " Retrying";
634
664
  const errMsg = (() => {
@@ -681,82 +711,86 @@ function init(project, options = {}) {
681
711
  metadata,
682
712
  gitMetadataSettings
683
713
  } = options || {};
684
- const lazyMetadata = (async () => {
685
- await login({
686
- orgName,
687
- apiKey,
688
- appUrl
689
- });
690
- const args = {
691
- project_name: project,
692
- org_id: _state.orgId
693
- };
694
- if (experiment) {
695
- args["experiment_name"] = experiment;
696
- }
697
- if (description) {
698
- args["description"] = description;
699
- }
700
- if (update) {
701
- args["update"] = update;
702
- }
703
- let mergedGitMetadataSettings = {
704
- ..._state.gitMetadataSettings || {
705
- collect: "all"
714
+ const lazyMetadata = new LazyValue(
715
+ async () => {
716
+ await login({
717
+ orgName,
718
+ apiKey,
719
+ appUrl
720
+ });
721
+ const args = {
722
+ project_name: project,
723
+ org_id: _state.orgId
724
+ };
725
+ if (experiment) {
726
+ args["experiment_name"] = experiment;
706
727
  }
707
- };
708
- if (gitMetadataSettings) {
709
- mergedGitMetadataSettings = mergeGitMetadataSettings(
710
- mergedGitMetadataSettings,
711
- gitMetadataSettings
712
- );
713
- }
714
- const repoStatus = await isomorph_default.getRepoStatus(gitMetadataSettings);
715
- if (repoStatus) {
716
- args["repo_info"] = repoStatus;
717
- }
718
- if (baseExperiment) {
719
- args["base_experiment"] = baseExperiment;
720
- } else {
721
- args["ancestor_commits"] = await isomorph_default.getPastNAncestors();
722
- }
723
- if (dataset !== void 0) {
724
- args["dataset_id"] = dataset.id;
725
- args["dataset_version"] = await dataset.version();
726
- }
727
- if (isPublic !== void 0) {
728
- args["public"] = isPublic;
729
- }
730
- if (metadata) {
731
- args["metadata"] = metadata;
732
- }
733
- let response = null;
734
- while (true) {
735
- try {
736
- response = await _state.apiConn().post_json("api/experiment/register", args);
737
- break;
738
- } catch (e) {
739
- if (args["base_experiment"] && `${"data" in e && e.data}`.includes("base experiment")) {
740
- console.warn(`Base experiment ${args["base_experiment"]} not found.`);
741
- delete args["base_experiment"];
742
- } else {
743
- throw e;
728
+ if (description) {
729
+ args["description"] = description;
730
+ }
731
+ if (update) {
732
+ args["update"] = update;
733
+ }
734
+ let mergedGitMetadataSettings = {
735
+ ..._state.gitMetadataSettings || {
736
+ collect: "all"
744
737
  }
738
+ };
739
+ if (gitMetadataSettings) {
740
+ mergedGitMetadataSettings = mergeGitMetadataSettings(
741
+ mergedGitMetadataSettings,
742
+ gitMetadataSettings
743
+ );
745
744
  }
746
- }
747
- return {
748
- project: {
749
- id: response.project.id,
750
- name: response.project.name,
751
- fullInfo: response.project
752
- },
753
- experiment: {
754
- id: response.experiment.id,
755
- name: response.experiment.name,
756
- fullInfo: response.experiment
745
+ const repoStatus = await isomorph_default.getRepoStatus(gitMetadataSettings);
746
+ if (repoStatus) {
747
+ args["repo_info"] = repoStatus;
757
748
  }
758
- };
759
- })();
749
+ if (baseExperiment) {
750
+ args["base_experiment"] = baseExperiment;
751
+ } else {
752
+ args["ancestor_commits"] = await isomorph_default.getPastNAncestors();
753
+ }
754
+ if (dataset !== void 0) {
755
+ args["dataset_id"] = dataset.id;
756
+ args["dataset_version"] = await dataset.version();
757
+ }
758
+ if (isPublic !== void 0) {
759
+ args["public"] = isPublic;
760
+ }
761
+ if (metadata) {
762
+ args["metadata"] = metadata;
763
+ }
764
+ let response = null;
765
+ while (true) {
766
+ try {
767
+ response = await _state.apiConn().post_json("api/experiment/register", args);
768
+ break;
769
+ } catch (e) {
770
+ if (args["base_experiment"] && `${"data" in e && e.data}`.includes("base experiment")) {
771
+ console.warn(
772
+ `Base experiment ${args["base_experiment"]} not found.`
773
+ );
774
+ delete args["base_experiment"];
775
+ } else {
776
+ throw e;
777
+ }
778
+ }
779
+ }
780
+ return {
781
+ project: {
782
+ id: response.project.id,
783
+ name: response.project.name,
784
+ fullInfo: response.project
785
+ },
786
+ experiment: {
787
+ id: response.experiment.id,
788
+ name: response.experiment.name,
789
+ fullInfo: response.experiment
790
+ }
791
+ };
792
+ }
793
+ );
760
794
  const ret = new Experiment(lazyMetadata, dataset);
761
795
  if (options.setCurrent ?? true) {
762
796
  _state.currentExperiment = ret;
@@ -779,32 +813,34 @@ function withLogger(callback, options = {}) {
779
813
  }
780
814
  function initDataset(project, options = {}) {
781
815
  const { dataset, description, version, appUrl, apiKey, orgName } = options || {};
782
- const lazyMetadata = (async () => {
783
- await login({
784
- orgName,
785
- apiKey,
786
- appUrl
787
- });
788
- const args = {
789
- org_id: _state.orgId,
790
- project_name: project,
791
- dataset_name: dataset,
792
- description
793
- };
794
- const response = await _state.apiConn().post_json("api/dataset/register", args);
795
- return {
796
- project: {
797
- id: response.project.id,
798
- name: response.project.name,
799
- fullInfo: response.project
800
- },
801
- dataset: {
802
- id: response.dataset.id,
803
- name: response.dataset.name,
804
- fullInfo: response.dataset
805
- }
806
- };
807
- })();
816
+ const lazyMetadata = new LazyValue(
817
+ async () => {
818
+ await login({
819
+ orgName,
820
+ apiKey,
821
+ appUrl
822
+ });
823
+ const args = {
824
+ org_id: _state.orgId,
825
+ project_name: project,
826
+ dataset_name: dataset,
827
+ description
828
+ };
829
+ const response = await _state.apiConn().post_json("api/dataset/register", args);
830
+ return {
831
+ project: {
832
+ id: response.project.id,
833
+ name: response.project.name,
834
+ fullInfo: response.project
835
+ },
836
+ dataset: {
837
+ id: response.dataset.id,
838
+ name: response.dataset.name,
839
+ fullInfo: response.dataset
840
+ }
841
+ };
842
+ }
843
+ );
808
844
  return new Dataset(lazyMetadata, version);
809
845
  }
810
846
  function withDataset(project, callback, options = {}) {
@@ -824,46 +860,48 @@ function initLogger(options = {}) {
824
860
  orgName,
825
861
  forceLogin
826
862
  } = options || {};
827
- const lazyMetadata = (async () => {
828
- await login({
829
- orgName,
830
- apiKey,
831
- appUrl,
832
- forceLogin
833
- });
834
- const org_id = _state.orgId;
835
- if (projectId === void 0) {
836
- const response = await _state.apiConn().post_json("api/project/register", {
837
- project_name: projectName || GLOBAL_PROJECT,
838
- org_id
863
+ const lazyMetadata = new LazyValue(
864
+ async () => {
865
+ await login({
866
+ orgName,
867
+ apiKey,
868
+ appUrl,
869
+ forceLogin
839
870
  });
840
- return {
841
- org_id,
842
- project: {
843
- id: response.project.id,
844
- name: response.project.name,
845
- fullInfo: response.project
846
- }
847
- };
848
- } else if (projectName === void 0) {
849
- const response = await _state.apiConn().get_json("api/project", {
850
- id: projectId
851
- });
852
- return {
853
- org_id,
854
- project: {
855
- id: projectId,
856
- name: response.name,
857
- fullInfo: response.project
858
- }
859
- };
860
- } else {
861
- return {
862
- org_id,
863
- project: { id: projectId, name: projectName, fullInfo: {} }
864
- };
871
+ const org_id = _state.orgId;
872
+ if (projectId === void 0) {
873
+ const response = await _state.apiConn().post_json("api/project/register", {
874
+ project_name: projectName || GLOBAL_PROJECT,
875
+ org_id
876
+ });
877
+ return {
878
+ org_id,
879
+ project: {
880
+ id: response.project.id,
881
+ name: response.project.name,
882
+ fullInfo: response.project
883
+ }
884
+ };
885
+ } else if (projectName === void 0) {
886
+ const response = await _state.apiConn().get_json("api/project", {
887
+ id: projectId
888
+ });
889
+ return {
890
+ org_id,
891
+ project: {
892
+ id: projectId,
893
+ name: response.name,
894
+ fullInfo: response.project
895
+ }
896
+ };
897
+ } else {
898
+ return {
899
+ org_id,
900
+ project: { id: projectId, name: projectName, fullInfo: {} }
901
+ };
902
+ }
865
903
  }
866
- })();
904
+ );
867
905
  const ret = new Logger(lazyMetadata, {
868
906
  asyncFlush
869
907
  });
@@ -1095,27 +1133,29 @@ var Experiment = class {
1095
1133
  this.kind = "experiment";
1096
1134
  this.lazyMetadata = lazyMetadata;
1097
1135
  this.dataset = dataset;
1098
- const logConn = this.getState().then((state) => state.logConn());
1136
+ const logConn = new LazyValue(
1137
+ () => this.getState().then((state) => state.logConn())
1138
+ );
1099
1139
  this.bgLogger = new BackgroundLogger(logConn);
1100
1140
  this.lastStartTime = getCurrentUnixTimestamp();
1101
1141
  }
1102
1142
  get id() {
1103
1143
  return (async () => {
1104
- return (await this.lazyMetadata).experiment.id;
1144
+ return (await this.lazyMetadata.get()).experiment.id;
1105
1145
  })();
1106
1146
  }
1107
1147
  get name() {
1108
1148
  return (async () => {
1109
- return (await this.lazyMetadata).experiment.name;
1149
+ return (await this.lazyMetadata.get()).experiment.name;
1110
1150
  })();
1111
1151
  }
1112
1152
  get project() {
1113
1153
  return (async () => {
1114
- return (await this.lazyMetadata).project;
1154
+ return (await this.lazyMetadata.get()).project;
1115
1155
  })();
1116
1156
  }
1117
1157
  async getState() {
1118
- await this.lazyMetadata;
1158
+ await this.lazyMetadata.get();
1119
1159
  return _state;
1120
1160
  }
1121
1161
  /**
@@ -1175,7 +1215,7 @@ var Experiment = class {
1175
1215
  startSpan(args) {
1176
1216
  const { name, ...argsRest } = args ?? {};
1177
1217
  return new SpanImpl({
1178
- parentIds: this.lazyParentIds(),
1218
+ parentIds: new LazyValue(() => this.lazyParentIds()),
1179
1219
  bgLogger: this.bgLogger,
1180
1220
  name: name ?? "root",
1181
1221
  ...argsRest
@@ -1249,7 +1289,11 @@ var Experiment = class {
1249
1289
  * @param event.source (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
1250
1290
  */
1251
1291
  logFeedback(event) {
1252
- logFeedbackImpl(this.bgLogger, this.lazyParentIds(), event);
1292
+ logFeedbackImpl(
1293
+ this.bgLogger,
1294
+ new LazyValue(() => this.lazyParentIds()),
1295
+ event
1296
+ );
1253
1297
  }
1254
1298
  /**
1255
1299
  * Flush any pending rows to the server.
@@ -1330,18 +1374,18 @@ var SpanImpl = class _SpanImpl {
1330
1374
  if (sanitizedAndInternalData.metrics?.end) {
1331
1375
  this.loggedEndTime = sanitizedAndInternalData.metrics?.end;
1332
1376
  }
1333
- const parentIds = (async () => {
1334
- const { kind, ...ids } = await this.parentIds;
1377
+ const parentIds = new LazyValue(async () => {
1378
+ const { kind, ...ids } = await this.parentIds.get();
1335
1379
  return ids;
1336
- })();
1337
- const record = (async () => {
1380
+ });
1381
+ const record = new LazyValue(async () => {
1338
1382
  return {
1339
1383
  ...sanitizedAndInternalData,
1340
1384
  ...this.rowIds,
1341
- ...await parentIds,
1385
+ ...await parentIds.get(),
1342
1386
  [IS_MERGE_FIELD]: this.isMerge
1343
1387
  };
1344
- })();
1388
+ });
1345
1389
  this.bgLogger.log([record]);
1346
1390
  }
1347
1391
  logFeedback(event) {
@@ -1395,26 +1439,28 @@ var Dataset = class {
1395
1439
  this._fetchedData = void 0;
1396
1440
  this.lazyMetadata = lazyMetadata;
1397
1441
  this.pinnedVersion = pinnedVersion;
1398
- const logConn = this.getState().then((state) => state.logConn());
1442
+ const logConn = new LazyValue(
1443
+ () => this.getState().then((state) => state.logConn())
1444
+ );
1399
1445
  this.bgLogger = new BackgroundLogger(logConn);
1400
1446
  }
1401
1447
  get id() {
1402
1448
  return (async () => {
1403
- return (await this.lazyMetadata).dataset.id;
1449
+ return (await this.lazyMetadata.get()).dataset.id;
1404
1450
  })();
1405
1451
  }
1406
1452
  get name() {
1407
1453
  return (async () => {
1408
- return (await this.lazyMetadata).dataset.name;
1454
+ return (await this.lazyMetadata.get()).dataset.name;
1409
1455
  })();
1410
1456
  }
1411
1457
  get project() {
1412
1458
  return (async () => {
1413
- return (await this.lazyMetadata).project;
1459
+ return (await this.lazyMetadata.get()).project;
1414
1460
  })();
1415
1461
  }
1416
1462
  async getState() {
1417
- await this.lazyMetadata;
1463
+ await this.lazyMetadata.get();
1418
1464
  return _state;
1419
1465
  }
1420
1466
  /**
@@ -1445,7 +1491,7 @@ var Dataset = class {
1445
1491
  }
1446
1492
  }
1447
1493
  const rowId = id || v4_default();
1448
- const args = (async () => ({
1494
+ const args = new LazyValue(async () => ({
1449
1495
  id: rowId,
1450
1496
  inputs: input,
1451
1497
  output,
@@ -1453,18 +1499,18 @@ var Dataset = class {
1453
1499
  dataset_id: await this.id,
1454
1500
  created: (/* @__PURE__ */ new Date()).toISOString(),
1455
1501
  metadata
1456
- }))();
1502
+ }));
1457
1503
  this.bgLogger.log([args]);
1458
1504
  return rowId;
1459
1505
  }
1460
1506
  delete(id) {
1461
- const args = (async () => ({
1507
+ const args = new LazyValue(async () => ({
1462
1508
  id,
1463
1509
  project_id: (await this.project).id,
1464
1510
  dataset_id: await this.id,
1465
1511
  created: (/* @__PURE__ */ new Date()).toISOString(),
1466
1512
  _object_delete: true
1467
- }))();
1513
+ }));
1468
1514
  this.bgLogger.log([args]);
1469
1515
  return id;
1470
1516
  }