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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braintrust",
3
- "version": "0.3.7",
3
+ "version": "0.4.0",
4
4
  "description": "SDK for integrating Braintrust",
5
5
  "repository": {
6
6
  "type": "git",
@@ -56,14 +56,16 @@
56
56
  "watch": "tsup --watch",
57
57
  "clean": "rm -r dist/* && rm -r dev/dist/*",
58
58
  "docs": "npx typedoc --options typedoc.json src/index.ts",
59
- "test": "vitest run --exclude src/wrappers/anthropic.test.ts --exclude src/wrappers/oai.test.ts --exclude src/otel.test.ts --exclude src/otel-no-deps.test.ts --exclude src/wrappers/ai-sdk-v1.test.ts --exclude src/wrappers/ai-sdk-v2.test.ts --exclude src/wrappers/ai-sdk-v3.test.ts",
59
+ "test": "vitest run --exclude src/wrappers/anthropic.test.ts --exclude src/wrappers/oai.test.ts --exclude src/otel.test.ts --exclude src/otel-no-deps.test.ts --exclude src/wrappers/ai-sdk-v1.test.ts --exclude src/wrappers/ai-sdk-v2.test.ts --exclude src/wrappers/ai-sdk-v3.test.ts --exclude src/wrappers/mastra.test.ts --exclude src/wrappers/claude-agent-sdk.test.ts",
60
60
  "test:anthropic": "vitest run src/wrappers/anthropic.test.ts",
61
61
  "test:openai": "vitest run src/wrappers/oai.test.ts",
62
62
  "test:otel": "vitest run src/otel.test.ts",
63
63
  "test:otel-no-deps": "vitest run src/otel-no-deps.test.ts --reporter=verbose",
64
64
  "test:ai-sdk-v1": "vitest run src/wrappers/ai-sdk-v1.test.ts",
65
65
  "test:ai-sdk-v2": "vitest run src/wrappers/ai-sdk-v2.test.ts src/wrappers/ai-sdk-v1.test.ts",
66
- "test:ai-sdk-v3": "vitest run src/wrappers/ai-sdk-v3.test.ts"
66
+ "test:ai-sdk-v3": "vitest run src/wrappers/ai-sdk-v3.test.ts",
67
+ "test:mastra": "vitest run src/wrappers/mastra.test.ts",
68
+ "test:claude-agent-sdk": "vitest run src/wrappers/claude-agent-sdk.test.ts"
67
69
  },
68
70
  "author": "",
69
71
  "license": "MIT",
@@ -109,7 +111,7 @@
109
111
  "cli-progress": "^3.12.0",
110
112
  "cors": "^2.8.5",
111
113
  "dotenv": "^16.4.5",
112
- "esbuild": "^0.25.9",
114
+ "esbuild": "^0.25.10",
113
115
  "eventsource-parser": "^1.1.2",
114
116
  "express": "^4.21.2",
115
117
  "graceful-fs": "^4.2.11",
@@ -5005,7 +5005,6 @@ declare class SpanComponentsV3 {
5005
5005
  export(): Promise<string>;
5006
5006
  private static fromJsonObj;
5007
5007
  }
5008
- declare function parseParent(parent: InvokeFunctionType["parent"]): string | undefined;
5009
5008
 
5010
5009
  declare const BT_FOUND_EXISTING_HEADER = "x-bt-found-existing";
5011
5010
  declare const BT_CURSOR_HEADER = "x-bt-cursor";
@@ -5194,6 +5193,70 @@ declare class SpanComponentsV2 {
5194
5193
  };
5195
5194
  }
5196
5195
 
