braintrust 0.3.7 → 0.4.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.
@@ -609,22 +609,6 @@ var SpanComponentsV3 = class _SpanComponentsV3 {
609
609
  return new _SpanComponentsV3(spanComponentsV3Schema.parse(jsonObj));
610
610
  }
611
611
  };
612
- function parseParent(parent) {
613
- return typeof parent === "string" ? parent : parent ? new SpanComponentsV3({
614
- object_type: parent.object_type === "experiment" ? 1 /* EXPERIMENT */ : parent.object_type === "playground_logs" ? 3 /* PLAYGROUND_LOGS */ : 2 /* PROJECT_LOGS */,
615
- object_id: parent.object_id,
616
- ...parent.row_ids ? {
617
- row_id: parent.row_ids.id,
618
- span_id: parent.row_ids.span_id,
619
- root_span_id: parent.row_ids.root_span_id
620
- } : {
621
- row_id: void 0,
622
- span_id: void 0,
623
- root_span_id: void 0
624
- },
625
- propagated_event: parent.propagated_event
626
- }).toStr() : void 0;
627
- }
628
612
 
629
613
  // util/http_headers.ts
630
614
  var BT_FOUND_EXISTING_HEADER = "x-bt-found-existing";
@@ -1097,6 +1081,232 @@ function camelToSnakeCase(s) {
1097
1081
  return s.replace(/([A-Z])/g, (m) => "_" + m.toLowerCase()).replace(/^_/, "");
1098
1082
  }
1099
1083
 