5196
+ declare const spanComponentsV4Schema: z.ZodIntersection<z.ZodIntersection<z.ZodObject<{
5197
+ object_type: z.ZodNativeEnum<typeof SpanObjectTypeV3>;
5198
+ propagated_event: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
5199
+ }, "strip", z.ZodTypeAny, {
5200
+ object_type: SpanObjectTypeV3;
5201
+ propagated_event?: Record<string, unknown> | null | undefined;
5202
+ }, {
5203
+ object_type: SpanObjectTypeV3;
5204
+ propagated_event?: Record<string, unknown> | null | undefined;
5205
+ }>, z.ZodUnion<[z.ZodObject<{
5206
+ object_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
5207
+ compute_object_metadata_args: z.ZodOptional<z.ZodNull>;
5208
+ }, "strip", z.ZodTypeAny, {
5209
+ object_id?: string | null | undefined;
5210
+ compute_object_metadata_args?: null | undefined;
5211
+ }, {
5212
+ object_id?: string | null | undefined;
5213
+ compute_object_metadata_args?: null | undefined;
5214
+ }>, z.ZodObject<{
5215
+ object_id: z.ZodOptional<z.ZodNull>;
5216
+ compute_object_metadata_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
5217
+ }, "strip", z.ZodTypeAny, {
5218
+ compute_object_metadata_args: Record<string, unknown>;
5219
+ object_id?: null | undefined;
5220
+ }, {
5221
+ compute_object_metadata_args: Record<string, unknown>;
5222
+ object_id?: null | undefined;
5223
+ }>]>>, z.ZodUnion<[z.ZodObject<{
5224
+ row_id: z.ZodString;
5225
+ span_id: z.ZodString;
5226
+ root_span_id: z.ZodString;
5227
+ }, "strip", z.ZodTypeAny, {
5228
+ span_id: string;
5229
+ root_span_id: string;
5230
+ row_id: string;
5231
+ }, {
5232
+ span_id: string;
5233
+ root_span_id: string;
5234
+ row_id: string;
5235
+ }>, z.ZodObject<{
5236
+ row_id: z.ZodOptional<z.ZodNull>;
5237
+ span_id: z.ZodOptional<z.ZodNull>;
5238
+ root_span_id: z.ZodOptional<z.ZodNull>;
5239
+ }, "strip", z.ZodTypeAny, {
5240
+ span_id?: null | undefined;
5241
+ root_span_id?: null | undefined;
5242
+ row_id?: null | undefined;
5243
+ }, {
5244
+ span_id?: null | undefined;
5245
+ root_span_id?: null | undefined;
5246
+ row_id?: null | undefined;
5247
+ }>]>>;
5248
+ type SpanComponentsV4Data = z.infer<typeof spanComponentsV4Schema>;
5249
+ declare class SpanComponentsV4 {
5250
+ data: SpanComponentsV4Data;
5251
+ constructor(data: SpanComponentsV4Data);
5252
+ toStr(): string;
5253
+ static fromStr(s: string): SpanComponentsV4;
5254
+ objectIdFields(): ParentExperimentIds | ParentProjectLogIds | ParentPlaygroundLogIds;
5255
+ export(): Promise<string>;
5256
+ private static fromJsonObj;
5257
+ }
5258
+ declare function parseParent(parent: InvokeFunctionType["parent"]): string | undefined;
5259
+
5197
5260
  declare const spanTypeAttributeValues: readonly ["llm", "score", "function", "eval", "task", "tool"];
5198
5261
  declare enum SpanTypeAttribute {
5199
5262
  LLM = "llm",
@@ -5226,4 +5289,4 @@ declare function base64ToUint8Array(base64: string): Uint8Array;
5226
5289
  declare function uint8ArrayToString(uint8Array: Uint8Array): string;
5227
5290
  declare function stringToUint8Array(str: string): Uint8Array;
5228
5291
 
5229
- export { ASYNC_SCORING_CONTROL_FIELD, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, BT_CURSOR_HEADER, BT_FOUND_EXISTING_HEADER, BT_IMPERSONATE_USER, BT_PARENT, type BackgroundLogEvent, CREATED_FIELD, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, ExtraFieldsError, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentPlaygroundLogIds, type ParentProjectLogIds, type PlaygroundLogEvent, SKIP_ASYNC_SCORING_FIELD, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanComponentsV1, SpanComponentsV2, SpanComponentsV3, type SpanComponentsV3Data, SpanObjectTypeV1, SpanObjectTypeV2, SpanObjectTypeV3, type SpanPurpose, SpanRowIdsV1, SpanRowIdsV2, type SpanType, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, _urljoin, base64ToUint8Array, batchItems, camelToSnakeCase, capitalize, concatUint8Arrays, constructJsonArray, deterministicReplacer, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, forEachMissingKey, getObjValueByPath, getRecordKeys, isArray, isEmpty, isNumber, isObject, isObjectOrArray, loadPrettyXact, lowercase, mapAt, mapSetDefault, mapSetNotPresent, mergeDicts, mergeDictsWithPaths, mergeGitMetadataSettings, mergeRowBatch, notEmpty, objectNullish, parseNoStrip, parseParent, prettifyXact, recordAt, recordFind, recordSetDefault, resolveParentHeader, snakeToCamelCase, snakeToTitleCase, spanComponentsV3Schema, spanObjectTypeV3EnumSchema, spanObjectTypeV3ToString, spanPurposeAttributeValues, spanTypeAttributeValues, stringToUint8Array, uint8ArrayToBase64, uint8ArrayToString };
5292
+ export { ASYNC_SCORING_CONTROL_FIELD, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, BT_CURSOR_HEADER, BT_FOUND_EXISTING_HEADER, BT_IMPERSONATE_USER, BT_PARENT, type BackgroundLogEvent, CREATED_FIELD, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, ExtraFieldsError, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentPlaygroundLogIds, type ParentProjectLogIds, type PlaygroundLogEvent, SKIP_ASYNC_SCORING_FIELD, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanComponentsV1, SpanComponentsV2, SpanComponentsV3, type SpanComponentsV3Data, SpanComponentsV4, type SpanComponentsV4Data, SpanObjectTypeV1, SpanObjectTypeV2, SpanObjectTypeV3, type SpanPurpose, SpanRowIdsV1, SpanRowIdsV2, type SpanType, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, _urljoin, base64ToUint8Array, batchItems, camelToSnakeCase, capitalize, concatUint8Arrays, constructJsonArray, deterministicReplacer, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, forEachMissingKey, getObjValueByPath, getRecordKeys, isArray, isEmpty, isNumber, isObject, isObjectOrArray, loadPrettyXact, lowercase, mapAt, mapSetDefault, mapSetNotPresent, mergeDicts, mergeDictsWithPaths, mergeGitMetadataSettings, mergeRowBatch, notEmpty, objectNullish, parseNoStrip, parseParent, prettifyXact, recordAt, recordFind, recordSetDefault, resolveParentHeader, snakeToCamelCase, snakeToTitleCase, spanComponentsV3Schema, spanComponentsV4Schema, spanObjectTypeV3EnumSchema, spanObjectTypeV3ToString, spanPurposeAttributeValues, spanTypeAttributeValues, stringToUint8Array, uint8ArrayToBase64, uint8ArrayToString };
@@ -5005,7 +5005,6 @@ declare class SpanComponentsV3 {
5005
5005
  export(): Promise<string>;
5006
5006
  private static fromJsonObj;
5007
5007
  }
5008
- declare function parseParent(parent: InvokeFunctionType["parent"]): string | undefined;
5009
5008
 
5010
5009
  declare const BT_FOUND_EXISTING_HEADER = "x-bt-found-existing";
5011
5010
  declare const BT_CURSOR_HEADER = "x-bt-cursor";
@@ -5194,6 +5193,70 @@ declare class SpanComponentsV2 {
5194
5193
  };
5195
5194
  }
5196
5195
 