1084
+ // util/span_identifier_v4.ts
1085
+ import { z as z4 } from "zod/v3";
1086
+ var ENCODING_VERSION_NUMBER_V4 = 4;
1087
+ function tryMakeHexTraceId(s) {
1088
+ try {
1089
+ if (typeof s === "string" && s.length === 32) {
1090
+ const bytes = new Uint8Array(16);
1091
+ for (let i = 0; i < 16; i++) {
1092
+ const hex = s.substr(i * 2, 2);
1093
+ const byte = parseInt(hex, 16);
1094
+ if (isNaN(byte)) throw new Error();
1095
+ bytes[i] = byte;
1096
+ }
1097
+ return { bytes, isHex: true };
1098
+ }
1099
+ } catch {
1100
+ }
1101
+ return { bytes: void 0, isHex: false };
1102
+ }
1103
+ function tryMakeHexSpanId(s) {
1104
+ try {
1105
+ if (typeof s === "string" && s.length === 16) {
1106
+ const bytes = new Uint8Array(8);
1107
+ for (let i = 0; i < 8; i++) {
1108
+ const hex = s.substr(i * 2, 2);
1109
+ const byte = parseInt(hex, 16);
1110
+ if (isNaN(byte)) throw new Error();
1111
+ bytes[i] = byte;
1112
+ }
1113
+ return { bytes, isHex: true };
1114
+ }
1115
+ } catch {
1116
+ }
1117
+ return { bytes: void 0, isHex: false };
1118
+ }
1119
+ var INVALID_ENCODING_ERRMSG_V4 = `SpanComponents string is not properly encoded. This library only supports encoding versions up to ${ENCODING_VERSION_NUMBER_V4}. Please make sure the SDK library used to decode the SpanComponents is at least as new as any library used to encode it.`;
1120
+ var FIELDS_ID_TO_NAME = {
1121
+ [1 /* OBJECT_ID */]: "object_id",
1122
+ [2 /* ROW_ID */]: "row_id",
1123
+ [3 /* SPAN_ID */]: "span_id",
1124
+ [4 /* ROOT_SPAN_ID */]: "root_span_id"
1125
+ };
1126
+ var spanComponentsV4Schema = z4.object({
1127
+ object_type: spanObjectTypeV3EnumSchema,
1128
+ propagated_event: z4.record(z4.unknown()).nullish()
1129
+ }).and(
1130
+ z4.union([
1131
+ // Must provide one or the other.
1132
+ z4.object({
1133
+ object_id: z4.string().nullish(),
1134
+ compute_object_metadata_args: z4.optional(z4.null())
1135
+ }),
1136
+ z4.object({
1137
+ object_id: z4.optional(z4.null()),
1138
+ compute_object_metadata_args: z4.record(z4.unknown())
1139
+ })
1140
+ ])
1141
+ ).and(
1142
+ z4.union([
1143
+ // Either all of these must be provided or none.
1144
+ z4.object({
1145
+ row_id: z4.string(),
1146
+ span_id: z4.string(),
1147
+ root_span_id: z4.string()
1148
+ }),
1149
+ z4.object({
1150
+ row_id: z4.optional(z4.null()),
1151
+ span_id: z4.optional(z4.null()),
1152
+ root_span_id: z4.optional(z4.null())
1153
+ })
1154
+ ])
1155
+ );
1156
+ var SpanComponentsV4 = class _SpanComponentsV4 {
1157
+ constructor(data) {
1158
+ this.data = data;
1159
+ }
1160
+ toStr() {
1161
+ const jsonObj = {
1162
+ compute_object_metadata_args: this.data.compute_object_metadata_args || void 0,
1163
+ propagated_event: this.data.propagated_event || void 0
1164
+ };
1165
+ Object.keys(jsonObj).forEach((key) => {
1166
+ if (jsonObj[key] === void 0) {
1167
+ delete jsonObj[key];
1168
+ }
1169
+ });
1170
+ const allBuffers = [];
1171
+ allBuffers.push(
1172
+ new Uint8Array([ENCODING_VERSION_NUMBER_V4, this.data.object_type])
1173
+ );
1174
+ const hexEntries = [];
1175
+ function addHexField(origVal, fieldId) {
1176
+ let hexResult;
1177
+ if (fieldId === 3 /* SPAN_ID */) {
1178
+ hexResult = tryMakeHexSpanId(origVal);
1179
+ } else if (fieldId === 4 /* ROOT_SPAN_ID */) {
1180
+ hexResult = tryMakeHexTraceId(origVal);
1181
+ } else {
1182
+ hexResult = { bytes: void 0, isHex: false };
1183
+ }
1184
+ if (hexResult.isHex) {
1185
+ hexEntries.push(
1186
+ concatUint8Arrays(new Uint8Array([fieldId]), hexResult.bytes)
1187
+ );
1188
+ } else {
1189
+ jsonObj[FIELDS_ID_TO_NAME[fieldId]] = origVal;
1190
+ }
1191
+ }
1192
+ if (this.data.object_id) {
1193
+ addHexField(this.data.object_id, 1 /* OBJECT_ID */);
1194
+ }
1195
+ if (this.data.row_id) {
1196
+ addHexField(this.data.row_id, 2 /* ROW_ID */);
1197
+ }
1198
+ if (this.data.span_id) {
1199
+ addHexField(this.data.span_id, 3 /* SPAN_ID */);
1200
+ }
1201
+ if (this.data.root_span_id) {
1202
+ addHexField(this.data.root_span_id, 4 /* ROOT_SPAN_ID */);
1203
+ }
1204
+ if (hexEntries.length > 255) {
1205
+ throw new Error("Impossible: too many hex entries to encode");
1206
+ }
1207
+ allBuffers.push(new Uint8Array([hexEntries.length]));
1208
+ allBuffers.push(...hexEntries);
1209
+ if (Object.keys(jsonObj).length > 0) {
1210
+ allBuffers.push(stringToUint8Array(JSON.stringify(jsonObj)));
1211
+ }
1212
+ return uint8ArrayToBase64(concatUint8Arrays(...allBuffers));
1213
+ }
1214
+ static fromStr(s) {
1215
+ try {
1216
+ const rawBytes = base64ToUint8Array(s);
1217
+ const jsonObj = {};
1218
+ if (rawBytes[0] < ENCODING_VERSION_NUMBER_V4) {
1219
+ const v3Components = SpanComponentsV3.fromStr(s);
1220
+ jsonObj["object_type"] = v3Components.data.object_type;
1221
+ jsonObj["object_id"] = v3Components.data.object_id;
1222
+ jsonObj["compute_object_metadata_args"] = v3Components.data.compute_object_metadata_args;
1223
+ jsonObj["row_id"] = v3Components.data.row_id;
1224
+ jsonObj["span_id"] = v3Components.data.span_id;
1225
+ jsonObj["root_span_id"] = v3Components.data.root_span_id;
1226
+ jsonObj["propagated_event"] = v3Components.data.propagated_event;
1227
+ } else {
1228
+ jsonObj["object_type"] = rawBytes[1];
1229
+ const numHexEntries = rawBytes[2];
1230
+ let byteOffset = 3;
1231
+ for (let i = 0; i < numHexEntries; i++) {
1232
+ const fieldId = rawBytes[byteOffset];
1233
+ if (fieldId === 3 /* SPAN_ID */) {
1234
+ const hexBytes = rawBytes.subarray(byteOffset + 1, byteOffset + 9);
1235
+ byteOffset += 9;
1236
+ jsonObj[FIELDS_ID_TO_NAME[fieldId]] = Array.from(
1237
+ hexBytes,
1238
+ (b) => b.toString(16).padStart(2, "0")
1239
+ ).join("");
1240
+ } else if (fieldId === 4 /* ROOT_SPAN_ID */) {
1241
+ const hexBytes = rawBytes.subarray(byteOffset + 1, byteOffset + 17);
1242
+ byteOffset += 17;
1243
+ jsonObj[FIELDS_ID_TO_NAME[fieldId]] = Array.from(
1244
+ hexBytes,
1245
+ (b) => b.toString(16).padStart(2, "0")
1246
+ ).join("");
1247
+ } else {
1248
+ const hexBytes = rawBytes.subarray(byteOffset + 1, byteOffset + 17);
1249
+ byteOffset += 17;
1250
+ jsonObj[FIELDS_ID_TO_NAME[fieldId]] = Array.from(
1251
+ hexBytes,
1252
+ (b) => b.toString(16).padStart(2, "0")
1253
+ ).join("");
1254
+ }
1255
+ }
1256
+ if (byteOffset < rawBytes.length) {
1257
+ const remainingJsonObj = JSON.parse(
1258
+ uint8ArrayToString(rawBytes.subarray(byteOffset))
1259
+ );
1260
+ Object.assign(jsonObj, remainingJsonObj);
1261
+ }
1262
+ }
1263
+ return _SpanComponentsV4.fromJsonObj(jsonObj);
1264
+ } catch {
1265
+ throw new Error(INVALID_ENCODING_ERRMSG_V4);
1266
+ }
1267
+ }
1268
+ objectIdFields() {
1269
+ if (!this.data.object_id) {
1270
+ throw new Error(
1271
+ "Impossible: cannot invoke `objectIdFields` unless SpanComponentsV4 is initialized with an `object_id`"
1272
+ );
1273
+ }
1274
+ switch (this.data.object_type) {
1275
+ case 1 /* EXPERIMENT */:
1276
+ return { experiment_id: this.data.object_id };
1277
+ case 2 /* PROJECT_LOGS */:
1278
+ return { project_id: this.data.object_id, log_id: "g" };
1279
+ case 3 /* PLAYGROUND_LOGS */:
1280
+ return { prompt_session_id: this.data.object_id, log_id: "x" };
1281
+ default:
1282
+ const _ = this.data.object_type;
1283
+ throw new Error(`Invalid object_type ${this.data.object_type}`);
1284
+ }
1285
+ }
1286
+ async export() {
1287
+ return this.toStr();
1288
+ }
1289
+ static fromJsonObj(jsonObj) {
1290
+ return new _SpanComponentsV4(spanComponentsV4Schema.parse(jsonObj));
1291
+ }
1292
+ };
1293
+ function parseParent(parent) {
1294
+ return typeof parent === "string" ? parent : parent ? new SpanComponentsV4({
1295
+ object_type: parent.object_type === "experiment" ? 1 /* EXPERIMENT */ : parent.object_type === "playground_logs" ? 3 /* PLAYGROUND_LOGS */ : 2 /* PROJECT_LOGS */,
1296
+ object_id: parent.object_id,
1297
+ ...parent.row_ids ? {
1298
+ row_id: parent.row_ids.id,
1299
+ span_id: parent.row_ids.span_id,
1300
+ root_span_id: parent.row_ids.root_span_id
1301
+ } : {
1302
+ row_id: void 0,
1303
+ span_id: void 0,
1304
+ root_span_id: void 0
1305
+ },
1306
+ propagated_event: parent.propagated_event
1307
+ }).toStr() : void 0;
1308
+ }
1309
+
1100
1310
  // util/span_types.ts