5196
+ declare const spanComponentsV4Schema: z.ZodIntersection<z.ZodIntersection<z.ZodObject<{
5197
+ object_type: z.ZodNativeEnum<typeof SpanObjectTypeV3>;
5198
+ propagated_event: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
5199
+ }, "strip", z.ZodTypeAny, {
5200
+ object_type: SpanObjectTypeV3;
5201
+ propagated_event?: Record<string, unknown> | null | undefined;
5202
+ }, {
5203
+ object_type: SpanObjectTypeV3;
5204
+ propagated_event?: Record<string, unknown> | null | undefined;
5205
+ }>, z.ZodUnion<[z.ZodObject<{
5206
+ object_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
5207
+ compute_object_metadata_args: z.ZodOptional<z.ZodNull>;
5208
+ }, "strip", z.ZodTypeAny, {
5209
+ object_id?: string | null | undefined;
5210
+ compute_object_metadata_args?: null | undefined;
5211
+ }, {
5212
+ object_id?: string | null | undefined;
5213
+ compute_object_metadata_args?: null | undefined;
5214
+ }>, z.ZodObject<{
5215
+ object_id: z.ZodOptional<z.ZodNull>;
5216
+ compute_object_metadata_args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
5217
+ }, "strip", z.ZodTypeAny, {
5218
+ compute_object_metadata_args: Record<string, unknown>;
5219
+ object_id?: null | undefined;
5220
+ }, {
5221
+ compute_object_metadata_args: Record<string, unknown>;
5222
+ object_id?: null | undefined;
5223
+ }>]>>, z.ZodUnion<[z.ZodObject<{
5224
+ row_id: z.ZodString;
5225
+ span_id: z.ZodString;
5226
+ root_span_id: z.ZodString;
5227
+ }, "strip", z.ZodTypeAny, {
5228
+ span_id: string;
5229
+ root_span_id: string;
5230
+ row_id: string;
5231
+ }, {
5232
+ span_id: string;
5233
+ root_span_id: string;
5234
+ row_id: string;
5235
+ }>, z.ZodObject<{
5236
+ row_id: z.ZodOptional<z.ZodNull>;
5237
+ span_id: z.ZodOptional<z.ZodNull>;
5238
+ root_span_id: z.ZodOptional<z.ZodNull>;
5239
+ }, "strip", z.ZodTypeAny, {
5240
+ span_id?: null | undefined;
5241
+ root_span_id?: null | undefined;
5242
+ row_id?: null | undefined;
5243
+ }, {
5244
+ span_id?: null | undefined;
5245
+ root_span_id?: null | undefined;
5246
+ row_id?: null | undefined;
5247
+ }>]>>;
5248
+ type SpanComponentsV4Data = z.infer<typeof spanComponentsV4Schema>;
5249
+ declare class SpanComponentsV4 {
5250
+ data: SpanComponentsV4Data;
5251
+ constructor(data: SpanComponentsV4Data);
5252
+ toStr(): string;
5253
+ static fromStr(s: string): SpanComponentsV4;
5254
+ objectIdFields(): ParentExperimentIds | ParentProjectLogIds | ParentPlaygroundLogIds;
5255
+ export(): Promise<string>;
5256
+ private static fromJsonObj;
5257
+ }
5258
+ declare function parseParent(parent: InvokeFunctionType["parent"]): string | undefined;
5259
+
5197
5260
  declare const spanTypeAttributeValues: readonly ["llm", "score", "function", "eval", "task", "tool"];
5198
5261
  declare enum SpanTypeAttribute {
5199
5262
  LLM = "llm",
@@ -5226,4 +5289,4 @@ declare function base64ToUint8Array(base64: string): Uint8Array;
5226
5289
  declare function uint8ArrayToString(uint8Array: Uint8Array): string;
5227
5290
  declare function stringToUint8Array(str: string): Uint8Array;
5228
5291
 
5229
- export { ASYNC_SCORING_CONTROL_FIELD, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, BT_CURSOR_HEADER, BT_FOUND_EXISTING_HEADER, BT_IMPERSONATE_USER, BT_PARENT, type BackgroundLogEvent, CREATED_FIELD, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, ExtraFieldsError, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentPlaygroundLogIds, type ParentProjectLogIds, type PlaygroundLogEvent, SKIP_ASYNC_SCORING_FIELD, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanComponentsV1, SpanComponentsV2, SpanComponentsV3, type SpanComponentsV3Data, SpanObjectTypeV1, SpanObjectTypeV2, SpanObjectTypeV3, type SpanPurpose, SpanRowIdsV1, SpanRowIdsV2, type SpanType, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, _urljoin, base64ToUint8Array, batchItems, camelToSnakeCase, capitalize, concatUint8Arrays, constructJsonArray, deterministicReplacer, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, forEachMissingKey, getObjValueByPath, getRecordKeys, isArray, isEmpty, isNumber, isObject, isObjectOrArray, loadPrettyXact, lowercase, mapAt, mapSetDefault, mapSetNotPresent, mergeDicts, mergeDictsWithPaths, mergeGitMetadataSettings, mergeRowBatch, notEmpty, objectNullish, parseNoStrip, parseParent, prettifyXact, recordAt, recordFind, recordSetDefault, resolveParentHeader, snakeToCamelCase, snakeToTitleCase, spanComponentsV3Schema, spanObjectTypeV3EnumSchema, spanObjectTypeV3ToString, spanPurposeAttributeValues, spanTypeAttributeValues, stringToUint8Array, uint8ArrayToBase64, uint8ArrayToString };
5292
+ export { ASYNC_SCORING_CONTROL_FIELD, AUDIT_METADATA_FIELD, AUDIT_SOURCE_FIELD, type AnyDatasetRecord, BT_CURSOR_HEADER, BT_FOUND_EXISTING_HEADER, BT_IMPERSONATE_USER, BT_PARENT, type BackgroundLogEvent, CREATED_FIELD, type CommentEvent, DEFAULT_IS_LEGACY_DATASET, type DatasetEvent, type DatasetRecord, type ExperimentEvent, type ExperimentLogFullArgs, type ExperimentLogPartialArgs, ExtraFieldsError, ID_FIELD, IS_MERGE_FIELD, type IdField, type InputField, type LogCommentFullArgs, type LogFeedbackFullArgs, type LoggingEvent, MERGE_PATHS_FIELD, OBJECT_DELETE_FIELD, type OtherExperimentLogFields, PARENT_ID_FIELD, type ParentExperimentIds, type ParentPlaygroundLogIds, type ParentProjectLogIds, type PlaygroundLogEvent, SKIP_ASYNC_SCORING_FIELD, type SanitizedExperimentLogPartialArgs, type Score, type Scorer, type ScorerArgs, type Source, SpanComponentsV1, SpanComponentsV2, SpanComponentsV3, type SpanComponentsV3Data, SpanComponentsV4, type SpanComponentsV4Data, SpanObjectTypeV1, SpanObjectTypeV2, SpanObjectTypeV3, type SpanPurpose, SpanRowIdsV1, SpanRowIdsV2, type SpanType, SpanTypeAttribute, TRANSACTION_ID_FIELD, type TransactionId, VALID_SOURCES, _urljoin, base64ToUint8Array, batchItems, camelToSnakeCase, capitalize, concatUint8Arrays, constructJsonArray, deterministicReplacer, ensureDatasetRecord, ensureLegacyDatasetRecord, ensureNewDatasetRecord, forEachMissingKey, getObjValueByPath, getRecordKeys, isArray, isEmpty, isNumber, isObject, isObjectOrArray, loadPrettyXact, lowercase, mapAt, mapSetDefault, mapSetNotPresent, mergeDicts, mergeDictsWithPaths, mergeGitMetadataSettings, mergeRowBatch, notEmpty, objectNullish, parseNoStrip, parseParent, prettifyXact, recordAt, recordFind, recordSetDefault, resolveParentHeader, snakeToCamelCase, snakeToTitleCase, spanComponentsV3Schema, spanComponentsV4Schema, spanObjectTypeV3EnumSchema, spanObjectTypeV3ToString, spanPurposeAttributeValues, spanTypeAttributeValues, stringToUint8Array, uint8ArrayToBase64, uint8ArrayToString };
@@ -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
+
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 (e4) {
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 (e5) {
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 = _v3.z.object({
1127
+ object_type: spanObjectTypeV3EnumSchema,
1128
+ propagated_event: _v3.z.record(_v3.z.unknown()).nullish()
1129
+ }).and(
1130
+ _v3.z.union([
1131
+ // Must provide one or the other.
1132
+ _v3.z.object({
1133
+ object_id: _v3.z.string().nullish(),
1134
+ compute_object_metadata_args: _v3.z.optional(_v3.z.null())
1135
+ }),
1136
+ _v3.z.object({
1137
+ object_id: _v3.z.optional(_v3.z.null()),
1138
+ compute_object_metadata_args: _v3.z.record(_v3.z.unknown())
1139
+ })
1140
+ ])
1141
+ ).and(
1142
+ _v3.z.union([
1143
+ // Either all of these must be provided or none.
1144
+ _v3.z.object({
1145
+ row_id: _v3.z.string(),
1146
+ span_id: _v3.z.string(),
1147
+ root_span_id: _v3.z.string()
1148
+ }),
1149
+ _v3.z.object({
1150
+ row_id: _v3.z.optional(_v3.z.null()),
1151
+ span_id: _v3.z.optional(_v3.z.null()),
1152
+ root_span_id: _v3.z.optional(_v3.z.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 (e6) {
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",
@@ -1261,4 +1471,6 @@ function objectNullish(object) {
1261
1471
 
1262
1472
 
1263
1473
 
1264
- exports.ASYNC_SCORING_CONTROL_FIELD = ASYNC_SCORING_CONTROL_FIELD; exports.AUDIT_METADATA_FIELD = AUDIT_METADATA_FIELD; exports.AUDIT_SOURCE_FIELD = AUDIT_SOURCE_FIELD; exports.BT_CURSOR_HEADER = BT_CURSOR_HEADER; exports.BT_FOUND_EXISTING_HEADER = BT_FOUND_EXISTING_HEADER; exports.BT_IMPERSONATE_USER = BT_IMPERSONATE_USER; exports.BT_PARENT = BT_PARENT; exports.CREATED_FIELD = CREATED_FIELD; exports.DEFAULT_IS_LEGACY_DATASET = DEFAULT_IS_LEGACY_DATASET; exports.ExtraFieldsError = ExtraFieldsError; exports.ID_FIELD = ID_FIELD; exports.IS_MERGE_FIELD = IS_MERGE_FIELD; exports.MERGE_PATHS_FIELD = MERGE_PATHS_FIELD; exports.OBJECT_DELETE_FIELD = OBJECT_DELETE_FIELD; exports.PARENT_ID_FIELD = PARENT_ID_FIELD; exports.SKIP_ASYNC_SCORING_FIELD = SKIP_ASYNC_SCORING_FIELD; exports.SpanComponentsV1 = SpanComponentsV1; exports.SpanComponentsV2 = SpanComponentsV2; exports.SpanComponentsV3 = SpanComponentsV3; exports.SpanObjectTypeV1 = SpanObjectTypeV1; exports.SpanObjectTypeV2 = SpanObjectTypeV2; exports.SpanObjectTypeV3 = SpanObjectTypeV3; exports.SpanRowIdsV1 = SpanRowIdsV1; exports.SpanRowIdsV2 = SpanRowIdsV2; exports.SpanTypeAttribute = SpanTypeAttribute; exports.TRANSACTION_ID_FIELD = TRANSACTION_ID_FIELD; exports.VALID_SOURCES = VALID_SOURCES; exports._urljoin = _urljoin; exports.base64ToUint8Array = base64ToUint8Array; exports.batchItems = batchItems; exports.camelToSnakeCase = camelToSnakeCase; exports.capitalize = capitalize; exports.concatUint8Arrays = concatUint8Arrays; exports.constructJsonArray = constructJsonArray; exports.deterministicReplacer = deterministicReplacer; exports.ensureDatasetRecord = ensureDatasetRecord; exports.ensureLegacyDatasetRecord = ensureLegacyDatasetRecord; exports.ensureNewDatasetRecord = ensureNewDatasetRecord; exports.forEachMissingKey = forEachMissingKey; exports.getObjValueByPath = getObjValueByPath; exports.getRecordKeys = getRecordKeys; exports.isArray = isArray; exports.isEmpty = isEmpty; exports.isNumber = isNumber; exports.isObject = isObject; exports.isObjectOrArray = isObjectOrArray; exports.loadPrettyXact = loadPrettyXact; exports.lowercase = lowercase; exports.mapAt = mapAt; exports.mapSetDefault = mapSetDefault; exports.mapSetNotPresent = mapSetNotPresent; exports.mergeDicts = mergeDicts; exports.mergeDictsWithPaths = mergeDictsWithPaths; exports.mergeGitMetadataSettings = mergeGitMetadataSettings; exports.mergeRowBatch = mergeRowBatch; exports.notEmpty = notEmpty; exports.objectNullish = objectNullish; exports.parseNoStrip = parseNoStrip; exports.parseParent = parseParent; exports.prettifyXact = prettifyXact; exports.recordAt = recordAt; exports.recordFind = recordFind; exports.recordSetDefault = recordSetDefault; exports.resolveParentHeader = resolveParentHeader; exports.snakeToCamelCase = snakeToCamelCase; exports.snakeToTitleCase = snakeToTitleCase; exports.spanComponentsV3Schema = spanComponentsV3Schema; exports.spanObjectTypeV3EnumSchema = spanObjectTypeV3EnumSchema; exports.spanObjectTypeV3ToString = spanObjectTypeV3ToString; exports.spanPurposeAttributeValues = spanPurposeAttributeValues; exports.spanTypeAttributeValues = spanTypeAttributeValues; exports.stringToUint8Array = stringToUint8Array; exports.uint8ArrayToBase64 = uint8ArrayToBase64; exports.uint8ArrayToString = uint8ArrayToString;
1474
+
1475
+
1476
+ exports.ASYNC_SCORING_CONTROL_FIELD = ASYNC_SCORING_CONTROL_FIELD; exports.AUDIT_METADATA_FIELD = AUDIT_METADATA_FIELD; exports.AUDIT_SOURCE_FIELD = AUDIT_SOURCE_FIELD; exports.BT_CURSOR_HEADER = BT_CURSOR_HEADER; exports.BT_FOUND_EXISTING_HEADER = BT_FOUND_EXISTING_HEADER; exports.BT_IMPERSONATE_USER = BT_IMPERSONATE_USER; exports.BT_PARENT = BT_PARENT; exports.CREATED_FIELD = CREATED_FIELD; exports.DEFAULT_IS_LEGACY_DATASET = DEFAULT_IS_LEGACY_DATASET; exports.ExtraFieldsError = ExtraFieldsError; exports.ID_FIELD = ID_FIELD; exports.IS_MERGE_FIELD = IS_MERGE_FIELD; exports.MERGE_PATHS_FIELD = MERGE_PATHS_FIELD; exports.OBJECT_DELETE_FIELD = OBJECT_DELETE_FIELD; exports.PARENT_ID_FIELD = PARENT_ID_FIELD; exports.SKIP_ASYNC_SCORING_FIELD = SKIP_ASYNC_SCORING_FIELD; exports.SpanComponentsV1 = SpanComponentsV1; exports.SpanComponentsV2 = SpanComponentsV2; exports.SpanComponentsV3 = SpanComponentsV3; exports.SpanComponentsV4 = SpanComponentsV4; exports.SpanObjectTypeV1 = SpanObjectTypeV1; exports.SpanObjectTypeV2 = SpanObjectTypeV2; exports.SpanObjectTypeV3 = SpanObjectTypeV3; exports.SpanRowIdsV1 = SpanRowIdsV1; exports.SpanRowIdsV2 = SpanRowIdsV2; exports.SpanTypeAttribute = SpanTypeAttribute; exports.TRANSACTION_ID_FIELD = TRANSACTION_ID_FIELD; exports.VALID_SOURCES = VALID_SOURCES; exports._urljoin = _urljoin; exports.base64ToUint8Array = base64ToUint8Array; exports.batchItems = batchItems; exports.camelToSnakeCase = camelToSnakeCase; exports.capitalize = capitalize; exports.concatUint8Arrays = concatUint8Arrays; exports.constructJsonArray = constructJsonArray; exports.deterministicReplacer = deterministicReplacer; exports.ensureDatasetRecord = ensureDatasetRecord; exports.ensureLegacyDatasetRecord = ensureLegacyDatasetRecord; exports.ensureNewDatasetRecord = ensureNewDatasetRecord; exports.forEachMissingKey = forEachMissingKey; exports.getObjValueByPath = getObjValueByPath; exports.getRecordKeys = getRecordKeys; exports.isArray = isArray; exports.isEmpty = isEmpty; exports.isNumber = isNumber; exports.isObject = isObject; exports.isObjectOrArray = isObjectOrArray; exports.loadPrettyXact = loadPrettyXact; exports.lowercase = lowercase; exports.mapAt = mapAt; exports.mapSetDefault = mapSetDefault; exports.mapSetNotPresent = mapSetNotPresent; exports.mergeDicts = mergeDicts; exports.mergeDictsWithPaths = mergeDictsWithPaths; exports.mergeGitMetadataSettings = mergeGitMetadataSettings; exports.mergeRowBatch = mergeRowBatch; exports.notEmpty = notEmpty; exports.objectNullish = objectNullish; exports.parseNoStrip = parseNoStrip; exports.parseParent = parseParent; exports.prettifyXact = prettifyXact; exports.recordAt = recordAt; exports.recordFind = recordFind; exports.recordSetDefault = recordSetDefault; exports.resolveParentHeader = resolveParentHeader; exports.snakeToCamelCase = snakeToCamelCase; exports.snakeToTitleCase = snakeToTitleCase; exports.spanComponentsV3Schema = spanComponentsV3Schema; exports.spanComponentsV4Schema = spanComponentsV4Schema; exports.spanObjectTypeV3EnumSchema = spanObjectTypeV3EnumSchema; exports.spanObjectTypeV3ToString = spanObjectTypeV3ToString; exports.spanPurposeAttributeValues = spanPurposeAttributeValues; exports.spanTypeAttributeValues = spanTypeAttributeValues; exports.stringToUint8Array = stringToUint8Array; exports.uint8ArrayToBase64 = uint8ArrayToBase64; exports.uint8ArrayToString = uint8ArrayToString;