1101
1311
  var spanTypeAttributeValues = [
1102
1312
  "llm",
@@ -1157,7 +1367,7 @@ function loadPrettyXact(encodedHex) {
1157
1367
  }
1158
1368
 
1159
1369
  // util/zod_util.ts
1160
- import { z as z4 } from "zod/v3";
1370
+ import { z as z5 } from "zod/v3";
1161
1371
  var ExtraFieldsError = class extends Error {
1162
1372
  constructor(key, path) {
1163
1373
  super(
@@ -1179,7 +1389,7 @@ function parseNoStrip(schema, input) {
1179
1389
  return output;
1180
1390
  }
1181
1391
  function objectNullish(object) {
1182
- return new z4.ZodObject({
1392
+ return new z5.ZodObject({
1183
1393
  ...object._def,
1184
1394
  shape: () => Object.fromEntries(
1185
1395
  Object.entries(object.shape).map(([k, v]) => [k, v.nullish()])
@@ -1206,6 +1416,7 @@ export {
1206
1416
  SpanComponentsV1,
1207
1417
  SpanComponentsV2,
1208
1418
  SpanComponentsV3,
1419
+ SpanComponentsV4,
1209
1420
  SpanObjectTypeV1,
1210
1421
  SpanObjectTypeV2,
1211
1422
  SpanObjectTypeV3,
@@ -1254,6 +1465,7 @@ export {
1254
1465
  snakeToCamelCase,
1255
1466
  snakeToTitleCase,
1256
1467
  spanComponentsV3Schema,
1468
+ spanComponentsV4Schema,
1257
1469
  spanObjectTypeV3EnumSchema,
1258
1470
  spanObjectTypeV3ToString,
1259
1471
  spanPurposeAttributeValues,